OSDN Git Service

* infptrace.c: Remove #ifdef'ed out inclusion of gdb_stat.h.
[pf3gnuchains/pf3gnuchains3x.git] / gdb / infptrace.c
1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3    1998, 1999, 2000, 2001, 2002, 2004
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "command.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "inferior.h"
28 #include "regcache.h"
29 #include "target.h"
30
31 #include "gdb_assert.h"
32 #include "gdb_wait.h"
33 #include "gdb_string.h"
34
35 #ifdef USG
36 #include <sys/types.h>
37 #endif
38
39 #include <sys/param.h>
40 #include "gdb_dirent.h"
41 #include <signal.h>
42 #include <sys/ioctl.h>
43
44 #include "gdb_ptrace.h"
45
46 #ifdef HAVE_SYS_FILE_H
47 #include <sys/file.h>
48 #endif
49
50 #if !defined (FETCH_INFERIOR_REGISTERS)
51 #include <sys/user.h>           /* Probably need to poke the user structure */
52 #endif /* !FETCH_INFERIOR_REGISTERS */
53
54 #if !defined (CHILD_XFER_MEMORY)
55 static void udot_info (char *, int);
56 #endif
57
58 void _initialize_infptrace (void);
59 \f
60
61 /* This function simply calls ptrace with the given arguments.  
62    It exists so that all calls to ptrace are isolated in this 
63    machine-dependent file. */
64 int
65 call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
66 {
67   int pt_status = 0;
68
69 #if 0
70   int saved_errno;
71
72   printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
73           request, pid, addr, data);
74 #endif
75 #if defined(PT_SETTRC)
76   /* If the parent can be told to attach to us, try to do it.  */
77   if (request == PT_SETTRC)
78     {
79       errno = 0;
80 #ifndef PTRACE_TYPE_ARG5
81       pt_status = ptrace (PT_SETTRC, pid, addr, data);
82 #else
83       /* Deal with HPUX 8.0 braindamage.  We never use the
84          calls which require the fifth argument.  */
85       pt_status = ptrace (PT_SETTRC, pid, addr, data, 0);
86 #endif
87       if (errno)
88         perror_with_name ("ptrace");
89 #if 0
90       printf (" = %d\n", pt_status);
91 #endif
92       if (pt_status < 0)
93         return pt_status;
94       else
95         return parent_attach_all (pid, addr, data);
96     }
97 #endif
98
99 #if defined(PT_CONTIN1)
100   /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
101      signals.  If it's available, use it.  */
102   if (request == PT_CONTINUE)
103     request = PT_CONTIN1;
104 #endif
105
106 #if defined(PT_SINGLE1)
107   /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
108      signals.  If it's available, use it.  */
109   if (request == PT_STEP)
110     request = PT_SINGLE1;
111 #endif
112
113 #if 0
114   saved_errno = errno;
115   errno = 0;
116 #endif
117 #ifndef PTRACE_TYPE_ARG5
118   pt_status = ptrace (request, pid, addr, data);
119 #else
120   /* Deal with HPUX 8.0 braindamage.  We never use the
121      calls which require the fifth argument.  */
122   pt_status = ptrace (request, pid, addr, data, 0);
123 #endif
124
125 #if 0
126   if (errno)
127     printf (" [errno = %d]", errno);
128
129   errno = saved_errno;
130   printf (" = 0x%x\n", pt_status);
131 #endif
132   return pt_status;
133 }
134
135
136 #if defined (DEBUG_PTRACE) || defined (PTRACE_TYPE_ARG5)
137 /* For the rest of the file, use an extra level of indirection */
138 /* This lets us breakpoint usefully on call_ptrace. */
139 #define ptrace call_ptrace
140 #endif
141
142 /* Wait for a process to finish, possibly running a target-specific
143    hook before returning.  */
144
145 int
146 ptrace_wait (ptid_t ptid, int *status)
147 {
148   int wstate;
149
150   wstate = wait (status);
151   target_post_wait (pid_to_ptid (wstate), *status);
152   return wstate;
153 }
154
155 #ifndef KILL_INFERIOR
156 void
157 kill_inferior (void)
158 {
159   int status;
160   int pid =  PIDGET (inferior_ptid);
161
162   if (pid == 0)
163     return;
164
165   /* This once used to call "kill" to kill the inferior just in case
166      the inferior was still running.  As others have noted in the past
167      (kingdon) there shouldn't be any way to get here if the inferior
168      is still running -- else there's a major problem elsewere in gdb
169      and it needs to be fixed.
170
171      The kill call causes problems under hpux10, so it's been removed;
172      if this causes problems we'll deal with them as they arise.  */
173   ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
174   ptrace_wait (null_ptid, &status);
175   target_mourn_inferior ();
176 }
177 #endif /* KILL_INFERIOR */
178
179 #ifndef CHILD_RESUME
180
181 /* Resume execution of the inferior process.
182    If STEP is nonzero, single-step it.
183    If SIGNAL is nonzero, give it that signal.  */
184
185 void
186 child_resume (ptid_t ptid, int step, enum target_signal signal)
187 {
188   int request = PT_CONTINUE;
189   int pid = PIDGET (ptid);
190
191   if (pid == -1)
192     /* Resume all threads.  */
193     /* I think this only gets used in the non-threaded case, where "resume
194        all threads" and "resume inferior_ptid" are the same.  */
195     pid = PIDGET (inferior_ptid);
196
197   if (step)
198     {
199       /* If this system does not support PT_STEP, a higher level
200          function will have called single_step() to transmute the step
201          request into a continue request (by setting breakpoints on
202          all possible successor instructions), so we don't have to
203          worry about that here.  */
204
205       gdb_assert (!SOFTWARE_SINGLE_STEP_P ());
206       request = PT_STEP;
207     }
208
209   /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
210      where it was.  If GDB wanted it to start some other way, we have
211      already written a new PC value to the child.  */
212
213   errno = 0;
214   ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal));
215   if (errno != 0)
216     perror_with_name ("ptrace");
217 }
218 #endif /* CHILD_RESUME */
219 \f
220
221 /* Start debugging the process whose number is PID.  */
222
223 int
224 attach (int pid)
225 {
226 #ifdef PT_ATTACH
227   errno = 0;
228   ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
229   if (errno != 0)
230     perror_with_name ("ptrace");
231   attach_flag = 1;
232   return pid;
233 #else
234   error ("This system does not support attaching to a process");
235 #endif
236 }
237
238 /* Stop debugging the process whose number is PID and continue it with
239    signal number SIGNAL.  SIGNAL = 0 means just continue it.  */
240
241 void
242 detach (int signal)
243 {
244 #ifdef PT_DETACH
245   int pid = PIDGET (inferior_ptid);
246
247   errno = 0;
248   ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, signal);
249   if (errno != 0)
250     perror_with_name ("ptrace");
251   attach_flag = 0;
252 #else
253   error ("This system does not support detaching from a process");
254 #endif
255 }
256 \f
257
258 #ifndef FETCH_INFERIOR_REGISTERS
259
260 /* U_REGS_OFFSET is the offset of the registers within the u area.  */
261 #ifndef U_REGS_OFFSET
262
263 #ifndef offsetof
264 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
265 #endif
266
267 #define U_REGS_OFFSET \
268   ptrace (PT_READ_U, PIDGET (inferior_ptid), \
269           (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \
270     - KERNEL_U_ADDR
271 #endif
272
273 /* Fetch register REGNUM from the inferior.  */
274
275 static void
276 fetch_register (int regnum)
277 {
278   CORE_ADDR addr;
279   size_t size;
280   PTRACE_TYPE_RET *buf;
281   int tid, i;
282
283   if (CANNOT_FETCH_REGISTER (regnum))
284     {
285       regcache_raw_supply (current_regcache, regnum, NULL);
286       return;
287     }
288
289   /* GNU/Linux LWP ID's are process ID's.  */
290   tid = TIDGET (inferior_ptid);
291   if (tid == 0)
292     tid = PIDGET (inferior_ptid); /* Not a threaded program.  */
293
294   /* This isn't really an address.  But ptrace thinks of it as one.  */
295   addr = register_addr (regnum, U_REGS_OFFSET);
296   size = register_size (current_gdbarch, regnum);
297
298   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
299   buf = alloca (size);
300
301   /* Read the register contents from the inferior a chuck at the time.  */
302   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
303     {
304       errno = 0;
305       buf[i] = ptrace (PT_READ_U, tid, (PTRACE_TYPE_ARG3) addr, 0);
306       if (errno != 0)
307         error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regnum),
308                regnum, safe_strerror (errno));
309
310       addr += sizeof (PTRACE_TYPE_RET);
311     }
312   regcache_raw_supply (current_regcache, regnum, buf);
313 }
314
315 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
316    for all registers.  */
317
318 void
319 fetch_inferior_registers (int regnum)
320 {
321   if (regnum == -1)
322     for (regnum = 0; regnum < NUM_REGS; regnum++)
323       fetch_register (regnum);
324   else
325     fetch_register (regnum);
326 }
327
328 /* Store register REGNUM into the inferior.  */
329
330 static void
331 store_register (int regnum)
332 {
333   CORE_ADDR addr;
334   size_t size;
335   PTRACE_TYPE_RET *buf;
336   int tid, i;
337
338   if (CANNOT_STORE_REGISTER (regnum))
339     return;
340
341   /* GNU/Linux LWP ID's are process ID's.  */
342   tid = TIDGET (inferior_ptid);
343   if (tid == 0)
344     tid = PIDGET (inferior_ptid); /* Not a threaded program.  */
345
346   /* This isn't really an address.  But ptrace thinks of it as one.  */
347   addr = register_addr (regnum, U_REGS_OFFSET);
348   size = register_size (current_gdbarch, regnum);
349
350   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
351   buf = alloca (size);
352
353   /* Write the register contents into the inferior a chunk at the time.  */
354   regcache_raw_collect (current_regcache, regnum, buf);
355   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
356     {
357       errno = 0;
358       ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) addr, buf[i]);
359       if (errno != 0)
360         error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regnum),
361                regnum, safe_strerror (errno));
362
363       addr += sizeof (PTRACE_TYPE_RET);
364     }
365 }
366
367 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
368    this for all registers (including the floating point registers).  */
369
370 void
371 store_inferior_registers (int regnum)
372 {
373   if (regnum == -1)
374     for (regnum = 0; regnum < NUM_REGS; regnum++)
375       store_register (regnum);
376   else
377     store_register (regnum);
378 }
379
380 #endif /* not FETCH_INFERIOR_REGISTERS.  */
381 \f
382
383 /* Set an upper limit on alloca.  */
384 #ifndef GDB_MAX_ALLOCA
385 #define GDB_MAX_ALLOCA 0x1000
386 #endif
387
388 #if !defined (CHILD_XFER_MEMORY)
389 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
390    in the NEW_SUN_PTRACE case.  It ought to be straightforward.  But
391    it appears that writing did not write the data that I specified.  I
392    cannot understand where it got the data that it actually did write.  */
393
394 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
395    debugger memory starting at MYADDR.  Copy to inferior if WRITE is
396    nonzero.  TARGET is ignored.
397
398    Returns the length copied, which is either the LEN argument or
399    zero.  This xfer function does not do partial moves, since
400    child_ops doesn't allow memory operations to cross below us in the
401    target stack anyway.  */
402
403 int
404 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
405                    struct mem_attrib *attrib, struct target_ops *target)
406 {
407   int i;
408   /* Round starting address down to longword boundary.  */
409   CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET);
410   /* Round ending address up; get number of longwords that makes.  */
411   int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
412                / sizeof (PTRACE_TYPE_RET));
413   int alloc = count * sizeof (PTRACE_TYPE_RET);
414   PTRACE_TYPE_RET *buffer;
415   struct cleanup *old_chain = NULL;
416
417 #ifdef PT_IO
418   /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
419      that promises to be much more efficient in reading and writing
420      data in the traced process's address space.  */
421
422   {
423     struct ptrace_io_desc piod;
424
425     /* NOTE: We assume that there are no distinct address spaces for
426        instruction and data.  */
427     piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
428     piod.piod_offs = (void *) memaddr;
429     piod.piod_addr = myaddr;
430     piod.piod_len = len;
431
432     if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1)
433       {
434         /* If the PT_IO request is somehow not supported, fallback on
435            using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
436            to indicate failure.  */
437         if (errno != EINVAL)
438           return 0;
439       }
440     else
441       {
442         /* Return the actual number of bytes read or written.  */
443         return piod.piod_len;
444       }
445   }
446 #endif
447
448   /* Allocate buffer of that many longwords.  */
449   if (len < GDB_MAX_ALLOCA)
450     {
451       buffer = (PTRACE_TYPE_RET *) alloca (alloc);
452     }
453   else
454     {
455       buffer = (PTRACE_TYPE_RET *) xmalloc (alloc);
456       old_chain = make_cleanup (xfree, buffer);
457     }
458
459   if (write)
460     {
461       /* Fill start and end extra bytes of buffer with existing memory
462          data.  */
463       if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
464         {
465           /* Need part of initial word -- fetch it.  */
466           buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid), 
467                               (PTRACE_TYPE_ARG3) addr, 0);
468         }
469
470       if (count > 1)            /* FIXME, avoid if even boundary.  */
471         {
472           buffer[count - 1] =
473             ptrace (PT_READ_I, PIDGET (inferior_ptid),
474                     ((PTRACE_TYPE_ARG3)
475                      (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0);
476         }
477
478       /* Copy data to be written over corresponding part of buffer.  */
479       memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
480               myaddr, len);
481
482       /* Write the entire buffer.  */
483       for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
484         {
485           errno = 0;
486           ptrace (PT_WRITE_D, PIDGET (inferior_ptid), 
487                   (PTRACE_TYPE_ARG3) addr, buffer[i]);
488           if (errno)
489             {
490               /* Using the appropriate one (I or D) is necessary for
491                  Gould NP1, at least.  */
492               errno = 0;
493               ptrace (PT_WRITE_I, PIDGET (inferior_ptid), 
494                       (PTRACE_TYPE_ARG3) addr, buffer[i]);
495             }
496           if (errno)
497             return 0;
498         }
499     }
500   else
501     {
502       /* Read all the longwords.  */
503       for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
504         {
505           errno = 0;
506           buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
507                               (PTRACE_TYPE_ARG3) addr, 0);
508           if (errno)
509             return 0;
510           QUIT;
511         }
512
513       /* Copy appropriate bytes out of the buffer.  */
514       memcpy (myaddr,
515               (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
516               len);
517     }
518
519   if (old_chain != NULL)
520     do_cleanups (old_chain);
521   return len;
522 }
523 \f
524
525 static void
526 udot_info (char *dummy1, int dummy2)
527 {
528 #if defined (KERNEL_U_SIZE)
529   long udot_off;                        /* Offset into user struct */
530   int udot_val;                 /* Value from user struct at udot_off */
531   char mess[128];               /* For messages */
532 #endif
533
534   if (!target_has_execution)
535     {
536       error ("The program is not being run.");
537     }
538
539 #if !defined (KERNEL_U_SIZE)
540
541   /* Adding support for this command is easy.  Typically you just add a
542      routine, called "kernel_u_size" that returns the size of the user
543      struct, to the appropriate *-nat.c file and then add to the native
544      config file "#define KERNEL_U_SIZE kernel_u_size()" */
545   error ("Don't know how large ``struct user'' is in this version of gdb.");
546
547 #else
548
549   for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
550     {
551       if ((udot_off % 24) == 0)
552         {
553           if (udot_off > 0)
554             {
555               printf_filtered ("\n");
556             }
557           printf_filtered ("%s:", paddr (udot_off));
558         }
559       udot_val = ptrace (PT_READ_U, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) udot_off, 0);
560       if (errno != 0)
561         {
562           sprintf (mess, "\nreading user struct at offset 0x%s",
563                    paddr_nz (udot_off));
564           perror_with_name (mess);
565         }
566       /* Avoid using nonportable (?) "*" in print specs */
567       printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
568     }
569   printf_filtered ("\n");
570
571 #endif
572 }
573 #endif /* !defined (CHILD_XFER_MEMORY).  */
574 \f
575
576 void
577 _initialize_infptrace (void)
578 {
579 #if !defined (CHILD_XFER_MEMORY)
580   add_info ("udot", udot_info,
581             "Print contents of kernel ``struct user'' for current child.");
582 #endif
583 }