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