builtin-trace.c 54 KB

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