* gdbint.texinfo (POP_FRAME): Document use by return_command.
[deliverable/binutils-gdb.git] / gdb / gdbserver / low-sim.c
CommitLineData
c906108c
SS
1/* Low level interface to simulators, for the remote server for GDB.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
c906108c 15
c5aa993b
JM
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21#include "defs.h"
22#include "bfd.h"
23#include "server.h"
c5aa993b
JM
24#include "callback.h" /* GDB simulator callback interface */
25#include "remote-sim.h" /* GDB simulator interface */
c906108c
SS
26
27extern int remote_debug;
28
29extern host_callback default_callback; /* in sim/common/callback.c */
30
5c44784c
JM
31static char my_registers[REGISTER_BYTES] __attribute__ ((aligned));
32char * registers = my_registers;
c906108c 33
c5aa993b 34int target_byte_order; /* used by simulator */
c906108c
SS
35
36/* We record the result of sim_open so we can pass it
37 back to the other sim_foo routines. */
38static SIM_DESC gdbsim_desc = 0;
39
40/* This version of "load" should be usable for any simulator that
41 does not support loading itself. */
42
43static void
fba45db2 44generic_load (bfd *loadfile_bfd)
c906108c
SS
45{
46 asection *s;
47
c5aa993b 48 for (s = loadfile_bfd->sections; s; s = s->next)
c906108c 49 {
c5aa993b 50 if (s->flags & SEC_LOAD)
c906108c
SS
51 {
52 bfd_size_type size;
53
54 size = bfd_get_section_size_before_reloc (s);
55 if (size > 0)
56 {
57 char *buffer;
58 bfd_vma lma; /* use load address, not virtual address */
59
60 buffer = xmalloc (size);
61 lma = s->lma;
62
63 /* Is this really necessary? I guess it gives the user something
c5aa993b 64 to look at during a long download. */
c906108c
SS
65 printf ("Loading section %s, size 0x%lx lma 0x%lx\n",
66 bfd_get_section_name (loadfile_bfd, s),
67 (unsigned long) size,
c5aa993b 68 (unsigned long) lma); /* chops high 32 bits. FIXME!! */
c906108c
SS
69
70 bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
71
72 write_inferior_memory (lma, buffer, size);
73 free (buffer);
74 }
75 }
76 }
77
78 printf ("Start address 0x%lx\n",
c5aa993b 79 (unsigned long) loadfile_bfd->start_address);
c906108c
SS
80
81 /* We were doing this in remote-mips.c, I suspect it is right
82 for other targets too. */
c5aa993b 83 /* write_pc (loadfile_bfd->start_address); *//* FIXME!! */
c906108c
SS
84}
85
86int
fba45db2 87create_inferior (char *program, char **argv)
c906108c
SS
88{
89 bfd *abfd;
90 int pid = 0;
91#ifdef TARGET_BYTE_ORDER_SELECTABLE
92 char **new_argv;
93 int nargs;
94#endif
95
96 abfd = bfd_openr (program, 0);
c5aa993b 97 if (!abfd)
c906108c 98 {
c5aa993b 99 fprintf (stderr, "gdbserver: can't open %s: %s\n",
c906108c
SS
100 program, bfd_errmsg (bfd_get_error ()));
101 exit (1);
102 }
103
104 if (!bfd_check_format (abfd, bfd_object))
105 {
106 fprintf (stderr, "gdbserver: unknown load format for %s: %s\n",
107 program, bfd_errmsg (bfd_get_error ()));
108 exit (1);
109 }
110
111#ifdef TARGET_BYTE_ORDER_SELECTABLE
112 /* Add "-E big" or "-E little" to the argument list depending on the
113 endianness of the program to be loaded. */
114 for (nargs = 0; argv[nargs] != NULL; nargs++) /* count the args */
115 ;
116 new_argv = alloca (sizeof (char *) * (nargs + 3)); /* allocate new args */
117 for (nargs = 0; argv[nargs] != NULL; nargs++) /* copy old to new */
118 new_argv[nargs] = argv[nargs];
119 new_argv[nargs] = "-E";
120 new_argv[nargs + 1] = bfd_big_endian (abfd) ? "big" : "little";
121 new_argv[nargs + 2] = NULL;
122 argv = new_argv;
123#endif
124
125 /* Create an instance of the simulator. */
126 default_callback.init (&default_callback);
127 gdbsim_desc = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, argv);
128 if (gdbsim_desc == 0)
129 exit (1);
130
131 /* Load the program into the simulator. */
132 if (abfd)
133 if (sim_load (gdbsim_desc, program, NULL, 0) == SIM_RC_FAIL)
134 generic_load (abfd);
135
136 /* Create an inferior process in the simulator. This initializes SP. */
137 sim_create_inferior (gdbsim_desc, abfd, argv, /* env */ NULL);
138 sim_resume (gdbsim_desc, 1, 0); /* execute one instr */
139 return pid;
140}
141
142/* Kill the inferior process. Make us have no inferior. */
143
144void
fba45db2 145kill_inferior (void)
c906108c
SS
146{
147 sim_close (gdbsim_desc, 0);
148 default_callback.shutdown (&default_callback);
149}
150
151/* Fetch one register. */
152
153static void
fba45db2 154fetch_register (int regno)
c906108c
SS
155{
156 sim_fetch_register (gdbsim_desc, regno, &registers[REGISTER_BYTE (regno)],
157 REGISTER_RAW_SIZE (regno));
158}
159
160/* Fetch all registers, or just one, from the child process. */
161
162void
fba45db2 163fetch_inferior_registers (int regno)
c906108c
SS
164{
165 if (regno == -1 || regno == 0)
c5aa993b 166 for (regno = 0; regno < NUM_REGS /*-NUM_FREGS*/ ; regno++)
c906108c
SS
167 fetch_register (regno);
168 else
169 fetch_register (regno);
170}
171
172/* Store our register values back into the inferior.
173 If REGNO is -1, do this for all registers.
174 Otherwise, REGNO specifies which register (so we can save time). */
175
176void
fba45db2 177store_inferior_registers (int regno)
c906108c 178{
c5aa993b 179 if (regno == -1)
c906108c
SS
180 {
181 for (regno = 0; regno < NUM_REGS; regno++)
182 store_inferior_registers (regno);
183 }
184 else
185 sim_store_register (gdbsim_desc, regno, &registers[REGISTER_BYTE (regno)],
186 REGISTER_RAW_SIZE (regno));
187}
188
189/* Return nonzero if the given thread is still alive. */
190int
fba45db2 191mythread_alive (int pid)
c906108c
SS
192{
193 return 1;
194}
195
196/* Wait for process, returns status */
197
198unsigned char
fba45db2 199mywait (char *status)
c906108c
SS
200{
201 int sigrc;
202 enum sim_stop reason;
203
204 sim_stop_reason (gdbsim_desc, &reason, &sigrc);
205 switch (reason)
206 {
207 case sim_exited:
208 if (remote_debug)
209 printf ("\nChild exited with retcode = %x \n", sigrc);
210 *status = 'W';
211 return sigrc;
212
213#if 0
214 case sim_stopped:
215 if (remote_debug)
216 printf ("\nChild terminated with signal = %x \n", sigrc);
217 *status = 'X';
218 return sigrc;
219#endif
220
c5aa993b 221 default: /* should this be sim_signalled or sim_stopped? FIXME!! */
c906108c
SS
222 if (remote_debug)
223 printf ("\nChild received signal = %x \n", sigrc);
224 fetch_inferior_registers (0);
225 *status = 'T';
226 return (unsigned char) sigrc;
227 }
228}
229
230/* Resume execution of the inferior process.
231 If STEP is nonzero, single-step it.
232 If SIGNAL is nonzero, give it that signal. */
233
234void
fba45db2 235myresume (int step, int signo)
c906108c
SS
236{
237 /* Should be using target_signal_to_host() or signal numbers in target.h
238 to convert GDB signal number to target signal number. */
239 sim_resume (gdbsim_desc, step, signo);
240}
241
242/* Copy LEN bytes from inferior's memory starting at MEMADDR
243 to debugger memory starting at MYADDR. */
244
245void
fba45db2 246read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c
SS
247{
248 sim_read (gdbsim_desc, memaddr, myaddr, len);
249}
250
251/* Copy LEN bytes of data from debugger memory at MYADDR
252 to inferior's memory at MEMADDR.
253 On failure (cannot write the inferior)
254 returns the value of errno. */
255
256int
fba45db2 257write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 258{
c5aa993b 259 sim_write (gdbsim_desc, memaddr, myaddr, len); /* should check for error. FIXME!! */
c906108c
SS
260 return 0;
261}
262
c906108c 263void
fba45db2 264initialize_low (void)
c906108c 265{
c906108c 266}
This page took 0.084225 seconds and 4 git commands to generate.