builtin-trace.c 86 KB

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