[BZ #1978]
[glibc.git] / sysdeps / unix / sysv / linux / ptrace.c
blobe33eee0ee815c3a3d66bc87a64c06f4bf1e31aec
1 /* Copyright (C) 1995,1996,1997,1998,2000,2003 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>
25 #include <sysdep.h>
26 #include <sys/syscall.h>
27 #include <bp-checks.h>
29 long int
30 ptrace (enum __ptrace_request request, ...)
32 long int res, ret;
33 va_list ap;
34 pid_t pid;
35 void *addr, *data;
37 va_start (ap, request);
38 pid = va_arg (ap, pid_t);
39 addr = va_arg (ap, void *);
40 data = va_arg (ap, void *);
41 va_end (ap);
43 if (request > 0 && request < 4)
44 data = &ret;
46 #if __BOUNDED_POINTERS__
47 switch (request)
49 case PTRACE_PEEKTEXT:
50 case PTRACE_PEEKDATA:
51 case PTRACE_PEEKUSER:
52 case PTRACE_POKETEXT:
53 case PTRACE_POKEDATA:
54 case PTRACE_POKEUSER:
55 (void) CHECK_1 ((int *) addr);
56 (void) CHECK_1 ((int *) data);
57 break;
59 case PTRACE_GETREGS:
60 case PTRACE_SETREGS:
61 #ifdef __i386__
62 (void) CHECK_1 ((struct user_regs_struct *) data);
63 #else
64 /* We don't know the size of data, so the best we can do is ensure
65 that `data' is valid for at least one word. */
66 (void) CHECK_1 ((int *) data);
67 #endif
68 break;
70 case PTRACE_GETFPREGS:
71 case PTRACE_SETFPREGS:
72 #ifdef __i386__
73 (void) CHECK_1 ((struct user_fpregs_struct *) data);
74 #else
75 /* We don't know the size of data, so the best we can do is ensure
76 that `data' is valid for at least one word. */
77 (void) CHECK_1 ((int *) data);
78 #endif
79 break;
81 case PTRACE_GETFPXREGS:
82 case PTRACE_SETFPXREGS:
83 #ifdef __i386__
84 (void) CHECK_1 ((struct user_fpxregs_struct *) data);
85 #else
86 /* We don't know the size of data, so the best we can do is ensure
87 that `data' is valid for at least one word. */
88 (void) CHECK_1 ((int *) data);
89 #endif
90 break;
92 case PTRACE_TRACEME:
93 case PTRACE_CONT:
94 case PTRACE_KILL:
95 case PTRACE_SINGLESTEP:
96 case PTRACE_ATTACH:
97 case PTRACE_DETACH:
98 case PTRACE_SYSCALL:
99 /* Neither `data' nor `addr' needs any checks. */
100 break;
102 #endif
104 res = INLINE_SYSCALL (ptrace, 4, request, pid,
105 __ptrvalue (addr), __ptrvalue (data));
106 if (res >= 0 && request > 0 && request < 4)
108 __set_errno (0);
109 return ret;
112 return res;