Robert Richter | 4e31902 | 2013-06-11 17:29:18 +0200 | [diff] [blame] | 1 | #include <traceevent/event-parse.h> |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2 | #include "builtin.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 3 | #include "util/color.h" |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 4 | #include "util/debug.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 5 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 6 | #include "util/machine.h" |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 7 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 8 | #include "util/thread.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 9 | #include "util/parse-options.h" |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 10 | #include "util/strlist.h" |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame^] | 11 | #include "util/intlist.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 12 | #include "util/thread_map.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 13 | |
| 14 | #include <libaudit.h> |
| 15 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | ae68538 | 2013-08-20 17:44:42 -0300 | [diff] [blame] | 16 | #include <sys/mman.h> |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 17 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 18 | static size_t syscall_arg__scnprintf_hex(char *bf, size_t size, unsigned long arg) |
| 19 | { |
| 20 | return scnprintf(bf, size, "%#lx", arg); |
| 21 | } |
| 22 | |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 23 | #define SCA_HEX syscall_arg__scnprintf_hex |
| 24 | |
Arnaldo Carvalho de Melo | ae68538 | 2013-08-20 17:44:42 -0300 | [diff] [blame] | 25 | static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size, unsigned long arg) |
| 26 | { |
| 27 | int printed = 0, prot = arg; |
| 28 | |
| 29 | if (prot == PROT_NONE) |
| 30 | return scnprintf(bf, size, "NONE"); |
| 31 | #define P_MMAP_PROT(n) \ |
| 32 | if (prot & PROT_##n) { \ |
| 33 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ |
| 34 | prot &= ~PROT_##n; \ |
| 35 | } |
| 36 | |
| 37 | P_MMAP_PROT(EXEC); |
| 38 | P_MMAP_PROT(READ); |
| 39 | P_MMAP_PROT(WRITE); |
| 40 | #ifdef PROT_SEM |
| 41 | P_MMAP_PROT(SEM); |
| 42 | #endif |
| 43 | P_MMAP_PROT(GROWSDOWN); |
| 44 | P_MMAP_PROT(GROWSUP); |
| 45 | #undef P_MMAP_PROT |
| 46 | |
| 47 | if (prot) |
| 48 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot); |
| 49 | |
| 50 | return printed; |
| 51 | } |
| 52 | |
| 53 | #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot |
| 54 | |
Arnaldo Carvalho de Melo | 941557e | 2013-08-23 10:48:33 -0300 | [diff] [blame] | 55 | static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size, unsigned long arg) |
| 56 | { |
| 57 | int printed = 0, flags = arg; |
| 58 | |
| 59 | #define P_MMAP_FLAG(n) \ |
| 60 | if (flags & MAP_##n) { \ |
| 61 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ |
| 62 | flags &= ~MAP_##n; \ |
| 63 | } |
| 64 | |
| 65 | P_MMAP_FLAG(SHARED); |
| 66 | P_MMAP_FLAG(PRIVATE); |
| 67 | P_MMAP_FLAG(32BIT); |
| 68 | P_MMAP_FLAG(ANONYMOUS); |
| 69 | P_MMAP_FLAG(DENYWRITE); |
| 70 | P_MMAP_FLAG(EXECUTABLE); |
| 71 | P_MMAP_FLAG(FILE); |
| 72 | P_MMAP_FLAG(FIXED); |
| 73 | P_MMAP_FLAG(GROWSDOWN); |
| 74 | P_MMAP_FLAG(HUGETLB); |
| 75 | P_MMAP_FLAG(LOCKED); |
| 76 | P_MMAP_FLAG(NONBLOCK); |
| 77 | P_MMAP_FLAG(NORESERVE); |
| 78 | P_MMAP_FLAG(POPULATE); |
| 79 | P_MMAP_FLAG(STACK); |
| 80 | #ifdef MAP_UNINITIALIZED |
| 81 | P_MMAP_FLAG(UNINITIALIZED); |
| 82 | #endif |
| 83 | #undef P_MMAP_FLAG |
| 84 | |
| 85 | if (flags) |
| 86 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); |
| 87 | |
| 88 | return printed; |
| 89 | } |
| 90 | |
| 91 | #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags |
| 92 | |
Arnaldo Carvalho de Melo | 9e9716d | 2013-08-23 10:06:41 -0300 | [diff] [blame] | 93 | static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size, unsigned long arg) |
| 94 | { |
| 95 | int behavior = arg; |
| 96 | |
| 97 | switch (behavior) { |
| 98 | #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n) |
| 99 | P_MADV_BHV(NORMAL); |
| 100 | P_MADV_BHV(RANDOM); |
| 101 | P_MADV_BHV(SEQUENTIAL); |
| 102 | P_MADV_BHV(WILLNEED); |
| 103 | P_MADV_BHV(DONTNEED); |
| 104 | P_MADV_BHV(REMOVE); |
| 105 | P_MADV_BHV(DONTFORK); |
| 106 | P_MADV_BHV(DOFORK); |
| 107 | P_MADV_BHV(HWPOISON); |
| 108 | #ifdef MADV_SOFT_OFFLINE |
| 109 | P_MADV_BHV(SOFT_OFFLINE); |
| 110 | #endif |
| 111 | P_MADV_BHV(MERGEABLE); |
| 112 | P_MADV_BHV(UNMERGEABLE); |
| 113 | P_MADV_BHV(HUGEPAGE); |
| 114 | P_MADV_BHV(NOHUGEPAGE); |
| 115 | #ifdef MADV_DONTDUMP |
| 116 | P_MADV_BHV(DONTDUMP); |
| 117 | #endif |
| 118 | #ifdef MADV_DODUMP |
| 119 | P_MADV_BHV(DODUMP); |
| 120 | #endif |
| 121 | #undef P_MADV_PHV |
| 122 | default: break; |
| 123 | } |
| 124 | |
| 125 | return scnprintf(bf, size, "%#x", behavior); |
| 126 | } |
| 127 | |
| 128 | #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior |
| 129 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 130 | static struct syscall_fmt { |
| 131 | const char *name; |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 132 | const char *alias; |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 133 | size_t (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 134 | bool errmsg; |
| 135 | bool timeout; |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 136 | bool hexret; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 137 | } syscall_fmts[] = { |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame] | 138 | { .name = "access", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 139 | { .name = "arch_prctl", .errmsg = true, .alias = "prctl", }, |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 140 | { .name = "brk", .hexret = true, |
| 141 | .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, }, |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 142 | { .name = "mmap", .hexret = true, }, |
Arnaldo Carvalho de Melo | a14bb86 | 2013-07-30 16:38:23 -0300 | [diff] [blame] | 143 | { .name = "connect", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 144 | { .name = "fstat", .errmsg = true, .alias = "newfstat", }, |
| 145 | { .name = "fstatat", .errmsg = true, .alias = "newfstatat", }, |
| 146 | { .name = "futex", .errmsg = true, }, |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 147 | { .name = "ioctl", .errmsg = true, |
| 148 | .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, }, |
Arnaldo Carvalho de Melo | e595968 | 2013-08-26 12:21:41 -0300 | [diff] [blame] | 149 | { .name = "lstat", .errmsg = true, .alias = "newlstat", }, |
Arnaldo Carvalho de Melo | 9e9716d | 2013-08-23 10:06:41 -0300 | [diff] [blame] | 150 | { .name = "madvise", .errmsg = true, |
| 151 | .arg_scnprintf = { [0] = SCA_HEX, /* start */ |
| 152 | [2] = SCA_MADV_BHV, /* behavior */ }, }, |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 153 | { .name = "mmap", .hexret = true, |
Arnaldo Carvalho de Melo | ae68538 | 2013-08-20 17:44:42 -0300 | [diff] [blame] | 154 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ |
Arnaldo Carvalho de Melo | 941557e | 2013-08-23 10:48:33 -0300 | [diff] [blame] | 155 | [2] = SCA_MMAP_PROT, /* prot */ |
| 156 | [3] = SCA_MMAP_FLAGS, /* flags */ }, }, |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 157 | { .name = "mprotect", .errmsg = true, |
Arnaldo Carvalho de Melo | ae68538 | 2013-08-20 17:44:42 -0300 | [diff] [blame] | 158 | .arg_scnprintf = { [0] = SCA_HEX, /* start */ |
| 159 | [2] = SCA_MMAP_PROT, /* prot */ }, }, |
| 160 | { .name = "mremap", .hexret = true, |
| 161 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ |
| 162 | [4] = SCA_HEX, /* new_addr */ }, }, |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 163 | { .name = "munmap", .errmsg = true, |
| 164 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, }, |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame] | 165 | { .name = "open", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 166 | { .name = "poll", .errmsg = true, .timeout = true, }, |
| 167 | { .name = "ppoll", .errmsg = true, .timeout = true, }, |
Arnaldo Carvalho de Melo | e595968 | 2013-08-26 12:21:41 -0300 | [diff] [blame] | 168 | { .name = "pread", .errmsg = true, .alias = "pread64", }, |
| 169 | { .name = "pwrite", .errmsg = true, .alias = "pwrite64", }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 170 | { .name = "read", .errmsg = true, }, |
| 171 | { .name = "recvfrom", .errmsg = true, }, |
| 172 | { .name = "select", .errmsg = true, .timeout = true, }, |
Arnaldo Carvalho de Melo | 8b74526 | 2012-10-18 19:06:13 -0300 | [diff] [blame] | 173 | { .name = "socket", .errmsg = true, }, |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 174 | { .name = "stat", .errmsg = true, .alias = "newstat", }, |
Arnaldo Carvalho de Melo | e595968 | 2013-08-26 12:21:41 -0300 | [diff] [blame] | 175 | { .name = "uname", .errmsg = true, .alias = "newuname", }, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static int syscall_fmt__cmp(const void *name, const void *fmtp) |
| 179 | { |
| 180 | const struct syscall_fmt *fmt = fmtp; |
| 181 | return strcmp(name, fmt->name); |
| 182 | } |
| 183 | |
| 184 | static struct syscall_fmt *syscall_fmt__find(const char *name) |
| 185 | { |
| 186 | const int nmemb = ARRAY_SIZE(syscall_fmts); |
| 187 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); |
| 188 | } |
| 189 | |
| 190 | struct syscall { |
| 191 | struct event_format *tp_format; |
| 192 | const char *name; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 193 | bool filtered; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 194 | struct syscall_fmt *fmt; |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 195 | size_t (**arg_scnprintf)(char *bf, size_t size, unsigned long arg); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 196 | }; |
| 197 | |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 198 | static size_t fprintf_duration(unsigned long t, FILE *fp) |
| 199 | { |
| 200 | double duration = (double)t / NSEC_PER_MSEC; |
| 201 | size_t printed = fprintf(fp, "("); |
| 202 | |
| 203 | if (duration >= 1.0) |
| 204 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); |
| 205 | else if (duration >= 0.01) |
| 206 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); |
| 207 | else |
| 208 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 209 | return printed + fprintf(fp, "): "); |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 210 | } |
| 211 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 212 | struct thread_trace { |
| 213 | u64 entry_time; |
| 214 | u64 exit_time; |
| 215 | bool entry_pending; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 216 | unsigned long nr_events; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 217 | char *entry_str; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 218 | double runtime_ms; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | static struct thread_trace *thread_trace__new(void) |
| 222 | { |
| 223 | return zalloc(sizeof(struct thread_trace)); |
| 224 | } |
| 225 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 226 | static struct thread_trace *thread__trace(struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 227 | { |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 228 | struct thread_trace *ttrace; |
| 229 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 230 | if (thread == NULL) |
| 231 | goto fail; |
| 232 | |
| 233 | if (thread->priv == NULL) |
| 234 | thread->priv = thread_trace__new(); |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 235 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 236 | if (thread->priv == NULL) |
| 237 | goto fail; |
| 238 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 239 | ttrace = thread->priv; |
| 240 | ++ttrace->nr_events; |
| 241 | |
| 242 | return ttrace; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 243 | fail: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 244 | color_fprintf(fp, PERF_COLOR_RED, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 245 | "WARNING: not enough memory, dropping samples!\n"); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 249 | struct trace { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 250 | struct perf_tool tool; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 251 | int audit_machine; |
| 252 | struct { |
| 253 | int max; |
| 254 | struct syscall *table; |
| 255 | } syscalls; |
| 256 | struct perf_record_opts opts; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 257 | struct machine host; |
| 258 | u64 base_time; |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 259 | FILE *output; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 260 | unsigned long nr_events; |
Arnaldo Carvalho de Melo | b059efd | 2013-08-21 12:56:21 -0300 | [diff] [blame] | 261 | struct strlist *ev_qualifier; |
| 262 | bool not_ev_qualifier; |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame^] | 263 | struct intlist *tid_list; |
| 264 | struct intlist *pid_list; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 265 | bool sched; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 266 | bool multiple_threads; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 267 | double duration_filter; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 268 | double runtime_ms; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 269 | }; |
| 270 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 271 | static bool trace__filter_duration(struct trace *trace, double t) |
| 272 | { |
| 273 | return t < (trace->duration_filter * NSEC_PER_MSEC); |
| 274 | } |
| 275 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 276 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
| 277 | { |
| 278 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; |
| 279 | |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 280 | return fprintf(fp, "%10.3f ", ts); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 281 | } |
| 282 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 283 | static bool done = false; |
| 284 | |
| 285 | static void sig_handler(int sig __maybe_unused) |
| 286 | { |
| 287 | done = true; |
| 288 | } |
| 289 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 290 | 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] | 291 | u64 duration, u64 tstamp, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 292 | { |
| 293 | size_t printed = trace__fprintf_tstamp(trace, tstamp, fp); |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 294 | printed += fprintf_duration(duration, fp); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 295 | |
| 296 | if (trace->multiple_threads) |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 297 | printed += fprintf(fp, "%d ", thread->tid); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 298 | |
| 299 | return printed; |
| 300 | } |
| 301 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 302 | static int trace__process_event(struct trace *trace, struct machine *machine, |
| 303 | union perf_event *event) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 304 | { |
| 305 | int ret = 0; |
| 306 | |
| 307 | switch (event->header.type) { |
| 308 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 309 | color_fprintf(trace->output, PERF_COLOR_RED, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 310 | "LOST %" PRIu64 " events!\n", event->lost.lost); |
| 311 | ret = machine__process_lost_event(machine, event); |
| 312 | default: |
| 313 | ret = machine__process_event(machine, event); |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 320 | static int trace__tool_process(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 321 | union perf_event *event, |
| 322 | struct perf_sample *sample __maybe_unused, |
| 323 | struct machine *machine) |
| 324 | { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 325 | struct trace *trace = container_of(tool, struct trace, tool); |
| 326 | return trace__process_event(trace, machine, event); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) |
| 330 | { |
| 331 | int err = symbol__init(); |
| 332 | |
| 333 | if (err) |
| 334 | return err; |
| 335 | |
| 336 | machine__init(&trace->host, "", HOST_KERNEL_ID); |
| 337 | machine__create_kernel_maps(&trace->host); |
| 338 | |
| 339 | if (perf_target__has_task(&trace->opts.target)) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 340 | err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 341 | trace__tool_process, |
| 342 | &trace->host); |
| 343 | } else { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 344 | err = perf_event__synthesize_threads(&trace->tool, trace__tool_process, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 345 | &trace->host); |
| 346 | } |
| 347 | |
| 348 | if (err) |
| 349 | symbol__exit(); |
| 350 | |
| 351 | return err; |
| 352 | } |
| 353 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 354 | static int syscall__set_arg_fmts(struct syscall *sc) |
| 355 | { |
| 356 | struct format_field *field; |
| 357 | int idx = 0; |
| 358 | |
| 359 | sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *)); |
| 360 | if (sc->arg_scnprintf == NULL) |
| 361 | return -1; |
| 362 | |
| 363 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 364 | if (sc->fmt && sc->fmt->arg_scnprintf[idx]) |
| 365 | sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx]; |
| 366 | else if (field->flags & FIELD_IS_POINTER) |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 367 | sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex; |
| 368 | ++idx; |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 374 | static int trace__read_syscall_info(struct trace *trace, int id) |
| 375 | { |
| 376 | char tp_name[128]; |
| 377 | struct syscall *sc; |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 378 | const char *name = audit_syscall_to_name(id, trace->audit_machine); |
| 379 | |
| 380 | if (name == NULL) |
| 381 | return -1; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 382 | |
| 383 | if (id > trace->syscalls.max) { |
| 384 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); |
| 385 | |
| 386 | if (nsyscalls == NULL) |
| 387 | return -1; |
| 388 | |
| 389 | if (trace->syscalls.max != -1) { |
| 390 | memset(nsyscalls + trace->syscalls.max + 1, 0, |
| 391 | (id - trace->syscalls.max) * sizeof(*sc)); |
| 392 | } else { |
| 393 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); |
| 394 | } |
| 395 | |
| 396 | trace->syscalls.table = nsyscalls; |
| 397 | trace->syscalls.max = id; |
| 398 | } |
| 399 | |
| 400 | sc = trace->syscalls.table + id; |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 401 | sc->name = name; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 402 | |
Arnaldo Carvalho de Melo | b059efd | 2013-08-21 12:56:21 -0300 | [diff] [blame] | 403 | if (trace->ev_qualifier) { |
| 404 | bool in = strlist__find(trace->ev_qualifier, name) != NULL; |
| 405 | |
| 406 | if (!(in ^ trace->not_ev_qualifier)) { |
| 407 | sc->filtered = true; |
| 408 | /* |
| 409 | * No need to do read tracepoint information since this will be |
| 410 | * filtered out. |
| 411 | */ |
| 412 | return 0; |
| 413 | } |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 414 | } |
| 415 | |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 416 | sc->fmt = syscall_fmt__find(sc->name); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 417 | |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 418 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
| 419 | sc->tp_format = event_format__new("syscalls", tp_name); |
| 420 | |
| 421 | if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) { |
| 422 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); |
| 423 | sc->tp_format = event_format__new("syscalls", tp_name); |
| 424 | } |
| 425 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 426 | if (sc->tp_format == NULL) |
| 427 | return -1; |
| 428 | |
| 429 | return syscall__set_arg_fmts(sc); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 430 | } |
| 431 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 432 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
| 433 | unsigned long *args) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 434 | { |
| 435 | int i = 0; |
| 436 | size_t printed = 0; |
| 437 | |
| 438 | if (sc->tp_format != NULL) { |
| 439 | struct format_field *field; |
| 440 | |
| 441 | 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] | 442 | printed += scnprintf(bf + printed, size - printed, |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 443 | "%s%s: ", printed ? ", " : "", field->name); |
| 444 | |
| 445 | if (sc->arg_scnprintf && sc->arg_scnprintf[i]) |
| 446 | printed += sc->arg_scnprintf[i](bf + printed, size - printed, args[i]); |
| 447 | else |
| 448 | printed += scnprintf(bf + printed, size - printed, |
| 449 | "%ld", args[i]); |
| 450 | ++i; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 451 | } |
| 452 | } else { |
| 453 | while (i < 6) { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 454 | printed += scnprintf(bf + printed, size - printed, |
| 455 | "%sarg%d: %ld", |
| 456 | printed ? ", " : "", i, args[i]); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 457 | ++i; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return printed; |
| 462 | } |
| 463 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 464 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
| 465 | struct perf_sample *sample); |
| 466 | |
| 467 | static struct syscall *trace__syscall_info(struct trace *trace, |
| 468 | struct perf_evsel *evsel, |
| 469 | struct perf_sample *sample) |
| 470 | { |
| 471 | int id = perf_evsel__intval(evsel, sample, "id"); |
| 472 | |
| 473 | if (id < 0) { |
Arnaldo Carvalho de Melo | adaa18b | 2013-08-22 17:55:25 -0300 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried |
| 477 | * before that, leaving at a higher verbosity level till that is |
| 478 | * explained. Reproduced with plain ftrace with: |
| 479 | * |
| 480 | * echo 1 > /t/events/raw_syscalls/sys_exit/enable |
| 481 | * grep "NR -1 " /t/trace_pipe |
| 482 | * |
| 483 | * After generating some load on the machine. |
| 484 | */ |
| 485 | if (verbose > 1) { |
| 486 | static u64 n; |
| 487 | fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", |
| 488 | id, perf_evsel__name(evsel), ++n); |
| 489 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && |
| 494 | trace__read_syscall_info(trace, id)) |
| 495 | goto out_cant_read; |
| 496 | |
| 497 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) |
| 498 | goto out_cant_read; |
| 499 | |
| 500 | return &trace->syscalls.table[id]; |
| 501 | |
| 502 | out_cant_read: |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 503 | if (verbose) { |
| 504 | fprintf(trace->output, "Problems reading syscall %d", id); |
| 505 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) |
| 506 | fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); |
| 507 | fputs(" information\n", trace->output); |
| 508 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 509 | return NULL; |
| 510 | } |
| 511 | |
| 512 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, |
| 513 | struct perf_sample *sample) |
| 514 | { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 515 | char *msg; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 516 | void *args; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 517 | size_t printed = 0; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 518 | struct thread *thread; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 519 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 520 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 521 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 522 | if (sc == NULL) |
| 523 | return -1; |
| 524 | |
| 525 | if (sc->filtered) |
| 526 | return 0; |
| 527 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 528 | thread = machine__findnew_thread(&trace->host, sample->pid, |
| 529 | sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 530 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 531 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 532 | return -1; |
| 533 | |
| 534 | args = perf_evsel__rawptr(evsel, sample, "args"); |
| 535 | if (args == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 536 | fprintf(trace->output, "Problems reading syscall arguments\n"); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 537 | return -1; |
| 538 | } |
| 539 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 540 | ttrace = thread->priv; |
| 541 | |
| 542 | if (ttrace->entry_str == NULL) { |
| 543 | ttrace->entry_str = malloc(1024); |
| 544 | if (!ttrace->entry_str) |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | ttrace->entry_time = sample->time; |
| 549 | msg = ttrace->entry_str; |
| 550 | printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name); |
| 551 | |
| 552 | printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args); |
| 553 | |
| 554 | if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) { |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 555 | if (!trace->duration_filter) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 556 | trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output); |
| 557 | fprintf(trace->output, "%-70s\n", ttrace->entry_str); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 558 | } |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 559 | } else |
| 560 | ttrace->entry_pending = true; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, |
| 566 | struct perf_sample *sample) |
| 567 | { |
| 568 | int ret; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 569 | u64 duration = 0; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 570 | struct thread *thread; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 571 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 572 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 573 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 574 | if (sc == NULL) |
| 575 | return -1; |
| 576 | |
| 577 | if (sc->filtered) |
| 578 | return 0; |
| 579 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 580 | thread = machine__findnew_thread(&trace->host, sample->pid, |
| 581 | sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 582 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 583 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 584 | return -1; |
| 585 | |
| 586 | ret = perf_evsel__intval(evsel, sample, "ret"); |
| 587 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 588 | ttrace = thread->priv; |
| 589 | |
| 590 | ttrace->exit_time = sample->time; |
| 591 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 592 | if (ttrace->entry_time) { |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 593 | duration = sample->time - ttrace->entry_time; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 594 | if (trace__filter_duration(trace, duration)) |
| 595 | goto out; |
| 596 | } else if (trace->duration_filter) |
| 597 | goto out; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 598 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 599 | trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 600 | |
| 601 | if (ttrace->entry_pending) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 602 | fprintf(trace->output, "%-70s", ttrace->entry_str); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 603 | } else { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 604 | fprintf(trace->output, " ... ["); |
| 605 | color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); |
| 606 | fprintf(trace->output, "]: %s()", sc->name); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 607 | } |
| 608 | |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 609 | if (sc->fmt == NULL) { |
| 610 | signed_print: |
| 611 | fprintf(trace->output, ") = %d", ret); |
| 612 | } else if (ret < 0 && sc->fmt->errmsg) { |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 613 | char bf[256]; |
| 614 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), |
| 615 | *e = audit_errno_to_name(-ret); |
| 616 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 617 | fprintf(trace->output, ") = -1 %s %s", e, emsg); |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 618 | } else if (ret == 0 && sc->fmt->timeout) |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 619 | fprintf(trace->output, ") = 0 Timeout"); |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 620 | else if (sc->fmt->hexret) |
| 621 | fprintf(trace->output, ") = %#x", ret); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 622 | else |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 623 | goto signed_print; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 624 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 625 | fputc('\n', trace->output); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 626 | out: |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 627 | ttrace->entry_pending = false; |
| 628 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 632 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
| 633 | struct perf_sample *sample) |
| 634 | { |
| 635 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); |
| 636 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 637 | struct thread *thread = machine__findnew_thread(&trace->host, |
| 638 | sample->pid, |
| 639 | sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 640 | struct thread_trace *ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 641 | |
| 642 | if (ttrace == NULL) |
| 643 | goto out_dump; |
| 644 | |
| 645 | ttrace->runtime_ms += runtime_ms; |
| 646 | trace->runtime_ms += runtime_ms; |
| 647 | return 0; |
| 648 | |
| 649 | out_dump: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 650 | fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 651 | evsel->name, |
| 652 | perf_evsel__strval(evsel, sample, "comm"), |
| 653 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), |
| 654 | runtime, |
| 655 | perf_evsel__intval(evsel, sample, "vruntime")); |
| 656 | return 0; |
| 657 | } |
| 658 | |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame^] | 659 | static bool skip_sample(struct trace *trace, struct perf_sample *sample) |
| 660 | { |
| 661 | if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) || |
| 662 | (trace->tid_list && intlist__find(trace->tid_list, sample->tid))) |
| 663 | return false; |
| 664 | |
| 665 | if (trace->pid_list || trace->tid_list) |
| 666 | return true; |
| 667 | |
| 668 | return false; |
| 669 | } |
| 670 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 671 | static int trace__process_sample(struct perf_tool *tool, |
| 672 | union perf_event *event __maybe_unused, |
| 673 | struct perf_sample *sample, |
| 674 | struct perf_evsel *evsel, |
| 675 | struct machine *machine __maybe_unused) |
| 676 | { |
| 677 | struct trace *trace = container_of(tool, struct trace, tool); |
| 678 | int err = 0; |
| 679 | |
| 680 | tracepoint_handler handler = evsel->handler.func; |
| 681 | |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame^] | 682 | if (skip_sample(trace, sample)) |
| 683 | return 0; |
| 684 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 685 | if (trace->base_time == 0) |
| 686 | trace->base_time = sample->time; |
| 687 | |
| 688 | if (handler) |
| 689 | handler(trace, evsel, sample); |
| 690 | |
| 691 | return err; |
| 692 | } |
| 693 | |
| 694 | static bool |
| 695 | perf_session__has_tp(struct perf_session *session, const char *name) |
| 696 | { |
| 697 | struct perf_evsel *evsel; |
| 698 | |
| 699 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name); |
| 700 | |
| 701 | return evsel != NULL; |
| 702 | } |
| 703 | |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame^] | 704 | static int parse_target_str(struct trace *trace) |
| 705 | { |
| 706 | if (trace->opts.target.pid) { |
| 707 | trace->pid_list = intlist__new(trace->opts.target.pid); |
| 708 | if (trace->pid_list == NULL) { |
| 709 | pr_err("Error parsing process id string\n"); |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (trace->opts.target.tid) { |
| 715 | trace->tid_list = intlist__new(trace->opts.target.tid); |
| 716 | if (trace->tid_list == NULL) { |
| 717 | pr_err("Error parsing thread id string\n"); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 725 | 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] | 726 | { |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 727 | struct perf_evlist *evlist = perf_evlist__new(); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 728 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 729 | int err = -1, i; |
| 730 | unsigned long before; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 731 | const bool forks = argc > 0; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 732 | |
| 733 | if (evlist == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 734 | fprintf(trace->output, "Not enough memory to run!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 735 | goto out; |
| 736 | } |
| 737 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 738 | if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) || |
| 739 | perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 740 | fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 741 | goto out_delete_evlist; |
| 742 | } |
| 743 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 744 | if (trace->sched && |
| 745 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", |
| 746 | trace__sched_stat_runtime)) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 747 | fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n"); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 748 | goto out_delete_evlist; |
| 749 | } |
| 750 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 751 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
| 752 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 753 | fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 754 | goto out_delete_evlist; |
| 755 | } |
| 756 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 757 | err = trace__symbols_init(trace, evlist); |
| 758 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 759 | fprintf(trace->output, "Problems initializing symbol libraries!\n"); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 760 | goto out_delete_maps; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 761 | } |
| 762 | |
Arnaldo Carvalho de Melo | f77a951 | 2012-12-10 16:41:31 -0300 | [diff] [blame] | 763 | perf_evlist__config(evlist, &trace->opts); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 764 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 765 | signal(SIGCHLD, sig_handler); |
| 766 | signal(SIGINT, sig_handler); |
| 767 | |
| 768 | if (forks) { |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 769 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 770 | argv, false, false); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 771 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 772 | fprintf(trace->output, "Couldn't run the workload!\n"); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 773 | goto out_delete_maps; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 777 | err = perf_evlist__open(evlist); |
| 778 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 779 | fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno)); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 780 | goto out_delete_maps; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | err = perf_evlist__mmap(evlist, UINT_MAX, false); |
| 784 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 785 | fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno)); |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 786 | goto out_close_evlist; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | perf_evlist__enable(evlist); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 790 | |
| 791 | if (forks) |
| 792 | perf_evlist__start_workload(evlist); |
| 793 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 794 | 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] | 795 | again: |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 796 | before = trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 797 | |
| 798 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 799 | union perf_event *event; |
| 800 | |
| 801 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
| 802 | const u32 type = event->header.type; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 803 | tracepoint_handler handler; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 804 | struct perf_sample sample; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 805 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 806 | ++trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 807 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 808 | err = perf_evlist__parse_sample(evlist, event, &sample); |
| 809 | if (err) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 810 | fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 811 | continue; |
| 812 | } |
| 813 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 814 | if (trace->base_time == 0) |
| 815 | trace->base_time = sample.time; |
| 816 | |
| 817 | if (type != PERF_RECORD_SAMPLE) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 818 | trace__process_event(trace, &trace->host, event); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 819 | continue; |
| 820 | } |
| 821 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 822 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
| 823 | if (evsel == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 824 | fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 825 | continue; |
| 826 | } |
| 827 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 828 | if (sample.raw_data == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 829 | fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 830 | perf_evsel__name(evsel), sample.tid, |
| 831 | sample.cpu, sample.raw_size); |
| 832 | continue; |
| 833 | } |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 834 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 835 | handler = evsel->handler.func; |
| 836 | handler(trace, evsel, &sample); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 840 | if (trace->nr_events == before) { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 841 | if (done) |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 842 | goto out_unmap_evlist; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 843 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 844 | poll(evlist->pollfd, evlist->nr_fds, -1); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | if (done) |
| 848 | perf_evlist__disable(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 849 | |
| 850 | goto again; |
| 851 | |
Namhyung Kim | 3beb086 | 2013-03-15 14:48:50 +0900 | [diff] [blame] | 852 | out_unmap_evlist: |
| 853 | perf_evlist__munmap(evlist); |
| 854 | out_close_evlist: |
| 855 | perf_evlist__close(evlist); |
| 856 | out_delete_maps: |
| 857 | perf_evlist__delete_maps(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 858 | out_delete_evlist: |
| 859 | perf_evlist__delete(evlist); |
| 860 | out: |
| 861 | return err; |
| 862 | } |
| 863 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 864 | static int trace__replay(struct trace *trace) |
| 865 | { |
| 866 | const struct perf_evsel_str_handler handlers[] = { |
| 867 | { "raw_syscalls:sys_enter", trace__sys_enter, }, |
| 868 | { "raw_syscalls:sys_exit", trace__sys_exit, }, |
| 869 | }; |
| 870 | |
| 871 | struct perf_session *session; |
| 872 | int err = -1; |
| 873 | |
| 874 | trace->tool.sample = trace__process_sample; |
| 875 | trace->tool.mmap = perf_event__process_mmap; |
| 876 | trace->tool.comm = perf_event__process_comm; |
| 877 | trace->tool.exit = perf_event__process_exit; |
| 878 | trace->tool.fork = perf_event__process_fork; |
| 879 | trace->tool.attr = perf_event__process_attr; |
| 880 | trace->tool.tracing_data = perf_event__process_tracing_data; |
| 881 | trace->tool.build_id = perf_event__process_build_id; |
| 882 | |
| 883 | trace->tool.ordered_samples = true; |
| 884 | trace->tool.ordering_requires_timestamps = true; |
| 885 | |
| 886 | /* add tid to output */ |
| 887 | trace->multiple_threads = true; |
| 888 | |
| 889 | if (symbol__init() < 0) |
| 890 | return -1; |
| 891 | |
| 892 | session = perf_session__new(input_name, O_RDONLY, 0, false, |
| 893 | &trace->tool); |
| 894 | if (session == NULL) |
| 895 | return -ENOMEM; |
| 896 | |
| 897 | err = perf_session__set_tracepoints_handlers(session, handlers); |
| 898 | if (err) |
| 899 | goto out; |
| 900 | |
| 901 | if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) { |
| 902 | pr_err("Data file does not have raw_syscalls:sys_enter events\n"); |
| 903 | goto out; |
| 904 | } |
| 905 | |
| 906 | if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) { |
| 907 | pr_err("Data file does not have raw_syscalls:sys_exit events\n"); |
| 908 | goto out; |
| 909 | } |
| 910 | |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame^] | 911 | err = parse_target_str(trace); |
| 912 | if (err != 0) |
| 913 | goto out; |
| 914 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 915 | setup_pager(); |
| 916 | |
| 917 | err = perf_session__process_events(session, &trace->tool); |
| 918 | if (err) |
| 919 | pr_err("Failed to process events, error %d", err); |
| 920 | |
| 921 | out: |
| 922 | perf_session__delete(session); |
| 923 | |
| 924 | return err; |
| 925 | } |
| 926 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 927 | static size_t trace__fprintf_threads_header(FILE *fp) |
| 928 | { |
| 929 | size_t printed; |
| 930 | |
| 931 | printed = fprintf(fp, "\n _____________________________________________________________________\n"); |
| 932 | printed += fprintf(fp," __) Summary of events (__\n\n"); |
| 933 | printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n"); |
| 934 | printed += fprintf(fp," _____________________________________________________________________\n\n"); |
| 935 | |
| 936 | return printed; |
| 937 | } |
| 938 | |
| 939 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) |
| 940 | { |
| 941 | size_t printed = trace__fprintf_threads_header(fp); |
| 942 | struct rb_node *nd; |
| 943 | |
| 944 | for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) { |
| 945 | struct thread *thread = rb_entry(nd, struct thread, rb_node); |
| 946 | struct thread_trace *ttrace = thread->priv; |
| 947 | const char *color; |
| 948 | double ratio; |
| 949 | |
| 950 | if (ttrace == NULL) |
| 951 | continue; |
| 952 | |
| 953 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; |
| 954 | |
| 955 | color = PERF_COLOR_NORMAL; |
| 956 | if (ratio > 50.0) |
| 957 | color = PERF_COLOR_RED; |
| 958 | else if (ratio > 25.0) |
| 959 | color = PERF_COLOR_GREEN; |
| 960 | else if (ratio > 5.0) |
| 961 | color = PERF_COLOR_YELLOW; |
| 962 | |
| 963 | printed += color_fprintf(fp, color, "%20s", thread->comm); |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 964 | printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 965 | printed += color_fprintf(fp, color, "%5.1f%%", ratio); |
| 966 | printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms); |
| 967 | } |
| 968 | |
| 969 | return printed; |
| 970 | } |
| 971 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 972 | static int trace__set_duration(const struct option *opt, const char *str, |
| 973 | int unset __maybe_unused) |
| 974 | { |
| 975 | struct trace *trace = opt->value; |
| 976 | |
| 977 | trace->duration_filter = atof(str); |
| 978 | return 0; |
| 979 | } |
| 980 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 981 | static int trace__open_output(struct trace *trace, const char *filename) |
| 982 | { |
| 983 | struct stat st; |
| 984 | |
| 985 | if (!stat(filename, &st) && st.st_size) { |
| 986 | char oldname[PATH_MAX]; |
| 987 | |
| 988 | scnprintf(oldname, sizeof(oldname), "%s.old", filename); |
| 989 | unlink(oldname); |
| 990 | rename(filename, oldname); |
| 991 | } |
| 992 | |
| 993 | trace->output = fopen(filename, "w"); |
| 994 | |
| 995 | return trace->output == NULL ? -errno : 0; |
| 996 | } |
| 997 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 998 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) |
| 999 | { |
| 1000 | const char * const trace_usage[] = { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 1001 | "perf trace [<options>] [<command>]", |
| 1002 | "perf trace [<options>] -- <command> [<options>]", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1003 | NULL |
| 1004 | }; |
| 1005 | struct trace trace = { |
| 1006 | .audit_machine = audit_detect_machine(), |
| 1007 | .syscalls = { |
| 1008 | . max = -1, |
| 1009 | }, |
| 1010 | .opts = { |
| 1011 | .target = { |
| 1012 | .uid = UINT_MAX, |
| 1013 | .uses_mmap = true, |
| 1014 | }, |
| 1015 | .user_freq = UINT_MAX, |
| 1016 | .user_interval = ULLONG_MAX, |
| 1017 | .no_delay = true, |
| 1018 | .mmap_pages = 1024, |
| 1019 | }, |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1020 | .output = stdout, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1021 | }; |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1022 | const char *output_name = NULL; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1023 | const char *ev_qualifier_str = NULL; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1024 | const struct option trace_options[] = { |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1025 | OPT_STRING('e', "expr", &ev_qualifier_str, "expr", |
| 1026 | "list of events to trace"), |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1027 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 1028 | OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1029 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
| 1030 | "trace events on existing process id"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 1031 | OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1032 | "trace events on existing thread id"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 1033 | OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1034 | "system-wide collection from all CPUs"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 1035 | OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1036 | "list of cpus to monitor"), |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 1037 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1038 | "child tasks do not inherit counters"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 1039 | OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1040 | "number of mmap data pages"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 1041 | OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1042 | "user to profile"), |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 1043 | OPT_CALLBACK(0, "duration", &trace, "float", |
| 1044 | "show only events with duration > N.M ms", |
| 1045 | trace__set_duration), |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 1046 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 1047 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1048 | OPT_END() |
| 1049 | }; |
| 1050 | int err; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 1051 | char bf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1052 | |
| 1053 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1054 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1055 | if (output_name != NULL) { |
| 1056 | err = trace__open_output(&trace, output_name); |
| 1057 | if (err < 0) { |
| 1058 | perror("failed to create output file"); |
| 1059 | goto out; |
| 1060 | } |
| 1061 | } |
| 1062 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1063 | if (ev_qualifier_str != NULL) { |
Arnaldo Carvalho de Melo | b059efd | 2013-08-21 12:56:21 -0300 | [diff] [blame] | 1064 | const char *s = ev_qualifier_str; |
| 1065 | |
| 1066 | trace.not_ev_qualifier = *s == '!'; |
| 1067 | if (trace.not_ev_qualifier) |
| 1068 | ++s; |
| 1069 | trace.ev_qualifier = strlist__new(true, s); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1070 | if (trace.ev_qualifier == NULL) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1071 | fputs("Not enough memory to parse event qualifier", |
| 1072 | trace.output); |
| 1073 | err = -ENOMEM; |
| 1074 | goto out_close; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 1078 | err = perf_target__validate(&trace.opts.target); |
| 1079 | if (err) { |
| 1080 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1081 | fprintf(trace.output, "%s", bf); |
| 1082 | goto out_close; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 1083 | } |
| 1084 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1085 | err = perf_target__parse_uid(&trace.opts.target); |
| 1086 | if (err) { |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1087 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1088 | fprintf(trace.output, "%s", bf); |
| 1089 | goto out_close; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1090 | } |
| 1091 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 1092 | if (!argc && perf_target__none(&trace.opts.target)) |
Namhyung Kim | ee76120 | 2012-10-05 14:02:14 +0900 | [diff] [blame] | 1093 | trace.opts.target.system_wide = true; |
| 1094 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 1095 | if (input_name) |
| 1096 | err = trace__replay(&trace); |
| 1097 | else |
| 1098 | err = trace__run(&trace, argc, argv); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 1099 | |
| 1100 | if (trace.sched && !err) |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1101 | trace__fprintf_thread_summary(&trace, trace.output); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 1102 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1103 | out_close: |
| 1104 | if (output_name != NULL) |
| 1105 | fclose(trace.output); |
| 1106 | out: |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 1107 | return err; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1108 | } |