[Lldb-commits] [lldb] [lldb] [scripting bridge] 167388 chore: add api to return arch name for target (PR #168273)
via lldb-commits
lldb-commits at lists.llvm.org
Sun Nov 16 09:18:48 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: None (n2h9)
<details>
<summary>Changes</summary>
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>
---
Full diff: https://github.com/llvm/llvm-project/pull/168273.diff
4 Files Affected:
- (modified) lldb/examples/python/templates/scripted_process.py (+1-3)
- (modified) lldb/include/lldb/API/SBTarget.h (+2)
- (modified) lldb/source/API/SBTarget.cpp (+13)
- (modified) lldb/test/API/python_api/target/TestTargetAPI.py (+14)
``````````diff
diff --git a/lldb/examples/python/templates/scripted_process.py b/lldb/examples/python/templates/scripted_process.py
index 49059d533f38a..25f17d7e71784 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.GetArchName()
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 379a0bb7e9513..d0da6aaa6044c 100644
--- a/lldb/include/lldb/API/SBTarget.h
+++ b/lldb/include/lldb/API/SBTarget.h
@@ -353,6 +353,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 98d10aa07c53f..f0458bb6b5fe5 100644
--- a/lldb/source/API/SBTarget.cpp
+++ b/lldb/source/API/SBTarget.cpp
@@ -1614,6 +1614,19 @@ const char *SBTarget::GetTriple() {
return nullptr;
}
+const char *SBTarget::GetArchName() {
+ LLDB_INSTRUMENT_VA(this);
+
+ if (TargetSP target_sp = GetSP()) {
+ std::string arch_name =
+ target_sp->GetArchitecture().GetTriple().getArchName().str();
+ ConstString const_arch_name(arch_name.c_str());
+
+ 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..b4a7bcbf3c8e3 100644
--- a/lldb/test/API/python_api/target/TestTargetAPI.py
+++ b/lldb/test/API/python_api/target/TestTargetAPI.py
@@ -105,6 +105,20 @@ 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.GetArchName()
+ self.assertNotEqual(len(arch_name), 0, "Got an arch name string")
+
+ # Test consistency with GetTriple().
+ triple = target.triple
+ if triple:
+ self.assertEqual(triple.split("-")[0], arch_name)
+
def test_get_ABIName(self):
d = {"EXE": "b.out"}
self.build(dictionary=d)
``````````
</details>
https://github.com/llvm/llvm-project/pull/168273
More information about the lldb-commits
mailing list