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