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