Thu Aug 7 13:39:31 1997 Geoffrey Noer <noer@cygnus.com>
[deliverable/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, 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
26 extern host_callback default_callback; /* in callback.c */
27
28 char registers[REGISTER_BYTES];
29
30 int target_byte_order; /* used by simulator */
31
32 /* This version of "load" should be usable for any simulator that
33 does not support loading itself. */
34
35 static void
36 generic_load (loadfile_bfd)
37 bfd *loadfile_bfd;
38 {
39 asection *s;
40
41 for (s = loadfile_bfd->sections; s; s = s->next)
42 {
43 if (s->flags & SEC_LOAD)
44 {
45 bfd_size_type size;
46
47 size = bfd_get_section_size_before_reloc (s);
48 if (size > 0)
49 {
50 char *buffer;
51 bfd_vma vma;
52
53 buffer = xmalloc (size);
54 vma = bfd_get_section_vma (loadfile_bfd, s);
55
56 /* Is this really necessary? I guess it gives the user something
57 to look at during a long download. */
58 fprintf (stderr, "Loading section %s, size 0x%lx vma 0x%lx\n",
59 bfd_get_section_name (loadfile_bfd, s),
60 (unsigned long) size,
61 (unsigned long) vma); /* chops high 32 bits. FIXME!! */
62
63 bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
64
65 write_inferior_memory (vma, buffer, size);
66 free (buffer);
67 }
68 }
69 }
70
71 fprintf (stderr, "Start address 0x%lx\n",
72 (unsigned long)loadfile_bfd->start_address);
73
74 /* We were doing this in remote-mips.c, I suspect it is right
75 for other targets too. */
76 /* write_pc (loadfile_bfd->start_address); */ /* FIXME!! */
77 }
78
79 int
80 create_inferior (program, allargs)
81 char *program;
82 char **allargs;
83 {
84 bfd *abfd;
85 int pid = 0;
86
87 abfd = bfd_openr (program, 0);
88 if (!abfd)
89 {
90 fprintf (stderr, "gdbserver: can't open %s: %s\n",
91 program, bfd_errmsg (bfd_get_error ()));
92 exit (1);
93 }
94
95 if (!bfd_check_format (abfd, bfd_object))
96 {
97 fprintf (stderr, "gdbserver: unknown load format for %s: %s\n",
98 program, bfd_errmsg (bfd_get_error ()));
99 exit (1);
100 }
101
102 /* This must be set before sim_open is called, because gdb assumes that
103 the simulator endianness is known immediately after the sim_open call. */
104 target_byte_order = bfd_big_endian (abfd) ? 4321 : 1234;
105
106 sim_set_callbacks (&default_callback);
107 default_callback.init (&default_callback);
108
109 /* Should concatenate args here. FIXME!! */
110 sim_open (allargs[0]);
111
112 /* Load program. */
113 if (sim_load (allargs[0], 0) != 0)
114 generic_load (abfd);
115
116 return pid;
117 }
118
119 /* Kill the inferior process. Make us have no inferior. */
120
121 void
122 kill_inferior ()
123 {
124 sim_close (0);
125 default_callback.shutdown (&default_callback);
126 }
127
128 /* Fetch one register. */
129
130 static void
131 fetch_register (regno)
132 int regno;
133 {
134 sim_fetch_register (regno, &registers[REGISTER_BYTE (regno)]);
135 }
136
137 /* Fetch all registers, or just one, from the child process. */
138
139 void
140 fetch_inferior_registers (regno)
141 int regno;
142 {
143 if (regno == -1 || regno == 0)
144 for (regno = 0; regno < NUM_REGS/*-NUM_FREGS*/; regno++)
145 fetch_register (regno);
146 else
147 fetch_register (regno);
148 }
149
150 /* Store our register values back into the inferior.
151 If REGNO is -1, do this for all registers.
152 Otherwise, REGNO specifies which register (so we can save time). */
153
154 void
155 store_inferior_registers (regno)
156 int regno;
157 {
158 if (regno == -1)
159 {
160 for (regno = 0; regno < NUM_REGS; regno++)
161 store_inferior_registers (regno);
162 }
163 else
164 sim_store_register (regno, &registers[REGISTER_BYTE (regno)]);
165 }
166
167 /* Return nonzero if the given thread is still alive. */
168 int
169 mythread_alive (pid)
170 int pid;
171 {
172 return 1;
173 }
174
175 /* Wait for process, returns status */
176
177 unsigned char
178 mywait (status)
179 char *status;
180 {
181 int sigrc;
182 enum sim_stop reason;
183
184 sim_stop_reason (&reason, &sigrc);
185 switch (reason)
186 {
187 case sim_exited:
188 fprintf (stderr, "\nChild exited with retcode = %x \n", sigrc);
189 *status = 'W';
190 return sigrc;
191
192 #if 0
193 case sim_stopped:
194 fprintf (stderr, "\nChild terminated with signal = %x \n", sigrc);
195 *status = 'X';
196 return sigrc;
197 #endif
198
199 default: /* should this be sim_signalled or sim_stopped? FIXME!! */
200 fprintf (stderr, "\nChild received signal = %x \n", sigrc);
201 fetch_inferior_registers (0);
202 *status = 'T';
203 return (unsigned char) sigrc;
204 }
205 }
206
207 /* Resume execution of the inferior process.
208 If STEP is nonzero, single-step it.
209 If SIGNAL is nonzero, give it that signal. */
210
211 void
212 myresume (step, signo)
213 int step;
214 int signo;
215 {
216 /* Should be using target_signal_to_host() or signal numbers in target.h
217 to convert GDB signal number to target signal number. */
218 sim_resume (step, signo);
219 }
220
221 /* Copy LEN bytes from inferior's memory starting at MEMADDR
222 to debugger memory starting at MYADDR. */
223
224 void
225 read_inferior_memory (memaddr, myaddr, len)
226 CORE_ADDR memaddr;
227 char *myaddr;
228 int len;
229 {
230 sim_read (memaddr, myaddr, len);
231 }
232
233 /* Copy LEN bytes of data from debugger memory at MYADDR
234 to inferior's memory at MEMADDR.
235 On failure (cannot write the inferior)
236 returns the value of errno. */
237
238 int
239 write_inferior_memory (memaddr, myaddr, len)
240 CORE_ADDR memaddr;
241 char *myaddr;
242 int len;
243 {
244 sim_write (memaddr, myaddr, len); /* should check for error. FIXME!! */
245 return 0;
246 }
247
248 #if 0
249 void
250 initialize ()
251 {
252 inferior_pid = 0;
253 }
254
255 int
256 have_inferior_p ()
257 {
258 return inferior_pid != 0;
259 }
260 #endif
This page took 0.034954 seconds and 4 git commands to generate.