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