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