blob: 15b597bcd7d316c0067d66a767d9d10acc3d183c [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 Cespedes3df476b2009-05-28 19:17:17 +020011#include "main.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010012#include "options.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +010013#include "sysdep.h"
Petr Machata55ed83b2007-05-17 16:24:15 +020014#include "debug.h"
15
16/* If the system headers did not provide the constants, hard-code the normal
17 values. */
18#ifndef PTRACE_EVENT_FORK
19
20#define PTRACE_OLDSETOPTIONS 21
21#define PTRACE_SETOPTIONS 0x4200
22#define PTRACE_GETEVENTMSG 0x4201
23
24/* options set using PTRACE_SETOPTIONS */
25#define PTRACE_O_TRACESYSGOOD 0x00000001
26#define PTRACE_O_TRACEFORK 0x00000002
27#define PTRACE_O_TRACEVFORK 0x00000004
28#define PTRACE_O_TRACECLONE 0x00000008
29#define PTRACE_O_TRACEEXEC 0x00000010
30#define PTRACE_O_TRACEVFORKDONE 0x00000020
31#define PTRACE_O_TRACEEXIT 0x00000040
32
33/* Wait extended result codes for the above trace options. */
34#define PTRACE_EVENT_FORK 1
35#define PTRACE_EVENT_VFORK 2
36#define PTRACE_EVENT_CLONE 3
37#define PTRACE_EVENT_EXEC 4
38#define PTRACE_EVENT_VFORK_DONE 5
39#define PTRACE_EVENT_EXIT 6
40
41#endif /* PTRACE_EVENT_FORK */
Ian Wienand9a2ad352006-02-20 22:44:45 +010042
Luis Machado55c5feb2008-03-12 15:56:01 +010043#ifdef ARCH_HAVE_UMOVELONG
Juan Cespedesa8909f72009-04-28 20:02:41 +020044extern int arch_umovelong (Process *, void *, long *, arg_type_info *);
Juan Cespedesf1350522008-12-16 18:19:58 +010045int
Juan Cespedesa8909f72009-04-28 20:02:41 +020046umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010047 return arch_umovelong (proc, addr, result, info);
48}
49#else
50/* Read a single long from the process's memory address 'addr' */
Juan Cespedesf1350522008-12-16 18:19:58 +010051int
Juan Cespedesa8909f72009-04-28 20:02:41 +020052umovelong (Process *proc, void *addr, long *result, arg_type_info *info) {
Luis Machado55c5feb2008-03-12 15:56:01 +010053 long pointed_to;
54
55 errno = 0;
56 pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0);
57 if (pointed_to == -1 && errno)
58 return -errno;
59
60 *result = pointed_to;
61 return 0;
62}
63#endif
64
Juan Cespedesf1350522008-12-16 18:19:58 +010065void
66trace_me(void) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020067 debug(DEBUG_PROCESS, "trace_me: pid=%d\n", getpid());
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010069 perror("PTRACE_TRACEME");
70 exit(1);
71 }
72}
73
Juan Cespedesf1350522008-12-16 18:19:58 +010074int
75trace_pid(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +020076 debug(DEBUG_PROCESS, "trace_pid: pid=%d\n", pid);
Juan Cespedes1fe93d51998-03-13 00:29:21 +010077 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010078 return -1;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010079 }
Petr Machata89a53602007-01-25 18:05:44 +010080
Juan Cespedes714ee9d2009-04-07 13:28:54 +020081 /* man ptrace: PTRACE_ATTACH attaches to the process specified
82 in pid. The child is sent a SIGSTOP, but will not
83 necessarily have stopped by the completion of this call;
84 use wait() to wait for the child to stop. */
85 if (waitpid (pid, NULL, 0) != pid) {
86 perror ("trace_pid: waitpid");
87 exit (1);
88 }
89
Juan Cespedes273ea6d1998-03-14 23:02:40 +010090 return 0;
91}
92
Juan Cespedesf1350522008-12-16 18:19:58 +010093void
Juan Cespedesa8909f72009-04-28 20:02:41 +020094trace_set_options(Process *proc, pid_t pid) {
Ian Wienand9a2ad352006-02-20 22:44:45 +010095 if (proc->tracesysgood & 0x80)
96 return;
Petr Machata55ed83b2007-05-17 16:24:15 +020097
Juan Cespedescd8976d2009-05-14 13:47:58 +020098 debug(DEBUG_PROCESS, "trace_set_options: pid=%d\n", pid);
99
Juan Cespedes1e583132009-04-07 18:17:11 +0200100 long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
101 PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
102 PTRACE_O_TRACEEXEC;
Petr Machata55ed83b2007-05-17 16:24:15 +0200103 if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 &&
104 ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100105 perror("PTRACE_SETOPTIONS");
106 return;
107 }
108 proc->tracesysgood |= 0x80;
109}
110
Juan Cespedesf1350522008-12-16 18:19:58 +0100111void
112untrace_pid(pid_t pid) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200113 debug(DEBUG_PROCESS, "untrace_pid: pid=%d\n", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100114 ptrace(PTRACE_DETACH, pid, 1, 0);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100115}
116
Juan Cespedesf1350522008-12-16 18:19:58 +0100117void
118continue_after_signal(pid_t pid, int signum) {
Juan Cespedesa8909f72009-04-28 20:02:41 +0200119 Process *proc;
Juan Cespedese74c80d2009-02-11 11:32:31 +0100120
Juan Cespedescd8976d2009-05-14 13:47:58 +0200121 debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum);
122
Juan Cespedese74c80d2009-02-11 11:32:31 +0100123 proc = pid2proc(pid);
124 if (proc && proc->breakpoint_being_enabled) {
125#if defined __sparc__ || defined __ia64___
126 ptrace(PTRACE_SYSCALL, pid, 0, signum);
127#else
128 ptrace(PTRACE_SINGLESTEP, pid, 0, signum);
129#endif
130 } else {
131 ptrace(PTRACE_SYSCALL, pid, 0, signum);
132 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100133}
134
Juan Cespedesf1350522008-12-16 18:19:58 +0100135void
136continue_process(pid_t pid) {
Juan Cespedese74c80d2009-02-11 11:32:31 +0100137 /* We always trace syscalls to control fork(), clone(), execve()... */
138
Juan Cespedescd8976d2009-05-14 13:47:58 +0200139 debug(DEBUG_PROCESS, "continue_process: pid=%d", pid);
140
Juan Cespedese74c80d2009-02-11 11:32:31 +0100141 ptrace(PTRACE_SYSCALL, pid, 0, 0);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100142}
143
Juan Cespedesf1350522008-12-16 18:19:58 +0100144void
Juan Cespedes1dec2172009-05-07 10:12:10 +0200145continue_enabling_breakpoint(pid_t pid, Breakpoint *sbp) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200146 enable_breakpoint(pid, sbp);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100147 continue_process(pid);
148}
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100149
Juan Cespedesf1350522008-12-16 18:19:58 +0100150void
Juan Cespedes1dec2172009-05-07 10:12:10 +0200151continue_after_breakpoint(Process *proc, Breakpoint *sbp) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100152 if (sbp->enabled)
153 disable_breakpoint(proc->pid, sbp);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200154 set_instruction_pointer(proc, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100155 if (sbp->enabled == 0) {
156 continue_process(proc->pid);
157 } else {
Juan Cespedes85f7d762009-05-14 13:53:59 +0200158 debug(DEBUG_PROCESS, "continue_after_breakpoint: pid=%d, addr=%p", proc->pid, sbp->addr);
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100159 proc->breakpoint_being_enabled = sbp;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100160#if defined __sparc__ || defined __ia64___
161 /* we don't want to singlestep here */
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200162 continue_process(proc->pid);
163#else
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100164 ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200165#endif
Juan Cespedes8f8282f2002-03-03 18:58:40 +0100166 }
167}
168
Steve Fink7bafff02006-08-07 04:50:42 +0200169/* Read a series of bytes starting at the process's memory address
170 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes
171 have been read.
172*/
Juan Cespedesf1350522008-12-16 18:19:58 +0100173int
Juan Cespedesa8909f72009-04-28 20:02:41 +0200174umovestr(Process *proc, void *addr, int len, void *laddr) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100175 union {
176 long a;
177 char c[sizeof(long)];
178 } a;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100179 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100180 int offset = 0;
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100181
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100182 while (offset < len) {
183 a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0);
184 for (i = 0; i < sizeof(long); i++) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200185 if (a.c[i] && offset + (signed)i < len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100186 *(char *)(laddr + offset + i) = a.c[i];
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100187 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100188 *(char *)(laddr + offset + i) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100189 return 0;
190 }
191 }
192 offset += sizeof(long);
193 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100194 *(char *)(laddr + offset) = '\0';
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100195 return 0;
196}