Skip to content

Commit 0f5a95e

Browse files
committed
[lldb-dap] Treat empty thread names as unset
1 parent 5fc3e76 commit 0f5a95e

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed

lldb/test/API/tools/lldb-dap/threads/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
C_SOURCES := main.c
1+
CXX_SOURCES := main.cpp
22

33
ENABLE_THREADS := YES
44

lldb/test/API/tools/lldb-dap/threads/TestDAP_threads.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Test lldb-dap setBreakpoints request
2+
Test lldb-dap threads request
33
"""
44

55
from lldbsuite.test.decorators import *
@@ -9,7 +9,6 @@
99

1010

1111
class TestDAP_threads(lldbdap_testcase.DAPTestCaseBase):
12-
@skipIfWindows
1312
def test_correct_thread(self):
1413
"""
1514
Tests that the correct thread is selected if we continue from
@@ -19,7 +18,7 @@ def test_correct_thread(self):
1918
"""
2019
program = self.getBuildArtifact("a.out")
2120
self.build_and_launch(program)
22-
source = "main.c"
21+
source = "main.cpp"
2322
breakpoint_line = line_number(source, "// break here")
2423
lines = [breakpoint_line]
2524
# Set breakpoint in the thread function
@@ -42,8 +41,10 @@ def test_correct_thread(self):
4241
)
4342
self.assertFalse(stopped_event[0]["body"]["preserveFocusHint"])
4443
self.assertTrue(stopped_event[0]["body"]["threadCausedFocus"])
44+
# All threads should be named Thread {index}
45+
threads = self.dap_server.get_threads()
46+
self.assertTrue(all(len(t["name"]) > 0 for t in threads))
4547

46-
@skipIfWindows
4748
def test_thread_format(self):
4849
"""
4950
Tests the support for custom thread formats.
@@ -54,7 +55,7 @@ def test_thread_format(self):
5455
customThreadFormat="This is thread index #${thread.index}",
5556
stopCommands=["thread list"],
5657
)
57-
source = "main.c"
58+
source = "main.cpp"
5859
breakpoint_line = line_number(source, "// break here")
5960
lines = [breakpoint_line]
6061
# Set breakpoint in the thread function
@@ -63,8 +64,18 @@ def test_thread_format(self):
6364
len(breakpoint_ids), len(lines), "expect correct number of breakpoints"
6465
)
6566
self.continue_to_breakpoints(breakpoint_ids)
66-
# We are stopped at the second thread
67+
# We are stopped at the first thread
6768
threads = self.dap_server.get_threads()
6869
print("got thread", threads)
69-
self.assertEqual(threads[0]["name"], "This is thread index #1")
70-
self.assertEqual(threads[1]["name"], "This is thread index #2")
70+
if self.getPlatform() == "windows":
71+
# Windows creates a thread pool once WaitForSingleObject is called
72+
# by thread.join(). As we are in the thread function, we can't be
73+
# certain that join() has been called yet and a thread pool has
74+
# been created, thus we only check for the first two threads.
75+
names = list(sorted(t["name"] for t in threads))[:2]
76+
self.assertEqual(
77+
names, ["This is thread index #1", "This is thread index #2"]
78+
)
79+
else:
80+
self.assertEqual(threads[0]["name"], "This is thread index #1")
81+
self.assertEqual(threads[1]["name"], "This is thread index #2")

lldb/test/API/tools/lldb-dap/threads/main.c

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <cstdio>
2+
#include <thread>
3+
4+
int state_var;
5+
6+
void thread() {
7+
state_var++; // break here
8+
}
9+
10+
int main(int argc, char **argv) {
11+
std::thread t1(thread);
12+
t1.join();
13+
std::thread t2(thread);
14+
t2.join();
15+
16+
printf("state_var is %d\n", state_var);
17+
return 0;
18+
}

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,12 @@ llvm::json::Value CreateThread(lldb::SBThread &thread, lldb::SBFormat &format) {
764764
if (format && thread.GetDescriptionWithFormat(format, stream).Success()) {
765765
thread_str = stream.GetData();
766766
} else {
767-
const char *thread_name = thread.GetName();
768-
const char *queue_name = thread.GetQueueName();
767+
llvm::StringRef thread_name(thread.GetName());
768+
llvm::StringRef queue_name(thread.GetQueueName());
769769

770-
if (thread_name) {
771-
thread_str = std::string(thread_name);
772-
} else if (queue_name) {
770+
if (!thread_name.empty()) {
771+
thread_str = thread_name.str();
772+
} else if (!queue_name.empty()) {
773773
auto kind = thread.GetQueue().GetKind();
774774
std::string queue_kind_label = "";
775775
if (kind == lldb::eQueueKindSerial) {

0 commit comments

Comments
 (0)