blob: 5e135376813d18317ecb9f7aebab5b73a76931b1 [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
Greg Clayton238c0a12010-09-18 01:14:36 +0000144CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
145 CommandObject (interpreter,
146 "disassemble",
147 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
148 "disassemble [<cmd-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +0000149{
150}
151
152CommandObjectDisassemble::~CommandObjectDisassemble()
153{
154}
155
Chris Lattner24943d22010-06-08 16:52:24 +0000156bool
157CommandObjectDisassemble::Execute
158(
159 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000160 CommandReturnObject &result
161)
162{
Greg Clayton238c0a12010-09-18 01:14:36 +0000163 Target *target = m_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 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000192 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
193 GetOptions()->GenerateOptionUsage (m_interpreter,
194 result.GetErrorStream(),
195 this);
196
Jim Ingham34e9a982010-06-15 18:47:14 +0000197 result.SetStatus (eReturnStatusFailed);
198 return false;
199 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000200 ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000201
Greg Clayton70436352010-06-30 23:03:03 +0000202 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton1924e242010-09-15 05:51:24 +0000203 m_options.num_lines_context = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000204
Greg Clayton70436352010-06-30 23:03:03 +0000205 if (!m_options.m_func_name.empty())
206 {
207 ConstString name(m_options.m_func_name.c_str());
208
Greg Clayton238c0a12010-09-18 01:14:36 +0000209 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton70436352010-06-30 23:03:03 +0000210 arch,
211 exe_ctx,
212 name,
213 NULL, // Module *
214 m_options.show_mixed ? m_options.num_lines_context : 0,
215 m_options.show_bytes,
216 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000217 {
Greg Clayton70436352010-06-30 23:03:03 +0000218 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000219 }
220 else
221 {
222 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
223 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000224 }
Greg Clayton70436352010-06-30 23:03:03 +0000225 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000226 else
Chris Lattner24943d22010-06-08 16:52:24 +0000227 {
Greg Clayton70436352010-06-30 23:03:03 +0000228 AddressRange range;
229 if (m_options.m_start_addr != LLDB_INVALID_ADDRESS)
230 {
231 range.GetBaseAddress().SetOffset (m_options.m_start_addr);
232 if (m_options.m_end_addr != LLDB_INVALID_ADDRESS)
233 {
234 if (m_options.m_end_addr < m_options.m_start_addr)
235 {
236 result.AppendErrorWithFormat ("End address before start address.\n");
237 result.SetStatus (eReturnStatusFailed);
238 return false;
239 }
240 range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr);
241 }
242 else
243 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
244 }
245 else
246 {
247 if (exe_ctx.frame)
248 {
249 SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
250 if (sc.function)
251 range = sc.function->GetAddressRange();
252 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
253 range = *sc.symbol->GetAddressRangePtr();
254 else
Greg Claytonb04e7a82010-08-24 21:05:24 +0000255 range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress();
Greg Clayton70436352010-06-30 23:03:03 +0000256 }
257 else
258 {
259 result.AppendError ("invalid frame");
260 result.SetStatus (eReturnStatusFailed);
261 return false;
262 }
263 }
264 if (range.GetByteSize() == 0)
265 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
266
Greg Clayton238c0a12010-09-18 01:14:36 +0000267 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton70436352010-06-30 23:03:03 +0000268 arch,
269 exe_ctx,
270 range,
271 m_options.show_mixed ? m_options.num_lines_context : 0,
272 m_options.show_bytes,
273 result.GetOutputStream()))
274 {
275 result.SetStatus (eReturnStatusSuccessFinishResult);
276 }
277 else
278 {
279 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr);
280 result.SetStatus (eReturnStatusFailed);
281 }
Chris Lattner24943d22010-06-08 16:52:24 +0000282 }
283
Jim Ingham34e9a982010-06-15 18:47:14 +0000284 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000285}