1 /* MI Command Set - disassemble commands.
2 Copyright (C) 2000, Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "mi-getopt.h"
29 static int gdb_dis_asm_read_memory (bfd_vma memaddr
, bfd_byte
* myaddr
, int len
,
30 disassemble_info
* info
);
31 static int compare_lines (const PTR mle1p
, const PTR mle2p
);
33 /* Disassemble functions. FIXME: these do not really belong here. We
34 should get rid of all the duplicate code in gdb that does the same
35 thing: disassemble_command() and the gdbtk variation. */
37 /* This Structure is used in mi_cmd_disassemble.
38 We need a different sort of line table from the normal one cuz we can't
39 depend upon implicit line-end pc's for lines to do the
40 reordering in this function. */
49 /* This variable determines where memory used for disassembly is read from. */
50 int gdb_disassemble_from_exec
= -1;
52 /* This is the memory_read_func for gdb_disassemble when we are
53 disassembling from the exec file. */
55 gdb_dis_asm_read_memory (bfd_vma memaddr
, bfd_byte
* myaddr
, int len
, disassemble_info
* info
)
57 extern struct target_ops exec_ops
;
61 res
= xfer_memory (memaddr
, myaddr
, len
, 0, &exec_ops
);
72 compare_lines (const PTR mle1p
, const PTR mle2p
)
74 struct dis_line_entry
*mle1
, *mle2
;
77 mle1
= (struct dis_line_entry
*) mle1p
;
78 mle2
= (struct dis_line_entry
*) mle2p
;
80 val
= mle1
->line
- mle2
->line
;
85 return mle1
->start_pc
- mle2
->start_pc
;
88 /* The arguments to be passed on the command line and parsed here are:
92 START-ADDRESS: address to start the disassembly at.
93 END-ADDRESS: address to end the disassembly at.
97 FILENAME: The name of the file where we want disassemble from.
98 LINE: The line around which we want to disassemble. It will
99 disassemble the function that contins that line.
100 HOW_MANY: Number of disassembly lines to display. In mixed mode, it
101 is the number of disassembly lines only, not counting the source
106 MODE: 0 or 1 for disassembly only, or mixed source and disassembly,
110 mi_cmd_disassemble (char *command
, char **argv
, int argc
)
118 int mixed_source_and_assembly
;
123 static disassemble_info di
;
124 static int di_initialized
;
128 /* To collect the instruction outputted from opcodes. */
129 static struct ui_stream
*stb
= NULL
;
131 /* parts of the symbolic representation of the address */
135 char *filename
= NULL
;
138 /* Which options have we processed? */
145 /* Options processing stuff. */
150 FILE_OPT
, LINE_OPT
, NUM_OPT
, START_OPT
, END_OPT
152 static struct mi_opt opts
[] =
162 /* Get the options with their arguments. Keep track of what we
166 int opt
= mi_getopt ("mi_cmd_disassemble", argc
, argv
, opts
,
170 switch ((enum opt
) opt
)
173 file_string
= xstrdup (optarg
);
177 line_num
= atoi (optarg
);
181 how_many
= atoi (optarg
);
185 low
= parse_and_eval_address (optarg
);
189 high
= parse_and_eval_address (optarg
);
197 /* Allow only filename + linenum (with how_many which is not
198 required) OR start_addr + and_addr */
200 if (!((line_seen
&& file_seen
&& num_seen
&& !start_seen
&& !end_seen
)
201 || (line_seen
&& file_seen
&& !num_seen
&& !start_seen
&& !end_seen
)
202 || (!line_seen
&& !file_seen
&& !num_seen
&& start_seen
&& end_seen
)))
203 error ("mi_cmd_disassemble: Usage: ( [-f filename -l linenum [-n howmany]] | [-s startaddr -e endaddr]) [--] mixed_mode.");
206 error ("mi_cmd_disassemble: Usage: [-f filename -l linenum [-n howmany]] [-s startaddr -e endaddr] [--] mixed_mode.");
208 mixed_source_and_assembly
= atoi (argv
[0]);
209 if ((mixed_source_and_assembly
!= 0) && (mixed_source_and_assembly
!= 1))
210 error ("mi_cmd_disassemble: Mixed_mode argument must be 0 or 1.");
212 /* We must get the function beginning and end where line_num is
215 if (line_seen
&& file_seen
)
217 s
= lookup_symtab (file_string
);
219 error ("mi_cmd_disassemble: Invalid filename.");
220 if (!find_line_pc (s
, line_num
, &start
))
221 error ("mi_cmd_disassemble: Invalid line number");
222 if (find_pc_partial_function (start
, NULL
, &low
, &high
) == 0)
223 error ("mi_cmd_disassemble: No function contains specified address");
228 /* We don't add a cleanup for this, because the allocation of
229 the stream is done once only for each gdb run, and we need to
230 keep it around until the end. Hopefully there won't be any
231 errors in the init code below, that make this function bail
233 stb
= ui_out_stream_new (uiout
);
234 INIT_DISASSEMBLE_INFO_NO_ARCH (di
, stb
->stream
,
235 (fprintf_ftype
) fprintf_unfiltered
);
236 di
.flavour
= bfd_target_unknown_flavour
;
237 di
.memory_error_func
= dis_asm_memory_error
;
238 di
.print_address_func
= dis_asm_print_address
;
242 di
.mach
= TARGET_PRINT_INSN_INFO
->mach
;
243 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
244 di
.endian
= BFD_ENDIAN_BIG
;
246 di
.endian
= BFD_ENDIAN_LITTLE
;
248 /* If gdb_disassemble_from_exec == -1, then we use the following heuristic to
249 determine whether or not to do disassembly from target memory or from the
252 If we're debugging a local process, read target memory, instead of the
253 exec file. This makes disassembly of functions in shared libs work
254 correctly. Also, read target memory if we are debugging native threads.
256 Else, we're debugging a remote process, and should disassemble from the
257 exec file for speed. However, this is no good if the target modifies its
258 code (for relocation, or whatever).
261 if (gdb_disassemble_from_exec
== -1)
263 if (strcmp (target_shortname
, "child") == 0
264 || strcmp (target_shortname
, "procfs") == 0
265 || strcmp (target_shortname
, "vxprocess") == 0
266 || strstr (target_shortname
, "-threads") != NULL
)
267 gdb_disassemble_from_exec
= 0; /* It's a child process, read inferior mem */
269 gdb_disassemble_from_exec
= 1; /* It's remote, read the exec file */
272 if (gdb_disassemble_from_exec
)
273 di
.read_memory_func
= gdb_dis_asm_read_memory
;
275 di
.read_memory_func
= dis_asm_read_memory
;
277 /* If just doing straight assembly, all we need to do is disassemble
278 everything between low and high. If doing mixed source/assembly,
279 we've got a totally different path to follow. */
281 if (mixed_source_and_assembly
)
283 /* Come here for mixed source/assembly */
284 /* The idea here is to present a source-O-centric view of a
285 function to the user. This means that things are presented
286 in source order, with (possibly) out of order assembly
287 immediately following. */
288 struct symtab
*symtab
;
289 struct linetable_entry
*le
;
292 struct dis_line_entry
*mle
;
293 struct symtab_and_line sal
;
298 /* Assume symtab is valid for whole PC range */
299 symtab
= find_pc_symtab (low
);
301 if (!symtab
|| !symtab
->linetable
)
304 /* First, convert the linetable to a bunch of my_line_entry's. */
306 le
= symtab
->linetable
->item
;
307 nlines
= symtab
->linetable
->nitems
;
312 mle
= (struct dis_line_entry
*) alloca (nlines
* sizeof (struct dis_line_entry
));
316 /* Copy linetable entries for this function into our data
317 structure, creating end_pc's and setting out_of_order as
320 /* First, skip all the preceding functions. */
322 for (i
= 0; i
< nlines
- 1 && le
[i
].pc
< low
; i
++);
324 /* Now, copy all entries before the end of this function. */
327 for (; i
< nlines
- 1 && le
[i
].pc
< high
; i
++)
329 if (le
[i
].line
== le
[i
+ 1].line
330 && le
[i
].pc
== le
[i
+ 1].pc
)
331 continue; /* Ignore duplicates */
333 mle
[newlines
].line
= le
[i
].line
;
334 if (le
[i
].line
> le
[i
+ 1].line
)
336 mle
[newlines
].start_pc
= le
[i
].pc
;
337 mle
[newlines
].end_pc
= le
[i
+ 1].pc
;
341 /* If we're on the last line, and it's part of the function,
342 then we need to get the end pc in a special way. */
347 mle
[newlines
].line
= le
[i
].line
;
348 mle
[newlines
].start_pc
= le
[i
].pc
;
349 sal
= find_pc_line (le
[i
].pc
, 0);
350 mle
[newlines
].end_pc
= sal
.end
;
354 /* Now, sort mle by line #s (and, then by addresses within
358 qsort (mle
, newlines
, sizeof (struct dis_line_entry
), compare_lines
);
360 /* Now, for each line entry, emit the specified lines (unless
361 they have been emitted before), followed by the assembly code
364 next_line
= 0; /* Force out first line */
365 ui_out_list_begin (uiout
, "asm_insns");
367 for (i
= 0; i
< newlines
; i
++)
370 /* Print out everything from next_line to the current line. */
371 if (mle
[i
].line
>= next_line
)
375 /* Just one line to print. */
376 if (next_line
== mle
[i
].line
)
378 ui_out_list_begin (uiout
, "src_and_asm_line");
379 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
383 /* Several source lines w/o asm instructions associated. */
384 for (; next_line
< mle
[i
].line
; next_line
++)
386 ui_out_list_begin (uiout
, "src_and_asm_line");
387 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
388 ui_out_list_begin (uiout
, "line_asm_insn");
389 ui_out_list_end (uiout
);
390 ui_out_list_end (uiout
);
392 /* Print the last line and leave list open for
393 asm instructions to be added. */
394 ui_out_list_begin (uiout
, "src_and_asm_line");
395 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
400 ui_out_list_begin (uiout
, "src_and_asm_line");
401 print_source_lines (symtab
, mle
[i
].line
, mle
[i
].line
+ 1, 0);
404 next_line
= mle
[i
].line
+ 1;
405 ui_out_list_begin (uiout
, "line_asm_insn");
406 if (i
+ 1 < newlines
&& mle
[i
+ 1].line
<= mle
[i
].line
)
409 for (pc
= mle
[i
].start_pc
; pc
< mle
[i
].end_pc
;)
414 if (num_displayed
>= how_many
)
419 ui_out_list_begin (uiout
, NULL
);
420 print_address_numeric (pc
, 1, stb
->stream
);
421 ui_out_field_stream (uiout
, "address", stb
);
423 if (!build_address_symbolic (pc
, 0, &name
, &offset
, &filename
, &line
, &unmapped
))
425 /* We don't care now about line, filename and
426 unmapped, but we might in the future. */
427 ui_out_field_string (uiout
, "func-name", name
);
428 ui_out_field_int (uiout
, "offset", offset
);
430 if (filename
!= NULL
)
435 ui_file_rewind (stb
->stream
);
436 pc
+= (*tm_print_insn
) (pc
, &di
);
437 ui_out_field_stream (uiout
, "inst", stb
);
438 ui_file_rewind (stb
->stream
);
439 ui_out_list_end (uiout
);
443 ui_out_list_end (uiout
);
444 ui_out_list_end (uiout
);
448 if (num_displayed
>= how_many
)
451 ui_out_list_end (uiout
);
456 ui_out_list_begin (uiout
, "asm_insns");
458 for (pc
= low
; pc
< high
;)
463 if (num_displayed
>= how_many
)
468 ui_out_list_begin (uiout
, NULL
);
469 print_address_numeric (pc
, 1, stb
->stream
);
470 ui_out_field_stream (uiout
, "address", stb
);
472 if (!build_address_symbolic (pc
, 0, &name
, &offset
, &filename
, &line
, &unmapped
))
474 /* We don't care now about line, filename and
475 unmapped. But we might in the future. */
476 ui_out_field_string (uiout
, "func-name", name
);
477 ui_out_field_int (uiout
, "offset", offset
);
479 if (filename
!= NULL
)
484 ui_file_rewind (stb
->stream
);
485 pc
+= (*tm_print_insn
) (pc
, &di
);
486 ui_out_field_stream (uiout
, "inst", stb
);
487 ui_file_rewind (stb
->stream
);
488 ui_out_list_end (uiout
);
490 ui_out_list_end (uiout
);
492 gdb_flush (gdb_stdout
);
497 /* Local variables: */
498 /* change-log-default-name: "ChangeLog-mi" */