builtin-trace.c 101 KB

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