Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1 | #include "builtin.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 2 | #include "util/color.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 4 | #include "util/machine.h" |
| 5 | #include "util/thread.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 6 | #include "util/parse-options.h" |
| 7 | #include "util/thread_map.h" |
| 8 | #include "event-parse.h" |
| 9 | |
| 10 | #include <libaudit.h> |
| 11 | #include <stdlib.h> |
| 12 | |
| 13 | static struct syscall_fmt { |
| 14 | const char *name; |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 15 | const char *alias; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 16 | bool errmsg; |
| 17 | bool timeout; |
| 18 | } syscall_fmts[] = { |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame^] | 19 | { .name = "access", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 20 | { .name = "arch_prctl", .errmsg = true, .alias = "prctl", }, |
| 21 | { .name = "fstat", .errmsg = true, .alias = "newfstat", }, |
| 22 | { .name = "fstatat", .errmsg = true, .alias = "newfstatat", }, |
| 23 | { .name = "futex", .errmsg = true, }, |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame^] | 24 | { .name = "open", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 25 | { .name = "poll", .errmsg = true, .timeout = true, }, |
| 26 | { .name = "ppoll", .errmsg = true, .timeout = true, }, |
| 27 | { .name = "read", .errmsg = true, }, |
| 28 | { .name = "recvfrom", .errmsg = true, }, |
| 29 | { .name = "select", .errmsg = true, .timeout = true, }, |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame^] | 30 | { .name = "socket", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 31 | { .name = "stat", .errmsg = true, .alias = "newstat", }, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static int syscall_fmt__cmp(const void *name, const void *fmtp) |
| 35 | { |
| 36 | const struct syscall_fmt *fmt = fmtp; |
| 37 | return strcmp(name, fmt->name); |
| 38 | } |
| 39 | |
| 40 | static struct syscall_fmt *syscall_fmt__find(const char *name) |
| 41 | { |
| 42 | const int nmemb = ARRAY_SIZE(syscall_fmts); |
| 43 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); |
| 44 | } |
| 45 | |
| 46 | struct syscall { |
| 47 | struct event_format *tp_format; |
| 48 | const char *name; |
| 49 | struct syscall_fmt *fmt; |
| 50 | }; |
| 51 | |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 52 | static size_t fprintf_duration(unsigned long t, FILE *fp) |
| 53 | { |
| 54 | double duration = (double)t / NSEC_PER_MSEC; |
| 55 | size_t printed = fprintf(fp, "("); |
| 56 | |
| 57 | if (duration >= 1.0) |
| 58 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); |
| 59 | else if (duration >= 0.01) |
| 60 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); |
| 61 | else |
| 62 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); |
| 63 | return printed + fprintf(stdout, "): "); |
| 64 | } |
| 65 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 66 | struct thread_trace { |
| 67 | u64 entry_time; |
| 68 | u64 exit_time; |
| 69 | bool entry_pending; |
| 70 | char *entry_str; |
| 71 | }; |
| 72 | |
| 73 | static struct thread_trace *thread_trace__new(void) |
| 74 | { |
| 75 | return zalloc(sizeof(struct thread_trace)); |
| 76 | } |
| 77 | |
| 78 | static struct thread_trace *thread__trace(struct thread *thread) |
| 79 | { |
| 80 | if (thread == NULL) |
| 81 | goto fail; |
| 82 | |
| 83 | if (thread->priv == NULL) |
| 84 | thread->priv = thread_trace__new(); |
| 85 | |
| 86 | if (thread->priv == NULL) |
| 87 | goto fail; |
| 88 | |
| 89 | return thread->priv; |
| 90 | fail: |
| 91 | color_fprintf(stdout, PERF_COLOR_RED, |
| 92 | "WARNING: not enough memory, dropping samples!\n"); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 96 | struct trace { |
| 97 | int audit_machine; |
| 98 | struct { |
| 99 | int max; |
| 100 | struct syscall *table; |
| 101 | } syscalls; |
| 102 | struct perf_record_opts opts; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 103 | struct machine host; |
| 104 | u64 base_time; |
| 105 | bool multiple_threads; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 106 | double duration_filter; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 107 | }; |
| 108 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 109 | static bool trace__filter_duration(struct trace *trace, double t) |
| 110 | { |
| 111 | return t < (trace->duration_filter * NSEC_PER_MSEC); |
| 112 | } |
| 113 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 114 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
| 115 | { |
| 116 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; |
| 117 | |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 118 | return fprintf(fp, "%10.3f ", ts); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 119 | } |
| 120 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 121 | static bool done = false; |
| 122 | |
| 123 | static void sig_handler(int sig __maybe_unused) |
| 124 | { |
| 125 | done = true; |
| 126 | } |
| 127 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 128 | static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 129 | u64 duration, u64 tstamp, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 130 | { |
| 131 | size_t printed = trace__fprintf_tstamp(trace, tstamp, fp); |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 132 | printed += fprintf_duration(duration, fp); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 133 | |
| 134 | if (trace->multiple_threads) |
| 135 | printed += fprintf(fp, "%d ", thread->pid); |
| 136 | |
| 137 | return printed; |
| 138 | } |
| 139 | |
| 140 | static int trace__process_event(struct machine *machine, union perf_event *event) |
| 141 | { |
| 142 | int ret = 0; |
| 143 | |
| 144 | switch (event->header.type) { |
| 145 | case PERF_RECORD_LOST: |
| 146 | color_fprintf(stdout, PERF_COLOR_RED, |
| 147 | "LOST %" PRIu64 " events!\n", event->lost.lost); |
| 148 | ret = machine__process_lost_event(machine, event); |
| 149 | default: |
| 150 | ret = machine__process_event(machine, event); |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int trace__tool_process(struct perf_tool *tool __maybe_unused, |
| 158 | union perf_event *event, |
| 159 | struct perf_sample *sample __maybe_unused, |
| 160 | struct machine *machine) |
| 161 | { |
| 162 | return trace__process_event(machine, event); |
| 163 | } |
| 164 | |
| 165 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) |
| 166 | { |
| 167 | int err = symbol__init(); |
| 168 | |
| 169 | if (err) |
| 170 | return err; |
| 171 | |
| 172 | machine__init(&trace->host, "", HOST_KERNEL_ID); |
| 173 | machine__create_kernel_maps(&trace->host); |
| 174 | |
| 175 | if (perf_target__has_task(&trace->opts.target)) { |
| 176 | err = perf_event__synthesize_thread_map(NULL, evlist->threads, |
| 177 | trace__tool_process, |
| 178 | &trace->host); |
| 179 | } else { |
| 180 | err = perf_event__synthesize_threads(NULL, trace__tool_process, |
| 181 | &trace->host); |
| 182 | } |
| 183 | |
| 184 | if (err) |
| 185 | symbol__exit(); |
| 186 | |
| 187 | return err; |
| 188 | } |
| 189 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 190 | static int trace__read_syscall_info(struct trace *trace, int id) |
| 191 | { |
| 192 | char tp_name[128]; |
| 193 | struct syscall *sc; |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 194 | const char *name = audit_syscall_to_name(id, trace->audit_machine); |
| 195 | |
| 196 | if (name == NULL) |
| 197 | return -1; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 198 | |
| 199 | if (id > trace->syscalls.max) { |
| 200 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); |
| 201 | |
| 202 | if (nsyscalls == NULL) |
| 203 | return -1; |
| 204 | |
| 205 | if (trace->syscalls.max != -1) { |
| 206 | memset(nsyscalls + trace->syscalls.max + 1, 0, |
| 207 | (id - trace->syscalls.max) * sizeof(*sc)); |
| 208 | } else { |
| 209 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); |
| 210 | } |
| 211 | |
| 212 | trace->syscalls.table = nsyscalls; |
| 213 | trace->syscalls.max = id; |
| 214 | } |
| 215 | |
| 216 | sc = trace->syscalls.table + id; |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 217 | sc->name = name; |
| 218 | sc->fmt = syscall_fmt__find(sc->name); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 219 | |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 220 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
| 221 | sc->tp_format = event_format__new("syscalls", tp_name); |
| 222 | |
| 223 | if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) { |
| 224 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); |
| 225 | sc->tp_format = event_format__new("syscalls", tp_name); |
| 226 | } |
| 227 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 228 | return sc->tp_format != NULL ? 0 : -1; |
| 229 | } |
| 230 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 231 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
| 232 | unsigned long *args) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 233 | { |
| 234 | int i = 0; |
| 235 | size_t printed = 0; |
| 236 | |
| 237 | if (sc->tp_format != NULL) { |
| 238 | struct format_field *field; |
| 239 | |
| 240 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 241 | printed += scnprintf(bf + printed, size - printed, |
| 242 | "%s%s: %ld", printed ? ", " : "", |
| 243 | field->name, args[i++]); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 244 | } |
| 245 | } else { |
| 246 | while (i < 6) { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 247 | printed += scnprintf(bf + printed, size - printed, |
| 248 | "%sarg%d: %ld", |
| 249 | printed ? ", " : "", i, args[i]); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 250 | ++i; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return printed; |
| 255 | } |
| 256 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 257 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
| 258 | struct perf_sample *sample); |
| 259 | |
| 260 | static struct syscall *trace__syscall_info(struct trace *trace, |
| 261 | struct perf_evsel *evsel, |
| 262 | struct perf_sample *sample) |
| 263 | { |
| 264 | int id = perf_evsel__intval(evsel, sample, "id"); |
| 265 | |
| 266 | if (id < 0) { |
| 267 | printf("Invalid syscall %d id, skipping...\n", id); |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && |
| 272 | trace__read_syscall_info(trace, id)) |
| 273 | goto out_cant_read; |
| 274 | |
| 275 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) |
| 276 | goto out_cant_read; |
| 277 | |
| 278 | return &trace->syscalls.table[id]; |
| 279 | |
| 280 | out_cant_read: |
| 281 | printf("Problems reading syscall %d information\n", id); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, |
| 286 | struct perf_sample *sample) |
| 287 | { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 288 | char *msg; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 289 | void *args; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 290 | size_t printed = 0; |
| 291 | struct thread *thread = machine__findnew_thread(&trace->host, sample->tid); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 292 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 293 | struct thread_trace *ttrace = thread__trace(thread); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 294 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 295 | if (ttrace == NULL || sc == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 296 | return -1; |
| 297 | |
| 298 | args = perf_evsel__rawptr(evsel, sample, "args"); |
| 299 | if (args == NULL) { |
| 300 | printf("Problems reading syscall arguments\n"); |
| 301 | return -1; |
| 302 | } |
| 303 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 304 | ttrace = thread->priv; |
| 305 | |
| 306 | if (ttrace->entry_str == NULL) { |
| 307 | ttrace->entry_str = malloc(1024); |
| 308 | if (!ttrace->entry_str) |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | ttrace->entry_time = sample->time; |
| 313 | msg = ttrace->entry_str; |
| 314 | printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name); |
| 315 | |
| 316 | printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args); |
| 317 | |
| 318 | if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) { |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 319 | if (!trace->duration_filter) { |
| 320 | trace__fprintf_entry_head(trace, thread, 1, sample->time, stdout); |
| 321 | printf("%-70s\n", ttrace->entry_str); |
| 322 | } |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 323 | } else |
| 324 | ttrace->entry_pending = true; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, |
| 330 | struct perf_sample *sample) |
| 331 | { |
| 332 | int ret; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 333 | u64 duration = 0; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 334 | struct thread *thread = machine__findnew_thread(&trace->host, sample->tid); |
| 335 | struct thread_trace *ttrace = thread__trace(thread); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 336 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
| 337 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 338 | if (ttrace == NULL || sc == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 339 | return -1; |
| 340 | |
| 341 | ret = perf_evsel__intval(evsel, sample, "ret"); |
| 342 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 343 | ttrace = thread->priv; |
| 344 | |
| 345 | ttrace->exit_time = sample->time; |
| 346 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 347 | if (ttrace->entry_time) { |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 348 | duration = sample->time - ttrace->entry_time; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 349 | if (trace__filter_duration(trace, duration)) |
| 350 | goto out; |
| 351 | } else if (trace->duration_filter) |
| 352 | goto out; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 353 | |
| 354 | trace__fprintf_entry_head(trace, thread, duration, sample->time, stdout); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 355 | |
| 356 | if (ttrace->entry_pending) { |
| 357 | printf("%-70s", ttrace->entry_str); |
| 358 | } else { |
| 359 | printf(" ... ["); |
| 360 | color_fprintf(stdout, PERF_COLOR_YELLOW, "continued"); |
| 361 | printf("]: %s()", sc->name); |
| 362 | } |
| 363 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 364 | if (ret < 0 && sc->fmt && sc->fmt->errmsg) { |
| 365 | char bf[256]; |
| 366 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), |
| 367 | *e = audit_errno_to_name(-ret); |
| 368 | |
| 369 | printf(") = -1 %s %s", e, emsg); |
| 370 | } else if (ret == 0 && sc->fmt && sc->fmt->timeout) |
| 371 | printf(") = 0 Timeout"); |
| 372 | else |
| 373 | printf(") = %d", ret); |
| 374 | |
| 375 | putchar('\n'); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 376 | out: |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 377 | ttrace->entry_pending = false; |
| 378 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 382 | static int trace__run(struct trace *trace, int argc, const char **argv) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 383 | { |
| 384 | struct perf_evlist *evlist = perf_evlist__new(NULL, NULL); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 385 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 386 | int err = -1, i, nr_events = 0, before; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 387 | const bool forks = argc > 0; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 388 | |
| 389 | if (evlist == NULL) { |
| 390 | printf("Not enough memory to run!\n"); |
| 391 | goto out; |
| 392 | } |
| 393 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 394 | if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) || |
| 395 | perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) { |
| 396 | printf("Couldn't read the raw_syscalls tracepoints information!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 397 | goto out_delete_evlist; |
| 398 | } |
| 399 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 400 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
| 401 | if (err < 0) { |
| 402 | printf("Problems parsing the target to trace, check your options!\n"); |
| 403 | goto out_delete_evlist; |
| 404 | } |
| 405 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 406 | err = trace__symbols_init(trace, evlist); |
| 407 | if (err < 0) { |
| 408 | printf("Problems initializing symbol libraries!\n"); |
| 409 | goto out_delete_evlist; |
| 410 | } |
| 411 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 412 | perf_evlist__config_attrs(evlist, &trace->opts); |
| 413 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 414 | signal(SIGCHLD, sig_handler); |
| 415 | signal(SIGINT, sig_handler); |
| 416 | |
| 417 | if (forks) { |
| 418 | err = perf_evlist__prepare_workload(evlist, &trace->opts, argv); |
| 419 | if (err < 0) { |
| 420 | printf("Couldn't run the workload!\n"); |
| 421 | goto out_delete_evlist; |
| 422 | } |
| 423 | } |
| 424 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 425 | err = perf_evlist__open(evlist); |
| 426 | if (err < 0) { |
| 427 | printf("Couldn't create the events: %s\n", strerror(errno)); |
| 428 | goto out_delete_evlist; |
| 429 | } |
| 430 | |
| 431 | err = perf_evlist__mmap(evlist, UINT_MAX, false); |
| 432 | if (err < 0) { |
| 433 | printf("Couldn't mmap the events: %s\n", strerror(errno)); |
| 434 | goto out_delete_evlist; |
| 435 | } |
| 436 | |
| 437 | perf_evlist__enable(evlist); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 438 | |
| 439 | if (forks) |
| 440 | perf_evlist__start_workload(evlist); |
| 441 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 442 | trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 443 | again: |
| 444 | before = nr_events; |
| 445 | |
| 446 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 447 | union perf_event *event; |
| 448 | |
| 449 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
| 450 | const u32 type = event->header.type; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 451 | tracepoint_handler handler; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 452 | struct perf_sample sample; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 453 | |
| 454 | ++nr_events; |
| 455 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 456 | err = perf_evlist__parse_sample(evlist, event, &sample); |
| 457 | if (err) { |
| 458 | printf("Can't parse sample, err = %d, skipping...\n", err); |
| 459 | continue; |
| 460 | } |
| 461 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 462 | if (trace->base_time == 0) |
| 463 | trace->base_time = sample.time; |
| 464 | |
| 465 | if (type != PERF_RECORD_SAMPLE) { |
| 466 | trace__process_event(&trace->host, event); |
| 467 | continue; |
| 468 | } |
| 469 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 470 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
| 471 | if (evsel == NULL) { |
| 472 | printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id); |
| 473 | continue; |
| 474 | } |
| 475 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 476 | if (sample.raw_data == NULL) { |
| 477 | printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", |
| 478 | perf_evsel__name(evsel), sample.tid, |
| 479 | sample.cpu, sample.raw_size); |
| 480 | continue; |
| 481 | } |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 482 | |
Arnaldo Carvalho de Melo | fc551f8 | 2012-10-20 13:08:46 -0300 | [diff] [blame] | 483 | if (sample.raw_data == NULL) { |
| 484 | printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", |
| 485 | perf_evsel__name(evsel), sample.tid, |
| 486 | sample.cpu, sample.raw_size); |
| 487 | continue; |
| 488 | } |
| 489 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 490 | handler = evsel->handler.func; |
| 491 | handler(trace, evsel, &sample); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 495 | if (nr_events == before) { |
| 496 | if (done) |
| 497 | goto out_delete_evlist; |
| 498 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 499 | poll(evlist->pollfd, evlist->nr_fds, -1); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | if (done) |
| 503 | perf_evlist__disable(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 504 | |
| 505 | goto again; |
| 506 | |
| 507 | out_delete_evlist: |
| 508 | perf_evlist__delete(evlist); |
| 509 | out: |
| 510 | return err; |
| 511 | } |
| 512 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 513 | static int trace__set_duration(const struct option *opt, const char *str, |
| 514 | int unset __maybe_unused) |
| 515 | { |
| 516 | struct trace *trace = opt->value; |
| 517 | |
| 518 | trace->duration_filter = atof(str); |
| 519 | return 0; |
| 520 | } |
| 521 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 522 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) |
| 523 | { |
| 524 | const char * const trace_usage[] = { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 525 | "perf trace [<options>] [<command>]", |
| 526 | "perf trace [<options>] -- <command> [<options>]", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 527 | NULL |
| 528 | }; |
| 529 | struct trace trace = { |
| 530 | .audit_machine = audit_detect_machine(), |
| 531 | .syscalls = { |
| 532 | . max = -1, |
| 533 | }, |
| 534 | .opts = { |
| 535 | .target = { |
| 536 | .uid = UINT_MAX, |
| 537 | .uses_mmap = true, |
| 538 | }, |
| 539 | .user_freq = UINT_MAX, |
| 540 | .user_interval = ULLONG_MAX, |
| 541 | .no_delay = true, |
| 542 | .mmap_pages = 1024, |
| 543 | }, |
| 544 | }; |
| 545 | const struct option trace_options[] = { |
| 546 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
| 547 | "trace events on existing process id"), |
| 548 | OPT_STRING(0, "tid", &trace.opts.target.tid, "tid", |
| 549 | "trace events on existing thread id"), |
| 550 | OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide, |
| 551 | "system-wide collection from all CPUs"), |
| 552 | OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu", |
| 553 | "list of cpus to monitor"), |
| 554 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, |
| 555 | "child tasks do not inherit counters"), |
| 556 | OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages, |
| 557 | "number of mmap data pages"), |
| 558 | OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user", |
| 559 | "user to profile"), |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 560 | OPT_CALLBACK(0, "duration", &trace, "float", |
| 561 | "show only events with duration > N.M ms", |
| 562 | trace__set_duration), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 563 | OPT_END() |
| 564 | }; |
| 565 | int err; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 566 | char bf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 567 | |
| 568 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 569 | |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 570 | err = perf_target__validate(&trace.opts.target); |
| 571 | if (err) { |
| 572 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
| 573 | printf("%s", bf); |
| 574 | return err; |
| 575 | } |
| 576 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 577 | err = perf_target__parse_uid(&trace.opts.target); |
| 578 | if (err) { |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 579 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
| 580 | printf("%s", bf); |
| 581 | return err; |
| 582 | } |
| 583 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 584 | if (!argc && perf_target__none(&trace.opts.target)) |
Namhyung Kim | ee76120 | 2012-10-05 14:02:14 +0900 | [diff] [blame] | 585 | trace.opts.target.system_wide = true; |
| 586 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 587 | return trace__run(&trace, argc, argv); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 588 | } |