builtin-trace.c 83 KB

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