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 | ||
55 | static int trace__read_syscall_info(struct trace *trace, int id) | |
56 | { | |
57 | char tp_name[128]; | |
58 | struct syscall *sc; | |
59 | ||
60 | if (id > trace->syscalls.max) { | |
61 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); | |
62 | ||
63 | if (nsyscalls == NULL) | |
64 | return -1; | |
65 | ||
66 | if (trace->syscalls.max != -1) { | |
67 | memset(nsyscalls + trace->syscalls.max + 1, 0, | |
68 | (id - trace->syscalls.max) * sizeof(*sc)); | |
69 | } else { | |
70 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); | |
71 | } | |
72 | ||
73 | trace->syscalls.table = nsyscalls; | |
74 | trace->syscalls.max = id; | |
75 | } | |
76 | ||
77 | sc = trace->syscalls.table + id; | |
78 | sc->name = audit_syscall_to_name(id, trace->audit_machine); | |
79 | if (sc->name == NULL) | |
80 | return -1; | |
81 | ||
aec1930b | 82 | sc->fmt = syscall_fmt__find(sc->name); |
514f1c67 | 83 | |
aec1930b | 84 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
514f1c67 | 85 | sc->tp_format = event_format__new("syscalls", tp_name); |
aec1930b ACM |
86 | |
87 | if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) { | |
88 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); | |
89 | sc->tp_format = event_format__new("syscalls", tp_name); | |
90 | } | |
514f1c67 ACM |
91 | |
92 | return sc->tp_format != NULL ? 0 : -1; | |
93 | } | |
94 | ||
95 | static size_t syscall__fprintf_args(struct syscall *sc, unsigned long *args, FILE *fp) | |
96 | { | |
97 | int i = 0; | |
98 | size_t printed = 0; | |
99 | ||
100 | if (sc->tp_format != NULL) { | |
101 | struct format_field *field; | |
102 | ||
103 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { | |
104 | printed += fprintf(fp, "%s%s: %ld", printed ? ", " : "", | |
105 | field->name, args[i++]); | |
106 | } | |
107 | } else { | |
108 | while (i < 6) { | |
109 | printed += fprintf(fp, "%sarg%d: %ld", printed ? ", " : "", i, args[i]); | |
110 | ++i; | |
111 | } | |
112 | } | |
113 | ||
114 | return printed; | |
115 | } | |
116 | ||
ba3d7dee ACM |
117 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
118 | struct perf_sample *sample); | |
119 | ||
120 | static struct syscall *trace__syscall_info(struct trace *trace, | |
121 | struct perf_evsel *evsel, | |
122 | struct perf_sample *sample) | |
123 | { | |
124 | int id = perf_evsel__intval(evsel, sample, "id"); | |
125 | ||
126 | if (id < 0) { | |
127 | printf("Invalid syscall %d id, skipping...\n", id); | |
128 | return NULL; | |
129 | } | |
130 | ||
131 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && | |
132 | trace__read_syscall_info(trace, id)) | |
133 | goto out_cant_read; | |
134 | ||
135 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) | |
136 | goto out_cant_read; | |
137 | ||
138 | return &trace->syscalls.table[id]; | |
139 | ||
140 | out_cant_read: | |
141 | printf("Problems reading syscall %d information\n", id); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, | |
146 | struct perf_sample *sample) | |
147 | { | |
148 | void *args; | |
149 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); | |
150 | ||
151 | if (sc == NULL) | |
152 | return -1; | |
153 | ||
154 | args = perf_evsel__rawptr(evsel, sample, "args"); | |
155 | if (args == NULL) { | |
156 | printf("Problems reading syscall arguments\n"); | |
157 | return -1; | |
158 | } | |
159 | ||
160 | printf("%s(", sc->name); | |
161 | syscall__fprintf_args(sc, args, stdout); | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
166 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, | |
167 | struct perf_sample *sample) | |
168 | { | |
169 | int ret; | |
170 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); | |
171 | ||
172 | if (sc == NULL) | |
173 | return -1; | |
174 | ||
175 | ret = perf_evsel__intval(evsel, sample, "ret"); | |
176 | ||
177 | if (ret < 0 && sc->fmt && sc->fmt->errmsg) { | |
178 | char bf[256]; | |
179 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), | |
180 | *e = audit_errno_to_name(-ret); | |
181 | ||
182 | printf(") = -1 %s %s", e, emsg); | |
183 | } else if (ret == 0 && sc->fmt && sc->fmt->timeout) | |
184 | printf(") = 0 Timeout"); | |
185 | else | |
186 | printf(") = %d", ret); | |
187 | ||
188 | putchar('\n'); | |
189 | return 0; | |
190 | } | |
191 | ||
514f1c67 ACM |
192 | static int trace__run(struct trace *trace) |
193 | { | |
194 | struct perf_evlist *evlist = perf_evlist__new(NULL, NULL); | |
ba3d7dee | 195 | struct perf_evsel *evsel; |
514f1c67 ACM |
196 | int err = -1, i, nr_events = 0, before; |
197 | ||
198 | if (evlist == NULL) { | |
199 | printf("Not enough memory to run!\n"); | |
200 | goto out; | |
201 | } | |
202 | ||
ba3d7dee ACM |
203 | evsel = perf_evsel__newtp("raw_syscalls", "sys_enter", 0); |
204 | if (evsel == NULL) { | |
514f1c67 ACM |
205 | printf("Couldn't read the raw_syscalls:sys_enter tracepoint information!\n"); |
206 | goto out_delete_evlist; | |
207 | } | |
208 | ||
ba3d7dee ACM |
209 | evsel->handler.func = trace__sys_enter; |
210 | perf_evlist__add(evlist, evsel); | |
514f1c67 | 211 | |
ba3d7dee ACM |
212 | evsel = perf_evsel__newtp("raw_syscalls", "sys_exit", 1); |
213 | if (evsel == NULL) { | |
514f1c67 ACM |
214 | printf("Couldn't read the raw_syscalls:sys_exit tracepoint information!\n"); |
215 | goto out_delete_evlist; | |
216 | } | |
217 | ||
ba3d7dee ACM |
218 | evsel->handler.func = trace__sys_exit; |
219 | perf_evlist__add(evlist, evsel); | |
514f1c67 ACM |
220 | |
221 | err = perf_evlist__create_maps(evlist, &trace->opts.target); | |
222 | if (err < 0) { | |
223 | printf("Problems parsing the target to trace, check your options!\n"); | |
224 | goto out_delete_evlist; | |
225 | } | |
226 | ||
227 | perf_evlist__config_attrs(evlist, &trace->opts); | |
228 | ||
229 | err = perf_evlist__open(evlist); | |
230 | if (err < 0) { | |
231 | printf("Couldn't create the events: %s\n", strerror(errno)); | |
232 | goto out_delete_evlist; | |
233 | } | |
234 | ||
235 | err = perf_evlist__mmap(evlist, UINT_MAX, false); | |
236 | if (err < 0) { | |
237 | printf("Couldn't mmap the events: %s\n", strerror(errno)); | |
238 | goto out_delete_evlist; | |
239 | } | |
240 | ||
241 | perf_evlist__enable(evlist); | |
242 | again: | |
243 | before = nr_events; | |
244 | ||
245 | for (i = 0; i < evlist->nr_mmaps; i++) { | |
246 | union perf_event *event; | |
247 | ||
248 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { | |
249 | const u32 type = event->header.type; | |
ba3d7dee | 250 | tracepoint_handler handler; |
514f1c67 | 251 | struct perf_sample sample; |
514f1c67 ACM |
252 | |
253 | ++nr_events; | |
254 | ||
255 | switch (type) { | |
256 | case PERF_RECORD_SAMPLE: | |
257 | break; | |
258 | case PERF_RECORD_LOST: | |
259 | printf("LOST %" PRIu64 " events!\n", event->lost.lost); | |
260 | continue; | |
261 | default: | |
262 | printf("Unexpected %s event, skipping...\n", | |
263 | perf_event__name(type)); | |
264 | continue; | |
265 | } | |
266 | ||
267 | err = perf_evlist__parse_sample(evlist, event, &sample); | |
268 | if (err) { | |
269 | printf("Can't parse sample, err = %d, skipping...\n", err); | |
270 | continue; | |
271 | } | |
272 | ||
273 | evsel = perf_evlist__id2evsel(evlist, sample.id); | |
274 | if (evsel == NULL) { | |
275 | printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id); | |
276 | continue; | |
277 | } | |
278 | ||
514f1c67 ACM |
279 | if (evlist->threads->map[0] == -1 || evlist->threads->nr > 1) |
280 | printf("%d ", sample.tid); | |
281 | ||
ba3d7dee ACM |
282 | handler = evsel->handler.func; |
283 | handler(trace, evsel, &sample); | |
514f1c67 ACM |
284 | } |
285 | } | |
286 | ||
287 | if (nr_events == before) | |
288 | poll(evlist->pollfd, evlist->nr_fds, -1); | |
289 | ||
290 | goto again; | |
291 | ||
292 | out_delete_evlist: | |
293 | perf_evlist__delete(evlist); | |
294 | out: | |
295 | return err; | |
296 | } | |
297 | ||
298 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) | |
299 | { | |
300 | const char * const trace_usage[] = { | |
301 | "perf trace [<options>]", | |
302 | NULL | |
303 | }; | |
304 | struct trace trace = { | |
305 | .audit_machine = audit_detect_machine(), | |
306 | .syscalls = { | |
307 | . max = -1, | |
308 | }, | |
309 | .opts = { | |
310 | .target = { | |
311 | .uid = UINT_MAX, | |
312 | .uses_mmap = true, | |
313 | }, | |
314 | .user_freq = UINT_MAX, | |
315 | .user_interval = ULLONG_MAX, | |
316 | .no_delay = true, | |
317 | .mmap_pages = 1024, | |
318 | }, | |
319 | }; | |
320 | const struct option trace_options[] = { | |
321 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", | |
322 | "trace events on existing process id"), | |
323 | OPT_STRING(0, "tid", &trace.opts.target.tid, "tid", | |
324 | "trace events on existing thread id"), | |
325 | OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide, | |
326 | "system-wide collection from all CPUs"), | |
327 | OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu", | |
328 | "list of cpus to monitor"), | |
329 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, | |
330 | "child tasks do not inherit counters"), | |
331 | OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages, | |
332 | "number of mmap data pages"), | |
333 | OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user", | |
334 | "user to profile"), | |
335 | OPT_END() | |
336 | }; | |
337 | int err; | |
338 | ||
339 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); | |
340 | if (argc) | |
341 | usage_with_options(trace_usage, trace_options); | |
342 | ||
343 | err = perf_target__parse_uid(&trace.opts.target); | |
344 | if (err) { | |
345 | char bf[BUFSIZ]; | |
346 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); | |
347 | printf("%s", bf); | |
348 | return err; | |
349 | } | |
350 | ||
351 | return trace__run(&trace); | |
352 | } |