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()):
90 v
= vars.GetValueAtIndex(i
)
91 if v
.GetName() is not None:
101 file.write(v
.GetName())
103 AlreadyPrintedValues
.clear()
104 print_var_value (v
, file, frame
)
107 # set_breakpoints - set breakpoints as listed in input file.
108 def set_breakpoints (target
, breakpoint_filename
, file):
109 f
= open(breakpoint_filename
, "r")
110 lines
= f
.readlines()
111 for l
in range(len(lines
)):
113 # print "setting break point - ", c
114 bp
= target
.BreakpointCreateByLocation (str(c
[0]), int(c
[1]))
115 file.write("#Breakpoint ")
116 file.write(str(c
[0]))
118 file.write(str(c
[1]))
120 file.write(str(bp
.GetThreadID()))
122 file.write(str(bp
.GetID()))
126 # stopeed_at_breakpoint - Return True if process is stopeed at a
128 def stopped_at_breakpoint (process
):
129 if process
.IsValid():
130 state
= process
.GetState()
131 if state
== lldb
.eStateStopped
:
132 thread
= process
.GetThreadAtIndex(0)
134 if thread
.GetStopReason() == lldb
.eStopReasonBreakpoint
:
138 # Create a new debugger instance
139 debugger
= lldb
.SBDebugger
.Create()
141 # When we step or continue, don't return from the function until the process
142 # stops. We do this by setting the async mode to false.
143 debugger
.SetAsync (False)
145 # Create a target from a file and arch
146 ##print "Creating a target for '%s'" % sys.argv[1]
148 target
= debugger
.CreateTargetWithFileAndArch (sys
.argv
[1], lldb
.LLDB_ARCH_DEFAULT
)
151 #print "target is valid"
152 file=open(str(sys
.argv
[3]), 'w')
153 set_breakpoints (target
, sys
.argv
[2], file)
155 # Launch the process. Since we specified synchronous mode, we won't return
156 # from this function until we hit the breakpoint at main
157 sberror
= lldb
.SBError()
158 process
= target
.Launch (None, None, os
.ctermid(), os
.ctermid(), os
.ctermid(), None, 0, False, sberror
)
159 # Make sure the launch went ok
160 while stopped_at_breakpoint(process
):
161 thread
= process
.GetThreadAtIndex (0)
162 frame
= thread
.GetFrameAtIndex (0)
164 # #Print some simple frame info
166 #print "frame is valid"
167 function
= frame
.GetFunction()
168 if function
.IsValid():
169 fname
= function
.GetMangledName()
171 fname
= function
.GetName()
172 #print "function : ",fname
173 line
= frame
.GetLineEntry().GetLine()
174 vars = frame
.GetVariables(1,0,0,0)
175 print_vars ("#Argument ", vars, fname
, line
, file, frame
, target
, thread
)
176 # vars = frame.GetVariables(0,1,0,0)
177 # print_vars ("#Variables ", vars, fname, line, file, frame, target, thread)
182 lldb
.SBDebugger
.Terminate()