2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / ptrace.c
blobe149d80477f978c8ab5d06cc13f77a716ded99b4
1 /* Copyright (C) 1995-1998,2000,2003,2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/ptrace.h>
22 #include <sys/user.h>
23 #include <stdarg.h>
24 #include <signal.h>
26 #include <sysdep.h>
27 #include <sys/syscall.h>
28 #include <bp-checks.h>
30 long int
31 ptrace (enum __ptrace_request request, ...)
33 long int res, ret;
34 va_list ap;
35 pid_t pid;
36 void *addr, *data;
38 va_start (ap, request);
39 pid = va_arg (ap, pid_t);
40 addr = va_arg (ap, void *);
41 data = va_arg (ap, void *);
42 va_end (ap);
44 if (request > 0 && request < 4)
45 data = &ret;
47 #if __BOUNDED_POINTERS__
48 switch (request)
50 case PTRACE_PEEKTEXT:
51 case PTRACE_PEEKDATA:
52 case PTRACE_PEEKUSER:
53 case PTRACE_POKETEXT:
54 case PTRACE_POKEDATA:
55 case PTRACE_POKEUSER:
56 (void) CHECK_1 ((int *) addr);
57 (void) CHECK_1 ((int *) data);
58 break;
60 case PTRACE_GETREGS:
61 case PTRACE_SETREGS:
62 #ifdef __i386__
63 (void) CHECK_1 ((struct user_regs_struct *) data);
64 #else
65 /* We don't know the size of data, so the best we can do is ensure
66 that `data' is valid for at least one word. */
67 (void) CHECK_1 ((int *) data);
68 #endif
69 break;
71 case PTRACE_GETFPREGS:
72 case PTRACE_SETFPREGS:
73 #ifdef __i386__
74 (void) CHECK_1 ((struct user_fpregs_struct *) data);
75 #else
76 /* We don't know the size of data, so the best we can do is ensure
77 that `data' is valid for at least one word. */
78 (void) CHECK_1 ((int *) data);
79 #endif
80 break;
82 case PTRACE_GETFPXREGS:
83 case PTRACE_SETFPXREGS:
84 #ifdef __i386__
85 (void) CHECK_1 ((struct user_fpxregs_struct *) data);
86 #else
87 /* We don't know the size of data, so the best we can do is ensure
88 that `data' is valid for at least one word. */
89 (void) CHECK_1 ((int *) data);
90 #endif
91 break;
93 case PTRACE_GETSIGINFO:
94 case PTRACE_SETSIGINFO:
95 (void) CHECK_1 ((siginfo_t *) data);
96 break;
98 case PTRACE_GETEVENTMSG:
99 (void) CHECK_1 ((unsigned long *) data);
100 break;
102 case PTRACE_SETOPTIONS:
103 (void) CHECK_1 ((long *) data);
104 break;
106 case PTRACE_TRACEME:
107 case PTRACE_CONT:
108 case PTRACE_KILL:
109 case PTRACE_SINGLESTEP:
110 case PTRACE_ATTACH:
111 case PTRACE_DETACH:
112 case PTRACE_SYSCALL:
113 /* Neither `data' nor `addr' needs any checks. */
114 break;
116 #endif
118 res = INLINE_SYSCALL (ptrace, 4, request, pid,
119 __ptrvalue (addr), __ptrvalue (data));
120 if (res >= 0 && request > 0 && request < 4)
122 __set_errno (0);
123 return ret;
126 return res;