1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993 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. */
26 #include <sys/param.h>
30 #include <sys/signal.h>
32 #include <sys/kernel.h>
36 #include <sys/itimer.h>
38 #include <sys/resource.h>
41 #include <sys/ioctl.h>
47 static char my_registers[REGISTER_BYTES];
48 char *registers = my_registers;
50 #include <sys/ptrace.h>
52 /* Start an inferior process and returns its pid.
53 ALLARGS is a vector of program-name and args. */
56 create_inferior (program, allargs)
64 perror_with_name ("fork");
70 /* Switch child to it's own process group so that signals won't
71 directly affect gdbserver. */
75 ioctl (0, TIOCSPGRP, &pgrp);
77 ptrace (PTRACE_TRACEME, 0, (PTRACE_ARG3_TYPE) 0, 0);
79 execv (program, allargs);
81 fprintf (stderr, "GDBserver (process %d): Cannot exec %s: %s.\n",
83 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
91 /* Kill the inferior process. Make us have no inferior. */
96 if (inferior_pid == 0)
98 ptrace (PTRACE_KILL, inferior_pid, 0, 0);
104 /* Return nonzero if the given thread is still alive. */
109 /* Arggh. Apparently pthread_kill only works for threads within
110 the process that calls pthread_kill.
112 We want to avoid the lynx signal extensions as they simply don't
113 map well to the generic gdb interface we want to keep.
115 All we want to do is determine if a particular thread is alive;
116 it appears as if we can just make a harmless thread specific
117 ptrace call to do that. */
118 return (ptrace (PTRACE_THREADUSER,
119 BUILDPID (PIDGET (inferior_pid), pid), 0, 0) != -1);
122 /* Wait for process, returns status */
139 if (pid != PIDGET (inferior_pid))
140 perror_with_name ("wait");
142 thread_from_wait = w.w_tid;
143 inferior_pid = BUILDPID (inferior_pid, w.w_tid);
146 && WSTOPSIG (w) == SIGTRAP)
150 realsig = ptrace (PTRACE_GETTRACESIG, inferior_pid,
151 (PTRACE_ARG3_TYPE) 0, 0);
153 if (realsig == SIGNEWTHREAD)
155 /* It's a new thread notification. Nothing to do here since
156 the machine independent code in wait_for_inferior will
157 add the thread to the thread list and restart the thread
158 when pid != inferior_pid and pid is not in the thread list.
159 We don't even want to muck with realsig -- the code in
160 wait_for_inferior expects SIGTRAP. */
170 return ((unsigned char) WEXITSTATUS (w));
172 else if (!WIFSTOPPED (w))
175 return ((unsigned char) WTERMSIG (w));
178 fetch_inferior_registers (0);
181 return ((unsigned char) WSTOPSIG (w));
184 /* Resume execution of the inferior process.
185 If STEP is nonzero, single-step it.
186 If SIGNAL is nonzero, give it that signal. */
189 myresume (step, signal)
194 ptrace (step ? PTRACE_SINGLESTEP_ONE : PTRACE_CONT,
195 BUILDPID (inferior_pid, cont_thread == -1 ? 0 : cont_thread),
198 perror_with_name ("ptrace");
202 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
204 /* Mapping between GDB register #s and offsets into econtext. Must be
205 consistent with REGISTER_NAMES macro in various tmXXX.h files. */
207 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
210 /* Mappings from tm-i386v.h */
212 static int regmap[] =
228 X (ecode), /* Lynx doesn't give us either fs or gs, so */
229 X (fault), /* we just substitute these two in the hopes
230 that they are useful. */
235 /* Mappings from tm-m68k.h */
237 static int regmap[] =
239 X (regs[0]), /* d0 */
240 X (regs[1]), /* d1 */
241 X (regs[2]), /* d2 */
242 X (regs[3]), /* d3 */
243 X (regs[4]), /* d4 */
244 X (regs[5]), /* d5 */
245 X (regs[6]), /* d6 */
246 X (regs[7]), /* d7 */
247 X (regs[8]), /* a0 */
248 X (regs[9]), /* a1 */
249 X (regs[10]), /* a2 */
250 X (regs[11]), /* a3 */
251 X (regs[12]), /* a4 */
252 X (regs[13]), /* a5 */
253 X (regs[14]), /* fp */
258 X (fregs[0 * 3]), /* fp0 */
259 X (fregs[1 * 3]), /* fp1 */
260 X (fregs[2 * 3]), /* fp2 */
261 X (fregs[3 * 3]), /* fp3 */
262 X (fregs[4 * 3]), /* fp4 */
263 X (fregs[5 * 3]), /* fp5 */
264 X (fregs[6 * 3]), /* fp6 */
265 X (fregs[7 * 3]), /* fp7 */
267 X (fcregs[0]), /* fpcontrol */
268 X (fcregs[1]), /* fpstatus */
269 X (fcregs[2]), /* fpiaddr */
270 X (ssw), /* fpcode */
271 X (fault), /* fpflags */
276 /* Mappings from tm-sparc.h */
278 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
280 static int regmap[] =
287 -1, /* g5->g7 aren't saved by Lynx */
300 -1, -1, -1, -1, -1, -1, -1, -1, /* l0 -> l7 */
302 -1, -1, -1, -1, -1, -1, -1, -1, /* i0 -> i7 */
304 FX (f.fregs[0]), /* f0 */
350 /* This routine handles some oddball cases for Sparc registers and LynxOS.
351 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
352 It also handles knows where to find the I & L regs on the stack. */
355 fetch_inferior_registers (regno)
361 #define WHATREGS_FLOAT 1
362 #define WHATREGS_GEN 2
363 #define WHATREGS_STACK 4
366 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
367 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
368 whatregs = WHATREGS_STACK;
369 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
370 whatregs = WHATREGS_FLOAT;
372 whatregs = WHATREGS_GEN;
374 if (whatregs & WHATREGS_GEN)
376 struct econtext ec; /* general regs */
377 char buf[MAX_REGISTER_RAW_SIZE];
382 retval = ptrace (PTRACE_GETREGS,
383 BUILDPID (inferior_pid, general_thread),
384 (PTRACE_ARG3_TYPE) & ec,
387 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
389 memset (buf, 0, REGISTER_RAW_SIZE (G0_REGNUM));
390 supply_register (G0_REGNUM, buf);
391 supply_register (TBR_REGNUM, (char *) &ec.tbr);
393 memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &ec.g1,
394 4 * REGISTER_RAW_SIZE (G1_REGNUM));
395 for (i = G1_REGNUM; i <= G1_REGNUM + 3; i++)
396 register_valid[i] = 1;
398 supply_register (PS_REGNUM, (char *) &ec.psr);
399 supply_register (Y_REGNUM, (char *) &ec.y);
400 supply_register (PC_REGNUM, (char *) &ec.pc);
401 supply_register (NPC_REGNUM, (char *) &ec.npc);
402 supply_register (WIM_REGNUM, (char *) &ec.wim);
404 memcpy (®isters[REGISTER_BYTE (O0_REGNUM)], ec.o,
405 8 * REGISTER_RAW_SIZE (O0_REGNUM));
406 for (i = O0_REGNUM; i <= O0_REGNUM + 7; i++)
407 register_valid[i] = 1;
410 if (whatregs & WHATREGS_STACK)
415 sp = read_register (SP_REGNUM);
417 target_xfer_memory (sp + FRAME_SAVED_I0,
418 ®isters[REGISTER_BYTE (I0_REGNUM)],
419 8 * REGISTER_RAW_SIZE (I0_REGNUM), 0);
420 for (i = I0_REGNUM; i <= I7_REGNUM; i++)
421 register_valid[i] = 1;
423 target_xfer_memory (sp + FRAME_SAVED_L0,
424 ®isters[REGISTER_BYTE (L0_REGNUM)],
425 8 * REGISTER_RAW_SIZE (L0_REGNUM), 0);
426 for (i = L0_REGNUM; i <= L0_REGNUM + 7; i++)
427 register_valid[i] = 1;
430 if (whatregs & WHATREGS_FLOAT)
432 struct fcontext fc; /* fp regs */
437 retval = ptrace (PTRACE_GETFPREGS, BUILDPID (inferior_pid, general_thread), (PTRACE_ARG3_TYPE) & fc,
440 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
442 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fc.f.fregs,
443 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
444 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
445 register_valid[i] = 1;
447 supply_register (FPS_REGNUM, (char *) &fc.fsr);
452 /* This routine handles storing of the I & L regs for the Sparc. The trick
453 here is that they actually live on the stack. The really tricky part is
454 that when changing the stack pointer, the I & L regs must be written to
455 where the new SP points, otherwise the regs will be incorrect when the
456 process is started up again. We assume that the I & L regs are valid at
460 store_inferior_registers (regno)
467 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
468 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
469 whatregs = WHATREGS_STACK;
470 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
471 whatregs = WHATREGS_FLOAT;
472 else if (regno == SP_REGNUM)
473 whatregs = WHATREGS_STACK | WHATREGS_GEN;
475 whatregs = WHATREGS_GEN;
477 if (whatregs & WHATREGS_GEN)
479 struct econtext ec; /* general regs */
482 ec.tbr = read_register (TBR_REGNUM);
483 memcpy (&ec.g1, ®isters[REGISTER_BYTE (G1_REGNUM)],
484 4 * REGISTER_RAW_SIZE (G1_REGNUM));
486 ec.psr = read_register (PS_REGNUM);
487 ec.y = read_register (Y_REGNUM);
488 ec.pc = read_register (PC_REGNUM);
489 ec.npc = read_register (NPC_REGNUM);
490 ec.wim = read_register (WIM_REGNUM);
492 memcpy (ec.o, ®isters[REGISTER_BYTE (O0_REGNUM)],
493 8 * REGISTER_RAW_SIZE (O0_REGNUM));
496 retval = ptrace (PTRACE_SETREGS, BUILDPID (inferior_pid, general_thread), (PTRACE_ARG3_TYPE) & ec,
499 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
502 if (whatregs & WHATREGS_STACK)
507 sp = read_register (SP_REGNUM);
509 if (regno == -1 || regno == SP_REGNUM)
511 if (!register_valid[L0_REGNUM + 5])
513 target_xfer_memory (sp + FRAME_SAVED_I0,
514 ®isters[REGISTER_BYTE (I0_REGNUM)],
515 8 * REGISTER_RAW_SIZE (I0_REGNUM), 1);
517 target_xfer_memory (sp + FRAME_SAVED_L0,
518 ®isters[REGISTER_BYTE (L0_REGNUM)],
519 8 * REGISTER_RAW_SIZE (L0_REGNUM), 1);
521 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
523 if (!register_valid[regno])
525 if (regno >= L0_REGNUM && regno <= L0_REGNUM + 7)
526 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM)
529 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (I0_REGNUM)
531 target_xfer_memory (sp + regoffset, ®isters[REGISTER_BYTE (regno)],
532 REGISTER_RAW_SIZE (regno), 1);
536 if (whatregs & WHATREGS_FLOAT)
538 struct fcontext fc; /* fp regs */
541 /* We read fcontext first so that we can get good values for fq_t... */
543 retval = ptrace (PTRACE_GETFPREGS, BUILDPID (inferior_pid, general_thread), (PTRACE_ARG3_TYPE) & fc,
546 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
548 memcpy (fc.f.fregs, ®isters[REGISTER_BYTE (FP0_REGNUM)],
549 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
551 fc.fsr = read_register (FPS_REGNUM);
554 retval = ptrace (PTRACE_SETFPREGS, BUILDPID (inferior_pid, general_thread), (PTRACE_ARG3_TYPE) & fc,
557 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
565 /* Return the offset relative to the start of the per-thread data to the
566 saved context block. */
569 lynx_registers_addr ()
572 int ecpoff = offsetof (st_t, ecp);
576 stblock = (CORE_ADDR) ptrace (PTRACE_THREADUSER, BUILDPID (inferior_pid, general_thread),
577 (PTRACE_ARG3_TYPE) 0, 0);
579 perror_with_name ("PTRACE_THREADUSER");
581 ecp = (CORE_ADDR) ptrace (PTRACE_PEEKTHREAD, BUILDPID (inferior_pid, general_thread),
582 (PTRACE_ARG3_TYPE) ecpoff, 0);
584 perror_with_name ("lynx_registers_addr(PTRACE_PEEKTHREAD)");
586 return ecp - stblock;
589 /* Fetch one or more registers from the inferior. REGNO == -1 to get
590 them all. We actually fetch more than requested, when convenient,
591 marking them as valid so we won't fetch them again. */
594 fetch_inferior_registers (ignored)
601 ecp = lynx_registers_addr ();
603 for (regno = 0; regno < NUM_REGS; regno++)
605 int ptrace_fun = PTRACE_PEEKTHREAD;
607 #ifdef PTRACE_PEEKUSP
608 ptrace_fun = regno == SP_REGNUM ? PTRACE_PEEKUSP : PTRACE_PEEKTHREAD;
612 reg = ptrace (ptrace_fun, BUILDPID (inferior_pid, general_thread),
613 (PTRACE_ARG3_TYPE) (ecp + regmap[regno]), 0);
615 perror_with_name ("fetch_inferior_registers(PTRACE_PEEKTHREAD)");
617 *(unsigned long *) ®isters[REGISTER_BYTE (regno)] = reg;
621 /* Store our register values back into the inferior.
622 If REGNO is -1, do this for all registers.
623 Otherwise, REGNO specifies which register (so we can save time). */
626 store_inferior_registers (ignored)
633 ecp = lynx_registers_addr ();
635 for (regno = 0; regno < NUM_REGS; regno++)
637 int ptrace_fun = PTRACE_POKEUSER;
639 #ifdef PTRACE_POKEUSP
640 ptrace_fun = regno == SP_REGNUM ? PTRACE_POKEUSP : PTRACE_POKEUSER;
643 reg = *(unsigned long *) ®isters[REGISTER_BYTE (regno)];
646 ptrace (ptrace_fun, BUILDPID (inferior_pid, general_thread),
647 (PTRACE_ARG3_TYPE) (ecp + regmap[regno]), reg);
649 perror_with_name ("PTRACE_POKEUSER");
655 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
656 in the NEW_SUN_PTRACE case.
657 It ought to be straightforward. But it appears that writing did
658 not write the data that I specified. I cannot understand where
659 it got the data that it actually did write. */
661 /* Copy LEN bytes from inferior's memory starting at MEMADDR
662 to debugger memory starting at MYADDR. */
665 read_inferior_memory (memaddr, myaddr, len)
671 /* Round starting address down to longword boundary. */
672 register CORE_ADDR addr = memaddr & -sizeof (int);
673 /* Round ending address up; get number of longwords that makes. */
675 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
676 /* Allocate buffer of that many longwords. */
677 register int *buffer = (int *) alloca (count * sizeof (int));
679 /* Read all the longwords */
680 for (i = 0; i < count; i++, addr += sizeof (int))
682 buffer[i] = ptrace (PTRACE_PEEKTEXT, BUILDPID (inferior_pid, general_thread), addr, 0);
685 /* Copy appropriate bytes out of the buffer. */
686 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
689 /* Copy LEN bytes of data from debugger memory at MYADDR
690 to inferior's memory at MEMADDR.
691 On failure (cannot write the inferior)
692 returns the value of errno. */
695 write_inferior_memory (memaddr, myaddr, len)
701 /* Round starting address down to longword boundary. */
702 register CORE_ADDR addr = memaddr & -sizeof (int);
703 /* Round ending address up; get number of longwords that makes. */
705 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
706 /* Allocate buffer of that many longwords. */
707 register int *buffer = (int *) alloca (count * sizeof (int));
710 /* Fill start and end extra bytes of buffer with existing memory data. */
712 buffer[0] = ptrace (PTRACE_PEEKTEXT, BUILDPID (inferior_pid, general_thread), addr, 0);
717 = ptrace (PTRACE_PEEKTEXT, BUILDPID (inferior_pid, general_thread),
718 addr + (count - 1) * sizeof (int), 0);
721 /* Copy data to be written over corresponding part of buffer */
723 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
725 /* Write the entire buffer. */
727 for (i = 0; i < count; i++, addr += sizeof (int))
732 ptrace (PTRACE_POKETEXT, BUILDPID (inferior_pid, general_thread), addr, buffer[i]);
736 ptrace (PTRACE_POKETEXT): errno=%d, pid=0x%x, addr=0x%x, buffer[i] = 0x%x\n",
737 errno, BUILDPID (inferior_pid, general_thread),
739 fprintf (stderr, "Sleeping for 1 second\n");