blob: e0fc02c3becd260704e2919fbcbad28fa1858b31 [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
28#include <memory>
29#include <string>
30
31using namespace lldb;
32using namespace lldb_private;
33
34
35static
36int DataExtractorByteReader(uint8_t *byte, uint64_t address, void *arg)
37{
38 DataExtractor &extractor = *((DataExtractor *)arg);
39
40 if (extractor.ValidOffset(address))
41 {
42 *byte = *(extractor.GetDataStart() + address);
43 return 0;
44 }
45 else
46 {
47 return -1;
48 }
49}
50
51namespace {
52 struct RegisterReaderArg {
53 const lldb::addr_t instructionPointer;
54 const EDDisassemblerRef disassembler;
55
56 RegisterReaderArg(lldb::addr_t ip,
57 EDDisassemblerRef dis) :
58 instructionPointer(ip),
59 disassembler(dis)
60 {
61 }
62 };
63}
64
65static int IPRegisterReader(uint64_t *value, unsigned regID, void* arg)
66{
67 uint64_t instructionPointer = ((RegisterReaderArg*)arg)->instructionPointer;
68 EDDisassemblerRef disassembler = ((RegisterReaderArg*)arg)->disassembler;
69
70 if(EDRegisterIsProgramCounter(disassembler, regID)) {
71 *value = instructionPointer;
72 return 0;
73 }
74
75 return -1;
76}
77
Greg Clayton5c4c7462010-10-06 03:09:58 +000078DisassemblerLLVM::InstructionLLVM::InstructionLLVM (EDDisassemblerRef disassembler, const Address &addr) :
79 Instruction (addr),
Chris Lattner24943d22010-06-08 16:52:24 +000080 m_disassembler (disassembler)
81{
82}
83
Greg Clayton5c4c7462010-10-06 03:09:58 +000084DisassemblerLLVM::InstructionLLVM::~InstructionLLVM()
Chris Lattner24943d22010-06-08 16:52:24 +000085{
86}
87
88static void
89PadString(Stream *s, const std::string &str, size_t width)
90{
91 int diff = width - str.length();
92
93 if (diff > 0)
94 s->Printf("%s%*.*s", str.c_str(), diff, diff, "");
95 else
96 s->Printf("%s ", str.c_str());
97}
98
99void
Greg Clayton5c4c7462010-10-06 03:09:58 +0000100DisassemblerLLVM::InstructionLLVM::Dump
Chris Lattner24943d22010-06-08 16:52:24 +0000101(
102 Stream *s,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000103 bool show_address,
Greg Clayton70436352010-06-30 23:03:03 +0000104 const DataExtractor *bytes,
Chris Lattner24943d22010-06-08 16:52:24 +0000105 uint32_t bytes_offset,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000106 const lldb_private::ExecutionContext* exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000107 bool raw
108)
109{
110 const size_t opcodeColumnWidth = 7;
111 const size_t operandColumnWidth = 25;
112
Greg Clayton5c4c7462010-10-06 03:09:58 +0000113 ExecutionContextScope *exe_scope = NULL;
114 if (exe_ctx)
115 exe_scope = exe_ctx->GetBestExecutionContextScope();
116
Chris Lattner24943d22010-06-08 16:52:24 +0000117 // If we have an address, print it out
Greg Clayton5c4c7462010-10-06 03:09:58 +0000118 if (GetAddress().IsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000119 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000120 if (GetAddress().Dump (s,
121 exe_scope,
122 Address::DumpStyleLoadAddress,
123 Address::DumpStyleModuleWithFileAddress,
124 0))
Greg Clayton70436352010-06-30 23:03:03 +0000125 s->PutCString(": ");
126 }
Chris Lattner24943d22010-06-08 16:52:24 +0000127
128 // If we are supposed to show bytes, "bytes" will be non-NULL.
129 if (bytes)
130 {
131 uint32_t bytes_dumped = bytes->Dump(s, bytes_offset, eFormatBytes, 1, EDInstByteSize(m_inst), UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0) - bytes_offset;
132 // Allow for 8 bytes of opcodes normally
133 const uint32_t default_num_opcode_bytes = 9;
134 if (bytes_dumped * 3 < (default_num_opcode_bytes*3))
135 {
136 uint32_t indent_level = (default_num_opcode_bytes*3) - (bytes_dumped * 3);
137 s->Printf("%*.*s", indent_level, indent_level, "");
138 }
139 }
140
141 int numTokens = EDNumTokens(m_inst);
142
143 int currentOpIndex = -1;
144
Sean Callanan8541f2f2010-07-23 02:19:15 +0000145 std::auto_ptr<RegisterReaderArg> rra;
146
147 if (!raw)
148 {
149 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Clayton5c4c7462010-10-06 03:09:58 +0000150 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
151 base_addr = GetAddress().GetLoadAddress (exe_ctx->target);
Sean Callanan8541f2f2010-07-23 02:19:15 +0000152 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton5c4c7462010-10-06 03:09:58 +0000153 base_addr = GetAddress().GetFileAddress ();
Sean Callanan8541f2f2010-07-23 02:19:15 +0000154
155 rra.reset(new RegisterReaderArg(base_addr + EDInstByteSize(m_inst), m_disassembler));
156 }
Chris Lattner24943d22010-06-08 16:52:24 +0000157
158 bool printTokenized = false;
159
160 if (numTokens != -1)
161 {
162 printTokenized = true;
163
164 // Handle the opcode column.
165
166 StreamString opcode;
167
168 int tokenIndex = 0;
169
170 EDTokenRef token;
171 const char *tokenStr;
172
173 if (EDGetToken(&token, m_inst, tokenIndex))
174 printTokenized = false;
175
176 if (!printTokenized || !EDTokenIsOpcode(token))
177 printTokenized = false;
178
179 if (!printTokenized || EDGetTokenString(&tokenStr, token))
180 printTokenized = false;
181
182 // Put the token string into our opcode string
183 opcode.PutCString(tokenStr);
184
185 // If anything follows, it probably starts with some whitespace. Skip it.
186
187 tokenIndex++;
188
189 if (printTokenized && tokenIndex < numTokens)
190 {
191 if(!printTokenized || EDGetToken(&token, m_inst, tokenIndex))
192 printTokenized = false;
193
194 if(!printTokenized || !EDTokenIsWhitespace(token))
195 printTokenized = false;
196 }
197
198 tokenIndex++;
199
200 // Handle the operands and the comment.
201
202 StreamString operands;
203 StreamString comment;
204
205 if (printTokenized)
206 {
207 bool show_token;
208
209 for (; tokenIndex < numTokens; ++tokenIndex)
210 {
211 if (EDGetToken(&token, m_inst, tokenIndex))
212 return;
213
214 if (raw)
215 {
216 show_token = true;
217 }
218 else
219 {
220 int operandIndex = EDOperandIndexForToken(token);
221
222 if (operandIndex >= 0)
223 {
224 if (operandIndex != currentOpIndex)
225 {
226 show_token = true;
227
228 currentOpIndex = operandIndex;
229 EDOperandRef operand;
230
231 if (!EDGetOperand(&operand, m_inst, currentOpIndex))
232 {
233 if (EDOperandIsMemory(operand))
234 {
235 uint64_t operand_value;
236
Sean Callanan8541f2f2010-07-23 02:19:15 +0000237 if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, rra.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000238 {
239 if (EDInstIsBranch(m_inst))
240 {
241 operands.Printf("0x%llx ", operand_value);
242 show_token = false;
243 }
244 else
245 {
246 // Put the address value into the comment
247 comment.Printf("0x%llx ", operand_value);
248 }
249
250 lldb_private::Address so_addr;
Greg Clayton5c4c7462010-10-06 03:09:58 +0000251 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
Chris Lattner24943d22010-06-08 16:52:24 +0000252 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000253 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000254 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Greg Clayton70436352010-06-30 23:03:03 +0000255 }
Greg Clayton5c4c7462010-10-06 03:09:58 +0000256 else
Greg Clayton70436352010-06-30 23:03:03 +0000257 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000258 Module *module = GetAddress().GetModule();
Greg Clayton70436352010-06-30 23:03:03 +0000259 if (module)
Chris Lattner24943d22010-06-08 16:52:24 +0000260 {
Greg Clayton70436352010-06-30 23:03:03 +0000261 if (module->ResolveFileAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000262 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Chris Lattner24943d22010-06-08 16:52:24 +0000263 }
264 }
Greg Clayton70436352010-06-30 23:03:03 +0000265
Chris Lattner24943d22010-06-08 16:52:24 +0000266 } // EDEvaluateOperand
267 } // EDOperandIsMemory
268 } // EDGetOperand
269 } // operandIndex != currentOpIndex
270 } // operandIndex >= 0
271 } // else(raw)
272
273 if (show_token)
274 {
275 if(EDGetTokenString(&tokenStr, token))
276 {
277 printTokenized = false;
278 break;
279 }
280
281 operands.PutCString(tokenStr);
282 }
283 } // for (tokenIndex)
284
285 if (printTokenized)
286 {
287 if (operands.GetString().empty())
288 {
289 s->PutCString(opcode.GetString().c_str());
290 }
291 else
292 {
293 PadString(s, opcode.GetString(), opcodeColumnWidth);
294
295 if (comment.GetString().empty())
296 {
297 s->PutCString(operands.GetString().c_str());
298 }
299 else
300 {
301 PadString(s, operands.GetString(), operandColumnWidth);
302
303 s->PutCString("; ");
304 s->PutCString(comment.GetString().c_str());
305 } // else (comment.GetString().empty())
306 } // else (operands.GetString().empty())
307 } // printTokenized
308 } // for (tokenIndex)
309 } // numTokens != -1
310
311 if (!printTokenized)
312 {
313 const char *str;
314
315 if (EDGetInstString(&str, m_inst))
316 return;
317 else
318 s->PutCString(str);
319 }
320}
321
322bool
Greg Clayton5c4c7462010-10-06 03:09:58 +0000323DisassemblerLLVM::InstructionLLVM::DoesBranch() const
Chris Lattner24943d22010-06-08 16:52:24 +0000324{
325 return EDInstIsBranch(m_inst);
326}
327
328size_t
Greg Clayton5c4c7462010-10-06 03:09:58 +0000329DisassemblerLLVM::InstructionLLVM::GetByteSize() const
Chris Lattner24943d22010-06-08 16:52:24 +0000330{
331 return EDInstByteSize(m_inst);
332}
333
334size_t
Greg Clayton5c4c7462010-10-06 03:09:58 +0000335DisassemblerLLVM::InstructionLLVM::Extract(const DataExtractor &data, uint32_t data_offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000336{
337 if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data)))
338 return EDInstByteSize(m_inst);
339 else
340 return 0;
341}
342
343static inline const char *
Greg Claytoncf015052010-06-11 03:25:34 +0000344TripleForArchSpec (const ArchSpec &arch, char *triple, size_t triple_len)
Chris Lattner24943d22010-06-08 16:52:24 +0000345{
Greg Claytoncf015052010-06-11 03:25:34 +0000346 const char *arch_name = arch.AsCString();
347
348 if (arch_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000349 {
Greg Claytoncf015052010-06-11 03:25:34 +0000350 snprintf(triple, triple_len, "%s-unknown-unknown", arch_name);
351 return triple;
Chris Lattner24943d22010-06-08 16:52:24 +0000352 }
Greg Claytoncf015052010-06-11 03:25:34 +0000353 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000354}
355
356static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000357SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000358{
Greg Claytoncf015052010-06-11 03:25:34 +0000359 const char *arch_name = arch.AsCString();
360
361 if (arch_name != NULL &&
362 ((strcasestr (arch_name, "i386") == arch_name) ||
363 (strcasestr (arch_name, "x86_64") == arch_name)))
Chris Lattner24943d22010-06-08 16:52:24 +0000364 return kEDAssemblySyntaxX86ATT;
Greg Claytoncf015052010-06-11 03:25:34 +0000365
366 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000367}
368
369Disassembler *
370DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
371{
Greg Claytoncf015052010-06-11 03:25:34 +0000372 char triple[256];
Chris Lattner24943d22010-06-08 16:52:24 +0000373
Greg Claytoncf015052010-06-11 03:25:34 +0000374 if (TripleForArchSpec (arch, triple, sizeof(triple)))
Sean Callanan0a298dc2010-06-16 22:19:05 +0000375 return new DisassemblerLLVM(arch);
Greg Claytoncf015052010-06-11 03:25:34 +0000376 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000377}
378
379DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
380 Disassembler(arch)
381{
Greg Claytoncf015052010-06-11 03:25:34 +0000382 char triple[256];
383 if (TripleForArchSpec (arch, triple, sizeof(triple)))
384 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000385 assert(!EDGetDisassembler(&m_disassembler, triple, SyntaxForArchSpec (arch)) && "No disassembler created!");
Greg Claytoncf015052010-06-11 03:25:34 +0000386 }
Chris Lattner24943d22010-06-08 16:52:24 +0000387}
388
389DisassemblerLLVM::~DisassemblerLLVM()
390{
391}
392
393size_t
Greg Clayton70436352010-06-30 23:03:03 +0000394DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000395(
Greg Clayton5c4c7462010-10-06 03:09:58 +0000396 const Address &base_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000397 const DataExtractor& data,
398 uint32_t data_offset,
Greg Clayton70436352010-06-30 23:03:03 +0000399 uint32_t num_instructions
Chris Lattner24943d22010-06-08 16:52:24 +0000400)
401{
402 size_t total_inst_byte_size = 0;
403
404 m_instruction_list.Clear();
405
406 while (data.ValidOffset(data_offset) && num_instructions)
407 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000408 Address inst_addr (base_addr);
409 inst_addr.Slide(data_offset);
410 InstructionSP inst_sp (new InstructionLLVM(m_disassembler, inst_addr));
Chris Lattner24943d22010-06-08 16:52:24 +0000411
Greg Clayton5c4c7462010-10-06 03:09:58 +0000412 size_t inst_byte_size = inst_sp->Extract (data, data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000413
414 if (inst_byte_size == 0)
415 break;
416
Greg Clayton5c4c7462010-10-06 03:09:58 +0000417 m_instruction_list.Append (inst_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000418
419 total_inst_byte_size += inst_byte_size;
420 data_offset += inst_byte_size;
421 num_instructions--;
422 }
423
424 return total_inst_byte_size;
425}
426
427void
428DisassemblerLLVM::Initialize()
429{
430 PluginManager::RegisterPlugin (GetPluginNameStatic(),
431 GetPluginDescriptionStatic(),
432 CreateInstance);
433}
434
435void
436DisassemblerLLVM::Terminate()
437{
438 PluginManager::UnregisterPlugin (CreateInstance);
439}
440
441
442const char *
443DisassemblerLLVM::GetPluginNameStatic()
444{
445 return "disassembler.llvm";
446}
447
448const char *
449DisassemblerLLVM::GetPluginDescriptionStatic()
450{
451 return "Disassembler that uses LLVM opcode tables to disassemble i386 and x86_64.";
452}
453
454//------------------------------------------------------------------
455// PluginInterface protocol
456//------------------------------------------------------------------
457const char *
458DisassemblerLLVM::GetPluginName()
459{
460 return "DisassemblerLLVM";
461}
462
463const char *
464DisassemblerLLVM::GetShortPluginName()
465{
466 return GetPluginNameStatic();
467}
468
469uint32_t
470DisassemblerLLVM::GetPluginVersion()
471{
472 return 1;
473}
474
475void
476DisassemblerLLVM::GetPluginCommandHelp (const char *command, Stream *strm)
477{
478}
479
480Error
481DisassemblerLLVM::ExecutePluginCommand (Args &command, Stream *strm)
482{
483 Error error;
484 error.SetErrorString("No plug-in command are currently supported.");
485 return error;
486}
487
488Log *
489DisassemblerLLVM::EnablePluginLogging (Stream *strm, Args &command)
490{
491 return NULL;
492}
493