proc.c revision ab3b72cc5d2d3efb3542192f0d72ff2ea4b082f9
1#include "config.h"
2
3#if defined(HAVE_LIBUNWIND)
4#include <libunwind.h>
5#include <libunwind-ptrace.h>
6#endif /* defined(HAVE_LIBUNWIND) */
7
8#include <sys/types.h>
9#include <string.h>
10#include <stdio.h>
11#include <errno.h>
12#include <stdlib.h>
13
14#include "common.h"
15
16Process *
17open_program(char *filename, pid_t pid) {
18	Process *proc;
19	proc = calloc(sizeof(Process), 1);
20	if (!proc) {
21		perror("malloc");
22		exit(1);
23	}
24	proc->filename = strdup(filename);
25	proc->breakpoints_enabled = -1;
26	if (pid) {
27		proc->pid = pid;
28#if defined(HAVE_LIBUNWIND)
29		proc->unwind_priv = _UPT_create(pid);
30	} else {
31		proc->unwind_priv = NULL;
32#endif /* defined(HAVE_LIBUNWIND) */
33	}
34
35	breakpoints_init(proc);
36
37	proc->next = list_of_processes;
38	list_of_processes = proc;
39
40#if defined(HAVE_LIBUNWIND)
41	proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
42#endif /* defined(HAVE_LIBUNWIND) */
43	return proc;
44}
45
46void
47open_pid(pid_t pid) {
48	Process *proc;
49	char *filename;
50
51	if (trace_pid(pid) < 0) {
52		fprintf(stderr, "Cannot attach to pid %u: %s\n", pid,
53			strerror(errno));
54		return;
55	}
56
57	filename = pid2name(pid);
58
59	if (!filename) {
60		fprintf(stderr, "Cannot trace pid %u: %s\n", pid,
61				strerror(errno));
62		return;
63	}
64
65	proc = open_program(filename, pid);
66	continue_process(pid);
67	proc->breakpoints_enabled = 1;
68}
69
70Process *
71pid2proc(pid_t pid) {
72	Process *tmp;
73
74	tmp = list_of_processes;
75	while (tmp) {
76		if (pid == tmp->pid) {
77			return tmp;
78		}
79		tmp = tmp->next;
80	}
81	return NULL;
82}
83