Add python scripts to extract debug info using LLDB and do comparison.
[oota-llvm.git] / utils / CollectDebugInfoUsingLLDB.py
1 #!/usr/bin/python
2
3 #----------------------------------------------------------------------
4 # Be sure to add the python path that points to the LLDB shared library.
5 # On MacOSX csh, tcsh:
6 #   setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
7 # On MacOSX sh, bash:
8 #   export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
9 #----------------------------------------------------------------------
10
11 import lldb
12 import os
13 import sys
14 import time
15
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:
20                         file.write("None")
21                 else:
22                         if (v.GetChildAtIndex(c).GetName()) is None:
23                                 file.write("None")
24                         else:
25                                 file.write(v.GetChildAtIndex(c).GetName())
26                                 file.write('=')
27                                 print_var_value(v.GetChildAtIndex(c), file, frame)
28                                 file.write(',')
29         else:
30             if v.GetValue(frame) is None:
31                 file.write("None")
32             else:
33                 file.write(v.GetValue(frame))
34
35
36 def print_vars (vars, fname, line, file, frame, target, thread):
37     # disable this thread.
38     count = thread.GetStopReasonDataCount()
39     bid = 0
40     tid = 0
41     for i in range(count):
42         id = thread.GetStopReasonDataAtIndex(i)
43         bp = target.FindBreakpointByID(id)
44         if bp.IsValid():
45             if bp.IsEnabled() == True:
46                     bid = bp.GetID()
47                     tid = bp.GetThreadID()
48                     bp.SetEnabled(False)
49         else:
50             bp_loc = bp.FindLocationByID(thread.GetStopReasonDataAtIndex(i+1))
51             if bp_loc.IsValid():
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);
56
57     for i in range(vars.GetSize()):
58         file.write("#Argument ")
59         file.write(fname)
60         file.write(':')
61         file.write(str(line))
62         file.write(' ')
63         file.write(str(tid))
64         file.write(':')
65         file.write(str(bid))
66         file.write(' ')
67         v = vars.GetValueAtIndex(i)
68         file.write(v.GetName())
69         file.write(' ')
70         print_var_value (v, file, frame)
71         file.write('\n')
72
73 def set_breakpoints (target, breakpoint_filename):
74     f = open(breakpoint_filename, "r")
75     lines = f.readlines()
76     for l in range(len(lines)):
77         c = lines[l].split()
78         # print "setting break point - ", c
79         bp = target.BreakpointCreateByLocation (str(c[0]), int(c[1]))
80     f.close()
81
82 def stop_at_breakpoint (process):
83     if process.IsValid():
84         state = process.GetState()
85         if state != lldb.eStateStopped:
86             return lldb.eStateInvalid
87         thread = process.GetThreadAtIndex(0)
88         if thread.IsValid():
89             if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
90                     return lldb.eStateStopped
91             else:
92                     return lldb.eStateInvalid
93         else:
94             return lldb.eStateInvalid
95     else:
96         return lldb.eStateInvalid
97
98 # Create a new debugger instance
99 debugger = lldb.SBDebugger.Create()
100
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)
104
105 # Create a target from a file and arch
106 ##print "Creating a target for '%s'" % sys.argv[1]
107
108 target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
109
110 if target.IsValid():
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)
115
116     ##print main_bp
117
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)
126         if frame.IsValid():
127             # #Print some simple frame info
128             ##print frame
129             #print "frame is valid"
130             function = frame.GetFunction()
131             if function.IsValid():
132                 fname = function.GetMangledName()
133                 if fname is None:
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)
139                 #print vars
140         process.Continue()
141     file.close()
142
143 lldb.SBDebugger.Terminate()