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