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