disasm.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Disassemble support for GDB.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef DISASM_H
  15. #define DISASM_H
  16. #include "dis-asm.h"
  17. #include "disasm-flags.h"
  18. struct gdbarch;
  19. struct ui_out;
  20. struct ui_file;
  21. class gdb_disassembler
  22. {
  23. using di_read_memory_ftype = decltype (disassemble_info::read_memory_func);
  24. public:
  25. gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file)
  26. : gdb_disassembler (gdbarch, file, dis_asm_read_memory)
  27. {}
  28. ~gdb_disassembler ();
  29. DISABLE_COPY_AND_ASSIGN (gdb_disassembler);
  30. int print_insn (CORE_ADDR memaddr, int *branch_delay_insns = NULL);
  31. /* Return the gdbarch of gdb_disassembler. */
  32. struct gdbarch *arch ()
  33. { return m_gdbarch; }
  34. protected:
  35. gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file,
  36. di_read_memory_ftype func);
  37. struct ui_file *stream ()
  38. { return (struct ui_file *) m_di.stream; }
  39. private:
  40. struct gdbarch *m_gdbarch;
  41. /* Stores data required for disassembling instructions in
  42. opcodes. */
  43. struct disassemble_info m_di;
  44. /* If we own the string in `m_di.disassembler_options', we do so
  45. using this field. */
  46. std::string m_disassembler_options_holder;
  47. /* This member variable is given a value by calling dis_asm_memory_error.
  48. If after calling into the libopcodes disassembler we get back a
  49. negative value (which indicates an error), then, if this variable has
  50. a value, we report a memory error to the user, otherwise, we report a
  51. non-memory error. */
  52. gdb::optional<CORE_ADDR> m_err_memaddr;
  53. /* Disassembler output is built up into this buffer. Whether this
  54. string_file is created with styling support or not depends on the
  55. value of use_ext_lang_colorization_p, as well as whether disassembler
  56. styling in general is turned on, and also, whether *m_dest supports
  57. styling or not. */
  58. string_file m_buffer;
  59. /* The stream to which disassembler output will be written. */
  60. ui_file *m_dest;
  61. /* When true, m_buffer will be created without styling support,
  62. otherwise, m_buffer will be created with styling support.
  63. This field will initially be true, but will be set to false if
  64. ext_lang_colorize_disasm fails to add styling at any time.
  65. If the extension language is going to add the styling then m_buffer
  66. should be created without styling support, the extension language will
  67. then add styling at the end of the disassembly process.
  68. If the extension language is not going to add the styling, then we
  69. create m_buffer with styling support, and GDB will add minimal styling
  70. (currently just to addresses and symbols) as it goes. */
  71. static bool use_ext_lang_colorization_p;
  72. static int dis_asm_fprintf (void *stream, const char *format, ...)
  73. ATTRIBUTE_PRINTF(2,3);
  74. /* Print formatted message to STREAM, the content can be styled based on
  75. STYLE if desired. */
  76. static int dis_asm_styled_fprintf (void *stream,
  77. enum disassembler_style style,
  78. const char *format, ...)
  79. ATTRIBUTE_PRINTF(3,4);
  80. static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
  81. unsigned int len,
  82. struct disassemble_info *info);
  83. static void dis_asm_memory_error (int err, bfd_vma memaddr,
  84. struct disassemble_info *info);
  85. static void dis_asm_print_address (bfd_vma addr,
  86. struct disassemble_info *info);
  87. };
  88. /* An instruction to be disassembled. */
  89. struct disasm_insn
  90. {
  91. /* The address of the memory containing the instruction. */
  92. CORE_ADDR addr;
  93. /* An optional instruction number. If non-zero, it is printed first. */
  94. unsigned int number;
  95. /* True if the instruction was executed speculatively. */
  96. unsigned int is_speculative:1;
  97. };
  98. extern void gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
  99. gdb_disassembly_flags flags, int how_many,
  100. CORE_ADDR low, CORE_ADDR high);
  101. /* Print the instruction at address MEMADDR in debugged memory,
  102. on STREAM. Returns the length of the instruction, in bytes,
  103. and, if requested, the number of branch delay slot instructions. */
  104. extern int gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
  105. struct ui_file *stream, int *branch_delay_insns);
  106. /* Class used to pretty-print instructions. */
  107. class gdb_pretty_print_disassembler
  108. {
  109. public:
  110. explicit gdb_pretty_print_disassembler (struct gdbarch *gdbarch,
  111. struct ui_out *uiout)
  112. : m_uiout (uiout),
  113. m_insn_stb (uiout->can_emit_style_escape ()),
  114. m_di (gdbarch, &m_insn_stb)
  115. {}
  116. /* Prints the instruction INSN into the saved ui_out and returns the
  117. length of the printed instruction in bytes. */
  118. int pretty_print_insn (const struct disasm_insn *insn,
  119. gdb_disassembly_flags flags);
  120. private:
  121. /* Returns the architecture used for disassembling. */
  122. struct gdbarch *arch () { return m_di.arch (); }
  123. /* The ui_out that is used by pretty_print_insn. */
  124. struct ui_out *m_uiout;
  125. /* The buffer used to build the instruction string. The
  126. disassembler is initialized with this stream. */
  127. string_file m_insn_stb;
  128. /* The disassembler used for instruction printing. */
  129. gdb_disassembler m_di;
  130. /* The buffer used to build the raw opcodes string. */
  131. string_file m_opcode_stb;
  132. };
  133. /* Return the length in bytes of the instruction at address MEMADDR in
  134. debugged memory. */
  135. extern int gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR memaddr);
  136. /* Return the length in bytes of INSN, originally at MEMADDR. MAX_LEN
  137. is the size of the buffer containing INSN. */
  138. extern int gdb_buffered_insn_length (struct gdbarch *gdbarch,
  139. const gdb_byte *insn, int max_len,
  140. CORE_ADDR memaddr);
  141. /* Returns GDBARCH's disassembler options. */
  142. extern char *get_disassembler_options (struct gdbarch *gdbarch);
  143. /* Sets the active gdbarch's disassembler options to OPTIONS. */
  144. extern void set_disassembler_options (const char *options);
  145. /* Setup DINFO with its output function and output stream setup so that
  146. nothing is printed while disassembling. */
  147. extern void init_disassemble_info_for_no_printing
  148. (struct disassemble_info *dinfo);
  149. #endif