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