2011-03-05 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-disas.c
CommitLineData
fb40c209 1/* MI Command Set - disassemble commands.
7b6bb8da 2 Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010, 2011
0fb0cc75 3 Free Software Foundation, Inc.
ab91fdd5 4 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
20
21#include "defs.h"
13274fc3 22#include "arch-utils.h"
fb40c209
AC
23#include "target.h"
24#include "value.h"
25#include "mi-cmds.h"
26#include "mi-getopt.h"
5f8a3188 27#include "gdb_string.h"
92df71f0
FN
28#include "ui-out.h"
29#include "disasm.h"
8f0eea0e 30
fb40c209
AC
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.
b716877b 43 HOW_MANY: Number of disassembly lines to display. With source, it
fb40c209
AC
44 is the number of disassembly lines only, not counting the source
45 lines.
46
47 always required:
48
b716877b
AB
49 MODE: 0 -- disassembly.
50 1 -- disassembly and source.
51 2 -- disassembly and opcodes.
52 3 -- disassembly, source and opcodes.
53*/
ce8f13f8 54void
fb40c209
AC
55mi_cmd_disassemble (char *command, char **argv, int argc)
56{
13274fc3 57 struct gdbarch *gdbarch = get_current_arch ();
fb40c209 58 CORE_ADDR start;
fb40c209 59
b716877b 60 int mode, disasm_flags;
fb40c209
AC
61 struct symtab *s;
62
8e2eec62 63 /* Which options have we processed ... */
fb40c209
AC
64 int file_seen = 0;
65 int line_seen = 0;
66 int num_seen = 0;
67 int start_seen = 0;
68 int end_seen = 0;
69
8e2eec62
AC
70 /* ... and their corresponding value. */
71 char *file_string = NULL;
72 int line_num = -1;
73 int how_many = -1;
74 CORE_ADDR low = 0;
75 CORE_ADDR high = 0;
76
fb40c209
AC
77 /* Options processing stuff. */
78 int optind = 0;
79 char *optarg;
80 enum opt
fb40c209 81 {
27cdacdb
EZ
82 FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT
83 };
84 static struct mi_opt opts[] = {
fb40c209
AC
85 {"f", FILE_OPT, 1},
86 {"l", LINE_OPT, 1},
87 {"n", NUM_OPT, 1},
88 {"s", START_OPT, 1},
89 {"e", END_OPT, 1},
d5d6fca5 90 { 0, 0, 0 }
fb40c209
AC
91 };
92
93 /* Get the options with their arguments. Keep track of what we
94 encountered. */
95 while (1)
96 {
1b05df00 97 int opt = mi_getopt ("-data-disassemble", argc, argv, opts,
fb40c209
AC
98 &optind, &optarg);
99 if (opt < 0)
100 break;
101 switch ((enum opt) opt)
102 {
103 case FILE_OPT:
104 file_string = xstrdup (optarg);
105 file_seen = 1;
106 break;
107 case LINE_OPT:
108 line_num = atoi (optarg);
109 line_seen = 1;
110 break;
111 case NUM_OPT:
112 how_many = atoi (optarg);
113 num_seen = 1;
114 break;
115 case START_OPT:
116 low = parse_and_eval_address (optarg);
117 start_seen = 1;
118 break;
119 case END_OPT:
120 high = parse_and_eval_address (optarg);
121 end_seen = 1;
122 break;
123 }
124 }
125 argv += optind;
126 argc -= optind;
127
128 /* Allow only filename + linenum (with how_many which is not
129 required) OR start_addr + and_addr */
130
131 if (!((line_seen && file_seen && num_seen && !start_seen && !end_seen)
132 || (line_seen && file_seen && !num_seen && !start_seen && !end_seen)
27cdacdb 133 || (!line_seen && !file_seen && !num_seen && start_seen && end_seen)))
1b05df00 134 error (_("-data-disassemble: Usage: ( [-f filename -l linenum [-n "
b716877b 135 "howmany]] | [-s startaddr -e endaddr]) [--] mode."));
fb40c209
AC
136
137 if (argc != 1)
1b05df00 138 error (_("-data-disassemble: Usage: [-f filename -l linenum "
b716877b 139 "[-n howmany]] [-s startaddr -e endaddr] [--] mode."));
fb40c209 140
b716877b
AB
141 mode = atoi (argv[0]);
142 if (mode < 0 || mode > 3)
1b05df00 143 error (_("-data-disassemble: Mode argument must be 0, 1, 2, or 3."));
fb40c209 144
b716877b
AB
145 /* Convert the mode into a set of disassembly flags */
146
147 disasm_flags = 0;
148 if (mode & 0x1)
149 disasm_flags |= DISASSEMBLY_SOURCE;
150 if (mode & 0x2)
151 disasm_flags |= DISASSEMBLY_RAW_INSN;
8f0eea0e 152
fb40c209
AC
153 /* We must get the function beginning and end where line_num is
154 contained. */
155
156 if (line_seen && file_seen)
157 {
158 s = lookup_symtab (file_string);
159 if (s == NULL)
1b05df00 160 error (_("-data-disassemble: Invalid filename."));
fb40c209 161 if (!find_line_pc (s, line_num, &start))
1b05df00 162 error (_("-data-disassemble: Invalid line number"));
fb40c209 163 if (find_pc_partial_function (start, NULL, &low, &high) == 0)
1b05df00 164 error (_("-data-disassemble: "
9a2b4c1b 165 "No function contains specified address"));
fb40c209
AC
166 }
167
13274fc3 168 gdb_disassembly (gdbarch, uiout,
92df71f0 169 file_string,
b716877b 170 disasm_flags,
328d0145 171 how_many, low, high);
fb40c209 172}
This page took 0.839434 seconds and 4 git commands to generate.