builtin-trace.c 87 KB

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