1 /* Generic simulator watchpoint support.
2 Copyright (C) 1997-2015 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 3 of the License, or
10 (at your option) any later version.
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "sim-options.h"
23 #include "sim-assert.h"
40 OPTION_WATCH_DELETE = OPTION_START,
51 /* Break an option number into its op/int-nr */
52 static watchpoint_type
53 option_to_type (SIM_DESC sd,
56 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
57 watchpoint_type type = ((option - OPTION_WATCH_OP)
58 / (watch->nr_interrupts + 1));
59 SIM_ASSERT (type >= 0 && type < nr_watchpoint_types);
64 option_to_interrupt_nr (SIM_DESC sd,
67 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
68 int interrupt_nr = ((option - OPTION_WATCH_OP)
69 % (watch->nr_interrupts + 1));
74 type_to_option (SIM_DESC sd,
78 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
79 return ((type * (watch->nr_interrupts + 1))
85 /* Delete one or more watchpoints. Fail if no watchpoints were found */
88 do_watchpoint_delete (SIM_DESC sd,
92 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
93 sim_watch_point **entry = &watch->points;
94 SIM_RC status = SIM_RC_FAIL;
95 while ((*entry) != NULL)
97 if ((*entry)->ident == ident
98 || (*entry)->type == type)
100 sim_watch_point *dead = (*entry);
101 (*entry) = (*entry)->next;
102 sim_events_deschedule (sd, dead->event);
107 entry = &(*entry)->next;
113 watchpoint_type_to_str (SIM_DESC sd,
114 watchpoint_type type)
120 case clock_watchpoint:
122 case cycles_watchpoint:
124 case invalid_watchpoint:
125 case nr_watchpoint_types:
126 return "(invalid-type)";
132 interrupt_nr_to_str (SIM_DESC sd,
135 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
136 if (interrupt_nr < 0)
137 return "(invalid-interrupt)";
138 else if (interrupt_nr >= watch->nr_interrupts)
141 return watch->interrupt_names[interrupt_nr];
146 do_watchpoint_info (SIM_DESC sd)
148 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
149 sim_watch_point *point;
150 sim_io_printf (sd, "Watchpoints:\n");
151 for (point = watch->points; point != NULL; point = point->next)
153 sim_io_printf (sd, "%3d: watch %s %s ",
155 watchpoint_type_to_str (sd, point->type),
156 interrupt_nr_to_str (sd, point->interrupt_nr));
157 if (point->is_periodic)
158 sim_io_printf (sd, "+");
159 if (!point->is_within)
160 sim_io_printf (sd, "!");
161 sim_io_printf (sd, "0x%lx", point->arg0);
162 if (point->arg1 != point->arg0)
163 sim_io_printf (sd, ",0x%lx", point->arg1);
164 sim_io_printf (sd, "\n");
170 static sim_event_handler handle_watchpoint;
173 schedule_watchpoint (SIM_DESC sd,
174 sim_watch_point *point)
176 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
180 point->event = sim_events_watch_sim (sd,
185 point->arg0, point->arg1,
186 /* PC in arg0..arg1 */
190 case clock_watchpoint:
191 point->event = sim_events_watch_clock (sd,
192 point->arg0, /* ms time */
196 case cycles_watchpoint:
197 point->event = sim_events_schedule (sd,
198 point->arg0, /* time */
203 sim_engine_abort (sd, NULL, NULL_CIA,
204 "handle_watchpoint - internal error - bad switch");
212 handle_watchpoint (SIM_DESC sd, void *data)
214 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
215 sim_watch_point *point = (sim_watch_point *) data;
216 int interrupt_nr = point->interrupt_nr;
218 if (point->is_periodic)
219 /* reschedule this event before processing it */
220 schedule_watchpoint (sd, point);
222 do_watchpoint_delete (sd, point->ident, invalid_watchpoint);
224 if (point->interrupt_nr == watch->nr_interrupts)
225 sim_engine_halt (sd, NULL, NULL, NULL_CIA, sim_stopped, SIM_SIGINT);
227 watch->interrupt_handler (sd, &watch->interrupt_names[interrupt_nr]);
232 do_watchpoint_create (SIM_DESC sd,
233 watchpoint_type type,
237 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
238 sim_watch_point **point;
240 /* create the watchpoint */
241 point = &watch->points;
242 while ((*point) != NULL)
243 point = &(*point)->next;
244 (*point) = ZALLOC (sim_watch_point);
246 /* fill in the details */
247 (*point)->ident = ++(watch->last_point_nr);
248 (*point)->type = option_to_type (sd, opt);
249 (*point)->interrupt_nr = option_to_interrupt_nr (sd, opt);
250 /* prefixes to arg - +== periodic, !==not or outside */
251 (*point)->is_within = 1;
255 (*point)->is_periodic = 1;
256 else if (arg[0] == '!')
257 (*point)->is_within = 0;
263 (*point)->arg0 = strtoul (arg, &arg, 0);
265 (*point)->arg0 = strtoul (arg, NULL, 0);
267 (*point)->arg1 = (*point)->arg0;
270 schedule_watchpoint (sd, (*point));
277 watchpoint_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
278 char *arg, int is_command)
280 if (opt >= OPTION_WATCH_OP)
281 return do_watchpoint_create (sd, clock_watchpoint, opt, arg);
286 case OPTION_WATCH_DELETE:
287 if (isdigit ((int) arg[0]))
289 int ident = strtol (arg, NULL, 0);
290 if (do_watchpoint_delete (sd, ident, invalid_watchpoint)
293 sim_io_eprintf (sd, "Watchpoint %d not found\n", ident);
298 else if (strcasecmp (arg, "all") == 0)
300 watchpoint_type type;
301 for (type = invalid_watchpoint + 1;
302 type < nr_watchpoint_types;
305 do_watchpoint_delete (sd, 0, type);
309 else if (strcasecmp (arg, "pc") == 0)
311 if (do_watchpoint_delete (sd, 0, pc_watchpoint)
314 sim_io_eprintf (sd, "No PC watchpoints found\n");
319 else if (strcasecmp (arg, "clock") == 0)
321 if (do_watchpoint_delete (sd, 0, clock_watchpoint) != SIM_RC_OK)
323 sim_io_eprintf (sd, "No CLOCK watchpoints found\n");
328 else if (strcasecmp (arg, "cycles") == 0)
330 if (do_watchpoint_delete (sd, 0, cycles_watchpoint) != SIM_RC_OK)
332 sim_io_eprintf (sd, "No CYCLES watchpoints found\n");
337 sim_io_eprintf (sd, "Unknown watchpoint type `%s'\n", arg);
340 case OPTION_WATCH_INFO:
342 do_watchpoint_info (sd);
347 sim_io_eprintf (sd, "Unknown watch option %d\n", opt);
356 sim_watchpoint_init (SIM_DESC sd)
358 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
359 sim_watch_point *point;
360 /* NOTE: Do not need to de-schedule any previous watchpoints as
361 sim-events has already done this */
362 /* schedule any watchpoints enabled by command line options */
363 for (point = watch->points; point != NULL; point = point->next)
365 schedule_watchpoint (sd, point);
371 static const OPTION watchpoint_options[] =
373 { {"watch-delete", required_argument, NULL, OPTION_WATCH_DELETE },
374 '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint",
375 watchpoint_option_handler, NULL },
377 { {"watch-info", no_argument, NULL, OPTION_WATCH_INFO },
378 '\0', NULL, "List scheduled watchpoints",
379 watchpoint_option_handler, NULL },
381 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
384 static const char *default_interrupt_names[] = { "int", 0, };
389 sim_watchpoint_install (SIM_DESC sd)
391 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
392 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
393 /* the basic command set */
394 sim_module_add_init_fn (sd, sim_watchpoint_init);
395 sim_add_option_table (sd, NULL, watchpoint_options);
396 /* fill in some details */
397 if (watch->interrupt_names == NULL)
398 watch->interrupt_names = default_interrupt_names;
399 watch->nr_interrupts = 0;
400 while (watch->interrupt_names[watch->nr_interrupts] != NULL)
401 watch->nr_interrupts++;
402 /* generate more advansed commands */
404 OPTION *int_options = NZALLOC (OPTION, 1 + (watch->nr_interrupts + 1) * nr_watchpoint_types);
406 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
408 watchpoint_type type;
409 for (type = 0; type < nr_watchpoint_types; type++)
412 int nr = interrupt_nr * nr_watchpoint_types + type;
413 OPTION *option = &int_options[nr];
414 if (asprintf (&name, "watch-%s-%s",
415 watchpoint_type_to_str (sd, type),
416 interrupt_nr_to_str (sd, interrupt_nr)) < 0)
418 option->opt.name = name;
419 option->opt.has_arg = required_argument;
420 option->opt.val = type_to_option (sd, type, interrupt_nr);
422 option->doc_name = "";
423 option->handler = watchpoint_option_handler;
426 /* adjust first few entries so that they contain real
427 documentation, the first entry includes a list of actions. */
430 "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is";
432 int len = strlen (prefix) + 1;
433 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
434 len += strlen (interrupt_nr_to_str (sd, interrupt_nr)) + 1;
435 doc = NZALLOC (char, len);
436 strcpy (doc, prefix);
437 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
440 strcat (doc, interrupt_nr_to_str (sd, interrupt_nr));
442 int_options[0].doc_name = "watch-cycles-ACTION";
443 int_options[0].arg = "[+]COUNT";
444 int_options[0].doc = doc;
446 int_options[1].doc_name = "watch-pc-ACTION";
447 int_options[1].arg = "[!]ADDRESS";
449 "Watch the PC, take ACTION when matches ADDRESS (in range ADDRESS,ADDRESS), `!' negates test";
450 int_options[2].doc_name = "watch-clock-ACTION";
451 int_options[2].arg = "[+]MILLISECONDS";
453 "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)";
455 sim_add_option_table (sd, NULL, int_options);