xref: /llvm-project/lldb/test/API/functionalities/json/object-file/TestObjectFileJSON.py (revision f40ed1f619046e98d08b092b1afb835ed5156f52)
1import lldb
2from lldbsuite.test.decorators import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5
6import json
7import uuid
8import os
9import shutil
10import time
11
12
13class TestObjectFileJSON(TestBase):
14    TRIPLE = "arm64-apple-macosx13.0.0"
15
16    def setUp(self):
17        TestBase.setUp(self)
18        self.source = "main.c"
19
20    def emitJSON(self, data, path):
21        json_object = json.dumps(data, indent=4)
22        with open(path, "w") as outfile:
23            outfile.write(json_object)
24
25    def toModuleSpec(self, path):
26        module_spec = lldb.SBModuleSpec()
27        module_spec.SetFileSpec(lldb.SBFileSpec(path))
28        return module_spec
29
30    @no_debug_info_test
31    def test_target(self):
32        triple = "arm64-apple-macosx13.0.0"
33        data = {
34            "triple": triple,
35            "uuid": str(uuid.uuid4()),
36            "type": "executable",
37        }
38
39        json_object_file = self.getBuildArtifact("a.json")
40        self.emitJSON(data, json_object_file)
41
42        target = self.dbg.CreateTarget(json_object_file)
43        self.assertTrue(target.IsValid())
44        self.assertEqual(target.GetTriple(), triple)
45
46    @no_debug_info_test
47    def test_module(self):
48        self.build()
49        exe = self.getBuildArtifact("a.out")
50        target = self.dbg.CreateTarget(exe)
51
52        data = {
53            "triple": target.GetTriple(),
54            "uuid": str(uuid.uuid4()),
55        }
56
57        json_object_file_b = self.getBuildArtifact("b.json")
58        self.emitJSON(data, json_object_file_b)
59
60        module = target.AddModule(self.toModuleSpec(json_object_file_b))
61        self.assertFalse(module.IsValid())
62
63        data = {
64            "triple": target.GetTriple(),
65            "uuid": str(uuid.uuid4()),
66            "type": "sharedlibrary",
67            "sections": [
68                {
69                    "name": "__TEXT",
70                    "type": "code",
71                    "address": 0,
72                    "size": 0x222,
73                }
74            ],
75            "symbols": [
76                {
77                    "name": "foo",
78                    "address": 0x100,
79                    "size": 0x11,
80                }
81            ],
82        }
83
84        json_object_file_c = self.getBuildArtifact("c.json")
85        self.emitJSON(data, json_object_file_c)
86
87        module = target.AddModule(self.toModuleSpec(json_object_file_c))
88        self.assertTrue(module.IsValid())
89
90        section = module.GetSectionAtIndex(0)
91        self.assertTrue(section.IsValid())
92        self.assertEqual(section.GetName(), "__TEXT")
93        self.assertEqual(section.file_addr, 0x0)
94        self.assertEqual(section.size, 0x222)
95
96        symbol = module.FindSymbol("foo")
97        self.assertTrue(symbol.IsValid())
98        self.assertEqual(symbol.addr.GetFileAddress(), 0x100)
99        self.assertEqual(symbol.GetSize(), 0x11)
100
101        error = target.SetSectionLoadAddress(section, 0x1000)
102        self.assertSuccess(error)
103        self.assertEqual(symbol.addr.GetLoadAddress(target), 0x1100)
104