https://github.com/Nerixyz updated https://github.com/llvm/llvm-project/pull/141529
>From 2a79486c4d45a8b4251ad32cd0629473c4ce828d Mon Sep 17 00:00:00 2001 From: Nerixyz <nerix...@outlook.de> Date: Mon, 26 May 2025 22:25:05 +0200 Subject: [PATCH] [lldb-dap] Treat empty thread names as unset --- lldb/test/API/tools/lldb-dap/threads/Makefile | 2 +- .../tools/lldb-dap/threads/TestDAP_threads.py | 27 ++++++++++++++----- lldb/test/API/tools/lldb-dap/threads/main.c | 21 --------------- lldb/test/API/tools/lldb-dap/threads/main.cpp | 18 +++++++++++++ lldb/tools/lldb-dap/JSONUtils.cpp | 10 +++---- 5 files changed, 44 insertions(+), 34 deletions(-) delete mode 100644 lldb/test/API/tools/lldb-dap/threads/main.c create mode 100644 lldb/test/API/tools/lldb-dap/threads/main.cpp diff --git a/lldb/test/API/tools/lldb-dap/threads/Makefile b/lldb/test/API/tools/lldb-dap/threads/Makefile index aa6b054685d61..c33ae5685efc7 100644 --- a/lldb/test/API/tools/lldb-dap/threads/Makefile +++ b/lldb/test/API/tools/lldb-dap/threads/Makefile @@ -1,4 +1,4 @@ -C_SOURCES := main.c +CXX_SOURCES := main.cpp ENABLE_THREADS := YES diff --git a/lldb/test/API/tools/lldb-dap/threads/TestDAP_threads.py b/lldb/test/API/tools/lldb-dap/threads/TestDAP_threads.py index a4658da58ac94..b9c8802144ebd 100644 --- a/lldb/test/API/tools/lldb-dap/threads/TestDAP_threads.py +++ b/lldb/test/API/tools/lldb-dap/threads/TestDAP_threads.py @@ -6,10 +6,10 @@ from lldbsuite.test.lldbtest import * from lldbsuite.test import lldbutil import lldbdap_testcase +import re class TestDAP_threads(lldbdap_testcase.DAPTestCaseBase): - @skipIfWindows def test_correct_thread(self): """ Tests that the correct thread is selected if we continue from @@ -19,7 +19,7 @@ def test_correct_thread(self): """ program = self.getBuildArtifact("a.out") self.build_and_launch(program) - source = "main.c" + source = "main.cpp" breakpoint_line = line_number(source, "// break here") lines = [breakpoint_line] # Set breakpoint in the thread function @@ -42,8 +42,10 @@ def test_correct_thread(self): ) self.assertFalse(stopped_event[0]["body"]["preserveFocusHint"]) self.assertTrue(stopped_event[0]["body"]["threadCausedFocus"]) + # All threads should be named Thread {index} + threads = self.dap_server.get_threads() + self.assertTrue(all(re.match(r'^Thread \d+$', t["name"]) for t in threads)) - @skipIfWindows def test_thread_format(self): """ Tests the support for custom thread formats. @@ -54,7 +56,7 @@ def test_thread_format(self): customThreadFormat="This is thread index #${thread.index}", stopCommands=["thread list"], ) - source = "main.c" + source = "main.cpp" breakpoint_line = line_number(source, "// break here") lines = [breakpoint_line] # Set breakpoint in the thread function @@ -63,8 +65,19 @@ def test_thread_format(self): len(breakpoint_ids), len(lines), "expect correct number of breakpoints" ) self.continue_to_breakpoints(breakpoint_ids) - # We are stopped at the second thread + # We are stopped at the first thread threads = self.dap_server.get_threads() print("got thread", threads) - self.assertEqual(threads[0]["name"], "This is thread index #1") - self.assertEqual(threads[1]["name"], "This is thread index #2") + if self.getPlatform() == "windows": + # Windows creates a thread pool once WaitForSingleObject is called + # by thread.join(). As we are in the thread function, we can't be + # certain that join() has been called yet and a thread pool has + # been created, thus we only check for the first two threads. + names = list(sorted(t["name"] for t in threads))[:2] + self.assertEqual(names, [ + "This is thread index #1", + "This is thread index #2" + ]) + else: + self.assertEqual(threads[0]["name"], "This is thread index #1") + self.assertEqual(threads[1]["name"], "This is thread index #2") diff --git a/lldb/test/API/tools/lldb-dap/threads/main.c b/lldb/test/API/tools/lldb-dap/threads/main.c deleted file mode 100644 index 3eeb1ef02cb87..0000000000000 --- a/lldb/test/API/tools/lldb-dap/threads/main.c +++ /dev/null @@ -1,21 +0,0 @@ -#include <pthread.h> -#include <stdio.h> - -int state_var; - -void *thread(void *in) { - state_var++; // break here - return NULL; -} - -int main(int argc, char **argv) { - pthread_t t1, t2; - - pthread_create(&t1, NULL, *thread, NULL); - pthread_join(t1, NULL); - pthread_create(&t2, NULL, *thread, NULL); - pthread_join(t2, NULL); - - printf("state_var is %d\n", state_var); - return 0; -} diff --git a/lldb/test/API/tools/lldb-dap/threads/main.cpp b/lldb/test/API/tools/lldb-dap/threads/main.cpp new file mode 100644 index 0000000000000..178dcb0b2f2ef --- /dev/null +++ b/lldb/test/API/tools/lldb-dap/threads/main.cpp @@ -0,0 +1,18 @@ +#include <cstdio> +#include <thread> + +int state_var; + +void thread() { + state_var++; // break here +} + +int main(int argc, char **argv) { + std::thread t1(thread); + t1.join(); + std::thread t2(thread); + t2.join(); + + printf("state_var is %d\n", state_var); + return 0; +} diff --git a/lldb/tools/lldb-dap/JSONUtils.cpp b/lldb/tools/lldb-dap/JSONUtils.cpp index c9c6f4554c325..f0b3dfb595717 100644 --- a/lldb/tools/lldb-dap/JSONUtils.cpp +++ b/lldb/tools/lldb-dap/JSONUtils.cpp @@ -764,12 +764,12 @@ llvm::json::Value CreateThread(lldb::SBThread &thread, lldb::SBFormat &format) { if (format && thread.GetDescriptionWithFormat(format, stream).Success()) { thread_str = stream.GetData(); } else { - const char *thread_name = thread.GetName(); - const char *queue_name = thread.GetQueueName(); + llvm::StringRef thread_name(thread.GetName()); + llvm::StringRef queue_name(thread.GetQueueName()); - if (thread_name) { - thread_str = std::string(thread_name); - } else if (queue_name) { + if (!thread_name.empty()) { + thread_str = thread_name.str(); + } else if (!queue_name.empty()) { auto kind = thread.GetQueue().GetKind(); std::string queue_kind_label = ""; if (kind == lldb::eQueueKindSerial) { _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits