mi_load_progress: Use unique_ptr to manage ui_out lifetime
[external/binutils.git] / gdb / mi / mi-cmd-disas.c
1 /* MI Command Set - disassemble commands.
2    Copyright (C) 2000-2017 Free Software Foundation, Inc.
3    Contributed by Cygnus Solutions (a Red Hat company).
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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "target.h"
23 #include "value.h"
24 #include "mi-cmds.h"
25 #include "mi-getopt.h"
26 #include "ui-out.h"
27 #include "disasm.h"
28
29 /* The arguments to be passed on the command line and parsed here are
30    either:
31
32    START-ADDRESS: address to start the disassembly at.
33    END-ADDRESS: address to end the disassembly at.
34
35    or:
36
37    FILENAME: The name of the file where we want disassemble from.
38    LINE: The line around which we want to disassemble. It will
39    disassemble the function that contins that line.
40    HOW_MANY: Number of disassembly lines to display. With source, it
41    is the number of disassembly lines only, not counting the source
42    lines.  
43
44    always required:
45
46    MODE: 0 -- disassembly.
47          1 -- disassembly and source (with deprecated source-centric view).
48          2 -- disassembly and opcodes.
49          3 -- disassembly, source-centric and opcodes.
50          4 -- disassembly, and source (with pc-centric view).
51          5 -- disassembly, source (pc-centric) and opcodes.  */
52
53 void
54 mi_cmd_disassemble (const char *command, char **argv, int argc)
55 {
56   struct gdbarch *gdbarch = get_current_arch ();
57   struct ui_out *uiout = current_uiout;
58   CORE_ADDR start;
59
60   int mode;
61   gdb_disassembly_flags 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 oind = 0;
81   char *oarg;
82   enum opt
83   {
84     FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT
85   };
86   static const struct mi_opt opts[] =
87     {
88       {"f", FILE_OPT, 1},
89       {"l", LINE_OPT, 1},
90       {"n", NUM_OPT, 1},
91       {"s", START_OPT, 1},
92       {"e", END_OPT, 1},
93       { 0, 0, 0 }
94     };
95
96   /* Get the options with their arguments. Keep track of what we
97      encountered.  */
98   while (1)
99     {
100       int opt = mi_getopt ("-data-disassemble", argc, argv, opts,
101                            &oind, &oarg);
102       if (opt < 0)
103         break;
104       switch ((enum opt) opt)
105         {
106         case FILE_OPT:
107           file_string = xstrdup (oarg);
108           file_seen = 1;
109           make_cleanup (xfree, file_string);
110           break;
111         case LINE_OPT:
112           line_num = atoi (oarg);
113           line_seen = 1;
114           break;
115         case NUM_OPT:
116           how_many = atoi (oarg);
117           num_seen = 1;
118           break;
119         case START_OPT:
120           low = parse_and_eval_address (oarg);
121           start_seen = 1;
122           break;
123         case END_OPT:
124           high = parse_and_eval_address (oarg);
125           end_seen = 1;
126           break;
127         }
128     }
129   argv += oind;
130   argc -= oind;
131
132   /* Allow only filename + linenum (with how_many which is not
133      required) OR start_addr + end_addr.  */
134
135   if (!((line_seen && file_seen && num_seen && !start_seen && !end_seen)
136         || (line_seen && file_seen && !num_seen && !start_seen && !end_seen)
137         || (!line_seen && !file_seen && !num_seen && start_seen && end_seen)))
138     error (_("-data-disassemble: Usage: ( [-f filename -l linenum [-n "
139              "howmany]] | [-s startaddr -e endaddr]) [--] mode."));
140
141   if (argc != 1)
142     error (_("-data-disassemble: Usage: [-f filename -l linenum "
143              "[-n howmany]] [-s startaddr -e endaddr] [--] mode."));
144
145   mode = atoi (argv[0]);
146   if (mode < 0 || mode > 5)
147     error (_("-data-disassemble: Mode argument must be in the range 0-5."));
148
149   /* Convert the mode into a set of disassembly flags.  */
150
151   disasm_flags = 0;  /* Initialize here for -Wall.  */
152   switch (mode)
153     {
154     case 0:
155       break;
156     case 1:
157       disasm_flags |= DISASSEMBLY_SOURCE_DEPRECATED;
158       break;
159     case 2:
160       disasm_flags |= DISASSEMBLY_RAW_INSN;
161       break;
162     case 3:
163       disasm_flags |= DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_RAW_INSN;
164       break;
165     case 4:
166       disasm_flags |= DISASSEMBLY_SOURCE;
167       break;
168     case 5:
169       disasm_flags |= DISASSEMBLY_SOURCE | DISASSEMBLY_RAW_INSN;
170       break;
171     default:
172       gdb_assert_not_reached ("bad disassembly mode");
173     }
174
175   /* We must get the function beginning and end where line_num is
176      contained.  */
177
178   if (line_seen && file_seen)
179     {
180       s = lookup_symtab (file_string);
181       if (s == NULL)
182         error (_("-data-disassemble: Invalid filename."));
183       if (!find_line_pc (s, line_num, &start))
184         error (_("-data-disassemble: Invalid line number"));
185       if (find_pc_partial_function (start, NULL, &low, &high) == 0)
186         error (_("-data-disassemble: "
187                  "No function contains specified address"));
188     }
189
190   gdb_disassembly (gdbarch, uiout,
191                    disasm_flags,
192                    how_many, low, high);
193
194   do_cleanups (cleanups);
195 }