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 (program
, allargs
)
77 perror_with_name ("fork");
81 ptrace (PTRACE_TRACEME
, 0, 0, 0);
83 execv (program
, allargs
);
85 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
86 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
94 /* Kill the inferior process. Make us have no inferior. */
99 if (inferior_pid
== 0)
101 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
103 /*************inferior_died ();****VK**************/
106 /* Return nonzero if the given thread is still alive. */
114 /* Wait for process, returns status */
124 if (pid
!= inferior_pid
)
125 perror_with_name ("wait");
129 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
131 return ((unsigned char) WEXITSTATUS (w
));
133 else if (!WIFSTOPPED (w
))
135 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
137 return ((unsigned char) WTERMSIG (w
));
140 fetch_inferior_registers (0);
143 return ((unsigned char) WSTOPSIG (w
));
146 /* Resume execution of the inferior process.
147 If STEP is nonzero, single-step it.
148 If SIGNAL is nonzero, give it that signal. */
151 myresume (step
, signal
)
156 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
158 perror_with_name ("ptrace");
162 #if !defined (offsetof)
163 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
166 /* U_REGS_OFFSET is the offset of the registers within the u area. */
167 #if !defined (U_REGS_OFFSET)
168 #define U_REGS_OFFSET \
169 ptrace (PT_READ_U, inferior_pid, \
170 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
174 #ifdef I386_GNULINUX_TARGET
175 /* i386_register_raw_size[i] is the number of bytes of storage in the
176 actual machine representation for register i. */
177 int i386_register_raw_size
[MAX_NUM_REGS
] = {
191 int i386_register_byte
[MAX_NUM_REGS
];
196 /* Initialize the table saying where each register starts in the
202 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
204 i386_register_byte
[i
] = offset
;
205 offset
+= i386_register_raw_size
[i
];
210 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
211 /* symbols like 'EAX' come from <sys/reg.h> */
212 static int regmap
[] =
221 i386_register_u_addr (blockend
, regnum
)
226 /* this will be needed if fp registers are reinstated */
227 /* for now, you can look at them with 'info float'
228 * sys5 wont let you change them with ptrace anyway
230 if (regnum
>= FP0_REGNUM
&& regnum
<= FP7_REGNUM
)
234 ubase
= blockend
+ 4 * (SS
+ 1) - KSTKSZ
;
235 fpstate
= ubase
+ ((char *) &u
.u_fpstate
- (char *) &u
);
236 return (fpstate
+ 0x1c + 10 * (regnum
- FP0_REGNUM
));
240 return (blockend
+ 4 * regmap
[regnum
]);
243 #elif defined(TARGET_M68K)
250 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
251 static int regmap
[] =
254 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
255 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
258 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
262 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
263 PT_FPCR
, PT_FPSR
, PT_FPIAR
265 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
269 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
273 m68k_linux_register_u_addr (blockend
, regnum
)
277 return (blockend
+ 4 * regmap
[regnum
]);
279 #elif defined(IA64_GNULINUX_TARGET)
283 #include <asm/ptrace_offsets.h>
285 static int u_offsets
[] =
287 /* general registers */
288 -1, /* gr0 not available; i.e, it's always zero */
320 /* gr32 through gr127 not directly available via the ptrace interface */
321 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
322 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
323 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
324 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 /* Floating point registers */
328 -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0) */
455 /* predicate registers - we don't fetch these individually */
456 -1, -1, -1, -1, -1, -1, -1, -1,
457 -1, -1, -1, -1, -1, -1, -1, -1,
458 -1, -1, -1, -1, -1, -1, -1, -1,
459 -1, -1, -1, -1, -1, -1, -1, -1,
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 /* branch registers */
473 /* virtual frame pointer and virtual return address pointer */
475 /* other registers */
478 PT_CR_IPSR
, /* psr */
480 /* kernel registers not visible via ptrace interface (?) */
481 -1, -1, -1, -1, -1, -1, -1, -1,
483 -1, -1, -1, -1, -1, -1, -1, -1,
489 -1, /* Not available: FCR, IA32 floating control register */
491 -1, /* Not available: EFLAG */
492 -1, /* Not available: CSD */
493 -1, /* Not available: SSD */
494 -1, /* Not available: CFLG */
495 -1, /* Not available: FSR */
496 -1, /* Not available: FIR */
497 -1, /* Not available: FDR */
505 -1, /* Not available: ITC */
506 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
507 -1, -1, -1, -1, -1, -1, -1, -1, -1,
510 -1, /* Not available: EC, the Epilog Count register */
511 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
512 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
513 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
514 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
515 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
516 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
518 /* nat bits - not fetched directly; instead we obtain these bits from
519 either rnat or unat or from memory. */
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,
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,
539 ia64_register_u_addr (int blockend
, int regnum
)
543 if (regnum
< 0 || regnum
>= NUM_REGS
)
544 error ("Invalid register number %d.", regnum
);
546 addr
= u_offsets
[regnum
];
561 register_addr (regno
, blockend
)
567 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
568 error ("Invalid register number %d.", regno
);
570 REGISTER_U_ADDR (addr
, blockend
, regno
);
575 /* Fetch one register. */
578 fetch_register (regno
)
584 /* Offset of registers within the u area. */
587 offset
= U_REGS_OFFSET
;
589 regaddr
= register_addr (regno
, offset
);
590 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
593 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
] =
594 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
595 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
598 /* Warning, not error, in case we are attached; sometimes the
599 kernel doesn't let us at the registers. */
600 char *err
= strerror (errno
);
601 char *msg
= alloca (strlen (err
) + 128);
602 sprintf (msg
, "reading register %d: %s", regno
, err
);
610 /* Fetch all registers, or just one, from the child process. */
613 fetch_inferior_registers (regno
)
616 if (regno
== -1 || regno
== 0)
617 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
618 fetch_register (regno
);
620 fetch_register (regno
);
623 /* Store our register values back into the inferior.
624 If REGNO is -1, do this for all registers.
625 Otherwise, REGNO specifies which register (so we can save time). */
628 store_inferior_registers (regno
)
633 unsigned int offset
= U_REGS_OFFSET
;
638 if (CANNOT_STORE_REGISTER (regno
))
641 regaddr
= register_addr (regno
, offset
);
644 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
646 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
647 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
651 /* Error, even if attached. Failing to write these two
652 registers is pretty serious. */
653 sprintf (buf
, "writing register number %d", regno
);
654 perror_with_name (buf
);
659 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
662 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
663 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
666 /* Warning, not error, in case we are attached; sometimes the
667 kernel doesn't let us at the registers. */
668 char *err
= strerror (errno
);
669 char *msg
= alloca (strlen (err
) + 128);
670 sprintf (msg
, "writing register %d: %s",
675 regaddr
+= sizeof (int);
679 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
680 store_inferior_registers (regno
);
683 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
684 in the NEW_SUN_PTRACE case.
685 It ought to be straightforward. But it appears that writing did
686 not write the data that I specified. I cannot understand where
687 it got the data that it actually did write. */
689 /* Copy LEN bytes from inferior's memory starting at MEMADDR
690 to debugger memory starting at MYADDR. */
693 read_inferior_memory (memaddr
, myaddr
, len
)
699 /* Round starting address down to longword boundary. */
700 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
701 /* Round ending address up; get number of longwords that makes. */
703 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
704 / sizeof (PTRACE_XFER_TYPE
);
705 /* Allocate buffer of that many longwords. */
706 register PTRACE_XFER_TYPE
*buffer
707 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
709 /* Read all the longwords */
710 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
712 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
715 /* Copy appropriate bytes out of the buffer. */
716 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
719 /* Copy LEN bytes of data from debugger memory at MYADDR
720 to inferior's memory at MEMADDR.
721 On failure (cannot write the inferior)
722 returns the value of errno. */
725 write_inferior_memory (memaddr
, myaddr
, len
)
731 /* Round starting address down to longword boundary. */
732 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
733 /* Round ending address up; get number of longwords that makes. */
735 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
736 /* Allocate buffer of that many longwords. */
737 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
740 /* Fill start and end extra bytes of buffer with existing memory data. */
742 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
747 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
748 addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
), 0);
751 /* Copy data to be written over corresponding part of buffer */
753 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
755 /* Write the entire buffer. */
757 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
760 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
This page took 0.046051 seconds and 4 git commands to generate.