1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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,
19 Boston, MA 02111-1307, USA. */
27 #include <sys/param.h>
29 #include <sys/ptrace.h>
32 #include <sys/ioctl.h>
35 /***************Begin MY defs*********************/
37 static char my_registers
[REGISTER_BYTES
];
38 char *registers
= my_registers
;
40 /* Index within `registers' of the first byte of the space for
44 char buf2
[MAX_REGISTER_RAW_SIZE
];
45 /***************End MY defs*********************/
51 /* Default the type of the ptrace transfer to int. */
52 #ifndef PTRACE_XFER_TYPE
53 #define PTRACE_XFER_TYPE int
56 extern char **environ
;
58 extern int inferior_pid
;
59 void quit (), perror_with_name ();
62 static void initialize_arch (void);
64 /* Start an inferior process and returns its pid.
65 ALLARGS is a vector of program-name and args.
66 ENV is the environment vector to pass. */
69 create_inferior (char *program
, char **allargs
)
75 perror_with_name ("fork");
79 ptrace (PTRACE_TRACEME
, 0, 0, 0);
81 execv (program
, allargs
);
83 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
84 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
92 /* Kill the inferior process. Make us have no inferior. */
97 if (inferior_pid
== 0)
99 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
101 /*************inferior_died ();****VK**************/
104 /* Return nonzero if the given thread is still alive. */
106 mythread_alive (int pid
)
111 /* Wait for process, returns status */
114 mywait (char *status
)
120 if (pid
!= inferior_pid
)
121 perror_with_name ("wait");
125 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
127 return ((unsigned char) WEXITSTATUS (w
));
129 else if (!WIFSTOPPED (w
))
131 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
133 return ((unsigned char) WTERMSIG (w
));
136 fetch_inferior_registers (0);
139 return ((unsigned char) WSTOPSIG (w
));
142 /* Resume execution of the inferior process.
143 If STEP is nonzero, single-step it.
144 If SIGNAL is nonzero, give it that signal. */
147 myresume (int step
, int signal
)
150 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
152 perror_with_name ("ptrace");
156 #if !defined (offsetof)
157 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
160 /* U_REGS_OFFSET is the offset of the registers within the u area. */
161 #if !defined (U_REGS_OFFSET)
162 #define U_REGS_OFFSET \
163 ptrace (PT_READ_U, inferior_pid, \
164 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
168 #ifdef I386_GNULINUX_TARGET
169 /* i386_register_raw_size[i] is the number of bytes of storage in the
170 actual machine representation for register i. */
171 int i386_register_raw_size
[MAX_NUM_REGS
] = {
185 int i386_register_byte
[MAX_NUM_REGS
];
188 initialize_arch (void)
190 /* Initialize the table saying where each register starts in the
196 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
198 i386_register_byte
[i
] = offset
;
199 offset
+= i386_register_raw_size
[i
];
204 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
205 /* symbols like 'EAX' come from <sys/reg.h> */
206 static int regmap
[] =
215 i386_register_u_addr (int blockend
, int regnum
)
218 /* this will be needed if fp registers are reinstated */
219 /* for now, you can look at them with 'info float'
220 * sys5 wont let you change them with ptrace anyway
222 if (regnum
>= FP0_REGNUM
&& regnum
<= FP7_REGNUM
)
226 ubase
= blockend
+ 4 * (SS
+ 1) - KSTKSZ
;
227 fpstate
= ubase
+ ((char *) &u
.u_fpstate
- (char *) &u
);
228 return (fpstate
+ 0x1c + 10 * (regnum
- FP0_REGNUM
));
232 return (blockend
+ 4 * regmap
[regnum
]);
235 #elif defined(TARGET_M68K)
237 initialize_arch (void)
242 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
243 static int regmap
[] =
246 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
247 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
250 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
254 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
255 PT_FPCR
, PT_FPSR
, PT_FPIAR
257 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
261 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
265 m68k_linux_register_u_addr (int blockend
, int regnum
)
267 return (blockend
+ 4 * regmap
[regnum
]);
269 #elif defined(IA64_GNULINUX_TARGET)
273 #include <asm/ptrace_offsets.h>
275 static int u_offsets
[] =
277 /* general registers */
278 -1, /* gr0 not available; i.e, it's always zero */
310 /* gr32 through gr127 not directly available via the ptrace interface */
311 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
312 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
313 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
314 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
315 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
316 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
317 /* Floating point registers */
318 -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0) */
445 /* predicate registers - we don't fetch these individually */
446 -1, -1, -1, -1, -1, -1, -1, -1,
447 -1, -1, -1, -1, -1, -1, -1, -1,
448 -1, -1, -1, -1, -1, -1, -1, -1,
449 -1, -1, -1, -1, -1, -1, -1, -1,
450 -1, -1, -1, -1, -1, -1, -1, -1,
451 -1, -1, -1, -1, -1, -1, -1, -1,
452 -1, -1, -1, -1, -1, -1, -1, -1,
453 -1, -1, -1, -1, -1, -1, -1, -1,
454 /* branch registers */
463 /* virtual frame pointer and virtual return address pointer */
465 /* other registers */
468 PT_CR_IPSR
, /* psr */
470 /* kernel registers not visible via ptrace interface (?) */
471 -1, -1, -1, -1, -1, -1, -1, -1,
473 -1, -1, -1, -1, -1, -1, -1, -1,
479 -1, /* Not available: FCR, IA32 floating control register */
481 -1, /* Not available: EFLAG */
482 -1, /* Not available: CSD */
483 -1, /* Not available: SSD */
484 -1, /* Not available: CFLG */
485 -1, /* Not available: FSR */
486 -1, /* Not available: FIR */
487 -1, /* Not available: FDR */
495 -1, /* Not available: ITC */
496 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
497 -1, -1, -1, -1, -1, -1, -1, -1, -1,
500 -1, /* Not available: EC, the Epilog Count register */
501 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
502 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
503 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
504 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
505 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
506 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
508 /* nat bits - not fetched directly; instead we obtain these bits from
509 either rnat or unat or from memory. */
510 -1, -1, -1, -1, -1, -1, -1, -1,
511 -1, -1, -1, -1, -1, -1, -1, -1,
512 -1, -1, -1, -1, -1, -1, -1, -1,
513 -1, -1, -1, -1, -1, -1, -1, -1,
514 -1, -1, -1, -1, -1, -1, -1, -1,
515 -1, -1, -1, -1, -1, -1, -1, -1,
516 -1, -1, -1, -1, -1, -1, -1, -1,
517 -1, -1, -1, -1, -1, -1, -1, -1,
518 -1, -1, -1, -1, -1, -1, -1, -1,
519 -1, -1, -1, -1, -1, -1, -1, -1,
520 -1, -1, -1, -1, -1, -1, -1, -1,
521 -1, -1, -1, -1, -1, -1, -1, -1,
522 -1, -1, -1, -1, -1, -1, -1, -1,
523 -1, -1, -1, -1, -1, -1, -1, -1,
524 -1, -1, -1, -1, -1, -1, -1, -1,
525 -1, -1, -1, -1, -1, -1, -1, -1,
529 ia64_register_u_addr (int blockend
, int regnum
)
533 if (regnum
< 0 || regnum
>= NUM_REGS
)
534 error ("Invalid register number %d.", regnum
);
536 addr
= u_offsets
[regnum
];
544 initialize_arch (void)
551 register_addr (int regno
, CORE_ADDR blockend
)
555 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
556 error ("Invalid register number %d.", regno
);
558 REGISTER_U_ADDR (addr
, blockend
, regno
);
563 /* Fetch one register. */
566 fetch_register (int regno
)
571 /* Offset of registers within the u area. */
574 offset
= U_REGS_OFFSET
;
576 regaddr
= register_addr (regno
, offset
);
577 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
580 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
] =
581 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
582 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
585 /* Warning, not error, in case we are attached; sometimes the
586 kernel doesn't let us at the registers. */
587 char *err
= strerror (errno
);
588 char *msg
= alloca (strlen (err
) + 128);
589 sprintf (msg
, "reading register %d: %s", regno
, err
);
597 /* Fetch all registers, or just one, from the child process. */
600 fetch_inferior_registers (int regno
)
602 if (regno
== -1 || regno
== 0)
603 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
604 fetch_register (regno
);
606 fetch_register (regno
);
609 /* Store our register values back into the inferior.
610 If REGNO is -1, do this for all registers.
611 Otherwise, REGNO specifies which register (so we can save time). */
614 store_inferior_registers (int regno
)
618 unsigned int offset
= U_REGS_OFFSET
;
623 if (CANNOT_STORE_REGISTER (regno
))
626 regaddr
= register_addr (regno
, offset
);
629 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
631 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
632 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
636 /* Error, even if attached. Failing to write these two
637 registers is pretty serious. */
638 sprintf (buf
, "writing register number %d", regno
);
639 perror_with_name (buf
);
644 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
647 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
648 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
651 /* Warning, not error, in case we are attached; sometimes the
652 kernel doesn't let us at the registers. */
653 char *err
= strerror (errno
);
654 char *msg
= alloca (strlen (err
) + 128);
655 sprintf (msg
, "writing register %d: %s",
660 regaddr
+= sizeof (int);
664 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
665 store_inferior_registers (regno
);
668 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
669 in the NEW_SUN_PTRACE case.
670 It ought to be straightforward. But it appears that writing did
671 not write the data that I specified. I cannot understand where
672 it got the data that it actually did write. */
674 /* Copy LEN bytes from inferior's memory starting at MEMADDR
675 to debugger memory starting at MYADDR. */
678 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
681 /* Round starting address down to longword boundary. */
682 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
683 /* Round ending address up; get number of longwords that makes. */
685 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
686 / sizeof (PTRACE_XFER_TYPE
);
687 /* Allocate buffer of that many longwords. */
688 register PTRACE_XFER_TYPE
*buffer
689 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
691 /* Read all the longwords */
692 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
694 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
697 /* Copy appropriate bytes out of the buffer. */
698 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
701 /* Copy LEN bytes of data from debugger memory at MYADDR
702 to inferior's memory at MEMADDR.
703 On failure (cannot write the inferior)
704 returns the value of errno. */
707 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
710 /* Round starting address down to longword boundary. */
711 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
712 /* Round ending address up; get number of longwords that makes. */
714 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
715 /* Allocate buffer of that many longwords. */
716 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
719 /* Fill start and end extra bytes of buffer with existing memory data. */
721 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
726 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
727 addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
), 0);
730 /* Copy data to be written over corresponding part of buffer */
732 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
734 /* Write the entire buffer. */
736 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
739 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
748 initialize_low (void)