Skip to content

Commit 0c2054b

Browse files
committed
Addressing reviewer feedback and fixing client_log having the incorrect scope when lldb-dap is running in server mode on accept.
1 parent ab5464a commit 0c2054b

File tree

7 files changed

+29
-20
lines changed

7 files changed

+29
-20
lines changed

lldb/tools/lldb-dap/DAP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "Transport.h"
2424
#include "Variables.h"
2525
#include "lldb/API/SBBroadcaster.h"
26+
#include "lldb/API/SBCommandInterpreter.h"
2627
#include "lldb/API/SBDebugger.h"
2728
#include "lldb/API/SBError.h"
2829
#include "lldb/API/SBFile.h"

lldb/tools/lldb-dap/DAPLog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace lldb_dap {
4141
/// `DAP_LOG_ERROR` helpers.
4242
class Log final {
4343
public:
44-
using Mutex = std::recursive_mutex;
44+
using Mutex = std::mutex;
4545

4646
Log(llvm::raw_ostream &stream, Mutex &mutex)
4747
: m_stream(stream), m_mutex(mutex) {}
@@ -51,7 +51,7 @@ class Log final {
5151
/// Retuns a new Log instance with the associated prefix for all messages.
5252
inline Log WithPrefix(llvm::StringRef prefix) const {
5353
std::string full_prefix =
54-
m_prefix.empty() ? prefix.str() : m_prefix + " " + prefix.str();
54+
m_prefix.empty() ? prefix.str() : m_prefix + prefix.str();
5555
full_prefix += " ";
5656
return Log(full_prefix, *this);
5757
}

lldb/tools/lldb-dap/EventHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ static void HandleTargetEvent(const lldb::SBEvent &event, Log &log) {
480480
}
481481
}
482482

483-
void HandleBreakpointEvent(const lldb::SBEvent &event, Log &log) {
483+
static void HandleBreakpointEvent(const lldb::SBEvent &event, Log &log) {
484484
const uint32_t event_mask = event.GetType();
485485
if (!(event_mask & lldb::SBTarget::eBroadcastBitBreakpointChanged))
486486
return;

lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "DAP.h"
10+
#include "JSONUtils.h"
1011
#include "Protocol/ProtocolRequests.h"
1112
#include "Protocol/ProtocolTypes.h"
1213
#include "RequestHandler.h"
1314
#include "lldb/API/SBStringList.h"
14-
#include "lldb/SBCommandInterpreter.h"
1515

1616
using namespace llvm;
1717
using namespace lldb_dap;

lldb/tools/lldb-dap/tool/lldb-dap.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,16 @@ static llvm::Error serveConnection(
453453
ResetConnectionTimeout(g_connection_timeout_mutex,
454454
g_connection_timeout_time_point);
455455
std::string client_name = llvm::formatv("client_{0}", clientCount++).str();
456-
Log client_log = log.WithPrefix("(" + client_name + ")");
457-
DAP_LOG(client_log, "client connected");
458-
459456
lldb::IOObjectSP io(std::move(sock));
460457

461458
// Move the client into a background thread to unblock accepting the next
462459
// client.
463-
std::thread client([=, &client_log]() {
460+
std::thread client([=, &log]() {
464461
llvm::set_thread_name(client_name + ".runloop");
462+
463+
Log client_log = log.WithPrefix("(" + client_name + ")");
464+
DAP_LOG(client_log, "client connected");
465+
465466
MainLoop loop;
466467
Transport transport(client_log, io, io);
467468
DAP dap(client_log, default_repl_mode, pre_init_commands, no_lldbinit,
@@ -652,7 +653,7 @@ int main(int argc, char *argv[]) {
652653
log_os = std::make_unique<llvm::raw_fd_ostream>(FD, /*shouldClose=*/true);
653654
}
654655
Log::Mutex mutex;
655-
Log log(log_os ? *log_os.get() : llvm::nulls(), mutex);
656+
Log log(log_os ? *log_os : llvm::nulls(), mutex);
656657

657658
// Initialize LLDB first before we do anything.
658659
lldb::SBError error = lldb::SBDebugger::InitializeWithErrorHandling();

lldb/unittests/DAP/DAPLogTest.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
#include "DAPLog.h"
1010
#include "llvm/Support/raw_ostream.h"
11+
#include "gmock/gmock.h"
1112
#include "gtest/gtest.h"
1213

1314
using namespace lldb_dap;
1415
using namespace llvm;
16+
using namespace testing;
1517

1618
static llvm::StringRef last_line(llvm::StringRef str) {
1719
size_t index = str.find_last_of('\n', str.size() - 1);
@@ -20,27 +22,32 @@ static llvm::StringRef last_line(llvm::StringRef str) {
2022
return str.substr(index + 1);
2123
}
2224

25+
#define TIMESTAMP_PATTERN "[0-9]+\\.[0-9]+ "
26+
2327
TEST(DAPLog, Emit) {
2428
Log::Mutex mux;
2529
std::string outs;
2630
raw_string_ostream os(outs);
2731
Log log(os, mux);
2832
Log inner_log = log.WithPrefix("my_prefix:");
2933

30-
// Line includes a timestamp, only check the suffix.
3134
log.Emit("Hi");
32-
EXPECT_TRUE(last_line(outs).ends_with(" Hi\n")) << outs;
35+
EXPECT_THAT(last_line(outs), MatchesRegex(TIMESTAMP_PATTERN "Hi\n"));
3336

3437
inner_log.Emit("foobar");
35-
EXPECT_TRUE(last_line(outs).ends_with(" my_prefix: foobar\n")) << outs;
38+
EXPECT_THAT(last_line(outs),
39+
MatchesRegex(TIMESTAMP_PATTERN "my_prefix: foobar\n"));
3640

3741
log.Emit("file.cpp", 42, "Hello from a file/line.");
38-
EXPECT_TRUE(
39-
last_line(outs).ends_with(" file.cpp:42 Hello from a file/line.\n"))
40-
<< outs;
42+
EXPECT_THAT(
43+
last_line(outs),
44+
MatchesRegex(TIMESTAMP_PATTERN "file.cpp:42 Hello from a file/line.\n"));
4145

4246
inner_log.Emit("file.cpp", 42, "Hello from a file/line.");
43-
EXPECT_TRUE(last_line(outs).ends_with(
44-
" file.cpp:42 my_prefix: Hello from a file/line.\n"))
45-
<< outs;
46-
}
47+
EXPECT_THAT(last_line(outs),
48+
MatchesRegex(TIMESTAMP_PATTERN
49+
"file.cpp:42 my_prefix: Hello from a file/line.\n"));
50+
51+
log.WithPrefix("a").WithPrefix("b").WithPrefix("c").Emit("msg");
52+
EXPECT_THAT(last_line(outs), MatchesRegex(TIMESTAMP_PATTERN "a b c msg\n"));
53+
}

lldb/unittests/DAP/TestBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void TransportBase::SetUp() {
3939

4040
log = std::make_unique<Log>(llvm::outs(), log_mutex);
4141
dap = std::make_unique<DAP>(
42-
/*log=*/*log.get(),
42+
/*log=*/*log,
4343
/*default_repl_mode=*/ReplMode::Auto,
4444
/*pre_init_commands=*/std::vector<std::string>(),
4545
/*no_lldbinit=*/false,

0 commit comments

Comments
 (0)