trace.c revision 1b9cfd6ad305ad909e8ff17139111a7c78f01464
1#include <stdio.h> 2#include <string.h> 3#include <errno.h> 4#include <sys/types.h> 5#include <sys/ptrace.h> 6#include <asm/unistd.h> 7 8#include "ltrace.h" 9#include "options.h" 10 11/* Returns 1 if the sysnum may make a new child to be created 12 * (ie, with fork() or clone()) 13 * Returns 0 otherwise. 14 */ 15int fork_p(int sysnum) 16{ 17 return 0 18#if defined(__NR_fork) 19 || (sysnum == __NR_fork) 20#endif 21#if defined(__NR_clone) 22 || (sysnum == __NR_clone) 23#endif 24#if defined(__NR_vfork) 25 || (sysnum == __NR_vfork) 26#endif 27 ; 28} 29 30/* Returns 1 if the sysnum may make the process exec other program 31 */ 32int exec_p(int sysnum) 33{ 34 return (sysnum == __NR_execve); 35} 36 37void trace_me(void) 38{ 39 if (ptrace(PTRACE_TRACEME, 0, 1, 0)<0) { 40 perror("PTRACE_TRACEME"); 41 exit(1); 42 } 43} 44 45int trace_pid(pid_t pid) 46{ 47 if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { 48 return -1; 49 } 50 return 0; 51} 52 53void untrace_pid(pid_t pid) 54{ 55 ptrace(PTRACE_DETACH, pid, 1, 0); 56} 57 58void continue_after_signal(pid_t pid, int signum) 59{ 60 /* We should always trace syscalls to be able to control fork(), clone(), execve()... */ 61#if 0 62 if (opt_S) { 63 ptrace(PTRACE_SYSCALL, pid, 1, signum); 64 } else { 65 ptrace(PTRACE_CONT, pid, 1, signum); 66 } 67#else 68 ptrace(PTRACE_SYSCALL, pid, 1, signum); 69#endif 70} 71 72void continue_process(pid_t pid) 73{ 74 continue_after_signal(pid, 0); 75} 76 77void continue_enabling_breakpoint(pid_t pid, struct breakpoint * sbp) 78{ 79 insert_breakpoint(pid, sbp); 80 continue_process(pid); 81} 82