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 (char *program
, char **allargs
)
67 perror_with_name ("fork");
71 ptrace (PT_TRACE_ME
, 0, 0, 0, 0);
73 execv (program
, allargs
);
75 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
76 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
84 /* Kill the inferior process. Make us have no inferior. */
89 if (inferior_pid
== 0)
91 ptrace (8, inferior_pid
, 0, 0, 0);
93 /*************inferior_died ();****VK**************/
96 /* Return nonzero if the given thread is still alive. */
98 mythread_alive (int pid
)
103 /* Wait for process, returns status */
106 mywait (char *status
)
112 if (pid
!= inferior_pid
)
113 perror_with_name ("wait");
117 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
119 return ((unsigned char) WEXITSTATUS (w
));
121 else if (!WIFSTOPPED (w
))
123 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
125 return ((unsigned char) WTERMSIG (w
));
128 fetch_inferior_registers (0);
131 return ((unsigned char) WSTOPSIG (w
));
134 /* Resume execution of the inferior process.
135 If STEP is nonzero, single-step it.
136 If SIGNAL is nonzero, give it that signal. */
139 myresume (int step
, int signal
)
142 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
, 1, signal
, 0);
144 perror_with_name ("ptrace");
148 #if !defined (offsetof)
149 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
152 /* U_REGS_OFFSET is the offset of the registers within the u area. */
153 #if !defined (U_REGS_OFFSET)
154 #define U_REGS_OFFSET \
155 ptrace (PT_READ_U, inferior_pid, \
156 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
161 register_addr (int regno
, CORE_ADDR blockend
)
165 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
166 error ("Invalid register number %d.", regno
);
168 REGISTER_U_ADDR (addr
, blockend
, regno
);
173 /* Fetch one register. */
176 fetch_register (int regno
)
178 register unsigned int regaddr
;
179 char buf
[MAX_REGISTER_RAW_SIZE
];
182 /* Offset of registers within the u area. */
185 offset
= U_REGS_OFFSET
;
187 regaddr
= register_addr (regno
, offset
);
188 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
191 *(int *) ®isters
[regno
* 4 + i
] = ptrace (PT_RUREGS
, inferior_pid
,
192 (PTRACE_ARG3_TYPE
) regaddr
, 0, 0);
193 regaddr
+= sizeof (int);
196 /* Warning, not error, in case we are attached; sometimes the
197 kernel doesn't let us at the registers. */
198 char *err
= strerror (errno
);
199 char *msg
= alloca (strlen (err
) + 128);
200 sprintf (msg
, "reading register %d: %s", regno
, err
);
208 /* Fetch all registers, or just one, from the child process. */
211 fetch_inferior_registers (int regno
)
213 if (regno
== -1 || regno
== 0)
214 for (regno
= 0; regno
< NUM_REGS
; regno
++)
215 fetch_register (regno
);
217 fetch_register (regno
);
220 /* Store our register values back into the inferior.
221 If REGNO is -1, do this for all registers.
222 Otherwise, REGNO specifies which register (so we can save time). */
225 store_inferior_registers (int regno
)
227 register unsigned int regaddr
;
229 extern char registers
[];
231 unsigned int offset
= U_REGS_OFFSET
;
236 if (CANNOT_STORE_REGISTER (regno
))
238 regaddr
= register_addr (regno
, offset
);
240 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
242 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
243 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
247 /* Error, even if attached. Failing to write these two
248 registers is pretty serious. */
249 sprintf (buf
, "writing register number %d", regno
);
250 perror_with_name (buf
);
254 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
257 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
258 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
261 /* Warning, not error, in case we are attached; sometimes the
262 kernel doesn't let us at the registers. */
263 char *err
= strerror (errno
);
264 char *msg
= alloca (strlen (err
) + 128);
265 sprintf (msg
, "writing register %d: %s",
270 regaddr
+= sizeof (int);
274 for (regno
= 0; regno
< NUM_REGS
; regno
++)
275 store_inferior_registers (regno
);
278 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
279 in the NEW_SUN_PTRACE case.
280 It ought to be straightforward. But it appears that writing did
281 not write the data that I specified. I cannot understand where
282 it got the data that it actually did write. */
284 /* Copy LEN bytes from inferior's memory starting at MEMADDR
285 to debugger memory starting at MYADDR. */
287 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
290 /* Round starting address down to longword boundary. */
291 register CORE_ADDR addr
= memaddr
& -sizeof (int);
292 /* Round ending address up; get number of longwords that makes. */
294 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
295 /* Allocate buffer of that many longwords. */
296 register int *buffer
= (int *) alloca (count
* sizeof (int));
298 /* Read all the longwords */
299 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
301 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0, 0);
304 /* Copy appropriate bytes out of the buffer. */
305 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
308 /* Copy LEN bytes of data from debugger memory at MYADDR
309 to inferior's memory at MEMADDR.
310 On failure (cannot write the inferior)
311 returns the value of errno. */
314 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
317 /* Round starting address down to longword boundary. */
318 register CORE_ADDR addr
= memaddr
& -sizeof (int);
319 /* Round ending address up; get number of longwords that makes. */
321 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
322 /* Allocate buffer of that many longwords. */
323 register int *buffer
= (int *) alloca (count
* sizeof (int));
326 /* Fill start and end extra bytes of buffer with existing memory data. */
328 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0, 0);
333 = ptrace (1, inferior_pid
,
334 addr
+ (count
- 1) * sizeof (int), 0, 0);
337 /* Copy data to be written over corresponding part of buffer */
339 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
341 /* Write the entire buffer. */
343 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
346 ptrace (4, inferior_pid
, addr
, buffer
[i
], 0);
355 initialize_low (void)
This page took 0.037794 seconds and 4 git commands to generate.