blob: 4e9320bf11e19fb0f81327102a2b00cb46dc8fe2 [file] [log] [blame]
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001#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
10static struct syscall_fmt {
11 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030012 const char *alias;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030013 bool errmsg;
14 bool timeout;
15} syscall_fmts[] = {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030016 { .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", },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030026};
27
28static 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
34static 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
40struct syscall {
41 struct event_format *tp_format;
42 const char *name;
43 struct syscall_fmt *fmt;
44};
45
46struct trace {
47 int audit_machine;
48 struct {
49 int max;
50 struct syscall *table;
51 } syscalls;
52 struct perf_record_opts opts;
53};
54
Namhyung Kimf15eb532012-10-05 14:02:16 +090055static bool done = false;
56
57static void sig_handler(int sig __maybe_unused)
58{
59 done = true;
60}
61
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030062static int trace__read_syscall_info(struct trace *trace, int id)
63{
64 char tp_name[128];
65 struct syscall *sc;
66
67 if (id > trace->syscalls.max) {
68 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
69
70 if (nsyscalls == NULL)
71 return -1;
72
73 if (trace->syscalls.max != -1) {
74 memset(nsyscalls + trace->syscalls.max + 1, 0,
75 (id - trace->syscalls.max) * sizeof(*sc));
76 } else {
77 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
78 }
79
80 trace->syscalls.table = nsyscalls;
81 trace->syscalls.max = id;
82 }
83
84 sc = trace->syscalls.table + id;
85 sc->name = audit_syscall_to_name(id, trace->audit_machine);
86 if (sc->name == NULL)
87 return -1;
88
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030089 sc->fmt = syscall_fmt__find(sc->name);
90
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -030091 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
92 sc->tp_format = event_format__new("syscalls", tp_name);
93
94 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
95 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
96 sc->tp_format = event_format__new("syscalls", tp_name);
97 }
98
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030099 return sc->tp_format != NULL ? 0 : -1;
100}
101
102static size_t syscall__fprintf_args(struct syscall *sc, unsigned long *args, FILE *fp)
103{
104 int i = 0;
105 size_t printed = 0;
106
107 if (sc->tp_format != NULL) {
108 struct format_field *field;
109
110 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
111 printed += fprintf(fp, "%s%s: %ld", printed ? ", " : "",
112 field->name, args[i++]);
113 }
114 } else {
115 while (i < 6) {
116 printed += fprintf(fp, "%sarg%d: %ld", printed ? ", " : "", i, args[i]);
117 ++i;
118 }
119 }
120
121 return printed;
122}
123
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300124typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
125 struct perf_sample *sample);
126
127static struct syscall *trace__syscall_info(struct trace *trace,
128 struct perf_evsel *evsel,
129 struct perf_sample *sample)
130{
131 int id = perf_evsel__intval(evsel, sample, "id");
132
133 if (id < 0) {
134 printf("Invalid syscall %d id, skipping...\n", id);
135 return NULL;
136 }
137
138 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
139 trace__read_syscall_info(trace, id))
140 goto out_cant_read;
141
142 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
143 goto out_cant_read;
144
145 return &trace->syscalls.table[id];
146
147out_cant_read:
148 printf("Problems reading syscall %d information\n", id);
149 return NULL;
150}
151
152static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
153 struct perf_sample *sample)
154{
155 void *args;
156 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
157
158 if (sc == NULL)
159 return -1;
160
161 args = perf_evsel__rawptr(evsel, sample, "args");
162 if (args == NULL) {
163 printf("Problems reading syscall arguments\n");
164 return -1;
165 }
166
167 printf("%s(", sc->name);
168 syscall__fprintf_args(sc, args, stdout);
169
170 return 0;
171}
172
173static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
174 struct perf_sample *sample)
175{
176 int ret;
177 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
178
179 if (sc == NULL)
180 return -1;
181
182 ret = perf_evsel__intval(evsel, sample, "ret");
183
184 if (ret < 0 && sc->fmt && sc->fmt->errmsg) {
185 char bf[256];
186 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
187 *e = audit_errno_to_name(-ret);
188
189 printf(") = -1 %s %s", e, emsg);
190 } else if (ret == 0 && sc->fmt && sc->fmt->timeout)
191 printf(") = 0 Timeout");
192 else
193 printf(") = %d", ret);
194
195 putchar('\n');
196 return 0;
197}
198
Namhyung Kimf15eb532012-10-05 14:02:16 +0900199static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300200{
201 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300202 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300203 int err = -1, i, nr_events = 0, before;
Namhyung Kimf15eb532012-10-05 14:02:16 +0900204 const bool forks = argc > 0;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300205
206 if (evlist == NULL) {
207 printf("Not enough memory to run!\n");
208 goto out;
209 }
210
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300211 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
212 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
213 printf("Couldn't read the raw_syscalls tracepoints information!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300214 goto out_delete_evlist;
215 }
216
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300217 err = perf_evlist__create_maps(evlist, &trace->opts.target);
218 if (err < 0) {
219 printf("Problems parsing the target to trace, check your options!\n");
220 goto out_delete_evlist;
221 }
222
223 perf_evlist__config_attrs(evlist, &trace->opts);
224
Namhyung Kimf15eb532012-10-05 14:02:16 +0900225 signal(SIGCHLD, sig_handler);
226 signal(SIGINT, sig_handler);
227
228 if (forks) {
229 err = perf_evlist__prepare_workload(evlist, &trace->opts, argv);
230 if (err < 0) {
231 printf("Couldn't run the workload!\n");
232 goto out_delete_evlist;
233 }
234 }
235
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300236 err = perf_evlist__open(evlist);
237 if (err < 0) {
238 printf("Couldn't create the events: %s\n", strerror(errno));
239 goto out_delete_evlist;
240 }
241
242 err = perf_evlist__mmap(evlist, UINT_MAX, false);
243 if (err < 0) {
244 printf("Couldn't mmap the events: %s\n", strerror(errno));
245 goto out_delete_evlist;
246 }
247
248 perf_evlist__enable(evlist);
Namhyung Kimf15eb532012-10-05 14:02:16 +0900249
250 if (forks)
251 perf_evlist__start_workload(evlist);
252
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300253again:
254 before = nr_events;
255
256 for (i = 0; i < evlist->nr_mmaps; i++) {
257 union perf_event *event;
258
259 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
260 const u32 type = event->header.type;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300261 tracepoint_handler handler;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300262 struct perf_sample sample;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300263
264 ++nr_events;
265
266 switch (type) {
267 case PERF_RECORD_SAMPLE:
268 break;
269 case PERF_RECORD_LOST:
270 printf("LOST %" PRIu64 " events!\n", event->lost.lost);
271 continue;
272 default:
273 printf("Unexpected %s event, skipping...\n",
274 perf_event__name(type));
275 continue;
276 }
277
278 err = perf_evlist__parse_sample(evlist, event, &sample);
279 if (err) {
280 printf("Can't parse sample, err = %d, skipping...\n", err);
281 continue;
282 }
283
284 evsel = perf_evlist__id2evsel(evlist, sample.id);
285 if (evsel == NULL) {
286 printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
287 continue;
288 }
289
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300290 if (evlist->threads->map[0] == -1 || evlist->threads->nr > 1)
291 printf("%d ", sample.tid);
292
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -0300293 handler = evsel->handler.func;
294 handler(trace, evsel, &sample);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300295 }
296 }
297
Namhyung Kimf15eb532012-10-05 14:02:16 +0900298 if (nr_events == before) {
299 if (done)
300 goto out_delete_evlist;
301
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300302 poll(evlist->pollfd, evlist->nr_fds, -1);
Namhyung Kimf15eb532012-10-05 14:02:16 +0900303 }
304
305 if (done)
306 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300307
308 goto again;
309
310out_delete_evlist:
311 perf_evlist__delete(evlist);
312out:
313 return err;
314}
315
316int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
317{
318 const char * const trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +0900319 "perf trace [<options>] [<command>]",
320 "perf trace [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300321 NULL
322 };
323 struct trace trace = {
324 .audit_machine = audit_detect_machine(),
325 .syscalls = {
326 . max = -1,
327 },
328 .opts = {
329 .target = {
330 .uid = UINT_MAX,
331 .uses_mmap = true,
332 },
333 .user_freq = UINT_MAX,
334 .user_interval = ULLONG_MAX,
335 .no_delay = true,
336 .mmap_pages = 1024,
337 },
338 };
339 const struct option trace_options[] = {
340 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
341 "trace events on existing process id"),
342 OPT_STRING(0, "tid", &trace.opts.target.tid, "tid",
343 "trace events on existing thread id"),
344 OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide,
345 "system-wide collection from all CPUs"),
346 OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu",
347 "list of cpus to monitor"),
348 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
349 "child tasks do not inherit counters"),
350 OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages,
351 "number of mmap data pages"),
352 OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user",
353 "user to profile"),
354 OPT_END()
355 };
356 int err;
Namhyung Kim32caf0d2012-10-05 14:02:13 +0900357 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300358
359 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300360
Namhyung Kim32caf0d2012-10-05 14:02:13 +0900361 err = perf_target__validate(&trace.opts.target);
362 if (err) {
363 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
364 printf("%s", bf);
365 return err;
366 }
367
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300368 err = perf_target__parse_uid(&trace.opts.target);
369 if (err) {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300370 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
371 printf("%s", bf);
372 return err;
373 }
374
Namhyung Kimf15eb532012-10-05 14:02:16 +0900375 if (!argc && perf_target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +0900376 trace.opts.target.system_wide = true;
377
Namhyung Kimf15eb532012-10-05 14:02:16 +0900378 return trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300379}