Imported Upstream version 0.5.3
[platform/upstream/ltrace.git] / proc.c
1 #include <sys/types.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <stdlib.h>
6
7 #include "common.h"
8
9 Process *
10 open_program(char *filename, pid_t pid) {
11         Process *proc;
12         proc = calloc(sizeof(Process), 1);
13         if (!proc) {
14                 perror("malloc");
15                 exit(1);
16         }
17         proc->filename = strdup(filename);
18         proc->breakpoints_enabled = -1;
19         if (pid) {
20                 proc->pid = pid;
21         }
22         breakpoints_init(proc);
23
24         proc->next = list_of_processes;
25         list_of_processes = proc;
26         return proc;
27 }
28
29 void
30 open_pid(pid_t pid) {
31         Process *proc;
32         char *filename;
33
34         if (trace_pid(pid) < 0) {
35                 fprintf(stderr, "Cannot attach to pid %u: %s\n", pid,
36                         strerror(errno));
37                 return;
38         }
39
40         filename = pid2name(pid);
41
42         if (!filename) {
43                 fprintf(stderr, "Cannot trace pid %u: %s\n", pid,
44                                 strerror(errno));
45                 return;
46         }
47
48         proc = open_program(filename, pid);
49         continue_process(pid);
50         proc->breakpoints_enabled = 1;
51 }
52
53 Process *
54 pid2proc(pid_t pid) {
55         Process *tmp;
56
57         tmp = list_of_processes;
58         while (tmp) {
59                 if (pid == tmp->pid) {
60                         return tmp;
61                 }
62                 tmp = tmp->next;
63         }
64         return NULL;
65 }