builtin-trace.c 38 KB

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