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