]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/low-sim.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / gdbserver / low-sim.c
1 /* Low level interface to simulators, for the remote server for GDB.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21 #include "defs.h"
22 #include "bfd.h"
23 #include "server.h"
24 #include "callback.h" /* GDB simulator callback interface */
25 #include "remote-sim.h" /* GDB simulator interface */
26
27 extern int remote_debug;
28
29 extern host_callback default_callback; /* in sim/common/callback.c */
30
31 char registers[REGISTER_BYTES] __attribute__ ((aligned));
32
33 int target_byte_order; /* used by simulator */
34
35 /* We record the result of sim_open so we can pass it
36 back to the other sim_foo routines. */
37 static SIM_DESC gdbsim_desc = 0;
38
39 /* This version of "load" should be usable for any simulator that
40 does not support loading itself. */
41
42 static void
43 generic_load (loadfile_bfd)
44 bfd *loadfile_bfd;
45 {
46 asection *s;
47
48 for (s = loadfile_bfd->sections; s; s = s->next)
49 {
50 if (s->flags & SEC_LOAD)
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
64 to look at during a long download. */
65 printf ("Loading section %s, size 0x%lx lma 0x%lx\n",
66 bfd_get_section_name (loadfile_bfd, s),
67 (unsigned long) size,
68 (unsigned long) lma); /* chops high 32 bits. FIXME!! */
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",
79 (unsigned long) loadfile_bfd->start_address);
80
81 /* We were doing this in remote-mips.c, I suspect it is right
82 for other targets too. */
83 /* write_pc (loadfile_bfd->start_address); *//* FIXME!! */
84 }
85
86 int
87 create_inferior (program, argv)
88 char *program;
89 char **argv;
90 {
91 bfd *abfd;
92 int pid = 0;
93 #ifdef TARGET_BYTE_ORDER_SELECTABLE
94 char **new_argv;
95 int nargs;
96 #endif
97
98 abfd = bfd_openr (program, 0);
99 if (!abfd)
100 {
101 fprintf (stderr, "gdbserver: can't open %s: %s\n",
102 program, bfd_errmsg (bfd_get_error ()));
103 exit (1);
104 }
105
106 if (!bfd_check_format (abfd, bfd_object))
107 {
108 fprintf (stderr, "gdbserver: unknown load format for %s: %s\n",
109 program, bfd_errmsg (bfd_get_error ()));
110 exit (1);
111 }
112
113 #ifdef TARGET_BYTE_ORDER_SELECTABLE
114 /* Add "-E big" or "-E little" to the argument list depending on the
115 endianness of the program to be loaded. */
116 for (nargs = 0; argv[nargs] != NULL; nargs++) /* count the args */
117 ;
118 new_argv = alloca (sizeof (char *) * (nargs + 3)); /* allocate new args */
119 for (nargs = 0; argv[nargs] != NULL; nargs++) /* copy old to new */
120 new_argv[nargs] = argv[nargs];
121 new_argv[nargs] = "-E";
122 new_argv[nargs + 1] = bfd_big_endian (abfd) ? "big" : "little";
123 new_argv[nargs + 2] = NULL;
124 argv = new_argv;
125 #endif
126
127 /* Create an instance of the simulator. */
128 default_callback.init (&default_callback);
129 gdbsim_desc = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, argv);
130 if (gdbsim_desc == 0)
131 exit (1);
132
133 /* Load the program into the simulator. */
134 if (abfd)
135 if (sim_load (gdbsim_desc, program, NULL, 0) == SIM_RC_FAIL)
136 generic_load (abfd);
137
138 /* Create an inferior process in the simulator. This initializes SP. */
139 sim_create_inferior (gdbsim_desc, abfd, argv, /* env */ NULL);
140 sim_resume (gdbsim_desc, 1, 0); /* execute one instr */
141 return pid;
142 }
143
144 /* Kill the inferior process. Make us have no inferior. */
145
146 void
147 kill_inferior ()
148 {
149 sim_close (gdbsim_desc, 0);
150 default_callback.shutdown (&default_callback);
151 }
152
153 /* Fetch one register. */
154
155 static void
156 fetch_register (regno)
157 int regno;
158 {
159 sim_fetch_register (gdbsim_desc, regno, &registers[REGISTER_BYTE (regno)],
160 REGISTER_RAW_SIZE (regno));
161 }
162
163 /* Fetch all registers, or just one, from the child process. */
164
165 void
166 fetch_inferior_registers (regno)
167 int regno;
168 {
169 if (regno == -1 || regno == 0)
170 for (regno = 0; regno < NUM_REGS /*-NUM_FREGS*/ ; regno++)
171 fetch_register (regno);
172 else
173 fetch_register (regno);
174 }
175
176 /* Store our register values back into the inferior.
177 If REGNO is -1, do this for all registers.
178 Otherwise, REGNO specifies which register (so we can save time). */
179
180 void
181 store_inferior_registers (regno)
182 int regno;
183 {
184 if (regno == -1)
185 {
186 for (regno = 0; regno < NUM_REGS; regno++)
187 store_inferior_registers (regno);
188 }
189 else
190 sim_store_register (gdbsim_desc, regno, &registers[REGISTER_BYTE (regno)],
191 REGISTER_RAW_SIZE (regno));
192 }
193
194 /* Return nonzero if the given thread is still alive. */
195 int
196 mythread_alive (pid)
197 int pid;
198 {
199 return 1;
200 }
201
202 /* Wait for process, returns status */
203
204 unsigned char
205 mywait (status)
206 char *status;
207 {
208 int sigrc;
209 enum sim_stop reason;
210
211 sim_stop_reason (gdbsim_desc, &reason, &sigrc);
212 switch (reason)
213 {
214 case sim_exited:
215 if (remote_debug)
216 printf ("\nChild exited with retcode = %x \n", sigrc);
217 *status = 'W';
218 return sigrc;
219
220 #if 0
221 case sim_stopped:
222 if (remote_debug)
223 printf ("\nChild terminated with signal = %x \n", sigrc);
224 *status = 'X';
225 return sigrc;
226 #endif
227
228 default: /* should this be sim_signalled or sim_stopped? FIXME!! */
229 if (remote_debug)
230 printf ("\nChild received signal = %x \n", sigrc);
231 fetch_inferior_registers (0);
232 *status = 'T';
233 return (unsigned char) sigrc;
234 }
235 }
236
237 /* Resume execution of the inferior process.
238 If STEP is nonzero, single-step it.
239 If SIGNAL is nonzero, give it that signal. */
240
241 void
242 myresume (step, signo)
243 int step;
244 int signo;
245 {
246 /* Should be using target_signal_to_host() or signal numbers in target.h
247 to convert GDB signal number to target signal number. */
248 sim_resume (gdbsim_desc, step, signo);
249 }
250
251 /* Copy LEN bytes from inferior's memory starting at MEMADDR
252 to debugger memory starting at MYADDR. */
253
254 void
255 read_inferior_memory (memaddr, myaddr, len)
256 CORE_ADDR memaddr;
257 char *myaddr;
258 int len;
259 {
260 sim_read (gdbsim_desc, memaddr, myaddr, len);
261 }
262
263 /* Copy LEN bytes of data from debugger memory at MYADDR
264 to inferior's memory at MEMADDR.
265 On failure (cannot write the inferior)
266 returns the value of errno. */
267
268 int
269 write_inferior_memory (memaddr, myaddr, len)
270 CORE_ADDR memaddr;
271 char *myaddr;
272 int len;
273 {
274 sim_write (gdbsim_desc, memaddr, myaddr, len); /* should check for error. FIXME!! */
275 return 0;
276 }
277
278 #if 0
279 void
280 initialize ()
281 {
282 inferior_pid = 0;
283 }
284
285 int
286 have_inferior_p ()
287 {
288 return inferior_pid != 0;
289 }
290 #endif