]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/disasm.c
GDB: Add support for the new set/show disassembler-options commands.
[thirdparty/binutils-gdb.git] / gdb / disasm.c
CommitLineData
92df71f0 1/* Disassemble support for GDB.
1bac305b 2
61baf725 3 Copyright (C) 2000-2017 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"
92df71f0
FN
22#include "target.h"
23#include "value.h"
24#include "ui-out.h"
92df71f0 25#include "disasm.h"
810ecf9f 26#include "gdbcore.h"
65b48a81 27#include "gdbcmd.h"
a89aa300 28#include "dis-asm.h"
6ff0ba5f 29#include "source.h"
65b48a81 30#include "safe-ctype.h"
325fac50 31#include <algorithm>
92df71f0
FN
32
33/* Disassemble functions.
34 FIXME: We should get rid of all the duplicate code in gdb that does
0963b4bd 35 the same thing: disassemble_command() and the gdbtk variation. */
92df71f0 36
65b48a81
PB
37/* This variable is used to hold the prospective disassembler_options value
38 which is set by the "set disassembler_options" command. */
39static char *prospective_options = NULL;
40
6ff0ba5f
DE
41/* This structure is used to store line number information for the
42 deprecated /m option.
92df71f0
FN
43 We need a different sort of line table from the normal one cuz we can't
44 depend upon implicit line-end pc's for lines to do the
45 reordering in this function. */
46
6ff0ba5f 47struct deprecated_dis_line_entry
92df71f0
FN
48{
49 int line;
50 CORE_ADDR start_pc;
51 CORE_ADDR end_pc;
52};
53
6ff0ba5f
DE
54/* This Structure is used to store line number information.
55 We need a different sort of line table from the normal one cuz we can't
56 depend upon implicit line-end pc's for lines to do the
57 reordering in this function. */
58
59struct dis_line_entry
60{
61 struct symtab *symtab;
62 int line;
63};
64
65/* Hash function for dis_line_entry. */
66
67static hashval_t
68hash_dis_line_entry (const void *item)
69{
9a3c8263 70 const struct dis_line_entry *dle = (const struct dis_line_entry *) item;
6ff0ba5f
DE
71
72 return htab_hash_pointer (dle->symtab) + dle->line;
73}
74
75/* Equal function for dis_line_entry. */
76
77static int
78eq_dis_line_entry (const void *item_lhs, const void *item_rhs)
79{
9a3c8263
SM
80 const struct dis_line_entry *lhs = (const struct dis_line_entry *) item_lhs;
81 const struct dis_line_entry *rhs = (const struct dis_line_entry *) item_rhs;
6ff0ba5f
DE
82
83 return (lhs->symtab == rhs->symtab
84 && lhs->line == rhs->line);
85}
86
87/* Create the table to manage lines for mixed source/disassembly. */
88
89static htab_t
90allocate_dis_line_table (void)
91{
92 return htab_create_alloc (41,
93 hash_dis_line_entry, eq_dis_line_entry,
94 xfree, xcalloc, xfree);
95}
96
4a099de2 97/* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
6ff0ba5f
DE
98
99static void
4a099de2 100add_dis_line_entry (htab_t table, struct symtab *symtab, int line)
6ff0ba5f
DE
101{
102 void **slot;
103 struct dis_line_entry dle, *dlep;
104
105 dle.symtab = symtab;
106 dle.line = line;
107 slot = htab_find_slot (table, &dle, INSERT);
108 if (*slot == NULL)
109 {
110 dlep = XNEW (struct dis_line_entry);
111 dlep->symtab = symtab;
112 dlep->line = line;
113 *slot = dlep;
114 }
115}
116
117/* Return non-zero if SYMTAB, LINE are in TABLE. */
118
119static int
120line_has_code_p (htab_t table, struct symtab *symtab, int line)
121{
122 struct dis_line_entry dle;
123
124 dle.symtab = symtab;
125 dle.line = line;
126 return htab_find (table, &dle) != NULL;
127}
128
e47ad6c0
YQ
129/* Wrapper of target_read_code. */
130
131int
132gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
133 unsigned int len,
134 struct disassemble_info *info)
810ecf9f 135{
283f7163 136 return target_read_code (memaddr, myaddr, len);
810ecf9f
AC
137}
138
e47ad6c0
YQ
139/* Wrapper of memory_error. */
140
141void
142gdb_disassembler::dis_asm_memory_error (int err, bfd_vma memaddr,
143 struct disassemble_info *info)
810ecf9f 144{
d8b49cf0
YQ
145 gdb_disassembler *self
146 = static_cast<gdb_disassembler *>(info->application_data);
147
148 self->m_err_memaddr = memaddr;
810ecf9f
AC
149}
150
e47ad6c0
YQ
151/* Wrapper of print_address. */
152
153void
154gdb_disassembler::dis_asm_print_address (bfd_vma addr,
155 struct disassemble_info *info)
810ecf9f 156{
e47ad6c0
YQ
157 gdb_disassembler *self
158 = static_cast<gdb_disassembler *>(info->application_data);
9a619af0 159
e47ad6c0 160 print_address (self->arch (), addr, self->stream ());
810ecf9f
AC
161}
162
92df71f0 163static int
bde58177 164compare_lines (const void *mle1p, const void *mle2p)
92df71f0 165{
6ff0ba5f 166 struct deprecated_dis_line_entry *mle1, *mle2;
92df71f0
FN
167 int val;
168
6ff0ba5f
DE
169 mle1 = (struct deprecated_dis_line_entry *) mle1p;
170 mle2 = (struct deprecated_dis_line_entry *) mle2p;
92df71f0 171
9011945e
AB
172 /* End of sequence markers have a line number of 0 but don't want to
173 be sorted to the head of the list, instead sort by PC. */
174 if (mle1->line == 0 || mle2->line == 0)
175 {
176 val = mle1->start_pc - mle2->start_pc;
177 if (val == 0)
178 val = mle1->line - mle2->line;
179 }
180 else
181 {
182 val = mle1->line - mle2->line;
183 if (val == 0)
184 val = mle1->start_pc - mle2->start_pc;
185 }
186 return val;
92df71f0
FN
187}
188
a50a4026 189/* See disasm.h. */
af70908d 190
a50a4026 191int
8b172ce7
PA
192gdb_pretty_print_disassembler::pretty_print_insn (struct ui_out *uiout,
193 const struct disasm_insn *insn,
194 int flags)
92df71f0 195{
92df71f0
FN
196 /* parts of the symbolic representation of the address */
197 int unmapped;
92df71f0
FN
198 int offset;
199 int line;
af70908d 200 int size;
3b31d625 201 struct cleanup *ui_out_chain;
af70908d
MM
202 char *filename = NULL;
203 char *name = NULL;
a50a4026 204 CORE_ADDR pc;
8b172ce7 205 struct gdbarch *gdbarch = arch ();
af70908d
MM
206
207 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
a50a4026
MM
208 pc = insn->addr;
209
210 if (insn->number != 0)
211 {
112e8700
SM
212 uiout->field_fmt ("insn-number", "%u", insn->number);
213 uiout->text ("\t");
a50a4026 214 }
92df71f0 215
a50a4026
MM
216 if ((flags & DISASSEMBLY_SPECULATIVE) != 0)
217 {
218 if (insn->is_speculative)
219 {
112e8700 220 uiout->field_string ("is-speculative", "?");
a50a4026
MM
221
222 /* The speculative execution indication overwrites the first
223 character of the PC prefix.
224 We assume a PC prefix length of 3 characters. */
225 if ((flags & DISASSEMBLY_OMIT_PC) == 0)
112e8700 226 uiout->text (pc_prefix (pc) + 1);
a50a4026 227 else
112e8700 228 uiout->text (" ");
a50a4026
MM
229 }
230 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
112e8700 231 uiout->text (pc_prefix (pc));
a50a4026 232 else
112e8700 233 uiout->text (" ");
a50a4026
MM
234 }
235 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
112e8700
SM
236 uiout->text (pc_prefix (pc));
237 uiout->field_core_addr ("address", gdbarch, pc);
af70908d
MM
238
239 if (!build_address_symbolic (gdbarch, pc, 0, &name, &offset, &filename,
240 &line, &unmapped))
92df71f0 241 {
af70908d
MM
242 /* We don't care now about line, filename and unmapped. But we might in
243 the future. */
112e8700 244 uiout->text (" <");
af70908d 245 if ((flags & DISASSEMBLY_OMIT_FNAME) == 0)
112e8700
SM
246 uiout->field_string ("func-name", name);
247 uiout->text ("+");
248 uiout->field_int ("offset", offset);
249 uiout->text (">:\t");
af70908d
MM
250 }
251 else
112e8700 252 uiout->text (":\t");
1211bce3 253
af70908d
MM
254 if (filename != NULL)
255 xfree (filename);
256 if (name != NULL)
257 xfree (name);
258
8b172ce7 259 m_insn_stb.clear ();
187808b0 260
af70908d
MM
261 if (flags & DISASSEMBLY_RAW_INSN)
262 {
263 CORE_ADDR end_pc;
264 bfd_byte data;
265 int err;
266 const char *spacer = "";
267
268 /* Build the opcodes using a temporary stream so we can
269 write them out in a single go for the MI. */
8b172ce7 270 m_opcode_stb.clear ();
946287b7 271
8b172ce7 272 size = m_di.print_insn (pc);
af70908d 273 end_pc = pc + size;
92df71f0 274
af70908d 275 for (;pc < end_pc; ++pc)
92df71f0 276 {
187808b0 277 read_code (pc, &data, 1);
8b172ce7 278 m_opcode_stb.printf ("%s%02x", spacer, (unsigned) data);
af70908d 279 spacer = " ";
92df71f0 280 }
af70908d 281
8b172ce7 282 uiout->field_stream ("opcodes", m_opcode_stb);
112e8700 283 uiout->text ("\t");
af70908d
MM
284 }
285 else
8b172ce7 286 size = m_di.print_insn (pc);
af70908d 287
8b172ce7 288 uiout->field_stream ("inst", m_insn_stb);
af70908d 289 do_cleanups (ui_out_chain);
112e8700 290 uiout->text ("\n");
af70908d
MM
291
292 return size;
293}
294
295static int
187808b0
PA
296dump_insns (struct gdbarch *gdbarch,
297 struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high,
298 int how_many, int flags, CORE_ADDR *end_pc)
af70908d 299{
a50a4026 300 struct disasm_insn insn;
af70908d
MM
301 int num_displayed = 0;
302
a50a4026
MM
303 memset (&insn, 0, sizeof (insn));
304 insn.addr = low;
305
8b172ce7
PA
306 gdb_pretty_print_disassembler disasm (gdbarch);
307
a50a4026 308 while (insn.addr < high && (how_many < 0 || num_displayed < how_many))
af70908d
MM
309 {
310 int size;
311
8b172ce7 312 size = disasm.pretty_print_insn (uiout, &insn, flags);
af70908d
MM
313 if (size <= 0)
314 break;
315
316 ++num_displayed;
a50a4026 317 insn.addr += size;
af70908d
MM
318
319 /* Allow user to bail out with ^C. */
320 QUIT;
92df71f0 321 }
6ff0ba5f
DE
322
323 if (end_pc != NULL)
a50a4026 324 *end_pc = insn.addr;
af70908d 325
92df71f0
FN
326 return num_displayed;
327}
328
329/* The idea here is to present a source-O-centric view of a
330 function to the user. This means that things are presented
331 in source order, with (possibly) out of order assembly
6ff0ba5f
DE
332 immediately following.
333
334 N.B. This view is deprecated. */
0963b4bd 335
92df71f0 336static void
6ff0ba5f 337do_mixed_source_and_assembly_deprecated
187808b0
PA
338 (struct gdbarch *gdbarch, struct ui_out *uiout,
339 struct symtab *symtab,
6ff0ba5f 340 CORE_ADDR low, CORE_ADDR high,
e47ad6c0 341 int how_many, int flags)
92df71f0
FN
342{
343 int newlines = 0;
6ff0ba5f
DE
344 int nlines;
345 struct linetable_entry *le;
346 struct deprecated_dis_line_entry *mle;
92df71f0
FN
347 struct symtab_and_line sal;
348 int i;
349 int out_of_order = 0;
350 int next_line = 0;
92df71f0 351 int num_displayed = 0;
8d297bbf 352 print_source_lines_flags psl_flags = 0;
3b31d625 353 struct cleanup *ui_out_chain;
0127c0d3
JJ
354 struct cleanup *ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
355 struct cleanup *ui_out_list_chain = make_cleanup (null_cleanup, 0);
92df71f0 356
6ff0ba5f
DE
357 gdb_assert (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL);
358
359 nlines = SYMTAB_LINETABLE (symtab)->nitems;
360 le = SYMTAB_LINETABLE (symtab)->item;
361
4cd29721
MM
362 if (flags & DISASSEMBLY_FILENAME)
363 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
364
6ff0ba5f
DE
365 mle = (struct deprecated_dis_line_entry *)
366 alloca (nlines * sizeof (struct deprecated_dis_line_entry));
92df71f0
FN
367
368 /* Copy linetable entries for this function into our data
369 structure, creating end_pc's and setting out_of_order as
370 appropriate. */
371
372 /* First, skip all the preceding functions. */
373
374 for (i = 0; i < nlines - 1 && le[i].pc < low; i++);
375
376 /* Now, copy all entries before the end of this function. */
377
378 for (; i < nlines - 1 && le[i].pc < high; i++)
379 {
380 if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc)
0963b4bd 381 continue; /* Ignore duplicates. */
92df71f0
FN
382
383 /* Skip any end-of-function markers. */
384 if (le[i].line == 0)
385 continue;
386
387 mle[newlines].line = le[i].line;
388 if (le[i].line > le[i + 1].line)
389 out_of_order = 1;
390 mle[newlines].start_pc = le[i].pc;
391 mle[newlines].end_pc = le[i + 1].pc;
392 newlines++;
393 }
394
395 /* If we're on the last line, and it's part of the function,
396 then we need to get the end pc in a special way. */
397
398 if (i == nlines - 1 && le[i].pc < high)
399 {
400 mle[newlines].line = le[i].line;
401 mle[newlines].start_pc = le[i].pc;
402 sal = find_pc_line (le[i].pc, 0);
403 mle[newlines].end_pc = sal.end;
404 newlines++;
405 }
406
6ff0ba5f 407 /* Now, sort mle by line #s (and, then by addresses within lines). */
92df71f0
FN
408
409 if (out_of_order)
6ff0ba5f
DE
410 qsort (mle, newlines, sizeof (struct deprecated_dis_line_entry),
411 compare_lines);
92df71f0
FN
412
413 /* Now, for each line entry, emit the specified lines (unless
414 they have been emitted before), followed by the assembly code
415 for that line. */
416
3b31d625 417 ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
92df71f0
FN
418
419 for (i = 0; i < newlines; i++)
420 {
92df71f0
FN
421 /* Print out everything from next_line to the current line. */
422 if (mle[i].line >= next_line)
423 {
424 if (next_line != 0)
425 {
0963b4bd 426 /* Just one line to print. */
92df71f0
FN
427 if (next_line == mle[i].line)
428 {
3b31d625
EZ
429 ui_out_tuple_chain
430 = make_cleanup_ui_out_tuple_begin_end (uiout,
431 "src_and_asm_line");
4cd29721 432 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
92df71f0
FN
433 }
434 else
435 {
0963b4bd 436 /* Several source lines w/o asm instructions associated. */
92df71f0
FN
437 for (; next_line < mle[i].line; next_line++)
438 {
3b31d625
EZ
439 struct cleanup *ui_out_list_chain_line;
440 struct cleanup *ui_out_tuple_chain_line;
441
442 ui_out_tuple_chain_line
443 = make_cleanup_ui_out_tuple_begin_end (uiout,
444 "src_and_asm_line");
92df71f0 445 print_source_lines (symtab, next_line, next_line + 1,
4cd29721 446 psl_flags);
3b31d625
EZ
447 ui_out_list_chain_line
448 = make_cleanup_ui_out_list_begin_end (uiout,
449 "line_asm_insn");
450 do_cleanups (ui_out_list_chain_line);
451 do_cleanups (ui_out_tuple_chain_line);
92df71f0
FN
452 }
453 /* Print the last line and leave list open for
0963b4bd 454 asm instructions to be added. */
3b31d625
EZ
455 ui_out_tuple_chain
456 = make_cleanup_ui_out_tuple_begin_end (uiout,
457 "src_and_asm_line");
4cd29721 458 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
92df71f0
FN
459 }
460 }
461 else
462 {
3b31d625 463 ui_out_tuple_chain
3e43a32a
MS
464 = make_cleanup_ui_out_tuple_begin_end (uiout,
465 "src_and_asm_line");
4cd29721 466 print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags);
92df71f0
FN
467 }
468
469 next_line = mle[i].line + 1;
3b31d625
EZ
470 ui_out_list_chain
471 = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
92df71f0
FN
472 }
473
187808b0 474 num_displayed += dump_insns (gdbarch, uiout,
13274fc3 475 mle[i].start_pc, mle[i].end_pc,
e47ad6c0 476 how_many, flags, NULL);
0127c0d3
JJ
477
478 /* When we've reached the end of the mle array, or we've seen the last
479 assembly range for this source line, close out the list/tuple. */
480 if (i == (newlines - 1) || mle[i + 1].line > mle[i].line)
92df71f0 481 {
3b31d625
EZ
482 do_cleanups (ui_out_list_chain);
483 do_cleanups (ui_out_tuple_chain);
0127c0d3
JJ
484 ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
485 ui_out_list_chain = make_cleanup (null_cleanup, 0);
112e8700 486 uiout->text ("\n");
92df71f0 487 }
0127c0d3
JJ
488 if (how_many >= 0 && num_displayed >= how_many)
489 break;
92df71f0 490 }
3b31d625 491 do_cleanups (ui_out_chain);
92df71f0
FN
492}
493
6ff0ba5f
DE
494/* The idea here is to present a source-O-centric view of a
495 function to the user. This means that things are presented
496 in source order, with (possibly) out of order assembly
497 immediately following. */
498
499static void
e47ad6c0
YQ
500do_mixed_source_and_assembly (struct gdbarch *gdbarch,
501 struct ui_out *uiout,
6ff0ba5f
DE
502 struct symtab *main_symtab,
503 CORE_ADDR low, CORE_ADDR high,
e47ad6c0 504 int how_many, int flags)
6ff0ba5f 505{
6ff0ba5f 506 const struct linetable_entry *le, *first_le;
6ff0ba5f 507 int i, nlines;
6ff0ba5f 508 int num_displayed = 0;
8d297bbf 509 print_source_lines_flags psl_flags = 0;
6ff0ba5f
DE
510 struct cleanup *ui_out_chain;
511 struct cleanup *ui_out_tuple_chain;
512 struct cleanup *ui_out_list_chain;
513 CORE_ADDR pc;
514 struct symtab *last_symtab;
515 int last_line;
6ff0ba5f
DE
516
517 gdb_assert (main_symtab != NULL && SYMTAB_LINETABLE (main_symtab) != NULL);
518
519 /* First pass: collect the list of all source files and lines.
520 We do this so that we can only print lines containing code once.
521 We try to print the source text leading up to the next instruction,
522 but if that text is for code that will be disassembled later, then
523 we'll want to defer printing it until later with its associated code. */
524
fc4007c9 525 htab_up dis_line_table (allocate_dis_line_table ());
6ff0ba5f
DE
526
527 pc = low;
528
529 /* The prologue may be empty, but there may still be a line number entry
530 for the opening brace which is distinct from the first line of code.
531 If the prologue has been eliminated find_pc_line may return the source
532 line after the opening brace. We still want to print this opening brace.
533 first_le is used to implement this. */
534
535 nlines = SYMTAB_LINETABLE (main_symtab)->nitems;
536 le = SYMTAB_LINETABLE (main_symtab)->item;
537 first_le = NULL;
538
539 /* Skip all the preceding functions. */
540 for (i = 0; i < nlines && le[i].pc < low; i++)
541 continue;
542
543 if (i < nlines && le[i].pc < high)
544 first_le = &le[i];
545
546 /* Add lines for every pc value. */
547 while (pc < high)
548 {
549 struct symtab_and_line sal;
550 int length;
551
552 sal = find_pc_line (pc, 0);
553 length = gdb_insn_length (gdbarch, pc);
554 pc += length;
555
556 if (sal.symtab != NULL)
fc4007c9 557 add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line);
6ff0ba5f
DE
558 }
559
560 /* Second pass: print the disassembly.
561
562 Output format, from an MI perspective:
563 The result is a ui_out list, field name "asm_insns", where elements have
564 name "src_and_asm_line".
565 Each element is a tuple of source line specs (field names line, file,
566 fullname), and field "line_asm_insn" which contains the disassembly.
567 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
568 opcodes, inst.
569
570 CLI output works on top of this because MI ignores ui_out_text output,
571 which is where we put file name and source line contents output.
572
573 Cleanup usage:
6ff0ba5f
DE
574 ui_out_chain
575 Handles the outer "asm_insns" list.
576 ui_out_tuple_chain
577 The tuples for each group of consecutive disassemblies.
578 ui_out_list_chain
579 List of consecutive source lines or disassembled insns. */
580
581 if (flags & DISASSEMBLY_FILENAME)
582 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
583
584 ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
585
586 ui_out_tuple_chain = NULL;
587 ui_out_list_chain = NULL;
588
589 last_symtab = NULL;
590 last_line = 0;
591 pc = low;
592
593 while (pc < high)
594 {
6ff0ba5f
DE
595 struct symtab_and_line sal;
596 CORE_ADDR end_pc;
597 int start_preceding_line_to_display = 0;
598 int end_preceding_line_to_display = 0;
599 int new_source_line = 0;
600
601 sal = find_pc_line (pc, 0);
602
603 if (sal.symtab != last_symtab)
604 {
605 /* New source file. */
606 new_source_line = 1;
607
608 /* If this is the first line of output, check for any preceding
609 lines. */
610 if (last_line == 0
611 && first_le != NULL
612 && first_le->line < sal.line)
613 {
614 start_preceding_line_to_display = first_le->line;
615 end_preceding_line_to_display = sal.line;
616 }
617 }
618 else
619 {
620 /* Same source file as last time. */
621 if (sal.symtab != NULL)
622 {
623 if (sal.line > last_line + 1 && last_line != 0)
624 {
625 int l;
626
627 /* Several preceding source lines. Print the trailing ones
628 not associated with code that we'll print later. */
629 for (l = sal.line - 1; l > last_line; --l)
630 {
fc4007c9
TT
631 if (line_has_code_p (dis_line_table.get (),
632 sal.symtab, l))
6ff0ba5f
DE
633 break;
634 }
635 if (l < sal.line - 1)
636 {
637 start_preceding_line_to_display = l + 1;
638 end_preceding_line_to_display = sal.line;
639 }
640 }
641 if (sal.line != last_line)
642 new_source_line = 1;
643 else
644 {
645 /* Same source line as last time. This can happen, depending
646 on the debug info. */
647 }
648 }
649 }
650
651 if (new_source_line)
652 {
653 /* Skip the newline if this is the first instruction. */
654 if (pc > low)
112e8700 655 uiout->text ("\n");
6ff0ba5f
DE
656 if (ui_out_tuple_chain != NULL)
657 {
658 gdb_assert (ui_out_list_chain != NULL);
659 do_cleanups (ui_out_list_chain);
660 do_cleanups (ui_out_tuple_chain);
661 }
662 if (sal.symtab != last_symtab
663 && !(flags & DISASSEMBLY_FILENAME))
664 {
665 /* Remember MI ignores ui_out_text.
666 We don't have to do anything here for MI because MI
667 output includes the source specs for each line. */
668 if (sal.symtab != NULL)
669 {
112e8700 670 uiout->text (symtab_to_filename_for_display (sal.symtab));
6ff0ba5f
DE
671 }
672 else
112e8700
SM
673 uiout->text ("unknown");
674 uiout->text (":\n");
6ff0ba5f
DE
675 }
676 if (start_preceding_line_to_display > 0)
677 {
678 /* Several source lines w/o asm instructions associated.
679 We need to preserve the structure of the output, so output
680 a bunch of line tuples with no asm entries. */
681 int l;
682 struct cleanup *ui_out_list_chain_line;
683 struct cleanup *ui_out_tuple_chain_line;
684
685 gdb_assert (sal.symtab != NULL);
686 for (l = start_preceding_line_to_display;
687 l < end_preceding_line_to_display;
688 ++l)
689 {
690 ui_out_tuple_chain_line
691 = make_cleanup_ui_out_tuple_begin_end (uiout,
692 "src_and_asm_line");
693 print_source_lines (sal.symtab, l, l + 1, psl_flags);
694 ui_out_list_chain_line
695 = make_cleanup_ui_out_list_begin_end (uiout,
696 "line_asm_insn");
697 do_cleanups (ui_out_list_chain_line);
698 do_cleanups (ui_out_tuple_chain_line);
699 }
700 }
701 ui_out_tuple_chain
702 = make_cleanup_ui_out_tuple_begin_end (uiout, "src_and_asm_line");
703 if (sal.symtab != NULL)
704 print_source_lines (sal.symtab, sal.line, sal.line + 1, psl_flags);
705 else
112e8700 706 uiout->text (_("--- no source info for this pc ---\n"));
6ff0ba5f
DE
707 ui_out_list_chain
708 = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
709 }
710 else
711 {
712 /* Here we're appending instructions to an existing line.
713 By construction the very first insn will have a symtab
714 and follow the new_source_line path above. */
715 gdb_assert (ui_out_tuple_chain != NULL);
716 gdb_assert (ui_out_list_chain != NULL);
717 }
718
719 if (sal.end != 0)
325fac50 720 end_pc = std::min (sal.end, high);
6ff0ba5f
DE
721 else
722 end_pc = pc + 1;
187808b0 723 num_displayed += dump_insns (gdbarch, uiout, pc, end_pc,
e47ad6c0 724 how_many, flags, &end_pc);
6ff0ba5f
DE
725 pc = end_pc;
726
727 if (how_many >= 0 && num_displayed >= how_many)
728 break;
729
730 last_symtab = sal.symtab;
731 last_line = sal.line;
732 }
733
734 do_cleanups (ui_out_chain);
6ff0ba5f 735}
92df71f0
FN
736
737static void
187808b0 738do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
92df71f0 739 CORE_ADDR low, CORE_ADDR high,
e47ad6c0 740 int how_many, int flags)
92df71f0 741{
3b31d625 742 struct cleanup *ui_out_chain;
92df71f0 743
3b31d625 744 ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
92df71f0 745
187808b0 746 dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL);
92df71f0 747
3b31d625 748 do_cleanups (ui_out_chain);
92df71f0
FN
749}
750
92bf2b80
AC
751/* Initialize the disassemble info struct ready for the specified
752 stream. */
753
a0b31db1 754static int ATTRIBUTE_PRINTF (2, 3)
242e8be5
AC
755fprintf_disasm (void *stream, const char *format, ...)
756{
757 va_list args;
9a619af0 758
242e8be5 759 va_start (args, format);
9a3c8263 760 vfprintf_filtered ((struct ui_file *) stream, format, args);
242e8be5
AC
761 va_end (args);
762 /* Something non -ve. */
763 return 0;
764}
765
e47ad6c0
YQ
766gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
767 struct ui_file *file,
768 di_read_memory_ftype read_memory_func)
d8b49cf0
YQ
769 : m_gdbarch (gdbarch),
770 m_err_memaddr (0)
92df71f0 771{
e47ad6c0
YQ
772 init_disassemble_info (&m_di, file, fprintf_disasm);
773 m_di.flavour = bfd_target_unknown_flavour;
774 m_di.memory_error_func = dis_asm_memory_error;
775 m_di.print_address_func = dis_asm_print_address;
2b6fd0d8
AC
776 /* NOTE: cagney/2003-04-28: The original code, from the old Insight
777 disassembler had a local optomization here. By default it would
778 access the executable file, instead of the target memory (there
ce2826aa 779 was a growing list of exceptions though). Unfortunately, the
2b6fd0d8
AC
780 heuristic was flawed. Commands like "disassemble &variable"
781 didn't work as they relied on the access going to the target.
782 Further, it has been supperseeded by trust-read-only-sections
783 (although that should be superseeded by target_trust..._p()). */
e47ad6c0
YQ
784 m_di.read_memory_func = read_memory_func;
785 m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
786 m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
787 m_di.endian = gdbarch_byte_order (gdbarch);
788 m_di.endian_code = gdbarch_byte_order_for_code (gdbarch);
789 m_di.application_data = this;
65b48a81 790 m_di.disassembler_options = get_disassembler_options (gdbarch);
e47ad6c0
YQ
791 disassemble_init_for_target (&m_di);
792}
793
794int
795gdb_disassembler::print_insn (CORE_ADDR memaddr,
796 int *branch_delay_insns)
797{
d8b49cf0
YQ
798 m_err_memaddr = 0;
799
e47ad6c0
YQ
800 int length = gdbarch_print_insn (arch (), memaddr, &m_di);
801
d8b49cf0
YQ
802 if (length < 0)
803 memory_error (TARGET_XFER_E_IO, m_err_memaddr);
804
e47ad6c0
YQ
805 if (branch_delay_insns != NULL)
806 {
807 if (m_di.insn_info_valid)
808 *branch_delay_insns = m_di.branch_delay_insns;
809 else
810 *branch_delay_insns = 0;
811 }
812 return length;
92bf2b80
AC
813}
814
815void
13274fc3 816gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
7a8eb317 817 int flags, int how_many,
9c419145 818 CORE_ADDR low, CORE_ADDR high)
92bf2b80 819{
34248c3a 820 struct symtab *symtab;
92bf2b80 821 int nlines = -1;
92df71f0 822
0963b4bd 823 /* Assume symtab is valid for whole PC range. */
34248c3a 824 symtab = find_pc_line_symtab (low);
92df71f0 825
8435453b 826 if (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL)
6ff0ba5f 827 nlines = SYMTAB_LINETABLE (symtab)->nitems;
92df71f0 828
6ff0ba5f
DE
829 if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
830 || nlines <= 0)
187808b0 831 do_assembly_only (gdbarch, uiout, low, high, how_many, flags);
92df71f0 832
e6158f16 833 else if (flags & DISASSEMBLY_SOURCE)
187808b0 834 do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high,
e47ad6c0 835 how_many, flags);
6ff0ba5f
DE
836
837 else if (flags & DISASSEMBLY_SOURCE_DEPRECATED)
187808b0 838 do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab,
e47ad6c0 839 low, high, how_many, flags);
92df71f0
FN
840
841 gdb_flush (gdb_stdout);
842}
810ecf9f 843
92bf2b80 844/* Print the instruction at address MEMADDR in debugged memory,
a4642986
MR
845 on STREAM. Returns the length of the instruction, in bytes,
846 and, if requested, the number of branch delay slot instructions. */
92bf2b80
AC
847
848int
13274fc3
UW
849gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
850 struct ui_file *stream, int *branch_delay_insns)
92bf2b80 851{
a4642986 852
e47ad6c0
YQ
853 gdb_disassembler di (gdbarch, stream);
854
855 return di.print_insn (memaddr, branch_delay_insns);
92bf2b80 856}
eda5a4d7 857
eda5a4d7
PA
858/* Return the length in bytes of the instruction at address MEMADDR in
859 debugged memory. */
860
861int
862gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
863{
d7e74731 864 return gdb_print_insn (gdbarch, addr, &null_stream, NULL);
eda5a4d7
PA
865}
866
867/* fprintf-function for gdb_buffered_insn_length. This function is a
868 nop, we don't want to print anything, we just want to compute the
869 length of the insn. */
870
871static int ATTRIBUTE_PRINTF (2, 3)
872gdb_buffered_insn_length_fprintf (void *stream, const char *format, ...)
873{
874 return 0;
875}
876
877/* Initialize a struct disassemble_info for gdb_buffered_insn_length. */
878
879static void
880gdb_buffered_insn_length_init_dis (struct gdbarch *gdbarch,
881 struct disassemble_info *di,
882 const gdb_byte *insn, int max_len,
883 CORE_ADDR addr)
884{
885 init_disassemble_info (di, NULL, gdb_buffered_insn_length_fprintf);
886
887 /* init_disassemble_info installs buffer_read_memory, etc.
888 so we don't need to do that here.
889 The cast is necessary until disassemble_info is const-ified. */
890 di->buffer = (gdb_byte *) insn;
891 di->buffer_length = max_len;
892 di->buffer_vma = addr;
893
894 di->arch = gdbarch_bfd_arch_info (gdbarch)->arch;
895 di->mach = gdbarch_bfd_arch_info (gdbarch)->mach;
896 di->endian = gdbarch_byte_order (gdbarch);
897 di->endian_code = gdbarch_byte_order_for_code (gdbarch);
898
65b48a81 899 di->disassembler_options = get_disassembler_options (gdbarch);
eda5a4d7
PA
900 disassemble_init_for_target (di);
901}
902
903/* Return the length in bytes of INSN. MAX_LEN is the size of the
904 buffer containing INSN. */
905
906int
907gdb_buffered_insn_length (struct gdbarch *gdbarch,
908 const gdb_byte *insn, int max_len, CORE_ADDR addr)
909{
910 struct disassemble_info di;
911
912 gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr);
913
914 return gdbarch_print_insn (gdbarch, addr, &di);
915}
65b48a81
PB
916
917char *
918get_disassembler_options (struct gdbarch *gdbarch)
919{
920 char **disassembler_options = gdbarch_disassembler_options (gdbarch);
921 if (disassembler_options == NULL)
922 return NULL;
923 return *disassembler_options;
924}
925
926void
927set_disassembler_options (char *prospective_options)
928{
929 struct gdbarch *gdbarch = get_current_arch ();
930 char **disassembler_options = gdbarch_disassembler_options (gdbarch);
931 const disasm_options_t *valid_options;
932 char *options = remove_whitespace_and_extra_commas (prospective_options);
933 char *opt;
934
935 /* Allow all architectures, even ones that do not support 'set disassembler',
936 to reset their disassembler options to NULL. */
937 if (options == NULL)
938 {
939 if (disassembler_options != NULL)
940 {
941 free (*disassembler_options);
942 *disassembler_options = NULL;
943 }
944 return;
945 }
946
947 valid_options = gdbarch_valid_disassembler_options (gdbarch);
948 if (valid_options == NULL)
949 {
950 fprintf_filtered (gdb_stdlog, _("\
951'set disassembler-options ...' is not supported on this architecture.\n"));
952 return;
953 }
954
955 /* Verify we have valid disassembler options. */
956 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
957 {
958 size_t i;
959 for (i = 0; valid_options->name[i] != NULL; i++)
960 if (disassembler_options_cmp (opt, valid_options->name[i]) == 0)
961 break;
962 if (valid_options->name[i] == NULL)
963 {
964 fprintf_filtered (gdb_stdlog,
965 _("Invalid disassembler option value: '%s'.\n"),
966 opt);
967 return;
968 }
969 }
970
971 free (*disassembler_options);
972 *disassembler_options = xstrdup (options);
973}
974
975static void
976set_disassembler_options_sfunc (char *args, int from_tty,
977 struct cmd_list_element *c)
978{
979 set_disassembler_options (prospective_options);
980}
981
982static void
983show_disassembler_options_sfunc (struct ui_file *file, int from_tty,
984 struct cmd_list_element *c, const char *value)
985{
986 struct gdbarch *gdbarch = get_current_arch ();
987 const disasm_options_t *valid_options;
988
989 const char *options = get_disassembler_options (gdbarch);
990 if (options == NULL)
991 options = "";
992
993 fprintf_filtered (file, _("The current disassembler options are '%s'\n"),
994 options);
995
996 valid_options = gdbarch_valid_disassembler_options (gdbarch);
997
998 if (valid_options == NULL)
999 return;
1000
1001 fprintf_filtered (file, _("\n\
1002The following disassembler options are supported for use with the\n\
1003'set disassembler-options <option>[,<option>...]' command:\n"));
1004
1005 if (valid_options->description != NULL)
1006 {
1007 size_t i, max_len = 0;
1008
1009 /* Compute the length of the longest option name. */
1010 for (i = 0; valid_options->name[i] != NULL; i++)
1011 {
1012 size_t len = strlen (valid_options->name[i]);
1013 if (max_len < len)
1014 max_len = len;
1015 }
1016
1017 for (i = 0, max_len++; valid_options->name[i] != NULL; i++)
1018 {
1019 fprintf_filtered (file, " %s", valid_options->name[i]);
1020 if (valid_options->description[i] != NULL)
1021 fprintf_filtered (file, "%*c %s",
1022 (int)(max_len - strlen (valid_options->name[i])), ' ',
1023 valid_options->description[i]);
1024 fprintf_filtered (file, "\n");
1025 }
1026 }
1027 else
1028 {
1029 size_t i;
1030 fprintf_filtered (file, " ");
1031 for (i = 0; valid_options->name[i] != NULL; i++)
1032 {
1033 fprintf_filtered (file, "%s", valid_options->name[i]);
1034 if (valid_options->name[i + 1] != NULL)
1035 fprintf_filtered (file, ", ");
1036 wrap_here (" ");
1037 }
1038 fprintf_filtered (file, "\n");
1039 }
1040}
1041
1042/* A completion function for "set disassembler". */
1043
1044static VEC (char_ptr) *
1045disassembler_options_completer (struct cmd_list_element *ignore,
1046 const char *text, const char *word)
1047{
1048 struct gdbarch *gdbarch = get_current_arch ();
1049 const disasm_options_t *opts = gdbarch_valid_disassembler_options (gdbarch);
1050
1051 if (opts != NULL)
1052 {
1053 /* Only attempt to complete on the last option text. */
1054 const char *separator = strrchr (text, ',');
1055 if (separator != NULL)
1056 text = separator + 1;
1057 text = skip_spaces_const (text);
1058 return complete_on_enum (opts->name, text, word);
1059 }
1060 return NULL;
1061}
1062
1063
1064/* Initialization code. */
1065
1066/* -Wmissing-prototypes */
1067extern initialize_file_ftype _initialize_disasm;
1068
1069void
1070_initialize_disasm (void)
1071{
1072 struct cmd_list_element *cmd;
1073
1074 /* Add the command that controls the disassembler options. */
1075 cmd = add_setshow_string_noescape_cmd ("disassembler-options", no_class,
1076 &prospective_options, _("\
1077Set the disassembler options.\n\
1078Usage: set disassembler-options <option>[,<option>...]\n\n\
1079See: 'show disassembler-options' for valid option values.\n"), _("\
1080Show the disassembler options."), NULL,
1081 set_disassembler_options_sfunc,
1082 show_disassembler_options_sfunc,
1083 &setlist, &showlist);
1084 set_cmd_completer (cmd, disassembler_options_completer);
1085}