2012-05-24 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
CommitLineData
2c4a536d 1/* Low-level child interface to ptrace.
5bf970f9 2
0b302171
JB
3 Copyright (C) 1988-1996, 1998-2002, 2004-2012 Free Software
4 Foundation, Inc.
5bf970f9
AC
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
5bf970f9
AC
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bf970f9
AC
20
21#include "defs.h"
5bf970f9 22#include "command.h"
2c4a536d
MK
23#include "inferior.h"
24#include "inflow.h"
191c4426 25#include "terminal.h"
5bf970f9 26#include "gdbcore.h"
8785ced0 27#include "regcache.h"
5bf970f9 28
8785ced0 29#include "gdb_assert.h"
2c4a536d
MK
30#include "gdb_string.h"
31#include "gdb_ptrace.h"
34a17005 32#include "gdb_wait.h"
5bf970f9
AC
33#include <signal.h>
34
2c0b251b 35#include "inf-ptrace.h"
2c4a536d 36#include "inf-child.h"
af990527 37#include "gdbthread.h"
2c4a536d 38
c7c14b96
MK
39\f
40
735f54b4
MK
41#ifdef PT_GET_PROCESS_STATE
42
43static int
ee057212 44inf_ptrace_follow_fork (struct target_ops *ops, int follow_child)
735f54b4
MK
45{
46 pid_t pid, fpid;
47 ptrace_state_t pe;
48
e58b0e63 49 pid = ptid_get_pid (inferior_ptid);
735f54b4
MK
50
51 if (ptrace (PT_GET_PROCESS_STATE, pid,
52 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
53 perror_with_name (("ptrace"));
54
55 gdb_assert (pe.pe_report_event == PTRACE_FORK);
56 fpid = pe.pe_other_pid;
57
58 if (follow_child)
59 {
191c4426 60 struct inferior *parent_inf, *child_inf;
4e1c45ea
PA
61 struct thread_info *tp;
62
191c4426
PA
63 parent_inf = find_inferior_pid (pid);
64
65 /* Add the child. */
66 child_inf = add_inferior (fpid);
67 child_inf->attach_flag = parent_inf->attach_flag;
68 copy_terminal_info (child_inf, parent_inf);
6479260d
JB
69 child_inf->pspace = parent_inf->pspace;
70 child_inf->aspace = parent_inf->aspace;
191c4426 71
4e1c45ea
PA
72 /* Before detaching from the parent, remove all breakpoints from
73 it. */
77435e4c 74 remove_breakpoints ();
735f54b4
MK
75
76 if (ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
77 perror_with_name (("ptrace"));
4e1c45ea 78
7f9f62ba
PA
79 /* Switch inferior_ptid out of the parent's way. */
80 inferior_ptid = pid_to_ptid (fpid);
81
4e1c45ea 82 /* Delete the parent. */
7f9f62ba 83 detach_inferior (pid);
4e1c45ea 84
e58b0e63 85 add_thread_silent (inferior_ptid);
735f54b4
MK
86 }
87 else
88 {
b242c3c2
PA
89 /* Breakpoints have already been detached from the child by
90 infrun.c. */
735f54b4
MK
91
92 if (ptrace (PT_DETACH, fpid, (PTRACE_TYPE_ARG3)1, 0) == -1)
93 perror_with_name (("ptrace"));
94 }
95
96 return 0;
97}
98
99#endif /* PT_GET_PROCESS_STATE */
100\f
101
4b8a1a28 102/* Prepare to be traced. */
5bf970f9
AC
103
104static void
c7c14b96 105inf_ptrace_me (void)
5bf970f9 106{
c7c14b96 107 /* "Trace me, Dr. Memory!" */
4b8a1a28 108 ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
5bf970f9
AC
109}
110
136d6dae
VP
111/* Start a new inferior Unix child process. EXEC_FILE is the file to
112 run, ALLARGS is a string containing the arguments to the program.
113 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
114 chatty about it. */
5bf970f9
AC
115
116static void
136d6dae
VP
117inf_ptrace_create_inferior (struct target_ops *ops,
118 char *exec_file, char *allargs, char **env,
119 int from_tty)
5bf970f9 120{
136d6dae
VP
121 int pid;
122
c0edd9ed
JK
123 /* Do not change either targets above or the same target if already present.
124 The reason is the target stack is shared across multiple inferiors. */
125 int ops_already_pushed = target_is_pushed (ops);
26590820 126 struct cleanup *back_to = NULL;
c0edd9ed
JK
127
128 if (! ops_already_pushed)
129 {
130 /* Clear possible core file with its process_stratum. */
131 push_target (ops);
132 back_to = make_cleanup_unpush_target (ops);
133 }
134
136d6dae 135 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
e69860f1 136 NULL, NULL, NULL);
136d6dae 137
c0edd9ed
JK
138 if (! ops_already_pushed)
139 discard_cleanups (back_to);
5bf970f9 140
c7c14b96
MK
141 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
142 be 1 or 2 depending on whether we're starting without or with a
143 shell. */
144 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
145
146 /* On some targets, there must be some explicit actions taken after
147 the inferior has been started up. */
148 target_post_startup_inferior (pid_to_ptid (pid));
5bf970f9
AC
149}
150
e4ef629d
MK
151#ifdef PT_GET_PROCESS_STATE
152
153static void
154inf_ptrace_post_startup_inferior (ptid_t pid)
155{
156 ptrace_event_t pe;
157
158 /* Set the initial event mask. */
159 memset (&pe, 0, sizeof pe);
160 pe.pe_set_event |= PTRACE_FORK;
161 if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
162 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
163 perror_with_name (("ptrace"));
164}
165
166#endif
167
4b8a1a28
MK
168/* Clean up a rotting corpse of an inferior after it died. */
169
c7c14b96 170static void
136d6dae 171inf_ptrace_mourn_inferior (struct target_ops *ops)
5bf970f9 172{
4b8a1a28
MK
173 int status;
174
175 /* Wait just one more time to collect the inferior's exit status.
f010475d 176 Do not check whether this succeeds though, since we may be
4b8a1a28 177 dealing with a process that we attached to. Such a process will
3d450bdd 178 only report its exit status to its original parent. */
4b8a1a28
MK
179 waitpid (ptid_get_pid (inferior_ptid), &status, 0);
180
c7c14b96 181 generic_mourn_inferior ();
d90e17a7
PA
182
183 if (!have_inferiors ())
184 unpush_target (ops);
5bf970f9
AC
185}
186
4b8a1a28
MK
187/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
188 be chatty about it. */
5bf970f9
AC
189
190static void
136d6dae 191inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty)
5bf970f9
AC
192{
193 char *exec_file;
4b8a1a28 194 pid_t pid;
181e7f93 195 struct inferior *inf;
5bf970f9 196
c0edd9ed
JK
197 /* Do not change either targets above or the same target if already present.
198 The reason is the target stack is shared across multiple inferiors. */
199 int ops_already_pushed = target_is_pushed (ops);
26590820 200 struct cleanup *back_to = NULL;
c0edd9ed 201
74164c56 202 pid = parse_pid_to_attach (args);
5bf970f9 203
f6ffd89b 204 if (pid == getpid ()) /* Trying to masturbate? */
8a3fe4f8 205 error (_("I refuse to debug myself!"));
5bf970f9 206
c0edd9ed
JK
207 if (! ops_already_pushed)
208 {
209 /* target_pid_to_str already uses the target. Also clear possible core
210 file with its process_stratum. */
211 push_target (ops);
212 back_to = make_cleanup_unpush_target (ops);
213 }
214
5bf970f9
AC
215 if (from_tty)
216 {
4b8a1a28 217 exec_file = get_exec_file (0);
5bf970f9
AC
218
219 if (exec_file)
a3f17187 220 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
5bf970f9
AC
221 target_pid_to_str (pid_to_ptid (pid)));
222 else
a3f17187 223 printf_unfiltered (_("Attaching to %s\n"),
5bf970f9
AC
224 target_pid_to_str (pid_to_ptid (pid)));
225
226 gdb_flush (gdb_stdout);
227 }
228
6e1e94ea
MK
229#ifdef PT_ATTACH
230 errno = 0;
4b8a1a28 231 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
6e1e94ea 232 if (errno != 0)
e2e0b3e5 233 perror_with_name (("ptrace"));
6e1e94ea 234#else
8a3fe4f8 235 error (_("This system does not support attaching to a process"));
6e1e94ea 236#endif
5bf970f9 237
6c95b8df
PA
238 inf = current_inferior ();
239 inferior_appeared (inf, pid);
181e7f93 240 inf->attach_flag = 1;
6c95b8df 241 inferior_ptid = pid_to_ptid (pid);
7f9f62ba 242
af990527
PA
243 /* Always add a main thread. If some target extends the ptrace
244 target, it should decorate the ptid later with more info. */
245 add_thread_silent (inferior_ptid);
246
c0edd9ed
JK
247 if (! ops_already_pushed)
248 discard_cleanups (back_to);
5bf970f9
AC
249}
250
e4ef629d
MK
251#ifdef PT_GET_PROCESS_STATE
252
460fac3c 253static void
e4ef629d
MK
254inf_ptrace_post_attach (int pid)
255{
256 ptrace_event_t pe;
257
258 /* Set the initial event mask. */
259 memset (&pe, 0, sizeof pe);
260 pe.pe_set_event |= PTRACE_FORK;
261 if (ptrace (PT_SET_EVENT_MASK, pid,
262 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
263 perror_with_name (("ptrace"));
264}
265
266#endif
267
4b8a1a28 268/* Detach from the inferior, optionally passing it the signal
f010475d 269 specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */
5bf970f9
AC
270
271static void
136d6dae 272inf_ptrace_detach (struct target_ops *ops, char *args, int from_tty)
5bf970f9 273{
4b8a1a28 274 pid_t pid = ptid_get_pid (inferior_ptid);
6e1e94ea 275 int sig = 0;
5bf970f9
AC
276
277 if (from_tty)
278 {
279 char *exec_file = get_exec_file (0);
280 if (exec_file == 0)
281 exec_file = "";
a3f17187 282 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
5bf970f9
AC
283 target_pid_to_str (pid_to_ptid (pid)));
284 gdb_flush (gdb_stdout);
285 }
286 if (args)
6e1e94ea 287 sig = atoi (args);
5bf970f9 288
6e1e94ea 289#ifdef PT_DETACH
4b8a1a28 290 /* We'd better not have left any breakpoints in the program or it'll
f010475d 291 die when it hits one. Also note that this may only work if we
4b8a1a28
MK
292 previously attached to the inferior. It *might* work if we
293 started the process ourselves. */
6e1e94ea 294 errno = 0;
4b8a1a28 295 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
6e1e94ea 296 if (errno != 0)
e2e0b3e5 297 perror_with_name (("ptrace"));
6e1e94ea 298#else
8a3fe4f8 299 error (_("This system does not support detaching from a process"));
6e1e94ea 300#endif
5bf970f9
AC
301
302 inferior_ptid = null_ptid;
7f9f62ba 303 detach_inferior (pid);
7a7d3353
PA
304
305 if (!have_inferiors ())
306 unpush_target (ops);
5bf970f9
AC
307}
308
4b8a1a28
MK
309/* Kill the inferior. */
310
5bf970f9 311static void
7d85a9c0 312inf_ptrace_kill (struct target_ops *ops)
5bf970f9 313{
4b8a1a28 314 pid_t pid = ptid_get_pid (inferior_ptid);
c7c14b96 315 int status;
c7c14b96
MK
316
317 if (pid == 0)
318 return;
319
4b8a1a28
MK
320 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
321 waitpid (pid, &status, 0);
322
c7c14b96 323 target_mourn_inferior ();
5bf970f9
AC
324}
325
4b8a1a28 326/* Stop the inferior. */
c7c14b96 327
5bf970f9 328static void
94cc34af 329inf_ptrace_stop (ptid_t ptid)
5bf970f9 330{
4b8a1a28
MK
331 /* Send a SIGINT to the process group. This acts just like the user
332 typed a ^C on the controlling terminal. Note that using a
333 negative process number in kill() is a System V-ism. The proper
334 BSD interface is killpg(). However, all modern BSDs support the
335 System V interface too. */
7e1789f5 336 kill (-inferior_process_group (), SIGINT);
5bf970f9
AC
337}
338
4b8a1a28
MK
339/* Resume execution of thread PTID, or all threads if PTID is -1. If
340 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
341 that signal. */
5bf970f9
AC
342
343static void
28439f5e
PA
344inf_ptrace_resume (struct target_ops *ops,
345 ptid_t ptid, int step, enum target_signal signal)
5bf970f9 346{
4b8a1a28 347 pid_t pid = ptid_get_pid (ptid);
a96d9b2e 348 int request;
c7c14b96
MK
349
350 if (pid == -1)
4b8a1a28
MK
351 /* Resume all threads. Traditionally ptrace() only supports
352 single-threaded processes, so simply resume the inferior. */
353 pid = ptid_get_pid (inferior_ptid);
c7c14b96 354
a96d9b2e
SDJ
355 if (catch_syscall_enabled () > 0)
356 request = PT_SYSCALL;
357 else
358 request = PT_CONTINUE;
359
c7c14b96
MK
360 if (step)
361 {
362 /* If this system does not support PT_STEP, a higher level
363 function will have called single_step() to transmute the step
364 request into a continue request (by setting breakpoints on
365 all possible successor instructions), so we don't have to
366 worry about that here. */
367 request = PT_STEP;
368 }
369
370 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
371 where it was. If GDB wanted it to start some other way, we have
4b8a1a28 372 already written a new program counter value to the child. */
c7c14b96 373 errno = 0;
4b8a1a28 374 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal));
c7c14b96
MK
375 if (errno != 0)
376 perror_with_name (("ptrace"));
5bf970f9
AC
377}
378
4b8a1a28
MK
379/* Wait for the child specified by PTID to do something. Return the
380 process ID of the child, or MINUS_ONE_PTID in case of error; store
381 the status in *OURSTATUS. */
5bf970f9 382
c7c14b96 383static ptid_t
117de6a9 384inf_ptrace_wait (struct target_ops *ops,
47608cb1 385 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
5bf970f9 386{
4b8a1a28
MK
387 pid_t pid;
388 int status, save_errno;
5bf970f9 389
c7c14b96
MK
390 do
391 {
4b8a1a28 392 set_sigint_trap ();
5bf970f9 393
4b8a1a28
MK
394 do
395 {
396 pid = waitpid (ptid_get_pid (ptid), &status, 0);
397 save_errno = errno;
398 }
399 while (pid == -1 && errno == EINTR);
5bf970f9 400
c7c14b96 401 clear_sigint_trap ();
5bf970f9 402
c7c14b96
MK
403 if (pid == -1)
404 {
c7c14b96 405 fprintf_unfiltered (gdb_stderr,
4b8a1a28 406 _("Child process unexpectedly missing: %s.\n"),
c7c14b96
MK
407 safe_strerror (save_errno));
408
409 /* Claim it exited with unknown signal. */
410 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
411 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
fb66883a 412 return inferior_ptid;
c7c14b96
MK
413 }
414
4b8a1a28
MK
415 /* Ignore terminated detached child processes. */
416 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
417 pid = -1;
c7c14b96 418 }
4b8a1a28 419 while (pid == -1);
c7c14b96 420
735f54b4
MK
421#ifdef PT_GET_PROCESS_STATE
422 if (WIFSTOPPED (status))
423 {
424 ptrace_state_t pe;
425 pid_t fpid;
426
427 if (ptrace (PT_GET_PROCESS_STATE, pid,
428 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
429 perror_with_name (("ptrace"));
430
431 switch (pe.pe_report_event)
432 {
433 case PTRACE_FORK:
434 ourstatus->kind = TARGET_WAITKIND_FORKED;
3a3e9ee3 435 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
735f54b4
MK
436
437 /* Make sure the other end of the fork is stopped too. */
438 fpid = waitpid (pe.pe_other_pid, &status, 0);
439 if (fpid == -1)
440 perror_with_name (("waitpid"));
441
442 if (ptrace (PT_GET_PROCESS_STATE, fpid,
443 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
444 perror_with_name (("ptrace"));
445
446 gdb_assert (pe.pe_report_event == PTRACE_FORK);
447 gdb_assert (pe.pe_other_pid == pid);
448 if (fpid == ptid_get_pid (inferior_ptid))
449 {
3a3e9ee3 450 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
735f54b4
MK
451 return pid_to_ptid (fpid);
452 }
453
454 return pid_to_ptid (pid);
455 }
456 }
457#endif
458
c7c14b96
MK
459 store_waitstatus (ourstatus, status);
460 return pid_to_ptid (pid);
5bf970f9
AC
461}
462
4b8a1a28
MK
463/* Attempt a transfer all LEN bytes starting at OFFSET between the
464 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
465 Return the number of bytes actually transferred. */
5bf970f9
AC
466
467static LONGEST
468inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5
MK
469 const char *annex, gdb_byte *readbuf,
470 const gdb_byte *writebuf,
471 ULONGEST offset, LONGEST len)
5bf970f9 472{
4b8a1a28
MK
473 pid_t pid = ptid_get_pid (inferior_ptid);
474
5bf970f9
AC
475 switch (object)
476 {
477 case TARGET_OBJECT_MEMORY:
f929a579
AC
478#ifdef PT_IO
479 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
480 request that promises to be much more efficient in reading
481 and writing data in the traced process's address space. */
482 {
483 struct ptrace_io_desc piod;
4b8a1a28 484
f929a579 485 /* NOTE: We assume that there are no distinct address spaces
b457b3dd
MK
486 for instruction and data. However, on OpenBSD 3.9 and
487 later, PIOD_WRITE_D doesn't allow changing memory that's
488 mapped read-only. Since most code segments will be
489 read-only, using PIOD_WRITE_D will prevent us from
490 inserting breakpoints, so we use PIOD_WRITE_I instead. */
491 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
f929a579
AC
492 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
493 piod.piod_offs = (void *) (long) offset;
494 piod.piod_len = len;
495
496 errno = 0;
4b8a1a28 497 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
f929a579
AC
498 /* Return the actual number of bytes read or written. */
499 return piod.piod_len;
500 /* If the PT_IO request is somehow not supported, fallback on
501 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
502 to indicate failure. */
503 if (errno != EINVAL)
504 return 0;
505 }
506#endif
507 {
508 union
509 {
510 PTRACE_TYPE_RET word;
4b8a1a28 511 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
f929a579
AC
512 } buffer;
513 ULONGEST rounded_offset;
514 LONGEST partial_len;
4b8a1a28 515
cb85a953
AC
516 /* Round the start offset down to the next long word
517 boundary. */
f929a579 518 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
4b8a1a28 519
cb85a953
AC
520 /* Since ptrace will transfer a single word starting at that
521 rounded_offset the partial_len needs to be adjusted down to
522 that (remember this function only does a single transfer).
523 Should the required length be even less, adjust it down
524 again. */
525 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
526 if (partial_len > len)
f929a579 527 partial_len = len;
4b8a1a28 528
f929a579
AC
529 if (writebuf)
530 {
cb85a953
AC
531 /* If OFFSET:PARTIAL_LEN is smaller than
532 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
533 be needed. Read in the entire word. */
f929a579 534 if (rounded_offset < offset
cb85a953
AC
535 || (offset + partial_len
536 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
f929a579 537 /* Need part of initial word -- fetch it. */
4b8a1a28 538 buffer.word = ptrace (PT_READ_I, pid,
f7dd0ed7
UW
539 (PTRACE_TYPE_ARG3)(uintptr_t)
540 rounded_offset, 0);
4b8a1a28 541
f929a579
AC
542 /* Copy data to be written over corresponding part of
543 buffer. */
f6ffd89b
MK
544 memcpy (buffer.byte + (offset - rounded_offset),
545 writebuf, partial_len);
4b8a1a28 546
f929a579 547 errno = 0;
4b8a1a28 548 ptrace (PT_WRITE_D, pid,
f7dd0ed7
UW
549 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
550 buffer.word);
f929a579
AC
551 if (errno)
552 {
553 /* Using the appropriate one (I or D) is necessary for
554 Gould NP1, at least. */
555 errno = 0;
4b8a1a28 556 ptrace (PT_WRITE_I, pid,
f7dd0ed7
UW
557 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
558 buffer.word);
f929a579
AC
559 if (errno)
560 return 0;
561 }
562 }
4b8a1a28 563
f929a579
AC
564 if (readbuf)
565 {
566 errno = 0;
4b8a1a28 567 buffer.word = ptrace (PT_READ_I, pid,
f7dd0ed7
UW
568 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
569 0);
f929a579
AC
570 if (errno)
571 return 0;
572 /* Copy appropriate bytes out of the buffer. */
573 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
574 partial_len);
575 }
4b8a1a28 576
f929a579
AC
577 return partial_len;
578 }
5bf970f9
AC
579
580 case TARGET_OBJECT_UNWIND_TABLE:
581 return -1;
582
583 case TARGET_OBJECT_AUXV:
e8ace1c0
MK
584#if defined (PT_IO) && defined (PIOD_READ_AUXV)
585 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
586 request that allows us to read the auxilliary vector. Other
587 BSD's may follow if they feel the need to support PIE. */
588 {
589 struct ptrace_io_desc piod;
590
591 if (writebuf)
a4d0b831 592 return -1;
e8ace1c0
MK
593 piod.piod_op = PIOD_READ_AUXV;
594 piod.piod_addr = readbuf;
595 piod.piod_offs = (void *) (long) offset;
596 piod.piod_len = len;
597
598 errno = 0;
599 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
600 /* Return the actual number of bytes read or written. */
601 return piod.piod_len;
602 }
603#endif
5bf970f9
AC
604 return -1;
605
606 case TARGET_OBJECT_WCOOKIE:
607 return -1;
608
609 default:
610 return -1;
611 }
612}
613
4b8a1a28 614/* Return non-zero if the thread specified by PTID is alive. */
c7c14b96
MK
615
616static int
28439f5e 617inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
c7c14b96 618{
4b8a1a28
MK
619 /* ??? Is kill the right way to do this? */
620 return (kill (ptid_get_pid (ptid), 0) != -1);
c7c14b96
MK
621}
622
623/* Print status information about what we're accessing. */
624
625static void
626inf_ptrace_files_info (struct target_ops *ignore)
627{
181e7f93
PA
628 struct inferior *inf = current_inferior ();
629
4b8a1a28 630 printf_filtered (_("\tUsing the running image of %s %s.\n"),
181e7f93 631 inf->attach_flag ? "attached" : "child",
4b8a1a28 632 target_pid_to_str (inferior_ptid));
5bf970f9
AC
633}
634
117de6a9
PA
635static char *
636inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
637{
638 return normal_pid_to_str (ptid);
639}
640
e8ace1c0
MK
641#if defined (PT_IO) && defined (PIOD_READ_AUXV)
642
643/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
644 Return 0 if *READPTR is already at the end of the buffer.
645 Return -1 if there is insufficient buffer for a whole entry.
646 Return 1 if an entry was read into *TYPEP and *VALP. */
647
648static int
649inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
650 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
651{
652 struct type *int_type = builtin_type (target_gdbarch)->builtin_int;
653 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
654 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
655 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
656 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
657 gdb_byte *ptr = *readptr;
658
659 if (endptr == ptr)
660 return 0;
661
662 if (endptr - ptr < 2 * sizeof_auxv_val)
663 return -1;
664
665 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
666 ptr += sizeof_auxv_val; /* Alignment. */
667 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
668 ptr += sizeof_auxv_val;
669
670 *readptr = ptr;
671 return 1;
672}
673
674#endif
675
8785ced0
MK
676/* Create a prototype ptrace target. The client can override it with
677 local methods. */
678
5bf970f9
AC
679struct target_ops *
680inf_ptrace_target (void)
681{
682 struct target_ops *t = inf_child_target ();
8785ced0 683
5bf970f9 684 t->to_attach = inf_ptrace_attach;
5bf970f9
AC
685 t->to_detach = inf_ptrace_detach;
686 t->to_resume = inf_ptrace_resume;
687 t->to_wait = inf_ptrace_wait;
5bf970f9 688 t->to_files_info = inf_ptrace_files_info;
4b8a1a28 689 t->to_kill = inf_ptrace_kill;
5bf970f9 690 t->to_create_inferior = inf_ptrace_create_inferior;
735f54b4
MK
691#ifdef PT_GET_PROCESS_STATE
692 t->to_follow_fork = inf_ptrace_follow_fork;
e4ef629d
MK
693 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
694 t->to_post_attach = inf_ptrace_post_attach;
735f54b4 695#endif
5bf970f9 696 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
5bf970f9 697 t->to_thread_alive = inf_ptrace_thread_alive;
117de6a9 698 t->to_pid_to_str = inf_ptrace_pid_to_str;
5bf970f9 699 t->to_stop = inf_ptrace_stop;
c7c14b96 700 t->to_xfer_partial = inf_ptrace_xfer_partial;
e8ace1c0
MK
701#if defined (PT_IO) && defined (PIOD_READ_AUXV)
702 t->to_auxv_parse = inf_ptrace_auxv_parse;
703#endif
8785ced0
MK
704
705 return t;
706}
707\f
708
4b8a1a28 709/* Pointer to a function that returns the offset within the user area
8785ced0 710 where a particular register is stored. */
7714d83a 711static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
8785ced0
MK
712
713/* Fetch register REGNUM from the inferior. */
714
715static void
56be3814 716inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
8785ced0 717{
3b3b1423 718 struct gdbarch *gdbarch = get_regcache_arch (regcache);
8785ced0
MK
719 CORE_ADDR addr;
720 size_t size;
721 PTRACE_TYPE_RET *buf;
722 int pid, i;
723
7714d83a 724 /* This isn't really an address, but ptrace thinks of it as one. */
3b3b1423 725 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
8d4c1ba3 726 if (addr == (CORE_ADDR)-1
3b3b1423 727 || gdbarch_cannot_fetch_register (gdbarch, regnum))
10d6c8cd 728 {
56be3814 729 regcache_raw_supply (regcache, regnum, NULL);
10d6c8cd
DJ
730 return;
731 }
732
8785ced0 733 /* Cater for systems like GNU/Linux, that implement threads as
10d6c8cd 734 separate processes. */
8785ced0
MK
735 pid = ptid_get_lwp (inferior_ptid);
736 if (pid == 0)
737 pid = ptid_get_pid (inferior_ptid);
738
3b3b1423 739 size = register_size (gdbarch, regnum);
8785ced0
MK
740 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
741 buf = alloca (size);
742
10d6c8cd 743 /* Read the register contents from the inferior a chunk at a time. */
8785ced0
MK
744 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
745 {
746 errno = 0;
f7dd0ed7 747 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
8785ced0 748 if (errno != 0)
4b8a1a28 749 error (_("Couldn't read register %s (#%d): %s."),
3b3b1423 750 gdbarch_register_name (gdbarch, regnum),
c9f4d572 751 regnum, safe_strerror (errno));
8785ced0
MK
752
753 addr += sizeof (PTRACE_TYPE_RET);
754 }
56be3814 755 regcache_raw_supply (regcache, regnum, buf);
8785ced0
MK
756}
757
758/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
759 for all registers. */
760
761static void
28439f5e
PA
762inf_ptrace_fetch_registers (struct target_ops *ops,
763 struct regcache *regcache, int regnum)
8785ced0
MK
764{
765 if (regnum == -1)
3b3b1423
UW
766 for (regnum = 0;
767 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
768 regnum++)
56be3814 769 inf_ptrace_fetch_register (regcache, regnum);
8785ced0 770 else
56be3814 771 inf_ptrace_fetch_register (regcache, regnum);
8785ced0
MK
772}
773
774/* Store register REGNUM into the inferior. */
775
776static void
56be3814 777inf_ptrace_store_register (const struct regcache *regcache, int regnum)
8785ced0 778{
3b3b1423 779 struct gdbarch *gdbarch = get_regcache_arch (regcache);
8785ced0
MK
780 CORE_ADDR addr;
781 size_t size;
782 PTRACE_TYPE_RET *buf;
783 int pid, i;
784
7714d83a 785 /* This isn't really an address, but ptrace thinks of it as one. */
3b3b1423 786 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
8d4c1ba3 787 if (addr == (CORE_ADDR)-1
3b3b1423 788 || gdbarch_cannot_store_register (gdbarch, regnum))
10d6c8cd
DJ
789 return;
790
8785ced0 791 /* Cater for systems like GNU/Linux, that implement threads as
10d6c8cd 792 separate processes. */
8785ced0
MK
793 pid = ptid_get_lwp (inferior_ptid);
794 if (pid == 0)
795 pid = ptid_get_pid (inferior_ptid);
796
3b3b1423 797 size = register_size (gdbarch, regnum);
8785ced0
MK
798 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
799 buf = alloca (size);
800
10d6c8cd 801 /* Write the register contents into the inferior a chunk at a time. */
56be3814 802 regcache_raw_collect (regcache, regnum, buf);
8785ced0
MK
803 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
804 {
805 errno = 0;
f7dd0ed7 806 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
8785ced0 807 if (errno != 0)
4b8a1a28 808 error (_("Couldn't write register %s (#%d): %s."),
3b3b1423 809 gdbarch_register_name (gdbarch, regnum),
c9f4d572 810 regnum, safe_strerror (errno));
8785ced0
MK
811
812 addr += sizeof (PTRACE_TYPE_RET);
813 }
814}
815
816/* Store register REGNUM back into the inferior. If REGNUM is -1, do
817 this for all registers. */
818
2c0b251b 819static void
28439f5e
PA
820inf_ptrace_store_registers (struct target_ops *ops,
821 struct regcache *regcache, int regnum)
8785ced0
MK
822{
823 if (regnum == -1)
3b3b1423
UW
824 for (regnum = 0;
825 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
826 regnum++)
56be3814 827 inf_ptrace_store_register (regcache, regnum);
8785ced0 828 else
56be3814 829 inf_ptrace_store_register (regcache, regnum);
8785ced0
MK
830}
831
832/* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
833 a function returning the offset within the user area where a
834 particular register is stored. */
835
836struct target_ops *
7714d83a
UW
837inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
838 (struct gdbarch *, int, int))
8785ced0
MK
839{
840 struct target_ops *t = inf_ptrace_target();
841
842 gdb_assert (register_u_offset);
843 inf_ptrace_register_u_offset = register_u_offset;
844 t->to_fetch_registers = inf_ptrace_fetch_registers;
845 t->to_store_registers = inf_ptrace_store_registers;
846
5bf970f9
AC
847 return t;
848}
This page took 0.661826 seconds and 4 git commands to generate.