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