forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathlldbplatformutil.py
397 lines (317 loc) · 12.9 KB
/
lldbplatformutil.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
""" This module contains functions used by the test cases to hide the
architecture and/or the platform dependent nature of the tests. """
# System modules
importitertools
importjson
importre
importsubprocess
importsys
importos
frompackagingimportversion
fromurllib.parseimporturlparse
# LLDB modules
importlldb
from . importconfiguration
from . importlldbtest_config
importlldbsuite.test.lldbplatformaslldbplatform
fromlldbsuite.test.buildersimportget_builder
fromlldbsuite.test.lldbutilimportis_exe
defcheck_first_register_readable(test_case):
arch=test_case.getArchitecture()
ifarchin ["x86_64", "i386"]:
test_case.expect("register read eax", substrs=["eax = 0x"])
elifarchin ["arm", "armv7", "armv7k", "armv8l", "armv7l"]:
test_case.expect("register read r0", substrs=["r0 = 0x"])
elifarchin ["aarch64", "arm64", "arm64e", "arm64_32"]:
test_case.expect("register read x0", substrs=["x0 = 0x"])
elifre.match("mips", arch):
test_case.expect("register read zero", substrs=["zero = 0x"])
elifarchin ["s390x"]:
test_case.expect("register read r0", substrs=["r0 = 0x"])
elifarchin ["powerpc64le"]:
test_case.expect("register read r0", substrs=["r0 = 0x"])
elifarchin ["riscv64", "riscv32"]:
test_case.expect("register read zero", substrs=["zero = 0x"])
else:
# TODO: Add check for other architectures
test_case.fail(
"Unsupported architecture for test case (arch: %s)"
%test_case.getArchitecture()
)
def_run_adb_command(cmd, device_id):
device_id_args= []
ifdevice_id:
device_id_args= ["-s", device_id]
full_cmd= ["adb"] +device_id_args+cmd
p=subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr=p.communicate()
returnp.returncode, stdout, stderr
deftarget_is_android():
returnconfiguration.lldb_platform_name=="remote-android"
defandroid_device_api():
ifnothasattr(android_device_api, "result"):
assertconfiguration.lldb_platform_urlisnotNone
device_id=None
parsed_url=urlparse(configuration.lldb_platform_url)
host_name=parsed_url.netloc.split(":")[0]
ifhost_name!="localhost":
device_id=host_name
ifdevice_id.startswith("[") anddevice_id.endswith("]"):
device_id=device_id[1:-1]
retcode, stdout, stderr=_run_adb_command(
["shell", "getprop", "ro.build.version.sdk"], device_id
)
ifretcode==0:
android_device_api.result=int(stdout)
else:
raiseLookupError(
">>> Unable to determine the API level of the Android device.\n"
">>> stdout:\n%s\n"
">>> stderr:\n%s\n"% (stdout, stderr)
)
returnandroid_device_api.result
defmatch_android_device(device_arch, valid_archs=None, valid_api_levels=None):
ifnottarget_is_android():
returnFalse
ifvalid_archsisnotNoneanddevice_archnotinvalid_archs:
returnFalse
ifvalid_api_levelsisnotNoneandandroid_device_api() notinvalid_api_levels:
returnFalse
returnTrue
deffinalize_build_dictionary(dictionary):
# Provide uname-like platform name
platform_name_to_uname= {
"linux": "Linux",
"netbsd": "NetBSD",
"freebsd": "FreeBSD",
"windows": "Windows_NT",
"macosx": "Darwin",
"darwin": "Darwin",
}
ifdictionaryisNone:
dictionary= {}
iftarget_is_android():
dictionary["OS"] ="Android"
dictionary["PIE"] =1
elifplatformIsDarwin():
dictionary["OS"] ="Darwin"
else:
dictionary["OS"] =platform_name_to_uname[getPlatform()]
dictionary["HOST_OS"] =platform_name_to_uname[getHostPlatform()]
returndictionary
def_get_platform_os(p):
# Use the triple to determine the platform if set.
triple=p.GetTriple()
iftriple:
platform=triple.split("-")[2]
ifplatform.startswith("freebsd"):
platform="freebsd"
elifplatform.startswith("netbsd"):
platform="netbsd"
elifplatform.startswith("openbsd"):
platform="openbsd"
returnplatform
return""
defgetHostPlatform():
"""Returns the host platform running the test suite."""
return_get_platform_os(lldb.SBPlatform("host"))
defgetDarwinOSTriples():
returnlldbplatform.translate(lldbplatform.darwin_all)
defgetPlatform():
"""Returns the target platform which the tests are running on."""
# Use the Apple SDK to determine the platform if set.
ifconfiguration.apple_sdk:
platform=configuration.apple_sdk
dot=platform.find(".")
ifdot!=-1:
platform=platform[:dot]
ifplatform=="iphoneos":
platform="ios"
returnplatform
return_get_platform_os(lldb.selected_platform)
defplatformIsDarwin():
"""Returns true if the OS triple for the selected platform is any valid apple OS"""
returngetPlatform() ingetDarwinOSTriples()
deffindMainThreadCheckerDylib():
ifnotplatformIsDarwin():
return""
ifgetPlatform() inlldbplatform.translate(lldbplatform.darwin_embedded):
return"/Developer/usr/lib/libMainThreadChecker.dylib"
withos.popen("xcode-select -p") asoutput:
xcode_developer_path=output.read().strip()
mtc_dylib_path="%s/usr/lib/libMainThreadChecker.dylib"%xcode_developer_path
ifos.path.isfile(mtc_dylib_path):
returnmtc_dylib_path
return""
deffindBacktraceRecordingDylib():
ifnotplatformIsDarwin():
return""
ifgetPlatform() inlldbplatform.translate(lldbplatform.darwin_embedded):
return"/Developer/usr/lib/libBacktraceRecording.dylib"
withos.popen("xcode-select -p") asoutput:
xcode_developer_path=output.read().strip()
mtc_dylib_path="%s/usr/lib/libBacktraceRecording.dylib"%xcode_developer_path
ifos.path.isfile(mtc_dylib_path):
returnmtc_dylib_path
return""
class_PlatformContext(object):
"""Value object class which contains platform-specific options."""
def__init__(
self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension
):
self.shlib_environment_var=shlib_environment_var
self.shlib_path_separator=shlib_path_separator
self.shlib_prefix=shlib_prefix
self.shlib_extension=shlib_extension
defcreatePlatformContext():
ifplatformIsDarwin():
return_PlatformContext("DYLD_LIBRARY_PATH", ":", "lib", "dylib")
elifgetPlatform() in ("linux", "freebsd", "netbsd", "openbsd"):
return_PlatformContext("LD_LIBRARY_PATH", ":", "lib", "so")
else:
return_PlatformContext("PATH", ";", "", "dll")
defhasChattyStderr(test_case):
"""Some targets produce garbage on the standard error output. This utility function
determines whether the tests can be strict about the expected stderr contents."""
ifmatch_android_device(
test_case.getArchitecture(), ["aarch64"], range(22, 25+1)
):
returnTrue# The dynamic linker on the device will complain about unknown DT entries
returnFalse
defbuilder_module():
returnget_builder(sys.platform)
defgetArchitecture():
"""Returns the architecture in effect the test suite is running with."""
module=builder_module()
arch=module.getArchitecture()
ifarch=="amd64":
arch="x86_64"
ifarchin ["armv7l", "armv8l"]:
arch="arm"
ifre.match("rv64*", arch):
arch="riscv64"
ifre.match("rv32*", arch):
arch="riscv32"
returnarch
lldbArchitecture=None
defgetLLDBArchitecture():
"""Returns the architecture of the lldb binary."""
globallldbArchitecture
ifnotlldbArchitecture:
# These two target settings prevent lldb from doing setup that does
# nothing but slow down the end goal of printing the architecture.
command= [
lldbtest_config.lldbExec,
"-x",
"-b",
"-o",
"settings set target.preload-symbols false",
"-o",
"settings set target.load-script-from-symbol-file false",
"-o",
"file "+lldbtest_config.lldbExec,
]
output=subprocess.check_output(command)
str=output.decode()
forlineinstr.splitlines():
m=re.search(r"Current executable set to '.*' \((.*)\)\.", line)
ifm:
lldbArchitecture=m.group(1)
break
returnlldbArchitecture
defgetCompiler():
"""Returns the compiler in effect the test suite is running with."""
module=builder_module()
returnmodule.getCompiler()
defgetCompilerVersion():
"""Returns a string that represents the compiler version.
Supports: llvm, clang.
"""
version_output=subprocess.check_output(
[getCompiler(), "--version"], errors="replace"
)
m=re.search("version ([0-9.]+)", version_output)
ifm:
returnm.group(1)
return"unknown"
defgetDwarfVersion():
"""Returns the dwarf version generated by clang or '0'."""
ifconfiguration.dwarf_version:
returnstr(configuration.dwarf_version)
if"clang"ingetCompiler():
try:
triple=builder_module().getTriple(getArchitecture())
target= ["-target", triple] iftripleelse []
driver_output=subprocess.check_output(
[getCompiler()] +target+"-g -c -x c - -o - -###".split(),
stderr=subprocess.STDOUT,
)
driver_output=driver_output.decode("utf-8")
forlineindriver_output.split(os.linesep):
m=re.search("dwarf-version=([0-9])", line)
ifm:
returnm.group(1)
exceptsubprocess.CalledProcessError:
pass
return"0"
defexpectedCompilerVersion(compiler_version):
"""Returns True iff compiler_version[1] matches the current compiler version.
Use compiler_version[0] to specify the operator used to determine if a match has occurred.
Any operator other than the following defaults to an equality test:
'>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not'
If the current compiler version cannot be determined, we assume it is close to the top
of trunk, so any less-than or equal-to comparisons will return False, and any
greater-than or not-equal-to comparisons will return True.
"""
ifcompiler_versionisNone:
returnTrue
operator=str(compiler_version[0])
version_str=str(compiler_version[1])
ifnotversion_str:
returnTrue
test_compiler_version_str=getCompilerVersion()
iftest_compiler_version_str=="unknown":
# Assume the compiler version is at or near the top of trunk.
returnoperatorin [">", ">=", "!", "!=", "not"]
actual_version=version.parse(version_str)
test_compiler_version=version.parse(test_compiler_version_str)
ifoperator==">":
returntest_compiler_version>actual_version
ifoperator==">="oroperator=="=>":
returntest_compiler_version>=actual_version
ifoperator=="<":
returntest_compiler_version<actual_version
ifoperator=="<="oroperator=="=<":
returntest_compiler_version<=actual_version
ifoperator=="!="oroperator=="!"oroperator=="not":
returnversion_strnotintest_compiler_version_str
returnversion_strintest_compiler_version_str
defexpectedCompiler(compilers):
"""Returns True iff any element of compilers is a sub-string of the current compiler."""
ifcompilersisNone:
returnTrue
forcompilerincompilers:
ifcompileringetCompiler():
returnTrue
returnFalse
# This is a helper function to determine if a specific version of Xcode's linker
# contains a TLS bug. We want to skip TLS tests if they contain this bug, but
# adding a linker/linker_version conditions to a decorator is challenging due to
# the number of ways linkers can enter the build process.
defxcode15LinkerBug():
"""Returns true iff a test is running on a darwin platform and the host linker is between versions 1000 and 1109."""
darwin_platforms=lldbplatform.translate(lldbplatform.darwin_all)
ifgetPlatform() notindarwin_platforms:
returnFalse
try:
raw_version_details=subprocess.check_output(
("xcrun", "ld", "-version_details")
)
version_details=json.loads(raw_version_details)
version=version_details.get("version", "0")
version_tuple=tuple(int(x) forxinversion.split("."))
if (1000,) <=version_tuple<= (1109,):
returnTrue
except:
pass
returnFalse