3 #----------------------------------------------------------------------
4 # Be sure to add the python path that points to the LLDB shared library.
6 # setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
8 # export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
9 #----------------------------------------------------------------------
16 def print_var_value (v, file, frame):
17 if v.GetNumChildren() > 0:
18 for c in range(v.GetNumChildren()):
19 if v.GetChildAtIndex(c) is None:
22 if (v.GetChildAtIndex(c).GetName()) is None:
25 file.write(v.GetChildAtIndex(c).GetName())
27 print_var_value(v.GetChildAtIndex(c), file, frame)
30 if v.GetValue(frame) is None:
33 file.write(v.GetValue(frame))
36 def print_vars (vars, fname, line, file, frame, target, thread):
37 # disable this thread.
38 count = thread.GetStopReasonDataCount()
41 for i in range(count):
42 id = thread.GetStopReasonDataAtIndex(i)
43 bp = target.FindBreakpointByID(id)
45 if bp.IsEnabled() == True:
47 tid = bp.GetThreadID()
50 bp_loc = bp.FindLocationByID(thread.GetStopReasonDataAtIndex(i+1))
52 bid = bp_loc.GetBreakPoint().GetID()
53 tid = bp_loc.ThreadGetID()
54 # print " { ", bp_loc.ThreadGetID(), " : ", bp_loc.GetBreakPoint().GetID(), " }} "
55 bp_loc.SetEnabled(False);
57 for i in range(vars.GetSize()):
58 file.write("#Argument ")
67 v = vars.GetValueAtIndex(i)
68 file.write(v.GetName())
70 print_var_value (v, file, frame)
73 def set_breakpoints (target, breakpoint_filename):
74 f = open(breakpoint_filename, "r")
76 for l in range(len(lines)):
78 # print "setting break point - ", c
79 bp = target.BreakpointCreateByLocation (str(c[0]), int(c[1]))
82 def stop_at_breakpoint (process):
84 state = process.GetState()
85 if state != lldb.eStateStopped:
86 return lldb.eStateInvalid
87 thread = process.GetThreadAtIndex(0)
89 if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
90 return lldb.eStateStopped
92 return lldb.eStateInvalid
94 return lldb.eStateInvalid
96 return lldb.eStateInvalid
98 # Create a new debugger instance
99 debugger = lldb.SBDebugger.Create()
101 # When we step or continue, don't return from the function until the process
102 # stops. We do this by setting the async mode to false.
103 debugger.SetAsync (False)
105 # Create a target from a file and arch
106 ##print "Creating a target for '%s'" % sys.argv[1]
108 target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
111 #print "target is valid"
112 set_breakpoints (target, sys.argv[2])
113 #main_bp = target.BreakpointCreateByLocation ("byval-alignment.c", 11)
114 #main_bp2 = target.BreakpointCreateByLocation ("byval-alignment.c", 20)
118 # Launch the process. Since we specified synchronous mode, we won't return
119 # from this function until we hit the breakpoint at main
120 process = target.LaunchProcess ([''], [''], "/dev/stdout", 0, False)
121 file=open(str(sys.argv[3]), 'w')
122 # Make sure the launch went ok
123 while stop_at_breakpoint(process) == lldb.eStateStopped:
124 thread = process.GetThreadAtIndex (0)
125 frame = thread.GetFrameAtIndex (0)
127 # #Print some simple frame info
129 #print "frame is valid"
130 function = frame.GetFunction()
131 if function.IsValid():
132 fname = function.GetMangledName()
134 fname = function.GetName()
135 #print "function : ",fname
136 vars = frame.GetVariables(1,0,0,0)
137 line = frame.GetLineEntry().GetLine()
138 print_vars (vars, fname, line, file, frame, target, thread)
143 lldb.SBDebugger.Terminate()