Commit | Line | Data |
---|---|---|
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 | 20 | |
c906108c | 21 | #include "server.h" |
f29d9b6d | 22 | #include "bfd.h" |
c5aa993b JM |
23 | #include "callback.h" /* GDB simulator callback interface */ |
24 | #include "remote-sim.h" /* GDB simulator interface */ | |
c906108c SS |
25 | |
26 | extern int remote_debug; | |
27 | ||
28 | extern host_callback default_callback; /* in sim/common/callback.c */ | |
29 | ||
5c44784c JM |
30 | static char my_registers[REGISTER_BYTES] __attribute__ ((aligned)); |
31 | char * registers = my_registers; | |
c906108c | 32 | |
c5aa993b | 33 | int target_byte_order; /* used by simulator */ |
c906108c SS |
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 | |
f29d9b6d | 43 | mygeneric_load (bfd *loadfile_bfd) |
c906108c SS |
44 | { |
45 | asection *s; | |
46 | ||
c5aa993b | 47 | for (s = loadfile_bfd->sections; s; s = s->next) |
c906108c | 48 | { |
c5aa993b | 49 | if (s->flags & SEC_LOAD) |
c906108c SS |
50 | { |
51 | bfd_size_type size; | |
52 | ||
53 | size = bfd_get_section_size_before_reloc (s); | |
54 | if (size > 0) | |
55 | { | |
56 | char *buffer; | |
57 | bfd_vma lma; /* use load address, not virtual address */ | |
58 | ||
59 | buffer = xmalloc (size); | |
60 | lma = s->lma; | |
61 | ||
62 | /* Is this really necessary? I guess it gives the user something | |
c5aa993b | 63 | to look at during a long download. */ |
c906108c SS |
64 | printf ("Loading section %s, size 0x%lx lma 0x%lx\n", |
65 | bfd_get_section_name (loadfile_bfd, s), | |
66 | (unsigned long) size, | |
c5aa993b | 67 | (unsigned long) lma); /* chops high 32 bits. FIXME!! */ |
c906108c SS |
68 | |
69 | bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size); | |
70 | ||
71 | write_inferior_memory (lma, buffer, size); | |
72 | free (buffer); | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | printf ("Start address 0x%lx\n", | |
c5aa993b | 78 | (unsigned long) loadfile_bfd->start_address); |
c906108c SS |
79 | |
80 | /* We were doing this in remote-mips.c, I suspect it is right | |
81 | for other targets too. */ | |
c5aa993b | 82 | /* write_pc (loadfile_bfd->start_address); *//* FIXME!! */ |
c906108c SS |
83 | } |
84 | ||
85 | int | |
fba45db2 | 86 | create_inferior (char *program, char **argv) |
c906108c SS |
87 | { |
88 | bfd *abfd; | |
89 | int pid = 0; | |
90 | #ifdef TARGET_BYTE_ORDER_SELECTABLE | |
91 | char **new_argv; | |
92 | int nargs; | |
93 | #endif | |
94 | ||
95 | abfd = bfd_openr (program, 0); | |
c5aa993b | 96 | if (!abfd) |
c906108c | 97 | { |
c5aa993b | 98 | fprintf (stderr, "gdbserver: can't open %s: %s\n", |
c906108c SS |
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 | ||
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_argv[nargs] = "-E"; | |
119 | new_argv[nargs + 1] = bfd_big_endian (abfd) ? "big" : "little"; | |
120 | new_argv[nargs + 2] = NULL; | |
121 | argv = new_argv; | |
122 | #endif | |
123 | ||
124 | /* Create an instance of the simulator. */ | |
125 | default_callback.init (&default_callback); | |
126 | gdbsim_desc = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, argv); | |
127 | if (gdbsim_desc == 0) | |
128 | exit (1); | |
129 | ||
130 | /* Load the program into the simulator. */ | |
131 | if (abfd) | |
132 | if (sim_load (gdbsim_desc, program, NULL, 0) == SIM_RC_FAIL) | |
f29d9b6d | 133 | mygeneric_load (abfd); |
c906108c SS |
134 | |
135 | /* Create an inferior process in the simulator. This initializes SP. */ | |
136 | sim_create_inferior (gdbsim_desc, abfd, argv, /* env */ NULL); | |
137 | sim_resume (gdbsim_desc, 1, 0); /* execute one instr */ | |
138 | return pid; | |
139 | } | |
140 | ||
141 | /* Kill the inferior process. Make us have no inferior. */ | |
142 | ||
143 | void | |
fba45db2 | 144 | kill_inferior (void) |
c906108c SS |
145 | { |
146 | sim_close (gdbsim_desc, 0); | |
147 | default_callback.shutdown (&default_callback); | |
148 | } | |
149 | ||
150 | /* Fetch one register. */ | |
151 | ||
152 | static void | |
fba45db2 | 153 | fetch_register (int regno) |
c906108c SS |
154 | { |
155 | sim_fetch_register (gdbsim_desc, regno, ®isters[REGISTER_BYTE (regno)], | |
156 | REGISTER_RAW_SIZE (regno)); | |
157 | } | |
158 | ||
159 | /* Fetch all registers, or just one, from the child process. */ | |
160 | ||
161 | void | |
fba45db2 | 162 | fetch_inferior_registers (int regno) |
c906108c SS |
163 | { |
164 | if (regno == -1 || regno == 0) | |
c5aa993b | 165 | for (regno = 0; regno < NUM_REGS /*-NUM_FREGS*/ ; regno++) |
c906108c SS |
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 | |
fba45db2 | 176 | store_inferior_registers (int regno) |
c906108c | 177 | { |
c5aa993b | 178 | if (regno == -1) |
c906108c SS |
179 | { |
180 | for (regno = 0; regno < NUM_REGS; regno++) | |
181 | store_inferior_registers (regno); | |
182 | } | |
183 | else | |
184 | sim_store_register (gdbsim_desc, regno, ®isters[REGISTER_BYTE (regno)], | |
185 | REGISTER_RAW_SIZE (regno)); | |
186 | } | |
187 | ||
188 | /* Return nonzero if the given thread is still alive. */ | |
189 | int | |
fba45db2 | 190 | mythread_alive (int pid) |
c906108c SS |
191 | { |
192 | return 1; | |
193 | } | |
194 | ||
195 | /* Wait for process, returns status */ | |
196 | ||
197 | unsigned char | |
fba45db2 | 198 | mywait (char *status) |
c906108c SS |
199 | { |
200 | int sigrc; | |
201 | enum sim_stop reason; | |
202 | ||
203 | sim_stop_reason (gdbsim_desc, &reason, &sigrc); | |
204 | switch (reason) | |
205 | { | |
206 | case sim_exited: | |
207 | if (remote_debug) | |
208 | printf ("\nChild exited with retcode = %x \n", sigrc); | |
209 | *status = 'W'; | |
210 | return sigrc; | |
211 | ||
212 | #if 0 | |
213 | case sim_stopped: | |
214 | if (remote_debug) | |
215 | printf ("\nChild terminated with signal = %x \n", sigrc); | |
216 | *status = 'X'; | |
217 | return sigrc; | |
218 | #endif | |
219 | ||
c5aa993b | 220 | default: /* should this be sim_signalled or sim_stopped? FIXME!! */ |
c906108c SS |
221 | if (remote_debug) |
222 | printf ("\nChild received signal = %x \n", sigrc); | |
223 | fetch_inferior_registers (0); | |
224 | *status = 'T'; | |
225 | return (unsigned char) sigrc; | |
226 | } | |
227 | } | |
228 | ||
229 | /* Resume execution of the inferior process. | |
230 | If STEP is nonzero, single-step it. | |
231 | If SIGNAL is nonzero, give it that signal. */ | |
232 | ||
233 | void | |
fba45db2 | 234 | myresume (int step, int signo) |
c906108c SS |
235 | { |
236 | /* Should be using target_signal_to_host() or signal numbers in target.h | |
237 | to convert GDB signal number to target signal number. */ | |
238 | sim_resume (gdbsim_desc, step, signo); | |
239 | } | |
240 | ||
241 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
242 | to debugger memory starting at MYADDR. */ | |
243 | ||
244 | void | |
fba45db2 | 245 | read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
246 | { |
247 | sim_read (gdbsim_desc, memaddr, myaddr, len); | |
248 | } | |
249 | ||
250 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
251 | to inferior's memory at MEMADDR. | |
252 | On failure (cannot write the inferior) | |
253 | returns the value of errno. */ | |
254 | ||
255 | int | |
fba45db2 | 256 | write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c | 257 | { |
c5aa993b | 258 | sim_write (gdbsim_desc, memaddr, myaddr, len); /* should check for error. FIXME!! */ |
c906108c SS |
259 | return 0; |
260 | } | |
261 | ||
c906108c | 262 | void |
fba45db2 | 263 | initialize_low (void) |
c906108c | 264 | { |
c906108c | 265 | } |