1 /* Disassemble support for GDB.
2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
29 /* Disassemble functions.
30 FIXME: We should get rid of all the duplicate code in gdb that does
31 the same thing: disassemble_command() and the gdbtk variation. */
33 /* This Structure is used to store line number information.
34 We need a different sort of line table from the normal one cuz we can't
35 depend upon implicit line-end pc's for lines to do the
36 reordering in this function. */
45 /* This variable determines where memory used for disassembly is read from. */
46 int gdb_disassemble_from_exec
= -1;
48 /* This is the memory_read_func for gdb_disassemble when we are
49 disassembling from the exec file. */
51 gdb_dis_asm_read_memory (bfd_vma memaddr
, bfd_byte
* myaddr
,
52 unsigned int len
, disassemble_info
* info
)
54 extern struct target_ops exec_ops
;
58 res
= xfer_memory (memaddr
, myaddr
, len
, 0, 0, &exec_ops
);
69 compare_lines (const PTR mle1p
, const PTR mle2p
)
71 struct dis_line_entry
*mle1
, *mle2
;
74 mle1
= (struct dis_line_entry
*) mle1p
;
75 mle2
= (struct dis_line_entry
*) mle2p
;
77 val
= mle1
->line
- mle2
->line
;
82 return mle1
->start_pc
- mle2
->start_pc
;
86 dump_insns (struct ui_out
*uiout
, disassemble_info
* di
,
87 CORE_ADDR low
, CORE_ADDR high
,
88 int how_many
, struct ui_stream
*stb
)
90 int num_displayed
= 0;
93 /* parts of the symbolic representation of the address */
95 char *filename
= NULL
;
100 for (pc
= low
; pc
< high
;)
105 if (num_displayed
>= how_many
)
110 ui_out_tuple_begin (uiout
, NULL
);
111 ui_out_field_core_addr (uiout
, "address", pc
);
113 if (!build_address_symbolic (pc
, 0, &name
, &offset
, &filename
,
116 /* We don't care now about line, filename and
117 unmapped. But we might in the future. */
118 ui_out_text (uiout
, " <");
119 ui_out_field_string (uiout
, "func-name", name
);
120 ui_out_text (uiout
, "+");
121 ui_out_field_int (uiout
, "offset", offset
);
122 ui_out_text (uiout
, ">:\t");
124 if (filename
!= NULL
)
129 ui_file_rewind (stb
->stream
);
130 pc
+= TARGET_PRINT_INSN (pc
, di
);
131 ui_out_field_stream (uiout
, "inst", stb
);
132 ui_file_rewind (stb
->stream
);
133 ui_out_tuple_end (uiout
);
134 ui_out_text (uiout
, "\n");
136 return num_displayed
;
139 /* The idea here is to present a source-O-centric view of a
140 function to the user. This means that things are presented
141 in source order, with (possibly) out of order assembly
142 immediately following. */
144 do_mixed_source_and_assembly (struct ui_out
*uiout
,
145 struct disassemble_info
*di
, int nlines
,
146 struct linetable_entry
*le
,
147 CORE_ADDR low
, CORE_ADDR high
,
148 struct symtab
*symtab
,
149 int how_many
, struct ui_stream
*stb
)
152 struct dis_line_entry
*mle
;
153 struct symtab_and_line sal
;
155 int out_of_order
= 0;
158 int num_displayed
= 0;
160 mle
= (struct dis_line_entry
*) alloca (nlines
161 * sizeof (struct dis_line_entry
));
163 /* Copy linetable entries for this function into our data
164 structure, creating end_pc's and setting out_of_order as
167 /* First, skip all the preceding functions. */
169 for (i
= 0; i
< nlines
- 1 && le
[i
].pc
< low
; i
++);
171 /* Now, copy all entries before the end of this function. */
173 for (; i
< nlines
- 1 && le
[i
].pc
< high
; i
++)
175 if (le
[i
].line
== le
[i
+ 1].line
&& le
[i
].pc
== le
[i
+ 1].pc
)
176 continue; /* Ignore duplicates */
178 /* Skip any end-of-function markers. */
182 mle
[newlines
].line
= le
[i
].line
;
183 if (le
[i
].line
> le
[i
+ 1].line
)
185 mle
[newlines
].start_pc
= le
[i
].pc
;
186 mle
[newlines
].end_pc
= le
[i
+ 1].pc
;
190 /* If we're on the last line, and it's part of the function,
191 then we need to get the end pc in a special way. */
193 if (i
== nlines
- 1 && le
[i
].pc
< high
)
195 mle
[newlines
].line
= le
[i
].line
;
196 mle
[newlines
].start_pc
= le
[i
].pc
;
197 sal
= find_pc_line (le
[i
].pc
, 0);
198 mle
[newlines
].end_pc
= sal
.end
;
202 /* Now, sort mle by line #s (and, then by addresses within
206 qsort (mle
, newlines
, sizeof (struct dis_line_entry
), compare_lines
);
208 /* Now, for each line entry, emit the specified lines (unless
209 they have been emitted before), followed by the assembly code
212 ui_out_list_begin (uiout
, "asm_insns");
214 for (i
= 0; i
< newlines
; i
++)
217 /* Print out everything from next_line to the current line. */
218 if (mle
[i
].line
>= next_line
)
222 /* Just one line to print. */
223 if (next_line
== mle
[i
].line
)
225 ui_out_tuple_begin (uiout
, "src_and_asm_line");
226 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
230 /* Several source lines w/o asm instructions associated. */
231 for (; next_line
< mle
[i
].line
; next_line
++)
233 ui_out_tuple_begin (uiout
, "src_and_asm_line");
234 print_source_lines (symtab
, next_line
, next_line
+ 1,
236 ui_out_list_begin (uiout
, "line_asm_insn");
237 ui_out_list_end (uiout
);
238 ui_out_tuple_end (uiout
);
240 /* Print the last line and leave list open for
241 asm instructions to be added. */
242 ui_out_tuple_begin (uiout
, "src_and_asm_line");
243 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
248 ui_out_tuple_begin (uiout
, "src_and_asm_line");
249 print_source_lines (symtab
, mle
[i
].line
, mle
[i
].line
+ 1, 0);
252 next_line
= mle
[i
].line
+ 1;
253 ui_out_list_begin (uiout
, "line_asm_insn");
254 /* Don't close the list if the lines are not in order. */
255 if (i
< (newlines
- 1) && mle
[i
+ 1].line
<= mle
[i
].line
)
259 num_displayed
+= dump_insns (uiout
, di
, mle
[i
].start_pc
, mle
[i
].end_pc
,
263 ui_out_list_end (uiout
);
264 ui_out_tuple_end (uiout
);
265 ui_out_text (uiout
, "\n");
269 if (num_displayed
>= how_many
)
272 ui_out_list_end (uiout
);
277 do_assembly_only (struct ui_out
*uiout
, disassemble_info
* di
,
278 CORE_ADDR low
, CORE_ADDR high
,
279 int how_many
, struct ui_stream
*stb
)
281 int num_displayed
= 0;
283 ui_out_list_begin (uiout
, "asm_insns");
285 num_displayed
= dump_insns (uiout
, di
, low
, high
, how_many
, stb
);
287 ui_out_list_end (uiout
);
291 gdb_disassembly (struct ui_out
*uiout
,
294 int mixed_source_and_assembly
,
295 int how_many
, CORE_ADDR low
, CORE_ADDR high
)
297 static disassemble_info di
;
298 static int di_initialized
;
299 /* To collect the instruction outputted from opcodes. */
300 static struct ui_stream
*stb
= NULL
;
301 struct symtab
*symtab
= NULL
;
302 struct linetable_entry
*le
= NULL
;
307 /* We don't add a cleanup for this, because the allocation of
308 the stream is done once only for each gdb run, and we need to
309 keep it around until the end. Hopefully there won't be any
310 errors in the init code below, that make this function bail
312 stb
= ui_out_stream_new (uiout
);
313 INIT_DISASSEMBLE_INFO_NO_ARCH (di
, stb
->stream
,
314 (fprintf_ftype
) fprintf_unfiltered
);
315 di
.flavour
= bfd_target_unknown_flavour
;
316 di
.memory_error_func
= dis_asm_memory_error
;
317 di
.print_address_func
= dis_asm_print_address
;
321 di
.mach
= TARGET_PRINT_INSN_INFO
->mach
;
322 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
323 di
.endian
= BFD_ENDIAN_BIG
;
325 di
.endian
= BFD_ENDIAN_LITTLE
;
327 /* If gdb_disassemble_from_exec == -1, then we use the following heuristic to
328 determine whether or not to do disassembly from target memory or from the
331 If we're debugging a local process, read target memory, instead of the
332 exec file. This makes disassembly of functions in shared libs work
333 correctly. Also, read target memory if we are debugging native threads.
335 Else, we're debugging a remote process, and should disassemble from the
336 exec file for speed. However, this is no good if the target modifies its
337 code (for relocation, or whatever). */
339 if (gdb_disassemble_from_exec
== -1)
341 if (strcmp (target_shortname
, "child") == 0
342 || strcmp (target_shortname
, "procfs") == 0
343 || strcmp (target_shortname
, "vxprocess") == 0
344 || strstr (target_shortname
, "-threads") != NULL
)
345 gdb_disassemble_from_exec
= 0; /* It's a child process, read inferior mem */
347 gdb_disassemble_from_exec
= 1; /* It's remote, read the exec file */
350 if (gdb_disassemble_from_exec
)
351 di
.read_memory_func
= gdb_dis_asm_read_memory
;
353 di
.read_memory_func
= dis_asm_read_memory
;
355 /* Assume symtab is valid for whole PC range */
356 symtab
= find_pc_symtab (low
);
358 if (symtab
!= NULL
&& symtab
->linetable
!= NULL
)
360 /* Convert the linetable to a bunch of my_line_entry's. */
361 le
= symtab
->linetable
->item
;
362 nlines
= symtab
->linetable
->nitems
;
365 if (!mixed_source_and_assembly
|| nlines
<= 0
366 || symtab
== NULL
|| symtab
->linetable
== NULL
)
367 do_assembly_only (uiout
, &di
, low
, high
, how_many
, stb
);
369 else if (mixed_source_and_assembly
)
370 do_mixed_source_and_assembly (uiout
, &di
, nlines
, le
, low
,
371 high
, symtab
, how_many
, stb
);
373 gdb_flush (gdb_stdout
);