builtin-trace.c 88 KB

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