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