builtin-trace.c 87 KB

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