1 //===-- Disassembler.h ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Disassembler_h_
11 #define liblldb_Disassembler_h_
13 #include "lldb/Core/Address.h"
14 #include "lldb/Core/EmulateInstruction.h"
15 #include "lldb/Core/FormatEntity.h" // for FormatEntity
16 #include "lldb/Core/Opcode.h"
17 #include "lldb/Core/PluginInterface.h"
18 #include "lldb/Interpreter/OptionValue.h"
19 #include "lldb/Symbol/LineEntry.h"
20 #include "lldb/Target/ExecutionContext.h" // for ExecutionContext
21 #include "lldb/Utility/ArchSpec.h"
22 #include "lldb/Utility/ConstString.h" // for ConstString
23 #include "lldb/Utility/FileSpec.h"
24 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
25 #include "lldb/lldb-forward.h" // for InstructionSP, DisassemblerSP
26 #include "lldb/lldb-private-enumerations.h" // for AddressClass
27 #include "lldb/lldb-types.h" // for addr_t, offset_t
29 #include "llvm/ADT/StringRef.h" // for StringRef
31 #include <functional> // for function
33 #include <memory> // for enable_shared_from_this
38 #include <stddef.h> // for size_t
39 #include <stdint.h> // for uint32_t, int64_t
40 #include <stdio.h> // for FILE
42 namespace lldb_private {
45 namespace lldb_private {
48 namespace lldb_private {
51 namespace lldb_private {
54 namespace lldb_private {
57 namespace lldb_private {
60 namespace lldb_private {
63 namespace lldb_private {
64 class SymbolContextList;
66 namespace lldb_private {
69 namespace lldb_private {
73 template <typename T> class SmallVectorImpl;
76 namespace lldb_private {
80 Instruction(const Address &address,
81 AddressClass addr_class = AddressClass::eInvalid);
83 virtual ~Instruction();
85 const Address &GetAddress() const { return m_address; }
87 const char *GetMnemonic(const ExecutionContext *exe_ctx) {
88 CalculateMnemonicOperandsAndCommentIfNeeded(exe_ctx);
89 return m_opcode_name.c_str();
92 const char *GetOperands(const ExecutionContext *exe_ctx) {
93 CalculateMnemonicOperandsAndCommentIfNeeded(exe_ctx);
94 return m_mnemonics.c_str();
97 const char *GetComment(const ExecutionContext *exe_ctx) {
98 CalculateMnemonicOperandsAndCommentIfNeeded(exe_ctx);
99 return m_comment.c_str();
103 CalculateMnemonicOperandsAndComment(const ExecutionContext *exe_ctx) = 0;
105 AddressClass GetAddressClass();
107 void SetAddress(const Address &addr) {
108 // Invalidate the address class to lazily discover it if we need to.
109 m_address_class = AddressClass::eInvalid;
113 //------------------------------------------------------------------
114 /// Dump the text representation of this Instruction to a Stream
116 /// Print the (optional) address, (optional) bytes, opcode,
117 /// operands, and instruction comments to a stream.
120 /// The Stream to add the text to.
122 /// @param[in] show_address
123 /// Whether the address (using disassembly_addr_format_spec formatting)
124 /// should be printed.
126 /// @param[in] show_bytes
127 /// Whether the bytes of the assembly instruction should be printed.
129 /// @param[in] max_opcode_byte_size
130 /// The size (in bytes) of the largest instruction in the list that
131 /// we are printing (for text justification/alignment purposes)
132 /// Only needed if show_bytes is true.
134 /// @param[in] exe_ctx
135 /// The current execution context, if available. May be used in
136 /// the assembling of the operands+comments for this instruction.
137 /// Pass NULL if not applicable.
139 /// @param[in] sym_ctx
140 /// The SymbolContext for this instruction.
141 /// Pass NULL if not available/computed.
142 /// Only needed if show_address is true.
144 /// @param[in] prev_sym_ctx
145 /// The SymbolContext for the previous instruction. Depending on
146 /// the disassembly address format specification, a change in
147 /// Symbol / Function may mean that a line is printed with the new
148 /// symbol/function name.
149 /// Pass NULL if unavailable, or if this is the first instruction of
150 /// the InstructionList.
151 /// Only needed if show_address is true.
153 /// @param[in] disassembly_addr_format
154 /// The format specification for how addresses are printed.
155 /// Only needed if show_address is true.
157 /// @param[in] max_address_text_size
158 /// The length of the longest address string at the start of the
159 /// disassembly line that will be printed (the
160 /// Debugger::FormatDisassemblerAddress() string)
161 /// so this method can properly align the instruction opcodes.
162 /// May be 0 to indicate no indentation/alignment of the opcodes.
163 //------------------------------------------------------------------
164 virtual void Dump(Stream *s, uint32_t max_opcode_byte_size, bool show_address,
165 bool show_bytes, const ExecutionContext *exe_ctx,
166 const SymbolContext *sym_ctx,
167 const SymbolContext *prev_sym_ctx,
168 const FormatEntity::Entry *disassembly_addr_format,
169 size_t max_address_text_size);
171 virtual bool DoesBranch() = 0;
173 virtual bool HasDelaySlot();
175 bool CanSetBreakpoint ();
177 virtual size_t Decode(const Disassembler &disassembler,
178 const DataExtractor &data,
179 lldb::offset_t data_offset) = 0;
181 virtual void SetDescription(llvm::StringRef) {
182 } // May be overridden in sub-classes that have descriptions.
184 lldb::OptionValueSP ReadArray(FILE *in_file, Stream *out_stream,
185 OptionValue::Type data_type);
187 lldb::OptionValueSP ReadDictionary(FILE *in_file, Stream *out_stream);
189 bool DumpEmulation(const ArchSpec &arch);
191 virtual bool TestEmulation(Stream *stream, const char *test_file_name);
193 bool Emulate(const ArchSpec &arch, uint32_t evaluate_options, void *baton,
194 EmulateInstruction::ReadMemoryCallback read_mem_callback,
195 EmulateInstruction::WriteMemoryCallback write_mem_calback,
196 EmulateInstruction::ReadRegisterCallback read_reg_callback,
197 EmulateInstruction::WriteRegisterCallback write_reg_callback);
199 const Opcode &GetOpcode() const { return m_opcode; }
201 uint32_t GetData(DataExtractor &data);
211 } m_type = Type::Invalid;
212 std::vector<Operand> m_children;
213 lldb::addr_t m_immediate = 0;
214 ConstString m_register;
215 bool m_negative = false;
216 bool m_clobbered = false;
218 bool IsValid() { return m_type != Type::Invalid; }
220 static Operand BuildRegister(ConstString &r);
221 static Operand BuildImmediate(lldb::addr_t imm, bool neg);
222 static Operand BuildImmediate(int64_t imm);
223 static Operand BuildDereference(const Operand &ref);
224 static Operand BuildSum(const Operand &lhs, const Operand &rhs);
225 static Operand BuildProduct(const Operand &lhs, const Operand &rhs);
228 virtual bool ParseOperands(llvm::SmallVectorImpl<Operand> &operands) {
232 virtual bool IsCall() { return false; }
235 Address m_address; // The section offset address of this instruction
236 // We include an address class in the Instruction class to
237 // allow the instruction specify the
238 // AddressClass::eCodeAlternateISA (currently used for
239 // thumb), and also to specify data (AddressClass::eData).
240 // The usual value will be AddressClass::eCode, but often
241 // when disassembling memory, you might run into data.
242 // This can help us to disassemble appropriately.
244 AddressClass m_address_class; // Use GetAddressClass () accessor function!
247 Opcode m_opcode; // The opcode for this instruction
248 std::string m_opcode_name;
249 std::string m_mnemonics;
250 std::string m_comment;
251 bool m_calculated_strings;
254 CalculateMnemonicOperandsAndCommentIfNeeded(const ExecutionContext *exe_ctx) {
255 if (!m_calculated_strings) {
256 m_calculated_strings = true;
257 CalculateMnemonicOperandsAndComment(exe_ctx);
262 namespace OperandMatchers {
263 std::function<bool(const Instruction::Operand &)>
264 MatchBinaryOp(std::function<bool(const Instruction::Operand &)> base,
265 std::function<bool(const Instruction::Operand &)> left,
266 std::function<bool(const Instruction::Operand &)> right);
268 std::function<bool(const Instruction::Operand &)>
269 MatchUnaryOp(std::function<bool(const Instruction::Operand &)> base,
270 std::function<bool(const Instruction::Operand &)> child);
272 std::function<bool(const Instruction::Operand &)>
273 MatchRegOp(const RegisterInfo &info);
275 std::function<bool(const Instruction::Operand &)> FetchRegOp(ConstString ®);
277 std::function<bool(const Instruction::Operand &)> MatchImmOp(int64_t imm);
279 std::function<bool(const Instruction::Operand &)> FetchImmOp(int64_t &imm);
281 std::function<bool(const Instruction::Operand &)>
282 MatchOpType(Instruction::Operand::Type type);
285 class InstructionList {
290 size_t GetSize() const;
292 uint32_t GetMaxOpcocdeByteSize() const;
294 lldb::InstructionSP GetInstructionAtIndex(size_t idx) const;
296 uint32_t GetIndexOfNextBranchInstruction(uint32_t start,
297 Target &target) const;
299 uint32_t GetIndexOfInstructionAtLoadAddress(lldb::addr_t load_addr,
302 uint32_t GetIndexOfInstructionAtAddress(const Address &addr);
306 void Append(lldb::InstructionSP &inst_sp);
308 void Dump(Stream *s, bool show_address, bool show_bytes,
309 const ExecutionContext *exe_ctx);
312 typedef std::vector<lldb::InstructionSP> collection;
313 typedef collection::iterator iterator;
314 typedef collection::const_iterator const_iterator;
316 collection m_instructions;
319 class PseudoInstruction : public Instruction {
323 ~PseudoInstruction() override;
325 bool DoesBranch() override;
327 bool HasDelaySlot() override;
329 void CalculateMnemonicOperandsAndComment(
330 const ExecutionContext *exe_ctx) override {
331 // TODO: fill this in and put opcode name into Instruction::m_opcode_name,
332 // mnemonic into Instruction::m_mnemonics, and any comment into
333 // Instruction::m_comment
336 size_t Decode(const Disassembler &disassembler, const DataExtractor &data,
337 lldb::offset_t data_offset) override;
339 void SetOpcode(size_t opcode_size, void *opcode_data);
341 void SetDescription(llvm::StringRef description) override;
344 std::string m_description;
346 DISALLOW_COPY_AND_ASSIGN(PseudoInstruction);
349 class Disassembler : public std::enable_shared_from_this<Disassembler>,
350 public PluginInterface {
354 eOptionShowBytes = (1u << 0),
355 eOptionRawOuput = (1u << 1),
356 eOptionMarkPCSourceLine = (1u << 2), // Mark the source line that contains
357 // the current PC (mixed mode only)
358 eOptionMarkPCAddress =
359 (1u << 3) // Mark the disassembly line the contains the PC
362 enum HexImmediateStyle {
367 // FindPlugin should be lax about the flavor string (it is too annoying to
368 // have various internal uses of the disassembler fail because the global
369 // flavor string gets set wrong. Instead, if you get a flavor string you
370 // don't understand, use the default. Folks who care to check can use the
371 // FlavorValidForArchSpec method on the disassembler they got back.
372 static lldb::DisassemblerSP
373 FindPlugin(const ArchSpec &arch, const char *flavor, const char *plugin_name);
375 // This version will use the value in the Target settings if flavor is NULL;
376 static lldb::DisassemblerSP
377 FindPluginForTarget(const lldb::TargetSP target_sp, const ArchSpec &arch,
378 const char *flavor, const char *plugin_name);
380 static lldb::DisassemblerSP
381 DisassembleRange(const ArchSpec &arch, const char *plugin_name,
382 const char *flavor, const ExecutionContext &exe_ctx,
383 const AddressRange &disasm_range, bool prefer_file_cache);
385 static lldb::DisassemblerSP
386 DisassembleBytes(const ArchSpec &arch, const char *plugin_name,
387 const char *flavor, const Address &start, const void *bytes,
388 size_t length, uint32_t max_num_instructions,
389 bool data_from_file);
391 static bool Disassemble(Debugger &debugger, const ArchSpec &arch,
392 const char *plugin_name, const char *flavor,
393 const ExecutionContext &exe_ctx,
394 const AddressRange &range, uint32_t num_instructions,
395 bool mixed_source_and_assembly,
396 uint32_t num_mixed_context_lines, uint32_t options,
399 static bool Disassemble(Debugger &debugger, const ArchSpec &arch,
400 const char *plugin_name, const char *flavor,
401 const ExecutionContext &exe_ctx, const Address &start,
402 uint32_t num_instructions,
403 bool mixed_source_and_assembly,
404 uint32_t num_mixed_context_lines, uint32_t options,
408 Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
409 const char *flavor, const ExecutionContext &exe_ctx,
410 SymbolContextList &sc_list, uint32_t num_instructions,
411 bool mixed_source_and_assembly, uint32_t num_mixed_context_lines,
412 uint32_t options, Stream &strm);
415 Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
416 const char *flavor, const ExecutionContext &exe_ctx,
417 const ConstString &name, Module *module,
418 uint32_t num_instructions, bool mixed_source_and_assembly,
419 uint32_t num_mixed_context_lines, uint32_t options, Stream &strm);
422 Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
423 const char *flavor, const ExecutionContext &exe_ctx,
424 uint32_t num_instructions, bool mixed_source_and_assembly,
425 uint32_t num_mixed_context_lines, uint32_t options, Stream &strm);
427 //------------------------------------------------------------------
428 // Constructors and Destructors
429 //------------------------------------------------------------------
430 Disassembler(const ArchSpec &arch, const char *flavor);
431 ~Disassembler() override;
433 typedef const char *(*SummaryCallback)(const Instruction &inst,
434 ExecutionContext *exe_context,
437 static bool PrintInstructions(Disassembler *disasm_ptr, Debugger &debugger,
438 const ArchSpec &arch,
439 const ExecutionContext &exe_ctx,
440 uint32_t num_instructions,
441 bool mixed_source_and_assembly,
442 uint32_t num_mixed_context_lines,
443 uint32_t options, Stream &strm);
445 size_t ParseInstructions(const ExecutionContext *exe_ctx,
446 const AddressRange &range, Stream *error_strm_ptr,
447 bool prefer_file_cache);
449 size_t ParseInstructions(const ExecutionContext *exe_ctx,
450 const Address &range, uint32_t num_instructions,
451 bool prefer_file_cache);
453 virtual size_t DecodeInstructions(const Address &base_addr,
454 const DataExtractor &data,
455 lldb::offset_t data_offset,
456 size_t num_instructions, bool append,
457 bool data_from_file) = 0;
459 InstructionList &GetInstructionList();
461 const InstructionList &GetInstructionList() const;
463 const ArchSpec &GetArchitecture() const { return m_arch; }
465 const char *GetFlavor() const { return m_flavor.c_str(); }
467 virtual bool FlavorValidForArchSpec(const lldb_private::ArchSpec &arch,
468 const char *flavor) = 0;
471 // SourceLine and SourceLinesToDisplay structures are only used in the mixed
472 // source and assembly display methods internal to this class.
479 SourceLine() : file(), line(LLDB_INVALID_LINE_NUMBER), column(0) {}
481 bool operator==(const SourceLine &rhs) const {
482 return file == rhs.file && line == rhs.line && rhs.column == column;
485 bool operator!=(const SourceLine &rhs) const {
486 return file != rhs.file || line != rhs.line || column != rhs.column;
489 bool IsValid() const { return line != LLDB_INVALID_LINE_NUMBER; }
492 struct SourceLinesToDisplay {
493 std::vector<SourceLine> lines;
495 // index of the "current" source line, if we want to highlight that when
496 // displaying the source lines. (as opposed to the surrounding source
497 // lines provided to give context)
498 size_t current_source_line;
500 // Whether to print a blank line at the end of the source lines.
501 bool print_source_context_end_eol;
503 SourceLinesToDisplay()
504 : lines(), current_source_line(-1), print_source_context_end_eol(true) {
508 // Get the function's declaration line number, hopefully a line number
509 // earlier than the opening curly brace at the start of the function body.
510 static SourceLine GetFunctionDeclLineEntry(const SymbolContext &sc);
512 // Add the provided SourceLine to the map of filenames-to-source-lines-seen.
513 static void AddLineToSourceLineTables(
515 std::map<FileSpec, std::set<uint32_t>> &source_lines_seen);
517 // Given a source line, determine if we should print it when we're doing
518 // mixed source & assembly output. We're currently using the
519 // target.process.thread.step-avoid-regexp setting (which is used for
520 // stepping over inlined STL functions by default) to determine what source
521 // lines to avoid showing.
523 // Returns true if this source line should be elided (if the source line
524 // should not be displayed).
526 ElideMixedSourceAndDisassemblyLine(const ExecutionContext &exe_ctx,
527 const SymbolContext &sc, SourceLine &line);
530 ElideMixedSourceAndDisassemblyLine(const ExecutionContext &exe_ctx,
531 const SymbolContext &sc, LineEntry &line) {
535 sl.column = line.column;
536 return ElideMixedSourceAndDisassemblyLine(exe_ctx, sc, sl);
539 //------------------------------------------------------------------
540 // Classes that inherit from Disassembler can see and modify these
541 //------------------------------------------------------------------
543 InstructionList m_instruction_list;
544 lldb::addr_t m_base_addr;
545 std::string m_flavor;
548 //------------------------------------------------------------------
549 // For Disassembler only
550 //------------------------------------------------------------------
551 DISALLOW_COPY_AND_ASSIGN(Disassembler);
554 } // namespace lldb_private
556 #endif // liblldb_Disassembler_h_