This commit was generated by cvs2svn to track changes on a CVS vendor
[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()
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()
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 (program, allargs)
123 char *program;
124 char **allargs;
125 {
126 int pid;
127
128 pid = fork ();
129 if (pid < 0)
130 perror_with_name ("fork");
131
132 if (pid == 0)
133 {
134 ptrace (PT_TRACE_ME, 0, 0, 0);
135
136 execv (program, allargs);
137
138 fprintf (stderr, "Cannot exec %s: %s.\n", program,
139 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
140 fflush (stderr);
141 _exit (0177);
142 }
143
144 return pid;
145 }
146
147 /* Kill the inferior process. Make us have no inferior. */
148
149 void
150 kill_inferior ()
151 {
152 if (inferior_pid == 0)
153 return;
154 ptrace (PT_KILL, inferior_pid, 0, 0);
155 wait (0);
156 /*************inferior_died ();****VK**************/
157 }
158
159 /* Return nonzero if the given thread is still alive. */
160 int
161 mythread_alive (pid)
162 int pid;
163 {
164 return 1;
165 }
166
167 /* Wait for process, returns status */
168
169 unsigned char
170 mywait (status)
171 char *status;
172 {
173 int pid;
174 int w;
175
176 pid = wait (&w);
177 if (pid != inferior_pid)
178 perror_with_name ("wait");
179
180 if (WIFEXITED (w))
181 {
182 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
183 *status = 'W';
184 return ((unsigned char) WEXITSTATUS (w));
185 }
186 else if (!WIFSTOPPED (w))
187 {
188 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
189 *status = 'X';
190 return ((unsigned char) WTERMSIG (w));
191 }
192
193 fetch_inferior_registers (0);
194
195 *status = 'T';
196 return ((unsigned char) WSTOPSIG (w));
197 }
198
199 /* Resume execution of the inferior process.
200 If STEP is nonzero, single-step it.
201 If SIGNAL is nonzero, give it that signal. */
202
203 void
204 myresume (step, signal)
205 int step;
206 int signal;
207 {
208 errno = 0;
209 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid,
210 (PTRACE_ARG3_TYPE) 1, signal);
211 if (errno)
212 perror_with_name ("ptrace");
213 }
214
215
216 #ifdef __i386__
217 /* Fetch one or more registers from the inferior. REGNO == -1 to get
218 them all. We actually fetch more than requested, when convenient,
219 marking them as valid so we won't fetch them again. */
220
221 void
222 fetch_inferior_registers (ignored)
223 int ignored;
224 {
225 struct reg inferior_registers;
226 struct env387 inferior_fp_registers;
227
228 ptrace (PT_GETREGS, inferior_pid,
229 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
230 ptrace (PT_GETFPREGS, inferior_pid,
231 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
232
233 RF ( 0, inferior_registers.r_eax);
234 RF ( 1, inferior_registers.r_ecx);
235 RF ( 2, inferior_registers.r_edx);
236 RF ( 3, inferior_registers.r_ebx);
237 RF ( 4, inferior_registers.r_esp);
238 RF ( 5, inferior_registers.r_ebp);
239 RF ( 6, inferior_registers.r_esi);
240 RF ( 7, inferior_registers.r_edi);
241 RF ( 8, inferior_registers.r_eip);
242 RF ( 9, inferior_registers.r_eflags);
243 RF (10, inferior_registers.r_cs);
244 RF (11, inferior_registers.r_ss);
245 RF (12, inferior_registers.r_ds);
246 RF (13, inferior_registers.r_es);
247 RF (14, inferior_registers.r_fs);
248 RF (15, inferior_registers.r_gs);
249
250 RF (FP0_REGNUM, inferior_fpregisters.regs[0]);
251 RF (FP0_REGNUM + 1, inferior_fpregisters.regs[1]);
252 RF (FP0_REGNUM + 2, inferior_fpregisters.regs[2]);
253 RF (FP0_REGNUM + 3, inferior_fpregisters.regs[3]);
254 RF (FP0_REGNUM + 4, inferior_fpregisters.regs[4]);
255 RF (FP0_REGNUM + 5, inferior_fpregisters.regs[5]);
256 RF (FP0_REGNUM + 6, inferior_fpregisters.regs[6]);
257 RF (FP0_REGNUM + 7, inferior_fpregisters.regs[7]);
258
259 RF (FCTRL_REGNUM, inferior_fpregisters.control);
260 RF (FSTAT_REGNUM, inferior_fpregisters.status);
261 RF (FTAG_REGNUM, inferior_fpregisters.tag);
262 RF (FCS_REGNUM, inferior_fpregisters.code_seg);
263 RF (FCOFF_REGNUM, inferior_fpregisters.eip);
264 RF (FDS_REGNUM, inferior_fpregisters.operand_seg);
265 RF (FDOFF_REGNUM, inferior_fpregisters.operand);
266 RF (FOP_REGNUM, inferior_fpregisters.opcode);
267 }
268
269 /* Store our register values back into the inferior.
270 If REGNO is -1, do this for all registers.
271 Otherwise, REGNO specifies which register (so we can save time). */
272
273 void
274 store_inferior_registers (ignored)
275 int ignored;
276 {
277 struct reg inferior_registers;
278 struct env387 inferior_fp_registers;
279
280 RS ( 0, inferior_registers.r_eax);
281 RS ( 1, inferior_registers.r_ecx);
282 RS ( 2, inferior_registers.r_edx);
283 RS ( 3, inferior_registers.r_ebx);
284 RS ( 4, inferior_registers.r_esp);
285 RS ( 5, inferior_registers.r_ebp);
286 RS ( 6, inferior_registers.r_esi);
287 RS ( 7, inferior_registers.r_edi);
288 RS ( 8, inferior_registers.r_eip);
289 RS ( 9, inferior_registers.r_eflags);
290 RS (10, inferior_registers.r_cs);
291 RS (11, inferior_registers.r_ss);
292 RS (12, inferior_registers.r_ds);
293 RS (13, inferior_registers.r_es);
294 RS (14, inferior_registers.r_fs);
295 RS (15, inferior_registers.r_gs);
296
297 RS (FP0_REGNUM, inferior_fpregisters.regs[0]);
298 RS (FP0_REGNUM + 1, inferior_fpregisters.regs[1]);
299 RS (FP0_REGNUM + 2, inferior_fpregisters.regs[2]);
300 RS (FP0_REGNUM + 3, inferior_fpregisters.regs[3]);
301 RS (FP0_REGNUM + 4, inferior_fpregisters.regs[4]);
302 RS (FP0_REGNUM + 5, inferior_fpregisters.regs[5]);
303 RS (FP0_REGNUM + 6, inferior_fpregisters.regs[6]);
304 RS (FP0_REGNUM + 7, inferior_fpregisters.regs[7]);
305
306 RS (FCTRL_REGNUM, inferior_fpregisters.control);
307 RS (FSTAT_REGNUM, inferior_fpregisters.status);
308 RS (FTAG_REGNUM, inferior_fpregisters.tag);
309 RS (FCS_REGNUM, inferior_fpregisters.code_seg);
310 RS (FCOFF_REGNUM, inferior_fpregisters.eip);
311 RS (FDS_REGNUM, inferior_fpregisters.operand_seg);
312 RS (FDOFF_REGNUM, inferior_fpregisters.operand);
313 RS (FOP_REGNUM, inferior_fpregisters.opcode);
314
315 ptrace (PT_SETREGS, inferior_pid,
316 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
317 ptrace (PT_SETFPREGS, inferior_pid,
318 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
319 }
320 #endif /* !__i386__ */
321
322 #ifdef __powerpc__
323 /* Fetch one or more registers from the inferior. REGNO == -1 to get
324 them all. We actually fetch more than requested, when convenient,
325 marking them as valid so we won't fetch them again. */
326
327 void
328 fetch_inferior_registers (regno)
329 int regno;
330 {
331 struct reg inferior_registers;
332 struct fpreg inferior_fp_registers;
333 int i;
334
335 ptrace (PT_GETREGS, inferior_pid,
336 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
337 ptrace (PT_GETFPREGS, inferior_pid,
338 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
339
340 for (i = 0; i < 32; i++)
341 RF (i, inferior_registers.fixreg[i]);
342 RF (LR_REGNUM, inferior_registers.lr);
343 RF (CR_REGNUM, inferior_registers.cr);
344 RF (XER_REGNUM, inferior_registers.xer);
345 RF (CTR_REGNUM, inferior_registers.ctr);
346 RF (PC_REGNUM, inferior_registers.pc);
347
348 for (i = 0; i < 32; i++)
349 RF (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
350 }
351
352 /* Store our register values back into the inferior.
353 If REGNO is -1, do this for all registers.
354 Otherwise, REGNO specifies which register (so we can save time). */
355
356 void
357 store_inferior_registers (regno)
358 int regno;
359 {
360 struct reg inferior_registers;
361 struct fpreg inferior_fp_registers;
362 int i;
363
364 for (i = 0; i < 32; i++)
365 RS (i, inferior_registers.fixreg[i]);
366 RS (LR_REGNUM, inferior_registers.lr);
367 RS (CR_REGNUM, inferior_registers.cr);
368 RS (XER_REGNUM, inferior_registers.xer);
369 RS (CTR_REGNUM, inferior_registers.ctr);
370 RS (PC_REGNUM, inferior_registers.pc);
371
372 for (i = 0; i < 32; i++)
373 RS (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
374
375 ptrace (PT_SETREGS, inferior_pid,
376 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
377 ptrace (PT_SETFPREGS, inferior_pid,
378 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
379 }
380 #endif /* !__powerpc__ */
381
382 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
383 in the NEW_SUN_PTRACE case.
384 It ought to be straightforward. But it appears that writing did
385 not write the data that I specified. I cannot understand where
386 it got the data that it actually did write. */
387
388 /* Copy LEN bytes from inferior's memory starting at MEMADDR
389 to debugger memory starting at MYADDR. */
390
391 read_inferior_memory (memaddr, myaddr, len)
392 CORE_ADDR memaddr;
393 char *myaddr;
394 int len;
395 {
396 register int i;
397 /* Round starting address down to longword boundary. */
398 register CORE_ADDR addr = memaddr & -sizeof (int);
399 /* Round ending address up; get number of longwords that makes. */
400 register int count
401 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
402 /* Allocate buffer of that many longwords. */
403 register int *buffer = (int *) alloca (count * sizeof (int));
404
405 /* Read all the longwords */
406 for (i = 0; i < count; i++, addr += sizeof (int))
407 {
408 buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
409 }
410
411 /* Copy appropriate bytes out of the buffer. */
412 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
413 }
414
415 /* Copy LEN bytes of data from debugger memory at MYADDR
416 to inferior's memory at MEMADDR.
417 On failure (cannot write the inferior)
418 returns the value of errno. */
419
420 int
421 write_inferior_memory (memaddr, myaddr, len)
422 CORE_ADDR memaddr;
423 char *myaddr;
424 int len;
425 {
426 register int i;
427 /* Round starting address down to longword boundary. */
428 register CORE_ADDR addr = memaddr & -sizeof (int);
429 /* Round ending address up; get number of longwords that makes. */
430 register int count
431 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
432 /* Allocate buffer of that many longwords. */
433 register int *buffer = (int *) alloca (count * sizeof (int));
434 extern int errno;
435
436 /* Fill start and end extra bytes of buffer with existing memory data. */
437
438 buffer[0] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
439
440 if (count > 1)
441 {
442 buffer[count - 1]
443 = ptrace (PT_READ_D, inferior_pid,
444 (PTRACE_ARG3_TYPE) addr + (count - 1) * sizeof (int), 0);
445 }
446
447 /* Copy data to be written over corresponding part of buffer */
448
449 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
450
451 /* Write the entire buffer. */
452
453 for (i = 0; i < count; i++, addr += sizeof (int))
454 {
455 errno = 0;
456 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
457 if (errno)
458 return errno;
459 }
460
461 return 0;
462 }
463 \f
464 void
465 initialize_low ()
466 {
467 initialize_arch ();
468 }