builtin-trace.c 39 KB

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