1 /* Generic simulator watchpoint support.
2 Copyright (C) 1997-2024 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. */
28 #include "libiberty.h"
31 #include "sim-options.h"
32 #include "sim-signal.h"
33 #include "sim-assert.h"
36 OPTION_WATCH_DELETE
= OPTION_START
,
47 /* Break an option number into its op/int-nr */
48 static watchpoint_type
49 option_to_type (SIM_DESC sd
,
52 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
53 watchpoint_type type
= ((option
- OPTION_WATCH_OP
)
54 / (watch
->nr_interrupts
+ 1));
55 SIM_ASSERT (type
>= 0 && type
< nr_watchpoint_types
);
60 option_to_interrupt_nr (SIM_DESC sd
,
63 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
64 int interrupt_nr
= ((option
- OPTION_WATCH_OP
)
65 % (watch
->nr_interrupts
+ 1));
70 type_to_option (SIM_DESC sd
,
74 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
75 return ((type
* (watch
->nr_interrupts
+ 1))
81 /* Delete one or more watchpoints. Fail if no watchpoints were found */
84 do_watchpoint_delete (SIM_DESC sd
,
88 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
89 sim_watch_point
**entry
= &watch
->points
;
90 SIM_RC status
= SIM_RC_FAIL
;
91 while ((*entry
) != NULL
)
93 if ((*entry
)->ident
== ident
94 || (*entry
)->type
== type
)
96 sim_watch_point
*dead
= (*entry
);
97 (*entry
) = (*entry
)->next
;
98 sim_events_deschedule (sd
, dead
->event
);
103 entry
= &(*entry
)->next
;
109 watchpoint_type_to_str (SIM_DESC sd
,
110 watchpoint_type type
)
116 case clock_watchpoint
:
118 case cycles_watchpoint
:
120 case invalid_watchpoint
:
121 case nr_watchpoint_types
:
122 return "(invalid-type)";
128 interrupt_nr_to_str (SIM_DESC sd
,
131 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
132 if (interrupt_nr
< 0)
133 return "(invalid-interrupt)";
134 else if (interrupt_nr
>= watch
->nr_interrupts
)
137 return watch
->interrupt_names
[interrupt_nr
];
142 do_watchpoint_info (SIM_DESC sd
)
144 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
145 sim_watch_point
*point
;
146 sim_io_printf (sd
, "Watchpoints:\n");
147 for (point
= watch
->points
; point
!= NULL
; point
= point
->next
)
149 sim_io_printf (sd
, "%3d: watch %s %s ",
151 watchpoint_type_to_str (sd
, point
->type
),
152 interrupt_nr_to_str (sd
, point
->interrupt_nr
));
153 if (point
->is_periodic
)
154 sim_io_printf (sd
, "+");
155 if (!point
->is_within
)
156 sim_io_printf (sd
, "!");
157 sim_io_printf (sd
, "0x%lx", point
->arg0
);
158 if (point
->arg1
!= point
->arg0
)
159 sim_io_printf (sd
, ",0x%lx", point
->arg1
);
160 sim_io_printf (sd
, "\n");
166 static sim_event_handler handle_watchpoint
;
169 schedule_watchpoint (SIM_DESC sd
,
170 sim_watch_point
*point
)
175 point
->event
= sim_events_watch_pc (sd
,
177 point
->arg0
, point
->arg1
,
178 /* PC in arg0..arg1 */
182 case clock_watchpoint
:
183 point
->event
= sim_events_watch_clock (sd
,
184 point
->arg0
, /* ms time */
188 case cycles_watchpoint
:
189 point
->event
= sim_events_schedule (sd
,
190 point
->arg0
, /* time */
195 sim_engine_abort (sd
, NULL
, NULL_CIA
,
196 "handle_watchpoint - internal error - bad switch");
204 handle_watchpoint (SIM_DESC sd
, void *data
)
206 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
207 sim_watch_point
*point
= (sim_watch_point
*) data
;
208 int interrupt_nr
= point
->interrupt_nr
;
210 if (point
->is_periodic
)
211 /* reschedule this event before processing it */
212 schedule_watchpoint (sd
, point
);
214 do_watchpoint_delete (sd
, point
->ident
, invalid_watchpoint
);
216 if (point
->interrupt_nr
== watch
->nr_interrupts
)
217 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGINT
);
219 watch
->interrupt_handler (sd
, &watch
->interrupt_names
[interrupt_nr
]);
224 do_watchpoint_create (SIM_DESC sd
,
225 watchpoint_type type
,
229 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
230 sim_watch_point
**point
;
232 /* create the watchpoint */
233 point
= &watch
->points
;
234 while ((*point
) != NULL
)
235 point
= &(*point
)->next
;
236 (*point
) = ZALLOC (sim_watch_point
);
238 /* fill in the details */
239 (*point
)->ident
= ++(watch
->last_point_nr
);
240 (*point
)->type
= option_to_type (sd
, opt
);
241 (*point
)->interrupt_nr
= option_to_interrupt_nr (sd
, opt
);
242 /* prefixes to arg - +== periodic, !==not or outside */
243 (*point
)->is_within
= 1;
247 (*point
)->is_periodic
= 1;
248 else if (arg
[0] == '!')
249 (*point
)->is_within
= 0;
255 (*point
)->arg0
= strtoul (arg
, &arg
, 0);
257 (*point
)->arg1
= strtoul (arg
+ 1, NULL
, 0);
259 (*point
)->arg1
= (*point
)->arg0
;
262 schedule_watchpoint (sd
, (*point
));
269 watchpoint_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
270 char *arg
, int is_command
)
272 if (opt
>= OPTION_WATCH_OP
)
273 return do_watchpoint_create (sd
, clock_watchpoint
, opt
, arg
);
278 case OPTION_WATCH_DELETE
:
279 if (isdigit ((int) arg
[0]))
281 int ident
= strtol (arg
, NULL
, 0);
282 if (do_watchpoint_delete (sd
, ident
, invalid_watchpoint
)
285 sim_io_eprintf (sd
, "Watchpoint %d not found\n", ident
);
290 else if (strcasecmp (arg
, "all") == 0)
292 watchpoint_type type
;
293 for (type
= invalid_watchpoint
+ 1;
294 type
< nr_watchpoint_types
;
297 do_watchpoint_delete (sd
, 0, type
);
301 else if (strcasecmp (arg
, "pc") == 0)
303 if (do_watchpoint_delete (sd
, 0, pc_watchpoint
)
306 sim_io_eprintf (sd
, "No PC watchpoints found\n");
311 else if (strcasecmp (arg
, "clock") == 0)
313 if (do_watchpoint_delete (sd
, 0, clock_watchpoint
) != SIM_RC_OK
)
315 sim_io_eprintf (sd
, "No CLOCK watchpoints found\n");
320 else if (strcasecmp (arg
, "cycles") == 0)
322 if (do_watchpoint_delete (sd
, 0, cycles_watchpoint
) != SIM_RC_OK
)
324 sim_io_eprintf (sd
, "No CYCLES watchpoints found\n");
329 sim_io_eprintf (sd
, "Unknown watchpoint type `%s'\n", arg
);
332 case OPTION_WATCH_INFO
:
334 do_watchpoint_info (sd
);
339 sim_io_eprintf (sd
, "Unknown watch option %d\n", opt
);
348 sim_watchpoint_init (SIM_DESC sd
)
350 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
351 sim_watch_point
*point
;
352 /* NOTE: Do not need to de-schedule any previous watchpoints as
353 sim-events has already done this */
354 /* schedule any watchpoints enabled by command line options */
355 for (point
= watch
->points
; point
!= NULL
; point
= point
->next
)
357 schedule_watchpoint (sd
, point
);
363 static const OPTION watchpoint_options
[] =
365 { {"watch-delete", required_argument
, NULL
, OPTION_WATCH_DELETE
},
366 '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint",
367 watchpoint_option_handler
, NULL
},
369 { {"watch-info", no_argument
, NULL
, OPTION_WATCH_INFO
},
370 '\0', NULL
, "List scheduled watchpoints",
371 watchpoint_option_handler
, NULL
},
373 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
376 static const char *default_interrupt_names
[] = { "int", 0, };
378 /* This default handler is "good enough" for targets that just want to trap into
379 gdb when watchpoints are hit, and have only configured the STATE_WATCHPOINTS
382 default_interrupt_handler (SIM_DESC sd
, void *data
)
384 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
385 address_word cia
= CPU_PC_GET (cpu
);
386 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_stopped
, SIM_SIGTRAP
);
390 sim_watchpoint_install (SIM_DESC sd
)
392 sim_watchpoints
*watch
= STATE_WATCHPOINTS (sd
);
393 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
394 /* the basic command set */
395 sim_module_add_init_fn (sd
, sim_watchpoint_init
);
396 sim_add_option_table (sd
, NULL
, watchpoint_options
);
397 /* fill in some details */
398 if (watch
->interrupt_names
== NULL
)
399 watch
->interrupt_names
= default_interrupt_names
;
400 if (watch
->interrupt_handler
== NULL
)
401 watch
->interrupt_handler
= default_interrupt_handler
;
402 watch
->nr_interrupts
= 0;
403 while (watch
->interrupt_names
[watch
->nr_interrupts
] != NULL
)
404 watch
->nr_interrupts
++;
405 /* generate more advansed commands */
407 OPTION
*int_options
= NZALLOC (OPTION
, 1 + (watch
->nr_interrupts
+ 1) * nr_watchpoint_types
);
409 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
411 watchpoint_type type
;
412 for (type
= 0; type
< nr_watchpoint_types
; type
++)
415 int nr
= interrupt_nr
* nr_watchpoint_types
+ type
;
416 OPTION
*option
= &int_options
[nr
];
417 if (asprintf (&name
, "watch-%s-%s",
418 watchpoint_type_to_str (sd
, type
),
419 interrupt_nr_to_str (sd
, interrupt_nr
)) < 0)
421 option
->opt
.name
= name
;
422 option
->opt
.has_arg
= required_argument
;
423 option
->opt
.val
= type_to_option (sd
, type
, interrupt_nr
);
425 option
->doc_name
= "";
426 option
->handler
= watchpoint_option_handler
;
429 /* adjust first few entries so that they contain real
430 documentation, the first entry includes a list of actions. */
433 "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is";
435 int len
= strlen (prefix
) + 1;
436 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
437 len
+= strlen (interrupt_nr_to_str (sd
, interrupt_nr
)) + 1;
438 doc
= NZALLOC (char, len
);
439 strcpy (doc
, prefix
);
440 for (interrupt_nr
= 0; interrupt_nr
<= watch
->nr_interrupts
; interrupt_nr
++)
443 strcat (doc
, interrupt_nr_to_str (sd
, interrupt_nr
));
445 int_options
[0].doc_name
= "watch-cycles-ACTION";
446 int_options
[0].arg
= "[+]COUNT";
447 int_options
[0].doc
= doc
;
449 int_options
[1].doc_name
= "watch-pc-ACTION";
450 int_options
[1].arg
= "[!]ADDRESS";
452 "Watch the PC, take ACTION when matches ADDRESS (in range ADDRESS,ADDRESS), `!' negates test";
453 int_options
[2].doc_name
= "watch-clock-ACTION";
454 int_options
[2].arg
= "[+]MILLISECONDS";
456 "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)";
458 sim_add_option_table (sd
, NULL
, int_options
);