2 * process interface for Linux.
4 * Uses ptrace for registers and data,
5 * /proc for some process status.
6 * There's not much point to worrying about
7 * byte order here -- using ptrace means
8 * we're running on the architecture we're debugging,
9 * unless truly weird stuff is going on.
11 * It is tempting to use /proc/%d/mem along with
12 * the sp and pc in the stat file to get a stack trace
13 * without attaching to the program, but unfortunately
14 * you can't read the mem file unless you've attached.
18 #include <sys/ptrace.h>
19 #include <sys/types.h>
27 Mach *machcpu = &mach386;
29 typedef struct PtraceRegs PtraceRegs;
37 static int ptracerw(Map*, Seg*, ulong, void*, uint, int);
38 static int ptraceregrw(Regs*, char*, ulong*, int);
47 for(i=0; i<map->nseg; i++)
48 while(i<map->nseg && map->seg[i].pid){
50 memmove(&map->seg[i], &map->seg[i+1],
51 (map->nseg-i)*sizeof(map->seg[0]));
56 mapproc(int pid, Map *map, Regs **rp)
61 if(ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
62 if(ptrace(PTRACE_PEEKUSER, pid, 0, 0) < 0)
63 if(ptrace(PTRACE_ATTACH, pid, 0, 0) < 0){
64 werrstr("ptrace attach %d: %r", pid);
68 if(ctlproc(pid, "waitstop") < 0){
69 ptrace(PTRACE_DETACH, pid, 0, 0);
73 memset(&s, 0, sizeof s);
81 if(addseg(map, s) < 0)
84 if((r = mallocz(sizeof(PtraceRegs), 1)) == nil)
86 r->r.rw = ptraceregrw;
95 return ptrace(PTRACE_DETACH, pid, 0, 0);
99 ptracerw(Map *map, Seg *seg, ulong addr, void *v, uint n, int isr)
109 u = ptrace(PTRACE_PEEKDATA, seg->pid, addr+i, 0);
113 *(u32int*)((char*)v+i) = u;
116 memmove((char*)v+i, buf, n-i);
120 u = *(u32int*)((char*)v+i);
123 u = ptrace(PTRACE_PEEKDATA, seg->pid, addr+i, 0);
127 memmove(buf, (char*)v+i, n-i);
130 if(ptrace(PTRACE_POKEDATA, seg->pid, addr+i, &u) < 0)
137 werrstr("ptrace: %r");
141 static char* linuxregs[] = {
166 for(i=0; i<nelem(linuxregs); i++)
167 if(strcmp(linuxregs[i], reg) == 0)
173 ptraceregrw(Regs *regs, char *name, ulong *val, int isr)
179 pid = ((PtraceRegs*)regs)->pid;
180 addr = reg2linux(name);
186 werrstr("register not available");
191 u = ptrace(PTRACE_PEEKUSER, pid, addr, 0);
197 if(ptrace(PTRACE_POKEUSER, pid, addr, &u) < 0)
203 werrstr("ptrace: %r");
214 snprint(buf, sizeof buf, "/proc/%d/stat", pid);
215 if((fd = open(buf, OREAD)) < 0)
217 n = read(fd, buf, sizeof buf-1);
223 /* command name is in parens, no parens afterward */
224 p = strrchr(buf, ')');
225 if(p == nil || *++p != ' ')
229 /* next is state - T is stopped for tracing */
233 /* /proc/pid/stat contains
242 6. flags (math=4, traced=10)
264 28. pending signal bitmap
265 29. blocked signal bitmap
266 30. ignored signal bitmap
267 31. caught signal bitmap
277 procnotes(int pid, char ***pnotes)
279 char buf[1024], *f[40];
281 char *p, *s, **notes;
283 extern char *_p9sigstr(int, char*);
286 snprint(buf, sizeof buf, "/proc/%d/stat", pid);
287 if((fd = open(buf, OREAD)) < 0){
288 fprint(2, "open %s: %r\n", buf);
291 n = read(fd, buf, sizeof buf-1);
294 fprint(2, "read %s: %r\n", buf);
299 /* command name is in parens, no parens afterward */
300 p = strrchr(buf, ')');
301 if(p == nil || *++p != ' '){
302 fprint(2, "bad format in /proc/%d/stat\n", pid);
307 nf = tokenize(p, f, nelem(f));
308 if(0) print("code 0x%lux-0x%lux stack 0x%lux kstk 0x%lux keip 0x%lux pending 0x%lux\n",
309 strtoul(f[23], 0, 0), strtoul(f[24], 0, 0), strtoul(f[25], 0, 0),
310 strtoul(f[26], 0, 0), strtoul(f[27], 0, 0), strtoul(f[28], 0, 0));
314 sigs = strtoul(f[28], 0, 0) & ~(1<<SIGCONT);
320 notes = mallocz(32*sizeof(char*), 0);
325 if((sigs&(1<<i)) == 0)
327 if((s = _p9sigstr(i, nil)) == nil)
338 ctlproc(int pid, char *msg)
342 if(strcmp(msg, "hang") == 0){
344 return ptrace(PTRACE_TRACEME, 0, 0, 0);
345 werrstr("can only hang self");
348 if(strcmp(msg, "kill") == 0)
349 return ptrace(PTRACE_KILL, pid, 0, 0);
350 if(strcmp(msg, "startstop") == 0){
351 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
355 if(strcmp(msg, "sysstop") == 0){
356 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
360 if(strcmp(msg, "stop") == 0){
361 if(kill(pid, SIGSTOP) < 0)
365 if(strcmp(msg, "waitstop") == 0){
370 p = waitpid(pid, &status, WUNTRACED);
373 if(WIFEXITED(status) || WIFSTOPPED(status))
377 if(strcmp(msg, "start") == 0)
378 return ptrace(PTRACE_CONT, pid, 0, 0);
379 werrstr("unknown control message '%s'", msg);
384 proctextfile(int pid)
386 static char buf[1024], pbuf[128];
388 snprint(pbuf, sizeof pbuf, "/proc/%d/exe", pid);
389 if(readlink(pbuf, buf, sizeof buf) >= 0)
391 if(access(pbuf, AEXIST) >= 0)
398 snprint(buf, sizeof buf, "/proc/%d/maps", pid);
399 if((b = Bopen(buf, OREAD)) == nil){
400 werrstr("open %s: %r", buf);
405 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm
406 08056000-08058000 rw-p 0000d000 03:0c 64593 /usr/sbin/gpm
407 08058000-0805b000 rwxp 00000000 00:00 0
408 40000000-40013000 r-xp 00000000 03:0c 4165 /lib/ld-2.2.4.so
409 40013000-40015000 rw-p 00012000 03:0c 4165 /lib/ld-2.2.4.so
410 4001f000-40135000 r-xp 00000000 03:0c 45494 /lib/libc-2.2.4.so
411 40135000-4013e000 rw-p 00115000 03:0c 45494 /lib/libc-2.2.4.so
412 4013e000-40142000 rw-p 00000000 00:00 0
413 bffff000-c0000000 rwxp 00000000 00:00 0
417 while((p = Brdline(b, '\n')) != nil){
418 p[Blinelen(b)-1] = 0;
419 memset(f, 0, sizeof f);
420 if((nf = getfields(p, f, 6, 1, " ")) < 5)
422 base = strtoul(f[0], &p, 16);
425 end = strtoul(p+1, &p, 16);
428 offset = strtoul(f[2], &p, 16);
433 zero = atoi(f[4]) == 0;
434 print("%lux-%lux %lux %s %s\n", base, end, offset, zero ? "data" : "text", file ? file : "");
438 s.name = zero ? "data" : "text";
439 s.file = strdup(file);
442 if(addseg(map, s) < 0){
444 ptrace(PTRACE_DETACH, pid, 0, 0);