title: LLDB Python access of iOS variables?
body: <p>As part of debugging a problem that might be related to my UIVIews, I want to write a python script to run from LLDB. I had thought to extract all settings for a view in a breakpoint and all view children, to allow me to compare states. I checked out the WWDC video on the topic and then spent time reading things at lldb.llvm.org/scripting.html, and didn't find them very helpful. A web search for examples led to nothing substantially different from those.</p>
  
  <p>My problem is that I'm trying to figure out how to access iOS variables at my breakpoint. The examples I've seen do things like convert numbers and mimic shell commands. Interesting stuff but not useful for my purposes. I've been reading my way through the help info with "script help(lldb.SBValue)" and the like, but it is slow going as the results are huge and it is not clear what the use patterns are. I feel like one decent example of how to traverse a few iOS objects would help me understand the system. Does anyone know of one or can share a snippet of code?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I wrote this to help me track down a bug in my UIView use. I want to do a bit more work to refine this to see if I could show the whole view tree, but this was sufficient to solve my problem, so I'll put it here to save others some time.</p>
  
  <pre><code>import lldb
  max_depth = 6
  filters = {'_view':'UIView *', '_layer':'CALayer *', '_viewFlags':'struct'}
  
  def print_value(var, depth, prefix):
      """ print values and recurse """
      global max_depth
      local_depth = max_depth - depth
      pad = ' ' * local_depth
      name = var.GetName()
      typ = str(var.GetType()).split('\n')[0].split('{')[0].split(':')[0].strip()
  
      found = name in filters.keys() # only visit filter items children
      if found:
          found = (filters.get(name) == typ)
  
      value = var.GetValue()
      if value is None or str(value) == '0x00000000':
          value = ''
      else:
          value = ' Val: %s' % value
  
      if var.GetNumChildren() == 0 and var.IsInScope():
          path = lldb.SBStream()
          var.GetExpressionPath(path)
          path = ' pathData: %s' % path.GetData()
      else:
          path = ''
  
      print '^' * local_depth, prefix, ' Adr:', var.GetAddress(), ' Name:', name, ' Type:', typ, value, path
  
      if var.GetNumChildren() &gt; 0:
          if local_depth &lt; 2 or found:
              print pad, var.GetNumChildren(), 'children, to depth', local_depth + 1
              counter = 0
              for subvar in var:
                  subprefix = '%d/%d' % (counter, var.GetNumChildren())
                  print_value(subvar, depth - 1, subprefix)
                  counter += 1
  
  def printvh (debugger, command_line, result, dict):
      """ print view hierarchy """
      global max_depth
      args = command_line.split()
      if len(args) &gt; 0:
          var = lldb.frame.FindVariable(args[0])
          depth = max_depth
          if len(args) &gt; 1:
              depth = int(args[1])
              max_depth = depth
          print_value(var, depth, 'ROOT')
      else:
          print 'pass a variable name and optional depth'
  </code></pre>
  
  <p>And I added the following to my .lldbinit :</p>
  
  <pre><code>script import os, sys
  # So that files in my dir takes precedence.
  script sys.path[:0] = [os.path.expanduser("~/lldbpy")]
  script import views
  command script add -f views.printvh printvh
  </code></pre>
  
  <p>so that I can just type "printvh self 3" at the LLDB prompt.</p>