disasm.h revision 1.1.1.9 1 1.1 christos /* Disassemble support for GDB.
2 1.1.1.8 christos Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of GDB.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3 of the License, or
9 1.1 christos (at your option) any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 1.1 christos
19 1.1.1.9 christos #ifndef GDB_DISASM_H
20 1.1.1.9 christos #define GDB_DISASM_H
21 1.1 christos
22 1.1.1.2 christos #include "dis-asm.h"
23 1.1.1.7 christos #include "disasm-flags.h"
24 1.1.1.9 christos #include "ui-out.h"
25 1.1 christos
26 1.1.1.2 christos struct gdbarch;
27 1.1 christos struct ui_out;
28 1.1 christos struct ui_file;
29 1.1 christos
30 1.1.1.7 christos /* A wrapper around a disassemble_info and a gdbarch. This is the core
31 1.1.1.7 christos set of data that all disassembler sub-classes will need. This class
32 1.1.1.7 christos doesn't actually implement the disassembling process, that is something
33 1.1.1.7 christos that sub-classes will do, with each sub-class doing things slightly
34 1.1.1.7 christos differently.
35 1.1.1.7 christos
36 1.1.1.7 christos The constructor of this class is protected, you should not create
37 1.1.1.7 christos instances of this class directly, instead create an instance of an
38 1.1.1.7 christos appropriate sub-class. */
39 1.1.1.6 christos
40 1.1.1.7 christos struct gdb_disassemble_info
41 1.1.1.7 christos {
42 1.1.1.7 christos DISABLE_COPY_AND_ASSIGN (gdb_disassemble_info);
43 1.1.1.4 christos
44 1.1.1.7 christos /* Return the gdbarch we are disassembling for. */
45 1.1.1.4 christos struct gdbarch *arch ()
46 1.1.1.4 christos { return m_gdbarch; }
47 1.1.1.4 christos
48 1.1.1.7 christos /* Return a pointer to the disassemble_info, this will be needed for
49 1.1.1.7 christos passing into the libopcodes disassembler. */
50 1.1.1.7 christos struct disassemble_info *disasm_info ()
51 1.1.1.7 christos { return &m_di; }
52 1.1.1.7 christos
53 1.1.1.4 christos protected:
54 1.1.1.4 christos
55 1.1.1.7 christos /* Types for the function callbacks within m_di. The actual function
56 1.1.1.8 christos signatures here are taken from include/dis-asm.h. */
57 1.1.1.7 christos using read_memory_ftype
58 1.1.1.7 christos = int (*) (bfd_vma, bfd_byte *, unsigned int, struct disassemble_info *)
59 1.1.1.8 christos noexcept;
60 1.1.1.7 christos using memory_error_ftype
61 1.1.1.8 christos = void (*) (int, bfd_vma, struct disassemble_info *) noexcept;
62 1.1.1.7 christos using print_address_ftype
63 1.1.1.8 christos = void (*) (bfd_vma, struct disassemble_info *) noexcept;
64 1.1.1.7 christos using fprintf_ftype
65 1.1.1.8 christos = int (*) (void *, const char *, ...) noexcept;
66 1.1.1.7 christos using fprintf_styled_ftype
67 1.1.1.8 christos = int (*) (void *, enum disassembler_style, const char *, ...) noexcept;
68 1.1.1.7 christos
69 1.1.1.7 christos /* Constructor, many fields in m_di are initialized from GDBARCH. The
70 1.1.1.7 christos remaining arguments are function callbacks that are written into m_di.
71 1.1.1.7 christos Of these function callbacks FPRINTF_FUNC and FPRINTF_STYLED_FUNC must
72 1.1.1.7 christos not be nullptr. If READ_MEMORY_FUNC, MEMORY_ERROR_FUNC, or
73 1.1.1.7 christos PRINT_ADDRESS_FUNC are nullptr, then that field within m_di is left
74 1.1.1.7 christos with its default value (see the libopcodes function
75 1.1.1.7 christos init_disassemble_info for the defaults). */
76 1.1.1.7 christos gdb_disassemble_info (struct gdbarch *gdbarch,
77 1.1.1.7 christos read_memory_ftype read_memory_func,
78 1.1.1.7 christos memory_error_ftype memory_error_func,
79 1.1.1.7 christos print_address_ftype print_address_func,
80 1.1.1.7 christos fprintf_ftype fprintf_func,
81 1.1.1.7 christos fprintf_styled_ftype fprintf_styled_func);
82 1.1.1.4 christos
83 1.1.1.7 christos /* Destructor. */
84 1.1.1.7 christos virtual ~gdb_disassemble_info ();
85 1.1.1.4 christos
86 1.1.1.4 christos /* Stores data required for disassembling instructions in
87 1.1.1.4 christos opcodes. */
88 1.1.1.4 christos struct disassemble_info m_di;
89 1.1.1.5 christos
90 1.1.1.7 christos private:
91 1.1.1.7 christos /* The architecture we are disassembling for. */
92 1.1.1.7 christos struct gdbarch *m_gdbarch;
93 1.1.1.7 christos
94 1.1.1.5 christos /* If we own the string in `m_di.disassembler_options', we do so
95 1.1.1.5 christos using this field. */
96 1.1.1.5 christos std::string m_disassembler_options_holder;
97 1.1.1.7 christos };
98 1.1.1.7 christos
99 1.1.1.7 christos /* A wrapper around gdb_disassemble_info. This class adds default
100 1.1.1.7 christos print functions that are supplied to the disassemble_info within the
101 1.1.1.7 christos parent class. These default print functions write to the stream, which
102 1.1.1.7 christos is also contained in the parent class.
103 1.1.1.7 christos
104 1.1.1.7 christos As with the parent class, the constructor for this class is protected,
105 1.1.1.7 christos you should not create instances of this class, but create an
106 1.1.1.7 christos appropriate sub-class instead. */
107 1.1.1.7 christos
108 1.1.1.7 christos struct gdb_printing_disassembler : public gdb_disassemble_info
109 1.1.1.7 christos {
110 1.1.1.7 christos DISABLE_COPY_AND_ASSIGN (gdb_printing_disassembler);
111 1.1.1.5 christos
112 1.1.1.7 christos /* The stream that disassembler output is being written too. */
113 1.1.1.7 christos struct ui_file *stream ()
114 1.1.1.7 christos { return m_stream; }
115 1.1.1.7 christos
116 1.1.1.8 christos protected:
117 1.1.1.8 christos
118 1.1.1.7 christos /* Constructor. All the arguments are just passed to the parent class.
119 1.1.1.7 christos We also add the two print functions to the arguments passed to the
120 1.1.1.7 christos parent. See gdb_disassemble_info for a description of how the
121 1.1.1.7 christos arguments are handled. */
122 1.1.1.7 christos gdb_printing_disassembler (struct gdbarch *gdbarch,
123 1.1.1.7 christos struct ui_file *stream,
124 1.1.1.7 christos read_memory_ftype read_memory_func,
125 1.1.1.7 christos memory_error_ftype memory_error_func,
126 1.1.1.7 christos print_address_ftype print_address_func)
127 1.1.1.7 christos : gdb_disassemble_info (gdbarch, read_memory_func,
128 1.1.1.7 christos memory_error_func, print_address_func,
129 1.1.1.7 christos fprintf_func, fprintf_styled_func),
130 1.1.1.7 christos m_stream (stream)
131 1.1.1.7 christos {
132 1.1.1.7 christos gdb_assert (stream != nullptr);
133 1.1.1.7 christos }
134 1.1.1.7 christos
135 1.1.1.7 christos /* Callback used as the disassemble_info's fprintf_func callback. The
136 1.1.1.7 christos DIS_INFO pointer is a pointer to a gdb_printing_disassembler object.
137 1.1.1.7 christos Content is written to the m_stream extracted from DIS_INFO. */
138 1.1.1.7 christos static int fprintf_func (void *dis_info, const char *format, ...) noexcept
139 1.1.1.8 christos ATTRIBUTE_PRINTF (2, 3);
140 1.1.1.7 christos
141 1.1.1.7 christos /* Callback used as the disassemble_info's fprintf_styled_func callback.
142 1.1.1.7 christos The DIS_INFO pointer is a pointer to a gdb_printing_disassembler
143 1.1.1.7 christos object. Content is written to the m_stream extracted from DIS_INFO. */
144 1.1.1.7 christos static int fprintf_styled_func (void *dis_info,
145 1.1.1.7 christos enum disassembler_style style,
146 1.1.1.7 christos const char *format, ...) noexcept
147 1.1.1.7 christos ATTRIBUTE_PRINTF(3,4);
148 1.1.1.7 christos
149 1.1.1.7 christos /* Return true if the disassembler is considered inside a comment, false
150 1.1.1.7 christos otherwise. */
151 1.1.1.7 christos bool in_comment_p () const
152 1.1.1.7 christos { return m_in_comment; }
153 1.1.1.7 christos
154 1.1.1.7 christos /* Set whether the disassembler should be considered as within comment
155 1.1.1.7 christos text or not. */
156 1.1.1.7 christos void set_in_comment (bool c)
157 1.1.1.7 christos { m_in_comment = c; }
158 1.1.1.7 christos
159 1.1.1.7 christos private:
160 1.1.1.4 christos
161 1.1.1.7 christos /* When libopcodes calls the fprintf_func and fprintf_styled_func
162 1.1.1.7 christos callbacks, a 'void *' argument is passed. We arrange, through our
163 1.1.1.7 christos call to init_disassemble_info that this argument will be a pointer to
164 1.1.1.7 christos a gdb_disassemble_info sub-class, specifically, a
165 1.1.1.7 christos gdb_printing_disassembler pointer. This helper function casts
166 1.1.1.7 christos DIS_INFO to the correct type (with some asserts), and then returns the
167 1.1.1.7 christos m_stream member variable. */
168 1.1.1.7 christos static ui_file *stream_from_gdb_disassemble_info (void *dis_info);
169 1.1.1.7 christos
170 1.1.1.7 christos /* The stream to which output should be sent. */
171 1.1.1.7 christos struct ui_file *m_stream;
172 1.1.1.7 christos
173 1.1.1.7 christos /* Are we inside a comment? This will be set true if the disassembler
174 1.1.1.7 christos uses styled output and emits a start of comment character. It is up
175 1.1.1.7 christos to the code that uses this disassembler class to reset this flag back
176 1.1.1.7 christos to false at a suitable time (e.g. at the end of every line). */
177 1.1.1.7 christos bool m_in_comment = false;
178 1.1.1.7 christos };
179 1.1.1.7 christos
180 1.1.1.7 christos /* A basic disassembler that doesn't actually print anything. */
181 1.1.1.7 christos
182 1.1.1.7 christos struct gdb_non_printing_disassembler : public gdb_disassemble_info
183 1.1.1.7 christos {
184 1.1.1.7 christos gdb_non_printing_disassembler (struct gdbarch *gdbarch,
185 1.1.1.7 christos read_memory_ftype read_memory_func)
186 1.1.1.7 christos : gdb_disassemble_info (gdbarch,
187 1.1.1.7 christos read_memory_func,
188 1.1.1.7 christos nullptr /* memory_error_func */,
189 1.1.1.7 christos nullptr /* print_address_func */,
190 1.1.1.7 christos null_fprintf_func,
191 1.1.1.7 christos null_fprintf_styled_func)
192 1.1.1.7 christos { /* Nothing. */ }
193 1.1.1.7 christos
194 1.1.1.7 christos private:
195 1.1.1.7 christos
196 1.1.1.7 christos /* Callback used as the disassemble_info's fprintf_func callback, this
197 1.1.1.7 christos doesn't write anything to STREAM, but just returns 0. */
198 1.1.1.7 christos static int null_fprintf_func (void *stream, const char *format, ...) noexcept
199 1.1.1.7 christos ATTRIBUTE_PRINTF(2,3);
200 1.1.1.7 christos
201 1.1.1.7 christos /* Callback used as the disassemble_info's fprintf_styled_func callback,
202 1.1.1.7 christos , this doesn't write anything to STREAM, but just returns 0. */
203 1.1.1.7 christos static int null_fprintf_styled_func (void *stream,
204 1.1.1.7 christos enum disassembler_style style,
205 1.1.1.7 christos const char *format, ...) noexcept
206 1.1.1.7 christos ATTRIBUTE_PRINTF(3,4);
207 1.1.1.7 christos };
208 1.1.1.7 christos
209 1.1.1.7 christos /* This is a helper class, for use as an additional base-class, by some of
210 1.1.1.7 christos the disassembler classes below. This class just defines a static method
211 1.1.1.7 christos for reading from target memory, which can then be used by the various
212 1.1.1.7 christos disassembler sub-classes. */
213 1.1.1.7 christos
214 1.1.1.7 christos struct gdb_disassembler_memory_reader
215 1.1.1.7 christos {
216 1.1.1.7 christos /* Implements the read_memory_func disassemble_info callback. */
217 1.1.1.4 christos static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
218 1.1.1.4 christos unsigned int len,
219 1.1.1.7 christos struct disassemble_info *info) noexcept;
220 1.1.1.7 christos };
221 1.1.1.7 christos
222 1.1.1.7 christos /* A non-printing disassemble_info management class. The disassemble_info
223 1.1.1.7 christos setup by this class will not print anything to the output stream (there
224 1.1.1.7 christos is no output stream), and the instruction to be disassembled will be
225 1.1.1.7 christos read from target memory. */
226 1.1.1.7 christos
227 1.1.1.7 christos struct gdb_non_printing_memory_disassembler
228 1.1.1.7 christos : public gdb_non_printing_disassembler,
229 1.1.1.7 christos private gdb_disassembler_memory_reader
230 1.1.1.7 christos {
231 1.1.1.7 christos /* Constructor. GDBARCH is the architecture to disassemble for. */
232 1.1.1.7 christos gdb_non_printing_memory_disassembler (struct gdbarch *gdbarch)
233 1.1.1.7 christos :gdb_non_printing_disassembler (gdbarch, dis_asm_read_memory)
234 1.1.1.7 christos { /* Nothing. */ }
235 1.1.1.7 christos };
236 1.1.1.7 christos
237 1.1.1.8 christos /* A disassembler class that provides 'print_insn', a method for
238 1.1.1.7 christos disassembling a single instruction to the output stream. */
239 1.1.1.7 christos
240 1.1.1.7 christos struct gdb_disassembler : public gdb_printing_disassembler,
241 1.1.1.7 christos private gdb_disassembler_memory_reader
242 1.1.1.7 christos {
243 1.1.1.7 christos gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file)
244 1.1.1.7 christos : gdb_disassembler (gdbarch, file, dis_asm_read_memory)
245 1.1.1.7 christos { /* Nothing. */ }
246 1.1.1.7 christos
247 1.1.1.7 christos DISABLE_COPY_AND_ASSIGN (gdb_disassembler);
248 1.1.1.7 christos
249 1.1.1.7 christos /* Disassemble a single instruction at MEMADDR to the ui_file* that was
250 1.1.1.7 christos passed to the constructor. If a memory error occurs while
251 1.1.1.7 christos disassembling this instruction then an error will be thrown. */
252 1.1.1.7 christos int print_insn (CORE_ADDR memaddr, int *branch_delay_insns = NULL);
253 1.1.1.7 christos
254 1.1.1.7 christos protected:
255 1.1.1.7 christos gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file,
256 1.1.1.7 christos read_memory_ftype func);
257 1.1.1.7 christos
258 1.1.1.7 christos private:
259 1.1.1.7 christos /* This member variable is given a value by calling dis_asm_memory_error.
260 1.1.1.7 christos If after calling into the libopcodes disassembler we get back a
261 1.1.1.7 christos negative value (which indicates an error), then, if this variable has
262 1.1.1.7 christos a value, we report a memory error to the user, otherwise, we report a
263 1.1.1.7 christos non-memory error. */
264 1.1.1.8 christos std::optional<CORE_ADDR> m_err_memaddr;
265 1.1.1.7 christos
266 1.1.1.7 christos /* The stream to which disassembler output will be written. */
267 1.1.1.7 christos ui_file *m_dest;
268 1.1.1.7 christos
269 1.1.1.7 christos /* Disassembler output is built up into this buffer. Whether this
270 1.1.1.7 christos string_file is created with styling support or not depends on the
271 1.1.1.7 christos value of use_ext_lang_colorization_p, as well as whether disassembler
272 1.1.1.7 christos styling in general is turned on, and also, whether *m_dest supports
273 1.1.1.7 christos styling or not. */
274 1.1.1.7 christos string_file m_buffer;
275 1.1.1.7 christos
276 1.1.1.7 christos /* When true, m_buffer will be created without styling support,
277 1.1.1.7 christos otherwise, m_buffer will be created with styling support.
278 1.1.1.7 christos
279 1.1.1.7 christos This field will initially be true, but will be set to false if
280 1.1.1.7 christos ext_lang_colorize_disasm fails to add styling at any time.
281 1.1.1.7 christos
282 1.1.1.7 christos If the extension language is going to add the styling then m_buffer
283 1.1.1.7 christos should be created without styling support, the extension language will
284 1.1.1.7 christos then add styling at the end of the disassembly process.
285 1.1.1.7 christos
286 1.1.1.7 christos If the extension language is not going to add the styling, then we
287 1.1.1.7 christos create m_buffer with styling support, and GDB will add minimal styling
288 1.1.1.7 christos (currently just to addresses and symbols) as it goes. */
289 1.1.1.7 christos static bool use_ext_lang_colorization_p;
290 1.1.1.7 christos
291 1.1.1.4 christos static void dis_asm_memory_error (int err, bfd_vma memaddr,
292 1.1.1.7 christos struct disassemble_info *info) noexcept;
293 1.1.1.4 christos static void dis_asm_print_address (bfd_vma addr,
294 1.1.1.7 christos struct disassemble_info *info) noexcept;
295 1.1.1.7 christos
296 1.1.1.7 christos /* Return true if we should use the extension language to apply
297 1.1.1.7 christos disassembler styling. This requires disassembler styling to be on
298 1.1.1.7 christos (i.e. 'set style disassembler enabled on'), the output stream needs to
299 1.1.1.7 christos support styling, and libopcode styling needs to be either off, or not
300 1.1.1.7 christos supported for the current architecture (libopcodes is used in
301 1.1.1.7 christos preference to the extension language method). */
302 1.1.1.7 christos bool use_ext_lang_for_styling () const;
303 1.1.1.7 christos
304 1.1.1.7 christos /* Return true if we should use libopcodes to apply disassembler styling.
305 1.1.1.7 christos This requires disassembler styling to be on (i.e. 'set style
306 1.1.1.7 christos disassembler enabled on'), the output stream needs to support styling,
307 1.1.1.7 christos and libopcodes styling needs to be supported for the current
308 1.1.1.7 christos architecture, and not disabled by the user. */
309 1.1.1.7 christos bool use_libopcodes_for_styling () const;
310 1.1.1.4 christos };
311 1.1.1.4 christos
312 1.1.1.3 christos /* An instruction to be disassembled. */
313 1.1.1.3 christos
314 1.1.1.3 christos struct disasm_insn
315 1.1.1.3 christos {
316 1.1.1.3 christos /* The address of the memory containing the instruction. */
317 1.1.1.3 christos CORE_ADDR addr;
318 1.1.1.3 christos
319 1.1.1.3 christos /* An optional instruction number. If non-zero, it is printed first. */
320 1.1.1.3 christos unsigned int number;
321 1.1.1.3 christos
322 1.1.1.3 christos /* True if the instruction was executed speculatively. */
323 1.1.1.3 christos unsigned int is_speculative:1;
324 1.1.1.3 christos };
325 1.1.1.3 christos
326 1.1 christos extern void gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
327 1.1.1.5 christos gdb_disassembly_flags flags, int how_many,
328 1.1 christos CORE_ADDR low, CORE_ADDR high);
329 1.1 christos
330 1.1 christos /* Print the instruction at address MEMADDR in debugged memory,
331 1.1 christos on STREAM. Returns the length of the instruction, in bytes,
332 1.1 christos and, if requested, the number of branch delay slot instructions. */
333 1.1 christos
334 1.1 christos extern int gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
335 1.1 christos struct ui_file *stream, int *branch_delay_insns);
336 1.1 christos
337 1.1.1.4 christos /* Class used to pretty-print instructions. */
338 1.1.1.4 christos
339 1.1.1.4 christos class gdb_pretty_print_disassembler
340 1.1.1.4 christos {
341 1.1.1.4 christos public:
342 1.1.1.6 christos explicit gdb_pretty_print_disassembler (struct gdbarch *gdbarch,
343 1.1.1.6 christos struct ui_out *uiout)
344 1.1.1.6 christos : m_uiout (uiout),
345 1.1.1.6 christos m_insn_stb (uiout->can_emit_style_escape ()),
346 1.1.1.6 christos m_di (gdbarch, &m_insn_stb)
347 1.1.1.4 christos {}
348 1.1.1.4 christos
349 1.1.1.6 christos /* Prints the instruction INSN into the saved ui_out and returns the
350 1.1.1.6 christos length of the printed instruction in bytes. */
351 1.1.1.6 christos int pretty_print_insn (const struct disasm_insn *insn,
352 1.1.1.5 christos gdb_disassembly_flags flags);
353 1.1.1.4 christos
354 1.1.1.4 christos private:
355 1.1.1.4 christos /* Returns the architecture used for disassembling. */
356 1.1.1.4 christos struct gdbarch *arch () { return m_di.arch (); }
357 1.1.1.4 christos
358 1.1.1.6 christos /* The ui_out that is used by pretty_print_insn. */
359 1.1.1.6 christos struct ui_out *m_uiout;
360 1.1.1.4 christos
361 1.1.1.4 christos /* The buffer used to build the instruction string. The
362 1.1.1.4 christos disassembler is initialized with this stream. */
363 1.1.1.4 christos string_file m_insn_stb;
364 1.1.1.4 christos
365 1.1.1.6 christos /* The disassembler used for instruction printing. */
366 1.1.1.6 christos gdb_disassembler m_di;
367 1.1.1.6 christos
368 1.1.1.4 christos /* The buffer used to build the raw opcodes string. */
369 1.1.1.4 christos string_file m_opcode_stb;
370 1.1.1.7 christos
371 1.1.1.7 christos /* The buffer used to hold the opcode bytes (if required). */
372 1.1.1.7 christos gdb::byte_vector m_opcode_data;
373 1.1.1.4 christos };
374 1.1.1.4 christos
375 1.1 christos /* Return the length in bytes of the instruction at address MEMADDR in
376 1.1 christos debugged memory. */
377 1.1 christos
378 1.1 christos extern int gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR memaddr);
379 1.1 christos
380 1.1 christos /* Return the length in bytes of INSN, originally at MEMADDR. MAX_LEN
381 1.1 christos is the size of the buffer containing INSN. */
382 1.1 christos
383 1.1 christos extern int gdb_buffered_insn_length (struct gdbarch *gdbarch,
384 1.1 christos const gdb_byte *insn, int max_len,
385 1.1 christos CORE_ADDR memaddr);
386 1.1 christos
387 1.1.1.4 christos /* Returns GDBARCH's disassembler options. */
388 1.1.1.4 christos
389 1.1.1.8 christos extern const char *get_disassembler_options (struct gdbarch *gdbarch);
390 1.1.1.4 christos
391 1.1.1.4 christos /* Sets the active gdbarch's disassembler options to OPTIONS. */
392 1.1.1.4 christos
393 1.1.1.7 christos extern void set_disassembler_options (const char *options);
394 1.1.1.4 christos
395 1.1.1.9 christos #endif /* GDB_DISASM_H */
396