* configure.in (--without-headers): Add missing double quotes.
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
CommitLineData
5bf970f9
AC
1/* Low level Unix child interface to ptrace, for GDB when running under Unix.
2
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1998, 1999, 2000, 2001, 2002, 2004 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 "observer.h"
25#include "gdb_ptrace.h"
26#include "inflow.h"
27#include "inferior.h"
28#include "regcache.h"
29#include "command.h"
30#include "gdbcore.h"
31#include "inf-child.h"
32#include "gdbcmd.h"
7681f339 33#include "gdb_string.h"
5bf970f9
AC
34
35#include <sys/wait.h>
36#include <signal.h>
37
38/* HACK: Save the ptrace ops returned by ptrace_target. */
39static struct target_ops *ptrace_ops_hack;
40
41static void
42inf_ptrace_kill_inferior (void)
43{
44 int status;
45 int pid = PIDGET (inferior_ptid);
46
47 if (pid == 0)
48 return;
49
50 /* This once used to call "kill" to kill the inferior just in case
51 the inferior was still running. As others have noted in the past
52 (kingdon) there shouldn't be any way to get here if the inferior
53 is still running -- else there's a major problem elsewere in gdb
54 and it needs to be fixed.
55
56 The kill call causes problems under hpux10, so it's been removed;
57 if this causes problems we'll deal with them as they arise. */
62ece330
MK
58 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
59 wait (&status);
5bf970f9
AC
60 target_mourn_inferior ();
61}
62
63/* Resume execution of the inferior process. If STEP is nonzero,
64 single-step it. If SIGNAL is nonzero, give it that signal. */
65
66static void
67inf_ptrace_resume (ptid_t ptid, int step, enum target_signal signal)
68{
69 int request = PT_CONTINUE;
70 int pid = PIDGET (ptid);
71
72 if (pid == -1)
73 /* Resume all threads. */
74 /* I think this only gets used in the non-threaded case, where
75 "resume all threads" and "resume inferior_ptid" are the
76 same. */
77 pid = PIDGET (inferior_ptid);
78
79 if (step)
80 {
81 /* If this system does not support PT_STEP, a higher level
82 function will have called single_step() to transmute the step
83 request into a continue request (by setting breakpoints on
84 all possible successor instructions), so we don't have to
85 worry about that here. */
86 request = PT_STEP;
87 }
88
89 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
90 where it was. If GDB wanted it to start some other way, we have
91 already written a new PC value to the child. */
92 errno = 0;
93 ptrace (request, pid, (PTRACE_TYPE_ARG3) 1, target_signal_to_host (signal));
94 if (errno != 0)
95 perror_with_name ("ptrace");
96}
97
98/* Set an upper limit on alloca. */
99#define GDB_MAX_ALLOCA 0x1000
100
101/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
102 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
103 it appears that writing did not write the data that I specified. I
104 cannot understand where it got the data that it actually did
105 write. */
106
107/* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
108 debugger memory starting at MYADDR. Copy to inferior if WRITE is
109 nonzero. TARGET is ignored.
110
111 Returns the length copied, which is either the LEN argument or
112 zero. This xfer function does not do partial moves, since
113 ptrace_ops_hack doesn't allow memory operations to cross below us in the
114 target stack anyway. */
115
116int
117inf_ptrace_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
118 struct mem_attrib *attrib, struct target_ops *target)
119{
120 int i;
121 /* Round starting address down to longword boundary. */
122 CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET);
123 /* Round ending address up; get number of longwords that makes. */
124 int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
125 / sizeof (PTRACE_TYPE_RET));
126 int alloc = count * sizeof (PTRACE_TYPE_RET);
127 PTRACE_TYPE_RET *buffer;
128 struct cleanup *old_chain = NULL;
129
130#ifdef PT_IO
131 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
132 that promises to be much more efficient in reading and writing
133 data in the traced process's address space. */
134
135 {
136 struct ptrace_io_desc piod;
137
138 /* NOTE: We assume that there are no distinct address spaces for
139 instruction and data. */
140 piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
141 piod.piod_offs = (void *) memaddr;
142 piod.piod_addr = myaddr;
143 piod.piod_len = len;
144
27acbe61 145 if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1)
5bf970f9
AC
146 {
147 /* If the PT_IO request is somehow not supported, fallback on
148 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
149 to indicate failure. */
150 if (errno != EINVAL)
151 return 0;
152 }
153 else
154 {
155 /* Return the actual number of bytes read or written. */
156 return piod.piod_len;
157 }
158 }
159#endif
160
161 /* Allocate buffer of that many longwords. */
162 if (len < GDB_MAX_ALLOCA)
163 {
164 buffer = (PTRACE_TYPE_RET *) alloca (alloc);
165 }
166 else
167 {
168 buffer = (PTRACE_TYPE_RET *) xmalloc (alloc);
169 old_chain = make_cleanup (xfree, buffer);
170 }
171
172 if (write)
173 {
174 /* Fill start and end extra bytes of buffer with existing memory
175 data. */
176 if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
177 {
178 /* Need part of initial word -- fetch it. */
179 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
180 (PTRACE_TYPE_ARG3) addr, 0);
181 }
182
183 if (count > 1) /* FIXME, avoid if even boundary. */
184 {
185 buffer[count - 1] =
186 ptrace (PT_READ_I, PIDGET (inferior_ptid),
187 ((PTRACE_TYPE_ARG3)
188 (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0);
189 }
190
191 /* Copy data to be written over corresponding part of buffer. */
192 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
193 myaddr, len);
194
195 /* Write the entire buffer. */
196 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
197 {
198 errno = 0;
199 ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
200 (PTRACE_TYPE_ARG3) addr, buffer[i]);
201 if (errno)
202 {
203 /* Using the appropriate one (I or D) is necessary for
204 Gould NP1, at least. */
205 errno = 0;
206 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
207 (PTRACE_TYPE_ARG3) addr, buffer[i]);
208 }
209 if (errno)
210 return 0;
211 }
212 }
213 else
214 {
215 /* Read all the longwords. */
216 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
217 {
218 errno = 0;
219 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
220 (PTRACE_TYPE_ARG3) addr, 0);
221 if (errno)
222 return 0;
223 QUIT;
224 }
225
226 /* Copy appropriate bytes out of the buffer. */
227 memcpy (myaddr,
228 (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
229 len);
230 }
231
232 if (old_chain != NULL)
233 do_cleanups (old_chain);
234 return len;
235}
236
237/* Wait for child to do something. Return pid of child, or -1 in case
238 of error; store status through argument pointer OURSTATUS. */
239
240static ptid_t
241inf_ptrace_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
242{
243 int save_errno;
244 int status;
245 char *execd_pathname = NULL;
246 int exit_status;
247 int related_pid;
248 int syscall_id;
249 enum target_waitkind kind;
250 int pid;
251
252 do
253 {
254 set_sigint_trap (); /* Causes SIGINT to be passed on to the
255 attached process. */
256 set_sigio_trap ();
257
62ece330
MK
258 pid = wait (&status);
259 target_post_wait (pid_to_ptid (pid), status);
5bf970f9
AC
260
261 save_errno = errno;
262
263 clear_sigio_trap ();
264
265 clear_sigint_trap ();
266
267 if (pid == -1)
268 {
269 if (save_errno == EINTR)
270 continue;
271
272 fprintf_unfiltered (gdb_stderr,
273 "Child process unexpectedly missing: %s.\n",
274 safe_strerror (save_errno));
275
276 /* Claim it exited with unknown signal. */
277 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
278 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
279 return pid_to_ptid (-1);
280 }
281
282 /* Did it exit?
283 */
284 if (target_has_exited (pid, status, &exit_status))
285 {
286 /* ??rehrauer: For now, ignore this. */
287 continue;
288 }
289
290 if (!target_thread_alive (pid_to_ptid (pid)))
291 {
292 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
293 return pid_to_ptid (pid);
294 }
295 }
296 while (pid != PIDGET (inferior_ptid)); /* Some other child died or stopped */
297
298 store_waitstatus (ourstatus, status);
299 return pid_to_ptid (pid);
300}
301
302void
303inf_ptrace_post_wait (ptid_t ptid, int wait_status)
304{
305 /* This version of Unix doesn't require a meaningful "post wait"
306 operation.
307 */
308}
309
310/* Check to see if the given thread is alive.
311
312 FIXME: Is kill() ever the right way to do this? I doubt it, but
313 for now we're going to try and be compatable with the old thread
314 code. */
315
316static int
317inf_ptrace_thread_alive (ptid_t ptid)
318{
319 pid_t pid = PIDGET (ptid);
320
321 return (kill (pid, 0) != -1);
322}
323
324/* Attach to process PID, then initialize for debugging it. */
325
326static void
327inf_ptrace_attach (char *args, int from_tty)
328{
329 char *exec_file;
330 int pid;
331 char *dummy;
332
333 if (!args)
334 error_no_arg ("process-id to attach");
335
336 dummy = args;
337 pid = strtol (args, &dummy, 0);
338 /* Some targets don't set errno on errors, grrr! */
6e1e94ea 339 if (pid == 0 && args == dummy)
5bf970f9
AC
340 error ("Illegal process-id: %s\n", args);
341
342 if (pid == getpid ()) /* Trying to masturbate? */
343 error ("I refuse to debug myself!");
344
345 if (from_tty)
346 {
347 exec_file = (char *) get_exec_file (0);
348
349 if (exec_file)
350 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
351 target_pid_to_str (pid_to_ptid (pid)));
352 else
353 printf_unfiltered ("Attaching to %s\n",
354 target_pid_to_str (pid_to_ptid (pid)));
355
356 gdb_flush (gdb_stdout);
357 }
358
6e1e94ea
MK
359#ifdef PT_ATTACH
360 errno = 0;
361 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
362 if (errno != 0)
363 perror_with_name ("ptrace");
364 attach_flag = 1;
365#else
366 error ("This system does not support attaching to a process");
367#endif
5bf970f9
AC
368
369 inferior_ptid = pid_to_ptid (pid);
370 push_target (ptrace_ops_hack);
371}
372
373static void
374inf_ptrace_post_attach (int pid)
375{
376 /* This version of Unix doesn't require a meaningful "post attach"
377 operation by a debugger. */
378}
379
380/* Take a program previously attached to and detaches it. The program
381 resumes execution and will no longer stop on signals, etc. We'd
382 better not have left any breakpoints in the program or it'll die
383 when it hits one. For this to work, it may be necessary for the
384 process to have been previously attached. It *might* work if the
385 program was started via the normal ptrace (PTRACE_TRACEME). */
386
387static void
388inf_ptrace_detach (char *args, int from_tty)
389{
6e1e94ea 390 int sig = 0;
5bf970f9
AC
391 int pid = PIDGET (inferior_ptid);
392
393 if (from_tty)
394 {
395 char *exec_file = get_exec_file (0);
396 if (exec_file == 0)
397 exec_file = "";
398 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
399 target_pid_to_str (pid_to_ptid (pid)));
400 gdb_flush (gdb_stdout);
401 }
402 if (args)
6e1e94ea 403 sig = atoi (args);
5bf970f9 404
6e1e94ea
MK
405#ifdef PT_DETACH
406 errno = 0;
407 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, sig);
408 if (errno != 0)
409 perror_with_name ("ptrace");
410 attach_flag = 0;
411#else
412 error ("This system does not support detaching from a process");
413#endif
5bf970f9
AC
414
415 inferior_ptid = null_ptid;
416 unpush_target (ptrace_ops_hack);
417}
418
419/* Get ready to modify the registers array. On machines which store
420 individual registers, this doesn't need to do anything. On
421 machines which store all the registers in one fell swoop, this
422 makes sure that registers contains all the registers from the
423 program being debugged. */
424
425static void
426inf_ptrace_prepare_to_store (void)
427{
428}
429
430/* Print status information about what we're accessing. */
431
432static void
433inf_ptrace_files_info (struct target_ops *ignore)
434{
435 printf_unfiltered ("\tUsing the running image of %s %s.\n",
436 attach_flag ? "attached" : "child",
437 target_pid_to_str (inferior_ptid));
438}
439
440static void
441inf_ptrace_open (char *arg, int from_tty)
442{
443 error ("Use the \"run\" command to start a Unix child process.");
444}
445
446/* Stub function which causes the inferior that runs it, to be ptrace-able
447 by its parent process. */
448
449static void
450inf_ptrace_me (void)
451{
452 /* "Trace me, Dr. Memory!" */
62ece330 453 ptrace (0, 0, (PTRACE_TYPE_ARG3) 0, 0);
5bf970f9
AC
454}
455
456/* Stub function which causes the GDB that runs it, to start ptrace-ing
457 the child process. */
458
459static void
460inf_ptrace_him (int pid)
461{
462 push_target (ptrace_ops_hack);
463
464 /* On some targets, there must be some explicit synchronization
465 between the parent and child processes after the debugger
466 forks, and before the child execs the debuggee program. This
467 call basically gives permission for the child to exec.
468 */
469
470 target_acknowledge_created_inferior (pid);
471
472 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
473 * and will be 1 or 2 depending on whether we're starting
474 * without or with a shell.
475 */
476 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
477
478 /* On some targets, there must be some explicit actions taken after
479 the inferior has been started up.
480 */
481 target_post_startup_inferior (pid_to_ptid (pid));
482}
483
484/* Start an inferior Unix child process and sets inferior_ptid to its
485 pid. EXEC_FILE is the file to run. ALLARGS is a string containing
486 the arguments to the program. ENV is the environment vector to
487 pass. Errors reported with error(). */
488
489static void
490inf_ptrace_create_inferior (char *exec_file, char *allargs, char **env,
491 int from_tty)
492{
493 fork_inferior (exec_file, allargs, env, inf_ptrace_me, inf_ptrace_him,
494 NULL, NULL);
495 /* We are at the first instruction we care about. */
496 observer_notify_inferior_created (&current_target, from_tty);
497 /* Pedal to the metal... */
498 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
499}
500
501static void
502inf_ptrace_post_startup_inferior (ptid_t ptid)
503{
504 /* This version of Unix doesn't require a meaningful "post startup inferior"
505 operation by a debugger.
506 */
507}
508
509static void
510inf_ptrace_acknowledge_created_inferior (int pid)
511{
512 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
513 operation by a debugger.
514 */
515}
516
517static int
518inf_ptrace_insert_fork_catchpoint (int pid)
519{
520 /* This version of Unix doesn't support notification of fork events. */
521 return 0;
522}
523
524static int
525inf_ptrace_remove_fork_catchpoint (int pid)
526{
527 /* This version of Unix doesn't support notification of fork events. */
528 return 0;
529}
530
531static int
532inf_ptrace_insert_vfork_catchpoint (int pid)
533{
534 /* This version of Unix doesn't support notification of vfork events. */
535 return 0;
536}
537
538static int
539inf_ptrace_remove_vfork_catchpoint (int pid)
540{
541 /* This version of Unix doesn't support notification of vfork events. */
542 return 0;
543}
544
545static int
546inf_ptrace_follow_fork (int follow_child)
547{
548 /* This version of Unix doesn't support following fork or vfork events. */
549 return 0;
550}
551
552static int
553inf_ptrace_insert_exec_catchpoint (int pid)
554{
555 /* This version of Unix doesn't support notification of exec events. */
556 return 0;
557}
558
559static int
560inf_ptrace_remove_exec_catchpoint (int pid)
561{
562 /* This version of Unix doesn't support notification of exec events. */
563 return 0;
564}
565
566static int
567inf_ptrace_reported_exec_events_per_exec_call (void)
568{
569 /* This version of Unix doesn't support notification of exec events.
570 */
571 return 1;
572}
573
574static int
575inf_ptrace_has_exited (int pid, int wait_status, int *exit_status)
576{
577 if (WIFEXITED (wait_status))
578 {
579 *exit_status = WEXITSTATUS (wait_status);
580 return 1;
581 }
582
583 if (WIFSIGNALED (wait_status))
584 {
585 *exit_status = 0; /* ?? Don't know what else to say here. */
586 return 1;
587 }
588
589 /* ?? Do we really need to consult the event state, too? Assume the
590 wait_state alone suffices.
591 */
592 return 0;
593}
594
595static void
596inf_ptrace_mourn_inferior (void)
597{
598 unpush_target (ptrace_ops_hack);
599 generic_mourn_inferior ();
600}
601
602static int
603inf_ptrace_can_run (void)
604{
605 return 1;
606}
607
608/* Send a SIGINT to the process group. This acts just like the user
609 typed a ^C on the controlling terminal.
610
611 XXX - This may not be correct for all systems. Some may want to
612 use killpg() instead of kill (-pgrp). */
613
614static void
615inf_ptrace_stop (void)
616{
617 kill (-inferior_process_group, SIGINT);
618}
619
620/* Perform a partial transfer to/from the specified object. For
621 memory transfers, fall back to the old memory xfer functions. */
622
623static LONGEST
624inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
625 const char *annex, void *readbuf,
626 const void *writebuf, ULONGEST offset, LONGEST len)
627{
628 switch (object)
629 {
630 case TARGET_OBJECT_MEMORY:
631 if (readbuf)
632 return inf_ptrace_xfer_memory (offset, readbuf, len, 0 /*write */ ,
633 NULL, ops);
634 if (writebuf)
635 return inf_ptrace_xfer_memory (offset, readbuf, len, 1 /*write */ ,
636 NULL, ops);
637 return -1;
638
639 case TARGET_OBJECT_UNWIND_TABLE:
640 return -1;
641
642 case TARGET_OBJECT_AUXV:
643 return -1;
644
645 case TARGET_OBJECT_WCOOKIE:
646 return -1;
647
648 default:
649 return -1;
650 }
651}
652
653static char *
654inf_ptrace_pid_to_str (ptid_t ptid)
655{
656 return normal_pid_to_str (ptid);
657}
658
659struct target_ops *
660inf_ptrace_target (void)
661{
662 struct target_ops *t = inf_child_target ();
663 t->to_open = inf_ptrace_open;
664 t->to_attach = inf_ptrace_attach;
665 t->to_post_attach = inf_ptrace_post_attach;
666 t->to_detach = inf_ptrace_detach;
667 t->to_resume = inf_ptrace_resume;
668 t->to_wait = inf_ptrace_wait;
669 t->to_post_wait = inf_ptrace_post_wait;
670 t->to_prepare_to_store = inf_ptrace_prepare_to_store;
671 t->to_xfer_memory = inf_ptrace_xfer_memory;
672 t->to_xfer_partial = inf_ptrace_xfer_partial;
673 t->to_files_info = inf_ptrace_files_info;
674 t->to_kill = inf_ptrace_kill_inferior;
675 t->to_create_inferior = inf_ptrace_create_inferior;
676 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
677 t->to_acknowledge_created_inferior =
678 inf_ptrace_acknowledge_created_inferior;
679 t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
680 t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
681 t->to_insert_vfork_catchpoint = inf_ptrace_insert_vfork_catchpoint;
682 t->to_remove_vfork_catchpoint = inf_ptrace_remove_vfork_catchpoint;
683 t->to_follow_fork = inf_ptrace_follow_fork;
684 t->to_insert_exec_catchpoint = inf_ptrace_insert_exec_catchpoint;
685 t->to_remove_exec_catchpoint = inf_ptrace_remove_exec_catchpoint;
686 t->to_reported_exec_events_per_exec_call =
687 inf_ptrace_reported_exec_events_per_exec_call;
688 t->to_has_exited = inf_ptrace_has_exited;
689 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
690 t->to_can_run = inf_ptrace_can_run;
691 t->to_thread_alive = inf_ptrace_thread_alive;
692 t->to_pid_to_str = inf_ptrace_pid_to_str;
693 t->to_stop = inf_ptrace_stop;
694 t->to_stratum = process_stratum;
695 t->to_has_all_memory = 1;
696 t->to_has_memory = 1;
697 t->to_has_stack = 1;
698 t->to_has_registers = 1;
699 t->to_has_execution = 1;
700 t->to_magic = OPS_MAGIC;
701 ptrace_ops_hack = t;
702 return t;
703}
This page took 0.062844 seconds and 4 git commands to generate.