1 /* Generic simulator watchpoint support.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
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 2, or (at your option)
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.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "sim-options.h"
24 #include "sim-assert.h"
44 OPTION_WATCH_DELETE = OPTION_START,
55 /* Break an option number into its op/int-nr */
56 static watchpoint_type
57 option_to_type (SIM_DESC sd,
60 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
61 watchpoint_type type = ((option - OPTION_WATCH_OP)
62 / (watch->nr_interrupts + 1));
63 SIM_ASSERT (type >= 0 && type < nr_watchpoint_types);
68 option_to_interrupt_nr (SIM_DESC sd,
71 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
72 int interrupt_nr = ((option - OPTION_WATCH_OP)
73 % (watch->nr_interrupts + 1));
78 type_to_option (SIM_DESC sd,
82 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
83 return ((type * (watch->nr_interrupts + 1))
89 /* Delete one or more watchpoints. Fail if no watchpoints were found */
92 do_watchpoint_delete (SIM_DESC sd,
96 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
97 sim_watch_point **entry = &watch->points;
98 SIM_RC status = SIM_RC_FAIL;
99 while ((*entry) != NULL)
101 if ((*entry)->ident == ident
102 || (*entry)->type == type)
104 sim_watch_point *dead = (*entry);
105 (*entry) = (*entry)->next;
106 sim_events_deschedule (sd, dead->event);
111 entry = &(*entry)->next;
117 watchpoint_type_to_str (SIM_DESC sd,
118 watchpoint_type type)
124 case clock_watchpoint:
126 case cycles_watchpoint:
128 case invalid_watchpoint:
129 case nr_watchpoint_types:
130 return "(invalid-type)";
136 interrupt_nr_to_str (SIM_DESC sd,
139 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
140 if (interrupt_nr < 0)
141 return "(invalid-interrupt)";
142 else if (interrupt_nr >= watch->nr_interrupts)
145 return watch->interrupt_names[interrupt_nr];
150 do_watchpoint_info (SIM_DESC sd)
152 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
153 sim_watch_point *point;
154 sim_io_printf (sd, "Watchpoints:\n");
155 for (point = watch->points; point != NULL; point = point->next)
157 sim_io_printf (sd, "%3d: watch %s %s ",
159 watchpoint_type_to_str (sd, point->type),
160 interrupt_nr_to_str (sd, point->interrupt_nr));
161 if (point->is_periodic)
162 sim_io_printf (sd, "+");
163 if (!point->is_within)
164 sim_io_printf (sd, "!");
165 sim_io_printf (sd, "0x%lx", point->arg0);
166 if (point->arg1 != point->arg0)
167 sim_io_printf (sd, ",0x%lx", point->arg1);
168 sim_io_printf (sd, "\n");
174 static sim_event_handler handle_watchpoint;
177 schedule_watchpoint (SIM_DESC sd,
178 sim_watch_point *point)
180 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
184 point->event = sim_events_watch_sim (sd,
189 point->arg0, point->arg1,
190 /* PC in arg0..arg1 */
194 case clock_watchpoint:
195 point->event = sim_events_watch_clock (sd,
196 point->arg0, /* ms time */
200 case cycles_watchpoint:
201 point->event = sim_events_schedule (sd,
202 point->arg0, /* time */
207 sim_engine_abort (sd, NULL, NULL_CIA,
208 "handle_watchpoint - internal error - bad switch");
216 handle_watchpoint (SIM_DESC sd, void *data)
218 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
219 sim_watch_point *point = (sim_watch_point *) data;
220 int interrupt_nr = point->interrupt_nr;
222 if (point->is_periodic)
223 /* reschedule this event before processing it */
224 schedule_watchpoint (sd, point);
226 do_watchpoint_delete (sd, point->ident, invalid_watchpoint);
228 if (point->interrupt_nr == watch->nr_interrupts)
229 sim_engine_halt (sd, NULL, NULL, NULL_CIA, sim_stopped, SIGINT);
231 watch->interrupt_handler (sd, &interrupt_nr);
236 do_watchpoint_create (SIM_DESC sd,
237 watchpoint_type type,
241 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
242 sim_watch_point **point;
244 /* create the watchpoint */
245 point = &watch->points;
246 while ((*point) != NULL)
247 point = &(*point)->next;
248 (*point) = ZALLOC (sim_watch_point);
250 /* fill in the details */
251 (*point)->ident = ++(watch->last_point_nr);
252 (*point)->type = option_to_type (sd, opt);
253 (*point)->interrupt_nr = option_to_interrupt_nr (sd, opt);
254 /* prefixes to arg - +== periodic, !==not or outside */
255 (*point)->is_within = 1;
259 (*point)->is_periodic = 1;
260 else if (arg[0] == '!')
261 (*point)->is_within = 0;
267 (*point)->arg0 = strtoul (arg, &arg, 0);
269 (*point)->arg0 = strtoul (arg, NULL, 0);
271 (*point)->arg1 = (*point)->arg0;
274 schedule_watchpoint (sd, (*point));
281 watchpoint_option_handler (sd, opt, arg, is_command)
287 if (opt >= OPTION_WATCH_OP)
288 return do_watchpoint_create (sd, clock_watchpoint, opt, arg);
293 case OPTION_WATCH_DELETE:
294 if (isdigit ((int) arg[0]))
296 int ident = strtol (arg, NULL, 0);
297 if (do_watchpoint_delete (sd, ident, invalid_watchpoint)
300 sim_io_eprintf (sd, "Watchpoint %d not found\n", ident);
305 else if (strcasecmp (arg, "all") == 0)
307 watchpoint_type type;
308 for (type = invalid_watchpoint + 1;
309 type < nr_watchpoint_types;
312 do_watchpoint_delete (sd, 0, type);
316 else if (strcasecmp (arg, "pc") == 0)
318 if (do_watchpoint_delete (sd, 0, pc_watchpoint)
321 sim_io_eprintf (sd, "No PC watchpoints found\n");
326 else if (strcasecmp (arg, "clock") == 0)
328 if (do_watchpoint_delete (sd, 0, clock_watchpoint) != SIM_RC_OK)
330 sim_io_eprintf (sd, "No CLOCK watchpoints found\n");
335 else if (strcasecmp (arg, "cycles") == 0)
337 if (do_watchpoint_delete (sd, 0, cycles_watchpoint) != SIM_RC_OK)
339 sim_io_eprintf (sd, "No CYCLES watchpoints found\n");
344 sim_io_eprintf (sd, "Unknown watchpoint type `%s'\n", arg);
347 case OPTION_WATCH_INFO:
349 do_watchpoint_info (sd);
354 sim_io_eprintf (sd, "Unknown watch option %d\n", opt);
363 sim_watchpoint_init (SIM_DESC sd)
365 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
366 sim_watch_point *point;
367 /* NOTE: Do not need to de-schedule any previous watchpoints as
368 sim-events has already done this */
369 /* schedule any watchpoints enabled by command line options */
370 for (point = watch->points; point != NULL; point = point->next)
372 schedule_watchpoint (sd, point);
378 static const OPTION watchpoint_options[] =
380 { {"watch-delete", required_argument, NULL, OPTION_WATCH_DELETE },
381 '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint",
382 watchpoint_option_handler },
384 { {"watch-info", no_argument, NULL, OPTION_WATCH_INFO },
385 '\0', NULL, "List scheduled watchpoints",
386 watchpoint_option_handler },
388 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
391 static const OPTION template_int_option = {
392 { NULL, required_argument, NULL, 0 },
393 '\0', "VALUE", "Create the specified watchpoint",
394 watchpoint_option_handler,
397 static char *default_interrupt_names[] = { "int", 0, };
402 sim_watchpoint_install (SIM_DESC sd)
404 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
405 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
406 /* the basic command set */
407 sim_module_add_init_fn (sd, sim_watchpoint_init);
408 sim_add_option_table (sd, watchpoint_options);
409 /* fill in some details */
410 if (watch->interrupt_names == NULL)
411 watch->interrupt_names = default_interrupt_names;
412 watch->nr_interrupts = 0;
413 while (watch->interrupt_names[watch->nr_interrupts] != NULL)
414 watch->nr_interrupts++;
415 /* generate more advansed commands */
417 OPTION *int_options = NZALLOC (OPTION, 1 + (watch->nr_interrupts + 1) * nr_watchpoint_types);
419 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
421 watchpoint_type type;
422 for (type = 0; type < nr_watchpoint_types; type++)
424 int nr = interrupt_nr * nr_watchpoint_types + type;
425 OPTION *option = &int_options[nr];
427 *option = template_int_option;
428 asprintf (&name, "watch-%s-%s",
429 watchpoint_type_to_str (sd, type),
430 interrupt_nr_to_str (sd, interrupt_nr));
431 option->opt.name = name;
432 option->opt.val = type_to_option (sd, type, interrupt_nr);
435 sim_add_option_table (sd, int_options);