blob: 49a56774e2465ac2a406d60be14cbdc52f6e0abf [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
10#include "CommandObjectDisassemble.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/AddressRange.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000017#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Interpreter/CommandCompletions.h"
19#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
21#include "lldb/Core/Disassembler.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000022#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/SourceManager.h"
24#include "lldb/Target/StackFrame.h"
25#include "lldb/Symbol/Symbol.h"
26#include "lldb/Target/Process.h"
27#include "lldb/Target/Target.h"
28
29#define DEFAULT_DISASM_BYTE_SIZE 32
30
31using namespace lldb;
32using namespace lldb_private;
33
34CommandObjectDisassemble::CommandOptions::CommandOptions () :
35 Options(),
36 m_func_name(),
Jim Ingham34e9a982010-06-15 18:47:14 +000037 m_start_addr(),
38 m_end_addr ()
Chris Lattner24943d22010-06-08 16:52:24 +000039{
40 ResetOptionValues();
41}
42
43CommandObjectDisassemble::CommandOptions::~CommandOptions ()
44{
45}
46
47Error
48CommandObjectDisassemble::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
49{
50 Error error;
51
52 char short_option = (char) m_getopt_table[option_idx].val;
53
54 switch (short_option)
55 {
56 case 'm':
57 show_mixed = true;
58 break;
59
60 case 'c':
61 num_lines_context = Args::StringToUInt32(option_arg, 0, 0);
62 break;
63
64 case 'b':
65 show_bytes = true;
66 break;
67
Jim Ingham34e9a982010-06-15 18:47:14 +000068 case 's':
69 m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0);
70 if (m_start_addr == LLDB_INVALID_ADDRESS)
71 m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16);
Chris Lattner24943d22010-06-08 16:52:24 +000072
Jim Ingham34e9a982010-06-15 18:47:14 +000073 if (m_start_addr == LLDB_INVALID_ADDRESS)
74 error.SetErrorStringWithFormat ("Invalid start address string '%s'.\n", optarg);
75 break;
76 case 'e':
77 m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0);
78 if (m_end_addr == LLDB_INVALID_ADDRESS)
79 m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16);
80
81 if (m_end_addr == LLDB_INVALID_ADDRESS)
82 error.SetErrorStringWithFormat ("Invalid end address string '%s'.\n", optarg);
Chris Lattner24943d22010-06-08 16:52:24 +000083 break;
84
85 case 'n':
86 m_func_name = option_arg;
87 break;
88
89 case 'r':
90 raw = true;
91 break;
92
93 default:
94 error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option);
95 break;
96 }
97
98 return error;
99}
100
101void
102CommandObjectDisassemble::CommandOptions::ResetOptionValues ()
103{
104 Options::ResetOptionValues();
105 show_mixed = false;
106 show_bytes = false;
107 num_lines_context = 0;
108 m_func_name.clear();
Jim Ingham34e9a982010-06-15 18:47:14 +0000109 m_start_addr = LLDB_INVALID_ADDRESS;
110 m_end_addr = LLDB_INVALID_ADDRESS;
Sean Callanan944be702010-06-17 00:32:05 +0000111 raw = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000112}
113
114const lldb::OptionDefinition*
115CommandObjectDisassemble::CommandOptions::GetDefinitions ()
116{
117 return g_option_table;
118}
119
120lldb::OptionDefinition
121CommandObjectDisassemble::CommandOptions::g_option_table[] =
122{
Jim Ingham34e9a982010-06-15 18:47:14 +0000123{ LLDB_OPT_SET_ALL, false, "bytes", 'b', no_argument, NULL, 0, NULL, "Show opcode bytes when disassembling."},
124{ LLDB_OPT_SET_ALL, false, "context", 'c', required_argument, NULL, 0, "<num-lines>", "Number of context lines of source to show."},
125{ LLDB_OPT_SET_ALL, false, "mixed", 'm', no_argument, NULL, 0, NULL, "Enable mixed source and assembly display."},
126{ LLDB_OPT_SET_ALL, false, "raw", 'r', no_argument, NULL, 0, NULL, "Print raw disassembly with no symbol information."},
Chris Lattner24943d22010-06-08 16:52:24 +0000127
Jim Ingham34e9a982010-06-15 18:47:14 +0000128{ LLDB_OPT_SET_1, true, "start-address", 's', required_argument, NULL, 0, "<start-address>", "Address to start disassembling."},
129{ LLDB_OPT_SET_1, false, "end-address", 'e', required_argument, NULL, 0, "<end-address>", "Address to start disassembling."},
Chris Lattner24943d22010-06-08 16:52:24 +0000130
Jim Ingham34e9a982010-06-15 18:47:14 +0000131{ LLDB_OPT_SET_2, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, "<function-name>", "Disassemble entire contents of the given function name."},
132
133{ LLDB_OPT_SET_3, false, "current-frame", 'f', no_argument, NULL, 0, "<current-frame>", "Disassemble entire contents of the current frame's function."},
Chris Lattner24943d22010-06-08 16:52:24 +0000134
135{ 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
136};
137
138
139
140//-------------------------------------------------------------------------
141// CommandObjectDisassemble
142//-------------------------------------------------------------------------
143
144CommandObjectDisassemble::CommandObjectDisassemble () :
145 CommandObject ("disassemble",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000146 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
Jim Ingham34e9a982010-06-15 18:47:14 +0000147 "disassemble [<cmd-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +0000148{
149}
150
151CommandObjectDisassemble::~CommandObjectDisassemble()
152{
153}
154
Chris Lattner24943d22010-06-08 16:52:24 +0000155bool
156CommandObjectDisassemble::Execute
157(
Greg Clayton63094e02010-06-23 01:19:29 +0000158 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000159 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000160 CommandReturnObject &result
161)
162{
Jim Inghamc8332952010-08-26 21:32:51 +0000163 Target *target = interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000164 if (target == NULL)
165 {
166 result.AppendError ("invalid target, set executable file using 'file' command");
167 result.SetStatus (eReturnStatusFailed);
168 return false;
169 }
170
171 ArchSpec arch(target->GetArchitecture());
172 if (!arch.IsValid())
173 {
174 result.AppendError ("target needs valid architecure in order to be able to disassemble");
175 result.SetStatus (eReturnStatusFailed);
176 return false;
177 }
178
179 Disassembler *disassembler = Disassembler::FindPlugin(arch);
180
181 if (disassembler == NULL)
182 {
183 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n", arch.AsCString());
184 result.SetStatus (eReturnStatusFailed);
185 return false;
186 }
187
188 result.SetStatus (eReturnStatusSuccessFinishResult);
189
Greg Clayton70436352010-06-30 23:03:03 +0000190 if (command.GetArgumentCount() != 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000191 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000192 result.AppendErrorWithFormat ("\"disassemble\" doesn't take any arguments.\n");
193 result.SetStatus (eReturnStatusFailed);
194 return false;
195 }
Greg Clayton70436352010-06-30 23:03:03 +0000196 ExecutionContext exe_ctx(interpreter.GetDebugger().GetExecutionContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000197
Greg Clayton70436352010-06-30 23:03:03 +0000198 if (m_options.show_mixed && m_options.num_lines_context == 0)
199 m_options.num_lines_context = 3;
Chris Lattner24943d22010-06-08 16:52:24 +0000200
Greg Clayton70436352010-06-30 23:03:03 +0000201 if (!m_options.m_func_name.empty())
202 {
203 ConstString name(m_options.m_func_name.c_str());
204
205 if (Disassembler::Disassemble (interpreter.GetDebugger(),
206 arch,
207 exe_ctx,
208 name,
209 NULL, // Module *
210 m_options.show_mixed ? m_options.num_lines_context : 0,
211 m_options.show_bytes,
212 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000213 {
Greg Clayton70436352010-06-30 23:03:03 +0000214 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000215 }
216 else
217 {
218 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
219 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000220 }
Greg Clayton70436352010-06-30 23:03:03 +0000221 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000222 else
Chris Lattner24943d22010-06-08 16:52:24 +0000223 {
Greg Clayton70436352010-06-30 23:03:03 +0000224 AddressRange range;
225 if (m_options.m_start_addr != LLDB_INVALID_ADDRESS)
226 {
227 range.GetBaseAddress().SetOffset (m_options.m_start_addr);
228 if (m_options.m_end_addr != LLDB_INVALID_ADDRESS)
229 {
230 if (m_options.m_end_addr < m_options.m_start_addr)
231 {
232 result.AppendErrorWithFormat ("End address before start address.\n");
233 result.SetStatus (eReturnStatusFailed);
234 return false;
235 }
236 range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr);
237 }
238 else
239 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
240 }
241 else
242 {
243 if (exe_ctx.frame)
244 {
245 SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
246 if (sc.function)
247 range = sc.function->GetAddressRange();
248 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
249 range = *sc.symbol->GetAddressRangePtr();
250 else
Greg Claytonb04e7a82010-08-24 21:05:24 +0000251 range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress();
Greg Clayton70436352010-06-30 23:03:03 +0000252 }
253 else
254 {
255 result.AppendError ("invalid frame");
256 result.SetStatus (eReturnStatusFailed);
257 return false;
258 }
259 }
260 if (range.GetByteSize() == 0)
261 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
262
263 if (Disassembler::Disassemble (interpreter.GetDebugger(),
264 arch,
265 exe_ctx,
266 range,
267 m_options.show_mixed ? m_options.num_lines_context : 0,
268 m_options.show_bytes,
269 result.GetOutputStream()))
270 {
271 result.SetStatus (eReturnStatusSuccessFinishResult);
272 }
273 else
274 {
275 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr);
276 result.SetStatus (eReturnStatusFailed);
277 }
Chris Lattner24943d22010-06-08 16:52:24 +0000278 }
279
Jim Ingham34e9a982010-06-15 18:47:14 +0000280 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000281}