]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-watch.c
sim: watch: fix pc watchpoints on little endian host systems
[thirdparty/binutils-gdb.git] / sim / common / sim-watch.c
CommitLineData
c906108c 1/* Generic simulator watchpoint support.
3666a048 2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
c906108c
SS
3 Contributed by Cygnus Support.
4
5This file is part of GDB, the GNU debugger.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4744ac1b
JB
9the Free Software Foundation; either version 3 of the License, or
10(at your option) any later version.
c906108c
SS
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
4744ac1b
JB
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
b6061d4d 20#include "config.h"
c906108c
SS
21#include "sim-main.h"
22#include "sim-options.h"
f074c07d 23#include "libiberty.h"
c906108c
SS
24
25#include "sim-assert.h"
26
27#include <ctype.h>
b6061d4d 28#include <stdio.h>
c906108c 29#include <string.h>
c906108c 30#include <stdlib.h>
c906108c
SS
31
32enum {
33 OPTION_WATCH_DELETE = OPTION_START,
34
35 OPTION_WATCH_INFO,
36 OPTION_WATCH_CLOCK,
37 OPTION_WATCH_CYCLES,
38 OPTION_WATCH_PC,
39
40 OPTION_WATCH_OP,
41};
42
43
44/* Break an option number into its op/int-nr */
45static watchpoint_type
46option_to_type (SIM_DESC sd,
47 int option)
48{
49 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
50 watchpoint_type type = ((option - OPTION_WATCH_OP)
51 / (watch->nr_interrupts + 1));
52 SIM_ASSERT (type >= 0 && type < nr_watchpoint_types);
53 return type;
54}
55
56static int
57option_to_interrupt_nr (SIM_DESC sd,
58 int option)
59{
60 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
61 int interrupt_nr = ((option - OPTION_WATCH_OP)
62 % (watch->nr_interrupts + 1));
63 return interrupt_nr;
64}
65
66static int
67type_to_option (SIM_DESC sd,
68 watchpoint_type type,
69 int interrupt_nr)
70{
71 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
72 return ((type * (watch->nr_interrupts + 1))
73 + interrupt_nr
74 + OPTION_WATCH_OP);
75}
76
77
78/* Delete one or more watchpoints. Fail if no watchpoints were found */
79
80static SIM_RC
81do_watchpoint_delete (SIM_DESC sd,
82 int ident,
83 watchpoint_type type)
84{
85 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
86 sim_watch_point **entry = &watch->points;
87 SIM_RC status = SIM_RC_FAIL;
88 while ((*entry) != NULL)
89 {
90 if ((*entry)->ident == ident
91 || (*entry)->type == type)
92 {
93 sim_watch_point *dead = (*entry);
94 (*entry) = (*entry)->next;
95 sim_events_deschedule (sd, dead->event);
d79fe0d6 96 free (dead);
c906108c
SS
97 status = SIM_RC_OK;
98 }
99 else
100 entry = &(*entry)->next;
101 }
102 return status;
103}
104
ff398ee4 105static const char *
c906108c
SS
106watchpoint_type_to_str (SIM_DESC sd,
107 watchpoint_type type)
108{
109 switch (type)
110 {
111 case pc_watchpoint:
112 return "pc";
113 case clock_watchpoint:
114 return "clock";
115 case cycles_watchpoint:
116 return "cycles";
117 case invalid_watchpoint:
118 case nr_watchpoint_types:
119 return "(invalid-type)";
120 }
121 return NULL;
122}
123
ff398ee4 124static const char *
c906108c
SS
125interrupt_nr_to_str (SIM_DESC sd,
126 int interrupt_nr)
127{
128 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
129 if (interrupt_nr < 0)
130 return "(invalid-interrupt)";
131 else if (interrupt_nr >= watch->nr_interrupts)
132 return "breakpoint";
133 else
134 return watch->interrupt_names[interrupt_nr];
135}
136
137
138static void
139do_watchpoint_info (SIM_DESC sd)
140{
141 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
142 sim_watch_point *point;
143 sim_io_printf (sd, "Watchpoints:\n");
144 for (point = watch->points; point != NULL; point = point->next)
145 {
146 sim_io_printf (sd, "%3d: watch %s %s ",
147 point->ident,
148 watchpoint_type_to_str (sd, point->type),
149 interrupt_nr_to_str (sd, point->interrupt_nr));
150 if (point->is_periodic)
151 sim_io_printf (sd, "+");
152 if (!point->is_within)
153 sim_io_printf (sd, "!");
154 sim_io_printf (sd, "0x%lx", point->arg0);
155 if (point->arg1 != point->arg0)
156 sim_io_printf (sd, ",0x%lx", point->arg1);
157 sim_io_printf (sd, "\n");
158 }
159}
028f6515 160
c906108c
SS
161
162
163static sim_event_handler handle_watchpoint;
164
165static SIM_RC
166schedule_watchpoint (SIM_DESC sd,
167 sim_watch_point *point)
168{
169 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
170 switch (point->type)
171 {
172 case pc_watchpoint:
173 point->event = sim_events_watch_sim (sd,
174 watch->pc,
175 watch->sizeof_pc,
62fe7512 176 HOST_BYTE_ORDER,
c906108c
SS
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
204static void
205handle_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);
028f6515 216
c906108c
SS
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
224static SIM_RC
225do_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 }
028f6515 255
c906108c
SS
256 (*point)->arg0 = strtoul (arg, &arg, 0);
257 if (arg[0] == ',')
258 (*point)->arg0 = strtoul (arg, 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
269static SIM_RC
270watchpoint_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 {
028f6515 278
c906108c
SS
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;
028f6515 332
c906108c
SS
333 case OPTION_WATCH_INFO:
334 {
335 do_watchpoint_info (sd);
336 return SIM_RC_OK;
337 }
028f6515 338
c906108c
SS
339 default:
340 sim_io_eprintf (sd, "Unknown watch option %d\n", opt);
341 return SIM_RC_FAIL;
028f6515 342
c906108c 343 }
028f6515 344
c906108c
SS
345}
346
347
348static SIM_RC
349sim_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
364static const OPTION watchpoint_options[] =
365{
366 { {"watch-delete", required_argument, NULL, OPTION_WATCH_DELETE },
367 '\0', "IDENT|all|pc|cycles|clock", "Delete a watchpoint",
21cf617c 368 watchpoint_option_handler, NULL },
c906108c
SS
369
370 { {"watch-info", no_argument, NULL, OPTION_WATCH_INFO },
371 '\0', NULL, "List scheduled watchpoints",
21cf617c 372 watchpoint_option_handler, NULL },
c906108c 373
21cf617c 374 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
c906108c
SS
375};
376
ff398ee4 377static const char *default_interrupt_names[] = { "int", 0, };
c906108c
SS
378
379
380
381SIM_RC
382sim_watchpoint_install (SIM_DESC sd)
383{
384 sim_watchpoints *watch = STATE_WATCHPOINTS (sd);
385 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
386 /* the basic command set */
387 sim_module_add_init_fn (sd, sim_watchpoint_init);
388 sim_add_option_table (sd, NULL, watchpoint_options);
389 /* fill in some details */
390 if (watch->interrupt_names == NULL)
391 watch->interrupt_names = default_interrupt_names;
392 watch->nr_interrupts = 0;
393 while (watch->interrupt_names[watch->nr_interrupts] != NULL)
394 watch->nr_interrupts++;
395 /* generate more advansed commands */
396 {
397 OPTION *int_options = NZALLOC (OPTION, 1 + (watch->nr_interrupts + 1) * nr_watchpoint_types);
398 int interrupt_nr;
399 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
400 {
401 watchpoint_type type;
402 for (type = 0; type < nr_watchpoint_types; type++)
403 {
404 char *name;
405 int nr = interrupt_nr * nr_watchpoint_types + type;
406 OPTION *option = &int_options[nr];
39a3ae0a
MF
407 if (asprintf (&name, "watch-%s-%s",
408 watchpoint_type_to_str (sd, type),
409 interrupt_nr_to_str (sd, interrupt_nr)) < 0)
410 return SIM_RC_FAIL;
c906108c
SS
411 option->opt.name = name;
412 option->opt.has_arg = required_argument;
413 option->opt.val = type_to_option (sd, type, interrupt_nr);
414 option->doc = "";
415 option->doc_name = "";
416 option->handler = watchpoint_option_handler;
417 }
418 }
419 /* adjust first few entries so that they contain real
420 documentation, the first entry includes a list of actions. */
421 {
ff398ee4 422 const char *prefix =
c906108c
SS
423 "Watch the simulator, take ACTION in COUNT cycles (`+' for every COUNT cycles), ACTION is";
424 char *doc;
425 int len = strlen (prefix) + 1;
426 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
427 len += strlen (interrupt_nr_to_str (sd, interrupt_nr)) + 1;
428 doc = NZALLOC (char, len);
429 strcpy (doc, prefix);
430 for (interrupt_nr = 0; interrupt_nr <= watch->nr_interrupts; interrupt_nr++)
431 {
432 strcat (doc, " ");
433 strcat (doc, interrupt_nr_to_str (sd, interrupt_nr));
434 }
435 int_options[0].doc_name = "watch-cycles-ACTION";
436 int_options[0].arg = "[+]COUNT";
437 int_options[0].doc = doc;
438 }
439 int_options[1].doc_name = "watch-pc-ACTION";
440 int_options[1].arg = "[!]ADDRESS";
441 int_options[1].doc =
442 "Watch the PC, take ACTION when matches ADDRESS (in range ADDRESS,ADDRESS), `!' negates test";
443 int_options[2].doc_name = "watch-clock-ACTION";
444 int_options[2].arg = "[+]MILLISECONDS";
445 int_options[2].doc =
446 "Watch the clock, take ACTION after MILLISECONDS (`+' for every MILLISECONDS)";
447
448 sim_add_option_table (sd, NULL, int_options);
449 }
450 return SIM_RC_OK;
451}