1 //===-- LLDBUtils.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "LLDBUtils.h"
11 #include "VSCode.h"
12 
13 namespace lldb_vscode {
14 
15 void RunLLDBCommands(llvm::StringRef prefix,
16                      const llvm::ArrayRef<std::string> &commands,
17                      llvm::raw_ostream &strm) {
18   if (commands.empty())
19     return;
20   lldb::SBCommandInterpreter interp = g_vsc.debugger.GetCommandInterpreter();
21   if (!prefix.empty())
22     strm << prefix << "\n";
23   for (const auto &command : commands) {
24     lldb::SBCommandReturnObject result;
25     strm << "(lldb) " << command << "\n";
26     interp.HandleCommand(command.c_str(), result);
27     auto output_len = result.GetOutputSize();
28     if (output_len) {
29       const char *output = result.GetOutput();
30       strm << output;
31     }
32     auto error_len = result.GetErrorSize();
33     if (error_len) {
34       const char *error = result.GetError();
35       strm << error;
36     }
37   }
38 }
39 
40 std::string RunLLDBCommands(llvm::StringRef prefix,
41                             const llvm::ArrayRef<std::string> &commands) {
42   std::string s;
43   llvm::raw_string_ostream strm(s);
44   RunLLDBCommands(prefix, commands, strm);
45   strm.flush();
46   return s;
47 }
48 
49 bool ThreadHasStopReason(lldb::SBThread &thread) {
50   switch (thread.GetStopReason()) {
51   case lldb::eStopReasonTrace:
52   case lldb::eStopReasonPlanComplete:
53   case lldb::eStopReasonBreakpoint:
54   case lldb::eStopReasonWatchpoint:
55   case lldb::eStopReasonInstrumentation:
56   case lldb::eStopReasonSignal:
57   case lldb::eStopReasonException:
58   case lldb::eStopReasonExec:
59     return true;
60   case lldb::eStopReasonThreadExiting:
61   case lldb::eStopReasonInvalid:
62   case lldb::eStopReasonNone:
63     break;
64   }
65   return false;
66 }
67 
68 static uint32_t constexpr THREAD_INDEX_SHIFT = 19;
69 
70 uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id) {
71   return dap_frame_id >> THREAD_INDEX_SHIFT;
72 }
73 
74 uint32_t GetLLDBFrameID(uint64_t dap_frame_id) {
75   return dap_frame_id & ((1u << THREAD_INDEX_SHIFT) - 1);
76 }
77 
78 int64_t MakeVSCodeFrameID(lldb::SBFrame &frame) {
79   return (int64_t)(frame.GetThread().GetIndexID() << THREAD_INDEX_SHIFT |
80                    frame.GetFrameID());
81 }
82 
83 static uint32_t constexpr BREAKPOINT_ID_SHIFT = 22;
84 
85 uint32_t GetLLDBBreakpointID(uint64_t dap_breakpoint_id) {
86   return dap_breakpoint_id >> BREAKPOINT_ID_SHIFT;
87 }
88 
89 uint32_t GetLLDBBreakpointLocationID(uint64_t dap_breakpoint_id) {
90   return dap_breakpoint_id & ((1u << BREAKPOINT_ID_SHIFT) - 1);
91 }
92 
93 int64_t MakeVSCodeBreakpointID(lldb::SBBreakpointLocation &bp_loc) {
94   return (int64_t)(bp_loc.GetBreakpoint().GetID() << BREAKPOINT_ID_SHIFT |
95                    bp_loc.GetID());
96 }
97 
98 } // namespace lldb_vscode
99