1 /* Generic simulator watchpoint support.
2 Copyright (C) 1997-2019 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/>. */
22 #include "sim-options.h"
24 #include "sim-assert.h"
42 OPTION_WATCH_DELETE
= OPTION_START
,
53 /* Break an option number into its op/int-nr */
54 static watchpoint_type
55 option_to_type (SIM_DESC sd
,
58 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
59 watchpoint_type type
= ((option
- OPTION_WATCH_OP
)
60 / (watch
->nr_interrupts
+ 1));
61 SIM_ASSERT (type
>= 0 && type
< nr_watchpoint_types
);
66 option_to_interrupt_nr (SIM_DESC sd
,
69 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
70 int interrupt_nr
= ((option
- OPTION_WATCH_OP
)
71 % (watch
->nr_interrupts
+ 1));
76 type_to_option (SIM_DESC sd
,
80 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
81 return ((type
* (watch
->nr_interrupts
+ 1))
87 /* Delete one or more watchpoints. Fail if no watchpoints were found */
90 do_watchpoint_delete (SIM_DESC sd
,
94 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
95 sim_watch_point
**entry
= &watch
->points
;
96 SIM_RC status
= SIM_RC_FAIL
;
97 while ((*entry
) != NULL
)
99 if ((*entry
)->ident
== ident
100 || (*entry
)->type
== type
)
102 sim_watch_point
*dead
= (*entry
);
103 (*entry
) = (*entry
)->next
;
104 sim_events_deschedule (sd
, dead
->event
);
109 entry
= &(*entry
)->next
;
115 watchpoint_type_to_str (SIM_DESC sd
,
116 watchpoint_type type
)
122 case clock_watchpoint
:
124 case cycles_watchpoint
:
126 case invalid_watchpoint
:
127 case nr_watchpoint_types
:
128 return "(invalid-type)";
134 interrupt_nr_to_str (SIM_DESC sd
,
137 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
138 if (interrupt_nr
< 0)
139 return "(invalid-interrupt)";
140 else if (interrupt_nr
>= watch
->nr_interrupts
)
143 return watch
->interrupt_names
[interrupt_nr
];
148 do_watchpoint_info (SIM_DESC sd
)
150 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
151 sim_watch_point
*point
;
152 sim_io_printf (sd
, "Watchpoints:\n");
153 for (point
= watch
->points
; point
!= NULL
; point
= point
->next
)
155 sim_io_printf (sd
, "%3d: watch %s %s ",
157 watchpoint_type_to_str (sd
, point
->type
),
158 interrupt_nr_to_str (sd
, point
->interrupt_nr
));
159 if (point
->is_periodic
)
160 sim_io_printf (sd
, "+");
161 if (!point
->is_within
)
162 sim_io_printf (sd
, "!");
163 sim_io_printf (sd
, "0x%lx", point
->arg0
);
164 if (point
->arg1
!= point
->arg0
)
165 sim_io_printf (sd
, ",0x%lx", point
->arg1
);
166 sim_io_printf (sd
, "\n");
172 static sim_event_handler handle_watchpoint
;
175 schedule_watchpoint (SIM_DESC sd
,
176 sim_watch_point
*point
)
178 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
182 point
->event
= sim_events_watch_sim (sd
,
187 point
->arg0
, point
->arg1
,
188 /* PC in arg0..arg1 */
192 case clock_watchpoint
:
193 point
->event
= sim_events_watch_clock (sd
,
194 point
->arg0
, /* ms time */
198 case cycles_watchpoint
:
199 point
->event
= sim_events_schedule (sd
,
200 point
->arg0
, /* time */
205 sim_engine_abort (sd
, NULL
, NULL_CIA
,
206 "handle_watchpoint - internal error - bad switch");
214 handle_watchpoint (SIM_DESC sd
, void *data
)
216 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
217 sim_watch_point
*point
= (sim_watch_point
*) data
;
218 int interrupt_nr
= point
->interrupt_nr
;
220 if (point
->is_periodic
)
221 /* reschedule this event before processing it */
222 schedule_watchpoint (sd
, point
);
224 do_watchpoint_delete (sd
, point
->ident
, invalid_watchpoint
);
226 if (point
->interrupt_nr
== watch
->nr_interrupts
)
227 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGINT
);
229 watch
->interrupt_handler (sd
, &watch
->interrupt_names
[interrupt_nr
]);
234 do_watchpoint_create (SIM_DESC sd
,
235 watchpoint_type type
,
239 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
240 sim_watch_point
**point
;
242 /* create the watchpoint */
243 point
= &watch
->points
;
244 while ((*point
) != NULL
)
245 point
= &(*point
)->next
;
246 (*point
) = ZALLOC (sim_watch_point
);
248 /* fill in the details */
249 (*point
)->ident
= ++(watch
->last_point_nr
);
250 (*point
)->type
= option_to_type (sd
, opt
);
251 (*point
)->interrupt_nr
= option_to_interrupt_nr (sd
, opt
);
252 /* prefixes to arg - +== periodic, !==not or outside */
253 (*point
)->is_within
= 1;
257 (*point
)->is_periodic
= 1;
258 else if (arg
[0] == '!')
259 (*point
)->is_within
= 0;
265 (*point
)->arg0
= strtoul (arg
, &arg
, 0);
267 (*point
)->arg0
= strtoul (arg
, NULL
, 0);
269 (*point
)->arg1
= (*point
)->arg0
;
272 schedule_watchpoint (sd
, (*point
));
279 watchpoint_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
280 char *arg
, int is_command
)
282 if (opt
>= OPTION_WATCH_OP
)
283 return do_watchpoint_create (sd
, clock_watchpoint
, opt
, arg
);
288 case OPTION_WATCH_DELETE
:
289 if (isdigit ((int) arg
[0]))
291 int ident
= strtol (arg
, NULL
, 0);
292 if (do_watchpoint_delete (sd
, ident
, invalid_watchpoint
)
295 sim_io_eprintf (sd
, "Watchpoint %d not found\n", ident
);
300 else if (strcasecmp (arg
, "all") == 0)
302 watchpoint_type type
;
303 for (type
= invalid_watchpoint
+ 1;
304 type
< nr_watchpoint_types
;
307 do_watchpoint_delete (sd
, 0, type
);
311 else if (strcasecmp (arg
, "pc") == 0)
313 if (do_watchpoint_delete (sd
, 0, pc_watchpoint
)
316 sim_io_eprintf (sd
, "No PC watchpoints found\n");
321 else if (strcasecmp (arg
, "clock") == 0)
323 if (do_watchpoint_delete (sd
, 0, clock_watchpoint
) != SIM_RC_OK
)
325 sim_io_eprintf (sd
, "No CLOCK watchpoints found\n");
330 else if (strcasecmp (arg
, "cycles") == 0)
332 if (do_watchpoint_delete (sd
, 0, cycles_watchpoint
) != SIM_RC_OK
)
334 sim_io_eprintf (sd
, "No CYCLES watchpoints found\n");
339 sim_io_eprintf (sd
, "Unknown watchpoint type `%s'\n", arg
);
342 case OPTION_WATCH_INFO
:
344 do_watchpoint_info (sd
);
349 sim_io_eprintf (sd
, "Unknown watch option %d\n", opt
);
358 sim_watchpoint_init (SIM_DESC sd
)
360 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
361 sim_watch_point
*point
;
362 /* NOTE: Do not need to de-schedule any previous watchpoints as
363 sim-events has already done this */
364 /* schedule any watchpoints enabled by command line options */
365 for (point
= watch
->points
; point
!= NULL
; point
= point
->next
)
367 schedule_watchpoint (sd
, point
);
373 static const OPTION watchpoint_options
[] =
375 { {"watch-delete", required_argument
, NULL
, OPTION_WATCH_DELETE
},
376 '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint",
377 watchpoint_option_handler
, NULL
},
379 { {"watch-info", no_argument
, NULL
, OPTION_WATCH_INFO
},
380 '\0', NULL
, "List scheduled watchpoints",
381 watchpoint_option_handler
, NULL
},
383 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
386 static const char *default_interrupt_names
[] = { "int", 0, };
391 sim_watchpoint_install (SIM_DESC sd
)
393 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
394 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
395 /* the basic command set */
396 sim_module_add_init_fn (sd
, sim_watchpoint_init
);
397 sim_add_option_table (sd
, NULL
, watchpoint_options
);
398 /* fill in some details */
399 if (watch
->interrupt_names
== NULL
)
400 watch
->interrupt_names
= default_interrupt_names
;
401 watch
->nr_interrupts
= 0;
402 while (watch
->interrupt_names
[watch
->nr_interrupts
] != NULL
)
403 watch
->nr_interrupts
++;
404 /* generate more advansed commands */
406 OPTION
*int_options
= NZALLOC (OPTION
, 1 + (watch
->nr_interrupts
+ 1) * nr_watchpoint_types
);
408 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
410 watchpoint_type type
;
411 for (type
= 0; type
< nr_watchpoint_types
; type
++)
414 int nr
= interrupt_nr
* nr_watchpoint_types
+ type
;
415 OPTION
*option
= &int_options
[nr
];
416 if (asprintf (&name
, "watch-%s-%s",
417 watchpoint_type_to_str (sd
, type
),
418 interrupt_nr_to_str (sd
, interrupt_nr
)) < 0)
420 option
->opt
.name
= name
;
421 option
->opt
.has_arg
= required_argument
;
422 option
->opt
.val
= type_to_option (sd
, type
, interrupt_nr
);
424 option
->doc_name
= "";
425 option
->handler
= watchpoint_option_handler
;
428 /* adjust first few entries so that they contain real
429 documentation, the first entry includes a list of actions. */
432 "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is";
434 int len
= strlen (prefix
) + 1;
435 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
436 len
+= strlen (interrupt_nr_to_str (sd
, interrupt_nr
)) + 1;
437 doc
= NZALLOC (char, len
);
438 strcpy (doc
, prefix
);
439 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
442 strcat (doc
, interrupt_nr_to_str (sd
, interrupt_nr
));
444 int_options
[0].doc_name
= "watch-cycles-ACTION";
445 int_options
[0].arg
= "[+]COUNT";
446 int_options
[0].doc
= doc
;
448 int_options
[1].doc_name
= "watch-pc-ACTION";
449 int_options
[1].arg
= "[!]ADDRESS";
451 "Watch the PC, take ACTION when matches ADDRESS (in range ADDRESS,ADDRESS), `!' negates test";
452 int_options
[2].doc_name
= "watch-clock-ACTION";
453 int_options
[2].arg
= "[+]MILLISECONDS";
455 "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)";
457 sim_add_option_table (sd
, NULL
, int_options
);