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