builtin-trace.c 88 KB

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