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