blob: cf334cb7cbfc25043c3e48b3888632f1b9306bce [file] [log] [blame]
Alexandre Julliard3e2517c2000-01-20 18:59:03 +00001/*
2 * i386 register context support
3 *
4 * Copyright (C) 1999 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000019 */
20
21#include "config.h"
22
23#ifdef __i386__
24
25#include <assert.h>
26#include <errno.h>
Ulrich Weigand8a1bdb32000-01-30 22:22:22 +000027#ifdef HAVE_SYS_REG_H
Patrik Stridvallde20c2a2000-01-30 00:23:12 +000028#include <sys/reg.h>
Ulrich Weigand8a1bdb32000-01-30 22:22:22 +000029#endif
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000030#include <stdarg.h>
Patrik Stridvallde20c2a2000-01-30 00:23:12 +000031#include <unistd.h>
Dimitrie O. Paun2af03e42000-11-29 20:04:09 +000032#ifdef HAVE_SYS_PTRACE_H
33# include <sys/ptrace.h>
34#endif
Patrik Stridvall49bcb932000-02-25 20:55:33 +000035#ifdef HAVE_SYS_PARAM_H
36# include <sys/param.h>
37#endif
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000038
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000039#include "windef.h"
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000040
41#include "file.h"
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000042#include "thread.h"
43#include "request.h"
44
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000045#ifndef PTRACE_PEEKUSER
Peter Hunnisettde1ff662000-03-24 19:45:11 +000046# ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
47# define PTRACE_PEEKUSER PTRACE_PEEKUSR
48# else
49# define PTRACE_PEEKUSER PT_READ_U
50# endif
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000051#endif
Peter Hunnisettde1ff662000-03-24 19:45:11 +000052
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000053#ifndef PTRACE_POKEUSER
Peter Hunnisettde1ff662000-03-24 19:45:11 +000054# ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
55# define PTRACE_POKEUSER PTRACE_POKEUSR
56# else
57# define PTRACE_POKEUSER PT_WRITE_U
58# endif
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000059#endif
Peter Hunnisettde1ff662000-03-24 19:45:11 +000060
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000061#ifndef PTRACE_GETREGS
62#define PTRACE_GETREGS PT_GETREGS
63#endif
64#ifndef PTRACE_GETFPREGS
65#define PTRACE_GETFPREGS PT_GETFPREGS
66#endif
Juergen Lock57827652000-02-07 16:03:08 +000067#ifndef PTRACE_SETREGS
68#define PTRACE_SETREGS PT_SETREGS
69#endif
70#ifndef PTRACE_SETFPREGS
71#define PTRACE_SETFPREGS PT_SETFPREGS
72#endif
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000073
Pierre Beyssac35287552002-08-14 20:59:03 +000074#ifdef PT_GETDBREGS
75#define PTRACE_GETDBREGS PT_GETDBREGS
76#endif
77
78#ifdef PT_SETDBREGS
79#define PTRACE_SETDBREGS PT_SETDBREGS
80#endif
81
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000082#ifdef linux
Francois Gouget5c84fff2001-11-20 18:52:42 +000083#ifdef HAVE_SYS_USER_H
84# include <sys/user.h>
85#endif
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000086
Alexandre Julliarda6611872000-01-29 23:35:42 +000087/* user_regs definitions from asm/user.h */
88struct kernel_user_regs_struct
89{
90 long ebx, ecx, edx, esi, edi, ebp, eax;
91 unsigned short ds, __ds, es, __es;
92 unsigned short fs, __fs, gs, __gs;
93 long orig_eax, eip;
94 unsigned short cs, __cs;
95 long eflags, esp;
96 unsigned short ss, __ss;
97};
98
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000099/* debug register offset in struct user */
100#define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
101
102/* retrieve a debug register */
103static inline int get_debug_reg( int pid, int num, DWORD *data )
104{
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000105 int res;
106 errno = 0;
107 res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000108 if ((res == -1) && errno)
109 {
110 file_set_error();
111 return -1;
112 }
113 *data = res;
114 return 0;
115}
116
117/* retrieve a thread context */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000118static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000119{
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000120 int pid = get_ptrace_pid(thread);
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000121 if (flags & CONTEXT_FULL)
122 {
Alexandre Julliarda6611872000-01-29 23:35:42 +0000123 struct kernel_user_regs_struct regs;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000124 if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
125 if (flags & CONTEXT_INTEGER)
126 {
127 context->Eax = regs.eax;
128 context->Ebx = regs.ebx;
129 context->Ecx = regs.ecx;
130 context->Edx = regs.edx;
131 context->Esi = regs.esi;
132 context->Edi = regs.edi;
133 }
134 if (flags & CONTEXT_CONTROL)
135 {
136 context->Ebp = regs.ebp;
137 context->Esp = regs.esp;
138 context->Eip = regs.eip;
Alexandre Julliarda6611872000-01-29 23:35:42 +0000139 context->SegCs = regs.cs;
140 context->SegSs = regs.ss;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000141 context->EFlags = regs.eflags;
142 }
143 if (flags & CONTEXT_SEGMENTS)
144 {
Alexandre Julliarda6611872000-01-29 23:35:42 +0000145 context->SegDs = regs.ds;
146 context->SegEs = regs.es;
147 context->SegFs = regs.fs;
148 context->SegGs = regs.gs;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000149 }
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100150 context->ContextFlags |= flags & CONTEXT_FULL;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000151 }
152 if (flags & CONTEXT_DEBUG_REGISTERS)
153 {
154 if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
155 if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
156 if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
157 if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
158 if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
159 if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100160 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000161 }
162 if (flags & CONTEXT_FLOATING_POINT)
163 {
164 /* we can use context->FloatSave directly as it is using the */
165 /* correct structure (the same as fsave/frstor) */
166 if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
167 context->FloatSave.Cr0NpxState = 0; /* FIXME */
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100168 context->ContextFlags |= CONTEXT_FLOATING_POINT;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000169 }
170 return;
171 error:
172 file_set_error();
173}
174
175
176/* set a thread context */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000177static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000178{
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000179 int pid = get_ptrace_pid(thread);
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000180 if (flags & CONTEXT_FULL)
181 {
Alexandre Julliarda6611872000-01-29 23:35:42 +0000182 struct kernel_user_regs_struct regs;
Alexandre Julliard76141802001-07-11 17:30:59 +0000183
184 /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
185 if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
186
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000187 if (flags & CONTEXT_INTEGER)
188 {
189 regs.eax = context->Eax;
190 regs.ebx = context->Ebx;
191 regs.ecx = context->Ecx;
192 regs.edx = context->Edx;
193 regs.esi = context->Esi;
194 regs.edi = context->Edi;
195 }
196 if (flags & CONTEXT_CONTROL)
197 {
198 regs.ebp = context->Ebp;
199 regs.esp = context->Esp;
200 regs.eip = context->Eip;
Alexandre Julliarda6611872000-01-29 23:35:42 +0000201 regs.cs = context->SegCs;
202 regs.ss = context->SegSs;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000203 regs.eflags = context->EFlags;
204 }
205 if (flags & CONTEXT_SEGMENTS)
206 {
Alexandre Julliarda6611872000-01-29 23:35:42 +0000207 regs.ds = context->SegDs;
208 regs.es = context->SegEs;
209 regs.fs = context->SegFs;
210 regs.gs = context->SegGs;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000211 }
212 if (ptrace( PTRACE_SETREGS, pid, 0, &regs ) == -1) goto error;
213 }
214 if (flags & CONTEXT_DEBUG_REGISTERS)
215 {
216 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
217 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
218 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
219 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
220 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
221 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
222 }
223 if (flags & CONTEXT_FLOATING_POINT)
224 {
225 /* we can use context->FloatSave directly as it is using the */
226 /* correct structure (the same as fsave/frstor) */
227 if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000228 }
229 return;
230 error:
231 file_set_error();
232}
233
Patrik Stridvall0e06d622000-02-26 13:16:13 +0000234#elif defined(__sun) || defined(__sun__)
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000235
236/* retrieve a thread context */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000237static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000238{
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000239 int pid = get_ptrace_pid(thread);
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000240 if (flags & CONTEXT_FULL)
241 {
242 struct regs regs;
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000243 if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000244 if (flags & CONTEXT_INTEGER)
245 {
246 context->Eax = regs.r_eax;
247 context->Ebx = regs.r_ebx;
248 context->Ecx = regs.r_ecx;
249 context->Edx = regs.r_edx;
250 context->Esi = regs.r_esi;
251 context->Edi = regs.r_edi;
252 }
253 if (flags & CONTEXT_CONTROL)
254 {
255 context->Ebp = regs.r_ebp;
256 context->Esp = regs.r_esp;
257 context->Eip = regs.r_eip;
258 context->SegCs = regs.r_cs & 0xffff;
259 context->SegSs = regs.r_ss & 0xffff;
260 context->EFlags = regs.r_efl;
261 }
262 if (flags & CONTEXT_SEGMENTS)
263 {
264 context->SegDs = regs.r_ds & 0xffff;
265 context->SegEs = regs.r_es & 0xffff;
266 context->SegFs = regs.r_fs & 0xffff;
267 context->SegGs = regs.r_gs & 0xffff;
268 }
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100269 context->ContextFlags |= flags & CONTEXT_FULL;
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000270 }
271 if (flags & CONTEXT_DEBUG_REGISTERS)
272 {
273 /* FIXME: How is this done on Solaris? */
274 }
275 if (flags & CONTEXT_FLOATING_POINT)
276 {
277 /* we can use context->FloatSave directly as it is using the */
278 /* correct structure (the same as fsave/frstor) */
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000279 if (ptrace( PTRACE_GETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000280 context->FloatSave.Cr0NpxState = 0; /* FIXME */
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100281 context->ContextFlags |= CONTEXT_FLOATING_POINT;
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000282 }
283 return;
284 error:
285 file_set_error();
286}
287
288
289/* set a thread context */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000290static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000291{
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000292 int pid = get_ptrace_pid(thread);
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000293 if (flags & CONTEXT_FULL)
294 {
295 struct regs regs;
Alexandre Julliardcd8cf5e2000-03-15 19:47:44 +0000296 if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000297 {
Alexandre Julliardcd8cf5e2000-03-15 19:47:44 +0000298 /* need to preserve some registers */
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000299 if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000300 }
301 if (flags & CONTEXT_INTEGER)
302 {
303 regs.r_eax = context->Eax;
304 regs.r_ebx = context->Ebx;
305 regs.r_ecx = context->Ecx;
306 regs.r_edx = context->Edx;
307 regs.r_esi = context->Esi;
308 regs.r_edi = context->Edi;
309 }
310 if (flags & CONTEXT_CONTROL)
311 {
312 regs.r_ebp = context->Ebp;
313 regs.r_esp = context->Esp;
314 regs.r_eip = context->Eip;
315 regs.r_cs = context->SegCs;
316 regs.r_ss = context->SegSs;
317 regs.r_efl = context->EFlags;
318 }
319 if (flags & CONTEXT_SEGMENTS)
320 {
321 regs.r_ds = context->SegDs;
322 regs.r_es = context->SegEs;
323 regs.r_fs = context->SegFs;
324 regs.r_gs = context->SegGs;
325 }
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000326 if (ptrace( PTRACE_SETREGS, pid, (int) &regs, 0 ) == -1) goto error;
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000327 }
328 if (flags & CONTEXT_DEBUG_REGISTERS)
329 {
330 /* FIXME: How is this done on Solaris? */
331 }
332 if (flags & CONTEXT_FLOATING_POINT)
333 {
334 /* we can use context->FloatSave directly as it is using the */
335 /* correct structure (the same as fsave/frstor) */
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000336 if (ptrace( PTRACE_SETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
Patrik Stridvallde20c2a2000-01-30 00:23:12 +0000337 }
338 return;
339 error:
340 file_set_error();
341}
342
Robert Millan88003522006-02-05 13:06:50 +0100343#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__)
Juergen Lock57827652000-02-07 16:03:08 +0000344#include <machine/reg.h>
345
346/* retrieve a thread context */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000347static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
Juergen Lock57827652000-02-07 16:03:08 +0000348{
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000349 int pid = get_ptrace_pid(thread);
Juergen Lock57827652000-02-07 16:03:08 +0000350 if (flags & CONTEXT_FULL)
351 {
352 struct reg regs;
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000353 if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
Juergen Lock57827652000-02-07 16:03:08 +0000354 if (flags & CONTEXT_INTEGER)
355 {
356 context->Eax = regs.r_eax;
357 context->Ebx = regs.r_ebx;
358 context->Ecx = regs.r_ecx;
359 context->Edx = regs.r_edx;
360 context->Esi = regs.r_esi;
361 context->Edi = regs.r_edi;
362 }
363 if (flags & CONTEXT_CONTROL)
364 {
365 context->Ebp = regs.r_ebp;
366 context->Esp = regs.r_esp;
367 context->Eip = regs.r_eip;
368 context->SegCs = regs.r_cs & 0xffff;
369 context->SegSs = regs.r_ss & 0xffff;
370 context->EFlags = regs.r_eflags;
371 }
372 if (flags & CONTEXT_SEGMENTS)
373 {
374 context->SegDs = regs.r_ds & 0xffff;
375 context->SegEs = regs.r_es & 0xffff;
376 context->SegFs = regs.r_fs & 0xffff;
377 context->SegGs = regs.r_gs & 0xffff;
378 }
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100379 context->ContextFlags |= flags & CONTEXT_FULL;
Juergen Lock57827652000-02-07 16:03:08 +0000380 }
381 if (flags & CONTEXT_DEBUG_REGISTERS)
382 {
Pierre Beyssac35287552002-08-14 20:59:03 +0000383#ifdef PTRACE_GETDBREGS
384 struct dbreg dbregs;
385 if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
386 goto error;
Pierre Beyssac4c5c90e2002-11-08 18:55:31 +0000387#ifdef DBREG_DRX
388 /* needed for FreeBSD, the structure fields have changed under 5.x */
389 context->Dr0 = DBREG_DRX((&dbregs), 0);
390 context->Dr1 = DBREG_DRX((&dbregs), 1);
391 context->Dr2 = DBREG_DRX((&dbregs), 2);
392 context->Dr3 = DBREG_DRX((&dbregs), 3);
393 context->Dr6 = DBREG_DRX((&dbregs), 6);
394 context->Dr7 = DBREG_DRX((&dbregs), 7);
395#else
Pierre Beyssac35287552002-08-14 20:59:03 +0000396 context->Dr0 = dbregs.dr0;
397 context->Dr1 = dbregs.dr1;
398 context->Dr2 = dbregs.dr2;
399 context->Dr3 = dbregs.dr3;
400 context->Dr6 = dbregs.dr6;
401 context->Dr7 = dbregs.dr7;
402#endif
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100403 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
Pierre Beyssac4c5c90e2002-11-08 18:55:31 +0000404#endif
Juergen Lock57827652000-02-07 16:03:08 +0000405 }
406 if (flags & CONTEXT_FLOATING_POINT)
407 {
408 /* we can use context->FloatSave directly as it is using the */
409 /* correct structure (the same as fsave/frstor) */
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000410 if (ptrace( PTRACE_GETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
Juergen Lock57827652000-02-07 16:03:08 +0000411 context->FloatSave.Cr0NpxState = 0; /* FIXME */
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100412 context->ContextFlags |= CONTEXT_FLOATING_POINT;
Juergen Lock57827652000-02-07 16:03:08 +0000413 }
414 return;
415 error:
416 file_set_error();
417}
418
419
420/* set a thread context */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000421static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
Juergen Lock57827652000-02-07 16:03:08 +0000422{
Alexandre Julliardbaf0a062003-03-11 01:48:53 +0000423 int pid = get_ptrace_pid(thread);
Juergen Lock57827652000-02-07 16:03:08 +0000424 if (flags & CONTEXT_FULL)
425 {
426 struct reg regs;
Alexandre Julliardcd8cf5e2000-03-15 19:47:44 +0000427 if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
Juergen Lock57827652000-02-07 16:03:08 +0000428 {
Alexandre Julliardcd8cf5e2000-03-15 19:47:44 +0000429 /* need to preserve some registers */
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000430 if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
Juergen Lock57827652000-02-07 16:03:08 +0000431 }
432 if (flags & CONTEXT_INTEGER)
433 {
434 regs.r_eax = context->Eax;
435 regs.r_ebx = context->Ebx;
436 regs.r_ecx = context->Ecx;
437 regs.r_edx = context->Edx;
438 regs.r_esi = context->Esi;
439 regs.r_edi = context->Edi;
440 }
441 if (flags & CONTEXT_CONTROL)
442 {
443 regs.r_ebp = context->Ebp;
444 regs.r_esp = context->Esp;
445 regs.r_eip = context->Eip;
446 regs.r_cs = context->SegCs;
447 regs.r_ss = context->SegSs;
448 regs.r_eflags = context->EFlags;
449 }
450 if (flags & CONTEXT_SEGMENTS)
451 {
452 regs.r_ds = context->SegDs;
453 regs.r_es = context->SegEs;
454 regs.r_fs = context->SegFs;
455 regs.r_gs = context->SegGs;
456 }
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000457 if (ptrace( PTRACE_SETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
Juergen Lock57827652000-02-07 16:03:08 +0000458 }
459 if (flags & CONTEXT_DEBUG_REGISTERS)
460 {
Pierre Beyssac35287552002-08-14 20:59:03 +0000461#ifdef PTRACE_SETDBREGS
462 struct dbreg dbregs;
Pierre Beyssac4c5c90e2002-11-08 18:55:31 +0000463#ifdef DBREG_DRX
464 /* needed for FreeBSD, the structure fields have changed under 5.x */
465 DBREG_DRX((&dbregs), 0) = context->Dr0;
466 DBREG_DRX((&dbregs), 1) = context->Dr1;
467 DBREG_DRX((&dbregs), 2) = context->Dr2;
468 DBREG_DRX((&dbregs), 3) = context->Dr3;
469 DBREG_DRX((&dbregs), 4) = 0;
470 DBREG_DRX((&dbregs), 5) = 0;
471 DBREG_DRX((&dbregs), 6) = context->Dr6;
472 DBREG_DRX((&dbregs), 7) = context->Dr7;
473#else
Pierre Beyssac35287552002-08-14 20:59:03 +0000474 dbregs.dr0 = context->Dr0;
475 dbregs.dr1 = context->Dr1;
476 dbregs.dr2 = context->Dr2;
477 dbregs.dr3 = context->Dr3;
478 dbregs.dr4 = 0;
479 dbregs.dr5 = 0;
480 dbregs.dr6 = context->Dr6;
481 dbregs.dr7 = context->Dr7;
Pierre Beyssac4c5c90e2002-11-08 18:55:31 +0000482#endif
Pierre Beyssac35287552002-08-14 20:59:03 +0000483 if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
484 goto error;
485#endif
Juergen Lock57827652000-02-07 16:03:08 +0000486 }
487 if (flags & CONTEXT_FLOATING_POINT)
488 {
489 /* we can use context->FloatSave directly as it is using the */
490 /* correct structure (the same as fsave/frstor) */
Pierre Beyssacbd15bef2002-08-13 03:18:14 +0000491 if (ptrace( PTRACE_SETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
Juergen Lock57827652000-02-07 16:03:08 +0000492 }
493 return;
494 error:
495 file_set_error();
496}
497
498#else /* linux || __sun__ || __FreeBSD__ */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000499#error You must implement get/set_thread_context_ptrace for your platform
Juergen Lock57827652000-02-07 16:03:08 +0000500#endif /* linux || __sun__ || __FreeBSD__ */
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000501
502
503/* copy a context structure according to the flags */
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100504static void copy_context( CONTEXT *to, const CONTEXT *from, unsigned int flags )
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000505{
506 if (flags & CONTEXT_CONTROL)
507 {
508 to->Ebp = from->Ebp;
509 to->Eip = from->Eip;
510 to->Esp = from->Esp;
511 to->SegCs = from->SegCs;
512 to->SegSs = from->SegSs;
513 to->EFlags = from->EFlags;
514 }
515 if (flags & CONTEXT_INTEGER)
516 {
517 to->Eax = from->Eax;
518 to->Ebx = from->Ebx;
519 to->Ecx = from->Ecx;
520 to->Edx = from->Edx;
521 to->Esi = from->Esi;
522 to->Edi = from->Edi;
523 }
524 if (flags & CONTEXT_SEGMENTS)
525 {
526 to->SegDs = from->SegDs;
527 to->SegEs = from->SegEs;
528 to->SegFs = from->SegFs;
529 to->SegGs = from->SegGs;
530 }
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000531 if (flags & CONTEXT_FLOATING_POINT)
532 {
533 to->FloatSave = from->FloatSave;
534 }
Alexandre Julliardcd8cf5e2000-03-15 19:47:44 +0000535 /* we don't bother copying the debug registers, since they */
536 /* always need to be accessed by ptrace anyway */
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100537 to->ContextFlags |= flags & ~CONTEXT_DEBUG_REGISTERS;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000538}
539
Alexandre Julliardb73421d2000-03-30 19:30:24 +0000540/* retrieve the current instruction pointer of a thread */
541void *get_thread_ip( struct thread *thread )
542{
543 CONTEXT context;
544 context.Eip = 0;
545 if (suspend_for_ptrace( thread ))
546 {
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000547 get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
Alexandre Julliardd04ccb82003-03-04 22:18:43 +0000548 resume_after_ptrace( thread );
Alexandre Julliardb73421d2000-03-30 19:30:24 +0000549 }
550 return (void *)context.Eip;
551}
552
Ove Kaavenada73832001-04-27 18:39:47 +0000553/* determine if we should continue the thread in single-step mode */
554int get_thread_single_step( struct thread *thread )
555{
556 CONTEXT context;
557 if (thread->context) return 0; /* don't single-step inside exception event */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000558 get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
Ove Kaavenada73832001-04-27 18:39:47 +0000559 return (context.EFlags & 0x100) != 0;
560}
561
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000562/* send a signal to a specific thread */
Eric Pouech22357ca2006-02-14 12:24:32 +0100563int tkill( int tgid, int pid, int sig )
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000564{
565#ifdef __linux__
566 int ret;
Eric Pouech22357ca2006-02-14 12:24:32 +0100567
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000568 __asm__( "pushl %%ebx\n\t"
569 "movl %2,%%ebx\n\t"
570 "int $0x80\n\t"
571 "popl %%ebx\n\t"
572 : "=a" (ret)
Eric Pouech22357ca2006-02-14 12:24:32 +0100573 : "0" (270) /*SYS_tgkill*/, "r" (tgid), "c" (pid), "d" (sig) );
574 if (ret == -ENOSYS)
575 __asm__( "pushl %%ebx\n\t"
576 "movl %2,%%ebx\n\t"
577 "int $0x80\n\t"
578 "popl %%ebx\n\t"
579 : "=a" (ret)
580 : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
Alexandre Julliard22824cb2003-05-07 03:22:22 +0000581 if (ret >= 0) return ret;
Alexandre Julliarda8497bd2003-03-22 21:00:09 +0000582 errno = -ret;
583 return -1;
584#else
585 errno = ENOSYS;
586 return -1;
587#endif
588}
589
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000590/* retrieve the thread context */
591void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000592{
Alexandre Julliardbd298b52005-12-21 20:06:42 +0100593 context->ContextFlags |= CONTEXT_i386;
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000594 flags &= ~CONTEXT_i386; /* get rid of CPU id */
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000595
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000596 if (thread->context) /* thread is inside an exception event or suspended */
Alexandre Julliard92643002000-08-31 01:59:51 +0000597 {
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000598 copy_context( context, thread->context, flags );
599 flags &= CONTEXT_DEBUG_REGISTERS;
Alexandre Julliard92643002000-08-31 01:59:51 +0000600 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000601
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000602 if (flags && suspend_for_ptrace( thread ))
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000603 {
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000604 get_thread_context_ptrace( thread, flags, context );
605 resume_after_ptrace( thread );
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000606 }
607}
608
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000609/* set the thread context */
610void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000611{
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000612 flags &= ~CONTEXT_i386; /* get rid of CPU id */
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000613
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000614 if (thread->context) /* thread is inside an exception event or suspended */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000615 copy_context( thread->context, context, flags );
Alexandre Julliard2654be02006-01-11 20:20:32 +0100616
617 flags &= CONTEXT_DEBUG_REGISTERS; /* the other registers are handled on the client side */
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000618
619 if (flags && suspend_for_ptrace( thread ))
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000620 {
Alexandre Julliard3f85e262005-11-02 14:12:13 +0000621 set_thread_context_ptrace( thread, flags, context );
622 resume_after_ptrace( thread );
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000623 }
624}
625
626#endif /* __i386__ */