Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- DisassemblerLLVM.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 "DisassemblerLLVM.h" |
| 11 | |
| 12 | #include "llvm-c/EnhancedDisassembly.h" |
| 13 | |
| 14 | #include "lldb/Core/Address.h" |
| 15 | #include "lldb/Core/DataExtractor.h" |
| 16 | #include "lldb/Core/Disassembler.h" |
| 17 | #include "lldb/Core/Module.h" |
| 18 | #include "lldb/Core/PluginManager.h" |
| 19 | #include "lldb/Core/Stream.h" |
| 20 | #include "lldb/Core/StreamString.h" |
| 21 | #include "lldb/Symbol/SymbolContext.h" |
| 22 | |
| 23 | #include "lldb/Target/ExecutionContext.h" |
| 24 | #include "lldb/Target/Process.h" |
| 25 | #include "lldb/Target/RegisterContext.h" |
| 26 | #include "lldb/Target/Target.h" |
| 27 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 28 | #include <assert.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 34 | static int |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 35 | DataExtractorByteReader (uint8_t *byte, uint64_t address, void *arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
| 37 | DataExtractor &extractor = *((DataExtractor *)arg); |
| 38 | |
| 39 | if (extractor.ValidOffset(address)) |
| 40 | { |
| 41 | *byte = *(extractor.GetDataStart() + address); |
| 42 | return 0; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | return -1; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | namespace { |
| 51 | struct RegisterReaderArg { |
| 52 | const lldb::addr_t instructionPointer; |
| 53 | const EDDisassemblerRef disassembler; |
| 54 | |
| 55 | RegisterReaderArg(lldb::addr_t ip, |
| 56 | EDDisassemblerRef dis) : |
| 57 | instructionPointer(ip), |
| 58 | disassembler(dis) |
| 59 | { |
| 60 | } |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | static int IPRegisterReader(uint64_t *value, unsigned regID, void* arg) |
| 65 | { |
| 66 | uint64_t instructionPointer = ((RegisterReaderArg*)arg)->instructionPointer; |
| 67 | EDDisassemblerRef disassembler = ((RegisterReaderArg*)arg)->disassembler; |
| 68 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 69 | if (EDRegisterIsProgramCounter(disassembler, regID)) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | *value = instructionPointer; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | return -1; |
| 75 | } |
| 76 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 77 | InstructionLLVM::InstructionLLVM (const Address &addr, |
| 78 | AddressClass addr_class, |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 79 | EDDisassemblerRef disassembler, |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 80 | llvm::Triple::ArchType arch_type) : |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 81 | Instruction (addr, addr_class), |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 82 | m_disassembler (disassembler), |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 83 | m_arch_type (arch_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 87 | InstructionLLVM::~InstructionLLVM() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | { |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | PadString(Stream *s, const std::string &str, size_t width) |
| 93 | { |
| 94 | int diff = width - str.length(); |
| 95 | |
| 96 | if (diff > 0) |
| 97 | s->Printf("%s%*.*s", str.c_str(), diff, diff, ""); |
| 98 | else |
| 99 | s->Printf("%s ", str.c_str()); |
| 100 | } |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 101 | static void |
| 102 | AddSymbolicInfo(const ExecutionContext *exe_ctx, ExecutionContextScope *exe_scope, |
| 103 | StreamString &comment, uint64_t operand_value, const Address &inst_addr) |
| 104 | { |
| 105 | Address so_addr; |
| 106 | if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty()) |
| 107 | { |
| 108 | if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress(operand_value, so_addr)) |
| 109 | so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | Module *module = inst_addr.GetModule(); |
| 114 | if (module) |
| 115 | { |
| 116 | if (module->ResolveFileAddress(operand_value, so_addr)) |
| 117 | so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset); |
| 118 | } |
| 119 | } |
| 120 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 122 | #include "llvm/ADT/StringRef.h" |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 123 | static inline void StripSpaces(llvm::StringRef &Str) |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 124 | { |
| 125 | while (!Str.empty() && isspace(Str[0])) |
| 126 | Str = Str.substr(1); |
| 127 | while (!Str.empty() && isspace(Str.back())) |
| 128 | Str = Str.substr(0, Str.size()-1); |
| 129 | } |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 130 | static inline void RStrip(llvm::StringRef &Str, char c) |
| 131 | { |
| 132 | if (!Str.empty() && Str.back() == c) |
| 133 | Str = Str.substr(0, Str.size()-1); |
| 134 | } |
Johnny Chen | 3930cbe | 2011-05-24 20:36:40 +0000 | [diff] [blame] | 135 | // Aligns the raw disassembly (passed as 'str') with the rest of edis'ed disassembly output. |
| 136 | // This is called from non-raw mode when edis of the current m_inst fails for some reason. |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 137 | static void |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 138 | Align(Stream *s, const char *str, size_t opcodeColWidth, size_t operandColWidth) |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 139 | { |
| 140 | llvm::StringRef raw_disasm(str); |
| 141 | StripSpaces(raw_disasm); |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 142 | // Split the raw disassembly into opcode and operands. |
| 143 | std::pair<llvm::StringRef, llvm::StringRef> p = raw_disasm.split('\t'); |
| 144 | PadString(s, p.first, opcodeColWidth); |
| 145 | if (!p.second.empty()) |
| 146 | PadString(s, p.second, operandColWidth); |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 147 | } |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | void |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 150 | InstructionLLVM::Dump |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | ( |
| 152 | Stream *s, |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 153 | uint32_t max_opcode_byte_size, |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 154 | bool show_address, |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 155 | bool show_bytes, |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 156 | const lldb_private::ExecutionContext* exe_ctx, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | bool raw |
| 158 | ) |
| 159 | { |
| 160 | const size_t opcodeColumnWidth = 7; |
| 161 | const size_t operandColumnWidth = 25; |
| 162 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 163 | ExecutionContextScope *exe_scope = NULL; |
| 164 | if (exe_ctx) |
| 165 | exe_scope = exe_ctx->GetBestExecutionContextScope(); |
| 166 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | // If we have an address, print it out |
Sean Callanan | 91557b0 | 2010-11-10 01:38:28 +0000 | [diff] [blame] | 168 | if (GetAddress().IsValid() && show_address) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 169 | { |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 170 | if (GetAddress().Dump (s, |
| 171 | exe_scope, |
| 172 | Address::DumpStyleLoadAddress, |
| 173 | Address::DumpStyleModuleWithFileAddress, |
| 174 | 0)) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 175 | s->PutCString(": "); |
| 176 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | |
| 178 | // If we are supposed to show bytes, "bytes" will be non-NULL. |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 179 | if (show_bytes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 181 | if (m_opcode.GetType() == Opcode::eTypeBytes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 183 | // x86_64 and i386 are the only ones that use bytes right now so |
| 184 | // pad out the byte dump to be able to always show 15 bytes (3 chars each) |
| 185 | // plus a space |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 186 | if (max_opcode_byte_size > 0) |
| 187 | m_opcode.Dump (s, max_opcode_byte_size * 3 + 1); |
| 188 | else |
| 189 | m_opcode.Dump (s, 15 * 3 + 1); |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 190 | } |
| 191 | else |
| 192 | { |
| 193 | // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces) |
| 194 | // plus two for padding... |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 195 | if (max_opcode_byte_size > 0) |
| 196 | m_opcode.Dump (s, max_opcode_byte_size * 3 + 1); |
| 197 | else |
| 198 | m_opcode.Dump (s, 12); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 202 | int numTokens = -1; |
| 203 | |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 204 | // FIXME!!! |
| 205 | /* Remove the following section of code related to force_raw .... */ |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 206 | /* |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 207 | bool force_raw = m_arch_type == llvm::Triple::arm || |
| 208 | m_arch_type == llvm::Triple::thumb; |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 209 | if (!raw) |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 210 | raw = force_raw; |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 211 | */ |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 212 | /* .... when we fix the edis for arm/thumb. */ |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 213 | |
| 214 | if (!raw) |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 215 | numTokens = EDNumTokens(m_inst); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | |
| 217 | int currentOpIndex = -1; |
| 218 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 219 | bool printTokenized = false; |
| 220 | |
| 221 | if (numTokens != -1 && !raw) |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 222 | { |
| 223 | addr_t base_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 224 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 225 | if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty()) |
| 226 | base_addr = GetAddress().GetLoadAddress (exe_ctx->target); |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 227 | if (base_addr == LLDB_INVALID_ADDRESS) |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 228 | base_addr = GetAddress().GetFileAddress (); |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 229 | |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 230 | lldb::addr_t PC = base_addr + EDInstByteSize(m_inst); |
| 231 | |
| 232 | // When executing an ARM instruction, PC reads as the address of the |
| 233 | // current instruction plus 8. And for Thumb, it is plus 4. |
| 234 | if (m_arch_type == llvm::Triple::arm) |
| 235 | PC = base_addr + 8; |
| 236 | else if (m_arch_type == llvm::Triple::thumb) |
| 237 | PC = base_addr + 4; |
| 238 | |
| 239 | RegisterReaderArg rra(PC, m_disassembler); |
Johnny Chen | c5272bf | 2011-05-12 18:48:11 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | printTokenized = true; |
| 242 | |
| 243 | // Handle the opcode column. |
| 244 | |
| 245 | StreamString opcode; |
| 246 | |
| 247 | int tokenIndex = 0; |
| 248 | |
| 249 | EDTokenRef token; |
| 250 | const char *tokenStr; |
| 251 | |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 252 | if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success |
| 253 | printTokenized = false; |
| 254 | else if (!EDTokenIsOpcode(token)) |
| 255 | printTokenized = false; |
| 256 | else if (EDGetTokenString(&tokenStr, token)) // 0 on success |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 257 | printTokenized = false; |
| 258 | |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 259 | if (printTokenized) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | { |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 261 | // Put the token string into our opcode string |
| 262 | opcode.PutCString(tokenStr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 264 | // If anything follows, it probably starts with some whitespace. Skip it. |
| 265 | if (++tokenIndex < numTokens) |
| 266 | { |
| 267 | if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success |
| 268 | printTokenized = false; |
| 269 | else if (!EDTokenIsWhitespace(token)) |
| 270 | printTokenized = false; |
| 271 | } |
| 272 | |
| 273 | ++tokenIndex; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | // Handle the operands and the comment. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | StreamString operands; |
| 278 | StreamString comment; |
| 279 | |
| 280 | if (printTokenized) |
| 281 | { |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 282 | bool show_token = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | |
| 284 | for (; tokenIndex < numTokens; ++tokenIndex) |
| 285 | { |
| 286 | if (EDGetToken(&token, m_inst, tokenIndex)) |
| 287 | return; |
| 288 | |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 289 | int operandIndex = EDOperandIndexForToken(token); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 290 | |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 291 | if (operandIndex >= 0) |
| 292 | { |
| 293 | if (operandIndex != currentOpIndex) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 294 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 295 | show_token = true; |
| 296 | |
| 297 | currentOpIndex = operandIndex; |
| 298 | EDOperandRef operand; |
| 299 | |
| 300 | if (!EDGetOperand(&operand, m_inst, currentOpIndex)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 302 | if (EDOperandIsMemory(operand)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 304 | uint64_t operand_value; |
| 305 | |
| 306 | if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, &rra)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 308 | if (EDInstIsBranch(m_inst)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 310 | operands.Printf("0x%llx ", operand_value); |
| 311 | show_token = false; |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | // Put the address value into the comment |
| 316 | comment.Printf("0x%llx ", operand_value); |
| 317 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 318 | |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 319 | AddSymbolicInfo(exe_ctx, exe_scope, comment, operand_value, GetAddress()); |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 320 | } // EDEvaluateOperand |
| 321 | } // EDOperandIsMemory |
| 322 | } // EDGetOperand |
| 323 | } // operandIndex != currentOpIndex |
| 324 | } // operandIndex >= 0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | |
| 326 | if (show_token) |
| 327 | { |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame^] | 328 | if (EDGetTokenString(&tokenStr, token)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | { |
| 330 | printTokenized = false; |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | operands.PutCString(tokenStr); |
| 335 | } |
| 336 | } // for (tokenIndex) |
| 337 | |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 338 | // FIXME!!! |
| 339 | // Workaround for llvm::tB's operands not properly parsed by ARMAsmParser. |
| 340 | if (m_arch_type == llvm::Triple::thumb && opcode.GetString() == "b") { |
| 341 | const char *inst_str; |
Peter Collingbourne | d77c039 | 2011-05-20 22:42:59 +0000 | [diff] [blame] | 342 | const char *pos = NULL; |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 343 | operands.Clear(); comment.Clear(); |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 344 | if (EDGetInstString(&inst_str, m_inst) == 0 && (pos = strstr(inst_str, "#")) != NULL) { |
| 345 | uint64_t operand_value = PC + atoi(++pos); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 346 | // Put the address value into the operands. |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 347 | operands.Printf("0x%llx ", operand_value); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 348 | AddSymbolicInfo(exe_ctx, exe_scope, comment, operand_value, GetAddress()); |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 351 | // Yet more workaround for "bl #..." and "blx #...". |
| 352 | if ((m_arch_type == llvm::Triple::arm || m_arch_type == llvm::Triple::thumb) && |
| 353 | (opcode.GetString() == "bl" || opcode.GetString() == "blx")) { |
| 354 | const char *inst_str; |
| 355 | const char *pos = NULL; |
| 356 | operands.Clear(); comment.Clear(); |
| 357 | if (EDGetInstString(&inst_str, m_inst) == 0 && (pos = strstr(inst_str, "#")) != NULL) { |
| 358 | uint64_t operand_value = PC + atoi(++pos); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 359 | // Put the address value into the comment. |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 360 | comment.Printf("0x%llx ", operand_value); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 361 | // And the original token string into the operands. |
| 362 | llvm::StringRef Str(pos - 1); |
| 363 | RStrip(Str, '\n'); |
| 364 | operands.PutCString(Str.str().c_str()); |
| 365 | AddSymbolicInfo(exe_ctx, exe_scope, comment, operand_value, GetAddress()); |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 368 | // END of workaround. |
| 369 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 370 | // If both operands and comment are empty, we will just print out |
| 371 | // the raw disassembly. |
| 372 | if (operands.GetString().empty() && comment.GetString().empty()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | { |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 374 | const char *str; |
| 375 | |
| 376 | if (EDGetInstString(&str, m_inst)) |
| 377 | return; |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 378 | Align(s, str, opcodeColumnWidth, operandColumnWidth); |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | { |
| 382 | PadString(s, opcode.GetString(), opcodeColumnWidth); |
| 383 | |
| 384 | if (comment.GetString().empty()) |
| 385 | s->PutCString(operands.GetString().c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | else |
| 387 | { |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 388 | PadString(s, operands.GetString(), operandColumnWidth); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 390 | s->PutCString("; "); |
| 391 | s->PutCString(comment.GetString().c_str()); |
| 392 | } // else (comment.GetString().empty()) |
| 393 | } // else (operands.GetString().empty() && comment.GetString().empty()) |
| 394 | } // printTokenized |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | } // numTokens != -1 |
| 396 | |
| 397 | if (!printTokenized) |
| 398 | { |
| 399 | const char *str; |
| 400 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 401 | if (EDGetInstString(&str, m_inst)) // 0 on success |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 402 | return; |
Johnny Chen | 08251ef | 2011-05-21 00:44:42 +0000 | [diff] [blame] | 403 | if (raw) |
| 404 | s->Write(str, strlen(str) - 1); |
| 405 | else |
| 406 | { |
| 407 | // EDis fails to parse the tokens of this inst. Need to align this |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 408 | // raw disassembly's opcode with the rest of output. |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 409 | Align(s, str, opcodeColumnWidth, operandColumnWidth); |
Johnny Chen | 08251ef | 2011-05-21 00:44:42 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | bool |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 415 | InstructionLLVM::DoesBranch() const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 416 | { |
| 417 | return EDInstIsBranch(m_inst); |
| 418 | } |
| 419 | |
| 420 | size_t |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 421 | InstructionLLVM::Decode (const Disassembler &disassembler, |
| 422 | const lldb_private::DataExtractor &data, |
| 423 | uint32_t data_offset) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 424 | { |
| 425 | if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data))) |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 426 | { |
| 427 | const int byte_size = EDInstByteSize(m_inst); |
| 428 | uint32_t offset = data_offset; |
| 429 | // Make a copy of the opcode in m_opcode |
| 430 | switch (disassembler.GetArchitecture().GetMachine()) |
| 431 | { |
| 432 | case llvm::Triple::x86: |
| 433 | case llvm::Triple::x86_64: |
| 434 | m_opcode.SetOpcodeBytes (data.PeekData (data_offset, byte_size), byte_size); |
| 435 | break; |
| 436 | |
| 437 | case llvm::Triple::arm: |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 438 | case llvm::Triple::thumb: |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 439 | switch (byte_size) |
| 440 | { |
| 441 | case 2: |
| 442 | m_opcode.SetOpcode16 (data.GetU16 (&offset)); |
| 443 | break; |
| 444 | |
| 445 | case 4: |
Caroline Tice | 6b8d3b5 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 446 | { |
| 447 | if (GetAddressClass() == eAddressClassCodeAlternateISA) |
| 448 | { |
| 449 | // If it is a 32-bit THUMB instruction, we need to swap the upper & lower halves. |
| 450 | uint32_t orig_bytes = data.GetU32 (&offset); |
| 451 | uint16_t upper_bits = (orig_bytes >> 16) & ((1u << 16) - 1); |
| 452 | uint16_t lower_bits = orig_bytes & ((1u << 16) - 1); |
| 453 | uint32_t swapped = (lower_bits << 16) | upper_bits; |
| 454 | m_opcode.SetOpcode32 (swapped); |
| 455 | } |
| 456 | else |
| 457 | m_opcode.SetOpcode32 (data.GetU32 (&offset)); |
| 458 | } |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 459 | break; |
| 460 | |
| 461 | default: |
| 462 | assert (!"Invalid ARM opcode size"); |
| 463 | break; |
| 464 | } |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 465 | break; |
| 466 | |
| 467 | default: |
| 468 | assert (!"This shouldn't happen since we control the architecture we allow DisassemblerLLVM to be created for"); |
| 469 | break; |
| 470 | } |
| 471 | return byte_size; |
| 472 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | else |
| 474 | return 0; |
| 475 | } |
| 476 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 477 | static inline EDAssemblySyntax_t |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 478 | SyntaxForArchSpec (const ArchSpec &arch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 480 | switch (arch.GetMachine ()) |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 481 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 482 | case llvm::Triple::x86: |
| 483 | case llvm::Triple::x86_64: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | return kEDAssemblySyntaxX86ATT; |
Sean Callanan | d151c8a | 2011-03-09 01:02:51 +0000 | [diff] [blame] | 485 | case llvm::Triple::arm: |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 486 | case llvm::Triple::thumb: |
Sean Callanan | d151c8a | 2011-03-09 01:02:51 +0000 | [diff] [blame] | 487 | return kEDAssemblySyntaxARMUAL; |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 488 | default: |
| 489 | break; |
| 490 | } |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 491 | return (EDAssemblySyntax_t)0; // default |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | Disassembler * |
| 495 | DisassemblerLLVM::CreateInstance(const ArchSpec &arch) |
| 496 | { |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 497 | std::auto_ptr<DisassemblerLLVM> disasm_ap (new DisassemblerLLVM(arch)); |
| 498 | |
Caroline Tice | 080bf61 | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 499 | if (disasm_ap.get() && disasm_ap->IsValid()) |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 500 | return disasm_ap.release(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 501 | |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 502 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) : |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 506 | Disassembler (arch), |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 507 | m_disassembler (NULL), |
| 508 | m_disassembler_thumb (NULL) // For ARM only |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 509 | { |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 510 | const std::string &arch_triple = arch.GetTriple().str(); |
| 511 | if (!arch_triple.empty()) |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 512 | { |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 513 | if (EDGetDisassembler(&m_disassembler, arch_triple.c_str(), SyntaxForArchSpec (arch))) |
| 514 | m_disassembler = NULL; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 515 | llvm::Triple::ArchType llvm_arch = arch.GetTriple().getArch(); |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 516 | // Don't have the lldb::Triple::thumb architecture here. If someone specifies |
| 517 | // "thumb" as the architecture, we want a thumb only disassembler. But if any |
| 518 | // architecture starting with "arm" if specified, we want to auto detect the |
| 519 | // arm/thumb code automatically using the AddressClass from section offset |
| 520 | // addresses. |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 521 | if (llvm_arch == llvm::Triple::arm) |
| 522 | { |
| 523 | if (EDGetDisassembler(&m_disassembler_thumb, "thumb-apple-darwin", kEDAssemblySyntaxARMUAL)) |
| 524 | m_disassembler_thumb = NULL; |
| 525 | } |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 526 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | DisassemblerLLVM::~DisassemblerLLVM() |
| 530 | { |
| 531 | } |
| 532 | |
| 533 | size_t |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 534 | DisassemblerLLVM::DecodeInstructions |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 535 | ( |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 536 | const Address &base_addr, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 537 | const DataExtractor& data, |
| 538 | uint32_t data_offset, |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 539 | uint32_t num_instructions, |
| 540 | bool append |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | ) |
| 542 | { |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 543 | if (m_disassembler == NULL) |
| 544 | return 0; |
| 545 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 546 | size_t total_inst_byte_size = 0; |
| 547 | |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 548 | if (!append) |
| 549 | m_instruction_list.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 550 | |
| 551 | while (data.ValidOffset(data_offset) && num_instructions) |
| 552 | { |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 553 | Address inst_addr (base_addr); |
| 554 | inst_addr.Slide(data_offset); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 555 | |
| 556 | bool use_thumb = false; |
| 557 | // If we have a thumb disassembler, then we have an ARM architecture |
| 558 | // so we need to check what the instruction address class is to make |
| 559 | // sure we shouldn't be disassembling as thumb... |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 560 | AddressClass inst_address_class = eAddressClassInvalid; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 561 | if (m_disassembler_thumb) |
| 562 | { |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 563 | inst_address_class = inst_addr.GetAddressClass (); |
| 564 | if (inst_address_class == eAddressClassCodeAlternateISA) |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 565 | use_thumb = true; |
| 566 | } |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 567 | |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 568 | InstructionSP inst_sp (new InstructionLLVM (inst_addr, |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 569 | inst_address_class, |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 570 | use_thumb ? m_disassembler_thumb : m_disassembler, |
Johnny Chen | 1608c87 | 2011-05-18 18:22:16 +0000 | [diff] [blame] | 571 | use_thumb ? llvm::Triple::thumb : m_arch.GetMachine())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 572 | |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 573 | size_t inst_byte_size = inst_sp->Decode (*this, data, data_offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 574 | |
| 575 | if (inst_byte_size == 0) |
| 576 | break; |
| 577 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 578 | m_instruction_list.Append (inst_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 579 | |
| 580 | total_inst_byte_size += inst_byte_size; |
| 581 | data_offset += inst_byte_size; |
| 582 | num_instructions--; |
| 583 | } |
| 584 | |
| 585 | return total_inst_byte_size; |
| 586 | } |
| 587 | |
| 588 | void |
| 589 | DisassemblerLLVM::Initialize() |
| 590 | { |
| 591 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 592 | GetPluginDescriptionStatic(), |
| 593 | CreateInstance); |
| 594 | } |
| 595 | |
| 596 | void |
| 597 | DisassemblerLLVM::Terminate() |
| 598 | { |
| 599 | PluginManager::UnregisterPlugin (CreateInstance); |
| 600 | } |
| 601 | |
| 602 | |
| 603 | const char * |
| 604 | DisassemblerLLVM::GetPluginNameStatic() |
| 605 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 606 | return "llvm"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | const char * |
| 610 | DisassemblerLLVM::GetPluginDescriptionStatic() |
| 611 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 612 | return "Disassembler that uses LLVM opcode tables to disassemble i386, x86_64 and ARM."; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | //------------------------------------------------------------------ |
| 616 | // PluginInterface protocol |
| 617 | //------------------------------------------------------------------ |
| 618 | const char * |
| 619 | DisassemblerLLVM::GetPluginName() |
| 620 | { |
| 621 | return "DisassemblerLLVM"; |
| 622 | } |
| 623 | |
| 624 | const char * |
| 625 | DisassemblerLLVM::GetShortPluginName() |
| 626 | { |
| 627 | return GetPluginNameStatic(); |
| 628 | } |
| 629 | |
| 630 | uint32_t |
| 631 | DisassemblerLLVM::GetPluginVersion() |
| 632 | { |
| 633 | return 1; |
| 634 | } |
| 635 | |