builtin-trace.c 36 KB

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