Add mi/ and testsuite/gdb.mi/ subdirectories.
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-disas.c
1 /* MI Command Set - disassemble commands.
2 Copyright (C) 2000, Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
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
9 the Free Software Foundation; either version 2 of the License, or
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
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. */
21
22 #include "defs.h"
23 #include "target.h"
24 #include "value.h"
25 #include "mi-cmds.h"
26 #include "mi-getopt.h"
27 #include "ui-out.h"
28
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);
32
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. */
36
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. */
41
42 struct dis_line_entry
43 {
44 int line;
45 CORE_ADDR start_pc;
46 CORE_ADDR end_pc;
47 };
48
49 /* This variable determines where memory used for disassembly is read from. */
50 int gdb_disassemble_from_exec = -1;
51
52 /* This is the memory_read_func for gdb_disassemble when we are
53 disassembling from the exec file. */
54 static int
55 gdb_dis_asm_read_memory (bfd_vma memaddr, bfd_byte * myaddr, int len, disassemble_info * info)
56 {
57 extern struct target_ops exec_ops;
58 int res;
59
60 errno = 0;
61 res = xfer_memory (memaddr, myaddr, len, 0, &exec_ops);
62
63 if (res == len)
64 return 0;
65 else if (errno == 0)
66 return EIO;
67 else
68 return errno;
69 }
70
71 static int
72 compare_lines (const PTR mle1p, const PTR mle2p)
73 {
74 struct dis_line_entry *mle1, *mle2;
75 int val;
76
77 mle1 = (struct dis_line_entry *) mle1p;
78 mle2 = (struct dis_line_entry *) mle2p;
79
80 val = mle1->line - mle2->line;
81
82 if (val != 0)
83 return val;
84
85 return mle1->start_pc - mle2->start_pc;
86 }
87
88 /* The arguments to be passed on the command line and parsed here are:
89
90 either:
91
92 START-ADDRESS: address to start the disassembly at.
93 END-ADDRESS: address to end the disassembly at.
94
95 or:
96
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
102 lines.
103
104 always required:
105
106 MODE: 0 or 1 for disassembly only, or mixed source and disassembly,
107 respectively. */
108
109 enum mi_cmd_result
110 mi_cmd_disassemble (char *command, char **argv, int argc)
111 {
112 CORE_ADDR pc;
113 CORE_ADDR start;
114 CORE_ADDR low = 0;
115 CORE_ADDR high = 0;
116
117 int how_many = -1;
118 int mixed_source_and_assembly;
119 int num_displayed;
120 int line_num;
121
122 char *file_string;
123 static disassemble_info di;
124 static int di_initialized;
125
126 struct symtab *s;
127
128 /* To collect the instruction outputted from opcodes. */
129 static struct ui_stream *stb = NULL;
130
131 /* parts of the symbolic representation of the address */
132 int line;
133 int offset;
134 int unmapped;
135 char *filename = NULL;
136 char *name = NULL;
137
138 /* Which options have we processed? */
139 int file_seen = 0;
140 int line_seen = 0;
141 int num_seen = 0;
142 int start_seen = 0;
143 int end_seen = 0;
144
145 /* Options processing stuff. */
146 int optind = 0;
147 char *optarg;
148 enum opt
149 {
150 FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT
151 };
152 static struct mi_opt opts[] =
153 {
154 {"f", FILE_OPT, 1},
155 {"l", LINE_OPT, 1},
156 {"n", NUM_OPT, 1},
157 {"s", START_OPT, 1},
158 {"e", END_OPT, 1},
159 0
160 };
161
162 /* Get the options with their arguments. Keep track of what we
163 encountered. */
164 while (1)
165 {
166 int opt = mi_getopt ("mi_cmd_disassemble", argc, argv, opts,
167 &optind, &optarg);
168 if (opt < 0)
169 break;
170 switch ((enum opt) opt)
171 {
172 case FILE_OPT:
173 file_string = xstrdup (optarg);
174 file_seen = 1;
175 break;
176 case LINE_OPT:
177 line_num = atoi (optarg);
178 line_seen = 1;
179 break;
180 case NUM_OPT:
181 how_many = atoi (optarg);
182 num_seen = 1;
183 break;
184 case START_OPT:
185 low = parse_and_eval_address (optarg);
186 start_seen = 1;
187 break;
188 case END_OPT:
189 high = parse_and_eval_address (optarg);
190 end_seen = 1;
191 break;
192 }
193 }
194 argv += optind;
195 argc -= optind;
196
197 /* Allow only filename + linenum (with how_many which is not
198 required) OR start_addr + and_addr */
199
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.");
204
205 if (argc != 1)
206 error ("mi_cmd_disassemble: Usage: [-f filename -l linenum [-n howmany]] [-s startaddr -e endaddr] [--] mixed_mode.");
207
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.");
211
212 /* We must get the function beginning and end where line_num is
213 contained. */
214
215 if (line_seen && file_seen)
216 {
217 s = lookup_symtab (file_string);
218 if (s == NULL)
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");
224 }
225
226 if (!di_initialized)
227 {
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
232 out. */
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;
239 di_initialized = 1;
240 }
241
242 di.mach = TARGET_PRINT_INSN_INFO->mach;
243 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
244 di.endian = BFD_ENDIAN_BIG;
245 else
246 di.endian = BFD_ENDIAN_LITTLE;
247
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
250 exec file:
251
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.
255
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).
259 */
260
261 if (gdb_disassemble_from_exec == -1)
262 {
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 */
268 else
269 gdb_disassemble_from_exec = 1; /* It's remote, read the exec file */
270 }
271
272 if (gdb_disassemble_from_exec)
273 di.read_memory_func = gdb_dis_asm_read_memory;
274 else
275 di.read_memory_func = dis_asm_read_memory;
276
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. */
280
281 if (mixed_source_and_assembly)
282 {
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;
290 int nlines;
291 int newlines;
292 struct dis_line_entry *mle;
293 struct symtab_and_line sal;
294 int i;
295 int out_of_order;
296 int next_line;
297
298 /* Assume symtab is valid for whole PC range */
299 symtab = find_pc_symtab (low);
300
301 if (!symtab || !symtab->linetable)
302 goto assembly_only;
303
304 /* First, convert the linetable to a bunch of my_line_entry's. */
305
306 le = symtab->linetable->item;
307 nlines = symtab->linetable->nitems;
308
309 if (nlines <= 0)
310 goto assembly_only;
311
312 mle = (struct dis_line_entry *) alloca (nlines * sizeof (struct dis_line_entry));
313
314 out_of_order = 0;
315
316 /* Copy linetable entries for this function into our data
317 structure, creating end_pc's and setting out_of_order as
318 appropriate. */
319
320 /* First, skip all the preceding functions. */
321
322 for (i = 0; i < nlines - 1 && le[i].pc < low; i++);
323
324 /* Now, copy all entries before the end of this function. */
325
326 newlines = 0;
327 for (; i < nlines - 1 && le[i].pc < high; i++)
328 {
329 if (le[i].line == le[i + 1].line
330 && le[i].pc == le[i + 1].pc)
331 continue; /* Ignore duplicates */
332
333 mle[newlines].line = le[i].line;
334 if (le[i].line > le[i + 1].line)
335 out_of_order = 1;
336 mle[newlines].start_pc = le[i].pc;
337 mle[newlines].end_pc = le[i + 1].pc;
338 newlines++;
339 }
340
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. */
343
344 if (i == nlines - 1
345 && le[i].pc < high)
346 {
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;
351 newlines++;
352 }
353
354 /* Now, sort mle by line #s (and, then by addresses within
355 lines). */
356
357 if (out_of_order)
358 qsort (mle, newlines, sizeof (struct dis_line_entry), compare_lines);
359
360 /* Now, for each line entry, emit the specified lines (unless
361 they have been emitted before), followed by the assembly code
362 for that line. */
363
364 next_line = 0; /* Force out first line */
365 ui_out_list_begin (uiout, "asm_insns");
366 num_displayed = 0;
367 for (i = 0; i < newlines; i++)
368 {
369 int close_list = 1;
370 /* Print out everything from next_line to the current line. */
371 if (mle[i].line >= next_line)
372 {
373 if (next_line != 0)
374 {
375 /* Just one line to print. */
376 if (next_line == mle[i].line)
377 {
378 ui_out_list_begin (uiout, "src_and_asm_line");
379 print_source_lines (symtab, next_line, mle[i].line + 1, 0);
380 }
381 else
382 {
383 /* Several source lines w/o asm instructions associated. */
384 for (; next_line < mle[i].line; next_line++)
385 {
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);
391 }
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);
396 }
397 }
398 else
399 {
400 ui_out_list_begin (uiout, "src_and_asm_line");
401 print_source_lines (symtab, mle[i].line, mle[i].line + 1, 0);
402 }
403
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)
407 close_list = 0;
408 }
409 for (pc = mle[i].start_pc; pc < mle[i].end_pc;)
410 {
411 QUIT;
412 if (how_many >= 0)
413 {
414 if (num_displayed >= how_many)
415 break;
416 else
417 num_displayed++;
418 }
419 ui_out_list_begin (uiout, NULL);
420 print_address_numeric (pc, 1, stb->stream);
421 ui_out_field_stream (uiout, "address", stb);
422
423 if (!build_address_symbolic (pc, 0, &name, &offset, &filename, &line, &unmapped))
424 {
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);
429 }
430 if (filename != NULL)
431 free (filename);
432 if (name != NULL)
433 free (name);
434
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);
440 }
441 if (close_list)
442 {
443 ui_out_list_end (uiout);
444 ui_out_list_end (uiout);
445 close_list = 0;
446 }
447 if (how_many >= 0)
448 if (num_displayed >= how_many)
449 break;
450 }
451 ui_out_list_end (uiout);
452 }
453 else
454 {
455 assembly_only:
456 ui_out_list_begin (uiout, "asm_insns");
457 num_displayed = 0;
458 for (pc = low; pc < high;)
459 {
460 QUIT;
461 if (how_many >= 0)
462 {
463 if (num_displayed >= how_many)
464 break;
465 else
466 num_displayed++;
467 }
468 ui_out_list_begin (uiout, NULL);
469 print_address_numeric (pc, 1, stb->stream);
470 ui_out_field_stream (uiout, "address", stb);
471
472 if (!build_address_symbolic (pc, 0, &name, &offset, &filename, &line, &unmapped))
473 {
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);
478 }
479 if (filename != NULL)
480 free (filename);
481 if (name != NULL)
482 free (name);
483
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);
489 }
490 ui_out_list_end (uiout);
491 }
492 gdb_flush (gdb_stdout);
493
494 return MI_CMD_DONE;
495 }
496
497 /* Local variables: */
498 /* change-log-default-name: "ChangeLog-mi" */
499 /* End: */
This page took 0.040439 seconds and 4 git commands to generate.