Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * i386 register context support |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #ifdef __i386__ |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <errno.h> |
Ulrich Weigand | 8a1bdb3 | 2000-01-30 22:22:22 +0000 | [diff] [blame] | 27 | #ifdef HAVE_SYS_REG_H |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 28 | #include <sys/reg.h> |
Ulrich Weigand | 8a1bdb3 | 2000-01-30 22:22:22 +0000 | [diff] [blame] | 29 | #endif |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 30 | #include <stdarg.h> |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
Dimitrie O. Paun | 2af03e4 | 2000-11-29 20:04:09 +0000 | [diff] [blame] | 32 | #ifdef HAVE_SYS_PTRACE_H |
| 33 | # include <sys/ptrace.h> |
| 34 | #endif |
Patrik Stridvall | 49bcb93 | 2000-02-25 20:55:33 +0000 | [diff] [blame] | 35 | #ifdef HAVE_SYS_PARAM_H |
| 36 | # include <sys/param.h> |
| 37 | #endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 38 | |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 39 | #include "windef.h" |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 40 | |
| 41 | #include "file.h" |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 42 | #include "thread.h" |
| 43 | #include "request.h" |
| 44 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 45 | #ifndef PTRACE_PEEKUSER |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 46 | # 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 51 | #endif |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 52 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 53 | #ifndef PTRACE_POKEUSER |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 54 | # 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 59 | #endif |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 60 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 61 | #ifndef PTRACE_GETREGS |
| 62 | #define PTRACE_GETREGS PT_GETREGS |
| 63 | #endif |
| 64 | #ifndef PTRACE_GETFPREGS |
| 65 | #define PTRACE_GETFPREGS PT_GETFPREGS |
| 66 | #endif |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 67 | #ifndef PTRACE_SETREGS |
| 68 | #define PTRACE_SETREGS PT_SETREGS |
| 69 | #endif |
| 70 | #ifndef PTRACE_SETFPREGS |
| 71 | #define PTRACE_SETFPREGS PT_SETFPREGS |
| 72 | #endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 73 | |
Pierre Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 74 | #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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 82 | #ifdef linux |
Francois Gouget | 5c84fff | 2001-11-20 18:52:42 +0000 | [diff] [blame] | 83 | #ifdef HAVE_SYS_USER_H |
| 84 | # include <sys/user.h> |
| 85 | #endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 86 | |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 87 | /* user_regs definitions from asm/user.h */ |
| 88 | struct 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 99 | /* debug register offset in struct user */ |
| 100 | #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr))) |
| 101 | |
| 102 | /* retrieve a debug register */ |
| 103 | static inline int get_debug_reg( int pid, int num, DWORD *data ) |
| 104 | { |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 105 | int res; |
| 106 | errno = 0; |
| 107 | res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 ); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 108 | 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 Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 118 | static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context ) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 119 | { |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 120 | int pid = get_ptrace_pid(thread); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 121 | if (flags & CONTEXT_FULL) |
| 122 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 123 | struct kernel_user_regs_struct regs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 124 | if (ptrace( PTRACE_GETREGS, pid, 0, ®s ) == -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 Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 139 | context->SegCs = regs.cs; |
| 140 | context->SegSs = regs.ss; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 141 | context->EFlags = regs.eflags; |
| 142 | } |
| 143 | if (flags & CONTEXT_SEGMENTS) |
| 144 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 145 | context->SegDs = regs.ds; |
| 146 | context->SegEs = regs.es; |
| 147 | context->SegFs = regs.fs; |
| 148 | context->SegGs = regs.gs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 149 | } |
Alexandre Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 150 | context->ContextFlags |= flags & CONTEXT_FULL; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 151 | } |
| 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 Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 160 | context->ContextFlags |= CONTEXT_DEBUG_REGISTERS; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 161 | } |
| 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 Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 168 | context->ContextFlags |= CONTEXT_FLOATING_POINT; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 169 | } |
| 170 | return; |
| 171 | error: |
| 172 | file_set_error(); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* set a thread context */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 177 | static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context ) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 178 | { |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 179 | int pid = get_ptrace_pid(thread); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 180 | if (flags & CONTEXT_FULL) |
| 181 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 182 | struct kernel_user_regs_struct regs; |
Alexandre Julliard | 7614180 | 2001-07-11 17:30:59 +0000 | [diff] [blame] | 183 | |
| 184 | /* need to preserve some registers (at a minimum orig_eax must always be preserved) */ |
| 185 | if (ptrace( PTRACE_GETREGS, pid, 0, ®s ) == -1) goto error; |
| 186 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 187 | 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 Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 201 | regs.cs = context->SegCs; |
| 202 | regs.ss = context->SegSs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 203 | regs.eflags = context->EFlags; |
| 204 | } |
| 205 | if (flags & CONTEXT_SEGMENTS) |
| 206 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 207 | regs.ds = context->SegDs; |
| 208 | regs.es = context->SegEs; |
| 209 | regs.fs = context->SegFs; |
| 210 | regs.gs = context->SegGs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 211 | } |
| 212 | if (ptrace( PTRACE_SETREGS, pid, 0, ®s ) == -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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 228 | } |
| 229 | return; |
| 230 | error: |
| 231 | file_set_error(); |
| 232 | } |
| 233 | |
Patrik Stridvall | 0e06d62 | 2000-02-26 13:16:13 +0000 | [diff] [blame] | 234 | #elif defined(__sun) || defined(__sun__) |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 235 | |
| 236 | /* retrieve a thread context */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 237 | static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context ) |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 238 | { |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 239 | int pid = get_ptrace_pid(thread); |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 240 | if (flags & CONTEXT_FULL) |
| 241 | { |
| 242 | struct regs regs; |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 243 | if (ptrace( PTRACE_GETREGS, pid, (int) ®s, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 244 | 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 Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 269 | context->ContextFlags |= flags & CONTEXT_FULL; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 270 | } |
| 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 Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 279 | if (ptrace( PTRACE_GETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 280 | context->FloatSave.Cr0NpxState = 0; /* FIXME */ |
Alexandre Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 281 | context->ContextFlags |= CONTEXT_FLOATING_POINT; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 282 | } |
| 283 | return; |
| 284 | error: |
| 285 | file_set_error(); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /* set a thread context */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 290 | static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context ) |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 291 | { |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 292 | int pid = get_ptrace_pid(thread); |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 293 | if (flags & CONTEXT_FULL) |
| 294 | { |
| 295 | struct regs regs; |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 296 | if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL) |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 297 | { |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 298 | /* need to preserve some registers */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 299 | if (ptrace( PTRACE_GETREGS, pid, (int) ®s, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 300 | } |
| 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 Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 326 | if (ptrace( PTRACE_SETREGS, pid, (int) ®s, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 327 | } |
| 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 Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 336 | if (ptrace( PTRACE_SETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 337 | } |
| 338 | return; |
| 339 | error: |
| 340 | file_set_error(); |
| 341 | } |
| 342 | |
Robert Millan | 8800352 | 2006-02-05 13:06:50 +0100 | [diff] [blame] | 343 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__) |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 344 | #include <machine/reg.h> |
| 345 | |
| 346 | /* retrieve a thread context */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 347 | static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context ) |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 348 | { |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 349 | int pid = get_ptrace_pid(thread); |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 350 | if (flags & CONTEXT_FULL) |
| 351 | { |
| 352 | struct reg regs; |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 353 | if (ptrace( PTRACE_GETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 354 | 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 Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 379 | context->ContextFlags |= flags & CONTEXT_FULL; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 380 | } |
| 381 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 382 | { |
Pierre Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 383 | #ifdef PTRACE_GETDBREGS |
| 384 | struct dbreg dbregs; |
| 385 | if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) |
| 386 | goto error; |
Pierre Beyssac | 4c5c90e | 2002-11-08 18:55:31 +0000 | [diff] [blame] | 387 | #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 Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 396 | 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 Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 403 | context->ContextFlags |= CONTEXT_DEBUG_REGISTERS; |
Pierre Beyssac | 4c5c90e | 2002-11-08 18:55:31 +0000 | [diff] [blame] | 404 | #endif |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 405 | } |
| 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 Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 410 | if (ptrace( PTRACE_GETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 411 | context->FloatSave.Cr0NpxState = 0; /* FIXME */ |
Alexandre Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 412 | context->ContextFlags |= CONTEXT_FLOATING_POINT; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 413 | } |
| 414 | return; |
| 415 | error: |
| 416 | file_set_error(); |
| 417 | } |
| 418 | |
| 419 | |
| 420 | /* set a thread context */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 421 | static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context ) |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 422 | { |
Alexandre Julliard | baf0a06 | 2003-03-11 01:48:53 +0000 | [diff] [blame] | 423 | int pid = get_ptrace_pid(thread); |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 424 | if (flags & CONTEXT_FULL) |
| 425 | { |
| 426 | struct reg regs; |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 427 | if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL) |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 428 | { |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 429 | /* need to preserve some registers */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 430 | if (ptrace( PTRACE_GETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 431 | } |
| 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 Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 457 | if (ptrace( PTRACE_SETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 458 | } |
| 459 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 460 | { |
Pierre Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 461 | #ifdef PTRACE_SETDBREGS |
| 462 | struct dbreg dbregs; |
Pierre Beyssac | 4c5c90e | 2002-11-08 18:55:31 +0000 | [diff] [blame] | 463 | #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 Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 474 | 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 Beyssac | 4c5c90e | 2002-11-08 18:55:31 +0000 | [diff] [blame] | 482 | #endif |
Pierre Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 483 | if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) |
| 484 | goto error; |
| 485 | #endif |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 486 | } |
| 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 Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 491 | if (ptrace( PTRACE_SETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 492 | } |
| 493 | return; |
| 494 | error: |
| 495 | file_set_error(); |
| 496 | } |
| 497 | |
| 498 | #else /* linux || __sun__ || __FreeBSD__ */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 499 | #error You must implement get/set_thread_context_ptrace for your platform |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 500 | #endif /* linux || __sun__ || __FreeBSD__ */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 501 | |
| 502 | |
| 503 | /* copy a context structure according to the flags */ |
Alexandre Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 504 | static void copy_context( CONTEXT *to, const CONTEXT *from, unsigned int flags ) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 505 | { |
| 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 531 | if (flags & CONTEXT_FLOATING_POINT) |
| 532 | { |
| 533 | to->FloatSave = from->FloatSave; |
| 534 | } |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 535 | /* we don't bother copying the debug registers, since they */ |
| 536 | /* always need to be accessed by ptrace anyway */ |
Alexandre Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 537 | to->ContextFlags |= flags & ~CONTEXT_DEBUG_REGISTERS; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Alexandre Julliard | b73421d | 2000-03-30 19:30:24 +0000 | [diff] [blame] | 540 | /* retrieve the current instruction pointer of a thread */ |
| 541 | void *get_thread_ip( struct thread *thread ) |
| 542 | { |
| 543 | CONTEXT context; |
| 544 | context.Eip = 0; |
| 545 | if (suspend_for_ptrace( thread )) |
| 546 | { |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 547 | get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context ); |
Alexandre Julliard | d04ccb8 | 2003-03-04 22:18:43 +0000 | [diff] [blame] | 548 | resume_after_ptrace( thread ); |
Alexandre Julliard | b73421d | 2000-03-30 19:30:24 +0000 | [diff] [blame] | 549 | } |
| 550 | return (void *)context.Eip; |
| 551 | } |
| 552 | |
Ove Kaaven | ada7383 | 2001-04-27 18:39:47 +0000 | [diff] [blame] | 553 | /* determine if we should continue the thread in single-step mode */ |
| 554 | int 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 Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 558 | get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context ); |
Ove Kaaven | ada7383 | 2001-04-27 18:39:47 +0000 | [diff] [blame] | 559 | return (context.EFlags & 0x100) != 0; |
| 560 | } |
| 561 | |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 562 | /* send a signal to a specific thread */ |
Eric Pouech | 22357ca | 2006-02-14 12:24:32 +0100 | [diff] [blame] | 563 | int tkill( int tgid, int pid, int sig ) |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 564 | { |
| 565 | #ifdef __linux__ |
| 566 | int ret; |
Eric Pouech | 22357ca | 2006-02-14 12:24:32 +0100 | [diff] [blame] | 567 | |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 568 | __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 Pouech | 22357ca | 2006-02-14 12:24:32 +0100 | [diff] [blame] | 573 | : "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 Julliard | 22824cb | 2003-05-07 03:22:22 +0000 | [diff] [blame] | 581 | if (ret >= 0) return ret; |
Alexandre Julliard | a8497bd | 2003-03-22 21:00:09 +0000 | [diff] [blame] | 582 | errno = -ret; |
| 583 | return -1; |
| 584 | #else |
| 585 | errno = ENOSYS; |
| 586 | return -1; |
| 587 | #endif |
| 588 | } |
| 589 | |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 590 | /* retrieve the thread context */ |
| 591 | void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags ) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 592 | { |
Alexandre Julliard | bd298b5 | 2005-12-21 20:06:42 +0100 | [diff] [blame] | 593 | context->ContextFlags |= CONTEXT_i386; |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 594 | flags &= ~CONTEXT_i386; /* get rid of CPU id */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 595 | |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 596 | if (thread->context) /* thread is inside an exception event or suspended */ |
Alexandre Julliard | 9264300 | 2000-08-31 01:59:51 +0000 | [diff] [blame] | 597 | { |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 598 | copy_context( context, thread->context, flags ); |
| 599 | flags &= CONTEXT_DEBUG_REGISTERS; |
Alexandre Julliard | 9264300 | 2000-08-31 01:59:51 +0000 | [diff] [blame] | 600 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 601 | |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 602 | if (flags && suspend_for_ptrace( thread )) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 603 | { |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 604 | get_thread_context_ptrace( thread, flags, context ); |
| 605 | resume_after_ptrace( thread ); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 609 | /* set the thread context */ |
| 610 | void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags ) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 611 | { |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 612 | flags &= ~CONTEXT_i386; /* get rid of CPU id */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 613 | |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 614 | if (thread->context) /* thread is inside an exception event or suspended */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 615 | copy_context( thread->context, context, flags ); |
Alexandre Julliard | 2654be0 | 2006-01-11 20:20:32 +0100 | [diff] [blame] | 616 | |
| 617 | flags &= CONTEXT_DEBUG_REGISTERS; /* the other registers are handled on the client side */ |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 618 | |
| 619 | if (flags && suspend_for_ptrace( thread )) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 620 | { |
Alexandre Julliard | 3f85e26 | 2005-11-02 14:12:13 +0000 | [diff] [blame] | 621 | set_thread_context_ptrace( thread, flags, context ); |
| 622 | resume_after_ptrace( thread ); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | #endif /* __i386__ */ |