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