forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathlldbinline.py
219 lines (177 loc) · 7.38 KB
/
lldbinline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
importlldb
fromlldbsuite.test.lldbtestimport*
importlldbsuite.test.lldbutilaslldbutil
importlldbsuite.test.test_categoriesastest_categories
# System modules
importos
importtextwrap
# Third-party modules
importio
# LLDB modules
importlldb
from .lldbtestimport*
from . importconfiguration
from . importlldbutil
from .decoratorsimport*
defsource_type(filename):
_, extension=os.path.splitext(filename)
return {
".c": "C_SOURCES",
".cpp": "CXX_SOURCES",
".cxx": "CXX_SOURCES",
".cc": "CXX_SOURCES",
".m": "OBJC_SOURCES",
".mm": "OBJCXX_SOURCES",
".swift": "SWIFT_SOURCES",
}.get(extension, None)
classCommandParser:
def__init__(self):
self.breakpoints= []
defparse_one_command(self, line):
parts=line.split("//%")
command=None
new_breakpoint=True
iflen(parts) ==2:
command=parts[1].rstrip()
new_breakpoint=parts[0].strip() !=""
return (command, new_breakpoint)
defparse_source_files(self, source_files):
forsource_fileinsource_files:
file_handle=io.open(source_file, encoding="utf-8")
lines=file_handle.readlines()
line_number=0
# non-NULL means we're looking through whitespace to find
# additional commands
current_breakpoint=None
forlineinlines:
line_number=line_number+1# 1-based, so we do this first
(command, new_breakpoint) =self.parse_one_command(line)
ifnew_breakpoint:
current_breakpoint=None
ifcommandisnotNone:
ifcurrent_breakpointisNone:
current_breakpoint= {}
current_breakpoint["file_name"] =source_file
current_breakpoint["line_number"] =line_number
current_breakpoint["command"] =command
self.breakpoints.append(current_breakpoint)
else:
current_breakpoint["command"] = (
current_breakpoint["command"] +"\n"+command
)
forbkptinself.breakpoints:
bkpt["command"] =textwrap.dedent(bkpt["command"])
defset_breakpoints(self, target):
forbreakpointinself.breakpoints:
breakpoint["breakpoint"] =target.BreakpointCreateByLocation(
breakpoint["file_name"], breakpoint["line_number"]
)
defhandle_breakpoint(self, test, breakpoint_id):
forbreakpointinself.breakpoints:
ifbreakpoint["breakpoint"].GetID() ==breakpoint_id:
test.execute_user_command(breakpoint["command"])
return
classInlineTest(TestBase):
defgetBuildDirBasename(self):
returnself.__class__.__name__+"."+self.testMethodName
defBuildMakefile(self):
makefilePath=self.getBuildArtifact("Makefile")
ifos.path.exists(makefilePath):
return
categories= {}
forfinos.listdir(self.getSourceDir()):
t=source_type(f)
ift:
iftinlist(categories.keys()):
categories[t].append(f)
else:
categories[t] = [f]
withopen(makefilePath, "w+") asmakefile:
fortinlist(categories.keys()):
line=t+" := "+" ".join(categories[t])
makefile.write(line+"\n")
if ("OBJCXX_SOURCES"inlist(categories.keys())) or (
"OBJC_SOURCES"inlist(categories.keys())
):
makefile.write("LDFLAGS = $(CFLAGS) -lobjc -framework Foundation\n")
if"CXX_SOURCES"inlist(categories.keys()):
makefile.write("CXXFLAGS += -std=c++11\n")
makefile.write("include Makefile.rules\n")
def_test(self):
self.BuildMakefile()
self.build(dictionary=self._build_dict)
self.do_test()
defexecute_user_command(self, __command):
exec(__command, globals(), locals())
def_get_breakpoint_ids(self, thread):
ids=set()
foriinrange(0, thread.GetStopReasonDataCount(), 2):
ids.add(thread.GetStopReasonDataAtIndex(i))
self.assertGreater(len(ids), 0)
returnsorted(ids)
defdo_test(self):
exe=self.getBuildArtifact("a.out")
source_files= [fforfinos.listdir(self.getSourceDir()) ifsource_type(f)]
target=self.dbg.CreateTarget(exe)
parser=CommandParser()
parser.parse_source_files(source_files)
parser.set_breakpoints(target)
process=target.LaunchSimple(None, None, self.get_process_working_directory())
self.assertIsNotNone(process, PROCESS_IS_VALID)
hit_breakpoints=0
whilelldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint):
hit_breakpoints+=1
thread=lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
forbp_idinself._get_breakpoint_ids(thread):
parser.handle_breakpoint(self, bp_id)
process.Continue()
self.assertTrue(
hit_breakpoints>0, "inline test did not hit a single breakpoint"
)
# Either the process exited or the stepping plan is complete.
self.assertTrue(
process.GetState() in [lldb.eStateStopped, lldb.eStateExited],
PROCESS_EXITED,
)
defcheck_expression(self, expression, expected_result, use_summary=True):
value=self.frame().EvaluateExpression(expression)
self.assertTrue(value.IsValid(), expression+"returned a valid value")
ifself.TraceOn():
print(value.GetSummary())
print(value.GetValue())
ifuse_summary:
answer=value.GetSummary()
else:
answer=value.GetValue()
report_str="%s expected: %s got: %s"% (expression, expected_result, answer)
self.assertTrue(answer==expected_result, report_str)
defApplyDecoratorsToFunction(func, decorators):
tmp=func
ifisinstance(decorators, list):
fordecoratorindecorators:
tmp=decorator(tmp)
elifhasattr(decorators, "__call__"):
tmp=decorators(tmp)
returntmp
defMakeInlineTest(__file, __globals, decorators=None, name=None, build_dict=None):
# Adjust the filename if it ends in .pyc. We want filenames to
# reflect the source python file, not the compiled variant.
if__fileisnotNoneand__file.endswith(".pyc"):
# Strip the trailing "c"
__file=__file[0:-1]
ifnameisNone:
# Derive the test name from the current file name
file_basename=os.path.basename(__file)
name, _=os.path.splitext(file_basename)
test_func=ApplyDecoratorsToFunction(InlineTest._test, decorators)
# Build the test case
test_class=type(
name, (InlineTest,), dict(test=test_func, name=name, _build_dict=build_dict)
)
# Add the test case to the globals, and hide InlineTest
__globals.update({name: test_class})
# Keep track of the original test filename so we report it
# correctly in test results.
test_class.test_filename=__file
test_class.mydir=TestBase.compute_mydir(__file)
returntest_class