builtin-trace.c 62 KB

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