1//===-- SourceBreakpoint.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 "SourceBreakpoint.h"
10#include "BreakpointBase.h"
11#include "DAP.h"
12#include "JSONUtils.h"
13#include "lldb/API/SBBreakpoint.h"
14#include "lldb/API/SBFileSpecList.h"
15#include "lldb/API/SBFrame.h"
16#include "lldb/API/SBInstruction.h"
17#include "lldb/API/SBMutex.h"
18#include "lldb/API/SBSymbol.h"
19#include "lldb/API/SBTarget.h"
20#include "lldb/API/SBThread.h"
21#include "lldb/API/SBValue.h"
22#include "lldb/lldb-enumerations.h"
23#include "llvm/Support/Error.h"
24#include <cassert>
25#include <cctype>
26#include <cstdlib>
27#include <mutex>
28#include <utility>
29
30namespace lldb_dap {
31
32SourceBreakpoint::SourceBreakpoint(DAP &dap,
33 const protocol::SourceBreakpoint &breakpoint)
34 : Breakpoint(dap, breakpoint.condition, breakpoint.hitCondition),
35 m_log_message(breakpoint.logMessage.value_or(u: "")),
36 m_line(breakpoint.line),
37 m_column(breakpoint.column.value_or(LLDB_INVALID_COLUMN_NUMBER)) {}
38
39llvm::Error SourceBreakpoint::SetBreakpoint(const protocol::Source &source) {
40 lldb::SBMutex lock = m_dap.GetAPIMutex();
41 std::lock_guard<lldb::SBMutex> guard(lock);
42
43 if (m_line == 0)
44 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
45 S: "Invalid line number.");
46
47 if (source.sourceReference) {
48 // Breakpoint set by assembly source.
49 std::optional<lldb::addr_t> raw_addr =
50 m_dap.GetSourceReferenceAddress(reference: *source.sourceReference);
51 if (!raw_addr)
52 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
53 S: "Invalid sourceReference.");
54
55 lldb::SBAddress source_address(*raw_addr, m_dap.target);
56 if (!source_address.IsValid())
57 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
58 S: "Invalid sourceReference.");
59
60 lldb::SBSymbol symbol = source_address.GetSymbol();
61 if (!symbol.IsValid()) {
62 // FIXME: Support assembly breakpoints without a valid symbol.
63 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
64 S: "Breakpoints in assembly without a valid "
65 "symbol are not supported yet.");
66 }
67
68 lldb::SBInstructionList inst_list =
69 m_dap.target.ReadInstructions(base_addr: symbol.GetStartAddress(), count: m_line);
70 if (inst_list.GetSize() < m_line)
71 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
72 S: "Invalid instruction list size.");
73
74 lldb::SBAddress address =
75 inst_list.GetInstructionAtIndex(idx: m_line - 1).GetAddress();
76
77 m_bp = m_dap.target.BreakpointCreateBySBAddress(address);
78 } else {
79 // Breakpoint set by a regular source file.
80 const auto source_path = source.path.value_or(u: "");
81 lldb::SBFileSpecList module_list;
82 m_bp = m_dap.target.BreakpointCreateByLocation(file_spec: source_path.c_str(), line: m_line,
83 column: m_column, offset: 0, module_list);
84 }
85
86 if (!m_log_message.empty())
87 SetLogMessage();
88 Breakpoint::SetBreakpoint();
89 return llvm::Error::success();
90}
91
92void SourceBreakpoint::UpdateBreakpoint(const SourceBreakpoint &request_bp) {
93 if (m_log_message != request_bp.m_log_message) {
94 m_log_message = request_bp.m_log_message;
95 SetLogMessage();
96 }
97 BreakpointBase::UpdateBreakpoint(request_bp);
98}
99
100lldb::SBError SourceBreakpoint::AppendLogMessagePart(llvm::StringRef part,
101 bool is_expr) {
102 if (is_expr) {
103 m_log_message_parts.emplace_back(args&: part, args&: is_expr);
104 } else {
105 std::string formatted;
106 lldb::SBError error = FormatLogText(text: part, formatted);
107 if (error.Fail())
108 return error;
109 m_log_message_parts.emplace_back(args&: formatted, args&: is_expr);
110 }
111 return lldb::SBError();
112}
113
114// TODO: consolidate this code with the implementation in
115// FormatEntity::ParseInternal().
116lldb::SBError SourceBreakpoint::FormatLogText(llvm::StringRef text,
117 std::string &formatted) {
118 lldb::SBError error;
119 while (!text.empty()) {
120 size_t backslash_pos = text.find_first_of(C: '\\');
121 if (backslash_pos == std::string::npos) {
122 formatted += text.str();
123 return error;
124 }
125
126 formatted += text.substr(Start: 0, N: backslash_pos).str();
127 // Skip the characters before and including '\'.
128 text = text.drop_front(N: backslash_pos + 1);
129
130 if (text.empty()) {
131 error.SetErrorString(
132 "'\\' character was not followed by another character");
133 return error;
134 }
135
136 const char desens_char = text[0];
137 text = text.drop_front(); // Skip the desensitized char character
138 switch (desens_char) {
139 case 'a':
140 formatted.push_back(c: '\a');
141 break;
142 case 'b':
143 formatted.push_back(c: '\b');
144 break;
145 case 'f':
146 formatted.push_back(c: '\f');
147 break;
148 case 'n':
149 formatted.push_back(c: '\n');
150 break;
151 case 'r':
152 formatted.push_back(c: '\r');
153 break;
154 case 't':
155 formatted.push_back(c: '\t');
156 break;
157 case 'v':
158 formatted.push_back(c: '\v');
159 break;
160 case '\'':
161 formatted.push_back(c: '\'');
162 break;
163 case '\\':
164 formatted.push_back(c: '\\');
165 break;
166 case '0':
167 // 1 to 3 octal chars
168 {
169 if (text.empty()) {
170 error.SetErrorString("missing octal number following '\\0'");
171 return error;
172 }
173
174 // Make a string that can hold onto the initial zero char, up to 3
175 // octal digits, and a terminating NULL.
176 char oct_str[5] = {0, 0, 0, 0, 0};
177
178 size_t i;
179 for (i = 0;
180 i < text.size() && i < 4 && (text[i] >= '0' && text[i] <= '7');
181 ++i) {
182 oct_str[i] = text[i];
183 }
184
185 text = text.drop_front(N: i);
186 unsigned long octal_value = ::strtoul(nptr: oct_str, endptr: nullptr, base: 8);
187 if (octal_value <= UINT8_MAX) {
188 formatted.push_back(c: (char)octal_value);
189 } else {
190 error.SetErrorString("octal number is larger than a single byte");
191 return error;
192 }
193 }
194 break;
195
196 case 'x': {
197 if (text.empty()) {
198 error.SetErrorString("missing hex number following '\\x'");
199 return error;
200 }
201 // hex number in the text
202 if (std::isxdigit(text[0])) {
203 // Make a string that can hold onto two hex chars plus a
204 // NULL terminator
205 char hex_str[3] = {0, 0, 0};
206 hex_str[0] = text[0];
207
208 text = text.drop_front();
209
210 if (!text.empty() && std::isxdigit(text[0])) {
211 hex_str[1] = text[0];
212 text = text.drop_front();
213 }
214
215 unsigned long hex_value = strtoul(nptr: hex_str, endptr: nullptr, base: 16);
216 if (hex_value <= UINT8_MAX) {
217 formatted.push_back(c: (char)hex_value);
218 } else {
219 error.SetErrorString("hex number is larger than a single byte");
220 return error;
221 }
222 } else {
223 formatted.push_back(c: desens_char);
224 }
225 break;
226 }
227
228 default:
229 // Just desensitize any other character by just printing what came
230 // after the '\'
231 formatted.push_back(c: desens_char);
232 break;
233 }
234 }
235 return error;
236}
237
238// logMessage will be divided into array of LogMessagePart as two kinds:
239// 1. raw print text message, and
240// 2. interpolated expression for evaluation which is inside matching curly
241// braces.
242//
243// The function tries to parse logMessage into a list of LogMessageParts
244// for easy later access in BreakpointHitCallback.
245void SourceBreakpoint::SetLogMessage() {
246 m_log_message_parts.clear();
247
248 // Contains unmatched open curly braces indices.
249 std::vector<int> unmatched_curly_braces;
250
251 // Contains all matched curly braces in logMessage.
252 // Loop invariant: matched_curly_braces_ranges are sorted by start index in
253 // ascending order without any overlap between them.
254 std::vector<std::pair<int, int>> matched_curly_braces_ranges;
255
256 lldb::SBError error;
257 // Part1 - parse matched_curly_braces_ranges.
258 // locating all curly braced expression ranges in logMessage.
259 // The algorithm takes care of nested and imbalanced curly braces.
260 for (size_t i = 0; i < m_log_message.size(); ++i) {
261 if (m_log_message[i] == '{') {
262 unmatched_curly_braces.push_back(x: i);
263 } else if (m_log_message[i] == '}') {
264 if (unmatched_curly_braces.empty())
265 // Nothing to match.
266 continue;
267
268 int last_unmatched_index = unmatched_curly_braces.back();
269 unmatched_curly_braces.pop_back();
270
271 // Erase any matched ranges included in the new match.
272 while (!matched_curly_braces_ranges.empty()) {
273 assert(matched_curly_braces_ranges.back().first !=
274 last_unmatched_index &&
275 "How can a curley brace be matched twice?");
276 if (matched_curly_braces_ranges.back().first < last_unmatched_index)
277 break;
278
279 // This is a nested range let's earse it.
280 assert((size_t)matched_curly_braces_ranges.back().second < i);
281 matched_curly_braces_ranges.pop_back();
282 }
283
284 // Assert invariant.
285 assert(matched_curly_braces_ranges.empty() ||
286 matched_curly_braces_ranges.back().first < last_unmatched_index);
287 matched_curly_braces_ranges.emplace_back(args&: last_unmatched_index, args&: i);
288 }
289 }
290
291 // Part2 - parse raw text and expresions parts.
292 // All expression ranges have been parsed in matched_curly_braces_ranges.
293 // The code below uses matched_curly_braces_ranges to divide logMessage
294 // into raw text parts and expression parts.
295 int last_raw_text_start = 0;
296 for (const std::pair<int, int> &curly_braces_range :
297 matched_curly_braces_ranges) {
298 // Raw text before open curly brace.
299 assert(curly_braces_range.first >= last_raw_text_start);
300 size_t raw_text_len = curly_braces_range.first - last_raw_text_start;
301 if (raw_text_len > 0) {
302 error = AppendLogMessagePart(
303 part: llvm::StringRef(m_log_message.c_str() + last_raw_text_start,
304 raw_text_len),
305 /*is_expr=*/false);
306 if (error.Fail()) {
307 NotifyLogMessageError(error: error.GetCString());
308 return;
309 }
310 }
311
312 // Expression between curly braces.
313 assert(curly_braces_range.second > curly_braces_range.first);
314 size_t expr_len = curly_braces_range.second - curly_braces_range.first - 1;
315 error = AppendLogMessagePart(
316 part: llvm::StringRef(m_log_message.c_str() + curly_braces_range.first + 1,
317 expr_len),
318 /*is_expr=*/true);
319 if (error.Fail()) {
320 NotifyLogMessageError(error: error.GetCString());
321 return;
322 }
323
324 last_raw_text_start = curly_braces_range.second + 1;
325 }
326 // Trailing raw text after close curly brace.
327 assert(last_raw_text_start >= 0);
328 if (m_log_message.size() > (size_t)last_raw_text_start) {
329 error = AppendLogMessagePart(
330 part: llvm::StringRef(m_log_message.c_str() + last_raw_text_start,
331 m_log_message.size() - last_raw_text_start),
332 /*is_expr=*/false);
333 if (error.Fail()) {
334 NotifyLogMessageError(error: error.GetCString());
335 return;
336 }
337 }
338
339 m_bp.SetCallback(callback: BreakpointHitCallback, baton: this);
340}
341
342void SourceBreakpoint::NotifyLogMessageError(llvm::StringRef error) {
343 std::string message = "Log message has error: ";
344 message += error;
345 m_dap.SendOutput(o: OutputType::Console, output: message);
346}
347
348/*static*/
349bool SourceBreakpoint::BreakpointHitCallback(
350 void *baton, lldb::SBProcess &process, lldb::SBThread &thread,
351 lldb::SBBreakpointLocation &location) {
352 if (!baton)
353 return true;
354
355 SourceBreakpoint *bp = (SourceBreakpoint *)baton;
356 lldb::SBFrame frame = thread.GetSelectedFrame();
357
358 std::string output;
359 for (const SourceBreakpoint::LogMessagePart &messagePart :
360 bp->m_log_message_parts) {
361 if (messagePart.is_expr) {
362 // Try local frame variables first before fall back to expression
363 // evaluation
364 const std::string &expr_str = messagePart.text;
365 const char *expr = expr_str.c_str();
366 lldb::SBValue value =
367 frame.GetValueForVariablePath(var_expr_cstr: expr, use_dynamic: lldb::eDynamicDontRunTarget);
368 if (value.GetError().Fail())
369 value = frame.EvaluateExpression(expr);
370 output += VariableDescription(
371 value, bp->m_dap.configuration.enableAutoVariableSummaries)
372 .display_value;
373 } else {
374 output += messagePart.text;
375 }
376 }
377 if (!output.empty() && output.back() != '\n')
378 output.push_back(c: '\n'); // Ensure log message has line break.
379 bp->m_dap.SendOutput(o: OutputType::Console, output: output.c_str());
380
381 // Do not stop.
382 return false;
383}
384
385} // namespace lldb_dap
386

source code of lldb/tools/lldb-dap/SourceBreakpoint.cpp