builtin-trace.c 38 KB

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