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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include <linux/types.h>
#include "event.h"
#include "debug.h"
#include "string.h"
static pid_t event__synthesize_comm(pid_t pid, int full,
int (*process)(event_t *event))
{
event_t ev;
char filename[PATH_MAX];
char bf[BUFSIZ];
FILE *fp;
size_t size = 0;
DIR *tasks;
struct dirent dirent, *next;
pid_t tgid = 0;
snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
fp = fopen(filename, "r");
if (fp == NULL) {
out_race:
/*
* We raced with a task exiting - just return:
*/
pr_debug("couldn't open %s\n", filename);
return 0;
}
memset(&ev.comm, 0, sizeof(ev.comm));
while (!ev.comm.comm[0] || !ev.comm.pid) {
if (fgets(bf, sizeof(bf), fp) == NULL)
goto out_failure;
if (memcmp(bf, "Name:", 5) == 0) {
char *name = bf + 5;
while (*name && isspace(*name))
++name;
size = strlen(name) - 1;
memcpy(ev.comm.comm, name, size++);
} else if (memcmp(bf, "Tgid:", 5) == 0) {
char *tgids = bf + 5;
while (*tgids && isspace(*tgids))
++tgids;
tgid = ev.comm.pid = atoi(tgids);
}
}
ev.comm.header.type = PERF_RECORD_COMM;
size = ALIGN(size, sizeof(u64));
ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
if (!full) {
ev.comm.tid = pid;
process(&ev);
goto out_fclose;
}
snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
tasks = opendir(filename);
if (tasks == NULL)
goto out_race;
while (!readdir_r(tasks, &dirent, &next) && next) {
char *end;
pid = strtol(dirent.d_name, &end, 10);
if (*end)
continue;
ev.comm.tid = pid;
process(&ev);
}
closedir(tasks);
out_fclose:
fclose(fp);
return tgid;
out_failure:
pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
return -1;
}
static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
int (*process)(event_t *event))
{
char filename[PATH_MAX];
FILE *fp;
snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
fp = fopen(filename, "r");
if (fp == NULL) {
/*
* We raced with a task exiting - just return:
*/
pr_debug("couldn't open %s\n", filename);
return -1;
}
while (1) {
char bf[BUFSIZ], *pbf = bf;
event_t ev = {
.header = { .type = PERF_RECORD_MMAP },
};
int n;
size_t size;
if (fgets(bf, sizeof(bf), fp) == NULL)
break;
/* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
n = hex2u64(pbf, &ev.mmap.start);
if (n < 0)
continue;
pbf += n + 1;
n = hex2u64(pbf, &ev.mmap.len);
if (n < 0)
continue;
pbf += n + 3;
if (*pbf == 'x') { /* vm_exec */
char *execname = strchr(bf, '/');
/* Catch VDSO */
if (execname == NULL)
execname = strstr(bf, "[vdso]");
if (execname == NULL)
continue;
size = strlen(execname);
execname[size - 1] = '\0'; /* Remove \n */
memcpy(ev.mmap.filename, execname, size);
size = ALIGN(size, sizeof(u64));
ev.mmap.len -= ev.mmap.start;
ev.mmap.header.size = (sizeof(ev.mmap) -
(sizeof(ev.mmap.filename) - size));
ev.mmap.pid = tgid;
ev.mmap.tid = pid;
process(&ev);
}
}
fclose(fp);
return 0;
}
int event__synthesize_thread(pid_t pid, int (*process)(event_t *event))
{
pid_t tgid = event__synthesize_comm(pid, 1, process);
if (tgid == -1)
return -1;
return event__synthesize_mmap_events(pid, tgid, process);
}
void event__synthesize_threads(int (*process)(event_t *event))
{
DIR *proc;
struct dirent dirent, *next;
proc = opendir("/proc");
while (!readdir_r(proc, &dirent, &next) && next) {
char *end;
pid_t pid = strtol(dirent.d_name, &end, 10);
if (*end) /* only interested in proper numerical dirents */
continue;
event__synthesize_thread(pid, process);
}
closedir(proc);
}
|