1061da546Spatrick //===-- LLDBUtils.cpp -------------------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "LLDBUtils.h"
10061da546Spatrick #include "VSCode.h"
11061da546Spatrick
12061da546Spatrick namespace lldb_vscode {
13061da546Spatrick
RunLLDBCommands(llvm::StringRef prefix,const llvm::ArrayRef<std::string> & commands,llvm::raw_ostream & strm)14061da546Spatrick void RunLLDBCommands(llvm::StringRef prefix,
15061da546Spatrick const llvm::ArrayRef<std::string> &commands,
16061da546Spatrick llvm::raw_ostream &strm) {
17061da546Spatrick if (commands.empty())
18061da546Spatrick return;
19061da546Spatrick lldb::SBCommandInterpreter interp = g_vsc.debugger.GetCommandInterpreter();
20061da546Spatrick if (!prefix.empty())
21061da546Spatrick strm << prefix << "\n";
22061da546Spatrick for (const auto &command : commands) {
23061da546Spatrick lldb::SBCommandReturnObject result;
24061da546Spatrick strm << "(lldb) " << command << "\n";
25061da546Spatrick interp.HandleCommand(command.c_str(), result);
26061da546Spatrick auto output_len = result.GetOutputSize();
27061da546Spatrick if (output_len) {
28061da546Spatrick const char *output = result.GetOutput();
29061da546Spatrick strm << output;
30061da546Spatrick }
31061da546Spatrick auto error_len = result.GetErrorSize();
32061da546Spatrick if (error_len) {
33061da546Spatrick const char *error = result.GetError();
34061da546Spatrick strm << error;
35061da546Spatrick }
36061da546Spatrick }
37061da546Spatrick }
38061da546Spatrick
RunLLDBCommands(llvm::StringRef prefix,const llvm::ArrayRef<std::string> & commands)39061da546Spatrick std::string RunLLDBCommands(llvm::StringRef prefix,
40061da546Spatrick const llvm::ArrayRef<std::string> &commands) {
41061da546Spatrick std::string s;
42061da546Spatrick llvm::raw_string_ostream strm(s);
43061da546Spatrick RunLLDBCommands(prefix, commands, strm);
44061da546Spatrick strm.flush();
45061da546Spatrick return s;
46061da546Spatrick }
47061da546Spatrick
ThreadHasStopReason(lldb::SBThread & thread)48061da546Spatrick bool ThreadHasStopReason(lldb::SBThread &thread) {
49061da546Spatrick switch (thread.GetStopReason()) {
50061da546Spatrick case lldb::eStopReasonTrace:
51061da546Spatrick case lldb::eStopReasonPlanComplete:
52061da546Spatrick case lldb::eStopReasonBreakpoint:
53061da546Spatrick case lldb::eStopReasonWatchpoint:
54061da546Spatrick case lldb::eStopReasonInstrumentation:
55061da546Spatrick case lldb::eStopReasonSignal:
56061da546Spatrick case lldb::eStopReasonException:
57061da546Spatrick case lldb::eStopReasonExec:
58*be691f3bSpatrick case lldb::eStopReasonProcessorTrace:
59*be691f3bSpatrick case lldb::eStopReasonFork:
60*be691f3bSpatrick case lldb::eStopReasonVFork:
61*be691f3bSpatrick case lldb::eStopReasonVForkDone:
62061da546Spatrick return true;
63061da546Spatrick case lldb::eStopReasonThreadExiting:
64061da546Spatrick case lldb::eStopReasonInvalid:
65061da546Spatrick case lldb::eStopReasonNone:
66061da546Spatrick break;
67061da546Spatrick }
68061da546Spatrick return false;
69061da546Spatrick }
70061da546Spatrick
71061da546Spatrick static uint32_t constexpr THREAD_INDEX_SHIFT = 19;
72061da546Spatrick
GetLLDBThreadIndexID(uint64_t dap_frame_id)73061da546Spatrick uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id) {
74061da546Spatrick return dap_frame_id >> THREAD_INDEX_SHIFT;
75061da546Spatrick }
76061da546Spatrick
GetLLDBFrameID(uint64_t dap_frame_id)77061da546Spatrick uint32_t GetLLDBFrameID(uint64_t dap_frame_id) {
78061da546Spatrick return dap_frame_id & ((1u << THREAD_INDEX_SHIFT) - 1);
79061da546Spatrick }
80061da546Spatrick
MakeVSCodeFrameID(lldb::SBFrame & frame)81061da546Spatrick int64_t MakeVSCodeFrameID(lldb::SBFrame &frame) {
82061da546Spatrick return (int64_t)(frame.GetThread().GetIndexID() << THREAD_INDEX_SHIFT |
83061da546Spatrick frame.GetFrameID());
84061da546Spatrick }
85061da546Spatrick
86061da546Spatrick } // namespace lldb_vscode
87