builtin-trace.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237
  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 || sa->entries[idx] == NULL)
  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. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  667. { .name = "socketpair",
  668. .arg = { [0] = STRARRAY(family, socket_families),
  669. [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
  670. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  671. { .name = "stat", .alias = "newstat", },
  672. { .name = "statx",
  673. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
  674. [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
  675. [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
  676. { .name = "swapoff",
  677. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
  678. { .name = "swapon",
  679. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
  680. { .name = "symlinkat",
  681. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  682. { .name = "tgkill",
  683. .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  684. { .name = "tkill",
  685. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  686. { .name = "uname", .alias = "newuname", },
  687. { .name = "unlinkat",
  688. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  689. { .name = "utimensat",
  690. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
  691. { .name = "wait4", .errpid = true,
  692. .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  693. { .name = "waitid", .errpid = true,
  694. .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  695. };
  696. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  697. {
  698. const struct syscall_fmt *fmt = fmtp;
  699. return strcmp(name, fmt->name);
  700. }
  701. static struct syscall_fmt *syscall_fmt__find(const char *name)
  702. {
  703. const int nmemb = ARRAY_SIZE(syscall_fmts);
  704. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  705. }
  706. struct syscall {
  707. struct event_format *tp_format;
  708. int nr_args;
  709. struct format_field *args;
  710. const char *name;
  711. bool is_exit;
  712. struct syscall_fmt *fmt;
  713. struct syscall_arg_fmt *arg_fmt;
  714. };
  715. /*
  716. * We need to have this 'calculated' boolean because in some cases we really
  717. * don't know what is the duration of a syscall, for instance, when we start
  718. * a session and some threads are waiting for a syscall to finish, say 'poll',
  719. * in which case all we can do is to print "( ? ) for duration and for the
  720. * start timestamp.
  721. */
  722. static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
  723. {
  724. double duration = (double)t / NSEC_PER_MSEC;
  725. size_t printed = fprintf(fp, "(");
  726. if (!calculated)
  727. printed += fprintf(fp, " ");
  728. else if (duration >= 1.0)
  729. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  730. else if (duration >= 0.01)
  731. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  732. else
  733. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  734. return printed + fprintf(fp, "): ");
  735. }
  736. /**
  737. * filename.ptr: The filename char pointer that will be vfs_getname'd
  738. * filename.entry_str_pos: Where to insert the string translated from
  739. * filename.ptr by the vfs_getname tracepoint/kprobe.
  740. * ret_scnprintf: syscall args may set this to a different syscall return
  741. * formatter, for instance, fcntl may return fds, file flags, etc.
  742. */
  743. struct thread_trace {
  744. u64 entry_time;
  745. bool entry_pending;
  746. unsigned long nr_events;
  747. unsigned long pfmaj, pfmin;
  748. char *entry_str;
  749. double runtime_ms;
  750. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  751. struct {
  752. unsigned long ptr;
  753. short int entry_str_pos;
  754. bool pending_open;
  755. unsigned int namelen;
  756. char *name;
  757. } filename;
  758. struct {
  759. int max;
  760. char **table;
  761. } paths;
  762. struct intlist *syscall_stats;
  763. };
  764. static struct thread_trace *thread_trace__new(void)
  765. {
  766. struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
  767. if (ttrace)
  768. ttrace->paths.max = -1;
  769. ttrace->syscall_stats = intlist__new(NULL);
  770. return ttrace;
  771. }
  772. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  773. {
  774. struct thread_trace *ttrace;
  775. if (thread == NULL)
  776. goto fail;
  777. if (thread__priv(thread) == NULL)
  778. thread__set_priv(thread, thread_trace__new());
  779. if (thread__priv(thread) == NULL)
  780. goto fail;
  781. ttrace = thread__priv(thread);
  782. ++ttrace->nr_events;
  783. return ttrace;
  784. fail:
  785. color_fprintf(fp, PERF_COLOR_RED,
  786. "WARNING: not enough memory, dropping samples!\n");
  787. return NULL;
  788. }
  789. void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
  790. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
  791. {
  792. struct thread_trace *ttrace = thread__priv(arg->thread);
  793. ttrace->ret_scnprintf = ret_scnprintf;
  794. }
  795. #define TRACE_PFMAJ (1 << 0)
  796. #define TRACE_PFMIN (1 << 1)
  797. static const size_t trace__entry_str_size = 2048;
  798. static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
  799. {
  800. struct thread_trace *ttrace = thread__priv(thread);
  801. if (fd > ttrace->paths.max) {
  802. char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
  803. if (npath == NULL)
  804. return -1;
  805. if (ttrace->paths.max != -1) {
  806. memset(npath + ttrace->paths.max + 1, 0,
  807. (fd - ttrace->paths.max) * sizeof(char *));
  808. } else {
  809. memset(npath, 0, (fd + 1) * sizeof(char *));
  810. }
  811. ttrace->paths.table = npath;
  812. ttrace->paths.max = fd;
  813. }
  814. ttrace->paths.table[fd] = strdup(pathname);
  815. return ttrace->paths.table[fd] != NULL ? 0 : -1;
  816. }
  817. static int thread__read_fd_path(struct thread *thread, int fd)
  818. {
  819. char linkname[PATH_MAX], pathname[PATH_MAX];
  820. struct stat st;
  821. int ret;
  822. if (thread->pid_ == thread->tid) {
  823. scnprintf(linkname, sizeof(linkname),
  824. "/proc/%d/fd/%d", thread->pid_, fd);
  825. } else {
  826. scnprintf(linkname, sizeof(linkname),
  827. "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
  828. }
  829. if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
  830. return -1;
  831. ret = readlink(linkname, pathname, sizeof(pathname));
  832. if (ret < 0 || ret > st.st_size)
  833. return -1;
  834. pathname[ret] = '\0';
  835. return trace__set_fd_pathname(thread, fd, pathname);
  836. }
  837. static const char *thread__fd_path(struct thread *thread, int fd,
  838. struct trace *trace)
  839. {
  840. struct thread_trace *ttrace = thread__priv(thread);
  841. if (ttrace == NULL)
  842. return NULL;
  843. if (fd < 0)
  844. return NULL;
  845. if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
  846. if (!trace->live)
  847. return NULL;
  848. ++trace->stats.proc_getname;
  849. if (thread__read_fd_path(thread, fd))
  850. return NULL;
  851. }
  852. return ttrace->paths.table[fd];
  853. }
  854. size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
  855. {
  856. int fd = arg->val;
  857. size_t printed = scnprintf(bf, size, "%d", fd);
  858. const char *path = thread__fd_path(arg->thread, fd, arg->trace);
  859. if (path)
  860. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  861. return printed;
  862. }
  863. size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
  864. {
  865. size_t printed = scnprintf(bf, size, "%d", fd);
  866. struct thread *thread = machine__find_thread(trace->host, pid, pid);
  867. if (thread) {
  868. const char *path = thread__fd_path(thread, fd, trace);
  869. if (path)
  870. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  871. thread__put(thread);
  872. }
  873. return printed;
  874. }
  875. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  876. struct syscall_arg *arg)
  877. {
  878. int fd = arg->val;
  879. size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
  880. struct thread_trace *ttrace = thread__priv(arg->thread);
  881. if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
  882. zfree(&ttrace->paths.table[fd]);
  883. return printed;
  884. }
  885. static void thread__set_filename_pos(struct thread *thread, const char *bf,
  886. unsigned long ptr)
  887. {
  888. struct thread_trace *ttrace = thread__priv(thread);
  889. ttrace->filename.ptr = ptr;
  890. ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
  891. }
  892. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  893. struct syscall_arg *arg)
  894. {
  895. unsigned long ptr = arg->val;
  896. if (!arg->trace->vfs_getname)
  897. return scnprintf(bf, size, "%#x", ptr);
  898. thread__set_filename_pos(arg->thread, bf, ptr);
  899. return 0;
  900. }
  901. static bool trace__filter_duration(struct trace *trace, double t)
  902. {
  903. return t < (trace->duration_filter * NSEC_PER_MSEC);
  904. }
  905. static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  906. {
  907. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  908. return fprintf(fp, "%10.3f ", ts);
  909. }
  910. /*
  911. * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
  912. * using ttrace->entry_time for a thread that receives a sys_exit without
  913. * first having received a sys_enter ("poll" issued before tracing session
  914. * starts, lost sys_enter exit due to ring buffer overflow).
  915. */
  916. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  917. {
  918. if (tstamp > 0)
  919. return __trace__fprintf_tstamp(trace, tstamp, fp);
  920. return fprintf(fp, " ? ");
  921. }
  922. static bool done = false;
  923. static bool interrupted = false;
  924. static void sig_handler(int sig)
  925. {
  926. done = true;
  927. interrupted = sig == SIGINT;
  928. }
  929. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  930. u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
  931. {
  932. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  933. printed += fprintf_duration(duration, duration_calculated, fp);
  934. if (trace->multiple_threads) {
  935. if (trace->show_comm)
  936. printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
  937. printed += fprintf(fp, "%d ", thread->tid);
  938. }
  939. return printed;
  940. }
  941. static int trace__process_event(struct trace *trace, struct machine *machine,
  942. union perf_event *event, struct perf_sample *sample)
  943. {
  944. int ret = 0;
  945. switch (event->header.type) {
  946. case PERF_RECORD_LOST:
  947. color_fprintf(trace->output, PERF_COLOR_RED,
  948. "LOST %" PRIu64 " events!\n", event->lost.lost);
  949. ret = machine__process_lost_event(machine, event, sample);
  950. break;
  951. default:
  952. ret = machine__process_event(machine, event, sample);
  953. break;
  954. }
  955. return ret;
  956. }
  957. static int trace__tool_process(struct perf_tool *tool,
  958. union perf_event *event,
  959. struct perf_sample *sample,
  960. struct machine *machine)
  961. {
  962. struct trace *trace = container_of(tool, struct trace, tool);
  963. return trace__process_event(trace, machine, event, sample);
  964. }
  965. static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  966. {
  967. struct machine *machine = vmachine;
  968. if (machine->kptr_restrict_warned)
  969. return NULL;
  970. if (symbol_conf.kptr_restrict) {
  971. pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  972. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  973. "Kernel samples will not be resolved.\n");
  974. machine->kptr_restrict_warned = true;
  975. return NULL;
  976. }
  977. return machine__resolve_kernel_addr(vmachine, addrp, modp);
  978. }
  979. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  980. {
  981. int err = symbol__init(NULL);
  982. if (err)
  983. return err;
  984. trace->host = machine__new_host();
  985. if (trace->host == NULL)
  986. return -ENOMEM;
  987. err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
  988. if (err < 0)
  989. goto out;
  990. err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  991. evlist->threads, trace__tool_process, false,
  992. trace->opts.proc_map_timeout, 1);
  993. out:
  994. if (err)
  995. symbol__exit();
  996. return err;
  997. }
  998. static void trace__symbols__exit(struct trace *trace)
  999. {
  1000. machine__exit(trace->host);
  1001. trace->host = NULL;
  1002. symbol__exit();
  1003. }
  1004. static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
  1005. {
  1006. int idx;
  1007. if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
  1008. nr_args = sc->fmt->nr_args;
  1009. sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
  1010. if (sc->arg_fmt == NULL)
  1011. return -1;
  1012. for (idx = 0; idx < nr_args; ++idx) {
  1013. if (sc->fmt)
  1014. sc->arg_fmt[idx] = sc->fmt->arg[idx];
  1015. }
  1016. sc->nr_args = nr_args;
  1017. return 0;
  1018. }
  1019. static int syscall__set_arg_fmts(struct syscall *sc)
  1020. {
  1021. struct format_field *field;
  1022. int idx = 0, len;
  1023. for (field = sc->args; field; field = field->next, ++idx) {
  1024. if (sc->fmt && sc->fmt->arg[idx].scnprintf)
  1025. continue;
  1026. if (strcmp(field->type, "const char *") == 0 &&
  1027. (strcmp(field->name, "filename") == 0 ||
  1028. strcmp(field->name, "path") == 0 ||
  1029. strcmp(field->name, "pathname") == 0))
  1030. sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
  1031. else if (field->flags & FIELD_IS_POINTER)
  1032. sc->arg_fmt[idx].scnprintf = syscall_arg__scnprintf_hex;
  1033. else if (strcmp(field->type, "pid_t") == 0)
  1034. sc->arg_fmt[idx].scnprintf = SCA_PID;
  1035. else if (strcmp(field->type, "umode_t") == 0)
  1036. sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
  1037. else if ((strcmp(field->type, "int") == 0 ||
  1038. strcmp(field->type, "unsigned int") == 0 ||
  1039. strcmp(field->type, "long") == 0) &&
  1040. (len = strlen(field->name)) >= 2 &&
  1041. strcmp(field->name + len - 2, "fd") == 0) {
  1042. /*
  1043. * /sys/kernel/tracing/events/syscalls/sys_enter*
  1044. * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
  1045. * 65 int
  1046. * 23 unsigned int
  1047. * 7 unsigned long
  1048. */
  1049. sc->arg_fmt[idx].scnprintf = SCA_FD;
  1050. }
  1051. }
  1052. return 0;
  1053. }
  1054. static int trace__read_syscall_info(struct trace *trace, int id)
  1055. {
  1056. char tp_name[128];
  1057. struct syscall *sc;
  1058. const char *name = syscalltbl__name(trace->sctbl, id);
  1059. if (name == NULL)
  1060. return -1;
  1061. if (id > trace->syscalls.max) {
  1062. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  1063. if (nsyscalls == NULL)
  1064. return -1;
  1065. if (trace->syscalls.max != -1) {
  1066. memset(nsyscalls + trace->syscalls.max + 1, 0,
  1067. (id - trace->syscalls.max) * sizeof(*sc));
  1068. } else {
  1069. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  1070. }
  1071. trace->syscalls.table = nsyscalls;
  1072. trace->syscalls.max = id;
  1073. }
  1074. sc = trace->syscalls.table + id;
  1075. sc->name = name;
  1076. sc->fmt = syscall_fmt__find(sc->name);
  1077. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1078. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1079. if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
  1080. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1081. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1082. }
  1083. if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
  1084. return -1;
  1085. if (IS_ERR(sc->tp_format))
  1086. return -1;
  1087. sc->args = sc->tp_format->format.fields;
  1088. /*
  1089. * We need to check and discard the first variable '__syscall_nr'
  1090. * or 'nr' that mean the syscall number. It is needless here.
  1091. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
  1092. */
  1093. if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
  1094. sc->args = sc->args->next;
  1095. --sc->nr_args;
  1096. }
  1097. sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
  1098. return syscall__set_arg_fmts(sc);
  1099. }
  1100. static int trace__validate_ev_qualifier(struct trace *trace)
  1101. {
  1102. int err = 0, i;
  1103. size_t nr_allocated;
  1104. struct str_node *pos;
  1105. trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
  1106. trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
  1107. sizeof(trace->ev_qualifier_ids.entries[0]));
  1108. if (trace->ev_qualifier_ids.entries == NULL) {
  1109. fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
  1110. trace->output);
  1111. err = -EINVAL;
  1112. goto out;
  1113. }
  1114. nr_allocated = trace->ev_qualifier_ids.nr;
  1115. i = 0;
  1116. strlist__for_each_entry(pos, trace->ev_qualifier) {
  1117. const char *sc = pos->s;
  1118. int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
  1119. if (id < 0) {
  1120. id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
  1121. if (id >= 0)
  1122. goto matches;
  1123. if (err == 0) {
  1124. fputs("Error:\tInvalid syscall ", trace->output);
  1125. err = -EINVAL;
  1126. } else {
  1127. fputs(", ", trace->output);
  1128. }
  1129. fputs(sc, trace->output);
  1130. }
  1131. matches:
  1132. trace->ev_qualifier_ids.entries[i++] = id;
  1133. if (match_next == -1)
  1134. continue;
  1135. while (1) {
  1136. id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
  1137. if (id < 0)
  1138. break;
  1139. if (nr_allocated == trace->ev_qualifier_ids.nr) {
  1140. void *entries;
  1141. nr_allocated += 8;
  1142. entries = realloc(trace->ev_qualifier_ids.entries,
  1143. nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
  1144. if (entries == NULL) {
  1145. err = -ENOMEM;
  1146. fputs("\nError:\t Not enough memory for parsing\n", trace->output);
  1147. goto out_free;
  1148. }
  1149. trace->ev_qualifier_ids.entries = entries;
  1150. }
  1151. trace->ev_qualifier_ids.nr++;
  1152. trace->ev_qualifier_ids.entries[i++] = id;
  1153. }
  1154. }
  1155. if (err < 0) {
  1156. fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
  1157. "\nHint:\tand: 'man syscalls'\n", trace->output);
  1158. out_free:
  1159. zfree(&trace->ev_qualifier_ids.entries);
  1160. trace->ev_qualifier_ids.nr = 0;
  1161. }
  1162. out:
  1163. return err;
  1164. }
  1165. /*
  1166. * args is to be interpreted as a series of longs but we need to handle
  1167. * 8-byte unaligned accesses. args points to raw_data within the event
  1168. * and raw_data is guaranteed to be 8-byte unaligned because it is
  1169. * preceded by raw_size which is a u32. So we need to copy args to a temp
  1170. * variable to read it. Most notably this avoids extended load instructions
  1171. * on unaligned addresses
  1172. */
  1173. unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
  1174. {
  1175. unsigned long val;
  1176. unsigned char *p = arg->args + sizeof(unsigned long) * idx;
  1177. memcpy(&val, p, sizeof(val));
  1178. return val;
  1179. }
  1180. static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
  1181. struct syscall_arg *arg)
  1182. {
  1183. if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
  1184. return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
  1185. return scnprintf(bf, size, "arg%d: ", arg->idx);
  1186. }
  1187. static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
  1188. struct syscall_arg *arg, unsigned long val)
  1189. {
  1190. if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
  1191. arg->val = val;
  1192. if (sc->arg_fmt[arg->idx].parm)
  1193. arg->parm = sc->arg_fmt[arg->idx].parm;
  1194. return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
  1195. }
  1196. return scnprintf(bf, size, "%ld", val);
  1197. }
  1198. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  1199. unsigned char *args, struct trace *trace,
  1200. struct thread *thread)
  1201. {
  1202. size_t printed = 0;
  1203. unsigned long val;
  1204. u8 bit = 1;
  1205. struct syscall_arg arg = {
  1206. .args = args,
  1207. .idx = 0,
  1208. .mask = 0,
  1209. .trace = trace,
  1210. .thread = thread,
  1211. };
  1212. struct thread_trace *ttrace = thread__priv(thread);
  1213. /*
  1214. * Things like fcntl will set this in its 'cmd' formatter to pick the
  1215. * right formatter for the return value (an fd? file flags?), which is
  1216. * not needed for syscalls that always return a given type, say an fd.
  1217. */
  1218. ttrace->ret_scnprintf = NULL;
  1219. if (sc->args != NULL) {
  1220. struct format_field *field;
  1221. for (field = sc->args; field;
  1222. field = field->next, ++arg.idx, bit <<= 1) {
  1223. if (arg.mask & bit)
  1224. continue;
  1225. val = syscall_arg__val(&arg, arg.idx);
  1226. /*
  1227. * Suppress this argument if its value is zero and
  1228. * and we don't have a string associated in an
  1229. * strarray for it.
  1230. */
  1231. if (val == 0 &&
  1232. !(sc->arg_fmt &&
  1233. (sc->arg_fmt[arg.idx].show_zero ||
  1234. sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
  1235. sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
  1236. sc->arg_fmt[arg.idx].parm))
  1237. continue;
  1238. printed += scnprintf(bf + printed, size - printed,
  1239. "%s%s: ", printed ? ", " : "", field->name);
  1240. printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
  1241. }
  1242. } else if (IS_ERR(sc->tp_format)) {
  1243. /*
  1244. * If we managed to read the tracepoint /format file, then we
  1245. * may end up not having any args, like with gettid(), so only
  1246. * print the raw args when we didn't manage to read it.
  1247. */
  1248. while (arg.idx < sc->nr_args) {
  1249. if (arg.mask & bit)
  1250. goto next_arg;
  1251. val = syscall_arg__val(&arg, arg.idx);
  1252. if (printed)
  1253. printed += scnprintf(bf + printed, size - printed, ", ");
  1254. printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
  1255. printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
  1256. next_arg:
  1257. ++arg.idx;
  1258. bit <<= 1;
  1259. }
  1260. }
  1261. return printed;
  1262. }
  1263. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  1264. union perf_event *event,
  1265. struct perf_sample *sample);
  1266. static struct syscall *trace__syscall_info(struct trace *trace,
  1267. struct perf_evsel *evsel, int id)
  1268. {
  1269. if (id < 0) {
  1270. /*
  1271. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  1272. * before that, leaving at a higher verbosity level till that is
  1273. * explained. Reproduced with plain ftrace with:
  1274. *
  1275. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  1276. * grep "NR -1 " /t/trace_pipe
  1277. *
  1278. * After generating some load on the machine.
  1279. */
  1280. if (verbose > 1) {
  1281. static u64 n;
  1282. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  1283. id, perf_evsel__name(evsel), ++n);
  1284. }
  1285. return NULL;
  1286. }
  1287. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  1288. trace__read_syscall_info(trace, id))
  1289. goto out_cant_read;
  1290. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  1291. goto out_cant_read;
  1292. return &trace->syscalls.table[id];
  1293. out_cant_read:
  1294. if (verbose > 0) {
  1295. fprintf(trace->output, "Problems reading syscall %d", id);
  1296. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  1297. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  1298. fputs(" information\n", trace->output);
  1299. }
  1300. return NULL;
  1301. }
  1302. static void thread__update_stats(struct thread_trace *ttrace,
  1303. int id, struct perf_sample *sample)
  1304. {
  1305. struct int_node *inode;
  1306. struct stats *stats;
  1307. u64 duration = 0;
  1308. inode = intlist__findnew(ttrace->syscall_stats, id);
  1309. if (inode == NULL)
  1310. return;
  1311. stats = inode->priv;
  1312. if (stats == NULL) {
  1313. stats = malloc(sizeof(struct stats));
  1314. if (stats == NULL)
  1315. return;
  1316. init_stats(stats);
  1317. inode->priv = stats;
  1318. }
  1319. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  1320. duration = sample->time - ttrace->entry_time;
  1321. update_stats(stats, duration);
  1322. }
  1323. static int trace__printf_interrupted_entry(struct trace *trace)
  1324. {
  1325. struct thread_trace *ttrace;
  1326. size_t printed;
  1327. if (trace->failure_only || trace->current == NULL)
  1328. return 0;
  1329. ttrace = thread__priv(trace->current);
  1330. if (!ttrace->entry_pending)
  1331. return 0;
  1332. printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
  1333. printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
  1334. ttrace->entry_pending = false;
  1335. return printed;
  1336. }
  1337. static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
  1338. struct perf_sample *sample, struct thread *thread)
  1339. {
  1340. int printed = 0;
  1341. if (trace->print_sample) {
  1342. double ts = (double)sample->time / NSEC_PER_MSEC;
  1343. printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
  1344. perf_evsel__name(evsel), ts,
  1345. thread__comm_str(thread),
  1346. sample->pid, sample->tid, sample->cpu);
  1347. }
  1348. return printed;
  1349. }
  1350. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  1351. union perf_event *event __maybe_unused,
  1352. struct perf_sample *sample)
  1353. {
  1354. char *msg;
  1355. void *args;
  1356. size_t printed = 0;
  1357. struct thread *thread;
  1358. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1359. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1360. struct thread_trace *ttrace;
  1361. if (sc == NULL)
  1362. return -1;
  1363. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1364. ttrace = thread__trace(thread, trace->output);
  1365. if (ttrace == NULL)
  1366. goto out_put;
  1367. trace__fprintf_sample(trace, evsel, sample, thread);
  1368. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1369. if (ttrace->entry_str == NULL) {
  1370. ttrace->entry_str = malloc(trace__entry_str_size);
  1371. if (!ttrace->entry_str)
  1372. goto out_put;
  1373. }
  1374. if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
  1375. trace__printf_interrupted_entry(trace);
  1376. ttrace->entry_time = sample->time;
  1377. msg = ttrace->entry_str;
  1378. printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
  1379. printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
  1380. args, trace, thread);
  1381. if (sc->is_exit) {
  1382. if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
  1383. trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
  1384. fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
  1385. }
  1386. } else {
  1387. ttrace->entry_pending = true;
  1388. /* See trace__vfs_getname & trace__sys_exit */
  1389. ttrace->filename.pending_open = false;
  1390. }
  1391. if (trace->current != thread) {
  1392. thread__put(trace->current);
  1393. trace->current = thread__get(thread);
  1394. }
  1395. err = 0;
  1396. out_put:
  1397. thread__put(thread);
  1398. return err;
  1399. }
  1400. static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
  1401. struct perf_sample *sample,
  1402. struct callchain_cursor *cursor)
  1403. {
  1404. struct addr_location al;
  1405. int max_stack = evsel->attr.sample_max_stack ?
  1406. evsel->attr.sample_max_stack :
  1407. trace->max_stack;
  1408. if (machine__resolve(trace->host, &al, sample) < 0 ||
  1409. thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack))
  1410. return -1;
  1411. return 0;
  1412. }
  1413. static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
  1414. {
  1415. /* TODO: user-configurable print_opts */
  1416. const unsigned int print_opts = EVSEL__PRINT_SYM |
  1417. EVSEL__PRINT_DSO |
  1418. EVSEL__PRINT_UNKNOWN_AS_ADDR;
  1419. return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
  1420. }
  1421. static const char *errno_to_name(struct perf_evsel *evsel, int err)
  1422. {
  1423. struct perf_env *env = perf_evsel__env(evsel);
  1424. const char *arch_name = perf_env__arch(env);
  1425. return arch_syscalls__strerrno(arch_name, err);
  1426. }
  1427. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  1428. union perf_event *event __maybe_unused,
  1429. struct perf_sample *sample)
  1430. {
  1431. long ret;
  1432. u64 duration = 0;
  1433. bool duration_calculated = false;
  1434. struct thread *thread;
  1435. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
  1436. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1437. struct thread_trace *ttrace;
  1438. if (sc == NULL)
  1439. return -1;
  1440. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1441. ttrace = thread__trace(thread, trace->output);
  1442. if (ttrace == NULL)
  1443. goto out_put;
  1444. trace__fprintf_sample(trace, evsel, sample, thread);
  1445. if (trace->summary)
  1446. thread__update_stats(ttrace, id, sample);
  1447. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  1448. if (id == trace->open_id && ret >= 0 && ttrace->filename.pending_open) {
  1449. trace__set_fd_pathname(thread, ret, ttrace->filename.name);
  1450. ttrace->filename.pending_open = false;
  1451. ++trace->stats.vfs_getname;
  1452. }
  1453. if (ttrace->entry_time) {
  1454. duration = sample->time - ttrace->entry_time;
  1455. if (trace__filter_duration(trace, duration))
  1456. goto out;
  1457. duration_calculated = true;
  1458. } else if (trace->duration_filter)
  1459. goto out;
  1460. if (sample->callchain) {
  1461. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1462. if (callchain_ret == 0) {
  1463. if (callchain_cursor.nr < trace->min_stack)
  1464. goto out;
  1465. callchain_ret = 1;
  1466. }
  1467. }
  1468. if (trace->summary_only || (ret >= 0 && trace->failure_only))
  1469. goto out;
  1470. trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
  1471. if (ttrace->entry_pending) {
  1472. fprintf(trace->output, "%-70s", ttrace->entry_str);
  1473. } else {
  1474. fprintf(trace->output, " ... [");
  1475. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  1476. fprintf(trace->output, "]: %s()", sc->name);
  1477. }
  1478. if (sc->fmt == NULL) {
  1479. if (ret < 0)
  1480. goto errno_print;
  1481. signed_print:
  1482. fprintf(trace->output, ") = %ld", ret);
  1483. } else if (ret < 0) {
  1484. errno_print: {
  1485. char bf[STRERR_BUFSIZE];
  1486. const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
  1487. *e = errno_to_name(evsel, -ret);
  1488. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  1489. }
  1490. } else if (ret == 0 && sc->fmt->timeout)
  1491. fprintf(trace->output, ") = 0 Timeout");
  1492. else if (ttrace->ret_scnprintf) {
  1493. char bf[1024];
  1494. struct syscall_arg arg = {
  1495. .val = ret,
  1496. .thread = thread,
  1497. .trace = trace,
  1498. };
  1499. ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
  1500. ttrace->ret_scnprintf = NULL;
  1501. fprintf(trace->output, ") = %s", bf);
  1502. } else if (sc->fmt->hexret)
  1503. fprintf(trace->output, ") = %#lx", ret);
  1504. else if (sc->fmt->errpid) {
  1505. struct thread *child = machine__find_thread(trace->host, ret, ret);
  1506. if (child != NULL) {
  1507. fprintf(trace->output, ") = %ld", ret);
  1508. if (child->comm_set)
  1509. fprintf(trace->output, " (%s)", thread__comm_str(child));
  1510. thread__put(child);
  1511. }
  1512. } else
  1513. goto signed_print;
  1514. fputc('\n', trace->output);
  1515. if (callchain_ret > 0)
  1516. trace__fprintf_callchain(trace, sample);
  1517. else if (callchain_ret < 0)
  1518. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1519. out:
  1520. ttrace->entry_pending = false;
  1521. err = 0;
  1522. out_put:
  1523. thread__put(thread);
  1524. return err;
  1525. }
  1526. static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
  1527. union perf_event *event __maybe_unused,
  1528. struct perf_sample *sample)
  1529. {
  1530. struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1531. struct thread_trace *ttrace;
  1532. size_t filename_len, entry_str_len, to_move;
  1533. ssize_t remaining_space;
  1534. char *pos;
  1535. const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
  1536. if (!thread)
  1537. goto out;
  1538. ttrace = thread__priv(thread);
  1539. if (!ttrace)
  1540. goto out_put;
  1541. filename_len = strlen(filename);
  1542. if (filename_len == 0)
  1543. goto out_put;
  1544. if (ttrace->filename.namelen < filename_len) {
  1545. char *f = realloc(ttrace->filename.name, filename_len + 1);
  1546. if (f == NULL)
  1547. goto out_put;
  1548. ttrace->filename.namelen = filename_len;
  1549. ttrace->filename.name = f;
  1550. }
  1551. strcpy(ttrace->filename.name, filename);
  1552. ttrace->filename.pending_open = true;
  1553. if (!ttrace->filename.ptr)
  1554. goto out_put;
  1555. entry_str_len = strlen(ttrace->entry_str);
  1556. remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
  1557. if (remaining_space <= 0)
  1558. goto out_put;
  1559. if (filename_len > (size_t)remaining_space) {
  1560. filename += filename_len - remaining_space;
  1561. filename_len = remaining_space;
  1562. }
  1563. to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
  1564. pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
  1565. memmove(pos + filename_len, pos, to_move);
  1566. memcpy(pos, filename, filename_len);
  1567. ttrace->filename.ptr = 0;
  1568. ttrace->filename.entry_str_pos = 0;
  1569. out_put:
  1570. thread__put(thread);
  1571. out:
  1572. return 0;
  1573. }
  1574. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  1575. union perf_event *event __maybe_unused,
  1576. struct perf_sample *sample)
  1577. {
  1578. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  1579. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  1580. struct thread *thread = machine__findnew_thread(trace->host,
  1581. sample->pid,
  1582. sample->tid);
  1583. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  1584. if (ttrace == NULL)
  1585. goto out_dump;
  1586. ttrace->runtime_ms += runtime_ms;
  1587. trace->runtime_ms += runtime_ms;
  1588. out_put:
  1589. thread__put(thread);
  1590. return 0;
  1591. out_dump:
  1592. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  1593. evsel->name,
  1594. perf_evsel__strval(evsel, sample, "comm"),
  1595. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  1596. runtime,
  1597. perf_evsel__intval(evsel, sample, "vruntime"));
  1598. goto out_put;
  1599. }
  1600. static int bpf_output__printer(enum binary_printer_ops op,
  1601. unsigned int val, void *extra __maybe_unused, FILE *fp)
  1602. {
  1603. unsigned char ch = (unsigned char)val;
  1604. switch (op) {
  1605. case BINARY_PRINT_CHAR_DATA:
  1606. return fprintf(fp, "%c", isprint(ch) ? ch : '.');
  1607. case BINARY_PRINT_DATA_BEGIN:
  1608. case BINARY_PRINT_LINE_BEGIN:
  1609. case BINARY_PRINT_ADDR:
  1610. case BINARY_PRINT_NUM_DATA:
  1611. case BINARY_PRINT_NUM_PAD:
  1612. case BINARY_PRINT_SEP:
  1613. case BINARY_PRINT_CHAR_PAD:
  1614. case BINARY_PRINT_LINE_END:
  1615. case BINARY_PRINT_DATA_END:
  1616. default:
  1617. break;
  1618. }
  1619. return 0;
  1620. }
  1621. static void bpf_output__fprintf(struct trace *trace,
  1622. struct perf_sample *sample)
  1623. {
  1624. binary__fprintf(sample->raw_data, sample->raw_size, 8,
  1625. bpf_output__printer, NULL, trace->output);
  1626. }
  1627. static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
  1628. union perf_event *event __maybe_unused,
  1629. struct perf_sample *sample)
  1630. {
  1631. int callchain_ret = 0;
  1632. if (sample->callchain) {
  1633. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1634. if (callchain_ret == 0) {
  1635. if (callchain_cursor.nr < trace->min_stack)
  1636. goto out;
  1637. callchain_ret = 1;
  1638. }
  1639. }
  1640. trace__printf_interrupted_entry(trace);
  1641. trace__fprintf_tstamp(trace, sample->time, trace->output);
  1642. if (trace->trace_syscalls)
  1643. fprintf(trace->output, "( ): ");
  1644. fprintf(trace->output, "%s:", evsel->name);
  1645. if (perf_evsel__is_bpf_output(evsel)) {
  1646. bpf_output__fprintf(trace, sample);
  1647. } else if (evsel->tp_format) {
  1648. event_format__fprintf(evsel->tp_format, sample->cpu,
  1649. sample->raw_data, sample->raw_size,
  1650. trace->output);
  1651. }
  1652. fprintf(trace->output, "\n");
  1653. if (callchain_ret > 0)
  1654. trace__fprintf_callchain(trace, sample);
  1655. else if (callchain_ret < 0)
  1656. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1657. out:
  1658. return 0;
  1659. }
  1660. static void print_location(FILE *f, struct perf_sample *sample,
  1661. struct addr_location *al,
  1662. bool print_dso, bool print_sym)
  1663. {
  1664. if ((verbose > 0 || print_dso) && al->map)
  1665. fprintf(f, "%s@", al->map->dso->long_name);
  1666. if ((verbose > 0 || print_sym) && al->sym)
  1667. fprintf(f, "%s+0x%" PRIx64, al->sym->name,
  1668. al->addr - al->sym->start);
  1669. else if (al->map)
  1670. fprintf(f, "0x%" PRIx64, al->addr);
  1671. else
  1672. fprintf(f, "0x%" PRIx64, sample->addr);
  1673. }
  1674. static int trace__pgfault(struct trace *trace,
  1675. struct perf_evsel *evsel,
  1676. union perf_event *event __maybe_unused,
  1677. struct perf_sample *sample)
  1678. {
  1679. struct thread *thread;
  1680. struct addr_location al;
  1681. char map_type = 'd';
  1682. struct thread_trace *ttrace;
  1683. int err = -1;
  1684. int callchain_ret = 0;
  1685. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1686. if (sample->callchain) {
  1687. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1688. if (callchain_ret == 0) {
  1689. if (callchain_cursor.nr < trace->min_stack)
  1690. goto out_put;
  1691. callchain_ret = 1;
  1692. }
  1693. }
  1694. ttrace = thread__trace(thread, trace->output);
  1695. if (ttrace == NULL)
  1696. goto out_put;
  1697. if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
  1698. ttrace->pfmaj++;
  1699. else
  1700. ttrace->pfmin++;
  1701. if (trace->summary_only)
  1702. goto out;
  1703. thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
  1704. trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
  1705. fprintf(trace->output, "%sfault [",
  1706. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
  1707. "maj" : "min");
  1708. print_location(trace->output, sample, &al, false, true);
  1709. fprintf(trace->output, "] => ");
  1710. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  1711. if (!al.map) {
  1712. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  1713. if (al.map)
  1714. map_type = 'x';
  1715. else
  1716. map_type = '?';
  1717. }
  1718. print_location(trace->output, sample, &al, true, false);
  1719. fprintf(trace->output, " (%c%c)\n", map_type, al.level);
  1720. if (callchain_ret > 0)
  1721. trace__fprintf_callchain(trace, sample);
  1722. else if (callchain_ret < 0)
  1723. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1724. out:
  1725. err = 0;
  1726. out_put:
  1727. thread__put(thread);
  1728. return err;
  1729. }
  1730. static void trace__set_base_time(struct trace *trace,
  1731. struct perf_evsel *evsel,
  1732. struct perf_sample *sample)
  1733. {
  1734. /*
  1735. * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
  1736. * and don't use sample->time unconditionally, we may end up having
  1737. * some other event in the future without PERF_SAMPLE_TIME for good
  1738. * reason, i.e. we may not be interested in its timestamps, just in
  1739. * it taking place, picking some piece of information when it
  1740. * appears in our event stream (vfs_getname comes to mind).
  1741. */
  1742. if (trace->base_time == 0 && !trace->full_time &&
  1743. (evsel->attr.sample_type & PERF_SAMPLE_TIME))
  1744. trace->base_time = sample->time;
  1745. }
  1746. static int trace__process_sample(struct perf_tool *tool,
  1747. union perf_event *event,
  1748. struct perf_sample *sample,
  1749. struct perf_evsel *evsel,
  1750. struct machine *machine __maybe_unused)
  1751. {
  1752. struct trace *trace = container_of(tool, struct trace, tool);
  1753. struct thread *thread;
  1754. int err = 0;
  1755. tracepoint_handler handler = evsel->handler;
  1756. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1757. if (thread && thread__is_filtered(thread))
  1758. goto out;
  1759. trace__set_base_time(trace, evsel, sample);
  1760. if (handler) {
  1761. ++trace->nr_events;
  1762. handler(trace, evsel, event, sample);
  1763. }
  1764. out:
  1765. thread__put(thread);
  1766. return err;
  1767. }
  1768. static int trace__record(struct trace *trace, int argc, const char **argv)
  1769. {
  1770. unsigned int rec_argc, i, j;
  1771. const char **rec_argv;
  1772. const char * const record_args[] = {
  1773. "record",
  1774. "-R",
  1775. "-m", "1024",
  1776. "-c", "1",
  1777. };
  1778. const char * const sc_args[] = { "-e", };
  1779. unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
  1780. const char * const majpf_args[] = { "-e", "major-faults" };
  1781. unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
  1782. const char * const minpf_args[] = { "-e", "minor-faults" };
  1783. unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
  1784. /* +1 is for the event string below */
  1785. rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
  1786. majpf_args_nr + minpf_args_nr + argc;
  1787. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1788. if (rec_argv == NULL)
  1789. return -ENOMEM;
  1790. j = 0;
  1791. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1792. rec_argv[j++] = record_args[i];
  1793. if (trace->trace_syscalls) {
  1794. for (i = 0; i < sc_args_nr; i++)
  1795. rec_argv[j++] = sc_args[i];
  1796. /* event string may be different for older kernels - e.g., RHEL6 */
  1797. if (is_valid_tracepoint("raw_syscalls:sys_enter"))
  1798. rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
  1799. else if (is_valid_tracepoint("syscalls:sys_enter"))
  1800. rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
  1801. else {
  1802. pr_err("Neither raw_syscalls nor syscalls events exist.\n");
  1803. free(rec_argv);
  1804. return -1;
  1805. }
  1806. }
  1807. if (trace->trace_pgfaults & TRACE_PFMAJ)
  1808. for (i = 0; i < majpf_args_nr; i++)
  1809. rec_argv[j++] = majpf_args[i];
  1810. if (trace->trace_pgfaults & TRACE_PFMIN)
  1811. for (i = 0; i < minpf_args_nr; i++)
  1812. rec_argv[j++] = minpf_args[i];
  1813. for (i = 0; i < (unsigned int)argc; i++)
  1814. rec_argv[j++] = argv[i];
  1815. return cmd_record(j, rec_argv);
  1816. }
  1817. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  1818. static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
  1819. {
  1820. struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
  1821. if (IS_ERR(evsel))
  1822. return false;
  1823. if (perf_evsel__field(evsel, "pathname") == NULL) {
  1824. perf_evsel__delete(evsel);
  1825. return false;
  1826. }
  1827. evsel->handler = trace__vfs_getname;
  1828. perf_evlist__add(evlist, evsel);
  1829. return true;
  1830. }
  1831. static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
  1832. {
  1833. struct perf_evsel *evsel;
  1834. struct perf_event_attr attr = {
  1835. .type = PERF_TYPE_SOFTWARE,
  1836. .mmap_data = 1,
  1837. };
  1838. attr.config = config;
  1839. attr.sample_period = 1;
  1840. event_attr_init(&attr);
  1841. evsel = perf_evsel__new(&attr);
  1842. if (evsel)
  1843. evsel->handler = trace__pgfault;
  1844. return evsel;
  1845. }
  1846. static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
  1847. {
  1848. const u32 type = event->header.type;
  1849. struct perf_evsel *evsel;
  1850. if (type != PERF_RECORD_SAMPLE) {
  1851. trace__process_event(trace, trace->host, event, sample);
  1852. return;
  1853. }
  1854. evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
  1855. if (evsel == NULL) {
  1856. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
  1857. return;
  1858. }
  1859. trace__set_base_time(trace, evsel, sample);
  1860. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  1861. sample->raw_data == NULL) {
  1862. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  1863. perf_evsel__name(evsel), sample->tid,
  1864. sample->cpu, sample->raw_size);
  1865. } else {
  1866. tracepoint_handler handler = evsel->handler;
  1867. handler(trace, evsel, event, sample);
  1868. }
  1869. }
  1870. static int trace__add_syscall_newtp(struct trace *trace)
  1871. {
  1872. int ret = -1;
  1873. struct perf_evlist *evlist = trace->evlist;
  1874. struct perf_evsel *sys_enter, *sys_exit;
  1875. sys_enter = perf_evsel__syscall_newtp("sys_enter", trace__sys_enter);
  1876. if (sys_enter == NULL)
  1877. goto out;
  1878. if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
  1879. goto out_delete_sys_enter;
  1880. sys_exit = perf_evsel__syscall_newtp("sys_exit", trace__sys_exit);
  1881. if (sys_exit == NULL)
  1882. goto out_delete_sys_enter;
  1883. if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
  1884. goto out_delete_sys_exit;
  1885. perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
  1886. perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
  1887. perf_evlist__add(evlist, sys_enter);
  1888. perf_evlist__add(evlist, sys_exit);
  1889. if (callchain_param.enabled && !trace->kernel_syscallchains) {
  1890. /*
  1891. * We're interested only in the user space callchain
  1892. * leading to the syscall, allow overriding that for
  1893. * debugging reasons using --kernel_syscall_callchains
  1894. */
  1895. sys_exit->attr.exclude_callchain_kernel = 1;
  1896. }
  1897. trace->syscalls.events.sys_enter = sys_enter;
  1898. trace->syscalls.events.sys_exit = sys_exit;
  1899. ret = 0;
  1900. out:
  1901. return ret;
  1902. out_delete_sys_exit:
  1903. perf_evsel__delete_priv(sys_exit);
  1904. out_delete_sys_enter:
  1905. perf_evsel__delete_priv(sys_enter);
  1906. goto out;
  1907. }
  1908. static int trace__set_ev_qualifier_filter(struct trace *trace)
  1909. {
  1910. int err = -1;
  1911. struct perf_evsel *sys_exit;
  1912. char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
  1913. trace->ev_qualifier_ids.nr,
  1914. trace->ev_qualifier_ids.entries);
  1915. if (filter == NULL)
  1916. goto out_enomem;
  1917. if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
  1918. filter)) {
  1919. sys_exit = trace->syscalls.events.sys_exit;
  1920. err = perf_evsel__append_tp_filter(sys_exit, filter);
  1921. }
  1922. free(filter);
  1923. out:
  1924. return err;
  1925. out_enomem:
  1926. errno = ENOMEM;
  1927. goto out;
  1928. }
  1929. static int trace__set_filter_loop_pids(struct trace *trace)
  1930. {
  1931. unsigned int nr = 1;
  1932. pid_t pids[32] = {
  1933. getpid(),
  1934. };
  1935. struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
  1936. while (thread && nr < ARRAY_SIZE(pids)) {
  1937. struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
  1938. if (parent == NULL)
  1939. break;
  1940. if (!strcmp(thread__comm_str(parent), "sshd")) {
  1941. pids[nr++] = parent->tid;
  1942. break;
  1943. }
  1944. thread = parent;
  1945. }
  1946. return perf_evlist__set_filter_pids(trace->evlist, nr, pids);
  1947. }
  1948. static int trace__run(struct trace *trace, int argc, const char **argv)
  1949. {
  1950. struct perf_evlist *evlist = trace->evlist;
  1951. struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
  1952. int err = -1, i;
  1953. unsigned long before;
  1954. const bool forks = argc > 0;
  1955. bool draining = false;
  1956. trace->live = true;
  1957. if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
  1958. goto out_error_raw_syscalls;
  1959. if (trace->trace_syscalls)
  1960. trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
  1961. if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
  1962. pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
  1963. if (pgfault_maj == NULL)
  1964. goto out_error_mem;
  1965. perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
  1966. perf_evlist__add(evlist, pgfault_maj);
  1967. }
  1968. if ((trace->trace_pgfaults & TRACE_PFMIN)) {
  1969. pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
  1970. if (pgfault_min == NULL)
  1971. goto out_error_mem;
  1972. perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
  1973. perf_evlist__add(evlist, pgfault_min);
  1974. }
  1975. if (trace->sched &&
  1976. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  1977. trace__sched_stat_runtime))
  1978. goto out_error_sched_stat_runtime;
  1979. /*
  1980. * If a global cgroup was set, apply it to all the events without an
  1981. * explicit cgroup. I.e.:
  1982. *
  1983. * trace -G A -e sched:*switch
  1984. *
  1985. * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
  1986. * _and_ sched:sched_switch to the 'A' cgroup, while:
  1987. *
  1988. * trace -e sched:*switch -G A
  1989. *
  1990. * will only set the sched:sched_switch event to the 'A' cgroup, all the
  1991. * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
  1992. * a cgroup (on the root cgroup, sys wide, etc).
  1993. *
  1994. * Multiple cgroups:
  1995. *
  1996. * trace -G A -e sched:*switch -G B
  1997. *
  1998. * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
  1999. * to the 'B' cgroup.
  2000. *
  2001. * evlist__set_default_cgroup() grabs a reference of the passed cgroup
  2002. * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
  2003. */
  2004. if (trace->cgroup)
  2005. evlist__set_default_cgroup(trace->evlist, trace->cgroup);
  2006. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  2007. if (err < 0) {
  2008. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  2009. goto out_delete_evlist;
  2010. }
  2011. err = trace__symbols_init(trace, evlist);
  2012. if (err < 0) {
  2013. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  2014. goto out_delete_evlist;
  2015. }
  2016. perf_evlist__config(evlist, &trace->opts, &callchain_param);
  2017. signal(SIGCHLD, sig_handler);
  2018. signal(SIGINT, sig_handler);
  2019. if (forks) {
  2020. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  2021. argv, false, NULL);
  2022. if (err < 0) {
  2023. fprintf(trace->output, "Couldn't run the workload!\n");
  2024. goto out_delete_evlist;
  2025. }
  2026. }
  2027. err = perf_evlist__open(evlist);
  2028. if (err < 0)
  2029. goto out_error_open;
  2030. err = bpf__apply_obj_config();
  2031. if (err) {
  2032. char errbuf[BUFSIZ];
  2033. bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
  2034. pr_err("ERROR: Apply config to BPF failed: %s\n",
  2035. errbuf);
  2036. goto out_error_open;
  2037. }
  2038. /*
  2039. * Better not use !target__has_task() here because we need to cover the
  2040. * case where no threads were specified in the command line, but a
  2041. * workload was, and in that case we will fill in the thread_map when
  2042. * we fork the workload in perf_evlist__prepare_workload.
  2043. */
  2044. if (trace->filter_pids.nr > 0)
  2045. err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
  2046. else if (thread_map__pid(evlist->threads, 0) == -1)
  2047. err = trace__set_filter_loop_pids(trace);
  2048. if (err < 0)
  2049. goto out_error_mem;
  2050. if (trace->ev_qualifier_ids.nr > 0) {
  2051. err = trace__set_ev_qualifier_filter(trace);
  2052. if (err < 0)
  2053. goto out_errno;
  2054. pr_debug("event qualifier tracepoint filter: %s\n",
  2055. trace->syscalls.events.sys_exit->filter);
  2056. }
  2057. err = perf_evlist__apply_filters(evlist, &evsel);
  2058. if (err < 0)
  2059. goto out_error_apply_filters;
  2060. err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
  2061. if (err < 0)
  2062. goto out_error_mmap;
  2063. if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
  2064. perf_evlist__enable(evlist);
  2065. if (forks)
  2066. perf_evlist__start_workload(evlist);
  2067. if (trace->opts.initial_delay) {
  2068. usleep(trace->opts.initial_delay * 1000);
  2069. perf_evlist__enable(evlist);
  2070. }
  2071. trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
  2072. evlist->threads->nr > 1 ||
  2073. perf_evlist__first(evlist)->attr.inherit;
  2074. /*
  2075. * Now that we already used evsel->attr to ask the kernel to setup the
  2076. * events, lets reuse evsel->attr.sample_max_stack as the limit in
  2077. * trace__resolve_callchain(), allowing per-event max-stack settings
  2078. * to override an explicitely set --max-stack global setting.
  2079. */
  2080. evlist__for_each_entry(evlist, evsel) {
  2081. if (evsel__has_callchain(evsel) &&
  2082. evsel->attr.sample_max_stack == 0)
  2083. evsel->attr.sample_max_stack = trace->max_stack;
  2084. }
  2085. again:
  2086. before = trace->nr_events;
  2087. for (i = 0; i < evlist->nr_mmaps; i++) {
  2088. union perf_event *event;
  2089. struct perf_mmap *md;
  2090. md = &evlist->mmap[i];
  2091. if (perf_mmap__read_init(md) < 0)
  2092. continue;
  2093. while ((event = perf_mmap__read_event(md)) != NULL) {
  2094. struct perf_sample sample;
  2095. ++trace->nr_events;
  2096. err = perf_evlist__parse_sample(evlist, event, &sample);
  2097. if (err) {
  2098. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  2099. goto next_event;
  2100. }
  2101. trace__handle_event(trace, event, &sample);
  2102. next_event:
  2103. perf_mmap__consume(md);
  2104. if (interrupted)
  2105. goto out_disable;
  2106. if (done && !draining) {
  2107. perf_evlist__disable(evlist);
  2108. draining = true;
  2109. }
  2110. }
  2111. perf_mmap__read_done(md);
  2112. }
  2113. if (trace->nr_events == before) {
  2114. int timeout = done ? 100 : -1;
  2115. if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
  2116. if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
  2117. draining = true;
  2118. goto again;
  2119. }
  2120. } else {
  2121. goto again;
  2122. }
  2123. out_disable:
  2124. thread__zput(trace->current);
  2125. perf_evlist__disable(evlist);
  2126. if (!err) {
  2127. if (trace->summary)
  2128. trace__fprintf_thread_summary(trace, trace->output);
  2129. if (trace->show_tool_stats) {
  2130. fprintf(trace->output, "Stats:\n "
  2131. " vfs_getname : %" PRIu64 "\n"
  2132. " proc_getname: %" PRIu64 "\n",
  2133. trace->stats.vfs_getname,
  2134. trace->stats.proc_getname);
  2135. }
  2136. }
  2137. out_delete_evlist:
  2138. trace__symbols__exit(trace);
  2139. perf_evlist__delete(evlist);
  2140. cgroup__put(trace->cgroup);
  2141. trace->evlist = NULL;
  2142. trace->live = false;
  2143. return err;
  2144. {
  2145. char errbuf[BUFSIZ];
  2146. out_error_sched_stat_runtime:
  2147. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
  2148. goto out_error;
  2149. out_error_raw_syscalls:
  2150. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
  2151. goto out_error;
  2152. out_error_mmap:
  2153. perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
  2154. goto out_error;
  2155. out_error_open:
  2156. perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  2157. out_error:
  2158. fprintf(trace->output, "%s\n", errbuf);
  2159. goto out_delete_evlist;
  2160. out_error_apply_filters:
  2161. fprintf(trace->output,
  2162. "Failed to set filter \"%s\" on event %s with %d (%s)\n",
  2163. evsel->filter, perf_evsel__name(evsel), errno,
  2164. str_error_r(errno, errbuf, sizeof(errbuf)));
  2165. goto out_delete_evlist;
  2166. }
  2167. out_error_mem:
  2168. fprintf(trace->output, "Not enough memory to run!\n");
  2169. goto out_delete_evlist;
  2170. out_errno:
  2171. fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
  2172. goto out_delete_evlist;
  2173. }
  2174. static int trace__replay(struct trace *trace)
  2175. {
  2176. const struct perf_evsel_str_handler handlers[] = {
  2177. { "probe:vfs_getname", trace__vfs_getname, },
  2178. };
  2179. struct perf_data data = {
  2180. .file = {
  2181. .path = input_name,
  2182. },
  2183. .mode = PERF_DATA_MODE_READ,
  2184. .force = trace->force,
  2185. };
  2186. struct perf_session *session;
  2187. struct perf_evsel *evsel;
  2188. int err = -1;
  2189. trace->tool.sample = trace__process_sample;
  2190. trace->tool.mmap = perf_event__process_mmap;
  2191. trace->tool.mmap2 = perf_event__process_mmap2;
  2192. trace->tool.comm = perf_event__process_comm;
  2193. trace->tool.exit = perf_event__process_exit;
  2194. trace->tool.fork = perf_event__process_fork;
  2195. trace->tool.attr = perf_event__process_attr;
  2196. trace->tool.tracing_data = perf_event__process_tracing_data;
  2197. trace->tool.build_id = perf_event__process_build_id;
  2198. trace->tool.namespaces = perf_event__process_namespaces;
  2199. trace->tool.ordered_events = true;
  2200. trace->tool.ordering_requires_timestamps = true;
  2201. /* add tid to output */
  2202. trace->multiple_threads = true;
  2203. session = perf_session__new(&data, false, &trace->tool);
  2204. if (session == NULL)
  2205. return -1;
  2206. if (trace->opts.target.pid)
  2207. symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
  2208. if (trace->opts.target.tid)
  2209. symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
  2210. if (symbol__init(&session->header.env) < 0)
  2211. goto out;
  2212. trace->host = &session->machines.host;
  2213. err = perf_session__set_tracepoints_handlers(session, handlers);
  2214. if (err)
  2215. goto out;
  2216. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2217. "raw_syscalls:sys_enter");
  2218. /* older kernels have syscalls tp versus raw_syscalls */
  2219. if (evsel == NULL)
  2220. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2221. "syscalls:sys_enter");
  2222. if (evsel &&
  2223. (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 ||
  2224. perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
  2225. pr_err("Error during initialize raw_syscalls:sys_enter event\n");
  2226. goto out;
  2227. }
  2228. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2229. "raw_syscalls:sys_exit");
  2230. if (evsel == NULL)
  2231. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2232. "syscalls:sys_exit");
  2233. if (evsel &&
  2234. (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 ||
  2235. perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
  2236. pr_err("Error during initialize raw_syscalls:sys_exit event\n");
  2237. goto out;
  2238. }
  2239. evlist__for_each_entry(session->evlist, evsel) {
  2240. if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
  2241. (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
  2242. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  2243. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
  2244. evsel->handler = trace__pgfault;
  2245. }
  2246. setup_pager();
  2247. err = perf_session__process_events(session);
  2248. if (err)
  2249. pr_err("Failed to process events, error %d", err);
  2250. else if (trace->summary)
  2251. trace__fprintf_thread_summary(trace, trace->output);
  2252. out:
  2253. perf_session__delete(session);
  2254. return err;
  2255. }
  2256. static size_t trace__fprintf_threads_header(FILE *fp)
  2257. {
  2258. size_t printed;
  2259. printed = fprintf(fp, "\n Summary of events:\n\n");
  2260. return printed;
  2261. }
  2262. DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
  2263. struct stats *stats;
  2264. double msecs;
  2265. int syscall;
  2266. )
  2267. {
  2268. struct int_node *source = rb_entry(nd, struct int_node, rb_node);
  2269. struct stats *stats = source->priv;
  2270. entry->syscall = source->i;
  2271. entry->stats = stats;
  2272. entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
  2273. }
  2274. static size_t thread__dump_stats(struct thread_trace *ttrace,
  2275. struct trace *trace, FILE *fp)
  2276. {
  2277. size_t printed = 0;
  2278. struct syscall *sc;
  2279. struct rb_node *nd;
  2280. DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
  2281. if (syscall_stats == NULL)
  2282. return 0;
  2283. printed += fprintf(fp, "\n");
  2284. printed += fprintf(fp, " syscall calls total min avg max stddev\n");
  2285. printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
  2286. printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
  2287. resort_rb__for_each_entry(nd, syscall_stats) {
  2288. struct stats *stats = syscall_stats_entry->stats;
  2289. if (stats) {
  2290. double min = (double)(stats->min) / NSEC_PER_MSEC;
  2291. double max = (double)(stats->max) / NSEC_PER_MSEC;
  2292. double avg = avg_stats(stats);
  2293. double pct;
  2294. u64 n = (u64) stats->n;
  2295. pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
  2296. avg /= NSEC_PER_MSEC;
  2297. sc = &trace->syscalls.table[syscall_stats_entry->syscall];
  2298. printed += fprintf(fp, " %-15s", sc->name);
  2299. printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
  2300. n, syscall_stats_entry->msecs, min, avg);
  2301. printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
  2302. }
  2303. }
  2304. resort_rb__delete(syscall_stats);
  2305. printed += fprintf(fp, "\n\n");
  2306. return printed;
  2307. }
  2308. static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
  2309. {
  2310. size_t printed = 0;
  2311. struct thread_trace *ttrace = thread__priv(thread);
  2312. double ratio;
  2313. if (ttrace == NULL)
  2314. return 0;
  2315. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  2316. printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
  2317. printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
  2318. printed += fprintf(fp, "%.1f%%", ratio);
  2319. if (ttrace->pfmaj)
  2320. printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
  2321. if (ttrace->pfmin)
  2322. printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
  2323. if (trace->sched)
  2324. printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
  2325. else if (fputc('\n', fp) != EOF)
  2326. ++printed;
  2327. printed += thread__dump_stats(ttrace, trace, fp);
  2328. return printed;
  2329. }
  2330. static unsigned long thread__nr_events(struct thread_trace *ttrace)
  2331. {
  2332. return ttrace ? ttrace->nr_events : 0;
  2333. }
  2334. DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
  2335. struct thread *thread;
  2336. )
  2337. {
  2338. entry->thread = rb_entry(nd, struct thread, rb_node);
  2339. }
  2340. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  2341. {
  2342. size_t printed = trace__fprintf_threads_header(fp);
  2343. struct rb_node *nd;
  2344. int i;
  2345. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  2346. DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
  2347. if (threads == NULL) {
  2348. fprintf(fp, "%s", "Error sorting output by nr_events!\n");
  2349. return 0;
  2350. }
  2351. resort_rb__for_each_entry(nd, threads)
  2352. printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
  2353. resort_rb__delete(threads);
  2354. }
  2355. return printed;
  2356. }
  2357. static int trace__set_duration(const struct option *opt, const char *str,
  2358. int unset __maybe_unused)
  2359. {
  2360. struct trace *trace = opt->value;
  2361. trace->duration_filter = atof(str);
  2362. return 0;
  2363. }
  2364. static int trace__set_filter_pids(const struct option *opt, const char *str,
  2365. int unset __maybe_unused)
  2366. {
  2367. int ret = -1;
  2368. size_t i;
  2369. struct trace *trace = opt->value;
  2370. /*
  2371. * FIXME: introduce a intarray class, plain parse csv and create a
  2372. * { int nr, int entries[] } struct...
  2373. */
  2374. struct intlist *list = intlist__new(str);
  2375. if (list == NULL)
  2376. return -1;
  2377. i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
  2378. trace->filter_pids.entries = calloc(i, sizeof(pid_t));
  2379. if (trace->filter_pids.entries == NULL)
  2380. goto out;
  2381. trace->filter_pids.entries[0] = getpid();
  2382. for (i = 1; i < trace->filter_pids.nr; ++i)
  2383. trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
  2384. intlist__delete(list);
  2385. ret = 0;
  2386. out:
  2387. return ret;
  2388. }
  2389. static int trace__open_output(struct trace *trace, const char *filename)
  2390. {
  2391. struct stat st;
  2392. if (!stat(filename, &st) && st.st_size) {
  2393. char oldname[PATH_MAX];
  2394. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  2395. unlink(oldname);
  2396. rename(filename, oldname);
  2397. }
  2398. trace->output = fopen(filename, "w");
  2399. return trace->output == NULL ? -errno : 0;
  2400. }
  2401. static int parse_pagefaults(const struct option *opt, const char *str,
  2402. int unset __maybe_unused)
  2403. {
  2404. int *trace_pgfaults = opt->value;
  2405. if (strcmp(str, "all") == 0)
  2406. *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
  2407. else if (strcmp(str, "maj") == 0)
  2408. *trace_pgfaults |= TRACE_PFMAJ;
  2409. else if (strcmp(str, "min") == 0)
  2410. *trace_pgfaults |= TRACE_PFMIN;
  2411. else
  2412. return -1;
  2413. return 0;
  2414. }
  2415. static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
  2416. {
  2417. struct perf_evsel *evsel;
  2418. evlist__for_each_entry(evlist, evsel)
  2419. evsel->handler = handler;
  2420. }
  2421. /*
  2422. * XXX: Hackish, just splitting the combined -e+--event (syscalls
  2423. * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
  2424. * existing facilities unchanged (trace->ev_qualifier + parse_options()).
  2425. *
  2426. * It'd be better to introduce a parse_options() variant that would return a
  2427. * list with the terms it didn't match to an event...
  2428. */
  2429. static int trace__parse_events_option(const struct option *opt, const char *str,
  2430. int unset __maybe_unused)
  2431. {
  2432. struct trace *trace = (struct trace *)opt->value;
  2433. const char *s = str;
  2434. char *sep = NULL, *lists[2] = { NULL, NULL, };
  2435. int len = strlen(str) + 1, err = -1, list, idx;
  2436. char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
  2437. char group_name[PATH_MAX];
  2438. if (strace_groups_dir == NULL)
  2439. return -1;
  2440. if (*s == '!') {
  2441. ++s;
  2442. trace->not_ev_qualifier = true;
  2443. }
  2444. while (1) {
  2445. if ((sep = strchr(s, ',')) != NULL)
  2446. *sep = '\0';
  2447. list = 0;
  2448. if (syscalltbl__id(trace->sctbl, s) >= 0 ||
  2449. syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
  2450. list = 1;
  2451. } else {
  2452. path__join(group_name, sizeof(group_name), strace_groups_dir, s);
  2453. if (access(group_name, R_OK) == 0)
  2454. list = 1;
  2455. }
  2456. if (lists[list]) {
  2457. sprintf(lists[list] + strlen(lists[list]), ",%s", s);
  2458. } else {
  2459. lists[list] = malloc(len);
  2460. if (lists[list] == NULL)
  2461. goto out;
  2462. strcpy(lists[list], s);
  2463. }
  2464. if (!sep)
  2465. break;
  2466. *sep = ',';
  2467. s = sep + 1;
  2468. }
  2469. if (lists[1] != NULL) {
  2470. struct strlist_config slist_config = {
  2471. .dirname = strace_groups_dir,
  2472. };
  2473. trace->ev_qualifier = strlist__new(lists[1], &slist_config);
  2474. if (trace->ev_qualifier == NULL) {
  2475. fputs("Not enough memory to parse event qualifier", trace->output);
  2476. goto out;
  2477. }
  2478. if (trace__validate_ev_qualifier(trace))
  2479. goto out;
  2480. trace->trace_syscalls = true;
  2481. }
  2482. err = 0;
  2483. if (lists[0]) {
  2484. struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
  2485. "event selector. use 'perf list' to list available events",
  2486. parse_events_option);
  2487. err = parse_events_option(&o, lists[0], 0);
  2488. }
  2489. out:
  2490. if (sep)
  2491. *sep = ',';
  2492. return err;
  2493. }
  2494. static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
  2495. {
  2496. struct trace *trace = opt->value;
  2497. if (!list_empty(&trace->evlist->entries))
  2498. return parse_cgroups(opt, str, unset);
  2499. trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
  2500. return 0;
  2501. }
  2502. int cmd_trace(int argc, const char **argv)
  2503. {
  2504. const char *trace_usage[] = {
  2505. "perf trace [<options>] [<command>]",
  2506. "perf trace [<options>] -- <command> [<options>]",
  2507. "perf trace record [<options>] [<command>]",
  2508. "perf trace record [<options>] -- <command> [<options>]",
  2509. NULL
  2510. };
  2511. struct trace trace = {
  2512. .syscalls = {
  2513. . max = -1,
  2514. },
  2515. .opts = {
  2516. .target = {
  2517. .uid = UINT_MAX,
  2518. .uses_mmap = true,
  2519. },
  2520. .user_freq = UINT_MAX,
  2521. .user_interval = ULLONG_MAX,
  2522. .no_buffering = true,
  2523. .mmap_pages = UINT_MAX,
  2524. .proc_map_timeout = 500,
  2525. },
  2526. .output = stderr,
  2527. .show_comm = true,
  2528. .trace_syscalls = false,
  2529. .kernel_syscallchains = false,
  2530. .max_stack = UINT_MAX,
  2531. };
  2532. const char *output_name = NULL;
  2533. const struct option trace_options[] = {
  2534. OPT_CALLBACK('e', "event", &trace, "event",
  2535. "event/syscall selector. use 'perf list' to list available events",
  2536. trace__parse_events_option),
  2537. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  2538. "show the thread COMM next to its id"),
  2539. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  2540. OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
  2541. trace__parse_events_option),
  2542. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  2543. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  2544. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  2545. "trace events on existing process id"),
  2546. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  2547. "trace events on existing thread id"),
  2548. OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
  2549. "pids to filter (by the kernel)", trace__set_filter_pids),
  2550. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  2551. "system-wide collection from all CPUs"),
  2552. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  2553. "list of cpus to monitor"),
  2554. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  2555. "child tasks do not inherit counters"),
  2556. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  2557. "number of mmap data pages",
  2558. perf_evlist__parse_mmap_pages),
  2559. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  2560. "user to profile"),
  2561. OPT_CALLBACK(0, "duration", &trace, "float",
  2562. "show only events with duration > N.M ms",
  2563. trace__set_duration),
  2564. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  2565. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  2566. OPT_BOOLEAN('T', "time", &trace.full_time,
  2567. "Show full timestamp, not time relative to first start"),
  2568. OPT_BOOLEAN(0, "failure", &trace.failure_only,
  2569. "Show only syscalls that failed"),
  2570. OPT_BOOLEAN('s', "summary", &trace.summary_only,
  2571. "Show only syscall summary with statistics"),
  2572. OPT_BOOLEAN('S', "with-summary", &trace.summary,
  2573. "Show all syscalls and summary with statistics"),
  2574. OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
  2575. "Trace pagefaults", parse_pagefaults, "maj"),
  2576. OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
  2577. OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
  2578. OPT_CALLBACK(0, "call-graph", &trace.opts,
  2579. "record_mode[,record_size]", record_callchain_help,
  2580. &record_parse_callchain_opt),
  2581. OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
  2582. "Show the kernel callchains on the syscall exit path"),
  2583. OPT_UINTEGER(0, "min-stack", &trace.min_stack,
  2584. "Set the minimum stack depth when parsing the callchain, "
  2585. "anything below the specified depth will be ignored."),
  2586. OPT_UINTEGER(0, "max-stack", &trace.max_stack,
  2587. "Set the maximum stack depth when parsing the callchain, "
  2588. "anything beyond the specified depth will be ignored. "
  2589. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  2590. OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
  2591. "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
  2592. OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
  2593. "per thread proc mmap processing timeout in ms"),
  2594. OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
  2595. trace__parse_cgroups),
  2596. OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
  2597. "ms to wait before starting measurement after program "
  2598. "start"),
  2599. OPT_END()
  2600. };
  2601. bool __maybe_unused max_stack_user_set = true;
  2602. bool mmap_pages_user_set = true;
  2603. const char * const trace_subcommands[] = { "record", NULL };
  2604. int err;
  2605. char bf[BUFSIZ];
  2606. signal(SIGSEGV, sighandler_dump_stack);
  2607. signal(SIGFPE, sighandler_dump_stack);
  2608. trace.evlist = perf_evlist__new();
  2609. trace.sctbl = syscalltbl__new();
  2610. if (trace.evlist == NULL || trace.sctbl == NULL) {
  2611. pr_err("Not enough memory to run!\n");
  2612. err = -ENOMEM;
  2613. goto out;
  2614. }
  2615. argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
  2616. trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  2617. if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
  2618. usage_with_options_msg(trace_usage, trace_options,
  2619. "cgroup monitoring only available in system-wide mode");
  2620. }
  2621. err = bpf__setup_stdout(trace.evlist);
  2622. if (err) {
  2623. bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
  2624. pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
  2625. goto out;
  2626. }
  2627. err = -1;
  2628. if (trace.trace_pgfaults) {
  2629. trace.opts.sample_address = true;
  2630. trace.opts.sample_time = true;
  2631. }
  2632. if (trace.opts.mmap_pages == UINT_MAX)
  2633. mmap_pages_user_set = false;
  2634. if (trace.max_stack == UINT_MAX) {
  2635. trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
  2636. max_stack_user_set = false;
  2637. }
  2638. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  2639. if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
  2640. record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
  2641. }
  2642. #endif
  2643. if (callchain_param.enabled) {
  2644. if (!mmap_pages_user_set && geteuid() == 0)
  2645. trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
  2646. symbol_conf.use_callchain = true;
  2647. }
  2648. if (trace.evlist->nr_entries > 0)
  2649. evlist__set_evsel_handler(trace.evlist, trace__event_handler);
  2650. if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
  2651. return trace__record(&trace, argc-1, &argv[1]);
  2652. /* summary_only implies summary option, but don't overwrite summary if set */
  2653. if (trace.summary_only)
  2654. trace.summary = trace.summary_only;
  2655. if (!trace.trace_syscalls && !trace.trace_pgfaults &&
  2656. trace.evlist->nr_entries == 0 /* Was --events used? */) {
  2657. trace.trace_syscalls = true;
  2658. }
  2659. if (output_name != NULL) {
  2660. err = trace__open_output(&trace, output_name);
  2661. if (err < 0) {
  2662. perror("failed to create output file");
  2663. goto out;
  2664. }
  2665. }
  2666. trace.open_id = syscalltbl__id(trace.sctbl, "open");
  2667. err = target__validate(&trace.opts.target);
  2668. if (err) {
  2669. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2670. fprintf(trace.output, "%s", bf);
  2671. goto out_close;
  2672. }
  2673. err = target__parse_uid(&trace.opts.target);
  2674. if (err) {
  2675. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2676. fprintf(trace.output, "%s", bf);
  2677. goto out_close;
  2678. }
  2679. if (!argc && target__none(&trace.opts.target))
  2680. trace.opts.target.system_wide = true;
  2681. if (input_name)
  2682. err = trace__replay(&trace);
  2683. else
  2684. err = trace__run(&trace, argc, argv);
  2685. out_close:
  2686. if (output_name != NULL)
  2687. fclose(trace.output);
  2688. out:
  2689. return err;
  2690. }