| 1 | //===-- SourceRequestHandler.cpp ------------------------------------------===// |
|---|---|
| 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 "DAP.h" |
| 10 | #include "Handler/RequestHandler.h" |
| 11 | #include "LLDBUtils.h" |
| 12 | #include "Protocol/ProtocolRequests.h" |
| 13 | #include "Protocol/ProtocolTypes.h" |
| 14 | #include "lldb/API/SBAddress.h" |
| 15 | #include "lldb/API/SBExecutionContext.h" |
| 16 | #include "lldb/API/SBFrame.h" |
| 17 | #include "lldb/API/SBInstructionList.h" |
| 18 | #include "lldb/API/SBProcess.h" |
| 19 | #include "lldb/API/SBStream.h" |
| 20 | #include "lldb/API/SBSymbol.h" |
| 21 | #include "lldb/API/SBTarget.h" |
| 22 | #include "lldb/API/SBThread.h" |
| 23 | #include "lldb/lldb-types.h" |
| 24 | #include "llvm/Support/Error.h" |
| 25 | |
| 26 | namespace lldb_dap { |
| 27 | |
| 28 | /// Source request; value of command field is 'source'. The request retrieves |
| 29 | /// the source code for a given source reference. |
| 30 | llvm::Expected<protocol::SourceResponseBody> |
| 31 | SourceRequestHandler::Run(const protocol::SourceArguments &args) const { |
| 32 | const auto source = |
| 33 | args.source->sourceReference.value_or(u: args.sourceReference); |
| 34 | |
| 35 | if (!source) |
| 36 | return llvm::make_error<DAPError>( |
| 37 | Args: "invalid arguments, expected source.sourceReference to be set"); |
| 38 | |
| 39 | lldb::SBAddress address(source, dap.target); |
| 40 | if (!address.IsValid()) |
| 41 | return llvm::make_error<DAPError>(Args: "source not found"); |
| 42 | |
| 43 | lldb::SBSymbol symbol = address.GetSymbol(); |
| 44 | |
| 45 | lldb::SBStream stream; |
| 46 | lldb::SBExecutionContext exe_ctx(dap.target); |
| 47 | |
| 48 | if (symbol.IsValid()) { |
| 49 | lldb::SBInstructionList insts = symbol.GetInstructions(target: dap.target); |
| 50 | insts.GetDescription(description&: stream, exe_ctx); |
| 51 | } else { |
| 52 | // No valid symbol, just return the disassembly. |
| 53 | lldb::SBInstructionList insts = dap.target.ReadInstructions( |
| 54 | base_addr: address, count: dap.k_number_of_assembly_lines_for_nodebug); |
| 55 | insts.GetDescription(description&: stream, exe_ctx); |
| 56 | } |
| 57 | |
| 58 | return protocol::SourceResponseBody{/*content=*/stream.GetData(), |
| 59 | /*mimeType=*/"text/x-lldb.disassembly"}; |
| 60 | } |
| 61 | |
| 62 | } // namespace lldb_dap |
| 63 |
