builtin-trace.c 58 KB

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