blob: bc6bfd16ccc0f505f4660c4e7d71250fa49e4fc8 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Claytonb01000f2011-01-17 03:46:26 +000028#include <assert.h>
Chris Lattner24943d22010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33
34static
35int DataExtractorByteReader(uint8_t *byte, uint64_t address, void *arg)
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
50namespace {
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
64static int IPRegisterReader(uint64_t *value, unsigned regID, void* arg)
65{
66 uint64_t instructionPointer = ((RegisterReaderArg*)arg)->instructionPointer;
67 EDDisassemblerRef disassembler = ((RegisterReaderArg*)arg)->disassembler;
68
69 if(EDRegisterIsProgramCounter(disassembler, regID)) {
70 *value = instructionPointer;
71 return 0;
72 }
73
74 return -1;
75}
76
Greg Clayton5c4c7462010-10-06 03:09:58 +000077DisassemblerLLVM::InstructionLLVM::InstructionLLVM (EDDisassemblerRef disassembler, const Address &addr) :
78 Instruction (addr),
Chris Lattner24943d22010-06-08 16:52:24 +000079 m_disassembler (disassembler)
80{
81}
82
Greg Clayton5c4c7462010-10-06 03:09:58 +000083DisassemblerLLVM::InstructionLLVM::~InstructionLLVM()
Chris Lattner24943d22010-06-08 16:52:24 +000084{
85}
86
87static void
88PadString(Stream *s, const std::string &str, size_t width)
89{
90 int diff = width - str.length();
91
92 if (diff > 0)
93 s->Printf("%s%*.*s", str.c_str(), diff, diff, "");
94 else
95 s->Printf("%s ", str.c_str());
96}
97
98void
Greg Clayton5c4c7462010-10-06 03:09:58 +000099DisassemblerLLVM::InstructionLLVM::Dump
Chris Lattner24943d22010-06-08 16:52:24 +0000100(
101 Stream *s,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000102 bool show_address,
Greg Clayton70436352010-06-30 23:03:03 +0000103 const DataExtractor *bytes,
Chris Lattner24943d22010-06-08 16:52:24 +0000104 uint32_t bytes_offset,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000105 const lldb_private::ExecutionContext* exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000106 bool raw
107)
108{
109 const size_t opcodeColumnWidth = 7;
110 const size_t operandColumnWidth = 25;
111
Greg Clayton5c4c7462010-10-06 03:09:58 +0000112 ExecutionContextScope *exe_scope = NULL;
113 if (exe_ctx)
114 exe_scope = exe_ctx->GetBestExecutionContextScope();
115
Chris Lattner24943d22010-06-08 16:52:24 +0000116 // If we have an address, print it out
Sean Callanan91557b02010-11-10 01:38:28 +0000117 if (GetAddress().IsValid() && show_address)
Greg Clayton70436352010-06-30 23:03:03 +0000118 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000119 if (GetAddress().Dump (s,
120 exe_scope,
121 Address::DumpStyleLoadAddress,
122 Address::DumpStyleModuleWithFileAddress,
123 0))
Greg Clayton70436352010-06-30 23:03:03 +0000124 s->PutCString(": ");
125 }
Chris Lattner24943d22010-06-08 16:52:24 +0000126
127 // If we are supposed to show bytes, "bytes" will be non-NULL.
128 if (bytes)
129 {
130 uint32_t bytes_dumped = bytes->Dump(s, bytes_offset, eFormatBytes, 1, EDInstByteSize(m_inst), UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0) - bytes_offset;
Greg Clayton1e2e2712011-01-08 22:55:04 +0000131 // Allow for 15 bytes of opcodes since this is the max for x86_64.
132 // TOOD: We need to taylor this better for different architectures. For
133 // ARM we would want to show 16 bit opcodes for Thumb as properly byte
134 // swapped uint16_t values, or 32 bit values swapped values for ARM.
135 const uint32_t default_num_opcode_bytes = 15;
Chris Lattner24943d22010-06-08 16:52:24 +0000136 if (bytes_dumped * 3 < (default_num_opcode_bytes*3))
137 {
Greg Clayton1e2e2712011-01-08 22:55:04 +0000138 uint32_t indent_level = (default_num_opcode_bytes*3) - (bytes_dumped * 3) + 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000139 s->Printf("%*.*s", indent_level, indent_level, "");
140 }
141 }
142
143 int numTokens = EDNumTokens(m_inst);
144
145 int currentOpIndex = -1;
146
Sean Callanan8541f2f2010-07-23 02:19:15 +0000147 std::auto_ptr<RegisterReaderArg> rra;
148
149 if (!raw)
150 {
151 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Clayton5c4c7462010-10-06 03:09:58 +0000152 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
153 base_addr = GetAddress().GetLoadAddress (exe_ctx->target);
Sean Callanan8541f2f2010-07-23 02:19:15 +0000154 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton5c4c7462010-10-06 03:09:58 +0000155 base_addr = GetAddress().GetFileAddress ();
Sean Callanan8541f2f2010-07-23 02:19:15 +0000156
157 rra.reset(new RegisterReaderArg(base_addr + EDInstByteSize(m_inst), m_disassembler));
158 }
Chris Lattner24943d22010-06-08 16:52:24 +0000159
160 bool printTokenized = false;
161
162 if (numTokens != -1)
163 {
164 printTokenized = true;
165
166 // Handle the opcode column.
167
168 StreamString opcode;
169
170 int tokenIndex = 0;
171
172 EDTokenRef token;
173 const char *tokenStr;
174
175 if (EDGetToken(&token, m_inst, tokenIndex))
176 printTokenized = false;
177
178 if (!printTokenized || !EDTokenIsOpcode(token))
179 printTokenized = false;
180
181 if (!printTokenized || EDGetTokenString(&tokenStr, token))
182 printTokenized = false;
183
184 // Put the token string into our opcode string
185 opcode.PutCString(tokenStr);
186
187 // If anything follows, it probably starts with some whitespace. Skip it.
188
189 tokenIndex++;
190
191 if (printTokenized && tokenIndex < numTokens)
192 {
193 if(!printTokenized || EDGetToken(&token, m_inst, tokenIndex))
194 printTokenized = false;
195
196 if(!printTokenized || !EDTokenIsWhitespace(token))
197 printTokenized = false;
198 }
199
200 tokenIndex++;
201
202 // Handle the operands and the comment.
203
204 StreamString operands;
205 StreamString comment;
206
207 if (printTokenized)
208 {
209 bool show_token;
210
211 for (; tokenIndex < numTokens; ++tokenIndex)
212 {
213 if (EDGetToken(&token, m_inst, tokenIndex))
214 return;
215
216 if (raw)
217 {
218 show_token = true;
219 }
220 else
221 {
222 int operandIndex = EDOperandIndexForToken(token);
223
224 if (operandIndex >= 0)
225 {
226 if (operandIndex != currentOpIndex)
227 {
228 show_token = true;
229
230 currentOpIndex = operandIndex;
231 EDOperandRef operand;
232
233 if (!EDGetOperand(&operand, m_inst, currentOpIndex))
234 {
235 if (EDOperandIsMemory(operand))
236 {
237 uint64_t operand_value;
238
Sean Callanan8541f2f2010-07-23 02:19:15 +0000239 if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, rra.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000240 {
241 if (EDInstIsBranch(m_inst))
242 {
243 operands.Printf("0x%llx ", operand_value);
244 show_token = false;
245 }
246 else
247 {
248 // Put the address value into the comment
249 comment.Printf("0x%llx ", operand_value);
250 }
251
252 lldb_private::Address so_addr;
Greg Clayton5c4c7462010-10-06 03:09:58 +0000253 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
Chris Lattner24943d22010-06-08 16:52:24 +0000254 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000255 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000256 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Greg Clayton70436352010-06-30 23:03:03 +0000257 }
Greg Clayton5c4c7462010-10-06 03:09:58 +0000258 else
Greg Clayton70436352010-06-30 23:03:03 +0000259 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000260 Module *module = GetAddress().GetModule();
Greg Clayton70436352010-06-30 23:03:03 +0000261 if (module)
Chris Lattner24943d22010-06-08 16:52:24 +0000262 {
Greg Clayton70436352010-06-30 23:03:03 +0000263 if (module->ResolveFileAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000264 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Chris Lattner24943d22010-06-08 16:52:24 +0000265 }
266 }
Greg Clayton70436352010-06-30 23:03:03 +0000267
Chris Lattner24943d22010-06-08 16:52:24 +0000268 } // EDEvaluateOperand
269 } // EDOperandIsMemory
270 } // EDGetOperand
271 } // operandIndex != currentOpIndex
272 } // operandIndex >= 0
273 } // else(raw)
274
275 if (show_token)
276 {
277 if(EDGetTokenString(&tokenStr, token))
278 {
279 printTokenized = false;
280 break;
281 }
282
283 operands.PutCString(tokenStr);
284 }
285 } // for (tokenIndex)
286
287 if (printTokenized)
288 {
289 if (operands.GetString().empty())
290 {
291 s->PutCString(opcode.GetString().c_str());
292 }
293 else
294 {
295 PadString(s, opcode.GetString(), opcodeColumnWidth);
296
297 if (comment.GetString().empty())
298 {
299 s->PutCString(operands.GetString().c_str());
300 }
301 else
302 {
303 PadString(s, operands.GetString(), operandColumnWidth);
304
305 s->PutCString("; ");
306 s->PutCString(comment.GetString().c_str());
307 } // else (comment.GetString().empty())
308 } // else (operands.GetString().empty())
309 } // printTokenized
310 } // for (tokenIndex)
311 } // numTokens != -1
312
313 if (!printTokenized)
314 {
315 const char *str;
316
317 if (EDGetInstString(&str, m_inst))
318 return;
319 else
320 s->PutCString(str);
321 }
322}
323
324bool
Greg Clayton5c4c7462010-10-06 03:09:58 +0000325DisassemblerLLVM::InstructionLLVM::DoesBranch() const
Chris Lattner24943d22010-06-08 16:52:24 +0000326{
327 return EDInstIsBranch(m_inst);
328}
329
330size_t
Greg Clayton5c4c7462010-10-06 03:09:58 +0000331DisassemblerLLVM::InstructionLLVM::GetByteSize() const
Chris Lattner24943d22010-06-08 16:52:24 +0000332{
333 return EDInstByteSize(m_inst);
334}
335
336size_t
Greg Clayton5c4c7462010-10-06 03:09:58 +0000337DisassemblerLLVM::InstructionLLVM::Extract(const DataExtractor &data, uint32_t data_offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000338{
339 if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data)))
340 return EDInstByteSize(m_inst);
341 else
342 return 0;
343}
344
Chris Lattner24943d22010-06-08 16:52:24 +0000345static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000346SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000347{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000348 switch (arch.GetGenericCPUType())
349 {
350 case ArchSpec::eCPU_i386:
351 case ArchSpec::eCPU_x86_64:
Chris Lattner24943d22010-06-08 16:52:24 +0000352 return kEDAssemblySyntaxX86ATT;
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000353
354 case ArchSpec::eCPU_arm:
355 case ArchSpec::eCPU_ppc:
356 case ArchSpec::eCPU_ppc64:
357 case ArchSpec::eCPU_sparc:
358 default:
359 break;
360 }
Greg Claytoncf015052010-06-11 03:25:34 +0000361 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000362}
363
364Disassembler *
365DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
366{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000367 std::auto_ptr<DisassemblerLLVM> disasm_ap (new DisassemblerLLVM(arch));
368
369 if (disasm_ap->IsValid())
370 return disasm_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000371
Greg Claytoncf015052010-06-11 03:25:34 +0000372 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000373}
374
375DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
Greg Claytonb01000f2011-01-17 03:46:26 +0000376 Disassembler (arch),
377 m_disassembler (NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000378{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000379 const std::string &arch_triple = arch.GetTriple().str();
380 if (!arch_triple.empty())
Greg Claytoncf015052010-06-11 03:25:34 +0000381 {
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000382 if (EDGetDisassembler(&m_disassembler, arch_triple.c_str(), SyntaxForArchSpec (arch)))
383 m_disassembler = NULL;
Greg Claytoncf015052010-06-11 03:25:34 +0000384 }
Chris Lattner24943d22010-06-08 16:52:24 +0000385}
386
387DisassemblerLLVM::~DisassemblerLLVM()
388{
389}
390
391size_t
Greg Clayton70436352010-06-30 23:03:03 +0000392DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000393(
Greg Clayton5c4c7462010-10-06 03:09:58 +0000394 const Address &base_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000395 const DataExtractor& data,
396 uint32_t data_offset,
Greg Clayton70436352010-06-30 23:03:03 +0000397 uint32_t num_instructions
Chris Lattner24943d22010-06-08 16:52:24 +0000398)
399{
Greg Claytonb01000f2011-01-17 03:46:26 +0000400 if (m_disassembler == NULL)
401 return 0;
402
Chris Lattner24943d22010-06-08 16:52:24 +0000403 size_t total_inst_byte_size = 0;
404
405 m_instruction_list.Clear();
406
407 while (data.ValidOffset(data_offset) && num_instructions)
408 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000409 Address inst_addr (base_addr);
410 inst_addr.Slide(data_offset);
411 InstructionSP inst_sp (new InstructionLLVM(m_disassembler, inst_addr));
Chris Lattner24943d22010-06-08 16:52:24 +0000412
Greg Clayton5c4c7462010-10-06 03:09:58 +0000413 size_t inst_byte_size = inst_sp->Extract (data, data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000414
415 if (inst_byte_size == 0)
416 break;
417
Greg Clayton5c4c7462010-10-06 03:09:58 +0000418 m_instruction_list.Append (inst_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000419
420 total_inst_byte_size += inst_byte_size;
421 data_offset += inst_byte_size;
422 num_instructions--;
423 }
424
425 return total_inst_byte_size;
426}
427
428void
429DisassemblerLLVM::Initialize()
430{
431 PluginManager::RegisterPlugin (GetPluginNameStatic(),
432 GetPluginDescriptionStatic(),
433 CreateInstance);
434}
435
436void
437DisassemblerLLVM::Terminate()
438{
439 PluginManager::UnregisterPlugin (CreateInstance);
440}
441
442
443const char *
444DisassemblerLLVM::GetPluginNameStatic()
445{
446 return "disassembler.llvm";
447}
448
449const char *
450DisassemblerLLVM::GetPluginDescriptionStatic()
451{
452 return "Disassembler that uses LLVM opcode tables to disassemble i386 and x86_64.";
453}
454
455//------------------------------------------------------------------
456// PluginInterface protocol
457//------------------------------------------------------------------
458const char *
459DisassemblerLLVM::GetPluginName()
460{
461 return "DisassemblerLLVM";
462}
463
464const char *
465DisassemblerLLVM::GetShortPluginName()
466{
467 return GetPluginNameStatic();
468}
469
470uint32_t
471DisassemblerLLVM::GetPluginVersion()
472{
473 return 1;
474}
475
476void
477DisassemblerLLVM::GetPluginCommandHelp (const char *command, Stream *strm)
478{
479}
480
481Error
482DisassemblerLLVM::ExecutePluginCommand (Args &command, Stream *strm)
483{
484 Error error;
485 error.SetErrorString("No plug-in command are currently supported.");
486 return error;
487}
488
489Log *
490DisassemblerLLVM::EnablePluginLogging (Stream *strm, Args &command)
491{
492 return NULL;
493}
494