1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001
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>
36 /***************Begin MY defs*********************/
37 static char my_registers
[REGISTER_BYTES
];
38 char *registers
= my_registers
;
39 /***************End MY defs*********************/
45 /* Default the type of the ptrace transfer to int. */
46 #ifndef PTRACE_XFER_TYPE
47 #define PTRACE_XFER_TYPE int
52 static void initialize_arch (void);
54 /* Start an inferior process and returns its pid.
55 ALLARGS is a vector of program-name and args. */
58 create_inferior (char *program
, char **allargs
)
64 perror_with_name ("fork");
68 ptrace (PTRACE_TRACEME
, 0, 0, 0);
70 execv (program
, allargs
);
72 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
73 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
81 /* Kill the inferior process. Make us have no inferior. */
86 if (inferior_pid
== 0)
88 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
90 /*************inferior_died ();****VK**************/
93 /* Return nonzero if the given thread is still alive. */
95 mythread_alive (int pid
)
100 /* Wait for process, returns status */
103 mywait (char *status
)
109 pid
= waitpid (inferior_pid
, &w
, 0);
111 if (pid
!= inferior_pid
)
112 perror_with_name ("wait");
116 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
118 return ((unsigned char) WEXITSTATUS (w
));
120 else if (!WIFSTOPPED (w
))
122 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
124 return ((unsigned char) WTERMSIG (w
));
127 fetch_inferior_registers (0);
130 return ((unsigned char) WSTOPSIG (w
));
133 /* Resume execution of the inferior process.
134 If STEP is nonzero, single-step it.
135 If SIGNAL is nonzero, give it that signal. */
138 myresume (int step
, int signal
)
141 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
143 perror_with_name ("ptrace");
147 #if !defined (offsetof)
148 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
151 /* U_REGS_OFFSET is the offset of the registers within the u area. */
152 #if !defined (U_REGS_OFFSET)
153 #define U_REGS_OFFSET \
154 ptrace (PT_READ_U, inferior_pid, \
155 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
159 #ifdef I386_GNULINUX_TARGET
160 /* This module only supports access to the general purpose registers.
161 Adjust the relevant constants accordingly.
163 FIXME: kettenis/2001-03-28: We should really use PTRACE_GETREGS to
164 get at the registers. Better yet, we should try to share code with
169 #define NUM_REGS NUM_GREGS
171 /* This stuff comes from i386-tdep.c. */
173 /* i386_register_byte[i] is the offset into the register file of the
174 start of register number i. We initialize this from
175 i386_register_raw_size. */
176 int i386_register_byte
[MAX_NUM_REGS
];
178 /* i386_register_raw_size[i] is the number of bytes of storage in
179 GDB's register array occupied by register i. */
180 int i386_register_raw_size
[MAX_NUM_REGS
] = {
195 initialize_arch (void)
197 /* Initialize the table saying where each register starts in the
203 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
205 i386_register_byte
[i
] = offset
;
206 offset
+= i386_register_raw_size
[i
];
211 /* This stuff comes from i386-linux-nat.c. */
213 /* Mapping between the general-purpose registers in `struct user'
214 format and GDB's register array layout. */
215 static int regmap
[] =
223 /* Return the address of register REGNUM. BLOCKEND is the value of
224 u.u_ar0, which should point to the registers. */
227 register_u_addr (CORE_ADDR blockend
, int regnum
)
229 return (blockend
+ 4 * regmap
[regnum
]);
231 #elif defined(TARGET_M68K)
233 initialize_arch (void)
238 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
239 static int regmap
[] =
242 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
243 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
246 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
250 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
251 PT_FPCR
, PT_FPSR
, PT_FPIAR
253 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
257 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
261 m68k_linux_register_u_addr (int blockend
, int regnum
)
263 return (blockend
+ 4 * regmap
[regnum
]);
265 #elif defined(IA64_GNULINUX_TARGET)
269 #include <asm/ptrace_offsets.h>
271 static int u_offsets
[] =
273 /* general registers */
274 -1, /* gr0 not available; i.e, it's always zero */
306 /* gr32 through gr127 not directly available via the ptrace interface */
307 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
308 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
309 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
310 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 /* Floating point registers */
314 -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0) */
441 /* predicate registers - we don't fetch these individually */
442 -1, -1, -1, -1, -1, -1, -1, -1,
443 -1, -1, -1, -1, -1, -1, -1, -1,
444 -1, -1, -1, -1, -1, -1, -1, -1,
445 -1, -1, -1, -1, -1, -1, -1, -1,
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 /* branch registers */
459 /* virtual frame pointer and virtual return address pointer */
461 /* other registers */
464 PT_CR_IPSR
, /* psr */
466 /* kernel registers not visible via ptrace interface (?) */
467 -1, -1, -1, -1, -1, -1, -1, -1,
469 -1, -1, -1, -1, -1, -1, -1, -1,
475 -1, /* Not available: FCR, IA32 floating control register */
477 -1, /* Not available: EFLAG */
478 -1, /* Not available: CSD */
479 -1, /* Not available: SSD */
480 -1, /* Not available: CFLG */
481 -1, /* Not available: FSR */
482 -1, /* Not available: FIR */
483 -1, /* Not available: FDR */
491 -1, /* Not available: ITC */
492 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
493 -1, -1, -1, -1, -1, -1, -1, -1, -1,
496 -1, /* Not available: EC, the Epilog Count register */
497 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
498 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
499 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
500 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
501 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
502 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
504 /* nat bits - not fetched directly; instead we obtain these bits from
505 either rnat or unat or from memory. */
506 -1, -1, -1, -1, -1, -1, -1, -1,
507 -1, -1, -1, -1, -1, -1, -1, -1,
508 -1, -1, -1, -1, -1, -1, -1, -1,
509 -1, -1, -1, -1, -1, -1, -1, -1,
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,
525 ia64_register_u_addr (int blockend
, int regnum
)
529 if (regnum
< 0 || regnum
>= NUM_REGS
)
530 error ("Invalid register number %d.", regnum
);
532 addr
= u_offsets
[regnum
];
540 initialize_arch (void)
545 #elif defined(ARM_GNULINUX_TARGET)
546 int arm_register_u_addr(blockend
, regnum
)
550 return blockend
+ REGISTER_BYTE(regnum
);
560 register_addr (int regno
, CORE_ADDR blockend
)
564 if (regno
< 0 || regno
>= NUM_REGS
)
565 error ("Invalid register number %d.", regno
);
567 REGISTER_U_ADDR (addr
, blockend
, regno
);
572 /* Fetch one register. */
575 fetch_register (int regno
)
580 /* Offset of registers within the u area. */
583 offset
= U_REGS_OFFSET
;
585 regaddr
= register_addr (regno
, offset
);
586 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
589 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
] =
590 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
591 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
594 /* Warning, not error, in case we are attached; sometimes the
595 kernel doesn't let us at the registers. */
596 char *err
= strerror (errno
);
597 char *msg
= alloca (strlen (err
) + 128);
598 sprintf (msg
, "reading register %d: %s", regno
, err
);
606 /* Fetch all registers, or just one, from the child process. */
609 fetch_inferior_registers (int regno
)
611 if (regno
== -1 || regno
== 0)
612 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
613 fetch_register (regno
);
615 fetch_register (regno
);
618 /* Store our register values back into the inferior.
619 If REGNO is -1, do this for all registers.
620 Otherwise, REGNO specifies which register (so we can save time). */
623 store_inferior_registers (int regno
)
627 unsigned int offset
= U_REGS_OFFSET
;
632 if (CANNOT_STORE_REGISTER (regno
))
635 regaddr
= register_addr (regno
, offset
);
638 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
640 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
641 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
645 /* Error, even if attached. Failing to write these two
646 registers is pretty serious. */
647 sprintf (buf
, "writing register number %d", regno
);
648 perror_with_name (buf
);
653 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
656 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
657 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
660 /* Warning, not error, in case we are attached; sometimes the
661 kernel doesn't let us at the registers. */
662 char *err
= strerror (errno
);
663 char *msg
= alloca (strlen (err
) + 128);
664 sprintf (msg
, "writing register %d: %s",
669 regaddr
+= sizeof (int);
673 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
674 store_inferior_registers (regno
);
677 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
678 in the NEW_SUN_PTRACE case.
679 It ought to be straightforward. But it appears that writing did
680 not write the data that I specified. I cannot understand where
681 it got the data that it actually did write. */
683 /* Copy LEN bytes from inferior's memory starting at MEMADDR
684 to debugger memory starting at MYADDR. */
687 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
690 /* Round starting address down to longword boundary. */
691 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
692 /* Round ending address up; get number of longwords that makes. */
694 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
695 / sizeof (PTRACE_XFER_TYPE
);
696 /* Allocate buffer of that many longwords. */
697 register PTRACE_XFER_TYPE
*buffer
698 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
700 /* Read all the longwords */
701 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
703 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
706 /* Copy appropriate bytes out of the buffer. */
707 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
710 /* Copy LEN bytes of data from debugger memory at MYADDR
711 to inferior's memory at MEMADDR.
712 On failure (cannot write the inferior)
713 returns the value of errno. */
716 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
719 /* Round starting address down to longword boundary. */
720 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
721 /* Round ending address up; get number of longwords that makes. */
723 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
724 /* Allocate buffer of that many longwords. */
725 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
728 /* Fill start and end extra bytes of buffer with existing memory data. */
730 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
735 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
736 addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
), 0);
739 /* Copy data to be written over corresponding part of buffer */
741 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
743 /* Write the entire buffer. */
745 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
748 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
757 initialize_low (void)
This page took 0.045871 seconds and 4 git commands to generate.