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