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 char registers
[REGISTER_BYTES
];
39 /* Index within `registers' of the first byte of the space for
43 char buf2
[MAX_REGISTER_RAW_SIZE
];
44 /***************End MY defs*********************/
46 #include <sys/ptrace.h>
47 #include <machine/reg.h>
49 extern char **environ
;
51 extern int inferior_pid
;
52 void quit (), perror_with_name ();
55 /* Start an inferior process and returns its pid.
56 ALLARGS is a vector of program-name and args.
57 ENV is the environment vector to pass. */
60 create_inferior (program
, allargs
)
68 perror_with_name ("fork");
72 ptrace (PT_TRACE_ME
, 0, 0, 0, 0);
74 execv (program
, allargs
);
76 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
77 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
85 /* Kill the inferior process. Make us have no inferior. */
90 if (inferior_pid
== 0)
92 ptrace (8, inferior_pid
, 0, 0, 0);
94 /*************inferior_died ();****VK**************/
97 /* Return nonzero if the given thread is still alive. */
105 /* Wait for process, returns status */
115 if (pid
!= inferior_pid
)
116 perror_with_name ("wait");
120 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
122 return ((unsigned char) WEXITSTATUS (w
));
124 else if (!WIFSTOPPED (w
))
126 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
128 return ((unsigned char) WTERMSIG (w
));
131 fetch_inferior_registers (0);
134 return ((unsigned char) WSTOPSIG (w
));
137 /* Resume execution of the inferior process.
138 If STEP is nonzero, single-step it.
139 If SIGNAL is nonzero, give it that signal. */
142 myresume (step
, signal
)
147 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
, 1, signal
, 0);
149 perror_with_name ("ptrace");
153 #if !defined (offsetof)
154 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
157 /* U_REGS_OFFSET is the offset of the registers within the u area. */
158 #if !defined (U_REGS_OFFSET)
159 #define U_REGS_OFFSET \
160 ptrace (PT_READ_U, inferior_pid, \
161 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
166 register_addr (regno
, blockend
)
172 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
173 error ("Invalid register number %d.", regno
);
175 REGISTER_U_ADDR (addr
, blockend
, regno
);
180 /* Fetch one register. */
183 fetch_register (regno
)
186 register unsigned int regaddr
;
187 char buf
[MAX_REGISTER_RAW_SIZE
];
190 /* Offset of registers within the u area. */
193 offset
= U_REGS_OFFSET
;
195 regaddr
= register_addr (regno
, offset
);
196 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
199 *(int *) ®isters
[regno
* 4 + i
] = ptrace (PT_RUREGS
, inferior_pid
,
200 (PTRACE_ARG3_TYPE
) regaddr
, 0, 0);
201 regaddr
+= sizeof (int);
204 /* Warning, not error, in case we are attached; sometimes the
205 kernel doesn't let us at the registers. */
206 char *err
= strerror (errno
);
207 char *msg
= alloca (strlen (err
) + 128);
208 sprintf (msg
, "reading register %d: %s", regno
, err
);
216 /* Fetch all registers, or just one, from the child process. */
219 fetch_inferior_registers (regno
)
222 if (regno
== -1 || regno
== 0)
223 for (regno
= 0; regno
< NUM_REGS
; regno
++)
224 fetch_register (regno
);
226 fetch_register (regno
);
229 /* Store our register values back into the inferior.
230 If REGNO is -1, do this for all registers.
231 Otherwise, REGNO specifies which register (so we can save time). */
234 store_inferior_registers (regno
)
237 register unsigned int regaddr
;
239 extern char registers
[];
241 unsigned int offset
= U_REGS_OFFSET
;
246 if (CANNOT_STORE_REGISTER (regno
))
248 regaddr
= register_addr (regno
, offset
);
250 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
252 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
253 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
257 /* Error, even if attached. Failing to write these two
258 registers is pretty serious. */
259 sprintf (buf
, "writing register number %d", regno
);
260 perror_with_name (buf
);
264 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
267 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
268 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
271 /* Warning, not error, in case we are attached; sometimes the
272 kernel doesn't let us at the registers. */
273 char *err
= strerror (errno
);
274 char *msg
= alloca (strlen (err
) + 128);
275 sprintf (msg
, "writing register %d: %s",
280 regaddr
+= sizeof (int);
284 for (regno
= 0; regno
< NUM_REGS
; regno
++)
285 store_inferior_registers (regno
);
288 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
289 in the NEW_SUN_PTRACE case.
290 It ought to be straightforward. But it appears that writing did
291 not write the data that I specified. I cannot understand where
292 it got the data that it actually did write. */
294 /* Copy LEN bytes from inferior's memory starting at MEMADDR
295 to debugger memory starting at MYADDR. */
297 read_inferior_memory (memaddr
, myaddr
, len
)
303 /* Round starting address down to longword boundary. */
304 register CORE_ADDR addr
= memaddr
& -sizeof (int);
305 /* Round ending address up; get number of longwords that makes. */
307 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
308 /* Allocate buffer of that many longwords. */
309 register int *buffer
= (int *) alloca (count
* sizeof (int));
311 /* Read all the longwords */
312 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
314 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0, 0);
317 /* Copy appropriate bytes out of the buffer. */
318 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
321 /* Copy LEN bytes of data from debugger memory at MYADDR
322 to inferior's memory at MEMADDR.
323 On failure (cannot write the inferior)
324 returns the value of errno. */
327 write_inferior_memory (memaddr
, myaddr
, len
)
333 /* Round starting address down to longword boundary. */
334 register CORE_ADDR addr
= memaddr
& -sizeof (int);
335 /* Round ending address up; get number of longwords that makes. */
337 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
338 /* Allocate buffer of that many longwords. */
339 register int *buffer
= (int *) alloca (count
* sizeof (int));
342 /* Fill start and end extra bytes of buffer with existing memory data. */
344 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0, 0);
349 = ptrace (1, inferior_pid
,
350 addr
+ (count
- 1) * sizeof (int), 0, 0);
353 /* Copy data to be written over corresponding part of buffer */
355 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
357 /* Write the entire buffer. */
359 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
362 ptrace (4, inferior_pid
, addr
, buffer
[i
], 0);
379 return inferior_pid
!= 0;
This page took 0.042201 seconds and 4 git commands to generate.