2 /*--------------------------------------------------------------------*/
3 /*--- Attaching a debugger. m_debugger.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2010 Julian Seward
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
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_libcbase.h"
38 #include "pub_core_libcprint.h"
39 #include "pub_core_libcproc.h"
40 #include "pub_core_libcsignal.h"
41 #include "pub_core_libcassert.h"
42 #include "pub_core_options.h"
45 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
46 #define WSTOPSIG(status) (((status) & 0xff00) >> 8)
48 static Int ptrace_setregs(Int pid, VexGuestArchState* vex)
50 #if defined(VGP_x86_linux)
51 struct vki_user_regs_struct regs;
52 VG_(memset)(®s, 0, sizeof(regs));
53 regs.cs = vex->guest_CS;
54 regs.ss = vex->guest_SS;
55 regs.ds = vex->guest_DS;
56 regs.es = vex->guest_ES;
57 regs.fs = vex->guest_FS;
58 regs.gs = vex->guest_GS;
59 regs.eax = vex->guest_EAX;
60 regs.ebx = vex->guest_EBX;
61 regs.ecx = vex->guest_ECX;
62 regs.edx = vex->guest_EDX;
63 regs.esi = vex->guest_ESI;
64 regs.edi = vex->guest_EDI;
65 regs.ebp = vex->guest_EBP;
66 regs.esp = vex->guest_ESP;
67 regs.eflags = LibVEX_GuestX86_get_eflags(vex);
68 regs.eip = vex->guest_EIP;
69 return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, ®s);
71 #elif defined(VGP_amd64_linux)
72 struct vki_user_regs_struct regs;
73 VG_(memset)(®s, 0, sizeof(regs));
74 regs.rax = vex->guest_RAX;
75 regs.rbx = vex->guest_RBX;
76 regs.rcx = vex->guest_RCX;
77 regs.rdx = vex->guest_RDX;
78 regs.rsi = vex->guest_RSI;
79 regs.rdi = vex->guest_RDI;
80 regs.rbp = vex->guest_RBP;
81 regs.rsp = vex->guest_RSP;
82 regs.r8 = vex->guest_R8;
83 regs.r9 = vex->guest_R9;
84 regs.r10 = vex->guest_R10;
85 regs.r11 = vex->guest_R11;
86 regs.r12 = vex->guest_R12;
87 regs.r13 = vex->guest_R13;
88 regs.r14 = vex->guest_R14;
89 regs.r15 = vex->guest_R15;
90 regs.eflags = LibVEX_GuestAMD64_get_rflags(vex);
91 regs.rip = vex->guest_RIP;
92 /* Set %{c,d,e,f,s,g}s and %{fs,gs}_base (whatever those are) to
93 values which don't fail the kernel's sanity checks. I have no
94 idea what these should really be set to. Anyway, mostly it
95 seems that zero is an allowable value, except for %cs and %ss
96 which have to have their lowest 2 bits be 11. See putreg() in
97 linux-2.6.23/arch/x86_64/kernel/ptrace.c for the apparently
98 relevant sanity checks. This fixes #145622. */
107 return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, ®s);
109 #elif defined(VGP_ppc32_linux)
111 /* apparently the casting to void* is the Right Thing To Do */
112 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R0 * 4), (void*)vex->guest_GPR0);
113 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R1 * 4), (void*)vex->guest_GPR1);
114 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R2 * 4), (void*)vex->guest_GPR2);
115 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R3 * 4), (void*)vex->guest_GPR3);
116 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R4 * 4), (void*)vex->guest_GPR4);
117 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R5 * 4), (void*)vex->guest_GPR5);
118 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R6 * 4), (void*)vex->guest_GPR6);
119 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R7 * 4), (void*)vex->guest_GPR7);
120 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R8 * 4), (void*)vex->guest_GPR8);
121 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R9 * 4), (void*)vex->guest_GPR9);
122 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R10 * 4), (void*)vex->guest_GPR10);
123 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R11 * 4), (void*)vex->guest_GPR11);
124 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R12 * 4), (void*)vex->guest_GPR12);
125 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R13 * 4), (void*)vex->guest_GPR13);
126 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R14 * 4), (void*)vex->guest_GPR14);
127 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R15 * 4), (void*)vex->guest_GPR15);
128 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R16 * 4), (void*)vex->guest_GPR16);
129 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R17 * 4), (void*)vex->guest_GPR17);
130 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R18 * 4), (void*)vex->guest_GPR18);
131 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R19 * 4), (void*)vex->guest_GPR19);
132 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R20 * 4), (void*)vex->guest_GPR20);
133 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R21 * 4), (void*)vex->guest_GPR21);
134 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R22 * 4), (void*)vex->guest_GPR22);
135 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R23 * 4), (void*)vex->guest_GPR23);
136 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R24 * 4), (void*)vex->guest_GPR24);
137 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R25 * 4), (void*)vex->guest_GPR25);
138 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R26 * 4), (void*)vex->guest_GPR26);
139 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R27 * 4), (void*)vex->guest_GPR27);
140 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R28 * 4), (void*)vex->guest_GPR28);
141 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R29 * 4), (void*)vex->guest_GPR29);
142 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R30 * 4), (void*)vex->guest_GPR30);
143 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R31 * 4), (void*)vex->guest_GPR31);
144 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_NIP * 4), (void*)vex->guest_CIA);
145 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CCR * 4),
146 (void*)LibVEX_GuestPPC32_get_CR(vex));
147 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_LNK * 4), (void*)vex->guest_LR);
148 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CTR * 4), (void*)vex->guest_CTR);
149 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_XER * 4),
150 (void*)LibVEX_GuestPPC32_get_XER(vex));
153 #elif defined(VGP_ppc64_linux)
155 /* FRJ: copied nearly verbatim from the ppc32 case. I compared the
156 vki-ppc64-linux.h with its ppc32 counterpart and saw no
157 appreciable differences, other than the registers being 8 bytes
158 instead of 4. No idea why we don't set all of the entries
159 declared in vki_pt_regs, but ppc32 doesn't so there must be a
162 Finally, note that CR and XER are 32 bits even for ppc64 (see
163 libvex_guest_ppc64.h), but the vki_pt_regs struct still gives
166 /* apparently the casting to void* is the Right Thing To Do */
167 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R0 * 8), (void*)vex->guest_GPR0);
168 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R1 * 8), (void*)vex->guest_GPR1);
169 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R2 * 8), (void*)vex->guest_GPR2);
170 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R3 * 8), (void*)vex->guest_GPR3);
171 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R4 * 8), (void*)vex->guest_GPR4);
172 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R5 * 8), (void*)vex->guest_GPR5);
173 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R6 * 8), (void*)vex->guest_GPR6);
174 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R7 * 8), (void*)vex->guest_GPR7);
175 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R8 * 8), (void*)vex->guest_GPR8);
176 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R9 * 8), (void*)vex->guest_GPR9);
177 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R10 * 8), (void*)vex->guest_GPR10);
178 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R11 * 8), (void*)vex->guest_GPR11);
179 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R12 * 8), (void*)vex->guest_GPR12);
180 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R13 * 8), (void*)vex->guest_GPR13);
181 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R14 * 8), (void*)vex->guest_GPR14);
182 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R15 * 8), (void*)vex->guest_GPR15);
183 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R16 * 8), (void*)vex->guest_GPR16);
184 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R17 * 8), (void*)vex->guest_GPR17);
185 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R18 * 8), (void*)vex->guest_GPR18);
186 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R19 * 8), (void*)vex->guest_GPR19);
187 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R20 * 8), (void*)vex->guest_GPR20);
188 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R21 * 8), (void*)vex->guest_GPR21);
189 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R22 * 8), (void*)vex->guest_GPR22);
190 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R23 * 8), (void*)vex->guest_GPR23);
191 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R24 * 8), (void*)vex->guest_GPR24);
192 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R25 * 8), (void*)vex->guest_GPR25);
193 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R26 * 8), (void*)vex->guest_GPR26);
194 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R27 * 8), (void*)vex->guest_GPR27);
195 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R28 * 8), (void*)vex->guest_GPR28);
196 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R29 * 8), (void*)vex->guest_GPR29);
197 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R30 * 8), (void*)vex->guest_GPR30);
198 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R31 * 8), (void*)vex->guest_GPR31);
199 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_NIP * 8), (void*)vex->guest_CIA);
200 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CCR * 8),
201 (void*)(long)LibVEX_GuestPPC64_get_CR(vex));
202 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_LNK * 8), (void*)vex->guest_LR);
203 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CTR * 8), (void*)vex->guest_CTR);
204 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_XER * 8),
205 (void*)(long)LibVEX_GuestPPC64_get_XER(vex));
208 #elif defined(VGP_arm_linux)
209 struct vki_user_regs_struct uregs;
210 VG_(memset)(&uregs, 0, sizeof(uregs));
211 uregs.ARM_r0 = vex->guest_R0;
212 uregs.ARM_r1 = vex->guest_R1;
213 uregs.ARM_r2 = vex->guest_R2;
214 uregs.ARM_r3 = vex->guest_R3;
215 uregs.ARM_r4 = vex->guest_R4;
216 uregs.ARM_r5 = vex->guest_R5;
217 uregs.ARM_r6 = vex->guest_R6;
218 uregs.ARM_r7 = vex->guest_R7;
219 uregs.ARM_r8 = vex->guest_R8;
220 uregs.ARM_r9 = vex->guest_R9;
221 uregs.ARM_r10 = vex->guest_R10;
222 uregs.ARM_fp = vex->guest_R11;
223 uregs.ARM_ip = vex->guest_R12;
224 uregs.ARM_sp = vex->guest_R13;
225 uregs.ARM_lr = vex->guest_R14;
226 uregs.ARM_pc = vex->guest_R15;
227 uregs.ARM_cpsr = LibVEX_GuestARM_get_cpsr(vex);
228 return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &uregs);
230 #elif defined(VGP_ppc32_aix5)
233 #elif defined(VGP_ppc64_aix5)
236 #elif defined(VGP_x86_darwin)
239 #elif defined(VGP_amd64_darwin)
242 #elif defined(VGP_x86_l4re)
250 /* Start debugger and get it to attach to this process. Called if the
251 user requests this service after an error has been shown, so she can
252 poke around and look at parameters, memory, etc. You can't
253 meaningfully get the debugger to continue the program, though; to
254 continue, quit the debugger. */
255 void VG_(start_debugger) ( ThreadId tid )
264 rc = VG_(ptrace)(VKI_PTRACE_TRACEME, 0, NULL, NULL);
266 rc = VG_(kill)(VG_(getpid)(), VKI_SIGSTOP);
269 } else if (pid > 0) {
274 if ((res = VG_(waitpid)(pid, &status, 0)) == pid &&
275 WIFSTOPPED(status) && WSTOPSIG(status) == VKI_SIGSTOP &&
276 ptrace_setregs(pid, &(VG_(threads)[tid].arch.vex)) == 0 &&
277 VG_(kill)(pid, VKI_SIGSTOP) == 0 &&
278 VG_(ptrace)(VKI_PTRACE_DETACH, pid, NULL, 0) == 0)
286 VG_(sprintf)(pidbuf, "%d", pid);
287 VG_(sprintf)(file, "/proc/%d/fd/%d", pid, VG_(cl_exec_fd));
290 cmdptr = VG_(clo_db_command);
293 /* each iteration can advance bufptr by at most the length
294 of file[], so the following assertion is generously
296 vg_assert(bufptr - buf < N_BUF-15-50-10/*paranoia*/);
301 VG_(memcpy)(bufptr, file, VG_(strlen)(file));
302 bufptr += VG_(strlen)(file);
306 VG_(memcpy)(bufptr, pidbuf, VG_(strlen)(pidbuf));
307 bufptr += VG_(strlen)(pidbuf);
311 *bufptr++ = *cmdptr++;
316 *bufptr++ = *cmdptr++;
319 vg_assert(bufptr - buf < N_BUF-15-50-10/*paranoia*/);
324 VG_(message)(Vg_UserMsg, "starting debugger with cmd: %s\n", buf);
325 res = VG_(system)(buf);
327 VG_(message)(Vg_UserMsg, "\n");
328 VG_(message)(Vg_UserMsg,
329 "Debugger has detached. Valgrind regains control."
332 VG_(message)(Vg_UserMsg,
333 "Warning: Debugger attach failed! (sys_system)\n");
334 VG_(message)(Vg_UserMsg, "\n");
337 VG_(message)(Vg_UserMsg,
338 "Warning: Debugger attach failed! (ptrace problem?)\n");
339 VG_(message)(Vg_UserMsg, "\n");
342 VG_(kill)(pid, VKI_SIGKILL);
343 VG_(waitpid)(pid, &status, 0);
350 /*--------------------------------------------------------------------*/
352 /*--------------------------------------------------------------------*/