builtin-trace.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  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. err = machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  1137. evlist->threads, trace__tool_process, false);
  1138. if (err)
  1139. symbol__exit();
  1140. return err;
  1141. }
  1142. static int syscall__set_arg_fmts(struct syscall *sc)
  1143. {
  1144. struct format_field *field;
  1145. int idx = 0;
  1146. sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
  1147. if (sc->arg_scnprintf == NULL)
  1148. return -1;
  1149. if (sc->fmt)
  1150. sc->arg_parm = sc->fmt->arg_parm;
  1151. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  1152. if (sc->fmt && sc->fmt->arg_scnprintf[idx])
  1153. sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
  1154. else if (field->flags & FIELD_IS_POINTER)
  1155. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  1156. ++idx;
  1157. }
  1158. return 0;
  1159. }
  1160. static int trace__read_syscall_info(struct trace *trace, int id)
  1161. {
  1162. char tp_name[128];
  1163. struct syscall *sc;
  1164. const char *name = audit_syscall_to_name(id, trace->audit.machine);
  1165. if (name == NULL)
  1166. return -1;
  1167. if (id > trace->syscalls.max) {
  1168. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  1169. if (nsyscalls == NULL)
  1170. return -1;
  1171. if (trace->syscalls.max != -1) {
  1172. memset(nsyscalls + trace->syscalls.max + 1, 0,
  1173. (id - trace->syscalls.max) * sizeof(*sc));
  1174. } else {
  1175. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  1176. }
  1177. trace->syscalls.table = nsyscalls;
  1178. trace->syscalls.max = id;
  1179. }
  1180. sc = trace->syscalls.table + id;
  1181. sc->name = name;
  1182. if (trace->ev_qualifier) {
  1183. bool in = strlist__find(trace->ev_qualifier, name) != NULL;
  1184. if (!(in ^ trace->not_ev_qualifier)) {
  1185. sc->filtered = true;
  1186. /*
  1187. * No need to do read tracepoint information since this will be
  1188. * filtered out.
  1189. */
  1190. return 0;
  1191. }
  1192. }
  1193. sc->fmt = syscall_fmt__find(sc->name);
  1194. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1195. sc->tp_format = event_format__new("syscalls", tp_name);
  1196. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  1197. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1198. sc->tp_format = event_format__new("syscalls", tp_name);
  1199. }
  1200. if (sc->tp_format == NULL)
  1201. return -1;
  1202. return syscall__set_arg_fmts(sc);
  1203. }
  1204. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  1205. unsigned long *args, struct trace *trace,
  1206. struct thread *thread)
  1207. {
  1208. size_t printed = 0;
  1209. if (sc->tp_format != NULL) {
  1210. struct format_field *field;
  1211. u8 bit = 1;
  1212. struct syscall_arg arg = {
  1213. .idx = 0,
  1214. .mask = 0,
  1215. .trace = trace,
  1216. .thread = thread,
  1217. };
  1218. for (field = sc->tp_format->format.fields->next; field;
  1219. field = field->next, ++arg.idx, bit <<= 1) {
  1220. if (arg.mask & bit)
  1221. continue;
  1222. /*
  1223. * Suppress this argument if its value is zero and
  1224. * and we don't have a string associated in an
  1225. * strarray for it.
  1226. */
  1227. if (args[arg.idx] == 0 &&
  1228. !(sc->arg_scnprintf &&
  1229. sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
  1230. sc->arg_parm[arg.idx]))
  1231. continue;
  1232. printed += scnprintf(bf + printed, size - printed,
  1233. "%s%s: ", printed ? ", " : "", field->name);
  1234. if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
  1235. arg.val = args[arg.idx];
  1236. if (sc->arg_parm)
  1237. arg.parm = sc->arg_parm[arg.idx];
  1238. printed += sc->arg_scnprintf[arg.idx](bf + printed,
  1239. size - printed, &arg);
  1240. } else {
  1241. printed += scnprintf(bf + printed, size - printed,
  1242. "%ld", args[arg.idx]);
  1243. }
  1244. }
  1245. } else {
  1246. int i = 0;
  1247. while (i < 6) {
  1248. printed += scnprintf(bf + printed, size - printed,
  1249. "%sarg%d: %ld",
  1250. printed ? ", " : "", i, args[i]);
  1251. ++i;
  1252. }
  1253. }
  1254. return printed;
  1255. }
  1256. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  1257. struct perf_sample *sample);
  1258. static struct syscall *trace__syscall_info(struct trace *trace,
  1259. struct perf_evsel *evsel, int id)
  1260. {
  1261. if (id < 0) {
  1262. /*
  1263. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  1264. * before that, leaving at a higher verbosity level till that is
  1265. * explained. Reproduced with plain ftrace with:
  1266. *
  1267. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  1268. * grep "NR -1 " /t/trace_pipe
  1269. *
  1270. * After generating some load on the machine.
  1271. */
  1272. if (verbose > 1) {
  1273. static u64 n;
  1274. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  1275. id, perf_evsel__name(evsel), ++n);
  1276. }
  1277. return NULL;
  1278. }
  1279. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  1280. trace__read_syscall_info(trace, id))
  1281. goto out_cant_read;
  1282. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  1283. goto out_cant_read;
  1284. return &trace->syscalls.table[id];
  1285. out_cant_read:
  1286. if (verbose) {
  1287. fprintf(trace->output, "Problems reading syscall %d", id);
  1288. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  1289. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  1290. fputs(" information\n", trace->output);
  1291. }
  1292. return NULL;
  1293. }
  1294. static void thread__update_stats(struct thread_trace *ttrace,
  1295. int id, struct perf_sample *sample)
  1296. {
  1297. struct int_node *inode;
  1298. struct stats *stats;
  1299. u64 duration = 0;
  1300. inode = intlist__findnew(ttrace->syscall_stats, id);
  1301. if (inode == NULL)
  1302. return;
  1303. stats = inode->priv;
  1304. if (stats == NULL) {
  1305. stats = malloc(sizeof(struct stats));
  1306. if (stats == NULL)
  1307. return;
  1308. init_stats(stats);
  1309. inode->priv = stats;
  1310. }
  1311. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  1312. duration = sample->time - ttrace->entry_time;
  1313. update_stats(stats, duration);
  1314. }
  1315. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  1316. struct perf_sample *sample)
  1317. {
  1318. char *msg;
  1319. void *args;
  1320. size_t printed = 0;
  1321. struct thread *thread;
  1322. int id = perf_evsel__sc_tp_uint(evsel, id, sample);
  1323. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1324. struct thread_trace *ttrace;
  1325. if (sc == NULL)
  1326. return -1;
  1327. if (sc->filtered)
  1328. return 0;
  1329. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1330. ttrace = thread__trace(thread, trace->output);
  1331. if (ttrace == NULL)
  1332. return -1;
  1333. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1334. ttrace = thread->priv;
  1335. if (ttrace->entry_str == NULL) {
  1336. ttrace->entry_str = malloc(1024);
  1337. if (!ttrace->entry_str)
  1338. return -1;
  1339. }
  1340. ttrace->entry_time = sample->time;
  1341. msg = ttrace->entry_str;
  1342. printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
  1343. printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed,
  1344. args, trace, thread);
  1345. if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
  1346. if (!trace->duration_filter) {
  1347. trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
  1348. fprintf(trace->output, "%-70s\n", ttrace->entry_str);
  1349. }
  1350. } else
  1351. ttrace->entry_pending = true;
  1352. return 0;
  1353. }
  1354. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  1355. struct perf_sample *sample)
  1356. {
  1357. int ret;
  1358. u64 duration = 0;
  1359. struct thread *thread;
  1360. int id = perf_evsel__sc_tp_uint(evsel, id, sample);
  1361. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1362. struct thread_trace *ttrace;
  1363. if (sc == NULL)
  1364. return -1;
  1365. if (sc->filtered)
  1366. return 0;
  1367. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1368. ttrace = thread__trace(thread, trace->output);
  1369. if (ttrace == NULL)
  1370. return -1;
  1371. if (trace->summary)
  1372. thread__update_stats(ttrace, id, sample);
  1373. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  1374. if (id == trace->audit.open_id && ret >= 0 && trace->last_vfs_getname) {
  1375. trace__set_fd_pathname(thread, ret, trace->last_vfs_getname);
  1376. trace->last_vfs_getname = NULL;
  1377. ++trace->stats.vfs_getname;
  1378. }
  1379. ttrace = thread->priv;
  1380. ttrace->exit_time = sample->time;
  1381. if (ttrace->entry_time) {
  1382. duration = sample->time - ttrace->entry_time;
  1383. if (trace__filter_duration(trace, duration))
  1384. goto out;
  1385. } else if (trace->duration_filter)
  1386. goto out;
  1387. trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
  1388. if (ttrace->entry_pending) {
  1389. fprintf(trace->output, "%-70s", ttrace->entry_str);
  1390. } else {
  1391. fprintf(trace->output, " ... [");
  1392. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  1393. fprintf(trace->output, "]: %s()", sc->name);
  1394. }
  1395. if (sc->fmt == NULL) {
  1396. signed_print:
  1397. fprintf(trace->output, ") = %d", ret);
  1398. } else if (ret < 0 && sc->fmt->errmsg) {
  1399. char bf[256];
  1400. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  1401. *e = audit_errno_to_name(-ret);
  1402. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  1403. } else if (ret == 0 && sc->fmt->timeout)
  1404. fprintf(trace->output, ") = 0 Timeout");
  1405. else if (sc->fmt->hexret)
  1406. fprintf(trace->output, ") = %#x", ret);
  1407. else
  1408. goto signed_print;
  1409. fputc('\n', trace->output);
  1410. out:
  1411. ttrace->entry_pending = false;
  1412. return 0;
  1413. }
  1414. static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
  1415. struct perf_sample *sample)
  1416. {
  1417. trace->last_vfs_getname = perf_evsel__rawptr(evsel, sample, "pathname");
  1418. return 0;
  1419. }
  1420. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  1421. struct perf_sample *sample)
  1422. {
  1423. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  1424. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  1425. struct thread *thread = machine__findnew_thread(trace->host,
  1426. sample->pid,
  1427. sample->tid);
  1428. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  1429. if (ttrace == NULL)
  1430. goto out_dump;
  1431. ttrace->runtime_ms += runtime_ms;
  1432. trace->runtime_ms += runtime_ms;
  1433. return 0;
  1434. out_dump:
  1435. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  1436. evsel->name,
  1437. perf_evsel__strval(evsel, sample, "comm"),
  1438. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  1439. runtime,
  1440. perf_evsel__intval(evsel, sample, "vruntime"));
  1441. return 0;
  1442. }
  1443. static bool skip_sample(struct trace *trace, struct perf_sample *sample)
  1444. {
  1445. if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
  1446. (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
  1447. return false;
  1448. if (trace->pid_list || trace->tid_list)
  1449. return true;
  1450. return false;
  1451. }
  1452. static int trace__process_sample(struct perf_tool *tool,
  1453. union perf_event *event __maybe_unused,
  1454. struct perf_sample *sample,
  1455. struct perf_evsel *evsel,
  1456. struct machine *machine __maybe_unused)
  1457. {
  1458. struct trace *trace = container_of(tool, struct trace, tool);
  1459. int err = 0;
  1460. tracepoint_handler handler = evsel->handler;
  1461. if (skip_sample(trace, sample))
  1462. return 0;
  1463. if (!trace->full_time && trace->base_time == 0)
  1464. trace->base_time = sample->time;
  1465. if (handler)
  1466. handler(trace, evsel, sample);
  1467. return err;
  1468. }
  1469. static bool
  1470. perf_session__has_tp(struct perf_session *session, const char *name)
  1471. {
  1472. struct perf_evsel *evsel;
  1473. evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
  1474. return evsel != NULL;
  1475. }
  1476. static int parse_target_str(struct trace *trace)
  1477. {
  1478. if (trace->opts.target.pid) {
  1479. trace->pid_list = intlist__new(trace->opts.target.pid);
  1480. if (trace->pid_list == NULL) {
  1481. pr_err("Error parsing process id string\n");
  1482. return -EINVAL;
  1483. }
  1484. }
  1485. if (trace->opts.target.tid) {
  1486. trace->tid_list = intlist__new(trace->opts.target.tid);
  1487. if (trace->tid_list == NULL) {
  1488. pr_err("Error parsing thread id string\n");
  1489. return -EINVAL;
  1490. }
  1491. }
  1492. return 0;
  1493. }
  1494. static int trace__record(int argc, const char **argv)
  1495. {
  1496. unsigned int rec_argc, i, j;
  1497. const char **rec_argv;
  1498. const char * const record_args[] = {
  1499. "record",
  1500. "-R",
  1501. "-m", "1024",
  1502. "-c", "1",
  1503. "-e", "raw_syscalls:sys_enter,raw_syscalls:sys_exit",
  1504. };
  1505. rec_argc = ARRAY_SIZE(record_args) + argc;
  1506. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1507. if (rec_argv == NULL)
  1508. return -ENOMEM;
  1509. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1510. rec_argv[i] = record_args[i];
  1511. for (j = 0; j < (unsigned int)argc; j++, i++)
  1512. rec_argv[i] = argv[j];
  1513. return cmd_record(i, rec_argv, NULL);
  1514. }
  1515. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  1516. static void perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
  1517. {
  1518. struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
  1519. if (evsel == NULL)
  1520. return;
  1521. if (perf_evsel__field(evsel, "pathname") == NULL) {
  1522. perf_evsel__delete(evsel);
  1523. return;
  1524. }
  1525. evsel->handler = trace__vfs_getname;
  1526. perf_evlist__add(evlist, evsel);
  1527. }
  1528. static int trace__run(struct trace *trace, int argc, const char **argv)
  1529. {
  1530. struct perf_evlist *evlist = perf_evlist__new();
  1531. struct perf_evsel *evsel;
  1532. int err = -1, i;
  1533. unsigned long before;
  1534. const bool forks = argc > 0;
  1535. trace->live = true;
  1536. if (evlist == NULL) {
  1537. fprintf(trace->output, "Not enough memory to run!\n");
  1538. goto out;
  1539. }
  1540. if (perf_evlist__add_syscall_newtp(evlist, trace__sys_enter, trace__sys_exit))
  1541. goto out_error_tp;
  1542. perf_evlist__add_vfs_getname(evlist);
  1543. if (trace->sched &&
  1544. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  1545. trace__sched_stat_runtime))
  1546. goto out_error_tp;
  1547. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  1548. if (err < 0) {
  1549. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  1550. goto out_delete_evlist;
  1551. }
  1552. err = trace__symbols_init(trace, evlist);
  1553. if (err < 0) {
  1554. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  1555. goto out_delete_maps;
  1556. }
  1557. perf_evlist__config(evlist, &trace->opts);
  1558. signal(SIGCHLD, sig_handler);
  1559. signal(SIGINT, sig_handler);
  1560. if (forks) {
  1561. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  1562. argv, false, false);
  1563. if (err < 0) {
  1564. fprintf(trace->output, "Couldn't run the workload!\n");
  1565. goto out_delete_maps;
  1566. }
  1567. }
  1568. err = perf_evlist__open(evlist);
  1569. if (err < 0)
  1570. goto out_error_open;
  1571. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  1572. if (err < 0) {
  1573. fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
  1574. goto out_close_evlist;
  1575. }
  1576. perf_evlist__enable(evlist);
  1577. if (forks)
  1578. perf_evlist__start_workload(evlist);
  1579. trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
  1580. again:
  1581. before = trace->nr_events;
  1582. for (i = 0; i < evlist->nr_mmaps; i++) {
  1583. union perf_event *event;
  1584. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  1585. const u32 type = event->header.type;
  1586. tracepoint_handler handler;
  1587. struct perf_sample sample;
  1588. ++trace->nr_events;
  1589. err = perf_evlist__parse_sample(evlist, event, &sample);
  1590. if (err) {
  1591. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  1592. goto next_event;
  1593. }
  1594. if (!trace->full_time && trace->base_time == 0)
  1595. trace->base_time = sample.time;
  1596. if (type != PERF_RECORD_SAMPLE) {
  1597. trace__process_event(trace, trace->host, event, &sample);
  1598. continue;
  1599. }
  1600. evsel = perf_evlist__id2evsel(evlist, sample.id);
  1601. if (evsel == NULL) {
  1602. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  1603. goto next_event;
  1604. }
  1605. if (sample.raw_data == NULL) {
  1606. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  1607. perf_evsel__name(evsel), sample.tid,
  1608. sample.cpu, sample.raw_size);
  1609. goto next_event;
  1610. }
  1611. handler = evsel->handler;
  1612. handler(trace, evsel, &sample);
  1613. next_event:
  1614. perf_evlist__mmap_consume(evlist, i);
  1615. if (interrupted)
  1616. goto out_disable;
  1617. }
  1618. }
  1619. if (trace->nr_events == before) {
  1620. int timeout = done ? 100 : -1;
  1621. if (poll(evlist->pollfd, evlist->nr_fds, timeout) > 0)
  1622. goto again;
  1623. } else {
  1624. goto again;
  1625. }
  1626. out_disable:
  1627. perf_evlist__disable(evlist);
  1628. if (!err) {
  1629. if (trace->summary)
  1630. trace__fprintf_thread_summary(trace, trace->output);
  1631. if (trace->show_tool_stats) {
  1632. fprintf(trace->output, "Stats:\n "
  1633. " vfs_getname : %" PRIu64 "\n"
  1634. " proc_getname: %" PRIu64 "\n",
  1635. trace->stats.vfs_getname,
  1636. trace->stats.proc_getname);
  1637. }
  1638. }
  1639. perf_evlist__munmap(evlist);
  1640. out_close_evlist:
  1641. perf_evlist__close(evlist);
  1642. out_delete_maps:
  1643. perf_evlist__delete_maps(evlist);
  1644. out_delete_evlist:
  1645. perf_evlist__delete(evlist);
  1646. out:
  1647. trace->live = false;
  1648. return err;
  1649. {
  1650. char errbuf[BUFSIZ];
  1651. out_error_tp:
  1652. perf_evlist__strerror_tp(evlist, errno, errbuf, sizeof(errbuf));
  1653. goto out_error;
  1654. out_error_open:
  1655. perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  1656. out_error:
  1657. fprintf(trace->output, "%s\n", errbuf);
  1658. goto out_delete_evlist;
  1659. }
  1660. }
  1661. static int trace__replay(struct trace *trace)
  1662. {
  1663. const struct perf_evsel_str_handler handlers[] = {
  1664. { "raw_syscalls:sys_enter", trace__sys_enter, },
  1665. { "raw_syscalls:sys_exit", trace__sys_exit, },
  1666. { "probe:vfs_getname", trace__vfs_getname, },
  1667. };
  1668. struct perf_data_file file = {
  1669. .path = input_name,
  1670. .mode = PERF_DATA_MODE_READ,
  1671. };
  1672. struct perf_session *session;
  1673. int err = -1;
  1674. trace->tool.sample = trace__process_sample;
  1675. trace->tool.mmap = perf_event__process_mmap;
  1676. trace->tool.mmap2 = perf_event__process_mmap2;
  1677. trace->tool.comm = perf_event__process_comm;
  1678. trace->tool.exit = perf_event__process_exit;
  1679. trace->tool.fork = perf_event__process_fork;
  1680. trace->tool.attr = perf_event__process_attr;
  1681. trace->tool.tracing_data = perf_event__process_tracing_data;
  1682. trace->tool.build_id = perf_event__process_build_id;
  1683. trace->tool.ordered_samples = true;
  1684. trace->tool.ordering_requires_timestamps = true;
  1685. /* add tid to output */
  1686. trace->multiple_threads = true;
  1687. if (symbol__init() < 0)
  1688. return -1;
  1689. session = perf_session__new(&file, false, &trace->tool);
  1690. if (session == NULL)
  1691. return -ENOMEM;
  1692. trace->host = &session->machines.host;
  1693. err = perf_session__set_tracepoints_handlers(session, handlers);
  1694. if (err)
  1695. goto out;
  1696. if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
  1697. pr_err("Data file does not have raw_syscalls:sys_enter events\n");
  1698. goto out;
  1699. }
  1700. if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
  1701. pr_err("Data file does not have raw_syscalls:sys_exit events\n");
  1702. goto out;
  1703. }
  1704. err = parse_target_str(trace);
  1705. if (err != 0)
  1706. goto out;
  1707. setup_pager();
  1708. err = perf_session__process_events(session, &trace->tool);
  1709. if (err)
  1710. pr_err("Failed to process events, error %d", err);
  1711. else if (trace->summary)
  1712. trace__fprintf_thread_summary(trace, trace->output);
  1713. out:
  1714. perf_session__delete(session);
  1715. return err;
  1716. }
  1717. static size_t trace__fprintf_threads_header(FILE *fp)
  1718. {
  1719. size_t printed;
  1720. printed = fprintf(fp, "\n _____________________________________________________________________________\n");
  1721. printed += fprintf(fp, " __) Summary of events (__\n\n");
  1722. printed += fprintf(fp, " [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
  1723. printed += fprintf(fp, " syscall count min max avg stddev\n");
  1724. printed += fprintf(fp, " msec msec msec %%\n");
  1725. printed += fprintf(fp, " _____________________________________________________________________________\n\n");
  1726. return printed;
  1727. }
  1728. static size_t thread__dump_stats(struct thread_trace *ttrace,
  1729. struct trace *trace, FILE *fp)
  1730. {
  1731. struct stats *stats;
  1732. size_t printed = 0;
  1733. struct syscall *sc;
  1734. struct int_node *inode = intlist__first(ttrace->syscall_stats);
  1735. if (inode == NULL)
  1736. return 0;
  1737. printed += fprintf(fp, "\n");
  1738. /* each int_node is a syscall */
  1739. while (inode) {
  1740. stats = inode->priv;
  1741. if (stats) {
  1742. double min = (double)(stats->min) / NSEC_PER_MSEC;
  1743. double max = (double)(stats->max) / NSEC_PER_MSEC;
  1744. double avg = avg_stats(stats);
  1745. double pct;
  1746. u64 n = (u64) stats->n;
  1747. pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
  1748. avg /= NSEC_PER_MSEC;
  1749. sc = &trace->syscalls.table[inode->i];
  1750. printed += fprintf(fp, "%24s %14s : ", "", sc->name);
  1751. printed += fprintf(fp, "%5" PRIu64 " %8.3f %8.3f",
  1752. n, min, max);
  1753. printed += fprintf(fp, " %8.3f %6.2f\n", avg, pct);
  1754. }
  1755. inode = intlist__next(inode);
  1756. }
  1757. printed += fprintf(fp, "\n\n");
  1758. return printed;
  1759. }
  1760. /* struct used to pass data to per-thread function */
  1761. struct summary_data {
  1762. FILE *fp;
  1763. struct trace *trace;
  1764. size_t printed;
  1765. };
  1766. static int trace__fprintf_one_thread(struct thread *thread, void *priv)
  1767. {
  1768. struct summary_data *data = priv;
  1769. FILE *fp = data->fp;
  1770. size_t printed = data->printed;
  1771. struct trace *trace = data->trace;
  1772. struct thread_trace *ttrace = thread->priv;
  1773. const char *color;
  1774. double ratio;
  1775. if (ttrace == NULL)
  1776. return 0;
  1777. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  1778. color = PERF_COLOR_NORMAL;
  1779. if (ratio > 50.0)
  1780. color = PERF_COLOR_RED;
  1781. else if (ratio > 25.0)
  1782. color = PERF_COLOR_GREEN;
  1783. else if (ratio > 5.0)
  1784. color = PERF_COLOR_YELLOW;
  1785. printed += color_fprintf(fp, color, "%20s", thread__comm_str(thread));
  1786. printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
  1787. printed += color_fprintf(fp, color, "%5.1f%%", ratio);
  1788. printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
  1789. printed += thread__dump_stats(ttrace, trace, fp);
  1790. data->printed += printed;
  1791. return 0;
  1792. }
  1793. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  1794. {
  1795. struct summary_data data = {
  1796. .fp = fp,
  1797. .trace = trace
  1798. };
  1799. data.printed = trace__fprintf_threads_header(fp);
  1800. machine__for_each_thread(trace->host, trace__fprintf_one_thread, &data);
  1801. return data.printed;
  1802. }
  1803. static int trace__set_duration(const struct option *opt, const char *str,
  1804. int unset __maybe_unused)
  1805. {
  1806. struct trace *trace = opt->value;
  1807. trace->duration_filter = atof(str);
  1808. return 0;
  1809. }
  1810. static int trace__open_output(struct trace *trace, const char *filename)
  1811. {
  1812. struct stat st;
  1813. if (!stat(filename, &st) && st.st_size) {
  1814. char oldname[PATH_MAX];
  1815. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  1816. unlink(oldname);
  1817. rename(filename, oldname);
  1818. }
  1819. trace->output = fopen(filename, "w");
  1820. return trace->output == NULL ? -errno : 0;
  1821. }
  1822. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  1823. {
  1824. const char * const trace_usage[] = {
  1825. "perf trace [<options>] [<command>]",
  1826. "perf trace [<options>] -- <command> [<options>]",
  1827. "perf trace record [<options>] [<command>]",
  1828. "perf trace record [<options>] -- <command> [<options>]",
  1829. NULL
  1830. };
  1831. struct trace trace = {
  1832. .audit = {
  1833. .machine = audit_detect_machine(),
  1834. .open_id = audit_name_to_syscall("open", trace.audit.machine),
  1835. },
  1836. .syscalls = {
  1837. . max = -1,
  1838. },
  1839. .opts = {
  1840. .target = {
  1841. .uid = UINT_MAX,
  1842. .uses_mmap = true,
  1843. },
  1844. .user_freq = UINT_MAX,
  1845. .user_interval = ULLONG_MAX,
  1846. .no_delay = true,
  1847. .mmap_pages = 1024,
  1848. },
  1849. .output = stdout,
  1850. .show_comm = true,
  1851. };
  1852. const char *output_name = NULL;
  1853. const char *ev_qualifier_str = NULL;
  1854. const struct option trace_options[] = {
  1855. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  1856. "show the thread COMM next to its id"),
  1857. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  1858. OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
  1859. "list of events to trace"),
  1860. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1861. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  1862. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  1863. "trace events on existing process id"),
  1864. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  1865. "trace events on existing thread id"),
  1866. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  1867. "system-wide collection from all CPUs"),
  1868. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  1869. "list of cpus to monitor"),
  1870. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  1871. "child tasks do not inherit counters"),
  1872. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  1873. "number of mmap data pages",
  1874. perf_evlist__parse_mmap_pages),
  1875. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  1876. "user to profile"),
  1877. OPT_CALLBACK(0, "duration", &trace, "float",
  1878. "show only events with duration > N.M ms",
  1879. trace__set_duration),
  1880. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  1881. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  1882. OPT_BOOLEAN('T', "time", &trace.full_time,
  1883. "Show full timestamp, not time relative to first start"),
  1884. OPT_BOOLEAN(0, "summary", &trace.summary,
  1885. "Show syscall summary with statistics"),
  1886. OPT_END()
  1887. };
  1888. int err;
  1889. char bf[BUFSIZ];
  1890. if ((argc > 1) && (strcmp(argv[1], "record") == 0))
  1891. return trace__record(argc-2, &argv[2]);
  1892. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  1893. if (output_name != NULL) {
  1894. err = trace__open_output(&trace, output_name);
  1895. if (err < 0) {
  1896. perror("failed to create output file");
  1897. goto out;
  1898. }
  1899. }
  1900. if (ev_qualifier_str != NULL) {
  1901. const char *s = ev_qualifier_str;
  1902. trace.not_ev_qualifier = *s == '!';
  1903. if (trace.not_ev_qualifier)
  1904. ++s;
  1905. trace.ev_qualifier = strlist__new(true, s);
  1906. if (trace.ev_qualifier == NULL) {
  1907. fputs("Not enough memory to parse event qualifier",
  1908. trace.output);
  1909. err = -ENOMEM;
  1910. goto out_close;
  1911. }
  1912. }
  1913. err = perf_target__validate(&trace.opts.target);
  1914. if (err) {
  1915. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  1916. fprintf(trace.output, "%s", bf);
  1917. goto out_close;
  1918. }
  1919. err = perf_target__parse_uid(&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. if (!argc && perf_target__none(&trace.opts.target))
  1926. trace.opts.target.system_wide = true;
  1927. if (input_name)
  1928. err = trace__replay(&trace);
  1929. else
  1930. err = trace__run(&trace, argc, argv);
  1931. out_close:
  1932. if (output_name != NULL)
  1933. fclose(trace.output);
  1934. out:
  1935. return err;
  1936. }