builtin-trace.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  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/exec_cmd.h"
  7. #include "util/machine.h"
  8. #include "util/session.h"
  9. #include "util/thread.h"
  10. #include "util/parse-options.h"
  11. #include "util/strlist.h"
  12. #include "util/intlist.h"
  13. #include "util/thread_map.h"
  14. #include "util/stat.h"
  15. #include "trace-event.h"
  16. #include "util/parse-events.h"
  17. #include <libaudit.h>
  18. #include <stdlib.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. #ifndef EFD_NONBLOCK
  38. # define EFD_NONBLOCK 00004000
  39. #endif
  40. #ifndef EFD_CLOEXEC
  41. # define EFD_CLOEXEC 02000000
  42. #endif
  43. #ifndef O_CLOEXEC
  44. # define O_CLOEXEC 02000000
  45. #endif
  46. #ifndef SOCK_DCCP
  47. # define SOCK_DCCP 6
  48. #endif
  49. #ifndef SOCK_CLOEXEC
  50. # define SOCK_CLOEXEC 02000000
  51. #endif
  52. #ifndef SOCK_NONBLOCK
  53. # define SOCK_NONBLOCK 00004000
  54. #endif
  55. #ifndef MSG_CMSG_CLOEXEC
  56. # define MSG_CMSG_CLOEXEC 0x40000000
  57. #endif
  58. #ifndef PERF_FLAG_FD_NO_GROUP
  59. # define PERF_FLAG_FD_NO_GROUP (1UL << 0)
  60. #endif
  61. #ifndef PERF_FLAG_FD_OUTPUT
  62. # define PERF_FLAG_FD_OUTPUT (1UL << 1)
  63. #endif
  64. #ifndef PERF_FLAG_PID_CGROUP
  65. # define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */
  66. #endif
  67. #ifndef PERF_FLAG_FD_CLOEXEC
  68. # define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */
  69. #endif
  70. struct tp_field {
  71. int offset;
  72. union {
  73. u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
  74. void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
  75. };
  76. };
  77. #define TP_UINT_FIELD(bits) \
  78. static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
  79. { \
  80. u##bits value; \
  81. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  82. return value; \
  83. }
  84. TP_UINT_FIELD(8);
  85. TP_UINT_FIELD(16);
  86. TP_UINT_FIELD(32);
  87. TP_UINT_FIELD(64);
  88. #define TP_UINT_FIELD__SWAPPED(bits) \
  89. static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
  90. { \
  91. u##bits value; \
  92. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  93. return bswap_##bits(value);\
  94. }
  95. TP_UINT_FIELD__SWAPPED(16);
  96. TP_UINT_FIELD__SWAPPED(32);
  97. TP_UINT_FIELD__SWAPPED(64);
  98. static int tp_field__init_uint(struct tp_field *field,
  99. struct format_field *format_field,
  100. bool needs_swap)
  101. {
  102. field->offset = format_field->offset;
  103. switch (format_field->size) {
  104. case 1:
  105. field->integer = tp_field__u8;
  106. break;
  107. case 2:
  108. field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
  109. break;
  110. case 4:
  111. field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
  112. break;
  113. case 8:
  114. field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
  115. break;
  116. default:
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
  122. {
  123. return sample->raw_data + field->offset;
  124. }
  125. static int tp_field__init_ptr(struct tp_field *field, struct format_field *format_field)
  126. {
  127. field->offset = format_field->offset;
  128. field->pointer = tp_field__ptr;
  129. return 0;
  130. }
  131. struct syscall_tp {
  132. struct tp_field id;
  133. union {
  134. struct tp_field args, ret;
  135. };
  136. };
  137. static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
  138. struct tp_field *field,
  139. const char *name)
  140. {
  141. struct format_field *format_field = perf_evsel__field(evsel, name);
  142. if (format_field == NULL)
  143. return -1;
  144. return tp_field__init_uint(field, format_field, evsel->needs_swap);
  145. }
  146. #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
  147. ({ struct syscall_tp *sc = evsel->priv;\
  148. perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
  149. static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
  150. struct tp_field *field,
  151. const char *name)
  152. {
  153. struct format_field *format_field = perf_evsel__field(evsel, name);
  154. if (format_field == NULL)
  155. return -1;
  156. return tp_field__init_ptr(field, format_field);
  157. }
  158. #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
  159. ({ struct syscall_tp *sc = evsel->priv;\
  160. perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
  161. static void perf_evsel__delete_priv(struct perf_evsel *evsel)
  162. {
  163. zfree(&evsel->priv);
  164. perf_evsel__delete(evsel);
  165. }
  166. static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler)
  167. {
  168. evsel->priv = malloc(sizeof(struct syscall_tp));
  169. if (evsel->priv != NULL) {
  170. if (perf_evsel__init_sc_tp_uint_field(evsel, id))
  171. goto out_delete;
  172. evsel->handler = handler;
  173. return 0;
  174. }
  175. return -ENOMEM;
  176. out_delete:
  177. zfree(&evsel->priv);
  178. return -ENOENT;
  179. }
  180. static struct perf_evsel *perf_evsel__syscall_newtp(const char *direction, void *handler)
  181. {
  182. struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
  183. /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
  184. if (evsel == NULL)
  185. evsel = perf_evsel__newtp("syscalls", direction);
  186. if (evsel) {
  187. if (perf_evsel__init_syscall_tp(evsel, handler))
  188. goto out_delete;
  189. }
  190. return evsel;
  191. out_delete:
  192. perf_evsel__delete_priv(evsel);
  193. return NULL;
  194. }
  195. #define perf_evsel__sc_tp_uint(evsel, name, sample) \
  196. ({ struct syscall_tp *fields = evsel->priv; \
  197. fields->name.integer(&fields->name, sample); })
  198. #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
  199. ({ struct syscall_tp *fields = evsel->priv; \
  200. fields->name.pointer(&fields->name, sample); })
  201. struct syscall_arg {
  202. unsigned long val;
  203. struct thread *thread;
  204. struct trace *trace;
  205. void *parm;
  206. u8 idx;
  207. u8 mask;
  208. };
  209. struct strarray {
  210. int offset;
  211. int nr_entries;
  212. const char **entries;
  213. };
  214. #define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
  215. .nr_entries = ARRAY_SIZE(array), \
  216. .entries = array, \
  217. }
  218. #define DEFINE_STRARRAY_OFFSET(array, off) struct strarray strarray__##array = { \
  219. .offset = off, \
  220. .nr_entries = ARRAY_SIZE(array), \
  221. .entries = array, \
  222. }
  223. static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
  224. const char *intfmt,
  225. struct syscall_arg *arg)
  226. {
  227. struct strarray *sa = arg->parm;
  228. int idx = arg->val - sa->offset;
  229. if (idx < 0 || idx >= sa->nr_entries)
  230. return scnprintf(bf, size, intfmt, arg->val);
  231. return scnprintf(bf, size, "%s", sa->entries[idx]);
  232. }
  233. static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
  234. struct syscall_arg *arg)
  235. {
  236. return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
  237. }
  238. #define SCA_STRARRAY syscall_arg__scnprintf_strarray
  239. #if defined(__i386__) || defined(__x86_64__)
  240. /*
  241. * FIXME: Make this available to all arches as soon as the ioctl beautifier
  242. * gets rewritten to support all arches.
  243. */
  244. static size_t syscall_arg__scnprintf_strhexarray(char *bf, size_t size,
  245. struct syscall_arg *arg)
  246. {
  247. return __syscall_arg__scnprintf_strarray(bf, size, "%#x", arg);
  248. }
  249. #define SCA_STRHEXARRAY syscall_arg__scnprintf_strhexarray
  250. #endif /* defined(__i386__) || defined(__x86_64__) */
  251. static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
  252. struct syscall_arg *arg);
  253. #define SCA_FD syscall_arg__scnprintf_fd
  254. static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
  255. struct syscall_arg *arg)
  256. {
  257. int fd = arg->val;
  258. if (fd == AT_FDCWD)
  259. return scnprintf(bf, size, "CWD");
  260. return syscall_arg__scnprintf_fd(bf, size, arg);
  261. }
  262. #define SCA_FDAT syscall_arg__scnprintf_fd_at
  263. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  264. struct syscall_arg *arg);
  265. #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
  266. static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
  267. struct syscall_arg *arg)
  268. {
  269. return scnprintf(bf, size, "%#lx", arg->val);
  270. }
  271. #define SCA_HEX syscall_arg__scnprintf_hex
  272. static size_t syscall_arg__scnprintf_int(char *bf, size_t size,
  273. struct syscall_arg *arg)
  274. {
  275. return scnprintf(bf, size, "%d", arg->val);
  276. }
  277. #define SCA_INT syscall_arg__scnprintf_int
  278. static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
  279. struct syscall_arg *arg)
  280. {
  281. int printed = 0, prot = arg->val;
  282. if (prot == PROT_NONE)
  283. return scnprintf(bf, size, "NONE");
  284. #define P_MMAP_PROT(n) \
  285. if (prot & PROT_##n) { \
  286. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  287. prot &= ~PROT_##n; \
  288. }
  289. P_MMAP_PROT(EXEC);
  290. P_MMAP_PROT(READ);
  291. P_MMAP_PROT(WRITE);
  292. #ifdef PROT_SEM
  293. P_MMAP_PROT(SEM);
  294. #endif
  295. P_MMAP_PROT(GROWSDOWN);
  296. P_MMAP_PROT(GROWSUP);
  297. #undef P_MMAP_PROT
  298. if (prot)
  299. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
  300. return printed;
  301. }
  302. #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
  303. static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
  304. struct syscall_arg *arg)
  305. {
  306. int printed = 0, flags = arg->val;
  307. #define P_MMAP_FLAG(n) \
  308. if (flags & MAP_##n) { \
  309. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  310. flags &= ~MAP_##n; \
  311. }
  312. P_MMAP_FLAG(SHARED);
  313. P_MMAP_FLAG(PRIVATE);
  314. #ifdef MAP_32BIT
  315. P_MMAP_FLAG(32BIT);
  316. #endif
  317. P_MMAP_FLAG(ANONYMOUS);
  318. P_MMAP_FLAG(DENYWRITE);
  319. P_MMAP_FLAG(EXECUTABLE);
  320. P_MMAP_FLAG(FILE);
  321. P_MMAP_FLAG(FIXED);
  322. P_MMAP_FLAG(GROWSDOWN);
  323. #ifdef MAP_HUGETLB
  324. P_MMAP_FLAG(HUGETLB);
  325. #endif
  326. P_MMAP_FLAG(LOCKED);
  327. P_MMAP_FLAG(NONBLOCK);
  328. P_MMAP_FLAG(NORESERVE);
  329. P_MMAP_FLAG(POPULATE);
  330. P_MMAP_FLAG(STACK);
  331. #ifdef MAP_UNINITIALIZED
  332. P_MMAP_FLAG(UNINITIALIZED);
  333. #endif
  334. #undef P_MMAP_FLAG
  335. if (flags)
  336. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  337. return printed;
  338. }
  339. #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
  340. static size_t syscall_arg__scnprintf_mremap_flags(char *bf, size_t size,
  341. struct syscall_arg *arg)
  342. {
  343. int printed = 0, flags = arg->val;
  344. #define P_MREMAP_FLAG(n) \
  345. if (flags & MREMAP_##n) { \
  346. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  347. flags &= ~MREMAP_##n; \
  348. }
  349. P_MREMAP_FLAG(MAYMOVE);
  350. #ifdef MREMAP_FIXED
  351. P_MREMAP_FLAG(FIXED);
  352. #endif
  353. #undef P_MREMAP_FLAG
  354. if (flags)
  355. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  356. return printed;
  357. }
  358. #define SCA_MREMAP_FLAGS syscall_arg__scnprintf_mremap_flags
  359. static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
  360. struct syscall_arg *arg)
  361. {
  362. int behavior = arg->val;
  363. switch (behavior) {
  364. #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
  365. P_MADV_BHV(NORMAL);
  366. P_MADV_BHV(RANDOM);
  367. P_MADV_BHV(SEQUENTIAL);
  368. P_MADV_BHV(WILLNEED);
  369. P_MADV_BHV(DONTNEED);
  370. P_MADV_BHV(REMOVE);
  371. P_MADV_BHV(DONTFORK);
  372. P_MADV_BHV(DOFORK);
  373. P_MADV_BHV(HWPOISON);
  374. #ifdef MADV_SOFT_OFFLINE
  375. P_MADV_BHV(SOFT_OFFLINE);
  376. #endif
  377. P_MADV_BHV(MERGEABLE);
  378. P_MADV_BHV(UNMERGEABLE);
  379. #ifdef MADV_HUGEPAGE
  380. P_MADV_BHV(HUGEPAGE);
  381. #endif
  382. #ifdef MADV_NOHUGEPAGE
  383. P_MADV_BHV(NOHUGEPAGE);
  384. #endif
  385. #ifdef MADV_DONTDUMP
  386. P_MADV_BHV(DONTDUMP);
  387. #endif
  388. #ifdef MADV_DODUMP
  389. P_MADV_BHV(DODUMP);
  390. #endif
  391. #undef P_MADV_PHV
  392. default: break;
  393. }
  394. return scnprintf(bf, size, "%#x", behavior);
  395. }
  396. #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
  397. static size_t syscall_arg__scnprintf_flock(char *bf, size_t size,
  398. struct syscall_arg *arg)
  399. {
  400. int printed = 0, op = arg->val;
  401. if (op == 0)
  402. return scnprintf(bf, size, "NONE");
  403. #define P_CMD(cmd) \
  404. if ((op & LOCK_##cmd) == LOCK_##cmd) { \
  405. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #cmd); \
  406. op &= ~LOCK_##cmd; \
  407. }
  408. P_CMD(SH);
  409. P_CMD(EX);
  410. P_CMD(NB);
  411. P_CMD(UN);
  412. P_CMD(MAND);
  413. P_CMD(RW);
  414. P_CMD(READ);
  415. P_CMD(WRITE);
  416. #undef P_OP
  417. if (op)
  418. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", op);
  419. return printed;
  420. }
  421. #define SCA_FLOCK syscall_arg__scnprintf_flock
  422. static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg)
  423. {
  424. enum syscall_futex_args {
  425. SCF_UADDR = (1 << 0),
  426. SCF_OP = (1 << 1),
  427. SCF_VAL = (1 << 2),
  428. SCF_TIMEOUT = (1 << 3),
  429. SCF_UADDR2 = (1 << 4),
  430. SCF_VAL3 = (1 << 5),
  431. };
  432. int op = arg->val;
  433. int cmd = op & FUTEX_CMD_MASK;
  434. size_t printed = 0;
  435. switch (cmd) {
  436. #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
  437. P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
  438. P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  439. P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  440. P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break;
  441. P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break;
  442. P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break;
  443. P_FUTEX_OP(WAKE_OP); break;
  444. P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  445. P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  446. P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
  447. P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break;
  448. P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break;
  449. P_FUTEX_OP(WAIT_REQUEUE_PI); break;
  450. default: printed = scnprintf(bf, size, "%#x", cmd); break;
  451. }
  452. if (op & FUTEX_PRIVATE_FLAG)
  453. printed += scnprintf(bf + printed, size - printed, "|PRIV");
  454. if (op & FUTEX_CLOCK_REALTIME)
  455. printed += scnprintf(bf + printed, size - printed, "|CLKRT");
  456. return printed;
  457. }
  458. #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
  459. static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
  460. static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
  461. static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
  462. static DEFINE_STRARRAY(itimers);
  463. static const char *whences[] = { "SET", "CUR", "END",
  464. #ifdef SEEK_DATA
  465. "DATA",
  466. #endif
  467. #ifdef SEEK_HOLE
  468. "HOLE",
  469. #endif
  470. };
  471. static DEFINE_STRARRAY(whences);
  472. static const char *fcntl_cmds[] = {
  473. "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
  474. "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "F_GETLK64",
  475. "F_SETLK64", "F_SETLKW64", "F_SETOWN_EX", "F_GETOWN_EX",
  476. "F_GETOWNER_UIDS",
  477. };
  478. static DEFINE_STRARRAY(fcntl_cmds);
  479. static const char *rlimit_resources[] = {
  480. "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
  481. "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
  482. "RTTIME",
  483. };
  484. static DEFINE_STRARRAY(rlimit_resources);
  485. static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
  486. static DEFINE_STRARRAY(sighow);
  487. static const char *clockid[] = {
  488. "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
  489. "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE",
  490. };
  491. static DEFINE_STRARRAY(clockid);
  492. static const char *socket_families[] = {
  493. "UNSPEC", "LOCAL", "INET", "AX25", "IPX", "APPLETALK", "NETROM",
  494. "BRIDGE", "ATMPVC", "X25", "INET6", "ROSE", "DECnet", "NETBEUI",
  495. "SECURITY", "KEY", "NETLINK", "PACKET", "ASH", "ECONET", "ATMSVC",
  496. "RDS", "SNA", "IRDA", "PPPOX", "WANPIPE", "LLC", "IB", "CAN", "TIPC",
  497. "BLUETOOTH", "IUCV", "RXRPC", "ISDN", "PHONET", "IEEE802154", "CAIF",
  498. "ALG", "NFC", "VSOCK",
  499. };
  500. static DEFINE_STRARRAY(socket_families);
  501. #ifndef SOCK_TYPE_MASK
  502. #define SOCK_TYPE_MASK 0xf
  503. #endif
  504. static size_t syscall_arg__scnprintf_socket_type(char *bf, size_t size,
  505. struct syscall_arg *arg)
  506. {
  507. size_t printed;
  508. int type = arg->val,
  509. flags = type & ~SOCK_TYPE_MASK;
  510. type &= SOCK_TYPE_MASK;
  511. /*
  512. * Can't use a strarray, MIPS may override for ABI reasons.
  513. */
  514. switch (type) {
  515. #define P_SK_TYPE(n) case SOCK_##n: printed = scnprintf(bf, size, #n); break;
  516. P_SK_TYPE(STREAM);
  517. P_SK_TYPE(DGRAM);
  518. P_SK_TYPE(RAW);
  519. P_SK_TYPE(RDM);
  520. P_SK_TYPE(SEQPACKET);
  521. P_SK_TYPE(DCCP);
  522. P_SK_TYPE(PACKET);
  523. #undef P_SK_TYPE
  524. default:
  525. printed = scnprintf(bf, size, "%#x", type);
  526. }
  527. #define P_SK_FLAG(n) \
  528. if (flags & SOCK_##n) { \
  529. printed += scnprintf(bf + printed, size - printed, "|%s", #n); \
  530. flags &= ~SOCK_##n; \
  531. }
  532. P_SK_FLAG(CLOEXEC);
  533. P_SK_FLAG(NONBLOCK);
  534. #undef P_SK_FLAG
  535. if (flags)
  536. printed += scnprintf(bf + printed, size - printed, "|%#x", flags);
  537. return printed;
  538. }
  539. #define SCA_SK_TYPE syscall_arg__scnprintf_socket_type
  540. #ifndef MSG_PROBE
  541. #define MSG_PROBE 0x10
  542. #endif
  543. #ifndef MSG_WAITFORONE
  544. #define MSG_WAITFORONE 0x10000
  545. #endif
  546. #ifndef MSG_SENDPAGE_NOTLAST
  547. #define MSG_SENDPAGE_NOTLAST 0x20000
  548. #endif
  549. #ifndef MSG_FASTOPEN
  550. #define MSG_FASTOPEN 0x20000000
  551. #endif
  552. static size_t syscall_arg__scnprintf_msg_flags(char *bf, size_t size,
  553. struct syscall_arg *arg)
  554. {
  555. int printed = 0, flags = arg->val;
  556. if (flags == 0)
  557. return scnprintf(bf, size, "NONE");
  558. #define P_MSG_FLAG(n) \
  559. if (flags & MSG_##n) { \
  560. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  561. flags &= ~MSG_##n; \
  562. }
  563. P_MSG_FLAG(OOB);
  564. P_MSG_FLAG(PEEK);
  565. P_MSG_FLAG(DONTROUTE);
  566. P_MSG_FLAG(TRYHARD);
  567. P_MSG_FLAG(CTRUNC);
  568. P_MSG_FLAG(PROBE);
  569. P_MSG_FLAG(TRUNC);
  570. P_MSG_FLAG(DONTWAIT);
  571. P_MSG_FLAG(EOR);
  572. P_MSG_FLAG(WAITALL);
  573. P_MSG_FLAG(FIN);
  574. P_MSG_FLAG(SYN);
  575. P_MSG_FLAG(CONFIRM);
  576. P_MSG_FLAG(RST);
  577. P_MSG_FLAG(ERRQUEUE);
  578. P_MSG_FLAG(NOSIGNAL);
  579. P_MSG_FLAG(MORE);
  580. P_MSG_FLAG(WAITFORONE);
  581. P_MSG_FLAG(SENDPAGE_NOTLAST);
  582. P_MSG_FLAG(FASTOPEN);
  583. P_MSG_FLAG(CMSG_CLOEXEC);
  584. #undef P_MSG_FLAG
  585. if (flags)
  586. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  587. return printed;
  588. }
  589. #define SCA_MSG_FLAGS syscall_arg__scnprintf_msg_flags
  590. static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
  591. struct syscall_arg *arg)
  592. {
  593. size_t printed = 0;
  594. int mode = arg->val;
  595. if (mode == F_OK) /* 0 */
  596. return scnprintf(bf, size, "F");
  597. #define P_MODE(n) \
  598. if (mode & n##_OK) { \
  599. printed += scnprintf(bf + printed, size - printed, "%s", #n); \
  600. mode &= ~n##_OK; \
  601. }
  602. P_MODE(R);
  603. P_MODE(W);
  604. P_MODE(X);
  605. #undef P_MODE
  606. if (mode)
  607. printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
  608. return printed;
  609. }
  610. #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
  611. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  612. struct syscall_arg *arg);
  613. #define SCA_FILENAME syscall_arg__scnprintf_filename
  614. static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
  615. struct syscall_arg *arg)
  616. {
  617. int printed = 0, flags = arg->val;
  618. if (!(flags & O_CREAT))
  619. arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
  620. if (flags == 0)
  621. return scnprintf(bf, size, "RDONLY");
  622. #define P_FLAG(n) \
  623. if (flags & O_##n) { \
  624. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  625. flags &= ~O_##n; \
  626. }
  627. P_FLAG(APPEND);
  628. P_FLAG(ASYNC);
  629. P_FLAG(CLOEXEC);
  630. P_FLAG(CREAT);
  631. P_FLAG(DIRECT);
  632. P_FLAG(DIRECTORY);
  633. P_FLAG(EXCL);
  634. P_FLAG(LARGEFILE);
  635. P_FLAG(NOATIME);
  636. P_FLAG(NOCTTY);
  637. #ifdef O_NONBLOCK
  638. P_FLAG(NONBLOCK);
  639. #elif O_NDELAY
  640. P_FLAG(NDELAY);
  641. #endif
  642. #ifdef O_PATH
  643. P_FLAG(PATH);
  644. #endif
  645. P_FLAG(RDWR);
  646. #ifdef O_DSYNC
  647. if ((flags & O_SYNC) == O_SYNC)
  648. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
  649. else {
  650. P_FLAG(DSYNC);
  651. }
  652. #else
  653. P_FLAG(SYNC);
  654. #endif
  655. P_FLAG(TRUNC);
  656. P_FLAG(WRONLY);
  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_OPEN_FLAGS syscall_arg__scnprintf_open_flags
  663. static size_t syscall_arg__scnprintf_perf_flags(char *bf, size_t size,
  664. struct syscall_arg *arg)
  665. {
  666. int printed = 0, flags = arg->val;
  667. if (flags == 0)
  668. return 0;
  669. #define P_FLAG(n) \
  670. if (flags & PERF_FLAG_##n) { \
  671. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  672. flags &= ~PERF_FLAG_##n; \
  673. }
  674. P_FLAG(FD_NO_GROUP);
  675. P_FLAG(FD_OUTPUT);
  676. P_FLAG(PID_CGROUP);
  677. P_FLAG(FD_CLOEXEC);
  678. #undef P_FLAG
  679. if (flags)
  680. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  681. return printed;
  682. }
  683. #define SCA_PERF_FLAGS syscall_arg__scnprintf_perf_flags
  684. static size_t syscall_arg__scnprintf_eventfd_flags(char *bf, size_t size,
  685. struct syscall_arg *arg)
  686. {
  687. int printed = 0, flags = arg->val;
  688. if (flags == 0)
  689. return scnprintf(bf, size, "NONE");
  690. #define P_FLAG(n) \
  691. if (flags & EFD_##n) { \
  692. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  693. flags &= ~EFD_##n; \
  694. }
  695. P_FLAG(SEMAPHORE);
  696. P_FLAG(CLOEXEC);
  697. P_FLAG(NONBLOCK);
  698. #undef P_FLAG
  699. if (flags)
  700. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  701. return printed;
  702. }
  703. #define SCA_EFD_FLAGS syscall_arg__scnprintf_eventfd_flags
  704. static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
  705. struct syscall_arg *arg)
  706. {
  707. int printed = 0, flags = arg->val;
  708. #define P_FLAG(n) \
  709. if (flags & O_##n) { \
  710. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  711. flags &= ~O_##n; \
  712. }
  713. P_FLAG(CLOEXEC);
  714. P_FLAG(NONBLOCK);
  715. #undef P_FLAG
  716. if (flags)
  717. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  718. return printed;
  719. }
  720. #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
  721. static size_t syscall_arg__scnprintf_signum(char *bf, size_t size, struct syscall_arg *arg)
  722. {
  723. int sig = arg->val;
  724. switch (sig) {
  725. #define P_SIGNUM(n) case SIG##n: return scnprintf(bf, size, #n)
  726. P_SIGNUM(HUP);
  727. P_SIGNUM(INT);
  728. P_SIGNUM(QUIT);
  729. P_SIGNUM(ILL);
  730. P_SIGNUM(TRAP);
  731. P_SIGNUM(ABRT);
  732. P_SIGNUM(BUS);
  733. P_SIGNUM(FPE);
  734. P_SIGNUM(KILL);
  735. P_SIGNUM(USR1);
  736. P_SIGNUM(SEGV);
  737. P_SIGNUM(USR2);
  738. P_SIGNUM(PIPE);
  739. P_SIGNUM(ALRM);
  740. P_SIGNUM(TERM);
  741. P_SIGNUM(CHLD);
  742. P_SIGNUM(CONT);
  743. P_SIGNUM(STOP);
  744. P_SIGNUM(TSTP);
  745. P_SIGNUM(TTIN);
  746. P_SIGNUM(TTOU);
  747. P_SIGNUM(URG);
  748. P_SIGNUM(XCPU);
  749. P_SIGNUM(XFSZ);
  750. P_SIGNUM(VTALRM);
  751. P_SIGNUM(PROF);
  752. P_SIGNUM(WINCH);
  753. P_SIGNUM(IO);
  754. P_SIGNUM(PWR);
  755. P_SIGNUM(SYS);
  756. #ifdef SIGEMT
  757. P_SIGNUM(EMT);
  758. #endif
  759. #ifdef SIGSTKFLT
  760. P_SIGNUM(STKFLT);
  761. #endif
  762. #ifdef SIGSWI
  763. P_SIGNUM(SWI);
  764. #endif
  765. default: break;
  766. }
  767. return scnprintf(bf, size, "%#x", sig);
  768. }
  769. #define SCA_SIGNUM syscall_arg__scnprintf_signum
  770. #if defined(__i386__) || defined(__x86_64__)
  771. /*
  772. * FIXME: Make this available to all arches.
  773. */
  774. #define TCGETS 0x5401
  775. static const char *tioctls[] = {
  776. "TCGETS", "TCSETS", "TCSETSW", "TCSETSF", "TCGETA", "TCSETA", "TCSETAW",
  777. "TCSETAF", "TCSBRK", "TCXONC", "TCFLSH", "TIOCEXCL", "TIOCNXCL",
  778. "TIOCSCTTY", "TIOCGPGRP", "TIOCSPGRP", "TIOCOUTQ", "TIOCSTI",
  779. "TIOCGWINSZ", "TIOCSWINSZ", "TIOCMGET", "TIOCMBIS", "TIOCMBIC",
  780. "TIOCMSET", "TIOCGSOFTCAR", "TIOCSSOFTCAR", "FIONREAD", "TIOCLINUX",
  781. "TIOCCONS", "TIOCGSERIAL", "TIOCSSERIAL", "TIOCPKT", "FIONBIO",
  782. "TIOCNOTTY", "TIOCSETD", "TIOCGETD", "TCSBRKP", [0x27] = "TIOCSBRK",
  783. "TIOCCBRK", "TIOCGSID", "TCGETS2", "TCSETS2", "TCSETSW2", "TCSETSF2",
  784. "TIOCGRS485", "TIOCSRS485", "TIOCGPTN", "TIOCSPTLCK",
  785. "TIOCGDEV||TCGETX", "TCSETX", "TCSETXF", "TCSETXW", "TIOCSIG",
  786. "TIOCVHANGUP", "TIOCGPKT", "TIOCGPTLCK", "TIOCGEXCL",
  787. [0x50] = "FIONCLEX", "FIOCLEX", "FIOASYNC", "TIOCSERCONFIG",
  788. "TIOCSERGWILD", "TIOCSERSWILD", "TIOCGLCKTRMIOS", "TIOCSLCKTRMIOS",
  789. "TIOCSERGSTRUCT", "TIOCSERGETLSR", "TIOCSERGETMULTI", "TIOCSERSETMULTI",
  790. "TIOCMIWAIT", "TIOCGICOUNT", [0x60] = "FIOQSIZE",
  791. };
  792. static DEFINE_STRARRAY_OFFSET(tioctls, 0x5401);
  793. #endif /* defined(__i386__) || defined(__x86_64__) */
  794. #define STRARRAY(arg, name, array) \
  795. .arg_scnprintf = { [arg] = SCA_STRARRAY, }, \
  796. .arg_parm = { [arg] = &strarray__##array, }
  797. static struct syscall_fmt {
  798. const char *name;
  799. const char *alias;
  800. size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg);
  801. void *arg_parm[6];
  802. bool errmsg;
  803. bool timeout;
  804. bool hexret;
  805. } syscall_fmts[] = {
  806. { .name = "access", .errmsg = true,
  807. .arg_scnprintf = { [1] = SCA_ACCMODE, /* mode */ }, },
  808. { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
  809. { .name = "brk", .hexret = true,
  810. .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
  811. { .name = "clock_gettime", .errmsg = true, STRARRAY(0, clk_id, clockid), },
  812. { .name = "close", .errmsg = true,
  813. .arg_scnprintf = { [0] = SCA_CLOSE_FD, /* fd */ }, },
  814. { .name = "connect", .errmsg = true, },
  815. { .name = "dup", .errmsg = true,
  816. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  817. { .name = "dup2", .errmsg = true,
  818. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  819. { .name = "dup3", .errmsg = true,
  820. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  821. { .name = "epoll_ctl", .errmsg = true, STRARRAY(1, op, epoll_ctl_ops), },
  822. { .name = "eventfd2", .errmsg = true,
  823. .arg_scnprintf = { [1] = SCA_EFD_FLAGS, /* flags */ }, },
  824. { .name = "faccessat", .errmsg = true,
  825. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  826. { .name = "fadvise64", .errmsg = true,
  827. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  828. { .name = "fallocate", .errmsg = true,
  829. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  830. { .name = "fchdir", .errmsg = true,
  831. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  832. { .name = "fchmod", .errmsg = true,
  833. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  834. { .name = "fchmodat", .errmsg = true,
  835. .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
  836. { .name = "fchown", .errmsg = true,
  837. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  838. { .name = "fchownat", .errmsg = true,
  839. .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
  840. { .name = "fcntl", .errmsg = true,
  841. .arg_scnprintf = { [0] = SCA_FD, /* fd */
  842. [1] = SCA_STRARRAY, /* cmd */ },
  843. .arg_parm = { [1] = &strarray__fcntl_cmds, /* cmd */ }, },
  844. { .name = "fdatasync", .errmsg = true,
  845. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  846. { .name = "flock", .errmsg = true,
  847. .arg_scnprintf = { [0] = SCA_FD, /* fd */
  848. [1] = SCA_FLOCK, /* cmd */ }, },
  849. { .name = "fsetxattr", .errmsg = true,
  850. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  851. { .name = "fstat", .errmsg = true, .alias = "newfstat",
  852. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  853. { .name = "fstatat", .errmsg = true, .alias = "newfstatat",
  854. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  855. { .name = "fstatfs", .errmsg = true,
  856. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  857. { .name = "fsync", .errmsg = true,
  858. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  859. { .name = "ftruncate", .errmsg = true,
  860. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  861. { .name = "futex", .errmsg = true,
  862. .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
  863. { .name = "futimesat", .errmsg = true,
  864. .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
  865. { .name = "getdents", .errmsg = true,
  866. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  867. { .name = "getdents64", .errmsg = true,
  868. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  869. { .name = "getitimer", .errmsg = true, STRARRAY(0, which, itimers), },
  870. { .name = "getrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
  871. { .name = "ioctl", .errmsg = true,
  872. .arg_scnprintf = { [0] = SCA_FD, /* fd */
  873. #if defined(__i386__) || defined(__x86_64__)
  874. /*
  875. * FIXME: Make this available to all arches.
  876. */
  877. [1] = SCA_STRHEXARRAY, /* cmd */
  878. [2] = SCA_HEX, /* arg */ },
  879. .arg_parm = { [1] = &strarray__tioctls, /* cmd */ }, },
  880. #else
  881. [2] = SCA_HEX, /* arg */ }, },
  882. #endif
  883. { .name = "kill", .errmsg = true,
  884. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  885. { .name = "linkat", .errmsg = true,
  886. .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
  887. { .name = "lseek", .errmsg = true,
  888. .arg_scnprintf = { [0] = SCA_FD, /* fd */
  889. [2] = SCA_STRARRAY, /* whence */ },
  890. .arg_parm = { [2] = &strarray__whences, /* whence */ }, },
  891. { .name = "lstat", .errmsg = true, .alias = "newlstat", },
  892. { .name = "madvise", .errmsg = true,
  893. .arg_scnprintf = { [0] = SCA_HEX, /* start */
  894. [2] = SCA_MADV_BHV, /* behavior */ }, },
  895. { .name = "mkdirat", .errmsg = true,
  896. .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
  897. { .name = "mknodat", .errmsg = true,
  898. .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
  899. { .name = "mlock", .errmsg = true,
  900. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  901. { .name = "mlockall", .errmsg = true,
  902. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  903. { .name = "mmap", .hexret = true,
  904. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  905. [2] = SCA_MMAP_PROT, /* prot */
  906. [3] = SCA_MMAP_FLAGS, /* flags */
  907. [4] = SCA_FD, /* fd */ }, },
  908. { .name = "mprotect", .errmsg = true,
  909. .arg_scnprintf = { [0] = SCA_HEX, /* start */
  910. [2] = SCA_MMAP_PROT, /* prot */ }, },
  911. { .name = "mremap", .hexret = true,
  912. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  913. [3] = SCA_MREMAP_FLAGS, /* flags */
  914. [4] = SCA_HEX, /* new_addr */ }, },
  915. { .name = "munlock", .errmsg = true,
  916. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  917. { .name = "munmap", .errmsg = true,
  918. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  919. { .name = "name_to_handle_at", .errmsg = true,
  920. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  921. { .name = "newfstatat", .errmsg = true,
  922. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  923. { .name = "open", .errmsg = true,
  924. .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */
  925. [1] = SCA_OPEN_FLAGS, /* flags */ }, },
  926. { .name = "open_by_handle_at", .errmsg = true,
  927. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
  928. [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  929. { .name = "openat", .errmsg = true,
  930. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
  931. [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  932. { .name = "perf_event_open", .errmsg = true,
  933. .arg_scnprintf = { [1] = SCA_INT, /* pid */
  934. [2] = SCA_INT, /* cpu */
  935. [3] = SCA_FD, /* group_fd */
  936. [4] = SCA_PERF_FLAGS, /* flags */ }, },
  937. { .name = "pipe2", .errmsg = true,
  938. .arg_scnprintf = { [1] = SCA_PIPE_FLAGS, /* flags */ }, },
  939. { .name = "poll", .errmsg = true, .timeout = true, },
  940. { .name = "ppoll", .errmsg = true, .timeout = true, },
  941. { .name = "pread", .errmsg = true, .alias = "pread64",
  942. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  943. { .name = "preadv", .errmsg = true, .alias = "pread",
  944. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  945. { .name = "prlimit64", .errmsg = true, STRARRAY(1, resource, rlimit_resources), },
  946. { .name = "pwrite", .errmsg = true, .alias = "pwrite64",
  947. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  948. { .name = "pwritev", .errmsg = true,
  949. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  950. { .name = "read", .errmsg = true,
  951. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  952. { .name = "readlinkat", .errmsg = true,
  953. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  954. { .name = "readv", .errmsg = true,
  955. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  956. { .name = "recvfrom", .errmsg = true,
  957. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  958. { .name = "recvmmsg", .errmsg = true,
  959. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  960. { .name = "recvmsg", .errmsg = true,
  961. .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
  962. { .name = "renameat", .errmsg = true,
  963. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  964. { .name = "rt_sigaction", .errmsg = true,
  965. .arg_scnprintf = { [0] = SCA_SIGNUM, /* sig */ }, },
  966. { .name = "rt_sigprocmask", .errmsg = true, STRARRAY(0, how, sighow), },
  967. { .name = "rt_sigqueueinfo", .errmsg = true,
  968. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  969. { .name = "rt_tgsigqueueinfo", .errmsg = true,
  970. .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
  971. { .name = "select", .errmsg = true, .timeout = true, },
  972. { .name = "sendmmsg", .errmsg = true,
  973. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  974. { .name = "sendmsg", .errmsg = true,
  975. .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
  976. { .name = "sendto", .errmsg = true,
  977. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  978. { .name = "setitimer", .errmsg = true, STRARRAY(0, which, itimers), },
  979. { .name = "setrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
  980. { .name = "shutdown", .errmsg = true,
  981. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  982. { .name = "socket", .errmsg = true,
  983. .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
  984. [1] = SCA_SK_TYPE, /* type */ },
  985. .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
  986. { .name = "socketpair", .errmsg = true,
  987. .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
  988. [1] = SCA_SK_TYPE, /* type */ },
  989. .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
  990. { .name = "stat", .errmsg = true, .alias = "newstat", },
  991. { .name = "symlinkat", .errmsg = true,
  992. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  993. { .name = "tgkill", .errmsg = true,
  994. .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
  995. { .name = "tkill", .errmsg = true,
  996. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  997. { .name = "uname", .errmsg = true, .alias = "newuname", },
  998. { .name = "unlinkat", .errmsg = true,
  999. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  1000. { .name = "utimensat", .errmsg = true,
  1001. .arg_scnprintf = { [0] = SCA_FDAT, /* dirfd */ }, },
  1002. { .name = "write", .errmsg = true,
  1003. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  1004. { .name = "writev", .errmsg = true,
  1005. .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
  1006. };
  1007. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  1008. {
  1009. const struct syscall_fmt *fmt = fmtp;
  1010. return strcmp(name, fmt->name);
  1011. }
  1012. static struct syscall_fmt *syscall_fmt__find(const char *name)
  1013. {
  1014. const int nmemb = ARRAY_SIZE(syscall_fmts);
  1015. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  1016. }
  1017. struct syscall {
  1018. struct event_format *tp_format;
  1019. int nr_args;
  1020. struct format_field *args;
  1021. const char *name;
  1022. bool is_exit;
  1023. struct syscall_fmt *fmt;
  1024. size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  1025. void **arg_parm;
  1026. };
  1027. static size_t fprintf_duration(unsigned long t, FILE *fp)
  1028. {
  1029. double duration = (double)t / NSEC_PER_MSEC;
  1030. size_t printed = fprintf(fp, "(");
  1031. if (duration >= 1.0)
  1032. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  1033. else if (duration >= 0.01)
  1034. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  1035. else
  1036. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  1037. return printed + fprintf(fp, "): ");
  1038. }
  1039. /**
  1040. * filename.ptr: The filename char pointer that will be vfs_getname'd
  1041. * filename.entry_str_pos: Where to insert the string translated from
  1042. * filename.ptr by the vfs_getname tracepoint/kprobe.
  1043. */
  1044. struct thread_trace {
  1045. u64 entry_time;
  1046. u64 exit_time;
  1047. bool entry_pending;
  1048. unsigned long nr_events;
  1049. unsigned long pfmaj, pfmin;
  1050. char *entry_str;
  1051. double runtime_ms;
  1052. struct {
  1053. unsigned long ptr;
  1054. int entry_str_pos;
  1055. } filename;
  1056. struct {
  1057. int max;
  1058. char **table;
  1059. } paths;
  1060. struct intlist *syscall_stats;
  1061. };
  1062. static struct thread_trace *thread_trace__new(void)
  1063. {
  1064. struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
  1065. if (ttrace)
  1066. ttrace->paths.max = -1;
  1067. ttrace->syscall_stats = intlist__new(NULL);
  1068. return ttrace;
  1069. }
  1070. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  1071. {
  1072. struct thread_trace *ttrace;
  1073. if (thread == NULL)
  1074. goto fail;
  1075. if (thread__priv(thread) == NULL)
  1076. thread__set_priv(thread, thread_trace__new());
  1077. if (thread__priv(thread) == NULL)
  1078. goto fail;
  1079. ttrace = thread__priv(thread);
  1080. ++ttrace->nr_events;
  1081. return ttrace;
  1082. fail:
  1083. color_fprintf(fp, PERF_COLOR_RED,
  1084. "WARNING: not enough memory, dropping samples!\n");
  1085. return NULL;
  1086. }
  1087. #define TRACE_PFMAJ (1 << 0)
  1088. #define TRACE_PFMIN (1 << 1)
  1089. static const size_t trace__entry_str_size = 2048;
  1090. struct trace {
  1091. struct perf_tool tool;
  1092. struct {
  1093. int machine;
  1094. int open_id;
  1095. } audit;
  1096. struct {
  1097. int max;
  1098. struct syscall *table;
  1099. struct {
  1100. struct perf_evsel *sys_enter,
  1101. *sys_exit;
  1102. } events;
  1103. } syscalls;
  1104. struct record_opts opts;
  1105. struct perf_evlist *evlist;
  1106. struct machine *host;
  1107. struct thread *current;
  1108. u64 base_time;
  1109. FILE *output;
  1110. unsigned long nr_events;
  1111. struct strlist *ev_qualifier;
  1112. struct {
  1113. size_t nr;
  1114. int *entries;
  1115. } ev_qualifier_ids;
  1116. const char *last_vfs_getname;
  1117. struct intlist *tid_list;
  1118. struct intlist *pid_list;
  1119. struct {
  1120. size_t nr;
  1121. pid_t *entries;
  1122. } filter_pids;
  1123. double duration_filter;
  1124. double runtime_ms;
  1125. struct {
  1126. u64 vfs_getname,
  1127. proc_getname;
  1128. } stats;
  1129. bool not_ev_qualifier;
  1130. bool live;
  1131. bool full_time;
  1132. bool sched;
  1133. bool multiple_threads;
  1134. bool summary;
  1135. bool summary_only;
  1136. bool show_comm;
  1137. bool show_tool_stats;
  1138. bool trace_syscalls;
  1139. bool force;
  1140. bool vfs_getname;
  1141. int trace_pgfaults;
  1142. };
  1143. static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
  1144. {
  1145. struct thread_trace *ttrace = thread__priv(thread);
  1146. if (fd > ttrace->paths.max) {
  1147. char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
  1148. if (npath == NULL)
  1149. return -1;
  1150. if (ttrace->paths.max != -1) {
  1151. memset(npath + ttrace->paths.max + 1, 0,
  1152. (fd - ttrace->paths.max) * sizeof(char *));
  1153. } else {
  1154. memset(npath, 0, (fd + 1) * sizeof(char *));
  1155. }
  1156. ttrace->paths.table = npath;
  1157. ttrace->paths.max = fd;
  1158. }
  1159. ttrace->paths.table[fd] = strdup(pathname);
  1160. return ttrace->paths.table[fd] != NULL ? 0 : -1;
  1161. }
  1162. static int thread__read_fd_path(struct thread *thread, int fd)
  1163. {
  1164. char linkname[PATH_MAX], pathname[PATH_MAX];
  1165. struct stat st;
  1166. int ret;
  1167. if (thread->pid_ == thread->tid) {
  1168. scnprintf(linkname, sizeof(linkname),
  1169. "/proc/%d/fd/%d", thread->pid_, fd);
  1170. } else {
  1171. scnprintf(linkname, sizeof(linkname),
  1172. "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
  1173. }
  1174. if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
  1175. return -1;
  1176. ret = readlink(linkname, pathname, sizeof(pathname));
  1177. if (ret < 0 || ret > st.st_size)
  1178. return -1;
  1179. pathname[ret] = '\0';
  1180. return trace__set_fd_pathname(thread, fd, pathname);
  1181. }
  1182. static const char *thread__fd_path(struct thread *thread, int fd,
  1183. struct trace *trace)
  1184. {
  1185. struct thread_trace *ttrace = thread__priv(thread);
  1186. if (ttrace == NULL)
  1187. return NULL;
  1188. if (fd < 0)
  1189. return NULL;
  1190. if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
  1191. if (!trace->live)
  1192. return NULL;
  1193. ++trace->stats.proc_getname;
  1194. if (thread__read_fd_path(thread, fd))
  1195. return NULL;
  1196. }
  1197. return ttrace->paths.table[fd];
  1198. }
  1199. static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
  1200. struct syscall_arg *arg)
  1201. {
  1202. int fd = arg->val;
  1203. size_t printed = scnprintf(bf, size, "%d", fd);
  1204. const char *path = thread__fd_path(arg->thread, fd, arg->trace);
  1205. if (path)
  1206. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  1207. return printed;
  1208. }
  1209. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  1210. struct syscall_arg *arg)
  1211. {
  1212. int fd = arg->val;
  1213. size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
  1214. struct thread_trace *ttrace = thread__priv(arg->thread);
  1215. if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
  1216. zfree(&ttrace->paths.table[fd]);
  1217. return printed;
  1218. }
  1219. static void thread__set_filename_pos(struct thread *thread, const char *bf,
  1220. unsigned long ptr)
  1221. {
  1222. struct thread_trace *ttrace = thread__priv(thread);
  1223. ttrace->filename.ptr = ptr;
  1224. ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
  1225. }
  1226. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  1227. struct syscall_arg *arg)
  1228. {
  1229. unsigned long ptr = arg->val;
  1230. if (!arg->trace->vfs_getname)
  1231. return scnprintf(bf, size, "%#x", ptr);
  1232. thread__set_filename_pos(arg->thread, bf, ptr);
  1233. return 0;
  1234. }
  1235. static bool trace__filter_duration(struct trace *trace, double t)
  1236. {
  1237. return t < (trace->duration_filter * NSEC_PER_MSEC);
  1238. }
  1239. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  1240. {
  1241. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  1242. return fprintf(fp, "%10.3f ", ts);
  1243. }
  1244. static bool done = false;
  1245. static bool interrupted = false;
  1246. static void sig_handler(int sig)
  1247. {
  1248. done = true;
  1249. interrupted = sig == SIGINT;
  1250. }
  1251. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  1252. u64 duration, u64 tstamp, FILE *fp)
  1253. {
  1254. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  1255. printed += fprintf_duration(duration, fp);
  1256. if (trace->multiple_threads) {
  1257. if (trace->show_comm)
  1258. printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
  1259. printed += fprintf(fp, "%d ", thread->tid);
  1260. }
  1261. return printed;
  1262. }
  1263. static int trace__process_event(struct trace *trace, struct machine *machine,
  1264. union perf_event *event, struct perf_sample *sample)
  1265. {
  1266. int ret = 0;
  1267. switch (event->header.type) {
  1268. case PERF_RECORD_LOST:
  1269. color_fprintf(trace->output, PERF_COLOR_RED,
  1270. "LOST %" PRIu64 " events!\n", event->lost.lost);
  1271. ret = machine__process_lost_event(machine, event, sample);
  1272. default:
  1273. ret = machine__process_event(machine, event, sample);
  1274. break;
  1275. }
  1276. return ret;
  1277. }
  1278. static int trace__tool_process(struct perf_tool *tool,
  1279. union perf_event *event,
  1280. struct perf_sample *sample,
  1281. struct machine *machine)
  1282. {
  1283. struct trace *trace = container_of(tool, struct trace, tool);
  1284. return trace__process_event(trace, machine, event, sample);
  1285. }
  1286. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  1287. {
  1288. int err = symbol__init(NULL);
  1289. if (err)
  1290. return err;
  1291. trace->host = machine__new_host();
  1292. if (trace->host == NULL)
  1293. return -ENOMEM;
  1294. if (trace_event__register_resolver(trace->host, machine__resolve_kernel_addr) < 0)
  1295. return -errno;
  1296. err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  1297. evlist->threads, trace__tool_process, false,
  1298. trace->opts.proc_map_timeout);
  1299. if (err)
  1300. symbol__exit();
  1301. return err;
  1302. }
  1303. static int syscall__set_arg_fmts(struct syscall *sc)
  1304. {
  1305. struct format_field *field;
  1306. int idx = 0;
  1307. sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *));
  1308. if (sc->arg_scnprintf == NULL)
  1309. return -1;
  1310. if (sc->fmt)
  1311. sc->arg_parm = sc->fmt->arg_parm;
  1312. for (field = sc->args; field; field = field->next) {
  1313. if (sc->fmt && sc->fmt->arg_scnprintf[idx])
  1314. sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
  1315. else if (field->flags & FIELD_IS_POINTER)
  1316. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  1317. ++idx;
  1318. }
  1319. return 0;
  1320. }
  1321. static int trace__read_syscall_info(struct trace *trace, int id)
  1322. {
  1323. char tp_name[128];
  1324. struct syscall *sc;
  1325. const char *name = audit_syscall_to_name(id, trace->audit.machine);
  1326. if (name == NULL)
  1327. return -1;
  1328. if (id > trace->syscalls.max) {
  1329. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  1330. if (nsyscalls == NULL)
  1331. return -1;
  1332. if (trace->syscalls.max != -1) {
  1333. memset(nsyscalls + trace->syscalls.max + 1, 0,
  1334. (id - trace->syscalls.max) * sizeof(*sc));
  1335. } else {
  1336. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  1337. }
  1338. trace->syscalls.table = nsyscalls;
  1339. trace->syscalls.max = id;
  1340. }
  1341. sc = trace->syscalls.table + id;
  1342. sc->name = name;
  1343. sc->fmt = syscall_fmt__find(sc->name);
  1344. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1345. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1346. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  1347. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1348. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1349. }
  1350. if (sc->tp_format == NULL)
  1351. return -1;
  1352. sc->args = sc->tp_format->format.fields;
  1353. sc->nr_args = sc->tp_format->format.nr_fields;
  1354. /* drop nr field - not relevant here; does not exist on older kernels */
  1355. if (sc->args && strcmp(sc->args->name, "nr") == 0) {
  1356. sc->args = sc->args->next;
  1357. --sc->nr_args;
  1358. }
  1359. sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
  1360. return syscall__set_arg_fmts(sc);
  1361. }
  1362. static int trace__validate_ev_qualifier(struct trace *trace)
  1363. {
  1364. int err = 0, i;
  1365. struct str_node *pos;
  1366. trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
  1367. trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
  1368. sizeof(trace->ev_qualifier_ids.entries[0]));
  1369. if (trace->ev_qualifier_ids.entries == NULL) {
  1370. fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
  1371. trace->output);
  1372. err = -EINVAL;
  1373. goto out;
  1374. }
  1375. i = 0;
  1376. strlist__for_each(pos, trace->ev_qualifier) {
  1377. const char *sc = pos->s;
  1378. int id = audit_name_to_syscall(sc, trace->audit.machine);
  1379. if (id < 0) {
  1380. if (err == 0) {
  1381. fputs("Error:\tInvalid syscall ", trace->output);
  1382. err = -EINVAL;
  1383. } else {
  1384. fputs(", ", trace->output);
  1385. }
  1386. fputs(sc, trace->output);
  1387. }
  1388. trace->ev_qualifier_ids.entries[i++] = id;
  1389. }
  1390. if (err < 0) {
  1391. fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
  1392. "\nHint:\tand: 'man syscalls'\n", trace->output);
  1393. zfree(&trace->ev_qualifier_ids.entries);
  1394. trace->ev_qualifier_ids.nr = 0;
  1395. }
  1396. out:
  1397. return err;
  1398. }
  1399. /*
  1400. * args is to be interpreted as a series of longs but we need to handle
  1401. * 8-byte unaligned accesses. args points to raw_data within the event
  1402. * and raw_data is guaranteed to be 8-byte unaligned because it is
  1403. * preceded by raw_size which is a u32. So we need to copy args to a temp
  1404. * variable to read it. Most notably this avoids extended load instructions
  1405. * on unaligned addresses
  1406. */
  1407. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  1408. unsigned char *args, struct trace *trace,
  1409. struct thread *thread)
  1410. {
  1411. size_t printed = 0;
  1412. unsigned char *p;
  1413. unsigned long val;
  1414. if (sc->args != NULL) {
  1415. struct format_field *field;
  1416. u8 bit = 1;
  1417. struct syscall_arg arg = {
  1418. .idx = 0,
  1419. .mask = 0,
  1420. .trace = trace,
  1421. .thread = thread,
  1422. };
  1423. for (field = sc->args; field;
  1424. field = field->next, ++arg.idx, bit <<= 1) {
  1425. if (arg.mask & bit)
  1426. continue;
  1427. /* special care for unaligned accesses */
  1428. p = args + sizeof(unsigned long) * arg.idx;
  1429. memcpy(&val, p, sizeof(val));
  1430. /*
  1431. * Suppress this argument if its value is zero and
  1432. * and we don't have a string associated in an
  1433. * strarray for it.
  1434. */
  1435. if (val == 0 &&
  1436. !(sc->arg_scnprintf &&
  1437. sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
  1438. sc->arg_parm[arg.idx]))
  1439. continue;
  1440. printed += scnprintf(bf + printed, size - printed,
  1441. "%s%s: ", printed ? ", " : "", field->name);
  1442. if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
  1443. arg.val = val;
  1444. if (sc->arg_parm)
  1445. arg.parm = sc->arg_parm[arg.idx];
  1446. printed += sc->arg_scnprintf[arg.idx](bf + printed,
  1447. size - printed, &arg);
  1448. } else {
  1449. printed += scnprintf(bf + printed, size - printed,
  1450. "%ld", val);
  1451. }
  1452. }
  1453. } else {
  1454. int i = 0;
  1455. while (i < 6) {
  1456. /* special care for unaligned accesses */
  1457. p = args + sizeof(unsigned long) * i;
  1458. memcpy(&val, p, sizeof(val));
  1459. printed += scnprintf(bf + printed, size - printed,
  1460. "%sarg%d: %ld",
  1461. printed ? ", " : "", i, val);
  1462. ++i;
  1463. }
  1464. }
  1465. return printed;
  1466. }
  1467. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  1468. union perf_event *event,
  1469. struct perf_sample *sample);
  1470. static struct syscall *trace__syscall_info(struct trace *trace,
  1471. struct perf_evsel *evsel, int id)
  1472. {
  1473. if (id < 0) {
  1474. /*
  1475. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  1476. * before that, leaving at a higher verbosity level till that is
  1477. * explained. Reproduced with plain ftrace with:
  1478. *
  1479. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  1480. * grep "NR -1 " /t/trace_pipe
  1481. *
  1482. * After generating some load on the machine.
  1483. */
  1484. if (verbose > 1) {
  1485. static u64 n;
  1486. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  1487. id, perf_evsel__name(evsel), ++n);
  1488. }
  1489. return NULL;
  1490. }
  1491. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  1492. trace__read_syscall_info(trace, id))
  1493. goto out_cant_read;
  1494. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  1495. goto out_cant_read;
  1496. return &trace->syscalls.table[id];
  1497. out_cant_read:
  1498. if (verbose) {
  1499. fprintf(trace->output, "Problems reading syscall %d", id);
  1500. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  1501. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  1502. fputs(" information\n", trace->output);
  1503. }
  1504. return NULL;
  1505. }
  1506. static void thread__update_stats(struct thread_trace *ttrace,
  1507. int id, struct perf_sample *sample)
  1508. {
  1509. struct int_node *inode;
  1510. struct stats *stats;
  1511. u64 duration = 0;
  1512. inode = intlist__findnew(ttrace->syscall_stats, id);
  1513. if (inode == NULL)
  1514. return;
  1515. stats = inode->priv;
  1516. if (stats == NULL) {
  1517. stats = malloc(sizeof(struct stats));
  1518. if (stats == NULL)
  1519. return;
  1520. init_stats(stats);
  1521. inode->priv = stats;
  1522. }
  1523. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  1524. duration = sample->time - ttrace->entry_time;
  1525. update_stats(stats, duration);
  1526. }
  1527. static int trace__printf_interrupted_entry(struct trace *trace, struct perf_sample *sample)
  1528. {
  1529. struct thread_trace *ttrace;
  1530. u64 duration;
  1531. size_t printed;
  1532. if (trace->current == NULL)
  1533. return 0;
  1534. ttrace = thread__priv(trace->current);
  1535. if (!ttrace->entry_pending)
  1536. return 0;
  1537. duration = sample->time - ttrace->entry_time;
  1538. printed = trace__fprintf_entry_head(trace, trace->current, duration, sample->time, trace->output);
  1539. printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
  1540. ttrace->entry_pending = false;
  1541. return printed;
  1542. }
  1543. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  1544. union perf_event *event __maybe_unused,
  1545. struct perf_sample *sample)
  1546. {
  1547. char *msg;
  1548. void *args;
  1549. size_t printed = 0;
  1550. struct thread *thread;
  1551. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1552. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1553. struct thread_trace *ttrace;
  1554. if (sc == NULL)
  1555. return -1;
  1556. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1557. ttrace = thread__trace(thread, trace->output);
  1558. if (ttrace == NULL)
  1559. goto out_put;
  1560. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1561. if (ttrace->entry_str == NULL) {
  1562. ttrace->entry_str = malloc(trace__entry_str_size);
  1563. if (!ttrace->entry_str)
  1564. goto out_put;
  1565. }
  1566. if (!trace->summary_only)
  1567. trace__printf_interrupted_entry(trace, sample);
  1568. ttrace->entry_time = sample->time;
  1569. msg = ttrace->entry_str;
  1570. printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
  1571. printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
  1572. args, trace, thread);
  1573. if (sc->is_exit) {
  1574. if (!trace->duration_filter && !trace->summary_only) {
  1575. trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
  1576. fprintf(trace->output, "%-70s\n", ttrace->entry_str);
  1577. }
  1578. } else
  1579. ttrace->entry_pending = true;
  1580. if (trace->current != thread) {
  1581. thread__put(trace->current);
  1582. trace->current = thread__get(thread);
  1583. }
  1584. err = 0;
  1585. out_put:
  1586. thread__put(thread);
  1587. return err;
  1588. }
  1589. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  1590. union perf_event *event __maybe_unused,
  1591. struct perf_sample *sample)
  1592. {
  1593. long ret;
  1594. u64 duration = 0;
  1595. struct thread *thread;
  1596. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1597. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1598. struct thread_trace *ttrace;
  1599. if (sc == NULL)
  1600. return -1;
  1601. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1602. ttrace = thread__trace(thread, trace->output);
  1603. if (ttrace == NULL)
  1604. goto out_put;
  1605. if (trace->summary)
  1606. thread__update_stats(ttrace, id, sample);
  1607. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  1608. if (id == trace->audit.open_id && ret >= 0 && trace->last_vfs_getname) {
  1609. trace__set_fd_pathname(thread, ret, trace->last_vfs_getname);
  1610. trace->last_vfs_getname = NULL;
  1611. ++trace->stats.vfs_getname;
  1612. }
  1613. ttrace->exit_time = sample->time;
  1614. if (ttrace->entry_time) {
  1615. duration = sample->time - ttrace->entry_time;
  1616. if (trace__filter_duration(trace, duration))
  1617. goto out;
  1618. } else if (trace->duration_filter)
  1619. goto out;
  1620. if (trace->summary_only)
  1621. goto out;
  1622. trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
  1623. if (ttrace->entry_pending) {
  1624. fprintf(trace->output, "%-70s", ttrace->entry_str);
  1625. } else {
  1626. fprintf(trace->output, " ... [");
  1627. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  1628. fprintf(trace->output, "]: %s()", sc->name);
  1629. }
  1630. if (sc->fmt == NULL) {
  1631. signed_print:
  1632. fprintf(trace->output, ") = %ld", ret);
  1633. } else if (ret < 0 && sc->fmt->errmsg) {
  1634. char bf[STRERR_BUFSIZE];
  1635. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  1636. *e = audit_errno_to_name(-ret);
  1637. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  1638. } else if (ret == 0 && sc->fmt->timeout)
  1639. fprintf(trace->output, ") = 0 Timeout");
  1640. else if (sc->fmt->hexret)
  1641. fprintf(trace->output, ") = %#lx", ret);
  1642. else
  1643. goto signed_print;
  1644. fputc('\n', trace->output);
  1645. out:
  1646. ttrace->entry_pending = false;
  1647. err = 0;
  1648. out_put:
  1649. thread__put(thread);
  1650. return err;
  1651. }
  1652. static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
  1653. union perf_event *event __maybe_unused,
  1654. struct perf_sample *sample)
  1655. {
  1656. struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1657. struct thread_trace *ttrace;
  1658. size_t filename_len, entry_str_len, to_move;
  1659. ssize_t remaining_space;
  1660. char *pos;
  1661. const char *filename;
  1662. trace->last_vfs_getname = perf_evsel__rawptr(evsel, sample, "pathname");
  1663. if (!thread)
  1664. goto out;
  1665. ttrace = thread__priv(thread);
  1666. if (!ttrace)
  1667. goto out;
  1668. if (!ttrace->filename.ptr)
  1669. goto out;
  1670. entry_str_len = strlen(ttrace->entry_str);
  1671. remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
  1672. if (remaining_space <= 0)
  1673. goto out;
  1674. filename = trace->last_vfs_getname;
  1675. filename_len = strlen(filename);
  1676. if (filename_len > (size_t)remaining_space) {
  1677. filename += filename_len - remaining_space;
  1678. filename_len = remaining_space;
  1679. }
  1680. to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
  1681. pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
  1682. memmove(pos + filename_len, pos, to_move);
  1683. memcpy(pos, filename, filename_len);
  1684. ttrace->filename.ptr = 0;
  1685. ttrace->filename.entry_str_pos = 0;
  1686. out:
  1687. return 0;
  1688. }
  1689. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  1690. union perf_event *event __maybe_unused,
  1691. struct perf_sample *sample)
  1692. {
  1693. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  1694. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  1695. struct thread *thread = machine__findnew_thread(trace->host,
  1696. sample->pid,
  1697. sample->tid);
  1698. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  1699. if (ttrace == NULL)
  1700. goto out_dump;
  1701. ttrace->runtime_ms += runtime_ms;
  1702. trace->runtime_ms += runtime_ms;
  1703. thread__put(thread);
  1704. return 0;
  1705. out_dump:
  1706. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  1707. evsel->name,
  1708. perf_evsel__strval(evsel, sample, "comm"),
  1709. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  1710. runtime,
  1711. perf_evsel__intval(evsel, sample, "vruntime"));
  1712. thread__put(thread);
  1713. return 0;
  1714. }
  1715. static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
  1716. union perf_event *event __maybe_unused,
  1717. struct perf_sample *sample)
  1718. {
  1719. trace__printf_interrupted_entry(trace, sample);
  1720. trace__fprintf_tstamp(trace, sample->time, trace->output);
  1721. if (trace->trace_syscalls)
  1722. fprintf(trace->output, "( ): ");
  1723. fprintf(trace->output, "%s:", evsel->name);
  1724. if (evsel->tp_format) {
  1725. event_format__fprintf(evsel->tp_format, sample->cpu,
  1726. sample->raw_data, sample->raw_size,
  1727. trace->output);
  1728. }
  1729. fprintf(trace->output, ")\n");
  1730. return 0;
  1731. }
  1732. static void print_location(FILE *f, struct perf_sample *sample,
  1733. struct addr_location *al,
  1734. bool print_dso, bool print_sym)
  1735. {
  1736. if ((verbose || print_dso) && al->map)
  1737. fprintf(f, "%s@", al->map->dso->long_name);
  1738. if ((verbose || print_sym) && al->sym)
  1739. fprintf(f, "%s+0x%" PRIx64, al->sym->name,
  1740. al->addr - al->sym->start);
  1741. else if (al->map)
  1742. fprintf(f, "0x%" PRIx64, al->addr);
  1743. else
  1744. fprintf(f, "0x%" PRIx64, sample->addr);
  1745. }
  1746. static int trace__pgfault(struct trace *trace,
  1747. struct perf_evsel *evsel,
  1748. union perf_event *event,
  1749. struct perf_sample *sample)
  1750. {
  1751. struct thread *thread;
  1752. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  1753. struct addr_location al;
  1754. char map_type = 'd';
  1755. struct thread_trace *ttrace;
  1756. int err = -1;
  1757. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1758. ttrace = thread__trace(thread, trace->output);
  1759. if (ttrace == NULL)
  1760. goto out_put;
  1761. if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
  1762. ttrace->pfmaj++;
  1763. else
  1764. ttrace->pfmin++;
  1765. if (trace->summary_only)
  1766. goto out;
  1767. thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
  1768. sample->ip, &al);
  1769. trace__fprintf_entry_head(trace, thread, 0, sample->time, trace->output);
  1770. fprintf(trace->output, "%sfault [",
  1771. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
  1772. "maj" : "min");
  1773. print_location(trace->output, sample, &al, false, true);
  1774. fprintf(trace->output, "] => ");
  1775. thread__find_addr_location(thread, cpumode, MAP__VARIABLE,
  1776. sample->addr, &al);
  1777. if (!al.map) {
  1778. thread__find_addr_location(thread, cpumode,
  1779. MAP__FUNCTION, sample->addr, &al);
  1780. if (al.map)
  1781. map_type = 'x';
  1782. else
  1783. map_type = '?';
  1784. }
  1785. print_location(trace->output, sample, &al, true, false);
  1786. fprintf(trace->output, " (%c%c)\n", map_type, al.level);
  1787. out:
  1788. err = 0;
  1789. out_put:
  1790. thread__put(thread);
  1791. return err;
  1792. }
  1793. static bool skip_sample(struct trace *trace, struct perf_sample *sample)
  1794. {
  1795. if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
  1796. (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
  1797. return false;
  1798. if (trace->pid_list || trace->tid_list)
  1799. return true;
  1800. return false;
  1801. }
  1802. static int trace__process_sample(struct perf_tool *tool,
  1803. union perf_event *event,
  1804. struct perf_sample *sample,
  1805. struct perf_evsel *evsel,
  1806. struct machine *machine __maybe_unused)
  1807. {
  1808. struct trace *trace = container_of(tool, struct trace, tool);
  1809. int err = 0;
  1810. tracepoint_handler handler = evsel->handler;
  1811. if (skip_sample(trace, sample))
  1812. return 0;
  1813. if (!trace->full_time && trace->base_time == 0)
  1814. trace->base_time = sample->time;
  1815. if (handler) {
  1816. ++trace->nr_events;
  1817. handler(trace, evsel, event, sample);
  1818. }
  1819. return err;
  1820. }
  1821. static int parse_target_str(struct trace *trace)
  1822. {
  1823. if (trace->opts.target.pid) {
  1824. trace->pid_list = intlist__new(trace->opts.target.pid);
  1825. if (trace->pid_list == NULL) {
  1826. pr_err("Error parsing process id string\n");
  1827. return -EINVAL;
  1828. }
  1829. }
  1830. if (trace->opts.target.tid) {
  1831. trace->tid_list = intlist__new(trace->opts.target.tid);
  1832. if (trace->tid_list == NULL) {
  1833. pr_err("Error parsing thread id string\n");
  1834. return -EINVAL;
  1835. }
  1836. }
  1837. return 0;
  1838. }
  1839. static int trace__record(struct trace *trace, int argc, const char **argv)
  1840. {
  1841. unsigned int rec_argc, i, j;
  1842. const char **rec_argv;
  1843. const char * const record_args[] = {
  1844. "record",
  1845. "-R",
  1846. "-m", "1024",
  1847. "-c", "1",
  1848. };
  1849. const char * const sc_args[] = { "-e", };
  1850. unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
  1851. const char * const majpf_args[] = { "-e", "major-faults" };
  1852. unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
  1853. const char * const minpf_args[] = { "-e", "minor-faults" };
  1854. unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
  1855. /* +1 is for the event string below */
  1856. rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
  1857. majpf_args_nr + minpf_args_nr + argc;
  1858. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1859. if (rec_argv == NULL)
  1860. return -ENOMEM;
  1861. j = 0;
  1862. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1863. rec_argv[j++] = record_args[i];
  1864. if (trace->trace_syscalls) {
  1865. for (i = 0; i < sc_args_nr; i++)
  1866. rec_argv[j++] = sc_args[i];
  1867. /* event string may be different for older kernels - e.g., RHEL6 */
  1868. if (is_valid_tracepoint("raw_syscalls:sys_enter"))
  1869. rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
  1870. else if (is_valid_tracepoint("syscalls:sys_enter"))
  1871. rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
  1872. else {
  1873. pr_err("Neither raw_syscalls nor syscalls events exist.\n");
  1874. return -1;
  1875. }
  1876. }
  1877. if (trace->trace_pgfaults & TRACE_PFMAJ)
  1878. for (i = 0; i < majpf_args_nr; i++)
  1879. rec_argv[j++] = majpf_args[i];
  1880. if (trace->trace_pgfaults & TRACE_PFMIN)
  1881. for (i = 0; i < minpf_args_nr; i++)
  1882. rec_argv[j++] = minpf_args[i];
  1883. for (i = 0; i < (unsigned int)argc; i++)
  1884. rec_argv[j++] = argv[i];
  1885. return cmd_record(j, rec_argv, NULL);
  1886. }
  1887. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  1888. static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
  1889. {
  1890. struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
  1891. if (evsel == NULL)
  1892. return false;
  1893. if (perf_evsel__field(evsel, "pathname") == NULL) {
  1894. perf_evsel__delete(evsel);
  1895. return false;
  1896. }
  1897. evsel->handler = trace__vfs_getname;
  1898. perf_evlist__add(evlist, evsel);
  1899. return true;
  1900. }
  1901. static int perf_evlist__add_pgfault(struct perf_evlist *evlist,
  1902. u64 config)
  1903. {
  1904. struct perf_evsel *evsel;
  1905. struct perf_event_attr attr = {
  1906. .type = PERF_TYPE_SOFTWARE,
  1907. .mmap_data = 1,
  1908. };
  1909. attr.config = config;
  1910. attr.sample_period = 1;
  1911. event_attr_init(&attr);
  1912. evsel = perf_evsel__new(&attr);
  1913. if (!evsel)
  1914. return -ENOMEM;
  1915. evsel->handler = trace__pgfault;
  1916. perf_evlist__add(evlist, evsel);
  1917. return 0;
  1918. }
  1919. static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
  1920. {
  1921. const u32 type = event->header.type;
  1922. struct perf_evsel *evsel;
  1923. if (!trace->full_time && trace->base_time == 0)
  1924. trace->base_time = sample->time;
  1925. if (type != PERF_RECORD_SAMPLE) {
  1926. trace__process_event(trace, trace->host, event, sample);
  1927. return;
  1928. }
  1929. evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
  1930. if (evsel == NULL) {
  1931. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
  1932. return;
  1933. }
  1934. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  1935. sample->raw_data == NULL) {
  1936. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  1937. perf_evsel__name(evsel), sample->tid,
  1938. sample->cpu, sample->raw_size);
  1939. } else {
  1940. tracepoint_handler handler = evsel->handler;
  1941. handler(trace, evsel, event, sample);
  1942. }
  1943. }
  1944. static int trace__add_syscall_newtp(struct trace *trace)
  1945. {
  1946. int ret = -1;
  1947. struct perf_evlist *evlist = trace->evlist;
  1948. struct perf_evsel *sys_enter, *sys_exit;
  1949. sys_enter = perf_evsel__syscall_newtp("sys_enter", trace__sys_enter);
  1950. if (sys_enter == NULL)
  1951. goto out;
  1952. if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
  1953. goto out_delete_sys_enter;
  1954. sys_exit = perf_evsel__syscall_newtp("sys_exit", trace__sys_exit);
  1955. if (sys_exit == NULL)
  1956. goto out_delete_sys_enter;
  1957. if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
  1958. goto out_delete_sys_exit;
  1959. perf_evlist__add(evlist, sys_enter);
  1960. perf_evlist__add(evlist, sys_exit);
  1961. trace->syscalls.events.sys_enter = sys_enter;
  1962. trace->syscalls.events.sys_exit = sys_exit;
  1963. ret = 0;
  1964. out:
  1965. return ret;
  1966. out_delete_sys_exit:
  1967. perf_evsel__delete_priv(sys_exit);
  1968. out_delete_sys_enter:
  1969. perf_evsel__delete_priv(sys_enter);
  1970. goto out;
  1971. }
  1972. static int trace__set_ev_qualifier_filter(struct trace *trace)
  1973. {
  1974. int err = -1;
  1975. char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
  1976. trace->ev_qualifier_ids.nr,
  1977. trace->ev_qualifier_ids.entries);
  1978. if (filter == NULL)
  1979. goto out_enomem;
  1980. if (!perf_evsel__append_filter(trace->syscalls.events.sys_enter, "&&", filter))
  1981. err = perf_evsel__append_filter(trace->syscalls.events.sys_exit, "&&", filter);
  1982. free(filter);
  1983. out:
  1984. return err;
  1985. out_enomem:
  1986. errno = ENOMEM;
  1987. goto out;
  1988. }
  1989. static int trace__run(struct trace *trace, int argc, const char **argv)
  1990. {
  1991. struct perf_evlist *evlist = trace->evlist;
  1992. struct perf_evsel *evsel;
  1993. int err = -1, i;
  1994. unsigned long before;
  1995. const bool forks = argc > 0;
  1996. bool draining = false;
  1997. trace->live = true;
  1998. if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
  1999. goto out_error_raw_syscalls;
  2000. if (trace->trace_syscalls)
  2001. trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
  2002. if ((trace->trace_pgfaults & TRACE_PFMAJ) &&
  2003. perf_evlist__add_pgfault(evlist, PERF_COUNT_SW_PAGE_FAULTS_MAJ)) {
  2004. goto out_error_mem;
  2005. }
  2006. if ((trace->trace_pgfaults & TRACE_PFMIN) &&
  2007. perf_evlist__add_pgfault(evlist, PERF_COUNT_SW_PAGE_FAULTS_MIN))
  2008. goto out_error_mem;
  2009. if (trace->sched &&
  2010. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  2011. trace__sched_stat_runtime))
  2012. goto out_error_sched_stat_runtime;
  2013. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  2014. if (err < 0) {
  2015. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  2016. goto out_delete_evlist;
  2017. }
  2018. err = trace__symbols_init(trace, evlist);
  2019. if (err < 0) {
  2020. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  2021. goto out_delete_evlist;
  2022. }
  2023. perf_evlist__config(evlist, &trace->opts);
  2024. signal(SIGCHLD, sig_handler);
  2025. signal(SIGINT, sig_handler);
  2026. if (forks) {
  2027. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  2028. argv, false, NULL);
  2029. if (err < 0) {
  2030. fprintf(trace->output, "Couldn't run the workload!\n");
  2031. goto out_delete_evlist;
  2032. }
  2033. }
  2034. err = perf_evlist__open(evlist);
  2035. if (err < 0)
  2036. goto out_error_open;
  2037. /*
  2038. * Better not use !target__has_task() here because we need to cover the
  2039. * case where no threads were specified in the command line, but a
  2040. * workload was, and in that case we will fill in the thread_map when
  2041. * we fork the workload in perf_evlist__prepare_workload.
  2042. */
  2043. if (trace->filter_pids.nr > 0)
  2044. err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
  2045. else if (thread_map__pid(evlist->threads, 0) == -1)
  2046. err = perf_evlist__set_filter_pid(evlist, getpid());
  2047. if (err < 0)
  2048. goto out_error_mem;
  2049. if (trace->ev_qualifier_ids.nr > 0) {
  2050. err = trace__set_ev_qualifier_filter(trace);
  2051. if (err < 0)
  2052. goto out_errno;
  2053. pr_debug("event qualifier tracepoint filter: %s\n",
  2054. trace->syscalls.events.sys_exit->filter);
  2055. }
  2056. err = perf_evlist__apply_filters(evlist, &evsel);
  2057. if (err < 0)
  2058. goto out_error_apply_filters;
  2059. err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false);
  2060. if (err < 0)
  2061. goto out_error_mmap;
  2062. if (!target__none(&trace->opts.target))
  2063. perf_evlist__enable(evlist);
  2064. if (forks)
  2065. perf_evlist__start_workload(evlist);
  2066. trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
  2067. evlist->threads->nr > 1 ||
  2068. perf_evlist__first(evlist)->attr.inherit;
  2069. again:
  2070. before = trace->nr_events;
  2071. for (i = 0; i < evlist->nr_mmaps; i++) {
  2072. union perf_event *event;
  2073. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  2074. struct perf_sample sample;
  2075. ++trace->nr_events;
  2076. err = perf_evlist__parse_sample(evlist, event, &sample);
  2077. if (err) {
  2078. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  2079. goto next_event;
  2080. }
  2081. trace__handle_event(trace, event, &sample);
  2082. next_event:
  2083. perf_evlist__mmap_consume(evlist, i);
  2084. if (interrupted)
  2085. goto out_disable;
  2086. if (done && !draining) {
  2087. perf_evlist__disable(evlist);
  2088. draining = true;
  2089. }
  2090. }
  2091. }
  2092. if (trace->nr_events == before) {
  2093. int timeout = done ? 100 : -1;
  2094. if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
  2095. if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
  2096. draining = true;
  2097. goto again;
  2098. }
  2099. } else {
  2100. goto again;
  2101. }
  2102. out_disable:
  2103. thread__zput(trace->current);
  2104. perf_evlist__disable(evlist);
  2105. if (!err) {
  2106. if (trace->summary)
  2107. trace__fprintf_thread_summary(trace, trace->output);
  2108. if (trace->show_tool_stats) {
  2109. fprintf(trace->output, "Stats:\n "
  2110. " vfs_getname : %" PRIu64 "\n"
  2111. " proc_getname: %" PRIu64 "\n",
  2112. trace->stats.vfs_getname,
  2113. trace->stats.proc_getname);
  2114. }
  2115. }
  2116. out_delete_evlist:
  2117. perf_evlist__delete(evlist);
  2118. trace->evlist = NULL;
  2119. trace->live = false;
  2120. return err;
  2121. {
  2122. char errbuf[BUFSIZ];
  2123. out_error_sched_stat_runtime:
  2124. debugfs__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
  2125. goto out_error;
  2126. out_error_raw_syscalls:
  2127. debugfs__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
  2128. goto out_error;
  2129. out_error_mmap:
  2130. perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
  2131. goto out_error;
  2132. out_error_open:
  2133. perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  2134. out_error:
  2135. fprintf(trace->output, "%s\n", errbuf);
  2136. goto out_delete_evlist;
  2137. out_error_apply_filters:
  2138. fprintf(trace->output,
  2139. "Failed to set filter \"%s\" on event %s with %d (%s)\n",
  2140. evsel->filter, perf_evsel__name(evsel), errno,
  2141. strerror_r(errno, errbuf, sizeof(errbuf)));
  2142. goto out_delete_evlist;
  2143. }
  2144. out_error_mem:
  2145. fprintf(trace->output, "Not enough memory to run!\n");
  2146. goto out_delete_evlist;
  2147. out_errno:
  2148. fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
  2149. goto out_delete_evlist;
  2150. }
  2151. static int trace__replay(struct trace *trace)
  2152. {
  2153. const struct perf_evsel_str_handler handlers[] = {
  2154. { "probe:vfs_getname", trace__vfs_getname, },
  2155. };
  2156. struct perf_data_file file = {
  2157. .path = input_name,
  2158. .mode = PERF_DATA_MODE_READ,
  2159. .force = trace->force,
  2160. };
  2161. struct perf_session *session;
  2162. struct perf_evsel *evsel;
  2163. int err = -1;
  2164. trace->tool.sample = trace__process_sample;
  2165. trace->tool.mmap = perf_event__process_mmap;
  2166. trace->tool.mmap2 = perf_event__process_mmap2;
  2167. trace->tool.comm = perf_event__process_comm;
  2168. trace->tool.exit = perf_event__process_exit;
  2169. trace->tool.fork = perf_event__process_fork;
  2170. trace->tool.attr = perf_event__process_attr;
  2171. trace->tool.tracing_data = perf_event__process_tracing_data;
  2172. trace->tool.build_id = perf_event__process_build_id;
  2173. trace->tool.ordered_events = true;
  2174. trace->tool.ordering_requires_timestamps = true;
  2175. /* add tid to output */
  2176. trace->multiple_threads = true;
  2177. session = perf_session__new(&file, false, &trace->tool);
  2178. if (session == NULL)
  2179. return -1;
  2180. if (symbol__init(&session->header.env) < 0)
  2181. goto out;
  2182. trace->host = &session->machines.host;
  2183. err = perf_session__set_tracepoints_handlers(session, handlers);
  2184. if (err)
  2185. goto out;
  2186. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2187. "raw_syscalls:sys_enter");
  2188. /* older kernels have syscalls tp versus raw_syscalls */
  2189. if (evsel == NULL)
  2190. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2191. "syscalls:sys_enter");
  2192. if (evsel &&
  2193. (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 ||
  2194. perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
  2195. pr_err("Error during initialize raw_syscalls:sys_enter event\n");
  2196. goto out;
  2197. }
  2198. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2199. "raw_syscalls:sys_exit");
  2200. if (evsel == NULL)
  2201. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2202. "syscalls:sys_exit");
  2203. if (evsel &&
  2204. (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 ||
  2205. perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
  2206. pr_err("Error during initialize raw_syscalls:sys_exit event\n");
  2207. goto out;
  2208. }
  2209. evlist__for_each(session->evlist, evsel) {
  2210. if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
  2211. (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
  2212. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  2213. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
  2214. evsel->handler = trace__pgfault;
  2215. }
  2216. err = parse_target_str(trace);
  2217. if (err != 0)
  2218. goto out;
  2219. setup_pager();
  2220. err = perf_session__process_events(session);
  2221. if (err)
  2222. pr_err("Failed to process events, error %d", err);
  2223. else if (trace->summary)
  2224. trace__fprintf_thread_summary(trace, trace->output);
  2225. out:
  2226. perf_session__delete(session);
  2227. return err;
  2228. }
  2229. static size_t trace__fprintf_threads_header(FILE *fp)
  2230. {
  2231. size_t printed;
  2232. printed = fprintf(fp, "\n Summary of events:\n\n");
  2233. return printed;
  2234. }
  2235. static size_t thread__dump_stats(struct thread_trace *ttrace,
  2236. struct trace *trace, FILE *fp)
  2237. {
  2238. struct stats *stats;
  2239. size_t printed = 0;
  2240. struct syscall *sc;
  2241. struct int_node *inode = intlist__first(ttrace->syscall_stats);
  2242. if (inode == NULL)
  2243. return 0;
  2244. printed += fprintf(fp, "\n");
  2245. printed += fprintf(fp, " syscall calls min avg max stddev\n");
  2246. printed += fprintf(fp, " (msec) (msec) (msec) (%%)\n");
  2247. printed += fprintf(fp, " --------------- -------- --------- --------- --------- ------\n");
  2248. /* each int_node is a syscall */
  2249. while (inode) {
  2250. stats = inode->priv;
  2251. if (stats) {
  2252. double min = (double)(stats->min) / NSEC_PER_MSEC;
  2253. double max = (double)(stats->max) / NSEC_PER_MSEC;
  2254. double avg = avg_stats(stats);
  2255. double pct;
  2256. u64 n = (u64) stats->n;
  2257. pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
  2258. avg /= NSEC_PER_MSEC;
  2259. sc = &trace->syscalls.table[inode->i];
  2260. printed += fprintf(fp, " %-15s", sc->name);
  2261. printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f",
  2262. n, min, avg);
  2263. printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
  2264. }
  2265. inode = intlist__next(inode);
  2266. }
  2267. printed += fprintf(fp, "\n\n");
  2268. return printed;
  2269. }
  2270. /* struct used to pass data to per-thread function */
  2271. struct summary_data {
  2272. FILE *fp;
  2273. struct trace *trace;
  2274. size_t printed;
  2275. };
  2276. static int trace__fprintf_one_thread(struct thread *thread, void *priv)
  2277. {
  2278. struct summary_data *data = priv;
  2279. FILE *fp = data->fp;
  2280. size_t printed = data->printed;
  2281. struct trace *trace = data->trace;
  2282. struct thread_trace *ttrace = thread__priv(thread);
  2283. double ratio;
  2284. if (ttrace == NULL)
  2285. return 0;
  2286. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  2287. printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
  2288. printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
  2289. printed += fprintf(fp, "%.1f%%", ratio);
  2290. if (ttrace->pfmaj)
  2291. printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
  2292. if (ttrace->pfmin)
  2293. printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
  2294. printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
  2295. printed += thread__dump_stats(ttrace, trace, fp);
  2296. data->printed += printed;
  2297. return 0;
  2298. }
  2299. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  2300. {
  2301. struct summary_data data = {
  2302. .fp = fp,
  2303. .trace = trace
  2304. };
  2305. data.printed = trace__fprintf_threads_header(fp);
  2306. machine__for_each_thread(trace->host, trace__fprintf_one_thread, &data);
  2307. return data.printed;
  2308. }
  2309. static int trace__set_duration(const struct option *opt, const char *str,
  2310. int unset __maybe_unused)
  2311. {
  2312. struct trace *trace = opt->value;
  2313. trace->duration_filter = atof(str);
  2314. return 0;
  2315. }
  2316. static int trace__set_filter_pids(const struct option *opt, const char *str,
  2317. int unset __maybe_unused)
  2318. {
  2319. int ret = -1;
  2320. size_t i;
  2321. struct trace *trace = opt->value;
  2322. /*
  2323. * FIXME: introduce a intarray class, plain parse csv and create a
  2324. * { int nr, int entries[] } struct...
  2325. */
  2326. struct intlist *list = intlist__new(str);
  2327. if (list == NULL)
  2328. return -1;
  2329. i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
  2330. trace->filter_pids.entries = calloc(i, sizeof(pid_t));
  2331. if (trace->filter_pids.entries == NULL)
  2332. goto out;
  2333. trace->filter_pids.entries[0] = getpid();
  2334. for (i = 1; i < trace->filter_pids.nr; ++i)
  2335. trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
  2336. intlist__delete(list);
  2337. ret = 0;
  2338. out:
  2339. return ret;
  2340. }
  2341. static int trace__open_output(struct trace *trace, const char *filename)
  2342. {
  2343. struct stat st;
  2344. if (!stat(filename, &st) && st.st_size) {
  2345. char oldname[PATH_MAX];
  2346. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  2347. unlink(oldname);
  2348. rename(filename, oldname);
  2349. }
  2350. trace->output = fopen(filename, "w");
  2351. return trace->output == NULL ? -errno : 0;
  2352. }
  2353. static int parse_pagefaults(const struct option *opt, const char *str,
  2354. int unset __maybe_unused)
  2355. {
  2356. int *trace_pgfaults = opt->value;
  2357. if (strcmp(str, "all") == 0)
  2358. *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
  2359. else if (strcmp(str, "maj") == 0)
  2360. *trace_pgfaults |= TRACE_PFMAJ;
  2361. else if (strcmp(str, "min") == 0)
  2362. *trace_pgfaults |= TRACE_PFMIN;
  2363. else
  2364. return -1;
  2365. return 0;
  2366. }
  2367. static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
  2368. {
  2369. struct perf_evsel *evsel;
  2370. evlist__for_each(evlist, evsel)
  2371. evsel->handler = handler;
  2372. }
  2373. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  2374. {
  2375. const char *trace_usage[] = {
  2376. "perf trace [<options>] [<command>]",
  2377. "perf trace [<options>] -- <command> [<options>]",
  2378. "perf trace record [<options>] [<command>]",
  2379. "perf trace record [<options>] -- <command> [<options>]",
  2380. NULL
  2381. };
  2382. struct trace trace = {
  2383. .audit = {
  2384. .machine = audit_detect_machine(),
  2385. .open_id = audit_name_to_syscall("open", trace.audit.machine),
  2386. },
  2387. .syscalls = {
  2388. . max = -1,
  2389. },
  2390. .opts = {
  2391. .target = {
  2392. .uid = UINT_MAX,
  2393. .uses_mmap = true,
  2394. },
  2395. .user_freq = UINT_MAX,
  2396. .user_interval = ULLONG_MAX,
  2397. .no_buffering = true,
  2398. .mmap_pages = UINT_MAX,
  2399. .proc_map_timeout = 500,
  2400. },
  2401. .output = stdout,
  2402. .show_comm = true,
  2403. .trace_syscalls = true,
  2404. };
  2405. const char *output_name = NULL;
  2406. const char *ev_qualifier_str = NULL;
  2407. const struct option trace_options[] = {
  2408. OPT_CALLBACK(0, "event", &trace.evlist, "event",
  2409. "event selector. use 'perf list' to list available events",
  2410. parse_events_option),
  2411. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  2412. "show the thread COMM next to its id"),
  2413. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  2414. OPT_STRING('e', "expr", &ev_qualifier_str, "expr", "list of syscalls to trace"),
  2415. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  2416. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  2417. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  2418. "trace events on existing process id"),
  2419. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  2420. "trace events on existing thread id"),
  2421. OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
  2422. "pids to filter (by the kernel)", trace__set_filter_pids),
  2423. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  2424. "system-wide collection from all CPUs"),
  2425. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  2426. "list of cpus to monitor"),
  2427. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  2428. "child tasks do not inherit counters"),
  2429. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  2430. "number of mmap data pages",
  2431. perf_evlist__parse_mmap_pages),
  2432. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  2433. "user to profile"),
  2434. OPT_CALLBACK(0, "duration", &trace, "float",
  2435. "show only events with duration > N.M ms",
  2436. trace__set_duration),
  2437. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  2438. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  2439. OPT_BOOLEAN('T', "time", &trace.full_time,
  2440. "Show full timestamp, not time relative to first start"),
  2441. OPT_BOOLEAN('s', "summary", &trace.summary_only,
  2442. "Show only syscall summary with statistics"),
  2443. OPT_BOOLEAN('S', "with-summary", &trace.summary,
  2444. "Show all syscalls and summary with statistics"),
  2445. OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
  2446. "Trace pagefaults", parse_pagefaults, "maj"),
  2447. OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
  2448. OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
  2449. OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
  2450. "per thread proc mmap processing timeout in ms"),
  2451. OPT_END()
  2452. };
  2453. const char * const trace_subcommands[] = { "record", NULL };
  2454. int err;
  2455. char bf[BUFSIZ];
  2456. signal(SIGSEGV, sighandler_dump_stack);
  2457. signal(SIGFPE, sighandler_dump_stack);
  2458. trace.evlist = perf_evlist__new();
  2459. if (trace.evlist == NULL) {
  2460. pr_err("Not enough memory to run!\n");
  2461. err = -ENOMEM;
  2462. goto out;
  2463. }
  2464. argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
  2465. trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  2466. if (trace.trace_pgfaults) {
  2467. trace.opts.sample_address = true;
  2468. trace.opts.sample_time = true;
  2469. }
  2470. if (trace.evlist->nr_entries > 0)
  2471. evlist__set_evsel_handler(trace.evlist, trace__event_handler);
  2472. if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
  2473. return trace__record(&trace, argc-1, &argv[1]);
  2474. /* summary_only implies summary option, but don't overwrite summary if set */
  2475. if (trace.summary_only)
  2476. trace.summary = trace.summary_only;
  2477. if (!trace.trace_syscalls && !trace.trace_pgfaults &&
  2478. trace.evlist->nr_entries == 0 /* Was --events used? */) {
  2479. pr_err("Please specify something to trace.\n");
  2480. return -1;
  2481. }
  2482. if (output_name != NULL) {
  2483. err = trace__open_output(&trace, output_name);
  2484. if (err < 0) {
  2485. perror("failed to create output file");
  2486. goto out;
  2487. }
  2488. }
  2489. if (ev_qualifier_str != NULL) {
  2490. const char *s = ev_qualifier_str;
  2491. struct strlist_config slist_config = {
  2492. .dirname = system_path(STRACE_GROUPS_DIR),
  2493. };
  2494. trace.not_ev_qualifier = *s == '!';
  2495. if (trace.not_ev_qualifier)
  2496. ++s;
  2497. trace.ev_qualifier = strlist__new(s, &slist_config);
  2498. if (trace.ev_qualifier == NULL) {
  2499. fputs("Not enough memory to parse event qualifier",
  2500. trace.output);
  2501. err = -ENOMEM;
  2502. goto out_close;
  2503. }
  2504. err = trace__validate_ev_qualifier(&trace);
  2505. if (err)
  2506. goto out_close;
  2507. }
  2508. err = target__validate(&trace.opts.target);
  2509. if (err) {
  2510. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2511. fprintf(trace.output, "%s", bf);
  2512. goto out_close;
  2513. }
  2514. err = target__parse_uid(&trace.opts.target);
  2515. if (err) {
  2516. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2517. fprintf(trace.output, "%s", bf);
  2518. goto out_close;
  2519. }
  2520. if (!argc && target__none(&trace.opts.target))
  2521. trace.opts.target.system_wide = true;
  2522. if (input_name)
  2523. err = trace__replay(&trace);
  2524. else
  2525. err = trace__run(&trace, argc, argv);
  2526. out_close:
  2527. if (output_name != NULL)
  2528. fclose(trace.output);
  2529. out:
  2530. return err;
  2531. }