Skip to content

Commit 2590576

Browse files
committed
[lldb] Add utility to create Mach-O corefile from YAML desc (llvm#153911)
I've wanted a utility to create a corefile for test purposes given a bit of memory and regsters, for a while. I've written a few API tests over the years that needed exactly this capability -- we have several one-off Mach-O corefile creator utility in the API testsuite to do this. But it's a lot of boilerplate when you only want to specify some register contents and memory contents, to create an API test. This adds yaml2mach-core, a tool that should build on any system, takes a yaml description of register values for one or more threads, optionally memory values for one or more memory regions, and can take a list of UUIDs that will be added as LC_NOTE "load binary" metadata to the corefile so binaries can be loaded into virtual address space in a test scenario. The format of the yaml file looks like ``` cpu: armv7m addressable-bits: num-bits: 39 binaries: - name: debug-binary.development uuid: 67942352-5857-3D3D-90CB-A3F80BA67B04 virtual-address: 0xfffffff01840c000 threads: - regsets: - flavor: gpr registers: [{name: sp, value: 0x2000fe70}, {name: r7, value: 0x2000fe80}, {name: pc, value: 0x0020392c}, {name: lr, value: 0x0020392d}] memory-regions: # stack memory - addr: 0x2000fe70 UInt32: [ 0x0000002a, 0x20010e58, 0x00203923, 0x00000001, 0x2000fe88, 0x00203911, 0x2000ffdc, 0xfffffff9 ] # instructions of a function - addr: 0x203910 UInt8: [ 0xf8, 0xb5, 0x04, 0xaf, 0x06, 0x4c, 0x07, 0x49, 0x74, 0xf0, 0x2e, 0xf8, 0x01, 0xac, 0x74, 0xf0 ] ``` and that's all that is needed to specify a corefile where four register values are specified (the others will be set to 0), and two memory regions will be emitted. The memory can be specified as an array of UInt8, UInt32, or UInt64, I anticipate that some of these corefiles may have stack values constructed manually and it may be simpler for a human to write them in a particular grouping of values. I needed this utility for an upcoming patch for ARM Cortex-M processors, to create a test for the change. I took the opportunity to remove two of the "trivial mach-o corefile" creator utilities I've written in the past, which also restricted the tests to only run on Darwin systems because I was using the system headers for Mach-O constant values. rdar://110663219 (cherry picked from commit a0c2d6e)
1 parent 57eef32 commit 2590576

26 files changed

+1159
-412
lines changed

lldb/packages/Python/lldbsuite/test/configuration.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
# Path to the yaml2obj tool. Not optional.
6868
yaml2obj = None
6969

70+
# Path to the yaml2macho-core tool. Not optional.
71+
yaml2macho_core = None
72+
7073
# The arch might dictate some specific CFLAGS to be passed to the toolchain to build
7174
# the inferior programs. The global variable cflags_extras provides a hook to do
7275
# just that.
@@ -178,3 +181,11 @@ def get_yaml2obj_path():
178181
"""
179182
if yaml2obj and os.path.lexists(yaml2obj):
180183
return yaml2obj
184+
185+
186+
def get_yaml2macho_core_path():
187+
"""
188+
Get the path to the yaml2macho-core tool.
189+
"""
190+
if yaml2macho_core and os.path.lexists(yaml2macho_core):
191+
return yaml2macho_core

lldb/packages/Python/lldbsuite/test/dotest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ def parseOptionsAndInitTestdirs():
279279
configuration.llvm_tools_dir = args.llvm_tools_dir
280280
configuration.filecheck = shutil.which("FileCheck", path=args.llvm_tools_dir)
281281
configuration.yaml2obj = shutil.which("yaml2obj", path=args.llvm_tools_dir)
282+
configuration.yaml2macho_core = shutil.which(
283+
"yaml2macho-core", path=args.llvm_tools_dir
284+
)
282285

283286
if not configuration.get_filecheck_path():
284287
logging.warning("No valid FileCheck executable; some tests may fail...")

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,29 @@ def yaml2obj(self, yaml_path, obj_path, max_size=None):
17151715
command += ["--max-size=%d" % max_size]
17161716
self.runBuildCommand(command)
17171717

1718+
def yaml2macho_core(self, yaml_path, obj_path, uuids=None):
1719+
"""
1720+
Create a Mach-O corefile at the given path from a yaml file.
1721+
1722+
Throws subprocess.CalledProcessError if the object could not be created.
1723+
"""
1724+
yaml2macho_core_bin = configuration.get_yaml2macho_core_path()
1725+
if not yaml2macho_core_bin:
1726+
self.assertTrue(False, "No valid yaml2macho-core executable specified")
1727+
if uuids != None:
1728+
command = [
1729+
yaml2macho_core_bin,
1730+
"-i",
1731+
yaml_path,
1732+
"-o",
1733+
obj_path,
1734+
"-u",
1735+
uuids,
1736+
]
1737+
else:
1738+
command = [yaml2macho_core_bin, "-i", yaml_path, "-o", obj_path]
1739+
self.runBuildCommand(command)
1740+
17181741
def cleanup(self, dictionary=None):
17191742
"""Platform specific way to do cleanup after build."""
17201743
module = builder_module()
@@ -2278,7 +2301,9 @@ def completions_match(self, command, completions, max_completions=-1):
22782301
given list of completions"""
22792302
interp = self.dbg.GetCommandInterpreter()
22802303
match_strings = lldb.SBStringList()
2281-
interp.HandleCompletion(command, len(command), 0, max_completions, match_strings)
2304+
interp.HandleCompletion(
2305+
command, len(command), 0, max_completions, match_strings
2306+
)
22822307
# match_strings is a 1-indexed list, so we have to slice...
22832308
self.assertCountEqual(
22842309
completions, list(match_strings)[1:], "List of returned completion is wrong"

lldb/test/API/macosx/arm-corefile-regctx/Makefile

Lines changed: 0 additions & 6 deletions
This file was deleted.

lldb/test/API/macosx/arm-corefile-regctx/TestArmMachoCorefileRegctx.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@
1313
class TestArmMachoCorefileRegctx(TestBase):
1414
NO_DEBUG_INFO_TESTCASE = True
1515

16-
@skipUnlessDarwin
17-
def setUp(self):
18-
TestBase.setUp(self)
19-
self.build()
20-
self.create_corefile = self.getBuildArtifact("a.out")
21-
self.corefile = self.getBuildArtifact("core")
22-
2316
def test_armv7_corefile(self):
2417
### Create corefile
25-
retcode = call(self.create_corefile + " armv7 " + self.corefile, shell=True)
18+
corefile = self.getBuildArtifact("core")
19+
self.yaml2macho_core("armv7m.yaml", corefile)
2620

2721
target = self.dbg.CreateTarget("")
2822
err = lldb.SBError()
29-
process = target.LoadCore(self.corefile)
23+
process = target.LoadCore(corefile)
3024
self.assertTrue(process.IsValid())
3125
thread = process.GetSelectedThread()
3226
frame = thread.GetSelectedFrame()
@@ -50,11 +44,12 @@ def test_armv7_corefile(self):
5044

5145
def test_arm64_corefile(self):
5246
### Create corefile
53-
retcode = call(self.create_corefile + " arm64 " + self.corefile, shell=True)
47+
corefile = self.getBuildArtifact("core")
48+
self.yaml2macho_core("arm64.yaml", corefile)
5449

5550
target = self.dbg.CreateTarget("")
5651
err = lldb.SBError()
57-
process = target.LoadCore(self.corefile)
52+
process = target.LoadCore(corefile)
5853
self.assertTrue(process.IsValid())
5954
thread = process.GetSelectedThread()
6055
frame = thread.GetSelectedFrame()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cpu: arm64
2+
threads:
3+
# (lldb) reg read
4+
# % pbpaste | grep = | sed 's, ,,g' | awk -F= '{print "{name: " $1 ", value: " $2 "},"}'
5+
- regsets:
6+
- flavor: gpr
7+
registers: [
8+
{name: x0, value: 0x0000000000000001}, {name: x1, value: 0x000000016fdff3c0},
9+
{name: x2, value: 0x000000016fdff3d0}, {name: x3, value: 0x000000016fdff510},
10+
{name: x4, value: 0x0000000000000000}, {name: x5, value: 0x0000000000000000},
11+
{name: x6, value: 0x0000000000000000}, {name: x7, value: 0x0000000000000000},
12+
{name: x8, value: 0x000000010000d910}, {name: x9, value: 0x0000000000000001},
13+
{name: x10, value: 0xe1e88de000000000}, {name: x11, value: 0x0000000000000003},
14+
{name: x12, value: 0x0000000000000148}, {name: x13, value: 0x0000000000004000},
15+
{name: x14, value: 0x0000000000000008}, {name: x15, value: 0x0000000000000000},
16+
{name: x16, value: 0x0000000000000000}, {name: x17, value: 0x0000000100003f5c},
17+
{name: x18, value: 0x0000000000000000}, {name: x19, value: 0x0000000100003f5c},
18+
{name: x20, value: 0x000000010000c000}, {name: x21, value: 0x000000010000d910},
19+
{name: x22, value: 0x000000016fdff250}, {name: x23, value: 0x000000018ce12366},
20+
{name: x24, value: 0x000000016fdff1d0}, {name: x25, value: 0x0000000000000001},
21+
{name: x26, value: 0x0000000000000000}, {name: x27, value: 0x0000000000000000},
22+
{name: x28, value: 0x0000000000000000}, {name: fp, value: 0x000000016fdff3a0},
23+
{name: lr, value: 0x000000018cd97f28}, {name: sp, value: 0x000000016fdff140},
24+
{name: pc, value: 0x0000000100003f5c}, {name: cpsr, value: 0x80001000}
25+
]
26+
- flavor: exc
27+
registers: [ {name: far, value: 0x0000000100003f5c},
28+
{name: esr, value: 0xf2000000},
29+
{name: exception, value: 0x00000000}
30+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cpu: armv7m
2+
threads:
3+
# (lldb) reg read
4+
# % pbpaste | grep = | sed 's, ,,g' | awk -F= '{print "{name: " $1 ", value: " $2 "},"}'
5+
- regsets:
6+
- flavor: gpr
7+
registers: [
8+
{name: r0, value: 0x00010000}, {name: r1, value: 0x00020000},
9+
{name: r2, value: 0x00030000}, {name: r3, value: 0x00040000},
10+
{name: r4, value: 0x00050000}, {name: r5, value: 0x00060000},
11+
{name: r6, value: 0x00070000}, {name: r7, value: 0x00080000},
12+
{name: r8, value: 0x00090000}, {name: r9, value: 0x000a0000},
13+
{name: r10, value: 0x000b0000}, {name: r11, value: 0x000c0000},
14+
{name: r12, value: 0x000d0000}, {name: sp, value: 0x000e0000},
15+
{name: lr, value: 0x000f0000}, {name: pc, value: 0x00100000},
16+
{name: cpsr, value: 0x00110000}
17+
]
18+
- flavor: exc
19+
registers: [ {name: far, value: 0x00003f5c},
20+
{name: esr, value: 0xf2000000},
21+
{name: exception, value: 0x00000000}
22+
]
23+
24+
memory-regions:
25+
# $sp is 0x000e0000, have bytes surrounding that address
26+
- addr: 0x000dffe0
27+
UInt8: [
28+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
29+
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
30+
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
31+
0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
32+
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
33+
0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
34+
0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
35+
0x3f
36+
]

0 commit comments

Comments
 (0)