builtin-trace.c 44 KB

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