1 //===-- BreakpointBase.cpp --------------------------------------*- 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 #include "BreakpointBase.h"
10 #include "VSCode.h"
11 #include "llvm/ADT/StringExtras.h"
12 
13 using namespace lldb_vscode;
14 
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"))) {}
19 
20 void BreakpointBase::SetCondition() { bp.SetCondition(condition.c_str()); }
21 
22 void BreakpointBase::SetHitCondition() {
23   uint64_t hitCount = 0;
24   if (llvm::to_integer(hitCondition, hitCount))
25     bp.SetIgnoreCount(hitCount - 1);
26 }
27 
28 lldb::SBError BreakpointBase::AppendLogMessagePart(llvm::StringRef part,
29                                                    bool is_expr) {
30   if (is_expr) {
31     logMessageParts.emplace_back(part, is_expr);
32   } else {
33     std::string formatted;
34     lldb::SBError error = FormatLogText(part, formatted);
35     if (error.Fail())
36       return error;
37     logMessageParts.emplace_back(formatted, is_expr);
38   }
39   return lldb::SBError();
40 }
41 
42 // TODO: consolidate this code with the implementation in
43 // FormatEntity::ParseInternal().
44 lldb::SBError BreakpointBase::FormatLogText(llvm::StringRef text,
45                                             std::string &formatted) {
46   lldb::SBError error;
47   while (!text.empty()) {
48     size_t backslash_pos = text.find_first_of('\\');
49     if (backslash_pos == std::string::npos) {
50       formatted += text.str();
51       return error;
52     }
53 
54     formatted += text.substr(0, backslash_pos).str();
55     // Skip the characters before and including '\'.
56     text = text.drop_front(backslash_pos + 1);
57 
58     if (text.empty()) {
59       error.SetErrorString(
60           "'\\' character was not followed by another character");
61       return error;
62     }
63 
64     const char desens_char = text[0];
65     text = text.drop_front(); // Skip the desensitized char character
66     switch (desens_char) {
67     case 'a':
68       formatted.push_back('\a');
69       break;
70     case 'b':
71       formatted.push_back('\b');
72       break;
73     case 'f':
74       formatted.push_back('\f');
75       break;
76     case 'n':
77       formatted.push_back('\n');
78       break;
79     case 'r':
80       formatted.push_back('\r');
81       break;
82     case 't':
83       formatted.push_back('\t');
84       break;
85     case 'v':
86       formatted.push_back('\v');
87       break;
88     case '\'':
89       formatted.push_back('\'');
90       break;
91     case '\\':
92       formatted.push_back('\\');
93       break;
94     case '0':
95       // 1 to 3 octal chars
96       {
97         if (text.empty()) {
98           error.SetErrorString("missing octal number following '\\0'");
99           return error;
100         }
101 
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};
105 
106         size_t i;
107         for (i = 0;
108              i < text.size() && i < 4 && (text[i] >= '0' && text[i] <= '7');
109              ++i) {
110           oct_str[i] = text[i];
111         }
112 
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);
117         } else {
118           error.SetErrorString("octal number is larger than a single byte");
119           return error;
120         }
121       }
122       break;
123 
124     case 'x': {
125       if (text.empty()) {
126         error.SetErrorString("missing hex number following '\\x'");
127         return error;
128       }
129       // hex number in the text
130       if (isxdigit(text[0])) {
131         // Make a string that can hold onto two hex chars plus a
132         // NULL terminator
133         char hex_str[3] = {0, 0, 0};
134         hex_str[0] = text[0];
135 
136         text = text.drop_front();
137 
138         if (!text.empty() && isxdigit(text[0])) {
139           hex_str[1] = text[0];
140           text = text.drop_front();
141         }
142 
143         unsigned long hex_value = strtoul(hex_str, nullptr, 16);
144         if (hex_value <= UINT8_MAX) {
145           formatted.push_back((char)hex_value);
146         } else {
147           error.SetErrorString("hex number is larger than a single byte");
148           return error;
149         }
150       } else {
151         formatted.push_back(desens_char);
152       }
153       break;
154     }
155 
156     default:
157       // Just desensitize any other character by just printing what came
158       // after the '\'
159       formatted.push_back(desens_char);
160       break;
161     }
162   }
163   return error;
164 }
165 
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
169 //    braces.
170 //
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();
175 
176   // Contains unmatched open curly braces indices.
177   std::vector<int> unmatched_curly_braces;
178 
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;
183 
184   lldb::SBError error;
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())
193         // Nothing to match.
194         continue;
195 
196       int last_unmatched_index = unmatched_curly_braces.back();
197       unmatched_curly_braces.pop_back();
198 
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)
205           break;
206 
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();
210       }
211 
212       // Assert invariant.
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);
216     }
217   }
218 
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,
232                           raw_text_len),
233           /*is_expr=*/false);
234       if (error.Fail()) {
235         NotifyLogMessageError(error.GetCString());
236         return;
237       }
238     }
239 
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,
245                         expr_len),
246         /*is_expr=*/true);
247     if (error.Fail()) {
248       NotifyLogMessageError(error.GetCString());
249       return;
250     }
251 
252     last_raw_text_start = curly_braces_range.second + 1;
253   }
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),
260         /*is_expr=*/false);
261     if (error.Fail()) {
262       NotifyLogMessageError(error.GetCString());
263       return;
264     }
265   }
266 
267   bp.SetCallback(BreakpointBase::BreakpointHitCallback, this);
268 }
269 
270 void BreakpointBase::NotifyLogMessageError(llvm::StringRef error) {
271   std::string message = "Log message has error: ";
272   message += error;
273   g_vsc.SendOutput(OutputType::Console, message);
274 }
275 
276 /*static*/
277 bool BreakpointBase::BreakpointHitCallback(
278     void *baton, lldb::SBProcess &process, lldb::SBThread &thread,
279     lldb::SBBreakpointLocation &location) {
280   if (!baton)
281     return true;
282 
283   BreakpointBase *bp = (BreakpointBase *)baton;
284   lldb::SBFrame frame = thread.GetSelectedFrame();
285 
286   std::string output;
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
291       // evaluation
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();
299       if (expr_val)
300         output += expr_val;
301     } else {
302       output += messagePart.text;
303     }
304   }
305   if (!output.empty() && output.back() != '\n')
306     output.push_back('\n'); // Ensure log message has line break.
307   g_vsc.SendOutput(OutputType::Console, output.c_str());
308 
309   // Do not stop.
310   return false;
311 }
312 
313 void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) {
314   if (condition != request_bp.condition) {
315     condition = request_bp.condition;
316     SetCondition();
317   }
318   if (hitCondition != request_bp.hitCondition) {
319     hitCondition = request_bp.hitCondition;
320     SetHitCondition();
321   }
322   if (logMessage != request_bp.logMessage) {
323     logMessage = request_bp.logMessage;
324     SetLogMessage();
325   }
326 }
327 
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 VS code 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.
341   return "vscode";
342 }
343