binutils-gdb/sim/common/hw-events.c

275 lines
6.3 KiB
C
Raw Normal View History

/* Hardware event manager.
Copyright (C) 1998-2021 Free Software Foundation, Inc.
Contributed by Cygnus Support.
This file is part of GDB, the GNU debugger.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* This must come before any other includes. */
#include "defs.h"
#include "hw-main.h"
#include "hw-base.h"
#include "sim-events.h"
sim/common: Fix warnings: "warning: implicit declaration of function..." During building of several cgen simulator's I notices the below warnings. Adding includes fixes these. Including config.h allows stdio.h to properly configure itself to expose asprintf(). The other warnings for abort, free, memset, strlen are trivial. Warnings: ../../../binutils-gdb/sim/or1k/../common/sim-watch.c: In function ‘sim_watchpoint_install’: ../../../binutils-gdb/sim/or1k/../common/sim-watch.c:415:10: warning: implicit declaration of function ‘asprintf’; did you mean ‘vasprintf’? [-Wimplicit-function-declaration] if (asprintf (&name, "watch-%s-%s", ^~~~~~~~ vasprintf ../../../binutils-gdb/sim/lm32/../common/hw-device.c: In function ‘hw_strdup’: ../../../binutils-gdb/sim/lm32/../common/hw-device.c:59:34: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration] char *dup = hw_zalloc (me, strlen (str) + 1); ^~~~~~ ../../../binutils-gdb/sim/lm32/../common/hw-events.c: In function ‘hw_event_queue_schedule’: ../../../binutils-gdb/sim/lm32/../common/hw-events.c:92:3: warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration] memset (&dummy, 0, sizeof dummy); ^~~~~~ ../../../binutils-gdb/sim/lm32/../common/hw-handles.c: In function ‘hw_handle_remove_ihandle’: ../../../binutils-gdb/sim/lm32/../common/hw-handles.c:211:4: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration] free (delete); ^~~~ ../../../binutils-gdb/sim/lm32/../common/sim-fpu.c: In function ‘pack_fpu’: ../../../binutils-gdb/sim/lm32/../common/sim-fpu.c:292:7: warning: implicit declaration of function ‘abort’ [-Wimplicit-function-declaration] abort (); ^~~~~ sim/common/ChangeLog: * sim-options.c: Include "config.h". Include <stdio.h>. * sim-watch.c: Include "config.h". Include <stdio.h>. * hw-device.c: Include <string.h>. * hw-events.c: Include <string.h>. * hw-handles.c: Include <stdlib.h>. * sim-fpu.c: Include <stdlib.h>.
2019-03-28 05:40:30 +08:00
#include <string.h>
/* The hw-events object is implemented using sim-events */
struct hw_event
{
void *data;
struct hw *me;
hw_event_callback *callback;
sim_event *real;
struct hw_event_data *entry;
};
struct hw_event_data
{
struct hw_event event;
struct hw_event_data *next;
};
void
create_hw_event_data (struct hw *me)
{
if (me->events_of_hw != NULL)
hw_abort (me, "stray events");
/* NOP */
}
void
delete_hw_event_data (struct hw *me)
{
/* Remove the scheduled event. */
while (me->events_of_hw)
hw_event_queue_deschedule (me, &me->events_of_hw->event);
}
/* Pass the H/W event onto the real callback */
static void
bounce_hw_event (SIM_DESC sd,
void *data)
{
/* save the data */
struct hw_event_data *entry = (struct hw_event_data *) data;
struct hw *me = entry->event.me;
void *event_data = entry->event.data;
hw_event_callback *callback = entry->event.callback;
struct hw_event_data **prev = &me->events_of_hw;
while ((*prev) != entry)
prev = &(*prev)->next;
(*prev) = entry->next;
hw_free (me, entry);
callback (me, event_data); /* may not return */
}
/* Map onto the event functions */
struct hw_event *
hw_event_queue_schedule (struct hw *me,
signed64 delta_time,
hw_event_callback *callback,
void *data)
{
sim: add ATTRIBUTE_PRINTF / ATTRIBUTE_NULL_PRINTF where necessary I finally got the all-targets sim building with Clang, these are all the instances where an ATTRIBUTE_PRINTF or ATTRIBUTE_NULL_PRINTF attribute needed to be added to avoid errors like: /home/simark/src/binutils-gdb/sim/aarch64/../common/sim-profile.c:464:19: error: format string is not a string literal [-Werror,-Wformat-nonliteral] vfprintf (fp, fmt, ap); ^~~ There are more fixes needed to get everything building, but adding these attributes is trivial enough, so I send them all in a single patch. Adding the format attributes introduces some format string errors when building with GCC (because now format strings are checked), so corresponding changes are needed to avoid breaking the build. Other than simple format string specified changes, there is this one: /home/simark/src/binutils-gdb/sim/aarch64/../common/hw-events.c: In function 'hw_event_queue_schedule': /home/simark/src/binutils-gdb/sim/aarch64/../common/hw-events.c:95:15: error: too many arguments for format [-Werror=format-extra-args] 95 | NULL, dummy); | ^~~~~ We can fix it and avoid using a dummy variable by simply calling hw_event_queue_schedule_tracef instead of hw_event_queue_schedule_vtracef. sim/arm/ChangeLog: * armdefs.h (ARMul_ConsolePrint): Use format attribute. * wrapper.c (op_printf): Likewise. sim/bfin/ChangeLog: * interp.c (sim_open): Adjust format string specifier. sim/common/ChangeLog: * hw-events.h (hw_event_queue_schedule_tracef): Use format attribute. (hw_event_queue_schedule_vtracef): Likewise. * hw-tree.h (hw_tree_vparse): Likewise. * sim-profile.c (profile_vprintf): Likewise. * sim-trace.c (dis_printf): Likewise. * sim-trace.h (trace_printf): Likewise. (trace_vprintf): Likewise. * sim-utils.h (sim_do_commandf): Likewise. * hw-events.c (hw_event_queue_schedule): Use hw_event_queue_schedule_tracef. sim/rx/ChangeLog: * trace.c (op_printf): Likewise. sim/v850/ChangeLog: * interp.c (sim_open): Adjust format string specifier. Change-Id: I1445115ce57db15bb8e35dca93014555e7555794
2021-05-03 22:54:08 +08:00
return hw_event_queue_schedule_tracef (me, delta_time, callback, data, NULL);
}
struct hw_event *
hw_event_queue_schedule_tracef (struct hw *me,
signed64 delta_time,
hw_event_callback *callback,
void *data,
const char *fmt,
...)
{
struct hw_event *event;
va_list ap;
va_start (ap, fmt);
event = hw_event_queue_schedule_vtracef (me, delta_time, callback, data, fmt, ap);
va_end (ap);
return event;
}
struct hw_event *
hw_event_queue_schedule_vtracef (struct hw *me,
signed64 delta_time,
hw_event_callback *callback,
void *data,
const char *fmt,
va_list ap)
{
struct hw_event_data *entry = HW_ZALLOC (me, struct hw_event_data);
entry->next = me->events_of_hw;
me->events_of_hw = entry;
/* fill it in */
entry->event.entry = entry;
entry->event.data = data;
entry->event.callback = callback;
entry->event.me = me;
entry->event.real = sim_events_schedule_vtracef (hw_system (me),
delta_time,
bounce_hw_event,
entry,
fmt, ap);
return &entry->event;
}
void
hw_event_queue_deschedule (struct hw *me,
struct hw_event *event_to_remove)
{
/* ZAP the event but only if it is still in the event queue. Note
that event_to_remove is only de-referenced after its validity has
been confirmed. */
struct hw_event_data **prev;
for (prev = &me->events_of_hw;
(*prev) != NULL;
prev = &(*prev)->next)
{
struct hw_event_data *entry = (*prev);
if (&entry->event == event_to_remove)
{
sim_events_deschedule (hw_system (me),
entry->event.real);
(*prev) = entry->next;
hw_free (me, entry);
return;
}
}
}
signed64
hw_event_queue_time (struct hw *me)
{
return sim_events_time (hw_system (me));
}
/* Returns the time that remains before the event is raised. */
signed64
hw_event_remain_time (struct hw *me, struct hw_event *event)
{
signed64 t;
t = sim_events_remain_time (hw_system (me), event->real);
return t;
}
/* Only worry about this compling on ANSI systems.
Build with `make test-hw-events' in sim/<cpu> directory*/
#if defined (MAIN)
#include "sim-main.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static void
test_handler (struct hw *me,
void *data)
{
int *n = data;
if (*n != hw_event_queue_time (me))
abort ();
*n = -(*n);
}
int
main (int argc,
char **argv)
{
host_callback *cb = ZALLOC (host_callback);
struct sim_state *sd = sim_state_alloc (0, cb);
struct hw *me = ZALLOC (struct hw);
sim_pre_argv_init (sd, "test-hw-events");
sim_post_argv_init (sd);
me->system_of_hw = sd;
printf ("Create hw-event-data\n");
{
create_hw_alloc_data (me);
create_hw_event_data (me);
delete_hw_event_data (me);
delete_hw_alloc_data (me);
}
printf ("Create hw-events\n");
{
struct hw_event *a;
struct hw_event *b;
struct hw_event *c;
struct hw_event *d;
create_hw_alloc_data (me);
create_hw_event_data (me);
a = hw_event_queue_schedule (me, 0, NULL, NULL);
b = hw_event_queue_schedule (me, 1, NULL, NULL);
c = hw_event_queue_schedule (me, 2, NULL, NULL);
d = hw_event_queue_schedule (me, 1, NULL, NULL);
hw_event_queue_deschedule (me, c);
hw_event_queue_deschedule (me, b);
hw_event_queue_deschedule (me, a);
hw_event_queue_deschedule (me, d);
c = HW_ZALLOC (me, struct hw_event);
hw_event_queue_deschedule (me, b); /* OOPS! */
hw_free (me, c);
delete_hw_event_data (me);
delete_hw_alloc_data (me);
}
printf ("Schedule hw-events\n");
{
struct hw_event **e;
int *n;
int i;
int nr = 4;
e = HW_NZALLOC (me, struct hw_event *, nr);
n = HW_NZALLOC (me, int, nr);
create_hw_alloc_data (me);
create_hw_event_data (me);
for (i = 0; i < nr; i++)
{
n[i] = i;
e[i] = hw_event_queue_schedule (me, i, test_handler, &n[i]);
}
sim_events_preprocess (sd, 1, 1);
for (i = 0; i < nr; i++)
{
if (sim_events_tick (sd))
sim_events_process (sd);
}
for (i = 0; i < nr; i++)
{
if (n[i] != -i)
abort ();
hw_event_queue_deschedule (me, e[i]);
}
hw_free (me, n);
hw_free (me, e);
delete_hw_event_data (me);
delete_hw_alloc_data (me);
}
return 0;
}
#endif