* gdb_ptrace.h (PT_TRACE_ME): Define to zero if not already
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
1 /* Low-level child interface to ptrace.
2
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1998, 1999, 2000, 2001, 2002, 2004, 2005
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "command.h"
26 #include "inferior.h"
27 #include "inflow.h"
28 #include "gdbcore.h"
29 #include "observer.h"
30 #include "regcache.h"
31
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
34 #include "gdb_ptrace.h"
35 #include "gdb_wait.h"
36 #include <signal.h>
37
38 #include "inf-child.h"
39
40 /* HACK: Save the ptrace ops returned by inf_ptrace_target. */
41 static struct target_ops *ptrace_ops_hack;
42 \f
43
44 /* Prepare to be traced. */
45
46 static void
47 inf_ptrace_me (void)
48 {
49 /* "Trace me, Dr. Memory!" */
50 ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
51 }
52
53 /* Start tracing PID. */
54
55 static void
56 inf_ptrace_him (int pid)
57 {
58 push_target (ptrace_ops_hack);
59
60 /* On some targets, there must be some explicit synchronization
61 between the parent and child processes after the debugger
62 forks, and before the child execs the debuggee program. This
63 call basically gives permission for the child to exec. */
64
65 target_acknowledge_created_inferior (pid);
66
67 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
68 be 1 or 2 depending on whether we're starting without or with a
69 shell. */
70 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
71
72 /* On some targets, there must be some explicit actions taken after
73 the inferior has been started up. */
74 target_post_startup_inferior (pid_to_ptid (pid));
75 }
76
77 /* Start a new inferior Unix child process. EXEC_FILE is the file to
78 run, ALLARGS is a string containing the arguments to the program.
79 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
80 chatty about it. */
81
82 static void
83 inf_ptrace_create_inferior (char *exec_file, char *allargs, char **env,
84 int from_tty)
85 {
86 fork_inferior (exec_file, allargs, env, inf_ptrace_me, inf_ptrace_him,
87 NULL, NULL);
88
89 /* We are at the first instruction we care about. */
90 observer_notify_inferior_created (&current_target, from_tty);
91
92 /* Pedal to the metal... */
93 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
94 }
95
96 /* Clean up a rotting corpse of an inferior after it died. */
97
98 static void
99 inf_ptrace_mourn_inferior (void)
100 {
101 int status;
102
103 /* Wait just one more time to collect the inferior's exit status.
104 Don not check whether this succeeds though, since we may be
105 dealing with a process that we attached to. Such a process will
106 only report its exit status to its origional parent. */
107 waitpid (ptid_get_pid (inferior_ptid), &status, 0);
108
109 unpush_target (ptrace_ops_hack);
110 generic_mourn_inferior ();
111 }
112
113 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
114 be chatty about it. */
115
116 static void
117 inf_ptrace_attach (char *args, int from_tty)
118 {
119 char *exec_file;
120 pid_t pid;
121 char *dummy;
122
123 if (!args)
124 error_no_arg (_("process-id to attach"));
125
126 dummy = args;
127 pid = strtol (args, &dummy, 0);
128 /* Some targets don't set errno on errors, grrr! */
129 if (pid == 0 && args == dummy)
130 error (_("Illegal process-id: %s."), args);
131
132 if (pid == getpid ()) /* Trying to masturbate? */
133 error (_("I refuse to debug myself!"));
134
135 if (from_tty)
136 {
137 exec_file = get_exec_file (0);
138
139 if (exec_file)
140 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
141 target_pid_to_str (pid_to_ptid (pid)));
142 else
143 printf_unfiltered (_("Attaching to %s\n"),
144 target_pid_to_str (pid_to_ptid (pid)));
145
146 gdb_flush (gdb_stdout);
147 }
148
149 #ifdef PT_ATTACH
150 errno = 0;
151 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
152 if (errno != 0)
153 perror_with_name (("ptrace"));
154 attach_flag = 1;
155 #else
156 error (_("This system does not support attaching to a process"));
157 #endif
158
159 inferior_ptid = pid_to_ptid (pid);
160 push_target (ptrace_ops_hack);
161
162 /* Do this first, before anything has had a chance to query the
163 inferior's symbol table or similar. */
164 observer_notify_inferior_created (&current_target, from_tty);
165 }
166
167 /* Detach from the inferior, optionally passing it the signal
168 specified ARGS. If FROM_TTY is non-zero, be chatty about it. */
169
170 static void
171 inf_ptrace_detach (char *args, int from_tty)
172 {
173 pid_t pid = ptid_get_pid (inferior_ptid);
174 int sig = 0;
175
176 if (from_tty)
177 {
178 char *exec_file = get_exec_file (0);
179 if (exec_file == 0)
180 exec_file = "";
181 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
182 target_pid_to_str (pid_to_ptid (pid)));
183 gdb_flush (gdb_stdout);
184 }
185 if (args)
186 sig = atoi (args);
187
188 #ifdef PT_DETACH
189 /* We'd better not have left any breakpoints in the program or it'll
190 die when it hits one. Alsno note that this may only work if we
191 previously attached to the inferior. It *might* work if we
192 started the process ourselves. */
193 errno = 0;
194 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
195 if (errno != 0)
196 perror_with_name (("ptrace"));
197 attach_flag = 0;
198 #else
199 error (_("This system does not support detaching from a process"));
200 #endif
201
202 inferior_ptid = null_ptid;
203 unpush_target (ptrace_ops_hack);
204 }
205
206 /* Kill the inferior. */
207
208 static void
209 inf_ptrace_kill (void)
210 {
211 pid_t pid = ptid_get_pid (inferior_ptid);
212 int status;
213
214 if (pid == 0)
215 return;
216
217 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
218 waitpid (pid, &status, 0);
219
220 target_mourn_inferior ();
221 }
222
223 /* Stop the inferior. */
224
225 static void
226 inf_ptrace_stop (void)
227 {
228 /* Send a SIGINT to the process group. This acts just like the user
229 typed a ^C on the controlling terminal. Note that using a
230 negative process number in kill() is a System V-ism. The proper
231 BSD interface is killpg(). However, all modern BSDs support the
232 System V interface too. */
233 kill (-inferior_process_group, SIGINT);
234 }
235
236 /* Resume execution of thread PTID, or all threads if PTID is -1. If
237 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
238 that signal. */
239
240 static void
241 inf_ptrace_resume (ptid_t ptid, int step, enum target_signal signal)
242 {
243 pid_t pid = ptid_get_pid (ptid);
244 int request = PT_CONTINUE;
245
246 if (pid == -1)
247 /* Resume all threads. Traditionally ptrace() only supports
248 single-threaded processes, so simply resume the inferior. */
249 pid = ptid_get_pid (inferior_ptid);
250
251 if (step)
252 {
253 /* If this system does not support PT_STEP, a higher level
254 function will have called single_step() to transmute the step
255 request into a continue request (by setting breakpoints on
256 all possible successor instructions), so we don't have to
257 worry about that here. */
258 request = PT_STEP;
259 }
260
261 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
262 where it was. If GDB wanted it to start some other way, we have
263 already written a new program counter value to the child. */
264 errno = 0;
265 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal));
266 if (errno != 0)
267 perror_with_name (("ptrace"));
268 }
269
270 /* Wait for the child specified by PTID to do something. Return the
271 process ID of the child, or MINUS_ONE_PTID in case of error; store
272 the status in *OURSTATUS. */
273
274 static ptid_t
275 inf_ptrace_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
276 {
277 pid_t pid;
278 int status, save_errno;
279
280 do
281 {
282 set_sigint_trap ();
283 set_sigio_trap ();
284
285 do
286 {
287 pid = waitpid (ptid_get_pid (ptid), &status, 0);
288 save_errno = errno;
289 }
290 while (pid == -1 && errno == EINTR);
291
292 clear_sigio_trap ();
293 clear_sigint_trap ();
294
295 if (pid == -1)
296 {
297 fprintf_unfiltered (gdb_stderr,
298 _("Child process unexpectedly missing: %s.\n"),
299 safe_strerror (save_errno));
300
301 /* Claim it exited with unknown signal. */
302 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
303 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
304 return minus_one_ptid;
305 }
306
307 /* Ignore terminated detached child processes. */
308 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
309 pid = -1;
310 }
311 while (pid == -1);
312
313 store_waitstatus (ourstatus, status);
314 return pid_to_ptid (pid);
315 }
316
317 /* Attempt a transfer all LEN bytes starting at OFFSET between the
318 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
319 Return the number of bytes actually transferred. */
320
321 static LONGEST
322 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
323 const char *annex, gdb_byte *readbuf,
324 const gdb_byte *writebuf,
325 ULONGEST offset, LONGEST len)
326 {
327 pid_t pid = ptid_get_pid (inferior_ptid);
328
329 switch (object)
330 {
331 case TARGET_OBJECT_MEMORY:
332 #ifdef PT_IO
333 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
334 request that promises to be much more efficient in reading
335 and writing data in the traced process's address space. */
336 {
337 struct ptrace_io_desc piod;
338
339 /* NOTE: We assume that there are no distinct address spaces
340 for instruction and data. */
341 piod.piod_op = writebuf ? PIOD_WRITE_D : PIOD_READ_D;
342 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
343 piod.piod_offs = (void *) (long) offset;
344 piod.piod_len = len;
345
346 errno = 0;
347 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
348 /* Return the actual number of bytes read or written. */
349 return piod.piod_len;
350 /* If the PT_IO request is somehow not supported, fallback on
351 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
352 to indicate failure. */
353 if (errno != EINVAL)
354 return 0;
355 }
356 #endif
357 {
358 union
359 {
360 PTRACE_TYPE_RET word;
361 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
362 } buffer;
363 ULONGEST rounded_offset;
364 LONGEST partial_len;
365
366 /* Round the start offset down to the next long word
367 boundary. */
368 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
369
370 /* Since ptrace will transfer a single word starting at that
371 rounded_offset the partial_len needs to be adjusted down to
372 that (remember this function only does a single transfer).
373 Should the required length be even less, adjust it down
374 again. */
375 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
376 if (partial_len > len)
377 partial_len = len;
378
379 if (writebuf)
380 {
381 /* If OFFSET:PARTIAL_LEN is smaller than
382 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
383 be needed. Read in the entire word. */
384 if (rounded_offset < offset
385 || (offset + partial_len
386 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
387 /* Need part of initial word -- fetch it. */
388 buffer.word = ptrace (PT_READ_I, pid,
389 (PTRACE_TYPE_ARG3)(long)rounded_offset, 0);
390
391 /* Copy data to be written over corresponding part of
392 buffer. */
393 memcpy (buffer.byte + (offset - rounded_offset),
394 writebuf, partial_len);
395
396 errno = 0;
397 ptrace (PT_WRITE_D, pid,
398 (PTRACE_TYPE_ARG3)(long)rounded_offset, buffer.word);
399 if (errno)
400 {
401 /* Using the appropriate one (I or D) is necessary for
402 Gould NP1, at least. */
403 errno = 0;
404 ptrace (PT_WRITE_I, pid,
405 (PTRACE_TYPE_ARG3)(long)rounded_offset, buffer.word);
406 if (errno)
407 return 0;
408 }
409 }
410
411 if (readbuf)
412 {
413 errno = 0;
414 buffer.word = ptrace (PT_READ_I, pid,
415 (PTRACE_TYPE_ARG3)(long)rounded_offset, 0);
416 if (errno)
417 return 0;
418 /* Copy appropriate bytes out of the buffer. */
419 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
420 partial_len);
421 }
422
423 return partial_len;
424 }
425
426 case TARGET_OBJECT_UNWIND_TABLE:
427 return -1;
428
429 case TARGET_OBJECT_AUXV:
430 return -1;
431
432 case TARGET_OBJECT_WCOOKIE:
433 return -1;
434
435 default:
436 return -1;
437 }
438 }
439
440 /* Return non-zero if the thread specified by PTID is alive. */
441
442 static int
443 inf_ptrace_thread_alive (ptid_t ptid)
444 {
445 /* ??? Is kill the right way to do this? */
446 return (kill (ptid_get_pid (ptid), 0) != -1);
447 }
448
449 /* Print status information about what we're accessing. */
450
451 static void
452 inf_ptrace_files_info (struct target_ops *ignore)
453 {
454 printf_filtered (_("\tUsing the running image of %s %s.\n"),
455 attach_flag ? "attached" : "child",
456 target_pid_to_str (inferior_ptid));
457 }
458
459 /* Create a prototype ptrace target. The client can override it with
460 local methods. */
461
462 struct target_ops *
463 inf_ptrace_target (void)
464 {
465 struct target_ops *t = inf_child_target ();
466
467 t->to_attach = inf_ptrace_attach;
468 t->to_detach = inf_ptrace_detach;
469 t->to_resume = inf_ptrace_resume;
470 t->to_wait = inf_ptrace_wait;
471 t->to_files_info = inf_ptrace_files_info;
472 t->to_kill = inf_ptrace_kill;
473 t->to_create_inferior = inf_ptrace_create_inferior;
474 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
475 t->to_thread_alive = inf_ptrace_thread_alive;
476 t->to_pid_to_str = normal_pid_to_str;
477 t->to_stop = inf_ptrace_stop;
478 t->to_xfer_partial = inf_ptrace_xfer_partial;
479
480 ptrace_ops_hack = t;
481 return t;
482 }
483 \f
484
485 /* Pointer to a function that returns the offset within the user area
486 where a particular register is stored. */
487 static CORE_ADDR (*inf_ptrace_register_u_offset)(int);
488
489 /* Fetch register REGNUM from the inferior. */
490
491 static void
492 inf_ptrace_fetch_register (int regnum)
493 {
494 CORE_ADDR addr;
495 size_t size;
496 PTRACE_TYPE_RET *buf;
497 int pid, i;
498
499 /* Cater for systems like GNU/Linux, that implement threads as
500 seperate processes. */
501 pid = ptid_get_lwp (inferior_ptid);
502 if (pid == 0)
503 pid = ptid_get_pid (inferior_ptid);
504
505 /* This isn't really an address, but ptrace thinks of it as one. */
506 addr = inf_ptrace_register_u_offset (regnum);
507 size = register_size (current_gdbarch, regnum);
508
509 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
510 buf = alloca (size);
511
512 /* Read the register contents from the inferior a chuck at the time. */
513 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
514 {
515 errno = 0;
516 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)addr, 0);
517 if (errno != 0)
518 error (_("Couldn't read register %s (#%d): %s."),
519 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
520
521 addr += sizeof (PTRACE_TYPE_RET);
522 }
523 regcache_raw_supply (current_regcache, regnum, buf);
524 }
525
526 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
527 for all registers. */
528
529 static void
530 inf_ptrace_fetch_registers (int regnum)
531 {
532 if (regnum == -1)
533 for (regnum = 0; regnum < NUM_REGS; regnum++)
534 inf_ptrace_fetch_register (regnum);
535 else
536 inf_ptrace_fetch_register (regnum);
537 }
538
539 /* Store register REGNUM into the inferior. */
540
541 static void
542 inf_ptrace_store_register (int regnum)
543 {
544 CORE_ADDR addr;
545 size_t size;
546 PTRACE_TYPE_RET *buf;
547 int pid, i;
548
549 /* Cater for systems like GNU/Linux, that implement threads as
550 seperate processes. */
551 pid = ptid_get_lwp (inferior_ptid);
552 if (pid == 0)
553 pid = ptid_get_pid (inferior_ptid);
554
555 /* This isn't really an address, but ptrace thinks of it as one. */
556 addr = inf_ptrace_register_u_offset (regnum);
557 size = register_size (current_gdbarch, regnum);
558
559 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
560 buf = alloca (size);
561
562 /* Write the register contents into the inferior a chunk at the time. */
563 regcache_raw_collect (current_regcache, regnum, buf);
564 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
565 {
566 errno = 0;
567 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)addr, buf[i]);
568 if (errno != 0)
569 error (_("Couldn't write register %s (#%d): %s."),
570 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
571
572 addr += sizeof (PTRACE_TYPE_RET);
573 }
574 }
575
576 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
577 this for all registers. */
578
579 void
580 inf_ptrace_store_registers (int regnum)
581 {
582 if (regnum == -1)
583 for (regnum = 0; regnum < NUM_REGS; regnum++)
584 inf_ptrace_store_register (regnum);
585 else
586 inf_ptrace_store_register (regnum);
587 }
588
589 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
590 a function returning the offset within the user area where a
591 particular register is stored. */
592
593 struct target_ops *
594 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)(int))
595 {
596 struct target_ops *t = inf_ptrace_target();
597
598 gdb_assert (register_u_offset);
599 inf_ptrace_register_u_offset = register_u_offset;
600 t->to_fetch_registers = inf_ptrace_fetch_registers;
601 t->to_store_registers = inf_ptrace_store_registers;
602
603 return t;
604 }
This page took 0.060121 seconds and 5 git commands to generate.