1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include <sys/param.h>
28 #include <sys/ptrace.h>
31 #include <sys/ioctl.h>
37 #define PTRACE_ARG3_TYPE long
38 #define PTRACE_XFER_TYPE int
44 /* Start an inferior process and returns its pid.
45 ALLARGS is a vector of program-name and args. */
48 create_inferior (char *program
, char **allargs
)
54 perror_with_name ("fork");
58 ptrace (PTRACE_TRACEME
, 0, 0, 0);
60 execv (program
, allargs
);
62 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
71 /* Attach to an inferior process. */
76 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
78 fprintf (stderr
, "Cannot attach to process %d: %s (%d)\n", pid
,
79 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error",
88 /* Kill the inferior process. Make us have no inferior. */
93 if (inferior_pid
== 0)
95 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
99 /* Return nonzero if the given thread is still alive. */
101 mythread_alive (int pid
)
106 /* Wait for process, returns status */
109 mywait (char *status
)
115 pid
= waitpid (inferior_pid
, &w
, 0);
117 if (pid
!= inferior_pid
)
118 perror_with_name ("wait");
122 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
124 return ((unsigned char) WEXITSTATUS (w
));
126 else if (!WIFSTOPPED (w
))
128 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
130 return ((unsigned char) WTERMSIG (w
));
133 fetch_inferior_registers (0);
136 return ((unsigned char) WSTOPSIG (w
));
139 /* Resume execution of the inferior process.
140 If STEP is nonzero, single-step it.
141 If SIGNAL is nonzero, give it that signal. */
144 myresume (int step
, int signal
)
147 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
149 perror_with_name ("ptrace");
152 #define REGISTER_RAW_SIZE(regno) register_size((regno))
155 register_addr (int regnum
)
159 if (regnum
< 0 || regnum
>= num_regs
)
160 error ("Invalid register number %d.", regnum
);
162 addr
= regmap
[regnum
];
169 /* Fetch one register. */
172 fetch_register (int regno
)
177 if (regno
>= num_regs
)
179 if (cannot_fetch_register (regno
))
182 regaddr
= register_addr (regno
);
185 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
188 *(PTRACE_XFER_TYPE
*) (register_data (regno
) + i
) =
189 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
190 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
193 /* Warning, not error, in case we are attached; sometimes the
194 kernel doesn't let us at the registers. */
195 char *err
= strerror (errno
);
196 char *msg
= alloca (strlen (err
) + 128);
197 sprintf (msg
, "reading register %d: %s", regno
, err
);
205 /* Fetch all registers, or just one, from the child process. */
208 fetch_inferior_registers (int regno
)
210 if (regno
== -1 || regno
== 0)
211 for (regno
= 0; regno
< num_regs
; regno
++)
212 fetch_register (regno
);
214 fetch_register (regno
);
217 /* Store our register values back into the inferior.
218 If REGNO is -1, do this for all registers.
219 Otherwise, REGNO specifies which register (so we can save time). */
222 store_inferior_registers (int regno
)
229 if (regno
>= num_regs
)
232 if (cannot_store_register (regno
))
235 regaddr
= register_addr (regno
);
239 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
242 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
243 *(int *) (register_data (regno
) + i
));
246 /* Warning, not error, in case we are attached; sometimes the
247 kernel doesn't let us at the registers. */
248 char *err
= strerror (errno
);
249 char *msg
= alloca (strlen (err
) + 128);
250 sprintf (msg
, "writing register %d: %s",
255 regaddr
+= sizeof (int);
259 for (regno
= 0; regno
< num_regs
; regno
++)
260 store_inferior_registers (regno
);
263 /* Copy LEN bytes from inferior's memory starting at MEMADDR
264 to debugger memory starting at MYADDR. */
267 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
270 /* Round starting address down to longword boundary. */
271 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
272 /* Round ending address up; get number of longwords that makes. */
274 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
275 / sizeof (PTRACE_XFER_TYPE
);
276 /* Allocate buffer of that many longwords. */
277 register PTRACE_XFER_TYPE
*buffer
278 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
280 /* Read all the longwords */
281 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
283 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
286 /* Copy appropriate bytes out of the buffer. */
287 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
290 /* Copy LEN bytes of data from debugger memory at MYADDR
291 to inferior's memory at MEMADDR.
292 On failure (cannot write the inferior)
293 returns the value of errno. */
296 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
299 /* Round starting address down to longword boundary. */
300 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
301 /* Round ending address up; get number of longwords that makes. */
303 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
304 /* Allocate buffer of that many longwords. */
305 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
308 /* Fill start and end extra bytes of buffer with existing memory data. */
310 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
311 (PTRACE_ARG3_TYPE
) addr
, 0);
316 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
317 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
318 * sizeof (PTRACE_XFER_TYPE
)),
322 /* Copy data to be written over corresponding part of buffer */
324 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
326 /* Write the entire buffer. */
328 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
331 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
340 initialize_low (void)
This page took 0.046562 seconds and 4 git commands to generate.