builtin-trace.c 86 KB

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