15ffd83dbSDimitry Andric //===-- CommandObjectDisassemble.cpp --------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "CommandObjectDisassemble.h"
100b57cec5SDimitry Andric #include "lldb/Core/AddressRange.h"
110b57cec5SDimitry Andric #include "lldb/Core/Disassembler.h"
120b57cec5SDimitry Andric #include "lldb/Core/Module.h"
130b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
140b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
150b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
160b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
170b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
180b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
190b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
200b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
210b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
220b57cec5SDimitry Andric #include "lldb/Target/Target.h"
230b57cec5SDimitry Andric 
245ffd83dbSDimitry Andric static constexpr unsigned default_disasm_byte_size = 32;
255ffd83dbSDimitry Andric static constexpr unsigned default_disasm_num_ins = 4;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using namespace lldb;
280b57cec5SDimitry Andric using namespace lldb_private;
290b57cec5SDimitry Andric 
309dba64beSDimitry Andric #define LLDB_OPTIONS_disassemble
319dba64beSDimitry Andric #include "CommandOptions.inc"
320b57cec5SDimitry Andric 
CommandOptions()330b57cec5SDimitry Andric CommandObjectDisassemble::CommandOptions::CommandOptions()
34*5f7ddb14SDimitry Andric     : Options(), func_name(), plugin_name(), flavor_string(), arch() {
350b57cec5SDimitry Andric   OptionParsingStarting(nullptr);
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric CommandObjectDisassemble::CommandOptions::~CommandOptions() = default;
390b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)400b57cec5SDimitry Andric Status CommandObjectDisassemble::CommandOptions::SetOptionValue(
410b57cec5SDimitry Andric     uint32_t option_idx, llvm::StringRef option_arg,
420b57cec5SDimitry Andric     ExecutionContext *execution_context) {
430b57cec5SDimitry Andric   Status error;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   const int short_option = m_getopt_table[option_idx].val;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   switch (short_option) {
480b57cec5SDimitry Andric   case 'm':
490b57cec5SDimitry Andric     show_mixed = true;
500b57cec5SDimitry Andric     break;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   case 'C':
530b57cec5SDimitry Andric     if (option_arg.getAsInteger(0, num_lines_context))
540b57cec5SDimitry Andric       error.SetErrorStringWithFormat("invalid num context lines string: \"%s\"",
550b57cec5SDimitry Andric                                      option_arg.str().c_str());
560b57cec5SDimitry Andric     break;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   case 'c':
590b57cec5SDimitry Andric     if (option_arg.getAsInteger(0, num_instructions))
600b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
610b57cec5SDimitry Andric           "invalid num of instructions string: \"%s\"",
620b57cec5SDimitry Andric           option_arg.str().c_str());
630b57cec5SDimitry Andric     break;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   case 'b':
660b57cec5SDimitry Andric     show_bytes = true;
670b57cec5SDimitry Andric     break;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   case 's': {
700b57cec5SDimitry Andric     start_addr = OptionArgParser::ToAddress(execution_context, option_arg,
710b57cec5SDimitry Andric                                             LLDB_INVALID_ADDRESS, &error);
720b57cec5SDimitry Andric     if (start_addr != LLDB_INVALID_ADDRESS)
730b57cec5SDimitry Andric       some_location_specified = true;
740b57cec5SDimitry Andric   } break;
750b57cec5SDimitry Andric   case 'e': {
760b57cec5SDimitry Andric     end_addr = OptionArgParser::ToAddress(execution_context, option_arg,
770b57cec5SDimitry Andric                                           LLDB_INVALID_ADDRESS, &error);
780b57cec5SDimitry Andric     if (end_addr != LLDB_INVALID_ADDRESS)
790b57cec5SDimitry Andric       some_location_specified = true;
800b57cec5SDimitry Andric   } break;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   case 'n':
835ffd83dbSDimitry Andric     func_name.assign(std::string(option_arg));
840b57cec5SDimitry Andric     some_location_specified = true;
850b57cec5SDimitry Andric     break;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   case 'p':
880b57cec5SDimitry Andric     at_pc = true;
890b57cec5SDimitry Andric     some_location_specified = true;
900b57cec5SDimitry Andric     break;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   case 'l':
930b57cec5SDimitry Andric     frame_line = true;
940b57cec5SDimitry Andric     // Disassemble the current source line kind of implies showing mixed source
950b57cec5SDimitry Andric     // code context.
960b57cec5SDimitry Andric     show_mixed = true;
970b57cec5SDimitry Andric     some_location_specified = true;
980b57cec5SDimitry Andric     break;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   case 'P':
1015ffd83dbSDimitry Andric     plugin_name.assign(std::string(option_arg));
1020b57cec5SDimitry Andric     break;
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   case 'F': {
1050b57cec5SDimitry Andric     TargetSP target_sp =
1060b57cec5SDimitry Andric         execution_context ? execution_context->GetTargetSP() : TargetSP();
1070b57cec5SDimitry Andric     if (target_sp && (target_sp->GetArchitecture().GetTriple().getArch() ==
1080b57cec5SDimitry Andric                           llvm::Triple::x86 ||
1090b57cec5SDimitry Andric                       target_sp->GetArchitecture().GetTriple().getArch() ==
1100b57cec5SDimitry Andric                           llvm::Triple::x86_64)) {
1115ffd83dbSDimitry Andric       flavor_string.assign(std::string(option_arg));
1120b57cec5SDimitry Andric     } else
1130b57cec5SDimitry Andric       error.SetErrorStringWithFormat("Disassembler flavors are currently only "
1140b57cec5SDimitry Andric                                      "supported for x86 and x86_64 targets.");
1150b57cec5SDimitry Andric     break;
1160b57cec5SDimitry Andric   }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   case 'r':
1190b57cec5SDimitry Andric     raw = true;
1200b57cec5SDimitry Andric     break;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   case 'f':
1230b57cec5SDimitry Andric     current_function = true;
1240b57cec5SDimitry Andric     some_location_specified = true;
1250b57cec5SDimitry Andric     break;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   case 'A':
1280b57cec5SDimitry Andric     if (execution_context) {
1290b57cec5SDimitry Andric       const auto &target_sp = execution_context->GetTargetSP();
1300b57cec5SDimitry Andric       auto platform_ptr = target_sp ? target_sp->GetPlatform().get() : nullptr;
1310b57cec5SDimitry Andric       arch = Platform::GetAugmentedArchSpec(platform_ptr, option_arg);
1320b57cec5SDimitry Andric     }
1330b57cec5SDimitry Andric     break;
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   case 'a': {
1360b57cec5SDimitry Andric     symbol_containing_addr = OptionArgParser::ToAddress(
1370b57cec5SDimitry Andric         execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
1380b57cec5SDimitry Andric     if (symbol_containing_addr != LLDB_INVALID_ADDRESS) {
1390b57cec5SDimitry Andric       some_location_specified = true;
1400b57cec5SDimitry Andric     }
1410b57cec5SDimitry Andric   } break;
1420b57cec5SDimitry Andric 
1435ffd83dbSDimitry Andric   case '\x01':
1445ffd83dbSDimitry Andric     force = true;
1455ffd83dbSDimitry Andric     break;
1465ffd83dbSDimitry Andric 
1470b57cec5SDimitry Andric   default:
1489dba64beSDimitry Andric     llvm_unreachable("Unimplemented option");
1490b57cec5SDimitry Andric   }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   return error;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)1540b57cec5SDimitry Andric void CommandObjectDisassemble::CommandOptions::OptionParsingStarting(
1550b57cec5SDimitry Andric     ExecutionContext *execution_context) {
1560b57cec5SDimitry Andric   show_mixed = false;
1570b57cec5SDimitry Andric   show_bytes = false;
1580b57cec5SDimitry Andric   num_lines_context = 0;
1590b57cec5SDimitry Andric   num_instructions = 0;
1600b57cec5SDimitry Andric   func_name.clear();
1610b57cec5SDimitry Andric   current_function = false;
1620b57cec5SDimitry Andric   at_pc = false;
1630b57cec5SDimitry Andric   frame_line = false;
1640b57cec5SDimitry Andric   start_addr = LLDB_INVALID_ADDRESS;
1650b57cec5SDimitry Andric   end_addr = LLDB_INVALID_ADDRESS;
1660b57cec5SDimitry Andric   symbol_containing_addr = LLDB_INVALID_ADDRESS;
1670b57cec5SDimitry Andric   raw = false;
1680b57cec5SDimitry Andric   plugin_name.clear();
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   Target *target =
1710b57cec5SDimitry Andric       execution_context ? execution_context->GetTargetPtr() : nullptr;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   // This is a hack till we get the ability to specify features based on
1740b57cec5SDimitry Andric   // architecture.  For now GetDisassemblyFlavor is really only valid for x86
1750b57cec5SDimitry Andric   // (and for the llvm assembler plugin, but I'm papering over that since that
1760b57cec5SDimitry Andric   // is the only disassembler plugin we have...
1770b57cec5SDimitry Andric   if (target) {
1780b57cec5SDimitry Andric     if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86 ||
1790b57cec5SDimitry Andric         target->GetArchitecture().GetTriple().getArch() ==
1800b57cec5SDimitry Andric             llvm::Triple::x86_64) {
1810b57cec5SDimitry Andric       flavor_string.assign(target->GetDisassemblyFlavor());
1820b57cec5SDimitry Andric     } else
1830b57cec5SDimitry Andric       flavor_string.assign("default");
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   } else
1860b57cec5SDimitry Andric     flavor_string.assign("default");
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   arch.Clear();
1890b57cec5SDimitry Andric   some_location_specified = false;
1905ffd83dbSDimitry Andric   force = false;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric 
OptionParsingFinished(ExecutionContext * execution_context)1930b57cec5SDimitry Andric Status CommandObjectDisassemble::CommandOptions::OptionParsingFinished(
1940b57cec5SDimitry Andric     ExecutionContext *execution_context) {
1950b57cec5SDimitry Andric   if (!some_location_specified)
1960b57cec5SDimitry Andric     current_function = true;
1970b57cec5SDimitry Andric   return Status();
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition>
GetDefinitions()2010b57cec5SDimitry Andric CommandObjectDisassemble::CommandOptions::GetDefinitions() {
2020b57cec5SDimitry Andric   return llvm::makeArrayRef(g_disassemble_options);
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric // CommandObjectDisassemble
2060b57cec5SDimitry Andric 
CommandObjectDisassemble(CommandInterpreter & interpreter)2070b57cec5SDimitry Andric CommandObjectDisassemble::CommandObjectDisassemble(
2080b57cec5SDimitry Andric     CommandInterpreter &interpreter)
2090b57cec5SDimitry Andric     : CommandObjectParsed(
2100b57cec5SDimitry Andric           interpreter, "disassemble",
2110b57cec5SDimitry Andric           "Disassemble specified instructions in the current target.  "
2120b57cec5SDimitry Andric           "Defaults to the current function for the current thread and "
2130b57cec5SDimitry Andric           "stack frame.",
2149dba64beSDimitry Andric           "disassemble [<cmd-options>]", eCommandRequiresTarget),
2150b57cec5SDimitry Andric       m_options() {}
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric CommandObjectDisassemble::~CommandObjectDisassemble() = default;
2180b57cec5SDimitry Andric 
CheckRangeSize(const AddressRange & range,llvm::StringRef what)2195ffd83dbSDimitry Andric llvm::Error CommandObjectDisassemble::CheckRangeSize(const AddressRange &range,
2205ffd83dbSDimitry Andric                                                      llvm::StringRef what) {
2215ffd83dbSDimitry Andric   if (m_options.num_instructions > 0 || m_options.force ||
222*5f7ddb14SDimitry Andric       range.GetByteSize() < GetDebugger().GetStopDisassemblyMaxSize())
2235ffd83dbSDimitry Andric     return llvm::Error::success();
2245ffd83dbSDimitry Andric   StreamString msg;
2255ffd83dbSDimitry Andric   msg << "Not disassembling " << what << " because it is very large ";
2265ffd83dbSDimitry Andric   range.Dump(&msg, &GetSelectedTarget(), Address::DumpStyleLoadAddress,
2275ffd83dbSDimitry Andric              Address::DumpStyleFileAddress);
2285ffd83dbSDimitry Andric   msg << ". To disassemble specify an instruction count limit, start/stop "
2295ffd83dbSDimitry Andric          "addresses or use the --force option.";
2305ffd83dbSDimitry Andric   return llvm::createStringError(llvm::inconvertibleErrorCode(),
2315ffd83dbSDimitry Andric                                  msg.GetString());
2325ffd83dbSDimitry Andric }
2335ffd83dbSDimitry Andric 
2345ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetContainingAddressRanges()2355ffd83dbSDimitry Andric CommandObjectDisassemble::GetContainingAddressRanges() {
2365ffd83dbSDimitry Andric   std::vector<AddressRange> ranges;
2375ffd83dbSDimitry Andric   const auto &get_range = [&](Address addr) {
2385ffd83dbSDimitry Andric     ModuleSP module_sp(addr.GetModule());
2395ffd83dbSDimitry Andric     SymbolContext sc;
2405ffd83dbSDimitry Andric     bool resolve_tail_call_address = true;
2415ffd83dbSDimitry Andric     addr.GetModule()->ResolveSymbolContextForAddress(
2425ffd83dbSDimitry Andric         addr, eSymbolContextEverything, sc, resolve_tail_call_address);
2435ffd83dbSDimitry Andric     if (sc.function || sc.symbol) {
2445ffd83dbSDimitry Andric       AddressRange range;
2455ffd83dbSDimitry Andric       sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
2465ffd83dbSDimitry Andric                          false, range);
2475ffd83dbSDimitry Andric       ranges.push_back(range);
2485ffd83dbSDimitry Andric     }
2495ffd83dbSDimitry Andric   };
2505ffd83dbSDimitry Andric 
2515ffd83dbSDimitry Andric   Target &target = GetSelectedTarget();
2525ffd83dbSDimitry Andric   if (!target.GetSectionLoadList().IsEmpty()) {
2535ffd83dbSDimitry Andric     Address symbol_containing_address;
2545ffd83dbSDimitry Andric     if (target.GetSectionLoadList().ResolveLoadAddress(
2555ffd83dbSDimitry Andric             m_options.symbol_containing_addr, symbol_containing_address)) {
2565ffd83dbSDimitry Andric       get_range(symbol_containing_address);
2575ffd83dbSDimitry Andric     }
2585ffd83dbSDimitry Andric   } else {
2595ffd83dbSDimitry Andric     for (lldb::ModuleSP module_sp : target.GetImages().Modules()) {
2605ffd83dbSDimitry Andric       Address file_address;
2615ffd83dbSDimitry Andric       if (module_sp->ResolveFileAddress(m_options.symbol_containing_addr,
2625ffd83dbSDimitry Andric                                         file_address)) {
2635ffd83dbSDimitry Andric         get_range(file_address);
2645ffd83dbSDimitry Andric       }
2655ffd83dbSDimitry Andric     }
2665ffd83dbSDimitry Andric   }
2675ffd83dbSDimitry Andric 
2685ffd83dbSDimitry Andric   if (ranges.empty()) {
2695ffd83dbSDimitry Andric     return llvm::createStringError(
2705ffd83dbSDimitry Andric         llvm::inconvertibleErrorCode(),
2715ffd83dbSDimitry Andric         "Could not find function bounds for address 0x%" PRIx64,
2725ffd83dbSDimitry Andric         m_options.symbol_containing_addr);
2735ffd83dbSDimitry Andric   }
2745ffd83dbSDimitry Andric 
2755ffd83dbSDimitry Andric   if (llvm::Error err = CheckRangeSize(ranges[0], "the function"))
2765ffd83dbSDimitry Andric     return std::move(err);
2775ffd83dbSDimitry Andric   return ranges;
2785ffd83dbSDimitry Andric }
2795ffd83dbSDimitry Andric 
2805ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetCurrentFunctionRanges()2815ffd83dbSDimitry Andric CommandObjectDisassemble::GetCurrentFunctionRanges() {
2825ffd83dbSDimitry Andric   StackFrame *frame = m_exe_ctx.GetFramePtr();
2835ffd83dbSDimitry Andric   if (!frame) {
2845ffd83dbSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
2855ffd83dbSDimitry Andric                                    "Cannot disassemble around the current "
2865ffd83dbSDimitry Andric                                    "function without a selected frame.\n");
2875ffd83dbSDimitry Andric   }
2885ffd83dbSDimitry Andric   SymbolContext sc(
2895ffd83dbSDimitry Andric       frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
2905ffd83dbSDimitry Andric   AddressRange range;
2915ffd83dbSDimitry Andric   if (sc.function)
2925ffd83dbSDimitry Andric     range = sc.function->GetAddressRange();
2935ffd83dbSDimitry Andric   else if (sc.symbol && sc.symbol->ValueIsAddress()) {
2945ffd83dbSDimitry Andric     range = {sc.symbol->GetAddress(), sc.symbol->GetByteSize()};
2955ffd83dbSDimitry Andric   } else
2965ffd83dbSDimitry Andric     range = {frame->GetFrameCodeAddress(), default_disasm_byte_size};
2975ffd83dbSDimitry Andric 
2985ffd83dbSDimitry Andric   if (llvm::Error err = CheckRangeSize(range, "the current function"))
2995ffd83dbSDimitry Andric     return std::move(err);
3005ffd83dbSDimitry Andric   return std::vector<AddressRange>{range};
3015ffd83dbSDimitry Andric }
3025ffd83dbSDimitry Andric 
3035ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetCurrentLineRanges()3045ffd83dbSDimitry Andric CommandObjectDisassemble::GetCurrentLineRanges() {
3055ffd83dbSDimitry Andric   StackFrame *frame = m_exe_ctx.GetFramePtr();
3065ffd83dbSDimitry Andric   if (!frame) {
3075ffd83dbSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3085ffd83dbSDimitry Andric                                    "Cannot disassemble around the current "
3095ffd83dbSDimitry Andric                                    "line without a selected frame.\n");
3105ffd83dbSDimitry Andric   }
3115ffd83dbSDimitry Andric 
3125ffd83dbSDimitry Andric   LineEntry pc_line_entry(
3135ffd83dbSDimitry Andric       frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
3145ffd83dbSDimitry Andric   if (pc_line_entry.IsValid())
3155ffd83dbSDimitry Andric     return std::vector<AddressRange>{pc_line_entry.range};
3165ffd83dbSDimitry Andric 
3175ffd83dbSDimitry Andric   // No line entry, so just disassemble around the current pc
3185ffd83dbSDimitry Andric   m_options.show_mixed = false;
3195ffd83dbSDimitry Andric   return GetPCRanges();
3205ffd83dbSDimitry Andric }
3215ffd83dbSDimitry Andric 
3225ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetNameRanges(CommandReturnObject & result)3235ffd83dbSDimitry Andric CommandObjectDisassemble::GetNameRanges(CommandReturnObject &result) {
3245ffd83dbSDimitry Andric   ConstString name(m_options.func_name.c_str());
3255ffd83dbSDimitry Andric   const bool include_symbols = true;
3265ffd83dbSDimitry Andric   const bool include_inlines = true;
3275ffd83dbSDimitry Andric 
3285ffd83dbSDimitry Andric   // Find functions matching the given name.
3295ffd83dbSDimitry Andric   SymbolContextList sc_list;
3305ffd83dbSDimitry Andric   GetSelectedTarget().GetImages().FindFunctions(
3315ffd83dbSDimitry Andric       name, eFunctionNameTypeAuto, include_symbols, include_inlines, sc_list);
3325ffd83dbSDimitry Andric 
3335ffd83dbSDimitry Andric   std::vector<AddressRange> ranges;
3345ffd83dbSDimitry Andric   llvm::Error range_errs = llvm::Error::success();
3355ffd83dbSDimitry Andric   AddressRange range;
3365ffd83dbSDimitry Andric   const uint32_t scope =
3375ffd83dbSDimitry Andric       eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
3385ffd83dbSDimitry Andric   const bool use_inline_block_range = true;
3395ffd83dbSDimitry Andric   for (SymbolContext sc : sc_list.SymbolContexts()) {
3405ffd83dbSDimitry Andric     for (uint32_t range_idx = 0;
3415ffd83dbSDimitry Andric          sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
3425ffd83dbSDimitry Andric          ++range_idx) {
3435ffd83dbSDimitry Andric       if (llvm::Error err = CheckRangeSize(range, "a range"))
3445ffd83dbSDimitry Andric         range_errs = joinErrors(std::move(range_errs), std::move(err));
3455ffd83dbSDimitry Andric       else
3465ffd83dbSDimitry Andric         ranges.push_back(range);
3475ffd83dbSDimitry Andric     }
3485ffd83dbSDimitry Andric   }
3495ffd83dbSDimitry Andric   if (ranges.empty()) {
3505ffd83dbSDimitry Andric     if (range_errs)
3515ffd83dbSDimitry Andric       return std::move(range_errs);
3525ffd83dbSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3535ffd83dbSDimitry Andric                                    "Unable to find symbol with name '%s'.\n",
3545ffd83dbSDimitry Andric                                    name.GetCString());
3555ffd83dbSDimitry Andric   }
3565ffd83dbSDimitry Andric   if (range_errs)
3575ffd83dbSDimitry Andric     result.AppendWarning(toString(std::move(range_errs)));
3585ffd83dbSDimitry Andric   return ranges;
3595ffd83dbSDimitry Andric }
3605ffd83dbSDimitry Andric 
3615ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetPCRanges()3625ffd83dbSDimitry Andric CommandObjectDisassemble::GetPCRanges() {
3635ffd83dbSDimitry Andric   StackFrame *frame = m_exe_ctx.GetFramePtr();
3645ffd83dbSDimitry Andric   if (!frame) {
3655ffd83dbSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3665ffd83dbSDimitry Andric                                    "Cannot disassemble around the current "
3675ffd83dbSDimitry Andric                                    "PC without a selected frame.\n");
3685ffd83dbSDimitry Andric   }
3695ffd83dbSDimitry Andric 
3705ffd83dbSDimitry Andric   if (m_options.num_instructions == 0) {
3715ffd83dbSDimitry Andric     // Disassembling at the PC always disassembles some number of
3725ffd83dbSDimitry Andric     // instructions (not the whole function).
3735ffd83dbSDimitry Andric     m_options.num_instructions = default_disasm_num_ins;
3745ffd83dbSDimitry Andric   }
3755ffd83dbSDimitry Andric   return std::vector<AddressRange>{{frame->GetFrameCodeAddress(), 0}};
3765ffd83dbSDimitry Andric }
3775ffd83dbSDimitry Andric 
3785ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetStartEndAddressRanges()3795ffd83dbSDimitry Andric CommandObjectDisassemble::GetStartEndAddressRanges() {
3805ffd83dbSDimitry Andric   addr_t size = 0;
3815ffd83dbSDimitry Andric   if (m_options.end_addr != LLDB_INVALID_ADDRESS) {
3825ffd83dbSDimitry Andric     if (m_options.end_addr <= m_options.start_addr) {
3835ffd83dbSDimitry Andric       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3845ffd83dbSDimitry Andric                                      "End address before start address.");
3855ffd83dbSDimitry Andric     }
3865ffd83dbSDimitry Andric     size = m_options.end_addr - m_options.start_addr;
3875ffd83dbSDimitry Andric   }
3885ffd83dbSDimitry Andric   return std::vector<AddressRange>{{Address(m_options.start_addr), size}};
3895ffd83dbSDimitry Andric }
3905ffd83dbSDimitry Andric 
3915ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetRangesForSelectedMode(CommandReturnObject & result)3925ffd83dbSDimitry Andric CommandObjectDisassemble::GetRangesForSelectedMode(
3935ffd83dbSDimitry Andric     CommandReturnObject &result) {
3945ffd83dbSDimitry Andric   if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS)
3955ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetContainingAddressRanges();
3965ffd83dbSDimitry Andric   if (m_options.current_function)
3975ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetCurrentFunctionRanges();
3985ffd83dbSDimitry Andric   if (m_options.frame_line)
3995ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetCurrentLineRanges();
4005ffd83dbSDimitry Andric   if (!m_options.func_name.empty())
4015ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetNameRanges(result);
4025ffd83dbSDimitry Andric   if (m_options.start_addr != LLDB_INVALID_ADDRESS)
4035ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetStartEndAddressRanges();
4045ffd83dbSDimitry Andric   return CommandObjectDisassemble::GetPCRanges();
4055ffd83dbSDimitry Andric }
4065ffd83dbSDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)4070b57cec5SDimitry Andric bool CommandObjectDisassemble::DoExecute(Args &command,
4080b57cec5SDimitry Andric                                          CommandReturnObject &result) {
4099dba64beSDimitry Andric   Target *target = &GetSelectedTarget();
4109dba64beSDimitry Andric 
4110b57cec5SDimitry Andric   if (!m_options.arch.IsValid())
4120b57cec5SDimitry Andric     m_options.arch = target->GetArchitecture();
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   if (!m_options.arch.IsValid()) {
4150b57cec5SDimitry Andric     result.AppendError(
4160b57cec5SDimitry Andric         "use the --arch option or set the target architecture to disassemble");
4170b57cec5SDimitry Andric     return false;
4180b57cec5SDimitry Andric   }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   const char *plugin_name = m_options.GetPluginName();
4210b57cec5SDimitry Andric   const char *flavor_string = m_options.GetFlavorString();
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   DisassemblerSP disassembler =
4240b57cec5SDimitry Andric       Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   if (!disassembler) {
4270b57cec5SDimitry Andric     if (plugin_name) {
4280b57cec5SDimitry Andric       result.AppendErrorWithFormat(
4290b57cec5SDimitry Andric           "Unable to find Disassembler plug-in named '%s' that supports the "
4300b57cec5SDimitry Andric           "'%s' architecture.\n",
4310b57cec5SDimitry Andric           plugin_name, m_options.arch.GetArchitectureName());
4320b57cec5SDimitry Andric     } else
4330b57cec5SDimitry Andric       result.AppendErrorWithFormat(
4340b57cec5SDimitry Andric           "Unable to find Disassembler plug-in for the '%s' architecture.\n",
4350b57cec5SDimitry Andric           m_options.arch.GetArchitectureName());
4360b57cec5SDimitry Andric     return false;
437480093f4SDimitry Andric   } else if (flavor_string != nullptr && !disassembler->FlavorValidForArchSpec(
438480093f4SDimitry Andric                                              m_options.arch, flavor_string))
4390b57cec5SDimitry Andric     result.AppendWarningWithFormat(
4400b57cec5SDimitry Andric         "invalid disassembler flavor \"%s\", using default.\n", flavor_string);
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   result.SetStatus(eReturnStatusSuccessFinishResult);
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric   if (!command.empty()) {
4450b57cec5SDimitry Andric     result.AppendErrorWithFormat(
4460b57cec5SDimitry Andric         "\"disassemble\" arguments are specified as options.\n");
4470b57cec5SDimitry Andric     const int terminal_width =
4480b57cec5SDimitry Andric         GetCommandInterpreter().GetDebugger().GetTerminalWidth();
4490b57cec5SDimitry Andric     GetOptions()->GenerateOptionUsage(result.GetErrorStream(), this,
4500b57cec5SDimitry Andric                                       terminal_width);
4510b57cec5SDimitry Andric     return false;
4520b57cec5SDimitry Andric   }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric   if (m_options.show_mixed && m_options.num_lines_context == 0)
4550b57cec5SDimitry Andric     m_options.num_lines_context = 2;
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   // Always show the PC in the disassembly
4580b57cec5SDimitry Andric   uint32_t options = Disassembler::eOptionMarkPCAddress;
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric   // Mark the source line for the current PC only if we are doing mixed source
4610b57cec5SDimitry Andric   // and assembly
4620b57cec5SDimitry Andric   if (m_options.show_mixed)
4630b57cec5SDimitry Andric     options |= Disassembler::eOptionMarkPCSourceLine;
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   if (m_options.show_bytes)
4660b57cec5SDimitry Andric     options |= Disassembler::eOptionShowBytes;
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   if (m_options.raw)
4690b57cec5SDimitry Andric     options |= Disassembler::eOptionRawOuput;
4700b57cec5SDimitry Andric 
4715ffd83dbSDimitry Andric   llvm::Expected<std::vector<AddressRange>> ranges =
4725ffd83dbSDimitry Andric       GetRangesForSelectedMode(result);
4735ffd83dbSDimitry Andric   if (!ranges) {
4745ffd83dbSDimitry Andric     result.AppendError(toString(ranges.takeError()));
4755ffd83dbSDimitry Andric     return result.Succeeded();
4760b57cec5SDimitry Andric   }
4770b57cec5SDimitry Andric 
4785ffd83dbSDimitry Andric   bool print_sc_header = ranges->size() > 1;
4795ffd83dbSDimitry Andric   for (AddressRange cur_range : *ranges) {
4805ffd83dbSDimitry Andric     Disassembler::Limit limit;
4810b57cec5SDimitry Andric     if (m_options.num_instructions == 0) {
4825ffd83dbSDimitry Andric       limit = {Disassembler::Limit::Bytes, cur_range.GetByteSize()};
4835ffd83dbSDimitry Andric       if (limit.value == 0)
4845ffd83dbSDimitry Andric         limit.value = default_disasm_byte_size;
4850b57cec5SDimitry Andric     } else {
4865ffd83dbSDimitry Andric       limit = {Disassembler::Limit::Instructions, m_options.num_instructions};
4870b57cec5SDimitry Andric     }
4880b57cec5SDimitry Andric     if (Disassembler::Disassemble(
4890b57cec5SDimitry Andric             GetDebugger(), m_options.arch, plugin_name, flavor_string,
4905ffd83dbSDimitry Andric             m_exe_ctx, cur_range.GetBaseAddress(), limit, m_options.show_mixed,
4910b57cec5SDimitry Andric             m_options.show_mixed ? m_options.num_lines_context : 0, options,
4920b57cec5SDimitry Andric             result.GetOutputStream())) {
4930b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
4940b57cec5SDimitry Andric     } else {
4955ffd83dbSDimitry Andric       if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) {
4960b57cec5SDimitry Andric         result.AppendErrorWithFormat(
4975ffd83dbSDimitry Andric             "Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n",
4980b57cec5SDimitry Andric             m_options.symbol_containing_addr);
4990b57cec5SDimitry Andric       } else {
5000b57cec5SDimitry Andric         result.AppendErrorWithFormat(
5010b57cec5SDimitry Andric             "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
5029dba64beSDimitry Andric             cur_range.GetBaseAddress().GetLoadAddress(target));
5035ffd83dbSDimitry Andric       }
5040b57cec5SDimitry Andric     }
5050b57cec5SDimitry Andric     if (print_sc_header)
5065ffd83dbSDimitry Andric       result.GetOutputStream() << "\n";
5070b57cec5SDimitry Andric   }
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   return result.Succeeded();
5100b57cec5SDimitry Andric }
511