builtin-trace.c 86 KB

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