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