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. */
23 #include "linux-low.h"
27 #include <sys/param.h>
29 #include <sys/ptrace.h>
32 #include <sys/ioctl.h>
38 #define PTRACE_ARG3_TYPE long
39 #define PTRACE_XFER_TYPE long
41 #ifdef HAVE_LINUX_REGSETS
42 static int use_regsets_p
= 1;
47 static int inferior_pid
;
49 /* Start an inferior process and returns its pid.
50 ALLARGS is a vector of program-name and args. */
53 linux_create_inferior (char *program
, char **allargs
)
59 perror_with_name ("fork");
63 ptrace (PTRACE_TRACEME
, 0, 0, 0);
65 execv (program
, allargs
);
67 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
79 /* Attach to an inferior process. */
82 linux_attach (int pid
)
84 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
86 fprintf (stderr
, "Cannot attach to process %d: %s (%d)\n", pid
,
87 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 (PTRACE_KILL
, inferior_pid
, 0, 0);
108 /* Return nonzero if the given thread is still alive. */
110 linux_thread_alive (int pid
)
115 /* Wait for process, returns status */
118 linux_wait (char *status
)
124 pid
= waitpid (inferior_pid
, &w
, 0);
126 if (pid
!= inferior_pid
)
127 perror_with_name ("wait");
131 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
134 return ((unsigned char) WEXITSTATUS (w
));
136 else if (!WIFSTOPPED (w
))
138 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
141 return ((unsigned char) WTERMSIG (w
));
144 fetch_inferior_registers (0);
147 return ((unsigned char) WSTOPSIG (w
));
150 /* Resume execution of the inferior process.
151 If STEP is nonzero, single-step it.
152 If SIGNAL is nonzero, give it that signal. */
155 linux_resume (int step
, int signal
)
158 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
160 perror_with_name ("ptrace");
164 #ifdef HAVE_LINUX_USRREGS
166 #define REGISTER_RAW_SIZE(regno) register_size((regno))
169 register_addr (int regnum
)
173 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
174 error ("Invalid register number %d.", regnum
);
176 addr
= the_low_target
.regmap
[regnum
];
183 /* Fetch one register. */
185 fetch_register (int regno
)
190 if (regno
>= the_low_target
.num_regs
)
192 if ((*the_low_target
.cannot_fetch_register
) (regno
))
195 regaddr
= register_addr (regno
);
198 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
201 *(PTRACE_XFER_TYPE
*) (register_data (regno
) + i
) =
202 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
203 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
206 /* Warning, not error, in case we are attached; sometimes the
207 kernel doesn't let us at the registers. */
208 char *err
= strerror (errno
);
209 char *msg
= alloca (strlen (err
) + 128);
210 sprintf (msg
, "reading register %d: %s", regno
, err
);
218 /* Fetch all registers, or just one, from the child process. */
220 usr_fetch_inferior_registers (int regno
)
222 if (regno
== -1 || regno
== 0)
223 for (regno
= 0; regno
< the_low_target
.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). */
233 usr_store_inferior_registers (int regno
)
240 if (regno
>= the_low_target
.num_regs
)
243 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
246 regaddr
= register_addr (regno
);
250 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
253 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
254 *(int *) (register_data (regno
) + i
));
257 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
259 char *err
= strerror (errno
);
260 char *msg
= alloca (strlen (err
) + 128);
261 sprintf (msg
, "writing register %d: %s",
267 regaddr
+= sizeof (int);
271 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
272 store_inferior_registers (regno
);
274 #endif /* HAVE_LINUX_USRREGS */
278 #ifdef HAVE_LINUX_REGSETS
281 regsets_fetch_inferior_registers (void)
283 struct regset_info
*regset
;
285 regset
= target_regsets
;
287 while (regset
->size
>= 0)
292 if (regset
->size
== 0)
298 buf
= malloc (regset
->size
);
299 res
= ptrace (regset
->get_request
, inferior_pid
, 0, (int) buf
);
304 /* If we get EIO on the first regset, do not try regsets again.
305 If we get EIO on a later regset, disable that regset. */
306 if (regset
== target_regsets
)
319 perror ("Warning: ptrace(regsets_fetch_inferior_registers)");
322 regset
->store_function (buf
);
329 regsets_store_inferior_registers (void)
331 struct regset_info
*regset
;
333 regset
= target_regsets
;
335 while (regset
->size
>= 0)
340 if (regset
->size
== 0)
346 buf
= malloc (regset
->size
);
347 regset
->fill_function (buf
);
348 res
= ptrace (regset
->set_request
, inferior_pid
, 0, (int) buf
);
353 /* If we get EIO on the first regset, do not try regsets again.
354 If we get EIO on a later regset, disable that regset. */
355 if (regset
== target_regsets
)
368 perror ("Warning: ptrace(regsets_store_inferior_registers)");
376 #endif /* HAVE_LINUX_REGSETS */
380 linux_fetch_registers (int regno
)
382 #ifdef HAVE_LINUX_REGSETS
385 if (regsets_fetch_inferior_registers () == 0)
389 #ifdef HAVE_LINUX_USRREGS
390 usr_fetch_inferior_registers (regno
);
395 linux_store_registers (int regno
)
397 #ifdef HAVE_LINUX_REGSETS
400 if (regsets_store_inferior_registers () == 0)
404 #ifdef HAVE_LINUX_USRREGS
405 usr_store_inferior_registers (regno
);
410 /* Copy LEN bytes from inferior's memory starting at MEMADDR
411 to debugger memory starting at MYADDR. */
414 linux_read_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
417 /* Round starting address down to longword boundary. */
418 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
419 /* Round ending address up; get number of longwords that makes. */
421 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
422 / sizeof (PTRACE_XFER_TYPE
);
423 /* Allocate buffer of that many longwords. */
424 register PTRACE_XFER_TYPE
*buffer
425 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
427 /* Read all the longwords */
428 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
430 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
433 /* Copy appropriate bytes out of the buffer. */
434 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
437 /* Copy LEN bytes of data from debugger memory at MYADDR
438 to inferior's memory at MEMADDR.
439 On failure (cannot write the inferior)
440 returns the value of errno. */
443 linux_write_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
446 /* Round starting address down to longword boundary. */
447 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
448 /* Round ending address up; get number of longwords that makes. */
450 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
451 /* Allocate buffer of that many longwords. */
452 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
455 /* Fill start and end extra bytes of buffer with existing memory data. */
457 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
458 (PTRACE_ARG3_TYPE
) addr
, 0);
463 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
464 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
465 * sizeof (PTRACE_XFER_TYPE
)),
469 /* Copy data to be written over corresponding part of buffer */
471 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
473 /* Write the entire buffer. */
475 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
478 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
487 linux_look_up_symbols (void)
489 /* Don't need to look up any symbols yet. */
493 static struct target_ops linux_target_ops
= {
494 linux_create_inferior
,
500 linux_fetch_registers
,
501 linux_store_registers
,
504 linux_look_up_symbols
,
508 initialize_low (void)
510 set_target_ops (&linux_target_ops
);