1995-10-02 23:47:57 +08:00
|
|
|
/* This file is part of the program psim.
|
|
|
|
|
1996-07-23 23:42:42 +08:00
|
|
|
Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
|
1995-10-02 23:47:57 +08:00
|
|
|
|
|
|
|
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 2 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, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <signal.h> /* FIXME - should be machine dependant version */
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "psim.h"
|
1995-11-12 22:20:39 +08:00
|
|
|
#include "options.h"
|
1995-10-02 23:47:57 +08:00
|
|
|
|
1996-11-20 16:38:13 +08:00
|
|
|
#undef printf_filtered /* blow away the mapping */
|
|
|
|
|
1995-11-03 04:21:35 +08:00
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1997-01-28 05:34:50 +08:00
|
|
|
#include "defs.h"
|
1997-04-17 18:31:52 +08:00
|
|
|
#include "bfd.h"
|
1997-01-28 05:34:50 +08:00
|
|
|
#include "callback.h"
|
|
|
|
#include "remote-sim.h"
|
1995-10-02 23:47:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Structures used by the simulator, for gdb just have static structures */
|
|
|
|
|
|
|
|
static psim *simulator;
|
1996-07-23 23:42:42 +08:00
|
|
|
static device *root_device;
|
|
|
|
static const char *register_names[] = REGISTER_NAMES;
|
1997-08-26 07:14:25 +08:00
|
|
|
static host_callback *callbacks;
|
1995-10-02 23:47:57 +08:00
|
|
|
|
1997-04-17 18:31:52 +08:00
|
|
|
SIM_DESC
|
1997-08-26 07:14:25 +08:00
|
|
|
sim_open (SIM_OPEN_KIND kind,
|
|
|
|
host_callback *callback,
|
|
|
|
struct _bfd *abfd,
|
|
|
|
char **argv)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1997-05-20 09:57:43 +08:00
|
|
|
callbacks = callback;
|
|
|
|
|
1996-07-23 23:42:42 +08:00
|
|
|
/* Note: The simulation is not created by sim_open() because
|
|
|
|
complete information is not yet available */
|
1995-10-02 23:47:57 +08:00
|
|
|
/* trace the call */
|
1997-04-17 18:31:52 +08:00
|
|
|
TRACE(trace_gdb, ("sim_open called\n"));
|
1995-10-02 23:47:57 +08:00
|
|
|
|
1996-07-23 23:42:42 +08:00
|
|
|
if (root_device != NULL)
|
1996-11-20 16:38:13 +08:00
|
|
|
sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
|
1996-07-23 23:42:42 +08:00
|
|
|
root_device = psim_tree();
|
|
|
|
simulator = NULL;
|
|
|
|
|
1997-04-17 18:31:52 +08:00
|
|
|
psim_options(root_device, argv + 1);
|
1995-10-02 23:47:57 +08:00
|
|
|
|
1995-11-12 22:20:39 +08:00
|
|
|
if (ppc_trace[trace_opts])
|
|
|
|
print_options ();
|
1997-04-17 18:31:52 +08:00
|
|
|
|
|
|
|
/* fudge our descriptor for now */
|
|
|
|
return (SIM_DESC) 1;
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_close (SIM_DESC sd, int quitting)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
|
|
|
TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
|
1996-07-23 23:42:42 +08:00
|
|
|
if (ppc_trace[trace_print_info] && simulator != NULL)
|
|
|
|
psim_print_info (simulator, ppc_trace[trace_print_info]);
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-17 18:31:52 +08:00
|
|
|
SIM_RC
|
|
|
|
sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1995-11-02 22:27:18 +08:00
|
|
|
char **argv;
|
1995-10-02 23:47:57 +08:00
|
|
|
TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
|
|
|
|
prog, from_tty));
|
1995-11-02 22:27:18 +08:00
|
|
|
ASSERT(prog != NULL);
|
1995-10-02 23:47:57 +08:00
|
|
|
|
1995-11-02 22:27:18 +08:00
|
|
|
/* parse the arguments, assume that the file is argument 0 */
|
|
|
|
argv = buildargv(prog);
|
|
|
|
ASSERT(argv != NULL && argv[0] != NULL);
|
1995-10-02 23:47:57 +08:00
|
|
|
|
|
|
|
/* create the simulator */
|
|
|
|
TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
|
1996-07-23 23:42:42 +08:00
|
|
|
simulator = psim_create(argv[0], root_device);
|
1995-10-02 23:47:57 +08:00
|
|
|
|
|
|
|
/* bring in all the data section */
|
1995-11-02 22:27:18 +08:00
|
|
|
psim_init(simulator);
|
1995-10-02 23:47:57 +08:00
|
|
|
|
1997-04-17 18:31:52 +08:00
|
|
|
/* get the start address */
|
1997-08-27 12:44:41 +08:00
|
|
|
if (abfd == NULL)
|
1997-04-17 18:31:52 +08:00
|
|
|
{
|
|
|
|
abfd = bfd_openr (argv[0], 0);
|
|
|
|
if (abfd == NULL)
|
|
|
|
error ("psim: can't open \"%s\": %s\n",
|
|
|
|
argv[0], bfd_errmsg (bfd_get_error ()));
|
|
|
|
if (!bfd_check_format (abfd, bfd_object))
|
|
|
|
{
|
|
|
|
const char *errmsg = bfd_errmsg (bfd_get_error ());
|
|
|
|
bfd_close (abfd);
|
|
|
|
error ("psim: \"%s\" is not an object file: %s\n",
|
|
|
|
argv[0], errmsg);
|
|
|
|
}
|
|
|
|
bfd_close (abfd);
|
|
|
|
}
|
|
|
|
|
1997-08-27 08:44:05 +08:00
|
|
|
/* release the arguments */
|
|
|
|
freeargv(argv);
|
|
|
|
|
1997-04-17 18:31:52 +08:00
|
|
|
return SIM_RC_OK;
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1995-11-02 22:27:18 +08:00
|
|
|
int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
|
|
|
|
buf, mem, length);
|
1995-11-12 22:20:39 +08:00
|
|
|
TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
|
|
|
|
(long)mem, (long)buf, length, result));
|
1995-11-02 22:27:18 +08:00
|
|
|
return result;
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1995-11-02 22:27:18 +08:00
|
|
|
int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
|
|
|
|
buf, mem, length,
|
|
|
|
1/*violate_ro*/);
|
1995-11-12 22:20:39 +08:00
|
|
|
TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
|
|
|
|
(long)mem, (long)buf, length, result));
|
1995-11-02 22:27:18 +08:00
|
|
|
return result;
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
|
|
|
if (simulator == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
1995-11-12 22:20:39 +08:00
|
|
|
TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
|
|
|
|
regno, register_names[regno], (long)buf));
|
1995-11-02 22:27:18 +08:00
|
|
|
psim_read_register(simulator, MAX_NR_PROCESSORS,
|
|
|
|
buf, register_names[regno],
|
1995-10-02 23:47:57 +08:00
|
|
|
raw_transfer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_store_register (SIM_DESC sd, int regno, unsigned char *buf)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
|
|
|
if (simulator == NULL)
|
|
|
|
return;
|
1995-11-12 22:20:39 +08:00
|
|
|
TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
|
|
|
|
regno, register_names[regno], (long)buf));
|
1995-11-02 22:27:18 +08:00
|
|
|
psim_write_register(simulator, MAX_NR_PROCESSORS,
|
|
|
|
buf, register_names[regno],
|
1995-10-02 23:47:57 +08:00
|
|
|
raw_transfer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_info (SIM_DESC sd, int verbose)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
|
|
|
TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
|
1995-10-03 02:19:17 +08:00
|
|
|
psim_print_info (simulator, verbose);
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-17 18:31:52 +08:00
|
|
|
SIM_RC
|
1997-08-27 12:44:41 +08:00
|
|
|
sim_create_inferior (SIM_DESC sd,
|
|
|
|
struct _bfd *abfd,
|
|
|
|
char **argv,
|
|
|
|
char **envp)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1997-08-27 12:44:41 +08:00
|
|
|
unsigned_word entry_point;
|
1995-10-02 23:47:57 +08:00
|
|
|
TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
|
1997-04-17 18:31:52 +08:00
|
|
|
entry_point));
|
1997-10-03 07:37:30 +08:00
|
|
|
|
|
|
|
if (simulator == NULL)
|
|
|
|
error ("No program loaded");
|
|
|
|
|
1997-08-27 12:44:41 +08:00
|
|
|
if (abfd != NULL)
|
|
|
|
entry_point = bfd_get_start_address (abfd);
|
|
|
|
else
|
|
|
|
entry_point = 0xfff00000; /* ??? */
|
1995-10-02 23:47:57 +08:00
|
|
|
|
1995-11-02 22:27:18 +08:00
|
|
|
psim_init(simulator);
|
1995-10-02 23:47:57 +08:00
|
|
|
psim_stack(simulator, argv, envp);
|
|
|
|
|
|
|
|
psim_write_register(simulator, -1 /* all start at same PC */,
|
|
|
|
&entry_point, "pc", cooked_transfer);
|
1997-04-17 18:31:52 +08:00
|
|
|
return SIM_RC_OK;
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
|
|
|
psim_status status = psim_get_status(simulator);
|
|
|
|
|
1997-05-20 09:57:43 +08:00
|
|
|
switch (status.reason) {
|
|
|
|
case was_continuing:
|
|
|
|
*reason = sim_stopped;
|
|
|
|
if (status.signal == 0)
|
1995-10-02 23:47:57 +08:00
|
|
|
*sigrc = SIGTRAP;
|
1997-05-20 09:57:43 +08:00
|
|
|
else
|
1995-10-02 23:47:57 +08:00
|
|
|
*sigrc = status.signal;
|
|
|
|
break;
|
1997-05-20 09:57:43 +08:00
|
|
|
case was_trap:
|
1995-10-02 23:47:57 +08:00
|
|
|
*reason = sim_stopped;
|
|
|
|
*sigrc = SIGTRAP;
|
|
|
|
break;
|
1997-05-20 09:57:43 +08:00
|
|
|
case was_exited:
|
|
|
|
*reason = sim_exited;
|
|
|
|
*sigrc = status.signal;
|
|
|
|
break;
|
|
|
|
case was_signalled:
|
|
|
|
*reason = sim_signalled;
|
|
|
|
*sigrc = status.signal;
|
|
|
|
break;
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
1995-11-02 22:27:18 +08:00
|
|
|
|
1995-11-12 22:20:39 +08:00
|
|
|
TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
|
|
|
|
(long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Run (or resume) the program. */
|
1997-05-20 09:57:43 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
sim_stop (SIM_DESC sd)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1997-05-20 09:57:43 +08:00
|
|
|
psim_stop (simulator);
|
|
|
|
return 1;
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_resume (SIM_DESC sd, int step, int siggnal)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1995-11-02 22:27:18 +08:00
|
|
|
TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
|
|
|
|
step, siggnal));
|
|
|
|
|
1995-10-02 23:47:57 +08:00
|
|
|
if (step)
|
1996-11-20 16:38:13 +08:00
|
|
|
{
|
1997-05-20 09:57:43 +08:00
|
|
|
psim_step (simulator);
|
1996-11-20 16:38:13 +08:00
|
|
|
}
|
1995-10-02 23:47:57 +08:00
|
|
|
else
|
1996-11-20 16:38:13 +08:00
|
|
|
{
|
1997-05-20 09:57:43 +08:00
|
|
|
psim_run (simulator);
|
1996-11-20 16:38:13 +08:00
|
|
|
}
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-04-17 18:31:52 +08:00
|
|
|
sim_do_command (SIM_DESC sd, char *cmd)
|
1995-10-02 23:47:57 +08:00
|
|
|
{
|
1996-07-23 23:42:42 +08:00
|
|
|
TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
|
|
|
|
cmd ? cmd : "(null)"));
|
1997-01-28 05:34:50 +08:00
|
|
|
if (cmd != NULL) {
|
1996-07-23 23:42:42 +08:00
|
|
|
char **argv = buildargv(cmd);
|
1997-01-28 05:34:50 +08:00
|
|
|
psim_command(root_device, argv);
|
1996-07-23 23:42:42 +08:00
|
|
|
freeargv(argv);
|
|
|
|
}
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
1996-11-20 16:38:13 +08:00
|
|
|
|
|
|
|
/* Map simulator IO operations onto the corresponding GDB I/O
|
|
|
|
functions.
|
|
|
|
|
|
|
|
NB: Only a limited subset of operations are mapped across. More
|
|
|
|
advanced operations (such as dup or write) must either be mapped to
|
|
|
|
one of the below calls or handled internally */
|
|
|
|
|
|
|
|
int
|
|
|
|
sim_io_read_stdin(char *buf,
|
|
|
|
int sizeof_buf)
|
|
|
|
{
|
|
|
|
switch (CURRENT_STDIO) {
|
|
|
|
case DO_USE_STDIO:
|
|
|
|
return callbacks->read_stdin(callbacks, buf, sizeof_buf);
|
|
|
|
break;
|
|
|
|
case DONT_USE_STDIO:
|
|
|
|
return callbacks->read(callbacks, 0, buf, sizeof_buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("sim_io_read_stdin: unaccounted switch\n");
|
|
|
|
break;
|
|
|
|
}
|
1997-01-28 05:34:50 +08:00
|
|
|
return 0;
|
1996-11-20 16:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sim_io_write_stdout(const char *buf,
|
|
|
|
int sizeof_buf)
|
|
|
|
{
|
|
|
|
switch (CURRENT_STDIO) {
|
|
|
|
case DO_USE_STDIO:
|
|
|
|
return callbacks->write_stdout(callbacks, buf, sizeof_buf);
|
|
|
|
break;
|
|
|
|
case DONT_USE_STDIO:
|
|
|
|
return callbacks->write(callbacks, 1, buf, sizeof_buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("sim_io_write_stdout: unaccounted switch\n");
|
|
|
|
break;
|
|
|
|
}
|
1997-01-28 05:34:50 +08:00
|
|
|
return 0;
|
1996-11-20 16:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sim_io_write_stderr(const char *buf,
|
|
|
|
int sizeof_buf)
|
|
|
|
{
|
|
|
|
switch (CURRENT_STDIO) {
|
|
|
|
case DO_USE_STDIO:
|
|
|
|
/* NB: I think there should be an explicit write_stderr callback */
|
|
|
|
return callbacks->write(callbacks, 3, buf, sizeof_buf);
|
|
|
|
break;
|
|
|
|
case DONT_USE_STDIO:
|
|
|
|
return callbacks->write(callbacks, 3, buf, sizeof_buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("sim_io_write_stderr: unaccounted switch\n");
|
|
|
|
break;
|
|
|
|
}
|
1997-01-28 05:34:50 +08:00
|
|
|
return 0;
|
1996-11-20 16:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
sim_io_printf_filtered(const char *fmt,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
char message[1024];
|
|
|
|
va_list ap;
|
|
|
|
/* format the message */
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsprintf(message, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
/* sanity check */
|
|
|
|
if (strlen(message) >= sizeof(message))
|
|
|
|
error("sim_io_printf_filtered: buffer overflow\n");
|
|
|
|
callbacks->printf_filtered(callbacks, "%s", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sim_io_flush_stdoutput(void)
|
|
|
|
{
|
|
|
|
switch (CURRENT_STDIO) {
|
|
|
|
case DO_USE_STDIO:
|
1997-10-03 07:37:30 +08:00
|
|
|
callbacks->flush_stdout (callbacks);
|
1996-11-20 16:38:13 +08:00
|
|
|
break;
|
|
|
|
case DONT_USE_STDIO:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("sim_io_read_stdin: unaccounted switch\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-10-02 23:47:57 +08:00
|
|
|
/****/
|
|
|
|
|
|
|
|
void *
|
|
|
|
zalloc(long size)
|
|
|
|
{
|
|
|
|
void *memory = (void*)xmalloc(size);
|
|
|
|
if (memory == NULL)
|
|
|
|
error("xmalloc failed\n");
|
1995-11-29 02:47:07 +08:00
|
|
|
memset(memory, 0, size);
|
1995-10-02 23:47:57 +08:00
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zfree(void *data)
|
|
|
|
{
|
1997-10-03 07:37:30 +08:00
|
|
|
free(NULL, data);
|
1995-10-02 23:47:57 +08:00
|
|
|
}
|