]>
Commit | Line | Data |
---|---|---|
514f1c67 ACM |
1 | #include "builtin.h" |
2 | #include "util/evlist.h" | |
3 | #include "util/parse-options.h" | |
4 | #include "util/thread_map.h" | |
5 | #include "event-parse.h" | |
6 | ||
7 | #include <libaudit.h> | |
8 | #include <stdlib.h> | |
9 | ||
10 | static struct syscall_fmt { | |
11 | const char *name; | |
aec1930b | 12 | const char *alias; |
514f1c67 ACM |
13 | bool errmsg; |
14 | bool timeout; | |
15 | } syscall_fmts[] = { | |
aec1930b ACM |
16 | { .name = "arch_prctl", .errmsg = true, .alias = "prctl", }, |
17 | { .name = "fstat", .errmsg = true, .alias = "newfstat", }, | |
18 | { .name = "fstatat", .errmsg = true, .alias = "newfstatat", }, | |
19 | { .name = "futex", .errmsg = true, }, | |
20 | { .name = "poll", .errmsg = true, .timeout = true, }, | |
21 | { .name = "ppoll", .errmsg = true, .timeout = true, }, | |
22 | { .name = "read", .errmsg = true, }, | |
23 | { .name = "recvfrom", .errmsg = true, }, | |
24 | { .name = "select", .errmsg = true, .timeout = true, }, | |
25 | { .name = "stat", .errmsg = true, .alias = "newstat", }, | |
514f1c67 ACM |
26 | }; |
27 | ||
28 | static int syscall_fmt__cmp(const void *name, const void *fmtp) | |
29 | { | |
30 | const struct syscall_fmt *fmt = fmtp; | |
31 | return strcmp(name, fmt->name); | |
32 | } | |
33 | ||
34 | static struct syscall_fmt *syscall_fmt__find(const char *name) | |
35 | { | |
36 | const int nmemb = ARRAY_SIZE(syscall_fmts); | |
37 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); | |
38 | } | |
39 | ||
40 | struct syscall { | |
41 | struct event_format *tp_format; | |
42 | const char *name; | |
43 | struct syscall_fmt *fmt; | |
44 | }; | |
45 | ||
46 | struct trace { | |
47 | int audit_machine; | |
48 | struct { | |
49 | int max; | |
50 | struct syscall *table; | |
51 | } syscalls; | |
52 | struct perf_record_opts opts; | |
53 | }; | |
54 | ||
f15eb531 NK |
55 | static bool done = false; |
56 | ||
57 | static void sig_handler(int sig __maybe_unused) | |
58 | { | |
59 | done = true; | |
60 | } | |
61 | ||
514f1c67 ACM |
62 | static int trace__read_syscall_info(struct trace *trace, int id) |
63 | { | |
64 | char tp_name[128]; | |
65 | struct syscall *sc; | |
3a531260 ACM |
66 | const char *name = audit_syscall_to_name(id, trace->audit_machine); |
67 | ||
68 | if (name == NULL) | |
69 | return -1; | |
514f1c67 ACM |
70 | |
71 | if (id > trace->syscalls.max) { | |
72 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); | |
73 | ||
74 | if (nsyscalls == NULL) | |
75 | return -1; | |
76 | ||
77 | if (trace->syscalls.max != -1) { | |
78 | memset(nsyscalls + trace->syscalls.max + 1, 0, | |
79 | (id - trace->syscalls.max) * sizeof(*sc)); | |
80 | } else { | |
81 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); | |
82 | } | |
83 | ||
84 | trace->syscalls.table = nsyscalls; | |
85 | trace->syscalls.max = id; | |
86 | } | |
87 | ||
88 | sc = trace->syscalls.table + id; | |
3a531260 ACM |
89 | sc->name = name; |
90 | sc->fmt = syscall_fmt__find(sc->name); | |
514f1c67 | 91 | |
aec1930b | 92 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
514f1c67 | 93 | sc->tp_format = event_format__new("syscalls", tp_name); |
aec1930b ACM |
94 | |
95 | if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) { | |
96 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); | |
97 | sc->tp_format = event_format__new("syscalls", tp_name); | |
98 | } | |
514f1c67 ACM |
99 | |
100 | return sc->tp_format != NULL ? 0 : -1; | |
101 | } | |
102 | ||
103 | static size_t syscall__fprintf_args(struct syscall *sc, unsigned long *args, FILE *fp) | |
104 | { | |
105 | int i = 0; | |
106 | size_t printed = 0; | |
107 | ||
108 | if (sc->tp_format != NULL) { | |
109 | struct format_field *field; | |
110 | ||
111 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { | |
112 | printed += fprintf(fp, "%s%s: %ld", printed ? ", " : "", | |
113 | field->name, args[i++]); | |
114 | } | |
115 | } else { | |
116 | while (i < 6) { | |
117 | printed += fprintf(fp, "%sarg%d: %ld", printed ? ", " : "", i, args[i]); | |
118 | ++i; | |
119 | } | |
120 | } | |
121 | ||
122 | return printed; | |
123 | } | |
124 | ||
ba3d7dee ACM |
125 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
126 | struct perf_sample *sample); | |
127 | ||
128 | static struct syscall *trace__syscall_info(struct trace *trace, | |
129 | struct perf_evsel *evsel, | |
130 | struct perf_sample *sample) | |
131 | { | |
132 | int id = perf_evsel__intval(evsel, sample, "id"); | |
133 | ||
134 | if (id < 0) { | |
135 | printf("Invalid syscall %d id, skipping...\n", id); | |
136 | return NULL; | |
137 | } | |
138 | ||
139 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && | |
140 | trace__read_syscall_info(trace, id)) | |
141 | goto out_cant_read; | |
142 | ||
143 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) | |
144 | goto out_cant_read; | |
145 | ||
146 | return &trace->syscalls.table[id]; | |
147 | ||
148 | out_cant_read: | |
149 | printf("Problems reading syscall %d information\n", id); | |
150 | return NULL; | |
151 | } | |
152 | ||
153 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, | |
154 | struct perf_sample *sample) | |
155 | { | |
156 | void *args; | |
157 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); | |
158 | ||
159 | if (sc == NULL) | |
160 | return -1; | |
161 | ||
162 | args = perf_evsel__rawptr(evsel, sample, "args"); | |
163 | if (args == NULL) { | |
164 | printf("Problems reading syscall arguments\n"); | |
165 | return -1; | |
166 | } | |
167 | ||
168 | printf("%s(", sc->name); | |
169 | syscall__fprintf_args(sc, args, stdout); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, | |
175 | struct perf_sample *sample) | |
176 | { | |
177 | int ret; | |
178 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); | |
179 | ||
180 | if (sc == NULL) | |
181 | return -1; | |
182 | ||
183 | ret = perf_evsel__intval(evsel, sample, "ret"); | |
184 | ||
185 | if (ret < 0 && sc->fmt && sc->fmt->errmsg) { | |
186 | char bf[256]; | |
187 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), | |
188 | *e = audit_errno_to_name(-ret); | |
189 | ||
190 | printf(") = -1 %s %s", e, emsg); | |
191 | } else if (ret == 0 && sc->fmt && sc->fmt->timeout) | |
192 | printf(") = 0 Timeout"); | |
193 | else | |
194 | printf(") = %d", ret); | |
195 | ||
196 | putchar('\n'); | |
197 | return 0; | |
198 | } | |
199 | ||
f15eb531 | 200 | static int trace__run(struct trace *trace, int argc, const char **argv) |
514f1c67 ACM |
201 | { |
202 | struct perf_evlist *evlist = perf_evlist__new(NULL, NULL); | |
ba3d7dee | 203 | struct perf_evsel *evsel; |
514f1c67 | 204 | int err = -1, i, nr_events = 0, before; |
f15eb531 | 205 | const bool forks = argc > 0; |
514f1c67 ACM |
206 | |
207 | if (evlist == NULL) { | |
208 | printf("Not enough memory to run!\n"); | |
209 | goto out; | |
210 | } | |
211 | ||
39876e7d ACM |
212 | if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) || |
213 | perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) { | |
214 | printf("Couldn't read the raw_syscalls tracepoints information!\n"); | |
514f1c67 ACM |
215 | goto out_delete_evlist; |
216 | } | |
217 | ||
514f1c67 ACM |
218 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
219 | if (err < 0) { | |
220 | printf("Problems parsing the target to trace, check your options!\n"); | |
221 | goto out_delete_evlist; | |
222 | } | |
223 | ||
224 | perf_evlist__config_attrs(evlist, &trace->opts); | |
225 | ||
f15eb531 NK |
226 | signal(SIGCHLD, sig_handler); |
227 | signal(SIGINT, sig_handler); | |
228 | ||
229 | if (forks) { | |
230 | err = perf_evlist__prepare_workload(evlist, &trace->opts, argv); | |
231 | if (err < 0) { | |
232 | printf("Couldn't run the workload!\n"); | |
233 | goto out_delete_evlist; | |
234 | } | |
235 | } | |
236 | ||
514f1c67 ACM |
237 | err = perf_evlist__open(evlist); |
238 | if (err < 0) { | |
239 | printf("Couldn't create the events: %s\n", strerror(errno)); | |
240 | goto out_delete_evlist; | |
241 | } | |
242 | ||
243 | err = perf_evlist__mmap(evlist, UINT_MAX, false); | |
244 | if (err < 0) { | |
245 | printf("Couldn't mmap the events: %s\n", strerror(errno)); | |
246 | goto out_delete_evlist; | |
247 | } | |
248 | ||
249 | perf_evlist__enable(evlist); | |
f15eb531 NK |
250 | |
251 | if (forks) | |
252 | perf_evlist__start_workload(evlist); | |
253 | ||
514f1c67 ACM |
254 | again: |
255 | before = nr_events; | |
256 | ||
257 | for (i = 0; i < evlist->nr_mmaps; i++) { | |
258 | union perf_event *event; | |
259 | ||
260 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { | |
261 | const u32 type = event->header.type; | |
ba3d7dee | 262 | tracepoint_handler handler; |
514f1c67 | 263 | struct perf_sample sample; |
514f1c67 ACM |
264 | |
265 | ++nr_events; | |
266 | ||
267 | switch (type) { | |
268 | case PERF_RECORD_SAMPLE: | |
269 | break; | |
270 | case PERF_RECORD_LOST: | |
271 | printf("LOST %" PRIu64 " events!\n", event->lost.lost); | |
272 | continue; | |
273 | default: | |
274 | printf("Unexpected %s event, skipping...\n", | |
275 | perf_event__name(type)); | |
276 | continue; | |
277 | } | |
278 | ||
279 | err = perf_evlist__parse_sample(evlist, event, &sample); | |
280 | if (err) { | |
281 | printf("Can't parse sample, err = %d, skipping...\n", err); | |
282 | continue; | |
283 | } | |
284 | ||
285 | evsel = perf_evlist__id2evsel(evlist, sample.id); | |
286 | if (evsel == NULL) { | |
287 | printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id); | |
288 | continue; | |
289 | } | |
290 | ||
514f1c67 ACM |
291 | if (evlist->threads->map[0] == -1 || evlist->threads->nr > 1) |
292 | printf("%d ", sample.tid); | |
293 | ||
fc551f8d ACM |
294 | if (sample.raw_data == NULL) { |
295 | printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", | |
296 | perf_evsel__name(evsel), sample.tid, | |
297 | sample.cpu, sample.raw_size); | |
298 | continue; | |
299 | } | |
300 | ||
ba3d7dee ACM |
301 | handler = evsel->handler.func; |
302 | handler(trace, evsel, &sample); | |
514f1c67 ACM |
303 | } |
304 | } | |
305 | ||
f15eb531 NK |
306 | if (nr_events == before) { |
307 | if (done) | |
308 | goto out_delete_evlist; | |
309 | ||
514f1c67 | 310 | poll(evlist->pollfd, evlist->nr_fds, -1); |
f15eb531 NK |
311 | } |
312 | ||
313 | if (done) | |
314 | perf_evlist__disable(evlist); | |
514f1c67 ACM |
315 | |
316 | goto again; | |
317 | ||
318 | out_delete_evlist: | |
319 | perf_evlist__delete(evlist); | |
320 | out: | |
321 | return err; | |
322 | } | |
323 | ||
324 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) | |
325 | { | |
326 | const char * const trace_usage[] = { | |
f15eb531 NK |
327 | "perf trace [<options>] [<command>]", |
328 | "perf trace [<options>] -- <command> [<options>]", | |
514f1c67 ACM |
329 | NULL |
330 | }; | |
331 | struct trace trace = { | |
332 | .audit_machine = audit_detect_machine(), | |
333 | .syscalls = { | |
334 | . max = -1, | |
335 | }, | |
336 | .opts = { | |
337 | .target = { | |
338 | .uid = UINT_MAX, | |
339 | .uses_mmap = true, | |
340 | }, | |
341 | .user_freq = UINT_MAX, | |
342 | .user_interval = ULLONG_MAX, | |
343 | .no_delay = true, | |
344 | .mmap_pages = 1024, | |
345 | }, | |
346 | }; | |
347 | const struct option trace_options[] = { | |
348 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", | |
349 | "trace events on existing process id"), | |
350 | OPT_STRING(0, "tid", &trace.opts.target.tid, "tid", | |
351 | "trace events on existing thread id"), | |
352 | OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide, | |
353 | "system-wide collection from all CPUs"), | |
354 | OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu", | |
355 | "list of cpus to monitor"), | |
356 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, | |
357 | "child tasks do not inherit counters"), | |
358 | OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages, | |
359 | "number of mmap data pages"), | |
360 | OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user", | |
361 | "user to profile"), | |
362 | OPT_END() | |
363 | }; | |
364 | int err; | |
32caf0d1 | 365 | char bf[BUFSIZ]; |
514f1c67 ACM |
366 | |
367 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); | |
514f1c67 | 368 | |
32caf0d1 NK |
369 | err = perf_target__validate(&trace.opts.target); |
370 | if (err) { | |
371 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); | |
372 | printf("%s", bf); | |
373 | return err; | |
374 | } | |
375 | ||
514f1c67 ACM |
376 | err = perf_target__parse_uid(&trace.opts.target); |
377 | if (err) { | |
514f1c67 ACM |
378 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
379 | printf("%s", bf); | |
380 | return err; | |
381 | } | |
382 | ||
f15eb531 | 383 | if (!argc && perf_target__none(&trace.opts.target)) |
ee76120e NK |
384 | trace.opts.target.system_wide = true; |
385 | ||
f15eb531 | 386 | return trace__run(&trace, argc, argv); |
514f1c67 | 387 | } |