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*********************/
36 static char my_registers
[REGISTER_BYTES
];
37 char *registers
= my_registers
;
38 /***************End MY defs*********************/
44 /* Default the type of the ptrace transfer to int. */
45 #ifndef PTRACE_XFER_TYPE
46 #define PTRACE_XFER_TYPE int
50 extern int inferior_pid
;
51 void perror_with_name ();
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
,
74 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
82 /* Kill the inferior process. Make us have no inferior. */
87 if (inferior_pid
== 0)
89 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
91 /*************inferior_died ();****VK**************/
94 /* Return nonzero if the given thread is still alive. */
96 mythread_alive (int pid
)
101 /* Wait for process, returns status */
104 mywait (char *status
)
110 if (pid
!= inferior_pid
)
111 perror_with_name ("wait");
115 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
117 return ((unsigned char) WEXITSTATUS (w
));
119 else if (!WIFSTOPPED (w
))
121 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
123 return ((unsigned char) WTERMSIG (w
));
126 fetch_inferior_registers (0);
129 return ((unsigned char) WSTOPSIG (w
));
132 /* Resume execution of the inferior process.
133 If STEP is nonzero, single-step it.
134 If SIGNAL is nonzero, give it that signal. */
137 myresume (int step
, int signal
)
140 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
142 perror_with_name ("ptrace");
146 #if !defined (offsetof)
147 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
150 /* U_REGS_OFFSET is the offset of the registers within the u area. */
151 #if !defined (U_REGS_OFFSET)
152 #define U_REGS_OFFSET \
153 ptrace (PT_READ_U, inferior_pid, \
154 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
158 #ifdef I386_GNULINUX_TARGET
159 /* i386_register_raw_size[i] is the number of bytes of storage in the
160 actual machine representation for register i. */
161 int i386_register_raw_size
[MAX_NUM_REGS
] = {
175 int i386_register_byte
[MAX_NUM_REGS
];
178 initialize_arch (void)
180 /* Initialize the table saying where each register starts in the
186 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
188 i386_register_byte
[i
] = offset
;
189 offset
+= i386_register_raw_size
[i
];
194 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
195 /* symbols like 'EAX' come from <sys/reg.h> */
196 static int regmap
[] =
205 i386_register_u_addr (int blockend
, int regnum
)
208 /* this will be needed if fp registers are reinstated */
209 /* for now, you can look at them with 'info float'
210 * sys5 wont let you change them with ptrace anyway
212 if (regnum
>= FP0_REGNUM
&& regnum
<= FP7_REGNUM
)
216 ubase
= blockend
+ 4 * (SS
+ 1) - KSTKSZ
;
217 fpstate
= ubase
+ ((char *) &u
.u_fpstate
- (char *) &u
);
218 return (fpstate
+ 0x1c + 10 * (regnum
- FP0_REGNUM
));
222 return (blockend
+ 4 * regmap
[regnum
]);
225 #elif defined(TARGET_M68K)
227 initialize_arch (void)
232 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
233 static int regmap
[] =
236 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
237 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
240 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
244 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
245 PT_FPCR
, PT_FPSR
, PT_FPIAR
247 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
251 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
255 m68k_linux_register_u_addr (int blockend
, int regnum
)
257 return (blockend
+ 4 * regmap
[regnum
]);
259 #elif defined(IA64_GNULINUX_TARGET)
263 #include <asm/ptrace_offsets.h>
265 static int u_offsets
[] =
267 /* general registers */
268 -1, /* gr0 not available; i.e, it's always zero */
300 /* gr32 through gr127 not directly available via the ptrace interface */
301 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
302 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
303 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
304 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
305 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
306 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
307 /* Floating point registers */
308 -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0) */
435 /* predicate registers - we don't fetch these individually */
436 -1, -1, -1, -1, -1, -1, -1, -1,
437 -1, -1, -1, -1, -1, -1, -1, -1,
438 -1, -1, -1, -1, -1, -1, -1, -1,
439 -1, -1, -1, -1, -1, -1, -1, -1,
440 -1, -1, -1, -1, -1, -1, -1, -1,
441 -1, -1, -1, -1, -1, -1, -1, -1,
442 -1, -1, -1, -1, -1, -1, -1, -1,
443 -1, -1, -1, -1, -1, -1, -1, -1,
444 /* branch registers */
453 /* virtual frame pointer and virtual return address pointer */
455 /* other registers */
458 PT_CR_IPSR
, /* psr */
460 /* kernel registers not visible via ptrace interface (?) */
461 -1, -1, -1, -1, -1, -1, -1, -1,
463 -1, -1, -1, -1, -1, -1, -1, -1,
469 -1, /* Not available: FCR, IA32 floating control register */
471 -1, /* Not available: EFLAG */
472 -1, /* Not available: CSD */
473 -1, /* Not available: SSD */
474 -1, /* Not available: CFLG */
475 -1, /* Not available: FSR */
476 -1, /* Not available: FIR */
477 -1, /* Not available: FDR */
485 -1, /* Not available: ITC */
486 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
487 -1, -1, -1, -1, -1, -1, -1, -1, -1,
490 -1, /* Not available: EC, the Epilog Count register */
491 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
492 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
493 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
494 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
495 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
496 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
498 /* nat bits - not fetched directly; instead we obtain these bits from
499 either rnat or unat or from memory. */
500 -1, -1, -1, -1, -1, -1, -1, -1,
501 -1, -1, -1, -1, -1, -1, -1, -1,
502 -1, -1, -1, -1, -1, -1, -1, -1,
503 -1, -1, -1, -1, -1, -1, -1, -1,
504 -1, -1, -1, -1, -1, -1, -1, -1,
505 -1, -1, -1, -1, -1, -1, -1, -1,
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,
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
];
534 initialize_arch (void)
541 register_addr (int regno
, CORE_ADDR blockend
)
545 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
546 error ("Invalid register number %d.", regno
);
548 REGISTER_U_ADDR (addr
, blockend
, regno
);
553 /* Fetch one register. */
556 fetch_register (int regno
)
561 /* Offset of registers within the u area. */
564 offset
= U_REGS_OFFSET
;
566 regaddr
= register_addr (regno
, offset
);
567 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
570 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
] =
571 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
572 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
575 /* Warning, not error, in case we are attached; sometimes the
576 kernel doesn't let us at the registers. */
577 char *err
= strerror (errno
);
578 char *msg
= alloca (strlen (err
) + 128);
579 sprintf (msg
, "reading register %d: %s", regno
, err
);
587 /* Fetch all registers, or just one, from the child process. */
590 fetch_inferior_registers (int regno
)
592 if (regno
== -1 || regno
== 0)
593 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
594 fetch_register (regno
);
596 fetch_register (regno
);
599 /* Store our register values back into the inferior.
600 If REGNO is -1, do this for all registers.
601 Otherwise, REGNO specifies which register (so we can save time). */
604 store_inferior_registers (int regno
)
608 unsigned int offset
= U_REGS_OFFSET
;
613 if (CANNOT_STORE_REGISTER (regno
))
616 regaddr
= register_addr (regno
, offset
);
619 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
621 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
622 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
626 /* Error, even if attached. Failing to write these two
627 registers is pretty serious. */
628 sprintf (buf
, "writing register number %d", regno
);
629 perror_with_name (buf
);
634 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
637 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
638 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
641 /* Warning, not error, in case we are attached; sometimes the
642 kernel doesn't let us at the registers. */
643 char *err
= strerror (errno
);
644 char *msg
= alloca (strlen (err
) + 128);
645 sprintf (msg
, "writing register %d: %s",
650 regaddr
+= sizeof (int);
654 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
655 store_inferior_registers (regno
);
658 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
659 in the NEW_SUN_PTRACE case.
660 It ought to be straightforward. But it appears that writing did
661 not write the data that I specified. I cannot understand where
662 it got the data that it actually did write. */
664 /* Copy LEN bytes from inferior's memory starting at MEMADDR
665 to debugger memory starting at MYADDR. */
668 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
671 /* Round starting address down to longword boundary. */
672 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
673 /* Round ending address up; get number of longwords that makes. */
675 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
676 / sizeof (PTRACE_XFER_TYPE
);
677 /* Allocate buffer of that many longwords. */
678 register PTRACE_XFER_TYPE
*buffer
679 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
681 /* Read all the longwords */
682 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
684 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
687 /* Copy appropriate bytes out of the buffer. */
688 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
691 /* Copy LEN bytes of data from debugger memory at MYADDR
692 to inferior's memory at MEMADDR.
693 On failure (cannot write the inferior)
694 returns the value of errno. */
697 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
700 /* Round starting address down to longword boundary. */
701 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
702 /* Round ending address up; get number of longwords that makes. */
704 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
705 /* Allocate buffer of that many longwords. */
706 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
709 /* Fill start and end extra bytes of buffer with existing memory data. */
711 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
716 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
717 addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
), 0);
720 /* Copy data to be written over corresponding part of buffer */
722 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
724 /* Write the entire buffer. */
726 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
729 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
738 initialize_low (void)
This page took 0.058951 seconds and 4 git commands to generate.