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>
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 /* Attach to an inferior process. */
86 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
88 fprintf (stderr
, "Cannot attach to process %d: %s (%d)\n", pid
,
89 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error",
98 /* Kill the inferior process. Make us have no inferior. */
103 if (inferior_pid
== 0)
105 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
107 /*************inferior_died ();****VK**************/
110 /* Return nonzero if the given thread is still alive. */
112 mythread_alive (int pid
)
117 /* Wait for process, returns status */
120 mywait (char *status
)
126 pid
= waitpid (inferior_pid
, &w
, 0);
128 if (pid
!= inferior_pid
)
129 perror_with_name ("wait");
133 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
135 return ((unsigned char) WEXITSTATUS (w
));
137 else if (!WIFSTOPPED (w
))
139 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
141 return ((unsigned char) WTERMSIG (w
));
144 fetch_inferior_registers (0);
147 return ((unsigned char) WSTOPSIG (w
));
150 /* Resume execution of the inferior process.
151 If STEP is nonzero, single-step it.
152 If SIGNAL is nonzero, give it that signal. */
155 myresume (int step
, int signal
)
158 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
160 perror_with_name ("ptrace");
164 #if !defined (offsetof)
165 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
168 /* U_REGS_OFFSET is the offset of the registers within the u area. */
169 #if !defined (U_REGS_OFFSET)
170 #define U_REGS_OFFSET \
171 ptrace (PT_READ_U, inferior_pid, \
172 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
176 #ifdef I386_GNULINUX_TARGET
177 /* This module only supports access to the general purpose registers.
178 Adjust the relevant constants accordingly.
180 FIXME: kettenis/2001-03-28: We should really use PTRACE_GETREGS to
181 get at the registers. Better yet, we should try to share code with
186 #define NUM_REGS NUM_GREGS
188 /* This stuff comes from i386-tdep.c. */
190 /* i386_register_byte[i] is the offset into the register file of the
191 start of register number i. We initialize this from
192 i386_register_raw_size. */
193 int i386_register_byte
[MAX_NUM_REGS
];
195 /* i386_register_raw_size[i] is the number of bytes of storage in
196 GDB's register array occupied by register i. */
197 int i386_register_raw_size
[MAX_NUM_REGS
] = {
212 initialize_arch (void)
214 /* Initialize the table saying where each register starts in the
220 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
222 i386_register_byte
[i
] = offset
;
223 offset
+= i386_register_raw_size
[i
];
228 /* This stuff comes from i386-linux-nat.c. */
230 /* Mapping between the general-purpose registers in `struct user'
231 format and GDB's register array layout. */
232 static int regmap
[] =
240 /* Return the address of register REGNUM. BLOCKEND is the value of
241 u.u_ar0, which should point to the registers. */
244 register_u_addr (CORE_ADDR blockend
, int regnum
)
246 return (blockend
+ 4 * regmap
[regnum
]);
248 #elif defined(TARGET_M68K)
250 initialize_arch (void)
255 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
256 static int regmap
[] =
259 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
260 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
263 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
267 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
268 PT_FPCR
, PT_FPSR
, PT_FPIAR
270 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
274 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
278 m68k_linux_register_u_addr (int blockend
, int regnum
)
280 return (blockend
+ 4 * regmap
[regnum
]);
282 #elif defined(IA64_GNULINUX_TARGET)
286 #include <asm/ptrace_offsets.h>
288 static int u_offsets
[] =
290 /* general registers */
291 -1, /* gr0 not available; i.e, it's always zero */
323 /* gr32 through gr127 not directly available via the ptrace interface */
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 -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 /* Floating point registers */
331 -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0) */
458 /* predicate registers - we don't fetch these individually */
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 -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 /* branch registers */
476 /* virtual frame pointer and virtual return address pointer */
478 /* other registers */
481 PT_CR_IPSR
, /* psr */
483 /* kernel registers not visible via ptrace interface (?) */
484 -1, -1, -1, -1, -1, -1, -1, -1,
486 -1, -1, -1, -1, -1, -1, -1, -1,
492 -1, /* Not available: FCR, IA32 floating control register */
494 -1, /* Not available: EFLAG */
495 -1, /* Not available: CSD */
496 -1, /* Not available: SSD */
497 -1, /* Not available: CFLG */
498 -1, /* Not available: FSR */
499 -1, /* Not available: FIR */
500 -1, /* Not available: FDR */
508 -1, /* Not available: ITC */
509 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
510 -1, -1, -1, -1, -1, -1, -1, -1, -1,
513 -1, /* Not available: EC, the Epilog Count register */
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,
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,
521 /* nat bits - not fetched directly; instead we obtain these bits from
522 either rnat or unat or from memory. */
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,
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,
542 ia64_register_u_addr (int blockend
, int regnum
)
546 if (regnum
< 0 || regnum
>= NUM_REGS
)
547 error ("Invalid register number %d.", regnum
);
549 addr
= u_offsets
[regnum
];
557 initialize_arch (void)
562 #elif defined(ARM_GNULINUX_TARGET)
563 int arm_register_u_addr(blockend
, regnum
)
567 return blockend
+ REGISTER_BYTE(regnum
);
577 register_addr (int regno
, CORE_ADDR blockend
)
581 if (regno
< 0 || regno
>= NUM_REGS
)
582 error ("Invalid register number %d.", regno
);
584 REGISTER_U_ADDR (addr
, blockend
, regno
);
589 /* Fetch one register. */
592 fetch_register (int regno
)
597 /* Offset of registers within the u area. */
600 offset
= U_REGS_OFFSET
;
602 regaddr
= register_addr (regno
, offset
);
603 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
606 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
] =
607 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
608 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
611 /* Warning, not error, in case we are attached; sometimes the
612 kernel doesn't let us at the registers. */
613 char *err
= strerror (errno
);
614 char *msg
= alloca (strlen (err
) + 128);
615 sprintf (msg
, "reading register %d: %s", regno
, err
);
623 /* Fetch all registers, or just one, from the child process. */
626 fetch_inferior_registers (int regno
)
628 if (regno
== -1 || regno
== 0)
629 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
630 fetch_register (regno
);
632 fetch_register (regno
);
635 /* Store our register values back into the inferior.
636 If REGNO is -1, do this for all registers.
637 Otherwise, REGNO specifies which register (so we can save time). */
640 store_inferior_registers (int regno
)
644 unsigned int offset
= U_REGS_OFFSET
;
649 if (CANNOT_STORE_REGISTER (regno
))
652 regaddr
= register_addr (regno
, offset
);
655 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
657 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
658 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
662 /* Error, even if attached. Failing to write these two
663 registers is pretty serious. */
664 sprintf (buf
, "writing register number %d", regno
);
665 perror_with_name (buf
);
670 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
673 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
674 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
677 /* Warning, not error, in case we are attached; sometimes the
678 kernel doesn't let us at the registers. */
679 char *err
= strerror (errno
);
680 char *msg
= alloca (strlen (err
) + 128);
681 sprintf (msg
, "writing register %d: %s",
686 regaddr
+= sizeof (int);
690 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
691 store_inferior_registers (regno
);
694 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
695 in the NEW_SUN_PTRACE case.
696 It ought to be straightforward. But it appears that writing did
697 not write the data that I specified. I cannot understand where
698 it got the data that it actually did write. */
700 /* Copy LEN bytes from inferior's memory starting at MEMADDR
701 to debugger memory starting at MYADDR. */
704 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
707 /* Round starting address down to longword boundary. */
708 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
709 /* Round ending address up; get number of longwords that makes. */
711 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
712 / sizeof (PTRACE_XFER_TYPE
);
713 /* Allocate buffer of that many longwords. */
714 register PTRACE_XFER_TYPE
*buffer
715 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
717 /* Read all the longwords */
718 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
720 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
723 /* Copy appropriate bytes out of the buffer. */
724 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
727 /* Copy LEN bytes of data from debugger memory at MYADDR
728 to inferior's memory at MEMADDR.
729 On failure (cannot write the inferior)
730 returns the value of errno. */
733 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
736 /* Round starting address down to longword boundary. */
737 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
738 /* Round ending address up; get number of longwords that makes. */
740 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
741 /* Allocate buffer of that many longwords. */
742 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
745 /* Fill start and end extra bytes of buffer with existing memory data. */
747 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
752 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
753 addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
), 0);
756 /* Copy data to be written over corresponding part of buffer */
758 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
760 /* Write the entire buffer. */
762 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
765 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
774 initialize_low (void)
This page took 0.071208 seconds and 4 git commands to generate.