title: Writing a Python script to print out an array of recs in lldb
body: <p>I need help with the <code>SBValue class</code> used in the <code>lldb</code> Python module which is used for creating scripts for lldb debugging sessions.</p>
  
  <p>I am in the process of porting my kext test and debug system from <code>gdb</code> to <code>lldb</code> so I can start using the latest version of <code>Xcode</code> and <code>Mac OS 10.9.1</code>.  Part of this process is rewriting my gdb debug scripts in python so they can be used with lldb.</p>
  
  <p>I have the two mac setup working, can drop into lldb and poke around in the kernel of the victim Mac.  I can also my Python script to be called when I am running an lldb session.  I'm stuck though in that I am unable to figure out how to display the contents of an array of records.  </p>
  
  <p>For instance, given an array of <code>TraceRecs</code>:</p>
  
  <pre><code>typedef struct
  {
  
      mach_timespec_t timeStamp;
      thread_t thread;
      TraceRecordType recordType;
      char entry[kEntrySize];
  } TraceRec;
  </code></pre>
  
  <p>a class which holds an array of trace records</p>
  
  <pre><code>class com_softraid_TraceLog
  {
      private:
          UInt32 fMaxNumberEntries;
          UInt32 fNextEntryNumber;
          TraceRec * fTraceRecArray;
          .
          .
          .
  </code></pre>
  
  <p>and a global in the kernel which points to an object of this class:</p>
  
  <pre><code>extern com_softraid_TraceLog * com_softraid_gTraceLogPtr;
  </code></pre>
  
  <p>I can use the following Python script to get the value of com_softraid_gTraceLogPtr in lldb:</p>
  
  <pre><code>#! /usr/bin/env python
  # -*- coding: utf-8 -*-
  
  import lldb
  import commands
  import optparse
  import shlex
  
  def __lldb_init_module(debugger, internal_dict):
      debugger.HandleCommand('command script add -f sr_debug_macros.srtrace srtrace')
  
  def srtrace(debugger, user_input, result, internal_dict):
      """srtrace [number_entries]  dump out that number of entries, default = 0 = all entries"""
  
      target = debugger.GetSelectedTarget()
      traceLog = target.FindFirstGlobalVariable("com_softraid_gTraceLogPtr")
      traceRecordArray = traceLog.GetChildMemberWithName("fTraceRecArray")
      maxNumberEntries = traceLog.GetChildMemberWithName("fMaxNumberEntries").GetValueAsUnsigned()
      nextEntryNumber = traceLog.GetChildMemberWithName("fNextEntryNumber").GetValueAsUnsigned()
  
      print &gt;&gt;result, "SRLog Current Entry: %d, Log Size: %d" % (nextEntryNumber, maxNumberEntries)
  
      print &gt;&gt;result, traceRecordArray
  </code></pre>
  
  <p>and the output is:</p>
  
  <pre><code>(lldb) srtrace
  SRLog Current Entry: 388, Log Size: 8192
  (TraceRec *) fTraceRecArray = 0xffffff80a48fd000
  </code></pre>
  
  <p>but I can't figure out how to display the values in fields of any of records in the array.  I have tried most of the methods in the SBValue class without any luck.</p>
  
  <p>Does anyone understand how this is supposed to work? Any help would be great.</p>
  
  <p>P. S.: If anyone else is trying to get this to work, the first step should be to update to Xcode 5.1 b5.  The version of lldb which ships with Xcode 5.1 b3 crashes frequently when displaying the contents subclasses of IOKit classes.</p>