1 //===-- VSCode.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 LLDBVSCODE_VSCODE_H_ 10 #define LLDBVSCODE_VSCODE_H_ 11 12 #include <iosfwd> 13 #include <map> 14 #include <set> 15 #include <stdio.h> 16 #include <thread> 17 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/DenseSet.h" 20 #include "llvm/ADT/StringMap.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 #include "lldb/API/SBAttachInfo.h" 25 #include "lldb/API/SBBreakpoint.h" 26 #include "lldb/API/SBBreakpointLocation.h" 27 #include "lldb/API/SBCommandInterpreter.h" 28 #include "lldb/API/SBCommandReturnObject.h" 29 #include "lldb/API/SBCommunication.h" 30 #include "lldb/API/SBDebugger.h" 31 #include "lldb/API/SBEvent.h" 32 #include "lldb/API/SBHostOS.h" 33 #include "lldb/API/SBInstruction.h" 34 #include "lldb/API/SBInstructionList.h" 35 #include "lldb/API/SBLanguageRuntime.h" 36 #include "lldb/API/SBLaunchInfo.h" 37 #include "lldb/API/SBLineEntry.h" 38 #include "lldb/API/SBListener.h" 39 #include "lldb/API/SBProcess.h" 40 #include "lldb/API/SBStream.h" 41 #include "lldb/API/SBStringList.h" 42 #include "lldb/API/SBTarget.h" 43 #include "lldb/API/SBThread.h" 44 45 #include "ExceptionBreakpoint.h" 46 #include "FunctionBreakpoint.h" 47 #include "IOStream.h" 48 #include "SourceBreakpoint.h" 49 #include "SourceReference.h" 50 51 #define VARREF_LOCALS (int64_t)1 52 #define VARREF_GLOBALS (int64_t)2 53 #define VARREF_REGS (int64_t)3 54 #define VARREF_FIRST_VAR_IDX (int64_t)4 55 #define VARREF_IS_SCOPE(v) (VARREF_LOCALS <= 1 && v < VARREF_FIRST_VAR_IDX) 56 #define VARIDX_TO_VARREF(i) ((i) + VARREF_FIRST_VAR_IDX) 57 #define VARREF_TO_VARIDX(v) ((v)-VARREF_FIRST_VAR_IDX) 58 #define NO_TYPENAME "<no-type>" 59 60 namespace lldb_vscode { 61 62 typedef llvm::DenseMap<uint32_t, SourceBreakpoint> SourceBreakpointMap; 63 typedef llvm::StringMap<FunctionBreakpoint> FunctionBreakpointMap; 64 enum class OutputType { Console, Stdout, Stderr, Telemetry }; 65 66 struct VSCode { 67 InputStream input; 68 OutputStream output; 69 lldb::SBDebugger debugger; 70 lldb::SBTarget target; 71 lldb::SBAttachInfo attach_info; 72 lldb::SBLaunchInfo launch_info; 73 lldb::SBValueList variables; 74 lldb::SBBroadcaster broadcaster; 75 int64_t num_regs; 76 int64_t num_locals; 77 int64_t num_globals; 78 std::thread event_thread; 79 std::unique_ptr<std::ofstream> log; 80 llvm::DenseMap<lldb::addr_t, int64_t> addr_to_source_ref; 81 llvm::DenseMap<int64_t, SourceReference> source_map; 82 llvm::StringMap<SourceBreakpointMap> source_breakpoints; 83 FunctionBreakpointMap function_breakpoints; 84 std::vector<ExceptionBreakpoint> exception_breakpoints; 85 std::vector<std::string> init_commands; 86 std::vector<std::string> pre_run_commands; 87 std::vector<std::string> exit_commands; 88 std::vector<std::string> stop_commands; 89 lldb::tid_t focus_tid; 90 bool sent_terminated_event; 91 bool stop_at_entry; 92 // Keep track of the last stop thread index IDs as threads won't go away 93 // unless we send a "thread" event to indicate the thread exited. 94 llvm::DenseSet<lldb::tid_t> thread_ids; 95 VSCode(); 96 ~VSCode(); 97 VSCode(const VSCode &rhs) = delete; 98 void operator=(const VSCode &rhs) = delete; 99 int64_t GetLineForPC(int64_t sourceReference, lldb::addr_t pc) const; 100 ExceptionBreakpoint *GetExceptionBreakpoint(const std::string &filter); 101 ExceptionBreakpoint *GetExceptionBreakpoint(const lldb::break_id_t bp_id); 102 // Send the JSON in "json_str" to the "out" stream. Correctly send the 103 // "Content-Length:" field followed by the length, followed by the raw 104 // JSON bytes. 105 void SendJSON(const std::string &json_str); 106 107 // Serialize the JSON value into a string and send the JSON packet to 108 // the "out" stream. 109 void SendJSON(const llvm::json::Value &json); 110 111 std::string ReadJSON(); 112 113 void SendOutput(OutputType o, const llvm::StringRef output); 114 115 void __attribute__((format(printf, 3, 4))) 116 SendFormattedOutput(OutputType o, const char *format, ...); 117 118 static int64_t GetNextSourceReference(); 119 120 ExceptionBreakpoint *GetExceptionBPFromStopReason(lldb::SBThread &thread); 121 122 lldb::SBThread GetLLDBThread(const llvm::json::Object &arguments); 123 124 lldb::SBFrame GetLLDBFrame(const llvm::json::Object &arguments); 125 126 llvm::json::Value CreateTopLevelScopes(); 127 128 void RunLLDBCommands(llvm::StringRef prefix, 129 const std::vector<std::string> &commands); 130 131 void RunInitCommands(); 132 void RunPreRunCommands(); 133 void RunStopCommands(); 134 void RunExitCommands(); 135 }; 136 137 extern VSCode g_vsc; 138 139 } // namespace lldb_vscode 140 141 #endif 142