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