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