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