Protoization.
[deliverable/binutils-gdb.git] / gdb / gdbserver / low-nbsd.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993, 2000 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/types.h>
22 #include <sys/wait.h>
23 #include "frame.h"
24 #include "inferior.h"
25
26 #include <stdio.h>
27 #include <errno.h>
28
29 /***************Begin MY defs*********************/
30 int quit_flag = 0;
31 static char my_registers[REGISTER_BYTES];
32 char *registers = my_registers;
33
34 /* Index within `registers' of the first byte of the space for
35 register N. */
36
37 char buf2[MAX_REGISTER_RAW_SIZE];
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 char **environ;
46 extern int inferior_pid;
47 void quit (), perror_with_name ();
48
49 #define RF(dst, src) \
50 memcpy(&registers[REGISTER_BYTE(dst)], &src, sizeof(src))
51
52 #define RS(src, dst) \
53 memcpy(&dst, &registers[REGISTER_BYTE(src)], sizeof(dst))
54
55 #ifdef __i386__
56 struct env387
57 {
58 unsigned short control;
59 unsigned short r0;
60 unsigned short status;
61 unsigned short r1;
62 unsigned short tag;
63 unsigned short r2;
64 unsigned long eip;
65 unsigned short code_seg;
66 unsigned short opcode;
67 unsigned long operand;
68 unsigned short operand_seg;
69 unsigned short r3;
70 unsigned char regs[8][10];
71 };
72
73 /* i386_register_raw_size[i] is the number of bytes of storage in the
74 actual machine representation for register i. */
75 int i386_register_raw_size[MAX_NUM_REGS] = {
76 4, 4, 4, 4,
77 4, 4, 4, 4,
78 4, 4, 4, 4,
79 4, 4, 4, 4,
80 10, 10, 10, 10,
81 10, 10, 10, 10,
82 4, 4, 4, 4,
83 4, 4, 4, 4,
84 16, 16, 16, 16,
85 16, 16, 16, 16,
86 4
87 };
88
89 int i386_register_byte[MAX_NUM_REGS];
90
91 static void
92 initialize_arch (void)
93 {
94 /* Initialize the table saying where each register starts in the
95 register file. */
96 {
97 int i, offset;
98
99 offset = 0;
100 for (i = 0; i < MAX_NUM_REGS; i++)
101 {
102 i386_register_byte[i] = offset;
103 offset += i386_register_raw_size[i];
104 }
105 }
106 }
107 #endif /* !__i386__ */
108
109 #ifdef __powerpc__
110 static void
111 initialize_arch (void)
112 {
113 }
114 #endif /* !__powerpc__ */
115
116
117 /* Start an inferior process and returns its pid.
118 ALLARGS is a vector of program-name and args.
119 ENV is the environment vector to pass. */
120
121 int
122 create_inferior (char *program, char **allargs)
123 {
124 int pid;
125
126 pid = fork ();
127 if (pid < 0)
128 perror_with_name ("fork");
129
130 if (pid == 0)
131 {
132 ptrace (PT_TRACE_ME, 0, 0, 0);
133
134 execv (program, allargs);
135
136 fprintf (stderr, "Cannot exec %s: %s.\n", program,
137 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
138 fflush (stderr);
139 _exit (0177);
140 }
141
142 return pid;
143 }
144
145 /* Kill the inferior process. Make us have no inferior. */
146
147 void
148 kill_inferior (void)
149 {
150 if (inferior_pid == 0)
151 return;
152 ptrace (PT_KILL, inferior_pid, 0, 0);
153 wait (0);
154 /*************inferior_died ();****VK**************/
155 }
156
157 /* Return nonzero if the given thread is still alive. */
158 int
159 mythread_alive (int pid)
160 {
161 return 1;
162 }
163
164 /* Wait for process, returns status */
165
166 unsigned char
167 mywait (char *status)
168 {
169 int pid;
170 int w;
171
172 pid = wait (&w);
173 if (pid != inferior_pid)
174 perror_with_name ("wait");
175
176 if (WIFEXITED (w))
177 {
178 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
179 *status = 'W';
180 return ((unsigned char) WEXITSTATUS (w));
181 }
182 else if (!WIFSTOPPED (w))
183 {
184 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
185 *status = 'X';
186 return ((unsigned char) WTERMSIG (w));
187 }
188
189 fetch_inferior_registers (0);
190
191 *status = 'T';
192 return ((unsigned char) WSTOPSIG (w));
193 }
194
195 /* Resume execution of the inferior process.
196 If STEP is nonzero, single-step it.
197 If SIGNAL is nonzero, give it that signal. */
198
199 void
200 myresume (int step, int signal)
201 {
202 errno = 0;
203 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid,
204 (PTRACE_ARG3_TYPE) 1, signal);
205 if (errno)
206 perror_with_name ("ptrace");
207 }
208
209
210 #ifdef __i386__
211 /* Fetch one or more registers from the inferior. REGNO == -1 to get
212 them all. We actually fetch more than requested, when convenient,
213 marking them as valid so we won't fetch them again. */
214
215 void
216 fetch_inferior_registers (int ignored)
217 {
218 struct reg inferior_registers;
219 struct env387 inferior_fp_registers;
220
221 ptrace (PT_GETREGS, inferior_pid,
222 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
223 ptrace (PT_GETFPREGS, inferior_pid,
224 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
225
226 RF ( 0, inferior_registers.r_eax);
227 RF ( 1, inferior_registers.r_ecx);
228 RF ( 2, inferior_registers.r_edx);
229 RF ( 3, inferior_registers.r_ebx);
230 RF ( 4, inferior_registers.r_esp);
231 RF ( 5, inferior_registers.r_ebp);
232 RF ( 6, inferior_registers.r_esi);
233 RF ( 7, inferior_registers.r_edi);
234 RF ( 8, inferior_registers.r_eip);
235 RF ( 9, inferior_registers.r_eflags);
236 RF (10, inferior_registers.r_cs);
237 RF (11, inferior_registers.r_ss);
238 RF (12, inferior_registers.r_ds);
239 RF (13, inferior_registers.r_es);
240 RF (14, inferior_registers.r_fs);
241 RF (15, inferior_registers.r_gs);
242
243 RF (FP0_REGNUM, inferior_fpregisters.regs[0]);
244 RF (FP0_REGNUM + 1, inferior_fpregisters.regs[1]);
245 RF (FP0_REGNUM + 2, inferior_fpregisters.regs[2]);
246 RF (FP0_REGNUM + 3, inferior_fpregisters.regs[3]);
247 RF (FP0_REGNUM + 4, inferior_fpregisters.regs[4]);
248 RF (FP0_REGNUM + 5, inferior_fpregisters.regs[5]);
249 RF (FP0_REGNUM + 6, inferior_fpregisters.regs[6]);
250 RF (FP0_REGNUM + 7, inferior_fpregisters.regs[7]);
251
252 RF (FCTRL_REGNUM, inferior_fpregisters.control);
253 RF (FSTAT_REGNUM, inferior_fpregisters.status);
254 RF (FTAG_REGNUM, inferior_fpregisters.tag);
255 RF (FCS_REGNUM, inferior_fpregisters.code_seg);
256 RF (FCOFF_REGNUM, inferior_fpregisters.eip);
257 RF (FDS_REGNUM, inferior_fpregisters.operand_seg);
258 RF (FDOFF_REGNUM, inferior_fpregisters.operand);
259 RF (FOP_REGNUM, inferior_fpregisters.opcode);
260 }
261
262 /* Store our register values back into the inferior.
263 If REGNO is -1, do this for all registers.
264 Otherwise, REGNO specifies which register (so we can save time). */
265
266 void
267 store_inferior_registers (int ignored)
268 {
269 struct reg inferior_registers;
270 struct env387 inferior_fp_registers;
271
272 RS ( 0, inferior_registers.r_eax);
273 RS ( 1, inferior_registers.r_ecx);
274 RS ( 2, inferior_registers.r_edx);
275 RS ( 3, inferior_registers.r_ebx);
276 RS ( 4, inferior_registers.r_esp);
277 RS ( 5, inferior_registers.r_ebp);
278 RS ( 6, inferior_registers.r_esi);
279 RS ( 7, inferior_registers.r_edi);
280 RS ( 8, inferior_registers.r_eip);
281 RS ( 9, inferior_registers.r_eflags);
282 RS (10, inferior_registers.r_cs);
283 RS (11, inferior_registers.r_ss);
284 RS (12, inferior_registers.r_ds);
285 RS (13, inferior_registers.r_es);
286 RS (14, inferior_registers.r_fs);
287 RS (15, inferior_registers.r_gs);
288
289 RS (FP0_REGNUM, inferior_fpregisters.regs[0]);
290 RS (FP0_REGNUM + 1, inferior_fpregisters.regs[1]);
291 RS (FP0_REGNUM + 2, inferior_fpregisters.regs[2]);
292 RS (FP0_REGNUM + 3, inferior_fpregisters.regs[3]);
293 RS (FP0_REGNUM + 4, inferior_fpregisters.regs[4]);
294 RS (FP0_REGNUM + 5, inferior_fpregisters.regs[5]);
295 RS (FP0_REGNUM + 6, inferior_fpregisters.regs[6]);
296 RS (FP0_REGNUM + 7, inferior_fpregisters.regs[7]);
297
298 RS (FCTRL_REGNUM, inferior_fpregisters.control);
299 RS (FSTAT_REGNUM, inferior_fpregisters.status);
300 RS (FTAG_REGNUM, inferior_fpregisters.tag);
301 RS (FCS_REGNUM, inferior_fpregisters.code_seg);
302 RS (FCOFF_REGNUM, inferior_fpregisters.eip);
303 RS (FDS_REGNUM, inferior_fpregisters.operand_seg);
304 RS (FDOFF_REGNUM, inferior_fpregisters.operand);
305 RS (FOP_REGNUM, inferior_fpregisters.opcode);
306
307 ptrace (PT_SETREGS, inferior_pid,
308 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
309 ptrace (PT_SETFPREGS, inferior_pid,
310 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
311 }
312 #endif /* !__i386__ */
313
314 #ifdef __powerpc__
315 /* Fetch one or more registers from the inferior. REGNO == -1 to get
316 them all. We actually fetch more than requested, when convenient,
317 marking them as valid so we won't fetch them again. */
318
319 void
320 fetch_inferior_registers (int regno)
321 {
322 struct reg inferior_registers;
323 struct fpreg inferior_fp_registers;
324 int i;
325
326 ptrace (PT_GETREGS, inferior_pid,
327 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
328 ptrace (PT_GETFPREGS, inferior_pid,
329 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
330
331 for (i = 0; i < 32; i++)
332 RF (i, inferior_registers.fixreg[i]);
333 RF (LR_REGNUM, inferior_registers.lr);
334 RF (CR_REGNUM, inferior_registers.cr);
335 RF (XER_REGNUM, inferior_registers.xer);
336 RF (CTR_REGNUM, inferior_registers.ctr);
337 RF (PC_REGNUM, inferior_registers.pc);
338
339 for (i = 0; i < 32; i++)
340 RF (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
341 }
342
343 /* Store our register values back into the inferior.
344 If REGNO is -1, do this for all registers.
345 Otherwise, REGNO specifies which register (so we can save time). */
346
347 void
348 store_inferior_registers (int regno)
349 {
350 struct reg inferior_registers;
351 struct fpreg inferior_fp_registers;
352 int i;
353
354 for (i = 0; i < 32; i++)
355 RS (i, inferior_registers.fixreg[i]);
356 RS (LR_REGNUM, inferior_registers.lr);
357 RS (CR_REGNUM, inferior_registers.cr);
358 RS (XER_REGNUM, inferior_registers.xer);
359 RS (CTR_REGNUM, inferior_registers.ctr);
360 RS (PC_REGNUM, inferior_registers.pc);
361
362 for (i = 0; i < 32; i++)
363 RS (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
364
365 ptrace (PT_SETREGS, inferior_pid,
366 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
367 ptrace (PT_SETFPREGS, inferior_pid,
368 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
369 }
370 #endif /* !__powerpc__ */
371
372 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
373 in the NEW_SUN_PTRACE case.
374 It ought to be straightforward. But it appears that writing did
375 not write the data that I specified. I cannot understand where
376 it got the data that it actually did write. */
377
378 /* Copy LEN bytes from inferior's memory starting at MEMADDR
379 to debugger memory starting at MYADDR. */
380
381 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
382 {
383 register int i;
384 /* Round starting address down to longword boundary. */
385 register CORE_ADDR addr = memaddr & -sizeof (int);
386 /* Round ending address up; get number of longwords that makes. */
387 register int count
388 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
389 /* Allocate buffer of that many longwords. */
390 register int *buffer = (int *) alloca (count * sizeof (int));
391
392 /* Read all the longwords */
393 for (i = 0; i < count; i++, addr += sizeof (int))
394 {
395 buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
396 }
397
398 /* Copy appropriate bytes out of the buffer. */
399 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
400 }
401
402 /* Copy LEN bytes of data from debugger memory at MYADDR
403 to inferior's memory at MEMADDR.
404 On failure (cannot write the inferior)
405 returns the value of errno. */
406
407 int
408 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
409 {
410 register int i;
411 /* Round starting address down to longword boundary. */
412 register CORE_ADDR addr = memaddr & -sizeof (int);
413 /* Round ending address up; get number of longwords that makes. */
414 register int count
415 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
416 /* Allocate buffer of that many longwords. */
417 register int *buffer = (int *) alloca (count * sizeof (int));
418 extern int errno;
419
420 /* Fill start and end extra bytes of buffer with existing memory data. */
421
422 buffer[0] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
423
424 if (count > 1)
425 {
426 buffer[count - 1]
427 = ptrace (PT_READ_D, inferior_pid,
428 (PTRACE_ARG3_TYPE) addr + (count - 1) * sizeof (int), 0);
429 }
430
431 /* Copy data to be written over corresponding part of buffer */
432
433 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
434
435 /* Write the entire buffer. */
436
437 for (i = 0; i < count; i++, addr += sizeof (int))
438 {
439 errno = 0;
440 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
441 if (errno)
442 return errno;
443 }
444
445 return 0;
446 }
447 \f
448 void
449 initialize_low (void)
450 {
451 initialize_arch ();
452 }
This page took 0.039164 seconds and 4 git commands to generate.