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 static CORE_ADDR linux_bp_reinsert;
40 static void linux_resume (int step, int signal);
42 #define PTRACE_ARG3_TYPE long
43 #define PTRACE_XFER_TYPE long
45 #ifdef HAVE_LINUX_REGSETS
46 static int use_regsets_p = 1;
51 static int inferior_pid;
53 struct inferior_linux_data
58 /* Start an inferior process and returns its pid.
59 ALLARGS is a vector of program-name and args. */
62 linux_create_inferior (char *program, char **allargs)
64 struct inferior_linux_data *tdata;
69 perror_with_name ("fork");
73 ptrace (PTRACE_TRACEME, 0, 0, 0);
75 execv (program, allargs);
77 fprintf (stderr, "Cannot exec %s: %s.\n", program,
84 tdata = (struct inferior_linux_data *) malloc (sizeof (*tdata));
86 set_inferior_target_data (current_inferior, tdata);
93 /* Attach to an inferior process. */
96 linux_attach (int pid)
98 struct inferior_linux_data *tdata;
100 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
102 fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
103 errno < sys_nerr ? sys_errlist[errno] : "unknown error",
110 tdata = (struct inferior_linux_data *) malloc (sizeof (*tdata));
112 set_inferior_target_data (current_inferior, tdata);
116 /* Kill the inferior process. Make us have no inferior. */
121 if (inferior_pid == 0)
123 ptrace (PTRACE_KILL, inferior_pid, 0, 0);
128 /* Return nonzero if the given thread is still alive. */
130 linux_thread_alive (int pid)
136 linux_wait_for_one_inferior (struct inferior_info *child)
138 struct inferior_linux_data *child_data = inferior_target_data (child);
143 pid = waitpid (child_data->pid, &wstat, 0);
145 if (pid != child_data->pid)
146 perror_with_name ("wait");
148 /* If this target supports breakpoints, see if we hit one. */
149 if (the_low_target.stop_pc != NULL
150 && WIFSTOPPED (wstat)
151 && WSTOPSIG (wstat) == SIGTRAP)
155 if (linux_bp_reinsert != 0)
157 reinsert_breakpoint (linux_bp_reinsert);
158 linux_bp_reinsert = 0;
163 fetch_inferior_registers (0);
164 stop_pc = (*the_low_target.stop_pc) ();
166 if (check_breakpoints (stop_pc) != 0)
168 if (the_low_target.set_pc != NULL)
169 (*the_low_target.set_pc) (stop_pc);
171 if (the_low_target.breakpoint_reinsert_addr == NULL)
173 linux_bp_reinsert = stop_pc;
174 uninsert_breakpoint (stop_pc);
179 reinsert_breakpoint_by_bp
180 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
194 /* Wait for process, returns status */
197 linux_wait (char *status)
202 w = linux_wait_for_one_inferior (current_inferior);
207 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
210 return ((unsigned char) WEXITSTATUS (w));
212 else if (!WIFSTOPPED (w))
214 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
217 return ((unsigned char) WTERMSIG (w));
220 fetch_inferior_registers (0);
223 return ((unsigned char) WSTOPSIG (w));
226 /* Resume execution of the inferior process.
227 If STEP is nonzero, single-step it.
228 If SIGNAL is nonzero, give it that signal. */
231 linux_resume (int step, int signal)
234 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
236 perror_with_name ("ptrace");
240 #ifdef HAVE_LINUX_USRREGS
242 #define REGISTER_RAW_SIZE(regno) register_size((regno))
245 register_addr (int regnum)
249 if (regnum < 0 || regnum >= the_low_target.num_regs)
250 error ("Invalid register number %d.", regnum);
252 addr = the_low_target.regmap[regnum];
259 /* Fetch one register. */
261 fetch_register (int regno)
266 if (regno >= the_low_target.num_regs)
268 if ((*the_low_target.cannot_fetch_register) (regno))
271 regaddr = register_addr (regno);
274 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
277 *(PTRACE_XFER_TYPE *) (register_data (regno) + i) =
278 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
279 regaddr += sizeof (PTRACE_XFER_TYPE);
282 /* Warning, not error, in case we are attached; sometimes the
283 kernel doesn't let us at the registers. */
284 char *err = strerror (errno);
285 char *msg = alloca (strlen (err) + 128);
286 sprintf (msg, "reading register %d: %s", regno, err);
294 /* Fetch all registers, or just one, from the child process. */
296 usr_fetch_inferior_registers (int regno)
298 if (regno == -1 || regno == 0)
299 for (regno = 0; regno < the_low_target.num_regs; regno++)
300 fetch_register (regno);
302 fetch_register (regno);
305 /* Store our register values back into the inferior.
306 If REGNO is -1, do this for all registers.
307 Otherwise, REGNO specifies which register (so we can save time). */
309 usr_store_inferior_registers (int regno)
316 if (regno >= the_low_target.num_regs)
319 if ((*the_low_target.cannot_store_register) (regno) == 1)
322 regaddr = register_addr (regno);
326 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
329 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
330 *(int *) (register_data (regno) + i));
333 if ((*the_low_target.cannot_store_register) (regno) == 0)
335 char *err = strerror (errno);
336 char *msg = alloca (strlen (err) + 128);
337 sprintf (msg, "writing register %d: %s",
343 regaddr += sizeof (int);
347 for (regno = 0; regno < the_low_target.num_regs; regno++)
348 store_inferior_registers (regno);
350 #endif /* HAVE_LINUX_USRREGS */
354 #ifdef HAVE_LINUX_REGSETS
357 regsets_fetch_inferior_registers (void)
359 struct regset_info *regset;
361 regset = target_regsets;
363 while (regset->size >= 0)
368 if (regset->size == 0)
374 buf = malloc (regset->size);
375 res = ptrace (regset->get_request, inferior_pid, 0, buf);
380 /* If we get EIO on the first regset, do not try regsets again.
381 If we get EIO on a later regset, disable that regset. */
382 if (regset == target_regsets)
395 perror ("Warning: ptrace(regsets_fetch_inferior_registers)");
398 regset->store_function (buf);
405 regsets_store_inferior_registers (void)
407 struct regset_info *regset;
409 regset = target_regsets;
411 while (regset->size >= 0)
416 if (regset->size == 0)
422 buf = malloc (regset->size);
423 regset->fill_function (buf);
424 res = ptrace (regset->set_request, inferior_pid, 0, buf);
429 /* If we get EIO on the first regset, do not try regsets again.
430 If we get EIO on a later regset, disable that regset. */
431 if (regset == target_regsets)
444 perror ("Warning: ptrace(regsets_store_inferior_registers)");
452 #endif /* HAVE_LINUX_REGSETS */
456 linux_fetch_registers (int regno)
458 #ifdef HAVE_LINUX_REGSETS
461 if (regsets_fetch_inferior_registers () == 0)
465 #ifdef HAVE_LINUX_USRREGS
466 usr_fetch_inferior_registers (regno);
471 linux_store_registers (int regno)
473 #ifdef HAVE_LINUX_REGSETS
476 if (regsets_store_inferior_registers () == 0)
480 #ifdef HAVE_LINUX_USRREGS
481 usr_store_inferior_registers (regno);
486 /* Copy LEN bytes from inferior's memory starting at MEMADDR
487 to debugger memory starting at MYADDR. */
490 linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
493 /* Round starting address down to longword boundary. */
494 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
495 /* Round ending address up; get number of longwords that makes. */
497 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
498 / sizeof (PTRACE_XFER_TYPE);
499 /* Allocate buffer of that many longwords. */
500 register PTRACE_XFER_TYPE *buffer
501 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
503 /* Read all the longwords */
504 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
506 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
509 /* Copy appropriate bytes out of the buffer. */
510 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
513 /* Copy LEN bytes of data from debugger memory at MYADDR
514 to inferior's memory at MEMADDR.
515 On failure (cannot write the inferior)
516 returns the value of errno. */
519 linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
522 /* Round starting address down to longword boundary. */
523 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
524 /* Round ending address up; get number of longwords that makes. */
526 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
527 /* Allocate buffer of that many longwords. */
528 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
531 /* Fill start and end extra bytes of buffer with existing memory data. */
533 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
534 (PTRACE_ARG3_TYPE) addr, 0);
539 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
540 (PTRACE_ARG3_TYPE) (addr + (count - 1)
541 * sizeof (PTRACE_XFER_TYPE)),
545 /* Copy data to be written over corresponding part of buffer */
547 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
549 /* Write the entire buffer. */
551 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
554 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
563 linux_look_up_symbols (void)
565 /* Don't need to look up any symbols yet. */
569 static struct target_ops linux_target_ops = {
570 linux_create_inferior,
576 linux_fetch_registers,
577 linux_store_registers,
580 linux_look_up_symbols,
584 initialize_low (void)
586 set_target_ops (&linux_target_ops);
587 set_breakpoint_data (the_low_target.breakpoint,
588 the_low_target.breakpoint_len);