Speculatively revert commit 121905 since it looks like it might have broken the
[oota-llvm.git] / utils / CollectDebugInfoUsingLLDB.py
1 #!/usr/bin/python
2
3 #----------------------------------------------------------------------
4
5 # Be sure to add the python path that points to the LLDB shared library.
6 # On MacOSX csh, tcsh:
7 #   setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
8 # On MacOSX sh, bash:
9 #   export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
10 #
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
13 # optimized builds.
14 #
15 # Usage:
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 #----------------------------------------------------------------------
23
24 import lldb
25 import os
26 import sys
27 import time
28
29 # AlreadyPrintedValues - A place to keep track of recursive values.
30 AlreadyPrintedValues = {}
31
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
36                 return False
37         return True
38
39
40 # print_var_value - Print a variable's value.
41 def print_var_value (v, file, frame):
42         if v.IsValid() == False:
43                 return
44         if IsAlreadyPrinted(v.GetName()):
45                 return
46         total_children = v.GetNumChildren()
47         if total_children > 0:
48             c = 0
49             while (c < total_children) :
50                     child = v.GetChildAtIndex(c)
51                     if child is None:
52                         file.write("None")
53                     else:
54                         if (child.GetName()) is None:
55                                 file.write("None")
56                         else:
57                                 file.write(child.GetName())
58                                 file.write('=')
59                                 print_var_value(child, file, frame)
60                                 file.write(',')
61                     c = c + 1
62         else:
63             if v.GetValue(frame) is None:
64                 file.write("None")
65             else:
66                 file.write(v.GetValue(frame))
67
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()
72     bid = 0
73     tid = 0
74     for i in range(count):
75         id = thread.GetStopReasonDataAtIndex(i)
76         bp = target.FindBreakpointByID(id)
77         if bp.IsValid():
78             if bp.IsEnabled() == True:
79                     bid = bp.GetID()
80                     tid = bp.GetThreadID()
81                     bp.SetEnabled(False)
82         else:
83             bp_loc = bp.FindLocationByID(thread.GetStopReasonDataAtIndex(i+1))
84             if bp_loc.IsValid():
85                 bid = bp_loc.GetBreakPoint().GetID()
86                 tid = bp_loc.ThreadGetID()
87                 bp_loc.SetEnabled(False);
88
89     for i in range(vars.GetSize()):
90         file.write(tag)
91         file.write(fname)
92         file.write(':')
93         file.write(str(line))
94         file.write(' ')
95         file.write(str(tid))
96         file.write(':')
97         file.write(str(bid))
98         file.write(' ')
99         v = vars.GetValueAtIndex(i)
100         file.write(v.GetName())
101         file.write(' ')
102         AlreadyPrintedValues.clear()
103         print_var_value (v, file, frame)
104         file.write('\n')
105
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)):
111         c = lines[l].split()
112         # print "setting break point - ", c
113         bp = target.BreakpointCreateByLocation (str(c[0]), int(c[1]))
114     f.close()
115
116 # stopeed_at_breakpoint - Return True if process is stopeed at a
117 # breakpoint.
118 def stopped_at_breakpoint (process):
119     if process.IsValid():
120         state = process.GetState()
121         if state == lldb.eStateStopped:
122                 thread = process.GetThreadAtIndex(0)
123                 if thread.IsValid():
124                         if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
125                                 return True
126     return False
127
128 # Create a new debugger instance
129 debugger = lldb.SBDebugger.Create()
130
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)
134
135 # Create a target from a file and arch
136 ##print "Creating a target for '%s'" % sys.argv[1]
137
138 target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
139
140 if target.IsValid():
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)
145
146     ##print main_bp
147
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)
156         if frame.IsValid():
157             # #Print some simple frame info
158             ##print frame
159             #print "frame is valid"
160             function = frame.GetFunction()
161             if function.IsValid():
162                 fname = function.GetMangledName()
163                 if fname is None:
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)
171
172         process.Continue()
173     file.close()
174
175 lldb.SBDebugger.Terminate()