* gdbserver/low-hppabsd.c (buf2, environ, quit, quit_flag):
[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 static char my_registers[REGISTER_BYTES];
31 char *registers = my_registers;
32 /***************End MY defs*********************/
33
34 #include <sys/ptrace.h>
35 #include <machine/reg.h>
36
37 // extern int sys_nerr;
38 // extern char **sys_errlist;
39 extern int inferior_pid;
40 void perror_with_name ();
41
42 #define RF(dst, src) \
43 memcpy(&registers[REGISTER_BYTE(dst)], &src, sizeof(src))
44
45 #define RS(src, dst) \
46 memcpy(&dst, &registers[REGISTER_BYTE(src)], sizeof(dst))
47
48 #ifdef __i386__
49 struct env387
50 {
51 unsigned short control;
52 unsigned short r0;
53 unsigned short status;
54 unsigned short r1;
55 unsigned short tag;
56 unsigned short r2;
57 unsigned long eip;
58 unsigned short code_seg;
59 unsigned short opcode;
60 unsigned long operand;
61 unsigned short operand_seg;
62 unsigned short r3;
63 unsigned char regs[8][10];
64 };
65
66 /* i386_register_raw_size[i] is the number of bytes of storage in the
67 actual machine representation for register i. */
68 int i386_register_raw_size[MAX_NUM_REGS] = {
69 4, 4, 4, 4,
70 4, 4, 4, 4,
71 4, 4, 4, 4,
72 4, 4, 4, 4,
73 10, 10, 10, 10,
74 10, 10, 10, 10,
75 4, 4, 4, 4,
76 4, 4, 4, 4,
77 16, 16, 16, 16,
78 16, 16, 16, 16,
79 4
80 };
81
82 int i386_register_byte[MAX_NUM_REGS];
83
84 static void
85 initialize_arch (void)
86 {
87 /* Initialize the table saying where each register starts in the
88 register file. */
89 {
90 int i, offset;
91
92 offset = 0;
93 for (i = 0; i < MAX_NUM_REGS; i++)
94 {
95 i386_register_byte[i] = offset;
96 offset += i386_register_raw_size[i];
97 }
98 }
99 }
100 #endif /* !__i386__ */
101
102 #ifdef __m68k__
103 static void
104 initialize_arch (void)
105 {
106 }
107 #endif /* !__m68k__ */
108
109 #ifdef __ns32k__
110 static void
111 initialize_arch (void)
112 {
113 }
114 #endif /* !__ns32k__ */
115
116 #ifdef __powerpc__
117 #include "ppc-tdep.h"
118
119 static void
120 initialize_arch (void)
121 {
122 }
123 #endif /* !__powerpc__ */
124
125
126 /* Start an inferior process and returns its pid.
127 ALLARGS is a vector of program-name and args. */
128
129 int
130 create_inferior (char *program, char **allargs)
131 {
132 int pid;
133
134 pid = fork ();
135 if (pid < 0)
136 perror_with_name ("fork");
137
138 if (pid == 0)
139 {
140 ptrace (PT_TRACE_ME, 0, 0, 0);
141
142 execv (program, allargs);
143
144 fprintf (stderr, "Cannot exec %s: %s.\n", program,
145 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
146 fflush (stderr);
147 _exit (0177);
148 }
149
150 return pid;
151 }
152
153 /* Kill the inferior process. Make us have no inferior. */
154
155 void
156 kill_inferior (void)
157 {
158 if (inferior_pid == 0)
159 return;
160 ptrace (PT_KILL, inferior_pid, 0, 0);
161 wait (0);
162 /*************inferior_died ();****VK**************/
163 }
164
165 /* Return nonzero if the given thread is still alive. */
166 int
167 mythread_alive (int pid)
168 {
169 return 1;
170 }
171
172 /* Wait for process, returns status */
173
174 unsigned char
175 mywait (char *status)
176 {
177 int pid;
178 int w;
179
180 pid = wait (&w);
181 if (pid != inferior_pid)
182 perror_with_name ("wait");
183
184 if (WIFEXITED (w))
185 {
186 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
187 *status = 'W';
188 return ((unsigned char) WEXITSTATUS (w));
189 }
190 else if (!WIFSTOPPED (w))
191 {
192 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
193 *status = 'X';
194 return ((unsigned char) WTERMSIG (w));
195 }
196
197 fetch_inferior_registers (0);
198
199 *status = 'T';
200 return ((unsigned char) WSTOPSIG (w));
201 }
202
203 /* Resume execution of the inferior process.
204 If STEP is nonzero, single-step it.
205 If SIGNAL is nonzero, give it that signal. */
206
207 void
208 myresume (int step, int signal)
209 {
210 errno = 0;
211 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid,
212 (PTRACE_ARG3_TYPE) 1, signal);
213 if (errno)
214 perror_with_name ("ptrace");
215 }
216
217
218 #ifdef __i386__
219 /* Fetch one or more registers from the inferior. REGNO == -1 to get
220 them all. We actually fetch more than requested, when convenient,
221 marking them as valid so we won't fetch them again. */
222
223 void
224 fetch_inferior_registers (int ignored)
225 {
226 struct reg inferior_registers;
227 struct env387 inferior_fp_registers;
228
229 ptrace (PT_GETREGS, inferior_pid,
230 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
231 ptrace (PT_GETFPREGS, inferior_pid,
232 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0);
233
234 RF ( 0, inferior_registers.r_eax);
235 RF ( 1, inferior_registers.r_ecx);
236 RF ( 2, inferior_registers.r_edx);
237 RF ( 3, inferior_registers.r_ebx);
238 RF ( 4, inferior_registers.r_esp);
239 RF ( 5, inferior_registers.r_ebp);
240 RF ( 6, inferior_registers.r_esi);
241 RF ( 7, inferior_registers.r_edi);
242 RF ( 8, inferior_registers.r_eip);
243 RF ( 9, inferior_registers.r_eflags);
244 RF (10, inferior_registers.r_cs);
245 RF (11, inferior_registers.r_ss);
246 RF (12, inferior_registers.r_ds);
247 RF (13, inferior_registers.r_es);
248 RF (14, inferior_registers.r_fs);
249 RF (15, inferior_registers.r_gs);
250
251 RF (FP0_REGNUM, inferior_fp_registers.regs[0]);
252 RF (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
253 RF (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
254 RF (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
255 RF (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
256 RF (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
257 RF (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
258 RF (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
259
260 RF (FCTRL_REGNUM, inferior_fp_registers.control);
261 RF (FSTAT_REGNUM, inferior_fp_registers.status);
262 RF (FTAG_REGNUM, inferior_fp_registers.tag);
263 RF (FCS_REGNUM, inferior_fp_registers.code_seg);
264 RF (FCOFF_REGNUM, inferior_fp_registers.eip);
265 RF (FDS_REGNUM, inferior_fp_registers.operand_seg);
266 RF (FDOFF_REGNUM, inferior_fp_registers.operand);
267 RF (FOP_REGNUM, inferior_fp_registers.opcode);
268 }
269
270 /* Store our register values back into the inferior.
271 If REGNO is -1, do this for all registers.
272 Otherwise, REGNO specifies which register (so we can save time). */
273
274 void
275 store_inferior_registers (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_fp_registers.regs[0]);
298 RS (FP0_REGNUM + 1, inferior_fp_registers.regs[1]);
299 RS (FP0_REGNUM + 2, inferior_fp_registers.regs[2]);
300 RS (FP0_REGNUM + 3, inferior_fp_registers.regs[3]);
301 RS (FP0_REGNUM + 4, inferior_fp_registers.regs[4]);
302 RS (FP0_REGNUM + 5, inferior_fp_registers.regs[5]);
303 RS (FP0_REGNUM + 6, inferior_fp_registers.regs[6]);
304 RS (FP0_REGNUM + 7, inferior_fp_registers.regs[7]);
305
306 RS (FCTRL_REGNUM, inferior_fp_registers.control);
307 RS (FSTAT_REGNUM, inferior_fp_registers.status);
308 RS (FTAG_REGNUM, inferior_fp_registers.tag);
309 RS (FCS_REGNUM, inferior_fp_registers.code_seg);
310 RS (FCOFF_REGNUM, inferior_fp_registers.eip);
311 RS (FDS_REGNUM, inferior_fp_registers.operand_seg);
312 RS (FDOFF_REGNUM, inferior_fp_registers.operand);
313 RS (FOP_REGNUM, inferior_fp_registers.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 __m68k__
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 (int regno)
329 {
330 struct reg inferior_registers;
331 struct fpreg inferior_fp_registers;
332
333 ptrace (PT_GETREGS, inferior_pid,
334 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
335 memcpy (&registers[REGISTER_BYTE (0)], &inferior_registers,
336 sizeof (inferior_registers));
337
338 ptrace (PT_GETFPREGS, inferior_pid,
339 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
340 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
341 sizeof (inferior_fp_registers));
342 }
343
344 /* Store our register values back into the inferior.
345 If REGNO is -1, do this for all registers.
346 Otherwise, REGNO specifies which register (so we can save time). */
347
348 void
349 store_inferior_registers (int regno)
350 {
351 struct reg inferior_registers;
352 struct fpreg inferior_fp_registers;
353
354 memcpy (&inferior_registers, &registers[REGISTER_BYTE (0)],
355 sizeof (inferior_registers));
356 ptrace (PT_SETREGS, inferior_pid,
357 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
358
359 memcpy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
360 sizeof (inferior_fp_registers));
361 ptrace (PT_SETFPREGS, inferior_pid,
362 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
363 }
364 #endif /* !__m68k__ */
365
366
367 #ifdef __ns32k__
368 /* Fetch one or more registers from the inferior. REGNO == -1 to get
369 them all. We actually fetch more than requested, when convenient,
370 marking them as valid so we won't fetch them again. */
371
372 void
373 fetch_inferior_registers (int regno)
374 {
375 struct reg inferior_registers;
376 struct fpreg inferior_fpregisters;
377
378 ptrace (PT_GETREGS, inferior_pid,
379 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
380 ptrace (PT_GETFPREGS, inferior_pid,
381 (PTRACE_ARG3_TYPE) & inferior_fpregisters, 0);
382
383 RF (R0_REGNUM + 0, inferior_registers.r_r0);
384 RF (R0_REGNUM + 1, inferior_registers.r_r1);
385 RF (R0_REGNUM + 2, inferior_registers.r_r2);
386 RF (R0_REGNUM + 3, inferior_registers.r_r3);
387 RF (R0_REGNUM + 4, inferior_registers.r_r4);
388 RF (R0_REGNUM + 5, inferior_registers.r_r5);
389 RF (R0_REGNUM + 6, inferior_registers.r_r6);
390 RF (R0_REGNUM + 7, inferior_registers.r_r7);
391
392 RF (SP_REGNUM, inferior_registers.r_sp);
393 RF (FP_REGNUM, inferior_registers.r_fp);
394 RF (PC_REGNUM, inferior_registers.r_pc);
395 RF (PS_REGNUM, inferior_registers.r_psr);
396
397 RF (FPS_REGNUM, inferior_fpregisters.r_fsr);
398 RF (FP0_REGNUM + 0, inferior_fpregisters.r_freg[0]);
399 RF (FP0_REGNUM + 2, inferior_fpregisters.r_freg[2]);
400 RF (FP0_REGNUM + 4, inferior_fpregisters.r_freg[4]);
401 RF (FP0_REGNUM + 6, inferior_fpregisters.r_freg[6]);
402 RF (LP0_REGNUM + 1, inferior_fpregisters.r_freg[1]);
403 RF (LP0_REGNUM + 3, inferior_fpregisters.r_freg[3]);
404 RF (LP0_REGNUM + 5, inferior_fpregisters.r_freg[5]);
405 RF (LP0_REGNUM + 7, inferior_fpregisters.r_freg[7]);
406 }
407
408 /* Store our register values back into the inferior.
409 If REGNO is -1, do this for all registers.
410 Otherwise, REGNO specifies which register (so we can save time). */
411
412 void
413 store_inferior_registers (int regno)
414 {
415 struct reg inferior_registers;
416 struct fpreg inferior_fpregisters;
417
418 RS (R0_REGNUM + 0, inferior_registers.r_r0);
419 RS (R0_REGNUM + 1, inferior_registers.r_r1);
420 RS (R0_REGNUM + 2, inferior_registers.r_r2);
421 RS (R0_REGNUM + 3, inferior_registers.r_r3);
422 RS (R0_REGNUM + 4, inferior_registers.r_r4);
423 RS (R0_REGNUM + 5, inferior_registers.r_r5);
424 RS (R0_REGNUM + 6, inferior_registers.r_r6);
425 RS (R0_REGNUM + 7, inferior_registers.r_r7);
426
427 RS (SP_REGNUM, inferior_registers.r_sp);
428 RS (FP_REGNUM, inferior_registers.r_fp);
429 RS (PC_REGNUM, inferior_registers.r_pc);
430 RS (PS_REGNUM, inferior_registers.r_psr);
431
432 RS (FPS_REGNUM, inferior_fpregisters.r_fsr);
433 RS (FP0_REGNUM + 0, inferior_fpregisters.r_freg[0]);
434 RS (FP0_REGNUM + 2, inferior_fpregisters.r_freg[2]);
435 RS (FP0_REGNUM + 4, inferior_fpregisters.r_freg[4]);
436 RS (FP0_REGNUM + 6, inferior_fpregisters.r_freg[6]);
437 RS (LP0_REGNUM + 1, inferior_fpregisters.r_freg[1]);
438 RS (LP0_REGNUM + 3, inferior_fpregisters.r_freg[3]);
439 RS (LP0_REGNUM + 5, inferior_fpregisters.r_freg[5]);
440 RS (LP0_REGNUM + 7, inferior_fpregisters.r_freg[7]);
441
442 ptrace (PT_SETREGS, inferior_pid,
443 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
444 ptrace (PT_SETFPREGS, inferior_pid,
445 (PTRACE_ARG3_TYPE) & inferior_fpregisters, 0);
446
447 }
448 #endif /* !__ns32k__ */
449
450 #ifdef __powerpc__
451 /* Fetch one or more registers from the inferior. REGNO == -1 to get
452 them all. We actually fetch more than requested, when convenient,
453 marking them as valid so we won't fetch them again. */
454
455 void
456 fetch_inferior_registers (int regno)
457 {
458 struct reg inferior_registers;
459 #ifdef PT_GETFPREGS
460 struct fpreg inferior_fp_registers;
461 #endif
462 int i;
463
464 ptrace (PT_GETREGS, inferior_pid,
465 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
466 for (i = 0; i < 32; i++)
467 RF (i, inferior_registers.fixreg[i]);
468 RF (PPC_LR_REGNUM, inferior_registers.lr);
469 RF (PPC_CR_REGNUM, inferior_registers.cr);
470 RF (PPC_XER_REGNUM, inferior_registers.xer);
471 RF (PPC_CTR_REGNUM, inferior_registers.ctr);
472 RF (PC_REGNUM, inferior_registers.pc);
473
474 #ifdef PT_GETFPREGS
475 ptrace (PT_GETFPREGS, inferior_pid,
476 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
477 for (i = 0; i < 32; i++)
478 RF (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
479 #endif
480 }
481
482 /* Store our register values back into the inferior.
483 If REGNO is -1, do this for all registers.
484 Otherwise, REGNO specifies which register (so we can save time). */
485
486 void
487 store_inferior_registers (int regno)
488 {
489 struct reg inferior_registers;
490 #ifdef PT_SETFPREGS
491 struct fpreg inferior_fp_registers;
492 #endif
493 int i;
494
495 for (i = 0; i < 32; i++)
496 RS (i, inferior_registers.fixreg[i]);
497 RS (PPC_LR_REGNUM, inferior_registers.lr);
498 RS (PPC_CR_REGNUM, inferior_registers.cr);
499 RS (PPC_XER_REGNUM, inferior_registers.xer);
500 RS (PPC_CTR_REGNUM, inferior_registers.ctr);
501 RS (PC_REGNUM, inferior_registers.pc);
502 ptrace (PT_SETREGS, inferior_pid,
503 (PTRACE_ARG3_TYPE) & inferior_registers, 0);
504
505 #ifdef PT_SETFPREGS
506 for (i = 0; i < 32; i++)
507 RS (FP0_REGNUM + i, inferior_fp_registers.r_regs[i]);
508 ptrace (PT_SETFPREGS, inferior_pid,
509 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0);
510 #endif
511 }
512 #endif /* !__powerpc__ */
513
514 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
515 in the NEW_SUN_PTRACE case.
516 It ought to be straightforward. But it appears that writing did
517 not write the data that I specified. I cannot understand where
518 it got the data that it actually did write. */
519
520 /* Copy LEN bytes from inferior's memory starting at MEMADDR
521 to debugger memory starting at MYADDR. */
522
523 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
524 {
525 register int i;
526 /* Round starting address down to longword boundary. */
527 register CORE_ADDR addr = memaddr & -sizeof (int);
528 /* Round ending address up; get number of longwords that makes. */
529 register int count
530 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
531 /* Allocate buffer of that many longwords. */
532 register int *buffer = (int *) alloca (count * sizeof (int));
533
534 /* Read all the longwords */
535 for (i = 0; i < count; i++, addr += sizeof (int))
536 {
537 buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
538 }
539
540 /* Copy appropriate bytes out of the buffer. */
541 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
542 }
543
544 /* Copy LEN bytes of data from debugger memory at MYADDR
545 to inferior's memory at MEMADDR.
546 On failure (cannot write the inferior)
547 returns the value of errno. */
548
549 int
550 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
551 {
552 register int i;
553 /* Round starting address down to longword boundary. */
554 register CORE_ADDR addr = memaddr & -sizeof (int);
555 /* Round ending address up; get number of longwords that makes. */
556 register int count
557 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
558 /* Allocate buffer of that many longwords. */
559 register int *buffer = (int *) alloca (count * sizeof (int));
560 extern int errno;
561
562 /* Fill start and end extra bytes of buffer with existing memory data. */
563
564 buffer[0] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
565
566 if (count > 1)
567 {
568 buffer[count - 1]
569 = ptrace (PT_READ_D, inferior_pid,
570 (PTRACE_ARG3_TYPE) addr + (count - 1) * sizeof (int), 0);
571 }
572
573 /* Copy data to be written over corresponding part of buffer */
574
575 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
576
577 /* Write the entire buffer. */
578
579 for (i = 0; i < count; i++, addr += sizeof (int))
580 {
581 errno = 0;
582 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
583 if (errno)
584 return errno;
585 }
586
587 return 0;
588 }
589 \f
590 void
591 initialize_low (void)
592 {
593 initialize_arch ();
594 }