130fdc8d8SChris Lattner //===-- CommandObjectDisassemble.cpp ----------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner // C Includes 1130fdc8d8SChris Lattner // C++ Includes 1230fdc8d8SChris Lattner // Other libraries and framework includes 1330fdc8d8SChris Lattner // Project includes 1426cac3afSEugene Zelenko #include "CommandObjectDisassemble.h" 1530fdc8d8SChris Lattner #include "lldb/Core/AddressRange.h" 161f746071SGreg Clayton #include "lldb/Core/Disassembler.h" 17801237a2SJason Molenda #include "lldb/Core/Module.h" 181f746071SGreg Clayton #include "lldb/Core/SourceManager.h" 195275aaa0SVince Harron #include "lldb/Host/StringConvert.h" 2030fdc8d8SChris Lattner #include "lldb/Interpreter/CommandCompletions.h" 2130fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h" 2230fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 2340af72e1SJim Ingham #include "lldb/Interpreter/Options.h" 241f746071SGreg Clayton #include "lldb/Symbol/Function.h" 2530fdc8d8SChris Lattner #include "lldb/Symbol/Symbol.h" 2630fdc8d8SChris Lattner #include "lldb/Target/Process.h" 27d5944cd1SGreg Clayton #include "lldb/Target/SectionLoadList.h" 28b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h" 2930fdc8d8SChris Lattner #include "lldb/Target/Target.h" 3030fdc8d8SChris Lattner 3130fdc8d8SChris Lattner #define DEFAULT_DISASM_BYTE_SIZE 32 3237023b06SJim Ingham #define DEFAULT_DISASM_NUM_INS 4 3330fdc8d8SChris Lattner 3430fdc8d8SChris Lattner using namespace lldb; 3530fdc8d8SChris Lattner using namespace lldb_private; 3630fdc8d8SChris Lattner 371f0f5b5bSZachary Turner static OptionDefinition g_disassemble_options[] = { 381f0f5b5bSZachary Turner // clang-format off 391f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "bytes", 'b', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Show opcode bytes when disassembling." }, 401f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "context", 'C', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNumLines, "Number of context lines of source to show." }, 411f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "mixed", 'm', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable mixed source and assembly display." }, 421f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "raw", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Print raw disassembly with no symbol information." }, 431f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use." }, 441f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "flavor", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. " 451f0f5b5bSZachary Turner "Currently the only valid options are default, and for Intel " 461f0f5b5bSZachary Turner "architectures, att and intel." }, 471f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "arch", 'A', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeArchitecture, "Specify the architecture to use from cross disassembly." }, 481f0f5b5bSZachary Turner { LLDB_OPT_SET_1 | 491f0f5b5bSZachary Turner LLDB_OPT_SET_2, true, "start-address", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Address at which to start disassembling." }, 501f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "end-address", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling." }, 511f0f5b5bSZachary Turner { LLDB_OPT_SET_2 | 521f0f5b5bSZachary Turner LLDB_OPT_SET_3 | 531f0f5b5bSZachary Turner LLDB_OPT_SET_4 | 541f0f5b5bSZachary Turner LLDB_OPT_SET_5, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNumLines, "Number of instructions to display." }, 551f0f5b5bSZachary Turner { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name." }, 561f0f5b5bSZachary Turner { LLDB_OPT_SET_4, false, "frame", 'f', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble from the start of the current frame's function." }, 571f0f5b5bSZachary Turner { LLDB_OPT_SET_5, false, "pc", 'p', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble around the current pc." }, 581f0f5b5bSZachary Turner { LLDB_OPT_SET_6, false, "line", 'l', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble the current frame's current source line instructions if there is debug line " 591f0f5b5bSZachary Turner "table information, else disassemble around the pc." }, 601f0f5b5bSZachary Turner { LLDB_OPT_SET_7, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address." }, 611f0f5b5bSZachary Turner // clang-format on 621f0f5b5bSZachary Turner }; 631f0f5b5bSZachary Turner 64b9c1b51eSKate Stone CommandObjectDisassemble::CommandOptions::CommandOptions() 65b9c1b51eSKate Stone : Options(), num_lines_context(0), num_instructions(0), func_name(), 66b9c1b51eSKate Stone current_function(false), start_addr(), end_addr(), at_pc(false), 67b9c1b51eSKate Stone frame_line(false), plugin_name(), flavor_string(), arch(), 68b9c1b51eSKate Stone some_location_specified(false), symbol_containing_addr() { 69e1cfbc79STodd Fiala OptionParsingStarting(nullptr); 7030fdc8d8SChris Lattner } 7130fdc8d8SChris Lattner 7226cac3afSEugene Zelenko CommandObjectDisassemble::CommandOptions::~CommandOptions() = default; 7330fdc8d8SChris Lattner 74b9c1b51eSKate Stone Error CommandObjectDisassemble::CommandOptions::SetOptionValue( 75b9c1b51eSKate Stone uint32_t option_idx, const char *option_arg, 76b9c1b51eSKate Stone ExecutionContext *execution_context) { 7730fdc8d8SChris Lattner Error error; 7830fdc8d8SChris Lattner 793bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 8030fdc8d8SChris Lattner 8137023b06SJim Ingham bool success; 8237023b06SJim Ingham 83b9c1b51eSKate Stone switch (short_option) { 8430fdc8d8SChris Lattner case 'm': 8530fdc8d8SChris Lattner show_mixed = true; 8630fdc8d8SChris Lattner break; 8730fdc8d8SChris Lattner 88357132ebSGreg Clayton case 'C': 895275aaa0SVince Harron num_lines_context = StringConvert::ToUInt32(option_arg, 0, 0, &success); 9037023b06SJim Ingham if (!success) 91b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid num context lines string: \"%s\"", 92b9c1b51eSKate Stone option_arg); 9337023b06SJim Ingham break; 9437023b06SJim Ingham 9530fdc8d8SChris Lattner case 'c': 965275aaa0SVince Harron num_instructions = StringConvert::ToUInt32(option_arg, 0, 0, &success); 9737023b06SJim Ingham if (!success) 98b9c1b51eSKate Stone error.SetErrorStringWithFormat( 99b9c1b51eSKate Stone "invalid num of instructions string: \"%s\"", option_arg); 10030fdc8d8SChris Lattner break; 10130fdc8d8SChris Lattner 10230fdc8d8SChris Lattner case 'b': 10330fdc8d8SChris Lattner show_bytes = true; 10430fdc8d8SChris Lattner break; 10530fdc8d8SChris Lattner 106b9c1b51eSKate Stone case 's': { 107e1cfbc79STodd Fiala start_addr = Args::StringToAddress(execution_context, option_arg, 108e1cfbc79STodd Fiala LLDB_INVALID_ADDRESS, &error); 109b9d5df58SGreg Clayton if (start_addr != LLDB_INVALID_ADDRESS) 1103555b5d7SJim Ingham some_location_specified = true; 111b9c1b51eSKate Stone } break; 112b9c1b51eSKate Stone case 'e': { 113e1cfbc79STodd Fiala end_addr = Args::StringToAddress(execution_context, option_arg, 114e1cfbc79STodd Fiala LLDB_INVALID_ADDRESS, &error); 115b9d5df58SGreg Clayton if (end_addr != LLDB_INVALID_ADDRESS) 1163555b5d7SJim Ingham some_location_specified = true; 117b9c1b51eSKate Stone } break; 11826cac3afSEugene Zelenko 11930fdc8d8SChris Lattner case 'n': 12032e0a750SGreg Clayton func_name.assign(option_arg); 1213555b5d7SJim Ingham some_location_specified = true; 12230fdc8d8SChris Lattner break; 12330fdc8d8SChris Lattner 12437023b06SJim Ingham case 'p': 12532e0a750SGreg Clayton at_pc = true; 1263555b5d7SJim Ingham some_location_specified = true; 12732e0a750SGreg Clayton break; 12832e0a750SGreg Clayton 12932e0a750SGreg Clayton case 'l': 13032e0a750SGreg Clayton frame_line = true; 13132e0a750SGreg Clayton // Disassemble the current source line kind of implies showing mixed 13232e0a750SGreg Clayton // source code context. 13332e0a750SGreg Clayton show_mixed = true; 1343555b5d7SJim Ingham some_location_specified = true; 13537023b06SJim Ingham break; 13637023b06SJim Ingham 1371080edbcSGreg Clayton case 'P': 13832e0a750SGreg Clayton plugin_name.assign(option_arg); 1391080edbcSGreg Clayton break; 1401080edbcSGreg Clayton 141b9c1b51eSKate Stone case 'F': { 142b9c1b51eSKate Stone TargetSP target_sp = 143b9c1b51eSKate Stone execution_context ? execution_context->GetTargetSP() : TargetSP(); 144b9c1b51eSKate Stone if (target_sp && (target_sp->GetArchitecture().GetTriple().getArch() == 145b9c1b51eSKate Stone llvm::Triple::x86 || 146b9c1b51eSKate Stone target_sp->GetArchitecture().GetTriple().getArch() == 147b9c1b51eSKate Stone llvm::Triple::x86_64)) { 1480f063ba6SJim Ingham flavor_string.assign(option_arg); 149b9c1b51eSKate Stone } else 150b9c1b51eSKate Stone error.SetErrorStringWithFormat("Disassembler flavors are currently only " 151b9c1b51eSKate Stone "supported for x86 and x86_64 targets."); 1520f063ba6SJim Ingham break; 1530f063ba6SJim Ingham } 15426cac3afSEugene Zelenko 15530fdc8d8SChris Lattner case 'r': 15630fdc8d8SChris Lattner raw = true; 15730fdc8d8SChris Lattner break; 15830fdc8d8SChris Lattner 1598ceb8ba2SJohnny Chen case 'f': 1601fb2e7dfSGreg Clayton current_function = true; 1613555b5d7SJim Ingham some_location_specified = true; 1628ceb8ba2SJohnny Chen break; 1638ceb8ba2SJohnny Chen 164801237a2SJason Molenda case 'A': 165b9c1b51eSKate Stone if (execution_context) { 166b9c1b51eSKate Stone auto target_sp = 167b9c1b51eSKate Stone execution_context ? execution_context->GetTargetSP() : TargetSP(); 168b9c1b51eSKate Stone auto platform_sp = target_sp ? target_sp->GetPlatform() : PlatformSP(); 169e1cfbc79STodd Fiala if (!arch.SetTriple(option_arg, platform_sp.get())) 17070512317SGreg Clayton arch.SetTriple(option_arg); 171e1cfbc79STodd Fiala } 172357132ebSGreg Clayton break; 173357132ebSGreg Clayton 174b9c1b51eSKate Stone case 'a': { 175b9c1b51eSKate Stone symbol_containing_addr = Args::StringToAddress( 176b9c1b51eSKate Stone execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); 177b9c1b51eSKate Stone if (symbol_containing_addr != LLDB_INVALID_ADDRESS) { 178801237a2SJason Molenda some_location_specified = true; 179801237a2SJason Molenda } 180b9c1b51eSKate Stone } break; 181801237a2SJason Molenda 18230fdc8d8SChris Lattner default: 183b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized short option '%c'", 184b9c1b51eSKate Stone short_option); 18530fdc8d8SChris Lattner break; 18630fdc8d8SChris Lattner } 18730fdc8d8SChris Lattner 18830fdc8d8SChris Lattner return error; 18930fdc8d8SChris Lattner } 19030fdc8d8SChris Lattner 191b9c1b51eSKate Stone void CommandObjectDisassemble::CommandOptions::OptionParsingStarting( 192b9c1b51eSKate Stone ExecutionContext *execution_context) { 19330fdc8d8SChris Lattner show_mixed = false; 19430fdc8d8SChris Lattner show_bytes = false; 19530fdc8d8SChris Lattner num_lines_context = 0; 19637023b06SJim Ingham num_instructions = 0; 19732e0a750SGreg Clayton func_name.clear(); 1981fb2e7dfSGreg Clayton current_function = false; 19932e0a750SGreg Clayton at_pc = false; 20032e0a750SGreg Clayton frame_line = false; 20132e0a750SGreg Clayton start_addr = LLDB_INVALID_ADDRESS; 20232e0a750SGreg Clayton end_addr = LLDB_INVALID_ADDRESS; 203801237a2SJason Molenda symbol_containing_addr = LLDB_INVALID_ADDRESS; 204a68c1a21SSean Callanan raw = false; 20532e0a750SGreg Clayton plugin_name.clear(); 2060f063ba6SJim Ingham 207e1cfbc79STodd Fiala Target *target = 208e1cfbc79STodd Fiala execution_context ? execution_context->GetTargetPtr() : nullptr; 2090f063ba6SJim Ingham 210b9c1b51eSKate Stone // This is a hack till we get the ability to specify features based on 211b9c1b51eSKate Stone // architecture. For now GetDisassemblyFlavor 212b9c1b51eSKate Stone // is really only valid for x86 (and for the llvm assembler plugin, but I'm 213b9c1b51eSKate Stone // papering over that since that is the 2140f063ba6SJim Ingham // only disassembler plugin we have... 215b9c1b51eSKate Stone if (target) { 216b9c1b51eSKate Stone if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86 || 217b9c1b51eSKate Stone target->GetArchitecture().GetTriple().getArch() == 218b9c1b51eSKate Stone llvm::Triple::x86_64) { 2190f063ba6SJim Ingham flavor_string.assign(target->GetDisassemblyFlavor()); 220b9c1b51eSKate Stone } else 2210f063ba6SJim Ingham flavor_string.assign("default"); 2220f063ba6SJim Ingham 223b9c1b51eSKate Stone } else 2240f063ba6SJim Ingham flavor_string.assign("default"); 2250f063ba6SJim Ingham 22632e0a750SGreg Clayton arch.Clear(); 2273555b5d7SJim Ingham some_location_specified = false; 2283555b5d7SJim Ingham } 2293555b5d7SJim Ingham 230b9c1b51eSKate Stone Error CommandObjectDisassemble::CommandOptions::OptionParsingFinished( 231b9c1b51eSKate Stone ExecutionContext *execution_context) { 2323555b5d7SJim Ingham if (!some_location_specified) 2331fb2e7dfSGreg Clayton current_function = true; 234*c1edf566SMehdi Amini return Error(); 23530fdc8d8SChris Lattner } 23630fdc8d8SChris Lattner 2371f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> 238b9c1b51eSKate Stone CommandObjectDisassemble::CommandOptions::GetDefinitions() { 23970602439SZachary Turner return llvm::makeArrayRef(g_disassemble_options); 24030fdc8d8SChris Lattner } 24130fdc8d8SChris Lattner 24230fdc8d8SChris Lattner //------------------------------------------------------------------------- 24330fdc8d8SChris Lattner // CommandObjectDisassemble 24430fdc8d8SChris Lattner //------------------------------------------------------------------------- 24530fdc8d8SChris Lattner 246b9c1b51eSKate Stone CommandObjectDisassemble::CommandObjectDisassemble( 247b9c1b51eSKate Stone CommandInterpreter &interpreter) 248b9c1b51eSKate Stone : CommandObjectParsed( 249b9c1b51eSKate Stone interpreter, "disassemble", 250b9c1b51eSKate Stone "Disassemble specified instructions in the current target. " 2517428a18cSKate Stone "Defaults to the current function for the current thread and " 2527428a18cSKate Stone "stack frame.", 253eb0103f2SGreg Clayton "disassemble [<cmd-options>]"), 254b9c1b51eSKate Stone m_options() {} 25530fdc8d8SChris Lattner 25626cac3afSEugene Zelenko CommandObjectDisassemble::~CommandObjectDisassemble() = default; 25730fdc8d8SChris Lattner 258b9c1b51eSKate Stone bool CommandObjectDisassemble::DoExecute(Args &command, 259b9c1b51eSKate Stone CommandReturnObject &result) { 260a7015092SGreg Clayton Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 261b9c1b51eSKate Stone if (target == nullptr) { 262b9c1b51eSKate Stone result.AppendError("invalid target, create a debug target using the " 263b9c1b51eSKate Stone "'target create' command"); 26430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 26530fdc8d8SChris Lattner return false; 26630fdc8d8SChris Lattner } 26732e0a750SGreg Clayton if (!m_options.arch.IsValid()) 26832e0a750SGreg Clayton m_options.arch = target->GetArchitecture(); 26930fdc8d8SChris Lattner 270b9c1b51eSKate Stone if (!m_options.arch.IsValid()) { 271b9c1b51eSKate Stone result.AppendError( 272b9c1b51eSKate Stone "use the --arch option or set the target architecture to disassemble"); 27330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 27430fdc8d8SChris Lattner return false; 27530fdc8d8SChris Lattner } 27630fdc8d8SChris Lattner 2771080edbcSGreg Clayton const char *plugin_name = m_options.GetPluginName(); 2780f063ba6SJim Ingham const char *flavor_string = m_options.GetFlavorString(); 2790f063ba6SJim Ingham 280b9c1b51eSKate Stone DisassemblerSP disassembler = 281b9c1b51eSKate Stone Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name); 28230fdc8d8SChris Lattner 283b9c1b51eSKate Stone if (!disassembler) { 284b9c1b51eSKate Stone if (plugin_name) { 285b9c1b51eSKate Stone result.AppendErrorWithFormat( 286b9c1b51eSKate Stone "Unable to find Disassembler plug-in named '%s' that supports the " 287b9c1b51eSKate Stone "'%s' architecture.\n", 288b9c1b51eSKate Stone plugin_name, m_options.arch.GetArchitectureName()); 289b9c1b51eSKate Stone } else 290b9c1b51eSKate Stone result.AppendErrorWithFormat( 291b9c1b51eSKate Stone "Unable to find Disassembler plug-in for the '%s' architecture.\n", 29232e0a750SGreg Clayton m_options.arch.GetArchitectureName()); 29330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 29430fdc8d8SChris Lattner return false; 295b9c1b51eSKate Stone } else if (flavor_string != nullptr && 296b9c1b51eSKate Stone !disassembler->FlavorValidForArchSpec(m_options.arch, 297b9c1b51eSKate Stone flavor_string)) 298b9c1b51eSKate Stone result.AppendWarningWithFormat( 299b9c1b51eSKate Stone "invalid disassembler flavor \"%s\", using default.\n", flavor_string); 30030fdc8d8SChris Lattner 30130fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 30230fdc8d8SChris Lattner 30311eb9c64SZachary Turner if (!command.empty()) { 304b9c1b51eSKate Stone result.AppendErrorWithFormat( 305b9c1b51eSKate Stone "\"disassemble\" arguments are specified as options.\n"); 306e1cfbc79STodd Fiala const int terminal_width = 307e1cfbc79STodd Fiala GetCommandInterpreter().GetDebugger().GetTerminalWidth(); 308e1cfbc79STodd Fiala GetOptions()->GenerateOptionUsage(result.GetErrorStream(), this, 309e1cfbc79STodd Fiala terminal_width); 3108651121cSJim Ingham result.SetStatus(eReturnStatusFailed); 3118651121cSJim Ingham return false; 3128651121cSJim Ingham } 3138651121cSJim Ingham 314dda4f7b5SGreg Clayton if (m_options.show_mixed && m_options.num_lines_context == 0) 3150b4c26b2SJason Molenda m_options.num_lines_context = 2; 316dda4f7b5SGreg Clayton 317b10d72f0SGreg Clayton // Always show the PC in the disassembly 318b10d72f0SGreg Clayton uint32_t options = Disassembler::eOptionMarkPCAddress; 3191da6f9d7SGreg Clayton 320b9c1b51eSKate Stone // Mark the source line for the current PC only if we are doing mixed source 321b9c1b51eSKate Stone // and assembly 322b10d72f0SGreg Clayton if (m_options.show_mixed) 323b10d72f0SGreg Clayton options |= Disassembler::eOptionMarkPCSourceLine; 3241da6f9d7SGreg Clayton 3251da6f9d7SGreg Clayton if (m_options.show_bytes) 3261da6f9d7SGreg Clayton options |= Disassembler::eOptionShowBytes; 3271da6f9d7SGreg Clayton 3281da6f9d7SGreg Clayton if (m_options.raw) 3291da6f9d7SGreg Clayton options |= Disassembler::eOptionRawOuput; 33037023b06SJim Ingham 331b9c1b51eSKate Stone if (!m_options.func_name.empty()) { 33232e0a750SGreg Clayton ConstString name(m_options.func_name.c_str()); 333dda4f7b5SGreg Clayton 334b9c1b51eSKate Stone if (Disassembler::Disassemble( 335b9c1b51eSKate Stone m_interpreter.GetDebugger(), m_options.arch, plugin_name, 336b9c1b51eSKate Stone flavor_string, m_exe_ctx, name, 33726cac3afSEugene Zelenko nullptr, // Module * 3380b4c26b2SJason Molenda m_options.num_instructions, m_options.show_mixed, 339b9c1b51eSKate Stone m_options.show_mixed ? m_options.num_lines_context : 0, options, 340b9c1b51eSKate Stone result.GetOutputStream())) { 341dda4f7b5SGreg Clayton result.SetStatus(eReturnStatusSuccessFinishResult); 342b9c1b51eSKate Stone } else { 343b9c1b51eSKate Stone result.AppendErrorWithFormat("Unable to find symbol with name '%s'.\n", 344b9c1b51eSKate Stone name.GetCString()); 345dda4f7b5SGreg Clayton result.SetStatus(eReturnStatusFailed); 346dda4f7b5SGreg Clayton } 347b9c1b51eSKate Stone } else { 3482f2c876eSJim Ingham std::vector<AddressRange> ranges; 34932e0a750SGreg Clayton AddressRange range; 350b57e4a1bSJason Molenda StackFrame *frame = m_exe_ctx.GetFramePtr(); 351b9c1b51eSKate Stone if (m_options.frame_line) { 352b9c1b51eSKate Stone if (frame == nullptr) { 353b9c1b51eSKate Stone result.AppendError("Cannot disassemble around the current line without " 354b9c1b51eSKate Stone "a selected frame.\n"); 3553555b5d7SJim Ingham result.SetStatus(eReturnStatusFailed); 3563555b5d7SJim Ingham return false; 3573555b5d7SJim Ingham } 358b9c1b51eSKate Stone LineEntry pc_line_entry( 359b9c1b51eSKate Stone frame->GetSymbolContext(eSymbolContextLineEntry).line_entry); 360b9c1b51eSKate Stone if (pc_line_entry.IsValid()) { 36132e0a750SGreg Clayton range = pc_line_entry.range; 362b9c1b51eSKate Stone } else { 363b9c1b51eSKate Stone m_options.at_pc = 364b9c1b51eSKate Stone true; // No line entry, so just disassemble around the current pc 36532e0a750SGreg Clayton m_options.show_mixed = false; 36632e0a750SGreg Clayton } 367b9c1b51eSKate Stone } else if (m_options.current_function) { 368b9c1b51eSKate Stone if (frame == nullptr) { 369b9c1b51eSKate Stone result.AppendError("Cannot disassemble around the current function " 370b9c1b51eSKate Stone "without a selected frame.\n"); 3713555b5d7SJim Ingham result.SetStatus(eReturnStatusFailed); 3723555b5d7SJim Ingham return false; 3733555b5d7SJim Ingham } 374c14ee32dSGreg Clayton Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol; 375b9c1b51eSKate Stone if (symbol) { 376e7612134SGreg Clayton range.GetBaseAddress() = symbol->GetAddress(); 377e7612134SGreg Clayton range.SetByteSize(symbol->GetByteSize()); 378e7612134SGreg Clayton } 3793555b5d7SJim Ingham } 38037023b06SJim Ingham 38132e0a750SGreg Clayton // Did the "m_options.frame_line" find a valid range already? If so 38232e0a750SGreg Clayton // skip the rest... 383b9c1b51eSKate Stone if (range.GetByteSize() == 0) { 384b9c1b51eSKate Stone if (m_options.at_pc) { 385b9c1b51eSKate Stone if (frame == nullptr) { 386b9c1b51eSKate Stone result.AppendError("Cannot disassemble around the current PC without " 387b9c1b51eSKate Stone "a selected frame.\n"); 38837023b06SJim Ingham result.SetStatus(eReturnStatusFailed); 38937023b06SJim Ingham return false; 39037023b06SJim Ingham } 391c14ee32dSGreg Clayton range.GetBaseAddress() = frame->GetFrameCodeAddress(); 392b9c1b51eSKate Stone if (m_options.num_instructions == 0) { 393b9c1b51eSKate Stone // Disassembling at the PC always disassembles some number of 394b9c1b51eSKate Stone // instructions (not the whole function). 39537023b06SJim Ingham m_options.num_instructions = DEFAULT_DISASM_NUM_INS; 39637023b06SJim Ingham } 3972f2c876eSJim Ingham ranges.push_back(range); 398b9c1b51eSKate Stone } else { 39932e0a750SGreg Clayton range.GetBaseAddress().SetOffset(m_options.start_addr); 400b9c1b51eSKate Stone if (range.GetBaseAddress().IsValid()) { 401b9c1b51eSKate Stone if (m_options.end_addr != LLDB_INVALID_ADDRESS) { 402b9c1b51eSKate Stone if (m_options.end_addr <= m_options.start_addr) { 403b9c1b51eSKate Stone result.AppendErrorWithFormat( 404b9c1b51eSKate Stone "End address before start address.\n"); 4058651121cSJim Ingham result.SetStatus(eReturnStatusFailed); 4068651121cSJim Ingham return false; 4078651121cSJim Ingham } 40832e0a750SGreg Clayton range.SetByteSize(m_options.end_addr - m_options.start_addr); 40932e0a750SGreg Clayton } 4102f4693aaSJim Ingham ranges.push_back(range); 411b9c1b51eSKate Stone } else { 412b9c1b51eSKate Stone if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS && 413b9c1b51eSKate Stone target) { 414b9c1b51eSKate Stone if (!target->GetSectionLoadList().IsEmpty()) { 415801237a2SJason Molenda bool failed = false; 416801237a2SJason Molenda Address symbol_containing_address; 417b9c1b51eSKate Stone if (target->GetSectionLoadList().ResolveLoadAddress( 418b9c1b51eSKate Stone m_options.symbol_containing_addr, 419b9c1b51eSKate Stone symbol_containing_address)) { 420801237a2SJason Molenda ModuleSP module_sp(symbol_containing_address.GetModule()); 421801237a2SJason Molenda SymbolContext sc; 422b9c1b51eSKate Stone bool resolve_tail_call_address = true; // PC can be one past the 423b9c1b51eSKate Stone // address range of the 424b9c1b51eSKate Stone // function. 425b9c1b51eSKate Stone module_sp->ResolveSymbolContextForAddress( 426b9c1b51eSKate Stone symbol_containing_address, eSymbolContextEverything, sc, 42735729bb1SAshok Thirumurthi resolve_tail_call_address); 428b9c1b51eSKate Stone if (sc.function || sc.symbol) { 429b9c1b51eSKate Stone sc.GetAddressRange(eSymbolContextFunction | 430b9c1b51eSKate Stone eSymbolContextSymbol, 431b9c1b51eSKate Stone 0, false, range); 432b9c1b51eSKate Stone } else { 433801237a2SJason Molenda failed = true; 434801237a2SJason Molenda } 435b9c1b51eSKate Stone } else { 436801237a2SJason Molenda failed = true; 437801237a2SJason Molenda } 438b9c1b51eSKate Stone if (failed) { 439b9c1b51eSKate Stone result.AppendErrorWithFormat( 440b9c1b51eSKate Stone "Could not find function bounds for address 0x%" PRIx64 441b9c1b51eSKate Stone "\n", 442b9c1b51eSKate Stone m_options.symbol_containing_addr); 443801237a2SJason Molenda result.SetStatus(eReturnStatusFailed); 444801237a2SJason Molenda return false; 445801237a2SJason Molenda } 4462f2c876eSJim Ingham ranges.push_back(range); 447b9c1b51eSKate Stone } else { 448b9c1b51eSKate Stone for (lldb::ModuleSP module_sp : target->GetImages().Modules()) { 4492f2c876eSJim Ingham lldb::addr_t file_addr = m_options.symbol_containing_addr; 4502f2c876eSJim Ingham Address file_address; 451b9c1b51eSKate Stone if (module_sp->ResolveFileAddress(file_addr, file_address)) { 4522f2c876eSJim Ingham SymbolContext sc; 453b9c1b51eSKate Stone bool resolve_tail_call_address = true; // PC can be one past 454b9c1b51eSKate Stone // the address range of 455b9c1b51eSKate Stone // the function. 456b9c1b51eSKate Stone module_sp->ResolveSymbolContextForAddress( 457b9c1b51eSKate Stone file_address, eSymbolContextEverything, sc, 458b9c1b51eSKate Stone resolve_tail_call_address); 459b9c1b51eSKate Stone if (sc.function || sc.symbol) { 460b9c1b51eSKate Stone sc.GetAddressRange(eSymbolContextFunction | 461b9c1b51eSKate Stone eSymbolContextSymbol, 462b9c1b51eSKate Stone 0, false, range); 4632f2c876eSJim Ingham ranges.push_back(range); 464801237a2SJason Molenda } 46537023b06SJim Ingham } 46637023b06SJim Ingham } 4672f2c876eSJim Ingham } 4682f2c876eSJim Ingham } 4692f2c876eSJim Ingham } 4702f2c876eSJim Ingham } 471b9c1b51eSKate Stone } else 4722f2c876eSJim Ingham ranges.push_back(range); 4732f2c876eSJim Ingham 474b9c1b51eSKate Stone if (m_options.num_instructions != 0) { 475b9c1b51eSKate Stone if (ranges.empty()) { 47637023b06SJim Ingham // The default action is to disassemble the current frame function. 477b9c1b51eSKate Stone if (frame) { 478b9c1b51eSKate Stone SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | 479b9c1b51eSKate Stone eSymbolContextSymbol)); 48037023b06SJim Ingham if (sc.function) 481b9c1b51eSKate Stone range.GetBaseAddress() = 482b9c1b51eSKate Stone sc.function->GetAddressRange().GetBaseAddress(); 483e7612134SGreg Clayton else if (sc.symbol && sc.symbol->ValueIsAddress()) 484e7612134SGreg Clayton range.GetBaseAddress() = sc.symbol->GetAddress(); 48537023b06SJim Ingham else 486c14ee32dSGreg Clayton range.GetBaseAddress() = frame->GetFrameCodeAddress(); 48737023b06SJim Ingham } 48837023b06SJim Ingham 489b9c1b51eSKate Stone if (!range.GetBaseAddress().IsValid()) { 49037023b06SJim Ingham result.AppendError("invalid frame"); 49137023b06SJim Ingham result.SetStatus(eReturnStatusFailed); 49237023b06SJim Ingham return false; 49337023b06SJim Ingham } 49437023b06SJim Ingham } 49537023b06SJim Ingham 4962f2c876eSJim Ingham bool print_sc_header = ranges.size() > 1; 497b9c1b51eSKate Stone for (AddressRange cur_range : ranges) { 498b9c1b51eSKate Stone if (Disassembler::Disassemble( 499b9c1b51eSKate Stone m_interpreter.GetDebugger(), m_options.arch, plugin_name, 500b9c1b51eSKate Stone flavor_string, m_exe_ctx, cur_range.GetBaseAddress(), 5010b4c26b2SJason Molenda m_options.num_instructions, m_options.show_mixed, 502b9c1b51eSKate Stone m_options.show_mixed ? m_options.num_lines_context : 0, options, 503b9c1b51eSKate Stone result.GetOutputStream())) { 50437023b06SJim Ingham result.SetStatus(eReturnStatusSuccessFinishResult); 505b9c1b51eSKate Stone } else { 5062f2c876eSJim Ingham if (m_options.start_addr != LLDB_INVALID_ADDRESS) 507b9c1b51eSKate Stone result.AppendErrorWithFormat( 508b9c1b51eSKate Stone "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", 509b9c1b51eSKate Stone m_options.start_addr); 5102f2c876eSJim Ingham else if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) 511b9c1b51eSKate Stone result.AppendErrorWithFormat( 512b9c1b51eSKate Stone "Failed to disassemble memory in function at 0x%8.8" PRIx64 513b9c1b51eSKate Stone ".\n", 514b9c1b51eSKate Stone m_options.symbol_containing_addr); 51537023b06SJim Ingham result.SetStatus(eReturnStatusFailed); 51637023b06SJim Ingham } 51737023b06SJim Ingham } 5182f2c876eSJim Ingham if (print_sc_header) 5192f2c876eSJim Ingham result.AppendMessage("\n"); 520b9c1b51eSKate Stone } else { 521b9c1b51eSKate Stone if (ranges.empty()) { 5228ceb8ba2SJohnny Chen // The default action is to disassemble the current frame function. 523b9c1b51eSKate Stone if (frame) { 524b9c1b51eSKate Stone SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | 525b9c1b51eSKate Stone eSymbolContextSymbol)); 52630fdc8d8SChris Lattner if (sc.function) 527dda4f7b5SGreg Clayton range = sc.function->GetAddressRange(); 528b9c1b51eSKate Stone else if (sc.symbol && sc.symbol->ValueIsAddress()) { 529e7612134SGreg Clayton range.GetBaseAddress() = sc.symbol->GetAddress(); 530e7612134SGreg Clayton range.SetByteSize(sc.symbol->GetByteSize()); 531b9c1b51eSKate Stone } else 532c14ee32dSGreg Clayton range.GetBaseAddress() = frame->GetFrameCodeAddress(); 533b9c1b51eSKate Stone } else { 53430fdc8d8SChris Lattner result.AppendError("invalid frame"); 53530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 53630fdc8d8SChris Lattner return false; 53730fdc8d8SChris Lattner } 5382f2c876eSJim Ingham ranges.push_back(range); 53930fdc8d8SChris Lattner } 5402f2c876eSJim Ingham 5412f2c876eSJim Ingham bool print_sc_header = ranges.size() > 1; 542b9c1b51eSKate Stone for (AddressRange cur_range : ranges) { 5432f2c876eSJim Ingham if (cur_range.GetByteSize() == 0) 5442f2c876eSJim Ingham cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE); 54530fdc8d8SChris Lattner 546b9c1b51eSKate Stone if (Disassembler::Disassemble( 547b9c1b51eSKate Stone m_interpreter.GetDebugger(), m_options.arch, plugin_name, 548b9c1b51eSKate Stone flavor_string, m_exe_ctx, cur_range, m_options.num_instructions, 5490b4c26b2SJason Molenda m_options.show_mixed, 550b9c1b51eSKate Stone m_options.show_mixed ? m_options.num_lines_context : 0, options, 551b9c1b51eSKate Stone result.GetOutputStream())) { 552dda4f7b5SGreg Clayton result.SetStatus(eReturnStatusSuccessFinishResult); 553b9c1b51eSKate Stone } else { 554b9c1b51eSKate Stone result.AppendErrorWithFormat( 555b9c1b51eSKate Stone "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", 556b9c1b51eSKate Stone m_options.start_addr); 55730fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55830fdc8d8SChris Lattner } 5592f2c876eSJim Ingham if (print_sc_header) 5602f2c876eSJim Ingham result.AppendMessage("\n"); 5612f2c876eSJim Ingham } 56230fdc8d8SChris Lattner } 56337023b06SJim Ingham } 56430fdc8d8SChris Lattner 56530fdc8d8SChris Lattner return result.Succeeded(); 56630fdc8d8SChris Lattner } 567