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 (char *program
, char **allargs
)
62 perror_with_name ("fork");
68 /* Switch child to it's own process group so that signals won't
69 directly affect gdbserver. */
73 ioctl (0, TIOCSPGRP
, &pgrp
);
75 ptrace (PTRACE_TRACEME
, 0, (PTRACE_ARG3_TYPE
) 0, 0);
77 execv (program
, allargs
);
79 fprintf (stderr
, "GDBserver (process %d): Cannot exec %s: %s.\n",
81 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
89 /* Kill the inferior process. Make us have no inferior. */
94 if (inferior_pid
== 0)
96 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
102 /* Return nonzero if the given thread is still alive. */
104 mythread_alive (int pid
)
106 /* Arggh. Apparently pthread_kill only works for threads within
107 the process that calls pthread_kill.
109 We want to avoid the lynx signal extensions as they simply don't
110 map well to the generic gdb interface we want to keep.
112 All we want to do is determine if a particular thread is alive;
113 it appears as if we can just make a harmless thread specific
114 ptrace call to do that. */
115 return (ptrace (PTRACE_THREADUSER
,
116 BUILDPID (PIDGET (inferior_pid
), pid
), 0, 0) != -1);
119 /* Wait for process, returns status */
122 mywait (char *status
)
135 if (pid
!= PIDGET (inferior_pid
))
136 perror_with_name ("wait");
138 thread_from_wait
= w
.w_tid
;
139 inferior_pid
= BUILDPID (inferior_pid
, w
.w_tid
);
142 && WSTOPSIG (w
) == SIGTRAP
)
146 realsig
= ptrace (PTRACE_GETTRACESIG
, inferior_pid
,
147 (PTRACE_ARG3_TYPE
) 0, 0);
149 if (realsig
== SIGNEWTHREAD
)
151 /* It's a new thread notification. Nothing to do here since
152 the machine independent code in wait_for_inferior will
153 add the thread to the thread list and restart the thread
154 when pid != inferior_pid and pid is not in the thread list.
155 We don't even want to muck with realsig -- the code in
156 wait_for_inferior expects SIGTRAP. */
166 return ((unsigned char) WEXITSTATUS (w
));
168 else if (!WIFSTOPPED (w
))
171 return ((unsigned char) WTERMSIG (w
));
174 fetch_inferior_registers (0);
177 return ((unsigned char) WSTOPSIG (w
));
180 /* Resume execution of the inferior process.
181 If STEP is nonzero, single-step it.
182 If SIGNAL is nonzero, give it that signal. */
185 myresume (int step
, int signal
)
188 ptrace (step
? PTRACE_SINGLESTEP_ONE
: PTRACE_CONT
,
189 BUILDPID (inferior_pid
, cont_thread
== -1 ? 0 : cont_thread
),
192 perror_with_name ("ptrace");
196 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
198 /* Mapping between GDB register #s and offsets into econtext. Must be
199 consistent with REGISTER_NAMES macro in various tmXXX.h files. */
201 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
204 /* Mappings from tm-i386v.h */
206 static int regmap
[] =
222 X (ecode
), /* Lynx doesn't give us either fs or gs, so */
223 X (fault
), /* we just substitute these two in the hopes
224 that they are useful. */
229 /* Mappings from tm-m68k.h */
231 static int regmap
[] =
233 X (regs
[0]), /* d0 */
234 X (regs
[1]), /* d1 */
235 X (regs
[2]), /* d2 */
236 X (regs
[3]), /* d3 */
237 X (regs
[4]), /* d4 */
238 X (regs
[5]), /* d5 */
239 X (regs
[6]), /* d6 */
240 X (regs
[7]), /* d7 */
241 X (regs
[8]), /* a0 */
242 X (regs
[9]), /* a1 */
243 X (regs
[10]), /* a2 */
244 X (regs
[11]), /* a3 */
245 X (regs
[12]), /* a4 */
246 X (regs
[13]), /* a5 */
247 X (regs
[14]), /* fp */
252 X (fregs
[0 * 3]), /* fp0 */
253 X (fregs
[1 * 3]), /* fp1 */
254 X (fregs
[2 * 3]), /* fp2 */
255 X (fregs
[3 * 3]), /* fp3 */
256 X (fregs
[4 * 3]), /* fp4 */
257 X (fregs
[5 * 3]), /* fp5 */
258 X (fregs
[6 * 3]), /* fp6 */
259 X (fregs
[7 * 3]), /* fp7 */
261 X (fcregs
[0]), /* fpcontrol */
262 X (fcregs
[1]), /* fpstatus */
263 X (fcregs
[2]), /* fpiaddr */
264 X (ssw
), /* fpcode */
265 X (fault
), /* fpflags */
270 /* Mappings from tm-sparc.h */
272 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
274 static int regmap
[] =
281 -1, /* g5->g7 aren't saved by Lynx */
294 -1, -1, -1, -1, -1, -1, -1, -1, /* l0 -> l7 */
296 -1, -1, -1, -1, -1, -1, -1, -1, /* i0 -> i7 */
298 FX (f
.fregs
[0]), /* f0 */
344 /* This routine handles some oddball cases for Sparc registers and LynxOS.
345 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
346 It also handles knows where to find the I & L regs on the stack. */
349 fetch_inferior_registers (int regno
)
354 #define WHATREGS_FLOAT 1
355 #define WHATREGS_GEN 2
356 #define WHATREGS_STACK 4
359 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
360 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
361 whatregs
= WHATREGS_STACK
;
362 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
363 whatregs
= WHATREGS_FLOAT
;
365 whatregs
= WHATREGS_GEN
;
367 if (whatregs
& WHATREGS_GEN
)
369 struct econtext ec
; /* general regs */
370 char buf
[MAX_REGISTER_RAW_SIZE
];
375 retval
= ptrace (PTRACE_GETREGS
,
376 BUILDPID (inferior_pid
, general_thread
),
377 (PTRACE_ARG3_TYPE
) & ec
,
380 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
382 memset (buf
, 0, REGISTER_RAW_SIZE (G0_REGNUM
));
383 supply_register (G0_REGNUM
, buf
);
384 supply_register (TBR_REGNUM
, (char *) &ec
.tbr
);
386 memcpy (®isters
[REGISTER_BYTE (G1_REGNUM
)], &ec
.g1
,
387 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
388 for (i
= G1_REGNUM
; i
<= G1_REGNUM
+ 3; i
++)
389 register_valid
[i
] = 1;
391 supply_register (PS_REGNUM
, (char *) &ec
.psr
);
392 supply_register (Y_REGNUM
, (char *) &ec
.y
);
393 supply_register (PC_REGNUM
, (char *) &ec
.pc
);
394 supply_register (NPC_REGNUM
, (char *) &ec
.npc
);
395 supply_register (WIM_REGNUM
, (char *) &ec
.wim
);
397 memcpy (®isters
[REGISTER_BYTE (O0_REGNUM
)], ec
.o
,
398 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
399 for (i
= O0_REGNUM
; i
<= O0_REGNUM
+ 7; i
++)
400 register_valid
[i
] = 1;
403 if (whatregs
& WHATREGS_STACK
)
408 sp
= read_register (SP_REGNUM
);
410 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
411 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
412 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 0);
413 for (i
= I0_REGNUM
; i
<= I7_REGNUM
; i
++)
414 register_valid
[i
] = 1;
416 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
417 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
418 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 0);
419 for (i
= L0_REGNUM
; i
<= L0_REGNUM
+ 7; i
++)
420 register_valid
[i
] = 1;
423 if (whatregs
& WHATREGS_FLOAT
)
425 struct fcontext fc
; /* fp regs */
430 retval
= ptrace (PTRACE_GETFPREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & fc
,
433 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
435 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], fc
.f
.fregs
,
436 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
437 for (i
= FP0_REGNUM
; i
<= FP0_REGNUM
+ 31; i
++)
438 register_valid
[i
] = 1;
440 supply_register (FPS_REGNUM
, (char *) &fc
.fsr
);
445 /* This routine handles storing of the I & L regs for the Sparc. The trick
446 here is that they actually live on the stack. The really tricky part is
447 that when changing the stack pointer, the I & L regs must be written to
448 where the new SP points, otherwise the regs will be incorrect when the
449 process is started up again. We assume that the I & L regs are valid at
453 store_inferior_registers (int regno
)
459 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
460 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
461 whatregs
= WHATREGS_STACK
;
462 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
463 whatregs
= WHATREGS_FLOAT
;
464 else if (regno
== SP_REGNUM
)
465 whatregs
= WHATREGS_STACK
| WHATREGS_GEN
;
467 whatregs
= WHATREGS_GEN
;
469 if (whatregs
& WHATREGS_GEN
)
471 struct econtext ec
; /* general regs */
474 ec
.tbr
= read_register (TBR_REGNUM
);
475 memcpy (&ec
.g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
476 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
478 ec
.psr
= read_register (PS_REGNUM
);
479 ec
.y
= read_register (Y_REGNUM
);
480 ec
.pc
= read_register (PC_REGNUM
);
481 ec
.npc
= read_register (NPC_REGNUM
);
482 ec
.wim
= read_register (WIM_REGNUM
);
484 memcpy (ec
.o
, ®isters
[REGISTER_BYTE (O0_REGNUM
)],
485 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
488 retval
= ptrace (PTRACE_SETREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & ec
,
491 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
494 if (whatregs
& WHATREGS_STACK
)
499 sp
= read_register (SP_REGNUM
);
501 if (regno
== -1 || regno
== SP_REGNUM
)
503 if (!register_valid
[L0_REGNUM
+ 5])
505 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
506 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
507 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 1);
509 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
510 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
511 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 1);
513 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
515 if (!register_valid
[regno
])
517 if (regno
>= L0_REGNUM
&& regno
<= L0_REGNUM
+ 7)
518 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (L0_REGNUM
)
521 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (I0_REGNUM
)
523 target_xfer_memory (sp
+ regoffset
, ®isters
[REGISTER_BYTE (regno
)],
524 REGISTER_RAW_SIZE (regno
), 1);
528 if (whatregs
& WHATREGS_FLOAT
)
530 struct fcontext fc
; /* fp regs */
533 /* We read fcontext first so that we can get good values for fq_t... */
535 retval
= ptrace (PTRACE_GETFPREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & fc
,
538 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
540 memcpy (fc
.f
.fregs
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
541 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
543 fc
.fsr
= read_register (FPS_REGNUM
);
546 retval
= ptrace (PTRACE_SETFPREGS
, BUILDPID (inferior_pid
, general_thread
), (PTRACE_ARG3_TYPE
) & fc
,
549 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
557 /* Return the offset relative to the start of the per-thread data to the
558 saved context block. */
561 lynx_registers_addr (void)
564 int ecpoff
= offsetof (st_t
, ecp
);
568 stblock
= (CORE_ADDR
) ptrace (PTRACE_THREADUSER
, BUILDPID (inferior_pid
, general_thread
),
569 (PTRACE_ARG3_TYPE
) 0, 0);
571 perror_with_name ("PTRACE_THREADUSER");
573 ecp
= (CORE_ADDR
) ptrace (PTRACE_PEEKTHREAD
, BUILDPID (inferior_pid
, general_thread
),
574 (PTRACE_ARG3_TYPE
) ecpoff
, 0);
576 perror_with_name ("lynx_registers_addr(PTRACE_PEEKTHREAD)");
578 return ecp
- stblock
;
581 /* Fetch one or more registers from the inferior. REGNO == -1 to get
582 them all. We actually fetch more than requested, when convenient,
583 marking them as valid so we won't fetch them again. */
586 fetch_inferior_registers (int ignored
)
592 ecp
= lynx_registers_addr ();
594 for (regno
= 0; regno
< NUM_REGS
; regno
++)
596 int ptrace_fun
= PTRACE_PEEKTHREAD
;
598 #ifdef PTRACE_PEEKUSP
599 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_PEEKUSP
: PTRACE_PEEKTHREAD
;
603 reg
= ptrace (ptrace_fun
, BUILDPID (inferior_pid
, general_thread
),
604 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
]), 0);
606 perror_with_name ("fetch_inferior_registers(PTRACE_PEEKTHREAD)");
608 *(unsigned long *) ®isters
[REGISTER_BYTE (regno
)] = reg
;
612 /* Store our register values back into the inferior.
613 If REGNO is -1, do this for all registers.
614 Otherwise, REGNO specifies which register (so we can save time). */
617 store_inferior_registers (int ignored
)
623 ecp
= lynx_registers_addr ();
625 for (regno
= 0; regno
< NUM_REGS
; regno
++)
627 int ptrace_fun
= PTRACE_POKEUSER
;
629 #ifdef PTRACE_POKEUSP
630 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_POKEUSP
: PTRACE_POKEUSER
;
633 reg
= *(unsigned long *) ®isters
[REGISTER_BYTE (regno
)];
636 ptrace (ptrace_fun
, BUILDPID (inferior_pid
, general_thread
),
637 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
]), reg
);
639 perror_with_name ("PTRACE_POKEUSER");
645 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
646 in the NEW_SUN_PTRACE case.
647 It ought to be straightforward. But it appears that writing did
648 not write the data that I specified. I cannot understand where
649 it got the data that it actually did write. */
651 /* Copy LEN bytes from inferior's memory starting at MEMADDR
652 to debugger memory starting at MYADDR. */
655 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
658 /* Round starting address down to longword boundary. */
659 register CORE_ADDR addr
= memaddr
& -sizeof (int);
660 /* Round ending address up; get number of longwords that makes. */
662 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
663 /* Allocate buffer of that many longwords. */
664 register int *buffer
= (int *) alloca (count
* sizeof (int));
666 /* Read all the longwords */
667 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
669 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, BUILDPID (inferior_pid
, general_thread
), addr
, 0);
672 /* Copy appropriate bytes out of the buffer. */
673 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
676 /* Copy LEN bytes of data from debugger memory at MYADDR
677 to inferior's memory at MEMADDR.
678 On failure (cannot write the inferior)
679 returns the value of errno. */
682 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
685 /* Round starting address down to longword boundary. */
686 register CORE_ADDR addr
= memaddr
& -sizeof (int);
687 /* Round ending address up; get number of longwords that makes. */
689 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
690 /* Allocate buffer of that many longwords. */
691 register int *buffer
= (int *) alloca (count
* sizeof (int));
694 /* Fill start and end extra bytes of buffer with existing memory data. */
696 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, BUILDPID (inferior_pid
, general_thread
), addr
, 0);
701 = ptrace (PTRACE_PEEKTEXT
, BUILDPID (inferior_pid
, general_thread
),
702 addr
+ (count
- 1) * sizeof (int), 0);
705 /* Copy data to be written over corresponding part of buffer */
707 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
709 /* Write the entire buffer. */
711 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
716 ptrace (PTRACE_POKETEXT
, BUILDPID (inferior_pid
, general_thread
), addr
, buffer
[i
]);
720 ptrace (PTRACE_POKETEXT): errno=%d, pid=0x%x, addr=0x%x, buffer[i] = 0x%x\n",
721 errno
, BUILDPID (inferior_pid
, general_thread
),
723 fprintf (stderr
, "Sleeping for 1 second\n");
735 initialize_low (void)
This page took 0.045653 seconds and 4 git commands to generate.