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