builtin-trace.c 42 KB

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