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