Skip to content

Conversation

@n2h9
Copy link

@n2h9 n2h9 commented Nov 16, 2025

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
existing tests (without newly added) image
existing tests (with newly added) image

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

only newly added image

Signed-off-by: Nikita B <n2h9z4@gmail.com>
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

…et.GetArchName() instead of target.arch_name in script

Signed-off-by: Nikita B <n2h9z4@gmail.com>
@n2h9 n2h9 marked this pull request as ready for review November 16, 2025 17:18
@n2h9 n2h9 requested a review from JDevlieghere as a code owner November 16, 2025 17:18
@llvmbot llvmbot added the lldb label Nov 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 16, 2025

@llvm/pr-subscribers-lldb

Author: None (n2h9)

Changes

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 --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)

@n2h9 n2h9 marked this pull request as draft November 16, 2025 17:23
n2h9 added 2 commits November 16, 2025 18:28
…_name readonly property to the binding

Signed-off-by: Nikita B <n2h9z4@gmail.com>
…et.arch_name instead of target.GetArchName() in script

Signed-off-by: Nikita B <n2h9z4@gmail.com>
…omment in test

Signed-off-by: Nikita B <n2h9z4@gmail.com>
@github-actions
Copy link

github-actions bot commented Nov 16, 2025

✅ With the latest revision this PR passed the Python code formatter.

@medismailben
Copy link
Member

Please run darker on your PR to satisfy the code formatter PR testing job.

…ormatting according to darker

Signed-off-by: Nikita B <n2h9z4@gmail.com>
@n2h9
Copy link
Author

n2h9 commented Nov 16, 2025

Please run darker on your PR to satisfy the code formatter PR testing job.

Updated ✅

screenshot image


# Test consistency with triple.
triple = target.triple
if triple:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a scenario where there wouldn't be a triple? Should we assert that there is one instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated ✅ thank you 🙇‍♀️

target = self.create_simple_target("b.out")

arch_name = target.arch_name
self.assertNotEqual(len(arch_name), 0, "Got an arch name string")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better test would check for a specific arch name. Since the binary doesn't need to run, can we target a specific arg in the build dictionary and then check for that, rather than the fact that we have "something"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

I tried to specify architecture when calling self.build, like in this commit 329aec5
but it does not work as expected, looks like the architecture is still the runtime one (if I put arm there GetArchName still returns x86_64, which is what I use to run the test).
Or how one properly specifies the specific architecture?

So, I used the value from self.getArchitecture() to assert the value from arch_name, it is working fine on my platform, will it be ok to use this value for assertion?

Also we could probably just assert that the arch_name is eq to the first element of the `triple, as this is probably we expect from this method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is not a correct one, if I run test specifying a different arch, like this

./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 \
        --arch aarch64 \
        lldb/test/API/python_api/target

arch_name returns me x86_64 😢
let me double check why . . .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think right now when performing build in tests, it does not take into account architecture from --arch or from self.build.

I assume it prepares build command in builder.py:293 which uses builder.py::getTriple which does not take into account provided arch and always use arch from configuration.

I can suggest for now to only check that arch_name is eq to first part of triple.
And I could probably create a ticket to investigate / solve this issue with taking into account architecture during test build, what do you think?

Copy link
Author

@n2h9 n2h9 Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated for now to only compare that arch_name is same as triple first element ✅ .
And created and issue from above comment #168286

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you specify the ARCH in the build dictionary?

dictionary={
  "EXE": "b.out",
  "arch": arch,
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case build also does not take architecture into account, tried both arch and architecture 😢 .

        expected_arch = "aarch64"
        d = {
            "EXE": "b.out",
            "arch": expected_arch,
        }
        self.build(dictionary=d)

       ....

        print(">>>>")
        print(f"target.arch_name = {target.arch_name}")
        print("<<<<")

test command output with traces
./build/bin/lldb-dotest \
	-v -t -p TestTargetAPI.py \
	-f test_get_arch_name_dwarf \
	-f test_get_arch_name_dwo \
	-f test_get_arch_name_dsym \
	--arch aarch64 \
	lldb/test/API/python_api/target



/usr/bin/python3 /home/ubuntu/code/llvm-project/lldb/test/API/dotest.py --arch x86_64 -u CXXFLAGS -u CFLAGS --build-dir /home/ubuntu/code/llvm-project/build/lldb-test-build.noindex --executable /home/ubuntu/code/llvm-project/build/./bin/lldb --compiler /home/ubuntu/code/llvm-project/build/./bin/clang --dsymutil /home/ubuntu/code/llvm-project/build/./bin/dsymutil --make /usr/bin/gmake --lldb-libs-dir /home/ubuntu/code/llvm-project/build/./lib --llvm-tools-dir /home/ubuntu/code/llvm-project/build/./bin --lldb-obj-root /home/ubuntu/code/llvm-project/build/tools/lldb --cmake-build-type Debug -v -t -p TestTargetAPI.py -f test_get_arch_name_dwarf -f test_get_arch_name_dwo -f test_get_arch_name_dsym --arch aarch64 lldb/test/API/python_api/target
lldb version 22.0.0git (https://github.com/n2h9/fork-llvm-project revision 0dfb712313fa7689e936e572fde6ac99197806df)
  clang revision 0dfb712313fa7689e936e572fde6ac99197806df
  llvm revision 0dfb712313fa7689e936e572fde6ac99197806df
libc++ tests will not be run because: API tests require a locally built libc++.
msvcstl tests will not be run because: Don't know how to build with MSVC's STL on linux
objc tests will be skipped because of unsupported platform
Skipping the following test categories: ['libc++', 'msvcstl', 'dsym', 'pdb', 'gmodules', 'debugserver', 'objc']
adding filter spec TargetAPITestCase.test_get_arch_name_dwarf to module <module 'TestTargetAPI' from '/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target/TestTargetAPI.py'>
adding filter spec TargetAPITestCase.test_get_arch_name_dwo to module <module 'TestTargetAPI' from '/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target/TestTargetAPI.py'>
adding filter spec TargetAPITestCase.test_get_arch_name_dsym to module <module 'TestTargetAPI' from '/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target/TestTargetAPI.py'>
compiler=/home/ubuntu/code/llvm-project/build/bin/clang

Configuration: arch=aarch64 compiler=/home/ubuntu/code/llvm-project/build/bin/clang
----------------------------------------------------------------------
Collected 3 tests

Change dir to: /home/ubuntu/code/llvm-project/lldb/test/API/python_api/target
1: test_get_arch_name_dwarf (TestTargetAPI.TargetAPITestCase.test_get_arch_name_dwarf) ... runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

runCmd: settings set target.auto-apply-fixits false

output: 

runCmd: settings set plugin.process.gdb-remote.packet-timeout 60

output: 

runCmd: settings set symbols.clang-modules-cache-path "/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/module-cache-lldb"

output: 

runCmd: settings set use-color false

output: 

runCmd: settings set show-statusline false

output: 

/usr/bin/gmake VPATH=/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -C /home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/python_api/target/TestTargetAPI.test_get_arch_name_dwarf -I /home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -I /home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make -f /home/ubuntu/code/llvm-project/lldb/test/API/python_api/target/Makefile MAKE_DSYM=NO all ARCH=aarch64 CC=/home/ubuntu/code/llvm-project/build/bin/clang CC_TYPE=clang CXX=/home/ubuntu/code/llvm-project/build/bin/clang++ LLVM_AR=/home/ubuntu/code/llvm-project/build/./bin/llvm-ar AR=/home/ubuntu/code/llvm-project/build/./bin/llvm-ar OBJCOPY=/home/ubuntu/code/llvm-project/build/./bin/llvm-objcopy STRIP=/home/ubuntu/code/llvm-project/build/./bin/llvm-strip ARCHIVER=/home/ubuntu/code/llvm-project/build/./bin/llvm-ar DWP=/home/ubuntu/code/llvm-project/build/./bin/llvm-dwp CLANG_MODULE_CACHE_DIR=/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/module-cache-clang LLDB_OBJ_ROOT=/home/ubuntu/code/llvm-project/build/tools/lldb EXE=b.out arch=aarch64 OS=Linux HOST_OS=Linux

gmake: Entering directory '/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/python_api/target/TestTargetAPI.test_get_arch_name_dwarf'
/home/ubuntu/code/llvm-project/build/bin/clang -g -O0   -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/../../../../..//include -I/home/ubuntu/code/llvm-project/build/tools/lldb/include -I/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make -include /home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/test_common.h -fno-limit-debug-info  -D_DEFAULT_SOURCE  -MT main.o -MD -MP -MF main.d -c -o main.o /home/ubuntu/code/llvm-project/lldb/test/API/python_api/target/main.c
/home/ubuntu/code/llvm-project/build/bin/clang main.o -g -O0   -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/../../../../..//include -I/home/ubuntu/code/llvm-project/build/tools/lldb/include -I/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make -include /home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/test_common.h -fno-limit-debug-info  -D_DEFAULT_SOURCE     --driver-mode=g++ -o "b.out"
gmake: Leaving directory '/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/python_api/target/TestTargetAPI.test_get_arch_name_dwarf'


>>>>
target.arch_name = x86_64
<<<<
ok
PASS: LLDB (/home/ubuntu/code/llvm-project/build/bin/clang-aarch64) :: test_get_arch_name_dwarf (TestTargetAPI.TargetAPITestCase.test_get_arch_name_dwarf)
2: test_get_arch_name_dwo (TestTargetAPI.TargetAPITestCase.test_get_arch_name_dwo) ... runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

runCmd: settings set target.auto-apply-fixits false

output: 

runCmd: settings set plugin.process.gdb-remote.packet-timeout 60

output: 

runCmd: settings set symbols.clang-modules-cache-path "/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/module-cache-lldb"

output: 

runCmd: settings set use-color false

output: 

runCmd: settings set show-statusline false

output: 

/usr/bin/gmake VPATH=/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -C /home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/python_api/target/TestTargetAPI.test_get_arch_name_dwo -I /home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -I /home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make -f /home/ubuntu/code/llvm-project/lldb/test/API/python_api/target/Makefile MAKE_DSYM=NO MAKE_DWO=YES all ARCH=aarch64 CC=/home/ubuntu/code/llvm-project/build/bin/clang CC_TYPE=clang CXX=/home/ubuntu/code/llvm-project/build/bin/clang++ LLVM_AR=/home/ubuntu/code/llvm-project/build/./bin/llvm-ar AR=/home/ubuntu/code/llvm-project/build/./bin/llvm-ar OBJCOPY=/home/ubuntu/code/llvm-project/build/./bin/llvm-objcopy STRIP=/home/ubuntu/code/llvm-project/build/./bin/llvm-strip ARCHIVER=/home/ubuntu/code/llvm-project/build/./bin/llvm-ar DWP=/home/ubuntu/code/llvm-project/build/./bin/llvm-dwp CLANG_MODULE_CACHE_DIR=/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/module-cache-clang LLDB_OBJ_ROOT=/home/ubuntu/code/llvm-project/build/tools/lldb EXE=b.out arch=aarch64 OS=Linux HOST_OS=Linux

gmake: Entering directory '/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/python_api/target/TestTargetAPI.test_get_arch_name_dwo'
/home/ubuntu/code/llvm-project/build/bin/clang -g -O0   -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/../../../../..//include -I/home/ubuntu/code/llvm-project/build/tools/lldb/include -I/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make -include /home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/test_common.h -fno-limit-debug-info  -gsplit-dwarf -D_DEFAULT_SOURCE  -MT main.o -MD -MP -MF main.d -c -o main.o /home/ubuntu/code/llvm-project/lldb/test/API/python_api/target/main.c
/home/ubuntu/code/llvm-project/build/bin/clang main.o -g -O0   -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/../../../../..//include -I/home/ubuntu/code/llvm-project/build/tools/lldb/include -I/home/ubuntu/code/llvm-project/lldb/test/API/python_api/target -I/home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make -include /home/ubuntu/code/llvm-project/lldb/packages/Python/lldbsuite/test/make/test_common.h -fno-limit-debug-info  -gsplit-dwarf -D_DEFAULT_SOURCE     --driver-mode=g++ -o "b.out"
gmake: Leaving directory '/home/ubuntu/code/llvm-project/build/lldb-test-build.noindex/python_api/target/TestTargetAPI.test_get_arch_name_dwo'


>>>>
target.arch_name = x86_64
<<<<
ok
PASS: LLDB (/home/ubuntu/code/llvm-project/build/bin/clang-aarch64) :: test_get_arch_name_dwo (TestTargetAPI.TargetAPITestCase.test_get_arch_name_dwo)
3: test_get_arch_name_dsym (TestTargetAPI.TargetAPITestCase.test_get_arch_name_dsym) ... skipped 'test case does not fall in any category of interest for this run'
UNSUPPORTED: LLDB (/home/ubuntu/code/llvm-project/build/bin/clang-aarch64) :: test_get_arch_name_dsym (TestTargetAPI.TargetAPITestCase.test_get_arch_name_dsym) (test case does not fall in any category of interest for this run) 
Restore dir to: /home/ubuntu/code/llvm-project

----------------------------------------------------------------------
Ran 3 tests in 0.659s

OK (skipped=1)
Session logs for test failures/errors/unexpected successes can be found in the test build directory

n2h9 and others added 4 commits November 16, 2025 19:50
Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
…est to check exact arch name string, repalce check if triple exists with assertion

Signed-off-by: Nikita B <n2h9z4@gmail.com>
…ith darker

Signed-off-by: Nikita B <n2h9z4@gmail.com>
…est to check exact arch name string, use current arch

Signed-off-by: Nikita B <n2h9z4@gmail.com>
@n2h9 n2h9 requested a review from JDevlieghere November 16, 2025 19:55
…o check only that arch_name is eq to triple first element

Signed-off-by: Nikita B <n2h9z4@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an API that returns the target's architecture instead of hand-parsing it from the triple.

4 participants