1 /* MI Command Set - disassemble commands.
2 Copyright 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
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
, unsigned 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
,
56 unsigned int len
, disassemble_info
* info
)
58 extern struct target_ops exec_ops
;
62 res
= xfer_memory (memaddr
, myaddr
, len
, 0, 0, &exec_ops
);
73 compare_lines (const PTR mle1p
, const PTR mle2p
)
75 struct dis_line_entry
*mle1
, *mle2
;
78 mle1
= (struct dis_line_entry
*) mle1p
;
79 mle2
= (struct dis_line_entry
*) mle2p
;
81 val
= mle1
->line
- mle2
->line
;
86 return mle1
->start_pc
- mle2
->start_pc
;
89 /* The arguments to be passed on the command line and parsed here are:
93 START-ADDRESS: address to start the disassembly at.
94 END-ADDRESS: address to end the disassembly at.
98 FILENAME: The name of the file where we want disassemble from.
99 LINE: The line around which we want to disassemble. It will
100 disassemble the function that contins that line.
101 HOW_MANY: Number of disassembly lines to display. In mixed mode, it
102 is the number of disassembly lines only, not counting the source
107 MODE: 0 or 1 for disassembly only, or mixed source and disassembly,
111 mi_cmd_disassemble (char *command
, char **argv
, int argc
)
116 int mixed_source_and_assembly
;
118 static disassemble_info di
;
119 static int di_initialized
;
123 /* To collect the instruction outputted from opcodes. */
124 static struct ui_stream
*stb
= NULL
;
126 /* parts of the symbolic representation of the address */
130 char *filename
= NULL
;
133 /* Which options have we processed ... */
140 /* ... and their corresponding value. */
141 char *file_string
= NULL
;
147 /* Options processing stuff. */
152 FILE_OPT
, LINE_OPT
, NUM_OPT
, START_OPT
, END_OPT
154 static struct mi_opt opts
[] =
164 /* Get the options with their arguments. Keep track of what we
168 int opt
= mi_getopt ("mi_cmd_disassemble", argc
, argv
, opts
,
172 switch ((enum opt
) opt
)
175 file_string
= xstrdup (optarg
);
179 line_num
= atoi (optarg
);
183 how_many
= atoi (optarg
);
187 low
= parse_and_eval_address (optarg
);
191 high
= parse_and_eval_address (optarg
);
199 /* Allow only filename + linenum (with how_many which is not
200 required) OR start_addr + and_addr */
202 if (!((line_seen
&& file_seen
&& num_seen
&& !start_seen
&& !end_seen
)
203 || (line_seen
&& file_seen
&& !num_seen
&& !start_seen
&& !end_seen
)
204 || (!line_seen
&& !file_seen
&& !num_seen
&& start_seen
&& end_seen
)))
205 error ("mi_cmd_disassemble: Usage: ( [-f filename -l linenum [-n howmany]] | [-s startaddr -e endaddr]) [--] mixed_mode.");
208 error ("mi_cmd_disassemble: Usage: [-f filename -l linenum [-n howmany]] [-s startaddr -e endaddr] [--] mixed_mode.");
210 mixed_source_and_assembly
= atoi (argv
[0]);
211 if ((mixed_source_and_assembly
!= 0) && (mixed_source_and_assembly
!= 1))
212 error ("mi_cmd_disassemble: Mixed_mode argument must be 0 or 1.");
214 /* We must get the function beginning and end where line_num is
217 if (line_seen
&& file_seen
)
219 s
= lookup_symtab (file_string
);
221 error ("mi_cmd_disassemble: Invalid filename.");
222 if (!find_line_pc (s
, line_num
, &start
))
223 error ("mi_cmd_disassemble: Invalid line number");
224 if (find_pc_partial_function (start
, NULL
, &low
, &high
) == 0)
225 error ("mi_cmd_disassemble: No function contains specified address");
230 /* We don't add a cleanup for this, because the allocation of
231 the stream is done once only for each gdb run, and we need to
232 keep it around until the end. Hopefully there won't be any
233 errors in the init code below, that make this function bail
235 stb
= ui_out_stream_new (uiout
);
236 INIT_DISASSEMBLE_INFO_NO_ARCH (di
, stb
->stream
,
237 (fprintf_ftype
) fprintf_unfiltered
);
238 di
.flavour
= bfd_target_unknown_flavour
;
239 di
.memory_error_func
= dis_asm_memory_error
;
240 di
.print_address_func
= dis_asm_print_address
;
244 di
.mach
= TARGET_PRINT_INSN_INFO
->mach
;
245 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
246 di
.endian
= BFD_ENDIAN_BIG
;
248 di
.endian
= BFD_ENDIAN_LITTLE
;
250 /* If gdb_disassemble_from_exec == -1, then we use the following heuristic to
251 determine whether or not to do disassembly from target memory or from the
254 If we're debugging a local process, read target memory, instead of the
255 exec file. This makes disassembly of functions in shared libs work
256 correctly. Also, read target memory if we are debugging native threads.
258 Else, we're debugging a remote process, and should disassemble from the
259 exec file for speed. However, this is no good if the target modifies its
260 code (for relocation, or whatever).
263 if (gdb_disassemble_from_exec
== -1)
265 if (strcmp (target_shortname
, "child") == 0
266 || strcmp (target_shortname
, "procfs") == 0
267 || strcmp (target_shortname
, "vxprocess") == 0
268 || strstr (target_shortname
, "-threads") != NULL
)
269 gdb_disassemble_from_exec
= 0; /* It's a child process, read inferior mem */
271 gdb_disassemble_from_exec
= 1; /* It's remote, read the exec file */
274 if (gdb_disassemble_from_exec
)
275 di
.read_memory_func
= gdb_dis_asm_read_memory
;
277 di
.read_memory_func
= dis_asm_read_memory
;
279 /* If just doing straight assembly, all we need to do is disassemble
280 everything between low and high. If doing mixed source/assembly,
281 we've got a totally different path to follow. */
283 if (mixed_source_and_assembly
)
285 /* Come here for mixed source/assembly */
286 /* The idea here is to present a source-O-centric view of a
287 function to the user. This means that things are presented
288 in source order, with (possibly) out of order assembly
289 immediately following. */
290 struct symtab
*symtab
;
291 struct linetable_entry
*le
;
294 struct dis_line_entry
*mle
;
295 struct symtab_and_line sal
;
300 /* Assume symtab is valid for whole PC range */
301 symtab
= find_pc_symtab (low
);
303 if (!symtab
|| !symtab
->linetable
)
306 /* First, convert the linetable to a bunch of my_line_entry's. */
308 le
= symtab
->linetable
->item
;
309 nlines
= symtab
->linetable
->nitems
;
314 mle
= (struct dis_line_entry
*) alloca (nlines
* sizeof (struct dis_line_entry
));
318 /* Copy linetable entries for this function into our data
319 structure, creating end_pc's and setting out_of_order as
322 /* First, skip all the preceding functions. */
324 for (i
= 0; i
< nlines
- 1 && le
[i
].pc
< low
; i
++);
326 /* Now, copy all entries before the end of this function. */
329 for (; i
< nlines
- 1 && le
[i
].pc
< high
; i
++)
331 if (le
[i
].line
== le
[i
+ 1].line
332 && le
[i
].pc
== le
[i
+ 1].pc
)
333 continue; /* Ignore duplicates */
335 mle
[newlines
].line
= le
[i
].line
;
336 if (le
[i
].line
> le
[i
+ 1].line
)
338 mle
[newlines
].start_pc
= le
[i
].pc
;
339 mle
[newlines
].end_pc
= le
[i
+ 1].pc
;
343 /* If we're on the last line, and it's part of the function,
344 then we need to get the end pc in a special way. */
349 mle
[newlines
].line
= le
[i
].line
;
350 mle
[newlines
].start_pc
= le
[i
].pc
;
351 sal
= find_pc_line (le
[i
].pc
, 0);
352 mle
[newlines
].end_pc
= sal
.end
;
356 /* Now, sort mle by line #s (and, then by addresses within
360 qsort (mle
, newlines
, sizeof (struct dis_line_entry
), compare_lines
);
362 /* Now, for each line entry, emit the specified lines (unless
363 they have been emitted before), followed by the assembly code
366 next_line
= 0; /* Force out first line */
367 ui_out_list_begin (uiout
, "asm_insns");
369 for (i
= 0; i
< newlines
; i
++)
372 /* Print out everything from next_line to the current line. */
373 if (mle
[i
].line
>= next_line
)
377 /* Just one line to print. */
378 if (next_line
== mle
[i
].line
)
380 ui_out_tuple_begin (uiout
, "src_and_asm_line");
381 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
385 /* Several source lines w/o asm instructions associated. */
386 for (; next_line
< mle
[i
].line
; next_line
++)
388 ui_out_tuple_begin (uiout
, "src_and_asm_line");
389 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
390 ui_out_list_begin (uiout
, "line_asm_insn");
391 ui_out_list_end (uiout
);
392 ui_out_tuple_end (uiout
);
394 /* Print the last line and leave list open for
395 asm instructions to be added. */
396 ui_out_tuple_begin (uiout
, "src_and_asm_line");
397 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, 0);
402 ui_out_tuple_begin (uiout
, "src_and_asm_line");
403 print_source_lines (symtab
, mle
[i
].line
, mle
[i
].line
+ 1, 0);
406 next_line
= mle
[i
].line
+ 1;
407 ui_out_list_begin (uiout
, "line_asm_insn");
408 if (i
+ 1 < newlines
&& mle
[i
+ 1].line
<= mle
[i
].line
)
411 for (pc
= mle
[i
].start_pc
; pc
< mle
[i
].end_pc
;)
416 if (num_displayed
>= how_many
)
421 ui_out_tuple_begin (uiout
, NULL
);
422 ui_out_field_core_addr (uiout
, "address", pc
);
424 if (!build_address_symbolic (pc
, 0, &name
, &offset
, &filename
, &line
, &unmapped
))
426 /* We don't care now about line, filename and
427 unmapped, but we might in the future. */
428 ui_out_field_string (uiout
, "func-name", name
);
429 ui_out_field_int (uiout
, "offset", offset
);
431 if (filename
!= NULL
)
436 ui_file_rewind (stb
->stream
);
437 pc
+= (*tm_print_insn
) (pc
, &di
);
438 ui_out_field_stream (uiout
, "inst", stb
);
439 ui_file_rewind (stb
->stream
);
440 ui_out_tuple_end (uiout
);
444 ui_out_list_end (uiout
);
445 ui_out_tuple_end (uiout
);
449 if (num_displayed
>= how_many
)
452 ui_out_list_end (uiout
);
457 ui_out_list_begin (uiout
, "asm_insns");
459 for (pc
= low
; pc
< high
;)
464 if (num_displayed
>= how_many
)
469 ui_out_tuple_begin (uiout
, NULL
);
470 ui_out_field_core_addr (uiout
, "address", pc
);
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_tuple_end (uiout
);
490 ui_out_list_end (uiout
);
492 gdb_flush (gdb_stdout
);