2010-05-24 Michael Snyder <msnyder@vmware.com>
[binutils-gdb.git] / gdb / mi / mi-cmd-break.c
1 /* MI Command Set - breakpoint and watchpoint commands.
2 Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010
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 "mi-cmds.h"
24 #include "ui-out.h"
25 #include "mi-out.h"
26 #include "breakpoint.h"
27 #include "gdb_string.h"
28 #include "mi-getopt.h"
29 #include "gdb.h"
30 #include "exceptions.h"
31 #include "observer.h"
32
33 enum
34 {
35 FROM_TTY = 0
36 };
37
38 /* True if MI breakpoint observers have been registered. */
39
40 static int mi_breakpoint_observers_installed;
41
42 /* Control whether breakpoint_notify may act. */
43
44 static int mi_can_breakpoint_notify;
45
46 /* Output a single breakpoint, when allowed. */
47
48 static void
49 breakpoint_notify (int b)
50 {
51 if (mi_can_breakpoint_notify)
52 gdb_breakpoint_query (uiout, b, NULL);
53 }
54
55 enum bp_type
56 {
57 REG_BP,
58 HW_BP,
59 REGEXP_BP
60 };
61
62 /* Implements the -break-insert command.
63 See the MI manual for the list of possible options. */
64
65 void
66 mi_cmd_break_insert (char *command, char **argv, int argc)
67 {
68 char *address = NULL;
69 int hardware = 0;
70 int temp_p = 0;
71 int thread = -1;
72 int ignore_count = 0;
73 char *condition = NULL;
74 int pending = 0;
75 int enabled = 1;
76 int tracepoint = 0;
77 struct cleanup *back_to;
78
79 enum opt
80 {
81 HARDWARE_OPT, TEMP_OPT, CONDITION_OPT,
82 IGNORE_COUNT_OPT, THREAD_OPT, PENDING_OPT, DISABLE_OPT,
83 TRACEPOINT_OPT,
84 };
85 static struct mi_opt opts[] =
86 {
87 {"h", HARDWARE_OPT, 0},
88 {"t", TEMP_OPT, 0},
89 {"c", CONDITION_OPT, 1},
90 {"i", IGNORE_COUNT_OPT, 1},
91 {"p", THREAD_OPT, 1},
92 {"f", PENDING_OPT, 0},
93 {"d", DISABLE_OPT, 0},
94 {"a", TRACEPOINT_OPT, 0},
95 { 0, 0, 0 }
96 };
97
98 /* Parse arguments. It could be -r or -h or -t, <location> or ``--''
99 to denote the end of the option list. */
100 int optind = 0;
101 char *optarg;
102
103 while (1)
104 {
105 int opt = mi_getopt ("mi_cmd_break_insert", argc, argv, opts, &optind, &optarg);
106 if (opt < 0)
107 break;
108 switch ((enum opt) opt)
109 {
110 case TEMP_OPT:
111 temp_p = 1;
112 break;
113 case HARDWARE_OPT:
114 hardware = 1;
115 break;
116 case CONDITION_OPT:
117 condition = optarg;
118 break;
119 case IGNORE_COUNT_OPT:
120 ignore_count = atol (optarg);
121 break;
122 case THREAD_OPT:
123 thread = atol (optarg);
124 break;
125 case PENDING_OPT:
126 pending = 1;
127 break;
128 case DISABLE_OPT:
129 enabled = 0;
130 break;
131 case TRACEPOINT_OPT:
132 tracepoint = 1;
133 break;
134 }
135 }
136
137 if (optind >= argc)
138 error (_("mi_cmd_break_insert: Missing <location>"));
139 if (optind < argc - 1)
140 error (_("mi_cmd_break_insert: Garbage following <location>"));
141 address = argv[optind];
142
143 /* Now we have what we need, let's insert the breakpoint! */
144 if (! mi_breakpoint_observers_installed)
145 {
146 observer_attach_breakpoint_created (breakpoint_notify);
147 observer_attach_breakpoint_modified (breakpoint_notify);
148 observer_attach_breakpoint_deleted (breakpoint_notify);
149 mi_breakpoint_observers_installed = 1;
150 }
151
152 back_to = make_cleanup_restore_integer (&mi_can_breakpoint_notify);
153 mi_can_breakpoint_notify = 1;
154 create_breakpoint (get_current_arch (), address, condition, thread,
155 0 /* condition and thread are valid. */,
156 temp_p, hardware, tracepoint,
157 ignore_count,
158 pending ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
159 NULL, 0, enabled);
160 do_cleanups (back_to);
161
162 }
163
164 enum wp_type
165 {
166 REG_WP,
167 READ_WP,
168 ACCESS_WP
169 };
170
171 void
172 mi_cmd_break_passcount (char *command, char **argv, int argc)
173 {
174 int n;
175 int p;
176 struct breakpoint *t;
177
178 if (argc != 2)
179 error (_("Usage: tracepoint-number passcount"));
180
181 n = atoi (argv[0]);
182 p = atoi (argv[1]);
183 t = get_tracepoint (n);
184
185 if (t)
186 {
187 t->pass_count = p;
188 observer_notify_tracepoint_modified (n);
189 }
190 else
191 {
192 error (_("Cound not find tracepoint %d"), n);
193 }
194 }
195
196 /* Insert a watchpoint. The type of watchpoint is specified by the
197 first argument:
198 -break-watch <expr> --> insert a regular wp.
199 -break-watch -r <expr> --> insert a read watchpoint.
200 -break-watch -a <expr> --> insert an access wp. */
201
202 void
203 mi_cmd_break_watch (char *command, char **argv, int argc)
204 {
205 char *expr = NULL;
206 enum wp_type type = REG_WP;
207 enum opt
208 {
209 READ_OPT, ACCESS_OPT
210 };
211 static struct mi_opt opts[] =
212 {
213 {"r", READ_OPT, 0},
214 {"a", ACCESS_OPT, 0},
215 { 0, 0, 0 }
216 };
217
218 /* Parse arguments. */
219 int optind = 0;
220 char *optarg;
221
222 while (1)
223 {
224 int opt = mi_getopt ("mi_cmd_break_watch", argc, argv,
225 opts, &optind, &optarg);
226
227 if (opt < 0)
228 break;
229 switch ((enum opt) opt)
230 {
231 case READ_OPT:
232 type = READ_WP;
233 break;
234 case ACCESS_OPT:
235 type = ACCESS_WP;
236 break;
237 }
238 }
239 if (optind >= argc)
240 error (_("mi_cmd_break_watch: Missing <expression>"));
241 if (optind < argc - 1)
242 error (_("mi_cmd_break_watch: Garbage following <expression>"));
243 expr = argv[optind];
244
245 /* Now we have what we need, let's insert the watchpoint! */
246 switch (type)
247 {
248 case REG_WP:
249 watch_command_wrapper (expr, FROM_TTY);
250 break;
251 case READ_WP:
252 rwatch_command_wrapper (expr, FROM_TTY);
253 break;
254 case ACCESS_WP:
255 awatch_command_wrapper (expr, FROM_TTY);
256 break;
257 default:
258 error (_("mi_cmd_break_watch: Unknown watchpoint type."));
259 }
260 }
261
262 /* The mi_read_next_line consults these variable to return successive
263 command lines. While it would be clearer to use a closure pointer,
264 it is not expected that any future code will use read_command_lines_1,
265 therefore no point of overengineering. */
266
267 static char **mi_command_line_array;
268 static int mi_command_line_array_cnt;
269 static int mi_command_line_array_ptr;
270
271 static char *
272 mi_read_next_line (void)
273 {
274 if (mi_command_line_array_ptr == mi_command_line_array_cnt)
275 return NULL;
276 else
277 return mi_command_line_array[mi_command_line_array_ptr++];
278 }
279
280 void
281 mi_cmd_break_commands (char *command, char **argv, int argc)
282 {
283 struct command_line *break_command;
284 char *endptr;
285 int bnum;
286 struct breakpoint *b;
287
288 if (argc < 1)
289 error ("USAGE: %s <BKPT> [<COMMAND> [<COMMAND>...]]", command);
290
291 bnum = strtol (argv[0], &endptr, 0);
292 if (endptr == argv[0])
293 error ("breakpoint number argument \"%s\" is not a number.",
294 argv[0]);
295 else if (*endptr != '\0')
296 error ("junk at the end of breakpoint number argument \"%s\".",
297 argv[0]);
298
299 b = get_breakpoint (bnum);
300 if (b == NULL)
301 error ("breakpoint %d not found.", bnum);
302
303 mi_command_line_array = argv;
304 mi_command_line_array_ptr = 1;
305 mi_command_line_array_cnt = argc;
306
307 if (is_tracepoint (b))
308 break_command = read_command_lines_1 (mi_read_next_line, 1,
309 check_tracepoint_command, b);
310 else
311 break_command = read_command_lines_1 (mi_read_next_line, 1, 0, 0);
312
313 breakpoint_set_commands (b, break_command);
314 }
315