Skip to content

Commit 2f597d5

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

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from lldbsuite.test.lldbtest import *
77
from lldbsuite.test import lldbutil
88
import lldbdap_testcase
9+
import re
910

1011

1112
class TestDAP_threads(lldbdap_testcase.DAPTestCaseBase):
12-
@skipIfWindows
1313
def test_correct_thread(self):
1414
"""
1515
Tests that the correct thread is selected if we continue from
@@ -19,7 +19,7 @@ def test_correct_thread(self):
1919
"""
2020
program = self.getBuildArtifact("a.out")
2121
self.build_and_launch(program)
22-
source = "main.c"
22+
source = "main.cpp"
2323
breakpoint_line = line_number(source, "// break here")
2424
lines = [breakpoint_line]
2525
# Set breakpoint in the thread function
@@ -42,8 +42,10 @@ def test_correct_thread(self):
4242
)
4343
self.assertFalse(stopped_event[0]["body"]["preserveFocusHint"])
4444
self.assertTrue(stopped_event[0]["body"]["threadCausedFocus"])
45+
# All threads should be named Thread {index}
46+
threads = self.dap_server.get_threads()
47+
self.assertTrue(all(re.match(r"^Thread \d+$", t["name"]) for t in threads))
4548

46-
@skipIfWindows
4749
def test_thread_format(self):
4850
"""
4951
Tests the support for custom thread formats.
@@ -54,7 +56,7 @@ def test_thread_format(self):
5456
customThreadFormat="This is thread index #${thread.index}",
5557
stopCommands=["thread list"],
5658
)
57-
source = "main.c"
59+
source = "main.cpp"
5860
breakpoint_line = line_number(source, "// break here")
5961
lines = [breakpoint_line]
6062
# Set breakpoint in the thread function
@@ -63,8 +65,18 @@ def test_thread_format(self):
6365
len(breakpoint_ids), len(lines), "expect correct number of breakpoints"
6466
)
6567
self.continue_to_breakpoints(breakpoint_ids)
66-
# We are stopped at the second thread
68+
# We are stopped at the first thread
6769
threads = self.dap_server.get_threads()
6870
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")
71+
if self.getPlatform() == "windows":
72+
# Windows creates a thread pool once WaitForSingleObject is called
73+
# by thread.join(). As we are in the thread function, we can't be
74+
# certain that join() has been called yet and a thread pool has
75+
# been created, thus we only check for the first two threads.
76+
names = list(sorted(t["name"] for t in threads))[:2]
77+
self.assertEqual(
78+
names, ["This is thread index #1", "This is thread index #2"]
79+
)
80+
else:
81+
self.assertEqual(threads[0]["name"], "This is thread index #1")
82+
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)