blob: 9a5cc0d9ee3dfad6217e73fcf05b461d87be8cdb [file] [log] [blame]
njn75b65aa2005-06-19 19:25:44 +00001
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-2005 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_threadstate.h"
sewardj45f4e7c2005-09-27 19:20:21 +000033#include "pub_core_clientstate.h"
njn75b65aa2005-06-19 19:25:44 +000034#include "pub_core_debugger.h"
35#include "pub_core_libcbase.h"
njn75b65aa2005-06-19 19:25:44 +000036#include "pub_core_libcprint.h"
37#include "pub_core_libcproc.h"
38#include "pub_core_libcsignal.h"
sewardj2c48c7b2005-11-29 13:05:56 +000039#include "pub_core_libcassert.h"
njn75b65aa2005-06-19 19:25:44 +000040#include "pub_core_options.h"
41
sewardjccc89d92005-11-09 14:43:03 +000042
sewardj45f4e7c2005-09-27 19:20:21 +000043#define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
44#define WSTOPSIG(status) (((status) & 0xff00) >> 8)
njn75b65aa2005-06-19 19:25:44 +000045
46static Int ptrace_setregs(Int pid, VexGuestArchState* vex)
47{
njn75b65aa2005-06-19 19:25:44 +000048#if defined(VGA_x86)
sewardjc9e73dc2005-11-09 15:14:16 +000049 struct vki_user_regs_struct regs;
njn75b65aa2005-06-19 19:25:44 +000050 regs.cs = vex->guest_CS;
51 regs.ss = vex->guest_SS;
52 regs.ds = vex->guest_DS;
53 regs.es = vex->guest_ES;
54 regs.fs = vex->guest_FS;
55 regs.gs = vex->guest_GS;
56 regs.eax = vex->guest_EAX;
57 regs.ebx = vex->guest_EBX;
58 regs.ecx = vex->guest_ECX;
59 regs.edx = vex->guest_EDX;
60 regs.esi = vex->guest_ESI;
61 regs.edi = vex->guest_EDI;
62 regs.ebp = vex->guest_EBP;
63 regs.esp = vex->guest_ESP;
64 regs.eflags = LibVEX_GuestX86_get_eflags(vex);
65 regs.eip = vex->guest_EIP;
sewardj45f4e7c2005-09-27 19:20:21 +000066 return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &regs);
sewardjc9e73dc2005-11-09 15:14:16 +000067
njn75b65aa2005-06-19 19:25:44 +000068#elif defined(VGA_amd64)
sewardjc9e73dc2005-11-09 15:14:16 +000069 struct vki_user_regs_struct regs;
tome4fed1c2005-07-19 18:19:48 +000070 regs.rax = vex->guest_RAX;
71 regs.rbx = vex->guest_RBX;
72 regs.rcx = vex->guest_RCX;
73 regs.rdx = vex->guest_RDX;
74 regs.rsi = vex->guest_RSI;
75 regs.rdi = vex->guest_RDI;
76 regs.rbp = vex->guest_RBP;
77 regs.rsp = vex->guest_RSP;
78 regs.r8 = vex->guest_R8;
79 regs.r9 = vex->guest_R9;
80 regs.r10 = vex->guest_R10;
81 regs.r11 = vex->guest_R11;
82 regs.r12 = vex->guest_R12;
83 regs.r13 = vex->guest_R13;
84 regs.r14 = vex->guest_R14;
85 regs.r15 = vex->guest_R15;
86 regs.eflags = LibVEX_GuestAMD64_get_rflags(vex);
87 regs.rip = vex->guest_RIP;
sewardj45f4e7c2005-09-27 19:20:21 +000088 return VG_(ptrace)(VKI_PTRACE_SETREGS, pid, NULL, &regs);
sewardjc9e73dc2005-11-09 15:14:16 +000089
cerion85665ca2005-06-20 15:51:07 +000090#elif defined(VGA_ppc32)
tom01093222005-11-09 08:52:56 +000091 Int rc = 0;
sewardjc9e73dc2005-11-09 15:14:16 +000092 /* apparently the casting to void* is the Right Thing To Do */
93 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R0 * 4), (void*)vex->guest_GPR0);
94 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R1 * 4), (void*)vex->guest_GPR1);
95 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R2 * 4), (void*)vex->guest_GPR2);
96 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R3 * 4), (void*)vex->guest_GPR3);
97 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R4 * 4), (void*)vex->guest_GPR4);
98 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R5 * 4), (void*)vex->guest_GPR5);
99 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R6 * 4), (void*)vex->guest_GPR6);
100 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R7 * 4), (void*)vex->guest_GPR7);
101 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R8 * 4), (void*)vex->guest_GPR8);
102 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R9 * 4), (void*)vex->guest_GPR9);
103 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R10 * 4), (void*)vex->guest_GPR10);
104 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R11 * 4), (void*)vex->guest_GPR11);
105 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R12 * 4), (void*)vex->guest_GPR12);
106 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R13 * 4), (void*)vex->guest_GPR13);
107 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R14 * 4), (void*)vex->guest_GPR14);
108 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R15 * 4), (void*)vex->guest_GPR15);
109 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R16 * 4), (void*)vex->guest_GPR16);
110 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R17 * 4), (void*)vex->guest_GPR17);
111 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R18 * 4), (void*)vex->guest_GPR18);
112 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R19 * 4), (void*)vex->guest_GPR19);
113 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R20 * 4), (void*)vex->guest_GPR20);
114 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R21 * 4), (void*)vex->guest_GPR21);
115 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R22 * 4), (void*)vex->guest_GPR22);
116 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R23 * 4), (void*)vex->guest_GPR23);
117 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R24 * 4), (void*)vex->guest_GPR24);
118 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R25 * 4), (void*)vex->guest_GPR25);
119 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R26 * 4), (void*)vex->guest_GPR26);
120 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R27 * 4), (void*)vex->guest_GPR27);
121 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R28 * 4), (void*)vex->guest_GPR28);
122 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R29 * 4), (void*)vex->guest_GPR29);
123 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R30 * 4), (void*)vex->guest_GPR30);
124 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_R31 * 4), (void*)vex->guest_GPR31);
125 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_NIP * 4), (void*)vex->guest_CIA);
126 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CCR * 4),
127 (void*)LibVEX_GuestPPC32_get_CR(vex));
128 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_LNK * 4), (void*)vex->guest_LR);
129 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_CTR * 4), (void*)vex->guest_CTR);
130 rc |= VG_(ptrace)(VKI_PTRACE_POKEUSR, pid, (void*)(VKI_PT_XER * 4),
131 (void*)LibVEX_GuestPPC32_get_XER(vex));
tom01093222005-11-09 08:52:56 +0000132 return rc;
sewardjc9e73dc2005-11-09 15:14:16 +0000133
sewardj2c48c7b2005-11-29 13:05:56 +0000134#elif defined(VGA_ppc64)
135 I_die_here;
136
njn75b65aa2005-06-19 19:25:44 +0000137#else
138# error Unknown arch
139#endif
140}
141
142/* Start debugger and get it to attach to this process. Called if the
143 user requests this service after an error has been shown, so she can
144 poke around and look at parameters, memory, etc. You can't
145 meaningfully get the debugger to continue the program, though; to
146 continue, quit the debugger. */
147void VG_(start_debugger) ( ThreadId tid )
148{
sewardj45f4e7c2005-09-27 19:20:21 +0000149 Int pid;
njn75b65aa2005-06-19 19:25:44 +0000150
sewardj45f4e7c2005-09-27 19:20:21 +0000151 if ((pid = VG_(fork)()) == 0) {
152 VG_(ptrace)(VKI_PTRACE_TRACEME, 0, NULL, NULL);
njn75b65aa2005-06-19 19:25:44 +0000153 VG_(kill)(VG_(getpid)(), VKI_SIGSTOP);
154
155 } else if (pid > 0) {
156 Int status;
157 Int res;
158
159 if ((res = VG_(waitpid)(pid, &status, 0)) == pid &&
sewardj45f4e7c2005-09-27 19:20:21 +0000160 WIFSTOPPED(status) && WSTOPSIG(status) == VKI_SIGSTOP &&
njn75b65aa2005-06-19 19:25:44 +0000161 ptrace_setregs(pid, &(VG_(threads)[tid].arch.vex)) == 0 &&
sewardj45f4e7c2005-09-27 19:20:21 +0000162 VG_(kill)(pid, VKI_SIGSTOP) == 0 &&
163 VG_(ptrace)(VKI_PTRACE_DETACH, pid, NULL, 0) == 0)
njn75b65aa2005-06-19 19:25:44 +0000164 {
165 Char pidbuf[15];
166 Char file[30];
167 Char buf[100];
168 Char *bufptr;
169 Char *cmdptr;
170
171 VG_(sprintf)(pidbuf, "%d", pid);
sewardj45f4e7c2005-09-27 19:20:21 +0000172 VG_(sprintf)(file, "/proc/%d/fd/%d", pid, VG_(cl_exec_fd));
njn75b65aa2005-06-19 19:25:44 +0000173
174 bufptr = buf;
175 cmdptr = VG_(clo_db_command);
176
177 while (*cmdptr) {
178 switch (*cmdptr) {
179 case '%':
180 switch (*++cmdptr) {
181 case 'f':
182 VG_(memcpy)(bufptr, file, VG_(strlen)(file));
183 bufptr += VG_(strlen)(file);
184 cmdptr++;
185 break;
186 case 'p':
187 VG_(memcpy)(bufptr, pidbuf, VG_(strlen)(pidbuf));
188 bufptr += VG_(strlen)(pidbuf);
189 cmdptr++;
190 break;
191 default:
192 *bufptr++ = *cmdptr++;
193 break;
194 }
195 break;
196 default:
197 *bufptr++ = *cmdptr++;
198 break;
199 }
200 }
201
202 *bufptr++ = '\0';
203
204 VG_(message)(Vg_UserMsg, "starting debugger with cmd: %s", buf);
205 res = VG_(system)(buf);
206 if (res == 0) {
207 VG_(message)(Vg_UserMsg, "");
208 VG_(message)(Vg_UserMsg,
209 "Debugger has detached. Valgrind regains control. We continue.");
210 } else {
211 VG_(message)(Vg_UserMsg, "Apparently failed!");
212 VG_(message)(Vg_UserMsg, "");
213 }
214 }
215
216 VG_(kill)(pid, VKI_SIGKILL);
217 VG_(waitpid)(pid, &status, 0);
218 }
219}
220
221
222
223/*--------------------------------------------------------------------*/
224/*--- end ---*/
225/*--------------------------------------------------------------------*/