2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
10 #include <linux/unistd.h>
13 #include "ptrace_user.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 /* Kill off a ptraced child by all means available. kill it normally first,
99 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
100 * which it can't exit directly.
103 void os_kill_ptraced_process(int pid
, int reap_child
)
106 ptrace(PTRACE_KILL
, pid
);
107 ptrace(PTRACE_CONT
, pid
);
109 CATCH_EINTR(waitpid(pid
, NULL
, 0));
112 void os_usr1_process(int pid
)
117 /*Don't use the glibc version, which caches the result in TLS. It misses some
118 * syscalls, and also breaks with clone(), which does not unshare the TLS.*/
119 inline _syscall0(pid_t
, getpid
)
131 int os_map_memory(void *virt
, int fd
, unsigned long long off
, unsigned long len
,
137 prot
= (r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
140 loc
= mmap64((void *) virt
, len
, prot
, MAP_SHARED
| MAP_FIXED
,
142 if(loc
== MAP_FAILED
)
147 int os_protect_memory(void *addr
, unsigned long len
, int r
, int w
, int x
)
149 int prot
= ((r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
150 (x
? PROT_EXEC
: 0));
152 if(mprotect(addr
, len
, prot
) < 0)
157 int os_unmap_memory(void *addr
, int len
)
161 err
= munmap(addr
, len
);
168 * Overrides for Emacs so that we follow Linus's tabbing style.
169 * Emacs will notice this stuff at the end of the file and automatically
170 * adjust the settings for this buffer only. This must remain at the end
172 * ---------------------------------------------------------------------------
174 * c-file-style: "linux"