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 char registers
[REGISTER_BYTES
];
49 #include <sys/ptrace.h>
51 /* Start an inferior process and returns its pid.
52 ALLARGS is a vector of program-name and args. */
55 create_inferior (program
, allargs
)
63 perror_with_name ("fork");
69 /* Switch child to it's own process group so that signals won't
70 directly affect gdbserver. */
74 ioctl (0, TIOCSPGRP
, &pgrp
);
76 ptrace (PTRACE_TRACEME
, 0, (PTRACE_ARG3_TYPE
) 0, 0);
78 execv (program
, allargs
);
80 fprintf (stderr
, "GDBserver (process %d): Cannot exec %s: %s.\n",
82 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
90 /* Kill the inferior process. Make us have no inferior. */
95 if (inferior_pid
== 0)
97 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
103 /* Return nonzero if the given thread is still alive. */
108 /* Arggh. Apparently pthread_kill only works for threads within
109 the process that calls pthread_kill.
111 We want to avoid the lynx signal extensions as they simply don't
112 map well to the generic gdb interface we want to keep.
114 All we want to do is determine if a particular thread is alive;
115 it appears as if we can just make a harmless thread specific
116 ptrace call to do that. */
117 return (ptrace (PTRACE_THREADUSER
,
118 BUILDPID (PIDGET (inferior_pid
), pid
), 0, 0) != -1);
121 /* Wait for process, returns status */
138 if (pid
!= PIDGET (inferior_pid
))
139 perror_with_name ("wait");
141 thread_from_wait
= w
.w_tid
;
142 inferior_pid
= BUILDPID (inferior_pid
, w
.w_tid
);
145 && WSTOPSIG (w
) == SIGTRAP
)
149 realsig
= ptrace (PTRACE_GETTRACESIG
, inferior_pid
,
150 (PTRACE_ARG3_TYPE
) 0, 0);
152 if (realsig
== SIGNEWTHREAD
)
154 /* It's a new thread notification. Nothing to do here since
155 the machine independent code in wait_for_inferior will
156 add the thread to the thread list and restart the thread
157 when pid != inferior_pid and pid is not in the thread list.
158 We don't even want to muck with realsig -- the code in
159 wait_for_inferior expects SIGTRAP. */
169 return ((unsigned char) WEXITSTATUS (w
));
171 else if (!WIFSTOPPED (w
))
174 return ((unsigned char) WTERMSIG (w
));
177 fetch_inferior_registers (0);
180 return ((unsigned char) WSTOPSIG (w
));
183 /* Resume execution of the inferior process.
184 If STEP is nonzero, single-step it.
185 If SIGNAL is nonzero, give it that signal. */
188 myresume (step
, signal
)
193 ptrace (step
? PTRACE_SINGLESTEP_ONE
: PTRACE_CONT
,
194 BUILDPID (inferior_pid
, cont_thread
== -1 ? 0 : cont_thread
),
197 perror_with_name ("ptrace");
201 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
203 /* Mapping between GDB register #s and offsets into econtext. Must be
204 consistent with REGISTER_NAMES macro in various tmXXX.h files. */
206 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
209 /* Mappings from tm-i386v.h */
211 static int regmap
[] =
227 X (ecode
), /* Lynx doesn't give us either fs or gs, so */
228 X (fault
), /* we just substitute these two in the hopes
229 that they are useful. */
234 /* Mappings from tm-m68k.h */
236 static int regmap
[] =
238 X (regs
[0]), /* d0 */
239 X (regs
[1]), /* d1 */
240 X (regs
[2]), /* d2 */
241 X (regs
[3]), /* d3 */
242 X (regs
[4]), /* d4 */
243 X (regs
[5]), /* d5 */
244 X (regs
[6]), /* d6 */
245 X (regs
[7]), /* d7 */
246 X (regs
[8]), /* a0 */
247 X (regs
[9]), /* a1 */
248 X (regs
[10]), /* a2 */
249 X (regs
[11]), /* a3 */
250 X (regs
[12]), /* a4 */
251 X (regs
[13]), /* a5 */
252 X (regs
[14]), /* fp */
257 X (fregs
[0 * 3]), /* fp0 */
258 X (fregs
[1 * 3]), /* fp1 */
259 X (fregs
[2 * 3]), /* fp2 */
260 X (fregs
[3 * 3]), /* fp3 */
261 X (fregs
[4 * 3]), /* fp4 */
262 X (fregs
[5 * 3]), /* fp5 */
263 X (fregs
[6 * 3]), /* fp6 */
264 X (fregs
[7 * 3]), /* fp7 */
266 X (fcregs
[0]), /* fpcontrol */
267 X (fcregs
[1]), /* fpstatus */
268 X (fcregs
[2]), /* fpiaddr */
269 X (ssw
), /* fpcode */
270 X (fault
), /* fpflags */
275 /* Mappings from tm-sparc.h */
277 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
279 static int regmap
[] =
286 -1, /* g5->g7 aren't saved by Lynx */
299 -1, -1, -1, -1, -1, -1, -1, -1, /* l0 -> l7 */
301 -1, -1, -1, -1, -1, -1, -1, -1, /* i0 -> i7 */
303 FX (f
.fregs
[0]), /* f0 */
349 /* This routine handles some oddball cases for Sparc registers and LynxOS.
350 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
351 It also handles knows where to find the I & L regs on the stack. */
354 fetch_inferior_registers (regno
)
360 #define WHATREGS_FLOAT 1
361 #define WHATREGS_GEN 2
362 #define WHATREGS_STACK 4
365 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
366 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
367 whatregs
= WHATREGS_STACK
;
368 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
369 whatregs
= WHATREGS_FLOAT
;
371 whatregs
= WHATREGS_GEN
;
373 if (whatregs
& WHATREGS_GEN
)
375 struct econtext ec
; /* general regs */
376 char buf
[MAX_REGISTER_RAW_SIZE
];
381 retval
= ptrace (PTRACE_GETREGS
,
382 BUILDPID (inferior_pid
, general_thread
),
383 (PTRACE_ARG3_TYPE
) & ec
,
386 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
388 memset (buf
, 0, REGISTER_RAW_SIZE (G0_REGNUM
));
389 supply_register (G0_REGNUM
, buf
);
390 supply_register (TBR_REGNUM
, (char *) &ec
.tbr
);
392 memcpy (®isters
[REGISTER_BYTE (G1_REGNUM
)], &ec
.g1
,
393 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
394 for (i
= G1_REGNUM
; i
<= G1_REGNUM
+ 3; i
++)
395 register_valid
[i
] = 1;
397 supply_register (PS_REGNUM
, (char *) &ec
.psr
);
398 supply_register (Y_REGNUM
, (char *) &ec
.y
);
399 supply_register (PC_REGNUM
, (char *) &ec
.pc
);
400 supply_register (NPC_REGNUM
, (char *) &ec
.npc
);
401 supply_register (WIM_REGNUM
, (char *) &ec
.wim
);
403 memcpy (®isters
[REGISTER_BYTE (O0_REGNUM
)], ec
.o
,
404 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
405 for (i
= O0_REGNUM
; i
<= O0_REGNUM
+ 7; i
++)
406 register_valid
[i
] = 1;
409 if (whatregs
& WHATREGS_STACK
)
414 sp
= read_register (SP_REGNUM
);
416 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
417 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
418 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 0);
419 for (i
= I0_REGNUM
; i
<= I7_REGNUM
; i
++)
420 register_valid
[i
] = 1;
422 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
423 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
424 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 0);
425 for (i
= L0_REGNUM
; i
<= L0_REGNUM
+ 7; i
++)
426 register_valid
[i
] = 1;
429 if (whatregs
& WHATREGS_FLOAT
)
431 struct fcontext fc
; /* fp regs */
436 retval
= ptrace (PTRACE_GETFPREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & fc
,
439 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
441 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], fc
.f
.fregs
,
442 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
443 for (i
= FP0_REGNUM
; i
<= FP0_REGNUM
+ 31; i
++)
444 register_valid
[i
] = 1;
446 supply_register (FPS_REGNUM
, (char *) &fc
.fsr
);
451 /* This routine handles storing of the I & L regs for the Sparc. The trick
452 here is that they actually live on the stack. The really tricky part is
453 that when changing the stack pointer, the I & L regs must be written to
454 where the new SP points, otherwise the regs will be incorrect when the
455 process is started up again. We assume that the I & L regs are valid at
459 store_inferior_registers (regno
)
466 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
467 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
468 whatregs
= WHATREGS_STACK
;
469 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
470 whatregs
= WHATREGS_FLOAT
;
471 else if (regno
== SP_REGNUM
)
472 whatregs
= WHATREGS_STACK
| WHATREGS_GEN
;
474 whatregs
= WHATREGS_GEN
;
476 if (whatregs
& WHATREGS_GEN
)
478 struct econtext ec
; /* general regs */
481 ec
.tbr
= read_register (TBR_REGNUM
);
482 memcpy (&ec
.g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
483 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
485 ec
.psr
= read_register (PS_REGNUM
);
486 ec
.y
= read_register (Y_REGNUM
);
487 ec
.pc
= read_register (PC_REGNUM
);
488 ec
.npc
= read_register (NPC_REGNUM
);
489 ec
.wim
= read_register (WIM_REGNUM
);
491 memcpy (ec
.o
, ®isters
[REGISTER_BYTE (O0_REGNUM
)],
492 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
495 retval
= ptrace (PTRACE_SETREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & ec
,
498 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
501 if (whatregs
& WHATREGS_STACK
)
506 sp
= read_register (SP_REGNUM
);
508 if (regno
== -1 || regno
== SP_REGNUM
)
510 if (!register_valid
[L0_REGNUM
+ 5])
512 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
513 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
514 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 1);
516 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
517 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
518 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 1);
520 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
522 if (!register_valid
[regno
])
524 if (regno
>= L0_REGNUM
&& regno
<= L0_REGNUM
+ 7)
525 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (L0_REGNUM
)
528 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (I0_REGNUM
)
530 target_xfer_memory (sp
+ regoffset
, ®isters
[REGISTER_BYTE (regno
)],
531 REGISTER_RAW_SIZE (regno
), 1);
535 if (whatregs
& WHATREGS_FLOAT
)
537 struct fcontext fc
; /* fp regs */
540 /* We read fcontext first so that we can get good values for fq_t... */
542 retval
= ptrace (PTRACE_GETFPREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & fc
,
545 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
547 memcpy (fc
.f
.fregs
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
548 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
550 fc
.fsr
= read_register (FPS_REGNUM
);
553 retval
= ptrace (PTRACE_SETFPREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & fc
,
556 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
564 /* Return the offset relative to the start of the per-thread data to the
565 saved context block. */
568 lynx_registers_addr ()
571 int ecpoff
= offsetof (st_t
, ecp
);
575 stblock
= (CORE_ADDR
) ptrace (PTRACE_THREADUSER
, BUILDPID (inferior_pid
, general_thread
),
576 (PTRACE_ARG3_TYPE
) 0, 0);
578 perror_with_name ("PTRACE_THREADUSER");
580 ecp
= (CORE_ADDR
) ptrace (PTRACE_PEEKTHREAD
, BUILDPID (inferior_pid
, general_thread
),
581 (PTRACE_ARG3_TYPE
) ecpoff
, 0);
583 perror_with_name ("lynx_registers_addr(PTRACE_PEEKTHREAD)");
585 return ecp
- stblock
;
588 /* Fetch one or more registers from the inferior. REGNO == -1 to get
589 them all. We actually fetch more than requested, when convenient,
590 marking them as valid so we won't fetch them again. */
593 fetch_inferior_registers (ignored
)
600 ecp
= lynx_registers_addr ();
602 for (regno
= 0; regno
< NUM_REGS
; regno
++)
604 int ptrace_fun
= PTRACE_PEEKTHREAD
;
606 #ifdef PTRACE_PEEKUSP
607 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_PEEKUSP
: PTRACE_PEEKTHREAD
;
611 reg
= ptrace (ptrace_fun
, BUILDPID (inferior_pid
, general_thread
),
612 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
]), 0);
614 perror_with_name ("fetch_inferior_registers(PTRACE_PEEKTHREAD)");
616 *(unsigned long *) ®isters
[REGISTER_BYTE (regno
)] = reg
;
620 /* Store our register values back into the inferior.
621 If REGNO is -1, do this for all registers.
622 Otherwise, REGNO specifies which register (so we can save time). */
625 store_inferior_registers (ignored
)
632 ecp
= lynx_registers_addr ();
634 for (regno
= 0; regno
< NUM_REGS
; regno
++)
636 int ptrace_fun
= PTRACE_POKEUSER
;
638 #ifdef PTRACE_POKEUSP
639 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_POKEUSP
: PTRACE_POKEUSER
;
642 reg
= *(unsigned long *) ®isters
[REGISTER_BYTE (regno
)];
645 ptrace (ptrace_fun
, BUILDPID (inferior_pid
, general_thread
),
646 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
]), reg
);
648 perror_with_name ("PTRACE_POKEUSER");
654 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
655 in the NEW_SUN_PTRACE case.
656 It ought to be straightforward. But it appears that writing did
657 not write the data that I specified. I cannot understand where
658 it got the data that it actually did write. */
660 /* Copy LEN bytes from inferior's memory starting at MEMADDR
661 to debugger memory starting at MYADDR. */
664 read_inferior_memory (memaddr
, myaddr
, len
)
670 /* Round starting address down to longword boundary. */
671 register CORE_ADDR addr
= memaddr
& -sizeof (int);
672 /* Round ending address up; get number of longwords that makes. */
674 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
675 /* Allocate buffer of that many longwords. */
676 register int *buffer
= (int *) alloca (count
* sizeof (int));
678 /* Read all the longwords */
679 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
681 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, BUILDPID (inferior_pid
, general_thread
), addr
, 0);
684 /* Copy appropriate bytes out of the buffer. */
685 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
688 /* Copy LEN bytes of data from debugger memory at MYADDR
689 to inferior's memory at MEMADDR.
690 On failure (cannot write the inferior)
691 returns the value of errno. */
694 write_inferior_memory (memaddr
, myaddr
, len
)
700 /* Round starting address down to longword boundary. */
701 register CORE_ADDR addr
= memaddr
& -sizeof (int);
702 /* Round ending address up; get number of longwords that makes. */
704 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
705 /* Allocate buffer of that many longwords. */
706 register int *buffer
= (int *) alloca (count
* sizeof (int));
709 /* Fill start and end extra bytes of buffer with existing memory data. */
711 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, BUILDPID (inferior_pid
, general_thread
), addr
, 0);
716 = ptrace (PTRACE_PEEKTEXT
, BUILDPID (inferior_pid
, general_thread
),
717 addr
+ (count
- 1) * sizeof (int), 0);
720 /* Copy data to be written over corresponding part of buffer */
722 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
724 /* Write the entire buffer. */
726 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
731 ptrace (PTRACE_POKETEXT
, BUILDPID (inferior_pid
, general_thread
), addr
, buffer
[i
]);
735 ptrace (PTRACE_POKETEXT): errno=%d, pid=0x%x, addr=0x%x, buffer[i] = 0x%x\n",
736 errno
, BUILDPID (inferior_pid
, general_thread
),
738 fprintf (stderr
, "Sleeping for 1 second\n");
This page took 0.045839 seconds and 5 git commands to generate.