builtin-trace.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949
  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 <linux/err.h>
  42. #include <linux/filter.h>
  43. #include <linux/audit.h>
  44. #include <linux/random.h>
  45. #include <linux/stringify.h>
  46. #ifndef O_CLOEXEC
  47. # define O_CLOEXEC 02000000
  48. #endif
  49. struct trace {
  50. struct perf_tool tool;
  51. struct syscalltbl *sctbl;
  52. struct {
  53. int max;
  54. struct syscall *table;
  55. struct {
  56. struct perf_evsel *sys_enter,
  57. *sys_exit;
  58. } events;
  59. } syscalls;
  60. struct record_opts opts;
  61. struct perf_evlist *evlist;
  62. struct machine *host;
  63. struct thread *current;
  64. u64 base_time;
  65. FILE *output;
  66. unsigned long nr_events;
  67. struct strlist *ev_qualifier;
  68. struct {
  69. size_t nr;
  70. int *entries;
  71. } ev_qualifier_ids;
  72. struct intlist *tid_list;
  73. struct intlist *pid_list;
  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_setscheduler", .errmsg = true,
  648. .arg_scnprintf = { [1] = SCA_SCHED_POLICY, /* policy */ }, },
  649. { .name = "seccomp", .errmsg = true,
  650. .arg_scnprintf = { [0] = SCA_SECCOMP_OP, /* op */
  651. [1] = SCA_SECCOMP_FLAGS, /* flags */ }, },
  652. { .name = "select", .errmsg = true, .timeout = true, },
  653. { .name = "sendmmsg", .errmsg = true,
  654. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  655. { .name = "sendmsg", .errmsg = true,
  656. .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
  657. { .name = "sendto", .errmsg = true,
  658. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  659. { .name = "set_tid_address", .errpid = true, },
  660. { .name = "setitimer", .errmsg = true, STRARRAY(0, which, itimers), },
  661. { .name = "setpgid", .errmsg = true, },
  662. { .name = "setrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
  663. { .name = "setxattr", .errmsg = true, },
  664. { .name = "shutdown", .errmsg = true, },
  665. { .name = "socket", .errmsg = true,
  666. .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
  667. [1] = SCA_SK_TYPE, /* type */ },
  668. .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
  669. { .name = "socketpair", .errmsg = true,
  670. .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
  671. [1] = SCA_SK_TYPE, /* type */ },
  672. .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
  673. { .name = "stat", .errmsg = true, .alias = "newstat", },
  674. { .name = "statfs", .errmsg = true, },
  675. { .name = "swapoff", .errmsg = true,
  676. .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, },
  677. { .name = "swapon", .errmsg = true,
  678. .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, },
  679. { .name = "symlinkat", .errmsg = true,
  680. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  681. { .name = "tgkill", .errmsg = true,
  682. .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
  683. { .name = "tkill", .errmsg = true,
  684. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  685. { .name = "truncate", .errmsg = true, },
  686. { .name = "uname", .errmsg = true, .alias = "newuname", },
  687. { .name = "unlinkat", .errmsg = true,
  688. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  689. { .name = "utime", .errmsg = true, },
  690. { .name = "utimensat", .errmsg = true,
  691. .arg_scnprintf = { [0] = SCA_FDAT, /* dirfd */ }, },
  692. { .name = "utimes", .errmsg = true, },
  693. { .name = "vmsplice", .errmsg = true, },
  694. { .name = "wait4", .errpid = true,
  695. .arg_scnprintf = { [2] = SCA_WAITID_OPTIONS, /* options */ }, },
  696. { .name = "waitid", .errpid = true,
  697. .arg_scnprintf = { [3] = SCA_WAITID_OPTIONS, /* options */ }, },
  698. { .name = "write", .errmsg = true, },
  699. { .name = "writev", .errmsg = true, },
  700. };
  701. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  702. {
  703. const struct syscall_fmt *fmt = fmtp;
  704. return strcmp(name, fmt->name);
  705. }
  706. static struct syscall_fmt *syscall_fmt__find(const char *name)
  707. {
  708. const int nmemb = ARRAY_SIZE(syscall_fmts);
  709. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  710. }
  711. struct syscall {
  712. struct event_format *tp_format;
  713. int nr_args;
  714. struct format_field *args;
  715. const char *name;
  716. bool is_exit;
  717. struct syscall_fmt *fmt;
  718. size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  719. void **arg_parm;
  720. };
  721. static size_t fprintf_duration(unsigned long t, FILE *fp)
  722. {
  723. double duration = (double)t / NSEC_PER_MSEC;
  724. size_t printed = fprintf(fp, "(");
  725. if (duration >= 1.0)
  726. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  727. else if (duration >= 0.01)
  728. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  729. else
  730. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  731. return printed + fprintf(fp, "): ");
  732. }
  733. /**
  734. * filename.ptr: The filename char pointer that will be vfs_getname'd
  735. * filename.entry_str_pos: Where to insert the string translated from
  736. * filename.ptr by the vfs_getname tracepoint/kprobe.
  737. */
  738. struct thread_trace {
  739. u64 entry_time;
  740. u64 exit_time;
  741. bool entry_pending;
  742. unsigned long nr_events;
  743. unsigned long pfmaj, pfmin;
  744. char *entry_str;
  745. double runtime_ms;
  746. struct {
  747. unsigned long ptr;
  748. short int entry_str_pos;
  749. bool pending_open;
  750. unsigned int namelen;
  751. char *name;
  752. } filename;
  753. struct {
  754. int max;
  755. char **table;
  756. } paths;
  757. struct intlist *syscall_stats;
  758. };
  759. static struct thread_trace *thread_trace__new(void)
  760. {
  761. struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
  762. if (ttrace)
  763. ttrace->paths.max = -1;
  764. ttrace->syscall_stats = intlist__new(NULL);
  765. return ttrace;
  766. }
  767. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  768. {
  769. struct thread_trace *ttrace;
  770. if (thread == NULL)
  771. goto fail;
  772. if (thread__priv(thread) == NULL)
  773. thread__set_priv(thread, thread_trace__new());
  774. if (thread__priv(thread) == NULL)
  775. goto fail;
  776. ttrace = thread__priv(thread);
  777. ++ttrace->nr_events;
  778. return ttrace;
  779. fail:
  780. color_fprintf(fp, PERF_COLOR_RED,
  781. "WARNING: not enough memory, dropping samples!\n");
  782. return NULL;
  783. }
  784. #define TRACE_PFMAJ (1 << 0)
  785. #define TRACE_PFMIN (1 << 1)
  786. static const size_t trace__entry_str_size = 2048;
  787. static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
  788. {
  789. struct thread_trace *ttrace = thread__priv(thread);
  790. if (fd > ttrace->paths.max) {
  791. char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
  792. if (npath == NULL)
  793. return -1;
  794. if (ttrace->paths.max != -1) {
  795. memset(npath + ttrace->paths.max + 1, 0,
  796. (fd - ttrace->paths.max) * sizeof(char *));
  797. } else {
  798. memset(npath, 0, (fd + 1) * sizeof(char *));
  799. }
  800. ttrace->paths.table = npath;
  801. ttrace->paths.max = fd;
  802. }
  803. ttrace->paths.table[fd] = strdup(pathname);
  804. return ttrace->paths.table[fd] != NULL ? 0 : -1;
  805. }
  806. static int thread__read_fd_path(struct thread *thread, int fd)
  807. {
  808. char linkname[PATH_MAX], pathname[PATH_MAX];
  809. struct stat st;
  810. int ret;
  811. if (thread->pid_ == thread->tid) {
  812. scnprintf(linkname, sizeof(linkname),
  813. "/proc/%d/fd/%d", thread->pid_, fd);
  814. } else {
  815. scnprintf(linkname, sizeof(linkname),
  816. "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
  817. }
  818. if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
  819. return -1;
  820. ret = readlink(linkname, pathname, sizeof(pathname));
  821. if (ret < 0 || ret > st.st_size)
  822. return -1;
  823. pathname[ret] = '\0';
  824. return trace__set_fd_pathname(thread, fd, pathname);
  825. }
  826. static const char *thread__fd_path(struct thread *thread, int fd,
  827. struct trace *trace)
  828. {
  829. struct thread_trace *ttrace = thread__priv(thread);
  830. if (ttrace == NULL)
  831. return NULL;
  832. if (fd < 0)
  833. return NULL;
  834. if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
  835. if (!trace->live)
  836. return NULL;
  837. ++trace->stats.proc_getname;
  838. if (thread__read_fd_path(thread, fd))
  839. return NULL;
  840. }
  841. return ttrace->paths.table[fd];
  842. }
  843. static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
  844. struct syscall_arg *arg)
  845. {
  846. int fd = arg->val;
  847. size_t printed = scnprintf(bf, size, "%d", fd);
  848. const char *path = thread__fd_path(arg->thread, fd, arg->trace);
  849. if (path)
  850. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  851. return printed;
  852. }
  853. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  854. struct syscall_arg *arg)
  855. {
  856. int fd = arg->val;
  857. size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
  858. struct thread_trace *ttrace = thread__priv(arg->thread);
  859. if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
  860. zfree(&ttrace->paths.table[fd]);
  861. return printed;
  862. }
  863. static void thread__set_filename_pos(struct thread *thread, const char *bf,
  864. unsigned long ptr)
  865. {
  866. struct thread_trace *ttrace = thread__priv(thread);
  867. ttrace->filename.ptr = ptr;
  868. ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
  869. }
  870. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  871. struct syscall_arg *arg)
  872. {
  873. unsigned long ptr = arg->val;
  874. if (!arg->trace->vfs_getname)
  875. return scnprintf(bf, size, "%#x", ptr);
  876. thread__set_filename_pos(arg->thread, bf, ptr);
  877. return 0;
  878. }
  879. static bool trace__filter_duration(struct trace *trace, double t)
  880. {
  881. return t < (trace->duration_filter * NSEC_PER_MSEC);
  882. }
  883. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  884. {
  885. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  886. return fprintf(fp, "%10.3f ", ts);
  887. }
  888. static bool done = false;
  889. static bool interrupted = false;
  890. static void sig_handler(int sig)
  891. {
  892. done = true;
  893. interrupted = sig == SIGINT;
  894. }
  895. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  896. u64 duration, u64 tstamp, FILE *fp)
  897. {
  898. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  899. printed += fprintf_duration(duration, fp);
  900. if (trace->multiple_threads) {
  901. if (trace->show_comm)
  902. printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
  903. printed += fprintf(fp, "%d ", thread->tid);
  904. }
  905. return printed;
  906. }
  907. static int trace__process_event(struct trace *trace, struct machine *machine,
  908. union perf_event *event, struct perf_sample *sample)
  909. {
  910. int ret = 0;
  911. switch (event->header.type) {
  912. case PERF_RECORD_LOST:
  913. color_fprintf(trace->output, PERF_COLOR_RED,
  914. "LOST %" PRIu64 " events!\n", event->lost.lost);
  915. ret = machine__process_lost_event(machine, event, sample);
  916. break;
  917. default:
  918. ret = machine__process_event(machine, event, sample);
  919. break;
  920. }
  921. return ret;
  922. }
  923. static int trace__tool_process(struct perf_tool *tool,
  924. union perf_event *event,
  925. struct perf_sample *sample,
  926. struct machine *machine)
  927. {
  928. struct trace *trace = container_of(tool, struct trace, tool);
  929. return trace__process_event(trace, machine, event, sample);
  930. }
  931. static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  932. {
  933. struct machine *machine = vmachine;
  934. if (machine->kptr_restrict_warned)
  935. return NULL;
  936. if (symbol_conf.kptr_restrict) {
  937. pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  938. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  939. "Kernel samples will not be resolved.\n");
  940. machine->kptr_restrict_warned = true;
  941. return NULL;
  942. }
  943. return machine__resolve_kernel_addr(vmachine, addrp, modp);
  944. }
  945. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  946. {
  947. int err = symbol__init(NULL);
  948. if (err)
  949. return err;
  950. trace->host = machine__new_host();
  951. if (trace->host == NULL)
  952. return -ENOMEM;
  953. if (trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr) < 0)
  954. return -errno;
  955. err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  956. evlist->threads, trace__tool_process, false,
  957. trace->opts.proc_map_timeout);
  958. if (err)
  959. symbol__exit();
  960. return err;
  961. }
  962. static int syscall__set_arg_fmts(struct syscall *sc)
  963. {
  964. struct format_field *field;
  965. int idx = 0, len;
  966. sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *));
  967. if (sc->arg_scnprintf == NULL)
  968. return -1;
  969. if (sc->fmt)
  970. sc->arg_parm = sc->fmt->arg_parm;
  971. for (field = sc->args; field; field = field->next) {
  972. if (sc->fmt && sc->fmt->arg_scnprintf[idx])
  973. sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
  974. else if (strcmp(field->type, "const char *") == 0 &&
  975. (strcmp(field->name, "filename") == 0 ||
  976. strcmp(field->name, "path") == 0 ||
  977. strcmp(field->name, "pathname") == 0))
  978. sc->arg_scnprintf[idx] = SCA_FILENAME;
  979. else if (field->flags & FIELD_IS_POINTER)
  980. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  981. else if (strcmp(field->type, "pid_t") == 0)
  982. sc->arg_scnprintf[idx] = SCA_PID;
  983. else if (strcmp(field->type, "umode_t") == 0)
  984. sc->arg_scnprintf[idx] = SCA_MODE_T;
  985. else if ((strcmp(field->type, "int") == 0 ||
  986. strcmp(field->type, "unsigned int") == 0 ||
  987. strcmp(field->type, "long") == 0) &&
  988. (len = strlen(field->name)) >= 2 &&
  989. strcmp(field->name + len - 2, "fd") == 0) {
  990. /*
  991. * /sys/kernel/tracing/events/syscalls/sys_enter*
  992. * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
  993. * 65 int
  994. * 23 unsigned int
  995. * 7 unsigned long
  996. */
  997. sc->arg_scnprintf[idx] = SCA_FD;
  998. }
  999. ++idx;
  1000. }
  1001. return 0;
  1002. }
  1003. static int trace__read_syscall_info(struct trace *trace, int id)
  1004. {
  1005. char tp_name[128];
  1006. struct syscall *sc;
  1007. const char *name = syscalltbl__name(trace->sctbl, id);
  1008. if (name == NULL)
  1009. return -1;
  1010. if (id > trace->syscalls.max) {
  1011. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  1012. if (nsyscalls == NULL)
  1013. return -1;
  1014. if (trace->syscalls.max != -1) {
  1015. memset(nsyscalls + trace->syscalls.max + 1, 0,
  1016. (id - trace->syscalls.max) * sizeof(*sc));
  1017. } else {
  1018. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  1019. }
  1020. trace->syscalls.table = nsyscalls;
  1021. trace->syscalls.max = id;
  1022. }
  1023. sc = trace->syscalls.table + id;
  1024. sc->name = name;
  1025. sc->fmt = syscall_fmt__find(sc->name);
  1026. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1027. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1028. if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
  1029. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1030. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1031. }
  1032. if (IS_ERR(sc->tp_format))
  1033. return -1;
  1034. sc->args = sc->tp_format->format.fields;
  1035. sc->nr_args = sc->tp_format->format.nr_fields;
  1036. /*
  1037. * We need to check and discard the first variable '__syscall_nr'
  1038. * or 'nr' that mean the syscall number. It is needless here.
  1039. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
  1040. */
  1041. if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
  1042. sc->args = sc->args->next;
  1043. --sc->nr_args;
  1044. }
  1045. sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
  1046. return syscall__set_arg_fmts(sc);
  1047. }
  1048. static int trace__validate_ev_qualifier(struct trace *trace)
  1049. {
  1050. int err = 0, i;
  1051. struct str_node *pos;
  1052. trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
  1053. trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
  1054. sizeof(trace->ev_qualifier_ids.entries[0]));
  1055. if (trace->ev_qualifier_ids.entries == NULL) {
  1056. fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
  1057. trace->output);
  1058. err = -EINVAL;
  1059. goto out;
  1060. }
  1061. i = 0;
  1062. strlist__for_each_entry(pos, trace->ev_qualifier) {
  1063. const char *sc = pos->s;
  1064. int id = syscalltbl__id(trace->sctbl, sc);
  1065. if (id < 0) {
  1066. if (err == 0) {
  1067. fputs("Error:\tInvalid syscall ", trace->output);
  1068. err = -EINVAL;
  1069. } else {
  1070. fputs(", ", trace->output);
  1071. }
  1072. fputs(sc, trace->output);
  1073. }
  1074. trace->ev_qualifier_ids.entries[i++] = id;
  1075. }
  1076. if (err < 0) {
  1077. fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
  1078. "\nHint:\tand: 'man syscalls'\n", trace->output);
  1079. zfree(&trace->ev_qualifier_ids.entries);
  1080. trace->ev_qualifier_ids.nr = 0;
  1081. }
  1082. out:
  1083. return err;
  1084. }
  1085. /*
  1086. * args is to be interpreted as a series of longs but we need to handle
  1087. * 8-byte unaligned accesses. args points to raw_data within the event
  1088. * and raw_data is guaranteed to be 8-byte unaligned because it is
  1089. * preceded by raw_size which is a u32. So we need to copy args to a temp
  1090. * variable to read it. Most notably this avoids extended load instructions
  1091. * on unaligned addresses
  1092. */
  1093. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  1094. unsigned char *args, struct trace *trace,
  1095. struct thread *thread)
  1096. {
  1097. size_t printed = 0;
  1098. unsigned char *p;
  1099. unsigned long val;
  1100. if (sc->args != NULL) {
  1101. struct format_field *field;
  1102. u8 bit = 1;
  1103. struct syscall_arg arg = {
  1104. .idx = 0,
  1105. .mask = 0,
  1106. .trace = trace,
  1107. .thread = thread,
  1108. };
  1109. for (field = sc->args; field;
  1110. field = field->next, ++arg.idx, bit <<= 1) {
  1111. if (arg.mask & bit)
  1112. continue;
  1113. /* special care for unaligned accesses */
  1114. p = args + sizeof(unsigned long) * arg.idx;
  1115. memcpy(&val, p, sizeof(val));
  1116. /*
  1117. * Suppress this argument if its value is zero and
  1118. * and we don't have a string associated in an
  1119. * strarray for it.
  1120. */
  1121. if (val == 0 &&
  1122. !(sc->arg_scnprintf &&
  1123. sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
  1124. sc->arg_parm[arg.idx]))
  1125. continue;
  1126. printed += scnprintf(bf + printed, size - printed,
  1127. "%s%s: ", printed ? ", " : "", field->name);
  1128. if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
  1129. arg.val = val;
  1130. if (sc->arg_parm)
  1131. arg.parm = sc->arg_parm[arg.idx];
  1132. printed += sc->arg_scnprintf[arg.idx](bf + printed,
  1133. size - printed, &arg);
  1134. } else {
  1135. printed += scnprintf(bf + printed, size - printed,
  1136. "%ld", val);
  1137. }
  1138. }
  1139. } else if (IS_ERR(sc->tp_format)) {
  1140. /*
  1141. * If we managed to read the tracepoint /format file, then we
  1142. * may end up not having any args, like with gettid(), so only
  1143. * print the raw args when we didn't manage to read it.
  1144. */
  1145. int i = 0;
  1146. while (i < 6) {
  1147. /* special care for unaligned accesses */
  1148. p = args + sizeof(unsigned long) * i;
  1149. memcpy(&val, p, sizeof(val));
  1150. printed += scnprintf(bf + printed, size - printed,
  1151. "%sarg%d: %ld",
  1152. printed ? ", " : "", i, val);
  1153. ++i;
  1154. }
  1155. }
  1156. return printed;
  1157. }
  1158. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  1159. union perf_event *event,
  1160. struct perf_sample *sample);
  1161. static struct syscall *trace__syscall_info(struct trace *trace,
  1162. struct perf_evsel *evsel, int id)
  1163. {
  1164. if (id < 0) {
  1165. /*
  1166. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  1167. * before that, leaving at a higher verbosity level till that is
  1168. * explained. Reproduced with plain ftrace with:
  1169. *
  1170. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  1171. * grep "NR -1 " /t/trace_pipe
  1172. *
  1173. * After generating some load on the machine.
  1174. */
  1175. if (verbose > 1) {
  1176. static u64 n;
  1177. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  1178. id, perf_evsel__name(evsel), ++n);
  1179. }
  1180. return NULL;
  1181. }
  1182. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  1183. trace__read_syscall_info(trace, id))
  1184. goto out_cant_read;
  1185. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  1186. goto out_cant_read;
  1187. return &trace->syscalls.table[id];
  1188. out_cant_read:
  1189. if (verbose) {
  1190. fprintf(trace->output, "Problems reading syscall %d", id);
  1191. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  1192. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  1193. fputs(" information\n", trace->output);
  1194. }
  1195. return NULL;
  1196. }
  1197. static void thread__update_stats(struct thread_trace *ttrace,
  1198. int id, struct perf_sample *sample)
  1199. {
  1200. struct int_node *inode;
  1201. struct stats *stats;
  1202. u64 duration = 0;
  1203. inode = intlist__findnew(ttrace->syscall_stats, id);
  1204. if (inode == NULL)
  1205. return;
  1206. stats = inode->priv;
  1207. if (stats == NULL) {
  1208. stats = malloc(sizeof(struct stats));
  1209. if (stats == NULL)
  1210. return;
  1211. init_stats(stats);
  1212. inode->priv = stats;
  1213. }
  1214. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  1215. duration = sample->time - ttrace->entry_time;
  1216. update_stats(stats, duration);
  1217. }
  1218. static int trace__printf_interrupted_entry(struct trace *trace, struct perf_sample *sample)
  1219. {
  1220. struct thread_trace *ttrace;
  1221. u64 duration;
  1222. size_t printed;
  1223. if (trace->current == NULL)
  1224. return 0;
  1225. ttrace = thread__priv(trace->current);
  1226. if (!ttrace->entry_pending)
  1227. return 0;
  1228. duration = sample->time - ttrace->entry_time;
  1229. printed = trace__fprintf_entry_head(trace, trace->current, duration, sample->time, trace->output);
  1230. printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
  1231. ttrace->entry_pending = false;
  1232. return printed;
  1233. }
  1234. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  1235. union perf_event *event __maybe_unused,
  1236. struct perf_sample *sample)
  1237. {
  1238. char *msg;
  1239. void *args;
  1240. size_t printed = 0;
  1241. struct thread *thread;
  1242. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1243. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1244. struct thread_trace *ttrace;
  1245. if (sc == NULL)
  1246. return -1;
  1247. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1248. ttrace = thread__trace(thread, trace->output);
  1249. if (ttrace == NULL)
  1250. goto out_put;
  1251. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1252. if (ttrace->entry_str == NULL) {
  1253. ttrace->entry_str = malloc(trace__entry_str_size);
  1254. if (!ttrace->entry_str)
  1255. goto out_put;
  1256. }
  1257. if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
  1258. trace__printf_interrupted_entry(trace, sample);
  1259. ttrace->entry_time = sample->time;
  1260. msg = ttrace->entry_str;
  1261. printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
  1262. printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
  1263. args, trace, thread);
  1264. if (sc->is_exit) {
  1265. if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) {
  1266. trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
  1267. fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
  1268. }
  1269. } else {
  1270. ttrace->entry_pending = true;
  1271. /* See trace__vfs_getname & trace__sys_exit */
  1272. ttrace->filename.pending_open = false;
  1273. }
  1274. if (trace->current != thread) {
  1275. thread__put(trace->current);
  1276. trace->current = thread__get(thread);
  1277. }
  1278. err = 0;
  1279. out_put:
  1280. thread__put(thread);
  1281. return err;
  1282. }
  1283. static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
  1284. struct perf_sample *sample,
  1285. struct callchain_cursor *cursor)
  1286. {
  1287. struct addr_location al;
  1288. if (machine__resolve(trace->host, &al, sample) < 0 ||
  1289. thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, trace->max_stack))
  1290. return -1;
  1291. return 0;
  1292. }
  1293. static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
  1294. {
  1295. /* TODO: user-configurable print_opts */
  1296. const unsigned int print_opts = EVSEL__PRINT_SYM |
  1297. EVSEL__PRINT_DSO |
  1298. EVSEL__PRINT_UNKNOWN_AS_ADDR;
  1299. return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
  1300. }
  1301. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  1302. union perf_event *event __maybe_unused,
  1303. struct perf_sample *sample)
  1304. {
  1305. long ret;
  1306. u64 duration = 0;
  1307. struct thread *thread;
  1308. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
  1309. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1310. struct thread_trace *ttrace;
  1311. if (sc == NULL)
  1312. return -1;
  1313. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1314. ttrace = thread__trace(thread, trace->output);
  1315. if (ttrace == NULL)
  1316. goto out_put;
  1317. if (trace->summary)
  1318. thread__update_stats(ttrace, id, sample);
  1319. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  1320. if (id == trace->open_id && ret >= 0 && ttrace->filename.pending_open) {
  1321. trace__set_fd_pathname(thread, ret, ttrace->filename.name);
  1322. ttrace->filename.pending_open = false;
  1323. ++trace->stats.vfs_getname;
  1324. }
  1325. ttrace->exit_time = sample->time;
  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, sample->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 || print_dso) && al->map)
  1520. fprintf(f, "%s@", al->map->dso->long_name);
  1521. if ((verbose || 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 bool skip_sample(struct trace *trace, struct perf_sample *sample)
  1589. {
  1590. if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
  1591. (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
  1592. return false;
  1593. if (trace->pid_list || trace->tid_list)
  1594. return true;
  1595. return false;
  1596. }
  1597. static void trace__set_base_time(struct trace *trace,
  1598. struct perf_evsel *evsel,
  1599. struct perf_sample *sample)
  1600. {
  1601. /*
  1602. * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
  1603. * and don't use sample->time unconditionally, we may end up having
  1604. * some other event in the future without PERF_SAMPLE_TIME for good
  1605. * reason, i.e. we may not be interested in its timestamps, just in
  1606. * it taking place, picking some piece of information when it
  1607. * appears in our event stream (vfs_getname comes to mind).
  1608. */
  1609. if (trace->base_time == 0 && !trace->full_time &&
  1610. (evsel->attr.sample_type & PERF_SAMPLE_TIME))
  1611. trace->base_time = sample->time;
  1612. }
  1613. static int trace__process_sample(struct perf_tool *tool,
  1614. union perf_event *event,
  1615. struct perf_sample *sample,
  1616. struct perf_evsel *evsel,
  1617. struct machine *machine __maybe_unused)
  1618. {
  1619. struct trace *trace = container_of(tool, struct trace, tool);
  1620. int err = 0;
  1621. tracepoint_handler handler = evsel->handler;
  1622. if (skip_sample(trace, sample))
  1623. return 0;
  1624. trace__set_base_time(trace, evsel, sample);
  1625. if (handler) {
  1626. ++trace->nr_events;
  1627. handler(trace, evsel, event, sample);
  1628. }
  1629. return err;
  1630. }
  1631. static int parse_target_str(struct trace *trace)
  1632. {
  1633. if (trace->opts.target.pid) {
  1634. trace->pid_list = intlist__new(trace->opts.target.pid);
  1635. if (trace->pid_list == NULL) {
  1636. pr_err("Error parsing process id string\n");
  1637. return -EINVAL;
  1638. }
  1639. }
  1640. if (trace->opts.target.tid) {
  1641. trace->tid_list = intlist__new(trace->opts.target.tid);
  1642. if (trace->tid_list == NULL) {
  1643. pr_err("Error parsing thread id string\n");
  1644. return -EINVAL;
  1645. }
  1646. }
  1647. return 0;
  1648. }
  1649. static int trace__record(struct trace *trace, int argc, const char **argv)
  1650. {
  1651. unsigned int rec_argc, i, j;
  1652. const char **rec_argv;
  1653. const char * const record_args[] = {
  1654. "record",
  1655. "-R",
  1656. "-m", "1024",
  1657. "-c", "1",
  1658. };
  1659. const char * const sc_args[] = { "-e", };
  1660. unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
  1661. const char * const majpf_args[] = { "-e", "major-faults" };
  1662. unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
  1663. const char * const minpf_args[] = { "-e", "minor-faults" };
  1664. unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
  1665. /* +1 is for the event string below */
  1666. rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
  1667. majpf_args_nr + minpf_args_nr + argc;
  1668. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1669. if (rec_argv == NULL)
  1670. return -ENOMEM;
  1671. j = 0;
  1672. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1673. rec_argv[j++] = record_args[i];
  1674. if (trace->trace_syscalls) {
  1675. for (i = 0; i < sc_args_nr; i++)
  1676. rec_argv[j++] = sc_args[i];
  1677. /* event string may be different for older kernels - e.g., RHEL6 */
  1678. if (is_valid_tracepoint("raw_syscalls:sys_enter"))
  1679. rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
  1680. else if (is_valid_tracepoint("syscalls:sys_enter"))
  1681. rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
  1682. else {
  1683. pr_err("Neither raw_syscalls nor syscalls events exist.\n");
  1684. return -1;
  1685. }
  1686. }
  1687. if (trace->trace_pgfaults & TRACE_PFMAJ)
  1688. for (i = 0; i < majpf_args_nr; i++)
  1689. rec_argv[j++] = majpf_args[i];
  1690. if (trace->trace_pgfaults & TRACE_PFMIN)
  1691. for (i = 0; i < minpf_args_nr; i++)
  1692. rec_argv[j++] = minpf_args[i];
  1693. for (i = 0; i < (unsigned int)argc; i++)
  1694. rec_argv[j++] = argv[i];
  1695. return cmd_record(j, rec_argv, NULL);
  1696. }
  1697. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  1698. static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
  1699. {
  1700. struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
  1701. if (IS_ERR(evsel))
  1702. return false;
  1703. if (perf_evsel__field(evsel, "pathname") == NULL) {
  1704. perf_evsel__delete(evsel);
  1705. return false;
  1706. }
  1707. evsel->handler = trace__vfs_getname;
  1708. perf_evlist__add(evlist, evsel);
  1709. return true;
  1710. }
  1711. static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
  1712. {
  1713. struct perf_evsel *evsel;
  1714. struct perf_event_attr attr = {
  1715. .type = PERF_TYPE_SOFTWARE,
  1716. .mmap_data = 1,
  1717. };
  1718. attr.config = config;
  1719. attr.sample_period = 1;
  1720. event_attr_init(&attr);
  1721. evsel = perf_evsel__new(&attr);
  1722. if (evsel)
  1723. evsel->handler = trace__pgfault;
  1724. return evsel;
  1725. }
  1726. static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
  1727. {
  1728. const u32 type = event->header.type;
  1729. struct perf_evsel *evsel;
  1730. if (type != PERF_RECORD_SAMPLE) {
  1731. trace__process_event(trace, trace->host, event, sample);
  1732. return;
  1733. }
  1734. evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
  1735. if (evsel == NULL) {
  1736. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
  1737. return;
  1738. }
  1739. trace__set_base_time(trace, evsel, sample);
  1740. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  1741. sample->raw_data == NULL) {
  1742. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  1743. perf_evsel__name(evsel), sample->tid,
  1744. sample->cpu, sample->raw_size);
  1745. } else {
  1746. tracepoint_handler handler = evsel->handler;
  1747. handler(trace, evsel, event, sample);
  1748. }
  1749. }
  1750. static int trace__add_syscall_newtp(struct trace *trace)
  1751. {
  1752. int ret = -1;
  1753. struct perf_evlist *evlist = trace->evlist;
  1754. struct perf_evsel *sys_enter, *sys_exit;
  1755. sys_enter = perf_evsel__syscall_newtp("sys_enter", trace__sys_enter);
  1756. if (sys_enter == NULL)
  1757. goto out;
  1758. if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
  1759. goto out_delete_sys_enter;
  1760. sys_exit = perf_evsel__syscall_newtp("sys_exit", trace__sys_exit);
  1761. if (sys_exit == NULL)
  1762. goto out_delete_sys_enter;
  1763. if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
  1764. goto out_delete_sys_exit;
  1765. perf_evlist__add(evlist, sys_enter);
  1766. perf_evlist__add(evlist, sys_exit);
  1767. if (callchain_param.enabled && !trace->kernel_syscallchains) {
  1768. /*
  1769. * We're interested only in the user space callchain
  1770. * leading to the syscall, allow overriding that for
  1771. * debugging reasons using --kernel_syscall_callchains
  1772. */
  1773. sys_exit->attr.exclude_callchain_kernel = 1;
  1774. }
  1775. trace->syscalls.events.sys_enter = sys_enter;
  1776. trace->syscalls.events.sys_exit = sys_exit;
  1777. ret = 0;
  1778. out:
  1779. return ret;
  1780. out_delete_sys_exit:
  1781. perf_evsel__delete_priv(sys_exit);
  1782. out_delete_sys_enter:
  1783. perf_evsel__delete_priv(sys_enter);
  1784. goto out;
  1785. }
  1786. static int trace__set_ev_qualifier_filter(struct trace *trace)
  1787. {
  1788. int err = -1;
  1789. char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
  1790. trace->ev_qualifier_ids.nr,
  1791. trace->ev_qualifier_ids.entries);
  1792. if (filter == NULL)
  1793. goto out_enomem;
  1794. if (!perf_evsel__append_filter(trace->syscalls.events.sys_enter, "&&", filter))
  1795. err = perf_evsel__append_filter(trace->syscalls.events.sys_exit, "&&", filter);
  1796. free(filter);
  1797. out:
  1798. return err;
  1799. out_enomem:
  1800. errno = ENOMEM;
  1801. goto out;
  1802. }
  1803. static int trace__run(struct trace *trace, int argc, const char **argv)
  1804. {
  1805. struct perf_evlist *evlist = trace->evlist;
  1806. struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
  1807. int err = -1, i;
  1808. unsigned long before;
  1809. const bool forks = argc > 0;
  1810. bool draining = false;
  1811. trace->live = true;
  1812. if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
  1813. goto out_error_raw_syscalls;
  1814. if (trace->trace_syscalls)
  1815. trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
  1816. if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
  1817. pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
  1818. if (pgfault_maj == NULL)
  1819. goto out_error_mem;
  1820. perf_evlist__add(evlist, pgfault_maj);
  1821. }
  1822. if ((trace->trace_pgfaults & TRACE_PFMIN)) {
  1823. pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
  1824. if (pgfault_min == NULL)
  1825. goto out_error_mem;
  1826. perf_evlist__add(evlist, pgfault_min);
  1827. }
  1828. if (trace->sched &&
  1829. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  1830. trace__sched_stat_runtime))
  1831. goto out_error_sched_stat_runtime;
  1832. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  1833. if (err < 0) {
  1834. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  1835. goto out_delete_evlist;
  1836. }
  1837. err = trace__symbols_init(trace, evlist);
  1838. if (err < 0) {
  1839. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  1840. goto out_delete_evlist;
  1841. }
  1842. perf_evlist__config(evlist, &trace->opts, NULL);
  1843. if (callchain_param.enabled) {
  1844. bool use_identifier = false;
  1845. if (trace->syscalls.events.sys_exit) {
  1846. perf_evsel__config_callchain(trace->syscalls.events.sys_exit,
  1847. &trace->opts, &callchain_param);
  1848. use_identifier = true;
  1849. }
  1850. if (pgfault_maj) {
  1851. perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
  1852. use_identifier = true;
  1853. }
  1854. if (pgfault_min) {
  1855. perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
  1856. use_identifier = true;
  1857. }
  1858. if (use_identifier) {
  1859. /*
  1860. * Now we have evsels with different sample_ids, use
  1861. * PERF_SAMPLE_IDENTIFIER to map from sample to evsel
  1862. * from a fixed position in each ring buffer record.
  1863. *
  1864. * As of this the changeset introducing this comment, this
  1865. * isn't strictly needed, as the fields that can come before
  1866. * PERF_SAMPLE_ID are all used, but we'll probably disable
  1867. * some of those for things like copying the payload of
  1868. * pointer syscall arguments, and for vfs_getname we don't
  1869. * need PERF_SAMPLE_ADDR and PERF_SAMPLE_IP, so do this
  1870. * here as a warning we need to use PERF_SAMPLE_IDENTIFIER.
  1871. */
  1872. perf_evlist__set_sample_bit(evlist, IDENTIFIER);
  1873. perf_evlist__reset_sample_bit(evlist, ID);
  1874. }
  1875. }
  1876. signal(SIGCHLD, sig_handler);
  1877. signal(SIGINT, sig_handler);
  1878. if (forks) {
  1879. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  1880. argv, false, NULL);
  1881. if (err < 0) {
  1882. fprintf(trace->output, "Couldn't run the workload!\n");
  1883. goto out_delete_evlist;
  1884. }
  1885. }
  1886. err = perf_evlist__open(evlist);
  1887. if (err < 0)
  1888. goto out_error_open;
  1889. err = bpf__apply_obj_config();
  1890. if (err) {
  1891. char errbuf[BUFSIZ];
  1892. bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
  1893. pr_err("ERROR: Apply config to BPF failed: %s\n",
  1894. errbuf);
  1895. goto out_error_open;
  1896. }
  1897. /*
  1898. * Better not use !target__has_task() here because we need to cover the
  1899. * case where no threads were specified in the command line, but a
  1900. * workload was, and in that case we will fill in the thread_map when
  1901. * we fork the workload in perf_evlist__prepare_workload.
  1902. */
  1903. if (trace->filter_pids.nr > 0)
  1904. err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
  1905. else if (thread_map__pid(evlist->threads, 0) == -1)
  1906. err = perf_evlist__set_filter_pid(evlist, getpid());
  1907. if (err < 0)
  1908. goto out_error_mem;
  1909. if (trace->ev_qualifier_ids.nr > 0) {
  1910. err = trace__set_ev_qualifier_filter(trace);
  1911. if (err < 0)
  1912. goto out_errno;
  1913. pr_debug("event qualifier tracepoint filter: %s\n",
  1914. trace->syscalls.events.sys_exit->filter);
  1915. }
  1916. err = perf_evlist__apply_filters(evlist, &evsel);
  1917. if (err < 0)
  1918. goto out_error_apply_filters;
  1919. err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false);
  1920. if (err < 0)
  1921. goto out_error_mmap;
  1922. if (!target__none(&trace->opts.target))
  1923. perf_evlist__enable(evlist);
  1924. if (forks)
  1925. perf_evlist__start_workload(evlist);
  1926. trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
  1927. evlist->threads->nr > 1 ||
  1928. perf_evlist__first(evlist)->attr.inherit;
  1929. again:
  1930. before = trace->nr_events;
  1931. for (i = 0; i < evlist->nr_mmaps; i++) {
  1932. union perf_event *event;
  1933. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  1934. struct perf_sample sample;
  1935. ++trace->nr_events;
  1936. err = perf_evlist__parse_sample(evlist, event, &sample);
  1937. if (err) {
  1938. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  1939. goto next_event;
  1940. }
  1941. trace__handle_event(trace, event, &sample);
  1942. next_event:
  1943. perf_evlist__mmap_consume(evlist, i);
  1944. if (interrupted)
  1945. goto out_disable;
  1946. if (done && !draining) {
  1947. perf_evlist__disable(evlist);
  1948. draining = true;
  1949. }
  1950. }
  1951. }
  1952. if (trace->nr_events == before) {
  1953. int timeout = done ? 100 : -1;
  1954. if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
  1955. if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
  1956. draining = true;
  1957. goto again;
  1958. }
  1959. } else {
  1960. goto again;
  1961. }
  1962. out_disable:
  1963. thread__zput(trace->current);
  1964. perf_evlist__disable(evlist);
  1965. if (!err) {
  1966. if (trace->summary)
  1967. trace__fprintf_thread_summary(trace, trace->output);
  1968. if (trace->show_tool_stats) {
  1969. fprintf(trace->output, "Stats:\n "
  1970. " vfs_getname : %" PRIu64 "\n"
  1971. " proc_getname: %" PRIu64 "\n",
  1972. trace->stats.vfs_getname,
  1973. trace->stats.proc_getname);
  1974. }
  1975. }
  1976. out_delete_evlist:
  1977. perf_evlist__delete(evlist);
  1978. trace->evlist = NULL;
  1979. trace->live = false;
  1980. return err;
  1981. {
  1982. char errbuf[BUFSIZ];
  1983. out_error_sched_stat_runtime:
  1984. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
  1985. goto out_error;
  1986. out_error_raw_syscalls:
  1987. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
  1988. goto out_error;
  1989. out_error_mmap:
  1990. perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
  1991. goto out_error;
  1992. out_error_open:
  1993. perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  1994. out_error:
  1995. fprintf(trace->output, "%s\n", errbuf);
  1996. goto out_delete_evlist;
  1997. out_error_apply_filters:
  1998. fprintf(trace->output,
  1999. "Failed to set filter \"%s\" on event %s with %d (%s)\n",
  2000. evsel->filter, perf_evsel__name(evsel), errno,
  2001. str_error_r(errno, errbuf, sizeof(errbuf)));
  2002. goto out_delete_evlist;
  2003. }
  2004. out_error_mem:
  2005. fprintf(trace->output, "Not enough memory to run!\n");
  2006. goto out_delete_evlist;
  2007. out_errno:
  2008. fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
  2009. goto out_delete_evlist;
  2010. }
  2011. static int trace__replay(struct trace *trace)
  2012. {
  2013. const struct perf_evsel_str_handler handlers[] = {
  2014. { "probe:vfs_getname", trace__vfs_getname, },
  2015. };
  2016. struct perf_data_file file = {
  2017. .path = input_name,
  2018. .mode = PERF_DATA_MODE_READ,
  2019. .force = trace->force,
  2020. };
  2021. struct perf_session *session;
  2022. struct perf_evsel *evsel;
  2023. int err = -1;
  2024. trace->tool.sample = trace__process_sample;
  2025. trace->tool.mmap = perf_event__process_mmap;
  2026. trace->tool.mmap2 = perf_event__process_mmap2;
  2027. trace->tool.comm = perf_event__process_comm;
  2028. trace->tool.exit = perf_event__process_exit;
  2029. trace->tool.fork = perf_event__process_fork;
  2030. trace->tool.attr = perf_event__process_attr;
  2031. trace->tool.tracing_data = perf_event__process_tracing_data;
  2032. trace->tool.build_id = perf_event__process_build_id;
  2033. trace->tool.ordered_events = true;
  2034. trace->tool.ordering_requires_timestamps = true;
  2035. /* add tid to output */
  2036. trace->multiple_threads = true;
  2037. session = perf_session__new(&file, false, &trace->tool);
  2038. if (session == NULL)
  2039. return -1;
  2040. if (symbol__init(&session->header.env) < 0)
  2041. goto out;
  2042. trace->host = &session->machines.host;
  2043. err = perf_session__set_tracepoints_handlers(session, handlers);
  2044. if (err)
  2045. goto out;
  2046. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2047. "raw_syscalls:sys_enter");
  2048. /* older kernels have syscalls tp versus raw_syscalls */
  2049. if (evsel == NULL)
  2050. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2051. "syscalls:sys_enter");
  2052. if (evsel &&
  2053. (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 ||
  2054. perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
  2055. pr_err("Error during initialize raw_syscalls:sys_enter event\n");
  2056. goto out;
  2057. }
  2058. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2059. "raw_syscalls:sys_exit");
  2060. if (evsel == NULL)
  2061. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2062. "syscalls:sys_exit");
  2063. if (evsel &&
  2064. (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 ||
  2065. perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
  2066. pr_err("Error during initialize raw_syscalls:sys_exit event\n");
  2067. goto out;
  2068. }
  2069. evlist__for_each_entry(session->evlist, evsel) {
  2070. if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
  2071. (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
  2072. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  2073. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
  2074. evsel->handler = trace__pgfault;
  2075. }
  2076. err = parse_target_str(trace);
  2077. if (err != 0)
  2078. goto out;
  2079. setup_pager();
  2080. err = perf_session__process_events(session);
  2081. if (err)
  2082. pr_err("Failed to process events, error %d", err);
  2083. else if (trace->summary)
  2084. trace__fprintf_thread_summary(trace, trace->output);
  2085. out:
  2086. perf_session__delete(session);
  2087. return err;
  2088. }
  2089. static size_t trace__fprintf_threads_header(FILE *fp)
  2090. {
  2091. size_t printed;
  2092. printed = fprintf(fp, "\n Summary of events:\n\n");
  2093. return printed;
  2094. }
  2095. DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
  2096. struct stats *stats;
  2097. double msecs;
  2098. int syscall;
  2099. )
  2100. {
  2101. struct int_node *source = rb_entry(nd, struct int_node, rb_node);
  2102. struct stats *stats = source->priv;
  2103. entry->syscall = source->i;
  2104. entry->stats = stats;
  2105. entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
  2106. }
  2107. static size_t thread__dump_stats(struct thread_trace *ttrace,
  2108. struct trace *trace, FILE *fp)
  2109. {
  2110. size_t printed = 0;
  2111. struct syscall *sc;
  2112. struct rb_node *nd;
  2113. DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
  2114. if (syscall_stats == NULL)
  2115. return 0;
  2116. printed += fprintf(fp, "\n");
  2117. printed += fprintf(fp, " syscall calls total min avg max stddev\n");
  2118. printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
  2119. printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
  2120. resort_rb__for_each_entry(nd, syscall_stats) {
  2121. struct stats *stats = syscall_stats_entry->stats;
  2122. if (stats) {
  2123. double min = (double)(stats->min) / NSEC_PER_MSEC;
  2124. double max = (double)(stats->max) / NSEC_PER_MSEC;
  2125. double avg = avg_stats(stats);
  2126. double pct;
  2127. u64 n = (u64) stats->n;
  2128. pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
  2129. avg /= NSEC_PER_MSEC;
  2130. sc = &trace->syscalls.table[syscall_stats_entry->syscall];
  2131. printed += fprintf(fp, " %-15s", sc->name);
  2132. printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
  2133. n, syscall_stats_entry->msecs, min, avg);
  2134. printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
  2135. }
  2136. }
  2137. resort_rb__delete(syscall_stats);
  2138. printed += fprintf(fp, "\n\n");
  2139. return printed;
  2140. }
  2141. static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
  2142. {
  2143. size_t printed = 0;
  2144. struct thread_trace *ttrace = thread__priv(thread);
  2145. double ratio;
  2146. if (ttrace == NULL)
  2147. return 0;
  2148. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  2149. printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
  2150. printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
  2151. printed += fprintf(fp, "%.1f%%", ratio);
  2152. if (ttrace->pfmaj)
  2153. printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
  2154. if (ttrace->pfmin)
  2155. printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
  2156. if (trace->sched)
  2157. printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
  2158. else if (fputc('\n', fp) != EOF)
  2159. ++printed;
  2160. printed += thread__dump_stats(ttrace, trace, fp);
  2161. return printed;
  2162. }
  2163. static unsigned long thread__nr_events(struct thread_trace *ttrace)
  2164. {
  2165. return ttrace ? ttrace->nr_events : 0;
  2166. }
  2167. DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
  2168. struct thread *thread;
  2169. )
  2170. {
  2171. entry->thread = rb_entry(nd, struct thread, rb_node);
  2172. }
  2173. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  2174. {
  2175. DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host);
  2176. size_t printed = trace__fprintf_threads_header(fp);
  2177. struct rb_node *nd;
  2178. if (threads == NULL) {
  2179. fprintf(fp, "%s", "Error sorting output by nr_events!\n");
  2180. return 0;
  2181. }
  2182. resort_rb__for_each_entry(nd, threads)
  2183. printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
  2184. resort_rb__delete(threads);
  2185. return printed;
  2186. }
  2187. static int trace__set_duration(const struct option *opt, const char *str,
  2188. int unset __maybe_unused)
  2189. {
  2190. struct trace *trace = opt->value;
  2191. trace->duration_filter = atof(str);
  2192. return 0;
  2193. }
  2194. static int trace__set_filter_pids(const struct option *opt, const char *str,
  2195. int unset __maybe_unused)
  2196. {
  2197. int ret = -1;
  2198. size_t i;
  2199. struct trace *trace = opt->value;
  2200. /*
  2201. * FIXME: introduce a intarray class, plain parse csv and create a
  2202. * { int nr, int entries[] } struct...
  2203. */
  2204. struct intlist *list = intlist__new(str);
  2205. if (list == NULL)
  2206. return -1;
  2207. i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
  2208. trace->filter_pids.entries = calloc(i, sizeof(pid_t));
  2209. if (trace->filter_pids.entries == NULL)
  2210. goto out;
  2211. trace->filter_pids.entries[0] = getpid();
  2212. for (i = 1; i < trace->filter_pids.nr; ++i)
  2213. trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
  2214. intlist__delete(list);
  2215. ret = 0;
  2216. out:
  2217. return ret;
  2218. }
  2219. static int trace__open_output(struct trace *trace, const char *filename)
  2220. {
  2221. struct stat st;
  2222. if (!stat(filename, &st) && st.st_size) {
  2223. char oldname[PATH_MAX];
  2224. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  2225. unlink(oldname);
  2226. rename(filename, oldname);
  2227. }
  2228. trace->output = fopen(filename, "w");
  2229. return trace->output == NULL ? -errno : 0;
  2230. }
  2231. static int parse_pagefaults(const struct option *opt, const char *str,
  2232. int unset __maybe_unused)
  2233. {
  2234. int *trace_pgfaults = opt->value;
  2235. if (strcmp(str, "all") == 0)
  2236. *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
  2237. else if (strcmp(str, "maj") == 0)
  2238. *trace_pgfaults |= TRACE_PFMAJ;
  2239. else if (strcmp(str, "min") == 0)
  2240. *trace_pgfaults |= TRACE_PFMIN;
  2241. else
  2242. return -1;
  2243. return 0;
  2244. }
  2245. static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
  2246. {
  2247. struct perf_evsel *evsel;
  2248. evlist__for_each_entry(evlist, evsel)
  2249. evsel->handler = handler;
  2250. }
  2251. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  2252. {
  2253. const char *trace_usage[] = {
  2254. "perf trace [<options>] [<command>]",
  2255. "perf trace [<options>] -- <command> [<options>]",
  2256. "perf trace record [<options>] [<command>]",
  2257. "perf trace record [<options>] -- <command> [<options>]",
  2258. NULL
  2259. };
  2260. struct trace trace = {
  2261. .syscalls = {
  2262. . max = -1,
  2263. },
  2264. .opts = {
  2265. .target = {
  2266. .uid = UINT_MAX,
  2267. .uses_mmap = true,
  2268. },
  2269. .user_freq = UINT_MAX,
  2270. .user_interval = ULLONG_MAX,
  2271. .no_buffering = true,
  2272. .mmap_pages = UINT_MAX,
  2273. .proc_map_timeout = 500,
  2274. },
  2275. .output = stderr,
  2276. .show_comm = true,
  2277. .trace_syscalls = true,
  2278. .kernel_syscallchains = false,
  2279. .max_stack = UINT_MAX,
  2280. };
  2281. const char *output_name = NULL;
  2282. const char *ev_qualifier_str = NULL;
  2283. const struct option trace_options[] = {
  2284. OPT_CALLBACK(0, "event", &trace.evlist, "event",
  2285. "event selector. use 'perf list' to list available events",
  2286. parse_events_option),
  2287. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  2288. "show the thread COMM next to its id"),
  2289. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  2290. OPT_STRING('e', "expr", &ev_qualifier_str, "expr", "list of syscalls to trace"),
  2291. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  2292. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  2293. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  2294. "trace events on existing process id"),
  2295. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  2296. "trace events on existing thread id"),
  2297. OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
  2298. "pids to filter (by the kernel)", trace__set_filter_pids),
  2299. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  2300. "system-wide collection from all CPUs"),
  2301. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  2302. "list of cpus to monitor"),
  2303. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  2304. "child tasks do not inherit counters"),
  2305. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  2306. "number of mmap data pages",
  2307. perf_evlist__parse_mmap_pages),
  2308. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  2309. "user to profile"),
  2310. OPT_CALLBACK(0, "duration", &trace, "float",
  2311. "show only events with duration > N.M ms",
  2312. trace__set_duration),
  2313. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  2314. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  2315. OPT_BOOLEAN('T', "time", &trace.full_time,
  2316. "Show full timestamp, not time relative to first start"),
  2317. OPT_BOOLEAN('s', "summary", &trace.summary_only,
  2318. "Show only syscall summary with statistics"),
  2319. OPT_BOOLEAN('S', "with-summary", &trace.summary,
  2320. "Show all syscalls and summary with statistics"),
  2321. OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
  2322. "Trace pagefaults", parse_pagefaults, "maj"),
  2323. OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
  2324. OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
  2325. OPT_CALLBACK(0, "call-graph", &trace.opts,
  2326. "record_mode[,record_size]", record_callchain_help,
  2327. &record_parse_callchain_opt),
  2328. OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
  2329. "Show the kernel callchains on the syscall exit path"),
  2330. OPT_UINTEGER(0, "min-stack", &trace.min_stack,
  2331. "Set the minimum stack depth when parsing the callchain, "
  2332. "anything below the specified depth will be ignored."),
  2333. OPT_UINTEGER(0, "max-stack", &trace.max_stack,
  2334. "Set the maximum stack depth when parsing the callchain, "
  2335. "anything beyond the specified depth will be ignored. "
  2336. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  2337. OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
  2338. "per thread proc mmap processing timeout in ms"),
  2339. OPT_END()
  2340. };
  2341. bool __maybe_unused max_stack_user_set = true;
  2342. bool mmap_pages_user_set = true;
  2343. const char * const trace_subcommands[] = { "record", NULL };
  2344. int err;
  2345. char bf[BUFSIZ];
  2346. signal(SIGSEGV, sighandler_dump_stack);
  2347. signal(SIGFPE, sighandler_dump_stack);
  2348. trace.evlist = perf_evlist__new();
  2349. trace.sctbl = syscalltbl__new();
  2350. if (trace.evlist == NULL || trace.sctbl == NULL) {
  2351. pr_err("Not enough memory to run!\n");
  2352. err = -ENOMEM;
  2353. goto out;
  2354. }
  2355. argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
  2356. trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  2357. err = bpf__setup_stdout(trace.evlist);
  2358. if (err) {
  2359. bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
  2360. pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
  2361. goto out;
  2362. }
  2363. err = -1;
  2364. if (trace.trace_pgfaults) {
  2365. trace.opts.sample_address = true;
  2366. trace.opts.sample_time = true;
  2367. }
  2368. if (trace.opts.mmap_pages == UINT_MAX)
  2369. mmap_pages_user_set = false;
  2370. if (trace.max_stack == UINT_MAX) {
  2371. trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl_perf_event_max_stack;
  2372. max_stack_user_set = false;
  2373. }
  2374. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  2375. if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled && trace.trace_syscalls)
  2376. record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
  2377. #endif
  2378. if (callchain_param.enabled) {
  2379. if (!mmap_pages_user_set && geteuid() == 0)
  2380. trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
  2381. symbol_conf.use_callchain = true;
  2382. }
  2383. if (trace.evlist->nr_entries > 0)
  2384. evlist__set_evsel_handler(trace.evlist, trace__event_handler);
  2385. if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
  2386. return trace__record(&trace, argc-1, &argv[1]);
  2387. /* summary_only implies summary option, but don't overwrite summary if set */
  2388. if (trace.summary_only)
  2389. trace.summary = trace.summary_only;
  2390. if (!trace.trace_syscalls && !trace.trace_pgfaults &&
  2391. trace.evlist->nr_entries == 0 /* Was --events used? */) {
  2392. pr_err("Please specify something to trace.\n");
  2393. return -1;
  2394. }
  2395. if (!trace.trace_syscalls && ev_qualifier_str) {
  2396. pr_err("The -e option can't be used with --no-syscalls.\n");
  2397. goto out;
  2398. }
  2399. if (output_name != NULL) {
  2400. err = trace__open_output(&trace, output_name);
  2401. if (err < 0) {
  2402. perror("failed to create output file");
  2403. goto out;
  2404. }
  2405. }
  2406. trace.open_id = syscalltbl__id(trace.sctbl, "open");
  2407. if (ev_qualifier_str != NULL) {
  2408. const char *s = ev_qualifier_str;
  2409. struct strlist_config slist_config = {
  2410. .dirname = system_path(STRACE_GROUPS_DIR),
  2411. };
  2412. trace.not_ev_qualifier = *s == '!';
  2413. if (trace.not_ev_qualifier)
  2414. ++s;
  2415. trace.ev_qualifier = strlist__new(s, &slist_config);
  2416. if (trace.ev_qualifier == NULL) {
  2417. fputs("Not enough memory to parse event qualifier",
  2418. trace.output);
  2419. err = -ENOMEM;
  2420. goto out_close;
  2421. }
  2422. err = trace__validate_ev_qualifier(&trace);
  2423. if (err)
  2424. goto out_close;
  2425. }
  2426. err = target__validate(&trace.opts.target);
  2427. if (err) {
  2428. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2429. fprintf(trace.output, "%s", bf);
  2430. goto out_close;
  2431. }
  2432. err = target__parse_uid(&trace.opts.target);
  2433. if (err) {
  2434. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2435. fprintf(trace.output, "%s", bf);
  2436. goto out_close;
  2437. }
  2438. if (!argc && target__none(&trace.opts.target))
  2439. trace.opts.target.system_wide = true;
  2440. if (input_name)
  2441. err = trace__replay(&trace);
  2442. else
  2443. err = trace__run(&trace, argc, argv);
  2444. out_close:
  2445. if (output_name != NULL)
  2446. fclose(trace.output);
  2447. out:
  2448. return err;
  2449. }