xref: /llvm-project/lldb/tools/lldb-dap/SourceBreakpoint.h (revision b99d4112585302cbd01f9b851a04adc6e4fb5218)
1 //===-- SourceBreakpoint.h --------------------------------------*- 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 #ifndef LLDB_TOOLS_LLDB_DAP_SOURCEBREAKPOINT_H
10 #define LLDB_TOOLS_LLDB_DAP_SOURCEBREAKPOINT_H
11 
12 #include "Breakpoint.h"
13 #include "DAPForward.h"
14 #include "lldb/API/SBError.h"
15 #include "llvm/ADT/StringRef.h"
16 #include <cstdint>
17 #include <string>
18 #include <vector>
19 
20 namespace lldb_dap {
21 
22 struct SourceBreakpoint : public Breakpoint {
23   // logMessage part can be either a raw text or an expression.
24   struct LogMessagePart {
25     LogMessagePart(llvm::StringRef text, bool is_expr)
26         : text(text), is_expr(is_expr) {}
27     std::string text;
28     bool is_expr;
29   };
30   // If this attribute exists and is non-empty, the backend must not 'break'
31   // (stop) but log the message instead. Expressions within {} are
32   // interpolated.
33   std::string logMessage;
34   std::vector<LogMessagePart> logMessageParts;
35 
36   uint32_t line;   ///< The source line of the breakpoint or logpoint
37   uint32_t column; ///< An optional source column of the breakpoint
38 
39   SourceBreakpoint(DAP &d, const llvm::json::Object &obj);
40 
41   // Set this breakpoint in LLDB as a new breakpoint
42   void SetBreakpoint(const llvm::StringRef source_path);
43   void UpdateBreakpoint(const SourceBreakpoint &request_bp);
44 
45   void SetLogMessage();
46   // Format \param text and return formatted text in \param formatted.
47   // \return any formatting failures.
48   lldb::SBError FormatLogText(llvm::StringRef text, std::string &formatted);
49   lldb::SBError AppendLogMessagePart(llvm::StringRef part, bool is_expr);
50   void NotifyLogMessageError(llvm::StringRef error);
51 
52   static bool BreakpointHitCallback(void *baton, lldb::SBProcess &process,
53                                     lldb::SBThread &thread,
54                                     lldb::SBBreakpointLocation &location);
55 };
56 
57 inline bool operator<(const SourceBreakpoint &lhs,
58                       const SourceBreakpoint &rhs) {
59   if (lhs.line == rhs.line)
60     return lhs.column < rhs.column;
61   return lhs.line < rhs.line;
62 }
63 
64 } // namespace lldb_dap
65 
66 #endif
67