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