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