blob: e0d41a40af86f5bda8cf43a6db622be018b2afc1 [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
345static inline const char *
Greg Claytoncf015052010-06-11 03:25:34 +0000346TripleForArchSpec (const ArchSpec &arch, char *triple, size_t triple_len)
Chris Lattner24943d22010-06-08 16:52:24 +0000347{
Greg Claytoncf015052010-06-11 03:25:34 +0000348 const char *arch_name = arch.AsCString();
349
350 if (arch_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000351 {
Greg Claytoncf015052010-06-11 03:25:34 +0000352 snprintf(triple, triple_len, "%s-unknown-unknown", arch_name);
353 return triple;
Chris Lattner24943d22010-06-08 16:52:24 +0000354 }
Greg Claytoncf015052010-06-11 03:25:34 +0000355 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000356}
357
358static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000359SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000360{
Greg Claytoncf015052010-06-11 03:25:34 +0000361 const char *arch_name = arch.AsCString();
362
Greg Claytond5f76bb2011-02-04 18:55:41 +0000363 if (arch_name != NULL
364 && ( (0 == ::strncasecmp (arch_name, "i386", 4))
365 || (0 == ::strncasecmp (arch_name, "x86_64", 6))))
Chris Lattner24943d22010-06-08 16:52:24 +0000366 return kEDAssemblySyntaxX86ATT;
Greg Claytoncf015052010-06-11 03:25:34 +0000367
368 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000369}
370
371Disassembler *
372DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
373{
Greg Claytoncf015052010-06-11 03:25:34 +0000374 char triple[256];
Chris Lattner24943d22010-06-08 16:52:24 +0000375
Greg Claytoncf015052010-06-11 03:25:34 +0000376 if (TripleForArchSpec (arch, triple, sizeof(triple)))
Sean Callanan0a298dc2010-06-16 22:19:05 +0000377 return new DisassemblerLLVM(arch);
Greg Claytoncf015052010-06-11 03:25:34 +0000378 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000379}
380
381DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
Greg Claytonb01000f2011-01-17 03:46:26 +0000382 Disassembler (arch),
383 m_disassembler (NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000384{
Greg Claytoncf015052010-06-11 03:25:34 +0000385 char triple[256];
386 if (TripleForArchSpec (arch, triple, sizeof(triple)))
387 {
Greg Claytonb01000f2011-01-17 03:46:26 +0000388 int err = EDGetDisassembler(&m_disassembler, triple, SyntaxForArchSpec (arch));
389 assert (err == 0);
Greg Claytoncf015052010-06-11 03:25:34 +0000390 }
Chris Lattner24943d22010-06-08 16:52:24 +0000391}
392
393DisassemblerLLVM::~DisassemblerLLVM()
394{
395}
396
397size_t
Greg Clayton70436352010-06-30 23:03:03 +0000398DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000399(
Greg Clayton5c4c7462010-10-06 03:09:58 +0000400 const Address &base_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000401 const DataExtractor& data,
402 uint32_t data_offset,
Greg Clayton70436352010-06-30 23:03:03 +0000403 uint32_t num_instructions
Chris Lattner24943d22010-06-08 16:52:24 +0000404)
405{
Greg Claytonb01000f2011-01-17 03:46:26 +0000406 if (m_disassembler == NULL)
407 return 0;
408
Chris Lattner24943d22010-06-08 16:52:24 +0000409 size_t total_inst_byte_size = 0;
410
411 m_instruction_list.Clear();
412
413 while (data.ValidOffset(data_offset) && num_instructions)
414 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000415 Address inst_addr (base_addr);
416 inst_addr.Slide(data_offset);
417 InstructionSP inst_sp (new InstructionLLVM(m_disassembler, inst_addr));
Chris Lattner24943d22010-06-08 16:52:24 +0000418
Greg Clayton5c4c7462010-10-06 03:09:58 +0000419 size_t inst_byte_size = inst_sp->Extract (data, data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000420
421 if (inst_byte_size == 0)
422 break;
423
Greg Clayton5c4c7462010-10-06 03:09:58 +0000424 m_instruction_list.Append (inst_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000425
426 total_inst_byte_size += inst_byte_size;
427 data_offset += inst_byte_size;
428 num_instructions--;
429 }
430
431 return total_inst_byte_size;
432}
433
434void
435DisassemblerLLVM::Initialize()
436{
437 PluginManager::RegisterPlugin (GetPluginNameStatic(),
438 GetPluginDescriptionStatic(),
439 CreateInstance);
440}
441
442void
443DisassemblerLLVM::Terminate()
444{
445 PluginManager::UnregisterPlugin (CreateInstance);
446}
447
448
449const char *
450DisassemblerLLVM::GetPluginNameStatic()
451{
452 return "disassembler.llvm";
453}
454
455const char *
456DisassemblerLLVM::GetPluginDescriptionStatic()
457{
458 return "Disassembler that uses LLVM opcode tables to disassemble i386 and x86_64.";
459}
460
461//------------------------------------------------------------------
462// PluginInterface protocol
463//------------------------------------------------------------------
464const char *
465DisassemblerLLVM::GetPluginName()
466{
467 return "DisassemblerLLVM";
468}
469
470const char *
471DisassemblerLLVM::GetShortPluginName()
472{
473 return GetPluginNameStatic();
474}
475
476uint32_t
477DisassemblerLLVM::GetPluginVersion()
478{
479 return 1;
480}
481
482void
483DisassemblerLLVM::GetPluginCommandHelp (const char *command, Stream *strm)
484{
485}
486
487Error
488DisassemblerLLVM::ExecutePluginCommand (Args &command, Stream *strm)
489{
490 Error error;
491 error.SetErrorString("No plug-in command are currently supported.");
492 return error;
493}
494
495Log *
496DisassemblerLLVM::EnablePluginLogging (Stream *strm, Args &command)
497{
498 return NULL;
499}
500