Author: n2h9
Date: 2025-11-26T11:36:19-08:00
New Revision: a4d42775b9af0d961f71934e38342a9384534022

URL: 
https://github.com/llvm/llvm-project/commit/a4d42775b9af0d961f71934e38342a9384534022
DIFF: 
https://github.com/llvm/llvm-project/commit/a4d42775b9af0d961f71934e38342a9384534022.diff

LOG: [lldb] [scripting bridge] 167388 chore: add api to return arch name for 
target (#168273)

This pr fixes #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]>

Added: 
    

Modified: 
    lldb/bindings/interface/SBTargetExtensions.i
    lldb/examples/python/templates/scripted_process.py
    lldb/include/lldb/API/SBTarget.h
    lldb/source/API/SBTarget.cpp
    lldb/test/API/python_api/target/TestTargetAPI.py

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBTargetExtensions.i 
b/lldb/bindings/interface/SBTargetExtensions.i
index 43125d8970615..ef1093b03ced9 100644
--- a/lldb/bindings/interface/SBTargetExtensions.i
+++ b/lldb/bindings/interface/SBTargetExtensions.i
@@ -190,6 +190,7 @@ STRING_EXTENSION_LEVEL_OUTSIDE(SBTarget, 
lldb::eDescriptionLevelBrief)
         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.''')
         addr_size = property(GetAddressByteSize, None, doc='''A read only 
property that returns the size in bytes of an address for this target.''')
         triple = property(GetTriple, None, doc='''A read only property that 
returns the target triple (arch-vendor-os) for this target as a string.''')
+        arch_name = property(GetArchName, None, doc='''A read only property 
that returns the architecture name for this target as a string.''')
         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.''')
         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.''')
         platform = property(GetPlatform, None, doc='''A read only property 
that returns the platform associated with with this target.''')

diff  --git a/lldb/examples/python/templates/scripted_process.py 
b/lldb/examples/python/templates/scripted_process.py
index 49059d533f38a..b4232f632a30a 100644
--- a/lldb/examples/python/templates/scripted_process.py
+++ b/lldb/examples/python/templates/scripted_process.py
@@ -35,9 +35,7 @@ def __init__(self, exe_ctx, args):
             target = exe_ctx.target
         if isinstance(target, lldb.SBTarget) and target.IsValid():
             self.target = target
-            triple = self.target.triple
-            if triple:
-                self.arch = triple.split("-")[0]
+            self.arch = target.arch_name
             self.dbg = target.GetDebugger()
         if isinstance(args, lldb.SBStructuredData) and args.IsValid():
             self.args = args

diff  --git a/lldb/include/lldb/API/SBTarget.h 
b/lldb/include/lldb/API/SBTarget.h
index d0b91ff4741fa..ce81ae46a0905 100644
--- a/lldb/include/lldb/API/SBTarget.h
+++ b/lldb/include/lldb/API/SBTarget.h
@@ -358,6 +358,8 @@ class LLDB_API SBTarget {
 
   const char *GetTriple();
 
+  const char *GetArchName();
+
   const char *GetABIName();
 
   const char *GetLabel() const;

diff  --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp
index 1879f3957ca32..578a7bdf7433d 100644
--- a/lldb/source/API/SBTarget.cpp
+++ b/lldb/source/API/SBTarget.cpp
@@ -1620,6 +1620,19 @@ const char *SBTarget::GetTriple() {
   return nullptr;
 }
 
+const char *SBTarget::GetArchName() {
+  LLDB_INSTRUMENT_VA(this);
+
+  if (TargetSP target_sp = GetSP()) {
+    llvm::StringRef arch_name =
+        target_sp->GetArchitecture().GetTriple().getArchName();
+    ConstString const_arch_name(arch_name);
+
+    return const_arch_name.GetCString();
+  }
+  return nullptr;
+}
+
 const char *SBTarget::GetABIName() {
   LLDB_INSTRUMENT_VA(this);
 

diff  --git a/lldb/test/API/python_api/target/TestTargetAPI.py 
b/lldb/test/API/python_api/target/TestTargetAPI.py
index d346563af18e2..d3c64d87375b4 100644
--- a/lldb/test/API/python_api/target/TestTargetAPI.py
+++ b/lldb/test/API/python_api/target/TestTargetAPI.py
@@ -105,6 +105,24 @@ def test_resolve_file_address(self):
         self.assertIsNotNone(data_section2)
         self.assertEqual(data_section.name, data_section2.name)
 
+    def test_get_arch_name(self):
+        d = {"EXE": "b.out"}
+        self.build(dictionary=d)
+        self.setTearDownCleanup(dictionary=d)
+        target = self.create_simple_target("b.out")
+
+        arch_name = target.arch_name
+        self.assertTrue(len(arch_name) > 0, "Got an arch name")
+
+        # Test consistency with triple.
+        triple = target.triple
+        self.assertTrue(len(triple) > 0, "Got a triple")
+        self.assertEqual(
+            triple.split("-")[0],
+            arch_name,
+            "Arch name is equal to the first item of the triple",
+        )
+
     def test_get_ABIName(self):
         d = {"EXE": "b.out"}
         self.build(dictionary=d)


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to