brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.0 KiB · 4a9d256 Raw
91 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "DAP.h"10#include "DAPError.h"11#include "Protocol/DAPTypes.h"12#include "RequestHandler.h"13#include "lldb/API/SBAddress.h"14#include "lldb/API/SBFileSpec.h"15#include "lldb/API/SBModule.h"16#include "lldb/API/SBModuleSpec.h"17#include "lldb/Utility/UUID.h"18#include "llvm/Support/Error.h"19#include <cstddef>20 21using namespace lldb_dap::protocol;22namespace lldb_dap {23 24llvm::Expected<ModuleSymbolsResponseBody>25ModuleSymbolsRequestHandler::Run(const ModuleSymbolsArguments &args) const {26  ModuleSymbolsResponseBody response;27 28  lldb::SBModuleSpec module_spec;29  if (!args.moduleId.empty()) {30    llvm::SmallVector<uint8_t, 20> uuid_bytes;31    if (!lldb_private::UUID::DecodeUUIDBytesFromString(args.moduleId,32                                                       uuid_bytes)33             .empty())34      return llvm::make_error<DAPError>("invalid module ID");35 36    module_spec.SetUUIDBytes(uuid_bytes.data(), uuid_bytes.size());37  }38 39  if (!args.moduleName.empty()) {40    lldb::SBFileSpec file_spec;41    file_spec.SetFilename(args.moduleName.c_str());42    module_spec.SetFileSpec(file_spec);43  }44 45  // Empty request, return empty response.46  if (!module_spec.IsValid())47    return response;48 49  std::vector<Symbol> &symbols = response.symbols;50  lldb::SBModule module = dap.target.FindModule(module_spec);51  if (!module.IsValid())52    return llvm::make_error<DAPError>("module not found");53 54  const size_t num_symbols = module.GetNumSymbols();55  const size_t start_index = args.startIndex.value_or(0);56  const size_t end_index =57      std::min(start_index + args.count.value_or(num_symbols), num_symbols);58  for (size_t i = start_index; i < end_index; ++i) {59    lldb::SBSymbol symbol = module.GetSymbolAtIndex(i);60    if (!symbol.IsValid())61      continue;62 63    Symbol dap_symbol;64    dap_symbol.id = symbol.GetID();65    dap_symbol.type = symbol.GetType();66    dap_symbol.isDebug = symbol.IsDebug();67    dap_symbol.isSynthetic = symbol.IsSynthetic();68    dap_symbol.isExternal = symbol.IsExternal();69 70    lldb::SBAddress start_address = symbol.GetStartAddress();71    if (start_address.IsValid()) {72      lldb::addr_t file_address = start_address.GetFileAddress();73      if (file_address != LLDB_INVALID_ADDRESS)74        dap_symbol.fileAddress = file_address;75 76      lldb::addr_t load_address = start_address.GetLoadAddress(dap.target);77      if (load_address != LLDB_INVALID_ADDRESS)78        dap_symbol.loadAddress = load_address;79    }80 81    dap_symbol.size = symbol.GetSize();82    if (const char *symbol_name = symbol.GetName())83      dap_symbol.name = symbol_name;84    symbols.push_back(std::move(dap_symbol));85  }86 87  return response;88}89 90} // namespace lldb_dap91