gdb: LoongArch: Define LOONGARCH_LINUX_NUM_GREGSET as 45
[binutils-gdb.git] / gdb / disasm.h
1 /* Disassemble support for GDB.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19 #ifndef DISASM_H
20 #define DISASM_H
21
22 #include "dis-asm.h"
23 #include "disasm-flags.h"
24
25 struct gdbarch;
26 struct ui_out;
27 struct ui_file;
28
29 class gdb_disassembler
30 {
31 using di_read_memory_ftype = decltype (disassemble_info::read_memory_func);
32
33 public:
34 gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file)
35 : gdb_disassembler (gdbarch, file, dis_asm_read_memory)
36 {}
37
38 ~gdb_disassembler ();
39
40 DISABLE_COPY_AND_ASSIGN (gdb_disassembler);
41
42 int print_insn (CORE_ADDR memaddr, int *branch_delay_insns = NULL);
43
44 /* Return the gdbarch of gdb_disassembler. */
45 struct gdbarch *arch ()
46 { return m_gdbarch; }
47
48 protected:
49 gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file,
50 di_read_memory_ftype func);
51
52 struct ui_file *stream ()
53 { return (struct ui_file *) m_di.stream; }
54
55 private:
56 struct gdbarch *m_gdbarch;
57
58 /* Stores data required for disassembling instructions in
59 opcodes. */
60 struct disassemble_info m_di;
61
62 /* If we own the string in `m_di.disassembler_options', we do so
63 using this field. */
64 std::string m_disassembler_options_holder;
65
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
70 non-memory error. */
71 gdb::optional<CORE_ADDR> m_err_memaddr;
72
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
77 styling or not. */
78 string_file m_buffer;
79
80 /* The stream to which disassembler output will be written. */
81 ui_file *m_dest;
82
83 /* When true, m_buffer will be created without styling support,
84 otherwise, m_buffer will be created with styling support.
85
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.
88
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.
92
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;
97
98 static int dis_asm_fprintf (void *stream, const char *format, ...)
99 ATTRIBUTE_PRINTF(2,3);
100
101 /* Print formatted message to STREAM, the content can be styled based on
102 STYLE if desired. */
103 static int dis_asm_styled_fprintf (void *stream,
104 enum disassembler_style style,
105 const char *format, ...)
106 ATTRIBUTE_PRINTF(3,4);
107
108 static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
109 unsigned int len,
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);
115 };
116
117 /* An instruction to be disassembled. */
118
119 struct disasm_insn
120 {
121 /* The address of the memory containing the instruction. */
122 CORE_ADDR addr;
123
124 /* An optional instruction number. If non-zero, it is printed first. */
125 unsigned int number;
126
127 /* True if the instruction was executed speculatively. */
128 unsigned int is_speculative:1;
129 };
130
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);
134
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. */
138
139 extern int gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
140 struct ui_file *stream, int *branch_delay_insns);
141
142 /* Class used to pretty-print instructions. */
143
144 class gdb_pretty_print_disassembler
145 {
146 public:
147 explicit gdb_pretty_print_disassembler (struct gdbarch *gdbarch,
148 struct ui_out *uiout)
149 : m_uiout (uiout),
150 m_insn_stb (uiout->can_emit_style_escape ()),
151 m_di (gdbarch, &m_insn_stb)
152 {}
153
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);
158
159 private:
160 /* Returns the architecture used for disassembling. */
161 struct gdbarch *arch () { return m_di.arch (); }
162
163 /* The ui_out that is used by pretty_print_insn. */
164 struct ui_out *m_uiout;
165
166 /* The buffer used to build the instruction string. The
167 disassembler is initialized with this stream. */
168 string_file m_insn_stb;
169
170 /* The disassembler used for instruction printing. */
171 gdb_disassembler m_di;
172
173 /* The buffer used to build the raw opcodes string. */
174 string_file m_opcode_stb;
175 };
176
177 /* Return the length in bytes of the instruction at address MEMADDR in
178 debugged memory. */
179
180 extern int gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR memaddr);
181
182 /* Return the length in bytes of INSN, originally at MEMADDR. MAX_LEN
183 is the size of the buffer containing INSN. */
184
185 extern int gdb_buffered_insn_length (struct gdbarch *gdbarch,
186 const gdb_byte *insn, int max_len,
187 CORE_ADDR memaddr);
188
189 /* Returns GDBARCH's disassembler options. */
190
191 extern char *get_disassembler_options (struct gdbarch *gdbarch);
192
193 /* Sets the active gdbarch's disassembler options to OPTIONS. */
194
195 extern void set_disassembler_options (const char *options);
196
197 /* Setup DINFO with its output function and output stream setup so that
198 nothing is printed while disassembling. */
199
200 extern void init_disassemble_info_for_no_printing
201 (struct disassemble_info *dinfo);
202
203 #endif