Skip to content

Commit 8d466e5

Browse files
committed
[lldb-dap] Updating the logging of lldb-dap to use existing LLDBLog.h helpers.
This only creates the basic types need to start using the LLDBLog.h helpers. Today, logging is handling by a simple `std::ofstream *` for handling logging. LLDBLog.h can help improve logging by adding new categories of logs and give us additional formatting support for log messages.
1 parent a3ac1f2 commit 8d466e5

File tree

9 files changed

+146
-102
lines changed

9 files changed

+146
-102
lines changed

lldb/tools/lldb-dap/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_lldb_tool(lldb-dap
2323
Breakpoint.cpp
2424
BreakpointBase.cpp
2525
DAP.cpp
26+
DAPLog.cpp
2627
EventHelper.cpp
2728
ExceptionBreakpoint.cpp
2829
FifoFiles.cpp

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "DAP.h"
10+
#include "DAPLog.h"
1011
#include "Handler/ResponseHandler.h"
1112
#include "JSONUtils.h"
1213
#include "LLDBUtils.h"
@@ -19,6 +20,7 @@
1920
#include "lldb/API/SBProcess.h"
2021
#include "lldb/API/SBStream.h"
2122
#include "lldb/Utility/IOObject.h"
23+
#include "lldb/Utility/Log.h"
2224
#include "lldb/Utility/Status.h"
2325
#include "lldb/lldb-defines.h"
2426
#include "lldb/lldb-enumerations.h"
@@ -50,6 +52,7 @@
5052
#endif
5153

5254
using namespace lldb_dap;
55+
using namespace lldb_private;
5356

5457
namespace {
5558
#ifdef _WIN32
@@ -61,13 +64,12 @@ const char DEV_NULL[] = "/dev/null";
6164

6265
namespace lldb_dap {
6366

64-
DAP::DAP(std::string name, llvm::StringRef path, std::ofstream *log,
65-
lldb::IOObjectSP input, lldb::IOObjectSP output, ReplMode repl_mode,
67+
DAP::DAP(std::string name, llvm::StringRef path, lldb::IOObjectSP input,
68+
lldb::IOObjectSP output, ReplMode repl_mode,
6669
std::vector<std::string> pre_init_commands)
67-
: name(std::move(name)), debug_adapter_path(path), log(log),
68-
input(std::move(input)), output(std::move(output)),
69-
broadcaster("lldb-dap"), exception_breakpoints(),
70-
pre_init_commands(std::move(pre_init_commands)),
70+
: name(std::move(name)), debug_adapter_path(path), input(std::move(input)),
71+
output(std::move(output)), broadcaster("lldb-dap"),
72+
exception_breakpoints(), pre_init_commands(std::move(pre_init_commands)),
7173
focus_tid(LLDB_INVALID_THREAD_ID), stop_at_entry(false), is_attach(false),
7274
enable_auto_variable_summaries(false),
7375
enable_synthetic_child_debugging(false),
@@ -245,6 +247,8 @@ void DAP::SendJSON(const std::string &json_str) {
245247
output.write_full(llvm::utostr(json_str.size()));
246248
output.write_full("\r\n\r\n");
247249
output.write_full(json_str);
250+
251+
LLDB_LOG(GetLog(DAPLog::Transport), "{0} <-- {1}", name, json_str);
248252
}
249253

250254
// Serialize the JSON value into a string and send the JSON packet to
@@ -256,15 +260,6 @@ void DAP::SendJSON(const llvm::json::Value &json) {
256260
static std::mutex mutex;
257261
std::lock_guard<std::mutex> locker(mutex);
258262
SendJSON(json_str);
259-
260-
if (log) {
261-
auto now = std::chrono::duration<double>(
262-
std::chrono::system_clock::now().time_since_epoch());
263-
*log << llvm::formatv("{0:f9} {1} <-- ", now.count(), name).str()
264-
<< std::endl
265-
<< "Content-Length: " << json_str.size() << "\r\n\r\n"
266-
<< llvm::formatv("{0:2}", json).str() << std::endl;
267-
}
268263
}
269264

270265
// Read a JSON packet from the "in" stream.
@@ -273,28 +268,22 @@ std::string DAP::ReadJSON() {
273268
std::string json_str;
274269
int length;
275270

276-
if (!input.read_expected(log, "Content-Length: "))
271+
if (!input.read_expected("Content-Length: "))
277272
return json_str;
278273

279-
if (!input.read_line(log, length_str))
274+
if (!input.read_line(length_str))
280275
return json_str;
281276

282277
if (!llvm::to_integer(length_str, length))
283278
return json_str;
284279

285-
if (!input.read_expected(log, "\r\n"))
280+
if (!input.read_expected("\r\n"))
286281
return json_str;
287282

288-
if (!input.read_full(log, length, json_str))
283+
if (!input.read_full(length, json_str))
289284
return json_str;
290285

291-
if (log) {
292-
auto now = std::chrono::duration<double>(
293-
std::chrono::system_clock::now().time_since_epoch());
294-
*log << llvm::formatv("{0:f9} {1} --> ", now.count(), name).str()
295-
<< std::endl
296-
<< "Content-Length: " << length << "\r\n\r\n";
297-
}
286+
LLDB_LOG(GetLog(DAPLog::Transport), "{0} --> {1}", name, json_str);
298287
return json_str;
299288
}
300289

@@ -729,24 +718,14 @@ PacketStatus DAP::GetNextObject(llvm::json::Object &object) {
729718
llvm::Expected<llvm::json::Value> json_value = llvm::json::parse(json_sref);
730719
if (!json_value) {
731720
auto error = json_value.takeError();
732-
if (log) {
733-
std::string error_str;
734-
llvm::raw_string_ostream strm(error_str);
735-
strm << error;
736-
*log << "error: failed to parse JSON: " << error_str << std::endl
737-
<< json << std::endl;
738-
}
721+
LLDB_LOG_ERROR(GetLog(DAPLog::Protocol), std::move(error),
722+
"failed to parse JSON: {0}");
739723
return PacketStatus::JSONMalformed;
740724
}
741725

742-
if (log) {
743-
*log << llvm::formatv("{0:2}", *json_value).str() << std::endl;
744-
}
745-
746726
llvm::json::Object *object_ptr = json_value->getAsObject();
747727
if (!object_ptr) {
748-
if (log)
749-
*log << "error: json packet isn't a object" << std::endl;
728+
LLDB_LOG(GetLog(DAPLog::Protocol), "error: json packet isn't a object");
750729
return PacketStatus::JSONNotObject;
751730
}
752731
object = *object_ptr;
@@ -764,9 +743,8 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
764743
return true; // Success
765744
}
766745

767-
if (log)
768-
*log << "error: unhandled command \"" << command.data() << "\""
769-
<< std::endl;
746+
LLDB_LOG(GetLog(DAPLog::Protocol), "error: unhandled command '{0}'",
747+
command);
770748
return false; // Fail
771749
}
772750

lldb/tools/lldb-dap/DAP.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,29 +125,28 @@ struct Variables {
125125

126126
struct StartDebuggingRequestHandler : public lldb::SBCommandPluginInterface {
127127
DAP &dap;
128-
explicit StartDebuggingRequestHandler(DAP &d) : dap(d) {};
128+
explicit StartDebuggingRequestHandler(DAP &d) : dap(d){};
129129
bool DoExecute(lldb::SBDebugger debugger, char **command,
130130
lldb::SBCommandReturnObject &result) override;
131131
};
132132

133133
struct ReplModeRequestHandler : public lldb::SBCommandPluginInterface {
134134
DAP &dap;
135-
explicit ReplModeRequestHandler(DAP &d) : dap(d) {};
135+
explicit ReplModeRequestHandler(DAP &d) : dap(d){};
136136
bool DoExecute(lldb::SBDebugger debugger, char **command,
137137
lldb::SBCommandReturnObject &result) override;
138138
};
139139

140140
struct SendEventRequestHandler : public lldb::SBCommandPluginInterface {
141141
DAP &dap;
142-
explicit SendEventRequestHandler(DAP &d) : dap(d) {};
142+
explicit SendEventRequestHandler(DAP &d) : dap(d){};
143143
bool DoExecute(lldb::SBDebugger debugger, char **command,
144144
lldb::SBCommandReturnObject &result) override;
145145
};
146146

147147
struct DAP {
148148
std::string name;
149149
llvm::StringRef debug_adapter_path;
150-
std::ofstream *log;
151150
InputStream input;
152151
OutputStream output;
153152
lldb::SBFile in;
@@ -210,8 +209,8 @@ struct DAP {
210209
// will contain that expression.
211210
std::string last_nonempty_var_expression;
212211

213-
DAP(std::string name, llvm::StringRef path, std::ofstream *log,
214-
lldb::IOObjectSP input, lldb::IOObjectSP output, ReplMode repl_mode,
212+
DAP(std::string name, llvm::StringRef path, lldb::IOObjectSP input,
213+
lldb::IOObjectSP output, ReplMode repl_mode,
215214
std::vector<std::string> pre_init_commands);
216215
~DAP();
217216
DAP(const DAP &rhs) = delete;

lldb/tools/lldb-dap/DAPLog.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "DAPLog.h"
2+
3+
using namespace lldb_private;
4+
using namespace lldb_dap;
5+
6+
static constexpr Log::Category g_categories[] = {
7+
{{"transport"}, {"log DAP transport"}, DAPLog::Transport},
8+
{{"protocol"}, {"log protocol handling"}, DAPLog::Protocol},
9+
{{"connection"}, {"log connection handling"}, DAPLog::Connection},
10+
};
11+
12+
static Log::Channel g_log_channel(g_categories, DAPLog::Transport |
13+
DAPLog::Protocol |
14+
DAPLog::Connection);
15+
16+
template <> Log::Channel &lldb_private::LogChannelFor<DAPLog>() {
17+
return g_log_channel;
18+
}
19+
20+
void lldb_dap::InitializeDAPChannel() {
21+
Log::Register("lldb-dap", g_log_channel);
22+
}

lldb/tools/lldb-dap/DAPLog.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- DAPLog.h ------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_UTILITY_LLDBLOG_H
10+
#define LLDB_UTILITY_LLDBLOG_H
11+
12+
#include "lldb/Utility/Log.h"
13+
#include "llvm/ADT/BitmaskEnum.h"
14+
15+
namespace lldb_dap {
16+
17+
enum class DAPLog : lldb_private::Log::MaskType {
18+
Transport = lldb_private::Log::ChannelFlag<0>,
19+
Protocol = lldb_private::Log::ChannelFlag<1>,
20+
Connection = lldb_private::Log::ChannelFlag<2>,
21+
LLVM_MARK_AS_BITMASK_ENUM(Connection),
22+
};
23+
24+
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
25+
26+
void InitializeDAPChannel();
27+
28+
} // end namespace lldb_dap
29+
30+
namespace lldb_private {
31+
template <> lldb_private::Log::Channel &LogChannelFor<lldb_dap::DAPLog>();
32+
} // namespace lldb_private
33+
34+
#endif

lldb/tools/lldb-dap/EventHelper.cpp

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

99
#include "EventHelper.h"
1010
#include "DAP.h"
11+
#include "DAPLog.h"
1112
#include "JSONUtils.h"
1213
#include "LLDBUtils.h"
1314
#include "lldb/API/SBFileSpec.h"
@@ -21,6 +22,9 @@
2122
#endif
2223
#endif
2324

25+
using namespace lldb_dap;
26+
using namespace lldb_private;
27+
2428
namespace lldb_dap {
2529

2630
static void SendThreadExitedEvent(DAP &dap, lldb::tid_t tid) {
@@ -178,15 +182,14 @@ void SendThreadStoppedEvent(DAP &dap) {
178182
SendThreadExitedEvent(dap, tid);
179183
}
180184
} else {
181-
if (dap.log)
182-
*dap.log << "error: SendThreadStoppedEvent() when process"
183-
" isn't stopped ("
184-
<< lldb::SBDebugger::StateAsCString(state) << ')' << std::endl;
185+
LLDB_LOG(GetLog(DAPLog::Protocol),
186+
"error: SendThreadStoppedEvent() when process"
187+
" isn't stopped ({0})",
188+
lldb::SBDebugger::StateAsCString(state));
185189
}
186190
} else {
187-
if (dap.log)
188-
*dap.log << "error: SendThreadStoppedEvent() invalid process"
189-
<< std::endl;
191+
LLDB_LOG(GetLog(DAPLog::Protocol),
192+
"error: SendThreadStoppedEvent() invalid process");
190193
}
191194
dap.RunStopCommands();
192195
}

lldb/tools/lldb-dap/IOStream.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "IOStream.h"
10+
#include "DAPLog.h"
1011
#include "lldb/Utility/IOObject.h"
1112
#include "lldb/Utility/Status.h"
12-
#include <fstream>
1313
#include <string>
1414

1515
using namespace lldb_dap;
16+
using namespace lldb_private;
1617

1718
bool OutputStream::write_full(llvm::StringRef str) {
1819
if (!descriptor)
@@ -23,8 +24,7 @@ bool OutputStream::write_full(llvm::StringRef str) {
2324
return status.Success();
2425
}
2526

26-
bool InputStream::read_full(std::ofstream *log, size_t length,
27-
std::string &text) {
27+
bool InputStream::read_full(size_t length, std::string &text) {
2828
if (!descriptor)
2929
return false;
3030

@@ -39,10 +39,10 @@ bool InputStream::read_full(std::ofstream *log, size_t length,
3939
return true;
4040
}
4141

42-
bool InputStream::read_line(std::ofstream *log, std::string &line) {
42+
bool InputStream::read_line(std::string &line) {
4343
line.clear();
4444
while (true) {
45-
if (!read_full(log, 1, line))
45+
if (!read_full(1, line))
4646
return false;
4747

4848
if (llvm::StringRef(line).ends_with("\r\n"))
@@ -52,14 +52,13 @@ bool InputStream::read_line(std::ofstream *log, std::string &line) {
5252
return true;
5353
}
5454

55-
bool InputStream::read_expected(std::ofstream *log, llvm::StringRef expected) {
55+
bool InputStream::read_expected(llvm::StringRef expected) {
5656
std::string result;
57-
if (!read_full(log, expected.size(), result))
57+
if (!read_full(expected.size(), result))
5858
return false;
5959
if (expected != result) {
60-
if (log)
61-
*log << "Warning: Expected '" << expected.str() << "', got '" << result
62-
<< "\n";
60+
LLDB_LOG(GetLog(DAPLog::Transport), "Warning: Expected '{0}', got '{1}'",
61+
expected, result);
6362
}
6463
return true;
6564
}

lldb/tools/lldb-dap/IOStream.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "lldb/lldb-forward.h"
1313
#include "llvm/ADT/StringRef.h"
14-
#include <fstream>
1514
#include <string>
1615

1716
namespace lldb_dap {
@@ -22,11 +21,11 @@ struct InputStream {
2221
explicit InputStream(lldb::IOObjectSP descriptor)
2322
: descriptor(std::move(descriptor)) {}
2423

25-
bool read_full(std::ofstream *log, size_t length, std::string &text);
24+
bool read_full(size_t length, std::string &text);
2625

27-
bool read_line(std::ofstream *log, std::string &line);
26+
bool read_line(std::string &line);
2827

29-
bool read_expected(std::ofstream *log, llvm::StringRef expected);
28+
bool read_expected(llvm::StringRef expected);
3029
};
3130

3231
struct OutputStream {

0 commit comments

Comments
 (0)