Commit | Line | Data |
---|---|---|
1bba340a | 1 | /* Generic simulator watchpoint support. |
50a2a691 AC |
2 | Copyright (C) 1997 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 2, or (at your option) | |
10 | 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 along | |
18 | with this program; if not, write to the Free Software Foundation, Inc., | |
19 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "sim-main.h" | |
22 | #include "sim-options.h" | |
23 | ||
24 | #include "sim-assert.h" | |
25 | ||
1bba340a AC |
26 | #include <ctype.h> |
27 | ||
50a2a691 AC |
28 | #ifdef HAVE_STRING_H |
29 | #include <string.h> | |
30 | #else | |
31 | #ifdef HAVE_STRINGS_H | |
32 | #include <strings.h> | |
33 | #endif | |
34 | #endif | |
35 | ||
36 | #ifdef HAVE_STDLIB_H | |
37 | #include <stdlib.h> | |
38 | #endif | |
39 | ||
40 | #include <signal.h> | |
41 | ||
50a2a691 AC |
42 | |
43 | enum { | |
44 | OPTION_WATCH_DELETE = OPTION_START, | |
45 | ||
1bba340a | 46 | OPTION_WATCH_INFO, |
50a2a691 AC |
47 | OPTION_WATCH_CLOCK, |
48 | OPTION_WATCH_CYCLES, | |
1bba340a | 49 | OPTION_WATCH_PC, |
50a2a691 | 50 | |
1bba340a | 51 | OPTION_WATCH_OP, |
50a2a691 AC |
52 | }; |
53 | ||
54 | ||
1bba340a AC |
55 | /* Break an option number into its op/int-nr */ |
56 | static watchpoint_type | |
57 | option_to_type (SIM_DESC sd, | |
58 | int option) | |
50a2a691 | 59 | { |
1bba340a AC |
60 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); |
61 | watchpoint_type type = ((option - OPTION_WATCH_OP) | |
62 | / (watch->nr_interrupts + 1)); | |
63 | SIM_ASSERT (type >= 0 && type < nr_watchpoint_types); | |
64 | return type; | |
50a2a691 AC |
65 | } |
66 | ||
1bba340a AC |
67 | static int |
68 | option_to_interrupt_nr (SIM_DESC sd, | |
69 | int option) | |
70 | { | |
71 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
72 | int interrupt_nr = ((option - OPTION_WATCH_OP) | |
73 | % (watch->nr_interrupts + 1)); | |
74 | return interrupt_nr; | |
75 | } | |
76 | ||
77 | static int | |
78 | type_to_option (SIM_DESC sd, | |
79 | watchpoint_type type, | |
80 | int interrupt_nr) | |
81 | { | |
82 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
83 | return ((type * (watch->nr_interrupts + 1)) | |
84 | + interrupt_nr | |
85 | + OPTION_WATCH_OP); | |
86 | } | |
50a2a691 | 87 | |
1bba340a AC |
88 | |
89 | /* Delete one or more watchpoints. Fail if no watchpoints were found */ | |
50a2a691 AC |
90 | |
91 | static SIM_RC | |
1bba340a AC |
92 | do_watchpoint_delete (SIM_DESC sd, |
93 | int ident, | |
94 | watchpoint_type type) | |
50a2a691 AC |
95 | { |
96 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
1bba340a AC |
97 | sim_watch_point **entry = &watch->points; |
98 | SIM_RC status = SIM_RC_FAIL; | |
99 | while ((*entry) != NULL) | |
100 | { | |
101 | if ((*entry)->ident == ident | |
102 | || (*entry)->type == type) | |
103 | { | |
104 | sim_watch_point *dead = (*entry); | |
105 | (*entry) = (*entry)->next; | |
106 | sim_events_deschedule (sd, dead->event); | |
107 | zfree (dead); | |
108 | status = SIM_RC_OK; | |
109 | } | |
110 | else | |
111 | entry = &(*entry)->next; | |
112 | } | |
113 | return status; | |
50a2a691 AC |
114 | } |
115 | ||
1bba340a AC |
116 | static char * |
117 | watchpoint_type_to_str (SIM_DESC sd, | |
118 | watchpoint_type type) | |
119 | { | |
120 | switch (type) | |
121 | { | |
122 | case pc_watchpoint: | |
123 | return "pc"; | |
124 | case clock_watchpoint: | |
125 | return "clock"; | |
126 | case cycles_watchpoint: | |
127 | return "cycles"; | |
128 | case invalid_watchpoint: | |
129 | case nr_watchpoint_types: | |
130 | return "(invalid-type)"; | |
131 | } | |
132 | return NULL; | |
133 | } | |
50a2a691 | 134 | |
1bba340a AC |
135 | static char * |
136 | interrupt_nr_to_str (SIM_DESC sd, | |
137 | int interrupt_nr) | |
50a2a691 AC |
138 | { |
139 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
1bba340a AC |
140 | if (interrupt_nr < 0) |
141 | return "(invalid-interrupt)"; | |
142 | else if (interrupt_nr >= watch->nr_interrupts) | |
143 | return "breakpoint"; | |
144 | else | |
145 | return watch->interrupt_names[interrupt_nr]; | |
146 | } | |
50a2a691 | 147 | |
50a2a691 | 148 | |
1bba340a AC |
149 | static void |
150 | do_watchpoint_info (SIM_DESC sd) | |
151 | { | |
152 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
153 | sim_watch_point *point; | |
154 | sim_io_printf (sd, "Watchpoints:\n"); | |
155 | for (point = watch->points; point != NULL; point = point->next) | |
156 | { | |
157 | sim_io_printf (sd, "%3d: watch %s %s ", | |
158 | point->ident, | |
159 | watchpoint_type_to_str (sd, point->type), | |
160 | interrupt_nr_to_str (sd, point->interrupt_nr)); | |
161 | if (point->is_periodic) | |
162 | sim_io_printf (sd, "+"); | |
163 | if (!point->is_within) | |
164 | sim_io_printf (sd, "!"); | |
165 | sim_io_printf (sd, "0x%lx", point->arg0); | |
166 | if (point->arg1 != point->arg0) | |
167 | sim_io_printf (sd, ",0x%lx", point->arg1); | |
168 | sim_io_printf (sd, "\n"); | |
169 | } | |
170 | } | |
171 | ||
50a2a691 | 172 | |
50a2a691 | 173 | |
1bba340a | 174 | static sim_event_handler handle_watchpoint; |
50a2a691 | 175 | |
1bba340a AC |
176 | static SIM_RC |
177 | schedule_watchpoint (SIM_DESC sd, | |
178 | sim_watch_point *point) | |
179 | { | |
180 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
181 | switch (point->type) | |
182 | { | |
183 | case pc_watchpoint: | |
184 | point->event = sim_events_watch_sim (sd, | |
185 | watch->pc, | |
186 | watch->sizeof_pc, | |
187 | 0/* host-endian */, | |
188 | point->is_within, | |
189 | point->arg0, point->arg1, | |
190 | /* PC in arg0..arg1 */ | |
191 | handle_watchpoint, | |
192 | point); | |
193 | return SIM_RC_OK; | |
194 | case clock_watchpoint: | |
195 | point->event = sim_events_watch_clock (sd, | |
196 | point->arg0, /* ms time */ | |
197 | handle_watchpoint, | |
198 | point); | |
199 | return SIM_RC_OK; | |
200 | case cycles_watchpoint: | |
201 | point->event = sim_events_schedule (sd, | |
202 | point->arg0, /* time */ | |
203 | handle_watchpoint, | |
204 | point); | |
205 | return SIM_RC_OK; | |
50a2a691 | 206 | default: |
a34abff8 AC |
207 | sim_engine_abort (sd, NULL, NULL_CIA, |
208 | "handle_watchpoint - internal error - bad switch"); | |
1bba340a | 209 | return SIM_RC_FAIL; |
50a2a691 | 210 | } |
1bba340a | 211 | return SIM_RC_OK; |
50a2a691 AC |
212 | } |
213 | ||
214 | ||
1bba340a AC |
215 | static void |
216 | handle_watchpoint (SIM_DESC sd, void *data) | |
50a2a691 AC |
217 | { |
218 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); | |
1bba340a AC |
219 | sim_watch_point *point = (sim_watch_point *) data; |
220 | int interrupt_nr = point->interrupt_nr; | |
221 | ||
222 | if (point->is_periodic) | |
223 | /* reschedule this event before processing it */ | |
224 | schedule_watchpoint (sd, point); | |
50a2a691 | 225 | else |
1bba340a AC |
226 | do_watchpoint_delete (sd, point->ident, invalid_watchpoint); |
227 | ||
228 | if (point->interrupt_nr == watch->nr_interrupts) | |
229 | sim_engine_halt (sd, NULL, NULL, NULL_CIA, sim_stopped, SIGINT); | |
230 | else | |
cad7297e | 231 | watch->interrupt_handler (sd, &watch->interrupt_names[interrupt_nr]); |
50a2a691 AC |
232 | } |
233 | ||
234 | ||
1bba340a AC |
235 | static SIM_RC |
236 | do_watchpoint_create (SIM_DESC sd, | |
237 | watchpoint_type type, | |
238 | int opt, | |
239 | char *arg) | |
50a2a691 | 240 | { |
1bba340a AC |
241 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); |
242 | sim_watch_point **point; | |
243 | ||
244 | /* create the watchpoint */ | |
245 | point = &watch->points; | |
246 | while ((*point) != NULL) | |
247 | point = &(*point)->next; | |
248 | (*point) = ZALLOC (sim_watch_point); | |
249 | ||
250 | /* fill in the details */ | |
251 | (*point)->ident = ++(watch->last_point_nr); | |
252 | (*point)->type = option_to_type (sd, opt); | |
253 | (*point)->interrupt_nr = option_to_interrupt_nr (sd, opt); | |
254 | /* prefixes to arg - +== periodic, !==not or outside */ | |
255 | (*point)->is_within = 1; | |
256 | while (1) | |
257 | { | |
258 | if (arg[0] == '+') | |
259 | (*point)->is_periodic = 1; | |
260 | else if (arg[0] == '!') | |
261 | (*point)->is_within = 0; | |
262 | else | |
263 | break; | |
264 | arg++; | |
265 | } | |
266 | ||
267 | (*point)->arg0 = strtoul (arg, &arg, 0); | |
268 | if (arg[0] == ',') | |
269 | (*point)->arg0 = strtoul (arg, NULL, 0); | |
270 | else | |
271 | (*point)->arg1 = (*point)->arg0; | |
50a2a691 | 272 | |
1bba340a AC |
273 | /* schedule it */ |
274 | schedule_watchpoint (sd, (*point)); | |
275 | ||
276 | return SIM_RC_OK; | |
277 | } | |
50a2a691 AC |
278 | |
279 | ||
280 | static SIM_RC | |
1bba340a | 281 | watchpoint_option_handler (sd, opt, arg, is_command) |
50a2a691 AC |
282 | SIM_DESC sd; |
283 | int opt; | |
284 | char *arg; | |
285 | int is_command; | |
286 | { | |
1bba340a AC |
287 | if (opt >= OPTION_WATCH_OP) |
288 | return do_watchpoint_create (sd, clock_watchpoint, opt, arg); | |
289 | else | |
290 | switch (opt) | |
291 | { | |
292 | ||
293 | case OPTION_WATCH_DELETE: | |
294 | if (isdigit ((int) arg[0])) | |
295 | { | |
296 | int ident = strtol (arg, NULL, 0); | |
297 | if (do_watchpoint_delete (sd, ident, invalid_watchpoint) | |
298 | != SIM_RC_OK) | |
299 | { | |
300 | sim_io_eprintf (sd, "Watchpoint %d not found\n", ident); | |
301 | return SIM_RC_FAIL; | |
302 | } | |
303 | return SIM_RC_OK; | |
304 | } | |
305 | else if (strcasecmp (arg, "all") == 0) | |
306 | { | |
307 | watchpoint_type type; | |
308 | for (type = invalid_watchpoint + 1; | |
309 | type < nr_watchpoint_types; | |
310 | type++) | |
311 | { | |
312 | do_watchpoint_delete (sd, 0, type); | |
313 | } | |
314 | return SIM_RC_OK; | |
315 | } | |
316 | else if (strcasecmp (arg, "pc") == 0) | |
317 | { | |
318 | if (do_watchpoint_delete (sd, 0, pc_watchpoint) | |
319 | != SIM_RC_OK) | |
320 | { | |
321 | sim_io_eprintf (sd, "No PC watchpoints found\n"); | |
322 | return SIM_RC_FAIL; | |
323 | } | |
324 | return SIM_RC_OK; | |
325 | } | |
326 | else if (strcasecmp (arg, "clock") == 0) | |
327 | { | |
328 | if (do_watchpoint_delete (sd, 0, clock_watchpoint) != SIM_RC_OK) | |
329 | { | |
330 | sim_io_eprintf (sd, "No CLOCK watchpoints found\n"); | |
331 | return SIM_RC_FAIL; | |
332 | } | |
333 | return SIM_RC_OK; | |
334 | } | |
335 | else if (strcasecmp (arg, "cycles") == 0) | |
336 | { | |
337 | if (do_watchpoint_delete (sd, 0, cycles_watchpoint) != SIM_RC_OK) | |
338 | { | |
339 | sim_io_eprintf (sd, "No CYCLES watchpoints found\n"); | |
340 | return SIM_RC_FAIL; | |
341 | } | |
342 | return SIM_RC_OK; | |
343 | } | |
344 | sim_io_eprintf (sd, "Unknown watchpoint type `%s'\n", arg); | |
345 | return SIM_RC_FAIL; | |
346 | ||
347 | case OPTION_WATCH_INFO: | |
50a2a691 | 348 | { |
1bba340a | 349 | do_watchpoint_info (sd); |
50a2a691 AC |
350 | return SIM_RC_OK; |
351 | } | |
1bba340a AC |
352 | |
353 | default: | |
354 | sim_io_eprintf (sd, "Unknown watch option %d\n", opt); | |
355 | return SIM_RC_FAIL; | |
356 | ||
357 | } | |
358 | ||
50a2a691 AC |
359 | } |
360 | ||
1bba340a | 361 | |
50a2a691 AC |
362 | static SIM_RC |
363 | sim_watchpoint_init (SIM_DESC sd) | |
364 | { | |
50a2a691 | 365 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); |
1bba340a AC |
366 | sim_watch_point *point; |
367 | /* NOTE: Do not need to de-schedule any previous watchpoints as | |
368 | sim-events has already done this */ | |
369 | /* schedule any watchpoints enabled by command line options */ | |
370 | for (point = watch->points; point != NULL; point = point->next) | |
50a2a691 | 371 | { |
1bba340a | 372 | schedule_watchpoint (sd, point); |
50a2a691 AC |
373 | } |
374 | return SIM_RC_OK; | |
375 | } | |
376 | ||
377 | ||
1bba340a AC |
378 | static const OPTION watchpoint_options[] = |
379 | { | |
380 | { {"watch-delete", required_argument, NULL, OPTION_WATCH_DELETE }, | |
381 | '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint", | |
382 | watchpoint_option_handler }, | |
383 | ||
384 | { {"watch-info", no_argument, NULL, OPTION_WATCH_INFO }, | |
385 | '\0', NULL, "List scheduled watchpoints", | |
386 | watchpoint_option_handler }, | |
387 | ||
388 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL } | |
389 | }; | |
390 | ||
1bba340a AC |
391 | static char *default_interrupt_names[] = { "int", 0, }; |
392 | ||
393 | ||
394 | ||
50a2a691 AC |
395 | SIM_RC |
396 | sim_watchpoint_install (SIM_DESC sd) | |
397 | { | |
1bba340a | 398 | sim_watchpoints *watch = STATE_WATCHPOINTS (sd); |
50a2a691 | 399 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); |
1bba340a | 400 | /* the basic command set */ |
50a2a691 | 401 | sim_module_add_init_fn (sd, sim_watchpoint_init); |
1bba340a AC |
402 | sim_add_option_table (sd, watchpoint_options); |
403 | /* fill in some details */ | |
404 | if (watch->interrupt_names == NULL) | |
405 | watch->interrupt_names = default_interrupt_names; | |
406 | watch->nr_interrupts = 0; | |
407 | while (watch->interrupt_names[watch->nr_interrupts] != NULL) | |
408 | watch->nr_interrupts++; | |
409 | /* generate more advansed commands */ | |
410 | { | |
411 | OPTION *int_options = NZALLOC (OPTION, 1 + (watch->nr_interrupts + 1) * nr_watchpoint_types); | |
412 | int interrupt_nr; | |
413 | for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++) | |
414 | { | |
415 | watchpoint_type type; | |
416 | for (type = 0; type < nr_watchpoint_types; type++) | |
417 | { | |
cad7297e | 418 | char *name; |
1bba340a AC |
419 | int nr = interrupt_nr * nr_watchpoint_types + type; |
420 | OPTION *option = &int_options[nr]; | |
1bba340a AC |
421 | asprintf (&name, "watch-%s-%s", |
422 | watchpoint_type_to_str (sd, type), | |
423 | interrupt_nr_to_str (sd, interrupt_nr)); | |
424 | option->opt.name = name; | |
cad7297e | 425 | option->opt.has_arg = required_argument; |
1bba340a | 426 | option->opt.val = type_to_option (sd, type, interrupt_nr); |
cad7297e AC |
427 | option->doc = ""; |
428 | option->doc_name = ""; | |
1bba340a AC |
429 | } |
430 | } | |
cad7297e AC |
431 | /* adjust first few entries so that they contain real |
432 | documentation, the first entry includes a list of actions. */ | |
433 | { | |
434 | char *prefix = | |
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 | ||
1bba340a AC |
460 | sim_add_option_table (sd, int_options); |
461 | } | |
50a2a691 AC |
462 | return SIM_RC_OK; |
463 | } |