* symtab.c (domain_name, search_domain_name): New functions.
[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
07107ca6
LM
43inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
44 int detach_fork)
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);
25f43500 126 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
c0edd9ed
JK
127
128 if (! ops_already_pushed)
129 {
130 /* Clear possible core file with its process_stratum. */
131 push_target (ops);
25f43500 132 make_cleanup_unpush_target (ops);
c0edd9ed
JK
133 }
134
136d6dae 135 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
e69860f1 136 NULL, NULL, NULL);
136d6dae 137
25f43500 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);
25f43500 199 struct cleanup *back_to = make_cleanup (null_cleanup, 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);
25f43500 211 make_cleanup_unpush_target (ops);
c0edd9ed
JK
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
25f43500 246 discard_cleanups (back_to);
5bf970f9
AC
247}
248
e4ef629d
MK
249#ifdef PT_GET_PROCESS_STATE
250
460fac3c 251static void
e4ef629d
MK
252inf_ptrace_post_attach (int pid)
253{
254 ptrace_event_t pe;
255
256 /* Set the initial event mask. */
257 memset (&pe, 0, sizeof pe);
258 pe.pe_set_event |= PTRACE_FORK;
259 if (ptrace (PT_SET_EVENT_MASK, pid,
260 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
261 perror_with_name (("ptrace"));
262}
263
264#endif
265
4b8a1a28 266/* Detach from the inferior, optionally passing it the signal
f010475d 267 specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */
5bf970f9
AC
268
269static void
136d6dae 270inf_ptrace_detach (struct target_ops *ops, char *args, int from_tty)
5bf970f9 271{
4b8a1a28 272 pid_t pid = ptid_get_pid (inferior_ptid);
6e1e94ea 273 int sig = 0;
5bf970f9
AC
274
275 if (from_tty)
276 {
277 char *exec_file = get_exec_file (0);
278 if (exec_file == 0)
279 exec_file = "";
a3f17187 280 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
5bf970f9
AC
281 target_pid_to_str (pid_to_ptid (pid)));
282 gdb_flush (gdb_stdout);
283 }
284 if (args)
6e1e94ea 285 sig = atoi (args);
5bf970f9 286
6e1e94ea 287#ifdef PT_DETACH
4b8a1a28 288 /* We'd better not have left any breakpoints in the program or it'll
f010475d 289 die when it hits one. Also note that this may only work if we
4b8a1a28
MK
290 previously attached to the inferior. It *might* work if we
291 started the process ourselves. */
6e1e94ea 292 errno = 0;
4b8a1a28 293 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
6e1e94ea 294 if (errno != 0)
e2e0b3e5 295 perror_with_name (("ptrace"));
6e1e94ea 296#else
8a3fe4f8 297 error (_("This system does not support detaching from a process"));
6e1e94ea 298#endif
5bf970f9
AC
299
300 inferior_ptid = null_ptid;
7f9f62ba 301 detach_inferior (pid);
7a7d3353
PA
302
303 if (!have_inferiors ())
304 unpush_target (ops);
5bf970f9
AC
305}
306
4b8a1a28
MK
307/* Kill the inferior. */
308
5bf970f9 309static void
7d85a9c0 310inf_ptrace_kill (struct target_ops *ops)
5bf970f9 311{
4b8a1a28 312 pid_t pid = ptid_get_pid (inferior_ptid);
c7c14b96 313 int status;
c7c14b96
MK
314
315 if (pid == 0)
316 return;
317
4b8a1a28
MK
318 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
319 waitpid (pid, &status, 0);
320
c7c14b96 321 target_mourn_inferior ();
5bf970f9
AC
322}
323
4b8a1a28 324/* Stop the inferior. */
c7c14b96 325
5bf970f9 326static void
94cc34af 327inf_ptrace_stop (ptid_t ptid)
5bf970f9 328{
4b8a1a28
MK
329 /* Send a SIGINT to the process group. This acts just like the user
330 typed a ^C on the controlling terminal. Note that using a
331 negative process number in kill() is a System V-ism. The proper
332 BSD interface is killpg(). However, all modern BSDs support the
333 System V interface too. */
7e1789f5 334 kill (-inferior_process_group (), SIGINT);
5bf970f9
AC
335}
336
4b8a1a28
MK
337/* Resume execution of thread PTID, or all threads if PTID is -1. If
338 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
339 that signal. */
5bf970f9
AC
340
341static void
28439f5e 342inf_ptrace_resume (struct target_ops *ops,
2ea28649 343 ptid_t ptid, int step, enum gdb_signal signal)
5bf970f9 344{
4b8a1a28 345 pid_t pid = ptid_get_pid (ptid);
a96d9b2e 346 int request;
c7c14b96
MK
347
348 if (pid == -1)
4b8a1a28
MK
349 /* Resume all threads. Traditionally ptrace() only supports
350 single-threaded processes, so simply resume the inferior. */
351 pid = ptid_get_pid (inferior_ptid);
c7c14b96 352
a96d9b2e
SDJ
353 if (catch_syscall_enabled () > 0)
354 request = PT_SYSCALL;
355 else
356 request = PT_CONTINUE;
357
c7c14b96
MK
358 if (step)
359 {
360 /* If this system does not support PT_STEP, a higher level
361 function will have called single_step() to transmute the step
362 request into a continue request (by setting breakpoints on
363 all possible successor instructions), so we don't have to
364 worry about that here. */
365 request = PT_STEP;
366 }
367
368 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
369 where it was. If GDB wanted it to start some other way, we have
4b8a1a28 370 already written a new program counter value to the child. */
c7c14b96 371 errno = 0;
2ea28649 372 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
c7c14b96
MK
373 if (errno != 0)
374 perror_with_name (("ptrace"));
5bf970f9
AC
375}
376
4b8a1a28
MK
377/* Wait for the child specified by PTID to do something. Return the
378 process ID of the child, or MINUS_ONE_PTID in case of error; store
379 the status in *OURSTATUS. */
5bf970f9 380
c7c14b96 381static ptid_t
117de6a9 382inf_ptrace_wait (struct target_ops *ops,
47608cb1 383 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
5bf970f9 384{
4b8a1a28
MK
385 pid_t pid;
386 int status, save_errno;
5bf970f9 387
c7c14b96
MK
388 do
389 {
4b8a1a28 390 set_sigint_trap ();
5bf970f9 391
4b8a1a28
MK
392 do
393 {
394 pid = waitpid (ptid_get_pid (ptid), &status, 0);
395 save_errno = errno;
396 }
397 while (pid == -1 && errno == EINTR);
5bf970f9 398
c7c14b96 399 clear_sigint_trap ();
5bf970f9 400
c7c14b96
MK
401 if (pid == -1)
402 {
c7c14b96 403 fprintf_unfiltered (gdb_stderr,
4b8a1a28 404 _("Child process unexpectedly missing: %s.\n"),
c7c14b96
MK
405 safe_strerror (save_errno));
406
407 /* Claim it exited with unknown signal. */
408 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 409 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
fb66883a 410 return inferior_ptid;
c7c14b96
MK
411 }
412
4b8a1a28
MK
413 /* Ignore terminated detached child processes. */
414 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
415 pid = -1;
c7c14b96 416 }
4b8a1a28 417 while (pid == -1);
c7c14b96 418
735f54b4
MK
419#ifdef PT_GET_PROCESS_STATE
420 if (WIFSTOPPED (status))
421 {
422 ptrace_state_t pe;
423 pid_t fpid;
424
425 if (ptrace (PT_GET_PROCESS_STATE, pid,
426 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
427 perror_with_name (("ptrace"));
428
429 switch (pe.pe_report_event)
430 {
431 case PTRACE_FORK:
432 ourstatus->kind = TARGET_WAITKIND_FORKED;
3a3e9ee3 433 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
735f54b4
MK
434
435 /* Make sure the other end of the fork is stopped too. */
436 fpid = waitpid (pe.pe_other_pid, &status, 0);
437 if (fpid == -1)
438 perror_with_name (("waitpid"));
439
440 if (ptrace (PT_GET_PROCESS_STATE, fpid,
441 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
442 perror_with_name (("ptrace"));
443
444 gdb_assert (pe.pe_report_event == PTRACE_FORK);
445 gdb_assert (pe.pe_other_pid == pid);
446 if (fpid == ptid_get_pid (inferior_ptid))
447 {
3a3e9ee3 448 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
735f54b4
MK
449 return pid_to_ptid (fpid);
450 }
451
452 return pid_to_ptid (pid);
453 }
454 }
455#endif
456
c7c14b96
MK
457 store_waitstatus (ourstatus, status);
458 return pid_to_ptid (pid);
5bf970f9
AC
459}
460
4b8a1a28
MK
461/* Attempt a transfer all LEN bytes starting at OFFSET between the
462 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
463 Return the number of bytes actually transferred. */
5bf970f9
AC
464
465static LONGEST
466inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5
MK
467 const char *annex, gdb_byte *readbuf,
468 const gdb_byte *writebuf,
469 ULONGEST offset, LONGEST len)
5bf970f9 470{
4b8a1a28
MK
471 pid_t pid = ptid_get_pid (inferior_ptid);
472
5bf970f9
AC
473 switch (object)
474 {
475 case TARGET_OBJECT_MEMORY:
f929a579
AC
476#ifdef PT_IO
477 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
478 request that promises to be much more efficient in reading
479 and writing data in the traced process's address space. */
480 {
481 struct ptrace_io_desc piod;
4b8a1a28 482
f929a579 483 /* NOTE: We assume that there are no distinct address spaces
b457b3dd
MK
484 for instruction and data. However, on OpenBSD 3.9 and
485 later, PIOD_WRITE_D doesn't allow changing memory that's
486 mapped read-only. Since most code segments will be
487 read-only, using PIOD_WRITE_D will prevent us from
488 inserting breakpoints, so we use PIOD_WRITE_I instead. */
489 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
f929a579
AC
490 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
491 piod.piod_offs = (void *) (long) offset;
492 piod.piod_len = len;
493
494 errno = 0;
4b8a1a28 495 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
f929a579
AC
496 /* Return the actual number of bytes read or written. */
497 return piod.piod_len;
498 /* If the PT_IO request is somehow not supported, fallback on
499 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
500 to indicate failure. */
501 if (errno != EINVAL)
502 return 0;
503 }
504#endif
505 {
506 union
507 {
508 PTRACE_TYPE_RET word;
4b8a1a28 509 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
f929a579
AC
510 } buffer;
511 ULONGEST rounded_offset;
512 LONGEST partial_len;
4b8a1a28 513
cb85a953
AC
514 /* Round the start offset down to the next long word
515 boundary. */
f929a579 516 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
4b8a1a28 517
cb85a953
AC
518 /* Since ptrace will transfer a single word starting at that
519 rounded_offset the partial_len needs to be adjusted down to
520 that (remember this function only does a single transfer).
521 Should the required length be even less, adjust it down
522 again. */
523 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
524 if (partial_len > len)
f929a579 525 partial_len = len;
4b8a1a28 526
f929a579
AC
527 if (writebuf)
528 {
cb85a953
AC
529 /* If OFFSET:PARTIAL_LEN is smaller than
530 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
531 be needed. Read in the entire word. */
f929a579 532 if (rounded_offset < offset
cb85a953
AC
533 || (offset + partial_len
534 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
f929a579 535 /* Need part of initial word -- fetch it. */
4b8a1a28 536 buffer.word = ptrace (PT_READ_I, pid,
f7dd0ed7
UW
537 (PTRACE_TYPE_ARG3)(uintptr_t)
538 rounded_offset, 0);
4b8a1a28 539
f929a579
AC
540 /* Copy data to be written over corresponding part of
541 buffer. */
f6ffd89b
MK
542 memcpy (buffer.byte + (offset - rounded_offset),
543 writebuf, partial_len);
4b8a1a28 544
f929a579 545 errno = 0;
4b8a1a28 546 ptrace (PT_WRITE_D, pid,
f7dd0ed7
UW
547 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
548 buffer.word);
f929a579
AC
549 if (errno)
550 {
551 /* Using the appropriate one (I or D) is necessary for
552 Gould NP1, at least. */
553 errno = 0;
4b8a1a28 554 ptrace (PT_WRITE_I, pid,
f7dd0ed7
UW
555 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
556 buffer.word);
f929a579
AC
557 if (errno)
558 return 0;
559 }
560 }
4b8a1a28 561
f929a579
AC
562 if (readbuf)
563 {
564 errno = 0;
4b8a1a28 565 buffer.word = ptrace (PT_READ_I, pid,
f7dd0ed7
UW
566 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
567 0);
f929a579
AC
568 if (errno)
569 return 0;
570 /* Copy appropriate bytes out of the buffer. */
571 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
572 partial_len);
573 }
4b8a1a28 574
f929a579
AC
575 return partial_len;
576 }
5bf970f9
AC
577
578 case TARGET_OBJECT_UNWIND_TABLE:
579 return -1;
580
581 case TARGET_OBJECT_AUXV:
e8ace1c0
MK
582#if defined (PT_IO) && defined (PIOD_READ_AUXV)
583 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
584 request that allows us to read the auxilliary vector. Other
585 BSD's may follow if they feel the need to support PIE. */
586 {
587 struct ptrace_io_desc piod;
588
589 if (writebuf)
a4d0b831 590 return -1;
e8ace1c0
MK
591 piod.piod_op = PIOD_READ_AUXV;
592 piod.piod_addr = readbuf;
593 piod.piod_offs = (void *) (long) offset;
594 piod.piod_len = len;
595
596 errno = 0;
597 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
598 /* Return the actual number of bytes read or written. */
599 return piod.piod_len;
600 }
601#endif
5bf970f9
AC
602 return -1;
603
604 case TARGET_OBJECT_WCOOKIE:
605 return -1;
606
607 default:
608 return -1;
609 }
610}
611
4b8a1a28 612/* Return non-zero if the thread specified by PTID is alive. */
c7c14b96
MK
613
614static int
28439f5e 615inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
c7c14b96 616{
4b8a1a28
MK
617 /* ??? Is kill the right way to do this? */
618 return (kill (ptid_get_pid (ptid), 0) != -1);
c7c14b96
MK
619}
620
621/* Print status information about what we're accessing. */
622
623static void
624inf_ptrace_files_info (struct target_ops *ignore)
625{
181e7f93
PA
626 struct inferior *inf = current_inferior ();
627
4b8a1a28 628 printf_filtered (_("\tUsing the running image of %s %s.\n"),
181e7f93 629 inf->attach_flag ? "attached" : "child",
4b8a1a28 630 target_pid_to_str (inferior_ptid));
5bf970f9
AC
631}
632
117de6a9
PA
633static char *
634inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
635{
636 return normal_pid_to_str (ptid);
637}
638
e8ace1c0
MK
639#if defined (PT_IO) && defined (PIOD_READ_AUXV)
640
641/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
642 Return 0 if *READPTR is already at the end of the buffer.
643 Return -1 if there is insufficient buffer for a whole entry.
644 Return 1 if an entry was read into *TYPEP and *VALP. */
645
646static int
647inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
648 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
649{
f5656ead
TT
650 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
651 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
e8ace1c0
MK
652 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
653 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
f5656ead 654 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
e8ace1c0
MK
655 gdb_byte *ptr = *readptr;
656
657 if (endptr == ptr)
658 return 0;
659
660 if (endptr - ptr < 2 * sizeof_auxv_val)
661 return -1;
662
663 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
664 ptr += sizeof_auxv_val; /* Alignment. */
665 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
666 ptr += sizeof_auxv_val;
667
668 *readptr = ptr;
669 return 1;
670}
671
672#endif
673
8785ced0
MK
674/* Create a prototype ptrace target. The client can override it with
675 local methods. */
676
5bf970f9
AC
677struct target_ops *
678inf_ptrace_target (void)
679{
680 struct target_ops *t = inf_child_target ();
8785ced0 681
5bf970f9 682 t->to_attach = inf_ptrace_attach;
5bf970f9
AC
683 t->to_detach = inf_ptrace_detach;
684 t->to_resume = inf_ptrace_resume;
685 t->to_wait = inf_ptrace_wait;
5bf970f9 686 t->to_files_info = inf_ptrace_files_info;
4b8a1a28 687 t->to_kill = inf_ptrace_kill;
5bf970f9 688 t->to_create_inferior = inf_ptrace_create_inferior;
735f54b4
MK
689#ifdef PT_GET_PROCESS_STATE
690 t->to_follow_fork = inf_ptrace_follow_fork;
e4ef629d
MK
691 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
692 t->to_post_attach = inf_ptrace_post_attach;
735f54b4 693#endif
5bf970f9 694 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
5bf970f9 695 t->to_thread_alive = inf_ptrace_thread_alive;
117de6a9 696 t->to_pid_to_str = inf_ptrace_pid_to_str;
5bf970f9 697 t->to_stop = inf_ptrace_stop;
c7c14b96 698 t->to_xfer_partial = inf_ptrace_xfer_partial;
e8ace1c0
MK
699#if defined (PT_IO) && defined (PIOD_READ_AUXV)
700 t->to_auxv_parse = inf_ptrace_auxv_parse;
701#endif
8785ced0
MK
702
703 return t;
704}
705\f
706
4b8a1a28 707/* Pointer to a function that returns the offset within the user area
8785ced0 708 where a particular register is stored. */
7714d83a 709static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
8785ced0
MK
710
711/* Fetch register REGNUM from the inferior. */
712
713static void
56be3814 714inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
8785ced0 715{
3b3b1423 716 struct gdbarch *gdbarch = get_regcache_arch (regcache);
8785ced0
MK
717 CORE_ADDR addr;
718 size_t size;
719 PTRACE_TYPE_RET *buf;
720 int pid, i;
721
7714d83a 722 /* This isn't really an address, but ptrace thinks of it as one. */
3b3b1423 723 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
8d4c1ba3 724 if (addr == (CORE_ADDR)-1
3b3b1423 725 || gdbarch_cannot_fetch_register (gdbarch, regnum))
10d6c8cd 726 {
56be3814 727 regcache_raw_supply (regcache, regnum, NULL);
10d6c8cd
DJ
728 return;
729 }
730
8785ced0 731 /* Cater for systems like GNU/Linux, that implement threads as
10d6c8cd 732 separate processes. */
8785ced0
MK
733 pid = ptid_get_lwp (inferior_ptid);
734 if (pid == 0)
735 pid = ptid_get_pid (inferior_ptid);
736
3b3b1423 737 size = register_size (gdbarch, regnum);
8785ced0
MK
738 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
739 buf = alloca (size);
740
10d6c8cd 741 /* Read the register contents from the inferior a chunk at a time. */
8785ced0
MK
742 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
743 {
744 errno = 0;
f7dd0ed7 745 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
8785ced0 746 if (errno != 0)
4b8a1a28 747 error (_("Couldn't read register %s (#%d): %s."),
3b3b1423 748 gdbarch_register_name (gdbarch, regnum),
c9f4d572 749 regnum, safe_strerror (errno));
8785ced0
MK
750
751 addr += sizeof (PTRACE_TYPE_RET);
752 }
56be3814 753 regcache_raw_supply (regcache, regnum, buf);
8785ced0
MK
754}
755
756/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
757 for all registers. */
758
759static void
28439f5e
PA
760inf_ptrace_fetch_registers (struct target_ops *ops,
761 struct regcache *regcache, int regnum)
8785ced0
MK
762{
763 if (regnum == -1)
3b3b1423
UW
764 for (regnum = 0;
765 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
766 regnum++)
56be3814 767 inf_ptrace_fetch_register (regcache, regnum);
8785ced0 768 else
56be3814 769 inf_ptrace_fetch_register (regcache, regnum);
8785ced0
MK
770}
771
772/* Store register REGNUM into the inferior. */
773
774static void
56be3814 775inf_ptrace_store_register (const struct regcache *regcache, int regnum)
8785ced0 776{
3b3b1423 777 struct gdbarch *gdbarch = get_regcache_arch (regcache);
8785ced0
MK
778 CORE_ADDR addr;
779 size_t size;
780 PTRACE_TYPE_RET *buf;
781 int pid, i;
782
7714d83a 783 /* This isn't really an address, but ptrace thinks of it as one. */
3b3b1423 784 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
8d4c1ba3 785 if (addr == (CORE_ADDR)-1
3b3b1423 786 || gdbarch_cannot_store_register (gdbarch, regnum))
10d6c8cd
DJ
787 return;
788
8785ced0 789 /* Cater for systems like GNU/Linux, that implement threads as
10d6c8cd 790 separate processes. */
8785ced0
MK
791 pid = ptid_get_lwp (inferior_ptid);
792 if (pid == 0)
793 pid = ptid_get_pid (inferior_ptid);
794
3b3b1423 795 size = register_size (gdbarch, regnum);
8785ced0
MK
796 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
797 buf = alloca (size);
798
10d6c8cd 799 /* Write the register contents into the inferior a chunk at a time. */
56be3814 800 regcache_raw_collect (regcache, regnum, buf);
8785ced0
MK
801 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
802 {
803 errno = 0;
f7dd0ed7 804 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
8785ced0 805 if (errno != 0)
4b8a1a28 806 error (_("Couldn't write register %s (#%d): %s."),
3b3b1423 807 gdbarch_register_name (gdbarch, regnum),
c9f4d572 808 regnum, safe_strerror (errno));
8785ced0
MK
809
810 addr += sizeof (PTRACE_TYPE_RET);
811 }
812}
813
814/* Store register REGNUM back into the inferior. If REGNUM is -1, do
815 this for all registers. */
816
2c0b251b 817static void
28439f5e
PA
818inf_ptrace_store_registers (struct target_ops *ops,
819 struct regcache *regcache, int regnum)
8785ced0
MK
820{
821 if (regnum == -1)
3b3b1423
UW
822 for (regnum = 0;
823 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
824 regnum++)
56be3814 825 inf_ptrace_store_register (regcache, regnum);
8785ced0 826 else
56be3814 827 inf_ptrace_store_register (regcache, regnum);
8785ced0
MK
828}
829
830/* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
831 a function returning the offset within the user area where a
832 particular register is stored. */
833
834struct target_ops *
7714d83a
UW
835inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
836 (struct gdbarch *, int, int))
8785ced0
MK
837{
838 struct target_ops *t = inf_ptrace_target();
839
840 gdb_assert (register_u_offset);
841 inf_ptrace_register_u_offset = register_u_offset;
842 t->to_fetch_registers = inf_ptrace_fetch_registers;
843 t->to_store_registers = inf_ptrace_store_registers;
844
5bf970f9
AC
845 return t;
846}
This page took 0.787486 seconds and 4 git commands to generate.