Skip to content

Commit f175b96

Browse files
jeffreytan81jeffreytan81
andauthored
Improve VSCode DAP logpoint value summary (#71723)
Currently VSCode logpoint uses `SBValue::GetValue` to get the value for printing. This is not providing an intuitive result for std::string or char * -- it shows the pointer value instead of the string content. This patch improves by prefers `SBValue::GetSummary()` before using `SBValue::GetValue()`. --------- Co-authored-by: jeffreytan81 <[email protected]>
1 parent 4c9f6d6 commit f175b96

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

lldb/test/API/tools/lldb-dap/breakpoint/TestDAP_logpoints.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_logmessage_basic(self):
4848
# 1. First at the loop line with logMessage
4949
# 2. Second guard breakpoint at a line after loop
5050
logMessage_prefix = "This is log message for { -- "
51-
logMessage = logMessage_prefix + "{i + 3}"
51+
logMessage = logMessage_prefix + "{i + 3}, {message}"
5252
[loop_breakpoint_id, post_loop_breakpoint_id] = self.set_source_breakpoints(
5353
self.main_path,
5454
[loop_line, after_loop_line],
@@ -72,10 +72,13 @@ def test_logmessage_basic(self):
7272
loop_count = 10
7373
self.assertEqual(len(logMessage_output), loop_count)
7474

75+
message_addr_pattern = r"\b0x[0-9A-Fa-f]+\b"
76+
message_content = '"Hello from main!"'
7577
# Verify log message match
7678
for idx, logMessage_line in enumerate(logMessage_output):
7779
result = idx + 3
78-
self.assertEqual(logMessage_line, logMessage_prefix + str(result))
80+
reg_str = f"{logMessage_prefix}{result}, {message_addr_pattern} {message_content}"
81+
self.assertRegex(logMessage_line, reg_str)
7982

8083
@skipIfWindows
8184
@skipIfRemote

lldb/test/API/tools/lldb-dap/breakpoint/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int main(int argc, char const *argv[]) {
2828
exit(1);
2929
}
3030

31+
const char *message = "Hello from main!";
3132
int (*foo)(int) = (int (*)(int))dlsym(handle, "foo");
3233
if (foo == nullptr) {
3334
fprintf(stderr, "%s\n", dlerror());
@@ -38,6 +39,7 @@ int main(int argc, char const *argv[]) {
3839
for (int i = 0; i < 10; ++i) {
3940
int x = twelve(i) + thirteen(i) + a::fourteen(i); // break loop
4041
}
42+
printf("%s\n", message);
4143
try {
4244
throw std::invalid_argument("throwing exception for testing");
4345
} catch (...) {

lldb/tools/lldb-dap/BreakpointBase.cpp

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

99
#include "BreakpointBase.h"
1010
#include "DAP.h"
11+
#include "JSONUtils.h"
1112
#include "llvm/ADT/StringExtras.h"
1213

1314
using namespace lldb_dap;
@@ -295,9 +296,7 @@ bool BreakpointBase::BreakpointHitCallback(
295296
frame.GetValueForVariablePath(expr, lldb::eDynamicDontRunTarget);
296297
if (value.GetError().Fail())
297298
value = frame.EvaluateExpression(expr);
298-
const char *expr_val = value.GetValue();
299-
if (expr_val)
300-
output += expr_val;
299+
output += ValueToString(value);
301300
} else {
302301
output += messagePart.text;
303302
}

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ static std::optional<std::string> TryCreateAutoSummary(lldb::SBValue value) {
210210
return TryCreateAutoSummaryForContainer(value);
211211
}
212212

213-
void SetValueForKey(lldb::SBValue &v, llvm::json::Object &object,
214-
llvm::StringRef key) {
213+
std::string ValueToString(lldb::SBValue v) {
215214
std::string result;
216215
llvm::raw_string_ostream strm(result);
217216

@@ -242,6 +241,12 @@ void SetValueForKey(lldb::SBValue &v, llvm::json::Object &object,
242241
}
243242
}
244243
}
244+
return result;
245+
}
246+
247+
void SetValueForKey(lldb::SBValue &v, llvm::json::Object &object,
248+
llvm::StringRef key) {
249+
std::string result = ValueToString(v);
245250
EmplaceSafeString(object, key, result);
246251
}
247252

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ std::vector<std::string> GetStrings(const llvm::json::Object *obj,
167167
void FillResponse(const llvm::json::Object &request,
168168
llvm::json::Object &response);
169169

170+
/// Utility function to convert SBValue \v into a string.
171+
std::string ValueToString(lldb::SBValue v);
172+
170173
/// Emplace the string value from an SBValue into the supplied object
171174
/// using \a key as the key that will contain the value.
172175
///

0 commit comments

Comments
 (0)