(sim_resume): Clarify use of SIGGNAL.
[deliverable/binutils-gdb.git] / gdb / infptrace.c
1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 89, 90, 91, 92, 93, 94, 95, 96, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "gdb_string.h"
27
28 #ifdef HAVE_WAIT_H
29 #include <wait.h>
30 #else
31 #ifdef HAVE_SYS_WAIT_H
32 #include <sys/wait.h>
33 #endif
34 #endif
35
36 /* "wait.h" fills in the gaps left by <wait.h> */
37 #include "wait.h" /* NOTE: This is ../include/wait.h */
38
39 #include "command.h"
40
41 #ifdef USG
42 #include <sys/types.h>
43 #endif
44
45 #include <sys/param.h>
46 #include <sys/dir.h>
47 #include <signal.h>
48 #include <sys/ioctl.h>
49
50 #ifdef HAVE_PTRACE_H
51 #include <ptrace.h>
52 #else
53 #ifdef HAVE_SYS_PTRACE_H
54 #include <sys/ptrace.h>
55 #endif
56 #endif
57
58 #if !defined (PT_READ_I)
59 #define PT_READ_I 1 /* Read word from text space */
60 #endif
61 #if !defined (PT_READ_D)
62 #define PT_READ_D 2 /* Read word from data space */
63 #endif
64 #if !defined (PT_READ_U)
65 #define PT_READ_U 3 /* Read word from kernel user struct */
66 #endif
67 #if !defined (PT_WRITE_I)
68 #define PT_WRITE_I 4 /* Write word to text space */
69 #endif
70 #if !defined (PT_WRITE_D)
71 #define PT_WRITE_D 5 /* Write word to data space */
72 #endif
73 #if !defined (PT_WRITE_U)
74 #define PT_WRITE_U 6 /* Write word to kernel user struct */
75 #endif
76 #if !defined (PT_CONTINUE)
77 #define PT_CONTINUE 7 /* Continue after signal */
78 #endif
79 #if !defined (PT_STEP)
80 #define PT_STEP 9 /* Set flag for single stepping */
81 #endif
82 #if !defined (PT_KILL)
83 #define PT_KILL 8 /* Send child a SIGKILL signal */
84 #endif
85
86 #ifndef PT_ATTACH
87 #define PT_ATTACH PTRACE_ATTACH
88 #endif
89 #ifndef PT_DETACH
90 #define PT_DETACH PTRACE_DETACH
91 #endif
92
93 #include "gdbcore.h"
94 #ifndef NO_SYS_FILE
95 #include <sys/file.h>
96 #endif
97 #if 0
98 /* Don't think this is used anymore. On the sequent (not sure whether it's
99 dynix or ptx or both), it is included unconditionally by sys/user.h and
100 not protected against multiple inclusion. */
101 #include "gdb_stat.h"
102 #endif
103
104 #if !defined (FETCH_INFERIOR_REGISTERS)
105 #include <sys/user.h> /* Probably need to poke the user structure */
106 #if defined (KERNEL_U_ADDR_BSD)
107 #include <a.out.h> /* For struct nlist */
108 #endif /* KERNEL_U_ADDR_BSD. */
109 #endif /* !FETCH_INFERIOR_REGISTERS */
110
111 #if !defined (CHILD_XFER_MEMORY)
112 static void udot_info PARAMS ((char *, int));
113 #endif
114
115 #if !defined (FETCH_INFERIOR_REGISTERS)
116 static void fetch_register PARAMS ((int));
117 static void store_register PARAMS ((int));
118 #endif
119
120 /*
121 * Some systems (Linux) may have threads implemented as pseudo-processes,
122 * in which case we may be tracing more than one process at a time.
123 * In that case, inferior_pid will contain the main process ID and the
124 * individual thread (process) id mashed together. These macros are
125 * used to separate them out. The definitions may be overridden in tm.h
126 *
127 * NOTE: default definitions here are for systems with no threads.
128 * Useful definitions MUST be provided in tm.h
129 */
130
131 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
132 #define PIDGET(PID) PID
133 #define TIDGET(PID) 0
134 #endif
135
136 void _initialize_kernel_u_addr PARAMS ((void));
137 void _initialize_infptrace PARAMS ((void));
138 \f
139
140 /* This function simply calls ptrace with the given arguments.
141 It exists so that all calls to ptrace are isolated in this
142 machine-dependent file. */
143 int
144 call_ptrace (request, pid, addr, data)
145 int request, pid;
146 PTRACE_ARG3_TYPE addr;
147 int data;
148 {
149 int pt_status = 0;
150
151 #if 0
152 int saved_errno;
153
154 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
155 request, pid, addr, data);
156 #endif
157 #if defined(PT_SETTRC)
158 /* If the parent can be told to attach to us, try to do it. */
159 if (request == PT_SETTRC)
160 {
161 errno = 0;
162 #if !defined (FIVE_ARG_PTRACE)
163 pt_status = ptrace (PT_SETTRC, pid, addr, data);
164 #else
165 /* Deal with HPUX 8.0 braindamage. We never use the
166 calls which require the fifth argument. */
167 pt_status = ptrace (PT_SETTRC, pid, addr, data, 0);
168 #endif
169 if (errno)
170 perror_with_name ("ptrace");
171 #if 0
172 printf (" = %d\n", pt_status);
173 #endif
174 if (pt_status < 0)
175 return pt_status;
176 else
177 return parent_attach_all (pid, addr, data);
178 }
179 #endif
180
181 #if defined(PT_CONTIN1)
182 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
183 signals. If it's available, use it. */
184 if (request == PT_CONTINUE)
185 request = PT_CONTIN1;
186 #endif
187
188 #if defined(PT_SINGLE1)
189 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
190 signals. If it's available, use it. */
191 if (request == PT_STEP)
192 request = PT_SINGLE1;
193 #endif
194
195 #if 0
196 saved_errno = errno;
197 errno = 0;
198 #endif
199 #if !defined (FIVE_ARG_PTRACE)
200 pt_status = ptrace (request, pid, addr, data);
201 #else
202 /* Deal with HPUX 8.0 braindamage. We never use the
203 calls which require the fifth argument. */
204 pt_status = ptrace (request, pid, addr, data, 0);
205 #endif
206
207 #if 0
208 if (errno)
209 printf (" [errno = %d]", errno);
210
211 errno = saved_errno;
212 printf (" = 0x%x\n", pt_status);
213 #endif
214 return pt_status;
215 }
216
217
218 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
219 /* For the rest of the file, use an extra level of indirection */
220 /* This lets us breakpoint usefully on call_ptrace. */
221 #define ptrace call_ptrace
222 #endif
223
224 /* Wait for a process to finish, possibly running a target-specific
225 hook before returning. */
226
227 int
228 ptrace_wait (pid, status)
229 int pid;
230 int *status;
231 {
232 int wstate;
233
234 wstate = wait (status);
235 target_post_wait (wstate, *status);
236 return wstate;
237 }
238
239 void
240 kill_inferior ()
241 {
242 int status;
243
244 if (inferior_pid == 0)
245 return;
246
247 /* This once used to call "kill" to kill the inferior just in case
248 the inferior was still running. As others have noted in the past
249 (kingdon) there shouldn't be any way to get here if the inferior
250 is still running -- else there's a major problem elsewere in gdb
251 and it needs to be fixed.
252
253 The kill call causes problems under hpux10, so it's been removed;
254 if this causes problems we'll deal with them as they arise. */
255 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
256 ptrace_wait (0, &status);
257 target_mourn_inferior ();
258 }
259
260 #ifndef CHILD_RESUME
261
262 /* Resume execution of the inferior process.
263 If STEP is nonzero, single-step it.
264 If SIGNAL is nonzero, give it that signal. */
265
266 void
267 child_resume (pid, step, signal)
268 int pid;
269 int step;
270 enum target_signal signal;
271 {
272 errno = 0;
273
274 if (pid == -1)
275 /* Resume all threads. */
276 /* I think this only gets used in the non-threaded case, where "resume
277 all threads" and "resume inferior_pid" are the same. */
278 pid = inferior_pid;
279
280 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
281 it was. (If GDB wanted it to start some other way, we have already
282 written a new PC value to the child.)
283
284 If this system does not support PT_STEP, a higher level function will
285 have called single_step() to transmute the step request into a
286 continue request (by setting breakpoints on all possible successor
287 instructions), so we don't have to worry about that here. */
288
289 if (step)
290 {
291 if (SOFTWARE_SINGLE_STEP_P)
292 abort (); /* Make sure this doesn't happen. */
293 else
294 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
295 target_signal_to_host (signal));
296 }
297 else
298 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
299 target_signal_to_host (signal));
300
301 if (errno)
302 {
303 perror_with_name ("ptrace");
304 }
305 }
306 #endif /* CHILD_RESUME */
307 \f
308
309 #ifdef ATTACH_DETACH
310 /* Start debugging the process whose number is PID. */
311 int
312 attach (pid)
313 int pid;
314 {
315 errno = 0;
316 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
317 if (errno)
318 perror_with_name ("ptrace");
319 attach_flag = 1;
320 return pid;
321 }
322
323 /* Stop debugging the process whose number is PID
324 and continue it with signal number SIGNAL.
325 SIGNAL = 0 means just continue it. */
326
327 void
328 detach (signal)
329 int signal;
330 {
331 errno = 0;
332 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
333 if (errno)
334 perror_with_name ("ptrace");
335 attach_flag = 0;
336 }
337 #endif /* ATTACH_DETACH */
338 \f
339 /* Default the type of the ptrace transfer to int. */
340 #ifndef PTRACE_XFER_TYPE
341 #define PTRACE_XFER_TYPE int
342 #endif
343
344 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
345 to get the offset in the core file of the register values. */
346 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
347 /* Get kernel_u_addr using BSD-style nlist(). */
348 CORE_ADDR kernel_u_addr;
349 #endif /* KERNEL_U_ADDR_BSD. */
350
351 void
352 _initialize_kernel_u_addr ()
353 {
354 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
355 struct nlist names[2];
356
357 names[0].n_un.n_name = "_u";
358 names[1].n_un.n_name = NULL;
359 if (nlist ("/vmunix", names) == 0)
360 kernel_u_addr = names[0].n_value;
361 else
362 internal_error ("Unable to get kernel u area address.");
363 #endif /* KERNEL_U_ADDR_BSD. */
364 }
365
366 #if !defined (FETCH_INFERIOR_REGISTERS)
367
368 #if !defined (offsetof)
369 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
370 #endif
371
372 /* U_REGS_OFFSET is the offset of the registers within the u area. */
373 #if !defined (U_REGS_OFFSET)
374 #define U_REGS_OFFSET \
375 ptrace (PT_READ_U, inferior_pid, \
376 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
377 - KERNEL_U_ADDR
378 #endif
379
380 /* Registers we shouldn't try to fetch. */
381 #if !defined (CANNOT_FETCH_REGISTER)
382 #define CANNOT_FETCH_REGISTER(regno) 0
383 #endif
384
385 /* Fetch one register. */
386
387 static void
388 fetch_register (regno)
389 int regno;
390 {
391 /* This isn't really an address. But ptrace thinks of it as one. */
392 CORE_ADDR regaddr;
393 char mess[128]; /* For messages */
394 register int i;
395 unsigned int offset; /* Offset of registers within the u area. */
396 char buf[MAX_REGISTER_RAW_SIZE];
397 int tid;
398
399 if (CANNOT_FETCH_REGISTER (regno))
400 {
401 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
402 supply_register (regno, buf);
403 return;
404 }
405
406 /* Overload thread id onto process id */
407 if ((tid = TIDGET (inferior_pid)) == 0)
408 tid = inferior_pid; /* no thread id, just use process id */
409
410 offset = U_REGS_OFFSET;
411
412 regaddr = register_addr (regno, offset);
413 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
414 {
415 errno = 0;
416 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
417 (PTRACE_ARG3_TYPE) regaddr, 0);
418 regaddr += sizeof (PTRACE_XFER_TYPE);
419 if (errno != 0)
420 {
421 sprintf (mess, "reading register %s (#%d)",
422 REGISTER_NAME (regno), regno);
423 perror_with_name (mess);
424 }
425 }
426 supply_register (regno, buf);
427 }
428
429
430 /* Fetch register values from the inferior.
431 If REGNO is negative, do this for all registers.
432 Otherwise, REGNO specifies which register (so we can save time). */
433
434 void
435 fetch_inferior_registers (regno)
436 int regno;
437 {
438 if (regno >= 0)
439 {
440 fetch_register (regno);
441 }
442 else
443 {
444 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
445 {
446 fetch_register (regno);
447 }
448 }
449 }
450
451 /* Registers we shouldn't try to store. */
452 #if !defined (CANNOT_STORE_REGISTER)
453 #define CANNOT_STORE_REGISTER(regno) 0
454 #endif
455
456 /* Store one register. */
457
458 static void
459 store_register (regno)
460 int regno;
461 {
462 /* This isn't really an address. But ptrace thinks of it as one. */
463 CORE_ADDR regaddr;
464 char mess[128]; /* For messages */
465 register int i;
466 unsigned int offset; /* Offset of registers within the u area. */
467 int tid;
468
469 if (CANNOT_STORE_REGISTER (regno))
470 {
471 return;
472 }
473
474 /* Overload thread id onto process id */
475 if ((tid = TIDGET (inferior_pid)) == 0)
476 tid = inferior_pid; /* no thread id, just use process id */
477
478 offset = U_REGS_OFFSET;
479
480 regaddr = register_addr (regno, offset);
481 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
482 {
483 errno = 0;
484 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
485 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
486 regaddr += sizeof (PTRACE_XFER_TYPE);
487 if (errno != 0)
488 {
489 sprintf (mess, "writing register %s (#%d)",
490 REGISTER_NAME (regno), regno);
491 perror_with_name (mess);
492 }
493 }
494 }
495
496 /* Store our register values back into the inferior.
497 If REGNO is negative, do this for all registers.
498 Otherwise, REGNO specifies which register (so we can save time). */
499
500 void
501 store_inferior_registers (regno)
502 int regno;
503 {
504 if (regno >= 0)
505 {
506 store_register (regno);
507 }
508 else
509 {
510 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
511 {
512 store_register (regno);
513 }
514 }
515 }
516 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
517 \f
518
519 #if !defined (CHILD_XFER_MEMORY)
520 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
521 in the NEW_SUN_PTRACE case.
522 It ought to be straightforward. But it appears that writing did
523 not write the data that I specified. I cannot understand where
524 it got the data that it actually did write. */
525
526 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
527 to debugger memory starting at MYADDR. Copy to inferior if
528 WRITE is nonzero.
529
530 Returns the length copied, which is either the LEN argument or zero.
531 This xfer function does not do partial moves, since child_ops
532 doesn't allow memory operations to cross below us in the target stack
533 anyway. */
534
535 int
536 child_xfer_memory (memaddr, myaddr, len, write, target)
537 CORE_ADDR memaddr;
538 char *myaddr;
539 int len;
540 int write;
541 struct target_ops *target; /* ignored */
542 {
543 register int i;
544 /* Round starting address down to longword boundary. */
545 register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE);
546 /* Round ending address up; get number of longwords that makes. */
547 register int count
548 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
549 / sizeof (PTRACE_XFER_TYPE);
550 /* Allocate buffer of that many longwords. */
551 register PTRACE_XFER_TYPE *buffer
552 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
553
554 if (write)
555 {
556 /* Fill start and end extra bytes of buffer with existing memory data. */
557
558 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE))
559 {
560 /* Need part of initial word -- fetch it. */
561 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_pid),
562 (PTRACE_ARG3_TYPE) addr, 0);
563 }
564
565 if (count > 1) /* FIXME, avoid if even boundary */
566 {
567 buffer[count - 1]
568 = ptrace (PT_READ_I, PIDGET (inferior_pid),
569 ((PTRACE_ARG3_TYPE)
570 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
571 0);
572 }
573
574 /* Copy data to be written over corresponding part of buffer */
575
576 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
577 myaddr,
578 len);
579
580 /* Write the entire buffer. */
581
582 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
583 {
584 errno = 0;
585 ptrace (PT_WRITE_D, PIDGET (inferior_pid),
586 (PTRACE_ARG3_TYPE) addr, buffer[i]);
587 if (errno)
588 {
589 /* Using the appropriate one (I or D) is necessary for
590 Gould NP1, at least. */
591 errno = 0;
592 ptrace (PT_WRITE_I, PIDGET (inferior_pid),
593 (PTRACE_ARG3_TYPE) addr, buffer[i]);
594 }
595 if (errno)
596 return 0;
597 }
598 #ifdef CLEAR_INSN_CACHE
599 CLEAR_INSN_CACHE ();
600 #endif
601 }
602 else
603 {
604 /* Read all the longwords */
605 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
606 {
607 errno = 0;
608 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_pid),
609 (PTRACE_ARG3_TYPE) addr, 0);
610 if (errno)
611 return 0;
612 QUIT;
613 }
614
615 /* Copy appropriate bytes out of the buffer. */
616 memcpy (myaddr,
617 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
618 len);
619 }
620 return len;
621 }
622 \f
623
624 static void
625 udot_info (dummy1, dummy2)
626 char *dummy1;
627 int dummy2;
628 {
629 #if defined (KERNEL_U_SIZE)
630 int udot_off; /* Offset into user struct */
631 int udot_val; /* Value from user struct at udot_off */
632 char mess[128]; /* For messages */
633 #endif
634
635 if (!target_has_execution)
636 {
637 error ("The program is not being run.");
638 }
639
640 #if !defined (KERNEL_U_SIZE)
641
642 /* Adding support for this command is easy. Typically you just add a
643 routine, called "kernel_u_size" that returns the size of the user
644 struct, to the appropriate *-nat.c file and then add to the native
645 config file "#define KERNEL_U_SIZE kernel_u_size()" */
646 error ("Don't know how large ``struct user'' is in this version of gdb.");
647
648 #else
649
650 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
651 {
652 if ((udot_off % 24) == 0)
653 {
654 if (udot_off > 0)
655 {
656 printf_filtered ("\n");
657 }
658 printf_filtered ("%04x:", udot_off);
659 }
660 udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0);
661 if (errno != 0)
662 {
663 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
664 perror_with_name (mess);
665 }
666 /* Avoid using nonportable (?) "*" in print specs */
667 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
668 }
669 printf_filtered ("\n");
670
671 #endif
672 }
673 #endif /* !defined (CHILD_XFER_MEMORY). */
674 \f
675
676 void
677 _initialize_infptrace ()
678 {
679 #if !defined (CHILD_XFER_MEMORY)
680 add_info ("udot", udot_info,
681 "Print contents of kernel ``struct user'' for current child.");
682 #endif
683 }
This page took 0.045175 seconds and 5 git commands to generate.