builtin-trace.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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 = "mlock", .errmsg = true,
  543. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  544. { .name = "mlockall", .errmsg = true,
  545. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  546. { .name = "mmap", .hexret = true,
  547. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  548. [2] = SCA_MMAP_PROT, /* prot */
  549. [3] = SCA_MMAP_FLAGS, /* flags */ }, },
  550. { .name = "mprotect", .errmsg = true,
  551. .arg_scnprintf = { [0] = SCA_HEX, /* start */
  552. [2] = SCA_MMAP_PROT, /* prot */ }, },
  553. { .name = "mremap", .hexret = true,
  554. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  555. [4] = SCA_HEX, /* new_addr */ }, },
  556. { .name = "munlock", .errmsg = true,
  557. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  558. { .name = "munmap", .errmsg = true,
  559. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  560. { .name = "open", .errmsg = true,
  561. .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
  562. { .name = "open_by_handle_at", .errmsg = true,
  563. .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  564. { .name = "openat", .errmsg = true,
  565. .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  566. { .name = "pipe2", .errmsg = true,
  567. .arg_scnprintf = { [1] = SCA_PIPE_FLAGS, /* flags */ }, },
  568. { .name = "poll", .errmsg = true, .timeout = true, },
  569. { .name = "ppoll", .errmsg = true, .timeout = true, },
  570. { .name = "pread", .errmsg = true, .alias = "pread64", },
  571. { .name = "prlimit64", .errmsg = true, STRARRAY(1, resource, rlimit_resources), },
  572. { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
  573. { .name = "read", .errmsg = true, },
  574. { .name = "recvfrom", .errmsg = true,
  575. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  576. { .name = "recvmmsg", .errmsg = true,
  577. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  578. { .name = "recvmsg", .errmsg = true,
  579. .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
  580. { .name = "rt_sigaction", .errmsg = true,
  581. .arg_scnprintf = { [0] = SCA_SIGNUM, /* sig */ }, },
  582. { .name = "rt_sigprocmask", .errmsg = true, STRARRAY(0, how, sighow), },
  583. { .name = "rt_sigqueueinfo", .errmsg = true,
  584. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  585. { .name = "rt_tgsigqueueinfo", .errmsg = true,
  586. .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
  587. { .name = "select", .errmsg = true, .timeout = true, },
  588. { .name = "sendmmsg", .errmsg = true,
  589. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  590. { .name = "sendmsg", .errmsg = true,
  591. .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
  592. { .name = "sendto", .errmsg = true,
  593. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  594. { .name = "setitimer", .errmsg = true, STRARRAY(0, which, itimers), },
  595. { .name = "setrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
  596. { .name = "socket", .errmsg = true,
  597. .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
  598. [1] = SCA_SK_TYPE, /* type */ },
  599. .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
  600. { .name = "socketpair", .errmsg = true,
  601. .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
  602. [1] = SCA_SK_TYPE, /* type */ },
  603. .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
  604. { .name = "stat", .errmsg = true, .alias = "newstat", },
  605. { .name = "tgkill", .errmsg = true,
  606. .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
  607. { .name = "tkill", .errmsg = true,
  608. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  609. { .name = "uname", .errmsg = true, .alias = "newuname", },
  610. };
  611. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  612. {
  613. const struct syscall_fmt *fmt = fmtp;
  614. return strcmp(name, fmt->name);
  615. }
  616. static struct syscall_fmt *syscall_fmt__find(const char *name)
  617. {
  618. const int nmemb = ARRAY_SIZE(syscall_fmts);
  619. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  620. }
  621. struct syscall {
  622. struct event_format *tp_format;
  623. const char *name;
  624. bool filtered;
  625. struct syscall_fmt *fmt;
  626. size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  627. void **arg_parm;
  628. };
  629. static size_t fprintf_duration(unsigned long t, FILE *fp)
  630. {
  631. double duration = (double)t / NSEC_PER_MSEC;
  632. size_t printed = fprintf(fp, "(");
  633. if (duration >= 1.0)
  634. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  635. else if (duration >= 0.01)
  636. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  637. else
  638. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  639. return printed + fprintf(fp, "): ");
  640. }
  641. struct thread_trace {
  642. u64 entry_time;
  643. u64 exit_time;
  644. bool entry_pending;
  645. unsigned long nr_events;
  646. char *entry_str;
  647. double runtime_ms;
  648. };
  649. static struct thread_trace *thread_trace__new(void)
  650. {
  651. return zalloc(sizeof(struct thread_trace));
  652. }
  653. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  654. {
  655. struct thread_trace *ttrace;
  656. if (thread == NULL)
  657. goto fail;
  658. if (thread->priv == NULL)
  659. thread->priv = thread_trace__new();
  660. if (thread->priv == NULL)
  661. goto fail;
  662. ttrace = thread->priv;
  663. ++ttrace->nr_events;
  664. return ttrace;
  665. fail:
  666. color_fprintf(fp, PERF_COLOR_RED,
  667. "WARNING: not enough memory, dropping samples!\n");
  668. return NULL;
  669. }
  670. struct trace {
  671. struct perf_tool tool;
  672. int audit_machine;
  673. struct {
  674. int max;
  675. struct syscall *table;
  676. } syscalls;
  677. struct perf_record_opts opts;
  678. struct machine host;
  679. u64 base_time;
  680. bool full_time;
  681. FILE *output;
  682. unsigned long nr_events;
  683. struct strlist *ev_qualifier;
  684. bool not_ev_qualifier;
  685. struct intlist *tid_list;
  686. struct intlist *pid_list;
  687. bool sched;
  688. bool multiple_threads;
  689. bool show_comm;
  690. double duration_filter;
  691. double runtime_ms;
  692. };
  693. static bool trace__filter_duration(struct trace *trace, double t)
  694. {
  695. return t < (trace->duration_filter * NSEC_PER_MSEC);
  696. }
  697. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  698. {
  699. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  700. return fprintf(fp, "%10.3f ", ts);
  701. }
  702. static bool done = false;
  703. static void sig_handler(int sig __maybe_unused)
  704. {
  705. done = true;
  706. }
  707. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  708. u64 duration, u64 tstamp, FILE *fp)
  709. {
  710. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  711. printed += fprintf_duration(duration, fp);
  712. if (trace->multiple_threads) {
  713. if (trace->show_comm)
  714. printed += fprintf(fp, "%.14s/", thread->comm);
  715. printed += fprintf(fp, "%d ", thread->tid);
  716. }
  717. return printed;
  718. }
  719. static int trace__process_event(struct trace *trace, struct machine *machine,
  720. union perf_event *event)
  721. {
  722. int ret = 0;
  723. switch (event->header.type) {
  724. case PERF_RECORD_LOST:
  725. color_fprintf(trace->output, PERF_COLOR_RED,
  726. "LOST %" PRIu64 " events!\n", event->lost.lost);
  727. ret = machine__process_lost_event(machine, event);
  728. default:
  729. ret = machine__process_event(machine, event);
  730. break;
  731. }
  732. return ret;
  733. }
  734. static int trace__tool_process(struct perf_tool *tool,
  735. union perf_event *event,
  736. struct perf_sample *sample __maybe_unused,
  737. struct machine *machine)
  738. {
  739. struct trace *trace = container_of(tool, struct trace, tool);
  740. return trace__process_event(trace, machine, event);
  741. }
  742. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  743. {
  744. int err = symbol__init();
  745. if (err)
  746. return err;
  747. machine__init(&trace->host, "", HOST_KERNEL_ID);
  748. machine__create_kernel_maps(&trace->host);
  749. if (perf_target__has_task(&trace->opts.target)) {
  750. err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
  751. trace__tool_process,
  752. &trace->host);
  753. } else {
  754. err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
  755. &trace->host);
  756. }
  757. if (err)
  758. symbol__exit();
  759. return err;
  760. }
  761. static int syscall__set_arg_fmts(struct syscall *sc)
  762. {
  763. struct format_field *field;
  764. int idx = 0;
  765. sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
  766. if (sc->arg_scnprintf == NULL)
  767. return -1;
  768. if (sc->fmt)
  769. sc->arg_parm = sc->fmt->arg_parm;
  770. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  771. if (sc->fmt && sc->fmt->arg_scnprintf[idx])
  772. sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
  773. else if (field->flags & FIELD_IS_POINTER)
  774. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  775. ++idx;
  776. }
  777. return 0;
  778. }
  779. static int trace__read_syscall_info(struct trace *trace, int id)
  780. {
  781. char tp_name[128];
  782. struct syscall *sc;
  783. const char *name = audit_syscall_to_name(id, trace->audit_machine);
  784. if (name == NULL)
  785. return -1;
  786. if (id > trace->syscalls.max) {
  787. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  788. if (nsyscalls == NULL)
  789. return -1;
  790. if (trace->syscalls.max != -1) {
  791. memset(nsyscalls + trace->syscalls.max + 1, 0,
  792. (id - trace->syscalls.max) * sizeof(*sc));
  793. } else {
  794. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  795. }
  796. trace->syscalls.table = nsyscalls;
  797. trace->syscalls.max = id;
  798. }
  799. sc = trace->syscalls.table + id;
  800. sc->name = name;
  801. if (trace->ev_qualifier) {
  802. bool in = strlist__find(trace->ev_qualifier, name) != NULL;
  803. if (!(in ^ trace->not_ev_qualifier)) {
  804. sc->filtered = true;
  805. /*
  806. * No need to do read tracepoint information since this will be
  807. * filtered out.
  808. */
  809. return 0;
  810. }
  811. }
  812. sc->fmt = syscall_fmt__find(sc->name);
  813. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  814. sc->tp_format = event_format__new("syscalls", tp_name);
  815. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  816. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  817. sc->tp_format = event_format__new("syscalls", tp_name);
  818. }
  819. if (sc->tp_format == NULL)
  820. return -1;
  821. return syscall__set_arg_fmts(sc);
  822. }
  823. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  824. unsigned long *args)
  825. {
  826. size_t printed = 0;
  827. if (sc->tp_format != NULL) {
  828. struct format_field *field;
  829. u8 bit = 1;
  830. struct syscall_arg arg = {
  831. .idx = 0,
  832. .mask = 0,
  833. };
  834. for (field = sc->tp_format->format.fields->next; field;
  835. field = field->next, ++arg.idx, bit <<= 1) {
  836. if (arg.mask & bit)
  837. continue;
  838. /*
  839. * Suppress this argument if its value is zero and
  840. * and we don't have a string associated in an
  841. * strarray for it.
  842. */
  843. if (args[arg.idx] == 0 &&
  844. !(sc->arg_scnprintf &&
  845. sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
  846. sc->arg_parm[arg.idx]))
  847. continue;
  848. printed += scnprintf(bf + printed, size - printed,
  849. "%s%s: ", printed ? ", " : "", field->name);
  850. if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
  851. arg.val = args[arg.idx];
  852. if (sc->arg_parm)
  853. arg.parm = sc->arg_parm[arg.idx];
  854. printed += sc->arg_scnprintf[arg.idx](bf + printed,
  855. size - printed, &arg);
  856. } else {
  857. printed += scnprintf(bf + printed, size - printed,
  858. "%ld", args[arg.idx]);
  859. }
  860. }
  861. } else {
  862. int i = 0;
  863. while (i < 6) {
  864. printed += scnprintf(bf + printed, size - printed,
  865. "%sarg%d: %ld",
  866. printed ? ", " : "", i, args[i]);
  867. ++i;
  868. }
  869. }
  870. return printed;
  871. }
  872. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  873. struct perf_sample *sample);
  874. static struct syscall *trace__syscall_info(struct trace *trace,
  875. struct perf_evsel *evsel,
  876. struct perf_sample *sample)
  877. {
  878. int id = perf_evsel__intval(evsel, sample, "id");
  879. if (id < 0) {
  880. /*
  881. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  882. * before that, leaving at a higher verbosity level till that is
  883. * explained. Reproduced with plain ftrace with:
  884. *
  885. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  886. * grep "NR -1 " /t/trace_pipe
  887. *
  888. * After generating some load on the machine.
  889. */
  890. if (verbose > 1) {
  891. static u64 n;
  892. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  893. id, perf_evsel__name(evsel), ++n);
  894. }
  895. return NULL;
  896. }
  897. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  898. trace__read_syscall_info(trace, id))
  899. goto out_cant_read;
  900. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  901. goto out_cant_read;
  902. return &trace->syscalls.table[id];
  903. out_cant_read:
  904. if (verbose) {
  905. fprintf(trace->output, "Problems reading syscall %d", id);
  906. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  907. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  908. fputs(" information\n", trace->output);
  909. }
  910. return NULL;
  911. }
  912. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  913. struct perf_sample *sample)
  914. {
  915. char *msg;
  916. void *args;
  917. size_t printed = 0;
  918. struct thread *thread;
  919. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  920. struct thread_trace *ttrace;
  921. if (sc == NULL)
  922. return -1;
  923. if (sc->filtered)
  924. return 0;
  925. thread = machine__findnew_thread(&trace->host, sample->pid,
  926. sample->tid);
  927. ttrace = thread__trace(thread, trace->output);
  928. if (ttrace == NULL)
  929. return -1;
  930. args = perf_evsel__rawptr(evsel, sample, "args");
  931. if (args == NULL) {
  932. fprintf(trace->output, "Problems reading syscall arguments\n");
  933. return -1;
  934. }
  935. ttrace = thread->priv;
  936. if (ttrace->entry_str == NULL) {
  937. ttrace->entry_str = malloc(1024);
  938. if (!ttrace->entry_str)
  939. return -1;
  940. }
  941. ttrace->entry_time = sample->time;
  942. msg = ttrace->entry_str;
  943. printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
  944. printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
  945. if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
  946. if (!trace->duration_filter) {
  947. trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
  948. fprintf(trace->output, "%-70s\n", ttrace->entry_str);
  949. }
  950. } else
  951. ttrace->entry_pending = true;
  952. return 0;
  953. }
  954. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  955. struct perf_sample *sample)
  956. {
  957. int ret;
  958. u64 duration = 0;
  959. struct thread *thread;
  960. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  961. struct thread_trace *ttrace;
  962. if (sc == NULL)
  963. return -1;
  964. if (sc->filtered)
  965. return 0;
  966. thread = machine__findnew_thread(&trace->host, sample->pid,
  967. sample->tid);
  968. ttrace = thread__trace(thread, trace->output);
  969. if (ttrace == NULL)
  970. return -1;
  971. ret = perf_evsel__intval(evsel, sample, "ret");
  972. ttrace = thread->priv;
  973. ttrace->exit_time = sample->time;
  974. if (ttrace->entry_time) {
  975. duration = sample->time - ttrace->entry_time;
  976. if (trace__filter_duration(trace, duration))
  977. goto out;
  978. } else if (trace->duration_filter)
  979. goto out;
  980. trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
  981. if (ttrace->entry_pending) {
  982. fprintf(trace->output, "%-70s", ttrace->entry_str);
  983. } else {
  984. fprintf(trace->output, " ... [");
  985. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  986. fprintf(trace->output, "]: %s()", sc->name);
  987. }
  988. if (sc->fmt == NULL) {
  989. signed_print:
  990. fprintf(trace->output, ") = %d", ret);
  991. } else if (ret < 0 && sc->fmt->errmsg) {
  992. char bf[256];
  993. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  994. *e = audit_errno_to_name(-ret);
  995. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  996. } else if (ret == 0 && sc->fmt->timeout)
  997. fprintf(trace->output, ") = 0 Timeout");
  998. else if (sc->fmt->hexret)
  999. fprintf(trace->output, ") = %#x", ret);
  1000. else
  1001. goto signed_print;
  1002. fputc('\n', trace->output);
  1003. out:
  1004. ttrace->entry_pending = false;
  1005. return 0;
  1006. }
  1007. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  1008. struct perf_sample *sample)
  1009. {
  1010. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  1011. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  1012. struct thread *thread = machine__findnew_thread(&trace->host,
  1013. sample->pid,
  1014. sample->tid);
  1015. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  1016. if (ttrace == NULL)
  1017. goto out_dump;
  1018. ttrace->runtime_ms += runtime_ms;
  1019. trace->runtime_ms += runtime_ms;
  1020. return 0;
  1021. out_dump:
  1022. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  1023. evsel->name,
  1024. perf_evsel__strval(evsel, sample, "comm"),
  1025. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  1026. runtime,
  1027. perf_evsel__intval(evsel, sample, "vruntime"));
  1028. return 0;
  1029. }
  1030. static bool skip_sample(struct trace *trace, struct perf_sample *sample)
  1031. {
  1032. if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
  1033. (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
  1034. return false;
  1035. if (trace->pid_list || trace->tid_list)
  1036. return true;
  1037. return false;
  1038. }
  1039. static int trace__process_sample(struct perf_tool *tool,
  1040. union perf_event *event __maybe_unused,
  1041. struct perf_sample *sample,
  1042. struct perf_evsel *evsel,
  1043. struct machine *machine __maybe_unused)
  1044. {
  1045. struct trace *trace = container_of(tool, struct trace, tool);
  1046. int err = 0;
  1047. tracepoint_handler handler = evsel->handler.func;
  1048. if (skip_sample(trace, sample))
  1049. return 0;
  1050. if (!trace->full_time && trace->base_time == 0)
  1051. trace->base_time = sample->time;
  1052. if (handler)
  1053. handler(trace, evsel, sample);
  1054. return err;
  1055. }
  1056. static bool
  1057. perf_session__has_tp(struct perf_session *session, const char *name)
  1058. {
  1059. struct perf_evsel *evsel;
  1060. evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
  1061. return evsel != NULL;
  1062. }
  1063. static int parse_target_str(struct trace *trace)
  1064. {
  1065. if (trace->opts.target.pid) {
  1066. trace->pid_list = intlist__new(trace->opts.target.pid);
  1067. if (trace->pid_list == NULL) {
  1068. pr_err("Error parsing process id string\n");
  1069. return -EINVAL;
  1070. }
  1071. }
  1072. if (trace->opts.target.tid) {
  1073. trace->tid_list = intlist__new(trace->opts.target.tid);
  1074. if (trace->tid_list == NULL) {
  1075. pr_err("Error parsing thread id string\n");
  1076. return -EINVAL;
  1077. }
  1078. }
  1079. return 0;
  1080. }
  1081. static int trace__run(struct trace *trace, int argc, const char **argv)
  1082. {
  1083. struct perf_evlist *evlist = perf_evlist__new();
  1084. struct perf_evsel *evsel;
  1085. int err = -1, i;
  1086. unsigned long before;
  1087. const bool forks = argc > 0;
  1088. if (evlist == NULL) {
  1089. fprintf(trace->output, "Not enough memory to run!\n");
  1090. goto out;
  1091. }
  1092. if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
  1093. perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
  1094. fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
  1095. goto out_delete_evlist;
  1096. }
  1097. if (trace->sched &&
  1098. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  1099. trace__sched_stat_runtime)) {
  1100. fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
  1101. goto out_delete_evlist;
  1102. }
  1103. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  1104. if (err < 0) {
  1105. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  1106. goto out_delete_evlist;
  1107. }
  1108. err = trace__symbols_init(trace, evlist);
  1109. if (err < 0) {
  1110. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  1111. goto out_delete_maps;
  1112. }
  1113. perf_evlist__config(evlist, &trace->opts);
  1114. signal(SIGCHLD, sig_handler);
  1115. signal(SIGINT, sig_handler);
  1116. if (forks) {
  1117. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  1118. argv, false, false);
  1119. if (err < 0) {
  1120. fprintf(trace->output, "Couldn't run the workload!\n");
  1121. goto out_delete_maps;
  1122. }
  1123. }
  1124. err = perf_evlist__open(evlist);
  1125. if (err < 0) {
  1126. fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
  1127. goto out_delete_maps;
  1128. }
  1129. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  1130. if (err < 0) {
  1131. fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
  1132. goto out_close_evlist;
  1133. }
  1134. perf_evlist__enable(evlist);
  1135. if (forks)
  1136. perf_evlist__start_workload(evlist);
  1137. trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
  1138. again:
  1139. before = trace->nr_events;
  1140. for (i = 0; i < evlist->nr_mmaps; i++) {
  1141. union perf_event *event;
  1142. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  1143. const u32 type = event->header.type;
  1144. tracepoint_handler handler;
  1145. struct perf_sample sample;
  1146. ++trace->nr_events;
  1147. err = perf_evlist__parse_sample(evlist, event, &sample);
  1148. if (err) {
  1149. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  1150. continue;
  1151. }
  1152. if (!trace->full_time && trace->base_time == 0)
  1153. trace->base_time = sample.time;
  1154. if (type != PERF_RECORD_SAMPLE) {
  1155. trace__process_event(trace, &trace->host, event);
  1156. continue;
  1157. }
  1158. evsel = perf_evlist__id2evsel(evlist, sample.id);
  1159. if (evsel == NULL) {
  1160. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  1161. continue;
  1162. }
  1163. if (sample.raw_data == NULL) {
  1164. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  1165. perf_evsel__name(evsel), sample.tid,
  1166. sample.cpu, sample.raw_size);
  1167. continue;
  1168. }
  1169. handler = evsel->handler.func;
  1170. handler(trace, evsel, &sample);
  1171. if (done)
  1172. goto out_unmap_evlist;
  1173. }
  1174. }
  1175. if (trace->nr_events == before) {
  1176. if (done)
  1177. goto out_unmap_evlist;
  1178. poll(evlist->pollfd, evlist->nr_fds, -1);
  1179. }
  1180. if (done)
  1181. perf_evlist__disable(evlist);
  1182. goto again;
  1183. out_unmap_evlist:
  1184. perf_evlist__munmap(evlist);
  1185. out_close_evlist:
  1186. perf_evlist__close(evlist);
  1187. out_delete_maps:
  1188. perf_evlist__delete_maps(evlist);
  1189. out_delete_evlist:
  1190. perf_evlist__delete(evlist);
  1191. out:
  1192. return err;
  1193. }
  1194. static int trace__replay(struct trace *trace)
  1195. {
  1196. const struct perf_evsel_str_handler handlers[] = {
  1197. { "raw_syscalls:sys_enter", trace__sys_enter, },
  1198. { "raw_syscalls:sys_exit", trace__sys_exit, },
  1199. };
  1200. struct perf_session *session;
  1201. int err = -1;
  1202. trace->tool.sample = trace__process_sample;
  1203. trace->tool.mmap = perf_event__process_mmap;
  1204. trace->tool.mmap2 = perf_event__process_mmap2;
  1205. trace->tool.comm = perf_event__process_comm;
  1206. trace->tool.exit = perf_event__process_exit;
  1207. trace->tool.fork = perf_event__process_fork;
  1208. trace->tool.attr = perf_event__process_attr;
  1209. trace->tool.tracing_data = perf_event__process_tracing_data;
  1210. trace->tool.build_id = perf_event__process_build_id;
  1211. trace->tool.ordered_samples = true;
  1212. trace->tool.ordering_requires_timestamps = true;
  1213. /* add tid to output */
  1214. trace->multiple_threads = true;
  1215. if (symbol__init() < 0)
  1216. return -1;
  1217. session = perf_session__new(input_name, O_RDONLY, 0, false,
  1218. &trace->tool);
  1219. if (session == NULL)
  1220. return -ENOMEM;
  1221. err = perf_session__set_tracepoints_handlers(session, handlers);
  1222. if (err)
  1223. goto out;
  1224. if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
  1225. pr_err("Data file does not have raw_syscalls:sys_enter events\n");
  1226. goto out;
  1227. }
  1228. if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
  1229. pr_err("Data file does not have raw_syscalls:sys_exit events\n");
  1230. goto out;
  1231. }
  1232. err = parse_target_str(trace);
  1233. if (err != 0)
  1234. goto out;
  1235. setup_pager();
  1236. err = perf_session__process_events(session, &trace->tool);
  1237. if (err)
  1238. pr_err("Failed to process events, error %d", err);
  1239. out:
  1240. perf_session__delete(session);
  1241. return err;
  1242. }
  1243. static size_t trace__fprintf_threads_header(FILE *fp)
  1244. {
  1245. size_t printed;
  1246. printed = fprintf(fp, "\n _____________________________________________________________________\n");
  1247. printed += fprintf(fp," __) Summary of events (__\n\n");
  1248. printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
  1249. printed += fprintf(fp," _____________________________________________________________________\n\n");
  1250. return printed;
  1251. }
  1252. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  1253. {
  1254. size_t printed = trace__fprintf_threads_header(fp);
  1255. struct rb_node *nd;
  1256. for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
  1257. struct thread *thread = rb_entry(nd, struct thread, rb_node);
  1258. struct thread_trace *ttrace = thread->priv;
  1259. const char *color;
  1260. double ratio;
  1261. if (ttrace == NULL)
  1262. continue;
  1263. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  1264. color = PERF_COLOR_NORMAL;
  1265. if (ratio > 50.0)
  1266. color = PERF_COLOR_RED;
  1267. else if (ratio > 25.0)
  1268. color = PERF_COLOR_GREEN;
  1269. else if (ratio > 5.0)
  1270. color = PERF_COLOR_YELLOW;
  1271. printed += color_fprintf(fp, color, "%20s", thread->comm);
  1272. printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
  1273. printed += color_fprintf(fp, color, "%5.1f%%", ratio);
  1274. printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
  1275. }
  1276. return printed;
  1277. }
  1278. static int trace__set_duration(const struct option *opt, const char *str,
  1279. int unset __maybe_unused)
  1280. {
  1281. struct trace *trace = opt->value;
  1282. trace->duration_filter = atof(str);
  1283. return 0;
  1284. }
  1285. static int trace__open_output(struct trace *trace, const char *filename)
  1286. {
  1287. struct stat st;
  1288. if (!stat(filename, &st) && st.st_size) {
  1289. char oldname[PATH_MAX];
  1290. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  1291. unlink(oldname);
  1292. rename(filename, oldname);
  1293. }
  1294. trace->output = fopen(filename, "w");
  1295. return trace->output == NULL ? -errno : 0;
  1296. }
  1297. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  1298. {
  1299. const char * const trace_usage[] = {
  1300. "perf trace [<options>] [<command>]",
  1301. "perf trace [<options>] -- <command> [<options>]",
  1302. NULL
  1303. };
  1304. struct trace trace = {
  1305. .audit_machine = audit_detect_machine(),
  1306. .syscalls = {
  1307. . max = -1,
  1308. },
  1309. .opts = {
  1310. .target = {
  1311. .uid = UINT_MAX,
  1312. .uses_mmap = true,
  1313. },
  1314. .user_freq = UINT_MAX,
  1315. .user_interval = ULLONG_MAX,
  1316. .no_delay = true,
  1317. .mmap_pages = 1024,
  1318. },
  1319. .output = stdout,
  1320. .show_comm = true,
  1321. };
  1322. const char *output_name = NULL;
  1323. const char *ev_qualifier_str = NULL;
  1324. const struct option trace_options[] = {
  1325. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  1326. "show the thread COMM next to its id"),
  1327. OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
  1328. "list of events to trace"),
  1329. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1330. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  1331. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  1332. "trace events on existing process id"),
  1333. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  1334. "trace events on existing thread id"),
  1335. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  1336. "system-wide collection from all CPUs"),
  1337. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  1338. "list of cpus to monitor"),
  1339. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  1340. "child tasks do not inherit counters"),
  1341. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  1342. "number of mmap data pages",
  1343. perf_evlist__parse_mmap_pages),
  1344. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  1345. "user to profile"),
  1346. OPT_CALLBACK(0, "duration", &trace, "float",
  1347. "show only events with duration > N.M ms",
  1348. trace__set_duration),
  1349. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  1350. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  1351. OPT_BOOLEAN('T', "time", &trace.full_time,
  1352. "Show full timestamp, not time relative to first start"),
  1353. OPT_END()
  1354. };
  1355. int err;
  1356. char bf[BUFSIZ];
  1357. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  1358. if (output_name != NULL) {
  1359. err = trace__open_output(&trace, output_name);
  1360. if (err < 0) {
  1361. perror("failed to create output file");
  1362. goto out;
  1363. }
  1364. }
  1365. if (ev_qualifier_str != NULL) {
  1366. const char *s = ev_qualifier_str;
  1367. trace.not_ev_qualifier = *s == '!';
  1368. if (trace.not_ev_qualifier)
  1369. ++s;
  1370. trace.ev_qualifier = strlist__new(true, s);
  1371. if (trace.ev_qualifier == NULL) {
  1372. fputs("Not enough memory to parse event qualifier",
  1373. trace.output);
  1374. err = -ENOMEM;
  1375. goto out_close;
  1376. }
  1377. }
  1378. err = perf_target__validate(&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. err = perf_target__parse_uid(&trace.opts.target);
  1385. if (err) {
  1386. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  1387. fprintf(trace.output, "%s", bf);
  1388. goto out_close;
  1389. }
  1390. if (!argc && perf_target__none(&trace.opts.target))
  1391. trace.opts.target.system_wide = true;
  1392. if (input_name)
  1393. err = trace__replay(&trace);
  1394. else
  1395. err = trace__run(&trace, argc, argv);
  1396. if (trace.sched && !err)
  1397. trace__fprintf_thread_summary(&trace, trace.output);
  1398. out_close:
  1399. if (output_name != NULL)
  1400. fclose(trace.output);
  1401. out:
  1402. return err;
  1403. }