]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/low-sparc.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / gdbserver / low-sparc.c
CommitLineData
c906108c
SS
1/* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993 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
SS
20
21#include "defs.h"
22#include <sys/wait.h>
23#include "frame.h"
24#include "inferior.h"
25/***************************
26#include "initialize.h"
27****************************/
28
29#include <stdio.h>
30#include <sys/param.h>
31#include <sys/dir.h>
32#include <sys/user.h>
33#include <signal.h>
34#include <sys/ioctl.h>
35#include <sgtty.h>
36#include <fcntl.h>
37
38/***************Begin MY defs*********************/
39int quit_flag = 0;
40char registers[REGISTER_BYTES];
41
42/* Index within `registers' of the first byte of the space for
43 register N. */
44
45
46char buf2[MAX_REGISTER_RAW_SIZE];
47/***************End MY defs*********************/
48
49#include <sys/ptrace.h>
50#include <sys/reg.h>
51
52extern int sys_nerr;
53extern char **sys_errlist;
54extern char **environ;
55extern int errno;
56extern int inferior_pid;
57void quit (), perror_with_name ();
58int query ();
59
60/* Start an inferior process and returns its pid.
61 ALLARGS is a vector of program-name and args.
62 ENV is the environment vector to pass. */
63
64int
65create_inferior (program, allargs)
66 char *program;
67 char **allargs;
68{
69 int pid;
70
71 pid = fork ();
72 if (pid < 0)
73 perror_with_name ("fork");
74
75 if (pid == 0)
76 {
77 ptrace (PTRACE_TRACEME);
78
79 execv (program, allargs);
80
81 fprintf (stderr, "Cannot exec %s: %s.\n", program,
82 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
83 fflush (stderr);
84 _exit (0177);
85 }
86
87 return pid;
88}
89
90/* Kill the inferior process. Make us have no inferior. */
91
92void
93kill_inferior ()
94{
95 if (inferior_pid == 0)
96 return;
97 ptrace (8, inferior_pid, 0, 0);
98 wait (0);
c5aa993b 99/*************inferior_died ();****VK**************/
c906108c
SS
100}
101
102/* Return nonzero if the given thread is still alive. */
103int
104mythread_alive (pid)
105 int pid;
106{
107 return 1;
108}
109
110/* Wait for process, returns status */
111
112unsigned char
113mywait (status)
114 char *status;
115{
116 int pid;
117 union wait w;
118
119 pid = wait (&w);
120 if (pid != inferior_pid)
121 perror_with_name ("wait");
122
123 if (WIFEXITED (w))
124 {
125 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
126 *status = 'W';
127 return ((unsigned char) WEXITSTATUS (w));
128 }
129 else if (!WIFSTOPPED (w))
130 {
131 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
132 *status = 'X';
133 return ((unsigned char) WTERMSIG (w));
134 }
135
136 fetch_inferior_registers (0);
137
138 *status = 'T';
139 return ((unsigned char) WSTOPSIG (w));
140}
141
142/* Resume execution of the inferior process.
143 If STEP is nonzero, single-step it.
144 If SIGNAL is nonzero, give it that signal. */
145
146void
147myresume (step, signal)
148 int step;
149 int signal;
150{
151 errno = 0;
152 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
153 if (errno)
154 perror_with_name ("ptrace");
155}
156
157/* Fetch one or more registers from the inferior. REGNO == -1 to get
158 them all. We actually fetch more than requested, when convenient,
159 marking them as valid so we won't fetch them again. */
160
161void
162fetch_inferior_registers (ignored)
163 int ignored;
164{
165 struct regs inferior_registers;
166 struct fp_status inferior_fp_registers;
167 int i;
168
169 /* Global and Out regs are fetched directly, as well as the control
170 registers. If we're getting one of the in or local regs,
171 and the stack pointer has not yet been fetched,
172 we have to do that first, since they're found in memory relative
173 to the stack pointer. */
174
175 if (ptrace (PTRACE_GETREGS, inferior_pid,
c5aa993b
JM
176 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
177 perror ("ptrace_getregs");
178
c906108c
SS
179 registers[REGISTER_BYTE (0)] = 0;
180 memcpy (&registers[REGISTER_BYTE (1)], &inferior_registers.r_g1,
181 15 * REGISTER_RAW_SIZE (G0_REGNUM));
c5aa993b
JM
182 *(int *) &registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
183 *(int *) &registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
184 *(int *) &registers[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc;
185 *(int *) &registers[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
c906108c
SS
186
187 /* Floating point registers */
188
189 if (ptrace (PTRACE_GETFPREGS, inferior_pid,
c5aa993b 190 (PTRACE_ARG3_TYPE) & inferior_fp_registers,
c906108c 191 0))
c5aa993b 192 perror ("ptrace_getfpregs");
c906108c
SS
193 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
194 sizeof inferior_fp_registers.fpu_fr);
195
196 /* These regs are saved on the stack by the kernel. Only read them
197 all (16 ptrace calls!) if we really need them. */
198
c5aa993b 199 read_inferior_memory (*(CORE_ADDR *) & registers[REGISTER_BYTE (SP_REGNUM)],
c906108c 200 &registers[REGISTER_BYTE (L0_REGNUM)],
c5aa993b 201 16 * REGISTER_RAW_SIZE (L0_REGNUM));
c906108c
SS
202}
203
204/* Store our register values back into the inferior.
205 If REGNO is -1, do this for all registers.
206 Otherwise, REGNO specifies which register (so we can save time). */
207
208void
209store_inferior_registers (ignored)
210 int ignored;
211{
212 struct regs inferior_registers;
213 struct fp_status inferior_fp_registers;
c5aa993b 214 CORE_ADDR sp = *(CORE_ADDR *) & registers[REGISTER_BYTE (SP_REGNUM)];
c906108c
SS
215
216 write_inferior_memory (sp, &registers[REGISTER_BYTE (L0_REGNUM)],
c5aa993b 217 16 * REGISTER_RAW_SIZE (L0_REGNUM));
c906108c
SS
218
219 memcpy (&inferior_registers.r_g1, &registers[REGISTER_BYTE (G1_REGNUM)],
220 15 * REGISTER_RAW_SIZE (G1_REGNUM));
221
222 inferior_registers.r_ps =
c5aa993b 223 *(int *) &registers[REGISTER_BYTE (PS_REGNUM)];
c906108c 224 inferior_registers.r_pc =
c5aa993b 225 *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
c906108c 226 inferior_registers.r_npc =
c5aa993b 227 *(int *) &registers[REGISTER_BYTE (NPC_REGNUM)];
c906108c 228 inferior_registers.r_y =
c5aa993b 229 *(int *) &registers[REGISTER_BYTE (Y_REGNUM)];
c906108c
SS
230
231 if (ptrace (PTRACE_SETREGS, inferior_pid,
c5aa993b
JM
232 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
233 perror ("ptrace_setregs");
c906108c
SS
234
235 memcpy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
236 sizeof inferior_fp_registers.fpu_fr);
237
238 if (ptrace (PTRACE_SETFPREGS, inferior_pid,
c5aa993b
JM
239 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0))
240 perror ("ptrace_setfpregs");
c906108c
SS
241}
242
243/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
244 in the NEW_SUN_PTRACE case.
245 It ought to be straightforward. But it appears that writing did
246 not write the data that I specified. I cannot understand where
247 it got the data that it actually did write. */
248
249/* Copy LEN bytes from inferior's memory starting at MEMADDR
250 to debugger memory starting at MYADDR. */
251
252read_inferior_memory (memaddr, myaddr, len)
253 CORE_ADDR memaddr;
254 char *myaddr;
255 int len;
256{
257 register int i;
258 /* Round starting address down to longword boundary. */
259 register CORE_ADDR addr = memaddr & -sizeof (int);
260 /* Round ending address up; get number of longwords that makes. */
261 register int count
262 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
263 /* Allocate buffer of that many longwords. */
264 register int *buffer = (int *) alloca (count * sizeof (int));
265
266 /* Read all the longwords */
267 for (i = 0; i < count; i++, addr += sizeof (int))
268 {
269 buffer[i] = ptrace (1, inferior_pid, addr, 0);
270 }
271
272 /* Copy appropriate bytes out of the buffer. */
273 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
274}
275
276/* Copy LEN bytes of data from debugger memory at MYADDR
277 to inferior's memory at MEMADDR.
278 On failure (cannot write the inferior)
279 returns the value of errno. */
280
281int
282write_inferior_memory (memaddr, myaddr, len)
283 CORE_ADDR memaddr;
284 char *myaddr;
285 int len;
286{
287 register int i;
288 /* Round starting address down to longword boundary. */
289 register CORE_ADDR addr = memaddr & -sizeof (int);
290 /* Round ending address up; get number of longwords that makes. */
291 register int count
292 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
293 /* Allocate buffer of that many longwords. */
294 register int *buffer = (int *) alloca (count * sizeof (int));
295 extern int errno;
296
297 /* Fill start and end extra bytes of buffer with existing memory data. */
298
299 buffer[0] = ptrace (1, inferior_pid, addr, 0);
300
301 if (count > 1)
302 {
303 buffer[count - 1]
304 = ptrace (1, inferior_pid,
305 addr + (count - 1) * sizeof (int), 0);
306 }
307
308 /* Copy data to be written over corresponding part of buffer */
309
310 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
311
312 /* Write the entire buffer. */
313
314 for (i = 0; i < count; i++, addr += sizeof (int))
315 {
316 errno = 0;
317 ptrace (4, inferior_pid, addr, buffer[i]);
318 if (errno)
319 return errno;
320 }
321
322 return 0;
323}
324\f
325void
326initialize ()
327{
328 inferior_pid = 0;
329}
330
331int
332have_inferior_p ()
333{
334 return inferior_pid != 0;
335}