1 /* Generic simulator watchpoint support.
2 Copyright (C) 1997-2021 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/>. */
20 /* This must come before any other includes. */
24 #include "sim-options.h"
25 #include "libiberty.h"
27 #include "sim-assert.h"
35 OPTION_WATCH_DELETE
= OPTION_START
,
46 /* Break an option number into its op/int-nr */
47 static watchpoint_type
48 option_to_type (SIM_DESC sd
,
51 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
52 watchpoint_type type
= ((option
- OPTION_WATCH_OP
)
53 / (watch
->nr_interrupts
+ 1));
54 SIM_ASSERT (type
>= 0 && type
< nr_watchpoint_types
);
59 option_to_interrupt_nr (SIM_DESC sd
,
62 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
63 int interrupt_nr
= ((option
- OPTION_WATCH_OP
)
64 % (watch
->nr_interrupts
+ 1));
69 type_to_option (SIM_DESC sd
,
73 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
74 return ((type
* (watch
->nr_interrupts
+ 1))
80 /* Delete one or more watchpoints. Fail if no watchpoints were found */
83 do_watchpoint_delete (SIM_DESC sd
,
87 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
88 sim_watch_point
**entry
= &watch
->points
;
89 SIM_RC status
= SIM_RC_FAIL
;
90 while ((*entry
) != NULL
)
92 if ((*entry
)->ident
== ident
93 || (*entry
)->type
== type
)
95 sim_watch_point
*dead
= (*entry
);
96 (*entry
) = (*entry
)->next
;
97 sim_events_deschedule (sd
, dead
->event
);
102 entry
= &(*entry
)->next
;
108 watchpoint_type_to_str (SIM_DESC sd
,
109 watchpoint_type type
)
115 case clock_watchpoint
:
117 case cycles_watchpoint
:
119 case invalid_watchpoint
:
120 case nr_watchpoint_types
:
121 return "(invalid-type)";
127 interrupt_nr_to_str (SIM_DESC sd
,
130 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
131 if (interrupt_nr
< 0)
132 return "(invalid-interrupt)";
133 else if (interrupt_nr
>= watch
->nr_interrupts
)
136 return watch
->interrupt_names
[interrupt_nr
];
141 do_watchpoint_info (SIM_DESC sd
)
143 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
144 sim_watch_point
*point
;
145 sim_io_printf (sd
, "Watchpoints:\n");
146 for (point
= watch
->points
; point
!= NULL
; point
= point
->next
)
148 sim_io_printf (sd
, "%3d: watch %s %s ",
150 watchpoint_type_to_str (sd
, point
->type
),
151 interrupt_nr_to_str (sd
, point
->interrupt_nr
));
152 if (point
->is_periodic
)
153 sim_io_printf (sd
, "+");
154 if (!point
->is_within
)
155 sim_io_printf (sd
, "!");
156 sim_io_printf (sd
, "0x%lx", point
->arg0
);
157 if (point
->arg1
!= point
->arg0
)
158 sim_io_printf (sd
, ",0x%lx", point
->arg1
);
159 sim_io_printf (sd
, "\n");
165 static sim_event_handler handle_watchpoint
;
168 schedule_watchpoint (SIM_DESC sd
,
169 sim_watch_point
*point
)
171 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
176 point
->event
= sim_events_watch_pc (sd
,
178 point
->arg0
, point
->arg1
,
179 /* PC in arg0..arg1 */
183 case clock_watchpoint
:
184 point
->event
= sim_events_watch_clock (sd
,
185 point
->arg0
, /* ms time */
189 case cycles_watchpoint
:
190 point
->event
= sim_events_schedule (sd
,
191 point
->arg0
, /* time */
196 sim_engine_abort (sd
, NULL
, NULL_CIA
,
197 "handle_watchpoint - internal error - bad switch");
205 handle_watchpoint (SIM_DESC sd
, void *data
)
207 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
208 sim_watch_point
*point
= (sim_watch_point
*) data
;
209 int interrupt_nr
= point
->interrupt_nr
;
211 if (point
->is_periodic
)
212 /* reschedule this event before processing it */
213 schedule_watchpoint (sd
, point
);
215 do_watchpoint_delete (sd
, point
->ident
, invalid_watchpoint
);
217 if (point
->interrupt_nr
== watch
->nr_interrupts
)
218 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGINT
);
220 watch
->interrupt_handler (sd
, &watch
->interrupt_names
[interrupt_nr
]);
225 do_watchpoint_create (SIM_DESC sd
,
226 watchpoint_type type
,
230 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
231 sim_watch_point
**point
;
233 /* create the watchpoint */
234 point
= &watch
->points
;
235 while ((*point
) != NULL
)
236 point
= &(*point
)->next
;
237 (*point
) = ZALLOC (sim_watch_point
);
239 /* fill in the details */
240 (*point
)->ident
= ++(watch
->last_point_nr
);
241 (*point
)->type
= option_to_type (sd
, opt
);
242 (*point
)->interrupt_nr
= option_to_interrupt_nr (sd
, opt
);
243 /* prefixes to arg - +== periodic, !==not or outside */
244 (*point
)->is_within
= 1;
248 (*point
)->is_periodic
= 1;
249 else if (arg
[0] == '!')
250 (*point
)->is_within
= 0;
256 (*point
)->arg0
= strtoul (arg
, &arg
, 0);
258 (*point
)->arg1
= strtoul (arg
+ 1, NULL
, 0);
260 (*point
)->arg1
= (*point
)->arg0
;
263 schedule_watchpoint (sd
, (*point
));
270 watchpoint_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
271 char *arg
, int is_command
)
273 if (opt
>= OPTION_WATCH_OP
)
274 return do_watchpoint_create (sd
, clock_watchpoint
, opt
, arg
);
279 case OPTION_WATCH_DELETE
:
280 if (isdigit ((int) arg
[0]))
282 int ident
= strtol (arg
, NULL
, 0);
283 if (do_watchpoint_delete (sd
, ident
, invalid_watchpoint
)
286 sim_io_eprintf (sd
, "Watchpoint %d not found\n", ident
);
291 else if (strcasecmp (arg
, "all") == 0)
293 watchpoint_type type
;
294 for (type
= invalid_watchpoint
+ 1;
295 type
< nr_watchpoint_types
;
298 do_watchpoint_delete (sd
, 0, type
);
302 else if (strcasecmp (arg
, "pc") == 0)
304 if (do_watchpoint_delete (sd
, 0, pc_watchpoint
)
307 sim_io_eprintf (sd
, "No PC watchpoints found\n");
312 else if (strcasecmp (arg
, "clock") == 0)
314 if (do_watchpoint_delete (sd
, 0, clock_watchpoint
) != SIM_RC_OK
)
316 sim_io_eprintf (sd
, "No CLOCK watchpoints found\n");
321 else if (strcasecmp (arg
, "cycles") == 0)
323 if (do_watchpoint_delete (sd
, 0, cycles_watchpoint
) != SIM_RC_OK
)
325 sim_io_eprintf (sd
, "No CYCLES watchpoints found\n");
330 sim_io_eprintf (sd
, "Unknown watchpoint type `%s'\n", arg
);
333 case OPTION_WATCH_INFO
:
335 do_watchpoint_info (sd
);
340 sim_io_eprintf (sd
, "Unknown watch option %d\n", opt
);
349 sim_watchpoint_init (SIM_DESC sd
)
351 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
352 sim_watch_point
*point
;
353 /* NOTE: Do not need to de-schedule any previous watchpoints as
354 sim-events has already done this */
355 /* schedule any watchpoints enabled by command line options */
356 for (point
= watch
->points
; point
!= NULL
; point
= point
->next
)
358 schedule_watchpoint (sd
, point
);
364 static const OPTION watchpoint_options
[] =
366 { {"watch-delete", required_argument
, NULL
, OPTION_WATCH_DELETE
},
367 '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint",
368 watchpoint_option_handler
, NULL
},
370 { {"watch-info", no_argument
, NULL
, OPTION_WATCH_INFO
},
371 '\0', NULL
, "List scheduled watchpoints",
372 watchpoint_option_handler
, NULL
},
374 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
377 static const char *default_interrupt_names
[] = { "int", 0, };
379 /* This default handler is "good enough" for targets that just want to trap into
380 gdb when watchpoints are hit, and have only configured the STATE_WATCHPOINTS
383 default_interrupt_handler (SIM_DESC sd
, void *data
)
385 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
386 address_word cia
= CPU_PC_GET (cpu
);
387 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_stopped
, SIM_SIGTRAP
);
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 if (watch
->interrupt_handler
== NULL
)
402 watch
->interrupt_handler
= default_interrupt_handler
;
403 watch
->nr_interrupts
= 0;
404 while (watch
->interrupt_names
[watch
->nr_interrupts
] != NULL
)
405 watch
->nr_interrupts
++;
406 /* generate more advansed commands */
408 OPTION
*int_options
= NZALLOC (OPTION
, 1 + (watch
->nr_interrupts
+ 1) * nr_watchpoint_types
);
410 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
412 watchpoint_type type
;
413 for (type
= 0; type
< nr_watchpoint_types
; type
++)
416 int nr
= interrupt_nr
* nr_watchpoint_types
+ type
;
417 OPTION
*option
= &int_options
[nr
];
418 if (asprintf (&name
, "watch-%s-%s",
419 watchpoint_type_to_str (sd
, type
),
420 interrupt_nr_to_str (sd
, interrupt_nr
)) < 0)
422 option
->opt
.name
= name
;
423 option
->opt
.has_arg
= required_argument
;
424 option
->opt
.val
= type_to_option (sd
, type
, interrupt_nr
);
426 option
->doc_name
= "";
427 option
->handler
= watchpoint_option_handler
;
430 /* adjust first few entries so that they contain real
431 documentation, the first entry includes a list of actions. */
434 "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is";
436 int len
= strlen (prefix
) + 1;
437 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
438 len
+= strlen (interrupt_nr_to_str (sd
, interrupt_nr
)) + 1;
439 doc
= NZALLOC (char, len
);
440 strcpy (doc
, prefix
);
441 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
444 strcat (doc
, interrupt_nr_to_str (sd
, interrupt_nr
));
446 int_options
[0].doc_name
= "watch-cycles-ACTION";
447 int_options
[0].arg
= "[+]COUNT";
448 int_options
[0].doc
= doc
;
450 int_options
[1].doc_name
= "watch-pc-ACTION";
451 int_options
[1].arg
= "[!]ADDRESS";
453 "Watch the PC, take ACTION when matches ADDRESS (in range ADDRESS,ADDRESS), `!' negates test";
454 int_options
[2].doc_name
= "watch-clock-ACTION";
455 int_options
[2].arg
= "[+]MILLISECONDS";
457 "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)";
459 sim_add_option_table (sd
, NULL
, int_options
);