builtin-trace.c 83 KB

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