Skip to content

Commit a4d4277

Browse files
n2h9JDevlieghere
andauthored
[lldb] [scripting bridge] 167388 chore: add api to return arch name for target (llvm#168273)
This pr fixes llvm#167388 . ## Description This pr adds new method `GetArchName` to `SBTarget` so that no need to parse triple to get arch name in client code. ## Testing ### All from `TestTargetAPI.py` run test with ``` ./build/bin/lldb-dotest -v -p TestTargetAPI.py ``` <details> <summary>existing tests (without newly added)</summary> <img width="1425" height="804" alt="image" src="https://github.com/user-attachments/assets/617e4c69-5c6b-44c4-9aeb-b751a47e253c" /> </details> <details> <summary>existing tests (with newly added)</summary> <img width="1422" height="778" alt="image" src="https://github.com/user-attachments/assets/746990a1-df88-4348-a090-224963d3c640" /> </details> ### Only `test_get_arch_name` run test with ``` ./build/bin/lldb-dotest -v -p TestTargetAPI.py -f test_get_arch_name_dwarf -f test_get_arch_name_dwo -f test_get_arch_name_dsym lldb/test/API/python_api/target ``` <details> <summary>only newly added</summary> <img width="1422" height="778" alt="image" src="https://github.com/user-attachments/assets/fcaafa5d-2622-4171-acee-e104ecee0652" /> </details> --------- Signed-off-by: Nikita B <[email protected]> Co-authored-by: Jonas Devlieghere <[email protected]>
1 parent 684f64c commit a4d4277

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

lldb/bindings/interface/SBTargetExtensions.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ STRING_EXTENSION_LEVEL_OUTSIDE(SBTarget, lldb::eDescriptionLevelBrief)
190190
byte_order = property(GetByteOrder, None, doc='''A read only property that returns an lldb enumeration value (lldb.eByteOrderLittle, lldb.eByteOrderBig, lldb.eByteOrderInvalid) that represents the byte order for this target.''')
191191
addr_size = property(GetAddressByteSize, None, doc='''A read only property that returns the size in bytes of an address for this target.''')
192192
triple = property(GetTriple, None, doc='''A read only property that returns the target triple (arch-vendor-os) for this target as a string.''')
193+
arch_name = property(GetArchName, None, doc='''A read only property that returns the architecture name for this target as a string.''')
193194
data_byte_size = property(GetDataByteSize, None, doc='''A read only property that returns the size in host bytes of a byte in the data address space for this target.''')
194195
code_byte_size = property(GetCodeByteSize, None, doc='''A read only property that returns the size in host bytes of a byte in the code address space for this target.''')
195196
platform = property(GetPlatform, None, doc='''A read only property that returns the platform associated with with this target.''')

lldb/examples/python/templates/scripted_process.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ def __init__(self, exe_ctx, args):
3535
target = exe_ctx.target
3636
if isinstance(target, lldb.SBTarget) and target.IsValid():
3737
self.target = target
38-
triple = self.target.triple
39-
if triple:
40-
self.arch = triple.split("-")[0]
38+
self.arch = target.arch_name
4139
self.dbg = target.GetDebugger()
4240
if isinstance(args, lldb.SBStructuredData) and args.IsValid():
4341
self.args = args

lldb/include/lldb/API/SBTarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ class LLDB_API SBTarget {
358358

359359
const char *GetTriple();
360360

361+
const char *GetArchName();
362+
361363
const char *GetABIName();
362364

363365
const char *GetLabel() const;

lldb/source/API/SBTarget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,19 @@ const char *SBTarget::GetTriple() {
16201620
return nullptr;
16211621
}
16221622

1623+
const char *SBTarget::GetArchName() {
1624+
LLDB_INSTRUMENT_VA(this);
1625+
1626+
if (TargetSP target_sp = GetSP()) {
1627+
llvm::StringRef arch_name =
1628+
target_sp->GetArchitecture().GetTriple().getArchName();
1629+
ConstString const_arch_name(arch_name);
1630+
1631+
return const_arch_name.GetCString();
1632+
}
1633+
return nullptr;
1634+
}
1635+
16231636
const char *SBTarget::GetABIName() {
16241637
LLDB_INSTRUMENT_VA(this);
16251638

lldb/test/API/python_api/target/TestTargetAPI.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ def test_resolve_file_address(self):
105105
self.assertIsNotNone(data_section2)
106106
self.assertEqual(data_section.name, data_section2.name)
107107

108+
def test_get_arch_name(self):
109+
d = {"EXE": "b.out"}
110+
self.build(dictionary=d)
111+
self.setTearDownCleanup(dictionary=d)
112+
target = self.create_simple_target("b.out")
113+
114+
arch_name = target.arch_name
115+
self.assertTrue(len(arch_name) > 0, "Got an arch name")
116+
117+
# Test consistency with triple.
118+
triple = target.triple
119+
self.assertTrue(len(triple) > 0, "Got a triple")
120+
self.assertEqual(
121+
triple.split("-")[0],
122+
arch_name,
123+
"Arch name is equal to the first item of the triple",
124+
)
125+
108126
def test_get_ABIName(self):
109127
d = {"EXE": "b.out"}
110128
self.build(dictionary=d)

0 commit comments

Comments
 (0)