builtin-trace.c 90 KB

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