builtin-trace.c 34 KB

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