1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
28 #include <sys/param.h>
30 #include <sys/ptrace.h>
33 #include <sys/ioctl.h>
37 /***************Begin MY defs*********************/
38 static char my_registers
[REGISTER_BYTES
];
39 char *registers
= my_registers
;
40 /***************End MY defs*********************/
46 /* Default the type of the ptrace transfer to int. */
47 #ifndef PTRACE_XFER_TYPE
48 #define PTRACE_XFER_TYPE int
53 static void initialize_arch (void);
55 /* Start an inferior process and returns its pid.
56 ALLARGS is a vector of program-name and args. */
59 create_inferior (char *program
, char **allargs
)
65 perror_with_name ("fork");
69 ptrace (PTRACE_TRACEME
, 0, 0, 0);
71 execv (program
, allargs
);
73 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
82 /* Attach to an inferior process. */
87 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
89 fprintf (stderr
, "Cannot attach to process %d: %s (%d)\n", pid
,
90 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error",
99 /* Kill the inferior process. Make us have no inferior. */
104 if (inferior_pid
== 0)
106 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
108 /*************inferior_died ();****VK**************/
111 /* Return nonzero if the given thread is still alive. */
113 mythread_alive (int pid
)
118 /* Wait for process, returns status */
121 mywait (char *status
)
127 pid
= waitpid (inferior_pid
, &w
, 0);
129 if (pid
!= inferior_pid
)
130 perror_with_name ("wait");
134 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
136 return ((unsigned char) WEXITSTATUS (w
));
138 else if (!WIFSTOPPED (w
))
140 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
142 return ((unsigned char) WTERMSIG (w
));
145 fetch_inferior_registers (0);
148 return ((unsigned char) WSTOPSIG (w
));
151 /* Resume execution of the inferior process.
152 If STEP is nonzero, single-step it.
153 If SIGNAL is nonzero, give it that signal. */
156 myresume (int step
, int signal
)
159 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
161 perror_with_name ("ptrace");
165 #if !defined (offsetof)
166 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
169 /* U_REGS_OFFSET is the offset of the registers within the u area. */
170 #if !defined (U_REGS_OFFSET)
171 #define U_REGS_OFFSET \
172 ptrace (PT_READ_U, inferior_pid, \
173 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
177 #ifdef I386_GNULINUX_TARGET
178 /* This module only supports access to the general purpose registers.
179 Adjust the relevant constants accordingly.
181 FIXME: kettenis/2001-03-28: We should really use PTRACE_GETREGS to
182 get at the registers. Better yet, we should try to share code with
187 #define NUM_REGS NUM_GREGS
189 /* This stuff comes from i386-tdep.c. */
191 /* i386_register_byte[i] is the offset into the register file of the
192 start of register number i. We initialize this from
193 i386_register_raw_size. */
194 int i386_register_byte
[MAX_NUM_REGS
];
196 /* i386_register_raw_size[i] is the number of bytes of storage in
197 GDB's register array occupied by register i. */
198 int i386_register_raw_size
[MAX_NUM_REGS
] = {
213 initialize_arch (void)
215 /* Initialize the table saying where each register starts in the
221 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
223 i386_register_byte
[i
] = offset
;
224 offset
+= i386_register_raw_size
[i
];
229 /* This stuff comes from i386-linux-nat.c. */
231 /* Mapping between the general-purpose registers in `struct user'
232 format and GDB's register array layout. */
233 static int regmap
[] =
241 /* Return the address of register REGNUM. BLOCKEND is the value of
242 u.u_ar0, which should point to the registers. */
245 register_u_addr (CORE_ADDR blockend
, int regnum
)
247 return (blockend
+ 4 * regmap
[regnum
]);
249 #elif defined(TARGET_M68K)
251 initialize_arch (void)
256 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
257 static int regmap
[] =
260 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
261 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
264 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
268 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
269 PT_FPCR
, PT_FPSR
, PT_FPIAR
271 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
275 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
279 m68k_linux_register_u_addr (int blockend
, int regnum
)
281 return (blockend
+ 4 * regmap
[regnum
]);
283 #elif defined(IA64_GNULINUX_TARGET)
287 #include <asm/ptrace_offsets.h>
289 static int u_offsets
[] =
291 /* general registers */
292 -1, /* gr0 not available; i.e, it's always zero */
324 /* gr32 through gr127 not directly available via the ptrace interface */
325 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
326 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
327 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
328 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
329 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
330 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
331 /* Floating point registers */
332 -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0) */
459 /* predicate registers - we don't fetch these individually */
460 -1, -1, -1, -1, -1, -1, -1, -1,
461 -1, -1, -1, -1, -1, -1, -1, -1,
462 -1, -1, -1, -1, -1, -1, -1, -1,
463 -1, -1, -1, -1, -1, -1, -1, -1,
464 -1, -1, -1, -1, -1, -1, -1, -1,
465 -1, -1, -1, -1, -1, -1, -1, -1,
466 -1, -1, -1, -1, -1, -1, -1, -1,
467 -1, -1, -1, -1, -1, -1, -1, -1,
468 /* branch registers */
477 /* virtual frame pointer and virtual return address pointer */
479 /* other registers */
482 PT_CR_IPSR
, /* psr */
484 /* kernel registers not visible via ptrace interface (?) */
485 -1, -1, -1, -1, -1, -1, -1, -1,
487 -1, -1, -1, -1, -1, -1, -1, -1,
493 -1, /* Not available: FCR, IA32 floating control register */
495 -1, /* Not available: EFLAG */
496 -1, /* Not available: CSD */
497 -1, /* Not available: SSD */
498 -1, /* Not available: CFLG */
499 -1, /* Not available: FSR */
500 -1, /* Not available: FIR */
501 -1, /* Not available: FDR */
509 -1, /* Not available: ITC */
510 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
511 -1, -1, -1, -1, -1, -1, -1, -1, -1,
514 -1, /* Not available: EC, the Epilog Count register */
515 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
516 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
517 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
518 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
519 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
520 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
522 /* nat bits - not fetched directly; instead we obtain these bits from
523 either rnat or unat or from memory. */
524 -1, -1, -1, -1, -1, -1, -1, -1,
525 -1, -1, -1, -1, -1, -1, -1, -1,
526 -1, -1, -1, -1, -1, -1, -1, -1,
527 -1, -1, -1, -1, -1, -1, -1, -1,
528 -1, -1, -1, -1, -1, -1, -1, -1,
529 -1, -1, -1, -1, -1, -1, -1, -1,
530 -1, -1, -1, -1, -1, -1, -1, -1,
531 -1, -1, -1, -1, -1, -1, -1, -1,
532 -1, -1, -1, -1, -1, -1, -1, -1,
533 -1, -1, -1, -1, -1, -1, -1, -1,
534 -1, -1, -1, -1, -1, -1, -1, -1,
535 -1, -1, -1, -1, -1, -1, -1, -1,
536 -1, -1, -1, -1, -1, -1, -1, -1,
537 -1, -1, -1, -1, -1, -1, -1, -1,
538 -1, -1, -1, -1, -1, -1, -1, -1,
539 -1, -1, -1, -1, -1, -1, -1, -1,
543 ia64_register_u_addr (int blockend
, int regnum
)
547 if (regnum
< 0 || regnum
>= NUM_REGS
)
548 error ("Invalid register number %d.", regnum
);
550 addr
= u_offsets
[regnum
];
558 initialize_arch (void)
563 #elif defined(ARM_GNULINUX_TARGET)
564 int arm_register_u_addr(blockend
, regnum
)
568 return blockend
+ REGISTER_BYTE(regnum
);
578 register_addr (int regno
, CORE_ADDR blockend
)
582 if (regno
< 0 || regno
>= NUM_REGS
)
583 error ("Invalid register number %d.", regno
);
585 REGISTER_U_ADDR (addr
, blockend
, regno
);
590 /* Fetch one register. */
593 fetch_register (int regno
)
598 /* Offset of registers within the u area. */
601 offset
= U_REGS_OFFSET
;
603 regaddr
= register_addr (regno
, offset
);
604 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
607 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
] =
608 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
609 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
612 /* Warning, not error, in case we are attached; sometimes the
613 kernel doesn't let us at the registers. */
614 char *err
= strerror (errno
);
615 char *msg
= alloca (strlen (err
) + 128);
616 sprintf (msg
, "reading register %d: %s", regno
, err
);
624 /* Fetch all registers, or just one, from the child process. */
627 fetch_inferior_registers (int regno
)
629 if (regno
== -1 || regno
== 0)
630 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
631 fetch_register (regno
);
633 fetch_register (regno
);
636 /* Store our register values back into the inferior.
637 If REGNO is -1, do this for all registers.
638 Otherwise, REGNO specifies which register (so we can save time). */
641 store_inferior_registers (int regno
)
645 unsigned int offset
= U_REGS_OFFSET
;
650 if (CANNOT_STORE_REGISTER (regno
))
653 regaddr
= register_addr (regno
, offset
);
656 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
658 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
659 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
663 /* Error, even if attached. Failing to write these two
664 registers is pretty serious. */
665 sprintf (buf
, "writing register number %d", regno
);
666 perror_with_name (buf
);
671 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
674 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
675 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
678 /* Warning, not error, in case we are attached; sometimes the
679 kernel doesn't let us at the registers. */
680 char *err
= strerror (errno
);
681 char *msg
= alloca (strlen (err
) + 128);
682 sprintf (msg
, "writing register %d: %s",
687 regaddr
+= sizeof (int);
691 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
692 store_inferior_registers (regno
);
695 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
696 in the NEW_SUN_PTRACE case.
697 It ought to be straightforward. But it appears that writing did
698 not write the data that I specified. I cannot understand where
699 it got the data that it actually did write. */
701 /* Copy LEN bytes from inferior's memory starting at MEMADDR
702 to debugger memory starting at MYADDR. */
705 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
708 /* Round starting address down to longword boundary. */
709 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
710 /* Round ending address up; get number of longwords that makes. */
712 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
713 / sizeof (PTRACE_XFER_TYPE
);
714 /* Allocate buffer of that many longwords. */
715 register PTRACE_XFER_TYPE
*buffer
716 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
718 /* Read all the longwords */
719 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
721 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
724 /* Copy appropriate bytes out of the buffer. */
725 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
728 /* Copy LEN bytes of data from debugger memory at MYADDR
729 to inferior's memory at MEMADDR.
730 On failure (cannot write the inferior)
731 returns the value of errno. */
734 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
737 /* Round starting address down to longword boundary. */
738 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
739 /* Round ending address up; get number of longwords that makes. */
741 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
742 /* Allocate buffer of that many longwords. */
743 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
746 /* Fill start and end extra bytes of buffer with existing memory data. */
748 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
753 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
754 addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
), 0);
757 /* Copy data to be written over corresponding part of buffer */
759 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
761 /* Write the entire buffer. */
763 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
766 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
775 initialize_low (void)