* gx sim prototype tweaks
[binutils-gdb.git] / sim / common / sim-watch.c
blobd1643b655b1f0a484de9beb3175ddeffc0dc1954
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)
10 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 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. */
21 #include "sim-main.h"
22 #include "sim-options.h"
24 #include "sim-assert.h"
26 #include <ctype.h>
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #else
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34 #endif
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
40 #include <signal.h>
43 enum {
44 OPTION_WATCH_DELETE = OPTION_START,
46 OPTION_WATCH_INFO,
47 OPTION_WATCH_CLOCK,
48 OPTION_WATCH_CYCLES,
49 OPTION_WATCH_PC,
51 OPTION_WATCH_OP,
55 /* Break an option number into its op/int-nr */
56 static watchpoint_type
57 option_to_type (SIM_DESC sd,
58 int option)
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);
64 return type;
67 static int
68 option_to_interrupt_nr (SIM_DESC sd,
69 int option)
71 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
72 int interrupt_nr = ((option - OPTION_WATCH_OP)
73 % (watch->nr_interrupts + 1));
74 return interrupt_nr;
77 static int
78 type_to_option (SIM_DESC sd,
79 watchpoint_type type,
80 int interrupt_nr)
82 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
83 return ((type * (watch->nr_interrupts + 1))
84 + interrupt_nr
85 + OPTION_WATCH_OP);
89 /* Delete one or more watchpoints. Fail if no watchpoints were found */
91 static SIM_RC
92 do_watchpoint_delete (SIM_DESC sd,
93 int ident,
94 watchpoint_type type)
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);
107 zfree (dead);
108 status = SIM_RC_OK;
110 else
111 entry = &(*entry)->next;
113 return status;
116 static char *
117 watchpoint_type_to_str (SIM_DESC sd,
118 watchpoint_type type)
120 switch (type)
122 case pc_watchpoint:
123 return "pc";
124 case clock_watchpoint:
125 return "clock";
126 case cycles_watchpoint:
127 return "cycles";
128 case invalid_watchpoint:
129 case nr_watchpoint_types:
130 return "(invalid-type)";
132 return NULL;
135 static char *
136 interrupt_nr_to_str (SIM_DESC sd,
137 int interrupt_nr)
139 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
140 if (interrupt_nr < 0)
141 return "(invalid-interrupt)";
142 else if (interrupt_nr >= watch->nr_interrupts)
143 return "breakpoint";
144 else
145 return watch->interrupt_names[interrupt_nr];
149 static void
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 ",
158 point->ident,
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;
176 static SIM_RC
177 schedule_watchpoint (SIM_DESC sd,
178 sim_watch_point *point)
180 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
181 switch (point->type)
183 case pc_watchpoint:
184 point->event = sim_events_watch_sim (sd,
185 watch->pc,
186 watch->sizeof_pc,
187 0/* host-endian */,
188 point->is_within,
189 point->arg0, point->arg1,
190 /* PC in arg0..arg1 */
191 handle_watchpoint,
192 point);
193 return SIM_RC_OK;
194 case clock_watchpoint:
195 point->event = sim_events_watch_clock (sd,
196 point->arg0, /* ms time */
197 handle_watchpoint,
198 point);
199 return SIM_RC_OK;
200 case cycles_watchpoint:
201 point->event = sim_events_schedule (sd,
202 point->arg0, /* time */
203 handle_watchpoint,
204 point);
205 return SIM_RC_OK;
206 default:
207 sim_engine_abort (sd, NULL, NULL_CIA,
208 "handle_watchpoint - internal error - bad switch");
209 return SIM_RC_FAIL;
211 return SIM_RC_OK;
215 static void
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);
225 else
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);
230 else
231 watch->interrupt_handler (sd, &watch->interrupt_names[interrupt_nr]);
235 static SIM_RC
236 do_watchpoint_create (SIM_DESC sd,
237 watchpoint_type type,
238 int opt,
239 char *arg)
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;
256 while (1)
258 if (arg[0] == '+')
259 (*point)->is_periodic = 1;
260 else if (arg[0] == '!')
261 (*point)->is_within = 0;
262 else
263 break;
264 arg++;
267 (*point)->arg0 = strtoul (arg, &arg, 0);
268 if (arg[0] == ',')
269 (*point)->arg0 = strtoul (arg, NULL, 0);
270 else
271 (*point)->arg1 = (*point)->arg0;
273 /* schedule it */
274 schedule_watchpoint (sd, (*point));
276 return SIM_RC_OK;
280 static SIM_RC
281 watchpoint_option_handler (sd, opt, arg, is_command)
282 SIM_DESC sd;
283 int opt;
284 char *arg;
285 int is_command;
287 if (opt >= OPTION_WATCH_OP)
288 return do_watchpoint_create (sd, clock_watchpoint, opt, arg);
289 else
290 switch (opt)
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)
298 != SIM_RC_OK)
300 sim_io_eprintf (sd, "Watchpoint %d not found\n", ident);
301 return SIM_RC_FAIL;
303 return SIM_RC_OK;
305 else if (strcasecmp (arg, "all") == 0)
307 watchpoint_type type;
308 for (type = invalid_watchpoint + 1;
309 type < nr_watchpoint_types;
310 type++)
312 do_watchpoint_delete (sd, 0, type);
314 return SIM_RC_OK;
316 else if (strcasecmp (arg, "pc") == 0)
318 if (do_watchpoint_delete (sd, 0, pc_watchpoint)
319 != SIM_RC_OK)
321 sim_io_eprintf (sd, "No PC watchpoints found\n");
322 return SIM_RC_FAIL;
324 return SIM_RC_OK;
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");
331 return SIM_RC_FAIL;
333 return SIM_RC_OK;
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");
340 return SIM_RC_FAIL;
342 return SIM_RC_OK;
344 sim_io_eprintf (sd, "Unknown watchpoint type `%s'\n", arg);
345 return SIM_RC_FAIL;
347 case OPTION_WATCH_INFO:
349 do_watchpoint_info (sd);
350 return SIM_RC_OK;
353 default:
354 sim_io_eprintf (sd, "Unknown watch option %d\n", opt);
355 return SIM_RC_FAIL;
362 static SIM_RC
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);
374 return SIM_RC_OK;
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 char *default_interrupt_names[] = { "int", 0, };
395 SIM_RC
396 sim_watchpoint_install (SIM_DESC sd)
398 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
399 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
400 /* the basic command set */
401 sim_module_add_init_fn (sd, sim_watchpoint_init);
402 sim_add_option_table (sd, watchpoint_options);
403 /* fill in some details */
404 if (watch->interrupt_names == NULL)
405 watch->interrupt_names = default_interrupt_names;
406 watch->nr_interrupts = 0;
407 while (watch->interrupt_names[watch->nr_interrupts] != NULL)
408 watch->nr_interrupts++;
409 /* generate more advansed commands */
411 OPTION *int_options = NZALLOC (OPTION, 1 + (watch->nr_interrupts + 1) * nr_watchpoint_types);
412 int interrupt_nr;
413 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
415 watchpoint_type type;
416 for (type = 0; type < nr_watchpoint_types; type++)
418 char *name;
419 int nr = interrupt_nr * nr_watchpoint_types + type;
420 OPTION *option = &int_options[nr];
421 asprintf (&name, "watch-%s-%s",
422 watchpoint_type_to_str (sd, type),
423 interrupt_nr_to_str (sd, interrupt_nr));
424 option->opt.name = name;
425 option->opt.has_arg = required_argument;
426 option->opt.val = type_to_option (sd, type, interrupt_nr);
427 option->doc = "";
428 option->doc_name = "";
431 /* adjust first few entries so that they contain real
432 documentation, the first entry includes a list of actions. */
434 char *prefix =
435 "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is";
436 char *doc;
437 int len = strlen (prefix) + 1;
438 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
439 len += strlen (interrupt_nr_to_str (sd, interrupt_nr)) + 1;
440 doc = NZALLOC (char, len);
441 strcpy (doc, prefix);
442 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
444 strcat (doc, " ");
445 strcat (doc, interrupt_nr_to_str (sd, interrupt_nr));
447 int_options[0].doc_name = "watch-cycles-ACTION";
448 int_options[0].arg = "[+]COUNT";
449 int_options[0].doc = doc;
451 int_options[1].doc_name = "watch-pc-ACTION";
452 int_options[1].arg = "[!]ADDRESS";
453 int_options[1].doc =
454 "Watch the PC, take ACTION when matches ADDRESS (in range ADDRESS,ADDRESS), `!' negates test";
455 int_options[2].doc_name = "watch-clock-ACTION";
456 int_options[2].arg = "[+]MILLISECONDS";
457 int_options[2].doc =
458 "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)";
460 sim_add_option_table (sd, int_options);
462 return SIM_RC_OK;