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"
15fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
160b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
170b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
180b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
190b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
200b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
210b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
220b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
230b57cec5SDimitry Andric #include "lldb/Target/Target.h"
240b57cec5SDimitry Andric 
255ffd83dbSDimitry Andric static constexpr unsigned default_disasm_byte_size = 32;
265ffd83dbSDimitry Andric static constexpr unsigned default_disasm_num_ins = 4;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace lldb;
290b57cec5SDimitry Andric using namespace lldb_private;
300b57cec5SDimitry Andric 
319dba64beSDimitry Andric #define LLDB_OPTIONS_disassemble
329dba64beSDimitry Andric #include "CommandOptions.inc"
330b57cec5SDimitry Andric 
CommandOptions()3404eeddc0SDimitry Andric CommandObjectDisassemble::CommandOptions::CommandOptions() {
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 
69753f127fSDimitry Andric   case 'k':
70753f127fSDimitry Andric     show_control_flow_kind = true;
71753f127fSDimitry Andric     break;
72753f127fSDimitry Andric 
730b57cec5SDimitry Andric   case 's': {
740b57cec5SDimitry Andric     start_addr = OptionArgParser::ToAddress(execution_context, option_arg,
750b57cec5SDimitry Andric                                             LLDB_INVALID_ADDRESS, &error);
760b57cec5SDimitry Andric     if (start_addr != LLDB_INVALID_ADDRESS)
770b57cec5SDimitry Andric       some_location_specified = true;
780b57cec5SDimitry Andric   } break;
790b57cec5SDimitry Andric   case 'e': {
800b57cec5SDimitry Andric     end_addr = OptionArgParser::ToAddress(execution_context, option_arg,
810b57cec5SDimitry Andric                                           LLDB_INVALID_ADDRESS, &error);
820b57cec5SDimitry Andric     if (end_addr != LLDB_INVALID_ADDRESS)
830b57cec5SDimitry Andric       some_location_specified = true;
840b57cec5SDimitry Andric   } break;
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   case 'n':
875ffd83dbSDimitry Andric     func_name.assign(std::string(option_arg));
880b57cec5SDimitry Andric     some_location_specified = true;
890b57cec5SDimitry Andric     break;
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   case 'p':
920b57cec5SDimitry Andric     at_pc = true;
930b57cec5SDimitry Andric     some_location_specified = true;
940b57cec5SDimitry Andric     break;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   case 'l':
970b57cec5SDimitry Andric     frame_line = true;
980b57cec5SDimitry Andric     // Disassemble the current source line kind of implies showing mixed source
990b57cec5SDimitry Andric     // code context.
1000b57cec5SDimitry Andric     show_mixed = true;
1010b57cec5SDimitry Andric     some_location_specified = true;
1020b57cec5SDimitry Andric     break;
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   case 'P':
1055ffd83dbSDimitry Andric     plugin_name.assign(std::string(option_arg));
1060b57cec5SDimitry Andric     break;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   case 'F': {
1090b57cec5SDimitry Andric     TargetSP target_sp =
1100b57cec5SDimitry Andric         execution_context ? execution_context->GetTargetSP() : TargetSP();
1110b57cec5SDimitry Andric     if (target_sp && (target_sp->GetArchitecture().GetTriple().getArch() ==
1120b57cec5SDimitry Andric                           llvm::Triple::x86 ||
1130b57cec5SDimitry Andric                       target_sp->GetArchitecture().GetTriple().getArch() ==
1140b57cec5SDimitry Andric                           llvm::Triple::x86_64)) {
1155ffd83dbSDimitry Andric       flavor_string.assign(std::string(option_arg));
1160b57cec5SDimitry Andric     } else
1170b57cec5SDimitry Andric       error.SetErrorStringWithFormat("Disassembler flavors are currently only "
1180b57cec5SDimitry Andric                                      "supported for x86 and x86_64 targets.");
1190b57cec5SDimitry Andric     break;
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   case 'r':
1230b57cec5SDimitry Andric     raw = true;
1240b57cec5SDimitry Andric     break;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   case 'f':
1270b57cec5SDimitry Andric     current_function = true;
1280b57cec5SDimitry Andric     some_location_specified = true;
1290b57cec5SDimitry Andric     break;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   case 'A':
1320b57cec5SDimitry Andric     if (execution_context) {
1330b57cec5SDimitry Andric       const auto &target_sp = execution_context->GetTargetSP();
1340b57cec5SDimitry Andric       auto platform_ptr = target_sp ? target_sp->GetPlatform().get() : nullptr;
1350b57cec5SDimitry Andric       arch = Platform::GetAugmentedArchSpec(platform_ptr, option_arg);
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric     break;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   case 'a': {
1400b57cec5SDimitry Andric     symbol_containing_addr = OptionArgParser::ToAddress(
1410b57cec5SDimitry Andric         execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
1420b57cec5SDimitry Andric     if (symbol_containing_addr != LLDB_INVALID_ADDRESS) {
1430b57cec5SDimitry Andric       some_location_specified = true;
1440b57cec5SDimitry Andric     }
1450b57cec5SDimitry Andric   } break;
1460b57cec5SDimitry Andric 
1475ffd83dbSDimitry Andric   case '\x01':
1485ffd83dbSDimitry Andric     force = true;
1495ffd83dbSDimitry Andric     break;
1505ffd83dbSDimitry Andric 
1510b57cec5SDimitry Andric   default:
1529dba64beSDimitry Andric     llvm_unreachable("Unimplemented option");
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   return error;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)1580b57cec5SDimitry Andric void CommandObjectDisassemble::CommandOptions::OptionParsingStarting(
1590b57cec5SDimitry Andric     ExecutionContext *execution_context) {
1600b57cec5SDimitry Andric   show_mixed = false;
1610b57cec5SDimitry Andric   show_bytes = false;
162753f127fSDimitry Andric   show_control_flow_kind = false;
1630b57cec5SDimitry Andric   num_lines_context = 0;
1640b57cec5SDimitry Andric   num_instructions = 0;
1650b57cec5SDimitry Andric   func_name.clear();
1660b57cec5SDimitry Andric   current_function = false;
1670b57cec5SDimitry Andric   at_pc = false;
1680b57cec5SDimitry Andric   frame_line = false;
1690b57cec5SDimitry Andric   start_addr = LLDB_INVALID_ADDRESS;
1700b57cec5SDimitry Andric   end_addr = LLDB_INVALID_ADDRESS;
1710b57cec5SDimitry Andric   symbol_containing_addr = LLDB_INVALID_ADDRESS;
1720b57cec5SDimitry Andric   raw = false;
1730b57cec5SDimitry Andric   plugin_name.clear();
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   Target *target =
1760b57cec5SDimitry Andric       execution_context ? execution_context->GetTargetPtr() : nullptr;
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   // This is a hack till we get the ability to specify features based on
1790b57cec5SDimitry Andric   // architecture.  For now GetDisassemblyFlavor is really only valid for x86
1800b57cec5SDimitry Andric   // (and for the llvm assembler plugin, but I'm papering over that since that
1810b57cec5SDimitry Andric   // is the only disassembler plugin we have...
1820b57cec5SDimitry Andric   if (target) {
1830b57cec5SDimitry Andric     if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86 ||
1840b57cec5SDimitry Andric         target->GetArchitecture().GetTriple().getArch() ==
1850b57cec5SDimitry Andric             llvm::Triple::x86_64) {
1860b57cec5SDimitry Andric       flavor_string.assign(target->GetDisassemblyFlavor());
1870b57cec5SDimitry Andric     } else
1880b57cec5SDimitry Andric       flavor_string.assign("default");
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   } else
1910b57cec5SDimitry Andric     flavor_string.assign("default");
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   arch.Clear();
1940b57cec5SDimitry Andric   some_location_specified = false;
1955ffd83dbSDimitry Andric   force = false;
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
OptionParsingFinished(ExecutionContext * execution_context)1980b57cec5SDimitry Andric Status CommandObjectDisassemble::CommandOptions::OptionParsingFinished(
1990b57cec5SDimitry Andric     ExecutionContext *execution_context) {
2000b57cec5SDimitry Andric   if (!some_location_specified)
2010b57cec5SDimitry Andric     current_function = true;
2020b57cec5SDimitry Andric   return Status();
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition>
GetDefinitions()2060b57cec5SDimitry Andric CommandObjectDisassemble::CommandOptions::GetDefinitions() {
207bdd1243dSDimitry Andric   return llvm::ArrayRef(g_disassemble_options);
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric // CommandObjectDisassemble
2110b57cec5SDimitry Andric 
CommandObjectDisassemble(CommandInterpreter & interpreter)2120b57cec5SDimitry Andric CommandObjectDisassemble::CommandObjectDisassemble(
2130b57cec5SDimitry Andric     CommandInterpreter &interpreter)
2140b57cec5SDimitry Andric     : CommandObjectParsed(
2150b57cec5SDimitry Andric           interpreter, "disassemble",
2160b57cec5SDimitry Andric           "Disassemble specified instructions in the current target.  "
2170b57cec5SDimitry Andric           "Defaults to the current function for the current thread and "
2180b57cec5SDimitry Andric           "stack frame.",
219972a253aSDimitry Andric           "disassemble [<cmd-options>]", eCommandRequiresTarget) {}
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric CommandObjectDisassemble::~CommandObjectDisassemble() = default;
2220b57cec5SDimitry Andric 
CheckRangeSize(const AddressRange & range,llvm::StringRef what)2235ffd83dbSDimitry Andric llvm::Error CommandObjectDisassemble::CheckRangeSize(const AddressRange &range,
2245ffd83dbSDimitry Andric                                                      llvm::StringRef what) {
2255ffd83dbSDimitry Andric   if (m_options.num_instructions > 0 || m_options.force ||
226fe6060f1SDimitry Andric       range.GetByteSize() < GetDebugger().GetStopDisassemblyMaxSize())
2275ffd83dbSDimitry Andric     return llvm::Error::success();
2285ffd83dbSDimitry Andric   StreamString msg;
2295ffd83dbSDimitry Andric   msg << "Not disassembling " << what << " because it is very large ";
2305ffd83dbSDimitry Andric   range.Dump(&msg, &GetSelectedTarget(), Address::DumpStyleLoadAddress,
2315ffd83dbSDimitry Andric              Address::DumpStyleFileAddress);
2325ffd83dbSDimitry Andric   msg << ". To disassemble specify an instruction count limit, start/stop "
2335ffd83dbSDimitry Andric          "addresses or use the --force option.";
2345ffd83dbSDimitry Andric   return llvm::createStringError(llvm::inconvertibleErrorCode(),
2355ffd83dbSDimitry Andric                                  msg.GetString());
2365ffd83dbSDimitry Andric }
2375ffd83dbSDimitry Andric 
2385ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetContainingAddressRanges()2395ffd83dbSDimitry Andric CommandObjectDisassemble::GetContainingAddressRanges() {
2405ffd83dbSDimitry Andric   std::vector<AddressRange> ranges;
2415ffd83dbSDimitry Andric   const auto &get_range = [&](Address addr) {
2425ffd83dbSDimitry Andric     ModuleSP module_sp(addr.GetModule());
2435ffd83dbSDimitry Andric     SymbolContext sc;
2445ffd83dbSDimitry Andric     bool resolve_tail_call_address = true;
2455ffd83dbSDimitry Andric     addr.GetModule()->ResolveSymbolContextForAddress(
2465ffd83dbSDimitry Andric         addr, eSymbolContextEverything, sc, resolve_tail_call_address);
2475ffd83dbSDimitry Andric     if (sc.function || sc.symbol) {
2485ffd83dbSDimitry Andric       AddressRange range;
2495ffd83dbSDimitry Andric       sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
2505ffd83dbSDimitry Andric                          false, range);
2515ffd83dbSDimitry Andric       ranges.push_back(range);
2525ffd83dbSDimitry Andric     }
2535ffd83dbSDimitry Andric   };
2545ffd83dbSDimitry Andric 
2555ffd83dbSDimitry Andric   Target &target = GetSelectedTarget();
2565ffd83dbSDimitry Andric   if (!target.GetSectionLoadList().IsEmpty()) {
2575ffd83dbSDimitry Andric     Address symbol_containing_address;
2585ffd83dbSDimitry Andric     if (target.GetSectionLoadList().ResolveLoadAddress(
2595ffd83dbSDimitry Andric             m_options.symbol_containing_addr, symbol_containing_address)) {
2605ffd83dbSDimitry Andric       get_range(symbol_containing_address);
2615ffd83dbSDimitry Andric     }
2625ffd83dbSDimitry Andric   } else {
2635ffd83dbSDimitry Andric     for (lldb::ModuleSP module_sp : target.GetImages().Modules()) {
2645ffd83dbSDimitry Andric       Address file_address;
2655ffd83dbSDimitry Andric       if (module_sp->ResolveFileAddress(m_options.symbol_containing_addr,
2665ffd83dbSDimitry Andric                                         file_address)) {
2675ffd83dbSDimitry Andric         get_range(file_address);
2685ffd83dbSDimitry Andric       }
2695ffd83dbSDimitry Andric     }
2705ffd83dbSDimitry Andric   }
2715ffd83dbSDimitry Andric 
2725ffd83dbSDimitry Andric   if (ranges.empty()) {
2735ffd83dbSDimitry Andric     return llvm::createStringError(
2745ffd83dbSDimitry Andric         llvm::inconvertibleErrorCode(),
2755ffd83dbSDimitry Andric         "Could not find function bounds for address 0x%" PRIx64,
2765ffd83dbSDimitry Andric         m_options.symbol_containing_addr);
2775ffd83dbSDimitry Andric   }
2785ffd83dbSDimitry Andric 
2795ffd83dbSDimitry Andric   if (llvm::Error err = CheckRangeSize(ranges[0], "the function"))
2805ffd83dbSDimitry Andric     return std::move(err);
2815ffd83dbSDimitry Andric   return ranges;
2825ffd83dbSDimitry Andric }
2835ffd83dbSDimitry Andric 
2845ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetCurrentFunctionRanges()2855ffd83dbSDimitry Andric CommandObjectDisassemble::GetCurrentFunctionRanges() {
28681ad6265SDimitry Andric   Process *process = m_exe_ctx.GetProcessPtr();
2875ffd83dbSDimitry Andric   StackFrame *frame = m_exe_ctx.GetFramePtr();
2885ffd83dbSDimitry Andric   if (!frame) {
28981ad6265SDimitry Andric     if (process) {
29081ad6265SDimitry Andric       return llvm::createStringError(
29181ad6265SDimitry Andric           llvm::inconvertibleErrorCode(),
29281ad6265SDimitry Andric           "Cannot disassemble around the current "
29381ad6265SDimitry Andric           "function without the process being stopped.\n");
29481ad6265SDimitry Andric     } else {
2955ffd83dbSDimitry Andric       return llvm::createStringError(llvm::inconvertibleErrorCode(),
2965ffd83dbSDimitry Andric                                      "Cannot disassemble around the current "
29781ad6265SDimitry Andric                                      "function without a selected frame: "
29881ad6265SDimitry Andric                                      "no currently running process.\n");
29981ad6265SDimitry Andric     }
3005ffd83dbSDimitry Andric   }
3015ffd83dbSDimitry Andric   SymbolContext sc(
3025ffd83dbSDimitry Andric       frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
3035ffd83dbSDimitry Andric   AddressRange range;
3045ffd83dbSDimitry Andric   if (sc.function)
3055ffd83dbSDimitry Andric     range = sc.function->GetAddressRange();
3065ffd83dbSDimitry Andric   else if (sc.symbol && sc.symbol->ValueIsAddress()) {
3075ffd83dbSDimitry Andric     range = {sc.symbol->GetAddress(), sc.symbol->GetByteSize()};
3085ffd83dbSDimitry Andric   } else
3095ffd83dbSDimitry Andric     range = {frame->GetFrameCodeAddress(), default_disasm_byte_size};
3105ffd83dbSDimitry Andric 
3115ffd83dbSDimitry Andric   if (llvm::Error err = CheckRangeSize(range, "the current function"))
3125ffd83dbSDimitry Andric     return std::move(err);
3135ffd83dbSDimitry Andric   return std::vector<AddressRange>{range};
3145ffd83dbSDimitry Andric }
3155ffd83dbSDimitry Andric 
3165ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetCurrentLineRanges()3175ffd83dbSDimitry Andric CommandObjectDisassemble::GetCurrentLineRanges() {
31881ad6265SDimitry Andric   Process *process = m_exe_ctx.GetProcessPtr();
3195ffd83dbSDimitry Andric   StackFrame *frame = m_exe_ctx.GetFramePtr();
3205ffd83dbSDimitry Andric   if (!frame) {
32181ad6265SDimitry Andric     if (process) {
32281ad6265SDimitry Andric       return llvm::createStringError(
32381ad6265SDimitry Andric           llvm::inconvertibleErrorCode(),
32481ad6265SDimitry Andric           "Cannot disassemble around the current "
32581ad6265SDimitry Andric           "function without the process being stopped.\n");
32681ad6265SDimitry Andric     } else {
3275ffd83dbSDimitry Andric       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3285ffd83dbSDimitry Andric                                      "Cannot disassemble around the current "
32981ad6265SDimitry Andric                                      "line without a selected frame: "
33081ad6265SDimitry Andric                                      "no currently running process.\n");
33181ad6265SDimitry Andric     }
3325ffd83dbSDimitry Andric   }
3335ffd83dbSDimitry Andric 
3345ffd83dbSDimitry Andric   LineEntry pc_line_entry(
3355ffd83dbSDimitry Andric       frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
3365ffd83dbSDimitry Andric   if (pc_line_entry.IsValid())
3375ffd83dbSDimitry Andric     return std::vector<AddressRange>{pc_line_entry.range};
3385ffd83dbSDimitry Andric 
3395ffd83dbSDimitry Andric   // No line entry, so just disassemble around the current pc
3405ffd83dbSDimitry Andric   m_options.show_mixed = false;
3415ffd83dbSDimitry Andric   return GetPCRanges();
3425ffd83dbSDimitry Andric }
3435ffd83dbSDimitry Andric 
3445ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetNameRanges(CommandReturnObject & result)3455ffd83dbSDimitry Andric CommandObjectDisassemble::GetNameRanges(CommandReturnObject &result) {
3465ffd83dbSDimitry Andric   ConstString name(m_options.func_name.c_str());
347349cc55cSDimitry Andric 
348349cc55cSDimitry Andric   ModuleFunctionSearchOptions function_options;
349349cc55cSDimitry Andric   function_options.include_symbols = true;
350349cc55cSDimitry Andric   function_options.include_inlines = true;
3515ffd83dbSDimitry Andric 
3525ffd83dbSDimitry Andric   // Find functions matching the given name.
3535ffd83dbSDimitry Andric   SymbolContextList sc_list;
354349cc55cSDimitry Andric   GetSelectedTarget().GetImages().FindFunctions(name, eFunctionNameTypeAuto,
355349cc55cSDimitry Andric                                                 function_options, sc_list);
3565ffd83dbSDimitry Andric 
3575ffd83dbSDimitry Andric   std::vector<AddressRange> ranges;
3585ffd83dbSDimitry Andric   llvm::Error range_errs = llvm::Error::success();
3595ffd83dbSDimitry Andric   AddressRange range;
3605ffd83dbSDimitry Andric   const uint32_t scope =
3615ffd83dbSDimitry Andric       eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
3625ffd83dbSDimitry Andric   const bool use_inline_block_range = true;
3635ffd83dbSDimitry Andric   for (SymbolContext sc : sc_list.SymbolContexts()) {
3645ffd83dbSDimitry Andric     for (uint32_t range_idx = 0;
3655ffd83dbSDimitry Andric          sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
3665ffd83dbSDimitry Andric          ++range_idx) {
3675ffd83dbSDimitry Andric       if (llvm::Error err = CheckRangeSize(range, "a range"))
3685ffd83dbSDimitry Andric         range_errs = joinErrors(std::move(range_errs), std::move(err));
3695ffd83dbSDimitry Andric       else
3705ffd83dbSDimitry Andric         ranges.push_back(range);
3715ffd83dbSDimitry Andric     }
3725ffd83dbSDimitry Andric   }
3735ffd83dbSDimitry Andric   if (ranges.empty()) {
3745ffd83dbSDimitry Andric     if (range_errs)
3755ffd83dbSDimitry Andric       return std::move(range_errs);
3765ffd83dbSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3775ffd83dbSDimitry Andric                                    "Unable to find symbol with name '%s'.\n",
3785ffd83dbSDimitry Andric                                    name.GetCString());
3795ffd83dbSDimitry Andric   }
3805ffd83dbSDimitry Andric   if (range_errs)
3815ffd83dbSDimitry Andric     result.AppendWarning(toString(std::move(range_errs)));
3825ffd83dbSDimitry Andric   return ranges;
3835ffd83dbSDimitry Andric }
3845ffd83dbSDimitry Andric 
3855ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetPCRanges()3865ffd83dbSDimitry Andric CommandObjectDisassemble::GetPCRanges() {
38781ad6265SDimitry Andric   Process *process = m_exe_ctx.GetProcessPtr();
3885ffd83dbSDimitry Andric   StackFrame *frame = m_exe_ctx.GetFramePtr();
3895ffd83dbSDimitry Andric   if (!frame) {
39081ad6265SDimitry Andric     if (process) {
39181ad6265SDimitry Andric       return llvm::createStringError(
39281ad6265SDimitry Andric           llvm::inconvertibleErrorCode(),
39381ad6265SDimitry Andric           "Cannot disassemble around the current "
39481ad6265SDimitry Andric           "function without the process being stopped.\n");
39581ad6265SDimitry Andric     } else {
3965ffd83dbSDimitry Andric       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3975ffd83dbSDimitry Andric                                      "Cannot disassemble around the current "
39881ad6265SDimitry Andric                                      "PC without a selected frame: "
39981ad6265SDimitry Andric                                      "no currently running process.\n");
40081ad6265SDimitry Andric     }
4015ffd83dbSDimitry Andric   }
4025ffd83dbSDimitry Andric 
4035ffd83dbSDimitry Andric   if (m_options.num_instructions == 0) {
4045ffd83dbSDimitry Andric     // Disassembling at the PC always disassembles some number of
4055ffd83dbSDimitry Andric     // instructions (not the whole function).
4065ffd83dbSDimitry Andric     m_options.num_instructions = default_disasm_num_ins;
4075ffd83dbSDimitry Andric   }
4085ffd83dbSDimitry Andric   return std::vector<AddressRange>{{frame->GetFrameCodeAddress(), 0}};
4095ffd83dbSDimitry Andric }
4105ffd83dbSDimitry Andric 
4115ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetStartEndAddressRanges()4125ffd83dbSDimitry Andric CommandObjectDisassemble::GetStartEndAddressRanges() {
4135ffd83dbSDimitry Andric   addr_t size = 0;
4145ffd83dbSDimitry Andric   if (m_options.end_addr != LLDB_INVALID_ADDRESS) {
4155ffd83dbSDimitry Andric     if (m_options.end_addr <= m_options.start_addr) {
4165ffd83dbSDimitry Andric       return llvm::createStringError(llvm::inconvertibleErrorCode(),
4175ffd83dbSDimitry Andric                                      "End address before start address.");
4185ffd83dbSDimitry Andric     }
4195ffd83dbSDimitry Andric     size = m_options.end_addr - m_options.start_addr;
4205ffd83dbSDimitry Andric   }
4215ffd83dbSDimitry Andric   return std::vector<AddressRange>{{Address(m_options.start_addr), size}};
4225ffd83dbSDimitry Andric }
4235ffd83dbSDimitry Andric 
4245ffd83dbSDimitry Andric llvm::Expected<std::vector<AddressRange>>
GetRangesForSelectedMode(CommandReturnObject & result)4255ffd83dbSDimitry Andric CommandObjectDisassemble::GetRangesForSelectedMode(
4265ffd83dbSDimitry Andric     CommandReturnObject &result) {
4275ffd83dbSDimitry Andric   if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS)
4285ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetContainingAddressRanges();
4295ffd83dbSDimitry Andric   if (m_options.current_function)
4305ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetCurrentFunctionRanges();
4315ffd83dbSDimitry Andric   if (m_options.frame_line)
4325ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetCurrentLineRanges();
4335ffd83dbSDimitry Andric   if (!m_options.func_name.empty())
4345ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetNameRanges(result);
4355ffd83dbSDimitry Andric   if (m_options.start_addr != LLDB_INVALID_ADDRESS)
4365ffd83dbSDimitry Andric     return CommandObjectDisassemble::GetStartEndAddressRanges();
4375ffd83dbSDimitry Andric   return CommandObjectDisassemble::GetPCRanges();
4385ffd83dbSDimitry Andric }
4395ffd83dbSDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)440*c9157d92SDimitry Andric void CommandObjectDisassemble::DoExecute(Args &command,
4410b57cec5SDimitry Andric                                          CommandReturnObject &result) {
4429dba64beSDimitry Andric   Target *target = &GetSelectedTarget();
4439dba64beSDimitry Andric 
4440b57cec5SDimitry Andric   if (!m_options.arch.IsValid())
4450b57cec5SDimitry Andric     m_options.arch = target->GetArchitecture();
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   if (!m_options.arch.IsValid()) {
4480b57cec5SDimitry Andric     result.AppendError(
4490b57cec5SDimitry Andric         "use the --arch option or set the target architecture to disassemble");
450*c9157d92SDimitry Andric     return;
4510b57cec5SDimitry Andric   }
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   const char *plugin_name = m_options.GetPluginName();
4540b57cec5SDimitry Andric   const char *flavor_string = m_options.GetFlavorString();
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   DisassemblerSP disassembler =
4570b57cec5SDimitry Andric       Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   if (!disassembler) {
4600b57cec5SDimitry Andric     if (plugin_name) {
4610b57cec5SDimitry Andric       result.AppendErrorWithFormat(
4620b57cec5SDimitry Andric           "Unable to find Disassembler plug-in named '%s' that supports the "
4630b57cec5SDimitry Andric           "'%s' architecture.\n",
4640b57cec5SDimitry Andric           plugin_name, m_options.arch.GetArchitectureName());
4650b57cec5SDimitry Andric     } else
4660b57cec5SDimitry Andric       result.AppendErrorWithFormat(
4670b57cec5SDimitry Andric           "Unable to find Disassembler plug-in for the '%s' architecture.\n",
4680b57cec5SDimitry Andric           m_options.arch.GetArchitectureName());
469*c9157d92SDimitry Andric     return;
470480093f4SDimitry Andric   } else if (flavor_string != nullptr && !disassembler->FlavorValidForArchSpec(
471480093f4SDimitry Andric                                              m_options.arch, flavor_string))
4720b57cec5SDimitry Andric     result.AppendWarningWithFormat(
4730b57cec5SDimitry Andric         "invalid disassembler flavor \"%s\", using default.\n", flavor_string);
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   result.SetStatus(eReturnStatusSuccessFinishResult);
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   if (!command.empty()) {
4780b57cec5SDimitry Andric     result.AppendErrorWithFormat(
4790b57cec5SDimitry Andric         "\"disassemble\" arguments are specified as options.\n");
4800b57cec5SDimitry Andric     const int terminal_width =
4810b57cec5SDimitry Andric         GetCommandInterpreter().GetDebugger().GetTerminalWidth();
48281ad6265SDimitry Andric     GetOptions()->GenerateOptionUsage(result.GetErrorStream(), *this,
4830b57cec5SDimitry Andric                                       terminal_width);
484*c9157d92SDimitry Andric     return;
4850b57cec5SDimitry Andric   }
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   if (m_options.show_mixed && m_options.num_lines_context == 0)
4880b57cec5SDimitry Andric     m_options.num_lines_context = 2;
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   // Always show the PC in the disassembly
4910b57cec5SDimitry Andric   uint32_t options = Disassembler::eOptionMarkPCAddress;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   // Mark the source line for the current PC only if we are doing mixed source
4940b57cec5SDimitry Andric   // and assembly
4950b57cec5SDimitry Andric   if (m_options.show_mixed)
4960b57cec5SDimitry Andric     options |= Disassembler::eOptionMarkPCSourceLine;
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   if (m_options.show_bytes)
4990b57cec5SDimitry Andric     options |= Disassembler::eOptionShowBytes;
5000b57cec5SDimitry Andric 
501753f127fSDimitry Andric   if (m_options.show_control_flow_kind)
502753f127fSDimitry Andric     options |= Disassembler::eOptionShowControlFlowKind;
503753f127fSDimitry Andric 
5040b57cec5SDimitry Andric   if (m_options.raw)
5050b57cec5SDimitry Andric     options |= Disassembler::eOptionRawOuput;
5060b57cec5SDimitry Andric 
5075ffd83dbSDimitry Andric   llvm::Expected<std::vector<AddressRange>> ranges =
5085ffd83dbSDimitry Andric       GetRangesForSelectedMode(result);
5095ffd83dbSDimitry Andric   if (!ranges) {
5105ffd83dbSDimitry Andric     result.AppendError(toString(ranges.takeError()));
511*c9157d92SDimitry Andric     return;
5120b57cec5SDimitry Andric   }
5130b57cec5SDimitry Andric 
5145ffd83dbSDimitry Andric   bool print_sc_header = ranges->size() > 1;
5155ffd83dbSDimitry Andric   for (AddressRange cur_range : *ranges) {
5165ffd83dbSDimitry Andric     Disassembler::Limit limit;
5170b57cec5SDimitry Andric     if (m_options.num_instructions == 0) {
5185ffd83dbSDimitry Andric       limit = {Disassembler::Limit::Bytes, cur_range.GetByteSize()};
5195ffd83dbSDimitry Andric       if (limit.value == 0)
5205ffd83dbSDimitry Andric         limit.value = default_disasm_byte_size;
5210b57cec5SDimitry Andric     } else {
5225ffd83dbSDimitry Andric       limit = {Disassembler::Limit::Instructions, m_options.num_instructions};
5230b57cec5SDimitry Andric     }
5240b57cec5SDimitry Andric     if (Disassembler::Disassemble(
5250b57cec5SDimitry Andric             GetDebugger(), m_options.arch, plugin_name, flavor_string,
5265ffd83dbSDimitry Andric             m_exe_ctx, cur_range.GetBaseAddress(), limit, m_options.show_mixed,
5270b57cec5SDimitry Andric             m_options.show_mixed ? m_options.num_lines_context : 0, options,
5280b57cec5SDimitry Andric             result.GetOutputStream())) {
5290b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
5300b57cec5SDimitry Andric     } else {
5315ffd83dbSDimitry Andric       if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) {
5320b57cec5SDimitry Andric         result.AppendErrorWithFormat(
5335ffd83dbSDimitry Andric             "Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n",
5340b57cec5SDimitry Andric             m_options.symbol_containing_addr);
5350b57cec5SDimitry Andric       } else {
5360b57cec5SDimitry Andric         result.AppendErrorWithFormat(
5370b57cec5SDimitry Andric             "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
5389dba64beSDimitry Andric             cur_range.GetBaseAddress().GetLoadAddress(target));
5395ffd83dbSDimitry Andric       }
5400b57cec5SDimitry Andric     }
5410b57cec5SDimitry Andric     if (print_sc_header)
5425ffd83dbSDimitry Andric       result.GetOutputStream() << "\n";
5430b57cec5SDimitry Andric   }
5440b57cec5SDimitry Andric }
545