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