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