Simplify MI breakpoint setting.
[deliverable/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 enum bp_type type = REG_BP;
70 int hardware = 0;
71 int temp_p = 0;
72 int thread = -1;
73 int ignore_count = 0;
74 char *condition = NULL;
75 int pending = 0;
76 int enabled = 1;
77 struct cleanup *back_to;
78
79 struct gdb_exception e;
80 struct gdb_events *old_hooks;
81 enum opt
82 {
83 HARDWARE_OPT, TEMP_OPT, CONDITION_OPT,
84 IGNORE_COUNT_OPT, THREAD_OPT, PENDING_OPT, DISABLE_OPT
85 };
86 static struct mi_opt opts[] =
87 {
88 {"h", HARDWARE_OPT, 0},
89 {"t", TEMP_OPT, 0},
90 {"c", CONDITION_OPT, 1},
91 {"i", IGNORE_COUNT_OPT, 1},
92 {"p", THREAD_OPT, 1},
93 {"f", PENDING_OPT, 0},
94 {"d", DISABLE_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 while (1)
103 {
104 int opt = mi_getopt ("mi_cmd_break_insert", argc, argv, opts, &optind, &optarg);
105 if (opt < 0)
106 break;
107 switch ((enum opt) opt)
108 {
109 case TEMP_OPT:
110 temp_p = 1;
111 break;
112 case HARDWARE_OPT:
113 hardware = 1;
114 break;
115 case CONDITION_OPT:
116 condition = optarg;
117 break;
118 case IGNORE_COUNT_OPT:
119 ignore_count = atol (optarg);
120 break;
121 case THREAD_OPT:
122 thread = atol (optarg);
123 break;
124 case PENDING_OPT:
125 pending = 1;
126 break;
127 case DISABLE_OPT:
128 enabled = 0;
129 }
130 }
131
132 if (optind >= argc)
133 error (_("mi_cmd_break_insert: Missing <location>"));
134 if (optind < argc - 1)
135 error (_("mi_cmd_break_insert: Garbage following <location>"));
136 address = argv[optind];
137
138 /* Now we have what we need, let's insert the breakpoint! */
139 if (! mi_breakpoint_observers_installed)
140 {
141 observer_attach_breakpoint_created (breakpoint_notify);
142 observer_attach_breakpoint_modified (breakpoint_notify);
143 observer_attach_breakpoint_deleted (breakpoint_notify);
144 mi_breakpoint_observers_installed = 1;
145 }
146
147 back_to = make_cleanup_restore_integer (&mi_can_breakpoint_notify);
148 mi_can_breakpoint_notify = 1;
149 create_breakpoint (get_current_arch (), address, condition, thread,
150 0 /* condition and thread are valid. */,
151 temp_p, hardware, 0 /* traceflag */,
152 ignore_count,
153 pending ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
154 NULL, 0, enabled);
155 do_cleanups (back_to);
156
157 }
158
159 enum wp_type
160 {
161 REG_WP,
162 READ_WP,
163 ACCESS_WP
164 };
165
166 /* Insert a watchpoint. The type of watchpoint is specified by the
167 first argument:
168 -break-watch <expr> --> insert a regular wp.
169 -break-watch -r <expr> --> insert a read watchpoint.
170 -break-watch -a <expr> --> insert an access wp. */
171
172 void
173 mi_cmd_break_watch (char *command, char **argv, int argc)
174 {
175 char *expr = NULL;
176 enum wp_type type = REG_WP;
177 enum opt
178 {
179 READ_OPT, ACCESS_OPT
180 };
181 static struct mi_opt opts[] =
182 {
183 {"r", READ_OPT, 0},
184 {"a", ACCESS_OPT, 0},
185 { 0, 0, 0 }
186 };
187
188 /* Parse arguments. */
189 int optind = 0;
190 char *optarg;
191 while (1)
192 {
193 int opt = mi_getopt ("mi_cmd_break_watch", argc, argv, opts, &optind, &optarg);
194 if (opt < 0)
195 break;
196 switch ((enum opt) opt)
197 {
198 case READ_OPT:
199 type = READ_WP;
200 break;
201 case ACCESS_OPT:
202 type = ACCESS_WP;
203 break;
204 }
205 }
206 if (optind >= argc)
207 error (_("mi_cmd_break_watch: Missing <expression>"));
208 if (optind < argc - 1)
209 error (_("mi_cmd_break_watch: Garbage following <expression>"));
210 expr = argv[optind];
211
212 /* Now we have what we need, let's insert the watchpoint! */
213 switch (type)
214 {
215 case REG_WP:
216 watch_command_wrapper (expr, FROM_TTY);
217 break;
218 case READ_WP:
219 rwatch_command_wrapper (expr, FROM_TTY);
220 break;
221 case ACCESS_WP:
222 awatch_command_wrapper (expr, FROM_TTY);
223 break;
224 default:
225 error (_("mi_cmd_break_watch: Unknown watchpoint type."));
226 }
227 }
228
229 /* The mi_read_next_line consults these variable to return successive
230 command lines. While it would be clearer to use a closure pointer,
231 it is not expected that any future code will use read_command_lines_1,
232 therefore no point of overengineering. */
233
234 static char **mi_command_line_array;
235 static int mi_command_line_array_cnt;
236 static int mi_command_line_array_ptr;
237
238 static char *
239 mi_read_next_line (void)
240 {
241 if (mi_command_line_array_ptr == mi_command_line_array_cnt)
242 return NULL;
243 else
244 return mi_command_line_array[mi_command_line_array_ptr++];
245 }
246
247 void
248 mi_cmd_break_commands (char *command, char **argv, int argc)
249 {
250 struct command_line *break_command;
251 char *endptr;
252 int bnum;
253 struct breakpoint *b;
254
255 if (argc < 1)
256 error ("USAGE: %s <BKPT> [<COMMAND> [<COMMAND>...]]", command);
257
258 bnum = strtol (argv[0], &endptr, 0);
259 if (endptr == argv[0])
260 error ("breakpoint number argument \"%s\" is not a number.",
261 argv[0]);
262 else if (*endptr != '\0')
263 error ("junk at the end of breakpoint number argument \"%s\".",
264 argv[0]);
265
266 b = get_breakpoint (bnum);
267 if (b == NULL)
268 error ("breakpoint %d not found.", bnum);
269
270 mi_command_line_array = argv;
271 mi_command_line_array_ptr = 1;
272 mi_command_line_array_cnt = argc;
273
274 break_command = read_command_lines_1 (mi_read_next_line, 1);
275 breakpoint_set_commands (b, break_command);
276 }
277
This page took 0.049989 seconds and 4 git commands to generate.