1 /* Generic simulator watchpoint support. 2 Copyright (C) 1997-2024 Free Software Foundation, Inc. 3 Contributed by Cygnus Support. 4 5 This file is part of GDB, the GNU debugger. 6 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. 11 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. 16 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/>. */ 19 20 /* This must come before any other includes. */ 21 #include "defs.h" 22 23 #include <ctype.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include "libiberty.h" 29 30 #include "sim-main.h" 31 #include "sim-options.h" 32 #include "sim-signal.h" 33 #include "sim-assert.h" 34 35 enum { 36 OPTION_WATCH_DELETE = OPTION_START, 37 38 OPTION_WATCH_INFO, 39 OPTION_WATCH_CLOCK, 40 OPTION_WATCH_CYCLES, 41 OPTION_WATCH_PC, 42 43 OPTION_WATCH_OP, 44 }; 45 46 47 /* Break an option number into its op/int-nr */ 48 static watchpoint_type 49 option_to_type (SIM_DESC sd, 50 int option) 51 { 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); 56 return type; 57 } 58 59 static int 60 option_to_interrupt_nr (SIM_DESC sd, 61 int option) 62 { 63 sim_watchpoints *watch = STATE_WATCHPOINTS (sd); 64 int interrupt_nr = ((option - OPTION_WATCH_OP) 65 % (watch->nr_interrupts + 1)); 66 return interrupt_nr; 67 } 68 69 static int 70 type_to_option (SIM_DESC sd, 71 watchpoint_type type, 72 int interrupt_nr) 73 { 74 sim_watchpoints *watch = STATE_WATCHPOINTS (sd); 75 return ((type * (watch->nr_interrupts + 1)) 76 + interrupt_nr 77 + OPTION_WATCH_OP); 78 } 79 80 81 /* Delete one or more watchpoints. Fail if no watchpoints were found */ 82 83 static SIM_RC 84 do_watchpoint_delete (SIM_DESC sd, 85 int ident, 86 watchpoint_type type) 87 { 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) 92 { 93 if ((*entry)->ident == ident 94 || (*entry)->type == type) 95 { 96 sim_watch_point *dead = (*entry); 97 (*entry) = (*entry)->next; 98 sim_events_deschedule (sd, dead->event); 99 free (dead); 100 status = SIM_RC_OK; 101 } 102 else 103 entry = &(*entry)->next; 104 } 105 return status; 106 } 107 108 static const char * 109 watchpoint_type_to_str (SIM_DESC sd, 110 watchpoint_type type) 111 { 112 switch (type) 113 { 114 case pc_watchpoint: 115 return "pc"; 116 case clock_watchpoint: 117 return "clock"; 118 case cycles_watchpoint: 119 return "cycles"; 120 case invalid_watchpoint: 121 case nr_watchpoint_types: 122 return "(invalid-type)"; 123 } 124 return NULL; 125 } 126 127 static const char * 128 interrupt_nr_to_str (SIM_DESC sd, 129 int interrupt_nr) 130 { 131 sim_watchpoints *watch = STATE_WATCHPOINTS (sd); 132 if (interrupt_nr < 0) 133 return "(invalid-interrupt)"; 134 else if (interrupt_nr >= watch->nr_interrupts) 135 return "breakpoint"; 136 else 137 return watch->interrupt_names[interrupt_nr]; 138 } 139 140 141 static void 142 do_watchpoint_info (SIM_DESC sd) 143 { 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) 148 { 149 sim_io_printf (sd, "%3d: watch %s %s ", 150 point->ident, 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"); 161 } 162 } 163 164 165 166 static sim_event_handler handle_watchpoint; 167 168 static SIM_RC 169 schedule_watchpoint (SIM_DESC sd, 170 sim_watch_point *point) 171 { 172 switch (point->type) 173 { 174 case pc_watchpoint: 175 point->event = sim_events_watch_pc (sd, 176 point->is_within, 177 point->arg0, point->arg1, 178 /* PC in arg0..arg1 */ 179 handle_watchpoint, 180 point); 181 return SIM_RC_OK; 182 case clock_watchpoint: 183 point->event = sim_events_watch_clock (sd, 184 point->arg0, /* ms time */ 185 handle_watchpoint, 186 point); 187 return SIM_RC_OK; 188 case cycles_watchpoint: 189 point->event = sim_events_schedule (sd, 190 point->arg0, /* time */ 191 handle_watchpoint, 192 point); 193 return SIM_RC_OK; 194 default: 195 sim_engine_abort (sd, NULL, NULL_CIA, 196 "handle_watchpoint - internal error - bad switch"); 197 return SIM_RC_FAIL; 198 } 199 return SIM_RC_OK; 200 } 201 202 203 static void 204 handle_watchpoint (SIM_DESC sd, void *data) 205 { 206 sim_watchpoints *watch = STATE_WATCHPOINTS (sd); 207 sim_watch_point *point = (sim_watch_point *) data; 208 int interrupt_nr = point->interrupt_nr; 209 210 if (point->is_periodic) 211 /* reschedule this event before processing it */ 212 schedule_watchpoint (sd, point); 213 else 214 do_watchpoint_delete (sd, point->ident, invalid_watchpoint); 215 216 if (point->interrupt_nr == watch->nr_interrupts) 217 sim_engine_halt (sd, NULL, NULL, NULL_CIA, sim_stopped, SIM_SIGINT); 218 else 219 watch->interrupt_handler (sd, &watch->interrupt_names[interrupt_nr]); 220 } 221 222 223 static SIM_RC 224 do_watchpoint_create (SIM_DESC sd, 225 watchpoint_type type, 226 int opt, 227 char *arg) 228 { 229 sim_watchpoints *watch = STATE_WATCHPOINTS (sd); 230 sim_watch_point **point; 231 232 /* create the watchpoint */ 233 point = &watch->points; 234 while ((*point) != NULL) 235 point = &(*point)->next; 236 (*point) = ZALLOC (sim_watch_point); 237 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; 244 while (1) 245 { 246 if (arg[0] == '+') 247 (*point)->is_periodic = 1; 248 else if (arg[0] == '!') 249 (*point)->is_within = 0; 250 else 251 break; 252 arg++; 253 } 254 255 (*point)->arg0 = strtoul (arg, &arg, 0); 256 if (arg[0] == ',') 257 (*point)->arg1 = strtoul (arg + 1, NULL, 0); 258 else 259 (*point)->arg1 = (*point)->arg0; 260 261 /* schedule it */ 262 schedule_watchpoint (sd, (*point)); 263 264 return SIM_RC_OK; 265 } 266 267 268 static SIM_RC 269 watchpoint_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, 270 char *arg, int is_command) 271 { 272 if (opt >= OPTION_WATCH_OP) 273 return do_watchpoint_create (sd, clock_watchpoint, opt, arg); 274 else 275 switch (opt) 276 { 277 278 case OPTION_WATCH_DELETE: 279 if (isdigit ((int) arg[0])) 280 { 281 int ident = strtol (arg, NULL, 0); 282 if (do_watchpoint_delete (sd, ident, invalid_watchpoint) 283 != SIM_RC_OK) 284 { 285 sim_io_eprintf (sd, "Watchpoint %d not found\n", ident); 286 return SIM_RC_FAIL; 287 } 288 return SIM_RC_OK; 289 } 290 else if (strcasecmp (arg, "all") == 0) 291 { 292 watchpoint_type type; 293 for (type = invalid_watchpoint + 1; 294 type < nr_watchpoint_types; 295 type++) 296 { 297 do_watchpoint_delete (sd, 0, type); 298 } 299 return SIM_RC_OK; 300 } 301 else if (strcasecmp (arg, "pc") == 0) 302 { 303 if (do_watchpoint_delete (sd, 0, pc_watchpoint) 304 != SIM_RC_OK) 305 { 306 sim_io_eprintf (sd, "No PC watchpoints found\n"); 307 return SIM_RC_FAIL; 308 } 309 return SIM_RC_OK; 310 } 311 else if (strcasecmp (arg, "clock") == 0) 312 { 313 if (do_watchpoint_delete (sd, 0, clock_watchpoint) != SIM_RC_OK) 314 { 315 sim_io_eprintf (sd, "No CLOCK watchpoints found\n"); 316 return SIM_RC_FAIL; 317 } 318 return SIM_RC_OK; 319 } 320 else if (strcasecmp (arg, "cycles") == 0) 321 { 322 if (do_watchpoint_delete (sd, 0, cycles_watchpoint) != SIM_RC_OK) 323 { 324 sim_io_eprintf (sd, "No CYCLES watchpoints found\n"); 325 return SIM_RC_FAIL; 326 } 327 return SIM_RC_OK; 328 } 329 sim_io_eprintf (sd, "Unknown watchpoint type `%s'\n", arg); 330 return SIM_RC_FAIL; 331 332 case OPTION_WATCH_INFO: 333 { 334 do_watchpoint_info (sd); 335 return SIM_RC_OK; 336 } 337 338 default: 339 sim_io_eprintf (sd, "Unknown watch option %d\n", opt); 340 return SIM_RC_FAIL; 341 342 } 343 344 } 345 346 347 static SIM_RC 348 sim_watchpoint_init (SIM_DESC sd) 349 { 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) 356 { 357 schedule_watchpoint (sd, point); 358 } 359 return SIM_RC_OK; 360 } 361 362 363 static const OPTION watchpoint_options[] = 364 { 365 { {"watch-delete", required_argument, NULL, OPTION_WATCH_DELETE }, 366 '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint", 367 watchpoint_option_handler, NULL }, 368 369 { {"watch-info", no_argument, NULL, OPTION_WATCH_INFO }, 370 '\0', NULL, "List scheduled watchpoints", 371 watchpoint_option_handler, NULL }, 372 373 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL } 374 }; 375 376 static const char *default_interrupt_names[] = { "int", 0, }; 377 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 380 pc field. */ 381 static void 382 default_interrupt_handler (SIM_DESC sd, void *data) 383 { 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); 387 } 388 389 SIM_RC 390 sim_watchpoint_install (SIM_DESC sd) 391 { 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 */ 406 { 407 OPTION *int_options = NZALLOC (OPTION, 1 + (watch->nr_interrupts + 1) * nr_watchpoint_types); 408 int interrupt_nr; 409 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++) 410 { 411 watchpoint_type type; 412 for (type = 0; type < nr_watchpoint_types; type++) 413 { 414 char *name; 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) 420 return SIM_RC_FAIL; 421 option->opt.name = name; 422 option->opt.has_arg = required_argument; 423 option->opt.val = type_to_option (sd, type, interrupt_nr); 424 option->doc = ""; 425 option->doc_name = ""; 426 option->handler = watchpoint_option_handler; 427 } 428 } 429 /* adjust first few entries so that they contain real 430 documentation, the first entry includes a list of actions. */ 431 { 432 const char *prefix = 433 "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is"; 434 char *doc; 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++) 441 { 442 strcat (doc, " "); 443 strcat (doc, interrupt_nr_to_str (sd, interrupt_nr)); 444 } 445 int_options[0].doc_name = "watch-cycles-ACTION"; 446 int_options[0].arg = "[+]COUNT"; 447 int_options[0].doc = doc; 448 } 449 int_options[1].doc_name = "watch-pc-ACTION"; 450 int_options[1].arg = "[!]ADDRESS"; 451 int_options[1].doc = 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"; 455 int_options[2].doc = 456 "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)"; 457 458 sim_add_option_table (sd, NULL, int_options); 459 } 460 return SIM_RC_OK; 461 } 462