1 //===-- LLDBUtils.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 "LLDBUtils.h"
10 #include "VSCode.h"
11
12 namespace lldb_vscode {
13
RunLLDBCommands(llvm::StringRef prefix,const llvm::ArrayRef<std::string> & commands,llvm::raw_ostream & strm)14 void RunLLDBCommands(llvm::StringRef prefix,
15 const llvm::ArrayRef<std::string> &commands,
16 llvm::raw_ostream &strm) {
17 if (commands.empty())
18 return;
19 lldb::SBCommandInterpreter interp = g_vsc.debugger.GetCommandInterpreter();
20 if (!prefix.empty())
21 strm << prefix << "\n";
22 for (const auto &command : commands) {
23 lldb::SBCommandReturnObject result;
24 strm << "(lldb) " << command << "\n";
25 interp.HandleCommand(command.c_str(), result);
26 auto output_len = result.GetOutputSize();
27 if (output_len) {
28 const char *output = result.GetOutput();
29 strm << output;
30 }
31 auto error_len = result.GetErrorSize();
32 if (error_len) {
33 const char *error = result.GetError();
34 strm << error;
35 }
36 }
37 }
38
RunLLDBCommands(llvm::StringRef prefix,const llvm::ArrayRef<std::string> & commands)39 std::string RunLLDBCommands(llvm::StringRef prefix,
40 const llvm::ArrayRef<std::string> &commands) {
41 std::string s;
42 llvm::raw_string_ostream strm(s);
43 RunLLDBCommands(prefix, commands, strm);
44 strm.flush();
45 return s;
46 }
47
ThreadHasStopReason(lldb::SBThread & thread)48 bool ThreadHasStopReason(lldb::SBThread &thread) {
49 switch (thread.GetStopReason()) {
50 case lldb::eStopReasonTrace:
51 case lldb::eStopReasonPlanComplete:
52 case lldb::eStopReasonBreakpoint:
53 case lldb::eStopReasonWatchpoint:
54 case lldb::eStopReasonInstrumentation:
55 case lldb::eStopReasonSignal:
56 case lldb::eStopReasonException:
57 case lldb::eStopReasonExec:
58 case lldb::eStopReasonProcessorTrace:
59 case lldb::eStopReasonFork:
60 case lldb::eStopReasonVFork:
61 case lldb::eStopReasonVForkDone:
62 return true;
63 case lldb::eStopReasonThreadExiting:
64 case lldb::eStopReasonInvalid:
65 case lldb::eStopReasonNone:
66 break;
67 }
68 return false;
69 }
70
71 static uint32_t constexpr THREAD_INDEX_SHIFT = 19;
72
GetLLDBThreadIndexID(uint64_t dap_frame_id)73 uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id) {
74 return dap_frame_id >> THREAD_INDEX_SHIFT;
75 }
76
GetLLDBFrameID(uint64_t dap_frame_id)77 uint32_t GetLLDBFrameID(uint64_t dap_frame_id) {
78 return dap_frame_id & ((1u << THREAD_INDEX_SHIFT) - 1);
79 }
80
MakeVSCodeFrameID(lldb::SBFrame & frame)81 int64_t MakeVSCodeFrameID(lldb::SBFrame &frame) {
82 return (int64_t)(frame.GetThread().GetIndexID() << THREAD_INDEX_SHIFT |
83 frame.GetFrameID());
84 }
85
86 } // namespace lldb_vscode
87