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 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
1230fdc8d8SChris Lattner #include "CommandObjectDisassemble.h"
1330fdc8d8SChris Lattner 
1430fdc8d8SChris Lattner // C Includes
1530fdc8d8SChris Lattner // C++ Includes
1630fdc8d8SChris Lattner // Other libraries and framework includes
1730fdc8d8SChris Lattner // Project includes
1830fdc8d8SChris Lattner #include "lldb/Core/AddressRange.h"
191f746071SGreg Clayton #include "lldb/Core/Disassembler.h"
20801237a2SJason Molenda #include "lldb/Core/Module.h"
211f746071SGreg Clayton #include "lldb/Core/SourceManager.h"
2240af72e1SJim Ingham #include "lldb/Interpreter/Args.h"
2330fdc8d8SChris Lattner #include "lldb/Interpreter/CommandCompletions.h"
2430fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
2530fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
2640af72e1SJim Ingham #include "lldb/Interpreter/Options.h"
271f746071SGreg Clayton #include "lldb/Symbol/Function.h"
2830fdc8d8SChris Lattner #include "lldb/Symbol/Symbol.h"
2930fdc8d8SChris Lattner #include "lldb/Target/Process.h"
301f746071SGreg Clayton #include "lldb/Target/StackFrame.h"
3130fdc8d8SChris Lattner #include "lldb/Target/Target.h"
3230fdc8d8SChris Lattner 
3330fdc8d8SChris Lattner #define DEFAULT_DISASM_BYTE_SIZE 32
3437023b06SJim Ingham #define DEFAULT_DISASM_NUM_INS  4
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner using namespace lldb;
3730fdc8d8SChris Lattner using namespace lldb_private;
3830fdc8d8SChris Lattner 
39eb0103f2SGreg Clayton CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
401f1b269bSJohnny Chen     Options(interpreter),
4137023b06SJim Ingham     num_lines_context(0),
4237023b06SJim Ingham     num_instructions (0),
4332e0a750SGreg Clayton     func_name(),
441fb2e7dfSGreg Clayton     current_function (false),
4532e0a750SGreg Clayton     start_addr(),
4632e0a750SGreg Clayton     end_addr (),
4732e0a750SGreg Clayton     at_pc (false),
4832e0a750SGreg Clayton     frame_line (false),
4932e0a750SGreg Clayton     plugin_name (),
500f063ba6SJim Ingham     flavor_string(),
513555b5d7SJim Ingham     arch(),
52801237a2SJason Molenda     some_location_specified (false),
53801237a2SJason Molenda     symbol_containing_addr ()
5430fdc8d8SChris Lattner {
55f6b8b581SGreg Clayton     OptionParsingStarting();
5630fdc8d8SChris Lattner }
5730fdc8d8SChris Lattner 
5830fdc8d8SChris Lattner CommandObjectDisassemble::CommandOptions::~CommandOptions ()
5930fdc8d8SChris Lattner {
6030fdc8d8SChris Lattner }
6130fdc8d8SChris Lattner 
6230fdc8d8SChris Lattner Error
63f6b8b581SGreg Clayton CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
6430fdc8d8SChris Lattner {
6530fdc8d8SChris Lattner     Error error;
6630fdc8d8SChris Lattner 
673bcdfc0eSGreg Clayton     const int short_option = m_getopt_table[option_idx].val;
6830fdc8d8SChris Lattner 
6937023b06SJim Ingham     bool success;
7037023b06SJim Ingham 
7130fdc8d8SChris Lattner     switch (short_option)
7230fdc8d8SChris Lattner     {
7330fdc8d8SChris Lattner     case 'm':
7430fdc8d8SChris Lattner         show_mixed = true;
7530fdc8d8SChris Lattner         break;
7630fdc8d8SChris Lattner 
77357132ebSGreg Clayton     case 'C':
7837023b06SJim Ingham         num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
7937023b06SJim Ingham         if (!success)
8086edbf41SGreg Clayton             error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
8137023b06SJim Ingham         break;
8237023b06SJim Ingham 
8330fdc8d8SChris Lattner     case 'c':
8437023b06SJim Ingham         num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
8537023b06SJim Ingham         if (!success)
8686edbf41SGreg Clayton             error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
8730fdc8d8SChris Lattner         break;
8830fdc8d8SChris Lattner 
8930fdc8d8SChris Lattner     case 'b':
9030fdc8d8SChris Lattner         show_bytes = true;
9130fdc8d8SChris Lattner         break;
9230fdc8d8SChris Lattner 
938651121cSJim Ingham     case 's':
94b9d5df58SGreg Clayton         {
95b9d5df58SGreg Clayton             ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
96b9d5df58SGreg Clayton             start_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
97b9d5df58SGreg Clayton             if (start_addr != LLDB_INVALID_ADDRESS)
983555b5d7SJim Ingham                 some_location_specified = true;
99b9d5df58SGreg Clayton         }
1008651121cSJim Ingham         break;
1018651121cSJim Ingham     case 'e':
102b9d5df58SGreg Clayton         {
103b9d5df58SGreg Clayton             ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
104b9d5df58SGreg Clayton             end_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
105b9d5df58SGreg Clayton             if (end_addr != LLDB_INVALID_ADDRESS)
1063555b5d7SJim Ingham                 some_location_specified = true;
107b9d5df58SGreg Clayton         }
108b9d5df58SGreg Clayton         break;
10930fdc8d8SChris Lattner     case 'n':
11032e0a750SGreg Clayton         func_name.assign (option_arg);
1113555b5d7SJim Ingham         some_location_specified = true;
11230fdc8d8SChris Lattner         break;
11330fdc8d8SChris Lattner 
11437023b06SJim Ingham     case 'p':
11532e0a750SGreg Clayton         at_pc = true;
1163555b5d7SJim Ingham         some_location_specified = true;
11732e0a750SGreg Clayton         break;
11832e0a750SGreg Clayton 
11932e0a750SGreg Clayton     case 'l':
12032e0a750SGreg Clayton         frame_line = true;
12132e0a750SGreg Clayton         // Disassemble the current source line kind of implies showing mixed
12232e0a750SGreg Clayton         // source code context.
12332e0a750SGreg Clayton         show_mixed = true;
1243555b5d7SJim Ingham         some_location_specified = true;
12537023b06SJim Ingham         break;
12637023b06SJim Ingham 
1271080edbcSGreg Clayton     case 'P':
12832e0a750SGreg Clayton         plugin_name.assign (option_arg);
1291080edbcSGreg Clayton         break;
1301080edbcSGreg Clayton 
1310f063ba6SJim Ingham     case 'F':
1320f063ba6SJim Ingham         {
1330f063ba6SJim Ingham             Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
1340f063ba6SJim Ingham             if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
1350f063ba6SJim Ingham                 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
1360f063ba6SJim Ingham             {
1370f063ba6SJim Ingham                 flavor_string.assign (option_arg);
1380f063ba6SJim Ingham             }
1390f063ba6SJim Ingham             else
1400f063ba6SJim Ingham                 error.SetErrorStringWithFormat("Disassembler flavors are currently only supported for x86 and x86_64 targets.");
1410f063ba6SJim Ingham             break;
1420f063ba6SJim Ingham         }
14330fdc8d8SChris Lattner     case 'r':
14430fdc8d8SChris Lattner         raw = true;
14530fdc8d8SChris Lattner         break;
14630fdc8d8SChris Lattner 
1478ceb8ba2SJohnny Chen     case 'f':
1481fb2e7dfSGreg Clayton         current_function = true;
1493555b5d7SJim Ingham         some_location_specified = true;
1508ceb8ba2SJohnny Chen         break;
1518ceb8ba2SJohnny Chen 
152801237a2SJason Molenda     case 'A':
15370512317SGreg Clayton         if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
15470512317SGreg Clayton             arch.SetTriple (option_arg);
155357132ebSGreg Clayton         break;
156357132ebSGreg Clayton 
157801237a2SJason Molenda     case 'a':
158801237a2SJason Molenda         {
159801237a2SJason Molenda             ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
160801237a2SJason Molenda             symbol_containing_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
161801237a2SJason Molenda             if (symbol_containing_addr != LLDB_INVALID_ADDRESS)
162801237a2SJason Molenda             {
163801237a2SJason Molenda                 some_location_specified = true;
164801237a2SJason Molenda             }
165801237a2SJason Molenda         }
166801237a2SJason Molenda         break;
167801237a2SJason Molenda 
16830fdc8d8SChris Lattner     default:
16986edbf41SGreg Clayton         error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
17030fdc8d8SChris Lattner         break;
17130fdc8d8SChris Lattner     }
17230fdc8d8SChris Lattner 
17330fdc8d8SChris Lattner     return error;
17430fdc8d8SChris Lattner }
17530fdc8d8SChris Lattner 
17630fdc8d8SChris Lattner void
177f6b8b581SGreg Clayton CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
17830fdc8d8SChris Lattner {
17930fdc8d8SChris Lattner     show_mixed = false;
18030fdc8d8SChris Lattner     show_bytes = false;
18130fdc8d8SChris Lattner     num_lines_context = 0;
18237023b06SJim Ingham     num_instructions = 0;
18332e0a750SGreg Clayton     func_name.clear();
1841fb2e7dfSGreg Clayton     current_function = false;
18532e0a750SGreg Clayton     at_pc = false;
18632e0a750SGreg Clayton     frame_line = false;
18732e0a750SGreg Clayton     start_addr = LLDB_INVALID_ADDRESS;
18832e0a750SGreg Clayton     end_addr = LLDB_INVALID_ADDRESS;
189801237a2SJason Molenda     symbol_containing_addr = LLDB_INVALID_ADDRESS;
190a68c1a21SSean Callanan     raw = false;
19132e0a750SGreg Clayton     plugin_name.clear();
1920f063ba6SJim Ingham 
1930f063ba6SJim Ingham     Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
1940f063ba6SJim Ingham 
1950f063ba6SJim Ingham     // This is a hack till we get the ability to specify features based on architecture.  For now GetDisassemblyFlavor
1960f063ba6SJim Ingham     // is really only valid for x86 (and for the llvm assembler plugin, but I'm papering over that since that is the
1970f063ba6SJim Ingham     // only disassembler plugin we have...
1980f063ba6SJim Ingham     if (target)
1990f063ba6SJim Ingham     {
2000f063ba6SJim Ingham         if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
2010f063ba6SJim Ingham             || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
2020f063ba6SJim Ingham         {
2030f063ba6SJim Ingham             flavor_string.assign(target->GetDisassemblyFlavor());
2040f063ba6SJim Ingham         }
2050f063ba6SJim Ingham         else
2060f063ba6SJim Ingham             flavor_string.assign ("default");
2070f063ba6SJim Ingham 
2080f063ba6SJim Ingham     }
2090f063ba6SJim Ingham     else
2100f063ba6SJim Ingham         flavor_string.assign("default");
2110f063ba6SJim Ingham 
21232e0a750SGreg Clayton     arch.Clear();
2133555b5d7SJim Ingham     some_location_specified = false;
2143555b5d7SJim Ingham }
2153555b5d7SJim Ingham 
2163555b5d7SJim Ingham Error
2173555b5d7SJim Ingham CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
2183555b5d7SJim Ingham {
2193555b5d7SJim Ingham     if (!some_location_specified)
2201fb2e7dfSGreg Clayton         current_function = true;
2213555b5d7SJim Ingham     return Error();
2223555b5d7SJim Ingham 
22330fdc8d8SChris Lattner }
22430fdc8d8SChris Lattner 
225e0d378b3SGreg Clayton const OptionDefinition*
22630fdc8d8SChris Lattner CommandObjectDisassemble::CommandOptions::GetDefinitions ()
22730fdc8d8SChris Lattner {
22830fdc8d8SChris Lattner     return g_option_table;
22930fdc8d8SChris Lattner }
23030fdc8d8SChris Lattner 
231e0d378b3SGreg Clayton OptionDefinition
23230fdc8d8SChris Lattner CommandObjectDisassemble::CommandOptions::g_option_table[] =
23330fdc8d8SChris Lattner {
234e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "bytes"        , 'b', OptionParser::eNoArgument        , NULL, 0, eArgTypeNone,        "Show opcode bytes when disassembling."},
235e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "context"      , 'C', OptionParser::eRequiredArgument  , NULL, 0, eArgTypeNumLines,    "Number of context lines of source to show."},
236e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "mixed"        , 'm', OptionParser::eNoArgument        , NULL, 0, eArgTypeNone,        "Enable mixed source and assembly display."},
237e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "raw"          , 'r', OptionParser::eNoArgument        , NULL, 0, eArgTypeNone,        "Print raw disassembly with no symbol information."},
238e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "plugin"       , 'P', OptionParser::eRequiredArgument  , NULL, 0, eArgTypePlugin,      "Name of the disassembler plugin you want to use."},
239e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "flavor"       , 'F', OptionParser::eRequiredArgument  , NULL, 0, eArgTypeDisassemblyFlavor,        "Name of the disassembly flavor you want to use.  "
2400f063ba6SJim Ingham                                                                                                     "Currently the only valid options are default, and for Intel"
2410f063ba6SJim Ingham                                                                                                     " architectures, att and intel."},
242e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "arch"         , 'A', OptionParser::eRequiredArgument  , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
243357132ebSGreg Clayton { LLDB_OPT_SET_1  |
244e2607b50SVirgile Bello   LLDB_OPT_SET_2  , true , "start-address", 's', OptionParser::eRequiredArgument  , NULL, 0, eArgTypeAddressOrExpression,"Address at which to start disassembling."},
245e2607b50SVirgile Bello { LLDB_OPT_SET_1  , false, "end-address"  , 'e', OptionParser::eRequiredArgument  , NULL, 0, eArgTypeAddressOrExpression,  "Address at which to end disassembling."},
246357132ebSGreg Clayton { LLDB_OPT_SET_2  |
247357132ebSGreg Clayton   LLDB_OPT_SET_3  |
248357132ebSGreg Clayton   LLDB_OPT_SET_4  |
249e2607b50SVirgile Bello   LLDB_OPT_SET_5  , false, "count"        , 'c', OptionParser::eRequiredArgument  , NULL, 0, eArgTypeNumLines,    "Number of instructions to display."},
250e2607b50SVirgile Bello { LLDB_OPT_SET_3  , false, "name"         , 'n', OptionParser::eRequiredArgument  , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
2511fb2e7dfSGreg Clayton                                                                                                     "Disassemble entire contents of the given function name."},
252e2607b50SVirgile Bello { LLDB_OPT_SET_4  , false, "frame"        , 'f', OptionParser::eNoArgument        , NULL, 0, eArgTypeNone,        "Disassemble from the start of the current frame's function."},
253e2607b50SVirgile Bello { LLDB_OPT_SET_5  , false, "pc"           , 'p', OptionParser::eNoArgument        , NULL, 0, eArgTypeNone,        "Disassemble around the current pc."},
254*bcf81ea4SBen Langmuir { LLDB_OPT_SET_6  , false, "line"         , 'l', OptionParser::eNoArgument        , NULL, 0, eArgTypeNone,        "Disassemble the current frame's current source line instructions if there is debug line table information, else disassemble around the pc."},
255e2607b50SVirgile Bello { LLDB_OPT_SET_7  , false, "address"      , 'a', OptionParser::eRequiredArgument  , NULL, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address."},
256deaab222SCaroline Tice { 0               , false, NULL           ,   0, 0                  , NULL, 0, eArgTypeNone,        NULL }
25730fdc8d8SChris Lattner };
25830fdc8d8SChris Lattner 
25930fdc8d8SChris Lattner 
26030fdc8d8SChris Lattner 
26130fdc8d8SChris Lattner //-------------------------------------------------------------------------
26230fdc8d8SChris Lattner // CommandObjectDisassemble
26330fdc8d8SChris Lattner //-------------------------------------------------------------------------
26430fdc8d8SChris Lattner 
265a7015092SGreg Clayton CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
2665a988416SJim Ingham     CommandObjectParsed (interpreter,
267a7015092SGreg Clayton                          "disassemble",
2683f4c09c1SCaroline Tice                          "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
269eb0103f2SGreg Clayton                          "disassemble [<cmd-options>]"),
270eb0103f2SGreg Clayton     m_options (interpreter)
27130fdc8d8SChris Lattner {
27230fdc8d8SChris Lattner }
27330fdc8d8SChris Lattner 
27430fdc8d8SChris Lattner CommandObjectDisassemble::~CommandObjectDisassemble()
27530fdc8d8SChris Lattner {
27630fdc8d8SChris Lattner }
27730fdc8d8SChris Lattner 
27830fdc8d8SChris Lattner bool
2795a988416SJim Ingham CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
28030fdc8d8SChris Lattner {
281a7015092SGreg Clayton     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
28230fdc8d8SChris Lattner     if (target == NULL)
28330fdc8d8SChris Lattner     {
284effe5c95SGreg Clayton         result.AppendError ("invalid target, create a debug target using the 'target create' command");
28530fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
28630fdc8d8SChris Lattner         return false;
28730fdc8d8SChris Lattner     }
28832e0a750SGreg Clayton     if (!m_options.arch.IsValid())
28932e0a750SGreg Clayton         m_options.arch = target->GetArchitecture();
29030fdc8d8SChris Lattner 
29132e0a750SGreg Clayton     if (!m_options.arch.IsValid())
29230fdc8d8SChris Lattner     {
293357132ebSGreg Clayton         result.AppendError ("use the --arch option or set the target architecure to disassemble");
29430fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
29530fdc8d8SChris Lattner         return false;
29630fdc8d8SChris Lattner     }
29730fdc8d8SChris Lattner 
2981080edbcSGreg Clayton     const char *plugin_name = m_options.GetPluginName ();
2990f063ba6SJim Ingham     const char *flavor_string = m_options.GetFlavorString();
3000f063ba6SJim Ingham 
3010f063ba6SJim Ingham     DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
30230fdc8d8SChris Lattner 
3039a028519SSean Callanan     if (!disassembler)
30430fdc8d8SChris Lattner     {
3051080edbcSGreg Clayton         if (plugin_name)
3060f063ba6SJim Ingham         {
307357132ebSGreg Clayton             result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
308357132ebSGreg Clayton                                           plugin_name,
30932e0a750SGreg Clayton                                           m_options.arch.GetArchitectureName());
3100f063ba6SJim Ingham         }
3111080edbcSGreg Clayton         else
312357132ebSGreg Clayton             result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
31332e0a750SGreg Clayton                                           m_options.arch.GetArchitectureName());
31430fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
31530fdc8d8SChris Lattner         return false;
31630fdc8d8SChris Lattner     }
3170f063ba6SJim Ingham     else if (flavor_string != NULL && !disassembler->FlavorValidForArchSpec(m_options.arch, flavor_string))
3180f063ba6SJim Ingham         result.AppendWarningWithFormat("invalid disassembler flavor \"%s\", using default.\n", flavor_string);
31930fdc8d8SChris Lattner 
32030fdc8d8SChris Lattner     result.SetStatus (eReturnStatusSuccessFinishResult);
32130fdc8d8SChris Lattner 
322dda4f7b5SGreg Clayton     if (command.GetArgumentCount() != 0)
32330fdc8d8SChris Lattner     {
324a7015092SGreg Clayton         result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
325eb0103f2SGreg Clayton         GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
3268651121cSJim Ingham         result.SetStatus (eReturnStatusFailed);
3278651121cSJim Ingham         return false;
3288651121cSJim Ingham     }
3298651121cSJim Ingham 
330dda4f7b5SGreg Clayton     if (m_options.show_mixed && m_options.num_lines_context == 0)
3316dbd3983SGreg Clayton         m_options.num_lines_context = 1;
332dda4f7b5SGreg Clayton 
333b10d72f0SGreg Clayton     // Always show the PC in the disassembly
334b10d72f0SGreg Clayton     uint32_t options = Disassembler::eOptionMarkPCAddress;
3351da6f9d7SGreg Clayton 
336b10d72f0SGreg Clayton     // Mark the source line for the current PC only if we are doing mixed source and assembly
337b10d72f0SGreg Clayton     if (m_options.show_mixed)
338b10d72f0SGreg Clayton         options |= Disassembler::eOptionMarkPCSourceLine;
3391da6f9d7SGreg Clayton 
3401da6f9d7SGreg Clayton     if (m_options.show_bytes)
3411da6f9d7SGreg Clayton         options |= Disassembler::eOptionShowBytes;
3421da6f9d7SGreg Clayton 
3431da6f9d7SGreg Clayton     if (m_options.raw)
3441da6f9d7SGreg Clayton         options |= Disassembler::eOptionRawOuput;
34537023b06SJim Ingham 
34632e0a750SGreg Clayton     if (!m_options.func_name.empty())
347dda4f7b5SGreg Clayton     {
34832e0a750SGreg Clayton         ConstString name(m_options.func_name.c_str());
349dda4f7b5SGreg Clayton 
350a7015092SGreg Clayton         if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
35132e0a750SGreg Clayton                                        m_options.arch,
3521080edbcSGreg Clayton                                        plugin_name,
3530f063ba6SJim Ingham                                        flavor_string,
354f9fc609fSGreg Clayton                                        m_exe_ctx,
355dda4f7b5SGreg Clayton                                        name,
356dda4f7b5SGreg Clayton                                        NULL,    // Module *
35737023b06SJim Ingham                                        m_options.num_instructions,
358dda4f7b5SGreg Clayton                                        m_options.show_mixed ? m_options.num_lines_context : 0,
3591da6f9d7SGreg Clayton                                        options,
360dda4f7b5SGreg Clayton                                        result.GetOutputStream()))
361dda4f7b5SGreg Clayton         {
362dda4f7b5SGreg Clayton             result.SetStatus (eReturnStatusSuccessFinishResult);
363dda4f7b5SGreg Clayton         }
364dda4f7b5SGreg Clayton         else
365dda4f7b5SGreg Clayton         {
366dda4f7b5SGreg Clayton             result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
367dda4f7b5SGreg Clayton             result.SetStatus (eReturnStatusFailed);
368dda4f7b5SGreg Clayton         }
369dda4f7b5SGreg Clayton     }
370dda4f7b5SGreg Clayton     else
371dda4f7b5SGreg Clayton     {
37232e0a750SGreg Clayton         AddressRange range;
373f9fc609fSGreg Clayton         StackFrame *frame = m_exe_ctx.GetFramePtr();
37432e0a750SGreg Clayton         if (m_options.frame_line)
37532e0a750SGreg Clayton         {
376c14ee32dSGreg Clayton             if (frame == NULL)
3773555b5d7SJim Ingham             {
3783555b5d7SJim Ingham                 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
3793555b5d7SJim Ingham                 result.SetStatus (eReturnStatusFailed);
3803555b5d7SJim Ingham                 return false;
3813555b5d7SJim Ingham             }
382c14ee32dSGreg Clayton             LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
38332e0a750SGreg Clayton             if (pc_line_entry.IsValid())
38432e0a750SGreg Clayton             {
38532e0a750SGreg Clayton                 range = pc_line_entry.range;
38632e0a750SGreg Clayton             }
38732e0a750SGreg Clayton             else
38832e0a750SGreg Clayton             {
38932e0a750SGreg Clayton                 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
39032e0a750SGreg Clayton                 m_options.show_mixed = false;
39132e0a750SGreg Clayton             }
39232e0a750SGreg Clayton         }
3931fb2e7dfSGreg Clayton         else if (m_options.current_function)
3943555b5d7SJim Ingham         {
395c14ee32dSGreg Clayton             if (frame == NULL)
3963555b5d7SJim Ingham             {
3973555b5d7SJim Ingham                 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
3983555b5d7SJim Ingham                 result.SetStatus (eReturnStatusFailed);
3993555b5d7SJim Ingham                 return false;
4003555b5d7SJim Ingham             }
401c14ee32dSGreg Clayton             Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
4023555b5d7SJim Ingham             if (symbol)
403e7612134SGreg Clayton             {
404e7612134SGreg Clayton                 range.GetBaseAddress() = symbol->GetAddress();
405e7612134SGreg Clayton                 range.SetByteSize(symbol->GetByteSize());
406e7612134SGreg Clayton             }
4073555b5d7SJim Ingham         }
40837023b06SJim Ingham 
40932e0a750SGreg Clayton         // Did the "m_options.frame_line" find a valid range already? If so
41032e0a750SGreg Clayton         // skip the rest...
41132e0a750SGreg Clayton         if (range.GetByteSize() == 0)
41232e0a750SGreg Clayton         {
41332e0a750SGreg Clayton             if (m_options.at_pc)
4148651121cSJim Ingham             {
415c14ee32dSGreg Clayton                 if (frame == NULL)
41637023b06SJim Ingham                 {
41737023b06SJim Ingham                     result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
41837023b06SJim Ingham                     result.SetStatus (eReturnStatusFailed);
41937023b06SJim Ingham                     return false;
42037023b06SJim Ingham                 }
421c14ee32dSGreg Clayton                 range.GetBaseAddress() = frame->GetFrameCodeAddress();
42237023b06SJim Ingham                 if (m_options.num_instructions == 0)
42337023b06SJim Ingham                 {
42437023b06SJim Ingham                     // Disassembling at the PC always disassembles some number of instructions (not the whole function).
42537023b06SJim Ingham                     m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
42637023b06SJim Ingham                 }
42737023b06SJim Ingham             }
42837023b06SJim Ingham             else
42937023b06SJim Ingham             {
43032e0a750SGreg Clayton                 range.GetBaseAddress().SetOffset (m_options.start_addr);
43132e0a750SGreg Clayton                 if (range.GetBaseAddress().IsValid())
43237023b06SJim Ingham                 {
43332e0a750SGreg Clayton                     if (m_options.end_addr != LLDB_INVALID_ADDRESS)
4348651121cSJim Ingham                     {
43532e0a750SGreg Clayton                         if (m_options.end_addr <= m_options.start_addr)
4368651121cSJim Ingham                         {
4378651121cSJim Ingham                             result.AppendErrorWithFormat ("End address before start address.\n");
4388651121cSJim Ingham                             result.SetStatus (eReturnStatusFailed);
4398651121cSJim Ingham                             return false;
4408651121cSJim Ingham                         }
44132e0a750SGreg Clayton                         range.SetByteSize (m_options.end_addr - m_options.start_addr);
44232e0a750SGreg Clayton                     }
44337023b06SJim Ingham                 }
444801237a2SJason Molenda                 else
445801237a2SJason Molenda                 {
446801237a2SJason Molenda                     if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS
447801237a2SJason Molenda                         && target
448801237a2SJason Molenda                         && !target->GetSectionLoadList().IsEmpty())
449801237a2SJason Molenda                     {
450801237a2SJason Molenda                         bool failed = false;
451801237a2SJason Molenda                         Address symbol_containing_address;
452801237a2SJason Molenda                         if (target->GetSectionLoadList().ResolveLoadAddress (m_options.symbol_containing_addr, symbol_containing_address))
453801237a2SJason Molenda                         {
454801237a2SJason Molenda                             ModuleSP module_sp (symbol_containing_address.GetModule());
455801237a2SJason Molenda                             SymbolContext sc;
45635729bb1SAshok Thirumurthi                             bool resolve_tail_call_address = true; // PC can be one past the address range of the function.
45735729bb1SAshok Thirumurthi                             module_sp->ResolveSymbolContextForAddress (symbol_containing_address, eSymbolContextEverything, sc,
45835729bb1SAshok Thirumurthi                                                                        resolve_tail_call_address);
459801237a2SJason Molenda                             if (sc.function || sc.symbol)
460801237a2SJason Molenda                             {
461801237a2SJason Molenda                                 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
462801237a2SJason Molenda                             }
463801237a2SJason Molenda                             else
464801237a2SJason Molenda                             {
465801237a2SJason Molenda                                 failed = true;
466801237a2SJason Molenda                             }
467801237a2SJason Molenda                         }
468801237a2SJason Molenda                         else
469801237a2SJason Molenda                         {
470801237a2SJason Molenda                             failed = true;
471801237a2SJason Molenda                         }
472801237a2SJason Molenda                         if (failed)
473801237a2SJason Molenda                         {
474801237a2SJason Molenda                             result.AppendErrorWithFormat ("Could not find function bounds for address 0x%" PRIx64 "\n", m_options.symbol_containing_addr);
475801237a2SJason Molenda                             result.SetStatus (eReturnStatusFailed);
476801237a2SJason Molenda                             return false;
477801237a2SJason Molenda                         }
478801237a2SJason Molenda                     }
479801237a2SJason Molenda                 }
48037023b06SJim Ingham             }
48137023b06SJim Ingham         }
48237023b06SJim Ingham 
48337023b06SJim Ingham         if (m_options.num_instructions != 0)
48437023b06SJim Ingham         {
48532e0a750SGreg Clayton             if (!range.GetBaseAddress().IsValid())
48637023b06SJim Ingham             {
48737023b06SJim Ingham                 // The default action is to disassemble the current frame function.
488c14ee32dSGreg Clayton                 if (frame)
48937023b06SJim Ingham                 {
490c14ee32dSGreg Clayton                     SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
49137023b06SJim Ingham                     if (sc.function)
49232e0a750SGreg Clayton                         range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
493e7612134SGreg Clayton                     else if (sc.symbol && sc.symbol->ValueIsAddress())
494e7612134SGreg Clayton                         range.GetBaseAddress() = sc.symbol->GetAddress();
49537023b06SJim Ingham                     else
496c14ee32dSGreg Clayton                         range.GetBaseAddress() = frame->GetFrameCodeAddress();
49737023b06SJim Ingham                 }
49837023b06SJim Ingham 
49932e0a750SGreg Clayton                 if (!range.GetBaseAddress().IsValid())
50037023b06SJim Ingham                 {
50137023b06SJim Ingham                     result.AppendError ("invalid frame");
50237023b06SJim Ingham                     result.SetStatus (eReturnStatusFailed);
50337023b06SJim Ingham                     return false;
50437023b06SJim Ingham                 }
50537023b06SJim Ingham             }
50637023b06SJim Ingham 
50737023b06SJim Ingham             if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
50832e0a750SGreg Clayton                                            m_options.arch,
5091080edbcSGreg Clayton                                            plugin_name,
5100f063ba6SJim Ingham                                            flavor_string,
511f9fc609fSGreg Clayton                                            m_exe_ctx,
51232e0a750SGreg Clayton                                            range.GetBaseAddress(),
51337023b06SJim Ingham                                            m_options.num_instructions,
51437023b06SJim Ingham                                            m_options.show_mixed ? m_options.num_lines_context : 0,
5151da6f9d7SGreg Clayton                                            options,
51637023b06SJim Ingham                                            result.GetOutputStream()))
51737023b06SJim Ingham             {
51837023b06SJim Ingham                 result.SetStatus (eReturnStatusSuccessFinishResult);
5198651121cSJim Ingham             }
5208651121cSJim Ingham             else
52137023b06SJim Ingham             {
522d01b2953SDaniel Malea                 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
52337023b06SJim Ingham                 result.SetStatus (eReturnStatusFailed);
52437023b06SJim Ingham             }
52537023b06SJim Ingham         }
52637023b06SJim Ingham         else
52737023b06SJim Ingham         {
52832e0a750SGreg Clayton             if (!range.GetBaseAddress().IsValid())
52930fdc8d8SChris Lattner             {
5308ceb8ba2SJohnny Chen                 // The default action is to disassemble the current frame function.
531c14ee32dSGreg Clayton                 if (frame)
53230fdc8d8SChris Lattner                 {
533c14ee32dSGreg Clayton                     SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
53430fdc8d8SChris Lattner                     if (sc.function)
535dda4f7b5SGreg Clayton                         range = sc.function->GetAddressRange();
536e7612134SGreg Clayton                     else if (sc.symbol && sc.symbol->ValueIsAddress())
537e7612134SGreg Clayton                     {
538e7612134SGreg Clayton                         range.GetBaseAddress() = sc.symbol->GetAddress();
539e7612134SGreg Clayton                         range.SetByteSize (sc.symbol->GetByteSize());
540e7612134SGreg Clayton                     }
54130fdc8d8SChris Lattner                     else
542c14ee32dSGreg Clayton                         range.GetBaseAddress() = frame->GetFrameCodeAddress();
54330fdc8d8SChris Lattner                 }
54430fdc8d8SChris Lattner                 else
54530fdc8d8SChris Lattner                 {
54630fdc8d8SChris Lattner                     result.AppendError ("invalid frame");
54730fdc8d8SChris Lattner                     result.SetStatus (eReturnStatusFailed);
54830fdc8d8SChris Lattner                     return false;
54930fdc8d8SChris Lattner                 }
55030fdc8d8SChris Lattner             }
551dda4f7b5SGreg Clayton             if (range.GetByteSize() == 0)
552dda4f7b5SGreg Clayton                 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
55330fdc8d8SChris Lattner 
554a7015092SGreg Clayton             if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
55532e0a750SGreg Clayton                                            m_options.arch,
5561080edbcSGreg Clayton                                            plugin_name,
5570f063ba6SJim Ingham                                            flavor_string,
558f9fc609fSGreg Clayton                                            m_exe_ctx,
559dda4f7b5SGreg Clayton                                            range,
56037023b06SJim Ingham                                            m_options.num_instructions,
561dda4f7b5SGreg Clayton                                            m_options.show_mixed ? m_options.num_lines_context : 0,
5621da6f9d7SGreg Clayton                                            options,
563dda4f7b5SGreg Clayton                                            result.GetOutputStream()))
56430fdc8d8SChris Lattner             {
565dda4f7b5SGreg Clayton                 result.SetStatus (eReturnStatusSuccessFinishResult);
56630fdc8d8SChris Lattner             }
56730fdc8d8SChris Lattner             else
56830fdc8d8SChris Lattner             {
569d01b2953SDaniel Malea                 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
57030fdc8d8SChris Lattner                 result.SetStatus (eReturnStatusFailed);
57130fdc8d8SChris Lattner             }
57230fdc8d8SChris Lattner         }
57337023b06SJim Ingham     }
57430fdc8d8SChris Lattner 
57530fdc8d8SChris Lattner     return result.Succeeded();
57630fdc8d8SChris Lattner }
577