blob: 8124ce1ef93b564da5d944aeecd4866bfc5ffc95 [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-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 Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Disassembler.h"
Jason Molenda801237a2013-04-11 03:14:01 +000020#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/SourceManager.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000022#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Interpreter/CommandCompletions.h"
24#include "lldb/Interpreter/CommandInterpreter.h"
25#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000026#include "lldb/Interpreter/Options.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Symbol/Symbol.h"
29#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000030#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000031#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/Target.h"
33
34#define DEFAULT_DISASM_BYTE_SIZE 32
Jim Ingham37023b02011-03-22 01:48:42 +000035#define DEFAULT_DISASM_NUM_INS 4
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
Greg Claytoneb0103f2011-04-07 22:46:35 +000040CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen1f1b2692011-04-08 22:15:29 +000041 Options(interpreter),
Jim Ingham37023b02011-03-22 01:48:42 +000042 num_lines_context(0),
43 num_instructions (0),
Greg Clayton32e0a752011-03-30 18:16:51 +000044 func_name(),
Greg Clayton1fb2e7d2012-12-14 22:36:35 +000045 current_function (false),
Greg Clayton32e0a752011-03-30 18:16:51 +000046 start_addr(),
47 end_addr (),
48 at_pc (false),
49 frame_line (false),
50 plugin_name (),
Jim Ingham0f063ba2013-03-02 00:26:47 +000051 flavor_string(),
Jim Ingham3555b5d2011-09-01 01:11:04 +000052 arch(),
Jason Molenda801237a2013-04-11 03:14:01 +000053 some_location_specified (false),
54 symbol_containing_addr ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055{
Greg Claytonf6b8b582011-04-13 00:18:08 +000056 OptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057}
58
59CommandObjectDisassemble::CommandOptions::~CommandOptions ()
60{
61}
62
63Error
Greg Claytonf6b8b582011-04-13 00:18:08 +000064CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065{
66 Error error;
67
Greg Clayton3bcdfc02012-12-04 00:32:51 +000068 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069
Jim Ingham37023b02011-03-22 01:48:42 +000070 bool success;
71
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 switch (short_option)
73 {
74 case 'm':
75 show_mixed = true;
76 break;
77
Greg Clayton357132e2011-03-26 19:14:58 +000078 case 'C':
Jim Ingham37023b02011-03-22 01:48:42 +000079 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
80 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +000081 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
Jim Ingham37023b02011-03-22 01:48:42 +000082 break;
83
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 case 'c':
Jim Ingham37023b02011-03-22 01:48:42 +000085 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
86 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +000087 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 break;
89
90 case 'b':
91 show_bytes = true;
92 break;
93
Jim Ingham86511212010-06-15 18:47:14 +000094 case 's':
Greg Claytonb9d5df52012-12-06 22:49:16 +000095 {
96 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
97 start_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
98 if (start_addr != LLDB_INVALID_ADDRESS)
99 some_location_specified = true;
100 }
Jim Ingham86511212010-06-15 18:47:14 +0000101 break;
102 case 'e':
Greg Claytonb9d5df52012-12-06 22:49:16 +0000103 {
104 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
105 end_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
106 if (end_addr != LLDB_INVALID_ADDRESS)
107 some_location_specified = true;
108 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 case 'n':
Greg Clayton32e0a752011-03-30 18:16:51 +0000111 func_name.assign (option_arg);
Jim Ingham3555b5d2011-09-01 01:11:04 +0000112 some_location_specified = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 break;
114
Jim Ingham37023b02011-03-22 01:48:42 +0000115 case 'p':
Greg Clayton32e0a752011-03-30 18:16:51 +0000116 at_pc = true;
Jim Ingham3555b5d2011-09-01 01:11:04 +0000117 some_location_specified = true;
Greg Clayton32e0a752011-03-30 18:16:51 +0000118 break;
119
120 case 'l':
121 frame_line = true;
122 // Disassemble the current source line kind of implies showing mixed
123 // source code context.
124 show_mixed = true;
Jim Ingham3555b5d2011-09-01 01:11:04 +0000125 some_location_specified = true;
Jim Ingham37023b02011-03-22 01:48:42 +0000126 break;
127
Greg Clayton1080edbc2011-03-25 18:03:16 +0000128 case 'P':
Greg Clayton32e0a752011-03-30 18:16:51 +0000129 plugin_name.assign (option_arg);
Greg Clayton1080edbc2011-03-25 18:03:16 +0000130 break;
131
Jim Ingham0f063ba2013-03-02 00:26:47 +0000132 case 'F':
133 {
134 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
135 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
136 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
137 {
138 flavor_string.assign (option_arg);
139 }
140 else
141 error.SetErrorStringWithFormat("Disassembler flavors are currently only supported for x86 and x86_64 targets.");
142 break;
143 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 case 'r':
145 raw = true;
146 break;
147
Johnny Chen8ceb8ba2010-10-29 19:33:40 +0000148 case 'f':
Greg Clayton1fb2e7d2012-12-14 22:36:35 +0000149 current_function = true;
Jim Ingham3555b5d2011-09-01 01:11:04 +0000150 some_location_specified = true;
Johnny Chen8ceb8ba2010-10-29 19:33:40 +0000151 break;
152
Jason Molenda801237a2013-04-11 03:14:01 +0000153 case 'A':
Greg Clayton70512312012-05-08 01:45:38 +0000154 if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
155 arch.SetTriple (option_arg);
Greg Clayton357132e2011-03-26 19:14:58 +0000156 break;
157
Jason Molenda801237a2013-04-11 03:14:01 +0000158 case 'a':
159 {
160 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
161 symbol_containing_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
162 if (symbol_containing_addr != LLDB_INVALID_ADDRESS)
163 {
164 some_location_specified = true;
165 }
166 }
167 break;
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000170 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 break;
172 }
173
174 return error;
175}
176
177void
Greg Claytonf6b8b582011-04-13 00:18:08 +0000178CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 show_mixed = false;
181 show_bytes = false;
182 num_lines_context = 0;
Jim Ingham37023b02011-03-22 01:48:42 +0000183 num_instructions = 0;
Greg Clayton32e0a752011-03-30 18:16:51 +0000184 func_name.clear();
Greg Clayton1fb2e7d2012-12-14 22:36:35 +0000185 current_function = false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000186 at_pc = false;
187 frame_line = false;
188 start_addr = LLDB_INVALID_ADDRESS;
189 end_addr = LLDB_INVALID_ADDRESS;
Jason Molenda801237a2013-04-11 03:14:01 +0000190 symbol_containing_addr = LLDB_INVALID_ADDRESS;
Sean Callanana68c1a22010-06-17 00:32:05 +0000191 raw = false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000192 plugin_name.clear();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000193
194 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
195
196 // This is a hack till we get the ability to specify features based on architecture. For now GetDisassemblyFlavor
197 // is really only valid for x86 (and for the llvm assembler plugin, but I'm papering over that since that is the
198 // only disassembler plugin we have...
199 if (target)
200 {
201 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
202 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
203 {
204 flavor_string.assign(target->GetDisassemblyFlavor());
205 }
206 else
207 flavor_string.assign ("default");
208
209 }
210 else
211 flavor_string.assign("default");
212
Greg Clayton32e0a752011-03-30 18:16:51 +0000213 arch.Clear();
Jim Ingham3555b5d2011-09-01 01:11:04 +0000214 some_location_specified = false;
215}
216
217Error
218CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
219{
220 if (!some_location_specified)
Greg Clayton1fb2e7d2012-12-14 22:36:35 +0000221 current_function = true;
Jim Ingham3555b5d2011-09-01 01:11:04 +0000222 return Error();
223
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224}
225
Greg Claytone0d378b2011-03-24 21:19:54 +0000226const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227CommandObjectDisassemble::CommandOptions::GetDefinitions ()
228{
229 return g_option_table;
230}
231
Greg Claytone0d378b2011-03-24 21:19:54 +0000232OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233CommandObjectDisassemble::CommandOptions::g_option_table[] =
234{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000235{ LLDB_OPT_SET_ALL, false, "bytes" , 'b', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
236{ LLDB_OPT_SET_ALL, false, "context" , 'C', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
237{ LLDB_OPT_SET_ALL, false, "mixed" , 'm', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
238{ LLDB_OPT_SET_ALL, false, "raw" , 'r', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
239{ LLDB_OPT_SET_ALL, false, "plugin" , 'P', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
240{ LLDB_OPT_SET_ALL, false, "flavor" , 'F', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. "
241 "Currently the only valid options are default, and for Intel"
242 " architectures, att and intel."},
243{ LLDB_OPT_SET_ALL, false, "arch" , 'A', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
Greg Clayton1fb2e7d2012-12-14 22:36:35 +0000244{ LLDB_OPT_SET_1 |
Zachary Turnerd37221d2014-07-09 16:31:49 +0000245 LLDB_OPT_SET_2 , true , "start-address", 's', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeAddressOrExpression,"Address at which to start disassembling."},
246{ LLDB_OPT_SET_1 , false, "end-address" , 'e', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling."},
Greg Clayton1fb2e7d2012-12-14 22:36:35 +0000247{ LLDB_OPT_SET_2 |
248 LLDB_OPT_SET_3 |
249 LLDB_OPT_SET_4 |
Zachary Turnerd37221d2014-07-09 16:31:49 +0000250 LLDB_OPT_SET_5 , false, "count" , 'c', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
251{ LLDB_OPT_SET_3 , false, "name" , 'n', OptionParser::eRequiredArgument , NULL, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
252 "Disassemble entire contents of the given function name."},
253{ LLDB_OPT_SET_4 , false, "frame" , 'f', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
254{ LLDB_OPT_SET_5 , false, "pc" , 'p', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
255{ LLDB_OPT_SET_6 , false, "line" , 'l', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Disassemble the current frame's current source line instructions if there is debug line table information, else disassemble around the pc."},
256{ LLDB_OPT_SET_7 , false, "address" , 'a', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address."},
257{ 0 , false, NULL , 0, 0 , NULL, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258};
259
260
261
262//-------------------------------------------------------------------------
263// CommandObjectDisassemble
264//-------------------------------------------------------------------------
265
Greg Claytona7015092010-09-18 01:14:36 +0000266CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000267 CommandObjectParsed (interpreter,
268 "disassemble",
269 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
270 "disassemble [<cmd-options>]"),
Greg Claytoneb0103f2011-04-07 22:46:35 +0000271 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272{
273}
274
275CommandObjectDisassemble::~CommandObjectDisassemble()
276{
277}
278
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279bool
Jim Ingham5a988412012-06-08 21:56:10 +0000280CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281{
Greg Claytona7015092010-09-18 01:14:36 +0000282 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 if (target == NULL)
284 {
Greg Claytoneffe5c92011-05-03 22:09:39 +0000285 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 result.SetStatus (eReturnStatusFailed);
287 return false;
288 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000289 if (!m_options.arch.IsValid())
290 m_options.arch = target->GetArchitecture();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291
Greg Clayton32e0a752011-03-30 18:16:51 +0000292 if (!m_options.arch.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 {
Greg Clayton357132e2011-03-26 19:14:58 +0000294 result.AppendError ("use the --arch option or set the target architecure to disassemble");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 result.SetStatus (eReturnStatusFailed);
296 return false;
297 }
298
Greg Clayton1080edbc2011-03-25 18:03:16 +0000299 const char *plugin_name = m_options.GetPluginName ();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000300 const char *flavor_string = m_options.GetFlavorString();
301
302 DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303
Sean Callanan9a028512012-08-09 00:50:26 +0000304 if (!disassembler)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 {
Greg Clayton1080edbc2011-03-25 18:03:16 +0000306 if (plugin_name)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000307 {
308 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
Greg Clayton357132e2011-03-26 19:14:58 +0000309 plugin_name,
Greg Clayton32e0a752011-03-30 18:16:51 +0000310 m_options.arch.GetArchitectureName());
Jim Ingham0f063ba2013-03-02 00:26:47 +0000311 }
Greg Clayton1080edbc2011-03-25 18:03:16 +0000312 else
Greg Clayton357132e2011-03-26 19:14:58 +0000313 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
Greg Clayton32e0a752011-03-30 18:16:51 +0000314 m_options.arch.GetArchitectureName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 result.SetStatus (eReturnStatusFailed);
316 return false;
317 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000318 else if (flavor_string != NULL && !disassembler->FlavorValidForArchSpec(m_options.arch, flavor_string))
319 result.AppendWarningWithFormat("invalid disassembler flavor \"%s\", using default.\n", flavor_string);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320
321 result.SetStatus (eReturnStatusSuccessFinishResult);
322
Greg Claytondda4f7b2010-06-30 23:03:03 +0000323 if (command.GetArgumentCount() != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 {
Greg Claytona7015092010-09-18 01:14:36 +0000325 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
Greg Claytoneb0103f2011-04-07 22:46:35 +0000326 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
Jim Ingham86511212010-06-15 18:47:14 +0000327 result.SetStatus (eReturnStatusFailed);
328 return false;
329 }
Jim Ingham37023b02011-03-22 01:48:42 +0000330
Greg Claytondda4f7b2010-06-30 23:03:03 +0000331 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton6dbd3982010-09-15 05:51:24 +0000332 m_options.num_lines_context = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333
Greg Claytonb10d72f2011-06-28 19:01:40 +0000334 // Always show the PC in the disassembly
335 uint32_t options = Disassembler::eOptionMarkPCAddress;
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000336
Greg Claytonb10d72f2011-06-28 19:01:40 +0000337 // Mark the source line for the current PC only if we are doing mixed source and assembly
338 if (m_options.show_mixed)
339 options |= Disassembler::eOptionMarkPCSourceLine;
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000340
341 if (m_options.show_bytes)
342 options |= Disassembler::eOptionShowBytes;
343
344 if (m_options.raw)
345 options |= Disassembler::eOptionRawOuput;
Jim Ingham37023b02011-03-22 01:48:42 +0000346
Greg Clayton32e0a752011-03-30 18:16:51 +0000347 if (!m_options.func_name.empty())
Greg Claytondda4f7b2010-06-30 23:03:03 +0000348 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000349 ConstString name(m_options.func_name.c_str());
Greg Claytondda4f7b2010-06-30 23:03:03 +0000350
Greg Claytona7015092010-09-18 01:14:36 +0000351 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton32e0a752011-03-30 18:16:51 +0000352 m_options.arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000353 plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000354 flavor_string,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000355 m_exe_ctx,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000356 name,
357 NULL, // Module *
Jim Ingham37023b02011-03-22 01:48:42 +0000358 m_options.num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000359 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000360 options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000361 result.GetOutputStream()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000363 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 }
365 else
366 {
367 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
368 result.SetStatus (eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000370 }
Jim Ingham86511212010-06-15 18:47:14 +0000371 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000373 std::vector<AddressRange> ranges;
Greg Clayton32e0a752011-03-30 18:16:51 +0000374 AddressRange range;
Jason Molendab57e4a12013-11-04 09:33:30 +0000375 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Clayton32e0a752011-03-30 18:16:51 +0000376 if (m_options.frame_line)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000377 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000378 if (frame == NULL)
Jim Ingham3555b5d2011-09-01 01:11:04 +0000379 {
380 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
381 result.SetStatus (eReturnStatusFailed);
382 return false;
383 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000384 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
Greg Clayton32e0a752011-03-30 18:16:51 +0000385 if (pc_line_entry.IsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +0000386 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000387 range = pc_line_entry.range;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000388 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000389 else
Jim Ingham37023b02011-03-22 01:48:42 +0000390 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000391 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
392 m_options.show_mixed = false;
Jim Ingham37023b02011-03-22 01:48:42 +0000393 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000394 }
Greg Clayton1fb2e7d2012-12-14 22:36:35 +0000395 else if (m_options.current_function)
Jim Ingham3555b5d2011-09-01 01:11:04 +0000396 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000397 if (frame == NULL)
Jim Ingham3555b5d2011-09-01 01:11:04 +0000398 {
399 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
400 result.SetStatus (eReturnStatusFailed);
401 return false;
402 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000403 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
Jim Ingham3555b5d2011-09-01 01:11:04 +0000404 if (symbol)
Greg Claytone7612132012-03-07 21:03:09 +0000405 {
406 range.GetBaseAddress() = symbol->GetAddress();
407 range.SetByteSize(symbol->GetByteSize());
408 }
Jim Ingham3555b5d2011-09-01 01:11:04 +0000409 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000410
411 // Did the "m_options.frame_line" find a valid range already? If so
412 // skip the rest...
413 if (range.GetByteSize() == 0)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000414 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000415 if (m_options.at_pc)
Jim Ingham37023b02011-03-22 01:48:42 +0000416 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000417 if (frame == NULL)
Jim Ingham37023b02011-03-22 01:48:42 +0000418 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000419 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
420 result.SetStatus (eReturnStatusFailed);
421 return false;
422 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000423 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Clayton32e0a752011-03-30 18:16:51 +0000424 if (m_options.num_instructions == 0)
425 {
426 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
427 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
428 }
Jim Ingham2f2c8762014-03-25 00:15:47 +0000429 ranges.push_back(range);
Greg Clayton32e0a752011-03-30 18:16:51 +0000430 }
431 else
432 {
433 range.GetBaseAddress().SetOffset (m_options.start_addr);
434 if (range.GetBaseAddress().IsValid())
435 {
436 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
Jim Ingham37023b02011-03-22 01:48:42 +0000437 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000438 if (m_options.end_addr <= m_options.start_addr)
439 {
440 result.AppendErrorWithFormat ("End address before start address.\n");
441 result.SetStatus (eReturnStatusFailed);
442 return false;
443 }
444 range.SetByteSize (m_options.end_addr - m_options.start_addr);
Jim Ingham37023b02011-03-22 01:48:42 +0000445 }
Jim Ingham2f4693a2014-04-03 17:16:17 +0000446 ranges.push_back(range);
Jim Ingham37023b02011-03-22 01:48:42 +0000447 }
Jason Molenda801237a2013-04-11 03:14:01 +0000448 else
449 {
450 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS
Jim Ingham2f2c8762014-03-25 00:15:47 +0000451 && target)
Jason Molenda801237a2013-04-11 03:14:01 +0000452 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000453 if (!target->GetSectionLoadList().IsEmpty())
Jason Molenda801237a2013-04-11 03:14:01 +0000454 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000455 bool failed = false;
456 Address symbol_containing_address;
457 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.symbol_containing_addr, symbol_containing_address))
Jason Molenda801237a2013-04-11 03:14:01 +0000458 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000459 ModuleSP module_sp (symbol_containing_address.GetModule());
460 SymbolContext sc;
461 bool resolve_tail_call_address = true; // PC can be one past the address range of the function.
462 module_sp->ResolveSymbolContextForAddress (symbol_containing_address, eSymbolContextEverything, sc,
463 resolve_tail_call_address);
464 if (sc.function || sc.symbol)
465 {
466 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
467 }
468 else
469 {
470 failed = true;
471 }
Jason Molenda801237a2013-04-11 03:14:01 +0000472 }
473 else
474 {
475 failed = true;
476 }
Jim Ingham2f2c8762014-03-25 00:15:47 +0000477 if (failed)
478 {
479 result.AppendErrorWithFormat ("Could not find function bounds for address 0x%" PRIx64 "\n", m_options.symbol_containing_addr);
480 result.SetStatus (eReturnStatusFailed);
481 return false;
482 }
483 ranges.push_back(range);
Jason Molenda801237a2013-04-11 03:14:01 +0000484 }
485 else
486 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000487 for (lldb::ModuleSP module_sp : target->GetImages().Modules())
488 {
489 lldb::addr_t file_addr = m_options.symbol_containing_addr;
490 Address file_address;
491 if (module_sp->ResolveFileAddress(file_addr, file_address))
492 {
493 SymbolContext sc;
494 bool resolve_tail_call_address = true; // PC can be one past the address range of the function.
495 module_sp->ResolveSymbolContextForAddress (file_address, eSymbolContextEverything, sc, resolve_tail_call_address);
496 if (sc.function || sc.symbol)
497 {
498 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
499 ranges.push_back(range);
500 }
501 }
502 }
503
Jason Molenda801237a2013-04-11 03:14:01 +0000504 }
505 }
506 }
Jim Ingham37023b02011-03-22 01:48:42 +0000507 }
508 }
Jim Ingham2f2c8762014-03-25 00:15:47 +0000509 else
510 ranges.push_back(range);
Jason Molenda801237a2013-04-11 03:14:01 +0000511
Jim Ingham37023b02011-03-22 01:48:42 +0000512 if (m_options.num_instructions != 0)
513 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000514 if (ranges.size() == 0)
Jim Ingham37023b02011-03-22 01:48:42 +0000515 {
516 // The default action is to disassemble the current frame function.
Greg Claytonc14ee322011-09-22 04:58:26 +0000517 if (frame)
Jim Ingham37023b02011-03-22 01:48:42 +0000518 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000519 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Ingham37023b02011-03-22 01:48:42 +0000520 if (sc.function)
Greg Clayton32e0a752011-03-30 18:16:51 +0000521 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
Greg Claytone7612132012-03-07 21:03:09 +0000522 else if (sc.symbol && sc.symbol->ValueIsAddress())
523 range.GetBaseAddress() = sc.symbol->GetAddress();
Jim Ingham37023b02011-03-22 01:48:42 +0000524 else
Greg Claytonc14ee322011-09-22 04:58:26 +0000525 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Ingham37023b02011-03-22 01:48:42 +0000526 }
527
Greg Clayton32e0a752011-03-30 18:16:51 +0000528 if (!range.GetBaseAddress().IsValid())
Jim Ingham37023b02011-03-22 01:48:42 +0000529 {
530 result.AppendError ("invalid frame");
531 result.SetStatus (eReturnStatusFailed);
532 return false;
533 }
534 }
Jim Ingham2f2c8762014-03-25 00:15:47 +0000535
536 bool print_sc_header = ranges.size() > 1;
537 for (AddressRange cur_range : ranges)
Jim Ingham37023b02011-03-22 01:48:42 +0000538 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000539 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
540 m_options.arch,
541 plugin_name,
542 flavor_string,
543 m_exe_ctx,
544 cur_range.GetBaseAddress(),
545 m_options.num_instructions,
546 m_options.show_mixed ? m_options.num_lines_context : 0,
547 options,
548 result.GetOutputStream()))
549 {
550 result.SetStatus (eReturnStatusSuccessFinishResult);
551 }
552 else
553 {
554 if (m_options.start_addr != LLDB_INVALID_ADDRESS)
555 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
556 else if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS)
557 result.AppendErrorWithFormat ("Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n", m_options.symbol_containing_addr);
558 result.SetStatus (eReturnStatusFailed);
559 }
Jim Ingham37023b02011-03-22 01:48:42 +0000560 }
Jim Ingham2f2c8762014-03-25 00:15:47 +0000561 if (print_sc_header)
562 result.AppendMessage("\n");
Jim Ingham37023b02011-03-22 01:48:42 +0000563 }
564 else
565 {
Jim Ingham2f2c8762014-03-25 00:15:47 +0000566 if (ranges.size() == 0)
Jim Ingham37023b02011-03-22 01:48:42 +0000567 {
568 // The default action is to disassemble the current frame function.
Greg Claytonc14ee322011-09-22 04:58:26 +0000569 if (frame)
Jim Ingham37023b02011-03-22 01:48:42 +0000570 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000571 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Ingham37023b02011-03-22 01:48:42 +0000572 if (sc.function)
573 range = sc.function->GetAddressRange();
Greg Claytone7612132012-03-07 21:03:09 +0000574 else if (sc.symbol && sc.symbol->ValueIsAddress())
575 {
576 range.GetBaseAddress() = sc.symbol->GetAddress();
577 range.SetByteSize (sc.symbol->GetByteSize());
578 }
Jim Ingham37023b02011-03-22 01:48:42 +0000579 else
Greg Claytonc14ee322011-09-22 04:58:26 +0000580 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Ingham37023b02011-03-22 01:48:42 +0000581 }
582 else
583 {
584 result.AppendError ("invalid frame");
585 result.SetStatus (eReturnStatusFailed);
586 return false;
587 }
Jim Ingham2f2c8762014-03-25 00:15:47 +0000588 ranges.push_back(range);
Jim Ingham37023b02011-03-22 01:48:42 +0000589 }
Jim Ingham2f2c8762014-03-25 00:15:47 +0000590
591 bool print_sc_header = ranges.size() > 1;
592 for (AddressRange cur_range : ranges)
593 {
594 if (cur_range.GetByteSize() == 0)
595 cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
Jim Ingham37023b02011-03-22 01:48:42 +0000596
Jim Ingham2f2c8762014-03-25 00:15:47 +0000597 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
598 m_options.arch,
599 plugin_name,
600 flavor_string,
601 m_exe_ctx,
602 cur_range,
603 m_options.num_instructions,
604 m_options.show_mixed ? m_options.num_lines_context : 0,
605 options,
606 result.GetOutputStream()))
607 {
608 result.SetStatus (eReturnStatusSuccessFinishResult);
609 }
610 else
611 {
612 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
613 result.SetStatus (eReturnStatusFailed);
614 }
615 if (print_sc_header)
616 result.AppendMessage("\n");
Jim Ingham37023b02011-03-22 01:48:42 +0000617 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000618 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 }
620
Jim Ingham86511212010-06-15 18:47:14 +0000621 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622}