builtin-trace.c 84 KB

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