|
| 1 | +""" |
| 2 | +Test support for the DebugInfoD network symbol acquisition protocol. |
| 3 | +""" |
| 4 | + |
| 5 | +import lldb |
| 6 | +import lldbsuite.test.lldbutil as lldbutil |
| 7 | +from lldbsuite.test.lldbtest import * |
| 8 | + |
| 9 | +def getUUID(aoutuuid): |
| 10 | + """ |
| 11 | + Pull the 20 byte UUID out of the .note.gnu.build-id section that was dumped |
| 12 | + to a file already, as part of the build. |
| 13 | + """ |
| 14 | + import struct |
| 15 | + with open(aoutuuid, "rb") as f: |
| 16 | + data = f.read(36) |
| 17 | + if len(data) != 36: |
| 18 | + return None |
| 19 | + header = struct.unpack_from("<4I", data) |
| 20 | + if len(header) != 4: |
| 21 | + return None |
| 22 | + # 4 element 'prefix', 20 bytes of uuid, 3 byte long string, 'GNU': |
| 23 | + if header[0] != 4 or header[1] != 20 or header[2] != 3 or header[3] != 0x554e47: |
| 24 | + return None |
| 25 | + return data[16:].hex() |
| 26 | + |
| 27 | +def config_test(local_files, uuid, debuginfo, executable): |
| 28 | + """ |
| 29 | + Set up a test with local_files[] copied to a particular location |
| 30 | + so that we control which files are, or are not, found in the file system. |
| 31 | + Also, create a stand-alone file-system 'hosted' debuginfod server for the |
| 32 | + given UUID. |
| 33 | +
|
| 34 | + Make the filesystem look like: |
| 35 | +
|
| 36 | + /tmp/<tmpdir>/test/[local_files] |
| 37 | +
|
| 38 | + /tmp/<tmpdir>/cache (for lldb to use as a temp cache) |
| 39 | +
|
| 40 | + /tmp/<tmpdir>/buildid/<uuid>/executable -> <executable> |
| 41 | + /tmp/<tmpdir>/buildid/<uuid>/debuginfo -> <debuginfo> |
| 42 | + Returns the /tmp/<tmpdir> path |
| 43 | + """ |
| 44 | + import os |
| 45 | + import shutil |
| 46 | + import tempfile |
| 47 | + |
| 48 | + tmp_dir = tempfile.mkdtemp() |
| 49 | + test_dir = os.path.join(tmp_dir, "test") |
| 50 | + uuid_dir = os.path.join(tmp_dir, "buildid", uuid) |
| 51 | + |
| 52 | + # Make the 3 directories |
| 53 | + os.makedirs(os.path.join(tmp_dir, "cache")) |
| 54 | + os.makedirs(uuid_dir) |
| 55 | + os.makedirs(test_dir) |
| 56 | + |
| 57 | + # Copy the files used by the test: |
| 58 | + for f in local_files: |
| 59 | + shutil.copy(f, test_dir) |
| 60 | + |
| 61 | + # Fill in the 'file://...' mocked Debuginfod server |
| 62 | + if debuginfo: |
| 63 | + shutil.move(debuginfo, os.path.join(uuiddir, "debuginfo")) |
| 64 | + if executable: |
| 65 | + shutil.move(executable, os.path.join(uuiddir, "executable")) |
| 66 | + |
| 67 | + return tmp_dir |
| 68 | + |
| 69 | + |
| 70 | +# Need to test 5 different scenarios: |
| 71 | +# 1 - A stripped binary with it's corresponding unstripped binary: |
| 72 | +# 2 - A stripped binary with a corresponding --only-keep-debug symbols file |
| 73 | +# 3 - A split binary with it's corresponding DWP file |
| 74 | +# 4 - A stripped, split binary with an unstripped binary and a DWP file |
| 75 | +# 5 - A stripped, split binary with an --only-keep-debug symbols file and a DWP file |
| 76 | + |
| 77 | +class DebugInfodTests(TestBase): |
| 78 | + # No need to try every flavor of debug inf. |
| 79 | + NO_DEBUG_INFO_TESTCASE = True |
| 80 | + |
| 81 | + def test_stuff(self): |
| 82 | + """This should test stuff.""" |
| 83 | + self.build() |
| 84 | + # Pull the UUID out of the binary. |
| 85 | + uuid_file = self.getBuildArtifact("a.out.uuid") |
| 86 | + self.aout = self.getBuildArtifact("a.out") |
| 87 | + self.debugbin = self.getBuildArtifact("a.out.debug") |
| 88 | + self.dwp = self.getBuildArtifact("a.out.dwp") |
| 89 | + self.uuid = getUUID(uuid_file) |
| 90 | + # Setup the fake DebugInfoD server. |
| 91 | + server_root = config_fake_debuginfod_server(self.uuid, self.dwp, self.debugbin) |
| 92 | + |
| 93 | + # Configure LLDB properly |
| 94 | + self.runCmd("settings set symbols.enable-external-lookup true") |
| 95 | + self.runCmd("settings set plugin.symbol-locator.debuginfod.cache-path %s/cache" % server_root) |
| 96 | + self.runCmd("settings clear plugin.symbol-locator.debuginfod.server-urls") |
| 97 | + cmd = "settings insert-before plugin.symbol-locator.debuginfod.server-urls 0 file://%s" % server_root |
| 98 | + self.runCmd(cmd) |
| 99 | + print(cmd) |
| 100 | + # Check to see if the symbol file is properly loaded |
| 101 | + self.main_source_file = lldb.SBFileSpec("main.c") |
| 102 | + self.sample_test() |
| 103 | + |
| 104 | + def sample_test(self): |
| 105 | + """You might use the test implementation in several ways, say so here.""" |
| 106 | + # This function starts a process, "a.out" by default, sets a source |
| 107 | + # breakpoint, runs to it, and returns the thread, process & target. |
| 108 | + # It optionally takes an SBLaunchOption argument if you want to pass |
| 109 | + # arguments or environment variables. |
| 110 | + target = target = self.dbg.CreateTarget(self.aout) |
| 111 | + self.assertTrue(target and target.IsValid(), "Target is valid") |
| 112 | + bp = target.BreakpointCreateByName("func") |
| 113 | + self.assertTrue(bp and bp.IsValid(), "Breakpoint is valid") |
| 114 | + self.assertEqual(bp.GetNumLocations(), 1) |
| 115 | + print("Loc @ Index 0:") |
| 116 | + print(bp.GetLocationAtIndex(0)) |
| 117 | + loc = bp.GetLocationAtIndex(0) |
| 118 | + self.assertTrue(loc and loc.IsValid(), "Location is valid") |
| 119 | + addr = loc.GetAddress() |
| 120 | + self.assertTrue(addr and addr.IsValid(), "Loc address is valid") |
| 121 | + line_entry = addr.GetLineEntry() |
| 122 | + self.assertTrue(line_entry and line_entry.IsValid(), "Loc line entry is valid") |
| 123 | + self.assertEqual(line_entry.GetLine(), 18) |
| 124 | + self.assertEqual(loc.GetLineEntry().GetFileSpec().GetFilename(), self.main_source_file.GetFilename()) |
| 125 | + |
| 126 | + def sample_test_no_launch(self): |
| 127 | + """Same as above but doesn't launch a process.""" |
| 128 | + |
| 129 | + target = self.createTestTarget() |
| 130 | + self.expect_expr("global_test_var", result_value="10") |
0 commit comments