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