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
|
|
|
|
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
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
/* retrieve the thread x86 debug registers */
|
|
|
|
static int get_thread_debug_regs( struct thread *thread, 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
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
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;
|
|
|
|
return 1;
|
2000-01-20 19:59:03 +01:00
|
|
|
error:
|
|
|
|
file_set_error();
|
2006-04-20 17:46:16 +02:00
|
|
|
return 0;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
/* set the thread x86 debug registers */
|
|
|
|
static int set_thread_debug_regs( struct thread *thread, 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
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
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;
|
|
|
|
return 1;
|
2000-01-30 01:23:12 +01:00
|
|
|
error:
|
|
|
|
file_set_error();
|
2006-04-20 17:46:16 +02:00
|
|
|
return 0;
|
2000-01-30 01:23:12 +01:00
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
/* retrieve the thread x86 debug registers */
|
|
|
|
static int get_thread_debug_regs( struct thread *thread, CONTEXT *context )
|
2000-02-07 17:03:08 +01:00
|
|
|
{
|
2002-08-14 22:59:03 +02:00
|
|
|
#ifdef PTRACE_GETDBREGS
|
2006-04-20 17:46:16 +02:00
|
|
|
int pid = get_ptrace_pid(thread);
|
|
|
|
|
|
|
|
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
|
2006-04-20 17:46:16 +02:00
|
|
|
/* 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);
|
2002-11-08 19:55:31 +01:00
|
|
|
#else
|
2006-04-20 17:46:16 +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-08-14 22:59:03 +02:00
|
|
|
#endif
|
2006-04-20 17:46:16 +02:00
|
|
|
return 1;
|
2000-02-07 17:03:08 +01:00
|
|
|
error:
|
|
|
|
file_set_error();
|
2006-04-20 17:46:16 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
/* set the thread x86 debug registers */
|
|
|
|
static int set_thread_debug_regs( struct thread *thread, const CONTEXT *context )
|
2000-02-07 17:03:08 +01:00
|
|
|
{
|
2002-08-14 22:59:03 +02:00
|
|
|
#ifdef PTRACE_SETDBREGS
|
2006-04-20 17:46:16 +02:00
|
|
|
int pid = get_ptrace_pid(thread);
|
|
|
|
struct dbreg dbregs;
|
2002-11-08 19:55:31 +01:00
|
|
|
#ifdef DBREG_DRX
|
2006-04-20 17:46:16 +02:00
|
|
|
/* 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;
|
2002-11-08 19:55:31 +01:00
|
|
|
#else
|
2006-04-20 17:46:16 +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
|
2006-04-20 17:46:16 +02:00
|
|
|
if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) != -1) return 1;
|
2000-02-07 17:03:08 +01:00
|
|
|
file_set_error();
|
2006-04-20 17:46:16 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
2000-02-07 17:03:08 +01:00
|
|
|
}
|
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
#else /* linux || __FreeBSD__ */
|
|
|
|
|
|
|
|
/* retrieve the thread x86 debug registers */
|
|
|
|
static int get_thread_debug_regs( struct thread *thread, CONTEXT *context )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the thread x86 debug registers */
|
|
|
|
static int set_thread_debug_regs( struct thread *thread, const CONTEXT *context )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* linux || __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;
|
|
|
|
}
|
2006-04-20 17:46:16 +02:00
|
|
|
if (flags & CONTEXT_DEBUG_REGISTERS)
|
|
|
|
{
|
|
|
|
to->Dr0 = from->Dr0;
|
|
|
|
to->Dr1 = from->Dr1;
|
|
|
|
to->Dr2 = from->Dr2;
|
|
|
|
to->Dr3 = from->Dr3;
|
|
|
|
to->Dr6 = from->Dr6;
|
|
|
|
to->Dr7 = from->Dr7;
|
|
|
|
}
|
|
|
|
to->ContextFlags |= flags;
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
|
2006-04-19 19:45:39 +02:00
|
|
|
/* retrieve the current instruction pointer of a context */
|
|
|
|
void *get_context_ip( const CONTEXT *context )
|
2000-03-30 21:30:24 +02:00
|
|
|
{
|
2006-04-19 19:45:39 +02:00
|
|
|
return (void *)context->Eip;
|
2000-03-30 21:30:24 +02:00
|
|
|
}
|
|
|
|
|
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 */
|
2006-04-20 17:46:16 +02:00
|
|
|
copy_context( context, thread->context, flags & ~CONTEXT_DEBUG_REGISTERS );
|
2001-11-30 19:46:42 +01:00
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
if ((flags & CONTEXT_DEBUG_REGISTERS) && suspend_for_ptrace( thread ))
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2006-04-20 17:46:16 +02:00
|
|
|
if (get_thread_debug_regs( thread, context )) context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
|
2005-11-02 15:12:13 +01:00
|
|
|
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
|
|
|
|
2006-04-20 17:46:16 +02:00
|
|
|
if ((flags & CONTEXT_DEBUG_REGISTERS) && suspend_for_ptrace( thread ))
|
2000-01-20 19:59:03 +01:00
|
|
|
{
|
2006-04-20 17:46:16 +02:00
|
|
|
if (!set_thread_debug_regs( thread, context )) flags &= ~CONTEXT_DEBUG_REGISTERS;
|
2005-11-02 15:12:13 +01:00
|
|
|
resume_after_ptrace( thread );
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
2006-04-20 17:46:16 +02:00
|
|
|
|
|
|
|
if (thread->context) copy_context( thread->context, context, flags );
|
2000-01-20 19:59:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __i386__ */
|