LLDB mainline
DisassemblerLLVMC.cpp
Go to the documentation of this file.
1//===-- DisassemblerLLVMC.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DisassemblerLLVMC.h"
10
11#include "llvm-c/Disassembler.h"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCDisassembler/MCDisassembler.h"
17#include "llvm/MC/MCDisassembler/MCExternalSymbolizer.h"
18#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCInstPrinter.h"
21#include "llvm/MC/MCInstrAnalysis.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/MC/MCTargetOptions.h"
26#include "llvm/MC/TargetRegistry.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/ScopedPrinter.h"
29#include "llvm/Support/TargetSelect.h"
30#include "llvm/TargetParser/AArch64TargetParser.h"
31
32#include "lldb/Core/Address.h"
33#include "lldb/Core/Module.h"
37#include "lldb/Target/Process.h"
41#include "lldb/Target/Target.h"
44#include "lldb/Utility/Log.h"
46#include "lldb/Utility/Stream.h"
47#include <optional>
48
49using namespace lldb;
50using namespace lldb_private;
51
53
55public:
56 static std::unique_ptr<MCDisasmInstance>
57 Create(const char *triple, const char *cpu, const char *features_str,
58 unsigned flavor, DisassemblerLLVMC &owner);
59
60 ~MCDisasmInstance() = default;
61
62 bool GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len,
63 lldb::addr_t pc, llvm::MCInst &mc_inst, uint64_t &size) const;
64 void PrintMCInst(llvm::MCInst &mc_inst, lldb::addr_t pc,
65 std::string &inst_string, std::string &comments_string);
66 void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style);
67 void SetUseColor(bool use_color);
68 bool GetUseColor() const;
69 bool CanBranch(llvm::MCInst &mc_inst) const;
70 bool HasDelaySlot(llvm::MCInst &mc_inst) const;
71 bool IsCall(llvm::MCInst &mc_inst) const;
72 bool IsLoad(llvm::MCInst &mc_inst) const;
73 bool IsBarrier(llvm::MCInst &mc_inst) const;
74 bool IsAuthenticated(llvm::MCInst &mc_inst) const;
75
76private:
77 MCDisasmInstance(std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up,
78 std::unique_ptr<llvm::MCRegisterInfo> &&reg_info_up,
79 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
80 llvm::MCTargetOptions mc_options,
81 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
82 std::unique_ptr<llvm::MCContext> &&context_up,
83 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
84 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up,
85 std::unique_ptr<llvm::MCInstrAnalysis> &&instr_analysis_up);
86
87 std::unique_ptr<llvm::MCInstrInfo> m_instr_info_up;
88 std::unique_ptr<llvm::MCRegisterInfo> m_reg_info_up;
89 std::unique_ptr<llvm::MCSubtargetInfo> m_subtarget_info_up;
90 llvm::MCTargetOptions m_mc_options;
91 std::unique_ptr<llvm::MCAsmInfo> m_asm_info_up;
92 std::unique_ptr<llvm::MCContext> m_context_up;
93 std::unique_ptr<llvm::MCDisassembler> m_disasm_up;
94 std::unique_ptr<llvm::MCInstPrinter> m_instr_printer_up;
95 std::unique_ptr<llvm::MCInstrAnalysis> m_instr_analysis_up;
96};
97
98namespace x86 {
99
100/// These are the three values deciding instruction control flow kind.
101/// InstructionLengthDecode function decodes an instruction and get this struct.
102///
103/// primary_opcode
104/// Primary opcode of the instruction.
105/// For one-byte opcode instruction, it's the first byte after prefix.
106/// For two- and three-byte opcodes, it's the second byte.
107///
108/// opcode_len
109/// The length of opcode in bytes. Valid opcode lengths are 1, 2, or 3.
110///
111/// modrm
112/// ModR/M byte of the instruction.
113/// Bits[7:6] indicate MOD. Bits[5:3] specify a register and R/M bits[2:0]
114/// may contain a register or specify an addressing mode, depending on MOD.
120
121/// Determine the InstructionControlFlowKind based on opcode and modrm bytes.
122/// Refer to http://ref.x86asm.net/coder.html for the full list of opcode and
123/// instruction set.
124///
125/// \param[in] opcode_and_modrm
126/// Contains primary_opcode byte, its length, and ModR/M byte.
127/// Refer to the struct InstructionOpcodeAndModrm for details.
128///
129/// \return
130/// The control flow kind of the instruction or
131/// eInstructionControlFlowKindOther if the instruction doesn't affect
132/// the control flow of the program.
135 uint8_t opcode = opcode_and_modrm.primary_opcode;
136 uint8_t opcode_len = opcode_and_modrm.opcode_len;
137 uint8_t modrm = opcode_and_modrm.modrm;
138
139 if (opcode_len > 2)
141
142 if (opcode >= 0x70 && opcode <= 0x7F) {
143 if (opcode_len == 1)
145 else
147 }
148
149 if (opcode >= 0x80 && opcode <= 0x8F) {
150 if (opcode_len == 2)
152 else
154 }
155
156 switch (opcode) {
157 case 0x9A:
158 if (opcode_len == 1)
160 break;
161 case 0xFF:
162 if (opcode_len == 1) {
163 uint8_t modrm_reg = (modrm >> 3) & 7;
164 if (modrm_reg == 2)
166 else if (modrm_reg == 3)
168 else if (modrm_reg == 4)
170 else if (modrm_reg == 5)
172 }
173 break;
174 case 0xE8:
175 if (opcode_len == 1)
177 break;
178 case 0xCD:
179 case 0xCC:
180 case 0xCE:
181 case 0xF1:
182 if (opcode_len == 1)
184 break;
185 case 0xCF:
186 if (opcode_len == 1)
188 break;
189 case 0xE9:
190 case 0xEB:
191 if (opcode_len == 1)
193 break;
194 case 0xEA:
195 if (opcode_len == 1)
197 break;
198 case 0xE3:
199 case 0xE0:
200 case 0xE1:
201 case 0xE2:
202 if (opcode_len == 1)
204 break;
205 case 0xC3:
206 case 0xC2:
207 if (opcode_len == 1)
209 break;
210 case 0xCB:
211 case 0xCA:
212 if (opcode_len == 1)
214 break;
215 case 0x05:
216 case 0x34:
217 if (opcode_len == 2)
219 break;
220 case 0x35:
221 case 0x07:
222 if (opcode_len == 2)
224 break;
225 case 0x01:
226 if (opcode_len == 2) {
227 switch (modrm) {
228 case 0xc1:
230 case 0xc2:
231 case 0xc3:
233 default:
234 break;
235 }
236 }
237 break;
238 default:
239 break;
240 }
241
243}
244
245/// Decode an instruction into opcode, modrm and opcode_len.
246/// Refer to http://ref.x86asm.net/coder.html for the instruction bytes layout.
247/// Opcodes in x86 are generally the first byte of instruction, though two-byte
248/// instructions and prefixes exist. ModR/M is the byte following the opcode
249/// and adds additional information for how the instruction is executed.
250///
251/// \param[in] inst_bytes
252/// Raw bytes of the instruction
253///
254///
255/// \param[in] bytes_len
256/// The length of the inst_bytes array.
257///
258/// \param[in] is_exec_mode_64b
259/// If true, the execution mode is 64 bit.
260///
261/// \return
262/// Returns decoded instruction as struct InstructionOpcodeAndModrm, holding
263/// primary_opcode, opcode_len and modrm byte. Refer to the struct definition
264/// for more details.
265/// Otherwise if the given instruction is invalid, returns std::nullopt.
266std::optional<InstructionOpcodeAndModrm>
267InstructionLengthDecode(const uint8_t *inst_bytes, int bytes_len,
268 bool is_exec_mode_64b) {
269 int op_idx = 0;
270 bool prefix_done = false;
271 InstructionOpcodeAndModrm ret = {0, 0, 0};
272
273 // In most cases, the primary_opcode is the first byte of the instruction
274 // but some instructions have a prefix to be skipped for these calculations.
275 // The following mapping is inspired from libipt's instruction decoding logic
276 // in `src/pt_ild.c`
277 while (!prefix_done) {
278 if (op_idx >= bytes_len)
279 return std::nullopt;
280
281 ret.primary_opcode = inst_bytes[op_idx];
282 switch (ret.primary_opcode) {
283 // prefix_ignore
284 case 0x26:
285 case 0x2e:
286 case 0x36:
287 case 0x3e:
288 case 0x64:
289 case 0x65:
290 // prefix_osz, prefix_asz
291 case 0x66:
292 case 0x67:
293 // prefix_lock, prefix_f2, prefix_f3
294 case 0xf0:
295 case 0xf2:
296 case 0xf3:
297 op_idx++;
298 break;
299
300 // prefix_rex
301 case 0x40:
302 case 0x41:
303 case 0x42:
304 case 0x43:
305 case 0x44:
306 case 0x45:
307 case 0x46:
308 case 0x47:
309 case 0x48:
310 case 0x49:
311 case 0x4a:
312 case 0x4b:
313 case 0x4c:
314 case 0x4d:
315 case 0x4e:
316 case 0x4f:
317 if (is_exec_mode_64b)
318 op_idx++;
319 else
320 prefix_done = true;
321 break;
322
323 // prefix_vex_c4, c5
324 case 0xc5:
325 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
326 prefix_done = true;
327 break;
328 }
329
330 ret.opcode_len = 2;
331 ret.primary_opcode = inst_bytes[op_idx + 2];
332 ret.modrm = inst_bytes[op_idx + 3];
333 return ret;
334
335 case 0xc4:
336 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
337 prefix_done = true;
338 break;
339 }
340 ret.opcode_len = inst_bytes[op_idx + 1] & 0x1f;
341 ret.primary_opcode = inst_bytes[op_idx + 3];
342 ret.modrm = inst_bytes[op_idx + 4];
343 return ret;
344
345 // prefix_evex
346 case 0x62:
347 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
348 prefix_done = true;
349 break;
350 }
351 ret.opcode_len = inst_bytes[op_idx + 1] & 0x03;
352 ret.primary_opcode = inst_bytes[op_idx + 4];
353 ret.modrm = inst_bytes[op_idx + 5];
354 return ret;
355
356 default:
357 prefix_done = true;
358 break;
359 }
360 } // prefix done
361
362 ret.primary_opcode = inst_bytes[op_idx];
363 ret.modrm = inst_bytes[op_idx + 1];
364 ret.opcode_len = 1;
365
366 // If the first opcode is 0F, it's two- or three- byte opcodes.
367 if (ret.primary_opcode == 0x0F) {
368 ret.primary_opcode = inst_bytes[++op_idx]; // get the next byte
369
370 if (ret.primary_opcode == 0x38) {
371 ret.opcode_len = 3;
372 ret.primary_opcode = inst_bytes[++op_idx]; // get the next byte
373 ret.modrm = inst_bytes[op_idx + 1];
374 } else if (ret.primary_opcode == 0x3A) {
375 ret.opcode_len = 3;
376 ret.primary_opcode = inst_bytes[++op_idx];
377 ret.modrm = inst_bytes[op_idx + 1];
378 } else if ((ret.primary_opcode & 0xf8) == 0x38) {
379 ret.opcode_len = 0;
380 ret.primary_opcode = inst_bytes[++op_idx];
381 ret.modrm = inst_bytes[op_idx + 1];
382 } else if (ret.primary_opcode == 0x0F) {
383 ret.opcode_len = 3;
384 // opcode is 0x0F, no needs to update
385 ret.modrm = inst_bytes[op_idx + 1];
386 } else {
387 ret.opcode_len = 2;
388 ret.modrm = inst_bytes[op_idx + 1];
389 }
390 }
391
392 return ret;
393}
394
396 Opcode m_opcode) {
397 std::optional<InstructionOpcodeAndModrm> ret;
398
399 if (m_opcode.GetOpcodeBytes() == nullptr || m_opcode.GetByteSize() <= 0) {
400 // x86_64 and i386 instructions are categorized as Opcode::Type::eTypeBytes
402 }
403
404 // Opcode bytes will be decoded into primary_opcode, modrm and opcode length.
405 // These are the three values deciding instruction control flow kind.
406 ret = InstructionLengthDecode((const uint8_t *)m_opcode.GetOpcodeBytes(),
407 m_opcode.GetByteSize(), is_exec_mode_64b);
408 if (!ret)
410 else
411 return MapOpcodeIntoControlFlowKind(*ret);
412}
413
414} // namespace x86
415
417public:
419 const lldb_private::Address &address,
420 AddressClass addr_class)
421 : Instruction(address, addr_class),
422 m_disasm_wp(std::static_pointer_cast<DisassemblerLLVMC>(
423 disasm.shared_from_this())) {}
424
425 ~InstructionLLVMC() override = default;
426
427 bool DoesBranch() override {
429 return m_does_branch;
430 }
431
432 bool HasDelaySlot() override {
434 return m_has_delay_slot;
435 }
436
437 bool IsLoad() override {
439 return m_is_load;
440 }
441
442 bool IsBarrier() override {
444 return m_is_barrier;
445 }
446
447 bool IsAuthenticated() override {
449 return m_is_authenticated;
450 }
451
453 DisassemblerScope disasm(*this);
454 return GetDisasmToUse(is_alternate_isa, disasm);
455 }
456
457 size_t Decode(const lldb_private::Disassembler &disassembler,
458 const lldb_private::DataExtractor &data,
459 lldb::offset_t data_offset) override {
460 // All we have to do is read the opcode which can be easy for some
461 // architectures
462 DisassemblerScope disasm(*this);
463 if (disasm) {
464 const ArchSpec &arch = disasm->GetArchitecture();
465 const lldb::ByteOrder byte_order = data.GetByteOrder();
466
467 const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize();
468 const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize();
469 if (min_op_byte_size == max_op_byte_size) {
470 // Fixed size instructions, just read that amount of data.
471 if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size))
472 return 0;
473
474 switch (min_op_byte_size) {
475 case 1:
476 m_opcode.SetOpcode8(data.GetU8(&data_offset), byte_order);
477 break;
478
479 case 2:
480 m_opcode.SetOpcode16(data.GetU16(&data_offset), byte_order);
481 break;
482
483 case 4:
484 m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order);
485 break;
486
487 case 8:
488 m_opcode.SetOpcode64(data.GetU64(&data_offset), byte_order);
489 break;
490
491 default:
492 m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size),
493 min_op_byte_size);
494 break;
495 }
496 } else {
497 bool is_alternate_isa = false;
499 GetDisasmToUse(is_alternate_isa, disasm);
500
501 const llvm::Triple::ArchType machine = arch.GetMachine();
502 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb) {
503 if (machine == llvm::Triple::thumb || is_alternate_isa) {
504 uint32_t thumb_opcode = data.GetU16(&data_offset);
505 if ((thumb_opcode & 0xe000) != 0xe000 ||
506 ((thumb_opcode & 0x1800u) == 0)) {
507 m_opcode.SetOpcode16(thumb_opcode, byte_order);
508 m_is_valid = true;
509 } else {
510 thumb_opcode <<= 16;
511 thumb_opcode |= data.GetU16(&data_offset);
512 m_opcode.SetOpcode16_2(thumb_opcode, byte_order);
513 m_is_valid = true;
514 }
515 } else {
516 m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order);
517 m_is_valid = true;
518 }
519 } else {
520 // The opcode isn't evenly sized, so we need to actually use the llvm
521 // disassembler to parse it and get the size.
522 uint8_t *opcode_data =
523 const_cast<uint8_t *>(data.PeekData(data_offset, 1));
524 const size_t opcode_data_len = data.BytesLeft(data_offset);
525 const addr_t pc = m_address.GetFileAddress();
526 llvm::MCInst inst;
527
528 uint64_t inst_size = 0;
529 m_is_valid = mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len,
530 pc, inst, inst_size);
531 m_opcode.Clear();
532 if (inst_size != 0) {
533 if (arch.GetTriple().isRISCV())
534 m_opcode.SetOpcode16_32TupleBytes(opcode_data, inst_size,
535 byte_order);
536 else
537 m_opcode.SetOpcodeBytes(opcode_data, inst_size);
538 }
539 }
540 }
541 return m_opcode.GetByteSize();
542 }
543 return 0;
544 }
545
546 void AppendComment(std::string &description) {
547 if (m_comment.empty())
548 m_comment.swap(description);
549 else {
550 m_comment.append(", ");
551 m_comment.append(description);
552 }
553 }
554
557 DisassemblerScope disasm(*this, exe_ctx);
558 if (disasm) {
559 if (disasm->GetArchitecture().GetMachine() == llvm::Triple::x86)
560 return x86::GetControlFlowKind(/*is_64b=*/false, m_opcode);
561 else if (disasm->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
562 return x86::GetControlFlowKind(/*is_64b=*/true, m_opcode);
563 }
564
566 }
567
569 const lldb_private::ExecutionContext *exe_ctx) override {
570 DataExtractor data;
571 const AddressClass address_class = GetAddressClass();
572
573 if (m_opcode.GetData(data)) {
574 std::string out_string;
575 std::string markup_out_string;
576 std::string comment_string;
577 std::string markup_comment_string;
578
579 DisassemblerScope disasm(*this, exe_ctx);
580 if (disasm) {
582
583 if (address_class == AddressClass::eCodeAlternateISA)
584 mc_disasm_ptr = disasm->m_alternate_disasm_up.get();
585 else
586 mc_disasm_ptr = disasm->m_disasm_up.get();
587
588 lldb::addr_t pc = m_address.GetFileAddress();
589 m_using_file_addr = true;
590
591 bool use_hex_immediates = true;
593
594 if (exe_ctx) {
595 Target *target = exe_ctx->GetTargetPtr();
596 if (target) {
597 use_hex_immediates = target->GetUseHexImmediates();
598 hex_style = target->GetHexImmediateStyle();
599
600 const lldb::addr_t load_addr = m_address.GetLoadAddress(target);
601 if (load_addr != LLDB_INVALID_ADDRESS) {
602 pc = load_addr;
603 m_using_file_addr = false;
604 }
605 }
606 }
607
608 const uint8_t *opcode_data = data.GetDataStart();
609 const size_t opcode_data_len = data.GetByteSize();
610 llvm::MCInst inst;
611 uint64_t inst_size = 0;
612 bool valid = mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc,
613 inst, inst_size);
614
615 if (valid && inst_size > 0) {
616 mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style);
617
618 const bool saved_use_color = mc_disasm_ptr->GetUseColor();
619 mc_disasm_ptr->SetUseColor(false);
620 mc_disasm_ptr->PrintMCInst(inst, pc, out_string, comment_string);
621 mc_disasm_ptr->SetUseColor(true);
622 mc_disasm_ptr->PrintMCInst(inst, pc, markup_out_string,
623 markup_comment_string);
624 mc_disasm_ptr->SetUseColor(saved_use_color);
625
626 if (!comment_string.empty()) {
627 AppendComment(comment_string);
628 }
629 }
630
631 if (inst_size == 0) {
632 m_comment.assign("unknown opcode");
633 inst_size = m_opcode.GetByteSize();
634 StreamString mnemonic_strm;
635 lldb::offset_t offset = 0;
636 lldb::ByteOrder byte_order = data.GetByteOrder();
637 switch (inst_size) {
638 case 1: {
639 const uint8_t uval8 = data.GetU8(&offset);
640 m_opcode.SetOpcode8(uval8, byte_order);
641 m_opcode_name.assign(".byte");
642 mnemonic_strm.Printf("0x%2.2x", uval8);
643 } break;
644 case 2: {
645 const uint16_t uval16 = data.GetU16(&offset);
646 m_opcode.SetOpcode16(uval16, byte_order);
647 m_opcode_name.assign(".short");
648 mnemonic_strm.Printf("0x%4.4x", uval16);
649 } break;
650 case 4: {
651 const uint32_t uval32 = data.GetU32(&offset);
652 m_opcode.SetOpcode32(uval32, byte_order);
653 m_opcode_name.assign(".long");
654 mnemonic_strm.Printf("0x%8.8x", uval32);
655 } break;
656 case 8: {
657 const uint64_t uval64 = data.GetU64(&offset);
658 m_opcode.SetOpcode64(uval64, byte_order);
659 m_opcode_name.assign(".quad");
660 mnemonic_strm.Printf("0x%16.16" PRIx64, uval64);
661 } break;
662 default:
663 if (inst_size == 0)
664 return;
665 else {
666 const uint8_t *bytes = data.PeekData(offset, inst_size);
667 if (bytes == nullptr)
668 return;
669 m_opcode_name.assign(".byte");
670 m_opcode.SetOpcodeBytes(bytes, inst_size);
671 mnemonic_strm.Printf("0x%2.2x", bytes[0]);
672 for (uint32_t i = 1; i < inst_size; ++i)
673 mnemonic_strm.Printf(" 0x%2.2x", bytes[i]);
674 }
675 break;
676 }
677 m_mnemonics = std::string(mnemonic_strm.GetString());
678 return;
679 }
680
681 static RegularExpression s_regex(
682 llvm::StringRef("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?"));
683
684 llvm::SmallVector<llvm::StringRef, 4> matches;
685 if (s_regex.Execute(out_string, &matches)) {
686 m_opcode_name = matches[1].str();
687 m_mnemonics = matches[2].str();
688 }
689 matches.clear();
690 if (s_regex.Execute(markup_out_string, &matches)) {
691 m_markup_opcode_name = matches[1].str();
692 m_markup_mnemonics = matches[2].str();
693 }
694 }
695 }
696 }
697
698 bool IsValid() const { return m_is_valid; }
699
700 bool UsingFileAddress() const { return m_using_file_addr; }
701 size_t GetByteSize() const { return m_opcode.GetByteSize(); }
702
703 /// Grants exclusive access to the disassembler and initializes it with the
704 /// given InstructionLLVMC and an optional ExecutionContext.
706 std::shared_ptr<DisassemblerLLVMC> m_disasm;
707
708 public:
711 const lldb_private::ExecutionContext *exe_ctx = nullptr)
712 : m_disasm(i.m_disasm_wp.lock()) {
713 m_disasm->m_mutex.lock();
714 m_disasm->m_inst = &i;
715 m_disasm->m_exe_ctx = exe_ctx;
716 }
717 ~DisassemblerScope() { m_disasm->m_mutex.unlock(); }
718
719 /// Evaluates to true if this scope contains a valid disassembler.
720 operator bool() const { return static_cast<bool>(m_disasm); }
721
722 std::shared_ptr<DisassemblerLLVMC> operator->() { return m_disasm; }
723 };
724
725 static llvm::StringRef::const_iterator
726 ConsumeWhitespace(llvm::StringRef::const_iterator osi,
727 llvm::StringRef::const_iterator ose) {
728 while (osi != ose) {
729 switch (*osi) {
730 default:
731 return osi;
732 case ' ':
733 case '\t':
734 break;
735 }
736 ++osi;
737 }
738
739 return osi;
740 }
741
742 static std::pair<bool, llvm::StringRef::const_iterator>
743 ConsumeChar(llvm::StringRef::const_iterator osi, const char c,
744 llvm::StringRef::const_iterator ose) {
745 bool found = false;
746
747 osi = ConsumeWhitespace(osi, ose);
748 if (osi != ose && *osi == c) {
749 found = true;
750 ++osi;
751 }
752
753 return std::make_pair(found, osi);
754 }
755
756 static std::pair<Operand, llvm::StringRef::const_iterator>
757 ParseRegisterName(llvm::StringRef::const_iterator osi,
758 llvm::StringRef::const_iterator ose) {
759 Operand ret;
760 ret.m_type = Operand::Type::Register;
761 std::string str;
762
763 osi = ConsumeWhitespace(osi, ose);
764
765 while (osi != ose) {
766 if (*osi >= '0' && *osi <= '9') {
767 if (str.empty()) {
768 return std::make_pair(Operand(), osi);
769 } else {
770 str.push_back(*osi);
771 }
772 } else if (*osi >= 'a' && *osi <= 'z') {
773 str.push_back(*osi);
774 } else {
775 switch (*osi) {
776 default:
777 if (str.empty()) {
778 return std::make_pair(Operand(), osi);
779 } else {
780 ret.m_register = ConstString(str);
781 return std::make_pair(ret, osi);
782 }
783 case '%':
784 if (!str.empty()) {
785 return std::make_pair(Operand(), osi);
786 }
787 break;
788 }
789 }
790 ++osi;
791 }
792
793 ret.m_register = ConstString(str);
794 return std::make_pair(ret, osi);
795 }
796
797 static std::pair<Operand, llvm::StringRef::const_iterator>
798 ParseImmediate(llvm::StringRef::const_iterator osi,
799 llvm::StringRef::const_iterator ose) {
800 Operand ret;
801 ret.m_type = Operand::Type::Immediate;
802 std::string str;
803 bool is_hex = false;
804
805 osi = ConsumeWhitespace(osi, ose);
806
807 while (osi != ose) {
808 if (*osi >= '0' && *osi <= '9') {
809 str.push_back(*osi);
810 } else if (*osi >= 'a' && *osi <= 'f') {
811 if (is_hex) {
812 str.push_back(*osi);
813 } else {
814 return std::make_pair(Operand(), osi);
815 }
816 } else {
817 switch (*osi) {
818 default:
819 if (str.empty()) {
820 return std::make_pair(Operand(), osi);
821 } else {
822 ret.m_immediate = strtoull(str.c_str(), nullptr, 0);
823 return std::make_pair(ret, osi);
824 }
825 case 'x':
826 if (str == "0") {
827 is_hex = true;
828 str.push_back(*osi);
829 } else {
830 return std::make_pair(Operand(), osi);
831 }
832 break;
833 case '#':
834 case '$':
835 if (!str.empty()) {
836 return std::make_pair(Operand(), osi);
837 }
838 break;
839 case '-':
840 if (str.empty()) {
841 ret.m_negative = true;
842 } else {
843 return std::make_pair(Operand(), osi);
844 }
845 }
846 }
847 ++osi;
848 }
849
850 ret.m_immediate = strtoull(str.c_str(), nullptr, 0);
851 return std::make_pair(ret, osi);
852 }
853
854 // -0x5(%rax,%rax,2)
855 static std::pair<Operand, llvm::StringRef::const_iterator>
856 ParseIntelIndexedAccess(llvm::StringRef::const_iterator osi,
857 llvm::StringRef::const_iterator ose) {
858 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
859 ParseImmediate(osi, ose);
860 if (offset_and_iterator.first.IsValid()) {
861 osi = offset_and_iterator.second;
862 }
863
864 bool found = false;
865 std::tie(found, osi) = ConsumeChar(osi, '(', ose);
866 if (!found) {
867 return std::make_pair(Operand(), osi);
868 }
869
870 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
871 ParseRegisterName(osi, ose);
872 if (base_and_iterator.first.IsValid()) {
873 osi = base_and_iterator.second;
874 } else {
875 return std::make_pair(Operand(), osi);
876 }
877
878 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
879 if (!found) {
880 return std::make_pair(Operand(), osi);
881 }
882
883 std::pair<Operand, llvm::StringRef::const_iterator> index_and_iterator =
884 ParseRegisterName(osi, ose);
885 if (index_and_iterator.first.IsValid()) {
886 osi = index_and_iterator.second;
887 } else {
888 return std::make_pair(Operand(), osi);
889 }
890
891 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
892 if (!found) {
893 return std::make_pair(Operand(), osi);
894 }
895
896 std::pair<Operand, llvm::StringRef::const_iterator>
897 multiplier_and_iterator = ParseImmediate(osi, ose);
898 if (index_and_iterator.first.IsValid()) {
899 osi = index_and_iterator.second;
900 } else {
901 return std::make_pair(Operand(), osi);
902 }
903
904 std::tie(found, osi) = ConsumeChar(osi, ')', ose);
905 if (!found) {
906 return std::make_pair(Operand(), osi);
907 }
908
909 Operand product;
910 product.m_type = Operand::Type::Product;
911 product.m_children.push_back(index_and_iterator.first);
912 product.m_children.push_back(multiplier_and_iterator.first);
913
914 Operand index;
915 index.m_type = Operand::Type::Sum;
916 index.m_children.push_back(base_and_iterator.first);
917 index.m_children.push_back(product);
918
919 if (offset_and_iterator.first.IsValid()) {
920 Operand offset;
921 offset.m_type = Operand::Type::Sum;
922 offset.m_children.push_back(offset_and_iterator.first);
923 offset.m_children.push_back(index);
924
925 Operand deref;
926 deref.m_type = Operand::Type::Dereference;
927 deref.m_children.push_back(offset);
928 return std::make_pair(deref, osi);
929 } else {
930 Operand deref;
931 deref.m_type = Operand::Type::Dereference;
932 deref.m_children.push_back(index);
933 return std::make_pair(deref, osi);
934 }
935 }
936
937 // -0x10(%rbp)
938 static std::pair<Operand, llvm::StringRef::const_iterator>
939 ParseIntelDerefAccess(llvm::StringRef::const_iterator osi,
940 llvm::StringRef::const_iterator ose) {
941 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
942 ParseImmediate(osi, ose);
943 if (offset_and_iterator.first.IsValid()) {
944 osi = offset_and_iterator.second;
945 }
946
947 bool found = false;
948 std::tie(found, osi) = ConsumeChar(osi, '(', ose);
949 if (!found) {
950 return std::make_pair(Operand(), osi);
951 }
952
953 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
954 ParseRegisterName(osi, ose);
955 if (base_and_iterator.first.IsValid()) {
956 osi = base_and_iterator.second;
957 } else {
958 return std::make_pair(Operand(), osi);
959 }
960
961 std::tie(found, osi) = ConsumeChar(osi, ')', ose);
962 if (!found) {
963 return std::make_pair(Operand(), osi);
964 }
965
966 if (offset_and_iterator.first.IsValid()) {
967 Operand offset;
968 offset.m_type = Operand::Type::Sum;
969 offset.m_children.push_back(offset_and_iterator.first);
970 offset.m_children.push_back(base_and_iterator.first);
971
972 Operand deref;
973 deref.m_type = Operand::Type::Dereference;
974 deref.m_children.push_back(offset);
975 return std::make_pair(deref, osi);
976 } else {
977 Operand deref;
978 deref.m_type = Operand::Type::Dereference;
979 deref.m_children.push_back(base_and_iterator.first);
980 return std::make_pair(deref, osi);
981 }
982 }
983
984 // [sp, #8]!
985 static std::pair<Operand, llvm::StringRef::const_iterator>
986 ParseARMOffsetAccess(llvm::StringRef::const_iterator osi,
987 llvm::StringRef::const_iterator ose) {
988 bool found = false;
989 std::tie(found, osi) = ConsumeChar(osi, '[', ose);
990 if (!found) {
991 return std::make_pair(Operand(), osi);
992 }
993
994 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
995 ParseRegisterName(osi, ose);
996 if (base_and_iterator.first.IsValid()) {
997 osi = base_and_iterator.second;
998 } else {
999 return std::make_pair(Operand(), osi);
1000 }
1001
1002 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
1003 if (!found) {
1004 return std::make_pair(Operand(), osi);
1005 }
1006
1007 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
1008 ParseImmediate(osi, ose);
1009 if (offset_and_iterator.first.IsValid()) {
1010 osi = offset_and_iterator.second;
1011 }
1012
1013 std::tie(found, osi) = ConsumeChar(osi, ']', ose);
1014 if (!found) {
1015 return std::make_pair(Operand(), osi);
1016 }
1017
1018 Operand offset;
1019 offset.m_type = Operand::Type::Sum;
1020 offset.m_children.push_back(offset_and_iterator.first);
1021 offset.m_children.push_back(base_and_iterator.first);
1022
1023 Operand deref;
1024 deref.m_type = Operand::Type::Dereference;
1025 deref.m_children.push_back(offset);
1026 return std::make_pair(deref, osi);
1027 }
1028
1029 // [sp]
1030 static std::pair<Operand, llvm::StringRef::const_iterator>
1031 ParseARMDerefAccess(llvm::StringRef::const_iterator osi,
1032 llvm::StringRef::const_iterator ose) {
1033 bool found = false;
1034 std::tie(found, osi) = ConsumeChar(osi, '[', ose);
1035 if (!found) {
1036 return std::make_pair(Operand(), osi);
1037 }
1038
1039 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
1040 ParseRegisterName(osi, ose);
1041 if (base_and_iterator.first.IsValid()) {
1042 osi = base_and_iterator.second;
1043 } else {
1044 return std::make_pair(Operand(), osi);
1045 }
1046
1047 std::tie(found, osi) = ConsumeChar(osi, ']', ose);
1048 if (!found) {
1049 return std::make_pair(Operand(), osi);
1050 }
1051
1052 Operand deref;
1053 deref.m_type = Operand::Type::Dereference;
1054 deref.m_children.push_back(base_and_iterator.first);
1055 return std::make_pair(deref, osi);
1056 }
1057
1058 static void DumpOperand(const Operand &op, Stream &s) {
1059 switch (op.m_type) {
1060 case Operand::Type::Dereference:
1061 s.PutCString("*");
1062 DumpOperand(op.m_children[0], s);
1063 break;
1064 case Operand::Type::Immediate:
1065 if (op.m_negative) {
1066 s.PutCString("-");
1067 }
1068 s.PutCString(llvm::to_string(op.m_immediate));
1069 break;
1070 case Operand::Type::Invalid:
1071 s.PutCString("Invalid");
1072 break;
1073 case Operand::Type::Product:
1074 s.PutCString("(");
1075 DumpOperand(op.m_children[0], s);
1076 s.PutCString("*");
1077 DumpOperand(op.m_children[1], s);
1078 s.PutCString(")");
1079 break;
1080 case Operand::Type::Register:
1082 break;
1083 case Operand::Type::Sum:
1084 s.PutCString("(");
1085 DumpOperand(op.m_children[0], s);
1086 s.PutCString("+");
1087 DumpOperand(op.m_children[1], s);
1088 s.PutCString(")");
1089 break;
1090 }
1091 }
1092
1095 const char *operands_string = GetOperands(nullptr);
1096
1097 if (!operands_string) {
1098 return false;
1099 }
1100
1101 llvm::StringRef operands_ref(operands_string);
1102
1103 llvm::StringRef::const_iterator osi = operands_ref.begin();
1104 llvm::StringRef::const_iterator ose = operands_ref.end();
1105
1106 while (osi != ose) {
1107 Operand operand;
1108 llvm::StringRef::const_iterator iter;
1109
1110 if ((std::tie(operand, iter) = ParseIntelIndexedAccess(osi, ose),
1111 operand.IsValid()) ||
1112 (std::tie(operand, iter) = ParseIntelDerefAccess(osi, ose),
1113 operand.IsValid()) ||
1114 (std::tie(operand, iter) = ParseARMOffsetAccess(osi, ose),
1115 operand.IsValid()) ||
1116 (std::tie(operand, iter) = ParseARMDerefAccess(osi, ose),
1117 operand.IsValid()) ||
1118 (std::tie(operand, iter) = ParseRegisterName(osi, ose),
1119 operand.IsValid()) ||
1120 (std::tie(operand, iter) = ParseImmediate(osi, ose),
1121 operand.IsValid())) {
1122 osi = iter;
1123 operands.push_back(operand);
1124 } else {
1125 return false;
1126 }
1127
1128 std::pair<bool, llvm::StringRef::const_iterator> found_and_iter =
1129 ConsumeChar(osi, ',', ose);
1130 if (found_and_iter.first) {
1131 osi = found_and_iter.second;
1132 }
1133
1134 osi = ConsumeWhitespace(osi, ose);
1135 }
1136
1137 DisassemblerSP disasm_sp = m_disasm_wp.lock();
1138
1139 if (disasm_sp && operands.size() > 1) {
1140 // TODO tie this into the MC Disassembler's notion of clobbers.
1141 switch (disasm_sp->GetArchitecture().GetMachine()) {
1142 default:
1143 break;
1144 case llvm::Triple::x86:
1145 case llvm::Triple::x86_64:
1146 operands[operands.size() - 1].m_clobbered = true;
1147 break;
1148 case llvm::Triple::arm:
1149 operands[0].m_clobbered = true;
1150 break;
1151 }
1152 }
1153
1155 StreamString ss;
1156
1157 ss.Printf("[%s] expands to %zu operands:\n", operands_string,
1158 operands.size());
1159 for (const Operand &operand : operands) {
1160 ss.PutCString(" ");
1161 DumpOperand(operand, ss);
1162 ss.PutCString("\n");
1163 }
1164
1165 log->PutString(ss.GetString());
1166 }
1167
1168 return true;
1169 }
1170
1171 bool IsCall() override {
1173 return m_is_call;
1174 }
1175
1176protected:
1177 std::weak_ptr<DisassemblerLLVMC> m_disasm_wp;
1178
1179 bool m_is_valid = false;
1180 bool m_using_file_addr = false;
1182
1183 // Be conservative. If we didn't understand the instruction, say it:
1184 // - Might branch
1185 // - Does not have a delay slot
1186 // - Is not a call
1187 // - Is not a load
1188 // - Is not an authenticated instruction
1189 bool m_does_branch = true;
1190 bool m_has_delay_slot = false;
1191 bool m_is_call = false;
1192 bool m_is_load = false;
1194 bool m_is_barrier = false;
1195
1198 return;
1199
1200 DisassemblerScope disasm(*this);
1201 if (!disasm)
1202 return;
1203
1204 DataExtractor data;
1205 if (!m_opcode.GetData(data))
1206 return;
1207
1208 bool is_alternate_isa;
1209 lldb::addr_t pc = m_address.GetFileAddress();
1211 GetDisasmToUse(is_alternate_isa, disasm);
1212 const uint8_t *opcode_data = data.GetDataStart();
1213 const size_t opcode_data_len = data.GetByteSize();
1214 llvm::MCInst inst;
1215 uint64_t inst_size = 0;
1216 const bool valid = mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len,
1217 pc, inst, inst_size);
1218 if (!valid)
1219 return;
1220
1222 m_does_branch = mc_disasm_ptr->CanBranch(inst);
1223 m_has_delay_slot = mc_disasm_ptr->HasDelaySlot(inst);
1224 m_is_call = mc_disasm_ptr->IsCall(inst);
1225 m_is_load = mc_disasm_ptr->IsLoad(inst);
1226 m_is_authenticated = mc_disasm_ptr->IsAuthenticated(inst);
1227 m_is_barrier = mc_disasm_ptr->IsBarrier(inst);
1228 }
1229
1230private:
1232 GetDisasmToUse(bool &is_alternate_isa, DisassemblerScope &disasm) {
1233 is_alternate_isa = false;
1234 if (disasm) {
1235 if (disasm->m_alternate_disasm_up) {
1236 const AddressClass address_class = GetAddressClass();
1237
1238 if (address_class == AddressClass::eCodeAlternateISA) {
1239 is_alternate_isa = true;
1240 return disasm->m_alternate_disasm_up.get();
1241 }
1242 }
1243 return disasm->m_disasm_up.get();
1244 }
1245 return nullptr;
1246 }
1247};
1248
1249std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>
1251 const char *cpu,
1252 const char *features_str,
1253 unsigned flavor,
1254 DisassemblerLLVMC &owner) {
1255 using Instance = std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>;
1256
1257 llvm::Triple triple(triple_name);
1258
1259 std::string Status;
1260 const llvm::Target *curr_target =
1261 llvm::TargetRegistry::lookupTarget(triple, Status);
1262 if (!curr_target)
1263 return Instance();
1264
1265 std::unique_ptr<llvm::MCInstrInfo> instr_info_up(
1266 curr_target->createMCInstrInfo());
1267 if (!instr_info_up)
1268 return Instance();
1269
1270 std::unique_ptr<llvm::MCRegisterInfo> reg_info_up(
1271 curr_target->createMCRegInfo(triple));
1272 if (!reg_info_up)
1273 return Instance();
1274
1275 std::unique_ptr<llvm::MCSubtargetInfo> subtarget_info_up(
1276 curr_target->createMCSubtargetInfo(triple, cpu, features_str));
1277 if (!subtarget_info_up)
1278 return Instance();
1279
1280 llvm::MCTargetOptions MCOptions;
1281 std::unique_ptr<llvm::MCAsmInfo> asm_info_up(
1282 curr_target->createMCAsmInfo(*reg_info_up, triple, MCOptions));
1283 if (!asm_info_up)
1284 return Instance();
1285
1286 std::unique_ptr<llvm::MCContext> context_up(
1287 new llvm::MCContext(llvm::Triple(triple), asm_info_up.get(),
1288 reg_info_up.get(), subtarget_info_up.get()));
1289 if (!context_up)
1290 return Instance();
1291
1292 std::unique_ptr<llvm::MCDisassembler> disasm_up(
1293 curr_target->createMCDisassembler(*subtarget_info_up, *context_up));
1294 if (!disasm_up)
1295 return Instance();
1296
1297 std::unique_ptr<llvm::MCRelocationInfo> rel_info_up(
1298 curr_target->createMCRelocationInfo(triple, *context_up));
1299 if (!rel_info_up)
1300 return Instance();
1301
1302 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(
1303 curr_target->createMCSymbolizer(
1304 triple, nullptr, DisassemblerLLVMC::SymbolLookupCallback, &owner,
1305 context_up.get(), std::move(rel_info_up)));
1306 disasm_up->setSymbolizer(std::move(symbolizer_up));
1307
1308 unsigned asm_printer_variant =
1309 flavor == ~0U ? asm_info_up->getAssemblerDialect() : flavor;
1310
1311 std::unique_ptr<llvm::MCInstPrinter> instr_printer_up(
1312 curr_target->createMCInstPrinter(llvm::Triple{triple},
1313 asm_printer_variant, *asm_info_up,
1314 *instr_info_up, *reg_info_up));
1315 if (!instr_printer_up)
1316 return Instance();
1317
1318 instr_printer_up->setPrintBranchImmAsAddress(true);
1319
1320 // Not all targets may have registered createMCInstrAnalysis().
1321 std::unique_ptr<llvm::MCInstrAnalysis> instr_analysis_up(
1322 curr_target->createMCInstrAnalysis(instr_info_up.get()));
1323
1324 return Instance(new MCDisasmInstance(
1325 std::move(instr_info_up), std::move(reg_info_up),
1326 std::move(subtarget_info_up), MCOptions, std::move(asm_info_up),
1327 std::move(context_up), std::move(disasm_up), std::move(instr_printer_up),
1328 std::move(instr_analysis_up)));
1329}
1330
1332 std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up,
1333 std::unique_ptr<llvm::MCRegisterInfo> &&reg_info_up,
1334 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
1335 llvm::MCTargetOptions mc_options,
1336 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
1337 std::unique_ptr<llvm::MCContext> &&context_up,
1338 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
1339 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up,
1340 std::unique_ptr<llvm::MCInstrAnalysis> &&instr_analysis_up)
1341 : m_instr_info_up(std::move(instr_info_up)),
1342 m_reg_info_up(std::move(reg_info_up)),
1343 m_subtarget_info_up(std::move(subtarget_info_up)),
1344 m_mc_options(mc_options), m_asm_info_up(std::move(asm_info_up)),
1345 m_context_up(std::move(context_up)), m_disasm_up(std::move(disasm_up)),
1346 m_instr_printer_up(std::move(instr_printer_up)),
1347 m_instr_analysis_up(std::move(instr_analysis_up)) {
1350}
1351
1353 size_t opcode_data_len,
1355 llvm::MCInst &mc_inst,
1356 uint64_t &size) const {
1357 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
1358 llvm::MCDisassembler::DecodeStatus status;
1359
1360 status = m_disasm_up->getInstruction(mc_inst, size, data, pc, llvm::nulls());
1361 if (status == llvm::MCDisassembler::Success)
1362 return true;
1363 else
1364 return false;
1365}
1366
1368 llvm::MCInst &mc_inst, lldb::addr_t pc, std::string &inst_string,
1369 std::string &comments_string) {
1370 llvm::raw_string_ostream inst_stream(inst_string);
1371 llvm::raw_string_ostream comments_stream(comments_string);
1372
1373 inst_stream.enable_colors(m_instr_printer_up->getUseColor());
1374 m_instr_printer_up->setCommentStream(comments_stream);
1375 m_instr_printer_up->printInst(&mc_inst, pc, llvm::StringRef(),
1376 *m_subtarget_info_up, inst_stream);
1377 m_instr_printer_up->setCommentStream(llvm::nulls());
1378
1379 static std::string g_newlines("\r\n");
1380
1381 for (size_t newline_pos = 0;
1382 (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) !=
1383 comments_string.npos;
1384 /**/) {
1385 comments_string.replace(comments_string.begin() + newline_pos,
1386 comments_string.begin() + newline_pos + 1, 1, ' ');
1387 }
1388}
1389
1391 bool use_hex_immed, HexImmediateStyle hex_style) {
1392 m_instr_printer_up->setPrintImmHex(use_hex_immed);
1393 switch (hex_style) {
1394 case eHexStyleC:
1395 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::C);
1396 break;
1397 case eHexStyleAsm:
1398 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::Asm);
1399 break;
1400 }
1401}
1402
1404 m_instr_printer_up->setUseColor(use_color);
1405}
1406
1408 return m_instr_printer_up->getUseColor();
1409}
1410
1412 llvm::MCInst &mc_inst) const {
1414 return m_instr_analysis_up->mayAffectControlFlow(mc_inst, *m_reg_info_up);
1415 return m_instr_info_up->get(mc_inst.getOpcode())
1416 .mayAffectControlFlow(mc_inst, *m_reg_info_up);
1417}
1418
1420 llvm::MCInst &mc_inst) const {
1421 return m_instr_info_up->get(mc_inst.getOpcode()).hasDelaySlot();
1422}
1423
1424bool DisassemblerLLVMC::MCDisasmInstance::IsCall(llvm::MCInst &mc_inst) const {
1426 return m_instr_analysis_up->isCall(mc_inst);
1427 return m_instr_info_up->get(mc_inst.getOpcode()).isCall();
1428}
1429
1430bool DisassemblerLLVMC::MCDisasmInstance::IsLoad(llvm::MCInst &mc_inst) const {
1431 return m_instr_info_up->get(mc_inst.getOpcode()).mayLoad();
1432}
1433
1435 llvm::MCInst &mc_inst) const {
1436 return m_instr_info_up->get(mc_inst.getOpcode()).isBarrier();
1437}
1438
1440 llvm::MCInst &mc_inst) const {
1441 const auto &InstrDesc = m_instr_info_up->get(mc_inst.getOpcode());
1442
1443 // Treat software auth traps (brk 0xc470 + aut key, where 0x70 == 'p', 0xc4
1444 // == 'a' + 'c') as authenticated instructions for reporting purposes, in
1445 // addition to the standard authenticated instructions specified in ARMv8.3.
1446 bool IsBrkC47x = false;
1447 if (InstrDesc.isTrap() && mc_inst.getNumOperands() == 1) {
1448 const llvm::MCOperand &Op0 = mc_inst.getOperand(0);
1449 if (Op0.isImm() && Op0.getImm() >= 0xc470 && Op0.getImm() <= 0xc474)
1450 IsBrkC47x = true;
1451 }
1452
1453 return InstrDesc.isAuthenticated() || IsBrkC47x;
1454}
1455
1457 const char *flavor_string,
1458 const char *cpu_string,
1459 const char *features_string)
1460 : Disassembler(arch, flavor_string), m_exe_ctx(nullptr), m_inst(nullptr),
1462 m_adrp_insn() {
1463 if (!FlavorValidForArchSpec(arch, m_flavor.c_str())) {
1464 m_flavor.assign("default");
1465 }
1466
1467 const bool cpu_or_features_overriden = cpu_string || features_string;
1468 unsigned flavor = ~0U;
1469 llvm::Triple triple = arch.GetTriple();
1470
1471 // So far the only supported flavor is "intel" on x86. The base class will
1472 // set this correctly coming in.
1473 if (triple.getArch() == llvm::Triple::x86 ||
1474 triple.getArch() == llvm::Triple::x86_64) {
1475 if (m_flavor == "intel") {
1476 flavor = 1;
1477 } else if (m_flavor == "att") {
1478 flavor = 0;
1479 }
1480 }
1481
1482 ArchSpec thumb_arch(arch);
1483 if (triple.getArch() == llvm::Triple::arm) {
1484 std::string thumb_arch_name(thumb_arch.GetTriple().getArchName().str());
1485 // Replace "arm" with "thumb" so we get all thumb variants correct
1486 if (thumb_arch_name.size() > 3) {
1487 thumb_arch_name.erase(0, 3);
1488 thumb_arch_name.insert(0, "thumb");
1489 } else {
1490 thumb_arch_name = "thumbv9.3a";
1491 }
1492 thumb_arch.GetTriple().setArchName(llvm::StringRef(thumb_arch_name));
1493 }
1494
1495 // If no sub architecture specified then use the most recent arm architecture
1496 // so the disassembler will return all instructions. Without it we will see a
1497 // lot of unknown opcodes if the code uses instructions which are not
1498 // available in the oldest arm version (which is used when no sub architecture
1499 // is specified).
1500 if (triple.getArch() == llvm::Triple::arm &&
1501 triple.getSubArch() == llvm::Triple::NoSubArch)
1502 triple.setArchName("armv9.3a");
1503
1504 std::string features_str =
1505 features_string ? std::string(features_string) : "";
1506 const char *triple_str = triple.getTriple().c_str();
1507
1508 // ARM Cortex M0-M7 devices only execute thumb instructions
1509 if (arch.IsAlwaysThumbInstructions()) {
1510 triple_str = thumb_arch.GetTriple().getTriple().c_str();
1511 if (!features_string)
1512 features_str += "+fp-armv8,";
1513 }
1514
1515 const char *cpu = cpu_string;
1516
1517 if (!cpu_or_features_overriden) {
1518 switch (arch.GetCore()) {
1521 cpu = "mips32";
1522 break;
1525 cpu = "mips32r2";
1526 break;
1529 cpu = "mips32r3";
1530 break;
1533 cpu = "mips32r5";
1534 break;
1537 cpu = "mips32r6";
1538 break;
1541 cpu = "mips64";
1542 break;
1545 cpu = "mips64r2";
1546 break;
1549 cpu = "mips64r3";
1550 break;
1553 cpu = "mips64r5";
1554 break;
1557 cpu = "mips64r6";
1558 break;
1559 default:
1560 cpu = "";
1561 break;
1562 }
1563 }
1564
1565 if (arch.IsMIPS() && !cpu_or_features_overriden) {
1566 uint32_t arch_flags = arch.GetFlags();
1567 if (arch_flags & ArchSpec::eMIPSAse_msa)
1568 features_str += "+msa,";
1569 if (arch_flags & ArchSpec::eMIPSAse_dsp)
1570 features_str += "+dsp,";
1571 if (arch_flags & ArchSpec::eMIPSAse_dspr2)
1572 features_str += "+dspr2,";
1573 }
1574
1575 // If any AArch64 variant, enable latest ISA with all extensions unless the
1576 // CPU or features were overridden.
1577 if (triple.isAArch64() && !cpu_or_features_overriden) {
1578 features_str += "+all,";
1579 if (triple.getVendor() == llvm::Triple::Apple)
1580 cpu = "apple-latest";
1581 }
1582
1583 if (triple.isRISCV() && !cpu_or_features_overriden) {
1584 auto subtarget_features = arch.GetSubtargetFeatures().getString();
1585 if (!subtarget_features.empty()) {
1586 features_str += subtarget_features;
1587 } else {
1588 uint32_t arch_flags = arch.GetFlags();
1589 if (arch_flags & ArchSpec::eRISCV_rvc)
1590 features_str += "+c,";
1591 if (arch_flags & ArchSpec::eRISCV_rve)
1592 features_str += "+e,";
1593 if ((arch_flags & ArchSpec::eRISCV_float_abi_single) ==
1595 features_str += "+f,";
1596 if ((arch_flags & ArchSpec::eRISCV_float_abi_double) ==
1598 features_str += "+f,+d,";
1599 if ((arch_flags & ArchSpec::eRISCV_float_abi_quad) ==
1601 features_str += "+f,+d,+q,";
1602 // FIXME: how do we detect features such as `+a`, `+m`?
1603 // Turn them on by default now, since everyone seems to use them
1604 features_str += "+a,+m,";
1605 }
1606 }
1607
1608 // We use m_disasm_up.get() to tell whether we are valid or not, so if this
1609 // isn't good for some reason, we won't be valid and FindPlugin will fail and
1610 // we won't get used.
1611 m_disasm_up = MCDisasmInstance::Create(triple_str, cpu, features_str.c_str(),
1612 flavor, *this);
1613
1614 llvm::Triple::ArchType llvm_arch = triple.getArch();
1615
1616 // For arm CPUs that can execute arm or thumb instructions, also create a
1617 // thumb instruction disassembler.
1618 if (llvm_arch == llvm::Triple::arm) {
1619 std::string thumb_triple(thumb_arch.GetTriple().getTriple());
1620 m_alternate_disasm_up = MCDisasmInstance::Create(
1621 thumb_triple.c_str(), "", features_str.c_str(), flavor, *this);
1622 if (!m_alternate_disasm_up)
1623 m_disasm_up.reset();
1624
1625 } else if (arch.IsMIPS()) {
1626 /* Create alternate disassembler for MIPS16 and microMIPS */
1627 uint32_t arch_flags = arch.GetFlags();
1628 if (arch_flags & ArchSpec::eMIPSAse_mips16)
1629 features_str += "+mips16,";
1630 else if (arch_flags & ArchSpec::eMIPSAse_micromips)
1631 features_str += "+micromips,";
1632
1633 m_alternate_disasm_up = MCDisasmInstance::Create(
1634 triple_str, cpu, features_str.c_str(), flavor, *this);
1635 if (!m_alternate_disasm_up)
1636 m_disasm_up.reset();
1637 }
1638}
1639
1641
1643 const char *flavor,
1644 const char *cpu,
1645 const char *features) {
1646 if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch) {
1647 auto disasm_sp =
1648 std::make_shared<DisassemblerLLVMC>(arch, flavor, cpu, features);
1649 if (disasm_sp && disasm_sp->IsValid())
1650 return disasm_sp;
1651 }
1652 return lldb::DisassemblerSP();
1653}
1654
1656 const DataExtractor &data,
1657 lldb::offset_t data_offset,
1658 size_t num_instructions,
1659 bool append, bool data_from_file) {
1660 if (!append)
1661 m_instruction_list.Clear();
1662
1663 if (!IsValid())
1664 return 0;
1665
1666 m_data_from_file = data_from_file;
1667 uint32_t data_cursor = data_offset;
1668 const size_t data_byte_size = data.GetByteSize();
1669 uint32_t instructions_parsed = 0;
1670 Address inst_addr(base_addr);
1671
1672 while (data_cursor < data_byte_size &&
1673 instructions_parsed < num_instructions) {
1674
1675 AddressClass address_class = AddressClass::eCode;
1676
1678 address_class = inst_addr.GetAddressClass();
1679
1680 InstructionSP inst_sp(
1681 new InstructionLLVMC(*this, inst_addr, address_class));
1682
1683 if (!inst_sp)
1684 break;
1685
1686 uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
1687
1688 if (inst_size == 0)
1689 break;
1690
1691 m_instruction_list.Append(inst_sp);
1692 data_cursor += inst_size;
1693 inst_addr.Slide(inst_size);
1694 instructions_parsed++;
1695 }
1696
1697 return data_cursor - data_offset;
1698}
1699
1702 "Disassembler that uses LLVM MC to disassemble "
1703 "i386, x86_64, ARM, and ARM64.",
1705
1706 llvm::InitializeAllTargetInfos();
1707 llvm::InitializeAllTargetMCs();
1708 llvm::InitializeAllAsmParsers();
1709 llvm::InitializeAllDisassemblers();
1710}
1711
1715
1716int DisassemblerLLVMC::OpInfoCallback(void *disassembler, uint64_t pc,
1717 uint64_t offset, uint64_t size,
1718 int tag_type, void *tag_bug) {
1719 return static_cast<DisassemblerLLVMC *>(disassembler)
1720 ->OpInfo(pc, offset, size, tag_type, tag_bug);
1721}
1722
1723const char *DisassemblerLLVMC::SymbolLookupCallback(void *disassembler,
1724 uint64_t value,
1725 uint64_t *type, uint64_t pc,
1726 const char **name) {
1727 return static_cast<DisassemblerLLVMC *>(disassembler)
1728 ->SymbolLookup(value, type, pc, name);
1729}
1730
1732 const lldb_private::ArchSpec &arch, const char *flavor) {
1733 llvm::Triple triple = arch.GetTriple();
1734 if (flavor == nullptr || strcmp(flavor, "default") == 0)
1735 return true;
1736
1737 if (triple.getArch() == llvm::Triple::x86 ||
1738 triple.getArch() == llvm::Triple::x86_64) {
1739 return strcmp(flavor, "intel") == 0 || strcmp(flavor, "att") == 0;
1740 } else
1741 return false;
1742}
1743
1744bool DisassemblerLLVMC::IsValid() const { return m_disasm_up.operator bool(); }
1745
1746int DisassemblerLLVMC::OpInfo(uint64_t PC, uint64_t Offset, uint64_t Size,
1747 int tag_type, void *tag_bug) {
1748 switch (tag_type) {
1749 default:
1750 break;
1751 case 1:
1752 memset(tag_bug, 0, sizeof(::LLVMOpInfo1));
1753 break;
1754 }
1755 return 0;
1756}
1757
1758const char *DisassemblerLLVMC::SymbolLookup(uint64_t value, uint64_t *type_ptr,
1759 uint64_t pc, const char **name) {
1760 if (*type_ptr) {
1761 if (m_exe_ctx && m_inst) {
1762 // std::string remove_this_prior_to_checkin;
1763 Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : nullptr;
1764 Address value_so_addr;
1765 Address pc_so_addr;
1766 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
1767 target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_be ||
1768 target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {
1769 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
1771 m_adrp_insn = value;
1772 *name = nullptr;
1773 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
1774 return nullptr;
1775 }
1776 // If this instruction is an ADD and
1777 // the previous instruction was an ADRP and
1778 // the ADRP's register and this ADD's register are the same,
1779 // then this is a pc-relative address calculation.
1780 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
1781 m_adrp_insn && m_adrp_address == pc - 4 &&
1782 (*m_adrp_insn & 0x1f) == ((value >> 5) & 0x1f)) {
1783 uint32_t addxri_inst;
1784 uint64_t adrp_imm, addxri_imm;
1785 // Get immlo and immhi bits, OR them together to get the ADRP imm
1786 // value.
1787 adrp_imm =
1788 ((*m_adrp_insn & 0x00ffffe0) >> 3) | ((*m_adrp_insn >> 29) & 0x3);
1789 // if high bit of immhi after right-shifting set, sign extend
1790 if (adrp_imm & (1ULL << 20))
1791 adrp_imm |= ~((1ULL << 21) - 1);
1792
1793 addxri_inst = value;
1794 addxri_imm = (addxri_inst >> 10) & 0xfff;
1795 // check if 'sh' bit is set, shift imm value up if so
1796 // (this would make no sense, ADRP already gave us this part)
1797 if ((addxri_inst >> (12 + 5 + 5)) & 1)
1798 addxri_imm <<= 12;
1799 value = (m_adrp_address & 0xfffffffffffff000LL) + (adrp_imm << 12) +
1800 addxri_imm;
1801 }
1803 m_adrp_insn.reset();
1804 }
1805
1806 if (m_inst->UsingFileAddress()) {
1807 ModuleSP module_sp(m_inst->GetAddress().GetModule());
1808 if (module_sp) {
1809 module_sp->ResolveFileAddress(value, value_so_addr);
1810 module_sp->ResolveFileAddress(pc, pc_so_addr);
1811 }
1812 } else if (target && target->HasLoadedSections()) {
1813 target->ResolveLoadAddress(value, value_so_addr);
1814 target->ResolveLoadAddress(pc, pc_so_addr);
1815 }
1816
1817 SymbolContext sym_ctx;
1818 const SymbolContextItem resolve_scope =
1819 eSymbolContextFunction | eSymbolContextSymbol;
1820 if (pc_so_addr.IsValid() && pc_so_addr.GetModule()) {
1821 pc_so_addr.GetModule()->ResolveSymbolContextForAddress(
1822 pc_so_addr, resolve_scope, sym_ctx);
1823 }
1824
1825 if (value_so_addr.IsValid() && value_so_addr.GetSection()) {
1826 StreamString ss;
1827
1828 bool format_omitting_current_func_name = false;
1829 if (sym_ctx.symbol || sym_ctx.function) {
1830 AddressRange range;
1831 for (uint32_t idx = 0;
1832 sym_ctx.GetAddressRange(resolve_scope, idx, false, range);
1833 ++idx) {
1834 if (range.ContainsLoadAddress(value_so_addr, target)) {
1835 format_omitting_current_func_name = true;
1836 break;
1837 }
1838 }
1839 }
1840
1841 // If the "value" address (the target address we're symbolicating) is
1842 // inside the same SymbolContext as the current instruction pc
1843 // (pc_so_addr), don't print the full function name - just print it
1844 // with DumpStyleNoFunctionName style, e.g. "<+36>".
1845 if (format_omitting_current_func_name) {
1846 value_so_addr.Dump(&ss, target, Address::DumpStyleNoFunctionName,
1848 } else {
1849 value_so_addr.Dump(
1850 &ss, target,
1853 }
1854
1855 if (!ss.GetString().empty()) {
1856 // If Address::Dump returned a multi-line description, most commonly
1857 // seen when we have multiple levels of inlined functions at an
1858 // address, only show the first line.
1859 std::string str = std::string(ss.GetString());
1860 size_t first_eol_char = str.find_first_of("\r\n");
1861 if (first_eol_char != std::string::npos) {
1862 str.erase(first_eol_char);
1863 }
1864 m_inst->AppendComment(str);
1865 }
1866 }
1867 }
1868 }
1869
1870 // TODO: llvm-objdump sets the type_ptr to the
1871 // LLVMDisassembler_ReferenceType_Out_* values
1872 // based on where value_so_addr is pointing, with
1873 // Mach-O specific augmentations in MachODump.cpp. e.g.
1874 // see what AArch64ExternalSymbolizer::tryAddingSymbolicOperand
1875 // handles.
1876 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
1877 *name = nullptr;
1878 return nullptr;
1879}
#define LLDB_PLUGIN_DEFINE(PluginName)
bool HasDelaySlot(llvm::MCInst &mc_inst) const
MCDisasmInstance(std::unique_ptr< llvm::MCInstrInfo > &&instr_info_up, std::unique_ptr< llvm::MCRegisterInfo > &&reg_info_up, std::unique_ptr< llvm::MCSubtargetInfo > &&subtarget_info_up, llvm::MCTargetOptions mc_options, std::unique_ptr< llvm::MCAsmInfo > &&asm_info_up, std::unique_ptr< llvm::MCContext > &&context_up, std::unique_ptr< llvm::MCDisassembler > &&disasm_up, std::unique_ptr< llvm::MCInstPrinter > &&instr_printer_up, std::unique_ptr< llvm::MCInstrAnalysis > &&instr_analysis_up)
bool IsAuthenticated(llvm::MCInst &mc_inst) const
std::unique_ptr< llvm::MCInstrInfo > m_instr_info_up
std::unique_ptr< llvm::MCRegisterInfo > m_reg_info_up
bool CanBranch(llvm::MCInst &mc_inst) const
std::unique_ptr< llvm::MCContext > m_context_up
std::unique_ptr< llvm::MCAsmInfo > m_asm_info_up
void PrintMCInst(llvm::MCInst &mc_inst, lldb::addr_t pc, std::string &inst_string, std::string &comments_string)
bool IsBarrier(llvm::MCInst &mc_inst) const
void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style)
static std::unique_ptr< MCDisasmInstance > Create(const char *triple, const char *cpu, const char *features_str, unsigned flavor, DisassemblerLLVMC &owner)
bool GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc, llvm::MCInst &mc_inst, uint64_t &size) const
bool IsLoad(llvm::MCInst &mc_inst) const
bool IsCall(llvm::MCInst &mc_inst) const
std::unique_ptr< llvm::MCSubtargetInfo > m_subtarget_info_up
std::unique_ptr< llvm::MCInstrAnalysis > m_instr_analysis_up
std::unique_ptr< llvm::MCDisassembler > m_disasm_up
std::unique_ptr< llvm::MCInstPrinter > m_instr_printer_up
std::optional< uint32_t > m_adrp_insn
DisassemblerLLVMC(const lldb_private::ArchSpec &arch, const char *flavor, const char *cpu, const char *features)
static const char * SymbolLookupCallback(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
int OpInfo(uint64_t PC, uint64_t Offset, uint64_t Size, int TagType, void *TagBug)
const lldb_private::ExecutionContext * m_exe_ctx
const char * SymbolLookup(uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
std::unique_ptr< MCDisasmInstance > m_disasm_up
static llvm::StringRef GetPluginNameStatic()
friend class InstructionLLVMC
static int OpInfoCallback(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t Size, int TagType, void *TagBug)
static lldb::DisassemblerSP CreateInstance(const lldb_private::ArchSpec &arch, const char *flavor, const char *cpu, const char *features)
lldb::addr_t m_adrp_address
bool FlavorValidForArchSpec(const lldb_private::ArchSpec &arch, const char *flavor) override
~DisassemblerLLVMC() override
std::unique_ptr< MCDisasmInstance > m_alternate_disasm_up
InstructionLLVMC * m_inst
size_t DecodeInstructions(const lldb_private::Address &base_addr, const lldb_private::DataExtractor &data, lldb::offset_t data_offset, size_t num_instructions, bool append, bool data_from_file) override
Grants exclusive access to the disassembler and initializes it with the given InstructionLLVMC and an...
std::shared_ptr< DisassemblerLLVMC > m_disasm
DisassemblerScope(InstructionLLVMC &i, const lldb_private::ExecutionContext *exe_ctx=nullptr)
std::shared_ptr< DisassemblerLLVMC > operator->()
static std::pair< Operand, llvm::StringRef::const_iterator > ParseIntelIndexedAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
bool DoesBranch() override
DisassemblerLLVMC::MCDisasmInstance * GetDisasmToUse(bool &is_alternate_isa)
static void DumpOperand(const Operand &op, Stream &s)
size_t GetByteSize() const
static llvm::StringRef::const_iterator ConsumeWhitespace(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
std::weak_ptr< DisassemblerLLVMC > m_disasm_wp
void CalculateMnemonicOperandsAndComment(const lldb_private::ExecutionContext *exe_ctx) override
bool IsLoad() override
static std::pair< Operand, llvm::StringRef::const_iterator > ParseARMOffsetAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
DisassemblerLLVMC::MCDisasmInstance * GetDisasmToUse(bool &is_alternate_isa, DisassemblerScope &disasm)
void AppendComment(std::string &description)
bool UsingFileAddress() const
bool IsAuthenticated() override
static std::pair< Operand, llvm::StringRef::const_iterator > ParseIntelDerefAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
static std::pair< bool, llvm::StringRef::const_iterator > ConsumeChar(llvm::StringRef::const_iterator osi, const char c, llvm::StringRef::const_iterator ose)
size_t Decode(const lldb_private::Disassembler &disassembler, const lldb_private::DataExtractor &data, lldb::offset_t data_offset) override
bool ParseOperands(llvm::SmallVectorImpl< Instruction::Operand > &operands) override
bool IsBarrier() override
bool HasDelaySlot() override
static std::pair< Operand, llvm::StringRef::const_iterator > ParseARMDerefAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
static std::pair< Operand, llvm::StringRef::const_iterator > ParseRegisterName(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
~InstructionLLVMC() override=default
lldb::InstructionControlFlowKind GetControlFlowKind(const lldb_private::ExecutionContext *exe_ctx) override
InstructionLLVMC(DisassemblerLLVMC &disasm, const lldb_private::Address &address, AddressClass addr_class)
static std::pair< Operand, llvm::StringRef::const_iterator > ParseImmediate(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
A section + offset based address range class.
bool ContainsLoadAddress(const Address &so_addr, Target *target) const
Check if a section offset so_addr when represented as a load address is contained within this object'...
A section + offset based address class.
Definition Address.h:62
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition Address.h:432
@ DumpStyleSectionNameOffset
Display as the section name + offset.
Definition Address.h:74
@ DumpStyleNoFunctionName
Elide the function name; display an offset into the current function.
Definition Address.h:109
@ DumpStyleResolvedDescriptionNoFunctionArguments
Definition Address.h:106
bool Slide(int64_t offset)
Definition Address.h:452
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Dump a description of this object to a Stream.
Definition Address.cpp:396
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition Address.cpp:273
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
AddressClass GetAddressClass() const
Definition Address.cpp:1020
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:457
uint32_t GetMinimumOpcodeByteSize() const
Definition ArchSpec.cpp:922
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition ArchSpec.cpp:673
@ eRISCV_float_abi_double
single precision floating point, +f
Definition ArchSpec.h:98
@ eRISCV_float_abi_quad
double precision floating point, +d
Definition ArchSpec.h:99
@ eRISCV_float_abi_single
soft float
Definition ArchSpec.h:97
uint32_t GetMaximumOpcodeByteSize() const
Definition ArchSpec.cpp:929
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
An data extractor class.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
virtual const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
virtual uint64_t GetByteSize() const
Get the number of bytes contained in this object.
virtual lldb::offset_t BytesLeft(lldb::offset_t offset) const
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
const uint8_t * GetDataStart() const
Get the data start pointer.
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
InstructionList m_instruction_list
Disassembler(const ArchSpec &arch, const char *flavor)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Instruction(const Address &address, AddressClass addr_class=AddressClass::eInvalid)
const char * GetOperands(const ExecutionContext *exe_ctx, bool markup=false)
uint32_t GetByteSize() const
Definition Opcode.h:231
const void * GetOpcodeBytes() const
Definition Opcode.h:225
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
Symbol * symbol
The Symbol for a given query.
Disassembler::HexImmediateStyle GetHexImmediateStyle() const
Definition Target.cpp:5136
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3319
const ArchSpec & GetArchitecture() const
Definition Target.h:1199
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Instruction > InstructionSP
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
ByteOrder
Byte ordering definitions.
InstructionControlFlowKind
Architecture-agnostic categorization of instructions for traversing the control flow of a trace.
@ eInstructionControlFlowKindReturn
The instruction is a near (function) return.
@ eInstructionControlFlowKindFarJump
The instruction is a jump-like far transfer.
@ eInstructionControlFlowKindOther
The instruction is something not listed below, i.e.
@ eInstructionControlFlowKindFarCall
The instruction is a call-like far transfer.
@ eInstructionControlFlowKindFarReturn
The instruction is a return-like far transfer.
@ eInstructionControlFlowKindUnknown
The instruction could not be classified.
@ eInstructionControlFlowKindJump
The instruction is a near unconditional jump.
@ eInstructionControlFlowKindCall
The instruction is a near (function) call.
@ eInstructionControlFlowKindCondJump
The instruction is a near conditional jump.
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
std::optional< InstructionOpcodeAndModrm > InstructionLengthDecode(const uint8_t *inst_bytes, int bytes_len, bool is_exec_mode_64b)
Decode an instruction into opcode, modrm and opcode_len.
lldb::InstructionControlFlowKind GetControlFlowKind(bool is_exec_mode_64b, Opcode m_opcode)
lldb::InstructionControlFlowKind MapOpcodeIntoControlFlowKind(InstructionOpcodeAndModrm opcode_and_modrm)
Determine the InstructionControlFlowKind based on opcode and modrm bytes.
enum lldb_private::Instruction::Operand::Type m_type
std::vector< Operand > m_children
These are the three values deciding instruction control flow kind.