CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
246 lines
8.1 KiB
C++
246 lines
8.1 KiB
C++
//===-- ProtocolUtils.cpp -------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "ProtocolUtils.h"
|
|
#include "JSONUtils.h"
|
|
#include "LLDBUtils.h"
|
|
|
|
#include "lldb/API/SBDebugger.h"
|
|
#include "lldb/API/SBDeclaration.h"
|
|
#include "lldb/API/SBFileSpec.h"
|
|
#include "lldb/API/SBFormat.h"
|
|
#include "lldb/API/SBMutex.h"
|
|
#include "lldb/API/SBStream.h"
|
|
#include "lldb/API/SBTarget.h"
|
|
#include "lldb/API/SBThread.h"
|
|
#include "lldb/Host/PosixApi.h" // Adds PATH_MAX for windows
|
|
|
|
#include <iomanip>
|
|
#include <optional>
|
|
#include <sstream>
|
|
|
|
using namespace lldb_dap::protocol;
|
|
namespace lldb_dap {
|
|
|
|
static bool ShouldDisplayAssemblySource(
|
|
lldb::SBLineEntry line_entry,
|
|
lldb::StopDisassemblyType stop_disassembly_display) {
|
|
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNever)
|
|
return false;
|
|
|
|
if (stop_disassembly_display == lldb::eStopDisassemblyTypeAlways)
|
|
return true;
|
|
|
|
// A line entry of 0 indicates the line is compiler generated i.e. no source
|
|
// file is associated with the frame.
|
|
auto file_spec = line_entry.GetFileSpec();
|
|
if (!file_spec.IsValid() || line_entry.GetLine() == 0 ||
|
|
line_entry.GetLine() == LLDB_INVALID_LINE_NUMBER)
|
|
return true;
|
|
|
|
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNoSource &&
|
|
!file_spec.Exists()) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static uint64_t GetDebugInfoSizeInSection(lldb::SBSection section) {
|
|
uint64_t debug_info_size = 0;
|
|
const llvm::StringRef section_name(section.GetName());
|
|
if (section_name.starts_with(".debug") ||
|
|
section_name.starts_with("__debug") ||
|
|
section_name.starts_with(".apple") || section_name.starts_with("__apple"))
|
|
debug_info_size += section.GetFileByteSize();
|
|
|
|
const size_t num_sub_sections = section.GetNumSubSections();
|
|
for (size_t i = 0; i < num_sub_sections; i++)
|
|
debug_info_size +=
|
|
GetDebugInfoSizeInSection(section.GetSubSectionAtIndex(i));
|
|
|
|
return debug_info_size;
|
|
}
|
|
|
|
static uint64_t GetDebugInfoSize(lldb::SBModule module) {
|
|
uint64_t debug_info_size = 0;
|
|
const size_t num_sections = module.GetNumSections();
|
|
for (size_t i = 0; i < num_sections; i++)
|
|
debug_info_size += GetDebugInfoSizeInSection(module.GetSectionAtIndex(i));
|
|
|
|
return debug_info_size;
|
|
}
|
|
|
|
std::string ConvertDebugInfoSizeToString(uint64_t debug_size) {
|
|
std::ostringstream oss;
|
|
oss << std::fixed << std::setprecision(1);
|
|
if (debug_size < 1024) {
|
|
oss << debug_size << "B";
|
|
} else if (debug_size < static_cast<uint64_t>(1024 * 1024)) {
|
|
double kb = double(debug_size) / 1024.0;
|
|
oss << kb << "KB";
|
|
} else if (debug_size < 1024 * 1024 * 1024) {
|
|
double mb = double(debug_size) / (1024.0 * 1024.0);
|
|
oss << mb << "MB";
|
|
} else {
|
|
double gb = double(debug_size) / (1024.0 * 1024.0 * 1024.0);
|
|
oss << gb << "GB";
|
|
}
|
|
return oss.str();
|
|
}
|
|
|
|
std::optional<protocol::Module> CreateModule(const lldb::SBTarget &target,
|
|
lldb::SBModule &module,
|
|
bool id_only) {
|
|
if (!target.IsValid() || !module.IsValid())
|
|
return std::nullopt;
|
|
|
|
const llvm::StringRef uuid = module.GetUUIDString();
|
|
if (uuid.empty())
|
|
return std::nullopt;
|
|
|
|
protocol::Module p_module;
|
|
p_module.id = uuid;
|
|
|
|
if (id_only)
|
|
return p_module;
|
|
|
|
std::array<char, PATH_MAX> path_buffer{};
|
|
if (const lldb::SBFileSpec file_spec = module.GetFileSpec()) {
|
|
p_module.name = file_spec.GetFilename();
|
|
|
|
const uint32_t path_size =
|
|
file_spec.GetPath(path_buffer.data(), path_buffer.size());
|
|
p_module.path = std::string(path_buffer.data(), path_size);
|
|
}
|
|
|
|
if (const uint32_t num_compile_units = module.GetNumCompileUnits();
|
|
num_compile_units > 0) {
|
|
p_module.symbolStatus = "Symbols loaded.";
|
|
|
|
p_module.debugInfoSizeBytes = GetDebugInfoSize(module);
|
|
|
|
if (const lldb::SBFileSpec symbol_fspec = module.GetSymbolFileSpec()) {
|
|
const uint32_t path_size =
|
|
symbol_fspec.GetPath(path_buffer.data(), path_buffer.size());
|
|
p_module.symbolFilePath = std::string(path_buffer.data(), path_size);
|
|
}
|
|
} else {
|
|
p_module.symbolStatus = "Symbols not found.";
|
|
}
|
|
|
|
const auto load_address = module.GetObjectFileHeaderAddress();
|
|
if (const lldb::addr_t raw_address = load_address.GetLoadAddress(target);
|
|
raw_address != LLDB_INVALID_ADDRESS)
|
|
p_module.addressRange = llvm::formatv("{0:x}", raw_address);
|
|
|
|
std::array<uint32_t, 3> version_nums{};
|
|
const uint32_t num_versions =
|
|
module.GetVersion(version_nums.data(), version_nums.size());
|
|
if (num_versions > 0) {
|
|
p_module.version = llvm::formatv(
|
|
"{:$[.]}", llvm::make_range(version_nums.begin(),
|
|
version_nums.begin() + num_versions));
|
|
}
|
|
|
|
return p_module;
|
|
}
|
|
|
|
std::optional<protocol::Source> CreateSource(const lldb::SBFileSpec &file) {
|
|
if (!file.IsValid())
|
|
return std::nullopt;
|
|
|
|
protocol::Source source;
|
|
if (const char *name = file.GetFilename())
|
|
source.name = name;
|
|
char path[PATH_MAX] = "";
|
|
if (file.GetPath(path, sizeof(path)) &&
|
|
lldb::SBFileSpec::ResolvePath(path, path, PATH_MAX)) {
|
|
source.path = path;
|
|
if (!lldb::SBFileSpec(path).Exists())
|
|
source.presentationHint = Source::eSourcePresentationHintDeemphasize;
|
|
}
|
|
return source;
|
|
}
|
|
|
|
bool IsAssemblySource(const protocol::Source &source) {
|
|
// According to the specification, a source must have either `path` or
|
|
// `sourceReference` specified. We use `path` for sources with known source
|
|
// code, and `sourceReferences` when falling back to assembly.
|
|
return source.sourceReference.value_or(LLDB_DAP_INVALID_SRC_REF) >
|
|
LLDB_DAP_INVALID_SRC_REF;
|
|
}
|
|
|
|
bool DisplayAssemblySource(lldb::SBDebugger &debugger,
|
|
lldb::SBLineEntry line_entry) {
|
|
const lldb::StopDisassemblyType stop_disassembly_display =
|
|
GetStopDisassemblyDisplay(debugger);
|
|
return ShouldDisplayAssemblySource(line_entry, stop_disassembly_display);
|
|
}
|
|
|
|
std::string GetLoadAddressString(const lldb::addr_t addr) {
|
|
return "0x" + llvm::utohexstr(addr, false, 16);
|
|
}
|
|
|
|
protocol::Thread CreateThread(lldb::SBThread &thread, lldb::SBFormat &format) {
|
|
std::string name;
|
|
lldb::SBStream stream;
|
|
if (format && thread.GetDescriptionWithFormat(format, stream).Success()) {
|
|
name = stream.GetData();
|
|
} else {
|
|
llvm::StringRef thread_name(thread.GetName());
|
|
llvm::StringRef queue_name(thread.GetQueueName());
|
|
|
|
if (!thread_name.empty()) {
|
|
name = thread_name.str();
|
|
} else if (!queue_name.empty()) {
|
|
auto kind = thread.GetQueue().GetKind();
|
|
std::string queue_kind_label = "";
|
|
if (kind == lldb::eQueueKindSerial)
|
|
queue_kind_label = " (serial)";
|
|
else if (kind == lldb::eQueueKindConcurrent)
|
|
queue_kind_label = " (concurrent)";
|
|
|
|
name = llvm::formatv("Thread {0} Queue: {1}{2}", thread.GetIndexID(),
|
|
queue_name, queue_kind_label);
|
|
} else {
|
|
name = llvm::formatv("Thread {0}", thread.GetIndexID());
|
|
}
|
|
}
|
|
return protocol::Thread{thread.GetThreadID(), name};
|
|
}
|
|
|
|
std::vector<protocol::Thread> GetThreads(lldb::SBProcess process,
|
|
lldb::SBFormat &format) {
|
|
lldb::SBMutex lock = process.GetTarget().GetAPIMutex();
|
|
std::lock_guard<lldb::SBMutex> guard(lock);
|
|
|
|
std::vector<protocol::Thread> threads;
|
|
|
|
const uint32_t num_threads = process.GetNumThreads();
|
|
threads.reserve(num_threads);
|
|
for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
|
|
lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
|
|
threads.emplace_back(CreateThread(thread, format));
|
|
}
|
|
return threads;
|
|
}
|
|
|
|
ExceptionBreakpointsFilter
|
|
CreateExceptionBreakpointFilter(const ExceptionBreakpoint &bp) {
|
|
ExceptionBreakpointsFilter filter;
|
|
filter.filter = bp.GetFilter();
|
|
filter.label = bp.GetLabel();
|
|
filter.description = bp.GetLabel();
|
|
filter.defaultState = ExceptionBreakpoint::kDefaultValue;
|
|
filter.supportsCondition = true;
|
|
return filter;
|
|
}
|
|
|
|
} // namespace lldb_dap
|