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