Don't keep the log files around. Just pipe to a log file instead.
[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             v = vars.GetValueAtIndex(i)
91             if v.GetName() is not None:
92                     file.write(tag)
93                     file.write(fname)
94                     file.write(':')
95                     file.write(str(line))
96                     file.write(' ')
97                     file.write(str(tid))
98                     file.write(':')
99                     file.write(str(bid))
100                     file.write(' ')
101                     file.write(v.GetName())
102                     file.write(' ')
103                     AlreadyPrintedValues.clear()
104                     print_var_value (v, file, frame)
105                     file.write('\n')
106
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)):
112         c = lines[l].split()
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]))
117         file.write(':')
118         file.write(str(c[1]))
119         file.write(' ')
120         file.write(str(bp.GetThreadID()))
121         file.write(':')
122         file.write(str(bp.GetID()))
123         file.write('\n')
124     f.close()
125
126 # stopeed_at_breakpoint - Return True if process is stopeed at a
127 # breakpoint.
128 def stopped_at_breakpoint (process):
129     if process.IsValid():
130         state = process.GetState()
131         if state == lldb.eStateStopped:
132                 thread = process.GetThreadAtIndex(0)
133                 if thread.IsValid():
134                         if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
135                                 return True
136     return False
137
138 # Create a new debugger instance
139 debugger = lldb.SBDebugger.Create()
140
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)
144
145 # Create a target from a file and arch
146 ##print "Creating a target for '%s'" % sys.argv[1]
147
148 target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
149
150 if target.IsValid():
151     #print "target is valid"
152     file=open(str(sys.argv[3]), 'w')    
153     set_breakpoints (target, sys.argv[2], file)
154
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)
163         if frame.IsValid():
164             # #Print some simple frame info
165             ##print frame
166             #print "frame is valid"
167             function = frame.GetFunction()
168             if function.IsValid():
169                 fname = function.GetMangledName()
170                 if fname is None:
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)
178
179         process.Continue()
180     file.close()
181
182 lldb.SBDebugger.Terminate()