2000-12-17 Elena Zannoni <ezannoni@kwikemart.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / low-sun3.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993 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,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "<sys/wait.h>"
23 #include "frame.h"
24 #include "inferior.h"
25
26 #include <stdio.h>
27 #include <sys/param.h>
28 #include <sys/dir.h>
29 #include <sys/user.h>
30 #include <signal.h>
31 #include <sys/ioctl.h>
32 #include <sgtty.h>
33 #include <fcntl.h>
34
35 /***************Begin MY defs*********************/
36 static char my_registers[REGISTER_BYTES];
37 char *registers = my_registers;
38 /***************End MY defs*********************/
39
40 #include <sys/ptrace.h>
41 #include <machine/reg.h>
42
43 extern int sys_nerr;
44 extern char **sys_errlist;
45 extern int errno;
46 extern int inferior_pid;
47 void perror_with_name ();
48
49 /* Start an inferior process and returns its pid.
50 ALLARGS is a vector of program-name and args. */
51
52 int
53 create_inferior (char *program, char **allargs)
54 {
55 int pid;
56
57 pid = fork ();
58 if (pid < 0)
59 perror_with_name ("fork");
60
61 if (pid == 0)
62 {
63 ptrace (PTRACE_TRACEME);
64
65 execv (program, allargs);
66
67 fprintf (stderr, "Cannot exec %s: %s.\n", program,
68 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
69 fflush (stderr);
70 _exit (0177);
71 }
72
73 return pid;
74 }
75
76 /* Kill the inferior process. Make us have no inferior. */
77
78 void
79 kill_inferior (void)
80 {
81 if (inferior_pid == 0)
82 return;
83 ptrace (8, inferior_pid, 0, 0);
84 wait (0);
85 /*************inferior_died ();****VK**************/
86 }
87
88 /* Return nonzero if the given thread is still alive. */
89 int
90 mythread_alive (int pid)
91 {
92 return 1;
93 }
94
95 /* Wait for process, returns status */
96
97 unsigned char
98 mywait (char *status)
99 {
100 int pid;
101 union wait w;
102
103 pid = wait (&w);
104 if (pid != inferior_pid)
105 perror_with_name ("wait");
106
107 if (WIFEXITED (w))
108 {
109 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
110 *status = 'W';
111 return ((unsigned char) WEXITSTATUS (w));
112 }
113 else if (!WIFSTOPPED (w))
114 {
115 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
116 *status = 'X';
117 return ((unsigned char) WTERMSIG (w));
118 }
119
120 fetch_inferior_registers (0);
121
122 *status = 'T';
123 return ((unsigned char) WSTOPSIG (w));
124 }
125
126 /* Resume execution of the inferior process.
127 If STEP is nonzero, single-step it.
128 If SIGNAL is nonzero, give it that signal. */
129
130 void
131 myresume (int step, int signal)
132 {
133 errno = 0;
134 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
135 if (errno)
136 perror_with_name ("ptrace");
137 }
138
139 /* Fetch one or more registers from the inferior. REGNO == -1 to get
140 them all. We actually fetch more than requested, when convenient,
141 marking them as valid so we won't fetch them again. */
142
143 void
144 fetch_inferior_registers (int ignored)
145 {
146 struct regs inferior_registers;
147 struct fp_status inferior_fp_registers;
148
149 ptrace (PTRACE_GETREGS, inferior_pid,
150 (PTRACE_ARG3_TYPE) & inferior_registers);
151 #ifdef FP0_REGNUM
152 ptrace (PTRACE_GETFPREGS, inferior_pid,
153 (PTRACE_ARG3_TYPE) & inferior_fp_registers);
154 #endif
155
156 memcpy (registers, &inferior_registers, 16 * 4);
157 #ifdef FP0_REGNUM
158 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
159 sizeof inferior_fp_registers.fps_regs);
160 #endif
161 *(int *) &registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
162 *(int *) &registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
163 #ifdef FP0_REGNUM
164 memcpy
165 (&registers[REGISTER_BYTE (FPC_REGNUM)],
166 &inferior_fp_registers.fps_control,
167 sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
168 #endif
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 (int ignored)
177 {
178 struct regs inferior_registers;
179 struct fp_status inferior_fp_registers;
180
181 memcpy (&inferior_registers, registers, 16 * 4);
182 #ifdef FP0_REGNUM
183 memcpy (&inferior_fp_registers,
184 &registers[REGISTER_BYTE (FP0_REGNUM)],
185 sizeof inferior_fp_registers.fps_regs);
186 #endif
187 inferior_registers.r_ps = *(int *) &registers[REGISTER_BYTE (PS_REGNUM)];
188 inferior_registers.r_pc = *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
189
190 #ifdef FP0_REGNUM
191 memcpy (&inferior_fp_registers.fps_control,
192 &registers[REGISTER_BYTE (FPC_REGNUM)],
193 (sizeof inferior_fp_registers
194 - sizeof inferior_fp_registers.fps_regs));
195 #endif
196
197 ptrace (PTRACE_SETREGS, inferior_pid,
198 (PTRACE_ARG3_TYPE) & inferior_registers);
199 #if FP0_REGNUM
200 ptrace (PTRACE_SETFPREGS, inferior_pid,
201 (PTRACE_ARG3_TYPE) & inferior_fp_registers);
202 #endif
203 }
204
205 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
206 in the NEW_SUN_PTRACE case.
207 It ought to be straightforward. But it appears that writing did
208 not write the data that I specified. I cannot understand where
209 it got the data that it actually did write. */
210
211 /* Copy LEN bytes from inferior's memory starting at MEMADDR
212 to debugger memory starting at MYADDR. */
213
214 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
215 {
216 register int i;
217 /* Round starting address down to longword boundary. */
218 register CORE_ADDR addr = memaddr & -sizeof (int);
219 /* Round ending address up; get number of longwords that makes. */
220 register int count
221 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
222 /* Allocate buffer of that many longwords. */
223 register int *buffer = (int *) alloca (count * sizeof (int));
224
225 /* Read all the longwords */
226 for (i = 0; i < count; i++, addr += sizeof (int))
227 {
228 buffer[i] = ptrace (1, inferior_pid, addr, 0);
229 }
230
231 /* Copy appropriate bytes out of the buffer. */
232 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
233 }
234
235 /* Copy LEN bytes of data from debugger memory at MYADDR
236 to inferior's memory at MEMADDR.
237 On failure (cannot write the inferior)
238 returns the value of errno. */
239
240 int
241 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
242 {
243 register int i;
244 /* Round starting address down to longword boundary. */
245 register CORE_ADDR addr = memaddr & -sizeof (int);
246 /* Round ending address up; get number of longwords that makes. */
247 register int count
248 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
249 /* Allocate buffer of that many longwords. */
250 register int *buffer = (int *) alloca (count * sizeof (int));
251 extern int errno;
252
253 /* Fill start and end extra bytes of buffer with existing memory data. */
254
255 buffer[0] = ptrace (1, inferior_pid, addr, 0);
256
257 if (count > 1)
258 {
259 buffer[count - 1]
260 = ptrace (1, inferior_pid,
261 addr + (count - 1) * sizeof (int), 0);
262 }
263
264 /* Copy data to be written over corresponding part of buffer */
265
266 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
267
268 /* Write the entire buffer. */
269
270 for (i = 0; i < count; i++, addr += sizeof (int))
271 {
272 errno = 0;
273 ptrace (4, inferior_pid, addr, buffer[i]);
274 if (errno)
275 return errno;
276 }
277
278 return 0;
279 }
280 \f
281 void
282 initialize_low (void)
283 {
284 }
This page took 0.059235 seconds and 4 git commands to generate.