blob: 0d40fcd7c0b5afda4e8495ebca6f12eea340a5b8 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectDisassemble.cpp ----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectDisassemble.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/AddressRange.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000019#include "lldb/Core/Disassembler.h"
Jason Molenda9afae622013-04-11 03:14:01 +000020#include "lldb/Core/Module.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000021#include "lldb/Core/SourceManager.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000022#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Interpreter/CommandCompletions.h"
24#include "lldb/Interpreter/CommandInterpreter.h"
25#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000026#include "lldb/Interpreter/Options.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000027#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Symbol/Symbol.h"
29#include "lldb/Target/Process.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000030#include "lldb/Target/StackFrame.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Target/Target.h"
32
33#define DEFAULT_DISASM_BYTE_SIZE 32
Jim Inghamaa3e3e12011-03-22 01:48:42 +000034#define DEFAULT_DISASM_NUM_INS 4
Chris Lattner24943d22010-06-08 16:52:24 +000035
36using namespace lldb;
37using namespace lldb_private;
38
Greg Claytonf15996e2011-04-07 22:46:35 +000039CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen0e0636d2011-04-08 22:15:29 +000040 Options(interpreter),
Jim Inghamaa3e3e12011-03-22 01:48:42 +000041 num_lines_context(0),
42 num_instructions (0),
Greg Clayton24bc5d92011-03-30 18:16:51 +000043 func_name(),
Greg Clayton8d646f22012-12-14 22:36:35 +000044 current_function (false),
Greg Clayton24bc5d92011-03-30 18:16:51 +000045 start_addr(),
46 end_addr (),
47 at_pc (false),
48 frame_line (false),
49 plugin_name (),
Jim Ingham7d408382013-03-02 00:26:47 +000050 flavor_string(),
Jim Inghamd2ad7fd2011-09-01 01:11:04 +000051 arch(),
Jason Molenda9afae622013-04-11 03:14:01 +000052 some_location_specified (false),
53 symbol_containing_addr ()
Chris Lattner24943d22010-06-08 16:52:24 +000054{
Greg Clayton143fcc32011-04-13 00:18:08 +000055 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000056}
57
58CommandObjectDisassemble::CommandOptions::~CommandOptions ()
59{
60}
61
62Error
Greg Clayton143fcc32011-04-13 00:18:08 +000063CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000064{
65 Error error;
66
Greg Clayton6475c422012-12-04 00:32:51 +000067 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner24943d22010-06-08 16:52:24 +000068
Jim Inghamaa3e3e12011-03-22 01:48:42 +000069 bool success;
70
Chris Lattner24943d22010-06-08 16:52:24 +000071 switch (short_option)
72 {
73 case 'm':
74 show_mixed = true;
75 break;
76
Greg Clayton889fbd02011-03-26 19:14:58 +000077 case 'C':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000078 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
79 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000080 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
Jim Inghamaa3e3e12011-03-22 01:48:42 +000081 break;
82
Chris Lattner24943d22010-06-08 16:52:24 +000083 case 'c':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000084 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
85 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000086 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000087 break;
88
89 case 'b':
90 show_bytes = true;
91 break;
92
Jim Ingham34e9a982010-06-15 18:47:14 +000093 case 's':
Greg Clayton49d888d2012-12-06 22:49:16 +000094 {
95 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
96 start_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
97 if (start_addr != LLDB_INVALID_ADDRESS)
98 some_location_specified = true;
99 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000100 break;
101 case 'e':
Greg Clayton49d888d2012-12-06 22:49:16 +0000102 {
103 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
104 end_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
105 if (end_addr != LLDB_INVALID_ADDRESS)
106 some_location_specified = true;
107 }
Chris Lattner24943d22010-06-08 16:52:24 +0000108 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000109 case 'n':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000110 func_name.assign (option_arg);
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000111 some_location_specified = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000112 break;
113
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000114 case 'p':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000115 at_pc = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000116 some_location_specified = true;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000117 break;
118
119 case 'l':
120 frame_line = true;
121 // Disassemble the current source line kind of implies showing mixed
122 // source code context.
123 show_mixed = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000124 some_location_specified = true;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000125 break;
126
Greg Clayton149731c2011-03-25 18:03:16 +0000127 case 'P':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000128 plugin_name.assign (option_arg);
Greg Clayton149731c2011-03-25 18:03:16 +0000129 break;
130
Jim Ingham7d408382013-03-02 00:26:47 +0000131 case 'F':
132 {
133 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
134 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
135 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
136 {
137 flavor_string.assign (option_arg);
138 }
139 else
140 error.SetErrorStringWithFormat("Disassembler flavors are currently only supported for x86 and x86_64 targets.");
141 break;
142 }
Chris Lattner24943d22010-06-08 16:52:24 +0000143 case 'r':
144 raw = true;
145 break;
146
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000147 case 'f':
Greg Clayton8d646f22012-12-14 22:36:35 +0000148 current_function = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000149 some_location_specified = true;
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000150 break;
151
Jason Molenda9afae622013-04-11 03:14:01 +0000152 case 'A':
Greg Claytonb170aee2012-05-08 01:45:38 +0000153 if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
154 arch.SetTriple (option_arg);
Greg Clayton889fbd02011-03-26 19:14:58 +0000155 break;
156
Jason Molenda9afae622013-04-11 03:14:01 +0000157 case 'a':
158 {
159 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
160 symbol_containing_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
161 if (symbol_containing_addr != LLDB_INVALID_ADDRESS)
162 {
163 some_location_specified = true;
164 }
165 }
166 break;
167
Chris Lattner24943d22010-06-08 16:52:24 +0000168 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000169 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000170 break;
171 }
172
173 return error;
174}
175
176void
Greg Clayton143fcc32011-04-13 00:18:08 +0000177CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000178{
Chris Lattner24943d22010-06-08 16:52:24 +0000179 show_mixed = false;
180 show_bytes = false;
181 num_lines_context = 0;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000182 num_instructions = 0;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000183 func_name.clear();
Greg Clayton8d646f22012-12-14 22:36:35 +0000184 current_function = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000185 at_pc = false;
186 frame_line = false;
187 start_addr = LLDB_INVALID_ADDRESS;
188 end_addr = LLDB_INVALID_ADDRESS;
Jason Molenda9afae622013-04-11 03:14:01 +0000189 symbol_containing_addr = LLDB_INVALID_ADDRESS;
Sean Callanan944be702010-06-17 00:32:05 +0000190 raw = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000191 plugin_name.clear();
Jim Ingham7d408382013-03-02 00:26:47 +0000192
193 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
194
195 // This is a hack till we get the ability to specify features based on architecture. For now GetDisassemblyFlavor
196 // is really only valid for x86 (and for the llvm assembler plugin, but I'm papering over that since that is the
197 // only disassembler plugin we have...
198 if (target)
199 {
200 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
201 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
202 {
203 flavor_string.assign(target->GetDisassemblyFlavor());
204 }
205 else
206 flavor_string.assign ("default");
207
208 }
209 else
210 flavor_string.assign("default");
211
Greg Clayton24bc5d92011-03-30 18:16:51 +0000212 arch.Clear();
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000213 some_location_specified = false;
214}
215
216Error
217CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
218{
219 if (!some_location_specified)
Greg Clayton8d646f22012-12-14 22:36:35 +0000220 current_function = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000221 return Error();
222
Chris Lattner24943d22010-06-08 16:52:24 +0000223}
224
Greg Claytonb3448432011-03-24 21:19:54 +0000225const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000226CommandObjectDisassemble::CommandOptions::GetDefinitions ()
227{
228 return g_option_table;
229}
230
Greg Claytonb3448432011-03-24 21:19:54 +0000231OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000232CommandObjectDisassemble::CommandOptions::g_option_table[] =
233{
Greg Clayton8d646f22012-12-14 22:36:35 +0000234{ LLDB_OPT_SET_ALL, false, "bytes" , 'b', no_argument , NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
235{ LLDB_OPT_SET_ALL, false, "context" , 'C', required_argument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
236{ LLDB_OPT_SET_ALL, false, "mixed" , 'm', no_argument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
237{ LLDB_OPT_SET_ALL, false, "raw" , 'r', no_argument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
238{ LLDB_OPT_SET_ALL, false, "plugin" , 'P', required_argument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
Jim Ingham7d408382013-03-02 00:26:47 +0000239{ LLDB_OPT_SET_ALL, false, "flavor" , 'F', required_argument , NULL, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. "
240 "Currently the only valid options are default, and for Intel"
241 " architectures, att and intel."},
Jason Molenda9afae622013-04-11 03:14:01 +0000242{ LLDB_OPT_SET_ALL, false, "arch" , 'A', required_argument , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
Greg Clayton8d646f22012-12-14 22:36:35 +0000243{ LLDB_OPT_SET_1 |
Enrico Granata4968ad82013-01-29 01:48:30 +0000244 LLDB_OPT_SET_2 , true , "start-address", 's', required_argument , NULL, 0, eArgTypeAddressOrExpression,"Address at which to start disassembling."},
245{ LLDB_OPT_SET_1 , false, "end-address" , 'e', required_argument , NULL, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling."},
Greg Clayton8d646f22012-12-14 22:36:35 +0000246{ LLDB_OPT_SET_2 |
247 LLDB_OPT_SET_3 |
248 LLDB_OPT_SET_4 |
249 LLDB_OPT_SET_5 , false, "count" , 'c', required_argument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
250{ LLDB_OPT_SET_3 , false, "name" , 'n', required_argument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
251 "Disassemble entire contents of the given function name."},
252{ LLDB_OPT_SET_4 , false, "frame" , 'f', no_argument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
253{ LLDB_OPT_SET_5 , false, "pc" , 'p', no_argument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
254{ LLDB_OPT_SET_6 , false, "line" , 'l', no_argument , NULL, 0, eArgTypeNone, "Disassemble the current frame's current source line instructions if there debug line table information, else disasemble around the pc."},
Jason Molenda9afae622013-04-11 03:14:01 +0000255{ LLDB_OPT_SET_7 , false, "address" , 'a', required_argument , NULL, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address."},
Greg Clayton8d646f22012-12-14 22:36:35 +0000256{ 0 , false, NULL , 0, 0 , NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000257};
258
259
260
261//-------------------------------------------------------------------------
262// CommandObjectDisassemble
263//-------------------------------------------------------------------------
264
Greg Clayton238c0a12010-09-18 01:14:36 +0000265CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000266 CommandObjectParsed (interpreter,
267 "disassemble",
268 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
269 "disassemble [<cmd-options>]"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000270 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000271{
272}
273
274CommandObjectDisassemble::~CommandObjectDisassemble()
275{
276}
277
Chris Lattner24943d22010-06-08 16:52:24 +0000278bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000279CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000280{
Greg Clayton238c0a12010-09-18 01:14:36 +0000281 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000282 if (target == NULL)
283 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000284 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000285 result.SetStatus (eReturnStatusFailed);
286 return false;
287 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000288 if (!m_options.arch.IsValid())
289 m_options.arch = target->GetArchitecture();
Chris Lattner24943d22010-06-08 16:52:24 +0000290
Greg Clayton24bc5d92011-03-30 18:16:51 +0000291 if (!m_options.arch.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000292 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000293 result.AppendError ("use the --arch option or set the target architecure to disassemble");
Chris Lattner24943d22010-06-08 16:52:24 +0000294 result.SetStatus (eReturnStatusFailed);
295 return false;
296 }
297
Greg Clayton149731c2011-03-25 18:03:16 +0000298 const char *plugin_name = m_options.GetPluginName ();
Jim Ingham7d408382013-03-02 00:26:47 +0000299 const char *flavor_string = m_options.GetFlavorString();
300
301 DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000302
Sean Callananb386d822012-08-09 00:50:26 +0000303 if (!disassembler)
Chris Lattner24943d22010-06-08 16:52:24 +0000304 {
Greg Clayton149731c2011-03-25 18:03:16 +0000305 if (plugin_name)
Jim Ingham7d408382013-03-02 00:26:47 +0000306 {
307 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
Greg Clayton889fbd02011-03-26 19:14:58 +0000308 plugin_name,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000309 m_options.arch.GetArchitectureName());
Jim Ingham7d408382013-03-02 00:26:47 +0000310 }
Greg Clayton149731c2011-03-25 18:03:16 +0000311 else
Greg Clayton889fbd02011-03-26 19:14:58 +0000312 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
Greg Clayton24bc5d92011-03-30 18:16:51 +0000313 m_options.arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000314 result.SetStatus (eReturnStatusFailed);
315 return false;
316 }
Jim Ingham7d408382013-03-02 00:26:47 +0000317 else if (flavor_string != NULL && !disassembler->FlavorValidForArchSpec(m_options.arch, flavor_string))
318 result.AppendWarningWithFormat("invalid disassembler flavor \"%s\", using default.\n", flavor_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000319
320 result.SetStatus (eReturnStatusSuccessFinishResult);
321
Greg Clayton70436352010-06-30 23:03:03 +0000322 if (command.GetArgumentCount() != 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000323 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000324 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
Greg Claytonf15996e2011-04-07 22:46:35 +0000325 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
Jim Ingham34e9a982010-06-15 18:47:14 +0000326 result.SetStatus (eReturnStatusFailed);
327 return false;
328 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000329
Greg Clayton70436352010-06-30 23:03:03 +0000330 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton1924e242010-09-15 05:51:24 +0000331 m_options.num_lines_context = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000332
Greg Clayton6377f5c2011-06-28 19:01:40 +0000333 // Always show the PC in the disassembly
334 uint32_t options = Disassembler::eOptionMarkPCAddress;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000335
Greg Clayton6377f5c2011-06-28 19:01:40 +0000336 // Mark the source line for the current PC only if we are doing mixed source and assembly
337 if (m_options.show_mixed)
338 options |= Disassembler::eOptionMarkPCSourceLine;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000339
340 if (m_options.show_bytes)
341 options |= Disassembler::eOptionShowBytes;
342
343 if (m_options.raw)
344 options |= Disassembler::eOptionRawOuput;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000345
Greg Clayton24bc5d92011-03-30 18:16:51 +0000346 if (!m_options.func_name.empty())
Greg Clayton70436352010-06-30 23:03:03 +0000347 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000348 ConstString name(m_options.func_name.c_str());
Greg Clayton70436352010-06-30 23:03:03 +0000349
Greg Clayton238c0a12010-09-18 01:14:36 +0000350 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000351 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000352 plugin_name,
Jim Ingham7d408382013-03-02 00:26:47 +0000353 flavor_string,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000354 m_exe_ctx,
Greg Clayton70436352010-06-30 23:03:03 +0000355 name,
356 NULL, // Module *
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000357 m_options.num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000358 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000359 options,
Greg Clayton70436352010-06-30 23:03:03 +0000360 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000361 {
Greg Clayton70436352010-06-30 23:03:03 +0000362 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000363 }
364 else
365 {
366 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
367 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000368 }
Greg Clayton70436352010-06-30 23:03:03 +0000369 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000370 else
Chris Lattner24943d22010-06-08 16:52:24 +0000371 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000372 AddressRange range;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000373 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000374 if (m_options.frame_line)
Greg Clayton70436352010-06-30 23:03:03 +0000375 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000376 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000377 {
378 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
379 result.SetStatus (eReturnStatusFailed);
380 return false;
381 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000382 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000383 if (pc_line_entry.IsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000384 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000385 range = pc_line_entry.range;
Greg Clayton70436352010-06-30 23:03:03 +0000386 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000387 else
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000388 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000389 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
390 m_options.show_mixed = false;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000391 }
Greg Clayton70436352010-06-30 23:03:03 +0000392 }
Greg Clayton8d646f22012-12-14 22:36:35 +0000393 else if (m_options.current_function)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000394 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000395 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000396 {
397 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
398 result.SetStatus (eReturnStatusFailed);
399 return false;
400 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000401 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000402 if (symbol)
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000403 {
404 range.GetBaseAddress() = symbol->GetAddress();
405 range.SetByteSize(symbol->GetByteSize());
406 }
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000407 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000408
409 // Did the "m_options.frame_line" find a valid range already? If so
410 // skip the rest...
411 if (range.GetByteSize() == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000412 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000413 if (m_options.at_pc)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000414 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000415 if (frame == NULL)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000416 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000417 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
418 result.SetStatus (eReturnStatusFailed);
419 return false;
420 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000421 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000422 if (m_options.num_instructions == 0)
423 {
424 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
425 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
426 }
427 }
428 else
429 {
430 range.GetBaseAddress().SetOffset (m_options.start_addr);
431 if (range.GetBaseAddress().IsValid())
432 {
433 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000434 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000435 if (m_options.end_addr <= m_options.start_addr)
436 {
437 result.AppendErrorWithFormat ("End address before start address.\n");
438 result.SetStatus (eReturnStatusFailed);
439 return false;
440 }
441 range.SetByteSize (m_options.end_addr - m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000442 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000443 }
Jason Molenda9afae622013-04-11 03:14:01 +0000444 else
445 {
446 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS
447 && target
448 && !target->GetSectionLoadList().IsEmpty())
449 {
450 bool failed = false;
451 Address symbol_containing_address;
452 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.symbol_containing_addr, symbol_containing_address))
453 {
454 ModuleSP module_sp (symbol_containing_address.GetModule());
455 SymbolContext sc;
456 module_sp->ResolveSymbolContextForAddress (symbol_containing_address, eSymbolContextEverything, sc);
457 if (sc.function || sc.symbol)
458 {
459 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
460 }
461 else
462 {
463 failed = true;
464 }
465 }
466 else
467 {
468 failed = true;
469 }
470 if (failed)
471 {
472 result.AppendErrorWithFormat ("Could not find function bounds for address 0x%" PRIx64 "\n", m_options.symbol_containing_addr);
473 result.SetStatus (eReturnStatusFailed);
474 return false;
475 }
476 }
477 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000478 }
479 }
Jason Molenda9afae622013-04-11 03:14:01 +0000480
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000481 if (m_options.num_instructions != 0)
482 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000483 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000484 {
485 // The default action is to disassemble the current frame function.
Greg Clayton567e7f32011-09-22 04:58:26 +0000486 if (frame)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000487 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000488 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000489 if (sc.function)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000490 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000491 else if (sc.symbol && sc.symbol->ValueIsAddress())
492 range.GetBaseAddress() = sc.symbol->GetAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000493 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000494 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000495 }
496
Greg Clayton24bc5d92011-03-30 18:16:51 +0000497 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000498 {
499 result.AppendError ("invalid frame");
500 result.SetStatus (eReturnStatusFailed);
501 return false;
502 }
503 }
504
505 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000506 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000507 plugin_name,
Jim Ingham7d408382013-03-02 00:26:47 +0000508 flavor_string,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000509 m_exe_ctx,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000510 range.GetBaseAddress(),
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000511 m_options.num_instructions,
512 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000513 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000514 result.GetOutputStream()))
515 {
516 result.SetStatus (eReturnStatusSuccessFinishResult);
517 }
518 else
519 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000520 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000521 result.SetStatus (eReturnStatusFailed);
522 }
523 }
524 else
525 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000526 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000527 {
528 // The default action is to disassemble the current frame function.
Greg Clayton567e7f32011-09-22 04:58:26 +0000529 if (frame)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000530 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000531 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000532 if (sc.function)
533 range = sc.function->GetAddressRange();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000534 else if (sc.symbol && sc.symbol->ValueIsAddress())
535 {
536 range.GetBaseAddress() = sc.symbol->GetAddress();
537 range.SetByteSize (sc.symbol->GetByteSize());
538 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000539 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000540 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000541 }
542 else
543 {
544 result.AppendError ("invalid frame");
545 result.SetStatus (eReturnStatusFailed);
546 return false;
547 }
548 }
549 if (range.GetByteSize() == 0)
550 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
551
552 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000553 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000554 plugin_name,
Jim Ingham7d408382013-03-02 00:26:47 +0000555 flavor_string,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000556 m_exe_ctx,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000557 range,
558 m_options.num_instructions,
559 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000560 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000561 result.GetOutputStream()))
562 {
563 result.SetStatus (eReturnStatusSuccessFinishResult);
564 }
565 else
566 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000567 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000568 result.SetStatus (eReturnStatusFailed);
569 }
Greg Clayton70436352010-06-30 23:03:03 +0000570 }
Chris Lattner24943d22010-06-08 16:52:24 +0000571 }
572
Jim Ingham34e9a982010-06-15 18:47:14 +0000573 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000574}