builtin-trace.c 39 KB

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