1 //===-- BreakpointBase.cpp --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "BreakpointBase.h"
11 #include "llvm/ADT/StringExtras.h"
13 using namespace lldb_dap
;
15 BreakpointBase::BreakpointBase(const llvm::json::Object
&obj
)
16 : condition(std::string(GetString(obj
, "condition"))),
17 hitCondition(std::string(GetString(obj
, "hitCondition"))),
18 logMessage(std::string(GetString(obj
, "logMessage"))) {}
20 void BreakpointBase::SetCondition() { bp
.SetCondition(condition
.c_str()); }
22 void BreakpointBase::SetHitCondition() {
23 uint64_t hitCount
= 0;
24 if (llvm::to_integer(hitCondition
, hitCount
))
25 bp
.SetIgnoreCount(hitCount
- 1);
28 lldb::SBError
BreakpointBase::AppendLogMessagePart(llvm::StringRef part
,
31 logMessageParts
.emplace_back(part
, is_expr
);
33 std::string formatted
;
34 lldb::SBError error
= FormatLogText(part
, formatted
);
37 logMessageParts
.emplace_back(formatted
, is_expr
);
39 return lldb::SBError();
42 // TODO: consolidate this code with the implementation in
43 // FormatEntity::ParseInternal().
44 lldb::SBError
BreakpointBase::FormatLogText(llvm::StringRef text
,
45 std::string
&formatted
) {
47 while (!text
.empty()) {
48 size_t backslash_pos
= text
.find_first_of('\\');
49 if (backslash_pos
== std::string::npos
) {
50 formatted
+= text
.str();
54 formatted
+= text
.substr(0, backslash_pos
).str();
55 // Skip the characters before and including '\'.
56 text
= text
.drop_front(backslash_pos
+ 1);
60 "'\\' character was not followed by another character");
64 const char desens_char
= text
[0];
65 text
= text
.drop_front(); // Skip the desensitized char character
66 switch (desens_char
) {
68 formatted
.push_back('\a');
71 formatted
.push_back('\b');
74 formatted
.push_back('\f');
77 formatted
.push_back('\n');
80 formatted
.push_back('\r');
83 formatted
.push_back('\t');
86 formatted
.push_back('\v');
89 formatted
.push_back('\'');
92 formatted
.push_back('\\');
98 error
.SetErrorString("missing octal number following '\\0'");
102 // Make a string that can hold onto the initial zero char, up to 3
103 // octal digits, and a terminating NULL.
104 char oct_str
[5] = {0, 0, 0, 0, 0};
108 i
< text
.size() && i
< 4 && (text
[i
] >= '0' && text
[i
] <= '7');
110 oct_str
[i
] = text
[i
];
113 text
= text
.drop_front(i
);
114 unsigned long octal_value
= ::strtoul(oct_str
, nullptr, 8);
115 if (octal_value
<= UINT8_MAX
) {
116 formatted
.push_back((char)octal_value
);
118 error
.SetErrorString("octal number is larger than a single byte");
126 error
.SetErrorString("missing hex number following '\\x'");
129 // hex number in the text
130 if (isxdigit(text
[0])) {
131 // Make a string that can hold onto two hex chars plus a
133 char hex_str
[3] = {0, 0, 0};
134 hex_str
[0] = text
[0];
136 text
= text
.drop_front();
138 if (!text
.empty() && isxdigit(text
[0])) {
139 hex_str
[1] = text
[0];
140 text
= text
.drop_front();
143 unsigned long hex_value
= strtoul(hex_str
, nullptr, 16);
144 if (hex_value
<= UINT8_MAX
) {
145 formatted
.push_back((char)hex_value
);
147 error
.SetErrorString("hex number is larger than a single byte");
151 formatted
.push_back(desens_char
);
157 // Just desensitize any other character by just printing what came
159 formatted
.push_back(desens_char
);
166 // logMessage will be divided into array of LogMessagePart as two kinds:
167 // 1. raw print text message, and
168 // 2. interpolated expression for evaluation which is inside matching curly
171 // The function tries to parse logMessage into a list of LogMessageParts
172 // for easy later access in BreakpointHitCallback.
173 void BreakpointBase::SetLogMessage() {
174 logMessageParts
.clear();
176 // Contains unmatched open curly braces indices.
177 std::vector
<int> unmatched_curly_braces
;
179 // Contains all matched curly braces in logMessage.
180 // Loop invariant: matched_curly_braces_ranges are sorted by start index in
181 // ascending order without any overlap between them.
182 std::vector
<std::pair
<int, int>> matched_curly_braces_ranges
;
185 // Part1 - parse matched_curly_braces_ranges.
186 // locating all curly braced expression ranges in logMessage.
187 // The algorithm takes care of nested and imbalanced curly braces.
188 for (size_t i
= 0; i
< logMessage
.size(); ++i
) {
189 if (logMessage
[i
] == '{') {
190 unmatched_curly_braces
.push_back(i
);
191 } else if (logMessage
[i
] == '}') {
192 if (unmatched_curly_braces
.empty())
196 int last_unmatched_index
= unmatched_curly_braces
.back();
197 unmatched_curly_braces
.pop_back();
199 // Erase any matched ranges included in the new match.
200 while (!matched_curly_braces_ranges
.empty()) {
201 assert(matched_curly_braces_ranges
.back().first
!=
202 last_unmatched_index
&&
203 "How can a curley brace be matched twice?");
204 if (matched_curly_braces_ranges
.back().first
< last_unmatched_index
)
207 // This is a nested range let's earse it.
208 assert((size_t)matched_curly_braces_ranges
.back().second
< i
);
209 matched_curly_braces_ranges
.pop_back();
213 assert(matched_curly_braces_ranges
.empty() ||
214 matched_curly_braces_ranges
.back().first
< last_unmatched_index
);
215 matched_curly_braces_ranges
.emplace_back(last_unmatched_index
, i
);
219 // Part2 - parse raw text and expresions parts.
220 // All expression ranges have been parsed in matched_curly_braces_ranges.
221 // The code below uses matched_curly_braces_ranges to divide logMessage
222 // into raw text parts and expression parts.
223 int last_raw_text_start
= 0;
224 for (const std::pair
<int, int> &curly_braces_range
:
225 matched_curly_braces_ranges
) {
226 // Raw text before open curly brace.
227 assert(curly_braces_range
.first
>= last_raw_text_start
);
228 size_t raw_text_len
= curly_braces_range
.first
- last_raw_text_start
;
229 if (raw_text_len
> 0) {
230 error
= AppendLogMessagePart(
231 llvm::StringRef(logMessage
.c_str() + last_raw_text_start
,
235 NotifyLogMessageError(error
.GetCString());
240 // Expression between curly braces.
241 assert(curly_braces_range
.second
> curly_braces_range
.first
);
242 size_t expr_len
= curly_braces_range
.second
- curly_braces_range
.first
- 1;
243 error
= AppendLogMessagePart(
244 llvm::StringRef(logMessage
.c_str() + curly_braces_range
.first
+ 1,
248 NotifyLogMessageError(error
.GetCString());
252 last_raw_text_start
= curly_braces_range
.second
+ 1;
254 // Trailing raw text after close curly brace.
255 assert(last_raw_text_start
>= 0);
256 if (logMessage
.size() > (size_t)last_raw_text_start
) {
257 error
= AppendLogMessagePart(
258 llvm::StringRef(logMessage
.c_str() + last_raw_text_start
,
259 logMessage
.size() - last_raw_text_start
),
262 NotifyLogMessageError(error
.GetCString());
267 bp
.SetCallback(BreakpointBase::BreakpointHitCallback
, this);
270 void BreakpointBase::NotifyLogMessageError(llvm::StringRef error
) {
271 std::string message
= "Log message has error: ";
273 g_dap
.SendOutput(OutputType::Console
, message
);
277 bool BreakpointBase::BreakpointHitCallback(
278 void *baton
, lldb::SBProcess
&process
, lldb::SBThread
&thread
,
279 lldb::SBBreakpointLocation
&location
) {
283 BreakpointBase
*bp
= (BreakpointBase
*)baton
;
284 lldb::SBFrame frame
= thread
.GetSelectedFrame();
287 for (const BreakpointBase::LogMessagePart
&messagePart
:
288 bp
->logMessageParts
) {
289 if (messagePart
.is_expr
) {
290 // Try local frame variables first before fall back to expression
292 const std::string
&expr_str
= messagePart
.text
;
293 const char *expr
= expr_str
.c_str();
294 lldb::SBValue value
=
295 frame
.GetValueForVariablePath(expr
, lldb::eDynamicDontRunTarget
);
296 if (value
.GetError().Fail())
297 value
= frame
.EvaluateExpression(expr
);
298 const char *expr_val
= value
.GetValue();
302 output
+= messagePart
.text
;
305 if (!output
.empty() && output
.back() != '\n')
306 output
.push_back('\n'); // Ensure log message has line break.
307 g_dap
.SendOutput(OutputType::Console
, output
.c_str());
313 void BreakpointBase::UpdateBreakpoint(const BreakpointBase
&request_bp
) {
314 if (condition
!= request_bp
.condition
) {
315 condition
= request_bp
.condition
;
318 if (hitCondition
!= request_bp
.hitCondition
) {
319 hitCondition
= request_bp
.hitCondition
;
322 if (logMessage
!= request_bp
.logMessage
) {
323 logMessage
= request_bp
.logMessage
;
328 const char *BreakpointBase::GetBreakpointLabel() {
329 // Breakpoints in LLDB can have names added to them which are kind of like
330 // labels or categories. All breakpoints that are set through the IDE UI get
331 // sent through the various DAP set*Breakpoint packets, and these
332 // breakpoints will be labeled with this name so if breakpoint update events
333 // come in for breakpoints that the IDE doesn't know about, like if a
334 // breakpoint is set manually using the debugger console, we won't report any
335 // updates on them and confused the IDE. This function gets called by all of
336 // the breakpoint classes after they set breakpoints to mark a breakpoint as
337 // a UI breakpoint. We can later check a lldb::SBBreakpoint object that comes
338 // in via LLDB breakpoint changed events and check the breakpoint by calling
339 // "bool lldb::SBBreakpoint::MatchesName(const char *)" to check if a
340 // breakpoint in one of the UI breakpoints that we should report changes for.