builtin-trace.c 62 KB

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