blob: 6fe8e5e37918c6a506b2a53d24b45c8fbc227625 [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>
10
Juan Cespedesf7281232009-06-25 16:11:21 +020011#include "common.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020012
13/* If the system headers did not provide the constants, hard-code the normal
14 values. */
15#ifndef PTRACE_EVENT_FORK
16
17#define PTRACE_OLDSETOPTIONS 21
18#define PTRACE_SETOPTIONS 0x4200
19#define PTRACE_GETEVENTMSG 0x4201
20
21/* options set using PTRACE_SETOPTIONS */
22#define PTRACE_O_TRACESYSGOOD 0x00000001
23#define PTRACE_O_TRACEFORK 0x00000002
24#define PTRACE_O_TRACEVFORK 0x00000004
25#define PTRACE_O_TRACECLONE 0x00000008
26#define PTRACE_O_TRACEEXEC 0x00000010
27#define PTRACE_O_TRACEVFORKDONE 0x00000020
28#define PTRACE_O_TRACEEXIT 0x00000040
29
30/* Wait extended result codes for the above trace options. */
31#define PTRACE_EVENT_FORK 1
32#define PTRACE_EVENT_VFORK 2
33#define PTRACE_EVENT_CLONE 3
34#define PTRACE_EVENT_EXEC 4
35#define PTRACE_EVENT_VFORK_DONE 5
36#define PTRACE_EVENT_EXIT 6
37
38#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010039
Luis Machado55c5feb2008-03-12 15:56:01 +010040#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020041extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010042int
Juan Cespedesa8909f72009-04-28 20:02:41 +020043umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010044 return arch_umovelong (proc, addr, result, info);
45}
46#else
47/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010048int
Juan Cespedesa8909f72009-04-28 20:02:41 +020049umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010050 long pointed_to;
51
52 errno = 0;
53 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
54 if (pointed_to == -1 && errno)
55 return -errno;
56
57 *result = pointed_to;
Arnaud Patardf16fcff2010-01-08 08:40:19 -050058 if (info) {
59 switch(info->type) {
60 case ARGTYPE_INT:
61 *result &= 0x00000000ffffffffUL;
62 default:
63 break;
64 };
65 }
Luis Machado55c5feb2008-03-12 15:56:01 +010066 return 0;
67}
68#endif
69
Juan Cespedesf1350522008-12-16 18:19:58 +010070void
71trace_me(void) {
Petr Machata26627682011-07-08 18:15:32 +020072 debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010074 perror("PTRACE_TRACEME");
75 exit(1);
76 }
77}
78
Juan Cespedesf1350522008-12-16 18:19:58 +010079int
80trace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +020081 debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid);
Juan Cespedes1fe93d51998-03-13 00:29:21 +010082 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010083 return -1;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010084 }
Petr Machata89a53602007-01-25 18:05:44 +010085
Juan Cespedes714ee9d2009-04-07 13:28:54 +020086 /* man ptrace: PTRACE_ATTACH attaches to the process specified
87 in pid. The child is sent a SIGSTOP, but will not
88 necessarily have stopped by the completion of this call;
89 use wait() to wait for the child to stop. */
90 if (waitpid (pid, NULL, 0) != pid) {
91 perror ("trace_pid: waitpid");
92 exit (1);
93 }
94
Juan Cespedes273ea6d1998-03-14 23:02:40 +010095 return 0;
96}
97
Juan Cespedesf1350522008-12-16 18:19:58 +010098void
Juan Cespedesa8909f72009-04-28 20:02:41 +020099trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100100 if (proc->tracesysgood & 0x80)
101 return;
Petr Machata55ed83b2007-05-17 16:24:15 +0200102
Petr Machata26627682011-07-08 18:15:32 +0200103 debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid);
Juan Cespedescd8976d2009-05-14 13:47:58 +0200104
Juan Cespedes1e583132009-04-07 18:17:11 +0200105 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
106 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
107 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200108 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
109 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100110 perror("PTRACE_SETOPTIONS");
111 return;
112 }
113 proc->tracesysgood |= 0x80;
114}
115
Juan Cespedesf1350522008-12-16 18:19:58 +0100116void
117untrace_pid(pid_t pid) {
Petr Machata26627682011-07-08 18:15:32 +0200118 debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100119 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100120}
121
Juan Cespedesf1350522008-12-16 18:19:58 +0100122void
123continue_after_signal(pid_t pid, int signum) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200124 Process *proc;
Juan Cespedese74c80d2009-02-11 11:32:31 +0100125
Juan Cespedescd8976d2009-05-14 13:47:58 +0200126 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
127
Juan Cespedese74c80d2009-02-11 11:32:31 +0100128 proc = pid2proc(pid);
129 if (proc && proc->breakpoint_being_enabled) {
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500130#if defined __sparc__ || defined __ia64___ || defined __mips__
Juan Cespedese74c80d2009-02-11 11:32:31 +0100131 ptrace(PTRACE_SYSCALL, pid, 0, signum);
132#else
133 ptrace(PTRACE_SINGLESTEP, pid, 0, signum);
134#endif
135 } else {
136 ptrace(PTRACE_SYSCALL, pid, 0, signum);
137 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100138}
139
Juan Cespedesf1350522008-12-16 18:19:58 +0100140void
141continue_process(pid_t pid) {
Juan Cespedese74c80d2009-02-11 11:32:31 +0100142 /* We always trace syscalls to control fork(), clone(), execve()... */
143
Juan Cespedescd8976d2009-05-14 13:47:58 +0200144 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
145
Juan Cespedese74c80d2009-02-11 11:32:31 +0100146 ptrace(PTRACE_SYSCALL, pid, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100147}
148
Juan Cespedesf1350522008-12-16 18:19:58 +0100149void
Petr Machataf789c9c2011-07-09 10:54:27 +0200150continue_enabling_breakpoint(Process * proc, Breakpoint *sbp)
151{
152 enable_breakpoint(proc, sbp);
153 continue_process(proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100154}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100155
Juan Cespedesf1350522008-12-16 18:19:58 +0100156void
Petr Machata26627682011-07-08 18:15:32 +0200157continue_after_breakpoint(Process *proc, Breakpoint *sbp)
158{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100159 if (sbp->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200160 disable_breakpoint(proc, sbp);
161
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200162 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100163 if (sbp->enabled == 0) {
164 continue_process(proc->pid);
165 } else {
Petr Machata26627682011-07-08 18:15:32 +0200166 debug(DEBUG_PROCESS,
167 "continue_after_breakpoint: pid=%d, addr=%p",
168 proc->pid, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100169 proc->breakpoint_being_enabled = sbp;
Arnaud Patardf3d1c532010-01-08 08:40:04 -0500170#if defined __sparc__ || defined __ia64___ || defined __mips__
Ian Wienand9a2ad352006-02-20 22:44:45 +0100171 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200172 continue_process(proc->pid);
173#else
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100174 ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200175#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100176 }
177}
178
Joe Damatodfa3fa32010-11-08 15:47:35 -0800179size_t
180umovebytes(Process *proc, void *addr, void *laddr, size_t len) {
181
182 union {
183 long a;
184 char c[sizeof(long)];
185 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800186 int started = 0;
187 size_t offset = 0, bytes_read = 0;
Joe Damatodfa3fa32010-11-08 15:47:35 -0800188
189 while (offset < len) {
190 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
191 if (a.a == -1 && errno) {
192 if (started && errno == EIO)
193 return bytes_read;
194 else
195 return -1;
196 }
197 started = 1;
198
199 if (len - offset >= sizeof(long)) {
200 memcpy(laddr + offset, &a.c[0], sizeof(long));
201 bytes_read += sizeof(long);
202 }
203 else {
204 memcpy(laddr + offset, &a.c[0], len - offset);
205 bytes_read += (len - offset);
206 }
207 offset += sizeof(long);
208 }
209
210 return bytes_read;
211}
212
Steve Fink7bafff02006-08-07 04:50:42 +0200213/* Read a series of bytes starting at the process's memory address
214 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
215 have been read.
216*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100217int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200218umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100219 union {
220 long a;
221 char c[sizeof(long)];
222 } a;
Zachary T Welchba6aca22010-12-08 18:55:09 -0800223 unsigned i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100225
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100226 while (offset < len) {
227 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
228 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200229 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100230 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100231 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100233 return 0;
234 }
235 }
236 offset += sizeof(long);
237 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100238 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100239 return 0;
240}