3 #----------------------------------------------------------------------
5 # Be sure to add the python path that points to the LLDB shared library.
7 # setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
9 # export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
11 # This script collect debugging information using LLDB. This script is
12 # used by TEST=dbg in llvm testsuite to measure quality of debug info in
16 # export PYTHONPATH=...
17 # ./CollectDebugInfUsingLLDB.py program bp_file out_file
18 # program - Executable program with debug info.
19 # bp_file - Simple text file listing breakpoints.
20 # <absolute file name> <line number>
21 # out_file - Output file where the debug info will be emitted.
22 #----------------------------------------------------------------------
29 # AlreadyPrintedValues - A place to keep track of recursive values.
30 AlreadyPrintedValues
= {}
32 # ISAlreadyPrinted - Return true if value is already printed.
33 def IsAlreadyPrinted(value_name
):
34 if AlreadyPrintedValues
.get(value_name
) is None:
35 AlreadyPrintedValues
[value_name
] = 1
40 # print_var_value - Print a variable's value.
41 def print_var_value (v
, file, frame
):
42 if v
.IsValid() == False:
44 if IsAlreadyPrinted(v
.GetName()):
46 total_children
= v
.GetNumChildren()
47 if total_children
> 0:
49 while (c
< total_children
) :
50 child
= v
.GetChildAtIndex(c
)
54 if (child
.GetName()) is None:
57 file.write(child
.GetName())
59 print_var_value(child
, file, frame
)
63 if v
.GetValue(frame
) is None:
66 file.write(v
.GetValue(frame
))
68 # print_vars - Print variable values in output file.
69 def print_vars (tag
, vars, fname
, line
, file, frame
, target
, thread
):
70 # disable this thread.
71 count
= thread
.GetStopReasonDataCount()
74 for i
in range(count
):
75 id = thread
.GetStopReasonDataAtIndex(i
)
76 bp
= target
.FindBreakpointByID(id)
78 if bp
.IsEnabled() == True:
80 tid
= bp
.GetThreadID()
83 bp_loc
= bp
.FindLocationByID(thread
.GetStopReasonDataAtIndex(i
+1))
85 bid
= bp_loc
.GetBreakPoint().GetID()
86 tid
= bp_loc
.ThreadGetID()
87 bp_loc
.SetEnabled(False);
89 for i
in range(vars.GetSize()):
99 v
= vars.GetValueAtIndex(i
)
100 file.write(v
.GetName())
102 AlreadyPrintedValues
.clear()
103 print_var_value (v
, file, frame
)
106 # set_breakpoints - set breakpoints as listed in input file.
107 def set_breakpoints (target
, breakpoint_filename
):
108 f
= open(breakpoint_filename
, "r")
109 lines
= f
.readlines()
110 for l
in range(len(lines
)):
112 # print "setting break point - ", c
113 bp
= target
.BreakpointCreateByLocation (str(c
[0]), int(c
[1]))
116 # stopeed_at_breakpoint - Return True if process is stopeed at a
118 def stopped_at_breakpoint (process
):
119 if process
.IsValid():
120 state
= process
.GetState()
121 if state
== lldb
.eStateStopped
:
122 thread
= process
.GetThreadAtIndex(0)
124 if thread
.GetStopReason() == lldb
.eStopReasonBreakpoint
:
128 # Create a new debugger instance
129 debugger
= lldb
.SBDebugger
.Create()
131 # When we step or continue, don't return from the function until the process
132 # stops. We do this by setting the async mode to false.
133 debugger
.SetAsync (False)
135 # Create a target from a file and arch
136 ##print "Creating a target for '%s'" % sys.argv[1]
138 target
= debugger
.CreateTargetWithFileAndArch (sys
.argv
[1], lldb
.LLDB_ARCH_DEFAULT
)
141 #print "target is valid"
142 set_breakpoints (target
, sys
.argv
[2])
143 #main_bp = target.BreakpointCreateByLocation ("byval-alignment.c", 11)
144 #main_bp2 = target.BreakpointCreateByLocation ("byval-alignment.c", 20)
148 # Launch the process. Since we specified synchronous mode, we won't return
149 # from this function until we hit the breakpoint at main
150 process
= target
.LaunchProcess ([''], [''], "/dev/stdout", 0, False)
151 file=open(str(sys
.argv
[3]), 'w')
152 # Make sure the launch went ok
153 while stopped_at_breakpoint(process
):
154 thread
= process
.GetThreadAtIndex (0)
155 frame
= thread
.GetFrameAtIndex (0)
157 # #Print some simple frame info
159 #print "frame is valid"
160 function
= frame
.GetFunction()
161 if function
.IsValid():
162 fname
= function
.GetMangledName()
164 fname
= function
.GetName()
165 #print "function : ",fname
166 line
= frame
.GetLineEntry().GetLine()
167 vars = frame
.GetVariables(1,0,0,0)
168 print_vars ("#Argument ", vars, fname
, line
, file, frame
, target
, thread
)
169 vars = frame
.GetVariables(0,1,0,0)
170 print_vars ("#Variables ", vars, fname
, line
, file, frame
, target
, thread
)
175 lldb
.SBDebugger
.Terminate()