2000-01-20 19:59:03 +01:00
|
|
|
/*
|
|
|
|
* i386 register context support
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2000-01-20 19:59:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2000-01-30 23:22:22 +01:00
|
|
|
#ifdef HAVE_SYS_REG_H
|
2000-01-30 01:23:12 +01:00
|
|
|
#include <sys/reg.h>
|
2000-01-30 23:22:22 +01:00
|
|
|
#endif
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-02-07 17:03:08 +01:00
|
|
|
#include <unistd.h>
|
2000-11-29 21:04:09 +01:00
|
|
|
#ifdef HAVE_SYS_PTRACE_H
|
|
|
|
# include <sys/ptrace.h>
|
|
|
|
#endif
|
2000-02-25 21:55:33 +01:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
#endif
|
2000-01-20 19:59:03 +01:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2003-02-14 21:27:09 +01:00
|
|
|
|
|
|
|
#include "file.h"
|
2000-01-20 19:59:03 +01:00
|
|
|
#include "thread.h"
|
|
|
|
#include "request.h"
|
|
|
|
|
|
|
|
#ifndef PTRACE_PEEKUSER
|
2000-03-24 20:45:11 +01:00
|
|
|
# ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
|
|
|
|
# define PTRACE_PEEKUSER PTRACE_PEEKUSR
|
|
|
|
# else
|
|
|
|
# define PTRACE_PEEKUSER PT_READ_U
|
|
|
|
# endif
|
2000-01-20 19:59:03 +01:00
|
|
|
#endif
|
2000-03-24 20:45:11 +01:00
|
|
|
|
2000-01-20 19:59:03 +01:00
|
|
|
#ifndef PTRACE_POKEUSER
|
2000-03-24 20:45:11 +01:00
|
|
|
# ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
|
|
|
|
# define PTRACE_POKEUSER PTRACE_POKEUSR
|
|
|
|
# else
|
|
|
|
# define PTRACE_POKEUSER PT_WRITE_U
|
|
|
|
# endif
|
2000-01-20 19:59:03 +01:00
|
|
|
#endif
|
2000-03-24 20:45:11 +01:00
|
|
|
|
2000-01-20 19:59:03 +01:00
|
|
|
#ifndef PTRACE_GETREGS
|
|
|
|
#define PTRACE_GETREGS PT_GETREGS
|
|
|
|
#endif
|
|
|
|
#ifndef PTRACE_GETFPREGS
|
|
|
|
#define PTRACE_GETFPREGS PT_GETFPREGS
|
|
|
|
#endif
|
2000-02-07 17:03:08 +01:00
|
|
|
#ifndef PTRACE_SETREGS
|
|
|
|
#define PTRACE_SETREGS PT_SETREGS
|
|
|
|
#endif
|
|
|
|
#ifndef PTRACE_SETFPREGS
|
|
|
|
#define PTRACE_SETFPREGS PT_SETFPREGS
|
|
|
|
#endif
|
2000-01-20 19:59:03 +01:00
|
|
|
|
2002-08-14 22:59:03 +02:00
|
|
|
#ifdef PT_GETDBREGS
|
|
|
|
#define PTRACE_GETDBREGS PT_GETDBREGS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PT_SETDBREGS
|
|
|
|
#define PTRACE_SETDBREGS PT_SETDBREGS
|
|
|
|
#endif
|
|
|
|
|
2000-01-20 19:59:03 +01:00
|
|
|
#ifdef linux
|
2001-11-20 19:52:42 +01:00
|
|
|
#ifdef HAVE_SYS_USER_H
|
|
|
|
# include <sys/user.h>
|
|
|
|
#endif
|
2000-01-20 19:59:03 +01:00
|
|
|
|
2000-01-30 00:35:42 +01:00
|
|
|
/* user_regs definitions from asm/user.h */
|
|
|
|
struct kernel_user_regs_struct
|
|
|
|
{
|
|
|
|
long ebx, ecx, edx, esi, edi, ebp, eax;
|
|
|
|
unsigned short ds, __ds, es, __es;
|
|
|
|
unsigned short fs, __fs, gs, __gs;
|
|
|
|
long orig_eax, eip;
|
|
|
|
unsigned short cs, __cs;
|
|
|
|
long eflags, esp;
|
|
|
|
unsigned short ss, __ss;
|
|
|
|
};
|
|
|
|
|
2000-01-20 19:59:03 +01:00
|
|
|
/* debug register offset in struct user */
|
|
|
|
#define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
|
|
|
|
|
|
|
|
/* retrieve a debug register */
|
|
|
|
static inline int get_debug_reg( int pid, int num, DWORD *data )
|
|
|
|
{
|
2003-03-22 22:00:09 +01:00
|
|
|
int res;
|
|
|
|
errno = 0;
|
|
|
|
res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
|
2000-01-20 19:59:03 +01:00
|
|
|
if ((res == -1) && errno)
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*data = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* retrieve a thread context */
|
2005-11-02 15:12:13 +01:00
|
|
|
static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2003-03-11 02:48:53 +01:00
|
|
|
int pid = get_ptrace_pid(thread);
|
2000-01-20 19:59:03 +01:00
|
|
|
if (flags & CONTEXT_FULL)
|
|
|
|
{
|
2000-01-30 00:35:42 +01:00
|
|
|
struct kernel_user_regs_struct regs;
|
2000-01-20 19:59:03 +01:00
|
|
|
if (ptrace( PTRACE_GETREGS, pid, 0, ®s ) == -1) goto error;
|
|
|
|
if (flags & CONTEXT_INTEGER)
|
|
|
|
{
|
|
|
|
context->Eax = regs.eax;
|
|
|
|
context->Ebx = regs.ebx;
|
|
|
|
context->Ecx = regs.ecx;
|
|
|
|
context->Edx = regs.edx;
|
|
|
|
context->Esi = regs.esi;
|
|
|
|
context->Edi = regs.edi;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_CONTROL)
|
|
|
|
{
|
|
|
|
context->Ebp = regs.ebp;
|
|
|
|
context->Esp = regs.esp;
|
|
|
|
context->Eip = regs.eip;
|
2000-01-30 00:35:42 +01:00
|
|
|
context->SegCs = regs.cs;
|
|
|
|
context->SegSs = regs.ss;
|
2000-01-20 19:59:03 +01:00
|
|
|
context->EFlags = regs.eflags;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_SEGMENTS)
|
|
|
|
{
|
2000-01-30 00:35:42 +01:00
|
|
|
context->SegDs = regs.ds;
|
|
|
|
context->SegEs = regs.es;
|
|
|
|
context->SegFs = regs.fs;
|
|
|
|
context->SegGs = regs.gs;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= flags & CONTEXT_FULL;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_DEBUG_REGISTERS)
|
|
|
|
{
|
|
|
|
if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
|
|
|
|
if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
|
|
|
|
if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
|
|
|
|
if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
|
|
|
|
if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
|
|
|
|
if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_FLOATING_POINT)
|
|
|
|
{
|
|
|
|
/* we can use context->FloatSave directly as it is using the */
|
|
|
|
/* correct structure (the same as fsave/frstor) */
|
|
|
|
if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
|
|
|
|
context->FloatSave.Cr0NpxState = 0; /* FIXME */
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= CONTEXT_FLOATING_POINT;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set a thread context */
|
2005-11-02 15:12:13 +01:00
|
|
|
static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2003-03-11 02:48:53 +01:00
|
|
|
int pid = get_ptrace_pid(thread);
|
2000-01-20 19:59:03 +01:00
|
|
|
if (flags & CONTEXT_FULL)
|
|
|
|
{
|
2000-01-30 00:35:42 +01:00
|
|
|
struct kernel_user_regs_struct regs;
|
2001-07-11 19:30:59 +02:00
|
|
|
|
|
|
|
/* need to preserve some registers (at a minimum orig_eax must always be preserved) */
|
|
|
|
if (ptrace( PTRACE_GETREGS, pid, 0, ®s ) == -1) goto error;
|
|
|
|
|
2000-01-20 19:59:03 +01:00
|
|
|
if (flags & CONTEXT_INTEGER)
|
|
|
|
{
|
|
|
|
regs.eax = context->Eax;
|
|
|
|
regs.ebx = context->Ebx;
|
|
|
|
regs.ecx = context->Ecx;
|
|
|
|
regs.edx = context->Edx;
|
|
|
|
regs.esi = context->Esi;
|
|
|
|
regs.edi = context->Edi;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_CONTROL)
|
|
|
|
{
|
|
|
|
regs.ebp = context->Ebp;
|
|
|
|
regs.esp = context->Esp;
|
|
|
|
regs.eip = context->Eip;
|
2000-01-30 00:35:42 +01:00
|
|
|
regs.cs = context->SegCs;
|
|
|
|
regs.ss = context->SegSs;
|
2000-01-20 19:59:03 +01:00
|
|
|
regs.eflags = context->EFlags;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_SEGMENTS)
|
|
|
|
{
|
2000-01-30 00:35:42 +01:00
|
|
|
regs.ds = context->SegDs;
|
|
|
|
regs.es = context->SegEs;
|
|
|
|
regs.fs = context->SegFs;
|
|
|
|
regs.gs = context->SegGs;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
if (ptrace( PTRACE_SETREGS, pid, 0, ®s ) == -1) goto error;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_DEBUG_REGISTERS)
|
|
|
|
{
|
|
|
|
if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
|
|
|
|
if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
|
|
|
|
if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
|
|
|
|
if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
|
|
|
|
if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
|
|
|
|
if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_FLOATING_POINT)
|
|
|
|
{
|
|
|
|
/* we can use context->FloatSave directly as it is using the */
|
|
|
|
/* correct structure (the same as fsave/frstor) */
|
|
|
|
if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
}
|
|
|
|
|
2000-02-26 14:16:13 +01:00
|
|
|
#elif defined(__sun) || defined(__sun__)
|
2000-01-30 01:23:12 +01:00
|
|
|
|
|
|
|
/* retrieve a thread context */
|
2005-11-02 15:12:13 +01:00
|
|
|
static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
|
2000-01-30 01:23:12 +01:00
|
|
|
{
|
2003-03-11 02:48:53 +01:00
|
|
|
int pid = get_ptrace_pid(thread);
|
2000-01-30 01:23:12 +01:00
|
|
|
if (flags & CONTEXT_FULL)
|
|
|
|
{
|
|
|
|
struct regs regs;
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_GETREGS, pid, (int) ®s, 0 ) == -1) goto error;
|
2000-01-30 01:23:12 +01:00
|
|
|
if (flags & CONTEXT_INTEGER)
|
|
|
|
{
|
|
|
|
context->Eax = regs.r_eax;
|
|
|
|
context->Ebx = regs.r_ebx;
|
|
|
|
context->Ecx = regs.r_ecx;
|
|
|
|
context->Edx = regs.r_edx;
|
|
|
|
context->Esi = regs.r_esi;
|
|
|
|
context->Edi = regs.r_edi;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_CONTROL)
|
|
|
|
{
|
|
|
|
context->Ebp = regs.r_ebp;
|
|
|
|
context->Esp = regs.r_esp;
|
|
|
|
context->Eip = regs.r_eip;
|
|
|
|
context->SegCs = regs.r_cs & 0xffff;
|
|
|
|
context->SegSs = regs.r_ss & 0xffff;
|
|
|
|
context->EFlags = regs.r_efl;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_SEGMENTS)
|
|
|
|
{
|
|
|
|
context->SegDs = regs.r_ds & 0xffff;
|
|
|
|
context->SegEs = regs.r_es & 0xffff;
|
|
|
|
context->SegFs = regs.r_fs & 0xffff;
|
|
|
|
context->SegGs = regs.r_gs & 0xffff;
|
|
|
|
}
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= flags & CONTEXT_FULL;
|
2000-01-30 01:23:12 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_DEBUG_REGISTERS)
|
|
|
|
{
|
|
|
|
/* FIXME: How is this done on Solaris? */
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_FLOATING_POINT)
|
|
|
|
{
|
|
|
|
/* we can use context->FloatSave directly as it is using the */
|
|
|
|
/* correct structure (the same as fsave/frstor) */
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_GETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
|
2000-01-30 01:23:12 +01:00
|
|
|
context->FloatSave.Cr0NpxState = 0; /* FIXME */
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= CONTEXT_FLOATING_POINT;
|
2000-01-30 01:23:12 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set a thread context */
|
2005-11-02 15:12:13 +01:00
|
|
|
static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
|
2000-01-30 01:23:12 +01:00
|
|
|
{
|
2003-03-11 02:48:53 +01:00
|
|
|
int pid = get_ptrace_pid(thread);
|
2000-01-30 01:23:12 +01:00
|
|
|
if (flags & CONTEXT_FULL)
|
|
|
|
{
|
|
|
|
struct regs regs;
|
2000-03-15 20:47:44 +01:00
|
|
|
if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
|
2000-01-30 01:23:12 +01:00
|
|
|
{
|
2000-03-15 20:47:44 +01:00
|
|
|
/* need to preserve some registers */
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_GETREGS, pid, (int) ®s, 0 ) == -1) goto error;
|
2000-01-30 01:23:12 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_INTEGER)
|
|
|
|
{
|
|
|
|
regs.r_eax = context->Eax;
|
|
|
|
regs.r_ebx = context->Ebx;
|
|
|
|
regs.r_ecx = context->Ecx;
|
|
|
|
regs.r_edx = context->Edx;
|
|
|
|
regs.r_esi = context->Esi;
|
|
|
|
regs.r_edi = context->Edi;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_CONTROL)
|
|
|
|
{
|
|
|
|
regs.r_ebp = context->Ebp;
|
|
|
|
regs.r_esp = context->Esp;
|
|
|
|
regs.r_eip = context->Eip;
|
|
|
|
regs.r_cs = context->SegCs;
|
|
|
|
regs.r_ss = context->SegSs;
|
|
|
|
regs.r_efl = context->EFlags;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_SEGMENTS)
|
|
|
|
{
|
|
|
|
regs.r_ds = context->SegDs;
|
|
|
|
regs.r_es = context->SegEs;
|
|
|
|
regs.r_fs = context->SegFs;
|
|
|
|
regs.r_gs = context->SegGs;
|
|
|
|
}
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_SETREGS, pid, (int) ®s, 0 ) == -1) goto error;
|
2000-01-30 01:23:12 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_DEBUG_REGISTERS)
|
|
|
|
{
|
|
|
|
/* FIXME: How is this done on Solaris? */
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_FLOATING_POINT)
|
|
|
|
{
|
|
|
|
/* we can use context->FloatSave directly as it is using the */
|
|
|
|
/* correct structure (the same as fsave/frstor) */
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_SETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
|
2000-01-30 01:23:12 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
}
|
|
|
|
|
2006-02-05 13:06:50 +01:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
2000-02-07 17:03:08 +01:00
|
|
|
#include <machine/reg.h>
|
|
|
|
|
|
|
|
/* retrieve a thread context */
|
2005-11-02 15:12:13 +01:00
|
|
|
static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
|
2000-02-07 17:03:08 +01:00
|
|
|
{
|
2003-03-11 02:48:53 +01:00
|
|
|
int pid = get_ptrace_pid(thread);
|
2000-02-07 17:03:08 +01:00
|
|
|
if (flags & CONTEXT_FULL)
|
|
|
|
{
|
|
|
|
struct reg regs;
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_GETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error;
|
2000-02-07 17:03:08 +01:00
|
|
|
if (flags & CONTEXT_INTEGER)
|
|
|
|
{
|
|
|
|
context->Eax = regs.r_eax;
|
|
|
|
context->Ebx = regs.r_ebx;
|
|
|
|
context->Ecx = regs.r_ecx;
|
|
|
|
context->Edx = regs.r_edx;
|
|
|
|
context->Esi = regs.r_esi;
|
|
|
|
context->Edi = regs.r_edi;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_CONTROL)
|
|
|
|
{
|
|
|
|
context->Ebp = regs.r_ebp;
|
|
|
|
context->Esp = regs.r_esp;
|
|
|
|
context->Eip = regs.r_eip;
|
|
|
|
context->SegCs = regs.r_cs & 0xffff;
|
|
|
|
context->SegSs = regs.r_ss & 0xffff;
|
|
|
|
context->EFlags = regs.r_eflags;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_SEGMENTS)
|
|
|
|
{
|
|
|
|
context->SegDs = regs.r_ds & 0xffff;
|
|
|
|
context->SegEs = regs.r_es & 0xffff;
|
|
|
|
context->SegFs = regs.r_fs & 0xffff;
|
|
|
|
context->SegGs = regs.r_gs & 0xffff;
|
|
|
|
}
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= flags & CONTEXT_FULL;
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_DEBUG_REGISTERS)
|
|
|
|
{
|
2002-08-14 22:59:03 +02:00
|
|
|
#ifdef PTRACE_GETDBREGS
|
|
|
|
struct dbreg dbregs;
|
|
|
|
if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
|
|
|
|
goto error;
|
2002-11-08 19:55:31 +01:00
|
|
|
#ifdef DBREG_DRX
|
|
|
|
/* needed for FreeBSD, the structure fields have changed under 5.x */
|
|
|
|
context->Dr0 = DBREG_DRX((&dbregs), 0);
|
|
|
|
context->Dr1 = DBREG_DRX((&dbregs), 1);
|
|
|
|
context->Dr2 = DBREG_DRX((&dbregs), 2);
|
|
|
|
context->Dr3 = DBREG_DRX((&dbregs), 3);
|
|
|
|
context->Dr6 = DBREG_DRX((&dbregs), 6);
|
|
|
|
context->Dr7 = DBREG_DRX((&dbregs), 7);
|
|
|
|
#else
|
2002-08-14 22:59:03 +02:00
|
|
|
context->Dr0 = dbregs.dr0;
|
|
|
|
context->Dr1 = dbregs.dr1;
|
|
|
|
context->Dr2 = dbregs.dr2;
|
|
|
|
context->Dr3 = dbregs.dr3;
|
|
|
|
context->Dr6 = dbregs.dr6;
|
|
|
|
context->Dr7 = dbregs.dr7;
|
2002-11-08 19:55:31 +01:00
|
|
|
#endif
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
|
2002-08-14 22:59:03 +02:00
|
|
|
#endif
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_FLOATING_POINT)
|
|
|
|
{
|
|
|
|
/* we can use context->FloatSave directly as it is using the */
|
|
|
|
/* correct structure (the same as fsave/frstor) */
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_GETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
|
2000-02-07 17:03:08 +01:00
|
|
|
context->FloatSave.Cr0NpxState = 0; /* FIXME */
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= CONTEXT_FLOATING_POINT;
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set a thread context */
|
2005-11-02 15:12:13 +01:00
|
|
|
static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
|
2000-02-07 17:03:08 +01:00
|
|
|
{
|
2003-03-11 02:48:53 +01:00
|
|
|
int pid = get_ptrace_pid(thread);
|
2000-02-07 17:03:08 +01:00
|
|
|
if (flags & CONTEXT_FULL)
|
|
|
|
{
|
|
|
|
struct reg regs;
|
2000-03-15 20:47:44 +01:00
|
|
|
if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
|
2000-02-07 17:03:08 +01:00
|
|
|
{
|
2000-03-15 20:47:44 +01:00
|
|
|
/* need to preserve some registers */
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_GETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error;
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_INTEGER)
|
|
|
|
{
|
|
|
|
regs.r_eax = context->Eax;
|
|
|
|
regs.r_ebx = context->Ebx;
|
|
|
|
regs.r_ecx = context->Ecx;
|
|
|
|
regs.r_edx = context->Edx;
|
|
|
|
regs.r_esi = context->Esi;
|
|
|
|
regs.r_edi = context->Edi;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_CONTROL)
|
|
|
|
{
|
|
|
|
regs.r_ebp = context->Ebp;
|
|
|
|
regs.r_esp = context->Esp;
|
|
|
|
regs.r_eip = context->Eip;
|
|
|
|
regs.r_cs = context->SegCs;
|
|
|
|
regs.r_ss = context->SegSs;
|
|
|
|
regs.r_eflags = context->EFlags;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_SEGMENTS)
|
|
|
|
{
|
|
|
|
regs.r_ds = context->SegDs;
|
|
|
|
regs.r_es = context->SegEs;
|
|
|
|
regs.r_fs = context->SegFs;
|
|
|
|
regs.r_gs = context->SegGs;
|
|
|
|
}
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_SETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error;
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_DEBUG_REGISTERS)
|
|
|
|
{
|
2002-08-14 22:59:03 +02:00
|
|
|
#ifdef PTRACE_SETDBREGS
|
|
|
|
struct dbreg dbregs;
|
2002-11-08 19:55:31 +01:00
|
|
|
#ifdef DBREG_DRX
|
|
|
|
/* needed for FreeBSD, the structure fields have changed under 5.x */
|
|
|
|
DBREG_DRX((&dbregs), 0) = context->Dr0;
|
|
|
|
DBREG_DRX((&dbregs), 1) = context->Dr1;
|
|
|
|
DBREG_DRX((&dbregs), 2) = context->Dr2;
|
|
|
|
DBREG_DRX((&dbregs), 3) = context->Dr3;
|
|
|
|
DBREG_DRX((&dbregs), 4) = 0;
|
|
|
|
DBREG_DRX((&dbregs), 5) = 0;
|
|
|
|
DBREG_DRX((&dbregs), 6) = context->Dr6;
|
|
|
|
DBREG_DRX((&dbregs), 7) = context->Dr7;
|
|
|
|
#else
|
2002-08-14 22:59:03 +02:00
|
|
|
dbregs.dr0 = context->Dr0;
|
|
|
|
dbregs.dr1 = context->Dr1;
|
|
|
|
dbregs.dr2 = context->Dr2;
|
|
|
|
dbregs.dr3 = context->Dr3;
|
|
|
|
dbregs.dr4 = 0;
|
|
|
|
dbregs.dr5 = 0;
|
|
|
|
dbregs.dr6 = context->Dr6;
|
|
|
|
dbregs.dr7 = context->Dr7;
|
2002-11-08 19:55:31 +01:00
|
|
|
#endif
|
2002-08-14 22:59:03 +02:00
|
|
|
if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
|
|
|
|
goto error;
|
|
|
|
#endif
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
if (flags & CONTEXT_FLOATING_POINT)
|
|
|
|
{
|
|
|
|
/* we can use context->FloatSave directly as it is using the */
|
|
|
|
/* correct structure (the same as fsave/frstor) */
|
2002-08-13 05:18:14 +02:00
|
|
|
if (ptrace( PTRACE_SETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* linux || __sun__ || __FreeBSD__ */
|
2005-11-02 15:12:13 +01:00
|
|
|
#error You must implement get/set_thread_context_ptrace for your platform
|
2000-02-07 17:03:08 +01:00
|
|
|
#endif /* linux || __sun__ || __FreeBSD__ */
|
2000-01-20 19:59:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* copy a context structure according to the flags */
|
2005-12-21 20:06:42 +01:00
|
|
|
static void copy_context( CONTEXT *to, const CONTEXT *from, unsigned int flags )
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
|
|
|
if (flags & CONTEXT_CONTROL)
|
|
|
|
{
|
|
|
|
to->Ebp = from->Ebp;
|
|
|
|
to->Eip = from->Eip;
|
|
|
|
to->Esp = from->Esp;
|
|
|
|
to->SegCs = from->SegCs;
|
|
|
|
to->SegSs = from->SegSs;
|
|
|
|
to->EFlags = from->EFlags;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_INTEGER)
|
|
|
|
{
|
|
|
|
to->Eax = from->Eax;
|
|
|
|
to->Ebx = from->Ebx;
|
|
|
|
to->Ecx = from->Ecx;
|
|
|
|
to->Edx = from->Edx;
|
|
|
|
to->Esi = from->Esi;
|
|
|
|
to->Edi = from->Edi;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_SEGMENTS)
|
|
|
|
{
|
|
|
|
to->SegDs = from->SegDs;
|
|
|
|
to->SegEs = from->SegEs;
|
|
|
|
to->SegFs = from->SegFs;
|
|
|
|
to->SegGs = from->SegGs;
|
|
|
|
}
|
|
|
|
if (flags & CONTEXT_FLOATING_POINT)
|
|
|
|
{
|
|
|
|
to->FloatSave = from->FloatSave;
|
|
|
|
}
|
2000-03-15 20:47:44 +01:00
|
|
|
/* we don't bother copying the debug registers, since they */
|
|
|
|
/* always need to be accessed by ptrace anyway */
|
2005-12-21 20:06:42 +01:00
|
|
|
to->ContextFlags |= flags & ~CONTEXT_DEBUG_REGISTERS;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
|
2000-03-30 21:30:24 +02:00
|
|
|
/* retrieve the current instruction pointer of a thread */
|
|
|
|
void *get_thread_ip( struct thread *thread )
|
|
|
|
{
|
|
|
|
CONTEXT context;
|
|
|
|
context.Eip = 0;
|
|
|
|
if (suspend_for_ptrace( thread ))
|
|
|
|
{
|
2005-11-02 15:12:13 +01:00
|
|
|
get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
|
2003-03-04 23:18:43 +01:00
|
|
|
resume_after_ptrace( thread );
|
2000-03-30 21:30:24 +02:00
|
|
|
}
|
|
|
|
return (void *)context.Eip;
|
|
|
|
}
|
|
|
|
|
2001-04-27 20:39:47 +02:00
|
|
|
/* determine if we should continue the thread in single-step mode */
|
|
|
|
int get_thread_single_step( struct thread *thread )
|
|
|
|
{
|
|
|
|
CONTEXT context;
|
|
|
|
if (thread->context) return 0; /* don't single-step inside exception event */
|
2005-11-02 15:12:13 +01:00
|
|
|
get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
|
2001-04-27 20:39:47 +02:00
|
|
|
return (context.EFlags & 0x100) != 0;
|
|
|
|
}
|
|
|
|
|
2003-03-22 22:00:09 +01:00
|
|
|
/* send a signal to a specific thread */
|
2006-02-14 12:24:32 +01:00
|
|
|
int tkill( int tgid, int pid, int sig )
|
2003-03-22 22:00:09 +01:00
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
int ret;
|
2006-02-14 12:24:32 +01:00
|
|
|
|
2003-03-22 22:00:09 +01:00
|
|
|
__asm__( "pushl %%ebx\n\t"
|
|
|
|
"movl %2,%%ebx\n\t"
|
|
|
|
"int $0x80\n\t"
|
|
|
|
"popl %%ebx\n\t"
|
|
|
|
: "=a" (ret)
|
2006-02-14 12:24:32 +01:00
|
|
|
: "0" (270) /*SYS_tgkill*/, "r" (tgid), "c" (pid), "d" (sig) );
|
|
|
|
if (ret == -ENOSYS)
|
|
|
|
__asm__( "pushl %%ebx\n\t"
|
|
|
|
"movl %2,%%ebx\n\t"
|
|
|
|
"int $0x80\n\t"
|
|
|
|
"popl %%ebx\n\t"
|
|
|
|
: "=a" (ret)
|
|
|
|
: "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
|
2003-05-07 05:22:22 +02:00
|
|
|
if (ret >= 0) return ret;
|
2003-03-22 22:00:09 +01:00
|
|
|
errno = -ret;
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-11-02 15:12:13 +01:00
|
|
|
/* retrieve the thread context */
|
|
|
|
void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2005-12-21 20:06:42 +01:00
|
|
|
context->ContextFlags |= CONTEXT_i386;
|
2005-11-02 15:12:13 +01:00
|
|
|
flags &= ~CONTEXT_i386; /* get rid of CPU id */
|
2000-01-20 19:59:03 +01:00
|
|
|
|
2005-11-02 15:12:13 +01:00
|
|
|
if (thread->context) /* thread is inside an exception event or suspended */
|
2000-08-31 03:59:51 +02:00
|
|
|
{
|
2005-11-02 15:12:13 +01:00
|
|
|
copy_context( context, thread->context, flags );
|
|
|
|
flags &= CONTEXT_DEBUG_REGISTERS;
|
2000-08-31 03:59:51 +02:00
|
|
|
}
|
2001-11-30 19:46:42 +01:00
|
|
|
|
2005-11-02 15:12:13 +01:00
|
|
|
if (flags && suspend_for_ptrace( thread ))
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2005-11-02 15:12:13 +01:00
|
|
|
get_thread_context_ptrace( thread, flags, context );
|
|
|
|
resume_after_ptrace( thread );
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-02 15:12:13 +01:00
|
|
|
/* set the thread context */
|
|
|
|
void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2005-11-02 15:12:13 +01:00
|
|
|
flags &= ~CONTEXT_i386; /* get rid of CPU id */
|
2000-01-20 19:59:03 +01:00
|
|
|
|
2005-11-02 15:12:13 +01:00
|
|
|
if (thread->context) /* thread is inside an exception event or suspended */
|
|
|
|
copy_context( thread->context, context, flags );
|
2006-01-11 20:20:32 +01:00
|
|
|
|
|
|
|
flags &= CONTEXT_DEBUG_REGISTERS; /* the other registers are handled on the client side */
|
2005-11-02 15:12:13 +01:00
|
|
|
|
|
|
|
if (flags && suspend_for_ptrace( thread ))
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2005-11-02 15:12:13 +01:00
|
|
|
set_thread_context_ptrace( thread, flags, context );
|
|
|
|
resume_after_ptrace( thread );
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __i386__ */
|