1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995 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>
31 #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*********************/
47 #include <sys/ptrace.h>
48 #include <machine/reg.h>
50 extern char **environ
;
52 extern int inferior_pid
;
53 void quit (), perror_with_name ();
56 /* Start an inferior process and returns its pid.
57 ALLARGS is a vector of program-name and args.
58 ENV is the environment vector to pass. */
61 create_inferior (program
, allargs
)
69 perror_with_name ("fork");
73 ptrace (PT_TRACE_ME
, 0, 0, 0, 0);
75 execv (program
, allargs
);
77 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
78 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
86 /* Kill the inferior process. Make us have no inferior. */
91 if (inferior_pid
== 0)
93 ptrace (8, inferior_pid
, 0, 0, 0);
95 /*************inferior_died ();****VK**************/
98 /* Return nonzero if the given thread is still alive. */
106 /* Wait for process, returns status */
116 if (pid
!= inferior_pid
)
117 perror_with_name ("wait");
121 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
123 return ((unsigned char) WEXITSTATUS (w
));
125 else if (!WIFSTOPPED (w
))
127 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
129 return ((unsigned char) WTERMSIG (w
));
132 fetch_inferior_registers (0);
135 return ((unsigned char) WSTOPSIG (w
));
138 /* Resume execution of the inferior process.
139 If STEP is nonzero, single-step it.
140 If SIGNAL is nonzero, give it that signal. */
143 myresume (step
, signal
)
148 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
, 1, signal
, 0);
150 perror_with_name ("ptrace");
154 #if !defined (offsetof)
155 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
158 /* U_REGS_OFFSET is the offset of the registers within the u area. */
159 #if !defined (U_REGS_OFFSET)
160 #define U_REGS_OFFSET \
161 ptrace (PT_READ_U, inferior_pid, \
162 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
167 register_addr (regno
, blockend
)
173 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
174 error ("Invalid register number %d.", regno
);
176 REGISTER_U_ADDR (addr
, blockend
, regno
);
181 /* Fetch one register. */
184 fetch_register (regno
)
187 register unsigned int regaddr
;
188 char buf
[MAX_REGISTER_RAW_SIZE
];
191 /* Offset of registers within the u area. */
194 offset
= U_REGS_OFFSET
;
196 regaddr
= register_addr (regno
, offset
);
197 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
200 *(int *) ®isters
[regno
* 4 + i
] = ptrace (PT_RUREGS
, inferior_pid
,
201 (PTRACE_ARG3_TYPE
) regaddr
, 0, 0);
202 regaddr
+= sizeof (int);
205 /* Warning, not error, in case we are attached; sometimes the
206 kernel doesn't let us at the registers. */
207 char *err
= strerror (errno
);
208 char *msg
= alloca (strlen (err
) + 128);
209 sprintf (msg
, "reading register %d: %s", regno
, err
);
217 /* Fetch all registers, or just one, from the child process. */
220 fetch_inferior_registers (regno
)
223 if (regno
== -1 || regno
== 0)
224 for (regno
= 0; regno
< NUM_REGS
; regno
++)
225 fetch_register (regno
);
227 fetch_register (regno
);
230 /* Store our register values back into the inferior.
231 If REGNO is -1, do this for all registers.
232 Otherwise, REGNO specifies which register (so we can save time). */
235 store_inferior_registers (regno
)
238 register unsigned int regaddr
;
240 extern char registers
[];
242 unsigned int offset
= U_REGS_OFFSET
;
247 if (CANNOT_STORE_REGISTER (regno
))
249 regaddr
= register_addr (regno
, offset
);
251 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
253 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
254 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
258 /* Error, even if attached. Failing to write these two
259 registers is pretty serious. */
260 sprintf (buf
, "writing register number %d", regno
);
261 perror_with_name (buf
);
265 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
268 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
269 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
272 /* Warning, not error, in case we are attached; sometimes the
273 kernel doesn't let us at the registers. */
274 char *err
= strerror (errno
);
275 char *msg
= alloca (strlen (err
) + 128);
276 sprintf (msg
, "writing register %d: %s",
281 regaddr
+= sizeof (int);
285 for (regno
= 0; regno
< NUM_REGS
; regno
++)
286 store_inferior_registers (regno
);
289 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
290 in the NEW_SUN_PTRACE case.
291 It ought to be straightforward. But it appears that writing did
292 not write the data that I specified. I cannot understand where
293 it got the data that it actually did write. */
295 /* Copy LEN bytes from inferior's memory starting at MEMADDR
296 to debugger memory starting at MYADDR. */
298 read_inferior_memory (memaddr
, myaddr
, len
)
304 /* Round starting address down to longword boundary. */
305 register CORE_ADDR addr
= memaddr
& -sizeof (int);
306 /* Round ending address up; get number of longwords that makes. */
308 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
309 /* Allocate buffer of that many longwords. */
310 register int *buffer
= (int *) alloca (count
* sizeof (int));
312 /* Read all the longwords */
313 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
315 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0, 0);
318 /* Copy appropriate bytes out of the buffer. */
319 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
322 /* Copy LEN bytes of data from debugger memory at MYADDR
323 to inferior's memory at MEMADDR.
324 On failure (cannot write the inferior)
325 returns the value of errno. */
328 write_inferior_memory (memaddr
, myaddr
, len
)
334 /* Round starting address down to longword boundary. */
335 register CORE_ADDR addr
= memaddr
& -sizeof (int);
336 /* Round ending address up; get number of longwords that makes. */
338 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
339 /* Allocate buffer of that many longwords. */
340 register int *buffer
= (int *) alloca (count
* sizeof (int));
343 /* Fill start and end extra bytes of buffer with existing memory data. */
345 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0, 0);
350 = ptrace (1, inferior_pid
,
351 addr
+ (count
- 1) * sizeof (int), 0, 0);
354 /* Copy data to be written over corresponding part of buffer */
356 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
358 /* Write the entire buffer. */
360 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
363 ptrace (4, inferior_pid
, addr
, buffer
[i
], 0);
380 return inferior_pid
!= 0;
This page took 0.057735 seconds and 4 git commands to generate.