]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/disasm.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / disasm.c
CommitLineData
92df71f0 1/* Disassemble support for GDB.
1bac305b 2
1d506c26 3 Copyright (C) 2000-2024 Free Software Foundation, Inc.
92df71f0
FN
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
92df71f0
FN
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
92df71f0 19
65b48a81 20#include "arch-utils.h"
e5dc0d5d 21#include "event-top.h"
4de283e4
TT
22#include "target.h"
23#include "value.h"
24#include "ui-out.h"
92df71f0 25#include "disasm.h"
d55e5aa6 26#include "gdbcore.h"
5b9707eb 27#include "cli/cli-cmds.h"
4de283e4 28#include "dis-asm.h"
d55e5aa6 29#include "source.h"
e0f4b3ec 30#include "gdbsupport/gdb-safe-ctype.h"
4de283e4 31#include <algorithm>
6b09f134 32#include <optional>
c7110220 33#include "valprint.h"
e43b10e1 34#include "cli/cli-style.h"
1acc9dca 35#include "objfiles.h"
99d9c3b9 36#include "inferior.h"
92df71f0
FN
37
38/* Disassemble functions.
39 FIXME: We should get rid of all the duplicate code in gdb that does
0963b4bd 40 the same thing: disassemble_command() and the gdbtk variation. */
92df71f0 41
65b48a81
PB
42/* This variable is used to hold the prospective disassembler_options value
43 which is set by the "set disassembler_options" command. */
e0700ba4 44static std::string prospective_options;
65b48a81 45
4cbe4ca5
AB
46/* When this is true we will try to use libopcodes to provide styling to
47 the disassembler output. */
48
49static bool use_libopcodes_styling = true;
50
51/* To support the set_use_libopcodes_styling function we have a second
52 variable which is connected to the actual set/show option. */
53
54static bool use_libopcodes_styling_option = use_libopcodes_styling;
55
56/* The "maint show libopcodes-styling enabled" command. */
57
58static void
59show_use_libopcodes_styling (struct ui_file *file, int from_tty,
60 struct cmd_list_element *c,
61 const char *value)
62{
99d9c3b9
SM
63 gdbarch *arch = current_inferior ()->arch ();
64 gdb_non_printing_memory_disassembler dis (arch);
4cbe4ca5
AB
65 bool supported = dis.disasm_info ()->created_styled_output;
66
67 if (supported || !use_libopcodes_styling)
68 gdb_printf (file, _("Use of libopcodes styling support is \"%s\".\n"),
69 value);
70 else
71 {
72 /* Use of libopcodes styling is not supported, and the user has this
73 turned on! */
74 gdb_printf (file, _("Use of libopcodes styling support is \"off\""
75 " (not supported on architecture \"%s\")\n"),
99d9c3b9 76 gdbarch_bfd_arch_info (arch)->printable_name);
4cbe4ca5
AB
77 }
78}
79
80/* The "maint set libopcodes-styling enabled" command. */
81
82static void
83set_use_libopcodes_styling (const char *args, int from_tty,
84 struct cmd_list_element *c)
85{
99d9c3b9
SM
86 gdbarch *arch = current_inferior ()->arch ();
87 gdb_non_printing_memory_disassembler dis (arch);
4cbe4ca5
AB
88 bool supported = dis.disasm_info ()->created_styled_output;
89
90 /* If the current architecture doesn't support libopcodes styling then we
91 give an error here, but leave the underlying setting enabled. This
92 means that if the user switches to an architecture that does support
93 libopcodes styling the setting will be enabled. */
94
95 if (use_libopcodes_styling_option && !supported)
96 {
97 use_libopcodes_styling_option = use_libopcodes_styling;
98 error (_("Use of libopcodes styling not supported on architecture \"%s\"."),
99d9c3b9 99 gdbarch_bfd_arch_info (arch)->printable_name);
4cbe4ca5
AB
100 }
101 else
102 use_libopcodes_styling = use_libopcodes_styling_option;
103}
104
6ff0ba5f
DE
105/* This structure is used to store line number information for the
106 deprecated /m option.
92df71f0
FN
107 We need a different sort of line table from the normal one cuz we can't
108 depend upon implicit line-end pc's for lines to do the
109 reordering in this function. */
110
6ff0ba5f 111struct deprecated_dis_line_entry
92df71f0
FN
112{
113 int line;
114 CORE_ADDR start_pc;
115 CORE_ADDR end_pc;
116};
117
6ff0ba5f
DE
118/* This Structure is used to store line number information.
119 We need a different sort of line table from the normal one cuz we can't
120 depend upon implicit line-end pc's for lines to do the
121 reordering in this function. */
122
123struct dis_line_entry
124{
125 struct symtab *symtab;
126 int line;
127};
128
129/* Hash function for dis_line_entry. */
130
131static hashval_t
132hash_dis_line_entry (const void *item)
133{
9a3c8263 134 const struct dis_line_entry *dle = (const struct dis_line_entry *) item;
6ff0ba5f
DE
135
136 return htab_hash_pointer (dle->symtab) + dle->line;
137}
138
139/* Equal function for dis_line_entry. */
140
141static int
142eq_dis_line_entry (const void *item_lhs, const void *item_rhs)
143{
9a3c8263
SM
144 const struct dis_line_entry *lhs = (const struct dis_line_entry *) item_lhs;
145 const struct dis_line_entry *rhs = (const struct dis_line_entry *) item_rhs;
6ff0ba5f
DE
146
147 return (lhs->symtab == rhs->symtab
148 && lhs->line == rhs->line);
149}
150
151/* Create the table to manage lines for mixed source/disassembly. */
152
153static htab_t
154allocate_dis_line_table (void)
155{
156 return htab_create_alloc (41,
157 hash_dis_line_entry, eq_dis_line_entry,
158 xfree, xcalloc, xfree);
159}
160
4a099de2 161/* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
6ff0ba5f
DE
162
163static void
4a099de2 164add_dis_line_entry (htab_t table, struct symtab *symtab, int line)
6ff0ba5f
DE
165{
166 void **slot;
167 struct dis_line_entry dle, *dlep;
168
169 dle.symtab = symtab;
170 dle.line = line;
171 slot = htab_find_slot (table, &dle, INSERT);
172 if (*slot == NULL)
173 {
174 dlep = XNEW (struct dis_line_entry);
175 dlep->symtab = symtab;
176 dlep->line = line;
177 *slot = dlep;
178 }
179}
180
181/* Return non-zero if SYMTAB, LINE are in TABLE. */
182
183static int
184line_has_code_p (htab_t table, struct symtab *symtab, int line)
185{
186 struct dis_line_entry dle;
187
188 dle.symtab = symtab;
189 dle.line = line;
190 return htab_find (table, &dle) != NULL;
191}
192
e47ad6c0
YQ
193/* Wrapper of target_read_code. */
194
195int
75033d08
AB
196gdb_disassembler_memory_reader::dis_asm_read_memory
197 (bfd_vma memaddr, gdb_byte *myaddr, unsigned int len,
8eb7d135 198 struct disassemble_info *info) noexcept
810ecf9f 199{
283f7163 200 return target_read_code (memaddr, myaddr, len);
810ecf9f
AC
201}
202
e47ad6c0
YQ
203/* Wrapper of memory_error. */
204
205void
8eb7d135
AB
206gdb_disassembler::dis_asm_memory_error
207 (int err, bfd_vma memaddr, struct disassemble_info *info) noexcept
810ecf9f 208{
d8b49cf0
YQ
209 gdb_disassembler *self
210 = static_cast<gdb_disassembler *>(info->application_data);
211
76b43c9b 212 self->m_err_memaddr.emplace (memaddr);
810ecf9f
AC
213}
214
e47ad6c0
YQ
215/* Wrapper of print_address. */
216
217void
8eb7d135
AB
218gdb_disassembler::dis_asm_print_address
219 (bfd_vma addr, struct disassemble_info *info) noexcept
810ecf9f 220{
e47ad6c0
YQ
221 gdb_disassembler *self
222 = static_cast<gdb_disassembler *>(info->application_data);
9a619af0 223
4cbe4ca5
AB
224 if (self->in_comment_p ())
225 {
226 /* Calling 'print_address' might add styling to the output (based on
227 the properties of the stream we're writing too). This is usually
228 fine, but if we are in an assembler comment then we'd prefer to
229 have the comment style, rather than the default address style.
230
231 Print the address into a temporary buffer which doesn't support
232 styling, then reprint this unstyled address with the default text
233 style.
234
235 As we are inside a comment right now, the standard print routine
236 will ensure that the comment is printed to the user with a
237 suitable comment style. */
238 string_file tmp;
239 print_address (self->arch (), addr, &tmp);
240 self->fprintf_styled_func (self, dis_style_text, "%s", tmp.c_str ());
241 }
242 else
243 print_address (self->arch (), addr, self->stream ());
810ecf9f
AC
244}
245
81384924
AB
246/* See disasm.h. */
247
248ui_file *
249gdb_printing_disassembler::stream_from_gdb_disassemble_info (void *dis_info)
250{
251 gdb_disassemble_info *di = (gdb_disassemble_info *) dis_info;
252 gdb_printing_disassembler *dis
5f48d886 253 = gdb::checked_static_cast<gdb_printing_disassembler *> (di);
81384924
AB
254 ui_file *stream = dis->stream ();
255 gdb_assert (stream != nullptr);
256 return stream;
257}
258
431be556
AB
259/* Format disassembler output to STREAM. */
260
261int
81384924 262gdb_printing_disassembler::fprintf_func (void *dis_info,
8eb7d135 263 const char *format, ...) noexcept
431be556 264{
81384924 265 ui_file *stream = stream_from_gdb_disassemble_info (dis_info);
431be556 266
81384924 267 va_list args;
431be556 268 va_start (args, format);
81384924 269 gdb_vprintf (stream, format, args);
431be556 270 va_end (args);
81384924 271
431be556
AB
272 /* Something non -ve. */
273 return 0;
274}
275
60a3da00
AB
276/* See disasm.h. */
277
278int
8eb7d135
AB
279gdb_printing_disassembler::fprintf_styled_func
280 (void *dis_info, enum disassembler_style style,
281 const char *format, ...) noexcept
60a3da00 282{
81384924 283 ui_file *stream = stream_from_gdb_disassemble_info (dis_info);
4cbe4ca5 284 gdb_printing_disassembler *dis = (gdb_printing_disassembler *) dis_info;
60a3da00 285
81384924 286 va_list args;
60a3da00 287 va_start (args, format);
4cbe4ca5 288 std::string content = string_vprintf (format, args);
60a3da00 289 va_end (args);
4cbe4ca5
AB
290
291 /* Once in a comment then everything should be styled as a comment. */
292 if (style == dis_style_comment_start)
293 dis->set_in_comment (true);
294 if (dis->in_comment_p ())
295 style = dis_style_comment_start;
296
297 /* Now print the content with the correct style. */
298 const char *txt = content.c_str ();
299 switch (style)
300 {
301 case dis_style_mnemonic:
90ed1593 302 case dis_style_sub_mnemonic:
4cbe4ca5
AB
303 case dis_style_assembler_directive:
304 fputs_styled (txt, disasm_mnemonic_style.style (), stream);
305 break;
306
307 case dis_style_register:
308 fputs_styled (txt, disasm_register_style.style (), stream);
309 break;
310
311 case dis_style_immediate:
312 case dis_style_address_offset:
313 fputs_styled (txt, disasm_immediate_style.style (), stream);
314 break;
315
316 case dis_style_address:
317 fputs_styled (txt, address_style.style (), stream);
318 break;
319
320 case dis_style_symbol:
321 fputs_styled (txt, function_name_style.style (), stream);
322 break;
323
324 case dis_style_comment_start:
325 fputs_styled (txt, disasm_comment_style.style (), stream);
326 break;
327
328 case dis_style_text:
329 gdb_puts (txt, stream);
330 break;
331 }
332
60a3da00
AB
333 /* Something non -ve. */
334 return 0;
335}
336
39ef2f62
CB
337static bool
338line_is_less_than (const deprecated_dis_line_entry &mle1,
339 const deprecated_dis_line_entry &mle2)
92df71f0 340{
39ef2f62 341 bool val;
92df71f0 342
9011945e
AB
343 /* End of sequence markers have a line number of 0 but don't want to
344 be sorted to the head of the list, instead sort by PC. */
39ef2f62 345 if (mle1.line == 0 || mle2.line == 0)
9011945e 346 {
39ef2f62
CB
347 if (mle1.start_pc != mle2.start_pc)
348 val = mle1.start_pc < mle2.start_pc;
492325c4 349 else
dda83cd7 350 val = mle1.line < mle2.line;
9011945e
AB
351 }
352 else
353 {
39ef2f62
CB
354 if (mle1.line != mle2.line)
355 val = mle1.line < mle2.line;
356 else
dda83cd7 357 val = mle1.start_pc < mle2.start_pc;
9011945e
AB
358 }
359 return val;
92df71f0
FN
360}
361
a50a4026 362/* See disasm.h. */
af70908d 363
a50a4026 364int
046bebe1 365gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn *insn,
9a24775b 366 gdb_disassembly_flags flags)
92df71f0 367{
92df71f0
FN
368 /* parts of the symbolic representation of the address */
369 int unmapped;
92df71f0
FN
370 int offset;
371 int line;
af70908d 372 int size;
a50a4026 373 CORE_ADDR pc;
8b172ce7 374 struct gdbarch *gdbarch = arch ();
af70908d 375
393702cd 376 {
046bebe1 377 ui_out_emit_tuple tuple_emitter (m_uiout, NULL);
393702cd
TT
378 pc = insn->addr;
379
380 if (insn->number != 0)
381 {
046bebe1
TT
382 m_uiout->field_unsigned ("insn-number", insn->number);
383 m_uiout->text ("\t");
393702cd
TT
384 }
385
386 if ((flags & DISASSEMBLY_SPECULATIVE) != 0)
387 {
388 if (insn->is_speculative)
389 {
046bebe1 390 m_uiout->field_string ("is-speculative", "?");
393702cd
TT
391
392 /* The speculative execution indication overwrites the first
393 character of the PC prefix.
394 We assume a PC prefix length of 3 characters. */
395 if ((flags & DISASSEMBLY_OMIT_PC) == 0)
046bebe1 396 m_uiout->text (pc_prefix (pc) + 1);
393702cd 397 else
046bebe1 398 m_uiout->text (" ");
393702cd
TT
399 }
400 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
046bebe1 401 m_uiout->text (pc_prefix (pc));
393702cd 402 else
046bebe1 403 m_uiout->text (" ");
393702cd
TT
404 }
405 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
046bebe1
TT
406 m_uiout->text (pc_prefix (pc));
407 m_uiout->field_core_addr ("address", gdbarch, pc);
393702cd 408
c7110220 409 std::string name, filename;
2dc80cf8
KB
410 bool omit_fname = ((flags & DISASSEMBLY_OMIT_FNAME) != 0);
411 if (!build_address_symbolic (gdbarch, pc, false, omit_fname, &name,
dda83cd7 412 &offset, &filename, &line, &unmapped))
393702cd
TT
413 {
414 /* We don't care now about line, filename and unmapped. But we might in
415 the future. */
046bebe1 416 m_uiout->text (" <");
2dc80cf8 417 if (!omit_fname)
8dd8c8d4 418 m_uiout->field_string ("func-name", name,
e43b10e1 419 function_name_style.style ());
2dc80cf8
KB
420 /* For negative offsets, avoid displaying them as +-N; the sign of
421 the offset takes the place of the "+" here. */
422 if (offset >= 0)
046bebe1
TT
423 m_uiout->text ("+");
424 m_uiout->field_signed ("offset", offset);
425 m_uiout->text (">:\t");
393702cd
TT
426 }
427 else
046bebe1 428 m_uiout->text (":\t");
393702cd 429
a5d83918 430 /* Clear the buffer into which we will disassemble the instruction. */
393702cd
TT
431 m_insn_stb.clear ();
432
a5d83918
AB
433 /* A helper function to write the M_INSN_STB buffer, followed by a
434 newline. This can be called in a couple of situations. */
435 auto write_out_insn_buffer = [&] ()
436 {
437 m_uiout->field_stream ("inst", m_insn_stb);
438 m_uiout->text ("\n");
439 };
440
441 try
442 {
443 /* Now we can disassemble the instruction. If the disassembler
444 returns a negative value this indicates an error and is handled
445 within the print_insn call, resulting in an exception being
446 thrown. Returning zero makes no sense, as this indicates we
447 disassembled something successfully, but it was something of no
448 size? */
449 size = m_di.print_insn (pc);
450 gdb_assert (size > 0);
451 }
0a9c805d 452 catch (const gdb_exception &)
a5d83918
AB
453 {
454 /* An exception was thrown while disassembling the instruction.
455 However, the disassembler might still have written something
456 out, so ensure that we flush the instruction buffer before
457 rethrowing the exception. We can't perform this write from an
458 object destructor as the write itself might throw an exception
459 if the pager kicks in, and the user selects quit. */
460 write_out_insn_buffer ();
0a9c805d 461 throw;
a5d83918
AB
462 }
463
d4ce49b7 464 if ((flags & (DISASSEMBLY_RAW_INSN | DISASSEMBLY_RAW_BYTES)) != 0)
393702cd 465 {
393702cd
TT
466 /* Build the opcodes using a temporary stream so we can
467 write them out in a single go for the MI. */
468 m_opcode_stb.clear ();
469
d309a8f9
AB
470 /* Read the instruction opcode data. */
471 m_opcode_data.resize (size);
472 read_code (pc, m_opcode_data.data (), size);
393702cd 473
d4ce49b7
AB
474 /* The disassembler provides information about the best way to
475 display the instruction bytes to the user. We provide some sane
476 defaults in case the disassembler gets it wrong. */
477 const struct disassemble_info *di = m_di.disasm_info ();
478 int bytes_per_line = std::max (di->bytes_per_line, size);
479 int bytes_per_chunk = std::max (di->bytes_per_chunk, 1);
480
481 /* If the user has requested the instruction bytes be displayed
482 byte at a time, then handle that here. Also, if the instruction
483 is not a multiple of the chunk size (which probably indicates a
484 disassembler problem) then avoid that causing display problems
485 by switching to byte at a time mode. */
486 if ((flags & DISASSEMBLY_RAW_BYTES) != 0
487 || (size % bytes_per_chunk) != 0)
488 bytes_per_chunk = 1;
489
490 /* Print the instruction opcodes bytes, grouped into chunks. */
491 for (int i = 0; i < size; i += bytes_per_chunk)
393702cd 492 {
d309a8f9
AB
493 if (i > 0)
494 m_opcode_stb.puts (" ");
d4ce49b7
AB
495
496 if (di->display_endian == BFD_ENDIAN_LITTLE)
497 {
498 for (int k = bytes_per_chunk; k-- != 0; )
499 m_opcode_stb.printf ("%02x", (unsigned) m_opcode_data[i + k]);
500 }
501 else
502 {
503 for (int k = 0; k < bytes_per_chunk; k++)
504 m_opcode_stb.printf ("%02x", (unsigned) m_opcode_data[i + k]);
505 }
506 }
507
508 /* Calculate required padding. */
509 int nspaces = 0;
510 for (int i = size; i < bytes_per_line; i += bytes_per_chunk)
511 {
512 if (i > size)
513 nspaces++;
514 nspaces += bytes_per_chunk * 2;
393702cd
TT
515 }
516
046bebe1 517 m_uiout->field_stream ("opcodes", m_opcode_stb);
d4ce49b7 518 m_uiout->spaces (nspaces);
046bebe1 519 m_uiout->text ("\t");
393702cd 520 }
af70908d 521
a5d83918
AB
522 /* Disassembly was a success, write out the instruction buffer. */
523 write_out_insn_buffer ();
393702cd 524 }
af70908d
MM
525
526 return size;
527}
528
529static int
187808b0
PA
530dump_insns (struct gdbarch *gdbarch,
531 struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high,
9a24775b 532 int how_many, gdb_disassembly_flags flags, CORE_ADDR *end_pc)
af70908d 533{
a50a4026 534 struct disasm_insn insn;
af70908d
MM
535 int num_displayed = 0;
536
a50a4026
MM
537 memset (&insn, 0, sizeof (insn));
538 insn.addr = low;
539
046bebe1 540 gdb_pretty_print_disassembler disasm (gdbarch, uiout);
8b172ce7 541
a50a4026 542 while (insn.addr < high && (how_many < 0 || num_displayed < how_many))
af70908d
MM
543 {
544 int size;
545
046bebe1 546 size = disasm.pretty_print_insn (&insn, flags);
af70908d
MM
547 if (size <= 0)
548 break;
549
550 ++num_displayed;
a50a4026 551 insn.addr += size;
af70908d
MM
552
553 /* Allow user to bail out with ^C. */
554 QUIT;
92df71f0 555 }
6ff0ba5f
DE
556
557 if (end_pc != NULL)
a50a4026 558 *end_pc = insn.addr;
af70908d 559
92df71f0
FN
560 return num_displayed;
561}
562
563/* The idea here is to present a source-O-centric view of a
564 function to the user. This means that things are presented
565 in source order, with (possibly) out of order assembly
6ff0ba5f
DE
566 immediately following.
567
568 N.B. This view is deprecated. */
0963b4bd 569
92df71f0 570static void
6ff0ba5f 571do_mixed_source_and_assembly_deprecated
187808b0
PA
572 (struct gdbarch *gdbarch, struct ui_out *uiout,
573 struct symtab *symtab,
6ff0ba5f 574 CORE_ADDR low, CORE_ADDR high,
9a24775b 575 int how_many, gdb_disassembly_flags flags)
92df71f0
FN
576{
577 int newlines = 0;
6ff0ba5f 578 int nlines;
977a0c16 579 const struct linetable_entry *le;
6ff0ba5f 580 struct deprecated_dis_line_entry *mle;
92df71f0
FN
581 struct symtab_and_line sal;
582 int i;
583 int out_of_order = 0;
584 int next_line = 0;
92df71f0 585 int num_displayed = 0;
8d297bbf 586 print_source_lines_flags psl_flags = 0;
92df71f0 587
5b607461 588 gdb_assert (symtab != nullptr && symtab->linetable () != nullptr);
6ff0ba5f 589
5b607461
SM
590 nlines = symtab->linetable ()->nitems;
591 le = symtab->linetable ()->item;
6ff0ba5f 592
4cd29721
MM
593 if (flags & DISASSEMBLY_FILENAME)
594 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
595
6ff0ba5f
DE
596 mle = (struct deprecated_dis_line_entry *)
597 alloca (nlines * sizeof (struct deprecated_dis_line_entry));
92df71f0 598
1acc9dca 599 struct objfile *objfile = symtab->compunit ()->objfile ();
48e0f38c
TT
600
601 unrelocated_addr unrel_low
602 = unrelocated_addr (low - objfile->text_section_offset ());
603 unrelocated_addr unrel_high
604 = unrelocated_addr (high - objfile->text_section_offset ());
1acc9dca 605
92df71f0
FN
606 /* Copy linetable entries for this function into our data
607 structure, creating end_pc's and setting out_of_order as
608 appropriate. */
609
610 /* First, skip all the preceding functions. */
611
0434c3ef 612 for (i = 0; i < nlines - 1 && le[i].unrelocated_pc () < unrel_low; i++);
92df71f0
FN
613
614 /* Now, copy all entries before the end of this function. */
615
0434c3ef 616 for (; i < nlines - 1 && le[i].unrelocated_pc () < unrel_high; i++)
92df71f0 617 {
6e6ac32d 618 if (le[i] == le[i + 1])
0963b4bd 619 continue; /* Ignore duplicates. */
92df71f0
FN
620
621 /* Skip any end-of-function markers. */
622 if (le[i].line == 0)
623 continue;
624
625 mle[newlines].line = le[i].line;
626 if (le[i].line > le[i + 1].line)
627 out_of_order = 1;
1acc9dca
TT
628 mle[newlines].start_pc = le[i].pc (objfile);
629 mle[newlines].end_pc = le[i + 1].pc (objfile);
92df71f0
FN
630 newlines++;
631 }
632
633 /* If we're on the last line, and it's part of the function,
634 then we need to get the end pc in a special way. */
635
0434c3ef 636 if (i == nlines - 1 && le[i].unrelocated_pc () < unrel_high)
92df71f0
FN
637 {
638 mle[newlines].line = le[i].line;
1acc9dca
TT
639 mle[newlines].start_pc = le[i].pc (objfile);
640 sal = find_pc_line (le[i].pc (objfile), 0);
92df71f0
FN
641 mle[newlines].end_pc = sal.end;
642 newlines++;
643 }
644
6ff0ba5f 645 /* Now, sort mle by line #s (and, then by addresses within lines). */
92df71f0
FN
646
647 if (out_of_order)
39ef2f62 648 std::sort (mle, mle + newlines, line_is_less_than);
92df71f0
FN
649
650 /* Now, for each line entry, emit the specified lines (unless
651 they have been emitted before), followed by the assembly code
652 for that line. */
653
30f0b101
TT
654 ui_out_emit_list asm_insns_list (uiout, "asm_insns");
655
6b09f134
LS
656 std::optional<ui_out_emit_tuple> outer_tuple_emitter;
657 std::optional<ui_out_emit_list> inner_list_emitter;
92df71f0
FN
658
659 for (i = 0; i < newlines; i++)
660 {
92df71f0
FN
661 /* Print out everything from next_line to the current line. */
662 if (mle[i].line >= next_line)
663 {
664 if (next_line != 0)
665 {
0963b4bd 666 /* Just one line to print. */
92df71f0
FN
667 if (next_line == mle[i].line)
668 {
30f0b101 669 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
4cd29721 670 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
92df71f0
FN
671 }
672 else
673 {
0963b4bd 674 /* Several source lines w/o asm instructions associated. */
92df71f0
FN
675 for (; next_line < mle[i].line; next_line++)
676 {
2e783024
TT
677 ui_out_emit_tuple tuple_emitter (uiout,
678 "src_and_asm_line");
92df71f0 679 print_source_lines (symtab, next_line, next_line + 1,
4cd29721 680 psl_flags);
b926417a
TT
681 ui_out_emit_list temp_list_emitter (uiout,
682 "line_asm_insn");
92df71f0
FN
683 }
684 /* Print the last line and leave list open for
0963b4bd 685 asm instructions to be added. */
30f0b101 686 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
4cd29721 687 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
92df71f0
FN
688 }
689 }
690 else
691 {
30f0b101 692 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
4cd29721 693 print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags);
92df71f0
FN
694 }
695
696 next_line = mle[i].line + 1;
30f0b101 697 inner_list_emitter.emplace (uiout, "line_asm_insn");
92df71f0
FN
698 }
699
187808b0 700 num_displayed += dump_insns (gdbarch, uiout,
13274fc3 701 mle[i].start_pc, mle[i].end_pc,
e47ad6c0 702 how_many, flags, NULL);
0127c0d3
JJ
703
704 /* When we've reached the end of the mle array, or we've seen the last
dda83cd7 705 assembly range for this source line, close out the list/tuple. */
0127c0d3 706 if (i == (newlines - 1) || mle[i + 1].line > mle[i].line)
92df71f0 707 {
30f0b101
TT
708 inner_list_emitter.reset ();
709 outer_tuple_emitter.reset ();
112e8700 710 uiout->text ("\n");
92df71f0 711 }
0127c0d3
JJ
712 if (how_many >= 0 && num_displayed >= how_many)
713 break;
92df71f0 714 }
92df71f0
FN
715}
716
6ff0ba5f
DE
717/* The idea here is to present a source-O-centric view of a
718 function to the user. This means that things are presented
719 in source order, with (possibly) out of order assembly
720 immediately following. */
721
722static void
e47ad6c0
YQ
723do_mixed_source_and_assembly (struct gdbarch *gdbarch,
724 struct ui_out *uiout,
6ff0ba5f
DE
725 struct symtab *main_symtab,
726 CORE_ADDR low, CORE_ADDR high,
9a24775b 727 int how_many, gdb_disassembly_flags flags)
6ff0ba5f 728{
6ff0ba5f 729 const struct linetable_entry *le, *first_le;
6ff0ba5f 730 int i, nlines;
6ff0ba5f 731 int num_displayed = 0;
8d297bbf 732 print_source_lines_flags psl_flags = 0;
6ff0ba5f
DE
733 CORE_ADDR pc;
734 struct symtab *last_symtab;
735 int last_line;
6ff0ba5f 736
5b607461 737 gdb_assert (main_symtab != NULL && main_symtab->linetable () != NULL);
6ff0ba5f
DE
738
739 /* First pass: collect the list of all source files and lines.
740 We do this so that we can only print lines containing code once.
741 We try to print the source text leading up to the next instruction,
742 but if that text is for code that will be disassembled later, then
743 we'll want to defer printing it until later with its associated code. */
744
fc4007c9 745 htab_up dis_line_table (allocate_dis_line_table ());
6ff0ba5f 746
1acc9dca 747 struct objfile *objfile = main_symtab->compunit ()->objfile ();
48e0f38c
TT
748
749 unrelocated_addr unrel_low
750 = unrelocated_addr (low - objfile->text_section_offset ());
751 unrelocated_addr unrel_high
752 = unrelocated_addr (high - objfile->text_section_offset ());
1acc9dca 753
6ff0ba5f
DE
754 pc = low;
755
756 /* The prologue may be empty, but there may still be a line number entry
757 for the opening brace which is distinct from the first line of code.
758 If the prologue has been eliminated find_pc_line may return the source
759 line after the opening brace. We still want to print this opening brace.
760 first_le is used to implement this. */
761
5b607461
SM
762 nlines = main_symtab->linetable ()->nitems;
763 le = main_symtab->linetable ()->item;
6ff0ba5f
DE
764 first_le = NULL;
765
766 /* Skip all the preceding functions. */
0434c3ef 767 for (i = 0; i < nlines && le[i].unrelocated_pc () < unrel_low; i++)
6ff0ba5f
DE
768 continue;
769
0434c3ef 770 if (i < nlines && le[i].unrelocated_pc () < unrel_high)
6ff0ba5f
DE
771 first_le = &le[i];
772
773 /* Add lines for every pc value. */
774 while (pc < high)
775 {
776 struct symtab_and_line sal;
777 int length;
778
779 sal = find_pc_line (pc, 0);
780 length = gdb_insn_length (gdbarch, pc);
781 pc += length;
782
783 if (sal.symtab != NULL)
fc4007c9 784 add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line);
6ff0ba5f
DE
785 }
786
787 /* Second pass: print the disassembly.
788
789 Output format, from an MI perspective:
790 The result is a ui_out list, field name "asm_insns", where elements have
791 name "src_and_asm_line".
792 Each element is a tuple of source line specs (field names line, file,
793 fullname), and field "line_asm_insn" which contains the disassembly.
794 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
795 opcodes, inst.
796
797 CLI output works on top of this because MI ignores ui_out_text output,
798 which is where we put file name and source line contents output.
799
30f0b101
TT
800 Emitter usage:
801 asm_insns_emitter
6ff0ba5f 802 Handles the outer "asm_insns" list.
30f0b101 803 tuple_emitter
6ff0ba5f 804 The tuples for each group of consecutive disassemblies.
30f0b101 805 list_emitter
6ff0ba5f
DE
806 List of consecutive source lines or disassembled insns. */
807
808 if (flags & DISASSEMBLY_FILENAME)
809 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
810
30f0b101 811 ui_out_emit_list asm_insns_emitter (uiout, "asm_insns");
6ff0ba5f 812
6b09f134
LS
813 std::optional<ui_out_emit_tuple> tuple_emitter;
814 std::optional<ui_out_emit_list> list_emitter;
6ff0ba5f
DE
815
816 last_symtab = NULL;
817 last_line = 0;
818 pc = low;
819
820 while (pc < high)
821 {
6ff0ba5f
DE
822 struct symtab_and_line sal;
823 CORE_ADDR end_pc;
824 int start_preceding_line_to_display = 0;
825 int end_preceding_line_to_display = 0;
826 int new_source_line = 0;
827
828 sal = find_pc_line (pc, 0);
829
830 if (sal.symtab != last_symtab)
831 {
832 /* New source file. */
833 new_source_line = 1;
834
835 /* If this is the first line of output, check for any preceding
836 lines. */
837 if (last_line == 0
838 && first_le != NULL
839 && first_le->line < sal.line)
840 {
841 start_preceding_line_to_display = first_le->line;
842 end_preceding_line_to_display = sal.line;
843 }
844 }
845 else
846 {
847 /* Same source file as last time. */
848 if (sal.symtab != NULL)
849 {
850 if (sal.line > last_line + 1 && last_line != 0)
851 {
852 int l;
853
854 /* Several preceding source lines. Print the trailing ones
855 not associated with code that we'll print later. */
856 for (l = sal.line - 1; l > last_line; --l)
857 {
fc4007c9
TT
858 if (line_has_code_p (dis_line_table.get (),
859 sal.symtab, l))
6ff0ba5f
DE
860 break;
861 }
862 if (l < sal.line - 1)
863 {
864 start_preceding_line_to_display = l + 1;
865 end_preceding_line_to_display = sal.line;
866 }
867 }
868 if (sal.line != last_line)
869 new_source_line = 1;
870 else
871 {
872 /* Same source line as last time. This can happen, depending
873 on the debug info. */
874 }
875 }
876 }
877
878 if (new_source_line)
879 {
880 /* Skip the newline if this is the first instruction. */
881 if (pc > low)
112e8700 882 uiout->text ("\n");
30f0b101 883 if (tuple_emitter.has_value ())
6ff0ba5f 884 {
30f0b101
TT
885 gdb_assert (list_emitter.has_value ());
886 list_emitter.reset ();
887 tuple_emitter.reset ();
6ff0ba5f
DE
888 }
889 if (sal.symtab != last_symtab
890 && !(flags & DISASSEMBLY_FILENAME))
891 {
892 /* Remember MI ignores ui_out_text.
893 We don't have to do anything here for MI because MI
894 output includes the source specs for each line. */
895 if (sal.symtab != NULL)
896 {
112e8700 897 uiout->text (symtab_to_filename_for_display (sal.symtab));
6ff0ba5f
DE
898 }
899 else
112e8700
SM
900 uiout->text ("unknown");
901 uiout->text (":\n");
6ff0ba5f
DE
902 }
903 if (start_preceding_line_to_display > 0)
904 {
905 /* Several source lines w/o asm instructions associated.
906 We need to preserve the structure of the output, so output
907 a bunch of line tuples with no asm entries. */
908 int l;
6ff0ba5f
DE
909
910 gdb_assert (sal.symtab != NULL);
911 for (l = start_preceding_line_to_display;
912 l < end_preceding_line_to_display;
913 ++l)
914 {
b926417a
TT
915 ui_out_emit_tuple line_tuple_emitter (uiout,
916 "src_and_asm_line");
6ff0ba5f 917 print_source_lines (sal.symtab, l, l + 1, psl_flags);
30f0b101 918 ui_out_emit_list chain_line_emitter (uiout, "line_asm_insn");
6ff0ba5f
DE
919 }
920 }
30f0b101 921 tuple_emitter.emplace (uiout, "src_and_asm_line");
6ff0ba5f
DE
922 if (sal.symtab != NULL)
923 print_source_lines (sal.symtab, sal.line, sal.line + 1, psl_flags);
924 else
112e8700 925 uiout->text (_("--- no source info for this pc ---\n"));
30f0b101 926 list_emitter.emplace (uiout, "line_asm_insn");
6ff0ba5f
DE
927 }
928 else
929 {
930 /* Here we're appending instructions to an existing line.
931 By construction the very first insn will have a symtab
932 and follow the new_source_line path above. */
30f0b101
TT
933 gdb_assert (tuple_emitter.has_value ());
934 gdb_assert (list_emitter.has_value ());
6ff0ba5f
DE
935 }
936
937 if (sal.end != 0)
325fac50 938 end_pc = std::min (sal.end, high);
6ff0ba5f
DE
939 else
940 end_pc = pc + 1;
187808b0 941 num_displayed += dump_insns (gdbarch, uiout, pc, end_pc,
e47ad6c0 942 how_many, flags, &end_pc);
6ff0ba5f
DE
943 pc = end_pc;
944
945 if (how_many >= 0 && num_displayed >= how_many)
946 break;
947
948 last_symtab = sal.symtab;
949 last_line = sal.line;
950 }
6ff0ba5f 951}
92df71f0
FN
952
953static void
187808b0 954do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
92df71f0 955 CORE_ADDR low, CORE_ADDR high,
9a24775b 956 int how_many, gdb_disassembly_flags flags)
92df71f0 957{
10f489e5 958 ui_out_emit_list list_emitter (uiout, "asm_insns");
92df71f0 959
187808b0 960 dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL);
92df71f0
FN
961}
962
471b9d15
MR
963/* Combine implicit and user disassembler options and return them
964 in a newly-created string. */
965
966static std::string
967get_all_disassembler_options (struct gdbarch *gdbarch)
968{
969 const char *implicit = gdbarch_disassembler_options_implicit (gdbarch);
970 const char *options = get_disassembler_options (gdbarch);
971 const char *comma = ",";
972
973 if (implicit == nullptr)
974 {
975 implicit = "";
976 comma = "";
977 }
978
979 if (options == nullptr)
980 {
981 options = "";
982 comma = "";
983 }
984
985 return string_printf ("%s%s%s", implicit, comma, options);
986}
987
e47ad6c0
YQ
988gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
989 struct ui_file *file,
f0c2e3e0
AB
990 read_memory_ftype func)
991 : gdb_printing_disassembler (gdbarch, &m_buffer, func,
992 dis_asm_memory_error, dis_asm_print_address),
f22c50c2
AB
993 m_dest (file),
994 m_buffer (!use_ext_lang_for_styling () && use_libopcodes_for_styling ())
f0c2e3e0
AB
995{ /* Nothing. */ }
996
997/* See disasm.h. */
998
f22c50c2
AB
999bool
1000gdb_disassembler::use_ext_lang_for_styling () const
1001{
1002 /* The use of m_di.created_styled_output here is a bit of a cheat, but
1003 it works fine for now.
1004
1005 This function is called in situations after m_di has been initialized,
1006 but before the instruction has been disassembled.
1007
1008 Currently, every target that supports libopcodes styling sets the
1009 created_styled_output field in disassemble_init_for_target, which was
1010 called as part of the initialization of gdb_printing_disassembler.
1011
1012 This means that we are OK to check the created_styled_output field
1013 here.
1014
1015 If, in the future, there's ever a target that only sets the
1016 created_styled_output field during the actual instruction disassembly
1017 phase, then we will need to update this code. */
1018 return (disassembler_styling
1019 && (!m_di.created_styled_output || !use_libopcodes_styling)
1020 && use_ext_lang_colorization_p
1021 && m_dest->can_emit_style_escape ());
1022}
1023
1024/* See disasm.h. */
1025
1026bool
1027gdb_disassembler::use_libopcodes_for_styling () const
1028{
1029 /* See the comment on the use of m_di.created_styled_output in the
1030 gdb_disassembler::use_ext_lang_for_styling function. */
1031 return (disassembler_styling
1032 && m_di.created_styled_output
1033 && use_libopcodes_styling
1034 && m_dest->can_emit_style_escape ());
1035}
1036
1037/* See disasm.h. */
1038
f0c2e3e0 1039gdb_disassemble_info::gdb_disassemble_info
81384924 1040 (struct gdbarch *gdbarch,
f0c2e3e0
AB
1041 read_memory_ftype read_memory_func, memory_error_ftype memory_error_func,
1042 print_address_ftype print_address_func, fprintf_ftype fprintf_func,
1043 fprintf_styled_ftype fprintf_styled_func)
1044 : m_gdbarch (gdbarch)
92df71f0 1045{
f0c2e3e0
AB
1046 gdb_assert (fprintf_func != nullptr);
1047 gdb_assert (fprintf_styled_func != nullptr);
81384924 1048 init_disassemble_info (&m_di, (void *) this, fprintf_func,
f0c2e3e0 1049 fprintf_styled_func);
e47ad6c0 1050 m_di.flavour = bfd_target_unknown_flavour;
f0c2e3e0
AB
1051
1052 /* The memory_error_func, print_address_func, and read_memory_func are
1053 all initialized to a default (non-nullptr) value by the call to
1054 init_disassemble_info above. If the user is overriding these fields
1055 (by passing non-nullptr values) then do that now, otherwise, leave
1056 these fields as the defaults. */
1057 if (memory_error_func != nullptr)
1058 m_di.memory_error_func = memory_error_func;
1059 if (print_address_func != nullptr)
1060 m_di.print_address_func = print_address_func;
1061 if (read_memory_func != nullptr)
1062 m_di.read_memory_func = read_memory_func;
1063
e47ad6c0
YQ
1064 m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1065 m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1066 m_di.endian = gdbarch_byte_order (gdbarch);
1067 m_di.endian_code = gdbarch_byte_order_for_code (gdbarch);
1068 m_di.application_data = this;
471b9d15
MR
1069 m_disassembler_options_holder = get_all_disassembler_options (gdbarch);
1070 if (!m_disassembler_options_holder.empty ())
1071 m_di.disassembler_options = m_disassembler_options_holder.c_str ();
e47ad6c0
YQ
1072 disassemble_init_for_target (&m_di);
1073}
1074
f0c2e3e0
AB
1075/* See disasm.h. */
1076
1077gdb_disassemble_info::~gdb_disassemble_info ()
4d89c1c7
TT
1078{
1079 disassemble_free_target (&m_di);
1080}
1081
e4ae3025
AB
1082/* Wrapper around calling gdbarch_print_insn. This function takes care of
1083 first calling the extension language hooks for print_insn, and, if none
1084 of the extension languages can print this instruction, calls
1085 gdbarch_print_insn to do the work.
1086
1087 GDBARCH is the architecture to disassemble in, VMA is the address of the
1088 instruction being disassembled, and INFO is the libopcodes disassembler
1089 related information. */
1090
1091static int
1092gdb_print_insn_1 (struct gdbarch *gdbarch, CORE_ADDR vma,
1093 struct disassemble_info *info)
1094{
1095 /* Call into the extension languages to do the disassembly. */
6b09f134 1096 std::optional<int> length = ext_lang_print_insn (gdbarch, vma, info);
e4ae3025
AB
1097 if (length.has_value ())
1098 return *length;
1099
1100 /* No extension language wanted to do the disassembly, so do it
1101 manually. */
1102 return gdbarch_print_insn (gdbarch, vma, info);
1103}
1104
e867795e
AB
1105/* See disasm.h. */
1106
1107bool gdb_disassembler::use_ext_lang_colorization_p = true;
1108
1109/* See disasm.h. */
1110
e47ad6c0
YQ
1111int
1112gdb_disassembler::print_insn (CORE_ADDR memaddr,
1113 int *branch_delay_insns)
1114{
76b43c9b 1115 m_err_memaddr.reset ();
e867795e 1116 m_buffer.clear ();
4cbe4ca5 1117 this->set_in_comment (false);
d8b49cf0 1118
e4ae3025 1119 int length = gdb_print_insn_1 (arch (), memaddr, &m_di);
e47ad6c0 1120
4cbe4ca5
AB
1121 /* If we have successfully disassembled an instruction, disassembler
1122 styling using the extension language is on, and libopcodes hasn't
1123 already styled the output for us, and, if the destination can support
1124 styling, then lets call into the extension languages in order to style
1125 this output. */
f22c50c2 1126 if (length > 0 && use_ext_lang_for_styling ())
e867795e 1127 {
6b09f134 1128 std::optional<std::string> ext_contents;
e867795e
AB
1129 ext_contents = ext_lang_colorize_disasm (m_buffer.string (), arch ());
1130 if (ext_contents.has_value ())
1131 m_buffer = std::move (*ext_contents);
1132 else
1133 {
1134 /* The extension language failed to add styling to the
1135 disassembly output. Set the static flag so that next time we
1136 disassemble we don't even bother attempting to use the
1137 extension language for styling. */
1138 use_ext_lang_colorization_p = false;
1139
f22c50c2
AB
1140 /* We're about to disassemble this instruction again, reset the
1141 in-comment state. */
1142 this->set_in_comment (false);
1143
e867795e
AB
1144 /* The instruction we just disassembled, and the extension
1145 languages failed to style, might have otherwise had some
1146 minimal styling applied by GDB. To regain that styling we
1147 need to recreate m_buffer, but this time with styling support.
1148
1149 To do this we perform an in-place new, but this time turn on
1150 the styling support, then we can re-disassembly the
1151 instruction, and gain any minimal styling GDB might add. */
69f6730d 1152 static_assert ((std::is_same<decltype (m_buffer),
e867795e
AB
1153 string_file>::value));
1154 gdb_assert (!m_buffer.term_out ());
1155 m_buffer.~string_file ();
f22c50c2 1156 new (&m_buffer) string_file (use_libopcodes_for_styling ());
e4ae3025 1157 length = gdb_print_insn_1 (arch (), memaddr, &m_di);
e867795e
AB
1158 gdb_assert (length > 0);
1159 }
1160 }
1161
1162 /* Push any disassemble output to the real destination stream. We do
1163 this even if the disassembler reported failure (-1) as the
1164 disassembler may have printed something to its output stream. */
81384924 1165 gdb_printf (m_dest, "%s", m_buffer.c_str ());
e867795e
AB
1166
1167 /* If the disassembler failed then report an appropriate error. */
d8b49cf0 1168 if (length < 0)
76b43c9b
AB
1169 {
1170 if (m_err_memaddr.has_value ())
1171 memory_error (TARGET_XFER_E_IO, *m_err_memaddr);
1172 else
1173 error (_("unknown disassembler error (error = %d)"), length);
1174 }
d8b49cf0 1175
e47ad6c0
YQ
1176 if (branch_delay_insns != NULL)
1177 {
1178 if (m_di.insn_info_valid)
1179 *branch_delay_insns = m_di.branch_delay_insns;
1180 else
1181 *branch_delay_insns = 0;
1182 }
1183 return length;
92bf2b80
AC
1184}
1185
1186void
13274fc3 1187gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
9a24775b 1188 gdb_disassembly_flags flags, int how_many,
9c419145 1189 CORE_ADDR low, CORE_ADDR high)
92bf2b80 1190{
34248c3a 1191 struct symtab *symtab;
92bf2b80 1192 int nlines = -1;
92df71f0 1193
0963b4bd 1194 /* Assume symtab is valid for whole PC range. */
34248c3a 1195 symtab = find_pc_line_symtab (low);
92df71f0 1196
5b607461
SM
1197 if (symtab != NULL && symtab->linetable () != NULL)
1198 nlines = symtab->linetable ()->nitems;
92df71f0 1199
6ff0ba5f
DE
1200 if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
1201 || nlines <= 0)
187808b0 1202 do_assembly_only (gdbarch, uiout, low, high, how_many, flags);
92df71f0 1203
e6158f16 1204 else if (flags & DISASSEMBLY_SOURCE)
187808b0 1205 do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high,
e47ad6c0 1206 how_many, flags);
6ff0ba5f
DE
1207
1208 else if (flags & DISASSEMBLY_SOURCE_DEPRECATED)
187808b0 1209 do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab,
e47ad6c0 1210 low, high, how_many, flags);
92df71f0
FN
1211
1212 gdb_flush (gdb_stdout);
1213}
810ecf9f 1214
92bf2b80 1215/* Print the instruction at address MEMADDR in debugged memory,
a4642986
MR
1216 on STREAM. Returns the length of the instruction, in bytes,
1217 and, if requested, the number of branch delay slot instructions. */
92bf2b80
AC
1218
1219int
13274fc3
UW
1220gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
1221 struct ui_file *stream, int *branch_delay_insns)
92bf2b80 1222{
a4642986 1223
e47ad6c0
YQ
1224 gdb_disassembler di (gdbarch, stream);
1225
1226 return di.print_insn (memaddr, branch_delay_insns);
92bf2b80 1227}
eda5a4d7 1228
eda5a4d7
PA
1229/* Return the length in bytes of the instruction at address MEMADDR in
1230 debugged memory. */
1231
1232int
1233gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
1234{
d7e74731 1235 return gdb_print_insn (gdbarch, addr, &null_stream, NULL);
eda5a4d7
PA
1236}
1237
8b39b1e7 1238/* See disasm.h. */
eda5a4d7 1239
8b39b1e7 1240int
8eb7d135
AB
1241gdb_non_printing_disassembler::null_fprintf_func
1242 (void *stream, const char *format, ...) noexcept
eda5a4d7
PA
1243{
1244 return 0;
1245}
1246
8b39b1e7 1247/* See disasm.h. */
60a3da00 1248
8b39b1e7
AB
1249int
1250gdb_non_printing_disassembler::null_fprintf_styled_func
8eb7d135
AB
1251 (void *stream, enum disassembler_style style,
1252 const char *format, ...) noexcept
60a3da00
AB
1253{
1254 return 0;
1255}
1256
8b39b1e7
AB
1257/* A non-printing disassemble_info management class. The disassemble_info
1258 setup by this class will not print anything to the output stream (there
1259 is no output stream), and the instruction to be disassembled will be
1260 read from a buffer passed to the constructor. */
eda5a4d7 1261
8b39b1e7
AB
1262struct gdb_non_printing_buffer_disassembler
1263 : public gdb_non_printing_disassembler
eda5a4d7 1264{
8b39b1e7
AB
1265 /* Constructor. GDBARCH is the architecture to disassemble for, BUFFER
1266 contains the instruction to disassemble, and INSN_ADDRESS is the
1267 address (in target memory) of the instruction to disassemble. */
1268 gdb_non_printing_buffer_disassembler (struct gdbarch *gdbarch,
1269 gdb::array_view<const gdb_byte> buffer,
1270 CORE_ADDR insn_address)
1271 : gdb_non_printing_disassembler (gdbarch, nullptr)
1272 {
1273 /* The cast is necessary until disassemble_info is const-ified. */
1274 m_di.buffer = (gdb_byte *) buffer.data ();
1275 m_di.buffer_length = buffer.size ();
1276 m_di.buffer_vma = insn_address;
1277 }
1278};
eda5a4d7
PA
1279
1280/* Return the length in bytes of INSN. MAX_LEN is the size of the
1281 buffer containing INSN. */
1282
1283int
1284gdb_buffered_insn_length (struct gdbarch *gdbarch,
1285 const gdb_byte *insn, int max_len, CORE_ADDR addr)
1286{
8b39b1e7
AB
1287 gdb::array_view<const gdb_byte> buffer
1288 = gdb::make_array_view (insn, max_len);
1289 gdb_non_printing_buffer_disassembler dis (gdbarch, buffer, addr);
1290 int result = gdb_print_insn_1 (gdbarch, addr, dis.disasm_info ());
4d89c1c7 1291 return result;
eda5a4d7 1292}
65b48a81 1293
9f1c9448 1294const char *
65b48a81
PB
1295get_disassembler_options (struct gdbarch *gdbarch)
1296{
c05dd511
TT
1297 std::string *disassembler_options = gdbarch_disassembler_options (gdbarch);
1298 if (disassembler_options == nullptr || disassembler_options->empty ())
1299 return nullptr;
1300 return disassembler_options->c_str ();
65b48a81
PB
1301}
1302
1303void
e0700ba4 1304set_disassembler_options (const char *prospective_options)
65b48a81
PB
1305{
1306 struct gdbarch *gdbarch = get_current_arch ();
c05dd511 1307 std::string *disassembler_options = gdbarch_disassembler_options (gdbarch);
471b9d15 1308 const disasm_options_and_args_t *valid_options_and_args;
65b48a81 1309 const disasm_options_t *valid_options;
e0700ba4
SM
1310 gdb::unique_xmalloc_ptr<char> prospective_options_local
1311 = make_unique_xstrdup (prospective_options);
1312 char *options = remove_whitespace_and_extra_commas
1313 (prospective_options_local.get ());
f995bbe8 1314 const char *opt;
65b48a81
PB
1315
1316 /* Allow all architectures, even ones that do not support 'set disassembler',
1317 to reset their disassembler options to NULL. */
1318 if (options == NULL)
1319 {
c05dd511
TT
1320 if (disassembler_options != nullptr)
1321 disassembler_options->clear ();
65b48a81
PB
1322 return;
1323 }
1324
471b9d15
MR
1325 valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
1326 if (valid_options_and_args == NULL)
65b48a81 1327 {
6cb06a8c 1328 gdb_printf (gdb_stderr, _("\
65b48a81
PB
1329'set disassembler-options ...' is not supported on this architecture.\n"));
1330 return;
1331 }
1332
471b9d15
MR
1333 valid_options = &valid_options_and_args->options;
1334
65b48a81
PB
1335 /* Verify we have valid disassembler options. */
1336 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
1337 {
1338 size_t i;
1339 for (i = 0; valid_options->name[i] != NULL; i++)
471b9d15
MR
1340 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1341 {
1342 size_t len = strlen (valid_options->name[i]);
1343 bool found = false;
1344 const char *arg;
1345 size_t j;
1346
1347 if (memcmp (opt, valid_options->name[i], len) != 0)
1348 continue;
1349 arg = opt + len;
f2028892
TO
1350 if (valid_options->arg[i]->values == NULL)
1351 break;
471b9d15
MR
1352 for (j = 0; valid_options->arg[i]->values[j] != NULL; j++)
1353 if (disassembler_options_cmp
1354 (arg, valid_options->arg[i]->values[j]) == 0)
1355 {
1356 found = true;
1357 break;
1358 }
1359 if (found)
1360 break;
1361 }
1362 else if (disassembler_options_cmp (opt, valid_options->name[i]) == 0)
65b48a81
PB
1363 break;
1364 if (valid_options->name[i] == NULL)
1365 {
6cb06a8c
TT
1366 gdb_printf (gdb_stderr,
1367 _("Invalid disassembler option value: '%s'.\n"),
1368 opt);
65b48a81
PB
1369 return;
1370 }
1371 }
1372
c05dd511 1373 *disassembler_options = options;
65b48a81
PB
1374}
1375
1376static void
eb4c3f4a 1377set_disassembler_options_sfunc (const char *args, int from_tty,
65b48a81
PB
1378 struct cmd_list_element *c)
1379{
e0700ba4 1380 set_disassembler_options (prospective_options.c_str ());
65b48a81
PB
1381}
1382
1383static void
1384show_disassembler_options_sfunc (struct ui_file *file, int from_tty,
1385 struct cmd_list_element *c, const char *value)
1386{
1387 struct gdbarch *gdbarch = get_current_arch ();
471b9d15
MR
1388 const disasm_options_and_args_t *valid_options_and_args;
1389 const disasm_option_arg_t *valid_args;
65b48a81
PB
1390 const disasm_options_t *valid_options;
1391
1392 const char *options = get_disassembler_options (gdbarch);
1393 if (options == NULL)
1394 options = "";
1395
6cb06a8c
TT
1396 gdb_printf (file, _("The current disassembler options are '%s'\n\n"),
1397 options);
65b48a81 1398
471b9d15 1399 valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
65b48a81 1400
471b9d15 1401 if (valid_options_and_args == NULL)
f4bab6ff 1402 {
0426ad51
TT
1403 gdb_puts (_("There are no disassembler options available "
1404 "for this architecture.\n"),
1405 file);
f4bab6ff
TT
1406 return;
1407 }
65b48a81 1408
471b9d15
MR
1409 valid_options = &valid_options_and_args->options;
1410
6cb06a8c 1411 gdb_printf (file, _("\
65b48a81 1412The following disassembler options are supported for use with the\n\
f4bab6ff 1413'set disassembler-options OPTION [,OPTION]...' command:\n"));
65b48a81
PB
1414
1415 if (valid_options->description != NULL)
1416 {
1417 size_t i, max_len = 0;
1418
6cb06a8c 1419 gdb_printf (file, "\n");
471b9d15 1420
65b48a81
PB
1421 /* Compute the length of the longest option name. */
1422 for (i = 0; valid_options->name[i] != NULL; i++)
1423 {
1424 size_t len = strlen (valid_options->name[i]);
471b9d15
MR
1425
1426 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1427 len += strlen (valid_options->arg[i]->name);
65b48a81
PB
1428 if (max_len < len)
1429 max_len = len;
1430 }
1431
1432 for (i = 0, max_len++; valid_options->name[i] != NULL; i++)
1433 {
6cb06a8c 1434 gdb_printf (file, " %s", valid_options->name[i]);
471b9d15 1435 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
6cb06a8c 1436 gdb_printf (file, "%s", valid_options->arg[i]->name);
65b48a81 1437 if (valid_options->description[i] != NULL)
471b9d15
MR
1438 {
1439 size_t len = strlen (valid_options->name[i]);
1440
1441 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1442 len += strlen (valid_options->arg[i]->name);
6cb06a8c
TT
1443 gdb_printf (file, "%*c %s", (int) (max_len - len), ' ',
1444 valid_options->description[i]);
471b9d15 1445 }
6cb06a8c 1446 gdb_printf (file, "\n");
65b48a81
PB
1447 }
1448 }
1449 else
1450 {
1451 size_t i;
6cb06a8c 1452 gdb_printf (file, " ");
65b48a81
PB
1453 for (i = 0; valid_options->name[i] != NULL; i++)
1454 {
6cb06a8c 1455 gdb_printf (file, "%s", valid_options->name[i]);
471b9d15 1456 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
6cb06a8c 1457 gdb_printf (file, "%s", valid_options->arg[i]->name);
65b48a81 1458 if (valid_options->name[i + 1] != NULL)
6cb06a8c 1459 gdb_printf (file, ", ");
1285ce86 1460 file->wrap_here (2);
65b48a81 1461 }
6cb06a8c 1462 gdb_printf (file, "\n");
65b48a81 1463 }
471b9d15
MR
1464
1465 valid_args = valid_options_and_args->args;
1466 if (valid_args != NULL)
1467 {
1468 size_t i, j;
1469
1470 for (i = 0; valid_args[i].name != NULL; i++)
1471 {
f2028892
TO
1472 if (valid_args[i].values == NULL)
1473 continue;
6cb06a8c 1474 gdb_printf (file, _("\n\
471b9d15 1475 For the options above, the following values are supported for \"%s\":\n "),
6cb06a8c 1476 valid_args[i].name);
471b9d15
MR
1477 for (j = 0; valid_args[i].values[j] != NULL; j++)
1478 {
6cb06a8c 1479 gdb_printf (file, " %s", valid_args[i].values[j]);
1285ce86 1480 file->wrap_here (3);
471b9d15 1481 }
6cb06a8c 1482 gdb_printf (file, "\n");
471b9d15
MR
1483 }
1484 }
65b48a81
PB
1485}
1486
1487/* A completion function for "set disassembler". */
1488
eb3ff9a5 1489static void
65b48a81 1490disassembler_options_completer (struct cmd_list_element *ignore,
eb3ff9a5 1491 completion_tracker &tracker,
65b48a81
PB
1492 const char *text, const char *word)
1493{
1494 struct gdbarch *gdbarch = get_current_arch ();
471b9d15
MR
1495 const disasm_options_and_args_t *opts_and_args
1496 = gdbarch_valid_disassembler_options (gdbarch);
65b48a81 1497
471b9d15 1498 if (opts_and_args != NULL)
65b48a81 1499 {
471b9d15
MR
1500 const disasm_options_t *opts = &opts_and_args->options;
1501
65b48a81
PB
1502 /* Only attempt to complete on the last option text. */
1503 const char *separator = strrchr (text, ',');
1504 if (separator != NULL)
1505 text = separator + 1;
f1735a53 1506 text = skip_spaces (text);
eb3ff9a5 1507 complete_on_enum (tracker, opts->name, text, word);
65b48a81 1508 }
65b48a81
PB
1509}
1510
1511
1512/* Initialization code. */
1513
6c265988 1514void _initialize_disasm ();
65b48a81 1515void
6c265988 1516_initialize_disasm ()
65b48a81 1517{
65b48a81 1518 /* Add the command that controls the disassembler options. */
af7f8f52
SM
1519 set_show_commands set_show_disas_opts
1520 = add_setshow_string_noescape_cmd ("disassembler-options", no_class,
1521 &prospective_options, _("\
65b48a81 1522Set the disassembler options.\n\
7c9ee61b 1523Usage: set disassembler-options OPTION [,OPTION]...\n\n\
89549d7f 1524See: 'show disassembler-options' for valid option values."), _("\
65b48a81
PB
1525Show the disassembler options."), NULL,
1526 set_disassembler_options_sfunc,
1527 show_disassembler_options_sfunc,
1528 &setlist, &showlist);
af7f8f52 1529 set_cmd_completer (set_show_disas_opts.set, disassembler_options_completer);
4cbe4ca5
AB
1530
1531
1532 /* All the 'maint set|show libopcodes-styling' sub-commands. */
1533 static struct cmd_list_element *maint_set_libopcodes_styling_cmdlist;
1534 static struct cmd_list_element *maint_show_libopcodes_styling_cmdlist;
1535
1536 /* Adds 'maint set|show libopcodes-styling'. */
1537 add_setshow_prefix_cmd ("libopcodes-styling", class_maintenance,
1538 _("Set libopcodes-styling specific variables."),
1539 _("Show libopcodes-styling specific variables."),
1540 &maint_set_libopcodes_styling_cmdlist,
1541 &maint_show_libopcodes_styling_cmdlist,
1542 &maintenance_set_cmdlist,
1543 &maintenance_show_cmdlist);
1544
1545 /* Adds 'maint set|show gnu-source-highlight enabled'. */
1546 add_setshow_boolean_cmd ("enabled", class_maintenance,
1547 &use_libopcodes_styling_option, _("\
1548Set whether the libopcodes styling support should be used."), _("\
1549Show whether the libopcodes styling support should be used."),_("\
1550When enabled, GDB will try to make use of the builtin libopcodes styling\n\
1551support, to style the disassembler output. Not every architecture has\n\
1552styling support within libopcodes, so enabling this is not a guarantee\n\
1553that libopcodes styling will be available.\n\
1554\n\
1555When this option is disabled, GDB will make use of the Python Pygments\n\
1556package (if available) to style the disassembler output.\n\
1557\n\
1558All disassembler styling can be disabled with:\n\
1559\n\
1560 set style disassembler enabled off"),
1561 set_use_libopcodes_styling,
1562 show_use_libopcodes_styling,
1563 &maint_set_libopcodes_styling_cmdlist,
1564 &maint_show_libopcodes_styling_cmdlist);
65b48a81 1565}