blob: fceef824e3a6bde13625b25fd89d21892615c430 [file] [log] [blame]
Juan Cespedes5e01f651998-03-08 22:31:44 +01001#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +01002#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +01003#include <string.h>
4#include <errno.h>
Juan Cespedes8f8282f2002-03-03 18:58:40 +01005#include <unistd.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01006#include <sys/types.h>
Petr Machata89a53602007-01-25 18:05:44 +01007#include <sys/wait.h>
Juan Cespedes5c3fe062004-06-14 18:08:37 +02008#include "ptrace.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <asm/unistd.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020010#include <assert.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010011
Juan Cespedesf7281232009-06-25 16:11:21 +020012#include "common.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020013
14/* If the system headers did not provide the constants, hard-code the normal
15 values. */
16#ifndef PTRACE_EVENT_FORK
17
18#define PTRACE_OLDSETOPTIONS 21
19#define PTRACE_SETOPTIONS 0x4200
20#define PTRACE_GETEVENTMSG 0x4201
21
22/* options set using PTRACE_SETOPTIONS */
23#define PTRACE_O_TRACESYSGOOD 0x00000001
24#define PTRACE_O_TRACEFORK 0x00000002
25#define PTRACE_O_TRACEVFORK 0x00000004
26#define PTRACE_O_TRACECLONE 0x00000008
27#define PTRACE_O_TRACEEXEC 0x00000010
28#define PTRACE_O_TRACEVFORKDONE 0x00000020
29#define PTRACE_O_TRACEEXIT 0x00000040
30
31/* Wait extended result codes for the above trace options. */
32#define PTRACE_EVENT_FORK 1
33#define PTRACE_EVENT_VFORK 2
34#define PTRACE_EVENT_CLONE 3
35#define PTRACE_EVENT_EXEC 4
36#define PTRACE_EVENT_VFORK_DONE 5
37#define PTRACE_EVENT_EXIT 6
38
39#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010040
Luis Machado55c5feb2008-03-12 15:56:01 +010041#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020042extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010043int
Juan Cespedesa8909f72009-04-28 20:02:41 +020044umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010045 return arch_umovelong (proc, addr, result, info);
46}
47#else
48/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010049int
Juan Cespedesa8909f72009-04-28 20:02:41 +020050umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010051 long pointed_to;
52
53 errno = 0;
54 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
55 if (pointed_to == -1 && errno)
56 return -errno;
57
58 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050059 if (info) {
60 switch(info->type) {
61 case ARGTYPE_INT:
62 *result &= 0x00000000ffffffffUL;
63 default:
64 break;
65 };
66 }
Luis Machado55c5feb2008-03-12 15:56:01 +010067 return 0;
68}
69#endif
70
Juan Cespedesf1350522008-12-16 18:19:58 +010071void
72trace_me(void) {
Petr Machata26627682011-07-08 18:15:32 +020073 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 perror("PTRACE_TRACEME");
76 exit(1);
77 }
78}
79
Juan Cespedesf1350522008-12-16 18:19:58 +010080int
81trace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +020082 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Juan Cespedes1fe93d51998-03-13 00:29:21 +010083 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010084 return -1;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010085 }
Petr Machata89a53602007-01-25 18:05:44 +010086
Juan Cespedes714ee9d2009-04-07 13:28:54 +020087 /* man ptrace: PTRACE_ATTACH attaches to the process specified
88 in pid. The child is sent a SIGSTOP, but will not
89 necessarily have stopped by the completion of this call;
90 use wait() to wait for the child to stop. */
Petr Machata9a5420c2011-07-09 11:21:23 +020091 if (waitpid (pid, NULL, __WALL) != pid) {
Juan Cespedes714ee9d2009-04-07 13:28:54 +020092 perror ("trace_pid: waitpid");
Petr Machata9a5420c2011-07-09 11:21:23 +020093 return -1;
Juan Cespedes714ee9d2009-04-07 13:28:54 +020094 }
95
Juan Cespedes273ea6d1998-03-14 23:02:40 +010096 return 0;
97}
98
Juan Cespedesf1350522008-12-16 18:19:58 +010099void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200100trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100101 if (proc->tracesysgood & 0x80)
102 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200103
Petr Machata26627682011-07-08 18:15:32 +0200104 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200105
Juan Cespedes1e583132009-04-07 18:17:11 +0200106 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
107 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
108 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200109 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
110 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100111 perror("PTRACE_SETOPTIONS");
112 return;
113 }
114 proc->tracesysgood |= 0x80;
115}
116
Juan Cespedesf1350522008-12-16 18:19:58 +0100117void
118untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200119 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100120 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100121}
122
Juan Cespedesf1350522008-12-16 18:19:58 +0100123void
124continue_after_signal(pid_t pid, int signum) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200125 Process *proc;
Juan Cespedese74c80d2009-02-11 11:32:31 +0100126
Juan Cespedescd8976d2009-05-14 13:47:58 +0200127 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
128
Juan Cespedese74c80d2009-02-11 11:32:31 +0100129 proc = pid2proc(pid);
130 if (proc && proc->breakpoint_being_enabled) {
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500131#if defined __sparc__ || defined __ia64___ || defined __mips__
Juan Cespedese74c80d2009-02-11 11:32:31 +0100132 ptrace(PTRACE_SYSCALL, pid, 0, signum);
133#else
134 ptrace(PTRACE_SINGLESTEP, pid, 0, signum);
135#endif
136 } else {
137 ptrace(PTRACE_SYSCALL, pid, 0, signum);
138 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100139}
140
Juan Cespedesf1350522008-12-16 18:19:58 +0100141void
142continue_process(pid_t pid) {
Juan Cespedese74c80d2009-02-11 11:32:31 +0100143 /* We always trace syscalls to control fork(), clone(), execve()... */
144
Juan Cespedescd8976d2009-05-14 13:47:58 +0200145 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
146
Juan Cespedese74c80d2009-02-11 11:32:31 +0100147 ptrace(PTRACE_SYSCALL, pid, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100148}
149
Juan Cespedesf1350522008-12-16 18:19:58 +0100150void
Petr Machataf789c9c2011-07-09 10:54:27 +0200151continue_enabling_breakpoint(Process * proc, Breakpoint *sbp)
152{
153 enable_breakpoint(proc, sbp);
154 continue_process(proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100155}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100156
Juan Cespedesf1350522008-12-16 18:19:58 +0100157void
Petr Machata26627682011-07-08 18:15:32 +0200158continue_after_breakpoint(Process *proc, Breakpoint *sbp)
159{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 if (sbp->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200161 disable_breakpoint(proc, sbp);
162
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200163 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100164 if (sbp->enabled == 0) {
165 continue_process(proc->pid);
166 } else {
Petr Machata26627682011-07-08 18:15:32 +0200167 debug(DEBUG_PROCESS,
168 "continue_after_breakpoint: pid=%d, addr=%p",
169 proc->pid, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100170 proc->breakpoint_being_enabled = sbp;
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500171#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100172 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200173 continue_process(proc->pid);
174#else
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100175 ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200176#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100177 }
178}
179
Joe Damatodfa3fa32010-11-08 15:47:35 -0800180size_t
181umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
182
183 union {
184 long a;
185 char c[sizeof(long)];
186 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800187 int started = 0;
188 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800189
190 while (offset < len) {
191 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
192 if (a.a == -1 && errno) {
193 if (started && errno == EIO)
194 return bytes_read;
195 else
196 return -1;
197 }
198 started = 1;
199
200 if (len - offset >= sizeof(long)) {
201 memcpy(laddr + offset, &a.c[0], sizeof(long));
202 bytes_read += sizeof(long);
203 }
204 else {
205 memcpy(laddr + offset, &a.c[0], len - offset);
206 bytes_read += (len - offset);
207 }
208 offset += sizeof(long);
209 }
210
211 return bytes_read;
212}
213
Steve Fink7bafff02006-08-07 04:50:42 +0200214/* Read a series of bytes starting at the process's memory address
215 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
216 have been read.
217*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100218int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200219umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 union {
221 long a;
222 char c[sizeof(long)];
223 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800224 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100225 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100226
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 while (offset < len) {
228 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
229 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200230 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100232 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100233 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100234 return 0;
235 }
236 }
237 offset += sizeof(long);
238 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100239 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100240 return 0;
241}