builtin-trace.c 64 KB

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