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 
BreakpointBase(const llvm::json::Object & obj)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 
SetCondition()20 void BreakpointBase::SetCondition() { bp.SetCondition(condition.c_str()); }
21 
SetHitCondition()22 void BreakpointBase::SetHitCondition() {
23   uint64_t hitCount = 0;
24   if (llvm::to_integer(hitCondition, hitCount))
25     bp.SetIgnoreCount(hitCount - 1);
26 }
27 
28 // logMessage will be divided into array of LogMessagePart as two kinds:
29 // 1. raw print text message, and
30 // 2. interpolated expression for evaluation which is inside matching curly
31 //    braces.
32 //
33 // The function tries to parse logMessage into a list of LogMessageParts
34 // for easy later access in BreakpointHitCallback.
SetLogMessage()35 void BreakpointBase::SetLogMessage() {
36   logMessageParts.clear();
37 
38   // Contains unmatched open curly braces indices.
39   std::vector<int> unmatched_curly_braces;
40 
41   // Contains all matched curly braces in logMessage.
42   // Loop invariant: matched_curly_braces_ranges are sorted by start index in
43   // ascending order without any overlap between them.
44   std::vector<std::pair<int, int>> matched_curly_braces_ranges;
45 
46   // Part1 - parse matched_curly_braces_ranges.
47   // locating all curly braced expression ranges in logMessage.
48   // The algorithm takes care of nested and imbalanced curly braces.
49   for (size_t i = 0; i < logMessage.size(); ++i) {
50     if (logMessage[i] == '{') {
51       unmatched_curly_braces.push_back(i);
52     } else if (logMessage[i] == '}') {
53       if (unmatched_curly_braces.empty())
54         // Nothing to match.
55         continue;
56 
57       int last_unmatched_index = unmatched_curly_braces.back();
58       unmatched_curly_braces.pop_back();
59 
60       // Erase any matched ranges included in the new match.
61       while (!matched_curly_braces_ranges.empty()) {
62         assert(matched_curly_braces_ranges.back().first !=
63                    last_unmatched_index &&
64                "How can a curley brace be matched twice?");
65         if (matched_curly_braces_ranges.back().first < last_unmatched_index)
66           break;
67 
68         // This is a nested range let's earse it.
69         assert((size_t)matched_curly_braces_ranges.back().second < i);
70         matched_curly_braces_ranges.pop_back();
71       }
72 
73       // Assert invariant.
74       assert(matched_curly_braces_ranges.empty() ||
75              matched_curly_braces_ranges.back().first < last_unmatched_index);
76       matched_curly_braces_ranges.emplace_back(last_unmatched_index, i);
77     }
78   }
79 
80   // Part2 - parse raw text and expresions parts.
81   // All expression ranges have been parsed in matched_curly_braces_ranges.
82   // The code below uses matched_curly_braces_ranges to divide logMessage
83   // into raw text parts and expression parts.
84   int last_raw_text_start = 0;
85   for (const std::pair<int, int> &curly_braces_range :
86        matched_curly_braces_ranges) {
87     // Raw text before open curly brace.
88     assert(curly_braces_range.first >= last_raw_text_start);
89     size_t raw_text_len = curly_braces_range.first - last_raw_text_start;
90     if (raw_text_len > 0)
91       logMessageParts.emplace_back(
92           llvm::StringRef(logMessage.c_str() + last_raw_text_start,
93                           raw_text_len),
94           /*is_expr=*/false);
95 
96     // Expression between curly braces.
97     assert(curly_braces_range.second > curly_braces_range.first);
98     size_t expr_len = curly_braces_range.second - curly_braces_range.first - 1;
99     logMessageParts.emplace_back(
100         llvm::StringRef(logMessage.c_str() + curly_braces_range.first + 1,
101                         expr_len),
102         /*is_expr=*/true);
103     last_raw_text_start = curly_braces_range.second + 1;
104   }
105   // Trailing raw text after close curly brace.
106   assert(last_raw_text_start >= 0);
107   if (logMessage.size() > (size_t)last_raw_text_start)
108     logMessageParts.emplace_back(
109         llvm::StringRef(logMessage.c_str() + last_raw_text_start,
110                         logMessage.size() - last_raw_text_start),
111         /*is_expr=*/false);
112   bp.SetCallback(BreakpointBase::BreakpointHitCallback, this);
113 }
114 
115 /*static*/
BreakpointHitCallback(void * baton,lldb::SBProcess & process,lldb::SBThread & thread,lldb::SBBreakpointLocation & location)116 bool BreakpointBase::BreakpointHitCallback(
117     void *baton, lldb::SBProcess &process, lldb::SBThread &thread,
118     lldb::SBBreakpointLocation &location) {
119   if (!baton)
120     return true;
121 
122   BreakpointBase *bp = (BreakpointBase *)baton;
123   lldb::SBFrame frame = thread.GetSelectedFrame();
124 
125   std::string output;
126   for (const BreakpointBase::LogMessagePart &messagePart :
127        bp->logMessageParts) {
128     if (messagePart.is_expr) {
129       // Try local frame variables first before fall back to expression
130       // evaluation
131       std::string expr_str = messagePart.text.str();
132       const char *expr = expr_str.c_str();
133       lldb::SBValue value =
134           frame.GetValueForVariablePath(expr, lldb::eDynamicDontRunTarget);
135       if (value.GetError().Fail())
136         value = frame.EvaluateExpression(expr);
137       const char *expr_val = value.GetValue();
138       if (expr_val)
139         output += expr_val;
140     } else {
141       output += messagePart.text.str();
142     }
143   }
144   g_vsc.SendOutput(OutputType::Console, output.c_str());
145 
146   // Do not stop.
147   return false;
148 }
149 
UpdateBreakpoint(const BreakpointBase & request_bp)150 void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) {
151   if (condition != request_bp.condition) {
152     condition = request_bp.condition;
153     SetCondition();
154   }
155   if (hitCondition != request_bp.hitCondition) {
156     hitCondition = request_bp.hitCondition;
157     SetHitCondition();
158   }
159   if (logMessage != request_bp.logMessage) {
160     logMessage = request_bp.logMessage;
161     SetLogMessage();
162   }
163 }
164 
GetBreakpointLabel()165 const char *BreakpointBase::GetBreakpointLabel() {
166   // Breakpoints in LLDB can have names added to them which are kind of like
167   // labels or categories. All breakpoints that are set through the IDE UI get
168   // sent through the various VS code DAP set*Breakpoint packets, and these
169   // breakpoints will be labeled with this name so if breakpoint update events
170   // come in for breakpoints that the IDE doesn't know about, like if a
171   // breakpoint is set manually using the debugger console, we won't report any
172   // updates on them and confused the IDE. This function gets called by all of
173   // the breakpoint classes after they set breakpoints to mark a breakpoint as
174   // a UI breakpoint. We can later check a lldb::SBBreakpoint object that comes
175   // in via LLDB breakpoint changed events and check the breakpoint by calling
176   // "bool lldb::SBBreakpoint::MatchesName(const char *)" to check if a
177   // breakpoint in one of the UI breakpoints that we should report changes for.
178   return "vscode";
179 }
180