Clean up the routing and networking code before I parallelize routing.
[dragonfly.git] / gnu / usr.bin / binutils215 / gdbserver / low-fbsd.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2    Copyright (C) 1995 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  * $DragonFly: src/gnu/usr.bin/binutils215/gdbserver/Attic/low-fbsd.c,v 1.1 2004/12/20 13:14:44 asmodai Exp $
21  */
22
23 #include "defs.h"
24 #include <sys/wait.h>
25 #include "frame.h"
26 #include "inferior.h"
27
28 #include <stdio.h>
29 #include <sys/param.h>
30 #include <dirent.h>
31 #include <sys/user.h>
32 #include <signal.h>
33 #include <sys/ioctl.h>
34 #include <sgtty.h>
35 #include <fcntl.h>
36 #include <string.h>
37
38 /***************Begin MY defs*********************/
39 int quit_flag = 0;
40 char registers[REGISTER_BYTES];
41
42 /* Index within `registers' of the first byte of the space for
43    register N.  */
44
45
46 char buf2[MAX_REGISTER_RAW_SIZE];
47 /***************End MY defs*********************/
48
49 #include <sys/ptrace.h>
50 #include <machine/reg.h>
51
52 extern char **environ;
53 extern int inferior_pid;
54 void quit (), perror_with_name ();
55 int query ();
56
57 /* Start an inferior process and returns its pid.
58    ALLARGS is a vector of program-name and args.
59    ENV is the environment vector to pass.  */
60
61 int
62 create_inferior (program, allargs)
63      char *program;
64      char **allargs;
65 {
66   int pid;
67
68   pid = fork ();
69   if (pid < 0)
70     perror_with_name ("fork");
71
72   if (pid == 0)
73     {
74       ptrace (PT_TRACE_ME, 0, 0, 0);
75
76       execv (program, allargs);
77
78       fprintf (stderr, "Cannot exec %s: %s.\n", program, strerror(errno));
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 (PT_KILL, 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   int 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 ? PT_STEP : PT_CONTINUE, inferior_pid,
149           (PTRACE_ARG3_TYPE) 1, signal);
150   if (errno)
151     perror_with_name ("ptrace");
152 }
153
154 #if defined(__i386__)
155
156 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
157 /* symbols like 'tEAX' come from <machine/reg.h> */
158 static int tregmap[] =
159 {
160   tEAX, tECX, tEDX, tEBX,
161   tESP, tEBP, tESI, tEDI,
162   tEIP, tEFLAGS, tCS, tSS,
163   tDS, tES, tFS, tGS,
164 };
165
166 static struct save87 pcb_savefpu;
167
168 void
169 fetch_inferior_registers (regno)
170      int regno;
171 {
172   struct reg inferior_registers;        /* ptrace order, not gcc/gdb order */
173   int r;
174
175   ptrace (PT_GETREGS, inferior_pid,
176           (PTRACE_ARG3_TYPE) &inferior_registers, 0);
177
178   for (r = 0; r < NUM_REGS; r++)
179     memcpy (&registers[REGISTER_BYTE (r)], ((int *)&inferior_registers) + tregmap[r], 4);
180 }
181
182 void
183 store_inferior_registers (regno)
184      int regno;
185 {
186   struct reg inferior_registers;        /* ptrace order, not gcc/gdb order */
187   int r;
188
189   ptrace (PT_GETREGS, inferior_pid,
190           (PTRACE_ARG3_TYPE) &inferior_registers, 0);
191
192   for (r = 0; r < NUM_REGS; r++)
193     memcpy (((int *)&inferior_registers) + tregmap[r], &registers[REGISTER_BYTE (r)], 4);
194
195   ptrace (PT_SETREGS, inferior_pid,
196           (PTRACE_ARG3_TYPE) &inferior_registers, 0);
197 }
198
199 #elif defined(__alpha__)
200
201 void
202 fetch_inferior_registers (regno)
203      int regno;
204 {
205   struct reg regs;      /* ptrace order, not gcc/gdb order */
206   struct fpreg fpregs;
207   int r;
208
209   ptrace (PT_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
210   ptrace (PT_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fpregs, 0);
211
212   for (r = 0; r < 31; r++)
213     memcpy (&registers[REGISTER_BYTE (r)],
214             &regs.r_regs[r], sizeof(u_int64_t));
215   for (r = 0; r < 32; r++)
216     memcpy (&registers[REGISTER_BYTE (r + FP0_REGNUM)],
217             &fpregs.fpr_regs[r], sizeof(u_int64_t));
218   memcpy (&registers[REGISTER_BYTE (PC_REGNUM)],
219           &regs.r_regs[31], sizeof(u_int64_t));
220
221   memset (&registers[REGISTER_BYTE (ZERO_REGNUM)], 0, sizeof(u_int64_t));
222   memset (&registers[REGISTER_BYTE (FP_REGNUM)], 0, sizeof(u_int64_t));
223 }
224
225 void
226 store_inferior_registers (regno)
227      int regno;
228 {
229   struct reg regs;      /* ptrace order, not gcc/gdb order */
230   struct fpreg fpregs;
231   int r;
232
233   for (r = 0; r < 31; r++)
234     memcpy (&regs.r_regs[r],
235             &registers[REGISTER_BYTE (r)], sizeof(u_int64_t));
236   for (r = 0; r < 32; r++)
237     memcpy (&fpregs.fpr_regs[r],
238             &registers[REGISTER_BYTE (r + FP0_REGNUM)], sizeof(u_int64_t));
239   memcpy (&regs.r_regs[31],
240           &registers[REGISTER_BYTE (PC_REGNUM)], sizeof(u_int64_t));
241
242   ptrace (PT_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
243   ptrace (PT_SETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fpregs, 0);
244 }
245
246 #endif
247
248
249 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
250    in the NEW_SUN_PTRACE case.
251    It ought to be straightforward.  But it appears that writing did
252    not write the data that I specified.  I cannot understand where
253    it got the data that it actually did write.  */
254
255 /* Copy LEN bytes from inferior's memory starting at MEMADDR
256    to debugger memory starting at MYADDR.  */
257
258 read_inferior_memory (memaddr, myaddr, len)
259      CORE_ADDR memaddr;
260      char *myaddr;
261      int len;
262 {
263   register int i;
264   /* Round starting address down to longword boundary.  */
265   register CORE_ADDR addr = memaddr & -sizeof (int);
266   /* Round ending address up; get number of longwords that makes.  */
267   register int count
268   = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
269   /* Allocate buffer of that many longwords.  */
270   register int *buffer = (int *) alloca (count * sizeof (int));
271
272   /* Read all the longwords */
273   for (i = 0; i < count; i++, addr += sizeof (int))
274     {
275       buffer[i] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
276     }
277
278   /* Copy appropriate bytes out of the buffer.  */
279   memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
280 }
281
282 /* Copy LEN bytes of data from debugger memory at MYADDR
283    to inferior's memory at MEMADDR.
284    On failure (cannot write the inferior)
285    returns the value of errno.  */
286
287 int
288 write_inferior_memory (memaddr, myaddr, len)
289      CORE_ADDR memaddr;
290      char *myaddr;
291      int len;
292 {
293   register int i;
294   /* Round starting address down to longword boundary.  */
295   register CORE_ADDR addr = memaddr & -sizeof (int);
296   /* Round ending address up; get number of longwords that makes.  */
297   register int count
298   = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
299   /* Allocate buffer of that many longwords.  */
300   register int *buffer = (int *) alloca (count * sizeof (int));
301   extern int errno;
302
303   /* Fill start and end extra bytes of buffer with existing memory data.  */
304
305   buffer[0] = ptrace (PT_READ_I, inferior_pid,
306                       (PTRACE_ARG3_TYPE) addr, 0);
307
308   if (count > 1)
309     {
310       buffer[count - 1]
311         = ptrace (PT_READ_I, inferior_pid,
312                   (PTRACE_ARG3_TYPE) addr + (count - 1) * sizeof (int), 0);
313     }
314
315   /* Copy data to be written over corresponding part of buffer */
316
317   memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
318
319   /* Write the entire buffer.  */
320
321   for (i = 0; i < count; i++, addr += sizeof (int))
322     {
323       errno = 0;
324       ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
325       if (errno)
326         return errno;
327     }
328
329   return 0;
330 }
331 \f
332 void
333 initialize ()
334 {
335   inferior_pid = 0;
336 }
337
338 int
339 have_inferior_p ()
340 {
341   return inferior_pid != 0;
342 }