1 /* Disassemble support for GDB.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "disasm-flags.h"
29 class gdb_disassembler
31 using di_read_memory_ftype
= decltype (disassemble_info::read_memory_func
);
34 gdb_disassembler (struct gdbarch
*gdbarch
, struct ui_file
*file
)
35 : gdb_disassembler (gdbarch
, file
, dis_asm_read_memory
)
40 DISABLE_COPY_AND_ASSIGN (gdb_disassembler
);
42 int print_insn (CORE_ADDR memaddr
, int *branch_delay_insns
= NULL
);
44 /* Return the gdbarch of gdb_disassembler. */
45 struct gdbarch
*arch ()
49 gdb_disassembler (struct gdbarch
*gdbarch
, struct ui_file
*file
,
50 di_read_memory_ftype func
);
52 struct ui_file
*stream ()
53 { return (struct ui_file
*) m_di
.stream
; }
56 struct gdbarch
*m_gdbarch
;
58 /* Stores data required for disassembling instructions in
60 struct disassemble_info m_di
;
62 /* If we own the string in `m_di.disassembler_options', we do so
64 std::string m_disassembler_options_holder
;
66 /* This member variable is given a value by calling dis_asm_memory_error.
67 If after calling into the libopcodes disassembler we get back a
68 negative value (which indicates an error), then, if this variable has
69 a value, we report a memory error to the user, otherwise, we report a
71 gdb::optional
<CORE_ADDR
> m_err_memaddr
;
73 /* Disassembler output is built up into this buffer. Whether this
74 string_file is created with styling support or not depends on the
75 value of use_ext_lang_colorization_p, as well as whether disassembler
76 styling in general is turned on, and also, whether *m_dest supports
80 /* The stream to which disassembler output will be written. */
83 /* When true, m_buffer will be created without styling support,
84 otherwise, m_buffer will be created with styling support.
86 This field will initially be true, but will be set to false if
87 ext_lang_colorize_disasm fails to add styling at any time.
89 If the extension language is going to add the styling then m_buffer
90 should be created without styling support, the extension language will
91 then add styling at the end of the disassembly process.
93 If the extension language is not going to add the styling, then we
94 create m_buffer with styling support, and GDB will add minimal styling
95 (currently just to addresses and symbols) as it goes. */
96 static bool use_ext_lang_colorization_p
;
98 static int dis_asm_fprintf (void *stream
, const char *format
, ...)
99 ATTRIBUTE_PRINTF(2,3);
101 /* Print formatted message to STREAM, the content can be styled based on
103 static int dis_asm_styled_fprintf (void *stream
,
104 enum disassembler_style style
,
105 const char *format
, ...)
106 ATTRIBUTE_PRINTF(3,4);
108 static int dis_asm_read_memory (bfd_vma memaddr
, gdb_byte
*myaddr
,
110 struct disassemble_info
*info
);
111 static void dis_asm_memory_error (int err
, bfd_vma memaddr
,
112 struct disassemble_info
*info
);
113 static void dis_asm_print_address (bfd_vma addr
,
114 struct disassemble_info
*info
);
117 /* An instruction to be disassembled. */
121 /* The address of the memory containing the instruction. */
124 /* An optional instruction number. If non-zero, it is printed first. */
127 /* True if the instruction was executed speculatively. */
128 unsigned int is_speculative
:1;
131 extern void gdb_disassembly (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
132 gdb_disassembly_flags flags
, int how_many
,
133 CORE_ADDR low
, CORE_ADDR high
);
135 /* Print the instruction at address MEMADDR in debugged memory,
136 on STREAM. Returns the length of the instruction, in bytes,
137 and, if requested, the number of branch delay slot instructions. */
139 extern int gdb_print_insn (struct gdbarch
*gdbarch
, CORE_ADDR memaddr
,
140 struct ui_file
*stream
, int *branch_delay_insns
);
142 /* Class used to pretty-print instructions. */
144 class gdb_pretty_print_disassembler
147 explicit gdb_pretty_print_disassembler (struct gdbarch
*gdbarch
,
148 struct ui_out
*uiout
)
150 m_insn_stb (uiout
->can_emit_style_escape ()),
151 m_di (gdbarch
, &m_insn_stb
)
154 /* Prints the instruction INSN into the saved ui_out and returns the
155 length of the printed instruction in bytes. */
156 int pretty_print_insn (const struct disasm_insn
*insn
,
157 gdb_disassembly_flags flags
);
160 /* Returns the architecture used for disassembling. */
161 struct gdbarch
*arch () { return m_di
.arch (); }
163 /* The ui_out that is used by pretty_print_insn. */
164 struct ui_out
*m_uiout
;
166 /* The buffer used to build the instruction string. The
167 disassembler is initialized with this stream. */
168 string_file m_insn_stb
;
170 /* The disassembler used for instruction printing. */
171 gdb_disassembler m_di
;
173 /* The buffer used to build the raw opcodes string. */
174 string_file m_opcode_stb
;
177 /* Return the length in bytes of the instruction at address MEMADDR in
180 extern int gdb_insn_length (struct gdbarch
*gdbarch
, CORE_ADDR memaddr
);
182 /* Return the length in bytes of INSN, originally at MEMADDR. MAX_LEN
183 is the size of the buffer containing INSN. */
185 extern int gdb_buffered_insn_length (struct gdbarch
*gdbarch
,
186 const gdb_byte
*insn
, int max_len
,
189 /* Returns GDBARCH's disassembler options. */
191 extern char *get_disassembler_options (struct gdbarch
*gdbarch
);
193 /* Sets the active gdbarch's disassembler options to OPTIONS. */
195 extern void set_disassembler_options (const char *options
);
197 /* Setup DINFO with its output function and output stream setup so that
198 nothing is printed while disassembling. */
200 extern void init_disassemble_info_for_no_printing
201 (struct disassemble_info
*dinfo
);