aboutsummaryrefslogtreecommitdiff
path: root/arch/um/kernel/tt/ptproxy/ptrace.c
blob: 528a5fc8d8879e85eb641bd5f0e84ea7aaca45f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**********************************************************************
ptrace.c

Copyright (C) 1999 Lars Brinkhoff.  See the file COPYING for licensing
terms and conditions.

Jeff Dike (jdike@karaya.com) : Modified for integration into uml
**********************************************************************/

#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>

#include "ptproxy.h"
#include "debug.h"
#include "user_util.h"
#include "kern_util.h"
#include "ptrace_user.h"
#include "tt.h"

long proxy_ptrace(struct debugger *debugger, int arg1, pid_t arg2,
		  long arg3, long arg4, pid_t child, int *ret)
{
	sigset_t relay;
	long result;
	int status;

	*ret = 0;
	if(debugger->debugee->died) return(-ESRCH);

	switch(arg1){
	case PTRACE_ATTACH:
		if(debugger->debugee->traced) return(-EPERM);

		debugger->debugee->pid = arg2;
		debugger->debugee->traced = 1;

		if(is_valid_pid(arg2) && (arg2 != child)){
			debugger->debugee->in_context = 0;
			kill(arg2, SIGSTOP);
			debugger->debugee->event = 1;
			debugger->debugee->wait_status = W_STOPCODE(SIGSTOP);
		}
		else {
			debugger->debugee->in_context = 1;
			if(debugger->debugee->stopped) 
				child_proxy(child, W_STOPCODE(SIGSTOP));
			else kill(child, SIGSTOP);
		}

		return(0);

	case PTRACE_DETACH:
		if(!debugger->debugee->traced) return(-EPERM);
		
		debugger->debugee->traced = 0;
		debugger->debugee->pid = 0;
		if(!debugger->debugee->in_context)
			kill(child, SIGCONT);

		return(0);

	case PTRACE_CONT:
		if(!debugger->debugee->in_context) return(-EPERM);
		*ret = PTRACE_CONT;
		return(ptrace(PTRACE_CONT, child, arg3, arg4));

#ifdef UM_HAVE_GETFPREGS
	case PTRACE_GETFPREGS:
	{
		long regs[FP_FRAME_SIZE];
		int i, result;

		result = ptrace(PTRACE_GETFPREGS, child, 0, regs);
		if(result == -1) return(-errno);
		
		for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
			ptrace(PTRACE_POKEDATA, debugger->pid, arg4 + 4 * i,
			       regs[i]);
		return(result);
	}
#endif

#ifdef UM_HAVE_GETFPXREGS
	case PTRACE_GETFPXREGS:
	{
		long regs[FPX_FRAME_SIZE];
		int i, result;

		result = ptrace(PTRACE_GETFPXREGS, child, 0, regs);
		if(result == -1) return(-errno);
		
		for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
			ptrace(PTRACE_POKEDATA, debugger->pid, arg4 + 4 * i,
			       regs[i]);
		return(result);
	}
#endif

#ifdef UM_HAVE_GETREGS
	case PTRACE_GETREGS:
	{
		long regs[FRAME_SIZE];
		int i, result;

		result = ptrace(PTRACE_GETREGS, child, 0, regs);
		if(result == -1) return(-errno);

		for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
			ptrace (PTRACE_POKEDATA, debugger->pid,
				arg4 + 4 * i, regs[i]);
		return(result);
	}
	break;
#endif

	case PTRACE_KILL:
		result = ptrace(PTRACE_KILL, child, arg3, arg4);
		if(result == -1) return(-errno);

		return(result);

	case PTRACE_PEEKDATA:
	case PTRACE_PEEKTEXT:
	case PTRACE_PEEKUSR:
		/* The value being read out could be -1, so we have to 
		 * check errno to see if there's an error, and zero it
		 * beforehand so we're not faked out by an old error
		 */

		errno = 0;
		result = ptrace(arg1, child, arg3, 0);
		if((result == -1) && (errno != 0)) return(-errno);

		result = ptrace(PTRACE_POKEDATA, debugger->pid, arg4, result);
		if(result == -1) return(-errno);
			
		return(result);

	case PTRACE_POKEDATA:
	case PTRACE_POKETEXT:
	case PTRACE_POKEUSR:
		result = ptrace(arg1, child, arg3, arg4);
		if(result == -1) return(-errno);

		if(arg1 == PTRACE_POKEUSR) ptrace_pokeuser(arg3, arg4);
		return(result);

#ifdef UM_HAVE_SETFPREGS
	case PTRACE_SETFPREGS:
	{
		long regs[FP_FRAME_SIZE];
		int i;

		for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
			regs[i] = ptrace (PTRACE_PEEKDATA, debugger->pid,
					  arg4 + 4 * i, 0);
		result = ptrace(PTRACE_SETFPREGS, child, 0, regs);
		if(result == -1) return(-errno);

		return(result);
	}
#endif

#ifdef UM_HAVE_SETFPXREGS
	case PTRACE_SETFPXREGS:
	{
		long regs[FPX_FRAME_SIZE];
		int i;

		for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
			regs[i] = ptrace (PTRACE_PEEKDATA, debugger->pid,
					  arg4 + 4 * i, 0);
		result = ptrace(PTRACE_SETFPXREGS, child, 0, regs);
		if(result == -1) return(-errno);

		return(result);
	}
#endif

#ifdef UM_HAVE_SETREGS
	case PTRACE_SETREGS:
	{
		long regs[FRAME_SIZE];
		int i;

		for (i = 0; i < sizeof(regs)/sizeof(regs[0]); i++)
			regs[i] = ptrace(PTRACE_PEEKDATA, debugger->pid,
					 arg4 + 4 * i, 0);
		result = ptrace(PTRACE_SETREGS, child, 0, regs);
		if(result == -1) return(-errno);

		return(result);
	}
#endif

	case PTRACE_SINGLESTEP:
		if(!debugger->debugee->in_context) return(-EPERM);
		sigemptyset(&relay);
		sigaddset(&relay, SIGSEGV);
		sigaddset(&relay, SIGILL);
		sigaddset(&relay, SIGBUS);
		result = ptrace(PTRACE_SINGLESTEP, child, arg3, arg4);
		if(result == -1) return(-errno);
		
		status = wait_for_stop(child, SIGTRAP, PTRACE_SINGLESTEP,
				       &relay);
		child_proxy(child, status);
		return(result);

	case PTRACE_SYSCALL:
		if(!debugger->debugee->in_context) return(-EPERM);
		result = ptrace(PTRACE_SYSCALL, child, arg3, arg4);
		if(result == -1) return(-errno);

		*ret = PTRACE_SYSCALL;
		return(result);

	case PTRACE_TRACEME:
	default:
		return(-EINVAL);
	}
}

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-file-style: "linux"
 * End:
 */