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