2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
10 #include <linux/unistd.h>
11 #include <sys/ptrace.h>
16 #include "user_util.h"
18 #define ARBITRARY_ADDR -1
19 #define FAILURE_PID -1
21 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
22 #define COMM_SCANF "%*[^)])"
24 unsigned long os_process_pc(int pid)
26 char proc_stat[STAT_PATH_LEN], buf[256];
30 sprintf(proc_stat, "/proc/%d/stat", pid);
31 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
33 printk("os_process_pc - couldn't open '%s', err = %d\n",
35 return(ARBITRARY_ADDR);
37 err = os_read_file(fd, buf, sizeof(buf));
39 printk("os_process_pc - couldn't read '%s', err = %d\n",
42 return(ARBITRARY_ADDR);
46 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
47 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
48 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
49 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
54 int os_process_parent(int pid)
56 char stat[STAT_PATH_LEN];
60 if(pid == -1) return(-1);
62 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
63 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
65 printk("Couldn't open '%s', err = %d\n", stat, -fd);
69 n = os_read_file(fd, data, sizeof(data));
73 printk("Couldn't read '%s', err = %d\n", stat, -n);
78 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
80 printk("Failed to scan '%s'\n", data);
85 void os_stop_process(int pid)
90 void os_kill_process(int pid, int reap_child)
94 CATCH_EINTR(waitpid(pid, NULL, 0));
98 void os_kill_ptraced_process(int pid, int reap_child)
100 ptrace(PTRACE_KILL, pid);
102 CATCH_EINTR(waitpid(pid, NULL, 0));
105 void os_usr1_process(int pid)
110 /*Don't use the glibc version, which caches the result in TLS. It misses some
111 * syscalls, and also breaks with clone(), which does not unshare the TLS.*/
112 inline _syscall0(pid_t, getpid)
119 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
125 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
128 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
130 if(loc == MAP_FAILED)
135 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
137 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
138 (x ? PROT_EXEC : 0));
140 if(mprotect(addr, len, prot) < 0)
145 int os_unmap_memory(void *addr, int len)
149 err = munmap(addr, len);
156 * Overrides for Emacs so that we follow Linus's tabbing style.
157 * Emacs will notice this stuff at the end of the file and automatically
158 * adjust the settings for this buffer only. This must remain at the end
160 * ---------------------------------------------------------------------------
162 * c-file-style: "linux"