Add back gdb_pretty_print_insn
[deliverable/binutils-gdb.git] / gdb / disasm.c
1 /* Disassemble support for GDB.
2
3 Copyright (C) 2000-2017 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 "target.h"
22 #include "value.h"
23 #include "ui-out.h"
24 #include "disasm.h"
25 #include "gdbcore.h"
26 #include "dis-asm.h"
27 #include "source.h"
28 #include <algorithm>
29
30 /* Disassemble functions.
31 FIXME: We should get rid of all the duplicate code in gdb that does
32 the same thing: disassemble_command() and the gdbtk variation. */
33
34 /* This structure is used to store line number information for the
35 deprecated /m option.
36 We need a different sort of line table from the normal one cuz we can't
37 depend upon implicit line-end pc's for lines to do the
38 reordering in this function. */
39
40 struct deprecated_dis_line_entry
41 {
42 int line;
43 CORE_ADDR start_pc;
44 CORE_ADDR end_pc;
45 };
46
47 /* This Structure is used to store line number information.
48 We need a different sort of line table from the normal one cuz we can't
49 depend upon implicit line-end pc's for lines to do the
50 reordering in this function. */
51
52 struct dis_line_entry
53 {
54 struct symtab *symtab;
55 int line;
56 };
57
58 /* Hash function for dis_line_entry. */
59
60 static hashval_t
61 hash_dis_line_entry (const void *item)
62 {
63 const struct dis_line_entry *dle = (const struct dis_line_entry *) item;
64
65 return htab_hash_pointer (dle->symtab) + dle->line;
66 }
67
68 /* Equal function for dis_line_entry. */
69
70 static int
71 eq_dis_line_entry (const void *item_lhs, const void *item_rhs)
72 {
73 const struct dis_line_entry *lhs = (const struct dis_line_entry *) item_lhs;
74 const struct dis_line_entry *rhs = (const struct dis_line_entry *) item_rhs;
75
76 return (lhs->symtab == rhs->symtab
77 && lhs->line == rhs->line);
78 }
79
80 /* Create the table to manage lines for mixed source/disassembly. */
81
82 static htab_t
83 allocate_dis_line_table (void)
84 {
85 return htab_create_alloc (41,
86 hash_dis_line_entry, eq_dis_line_entry,
87 xfree, xcalloc, xfree);
88 }
89
90 /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
91
92 static void
93 add_dis_line_entry (htab_t table, struct symtab *symtab, int line)
94 {
95 void **slot;
96 struct dis_line_entry dle, *dlep;
97
98 dle.symtab = symtab;
99 dle.line = line;
100 slot = htab_find_slot (table, &dle, INSERT);
101 if (*slot == NULL)
102 {
103 dlep = XNEW (struct dis_line_entry);
104 dlep->symtab = symtab;
105 dlep->line = line;
106 *slot = dlep;
107 }
108 }
109
110 /* Return non-zero if SYMTAB, LINE are in TABLE. */
111
112 static int
113 line_has_code_p (htab_t table, struct symtab *symtab, int line)
114 {
115 struct dis_line_entry dle;
116
117 dle.symtab = symtab;
118 dle.line = line;
119 return htab_find (table, &dle) != NULL;
120 }
121
122 /* Wrapper of target_read_code. */
123
124 int
125 gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
126 unsigned int len,
127 struct disassemble_info *info)
128 {
129 return target_read_code (memaddr, myaddr, len);
130 }
131
132 /* Wrapper of memory_error. */
133
134 void
135 gdb_disassembler::dis_asm_memory_error (int err, bfd_vma memaddr,
136 struct disassemble_info *info)
137 {
138 gdb_disassembler *self
139 = static_cast<gdb_disassembler *>(info->application_data);
140
141 self->m_err_memaddr = memaddr;
142 }
143
144 /* Wrapper of print_address. */
145
146 void
147 gdb_disassembler::dis_asm_print_address (bfd_vma addr,
148 struct disassemble_info *info)
149 {
150 gdb_disassembler *self
151 = static_cast<gdb_disassembler *>(info->application_data);
152
153 print_address (self->arch (), addr, self->stream ());
154 }
155
156 static int
157 compare_lines (const void *mle1p, const void *mle2p)
158 {
159 struct deprecated_dis_line_entry *mle1, *mle2;
160 int val;
161
162 mle1 = (struct deprecated_dis_line_entry *) mle1p;
163 mle2 = (struct deprecated_dis_line_entry *) mle2p;
164
165 /* End of sequence markers have a line number of 0 but don't want to
166 be sorted to the head of the list, instead sort by PC. */
167 if (mle1->line == 0 || mle2->line == 0)
168 {
169 val = mle1->start_pc - mle2->start_pc;
170 if (val == 0)
171 val = mle1->line - mle2->line;
172 }
173 else
174 {
175 val = mle1->line - mle2->line;
176 if (val == 0)
177 val = mle1->start_pc - mle2->start_pc;
178 }
179 return val;
180 }
181
182 /* See disasm.h. */
183
184 int
185 gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout,
186 const struct disasm_insn *insn,
187 int flags)
188 {
189 /* parts of the symbolic representation of the address */
190 int unmapped;
191 int offset;
192 int line;
193 int size;
194 struct cleanup *ui_out_chain;
195 char *filename = NULL;
196 char *name = NULL;
197 CORE_ADDR pc;
198
199 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
200 pc = insn->addr;
201
202 if (insn->number != 0)
203 {
204 uiout->field_fmt ("insn-number", "%u", insn->number);
205 uiout->text ("\t");
206 }
207
208 if ((flags & DISASSEMBLY_SPECULATIVE) != 0)
209 {
210 if (insn->is_speculative)
211 {
212 uiout->field_string ("is-speculative", "?");
213
214 /* The speculative execution indication overwrites the first
215 character of the PC prefix.
216 We assume a PC prefix length of 3 characters. */
217 if ((flags & DISASSEMBLY_OMIT_PC) == 0)
218 uiout->text (pc_prefix (pc) + 1);
219 else
220 uiout->text (" ");
221 }
222 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
223 uiout->text (pc_prefix (pc));
224 else
225 uiout->text (" ");
226 }
227 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
228 uiout->text (pc_prefix (pc));
229 uiout->field_core_addr ("address", gdbarch, pc);
230
231 if (!build_address_symbolic (gdbarch, pc, 0, &name, &offset, &filename,
232 &line, &unmapped))
233 {
234 /* We don't care now about line, filename and unmapped. But we might in
235 the future. */
236 uiout->text (" <");
237 if ((flags & DISASSEMBLY_OMIT_FNAME) == 0)
238 uiout->field_string ("func-name", name);
239 uiout->text ("+");
240 uiout->field_int ("offset", offset);
241 uiout->text (">:\t");
242 }
243 else
244 uiout->text (":\t");
245
246 if (filename != NULL)
247 xfree (filename);
248 if (name != NULL)
249 xfree (name);
250
251 struct ui_file *stb = mem_fileopen ();
252 make_cleanup_ui_file_delete (stb);
253
254 if (flags & DISASSEMBLY_RAW_INSN)
255 {
256 CORE_ADDR end_pc;
257 bfd_byte data;
258 int err;
259 const char *spacer = "";
260
261 /* Build the opcodes using a temporary stream so we can
262 write them out in a single go for the MI. */
263 struct ui_file *opcode_stream = mem_fileopen ();
264 struct cleanup *cleanups =
265 make_cleanup_ui_file_delete (opcode_stream);
266
267 size = gdb_print_insn (gdbarch, pc, stb, NULL);
268 end_pc = pc + size;
269
270 for (;pc < end_pc; ++pc)
271 {
272 read_code (pc, &data, 1);
273 fprintf_filtered (opcode_stream, "%s%02x",
274 spacer, (unsigned) data);
275 spacer = " ";
276 }
277
278 uiout->field_stream ("opcodes", opcode_stream);
279 uiout->text ("\t");
280
281 do_cleanups (cleanups);
282 }
283 else
284 size = gdb_print_insn (gdbarch, pc, stb, NULL);
285
286 uiout->field_stream ("inst", stb);
287 do_cleanups (ui_out_chain);
288 uiout->text ("\n");
289
290 return size;
291 }
292
293 static int
294 dump_insns (struct gdbarch *gdbarch,
295 struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high,
296 int how_many, int flags, CORE_ADDR *end_pc)
297 {
298 struct disasm_insn insn;
299 int num_displayed = 0;
300
301 memset (&insn, 0, sizeof (insn));
302 insn.addr = low;
303
304 while (insn.addr < high && (how_many < 0 || num_displayed < how_many))
305 {
306 int size;
307
308 size = gdb_pretty_print_insn (gdbarch, uiout, &insn, flags);
309 if (size <= 0)
310 break;
311
312 ++num_displayed;
313 insn.addr += size;
314
315 /* Allow user to bail out with ^C. */
316 QUIT;
317 }
318
319 if (end_pc != NULL)
320 *end_pc = insn.addr;
321
322 return num_displayed;
323 }
324
325 /* The idea here is to present a source-O-centric view of a
326 function to the user. This means that things are presented
327 in source order, with (possibly) out of order assembly
328 immediately following.
329
330 N.B. This view is deprecated. */
331
332 static void
333 do_mixed_source_and_assembly_deprecated
334 (struct gdbarch *gdbarch, struct ui_out *uiout,
335 struct symtab *symtab,
336 CORE_ADDR low, CORE_ADDR high,
337 int how_many, int flags)
338 {
339 int newlines = 0;
340 int nlines;
341 struct linetable_entry *le;
342 struct deprecated_dis_line_entry *mle;
343 struct symtab_and_line sal;
344 int i;
345 int out_of_order = 0;
346 int next_line = 0;
347 int num_displayed = 0;
348 print_source_lines_flags psl_flags = 0;
349 struct cleanup *ui_out_chain;
350 struct cleanup *ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
351 struct cleanup *ui_out_list_chain = make_cleanup (null_cleanup, 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 /* Skip any end-of-function markers. */
380 if (le[i].line == 0)
381 continue;
382
383 mle[newlines].line = le[i].line;
384 if (le[i].line > le[i + 1].line)
385 out_of_order = 1;
386 mle[newlines].start_pc = le[i].pc;
387 mle[newlines].end_pc = le[i + 1].pc;
388 newlines++;
389 }
390
391 /* If we're on the last line, and it's part of the function,
392 then we need to get the end pc in a special way. */
393
394 if (i == nlines - 1 && le[i].pc < high)
395 {
396 mle[newlines].line = le[i].line;
397 mle[newlines].start_pc = le[i].pc;
398 sal = find_pc_line (le[i].pc, 0);
399 mle[newlines].end_pc = sal.end;
400 newlines++;
401 }
402
403 /* Now, sort mle by line #s (and, then by addresses within lines). */
404
405 if (out_of_order)
406 qsort (mle, newlines, sizeof (struct deprecated_dis_line_entry),
407 compare_lines);
408
409 /* Now, for each line entry, emit the specified lines (unless
410 they have been emitted before), followed by the assembly code
411 for that line. */
412
413 ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
414
415 for (i = 0; i < newlines; i++)
416 {
417 /* Print out everything from next_line to the current line. */
418 if (mle[i].line >= next_line)
419 {
420 if (next_line != 0)
421 {
422 /* Just one line to print. */
423 if (next_line == mle[i].line)
424 {
425 ui_out_tuple_chain
426 = make_cleanup_ui_out_tuple_begin_end (uiout,
427 "src_and_asm_line");
428 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
429 }
430 else
431 {
432 /* Several source lines w/o asm instructions associated. */
433 for (; next_line < mle[i].line; next_line++)
434 {
435 struct cleanup *ui_out_list_chain_line;
436 struct cleanup *ui_out_tuple_chain_line;
437
438 ui_out_tuple_chain_line
439 = make_cleanup_ui_out_tuple_begin_end (uiout,
440 "src_and_asm_line");
441 print_source_lines (symtab, next_line, next_line + 1,
442 psl_flags);
443 ui_out_list_chain_line
444 = make_cleanup_ui_out_list_begin_end (uiout,
445 "line_asm_insn");
446 do_cleanups (ui_out_list_chain_line);
447 do_cleanups (ui_out_tuple_chain_line);
448 }
449 /* Print the last line and leave list open for
450 asm instructions to be added. */
451 ui_out_tuple_chain
452 = make_cleanup_ui_out_tuple_begin_end (uiout,
453 "src_and_asm_line");
454 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
455 }
456 }
457 else
458 {
459 ui_out_tuple_chain
460 = make_cleanup_ui_out_tuple_begin_end (uiout,
461 "src_and_asm_line");
462 print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags);
463 }
464
465 next_line = mle[i].line + 1;
466 ui_out_list_chain
467 = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
468 }
469
470 num_displayed += dump_insns (gdbarch, uiout,
471 mle[i].start_pc, mle[i].end_pc,
472 how_many, flags, NULL);
473
474 /* When we've reached the end of the mle array, or we've seen the last
475 assembly range for this source line, close out the list/tuple. */
476 if (i == (newlines - 1) || mle[i + 1].line > mle[i].line)
477 {
478 do_cleanups (ui_out_list_chain);
479 do_cleanups (ui_out_tuple_chain);
480 ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
481 ui_out_list_chain = make_cleanup (null_cleanup, 0);
482 uiout->text ("\n");
483 }
484 if (how_many >= 0 && num_displayed >= how_many)
485 break;
486 }
487 do_cleanups (ui_out_chain);
488 }
489
490 /* The idea here is to present a source-O-centric view of a
491 function to the user. This means that things are presented
492 in source order, with (possibly) out of order assembly
493 immediately following. */
494
495 static void
496 do_mixed_source_and_assembly (struct gdbarch *gdbarch,
497 struct ui_out *uiout,
498 struct symtab *main_symtab,
499 CORE_ADDR low, CORE_ADDR high,
500 int how_many, int flags)
501 {
502 const struct linetable_entry *le, *first_le;
503 int i, nlines;
504 int num_displayed = 0;
505 print_source_lines_flags psl_flags = 0;
506 struct cleanup *ui_out_chain;
507 struct cleanup *ui_out_tuple_chain;
508 struct cleanup *ui_out_list_chain;
509 CORE_ADDR pc;
510 struct symtab *last_symtab;
511 int last_line;
512
513 gdb_assert (main_symtab != NULL && SYMTAB_LINETABLE (main_symtab) != NULL);
514
515 /* First pass: collect the list of all source files and lines.
516 We do this so that we can only print lines containing code once.
517 We try to print the source text leading up to the next instruction,
518 but if that text is for code that will be disassembled later, then
519 we'll want to defer printing it until later with its associated code. */
520
521 htab_up dis_line_table (allocate_dis_line_table ());
522
523 pc = low;
524
525 /* The prologue may be empty, but there may still be a line number entry
526 for the opening brace which is distinct from the first line of code.
527 If the prologue has been eliminated find_pc_line may return the source
528 line after the opening brace. We still want to print this opening brace.
529 first_le is used to implement this. */
530
531 nlines = SYMTAB_LINETABLE (main_symtab)->nitems;
532 le = SYMTAB_LINETABLE (main_symtab)->item;
533 first_le = NULL;
534
535 /* Skip all the preceding functions. */
536 for (i = 0; i < nlines && le[i].pc < low; i++)
537 continue;
538
539 if (i < nlines && le[i].pc < high)
540 first_le = &le[i];
541
542 /* Add lines for every pc value. */
543 while (pc < high)
544 {
545 struct symtab_and_line sal;
546 int length;
547
548 sal = find_pc_line (pc, 0);
549 length = gdb_insn_length (gdbarch, pc);
550 pc += length;
551
552 if (sal.symtab != NULL)
553 add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line);
554 }
555
556 /* Second pass: print the disassembly.
557
558 Output format, from an MI perspective:
559 The result is a ui_out list, field name "asm_insns", where elements have
560 name "src_and_asm_line".
561 Each element is a tuple of source line specs (field names line, file,
562 fullname), and field "line_asm_insn" which contains the disassembly.
563 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
564 opcodes, inst.
565
566 CLI output works on top of this because MI ignores ui_out_text output,
567 which is where we put file name and source line contents output.
568
569 Cleanup usage:
570 ui_out_chain
571 Handles the outer "asm_insns" list.
572 ui_out_tuple_chain
573 The tuples for each group of consecutive disassemblies.
574 ui_out_list_chain
575 List of consecutive source lines or disassembled insns. */
576
577 if (flags & DISASSEMBLY_FILENAME)
578 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
579
580 ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
581
582 ui_out_tuple_chain = NULL;
583 ui_out_list_chain = NULL;
584
585 last_symtab = NULL;
586 last_line = 0;
587 pc = low;
588
589 while (pc < high)
590 {
591 struct symtab_and_line sal;
592 CORE_ADDR end_pc;
593 int start_preceding_line_to_display = 0;
594 int end_preceding_line_to_display = 0;
595 int new_source_line = 0;
596
597 sal = find_pc_line (pc, 0);
598
599 if (sal.symtab != last_symtab)
600 {
601 /* New source file. */
602 new_source_line = 1;
603
604 /* If this is the first line of output, check for any preceding
605 lines. */
606 if (last_line == 0
607 && first_le != NULL
608 && first_le->line < sal.line)
609 {
610 start_preceding_line_to_display = first_le->line;
611 end_preceding_line_to_display = sal.line;
612 }
613 }
614 else
615 {
616 /* Same source file as last time. */
617 if (sal.symtab != NULL)
618 {
619 if (sal.line > last_line + 1 && last_line != 0)
620 {
621 int l;
622
623 /* Several preceding source lines. Print the trailing ones
624 not associated with code that we'll print later. */
625 for (l = sal.line - 1; l > last_line; --l)
626 {
627 if (line_has_code_p (dis_line_table.get (),
628 sal.symtab, l))
629 break;
630 }
631 if (l < sal.line - 1)
632 {
633 start_preceding_line_to_display = l + 1;
634 end_preceding_line_to_display = sal.line;
635 }
636 }
637 if (sal.line != last_line)
638 new_source_line = 1;
639 else
640 {
641 /* Same source line as last time. This can happen, depending
642 on the debug info. */
643 }
644 }
645 }
646
647 if (new_source_line)
648 {
649 /* Skip the newline if this is the first instruction. */
650 if (pc > low)
651 uiout->text ("\n");
652 if (ui_out_tuple_chain != NULL)
653 {
654 gdb_assert (ui_out_list_chain != NULL);
655 do_cleanups (ui_out_list_chain);
656 do_cleanups (ui_out_tuple_chain);
657 }
658 if (sal.symtab != last_symtab
659 && !(flags & DISASSEMBLY_FILENAME))
660 {
661 /* Remember MI ignores ui_out_text.
662 We don't have to do anything here for MI because MI
663 output includes the source specs for each line. */
664 if (sal.symtab != NULL)
665 {
666 uiout->text (symtab_to_filename_for_display (sal.symtab));
667 }
668 else
669 uiout->text ("unknown");
670 uiout->text (":\n");
671 }
672 if (start_preceding_line_to_display > 0)
673 {
674 /* Several source lines w/o asm instructions associated.
675 We need to preserve the structure of the output, so output
676 a bunch of line tuples with no asm entries. */
677 int l;
678 struct cleanup *ui_out_list_chain_line;
679 struct cleanup *ui_out_tuple_chain_line;
680
681 gdb_assert (sal.symtab != NULL);
682 for (l = start_preceding_line_to_display;
683 l < end_preceding_line_to_display;
684 ++l)
685 {
686 ui_out_tuple_chain_line
687 = make_cleanup_ui_out_tuple_begin_end (uiout,
688 "src_and_asm_line");
689 print_source_lines (sal.symtab, l, l + 1, psl_flags);
690 ui_out_list_chain_line
691 = make_cleanup_ui_out_list_begin_end (uiout,
692 "line_asm_insn");
693 do_cleanups (ui_out_list_chain_line);
694 do_cleanups (ui_out_tuple_chain_line);
695 }
696 }
697 ui_out_tuple_chain
698 = make_cleanup_ui_out_tuple_begin_end (uiout, "src_and_asm_line");
699 if (sal.symtab != NULL)
700 print_source_lines (sal.symtab, sal.line, sal.line + 1, psl_flags);
701 else
702 uiout->text (_("--- no source info for this pc ---\n"));
703 ui_out_list_chain
704 = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
705 }
706 else
707 {
708 /* Here we're appending instructions to an existing line.
709 By construction the very first insn will have a symtab
710 and follow the new_source_line path above. */
711 gdb_assert (ui_out_tuple_chain != NULL);
712 gdb_assert (ui_out_list_chain != NULL);
713 }
714
715 if (sal.end != 0)
716 end_pc = std::min (sal.end, high);
717 else
718 end_pc = pc + 1;
719 num_displayed += dump_insns (gdbarch, uiout, pc, end_pc,
720 how_many, flags, &end_pc);
721 pc = end_pc;
722
723 if (how_many >= 0 && num_displayed >= how_many)
724 break;
725
726 last_symtab = sal.symtab;
727 last_line = sal.line;
728 }
729
730 do_cleanups (ui_out_chain);
731 }
732
733 static void
734 do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
735 CORE_ADDR low, CORE_ADDR high,
736 int how_many, int flags)
737 {
738 struct cleanup *ui_out_chain;
739
740 ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
741
742 dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL);
743
744 do_cleanups (ui_out_chain);
745 }
746
747 /* Initialize the disassemble info struct ready for the specified
748 stream. */
749
750 static int ATTRIBUTE_PRINTF (2, 3)
751 fprintf_disasm (void *stream, const char *format, ...)
752 {
753 va_list args;
754
755 va_start (args, format);
756 vfprintf_filtered ((struct ui_file *) stream, format, args);
757 va_end (args);
758 /* Something non -ve. */
759 return 0;
760 }
761
762 gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
763 struct ui_file *file,
764 di_read_memory_ftype read_memory_func)
765 : m_gdbarch (gdbarch),
766 m_err_memaddr (0)
767 {
768 init_disassemble_info (&m_di, file, fprintf_disasm);
769 m_di.flavour = bfd_target_unknown_flavour;
770 m_di.memory_error_func = dis_asm_memory_error;
771 m_di.print_address_func = dis_asm_print_address;
772 /* NOTE: cagney/2003-04-28: The original code, from the old Insight
773 disassembler had a local optomization here. By default it would
774 access the executable file, instead of the target memory (there
775 was a growing list of exceptions though). Unfortunately, the
776 heuristic was flawed. Commands like "disassemble &variable"
777 didn't work as they relied on the access going to the target.
778 Further, it has been supperseeded by trust-read-only-sections
779 (although that should be superseeded by target_trust..._p()). */
780 m_di.read_memory_func = read_memory_func;
781 m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
782 m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
783 m_di.endian = gdbarch_byte_order (gdbarch);
784 m_di.endian_code = gdbarch_byte_order_for_code (gdbarch);
785 m_di.application_data = this;
786 disassemble_init_for_target (&m_di);
787 }
788
789 int
790 gdb_disassembler::print_insn (CORE_ADDR memaddr,
791 int *branch_delay_insns)
792 {
793 m_err_memaddr = 0;
794
795 int length = gdbarch_print_insn (arch (), memaddr, &m_di);
796
797 if (length < 0)
798 memory_error (TARGET_XFER_E_IO, m_err_memaddr);
799
800 if (branch_delay_insns != NULL)
801 {
802 if (m_di.insn_info_valid)
803 *branch_delay_insns = m_di.branch_delay_insns;
804 else
805 *branch_delay_insns = 0;
806 }
807 return length;
808 }
809
810 void
811 gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
812 int flags, int how_many,
813 CORE_ADDR low, CORE_ADDR high)
814 {
815 struct symtab *symtab;
816 int nlines = -1;
817
818 /* Assume symtab is valid for whole PC range. */
819 symtab = find_pc_line_symtab (low);
820
821 if (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL)
822 nlines = SYMTAB_LINETABLE (symtab)->nitems;
823
824 if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
825 || nlines <= 0)
826 do_assembly_only (gdbarch, uiout, low, high, how_many, flags);
827
828 else if (flags & DISASSEMBLY_SOURCE)
829 do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high,
830 how_many, flags);
831
832 else if (flags & DISASSEMBLY_SOURCE_DEPRECATED)
833 do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab,
834 low, high, how_many, flags);
835
836 gdb_flush (gdb_stdout);
837 }
838
839 /* Print the instruction at address MEMADDR in debugged memory,
840 on STREAM. Returns the length of the instruction, in bytes,
841 and, if requested, the number of branch delay slot instructions. */
842
843 int
844 gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
845 struct ui_file *stream, int *branch_delay_insns)
846 {
847
848 gdb_disassembler di (gdbarch, stream);
849
850 return di.print_insn (memaddr, branch_delay_insns);
851 }
852
853 /* Return the length in bytes of the instruction at address MEMADDR in
854 debugged memory. */
855
856 int
857 gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
858 {
859 return gdb_print_insn (gdbarch, addr, null_stream (), NULL);
860 }
861
862 /* fprintf-function for gdb_buffered_insn_length. This function is a
863 nop, we don't want to print anything, we just want to compute the
864 length of the insn. */
865
866 static int ATTRIBUTE_PRINTF (2, 3)
867 gdb_buffered_insn_length_fprintf (void *stream, const char *format, ...)
868 {
869 return 0;
870 }
871
872 /* Initialize a struct disassemble_info for gdb_buffered_insn_length. */
873
874 static void
875 gdb_buffered_insn_length_init_dis (struct gdbarch *gdbarch,
876 struct disassemble_info *di,
877 const gdb_byte *insn, int max_len,
878 CORE_ADDR addr)
879 {
880 init_disassemble_info (di, NULL, gdb_buffered_insn_length_fprintf);
881
882 /* init_disassemble_info installs buffer_read_memory, etc.
883 so we don't need to do that here.
884 The cast is necessary until disassemble_info is const-ified. */
885 di->buffer = (gdb_byte *) insn;
886 di->buffer_length = max_len;
887 di->buffer_vma = addr;
888
889 di->arch = gdbarch_bfd_arch_info (gdbarch)->arch;
890 di->mach = gdbarch_bfd_arch_info (gdbarch)->mach;
891 di->endian = gdbarch_byte_order (gdbarch);
892 di->endian_code = gdbarch_byte_order_for_code (gdbarch);
893
894 disassemble_init_for_target (di);
895 }
896
897 /* Return the length in bytes of INSN. MAX_LEN is the size of the
898 buffer containing INSN. */
899
900 int
901 gdb_buffered_insn_length (struct gdbarch *gdbarch,
902 const gdb_byte *insn, int max_len, CORE_ADDR addr)
903 {
904 struct disassemble_info di;
905
906 gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr);
907
908 return gdbarch_print_insn (gdbarch, addr, &di);
909 }
This page took 0.059584 seconds and 5 git commands to generate.