builtin-trace.c 97 KB

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