builtin-trace.c 94 KB

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