Home | History | Annotate | Download | only in coregrind
      1 
      2 /*--------------------------------------------------------------------*/
      3 /*--- Attaching a debugger.                           m_debugger.c ---*/
      4 /*--------------------------------------------------------------------*/
      5 
      6 /*
      7    This file is part of Valgrind, a dynamic binary instrumentation
      8    framework.
      9 
     10    Copyright (C) 2000-2013 Julian Seward
     11       jseward (at) acm.org
     12 
     13    This program is free software; you can redistribute it and/or
     14    modify it under the terms of the GNU General Public License as
     15    published by the Free Software Foundation; either version 2 of the
     16    License, or (at your option) any later version.
     17 
     18    This program is distributed in the hope that it will be useful, but
     19    WITHOUT ANY WARRANTY; without even the implied warranty of
     20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     21    General Public License for more details.
     22 
     23    You should have received a copy of the GNU General Public License
     24    along with this program; if not, write to the Free Software
     25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     26    02111-1307, USA.
     27 
     28    The GNU General Public License is contained in the file COPYING.
     29 */
     30 
     31 #include "pub_core_basics.h"
     32 #include "pub_core_vki.h"
     33 #include "pub_core_libcsetjmp.h"
     34 #include "pub_core_threadstate.h"
     35 #include "pub_core_xarray.h"
     36 #include "pub_core_clientstate.h"
     37 #include "pub_core_debugger.h"
     38 #include "pub_core_gdbserver.h"
     39 #include "pub_core_libcbase.h"
     40 #include "pub_core_libcprint.h"
     41 #include "pub_core_libcproc.h"
     42 #include "pub_core_libcsignal.h"
     43 #include "pub_core_libcassert.h"
     44 #include "pub_core_options.h"
     45 
     46 
     47 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
     48 #define WSTOPSIG(status) (((status) & 0xff00) >> 8)
     49 
     50 static Int ptrace_setregs(Int pid, VexGuestArchState* vex)
     51 {
     52 #if defined(VGP_x86_linux)
     53    struct vki_user_regs_struct regs;
     54    VG_(memset)(&regs, 0, sizeof(regs));
     55    regs.cs     = vex->guest_CS;
     56    regs.ss     = vex->guest_SS;
     57    regs.ds     = vex->guest_DS;
     58    regs.es     = vex->guest_ES;
     59    regs.fs     = vex->guest_FS;
     60    regs.gs     = vex->guest_GS;
     61    regs.eax    = vex->guest_EAX;
     62    regs.ebx    = vex->guest_EBX;
     63    regs.ecx    = vex->guest_ECX;
     64    regs.edx    = vex->guest_EDX;
     65    regs.esi    = vex->guest_ESI;
     66    regs.edi    = vex->guest_EDI;
     67    regs.ebp    = vex->guest_EBP;
     68    regs.esp    = vex->guest_ESP;
     69    regs.eflags = LibVEX_GuestX86_get_eflags(vex);
     70    regs.eip    = vex->guest_EIP;
     71    return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &regs);
     72 
     73 #elif defined(VGP_amd64_linux)
     74    struct vki_user_regs_struct regs;
     75    VG_(memset)(&regs, 0, sizeof(regs));
     76    regs.rax    = vex->guest_RAX;
     77    regs.rbx    = vex->guest_RBX;
     78    regs.rcx    = vex->guest_RCX;
     79    regs.rdx    = vex->guest_RDX;
     80    regs.rsi    = vex->guest_RSI;
     81    regs.rdi    = vex->guest_RDI;
     82    regs.rbp    = vex->guest_RBP;
     83    regs.rsp    = vex->guest_RSP;
     84    regs.r8     = vex->guest_R8;
     85    regs.r9     = vex->guest_R9;
     86    regs.r10    = vex->guest_R10;
     87    regs.r11    = vex->guest_R11;
     88    regs.r12    = vex->guest_R12;
     89    regs.r13    = vex->guest_R13;
     90    regs.r14    = vex->guest_R14;
     91    regs.r15    = vex->guest_R15;
     92    regs.eflags = LibVEX_GuestAMD64_get_rflags(vex);
     93    regs.rip    = vex->guest_RIP;
     94    /* Set %{c,d,e,f,s,g}s and %{fs,gs}_base (whatever those are) to
     95       values which don't fail the kernel's sanity checks.  I have no
     96       idea what these should really be set to.  Anyway, mostly it
     97       seems that zero is an allowable value, except for %cs and %ss
     98       which have to have their lowest 2 bits be 11.  See putreg() in
     99       linux-2.6.23/arch/x86_64/kernel/ptrace.c for the apparently
    100       relevant sanity checks.  This fixes #145622. */
    101    regs.cs      = 3;
    102    regs.ds      = 0;
    103    regs.es      = 0;
    104    regs.fs      = 0;
    105    regs.ss      = 3;
    106    regs.gs      = 0;
    107    regs.fs_base = 0;
    108    regs.gs_base = 0;
    109    return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &regs);
    110 
    111 #elif defined(VGP_ppc32_linux)
    112    Int rc = 0;
    113    /* apparently the casting to void* is the Right Thing To Do */
    114    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R0  * 4), (void*)vex->guest_GPR0);
    115    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R1  * 4), (void*)vex->guest_GPR1);
    116    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R2  * 4), (void*)vex->guest_GPR2);
    117    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R3  * 4), (void*)vex->guest_GPR3);
    118    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R4  * 4), (void*)vex->guest_GPR4);
    119    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R5  * 4), (void*)vex->guest_GPR5);
    120    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R6  * 4), (void*)vex->guest_GPR6);
    121    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R7  * 4), (void*)vex->guest_GPR7);
    122    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R8  * 4), (void*)vex->guest_GPR8);
    123    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R9  * 4), (void*)vex->guest_GPR9);
    124    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R10 * 4), (void*)vex->guest_GPR10);
    125    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R11 * 4), (void*)vex->guest_GPR11);
    126    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R12 * 4), (void*)vex->guest_GPR12);
    127    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R13 * 4), (void*)vex->guest_GPR13);
    128    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R14 * 4), (void*)vex->guest_GPR14);
    129    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R15 * 4), (void*)vex->guest_GPR15);
    130    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R16 * 4), (void*)vex->guest_GPR16);
    131    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R17 * 4), (void*)vex->guest_GPR17);
    132    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R18 * 4), (void*)vex->guest_GPR18);
    133    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R19 * 4), (void*)vex->guest_GPR19);
    134    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R20 * 4), (void*)vex->guest_GPR20);
    135    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R21 * 4), (void*)vex->guest_GPR21);
    136    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R22 * 4), (void*)vex->guest_GPR22);
    137    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R23 * 4), (void*)vex->guest_GPR23);
    138    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R24 * 4), (void*)vex->guest_GPR24);
    139    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R25 * 4), (void*)vex->guest_GPR25);
    140    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R26 * 4), (void*)vex->guest_GPR26);
    141    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R27 * 4), (void*)vex->guest_GPR27);
    142    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R28 * 4), (void*)vex->guest_GPR28);
    143    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R29 * 4), (void*)vex->guest_GPR29);
    144    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R30 * 4), (void*)vex->guest_GPR30);
    145    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R31 * 4), (void*)vex->guest_GPR31);
    146    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_NIP * 4), (void*)vex->guest_CIA);
    147    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CCR * 4),
    148                      (void*)LibVEX_GuestPPC32_get_CR(vex));
    149    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_LNK * 4), (void*)vex->guest_LR);
    150    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CTR * 4), (void*)vex->guest_CTR);
    151    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_XER * 4),
    152                      (void*)LibVEX_GuestPPC32_get_XER(vex));
    153    return rc;
    154 
    155 #elif defined(VGP_ppc64_linux)
    156    Int rc = 0;
    157    /* FRJ: copied nearly verbatim from the ppc32 case. I compared the
    158       vki-ppc64-linux.h with its ppc32 counterpart and saw no
    159       appreciable differences, other than the registers being 8 bytes
    160       instead of 4. No idea why we don't set all of the entries
    161       declared in vki_pt_regs, but ppc32 doesn't so there must be a
    162       reason.
    163 
    164       Finally, note that CR and XER are 32 bits even for ppc64 (see
    165       libvex_guest_ppc64.h), but the vki_pt_regs struct still gives
    166       them 64 bits.
    167    */
    168    /* apparently the casting to void* is the Right Thing To Do */
    169    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R0  * 8), (void*)vex->guest_GPR0);
    170    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R1  * 8), (void*)vex->guest_GPR1);
    171    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R2  * 8), (void*)vex->guest_GPR2);
    172    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R3  * 8), (void*)vex->guest_GPR3);
    173    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R4  * 8), (void*)vex->guest_GPR4);
    174    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R5  * 8), (void*)vex->guest_GPR5);
    175    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R6  * 8), (void*)vex->guest_GPR6);
    176    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R7  * 8), (void*)vex->guest_GPR7);
    177    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R8  * 8), (void*)vex->guest_GPR8);
    178    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R9  * 8), (void*)vex->guest_GPR9);
    179    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R10 * 8), (void*)vex->guest_GPR10);
    180    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R11 * 8), (void*)vex->guest_GPR11);
    181    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R12 * 8), (void*)vex->guest_GPR12);
    182    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R13 * 8), (void*)vex->guest_GPR13);
    183    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R14 * 8), (void*)vex->guest_GPR14);
    184    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R15 * 8), (void*)vex->guest_GPR15);
    185    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R16 * 8), (void*)vex->guest_GPR16);
    186    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R17 * 8), (void*)vex->guest_GPR17);
    187    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R18 * 8), (void*)vex->guest_GPR18);
    188    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R19 * 8), (void*)vex->guest_GPR19);
    189    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R20 * 8), (void*)vex->guest_GPR20);
    190    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R21 * 8), (void*)vex->guest_GPR21);
    191    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R22 * 8), (void*)vex->guest_GPR22);
    192    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R23 * 8), (void*)vex->guest_GPR23);
    193    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R24 * 8), (void*)vex->guest_GPR24);
    194    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R25 * 8), (void*)vex->guest_GPR25);
    195    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R26 * 8), (void*)vex->guest_GPR26);
    196    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R27 * 8), (void*)vex->guest_GPR27);
    197    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R28 * 8), (void*)vex->guest_GPR28);
    198    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R29 * 8), (void*)vex->guest_GPR29);
    199    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R30 * 8), (void*)vex->guest_GPR30);
    200    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R31 * 8), (void*)vex->guest_GPR31);
    201    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_NIP * 8), (void*)vex->guest_CIA);
    202    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CCR * 8),
    203                                               (void*)(long)LibVEX_GuestPPC64_get_CR(vex));
    204    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_LNK * 8), (void*)vex->guest_LR);
    205    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CTR * 8), (void*)vex->guest_CTR);
    206    rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_XER * 8),
    207                                               (void*)(long)LibVEX_GuestPPC64_get_XER(vex));
    208    return rc;
    209 
    210 #elif defined(VGP_arm_linux)
    211    struct vki_user_regs_struct uregs;
    212    VG_(memset)(&uregs, 0, sizeof(uregs));
    213    uregs.ARM_r0   = vex->guest_R0;
    214    uregs.ARM_r1   = vex->guest_R1;
    215    uregs.ARM_r2   = vex->guest_R2;
    216    uregs.ARM_r3   = vex->guest_R3;
    217    uregs.ARM_r4   = vex->guest_R4;
    218    uregs.ARM_r5   = vex->guest_R5;
    219    uregs.ARM_r6   = vex->guest_R6;
    220    uregs.ARM_r7   = vex->guest_R7;
    221    uregs.ARM_r8   = vex->guest_R8;
    222    uregs.ARM_r9   = vex->guest_R9;
    223    uregs.ARM_r10  = vex->guest_R10;
    224    uregs.ARM_fp   = vex->guest_R11;
    225    uregs.ARM_ip   = vex->guest_R12;
    226    uregs.ARM_sp   = vex->guest_R13;
    227    uregs.ARM_lr   = vex->guest_R14;
    228    // Remove the T bit from the bottom of R15T.  It will get shipped
    229    // over in CPSR.T instead, since LibVEX_GuestARM_get_cpsr copies
    230    // it from R15T[0].
    231    uregs.ARM_pc   = vex->guest_R15T & 0xFFFFFFFE;
    232    uregs.ARM_cpsr = LibVEX_GuestARM_get_cpsr(vex);
    233    return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &uregs);
    234 
    235 #elif defined(VGP_arm64_linux)
    236    I_die_here;
    237    //ATC
    238    struct vki_user_pt_regs uregs;
    239    VG_(memset)(&uregs, 0, sizeof(uregs));
    240    uregs.regs[0]  = vex->guest_X0;
    241    uregs.regs[1]  = vex->guest_X1;
    242    uregs.regs[2]  = vex->guest_X2;
    243    uregs.regs[3]  = vex->guest_X3;
    244    uregs.regs[4]  = vex->guest_X4;
    245    uregs.regs[5]  = vex->guest_X5;
    246    uregs.regs[6]  = vex->guest_X6;
    247    uregs.regs[7]  = vex->guest_X7;
    248    uregs.regs[8]  = vex->guest_X8;
    249    uregs.regs[9]  = vex->guest_X9;
    250    uregs.regs[10] = vex->guest_X10;
    251    uregs.regs[11] = vex->guest_X11;
    252    uregs.regs[12] = vex->guest_X12;
    253    uregs.regs[13] = vex->guest_X13;
    254    uregs.regs[14] = vex->guest_X14;
    255    uregs.regs[15] = vex->guest_X15;
    256    uregs.regs[16] = vex->guest_X16;
    257    uregs.regs[17] = vex->guest_X17;
    258    uregs.regs[18] = vex->guest_X18;
    259    uregs.regs[19] = vex->guest_X19;
    260    uregs.regs[20] = vex->guest_X20;
    261    uregs.regs[21] = vex->guest_X21;
    262    uregs.regs[22] = vex->guest_X22;
    263    uregs.regs[23] = vex->guest_X23;
    264    uregs.regs[24] = vex->guest_X24;
    265    uregs.regs[25] = vex->guest_X25;
    266    uregs.regs[26] = vex->guest_X26;
    267    uregs.regs[27] = vex->guest_X27;
    268    uregs.regs[28] = vex->guest_X28;
    269    uregs.regs[29] = vex->guest_X29;
    270    uregs.regs[30] = vex->guest_X30;
    271    uregs.sp       = vex->guest_XSP;
    272    uregs.pc       = vex->guest_PC;
    273    uregs.pstate   = LibVEX_GuestARM64_get_nzcv(vex); /* is this correct? */
    274    return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &uregs);
    275 
    276 #elif defined(VGP_x86_darwin)
    277    I_die_here;
    278 
    279 #elif defined(VGP_amd64_darwin)
    280    I_die_here;
    281 
    282 #elif defined(VGP_s390x_linux)
    283    struct vki_user_regs_struct regs;
    284    vki_ptrace_area pa;
    285 
    286    /* We don't set the psw mask and start at offset 8 */
    287    pa.vki_len = (unsigned long) &regs.per_info - (unsigned long) &regs.psw.addr;
    288    pa.vki_process_addr = (unsigned long) &regs.psw.addr;
    289    pa.vki_kernel_addr = 8;
    290 
    291    VG_(memset)(&regs, 0, sizeof(regs));
    292    regs.psw.addr = vex->guest_IA;
    293 
    294    /* We don't set the mask */
    295    regs.gprs[0] = vex->guest_r0;
    296    regs.gprs[1] = vex->guest_r1;
    297    regs.gprs[2] = vex->guest_r2;
    298    regs.gprs[3] = vex->guest_r3;
    299    regs.gprs[4] = vex->guest_r4;
    300    regs.gprs[5] = vex->guest_r5;
    301    regs.gprs[6] = vex->guest_r6;
    302    regs.gprs[7] = vex->guest_r7;
    303    regs.gprs[8] = vex->guest_r8;
    304    regs.gprs[9] = vex->guest_r9;
    305    regs.gprs[10] = vex->guest_r10;
    306    regs.gprs[11] = vex->guest_r11;
    307    regs.gprs[12] = vex->guest_r12;
    308    regs.gprs[13] = vex->guest_r13;
    309    regs.gprs[14] = vex->guest_r14;
    310    regs.gprs[15] = vex->guest_r15;
    311 
    312    regs.acrs[0] = vex->guest_a0;
    313    regs.acrs[1] = vex->guest_a1;
    314    regs.acrs[2] = vex->guest_a2;
    315    regs.acrs[3] = vex->guest_a3;
    316    regs.acrs[4] = vex->guest_a4;
    317    regs.acrs[5] = vex->guest_a5;
    318    regs.acrs[6] = vex->guest_a6;
    319    regs.acrs[7] = vex->guest_a7;
    320    regs.acrs[8] = vex->guest_a8;
    321    regs.acrs[9] = vex->guest_a9;
    322    regs.acrs[10] = vex->guest_a10;
    323    regs.acrs[11] = vex->guest_a11;
    324    regs.acrs[12] = vex->guest_a12;
    325    regs.acrs[13] = vex->guest_a13;
    326    regs.acrs[14] = vex->guest_a14;
    327    regs.acrs[15] = vex->guest_a15;
    328 
    329    /* only used for system call restart and friends, just use r2 */
    330    regs.orig_gpr2 = vex->guest_r2;
    331 
    332    regs.fp_regs.fprs[0].ui = vex->guest_f0;
    333    regs.fp_regs.fprs[1].ui = vex->guest_f1;
    334    regs.fp_regs.fprs[2].ui = vex->guest_f2;
    335    regs.fp_regs.fprs[3].ui = vex->guest_f3;
    336    regs.fp_regs.fprs[4].ui = vex->guest_f4;
    337    regs.fp_regs.fprs[5].ui = vex->guest_f5;
    338    regs.fp_regs.fprs[6].ui = vex->guest_f6;
    339    regs.fp_regs.fprs[7].ui = vex->guest_f7;
    340    regs.fp_regs.fprs[8].ui = vex->guest_f8;
    341    regs.fp_regs.fprs[9].ui = vex->guest_f9;
    342    regs.fp_regs.fprs[10].ui = vex->guest_f10;
    343    regs.fp_regs.fprs[11].ui = vex->guest_f11;
    344    regs.fp_regs.fprs[12].ui = vex->guest_f12;
    345    regs.fp_regs.fprs[13].ui = vex->guest_f13;
    346    regs.fp_regs.fprs[14].ui = vex->guest_f14;
    347    regs.fp_regs.fprs[15].ui = vex->guest_f15;
    348    regs.fp_regs.fpc = vex->guest_fpc;
    349 
    350    return VG_(ptrace)(VKI_PTRACE_POKEUSR_AREA, pid,  &pa, NULL);
    351 
    352 #elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
    353    struct vki_user_regs_struct regs;
    354    VG_(memset)(&regs, 0, sizeof(regs));
    355    regs.MIPS_r0     = vex->guest_r0;
    356    regs.MIPS_r1     = vex->guest_r1;
    357    regs.MIPS_r2     = vex->guest_r2;
    358    regs.MIPS_r3     = vex->guest_r3;
    359    regs.MIPS_r4     = vex->guest_r4;
    360    regs.MIPS_r5     = vex->guest_r5;
    361    regs.MIPS_r6     = vex->guest_r6;
    362    regs.MIPS_r7     = vex->guest_r7;
    363    regs.MIPS_r8     = vex->guest_r8;
    364    regs.MIPS_r9     = vex->guest_r9;
    365    regs.MIPS_r10     = vex->guest_r10;
    366    regs.MIPS_r11     = vex->guest_r11;
    367    regs.MIPS_r12     = vex->guest_r12;
    368    regs.MIPS_r13     = vex->guest_r13;
    369    regs.MIPS_r14     = vex->guest_r14;
    370    regs.MIPS_r15     = vex->guest_r15;
    371    regs.MIPS_r16     = vex->guest_r16;
    372    regs.MIPS_r17     = vex->guest_r17;
    373    regs.MIPS_r18     = vex->guest_r18;
    374    regs.MIPS_r19     = vex->guest_r19;
    375    regs.MIPS_r20     = vex->guest_r20;
    376    regs.MIPS_r21     = vex->guest_r21;
    377    regs.MIPS_r22     = vex->guest_r22;
    378    regs.MIPS_r23     = vex->guest_r23;
    379    regs.MIPS_r24     = vex->guest_r24;
    380    regs.MIPS_r25     = vex->guest_r25;
    381    regs.MIPS_r26     = vex->guest_r26;
    382    regs.MIPS_r27     = vex->guest_r27;
    383    regs.MIPS_r28     = vex->guest_r28;
    384    regs.MIPS_r29     = vex->guest_r29;
    385    regs.MIPS_r30     = vex->guest_r30;
    386    regs.MIPS_r31     = vex->guest_r31;
    387    return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &regs);
    388 
    389 #else
    390 #  error Unknown arch
    391 #endif
    392 }
    393 
    394 /* Start debugger and get it to attach to this process.  Called if the
    395    user requests this service after an error has been shown, so she can
    396    poke around and look at parameters, memory, etc.  You can't
    397    meaningfully get the debugger to continue the program, though; to
    398    continue, quit the debugger.  */
    399 void VG_(start_debugger) ( ThreadId tid )
    400 {
    401 #  define N_BUF 4096
    402    Int pid, rc;
    403 
    404    pid = VG_(fork)();
    405 
    406    if (pid == 0) {
    407       /* child */
    408       VG_(set_ptracer)();
    409       rc = VG_(ptrace)(VKI_PTRACE_TRACEME, 0, NULL, NULL);
    410       vg_assert(rc == 0);
    411       rc = VG_(kill)(VG_(getpid)(), VKI_SIGSTOP);
    412       vg_assert(rc == 0);
    413 
    414    } else if (pid > 0) {
    415       /* parent */
    416       Int status;
    417       Int res;
    418 
    419       if ((res = VG_(waitpid)(pid, &status, 0)) == pid &&
    420           WIFSTOPPED(status) && WSTOPSIG(status) == VKI_SIGSTOP &&
    421           ptrace_setregs(pid, &(VG_(threads)[tid].arch.vex)) == 0 &&
    422           VG_(kill)(pid, VKI_SIGSTOP) == 0 &&
    423           VG_(ptrace)(VKI_PTRACE_DETACH, pid, NULL, 0) == 0)
    424       {
    425          HChar pidbuf[15];
    426          HChar file[50];
    427          HChar buf[N_BUF];
    428          HChar *bufptr;
    429          const HChar *cmdptr;
    430 
    431          VG_(sprintf)(pidbuf, "%d", pid);
    432          VG_(sprintf)(file, "/proc/%d/fd/%d", pid, VG_(cl_exec_fd));
    433 
    434          bufptr = buf;
    435          cmdptr = VG_(clo_db_command);
    436 
    437          while (*cmdptr) {
    438             /* each iteration can advance bufptr by at most the length
    439                of file[], so the following assertion is generously
    440                over-paranoid. */
    441             vg_assert(bufptr - buf < N_BUF-15-50-10/*paranoia*/);
    442             switch (*cmdptr) {
    443                case '%':
    444                   switch (*++cmdptr) {
    445                      case 'f':
    446                         VG_(memcpy)(bufptr, file, VG_(strlen)(file));
    447                         bufptr += VG_(strlen)(file);
    448                         cmdptr++;
    449                         break;
    450                      case 'p':
    451                         VG_(memcpy)(bufptr, pidbuf, VG_(strlen)(pidbuf));
    452                         bufptr += VG_(strlen)(pidbuf);
    453                         cmdptr++;
    454                         break;
    455                      default:
    456                         *bufptr++ = *cmdptr++;
    457                         break;
    458                   }
    459                   break;
    460                default:
    461                   *bufptr++ = *cmdptr++;
    462                   break;
    463             }
    464             vg_assert(bufptr - buf < N_BUF-15-50-10/*paranoia*/);
    465          }
    466 
    467          *bufptr++ = '\0';
    468 
    469          VG_(message)(Vg_UserMsg, "starting debugger with cmd: %s\n", buf);
    470          res = VG_(system)(buf);
    471          if (res == 0) {
    472             VG_(message)(Vg_UserMsg, "\n");
    473             VG_(message)(Vg_UserMsg,
    474                          "Debugger has detached.  Valgrind regains control."
    475                          "  We continue.\n");
    476          } else {
    477             VG_(message)(Vg_UserMsg,
    478                          "Warning: Debugger attach failed! (sys_system)\n");
    479             VG_(message)(Vg_UserMsg, "\n");
    480          }
    481       } else {
    482          VG_(message)(Vg_UserMsg,
    483                       "Warning: Debugger attach failed! (ptrace problem?)\n");
    484          VG_(message)(Vg_UserMsg, "\n");
    485       }
    486 
    487       VG_(kill)(pid, VKI_SIGKILL);
    488       VG_(waitpid)(pid, &status, 0);
    489    }
    490 #  undef N_BUF
    491 }
    492 
    493 
    494 
    495 /*--------------------------------------------------------------------*/
    496 /*--- end                                                          ---*/
    497 /*--------------------------------------------------------------------*/
    498