builtin-trace.c 41 KB

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