-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpid.c
More file actions
93 lines (76 loc) · 1.56 KB
/
Copy pathpid.c
File metadata and controls
93 lines (76 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (C) 2011-2012 IITiS PAN Gliwice <http://www.iitis.pl/>
* Author: Paweł Foremski <pjf@iitis.pl>
* Licensed under GNU GPL v. 3
*/
#include "tracedump.h"
struct pid *pid_get(struct tracedump *td, pid_t pid)
{
struct pid *sp;
/* speed-up cache */
if (td->sp && td->sp->pid == pid)
return td->sp;
sp = thash_uint_get(td->pids, pid);
if (!sp) {
dbg(7, "new pid %d\n", pid);
sp = mmatic_zalloc(td->mm, sizeof *sp);
thash_uint_set(td->pids, pid, sp);
sp->td = td;
sp->pid = pid;
}
return sp;
}
void pid_del(struct tracedump *td, pid_t pid)
{
dbg(7, "deleting pid %d\n", pid);
thash_uint_set(td->pids, pid, NULL);
}
void pid_detach_all(struct tracedump *td)
{
struct pid *sp;
thash_reset(td->pids);
while ((sp = thash_uint_iter(td->pids, NULL))) {
ptrace_detach(sp, 0);
pid_del(td, sp->pid);
}
}
int pid_tgid(pid_t pid)
{
char buf[256];
FILE *fp;
int ret = 0;
snprintf(buf, sizeof buf, "/proc/%d/status", pid);
fp = fopen(buf, "r");
if (!fp) {
dbg(1, "fopen(%s): %s\n", buf, strerror(errno));
return 0;
}
while (fgets(buf, sizeof buf, fp)) {
if (strncmp("Tgid:", buf, 5) == 0) {
ret = atoi(buf+6);
break;
}
}
fclose(fp);
return ret;
}
char *pid_state(pid_t pid)
{
static char buf[256];
FILE *fp;
char *ret = NULL;
snprintf(buf, sizeof buf, "/proc/%d/status", pid);
fp = fopen(buf, "r");
if (!fp) {
dbg(1, "fopen(%s): %s\n", buf, strerror(errno));
return ret;
}
while (fgets(buf, sizeof buf, fp)) {
if (strncmp("State:", buf, 5) == 0) {
ret = buf+6;
break;
}
}
fclose(fp);
return ret;
}