1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /***************************
24 #include "initialize.h"
25 ****************************/
28 #include <sys/param.h>
30 /*#include <sys/user.h>*/
33 #include <sys/signal.h>
35 #include <sys/kernel.h>
36 #include <sys/itimer.h>
38 #include <sys/resource.h>
41 #include <sys/ioctl.h>
44 #include "/usr/include/wait.h"
46 /***************Begin MY defs*********************/
48 char registers
[REGISTER_BYTES
];
50 /* Index within `registers' of the first byte of the space for
54 char buf2
[MAX_REGISTER_RAW_SIZE
];
55 /***************End MY defs*********************/
57 #include <sys/ptrace.h>
58 /*#include <machine/reg.h>*/
60 extern char **environ
;
62 extern int inferior_pid
;
63 void error (), quit (), perror_with_name ();
66 /* Start an inferior process and returns its pid.
67 ALLARGS is a vector of program-name and args.
68 ENV is the environment vector to pass. */
71 create_inferior (program
, allargs
)
79 perror_with_name ("fork");
83 ptrace (PTRACE_TRACEME
);
85 execv (program
, allargs
);
87 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
88 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
96 /* Kill the inferior process. Make us have no inferior. */
101 if (inferior_pid
== 0)
103 ptrace (8, inferior_pid
, 0, 0);
105 /*************inferior_died ();****VK**************/
108 /* Wait for process, returns status */
118 if (pid
!= PIDGET(inferior_pid
))
119 perror_with_name ("wait");
121 inferior_pid
= BUILDPID (inferior_pid
, w
.w_tid
);
125 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
127 return ((unsigned char) WEXITSTATUS (w
));
129 else if (!WIFSTOPPED (w
))
131 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
133 return ((unsigned char) WTERMSIG (w
));
136 fetch_inferior_registers (0);
139 return ((unsigned char) WSTOPSIG (w
));
142 /* Resume execution of the inferior process.
143 If STEP is nonzero, single-step it.
144 If SIGNAL is nonzero, give it that signal. */
147 myresume (step
, signal
)
152 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
154 perror_with_name ("ptrace");
158 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
160 static struct econtext
*
161 lynx_registers_addr()
164 int ecpoff
= offsetof(st_t
, ecp
);
168 stblock
= (st_t
*) ptrace (PTRACE_THREADUSER
, inferior_pid
,
169 (PTRACE_ARG3_TYPE
)0, 0);
171 perror_with_name ("PTRACE_THREADUSER");
173 ecp
= (CORE_ADDR
) ptrace (PTRACE_PEEKTHREAD
, inferior_pid
,
174 (PTRACE_ARG3_TYPE
)ecpoff
, 0);
175 ecp
-= (CORE_ADDR
)stblock
;
177 perror_with_name ("lynx_registers_addr(PTRACE_PEEKTHREAD)");
179 return (struct econtext
*)ecp
;
182 static struct econtext
*ecp
;
184 /* Mapping between GDB register #s and offsets into econtext. Must be
185 consistent with REGISTER_NAMES macro in tm-i386v.h. */
187 #define X(ENTRY)(offsetof(struct econtext, ENTRY) / 4)
188 static int regmap
[] = {
203 X(ecode
), /* Lynx doesn't give us either fs or gs, so */
204 X(fault
) /* we just substitute these two in the hopes
205 that they are useful. */
208 /* Fetch one or more registers from the inferior. REGNO == -1 to get
209 them all. We actually fetch more than requested, when convenient,
210 marking them as valid so we won't fetch them again. */
213 fetch_inferior_registers (ignored
)
218 struct econtext
*ecp
;
220 ecp
= lynx_registers_addr();
222 for (regno
= 0; regno
< NUM_REGS
; regno
++)
225 reg
= ptrace (PTRACE_PEEKTHREAD
, inferior_pid
,
226 (PTRACE_ARG3_TYPE
) (&ecp
->fault
+ regmap
[regno
]), 0);
228 perror_with_name ("fetch_inferior_registers(PTRACE_PEEKTHREAD)");
230 *(unsigned long *)®isters
[REGISTER_BYTE (regno
)] = reg
;
234 /* Store our register values back into the inferior.
235 If REGNO is -1, do this for all registers.
236 Otherwise, REGNO specifies which register (so we can save time). */
239 store_inferior_registers (ignored
)
244 struct econtext
*ecp
;
246 ecp
= lynx_registers_addr();
248 for (regno
= 0; regno
< NUM_REGS
; regno
++)
250 reg
= *(unsigned long *)®isters
[REGISTER_BYTE (regno
)];
253 ptrace (PTRACE_POKEUSER
, inferior_pid
,
254 (PTRACE_ARG3_TYPE
) (&ecp
->fault
+ regmap
[regno
]), reg
);
256 perror_with_name ("PTRACE_POKEUSER");
260 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
261 in the NEW_SUN_PTRACE case.
262 It ought to be straightforward. But it appears that writing did
263 not write the data that I specified. I cannot understand where
264 it got the data that it actually did write. */
266 /* Copy LEN bytes from inferior's memory starting at MEMADDR
267 to debugger memory starting at MYADDR. */
269 read_inferior_memory (memaddr
, myaddr
, len
)
275 /* Round starting address down to longword boundary. */
276 register CORE_ADDR addr
= memaddr
& -sizeof (int);
277 /* Round ending address up; get number of longwords that makes. */
279 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
280 /* Allocate buffer of that many longwords. */
281 register int *buffer
= (int *) alloca (count
* sizeof (int));
283 /* Read all the longwords */
284 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
286 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
289 /* Copy appropriate bytes out of the buffer. */
290 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
293 /* Copy LEN bytes of data from debugger memory at MYADDR
294 to inferior's memory at MEMADDR.
295 On failure (cannot write the inferior)
296 returns the value of errno. */
299 write_inferior_memory (memaddr
, myaddr
, len
)
305 /* Round starting address down to longword boundary. */
306 register CORE_ADDR addr
= memaddr
& -sizeof (int);
307 /* Round ending address up; get number of longwords that makes. */
309 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
310 /* Allocate buffer of that many longwords. */
311 register int *buffer
= (int *) alloca (count
* sizeof (int));
314 /* Fill start and end extra bytes of buffer with existing memory data. */
316 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
321 = ptrace (1, inferior_pid
,
322 addr
+ (count
- 1) * sizeof (int), 0);
325 /* Copy data to be written over corresponding part of buffer */
327 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
329 /* Write the entire buffer. */
331 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
334 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
351 return inferior_pid
!= 0;
This page took 0.04259 seconds and 4 git commands to generate.