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