]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Generic simulator watchpoint support. |
3666a048 | 2 | Copyright (C) 1997-2021 Free Software Foundation, Inc. |
c906108c SS |
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 | |
4744ac1b JB |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
c906108c SS |
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 | ||
4744ac1b JB |
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/>. */ | |
c906108c | 19 | |
6df01ab8 MF |
20 | /* This must come before any other includes. */ |
21 | #include "defs.h" | |
22 | ||
c906108c SS |
23 | #include "sim-main.h" |
24 | #include "sim-options.h" | |
1fef66b0 | 25 | #include "sim-signal.h" |
f074c07d | 26 | #include "libiberty.h" |
c906108c SS |
27 | |
28 | #include "sim-assert.h" | |
29 | ||
30 | #include <ctype.h> | |
b6061d4d | 31 | #include <stdio.h> |
c906108c | 32 | #include <string.h> |
c906108c | 33 | #include <stdlib.h> |
c906108c SS |
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); | |
d79fe0d6 | 99 | free (dead); |
c906108c SS |
100 | status = SIM_RC_OK; |
101 | } | |
102 | else | |
103 | entry = &(*entry)->next; | |
104 | } | |
105 | return status; | |
106 | } | |
107 | ||
ff398ee4 | 108 | static const char * |
c906108c SS |
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 | ||
ff398ee4 | 127 | static const char * |
c906108c SS |
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 | } | |
028f6515 | 163 | |
c906108c SS |
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 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
4c0d76b9 | 173 | |
c906108c SS |
174 | switch (point->type) |
175 | { | |
176 | case pc_watchpoint: | |
4c0d76b9 MF |
177 | point->event = sim_events_watch_pc (sd, |
178 | point->is_within, | |
179 | point->arg0, point->arg1, | |
180 | /* PC in arg0..arg1 */ | |
181 | handle_watchpoint, | |
182 | point); | |
c906108c SS |
183 | return SIM_RC_OK; |
184 | case clock_watchpoint: | |
185 | point->event = sim_events_watch_clock (sd, | |
186 | point->arg0, /* ms time */ | |
187 | handle_watchpoint, | |
188 | point); | |
189 | return SIM_RC_OK; | |
190 | case cycles_watchpoint: | |
191 | point->event = sim_events_schedule (sd, | |
192 | point->arg0, /* time */ | |
193 | handle_watchpoint, | |
194 | point); | |
195 | return SIM_RC_OK; | |
196 | default: | |
197 | sim_engine_abort (sd, NULL, NULL_CIA, | |
198 | "handle_watchpoint - internal error - bad switch"); | |
199 | return SIM_RC_FAIL; | |
200 | } | |
201 | return SIM_RC_OK; | |
202 | } | |
203 | ||
204 | ||
205 | static void | |
206 | handle_watchpoint (SIM_DESC sd, void *data) | |
207 | { | |
208 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
209 | sim_watch_point *point = (sim_watch_point *) data; | |
210 | int interrupt_nr = point->interrupt_nr; | |
211 | ||
212 | if (point->is_periodic) | |
213 | /* reschedule this event before processing it */ | |
214 | schedule_watchpoint (sd, point); | |
215 | else | |
216 | do_watchpoint_delete (sd, point->ident, invalid_watchpoint); | |
028f6515 | 217 | |
c906108c SS |
218 | if (point->interrupt_nr == watch->nr_interrupts) |
219 | sim_engine_halt (sd, NULL, NULL, NULL_CIA, sim_stopped, SIM_SIGINT); | |
220 | else | |
221 | watch->interrupt_handler (sd, &watch->interrupt_names[interrupt_nr]); | |
222 | } | |
223 | ||
224 | ||
225 | static SIM_RC | |
226 | do_watchpoint_create (SIM_DESC sd, | |
227 | watchpoint_type type, | |
228 | int opt, | |
229 | char *arg) | |
230 | { | |
231 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
232 | sim_watch_point **point; | |
233 | ||
234 | /* create the watchpoint */ | |
235 | point = &watch->points; | |
236 | while ((*point) != NULL) | |
237 | point = &(*point)->next; | |
238 | (*point) = ZALLOC (sim_watch_point); | |
239 | ||
240 | /* fill in the details */ | |
241 | (*point)->ident = ++(watch->last_point_nr); | |
242 | (*point)->type = option_to_type (sd, opt); | |
243 | (*point)->interrupt_nr = option_to_interrupt_nr (sd, opt); | |
244 | /* prefixes to arg - +== periodic, !==not or outside */ | |
245 | (*point)->is_within = 1; | |
246 | while (1) | |
247 | { | |
248 | if (arg[0] == '+') | |
249 | (*point)->is_periodic = 1; | |
250 | else if (arg[0] == '!') | |
251 | (*point)->is_within = 0; | |
252 | else | |
253 | break; | |
254 | arg++; | |
255 | } | |
028f6515 | 256 | |
c906108c SS |
257 | (*point)->arg0 = strtoul (arg, &arg, 0); |
258 | if (arg[0] == ',') | |
c54f3efd | 259 | (*point)->arg1 = strtoul (arg + 1, NULL, 0); |
c906108c SS |
260 | else |
261 | (*point)->arg1 = (*point)->arg0; | |
262 | ||
263 | /* schedule it */ | |
264 | schedule_watchpoint (sd, (*point)); | |
265 | ||
266 | return SIM_RC_OK; | |
267 | } | |
268 | ||
269 | ||
270 | static SIM_RC | |
271 | watchpoint_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, | |
272 | char *arg, int is_command) | |
273 | { | |
274 | if (opt >= OPTION_WATCH_OP) | |
275 | return do_watchpoint_create (sd, clock_watchpoint, opt, arg); | |
276 | else | |
277 | switch (opt) | |
278 | { | |
028f6515 | 279 | |
c906108c SS |
280 | case OPTION_WATCH_DELETE: |
281 | if (isdigit ((int) arg[0])) | |
282 | { | |
283 | int ident = strtol (arg, NULL, 0); | |
284 | if (do_watchpoint_delete (sd, ident, invalid_watchpoint) | |
285 | != SIM_RC_OK) | |
286 | { | |
287 | sim_io_eprintf (sd, "Watchpoint %d not found\n", ident); | |
288 | return SIM_RC_FAIL; | |
289 | } | |
290 | return SIM_RC_OK; | |
291 | } | |
292 | else if (strcasecmp (arg, "all") == 0) | |
293 | { | |
294 | watchpoint_type type; | |
295 | for (type = invalid_watchpoint + 1; | |
296 | type < nr_watchpoint_types; | |
297 | type++) | |
298 | { | |
299 | do_watchpoint_delete (sd, 0, type); | |
300 | } | |
301 | return SIM_RC_OK; | |
302 | } | |
303 | else if (strcasecmp (arg, "pc") == 0) | |
304 | { | |
305 | if (do_watchpoint_delete (sd, 0, pc_watchpoint) | |
306 | != SIM_RC_OK) | |
307 | { | |
308 | sim_io_eprintf (sd, "No PC watchpoints found\n"); | |
309 | return SIM_RC_FAIL; | |
310 | } | |
311 | return SIM_RC_OK; | |
312 | } | |
313 | else if (strcasecmp (arg, "clock") == 0) | |
314 | { | |
315 | if (do_watchpoint_delete (sd, 0, clock_watchpoint) != SIM_RC_OK) | |
316 | { | |
317 | sim_io_eprintf (sd, "No CLOCK watchpoints found\n"); | |
318 | return SIM_RC_FAIL; | |
319 | } | |
320 | return SIM_RC_OK; | |
321 | } | |
322 | else if (strcasecmp (arg, "cycles") == 0) | |
323 | { | |
324 | if (do_watchpoint_delete (sd, 0, cycles_watchpoint) != SIM_RC_OK) | |
325 | { | |
326 | sim_io_eprintf (sd, "No CYCLES watchpoints found\n"); | |
327 | return SIM_RC_FAIL; | |
328 | } | |
329 | return SIM_RC_OK; | |
330 | } | |
331 | sim_io_eprintf (sd, "Unknown watchpoint type `%s'\n", arg); | |
332 | return SIM_RC_FAIL; | |
028f6515 | 333 | |
c906108c SS |
334 | case OPTION_WATCH_INFO: |
335 | { | |
336 | do_watchpoint_info (sd); | |
337 | return SIM_RC_OK; | |
338 | } | |
028f6515 | 339 | |
c906108c SS |
340 | default: |
341 | sim_io_eprintf (sd, "Unknown watch option %d\n", opt); | |
342 | return SIM_RC_FAIL; | |
028f6515 | 343 | |
c906108c | 344 | } |
028f6515 | 345 | |
c906108c SS |
346 | } |
347 | ||
348 | ||
349 | static SIM_RC | |
350 | sim_watchpoint_init (SIM_DESC sd) | |
351 | { | |
352 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
353 | sim_watch_point *point; | |
354 | /* NOTE: Do not need to de-schedule any previous watchpoints as | |
355 | sim-events has already done this */ | |
356 | /* schedule any watchpoints enabled by command line options */ | |
357 | for (point = watch->points; point != NULL; point = point->next) | |
358 | { | |
359 | schedule_watchpoint (sd, point); | |
360 | } | |
361 | return SIM_RC_OK; | |
362 | } | |
363 | ||
364 | ||
365 | static const OPTION watchpoint_options[] = | |
366 | { | |
367 | { {"watch-delete", required_argument, NULL, OPTION_WATCH_DELETE }, | |
368 | '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint", | |
21cf617c | 369 | watchpoint_option_handler, NULL }, |
c906108c SS |
370 | |
371 | { {"watch-info", no_argument, NULL, OPTION_WATCH_INFO }, | |
372 | '\0', NULL, "List scheduled watchpoints", | |
21cf617c | 373 | watchpoint_option_handler, NULL }, |
c906108c | 374 | |
21cf617c | 375 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL } |
c906108c SS |
376 | }; |
377 | ||
ff398ee4 | 378 | static const char *default_interrupt_names[] = { "int", 0, }; |
c906108c | 379 | |
d9b1deff | 380 | /* This default handler is "good enough" for targets that just want to trap into |
d4e3adda MF |
381 | gdb when watchpoints are hit, and have only configured the STATE_WATCHPOINTS |
382 | pc field. */ | |
d9b1deff MF |
383 | static void |
384 | default_interrupt_handler (SIM_DESC sd, void *data) | |
385 | { | |
386 | sim_cpu *cpu = STATE_CPU (sd, 0); | |
387 | address_word cia = CPU_PC_GET (cpu); | |
388 | sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGTRAP); | |
389 | } | |
c906108c SS |
390 | |
391 | SIM_RC | |
392 | sim_watchpoint_install (SIM_DESC sd) | |
393 | { | |
394 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
395 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
396 | /* the basic command set */ | |
397 | sim_module_add_init_fn (sd, sim_watchpoint_init); | |
398 | sim_add_option_table (sd, NULL, watchpoint_options); | |
399 | /* fill in some details */ | |
400 | if (watch->interrupt_names == NULL) | |
401 | watch->interrupt_names = default_interrupt_names; | |
d9b1deff MF |
402 | if (watch->interrupt_handler == NULL) |
403 | watch->interrupt_handler = default_interrupt_handler; | |
c906108c SS |
404 | watch->nr_interrupts = 0; |
405 | while (watch->interrupt_names[watch->nr_interrupts] != NULL) | |
406 | watch->nr_interrupts++; | |
407 | /* generate more advansed commands */ | |
408 | { | |
409 | OPTION *int_options = NZALLOC (OPTION, 1 + (watch->nr_interrupts + 1) * nr_watchpoint_types); | |
410 | int interrupt_nr; | |
411 | for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++) | |
412 | { | |
413 | watchpoint_type type; | |
414 | for (type = 0; type < nr_watchpoint_types; type++) | |
415 | { | |
416 | char *name; | |
417 | int nr = interrupt_nr * nr_watchpoint_types + type; | |
418 | OPTION *option = &int_options[nr]; | |
39a3ae0a MF |
419 | if (asprintf (&name, "watch-%s-%s", |
420 | watchpoint_type_to_str (sd, type), | |
421 | interrupt_nr_to_str (sd, interrupt_nr)) < 0) | |
422 | return SIM_RC_FAIL; | |
c906108c SS |
423 | option->opt.name = name; |
424 | option->opt.has_arg = required_argument; | |
425 | option->opt.val = type_to_option (sd, type, interrupt_nr); | |
426 | option->doc = ""; | |
427 | option->doc_name = ""; | |
428 | option->handler = watchpoint_option_handler; | |
429 | } | |
430 | } | |
431 | /* adjust first few entries so that they contain real | |
432 | documentation, the first entry includes a list of actions. */ | |
433 | { | |
ff398ee4 | 434 | const char *prefix = |
c906108c SS |
435 | "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is"; |
436 | char *doc; | |
437 | int len = strlen (prefix) + 1; | |
438 | for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++) | |
439 | len += strlen (interrupt_nr_to_str (sd, interrupt_nr)) + 1; | |
440 | doc = NZALLOC (char, len); | |
441 | strcpy (doc, prefix); | |
442 | for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++) | |
443 | { | |
444 | strcat (doc, " "); | |
445 | strcat (doc, interrupt_nr_to_str (sd, interrupt_nr)); | |
446 | } | |
447 | int_options[0].doc_name = "watch-cycles-ACTION"; | |
448 | int_options[0].arg = "[+]COUNT"; | |
449 | int_options[0].doc = doc; | |
450 | } | |
451 | int_options[1].doc_name = "watch-pc-ACTION"; | |
452 | int_options[1].arg = "[!]ADDRESS"; | |
453 | int_options[1].doc = | |
454 | "Watch the PC, take ACTION when matches ADDRESS (in range ADDRESS,ADDRESS), `!' negates test"; | |
455 | int_options[2].doc_name = "watch-clock-ACTION"; | |
456 | int_options[2].arg = "[+]MILLISECONDS"; | |
457 | int_options[2].doc = | |
458 | "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)"; | |
459 | ||
460 | sim_add_option_table (sd, NULL, int_options); | |
461 | } | |
462 | return SIM_RC_OK; | |
463 | } |