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