2011-08-04 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-disas.c
1 /* MI Command Set - disassemble commands.
2 Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions (a Red Hat company).
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include "target.h"
24 #include "value.h"
25 #include "mi-cmds.h"
26 #include "mi-getopt.h"
27 #include "gdb_string.h"
28 #include "ui-out.h"
29 #include "disasm.h"
30
31 /* The arguments to be passed on the command line and parsed here are:
32
33 either:
34
35 START-ADDRESS: address to start the disassembly at.
36 END-ADDRESS: address to end the disassembly at.
37
38 or:
39
40 FILENAME: The name of the file where we want disassemble from.
41 LINE: The line around which we want to disassemble. It will
42 disassemble the function that contins that line.
43 HOW_MANY: Number of disassembly lines to display. With source, it
44 is the number of disassembly lines only, not counting the source
45 lines.
46
47 always required:
48
49 MODE: 0 -- disassembly.
50 1 -- disassembly and source.
51 2 -- disassembly and opcodes.
52 3 -- disassembly, source and opcodes.
53 */
54 void
55 mi_cmd_disassemble (char *command, char **argv, int argc)
56 {
57 struct gdbarch *gdbarch = get_current_arch ();
58 struct ui_out *uiout = current_uiout;
59 CORE_ADDR start;
60
61 int mode, disasm_flags;
62 struct symtab *s;
63
64 /* Which options have we processed ... */
65 int file_seen = 0;
66 int line_seen = 0;
67 int num_seen = 0;
68 int start_seen = 0;
69 int end_seen = 0;
70
71 /* ... and their corresponding value. */
72 char *file_string = NULL;
73 int line_num = -1;
74 int how_many = -1;
75 CORE_ADDR low = 0;
76 CORE_ADDR high = 0;
77 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
78
79 /* Options processing stuff. */
80 int optind = 0;
81 char *optarg;
82 enum opt
83 {
84 FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT
85 };
86 static struct mi_opt opts[] = {
87 {"f", FILE_OPT, 1},
88 {"l", LINE_OPT, 1},
89 {"n", NUM_OPT, 1},
90 {"s", START_OPT, 1},
91 {"e", END_OPT, 1},
92 { 0, 0, 0 }
93 };
94
95 /* Get the options with their arguments. Keep track of what we
96 encountered. */
97 while (1)
98 {
99 int opt = mi_getopt ("-data-disassemble", argc, argv, opts,
100 &optind, &optarg);
101 if (opt < 0)
102 break;
103 switch ((enum opt) opt)
104 {
105 case FILE_OPT:
106 file_string = xstrdup (optarg);
107 file_seen = 1;
108 make_cleanup (xfree, file_string);
109 break;
110 case LINE_OPT:
111 line_num = atoi (optarg);
112 line_seen = 1;
113 break;
114 case NUM_OPT:
115 how_many = atoi (optarg);
116 num_seen = 1;
117 break;
118 case START_OPT:
119 low = parse_and_eval_address (optarg);
120 start_seen = 1;
121 break;
122 case END_OPT:
123 high = parse_and_eval_address (optarg);
124 end_seen = 1;
125 break;
126 }
127 }
128 argv += optind;
129 argc -= optind;
130
131 /* Allow only filename + linenum (with how_many which is not
132 required) OR start_addr + and_addr */
133
134 if (!((line_seen && file_seen && num_seen && !start_seen && !end_seen)
135 || (line_seen && file_seen && !num_seen && !start_seen && !end_seen)
136 || (!line_seen && !file_seen && !num_seen && start_seen && end_seen)))
137 error (_("-data-disassemble: Usage: ( [-f filename -l linenum [-n "
138 "howmany]] | [-s startaddr -e endaddr]) [--] mode."));
139
140 if (argc != 1)
141 error (_("-data-disassemble: Usage: [-f filename -l linenum "
142 "[-n howmany]] [-s startaddr -e endaddr] [--] mode."));
143
144 mode = atoi (argv[0]);
145 if (mode < 0 || mode > 3)
146 error (_("-data-disassemble: Mode argument must be 0, 1, 2, or 3."));
147
148 /* Convert the mode into a set of disassembly flags */
149
150 disasm_flags = 0;
151 if (mode & 0x1)
152 disasm_flags |= DISASSEMBLY_SOURCE;
153 if (mode & 0x2)
154 disasm_flags |= DISASSEMBLY_RAW_INSN;
155
156 /* We must get the function beginning and end where line_num is
157 contained. */
158
159 if (line_seen && file_seen)
160 {
161 s = lookup_symtab (file_string);
162 if (s == NULL)
163 error (_("-data-disassemble: Invalid filename."));
164 if (!find_line_pc (s, line_num, &start))
165 error (_("-data-disassemble: Invalid line number"));
166 if (find_pc_partial_function (start, NULL, &low, &high) == 0)
167 error (_("-data-disassemble: "
168 "No function contains specified address"));
169 }
170
171 gdb_disassembly (gdbarch, uiout,
172 file_string,
173 disasm_flags,
174 how_many, low, high);
175
176 do_cleanups (cleanups);
177 }
This page took 0.035483 seconds and 4 git commands to generate.