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 /* Start an inferior process and returns its pid.
63 ALLARGS is a vector of program-name and args.
64 ENV is the environment vector to pass. */
67 create_inferior (program
, 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. */
112 /* Wait for process, returns status */
122 if (pid
!= inferior_pid
)
123 perror_with_name ("wait");
127 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
129 return ((unsigned char) WEXITSTATUS (w
));
131 else if (!WIFSTOPPED (w
))
133 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
135 return ((unsigned char) WTERMSIG (w
));
138 fetch_inferior_registers (0);
141 return ((unsigned char) WSTOPSIG (w
));
144 /* Resume execution of the inferior process.
145 If STEP is nonzero, single-step it.
146 If SIGNAL is nonzero, give it that signal. */
149 myresume (step
, signal
)
154 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
156 perror_with_name ("ptrace");
160 #if !defined (offsetof)
161 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
164 /* U_REGS_OFFSET is the offset of the registers within the u area. */
165 #if !defined (U_REGS_OFFSET)
166 #define U_REGS_OFFSET \
167 ptrace (PT_READ_U, inferior_pid, \
168 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
172 #ifdef I386_GNULINUX_TARGET
173 /* i386_register_raw_size[i] is the number of bytes of storage in the
174 actual machine representation for register i. */
175 int i386_register_raw_size
[MAX_NUM_REGS
] = {
189 int i386_register_byte
[MAX_NUM_REGS
];
194 /* Initialize the table saying where each register starts in the
200 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
202 i386_register_byte
[i
] = offset
;
203 offset
+= i386_register_raw_size
[i
];
208 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
209 /* symbols like 'EAX' come from <sys/reg.h> */
210 static int regmap
[] =
219 i386_register_u_addr (blockend
, regnum
)
224 /* this will be needed if fp registers are reinstated */
225 /* for now, you can look at them with 'info float'
226 * sys5 wont let you change them with ptrace anyway
228 if (regnum
>= FP0_REGNUM
&& regnum
<= FP7_REGNUM
)
232 ubase
= blockend
+ 4 * (SS
+ 1) - KSTKSZ
;
233 fpstate
= ubase
+ ((char *) &u
.u_fpstate
- (char *) &u
);
234 return (fpstate
+ 0x1c + 10 * (regnum
- FP0_REGNUM
));
238 return (blockend
+ 4 * regmap
[regnum
]);
241 #elif defined(TARGET_M68K)
248 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
249 static int regmap
[] =
252 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
253 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
256 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
260 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
261 PT_FPCR
, PT_FPSR
, PT_FPIAR
263 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
267 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
271 m68k_linux_register_u_addr (blockend
, regnum
)
275 return (blockend
+ 4 * regmap
[regnum
]);
277 /* start-sanitize-ia64 */
278 #elif defined(IA64_GNULINUX_TARGET)
282 #include <asm/ptrace_offsets.h>
284 static int u_offsets
[] =
286 /* general registers */
287 -1, /* gr0 not available; i.e, it's always zero */
319 /* gr32 through gr127 not directly available via the ptrace interface */
320 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 /* Floating point registers */
327 -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0) */
454 /* predicate registers - we don't fetch these individually */
455 -1, -1, -1, -1, -1, -1, -1, -1,
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 /* branch registers */
472 /* virtual frame pointer and virtual return address pointer */
474 /* other registers */
478 /* kernel registers not visible via ptrace interface (?) */
479 -1, -1, -1, -1, -1, -1, -1, -1,
481 -1, -1, -1, -1, -1, -1, -1, -1,
487 -1, /* Not available: FCR, IA32 floating control register */
489 -1, /* Not available: EFLAG */
490 -1, /* Not available: CSD */
491 -1, /* Not available: SSD */
492 -1, /* Not available: CFLG */
493 -1, /* Not available: FSR */
494 -1, /* Not available: FIR */
495 -1, /* Not available: FDR */
503 -1, /* Not available: ITC */
504 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
505 -1, -1, -1, -1, -1, -1, -1, -1, -1,
506 PT_CR_IFS
, /* was PT_AR_PFS, but it seemed bogus */
508 -1, /* Not available: EC, the Epilog Count register */
509 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
510 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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,
519 ia64_register_u_addr (int blockend
, int regnum
)
523 if (regnum
< 0 || regnum
>= NUM_REGS
)
524 error ("Invalid register number %d.", regnum
);
526 addr
= u_offsets
[regnum
];
537 /* end-sanitize-ia64 */
541 register_addr (regno
, blockend
)
547 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
548 error ("Invalid register number %d.", regno
);
550 REGISTER_U_ADDR (addr
, blockend
, regno
);
555 /* Fetch one register. */
558 fetch_register (regno
)
564 /* Offset of registers within the u area. */
567 offset
= U_REGS_OFFSET
;
569 regaddr
= register_addr (regno
, offset
);
570 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
573 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
] =
574 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
575 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
578 /* Warning, not error, in case we are attached; sometimes the
579 kernel doesn't let us at the registers. */
580 char *err
= strerror (errno
);
581 char *msg
= alloca (strlen (err
) + 128);
582 sprintf (msg
, "reading register %d: %s", regno
, err
);
590 /* Fetch all registers, or just one, from the child process. */
593 fetch_inferior_registers (regno
)
596 if (regno
== -1 || regno
== 0)
597 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
598 fetch_register (regno
);
600 fetch_register (regno
);
603 /* Store our register values back into the inferior.
604 If REGNO is -1, do this for all registers.
605 Otherwise, REGNO specifies which register (so we can save time). */
608 store_inferior_registers (regno
)
613 unsigned int offset
= U_REGS_OFFSET
;
618 if (CANNOT_STORE_REGISTER (regno
))
621 regaddr
= register_addr (regno
, offset
);
624 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
626 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
627 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
631 /* Error, even if attached. Failing to write these two
632 registers is pretty serious. */
633 sprintf (buf
, "writing register number %d", regno
);
634 perror_with_name (buf
);
639 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
642 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
643 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
646 /* Warning, not error, in case we are attached; sometimes the
647 kernel doesn't let us at the registers. */
648 char *err
= strerror (errno
);
649 char *msg
= alloca (strlen (err
) + 128);
650 sprintf (msg
, "writing register %d: %s",
655 regaddr
+= sizeof (int);
659 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
660 store_inferior_registers (regno
);
663 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
664 in the NEW_SUN_PTRACE case.
665 It ought to be straightforward. But it appears that writing did
666 not write the data that I specified. I cannot understand where
667 it got the data that it actually did write. */
669 /* Copy LEN bytes from inferior's memory starting at MEMADDR
670 to debugger memory starting at MYADDR. */
673 read_inferior_memory (memaddr
, myaddr
, len
)
679 /* Round starting address down to longword boundary. */
680 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
681 /* Round ending address up; get number of longwords that makes. */
683 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
684 / sizeof (PTRACE_XFER_TYPE
);
685 /* Allocate buffer of that many longwords. */
686 register PTRACE_XFER_TYPE
*buffer
687 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
689 /* Read all the longwords */
690 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
692 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
695 /* Copy appropriate bytes out of the buffer. */
696 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
699 /* Copy LEN bytes of data from debugger memory at MYADDR
700 to inferior's memory at MEMADDR.
701 On failure (cannot write the inferior)
702 returns the value of errno. */
705 write_inferior_memory (memaddr
, myaddr
, len
)
711 /* Round starting address down to longword boundary. */
712 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
713 /* Round ending address up; get number of longwords that makes. */
715 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
716 /* Allocate buffer of that many longwords. */
717 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
720 /* Fill start and end extra bytes of buffer with existing memory data. */
722 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
727 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
728 addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
), 0);
731 /* Copy data to be written over corresponding part of buffer */
733 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
735 /* Write the entire buffer. */
737 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
740 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
This page took 0.072945 seconds and 4 git commands to generate.