Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/CommandCompletions.h" |
| 19 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 20 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 21 | #include "lldb/Core/Disassembler.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/SourceManager.h" |
Jason Molenda | a6b71de | 2010-11-04 09:40:56 +0000 | [diff] [blame^] | 24 | #include "lldb/Core/Stream.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Target/StackFrame.h" |
| 26 | #include "lldb/Symbol/Symbol.h" |
| 27 | #include "lldb/Target/Process.h" |
| 28 | #include "lldb/Target/Target.h" |
| 29 | |
Jason Molenda | a6b71de | 2010-11-04 09:40:56 +0000 | [diff] [blame^] | 30 | #include "lldb/Target/StackFrame.h" |
| 31 | #include "lldb/Target/Thread.h" |
| 32 | #include "lldb/Symbol/FuncUnwinders.h" |
| 33 | #include "lldb/Symbol/UnwindPlan.h" |
| 34 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
| 35 | #include "lldb/Utility/ArchVolatileRegs.h" |
| 36 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | #define DEFAULT_DISASM_BYTE_SIZE 32 |
| 38 | |
| 39 | using namespace lldb; |
| 40 | using namespace lldb_private; |
| 41 | |
| 42 | CommandObjectDisassemble::CommandOptions::CommandOptions () : |
| 43 | Options(), |
| 44 | m_func_name(), |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 45 | m_start_addr(), |
| 46 | m_end_addr () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | { |
| 48 | ResetOptionValues(); |
| 49 | } |
| 50 | |
| 51 | CommandObjectDisassemble::CommandOptions::~CommandOptions () |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | Error |
| 56 | CommandObjectDisassemble::CommandOptions::SetOptionValue (int option_idx, const char *option_arg) |
| 57 | { |
| 58 | Error error; |
| 59 | |
| 60 | char short_option = (char) m_getopt_table[option_idx].val; |
| 61 | |
| 62 | switch (short_option) |
| 63 | { |
| 64 | case 'm': |
| 65 | show_mixed = true; |
| 66 | break; |
| 67 | |
| 68 | case 'c': |
| 69 | num_lines_context = Args::StringToUInt32(option_arg, 0, 0); |
| 70 | break; |
| 71 | |
| 72 | case 'b': |
| 73 | show_bytes = true; |
| 74 | break; |
| 75 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 76 | case 's': |
| 77 | m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0); |
| 78 | if (m_start_addr == LLDB_INVALID_ADDRESS) |
| 79 | m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 81 | if (m_start_addr == LLDB_INVALID_ADDRESS) |
| 82 | error.SetErrorStringWithFormat ("Invalid start address string '%s'.\n", optarg); |
| 83 | break; |
| 84 | case 'e': |
| 85 | m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0); |
| 86 | if (m_end_addr == LLDB_INVALID_ADDRESS) |
| 87 | m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16); |
| 88 | |
| 89 | if (m_end_addr == LLDB_INVALID_ADDRESS) |
| 90 | error.SetErrorStringWithFormat ("Invalid end address string '%s'.\n", optarg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | break; |
| 92 | |
| 93 | case 'n': |
| 94 | m_func_name = option_arg; |
| 95 | break; |
| 96 | |
| 97 | case 'r': |
| 98 | raw = true; |
| 99 | break; |
| 100 | |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 101 | case 'f': |
| 102 | // The default action is to disassemble the function for the current frame. |
| 103 | // There's no need to set any flag. |
| 104 | break; |
| 105 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | default: |
| 107 | error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option); |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | return error; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | CommandObjectDisassemble::CommandOptions::ResetOptionValues () |
| 116 | { |
| 117 | Options::ResetOptionValues(); |
| 118 | show_mixed = false; |
| 119 | show_bytes = false; |
| 120 | num_lines_context = 0; |
| 121 | m_func_name.clear(); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 122 | m_start_addr = LLDB_INVALID_ADDRESS; |
| 123 | m_end_addr = LLDB_INVALID_ADDRESS; |
Sean Callanan | 944be70 | 2010-06-17 00:32:05 +0000 | [diff] [blame] | 124 | raw = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | const lldb::OptionDefinition* |
| 128 | CommandObjectDisassemble::CommandOptions::GetDefinitions () |
| 129 | { |
| 130 | return g_option_table; |
| 131 | } |
| 132 | |
| 133 | lldb::OptionDefinition |
| 134 | CommandObjectDisassemble::CommandOptions::g_option_table[] = |
| 135 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 136 | { LLDB_OPT_SET_ALL, false, "bytes", 'b', no_argument, NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."}, |
| 137 | { LLDB_OPT_SET_ALL, false, "context", 'c', required_argument, NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."}, |
| 138 | { LLDB_OPT_SET_ALL, false, "mixed", 'm', no_argument, NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."}, |
| 139 | { LLDB_OPT_SET_ALL, false, "raw", 'r', no_argument, NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 141 | { LLDB_OPT_SET_1, true, "start-address", 's', required_argument, NULL, 0, eArgTypeStartAddress, "Address at which to start disassembling."}, |
| 142 | { LLDB_OPT_SET_1, false, "end-address", 'e', required_argument, NULL, 0, eArgTypeEndAddress, "Address at which to end disassembling."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 144 | { LLDB_OPT_SET_2, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."}, |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 145 | |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 146 | { LLDB_OPT_SET_3, true, "current-frame", 'f', no_argument, NULL, 0, eArgTypeNone, "Disassemble entire contents of the current frame's function."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 148 | { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | |
| 152 | |
| 153 | //------------------------------------------------------------------------- |
| 154 | // CommandObjectDisassemble |
| 155 | //------------------------------------------------------------------------- |
| 156 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 157 | CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) : |
| 158 | CommandObject (interpreter, |
| 159 | "disassemble", |
| 160 | "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.", |
| 161 | "disassemble [<cmd-options>]") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | { |
| 163 | } |
| 164 | |
| 165 | CommandObjectDisassemble::~CommandObjectDisassemble() |
| 166 | { |
| 167 | } |
| 168 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | bool |
| 170 | CommandObjectDisassemble::Execute |
| 171 | ( |
| 172 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | CommandReturnObject &result |
| 174 | ) |
| 175 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 176 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | if (target == NULL) |
| 178 | { |
| 179 | result.AppendError ("invalid target, set executable file using 'file' command"); |
| 180 | result.SetStatus (eReturnStatusFailed); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | ArchSpec arch(target->GetArchitecture()); |
| 185 | if (!arch.IsValid()) |
| 186 | { |
| 187 | result.AppendError ("target needs valid architecure in order to be able to disassemble"); |
| 188 | result.SetStatus (eReturnStatusFailed); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | Disassembler *disassembler = Disassembler::FindPlugin(arch); |
| 193 | |
| 194 | if (disassembler == NULL) |
| 195 | { |
| 196 | result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n", arch.AsCString()); |
| 197 | result.SetStatus (eReturnStatusFailed); |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 202 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 203 | if (command.GetArgumentCount() != 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 205 | result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n"); |
| 206 | GetOptions()->GenerateOptionUsage (m_interpreter, |
| 207 | result.GetErrorStream(), |
| 208 | this); |
| 209 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 210 | result.SetStatus (eReturnStatusFailed); |
| 211 | return false; |
| 212 | } |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 213 | ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 215 | if (m_options.show_mixed && m_options.num_lines_context == 0) |
Greg Clayton | 1924e24 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 216 | m_options.num_lines_context = 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 218 | if (!m_options.m_func_name.empty()) |
| 219 | { |
| 220 | ConstString name(m_options.m_func_name.c_str()); |
| 221 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 222 | if (Disassembler::Disassemble (m_interpreter.GetDebugger(), |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 223 | arch, |
| 224 | exe_ctx, |
| 225 | name, |
| 226 | NULL, // Module * |
| 227 | m_options.show_mixed ? m_options.num_lines_context : 0, |
| 228 | m_options.show_bytes, |
| 229 | result.GetOutputStream())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | { |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 231 | result.SetStatus (eReturnStatusSuccessFinishResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | } |
| 233 | else |
| 234 | { |
| 235 | result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString()); |
| 236 | result.SetStatus (eReturnStatusFailed); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 238 | } |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 239 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | { |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 241 | AddressRange range; |
| 242 | if (m_options.m_start_addr != LLDB_INVALID_ADDRESS) |
| 243 | { |
| 244 | range.GetBaseAddress().SetOffset (m_options.m_start_addr); |
| 245 | if (m_options.m_end_addr != LLDB_INVALID_ADDRESS) |
| 246 | { |
| 247 | if (m_options.m_end_addr < m_options.m_start_addr) |
| 248 | { |
| 249 | result.AppendErrorWithFormat ("End address before start address.\n"); |
| 250 | result.SetStatus (eReturnStatusFailed); |
| 251 | return false; |
| 252 | } |
| 253 | range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr); |
| 254 | } |
| 255 | else |
| 256 | range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE); |
| 257 | } |
| 258 | else |
| 259 | { |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 260 | // The default action is to disassemble the current frame function. |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 261 | if (exe_ctx.frame) |
| 262 | { |
Jason Molenda | a6b71de | 2010-11-04 09:40:56 +0000 | [diff] [blame^] | 263 | // SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); |
| 264 | SymbolContext sc(exe_ctx.frame->GetSymbolContext (eSymbolContextEverything)); |
| 265 | |
| 266 | ArchVolatileRegs *vr = ArchVolatileRegs::FindPlugin (exe_ctx.target->GetArchitecture()); |
| 267 | Address pc = exe_ctx.frame->GetFrameCodeAddress(); |
| 268 | FuncUnwindersSP fu = pc.GetModule()->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (pc, sc); |
| 269 | if (fu != NULL) |
| 270 | { |
| 271 | |
| 272 | Address first_non_prologue_insn = fu->GetFirstNonPrologueInsn (*exe_ctx.target); |
| 273 | UnwindPlan *up = fu->GetUnwindPlanAtCallSite(); |
| 274 | Stream *s = &result.GetOutputStream(); |
| 275 | if (up) |
| 276 | { |
| 277 | s->Printf ("\nJSMDEBUG: unwind plan at call site (from eh_frame)\n"); |
| 278 | up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread()); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | result.GetOutputStream().Printf("No UnwindPlanAtCallSite available.\n"); |
| 283 | } |
| 284 | up = fu->GetUnwindPlanAtNonCallSite(exe_ctx.frame->GetThread()); |
| 285 | s->Printf ("\nJSMDEBUG: unwind plan at non-call site (from disassembly)\n"); |
| 286 | up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread()); |
| 287 | |
| 288 | up = fu->GetUnwindPlanFastUnwind(exe_ctx.frame->GetThread()); |
| 289 | if (up) |
| 290 | { |
| 291 | s->Printf ("\nJSMDEBUG: fast unwind plan\n"); |
| 292 | up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread()); |
| 293 | } |
| 294 | |
| 295 | up = fu->GetUnwindPlanArchitectureDefault(exe_ctx.frame->GetThread()); |
| 296 | if (up) |
| 297 | { |
| 298 | s->Printf ("\nJSMDEBUG: architectural default unwind plan\n"); |
| 299 | up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread()); |
| 300 | } |
| 301 | } |
| 302 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 303 | if (sc.function) |
| 304 | range = sc.function->GetAddressRange(); |
| 305 | else if (sc.symbol && sc.symbol->GetAddressRangePtr()) |
| 306 | range = *sc.symbol->GetAddressRangePtr(); |
| 307 | else |
Greg Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 308 | range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress(); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 309 | } |
| 310 | else |
| 311 | { |
| 312 | result.AppendError ("invalid frame"); |
| 313 | result.SetStatus (eReturnStatusFailed); |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | if (range.GetByteSize() == 0) |
| 318 | range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE); |
| 319 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 320 | if (Disassembler::Disassemble (m_interpreter.GetDebugger(), |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 321 | arch, |
| 322 | exe_ctx, |
| 323 | range, |
| 324 | m_options.show_mixed ? m_options.num_lines_context : 0, |
| 325 | m_options.show_bytes, |
| 326 | result.GetOutputStream())) |
| 327 | { |
| 328 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr); |
| 333 | result.SetStatus (eReturnStatusFailed); |
| 334 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 337 | return result.Succeeded(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 338 | } |