blob: c831d95aab239ead9d7bf2608594ad2e7a83eeef [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
78DisassemblerLLVM::Instruction::Instruction(EDDisassemblerRef disassembler) :
79 Disassembler::Instruction (),
80 m_disassembler (disassembler)
81{
82}
83
84DisassemblerLLVM::Instruction::~Instruction()
85{
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
100DisassemblerLLVM::Instruction::Dump
101(
102 Stream *s,
Greg Clayton70436352010-06-30 23:03:03 +0000103 lldb_private::Address *inst_addr_ptr,
104 const DataExtractor *bytes,
Chris Lattner24943d22010-06-08 16:52:24 +0000105 uint32_t bytes_offset,
Greg Clayton70436352010-06-30 23:03:03 +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 Clayton70436352010-06-30 23:03:03 +0000113 ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
Chris Lattner24943d22010-06-08 16:52:24 +0000114 // If we have an address, print it out
Greg Clayton70436352010-06-30 23:03:03 +0000115 if (inst_addr_ptr)
116 {
117 if (inst_addr_ptr->Dump (s,
118 exe_scope,
119 Address::DumpStyleLoadAddress,
120 Address::DumpStyleModuleWithFileAddress,
121 0))
122 s->PutCString(": ");
123 }
Chris Lattner24943d22010-06-08 16:52:24 +0000124
125 // If we are supposed to show bytes, "bytes" will be non-NULL.
126 if (bytes)
127 {
128 uint32_t bytes_dumped = bytes->Dump(s, bytes_offset, eFormatBytes, 1, EDInstByteSize(m_inst), UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0) - bytes_offset;
129 // Allow for 8 bytes of opcodes normally
130 const uint32_t default_num_opcode_bytes = 9;
131 if (bytes_dumped * 3 < (default_num_opcode_bytes*3))
132 {
133 uint32_t indent_level = (default_num_opcode_bytes*3) - (bytes_dumped * 3);
134 s->Printf("%*.*s", indent_level, indent_level, "");
135 }
136 }
137
138 int numTokens = EDNumTokens(m_inst);
139
140 int currentOpIndex = -1;
141
Chris Lattner24943d22010-06-08 16:52:24 +0000142 lldb_private::Process *process = exe_ctx.process;
Greg Clayton70436352010-06-30 23:03:03 +0000143 addr_t base_addr = LLDB_INVALID_ADDRESS;
144 if (process && process->IsAlive())
145 base_addr = inst_addr_ptr->GetLoadAddress (process);
146 if (base_addr == LLDB_INVALID_ADDRESS)
147 base_addr = inst_addr_ptr->GetFileAddress ();
148
149 RegisterReaderArg rra(base_addr + EDInstByteSize(m_inst), m_disassembler);
150
Chris Lattner24943d22010-06-08 16:52:24 +0000151
152 bool printTokenized = false;
153
154 if (numTokens != -1)
155 {
156 printTokenized = true;
157
158 // Handle the opcode column.
159
160 StreamString opcode;
161
162 int tokenIndex = 0;
163
164 EDTokenRef token;
165 const char *tokenStr;
166
167 if (EDGetToken(&token, m_inst, tokenIndex))
168 printTokenized = false;
169
170 if (!printTokenized || !EDTokenIsOpcode(token))
171 printTokenized = false;
172
173 if (!printTokenized || EDGetTokenString(&tokenStr, token))
174 printTokenized = false;
175
176 // Put the token string into our opcode string
177 opcode.PutCString(tokenStr);
178
179 // If anything follows, it probably starts with some whitespace. Skip it.
180
181 tokenIndex++;
182
183 if (printTokenized && tokenIndex < numTokens)
184 {
185 if(!printTokenized || EDGetToken(&token, m_inst, tokenIndex))
186 printTokenized = false;
187
188 if(!printTokenized || !EDTokenIsWhitespace(token))
189 printTokenized = false;
190 }
191
192 tokenIndex++;
193
194 // Handle the operands and the comment.
195
196 StreamString operands;
197 StreamString comment;
198
199 if (printTokenized)
200 {
201 bool show_token;
202
203 for (; tokenIndex < numTokens; ++tokenIndex)
204 {
205 if (EDGetToken(&token, m_inst, tokenIndex))
206 return;
207
208 if (raw)
209 {
210 show_token = true;
211 }
212 else
213 {
214 int operandIndex = EDOperandIndexForToken(token);
215
216 if (operandIndex >= 0)
217 {
218 if (operandIndex != currentOpIndex)
219 {
220 show_token = true;
221
222 currentOpIndex = operandIndex;
223 EDOperandRef operand;
224
225 if (!EDGetOperand(&operand, m_inst, currentOpIndex))
226 {
227 if (EDOperandIsMemory(operand))
228 {
229 uint64_t operand_value;
230
231 if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, &rra))
232 {
233 if (EDInstIsBranch(m_inst))
234 {
235 operands.Printf("0x%llx ", operand_value);
236 show_token = false;
237 }
238 else
239 {
240 // Put the address value into the comment
241 comment.Printf("0x%llx ", operand_value);
242 }
243
244 lldb_private::Address so_addr;
Greg Clayton70436352010-06-30 23:03:03 +0000245 if (process && process->IsAlive())
Chris Lattner24943d22010-06-08 16:52:24 +0000246 {
Greg Clayton70436352010-06-30 23:03:03 +0000247 if (process->ResolveLoadAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000248 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Greg Clayton70436352010-06-30 23:03:03 +0000249 }
250 else if (inst_addr_ptr)
251 {
252 Module *module = inst_addr_ptr->GetModule();
253 if (module)
Chris Lattner24943d22010-06-08 16:52:24 +0000254 {
Greg Clayton70436352010-06-30 23:03:03 +0000255 if (module->ResolveFileAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000256 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Chris Lattner24943d22010-06-08 16:52:24 +0000257 }
258 }
Greg Clayton70436352010-06-30 23:03:03 +0000259
Chris Lattner24943d22010-06-08 16:52:24 +0000260 } // EDEvaluateOperand
261 } // EDOperandIsMemory
262 } // EDGetOperand
263 } // operandIndex != currentOpIndex
264 } // operandIndex >= 0
265 } // else(raw)
266
267 if (show_token)
268 {
269 if(EDGetTokenString(&tokenStr, token))
270 {
271 printTokenized = false;
272 break;
273 }
274
275 operands.PutCString(tokenStr);
276 }
277 } // for (tokenIndex)
278
279 if (printTokenized)
280 {
281 if (operands.GetString().empty())
282 {
283 s->PutCString(opcode.GetString().c_str());
284 }
285 else
286 {
287 PadString(s, opcode.GetString(), opcodeColumnWidth);
288
289 if (comment.GetString().empty())
290 {
291 s->PutCString(operands.GetString().c_str());
292 }
293 else
294 {
295 PadString(s, operands.GetString(), operandColumnWidth);
296
297 s->PutCString("; ");
298 s->PutCString(comment.GetString().c_str());
299 } // else (comment.GetString().empty())
300 } // else (operands.GetString().empty())
301 } // printTokenized
302 } // for (tokenIndex)
303 } // numTokens != -1
304
305 if (!printTokenized)
306 {
307 const char *str;
308
309 if (EDGetInstString(&str, m_inst))
310 return;
311 else
312 s->PutCString(str);
313 }
314}
315
316bool
317DisassemblerLLVM::Instruction::DoesBranch() const
318{
319 return EDInstIsBranch(m_inst);
320}
321
322size_t
323DisassemblerLLVM::Instruction::GetByteSize() const
324{
325 return EDInstByteSize(m_inst);
326}
327
328size_t
329DisassemblerLLVM::Instruction::Extract(const DataExtractor &data, uint32_t data_offset)
330{
331 if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data)))
332 return EDInstByteSize(m_inst);
333 else
334 return 0;
335}
336
337static inline const char *
Greg Claytoncf015052010-06-11 03:25:34 +0000338TripleForArchSpec (const ArchSpec &arch, char *triple, size_t triple_len)
Chris Lattner24943d22010-06-08 16:52:24 +0000339{
Greg Claytoncf015052010-06-11 03:25:34 +0000340 const char *arch_name = arch.AsCString();
341
342 if (arch_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000343 {
Greg Claytoncf015052010-06-11 03:25:34 +0000344 snprintf(triple, triple_len, "%s-unknown-unknown", arch_name);
345 return triple;
Chris Lattner24943d22010-06-08 16:52:24 +0000346 }
Greg Claytoncf015052010-06-11 03:25:34 +0000347 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000348}
349
350static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000351SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000352{
Greg Claytoncf015052010-06-11 03:25:34 +0000353 const char *arch_name = arch.AsCString();
354
355 if (arch_name != NULL &&
356 ((strcasestr (arch_name, "i386") == arch_name) ||
357 (strcasestr (arch_name, "x86_64") == arch_name)))
Chris Lattner24943d22010-06-08 16:52:24 +0000358 return kEDAssemblySyntaxX86ATT;
Greg Claytoncf015052010-06-11 03:25:34 +0000359
360 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000361}
362
363Disassembler *
364DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
365{
Greg Claytoncf015052010-06-11 03:25:34 +0000366 char triple[256];
Chris Lattner24943d22010-06-08 16:52:24 +0000367
Greg Claytoncf015052010-06-11 03:25:34 +0000368 if (TripleForArchSpec (arch, triple, sizeof(triple)))
Sean Callanan0a298dc2010-06-16 22:19:05 +0000369 return new DisassemblerLLVM(arch);
Greg Claytoncf015052010-06-11 03:25:34 +0000370 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000371}
372
373DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
374 Disassembler(arch)
375{
Greg Claytoncf015052010-06-11 03:25:34 +0000376 char triple[256];
377 if (TripleForArchSpec (arch, triple, sizeof(triple)))
378 {
379 EDAssemblySyntax_t syntax = SyntaxForArchSpec (arch);
380 assert(!EDGetDisassembler(&m_disassembler, triple, syntax) && "No disassembler created!");
381 }
Chris Lattner24943d22010-06-08 16:52:24 +0000382}
383
384DisassemblerLLVM::~DisassemblerLLVM()
385{
386}
387
388size_t
Greg Clayton70436352010-06-30 23:03:03 +0000389DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000390(
391 const DataExtractor& data,
392 uint32_t data_offset,
Greg Clayton70436352010-06-30 23:03:03 +0000393 uint32_t num_instructions
Chris Lattner24943d22010-06-08 16:52:24 +0000394)
395{
396 size_t total_inst_byte_size = 0;
397
398 m_instruction_list.Clear();
399
400 while (data.ValidOffset(data_offset) && num_instructions)
401 {
402 Instruction::shared_ptr inst_sp (new Instruction(m_disassembler));
403
404 size_t inst_byte_size = inst_sp->Extract(data, data_offset);
405
406 if (inst_byte_size == 0)
407 break;
408
409 m_instruction_list.AppendInstruction(inst_sp);
410
411 total_inst_byte_size += inst_byte_size;
412 data_offset += inst_byte_size;
413 num_instructions--;
414 }
415
416 return total_inst_byte_size;
417}
418
419void
420DisassemblerLLVM::Initialize()
421{
422 PluginManager::RegisterPlugin (GetPluginNameStatic(),
423 GetPluginDescriptionStatic(),
424 CreateInstance);
425}
426
427void
428DisassemblerLLVM::Terminate()
429{
430 PluginManager::UnregisterPlugin (CreateInstance);
431}
432
433
434const char *
435DisassemblerLLVM::GetPluginNameStatic()
436{
437 return "disassembler.llvm";
438}
439
440const char *
441DisassemblerLLVM::GetPluginDescriptionStatic()
442{
443 return "Disassembler that uses LLVM opcode tables to disassemble i386 and x86_64.";
444}
445
446//------------------------------------------------------------------
447// PluginInterface protocol
448//------------------------------------------------------------------
449const char *
450DisassemblerLLVM::GetPluginName()
451{
452 return "DisassemblerLLVM";
453}
454
455const char *
456DisassemblerLLVM::GetShortPluginName()
457{
458 return GetPluginNameStatic();
459}
460
461uint32_t
462DisassemblerLLVM::GetPluginVersion()
463{
464 return 1;
465}
466
467void
468DisassemblerLLVM::GetPluginCommandHelp (const char *command, Stream *strm)
469{
470}
471
472Error
473DisassemblerLLVM::ExecutePluginCommand (Args &command, Stream *strm)
474{
475 Error error;
476 error.SetErrorString("No plug-in command are currently supported.");
477 return error;
478}
479
480Log *
481DisassemblerLLVM::EnablePluginLogging (Stream *strm, Args &command)
482{
483 return NULL;
484}
485