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