builtin-trace.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  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. /* The standard mmap maps to old_mmap on s390x */
  586. #if defined(__s390x__)
  587. .alias = "old_mmap",
  588. #endif
  589. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  590. [2] = SCA_MMAP_PROT, /* prot */
  591. [3] = SCA_MMAP_FLAGS, /* flags */ }, },
  592. { .name = "mprotect", .errmsg = true,
  593. .arg_scnprintf = { [0] = SCA_HEX, /* start */
  594. [2] = SCA_MMAP_PROT, /* prot */ }, },
  595. { .name = "mq_unlink", .errmsg = true,
  596. .arg_scnprintf = { [0] = SCA_FILENAME, /* u_name */ }, },
  597. { .name = "mremap", .hexret = true,
  598. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  599. [3] = SCA_MREMAP_FLAGS, /* flags */
  600. [4] = SCA_HEX, /* new_addr */ }, },
  601. { .name = "munlock", .errmsg = true,
  602. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  603. { .name = "munmap", .errmsg = true,
  604. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  605. { .name = "name_to_handle_at", .errmsg = true,
  606. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  607. { .name = "newfstatat", .errmsg = true,
  608. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  609. { .name = "open", .errmsg = true,
  610. .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
  611. { .name = "open_by_handle_at", .errmsg = true,
  612. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
  613. [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  614. { .name = "openat", .errmsg = true,
  615. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
  616. [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  617. { .name = "perf_event_open", .errmsg = true,
  618. .arg_scnprintf = { [2] = SCA_INT, /* cpu */
  619. [3] = SCA_FD, /* group_fd */
  620. [4] = SCA_PERF_FLAGS, /* flags */ }, },
  621. { .name = "pipe2", .errmsg = true,
  622. .arg_scnprintf = { [1] = SCA_PIPE_FLAGS, /* flags */ }, },
  623. { .name = "poll", .errmsg = true, .timeout = true, },
  624. { .name = "ppoll", .errmsg = true, .timeout = true, },
  625. { .name = "pread", .errmsg = true, .alias = "pread64", },
  626. { .name = "preadv", .errmsg = true, .alias = "pread", },
  627. { .name = "prlimit64", .errmsg = true, STRARRAY(1, resource, rlimit_resources), },
  628. { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
  629. { .name = "pwritev", .errmsg = true, },
  630. { .name = "read", .errmsg = true, },
  631. { .name = "readlink", .errmsg = true, },
  632. { .name = "readlinkat", .errmsg = true,
  633. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  634. { .name = "readv", .errmsg = true, },
  635. { .name = "recvfrom", .errmsg = true,
  636. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  637. { .name = "recvmmsg", .errmsg = true,
  638. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  639. { .name = "recvmsg", .errmsg = true,
  640. .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
  641. { .name = "removexattr", .errmsg = true, },
  642. { .name = "renameat", .errmsg = true,
  643. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  644. { .name = "rmdir", .errmsg = true, },
  645. { .name = "rt_sigaction", .errmsg = true,
  646. .arg_scnprintf = { [0] = SCA_SIGNUM, /* sig */ }, },
  647. { .name = "rt_sigprocmask", .errmsg = true, STRARRAY(0, how, sighow), },
  648. { .name = "rt_sigqueueinfo", .errmsg = true,
  649. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  650. { .name = "rt_tgsigqueueinfo", .errmsg = true,
  651. .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
  652. { .name = "sched_getattr", .errmsg = true, },
  653. { .name = "sched_setattr", .errmsg = true, },
  654. { .name = "sched_setscheduler", .errmsg = true,
  655. .arg_scnprintf = { [1] = SCA_SCHED_POLICY, /* policy */ }, },
  656. { .name = "seccomp", .errmsg = true,
  657. .arg_scnprintf = { [0] = SCA_SECCOMP_OP, /* op */
  658. [1] = SCA_SECCOMP_FLAGS, /* flags */ }, },
  659. { .name = "select", .errmsg = true, .timeout = true, },
  660. { .name = "sendmmsg", .errmsg = true,
  661. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  662. { .name = "sendmsg", .errmsg = true,
  663. .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
  664. { .name = "sendto", .errmsg = true,
  665. .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
  666. { .name = "set_tid_address", .errpid = true, },
  667. { .name = "setitimer", .errmsg = true, STRARRAY(0, which, itimers), },
  668. { .name = "setpgid", .errmsg = true, },
  669. { .name = "setrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
  670. { .name = "setxattr", .errmsg = true, },
  671. { .name = "shutdown", .errmsg = true, },
  672. { .name = "socket", .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 = "socketpair", .errmsg = true,
  677. .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
  678. [1] = SCA_SK_TYPE, /* type */ },
  679. .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
  680. { .name = "stat", .errmsg = true, .alias = "newstat", },
  681. { .name = "statfs", .errmsg = true, },
  682. { .name = "swapoff", .errmsg = true,
  683. .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, },
  684. { .name = "swapon", .errmsg = true,
  685. .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, },
  686. { .name = "symlinkat", .errmsg = true,
  687. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  688. { .name = "tgkill", .errmsg = true,
  689. .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
  690. { .name = "tkill", .errmsg = true,
  691. .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
  692. { .name = "truncate", .errmsg = true, },
  693. { .name = "uname", .errmsg = true, .alias = "newuname", },
  694. { .name = "unlinkat", .errmsg = true,
  695. .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
  696. { .name = "utime", .errmsg = true, },
  697. { .name = "utimensat", .errmsg = true,
  698. .arg_scnprintf = { [0] = SCA_FDAT, /* dirfd */ }, },
  699. { .name = "utimes", .errmsg = true, },
  700. { .name = "vmsplice", .errmsg = true, },
  701. { .name = "wait4", .errpid = true,
  702. .arg_scnprintf = { [2] = SCA_WAITID_OPTIONS, /* options */ }, },
  703. { .name = "waitid", .errpid = true,
  704. .arg_scnprintf = { [3] = SCA_WAITID_OPTIONS, /* options */ }, },
  705. { .name = "write", .errmsg = true, },
  706. { .name = "writev", .errmsg = true, },
  707. };
  708. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  709. {
  710. const struct syscall_fmt *fmt = fmtp;
  711. return strcmp(name, fmt->name);
  712. }
  713. static struct syscall_fmt *syscall_fmt__find(const char *name)
  714. {
  715. const int nmemb = ARRAY_SIZE(syscall_fmts);
  716. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  717. }
  718. struct syscall {
  719. struct event_format *tp_format;
  720. int nr_args;
  721. struct format_field *args;
  722. const char *name;
  723. bool is_exit;
  724. struct syscall_fmt *fmt;
  725. size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  726. void **arg_parm;
  727. };
  728. /*
  729. * We need to have this 'calculated' boolean because in some cases we really
  730. * don't know what is the duration of a syscall, for instance, when we start
  731. * a session and some threads are waiting for a syscall to finish, say 'poll',
  732. * in which case all we can do is to print "( ? ) for duration and for the
  733. * start timestamp.
  734. */
  735. static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
  736. {
  737. double duration = (double)t / NSEC_PER_MSEC;
  738. size_t printed = fprintf(fp, "(");
  739. if (!calculated)
  740. printed += fprintf(fp, " ? ");
  741. else if (duration >= 1.0)
  742. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  743. else if (duration >= 0.01)
  744. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  745. else
  746. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  747. return printed + fprintf(fp, "): ");
  748. }
  749. /**
  750. * filename.ptr: The filename char pointer that will be vfs_getname'd
  751. * filename.entry_str_pos: Where to insert the string translated from
  752. * filename.ptr by the vfs_getname tracepoint/kprobe.
  753. */
  754. struct thread_trace {
  755. u64 entry_time;
  756. u64 exit_time;
  757. bool entry_pending;
  758. unsigned long nr_events;
  759. unsigned long pfmaj, pfmin;
  760. char *entry_str;
  761. double runtime_ms;
  762. struct {
  763. unsigned long ptr;
  764. short int entry_str_pos;
  765. bool pending_open;
  766. unsigned int namelen;
  767. char *name;
  768. } filename;
  769. struct {
  770. int max;
  771. char **table;
  772. } paths;
  773. struct intlist *syscall_stats;
  774. };
  775. static struct thread_trace *thread_trace__new(void)
  776. {
  777. struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
  778. if (ttrace)
  779. ttrace->paths.max = -1;
  780. ttrace->syscall_stats = intlist__new(NULL);
  781. return ttrace;
  782. }
  783. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  784. {
  785. struct thread_trace *ttrace;
  786. if (thread == NULL)
  787. goto fail;
  788. if (thread__priv(thread) == NULL)
  789. thread__set_priv(thread, thread_trace__new());
  790. if (thread__priv(thread) == NULL)
  791. goto fail;
  792. ttrace = thread__priv(thread);
  793. ++ttrace->nr_events;
  794. return ttrace;
  795. fail:
  796. color_fprintf(fp, PERF_COLOR_RED,
  797. "WARNING: not enough memory, dropping samples!\n");
  798. return NULL;
  799. }
  800. #define TRACE_PFMAJ (1 << 0)
  801. #define TRACE_PFMIN (1 << 1)
  802. static const size_t trace__entry_str_size = 2048;
  803. static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
  804. {
  805. struct thread_trace *ttrace = thread__priv(thread);
  806. if (fd > ttrace->paths.max) {
  807. char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
  808. if (npath == NULL)
  809. return -1;
  810. if (ttrace->paths.max != -1) {
  811. memset(npath + ttrace->paths.max + 1, 0,
  812. (fd - ttrace->paths.max) * sizeof(char *));
  813. } else {
  814. memset(npath, 0, (fd + 1) * sizeof(char *));
  815. }
  816. ttrace->paths.table = npath;
  817. ttrace->paths.max = fd;
  818. }
  819. ttrace->paths.table[fd] = strdup(pathname);
  820. return ttrace->paths.table[fd] != NULL ? 0 : -1;
  821. }
  822. static int thread__read_fd_path(struct thread *thread, int fd)
  823. {
  824. char linkname[PATH_MAX], pathname[PATH_MAX];
  825. struct stat st;
  826. int ret;
  827. if (thread->pid_ == thread->tid) {
  828. scnprintf(linkname, sizeof(linkname),
  829. "/proc/%d/fd/%d", thread->pid_, fd);
  830. } else {
  831. scnprintf(linkname, sizeof(linkname),
  832. "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
  833. }
  834. if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
  835. return -1;
  836. ret = readlink(linkname, pathname, sizeof(pathname));
  837. if (ret < 0 || ret > st.st_size)
  838. return -1;
  839. pathname[ret] = '\0';
  840. return trace__set_fd_pathname(thread, fd, pathname);
  841. }
  842. static const char *thread__fd_path(struct thread *thread, int fd,
  843. struct trace *trace)
  844. {
  845. struct thread_trace *ttrace = thread__priv(thread);
  846. if (ttrace == NULL)
  847. return NULL;
  848. if (fd < 0)
  849. return NULL;
  850. if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
  851. if (!trace->live)
  852. return NULL;
  853. ++trace->stats.proc_getname;
  854. if (thread__read_fd_path(thread, fd))
  855. return NULL;
  856. }
  857. return ttrace->paths.table[fd];
  858. }
  859. static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
  860. struct syscall_arg *arg)
  861. {
  862. int fd = arg->val;
  863. size_t printed = scnprintf(bf, size, "%d", fd);
  864. const char *path = thread__fd_path(arg->thread, fd, arg->trace);
  865. if (path)
  866. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  867. return printed;
  868. }
  869. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  870. struct syscall_arg *arg)
  871. {
  872. int fd = arg->val;
  873. size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
  874. struct thread_trace *ttrace = thread__priv(arg->thread);
  875. if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
  876. zfree(&ttrace->paths.table[fd]);
  877. return printed;
  878. }
  879. static void thread__set_filename_pos(struct thread *thread, const char *bf,
  880. unsigned long ptr)
  881. {
  882. struct thread_trace *ttrace = thread__priv(thread);
  883. ttrace->filename.ptr = ptr;
  884. ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
  885. }
  886. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  887. struct syscall_arg *arg)
  888. {
  889. unsigned long ptr = arg->val;
  890. if (!arg->trace->vfs_getname)
  891. return scnprintf(bf, size, "%#x", ptr);
  892. thread__set_filename_pos(arg->thread, bf, ptr);
  893. return 0;
  894. }
  895. static bool trace__filter_duration(struct trace *trace, double t)
  896. {
  897. return t < (trace->duration_filter * NSEC_PER_MSEC);
  898. }
  899. static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  900. {
  901. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  902. return fprintf(fp, "%10.3f ", ts);
  903. }
  904. /*
  905. * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
  906. * using ttrace->entry_time for a thread that receives a sys_exit without
  907. * first having received a sys_enter ("poll" issued before tracing session
  908. * starts, lost sys_enter exit due to ring buffer overflow).
  909. */
  910. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  911. {
  912. if (tstamp > 0)
  913. return __trace__fprintf_tstamp(trace, tstamp, fp);
  914. return fprintf(fp, " ? ");
  915. }
  916. static bool done = false;
  917. static bool interrupted = false;
  918. static void sig_handler(int sig)
  919. {
  920. done = true;
  921. interrupted = sig == SIGINT;
  922. }
  923. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  924. u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
  925. {
  926. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  927. printed += fprintf_duration(duration, duration_calculated, fp);
  928. if (trace->multiple_threads) {
  929. if (trace->show_comm)
  930. printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
  931. printed += fprintf(fp, "%d ", thread->tid);
  932. }
  933. return printed;
  934. }
  935. static int trace__process_event(struct trace *trace, struct machine *machine,
  936. union perf_event *event, struct perf_sample *sample)
  937. {
  938. int ret = 0;
  939. switch (event->header.type) {
  940. case PERF_RECORD_LOST:
  941. color_fprintf(trace->output, PERF_COLOR_RED,
  942. "LOST %" PRIu64 " events!\n", event->lost.lost);
  943. ret = machine__process_lost_event(machine, event, sample);
  944. break;
  945. default:
  946. ret = machine__process_event(machine, event, sample);
  947. break;
  948. }
  949. return ret;
  950. }
  951. static int trace__tool_process(struct perf_tool *tool,
  952. union perf_event *event,
  953. struct perf_sample *sample,
  954. struct machine *machine)
  955. {
  956. struct trace *trace = container_of(tool, struct trace, tool);
  957. return trace__process_event(trace, machine, event, sample);
  958. }
  959. static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  960. {
  961. struct machine *machine = vmachine;
  962. if (machine->kptr_restrict_warned)
  963. return NULL;
  964. if (symbol_conf.kptr_restrict) {
  965. pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  966. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  967. "Kernel samples will not be resolved.\n");
  968. machine->kptr_restrict_warned = true;
  969. return NULL;
  970. }
  971. return machine__resolve_kernel_addr(vmachine, addrp, modp);
  972. }
  973. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  974. {
  975. int err = symbol__init(NULL);
  976. if (err)
  977. return err;
  978. trace->host = machine__new_host();
  979. if (trace->host == NULL)
  980. return -ENOMEM;
  981. if (trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr) < 0)
  982. return -errno;
  983. err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  984. evlist->threads, trace__tool_process, false,
  985. trace->opts.proc_map_timeout);
  986. if (err)
  987. symbol__exit();
  988. return err;
  989. }
  990. static int syscall__set_arg_fmts(struct syscall *sc)
  991. {
  992. struct format_field *field;
  993. int idx = 0, len;
  994. sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *));
  995. if (sc->arg_scnprintf == NULL)
  996. return -1;
  997. if (sc->fmt)
  998. sc->arg_parm = sc->fmt->arg_parm;
  999. for (field = sc->args; field; field = field->next) {
  1000. if (sc->fmt && sc->fmt->arg_scnprintf[idx])
  1001. sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
  1002. else if (strcmp(field->type, "const char *") == 0 &&
  1003. (strcmp(field->name, "filename") == 0 ||
  1004. strcmp(field->name, "path") == 0 ||
  1005. strcmp(field->name, "pathname") == 0))
  1006. sc->arg_scnprintf[idx] = SCA_FILENAME;
  1007. else if (field->flags & FIELD_IS_POINTER)
  1008. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  1009. else if (strcmp(field->type, "pid_t") == 0)
  1010. sc->arg_scnprintf[idx] = SCA_PID;
  1011. else if (strcmp(field->type, "umode_t") == 0)
  1012. sc->arg_scnprintf[idx] = SCA_MODE_T;
  1013. else if ((strcmp(field->type, "int") == 0 ||
  1014. strcmp(field->type, "unsigned int") == 0 ||
  1015. strcmp(field->type, "long") == 0) &&
  1016. (len = strlen(field->name)) >= 2 &&
  1017. strcmp(field->name + len - 2, "fd") == 0) {
  1018. /*
  1019. * /sys/kernel/tracing/events/syscalls/sys_enter*
  1020. * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
  1021. * 65 int
  1022. * 23 unsigned int
  1023. * 7 unsigned long
  1024. */
  1025. sc->arg_scnprintf[idx] = SCA_FD;
  1026. }
  1027. ++idx;
  1028. }
  1029. return 0;
  1030. }
  1031. static int trace__read_syscall_info(struct trace *trace, int id)
  1032. {
  1033. char tp_name[128];
  1034. struct syscall *sc;
  1035. const char *name = syscalltbl__name(trace->sctbl, id);
  1036. if (name == NULL)
  1037. return -1;
  1038. if (id > trace->syscalls.max) {
  1039. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  1040. if (nsyscalls == NULL)
  1041. return -1;
  1042. if (trace->syscalls.max != -1) {
  1043. memset(nsyscalls + trace->syscalls.max + 1, 0,
  1044. (id - trace->syscalls.max) * sizeof(*sc));
  1045. } else {
  1046. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  1047. }
  1048. trace->syscalls.table = nsyscalls;
  1049. trace->syscalls.max = id;
  1050. }
  1051. sc = trace->syscalls.table + id;
  1052. sc->name = name;
  1053. sc->fmt = syscall_fmt__find(sc->name);
  1054. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1055. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1056. if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
  1057. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1058. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1059. }
  1060. if (IS_ERR(sc->tp_format))
  1061. return -1;
  1062. sc->args = sc->tp_format->format.fields;
  1063. sc->nr_args = sc->tp_format->format.nr_fields;
  1064. /*
  1065. * We need to check and discard the first variable '__syscall_nr'
  1066. * or 'nr' that mean the syscall number. It is needless here.
  1067. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
  1068. */
  1069. if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
  1070. sc->args = sc->args->next;
  1071. --sc->nr_args;
  1072. }
  1073. sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
  1074. return syscall__set_arg_fmts(sc);
  1075. }
  1076. static int trace__validate_ev_qualifier(struct trace *trace)
  1077. {
  1078. int err = 0, i;
  1079. struct str_node *pos;
  1080. trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
  1081. trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
  1082. sizeof(trace->ev_qualifier_ids.entries[0]));
  1083. if (trace->ev_qualifier_ids.entries == NULL) {
  1084. fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
  1085. trace->output);
  1086. err = -EINVAL;
  1087. goto out;
  1088. }
  1089. i = 0;
  1090. strlist__for_each_entry(pos, trace->ev_qualifier) {
  1091. const char *sc = pos->s;
  1092. int id = syscalltbl__id(trace->sctbl, sc);
  1093. if (id < 0) {
  1094. if (err == 0) {
  1095. fputs("Error:\tInvalid syscall ", trace->output);
  1096. err = -EINVAL;
  1097. } else {
  1098. fputs(", ", trace->output);
  1099. }
  1100. fputs(sc, trace->output);
  1101. }
  1102. trace->ev_qualifier_ids.entries[i++] = id;
  1103. }
  1104. if (err < 0) {
  1105. fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
  1106. "\nHint:\tand: 'man syscalls'\n", trace->output);
  1107. zfree(&trace->ev_qualifier_ids.entries);
  1108. trace->ev_qualifier_ids.nr = 0;
  1109. }
  1110. out:
  1111. return err;
  1112. }
  1113. /*
  1114. * args is to be interpreted as a series of longs but we need to handle
  1115. * 8-byte unaligned accesses. args points to raw_data within the event
  1116. * and raw_data is guaranteed to be 8-byte unaligned because it is
  1117. * preceded by raw_size which is a u32. So we need to copy args to a temp
  1118. * variable to read it. Most notably this avoids extended load instructions
  1119. * on unaligned addresses
  1120. */
  1121. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  1122. unsigned char *args, struct trace *trace,
  1123. struct thread *thread)
  1124. {
  1125. size_t printed = 0;
  1126. unsigned char *p;
  1127. unsigned long val;
  1128. if (sc->args != NULL) {
  1129. struct format_field *field;
  1130. u8 bit = 1;
  1131. struct syscall_arg arg = {
  1132. .idx = 0,
  1133. .mask = 0,
  1134. .trace = trace,
  1135. .thread = thread,
  1136. };
  1137. for (field = sc->args; field;
  1138. field = field->next, ++arg.idx, bit <<= 1) {
  1139. if (arg.mask & bit)
  1140. continue;
  1141. /* special care for unaligned accesses */
  1142. p = args + sizeof(unsigned long) * arg.idx;
  1143. memcpy(&val, p, sizeof(val));
  1144. /*
  1145. * Suppress this argument if its value is zero and
  1146. * and we don't have a string associated in an
  1147. * strarray for it.
  1148. */
  1149. if (val == 0 &&
  1150. !(sc->arg_scnprintf &&
  1151. sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
  1152. sc->arg_parm[arg.idx]))
  1153. continue;
  1154. printed += scnprintf(bf + printed, size - printed,
  1155. "%s%s: ", printed ? ", " : "", field->name);
  1156. if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
  1157. arg.val = val;
  1158. if (sc->arg_parm)
  1159. arg.parm = sc->arg_parm[arg.idx];
  1160. printed += sc->arg_scnprintf[arg.idx](bf + printed,
  1161. size - printed, &arg);
  1162. } else {
  1163. printed += scnprintf(bf + printed, size - printed,
  1164. "%ld", val);
  1165. }
  1166. }
  1167. } else if (IS_ERR(sc->tp_format)) {
  1168. /*
  1169. * If we managed to read the tracepoint /format file, then we
  1170. * may end up not having any args, like with gettid(), so only
  1171. * print the raw args when we didn't manage to read it.
  1172. */
  1173. int i = 0;
  1174. while (i < 6) {
  1175. /* special care for unaligned accesses */
  1176. p = args + sizeof(unsigned long) * i;
  1177. memcpy(&val, p, sizeof(val));
  1178. printed += scnprintf(bf + printed, size - printed,
  1179. "%sarg%d: %ld",
  1180. printed ? ", " : "", i, val);
  1181. ++i;
  1182. }
  1183. }
  1184. return printed;
  1185. }
  1186. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  1187. union perf_event *event,
  1188. struct perf_sample *sample);
  1189. static struct syscall *trace__syscall_info(struct trace *trace,
  1190. struct perf_evsel *evsel, int id)
  1191. {
  1192. if (id < 0) {
  1193. /*
  1194. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  1195. * before that, leaving at a higher verbosity level till that is
  1196. * explained. Reproduced with plain ftrace with:
  1197. *
  1198. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  1199. * grep "NR -1 " /t/trace_pipe
  1200. *
  1201. * After generating some load on the machine.
  1202. */
  1203. if (verbose > 1) {
  1204. static u64 n;
  1205. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  1206. id, perf_evsel__name(evsel), ++n);
  1207. }
  1208. return NULL;
  1209. }
  1210. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  1211. trace__read_syscall_info(trace, id))
  1212. goto out_cant_read;
  1213. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  1214. goto out_cant_read;
  1215. return &trace->syscalls.table[id];
  1216. out_cant_read:
  1217. if (verbose) {
  1218. fprintf(trace->output, "Problems reading syscall %d", id);
  1219. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  1220. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  1221. fputs(" information\n", trace->output);
  1222. }
  1223. return NULL;
  1224. }
  1225. static void thread__update_stats(struct thread_trace *ttrace,
  1226. int id, struct perf_sample *sample)
  1227. {
  1228. struct int_node *inode;
  1229. struct stats *stats;
  1230. u64 duration = 0;
  1231. inode = intlist__findnew(ttrace->syscall_stats, id);
  1232. if (inode == NULL)
  1233. return;
  1234. stats = inode->priv;
  1235. if (stats == NULL) {
  1236. stats = malloc(sizeof(struct stats));
  1237. if (stats == NULL)
  1238. return;
  1239. init_stats(stats);
  1240. inode->priv = stats;
  1241. }
  1242. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  1243. duration = sample->time - ttrace->entry_time;
  1244. update_stats(stats, duration);
  1245. }
  1246. static int trace__printf_interrupted_entry(struct trace *trace, struct perf_sample *sample)
  1247. {
  1248. struct thread_trace *ttrace;
  1249. u64 duration;
  1250. size_t printed;
  1251. if (trace->current == NULL)
  1252. return 0;
  1253. ttrace = thread__priv(trace->current);
  1254. if (!ttrace->entry_pending)
  1255. return 0;
  1256. duration = sample->time - ttrace->entry_time;
  1257. printed = trace__fprintf_entry_head(trace, trace->current, duration, true, ttrace->entry_time, trace->output);
  1258. printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
  1259. ttrace->entry_pending = false;
  1260. return printed;
  1261. }
  1262. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  1263. union perf_event *event __maybe_unused,
  1264. struct perf_sample *sample)
  1265. {
  1266. char *msg;
  1267. void *args;
  1268. size_t printed = 0;
  1269. struct thread *thread;
  1270. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1271. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1272. struct thread_trace *ttrace;
  1273. if (sc == NULL)
  1274. return -1;
  1275. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1276. ttrace = thread__trace(thread, trace->output);
  1277. if (ttrace == NULL)
  1278. goto out_put;
  1279. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1280. if (ttrace->entry_str == NULL) {
  1281. ttrace->entry_str = malloc(trace__entry_str_size);
  1282. if (!ttrace->entry_str)
  1283. goto out_put;
  1284. }
  1285. if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
  1286. trace__printf_interrupted_entry(trace, sample);
  1287. ttrace->entry_time = sample->time;
  1288. msg = ttrace->entry_str;
  1289. printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
  1290. printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
  1291. args, trace, thread);
  1292. if (sc->is_exit) {
  1293. if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) {
  1294. trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
  1295. fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
  1296. }
  1297. } else {
  1298. ttrace->entry_pending = true;
  1299. /* See trace__vfs_getname & trace__sys_exit */
  1300. ttrace->filename.pending_open = false;
  1301. }
  1302. if (trace->current != thread) {
  1303. thread__put(trace->current);
  1304. trace->current = thread__get(thread);
  1305. }
  1306. err = 0;
  1307. out_put:
  1308. thread__put(thread);
  1309. return err;
  1310. }
  1311. static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
  1312. struct perf_sample *sample,
  1313. struct callchain_cursor *cursor)
  1314. {
  1315. struct addr_location al;
  1316. if (machine__resolve(trace->host, &al, sample) < 0 ||
  1317. thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, trace->max_stack))
  1318. return -1;
  1319. return 0;
  1320. }
  1321. static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
  1322. {
  1323. /* TODO: user-configurable print_opts */
  1324. const unsigned int print_opts = EVSEL__PRINT_SYM |
  1325. EVSEL__PRINT_DSO |
  1326. EVSEL__PRINT_UNKNOWN_AS_ADDR;
  1327. return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
  1328. }
  1329. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  1330. union perf_event *event __maybe_unused,
  1331. struct perf_sample *sample)
  1332. {
  1333. long ret;
  1334. u64 duration = 0;
  1335. bool duration_calculated = false;
  1336. struct thread *thread;
  1337. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
  1338. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1339. struct thread_trace *ttrace;
  1340. if (sc == NULL)
  1341. return -1;
  1342. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1343. ttrace = thread__trace(thread, trace->output);
  1344. if (ttrace == NULL)
  1345. goto out_put;
  1346. if (trace->summary)
  1347. thread__update_stats(ttrace, id, sample);
  1348. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  1349. if (id == trace->open_id && ret >= 0 && ttrace->filename.pending_open) {
  1350. trace__set_fd_pathname(thread, ret, ttrace->filename.name);
  1351. ttrace->filename.pending_open = false;
  1352. ++trace->stats.vfs_getname;
  1353. }
  1354. ttrace->exit_time = sample->time;
  1355. if (ttrace->entry_time) {
  1356. duration = sample->time - ttrace->entry_time;
  1357. if (trace__filter_duration(trace, duration))
  1358. goto out;
  1359. duration_calculated = true;
  1360. } else if (trace->duration_filter)
  1361. goto out;
  1362. if (sample->callchain) {
  1363. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1364. if (callchain_ret == 0) {
  1365. if (callchain_cursor.nr < trace->min_stack)
  1366. goto out;
  1367. callchain_ret = 1;
  1368. }
  1369. }
  1370. if (trace->summary_only)
  1371. goto out;
  1372. trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
  1373. if (ttrace->entry_pending) {
  1374. fprintf(trace->output, "%-70s", ttrace->entry_str);
  1375. } else {
  1376. fprintf(trace->output, " ... [");
  1377. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  1378. fprintf(trace->output, "]: %s()", sc->name);
  1379. }
  1380. if (sc->fmt == NULL) {
  1381. signed_print:
  1382. fprintf(trace->output, ") = %ld", ret);
  1383. } else if (ret < 0 && (sc->fmt->errmsg || sc->fmt->errpid)) {
  1384. char bf[STRERR_BUFSIZE];
  1385. const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
  1386. *e = audit_errno_to_name(-ret);
  1387. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  1388. } else if (ret == 0 && sc->fmt->timeout)
  1389. fprintf(trace->output, ") = 0 Timeout");
  1390. else if (sc->fmt->hexret)
  1391. fprintf(trace->output, ") = %#lx", ret);
  1392. else if (sc->fmt->errpid) {
  1393. struct thread *child = machine__find_thread(trace->host, ret, ret);
  1394. if (child != NULL) {
  1395. fprintf(trace->output, ") = %ld", ret);
  1396. if (child->comm_set)
  1397. fprintf(trace->output, " (%s)", thread__comm_str(child));
  1398. thread__put(child);
  1399. }
  1400. } else
  1401. goto signed_print;
  1402. fputc('\n', trace->output);
  1403. if (callchain_ret > 0)
  1404. trace__fprintf_callchain(trace, sample);
  1405. else if (callchain_ret < 0)
  1406. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1407. out:
  1408. ttrace->entry_pending = false;
  1409. err = 0;
  1410. out_put:
  1411. thread__put(thread);
  1412. return err;
  1413. }
  1414. static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
  1415. union perf_event *event __maybe_unused,
  1416. struct perf_sample *sample)
  1417. {
  1418. struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1419. struct thread_trace *ttrace;
  1420. size_t filename_len, entry_str_len, to_move;
  1421. ssize_t remaining_space;
  1422. char *pos;
  1423. const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
  1424. if (!thread)
  1425. goto out;
  1426. ttrace = thread__priv(thread);
  1427. if (!ttrace)
  1428. goto out;
  1429. filename_len = strlen(filename);
  1430. if (ttrace->filename.namelen < filename_len) {
  1431. char *f = realloc(ttrace->filename.name, filename_len + 1);
  1432. if (f == NULL)
  1433. goto out;
  1434. ttrace->filename.namelen = filename_len;
  1435. ttrace->filename.name = f;
  1436. }
  1437. strcpy(ttrace->filename.name, filename);
  1438. ttrace->filename.pending_open = true;
  1439. if (!ttrace->filename.ptr)
  1440. goto out;
  1441. entry_str_len = strlen(ttrace->entry_str);
  1442. remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
  1443. if (remaining_space <= 0)
  1444. goto out;
  1445. if (filename_len > (size_t)remaining_space) {
  1446. filename += filename_len - remaining_space;
  1447. filename_len = remaining_space;
  1448. }
  1449. to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
  1450. pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
  1451. memmove(pos + filename_len, pos, to_move);
  1452. memcpy(pos, filename, filename_len);
  1453. ttrace->filename.ptr = 0;
  1454. ttrace->filename.entry_str_pos = 0;
  1455. out:
  1456. return 0;
  1457. }
  1458. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  1459. union perf_event *event __maybe_unused,
  1460. struct perf_sample *sample)
  1461. {
  1462. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  1463. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  1464. struct thread *thread = machine__findnew_thread(trace->host,
  1465. sample->pid,
  1466. sample->tid);
  1467. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  1468. if (ttrace == NULL)
  1469. goto out_dump;
  1470. ttrace->runtime_ms += runtime_ms;
  1471. trace->runtime_ms += runtime_ms;
  1472. thread__put(thread);
  1473. return 0;
  1474. out_dump:
  1475. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  1476. evsel->name,
  1477. perf_evsel__strval(evsel, sample, "comm"),
  1478. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  1479. runtime,
  1480. perf_evsel__intval(evsel, sample, "vruntime"));
  1481. thread__put(thread);
  1482. return 0;
  1483. }
  1484. static void bpf_output__printer(enum binary_printer_ops op,
  1485. unsigned int val, void *extra)
  1486. {
  1487. FILE *output = extra;
  1488. unsigned char ch = (unsigned char)val;
  1489. switch (op) {
  1490. case BINARY_PRINT_CHAR_DATA:
  1491. fprintf(output, "%c", isprint(ch) ? ch : '.');
  1492. break;
  1493. case BINARY_PRINT_DATA_BEGIN:
  1494. case BINARY_PRINT_LINE_BEGIN:
  1495. case BINARY_PRINT_ADDR:
  1496. case BINARY_PRINT_NUM_DATA:
  1497. case BINARY_PRINT_NUM_PAD:
  1498. case BINARY_PRINT_SEP:
  1499. case BINARY_PRINT_CHAR_PAD:
  1500. case BINARY_PRINT_LINE_END:
  1501. case BINARY_PRINT_DATA_END:
  1502. default:
  1503. break;
  1504. }
  1505. }
  1506. static void bpf_output__fprintf(struct trace *trace,
  1507. struct perf_sample *sample)
  1508. {
  1509. print_binary(sample->raw_data, sample->raw_size, 8,
  1510. bpf_output__printer, trace->output);
  1511. }
  1512. static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
  1513. union perf_event *event __maybe_unused,
  1514. struct perf_sample *sample)
  1515. {
  1516. int callchain_ret = 0;
  1517. if (sample->callchain) {
  1518. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1519. if (callchain_ret == 0) {
  1520. if (callchain_cursor.nr < trace->min_stack)
  1521. goto out;
  1522. callchain_ret = 1;
  1523. }
  1524. }
  1525. trace__printf_interrupted_entry(trace, sample);
  1526. trace__fprintf_tstamp(trace, sample->time, trace->output);
  1527. if (trace->trace_syscalls)
  1528. fprintf(trace->output, "( ): ");
  1529. fprintf(trace->output, "%s:", evsel->name);
  1530. if (perf_evsel__is_bpf_output(evsel)) {
  1531. bpf_output__fprintf(trace, sample);
  1532. } else if (evsel->tp_format) {
  1533. event_format__fprintf(evsel->tp_format, sample->cpu,
  1534. sample->raw_data, sample->raw_size,
  1535. trace->output);
  1536. }
  1537. fprintf(trace->output, ")\n");
  1538. if (callchain_ret > 0)
  1539. trace__fprintf_callchain(trace, sample);
  1540. else if (callchain_ret < 0)
  1541. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1542. out:
  1543. return 0;
  1544. }
  1545. static void print_location(FILE *f, struct perf_sample *sample,
  1546. struct addr_location *al,
  1547. bool print_dso, bool print_sym)
  1548. {
  1549. if ((verbose || print_dso) && al->map)
  1550. fprintf(f, "%s@", al->map->dso->long_name);
  1551. if ((verbose || print_sym) && al->sym)
  1552. fprintf(f, "%s+0x%" PRIx64, al->sym->name,
  1553. al->addr - al->sym->start);
  1554. else if (al->map)
  1555. fprintf(f, "0x%" PRIx64, al->addr);
  1556. else
  1557. fprintf(f, "0x%" PRIx64, sample->addr);
  1558. }
  1559. static int trace__pgfault(struct trace *trace,
  1560. struct perf_evsel *evsel,
  1561. union perf_event *event __maybe_unused,
  1562. struct perf_sample *sample)
  1563. {
  1564. struct thread *thread;
  1565. struct addr_location al;
  1566. char map_type = 'd';
  1567. struct thread_trace *ttrace;
  1568. int err = -1;
  1569. int callchain_ret = 0;
  1570. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1571. if (sample->callchain) {
  1572. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1573. if (callchain_ret == 0) {
  1574. if (callchain_cursor.nr < trace->min_stack)
  1575. goto out_put;
  1576. callchain_ret = 1;
  1577. }
  1578. }
  1579. ttrace = thread__trace(thread, trace->output);
  1580. if (ttrace == NULL)
  1581. goto out_put;
  1582. if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
  1583. ttrace->pfmaj++;
  1584. else
  1585. ttrace->pfmin++;
  1586. if (trace->summary_only)
  1587. goto out;
  1588. thread__find_addr_location(thread, sample->cpumode, MAP__FUNCTION,
  1589. sample->ip, &al);
  1590. trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
  1591. fprintf(trace->output, "%sfault [",
  1592. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
  1593. "maj" : "min");
  1594. print_location(trace->output, sample, &al, false, true);
  1595. fprintf(trace->output, "] => ");
  1596. thread__find_addr_location(thread, sample->cpumode, MAP__VARIABLE,
  1597. sample->addr, &al);
  1598. if (!al.map) {
  1599. thread__find_addr_location(thread, sample->cpumode,
  1600. MAP__FUNCTION, sample->addr, &al);
  1601. if (al.map)
  1602. map_type = 'x';
  1603. else
  1604. map_type = '?';
  1605. }
  1606. print_location(trace->output, sample, &al, true, false);
  1607. fprintf(trace->output, " (%c%c)\n", map_type, al.level);
  1608. if (callchain_ret > 0)
  1609. trace__fprintf_callchain(trace, sample);
  1610. else if (callchain_ret < 0)
  1611. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1612. out:
  1613. err = 0;
  1614. out_put:
  1615. thread__put(thread);
  1616. return err;
  1617. }
  1618. static bool skip_sample(struct trace *trace, struct perf_sample *sample)
  1619. {
  1620. if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
  1621. (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
  1622. return false;
  1623. if (trace->pid_list || trace->tid_list)
  1624. return true;
  1625. return false;
  1626. }
  1627. static void trace__set_base_time(struct trace *trace,
  1628. struct perf_evsel *evsel,
  1629. struct perf_sample *sample)
  1630. {
  1631. /*
  1632. * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
  1633. * and don't use sample->time unconditionally, we may end up having
  1634. * some other event in the future without PERF_SAMPLE_TIME for good
  1635. * reason, i.e. we may not be interested in its timestamps, just in
  1636. * it taking place, picking some piece of information when it
  1637. * appears in our event stream (vfs_getname comes to mind).
  1638. */
  1639. if (trace->base_time == 0 && !trace->full_time &&
  1640. (evsel->attr.sample_type & PERF_SAMPLE_TIME))
  1641. trace->base_time = sample->time;
  1642. }
  1643. static int trace__process_sample(struct perf_tool *tool,
  1644. union perf_event *event,
  1645. struct perf_sample *sample,
  1646. struct perf_evsel *evsel,
  1647. struct machine *machine __maybe_unused)
  1648. {
  1649. struct trace *trace = container_of(tool, struct trace, tool);
  1650. int err = 0;
  1651. tracepoint_handler handler = evsel->handler;
  1652. if (skip_sample(trace, sample))
  1653. return 0;
  1654. trace__set_base_time(trace, evsel, sample);
  1655. if (handler) {
  1656. ++trace->nr_events;
  1657. handler(trace, evsel, event, sample);
  1658. }
  1659. return err;
  1660. }
  1661. static int parse_target_str(struct trace *trace)
  1662. {
  1663. if (trace->opts.target.pid) {
  1664. trace->pid_list = intlist__new(trace->opts.target.pid);
  1665. if (trace->pid_list == NULL) {
  1666. pr_err("Error parsing process id string\n");
  1667. return -EINVAL;
  1668. }
  1669. }
  1670. if (trace->opts.target.tid) {
  1671. trace->tid_list = intlist__new(trace->opts.target.tid);
  1672. if (trace->tid_list == NULL) {
  1673. pr_err("Error parsing thread id string\n");
  1674. return -EINVAL;
  1675. }
  1676. }
  1677. return 0;
  1678. }
  1679. static int trace__record(struct trace *trace, int argc, const char **argv)
  1680. {
  1681. unsigned int rec_argc, i, j;
  1682. const char **rec_argv;
  1683. const char * const record_args[] = {
  1684. "record",
  1685. "-R",
  1686. "-m", "1024",
  1687. "-c", "1",
  1688. };
  1689. const char * const sc_args[] = { "-e", };
  1690. unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
  1691. const char * const majpf_args[] = { "-e", "major-faults" };
  1692. unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
  1693. const char * const minpf_args[] = { "-e", "minor-faults" };
  1694. unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
  1695. /* +1 is for the event string below */
  1696. rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
  1697. majpf_args_nr + minpf_args_nr + argc;
  1698. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1699. if (rec_argv == NULL)
  1700. return -ENOMEM;
  1701. j = 0;
  1702. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1703. rec_argv[j++] = record_args[i];
  1704. if (trace->trace_syscalls) {
  1705. for (i = 0; i < sc_args_nr; i++)
  1706. rec_argv[j++] = sc_args[i];
  1707. /* event string may be different for older kernels - e.g., RHEL6 */
  1708. if (is_valid_tracepoint("raw_syscalls:sys_enter"))
  1709. rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
  1710. else if (is_valid_tracepoint("syscalls:sys_enter"))
  1711. rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
  1712. else {
  1713. pr_err("Neither raw_syscalls nor syscalls events exist.\n");
  1714. return -1;
  1715. }
  1716. }
  1717. if (trace->trace_pgfaults & TRACE_PFMAJ)
  1718. for (i = 0; i < majpf_args_nr; i++)
  1719. rec_argv[j++] = majpf_args[i];
  1720. if (trace->trace_pgfaults & TRACE_PFMIN)
  1721. for (i = 0; i < minpf_args_nr; i++)
  1722. rec_argv[j++] = minpf_args[i];
  1723. for (i = 0; i < (unsigned int)argc; i++)
  1724. rec_argv[j++] = argv[i];
  1725. return cmd_record(j, rec_argv, NULL);
  1726. }
  1727. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  1728. static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
  1729. {
  1730. struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
  1731. if (IS_ERR(evsel))
  1732. return false;
  1733. if (perf_evsel__field(evsel, "pathname") == NULL) {
  1734. perf_evsel__delete(evsel);
  1735. return false;
  1736. }
  1737. evsel->handler = trace__vfs_getname;
  1738. perf_evlist__add(evlist, evsel);
  1739. return true;
  1740. }
  1741. static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
  1742. {
  1743. struct perf_evsel *evsel;
  1744. struct perf_event_attr attr = {
  1745. .type = PERF_TYPE_SOFTWARE,
  1746. .mmap_data = 1,
  1747. };
  1748. attr.config = config;
  1749. attr.sample_period = 1;
  1750. event_attr_init(&attr);
  1751. evsel = perf_evsel__new(&attr);
  1752. if (evsel)
  1753. evsel->handler = trace__pgfault;
  1754. return evsel;
  1755. }
  1756. static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
  1757. {
  1758. const u32 type = event->header.type;
  1759. struct perf_evsel *evsel;
  1760. if (type != PERF_RECORD_SAMPLE) {
  1761. trace__process_event(trace, trace->host, event, sample);
  1762. return;
  1763. }
  1764. evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
  1765. if (evsel == NULL) {
  1766. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
  1767. return;
  1768. }
  1769. trace__set_base_time(trace, evsel, sample);
  1770. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  1771. sample->raw_data == NULL) {
  1772. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  1773. perf_evsel__name(evsel), sample->tid,
  1774. sample->cpu, sample->raw_size);
  1775. } else {
  1776. tracepoint_handler handler = evsel->handler;
  1777. handler(trace, evsel, event, sample);
  1778. }
  1779. }
  1780. static int trace__add_syscall_newtp(struct trace *trace)
  1781. {
  1782. int ret = -1;
  1783. struct perf_evlist *evlist = trace->evlist;
  1784. struct perf_evsel *sys_enter, *sys_exit;
  1785. sys_enter = perf_evsel__syscall_newtp("sys_enter", trace__sys_enter);
  1786. if (sys_enter == NULL)
  1787. goto out;
  1788. if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
  1789. goto out_delete_sys_enter;
  1790. sys_exit = perf_evsel__syscall_newtp("sys_exit", trace__sys_exit);
  1791. if (sys_exit == NULL)
  1792. goto out_delete_sys_enter;
  1793. if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
  1794. goto out_delete_sys_exit;
  1795. perf_evlist__add(evlist, sys_enter);
  1796. perf_evlist__add(evlist, sys_exit);
  1797. if (callchain_param.enabled && !trace->kernel_syscallchains) {
  1798. /*
  1799. * We're interested only in the user space callchain
  1800. * leading to the syscall, allow overriding that for
  1801. * debugging reasons using --kernel_syscall_callchains
  1802. */
  1803. sys_exit->attr.exclude_callchain_kernel = 1;
  1804. }
  1805. trace->syscalls.events.sys_enter = sys_enter;
  1806. trace->syscalls.events.sys_exit = sys_exit;
  1807. ret = 0;
  1808. out:
  1809. return ret;
  1810. out_delete_sys_exit:
  1811. perf_evsel__delete_priv(sys_exit);
  1812. out_delete_sys_enter:
  1813. perf_evsel__delete_priv(sys_enter);
  1814. goto out;
  1815. }
  1816. static int trace__set_ev_qualifier_filter(struct trace *trace)
  1817. {
  1818. int err = -1;
  1819. struct perf_evsel *sys_exit;
  1820. char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
  1821. trace->ev_qualifier_ids.nr,
  1822. trace->ev_qualifier_ids.entries);
  1823. if (filter == NULL)
  1824. goto out_enomem;
  1825. if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
  1826. filter)) {
  1827. sys_exit = trace->syscalls.events.sys_exit;
  1828. err = perf_evsel__append_tp_filter(sys_exit, filter);
  1829. }
  1830. free(filter);
  1831. out:
  1832. return err;
  1833. out_enomem:
  1834. errno = ENOMEM;
  1835. goto out;
  1836. }
  1837. static int trace__run(struct trace *trace, int argc, const char **argv)
  1838. {
  1839. struct perf_evlist *evlist = trace->evlist;
  1840. struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
  1841. int err = -1, i;
  1842. unsigned long before;
  1843. const bool forks = argc > 0;
  1844. bool draining = false;
  1845. trace->live = true;
  1846. if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
  1847. goto out_error_raw_syscalls;
  1848. if (trace->trace_syscalls)
  1849. trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
  1850. if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
  1851. pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
  1852. if (pgfault_maj == NULL)
  1853. goto out_error_mem;
  1854. perf_evlist__add(evlist, pgfault_maj);
  1855. }
  1856. if ((trace->trace_pgfaults & TRACE_PFMIN)) {
  1857. pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
  1858. if (pgfault_min == NULL)
  1859. goto out_error_mem;
  1860. perf_evlist__add(evlist, pgfault_min);
  1861. }
  1862. if (trace->sched &&
  1863. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  1864. trace__sched_stat_runtime))
  1865. goto out_error_sched_stat_runtime;
  1866. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  1867. if (err < 0) {
  1868. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  1869. goto out_delete_evlist;
  1870. }
  1871. err = trace__symbols_init(trace, evlist);
  1872. if (err < 0) {
  1873. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  1874. goto out_delete_evlist;
  1875. }
  1876. perf_evlist__config(evlist, &trace->opts, NULL);
  1877. if (callchain_param.enabled) {
  1878. bool use_identifier = false;
  1879. if (trace->syscalls.events.sys_exit) {
  1880. perf_evsel__config_callchain(trace->syscalls.events.sys_exit,
  1881. &trace->opts, &callchain_param);
  1882. use_identifier = true;
  1883. }
  1884. if (pgfault_maj) {
  1885. perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
  1886. use_identifier = true;
  1887. }
  1888. if (pgfault_min) {
  1889. perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
  1890. use_identifier = true;
  1891. }
  1892. if (use_identifier) {
  1893. /*
  1894. * Now we have evsels with different sample_ids, use
  1895. * PERF_SAMPLE_IDENTIFIER to map from sample to evsel
  1896. * from a fixed position in each ring buffer record.
  1897. *
  1898. * As of this the changeset introducing this comment, this
  1899. * isn't strictly needed, as the fields that can come before
  1900. * PERF_SAMPLE_ID are all used, but we'll probably disable
  1901. * some of those for things like copying the payload of
  1902. * pointer syscall arguments, and for vfs_getname we don't
  1903. * need PERF_SAMPLE_ADDR and PERF_SAMPLE_IP, so do this
  1904. * here as a warning we need to use PERF_SAMPLE_IDENTIFIER.
  1905. */
  1906. perf_evlist__set_sample_bit(evlist, IDENTIFIER);
  1907. perf_evlist__reset_sample_bit(evlist, ID);
  1908. }
  1909. }
  1910. signal(SIGCHLD, sig_handler);
  1911. signal(SIGINT, sig_handler);
  1912. if (forks) {
  1913. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  1914. argv, false, NULL);
  1915. if (err < 0) {
  1916. fprintf(trace->output, "Couldn't run the workload!\n");
  1917. goto out_delete_evlist;
  1918. }
  1919. }
  1920. err = perf_evlist__open(evlist);
  1921. if (err < 0)
  1922. goto out_error_open;
  1923. err = bpf__apply_obj_config();
  1924. if (err) {
  1925. char errbuf[BUFSIZ];
  1926. bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
  1927. pr_err("ERROR: Apply config to BPF failed: %s\n",
  1928. errbuf);
  1929. goto out_error_open;
  1930. }
  1931. /*
  1932. * Better not use !target__has_task() here because we need to cover the
  1933. * case where no threads were specified in the command line, but a
  1934. * workload was, and in that case we will fill in the thread_map when
  1935. * we fork the workload in perf_evlist__prepare_workload.
  1936. */
  1937. if (trace->filter_pids.nr > 0)
  1938. err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
  1939. else if (thread_map__pid(evlist->threads, 0) == -1)
  1940. err = perf_evlist__set_filter_pid(evlist, getpid());
  1941. if (err < 0)
  1942. goto out_error_mem;
  1943. if (trace->ev_qualifier_ids.nr > 0) {
  1944. err = trace__set_ev_qualifier_filter(trace);
  1945. if (err < 0)
  1946. goto out_errno;
  1947. pr_debug("event qualifier tracepoint filter: %s\n",
  1948. trace->syscalls.events.sys_exit->filter);
  1949. }
  1950. err = perf_evlist__apply_filters(evlist, &evsel);
  1951. if (err < 0)
  1952. goto out_error_apply_filters;
  1953. err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false);
  1954. if (err < 0)
  1955. goto out_error_mmap;
  1956. if (!target__none(&trace->opts.target))
  1957. perf_evlist__enable(evlist);
  1958. if (forks)
  1959. perf_evlist__start_workload(evlist);
  1960. trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
  1961. evlist->threads->nr > 1 ||
  1962. perf_evlist__first(evlist)->attr.inherit;
  1963. again:
  1964. before = trace->nr_events;
  1965. for (i = 0; i < evlist->nr_mmaps; i++) {
  1966. union perf_event *event;
  1967. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  1968. struct perf_sample sample;
  1969. ++trace->nr_events;
  1970. err = perf_evlist__parse_sample(evlist, event, &sample);
  1971. if (err) {
  1972. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  1973. goto next_event;
  1974. }
  1975. trace__handle_event(trace, event, &sample);
  1976. next_event:
  1977. perf_evlist__mmap_consume(evlist, i);
  1978. if (interrupted)
  1979. goto out_disable;
  1980. if (done && !draining) {
  1981. perf_evlist__disable(evlist);
  1982. draining = true;
  1983. }
  1984. }
  1985. }
  1986. if (trace->nr_events == before) {
  1987. int timeout = done ? 100 : -1;
  1988. if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
  1989. if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
  1990. draining = true;
  1991. goto again;
  1992. }
  1993. } else {
  1994. goto again;
  1995. }
  1996. out_disable:
  1997. thread__zput(trace->current);
  1998. perf_evlist__disable(evlist);
  1999. if (!err) {
  2000. if (trace->summary)
  2001. trace__fprintf_thread_summary(trace, trace->output);
  2002. if (trace->show_tool_stats) {
  2003. fprintf(trace->output, "Stats:\n "
  2004. " vfs_getname : %" PRIu64 "\n"
  2005. " proc_getname: %" PRIu64 "\n",
  2006. trace->stats.vfs_getname,
  2007. trace->stats.proc_getname);
  2008. }
  2009. }
  2010. out_delete_evlist:
  2011. perf_evlist__delete(evlist);
  2012. trace->evlist = NULL;
  2013. trace->live = false;
  2014. return err;
  2015. {
  2016. char errbuf[BUFSIZ];
  2017. out_error_sched_stat_runtime:
  2018. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
  2019. goto out_error;
  2020. out_error_raw_syscalls:
  2021. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
  2022. goto out_error;
  2023. out_error_mmap:
  2024. perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
  2025. goto out_error;
  2026. out_error_open:
  2027. perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  2028. out_error:
  2029. fprintf(trace->output, "%s\n", errbuf);
  2030. goto out_delete_evlist;
  2031. out_error_apply_filters:
  2032. fprintf(trace->output,
  2033. "Failed to set filter \"%s\" on event %s with %d (%s)\n",
  2034. evsel->filter, perf_evsel__name(evsel), errno,
  2035. str_error_r(errno, errbuf, sizeof(errbuf)));
  2036. goto out_delete_evlist;
  2037. }
  2038. out_error_mem:
  2039. fprintf(trace->output, "Not enough memory to run!\n");
  2040. goto out_delete_evlist;
  2041. out_errno:
  2042. fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
  2043. goto out_delete_evlist;
  2044. }
  2045. static int trace__replay(struct trace *trace)
  2046. {
  2047. const struct perf_evsel_str_handler handlers[] = {
  2048. { "probe:vfs_getname", trace__vfs_getname, },
  2049. };
  2050. struct perf_data_file file = {
  2051. .path = input_name,
  2052. .mode = PERF_DATA_MODE_READ,
  2053. .force = trace->force,
  2054. };
  2055. struct perf_session *session;
  2056. struct perf_evsel *evsel;
  2057. int err = -1;
  2058. trace->tool.sample = trace__process_sample;
  2059. trace->tool.mmap = perf_event__process_mmap;
  2060. trace->tool.mmap2 = perf_event__process_mmap2;
  2061. trace->tool.comm = perf_event__process_comm;
  2062. trace->tool.exit = perf_event__process_exit;
  2063. trace->tool.fork = perf_event__process_fork;
  2064. trace->tool.attr = perf_event__process_attr;
  2065. trace->tool.tracing_data = perf_event__process_tracing_data;
  2066. trace->tool.build_id = perf_event__process_build_id;
  2067. trace->tool.ordered_events = true;
  2068. trace->tool.ordering_requires_timestamps = true;
  2069. /* add tid to output */
  2070. trace->multiple_threads = true;
  2071. session = perf_session__new(&file, false, &trace->tool);
  2072. if (session == NULL)
  2073. return -1;
  2074. if (symbol__init(&session->header.env) < 0)
  2075. goto out;
  2076. trace->host = &session->machines.host;
  2077. err = perf_session__set_tracepoints_handlers(session, handlers);
  2078. if (err)
  2079. goto out;
  2080. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2081. "raw_syscalls:sys_enter");
  2082. /* older kernels have syscalls tp versus raw_syscalls */
  2083. if (evsel == NULL)
  2084. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2085. "syscalls:sys_enter");
  2086. if (evsel &&
  2087. (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 ||
  2088. perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
  2089. pr_err("Error during initialize raw_syscalls:sys_enter event\n");
  2090. goto out;
  2091. }
  2092. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2093. "raw_syscalls:sys_exit");
  2094. if (evsel == NULL)
  2095. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2096. "syscalls:sys_exit");
  2097. if (evsel &&
  2098. (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 ||
  2099. perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
  2100. pr_err("Error during initialize raw_syscalls:sys_exit event\n");
  2101. goto out;
  2102. }
  2103. evlist__for_each_entry(session->evlist, evsel) {
  2104. if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
  2105. (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
  2106. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  2107. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
  2108. evsel->handler = trace__pgfault;
  2109. }
  2110. err = parse_target_str(trace);
  2111. if (err != 0)
  2112. goto out;
  2113. setup_pager();
  2114. err = perf_session__process_events(session);
  2115. if (err)
  2116. pr_err("Failed to process events, error %d", err);
  2117. else if (trace->summary)
  2118. trace__fprintf_thread_summary(trace, trace->output);
  2119. out:
  2120. perf_session__delete(session);
  2121. return err;
  2122. }
  2123. static size_t trace__fprintf_threads_header(FILE *fp)
  2124. {
  2125. size_t printed;
  2126. printed = fprintf(fp, "\n Summary of events:\n\n");
  2127. return printed;
  2128. }
  2129. DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
  2130. struct stats *stats;
  2131. double msecs;
  2132. int syscall;
  2133. )
  2134. {
  2135. struct int_node *source = rb_entry(nd, struct int_node, rb_node);
  2136. struct stats *stats = source->priv;
  2137. entry->syscall = source->i;
  2138. entry->stats = stats;
  2139. entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
  2140. }
  2141. static size_t thread__dump_stats(struct thread_trace *ttrace,
  2142. struct trace *trace, FILE *fp)
  2143. {
  2144. size_t printed = 0;
  2145. struct syscall *sc;
  2146. struct rb_node *nd;
  2147. DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
  2148. if (syscall_stats == NULL)
  2149. return 0;
  2150. printed += fprintf(fp, "\n");
  2151. printed += fprintf(fp, " syscall calls total min avg max stddev\n");
  2152. printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
  2153. printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
  2154. resort_rb__for_each_entry(nd, syscall_stats) {
  2155. struct stats *stats = syscall_stats_entry->stats;
  2156. if (stats) {
  2157. double min = (double)(stats->min) / NSEC_PER_MSEC;
  2158. double max = (double)(stats->max) / NSEC_PER_MSEC;
  2159. double avg = avg_stats(stats);
  2160. double pct;
  2161. u64 n = (u64) stats->n;
  2162. pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
  2163. avg /= NSEC_PER_MSEC;
  2164. sc = &trace->syscalls.table[syscall_stats_entry->syscall];
  2165. printed += fprintf(fp, " %-15s", sc->name);
  2166. printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
  2167. n, syscall_stats_entry->msecs, min, avg);
  2168. printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
  2169. }
  2170. }
  2171. resort_rb__delete(syscall_stats);
  2172. printed += fprintf(fp, "\n\n");
  2173. return printed;
  2174. }
  2175. static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
  2176. {
  2177. size_t printed = 0;
  2178. struct thread_trace *ttrace = thread__priv(thread);
  2179. double ratio;
  2180. if (ttrace == NULL)
  2181. return 0;
  2182. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  2183. printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
  2184. printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
  2185. printed += fprintf(fp, "%.1f%%", ratio);
  2186. if (ttrace->pfmaj)
  2187. printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
  2188. if (ttrace->pfmin)
  2189. printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
  2190. if (trace->sched)
  2191. printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
  2192. else if (fputc('\n', fp) != EOF)
  2193. ++printed;
  2194. printed += thread__dump_stats(ttrace, trace, fp);
  2195. return printed;
  2196. }
  2197. static unsigned long thread__nr_events(struct thread_trace *ttrace)
  2198. {
  2199. return ttrace ? ttrace->nr_events : 0;
  2200. }
  2201. DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
  2202. struct thread *thread;
  2203. )
  2204. {
  2205. entry->thread = rb_entry(nd, struct thread, rb_node);
  2206. }
  2207. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  2208. {
  2209. DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host);
  2210. size_t printed = trace__fprintf_threads_header(fp);
  2211. struct rb_node *nd;
  2212. if (threads == NULL) {
  2213. fprintf(fp, "%s", "Error sorting output by nr_events!\n");
  2214. return 0;
  2215. }
  2216. resort_rb__for_each_entry(nd, threads)
  2217. printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
  2218. resort_rb__delete(threads);
  2219. return printed;
  2220. }
  2221. static int trace__set_duration(const struct option *opt, const char *str,
  2222. int unset __maybe_unused)
  2223. {
  2224. struct trace *trace = opt->value;
  2225. trace->duration_filter = atof(str);
  2226. return 0;
  2227. }
  2228. static int trace__set_filter_pids(const struct option *opt, const char *str,
  2229. int unset __maybe_unused)
  2230. {
  2231. int ret = -1;
  2232. size_t i;
  2233. struct trace *trace = opt->value;
  2234. /*
  2235. * FIXME: introduce a intarray class, plain parse csv and create a
  2236. * { int nr, int entries[] } struct...
  2237. */
  2238. struct intlist *list = intlist__new(str);
  2239. if (list == NULL)
  2240. return -1;
  2241. i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
  2242. trace->filter_pids.entries = calloc(i, sizeof(pid_t));
  2243. if (trace->filter_pids.entries == NULL)
  2244. goto out;
  2245. trace->filter_pids.entries[0] = getpid();
  2246. for (i = 1; i < trace->filter_pids.nr; ++i)
  2247. trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
  2248. intlist__delete(list);
  2249. ret = 0;
  2250. out:
  2251. return ret;
  2252. }
  2253. static int trace__open_output(struct trace *trace, const char *filename)
  2254. {
  2255. struct stat st;
  2256. if (!stat(filename, &st) && st.st_size) {
  2257. char oldname[PATH_MAX];
  2258. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  2259. unlink(oldname);
  2260. rename(filename, oldname);
  2261. }
  2262. trace->output = fopen(filename, "w");
  2263. return trace->output == NULL ? -errno : 0;
  2264. }
  2265. static int parse_pagefaults(const struct option *opt, const char *str,
  2266. int unset __maybe_unused)
  2267. {
  2268. int *trace_pgfaults = opt->value;
  2269. if (strcmp(str, "all") == 0)
  2270. *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
  2271. else if (strcmp(str, "maj") == 0)
  2272. *trace_pgfaults |= TRACE_PFMAJ;
  2273. else if (strcmp(str, "min") == 0)
  2274. *trace_pgfaults |= TRACE_PFMIN;
  2275. else
  2276. return -1;
  2277. return 0;
  2278. }
  2279. static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
  2280. {
  2281. struct perf_evsel *evsel;
  2282. evlist__for_each_entry(evlist, evsel)
  2283. evsel->handler = handler;
  2284. }
  2285. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  2286. {
  2287. const char *trace_usage[] = {
  2288. "perf trace [<options>] [<command>]",
  2289. "perf trace [<options>] -- <command> [<options>]",
  2290. "perf trace record [<options>] [<command>]",
  2291. "perf trace record [<options>] -- <command> [<options>]",
  2292. NULL
  2293. };
  2294. struct trace trace = {
  2295. .syscalls = {
  2296. . max = -1,
  2297. },
  2298. .opts = {
  2299. .target = {
  2300. .uid = UINT_MAX,
  2301. .uses_mmap = true,
  2302. },
  2303. .user_freq = UINT_MAX,
  2304. .user_interval = ULLONG_MAX,
  2305. .no_buffering = true,
  2306. .mmap_pages = UINT_MAX,
  2307. .proc_map_timeout = 500,
  2308. },
  2309. .output = stderr,
  2310. .show_comm = true,
  2311. .trace_syscalls = true,
  2312. .kernel_syscallchains = false,
  2313. .max_stack = UINT_MAX,
  2314. };
  2315. const char *output_name = NULL;
  2316. const char *ev_qualifier_str = NULL;
  2317. const struct option trace_options[] = {
  2318. OPT_CALLBACK(0, "event", &trace.evlist, "event",
  2319. "event selector. use 'perf list' to list available events",
  2320. parse_events_option),
  2321. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  2322. "show the thread COMM next to its id"),
  2323. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  2324. OPT_STRING('e', "expr", &ev_qualifier_str, "expr", "list of syscalls to trace"),
  2325. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  2326. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  2327. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  2328. "trace events on existing process id"),
  2329. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  2330. "trace events on existing thread id"),
  2331. OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
  2332. "pids to filter (by the kernel)", trace__set_filter_pids),
  2333. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  2334. "system-wide collection from all CPUs"),
  2335. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  2336. "list of cpus to monitor"),
  2337. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  2338. "child tasks do not inherit counters"),
  2339. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  2340. "number of mmap data pages",
  2341. perf_evlist__parse_mmap_pages),
  2342. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  2343. "user to profile"),
  2344. OPT_CALLBACK(0, "duration", &trace, "float",
  2345. "show only events with duration > N.M ms",
  2346. trace__set_duration),
  2347. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  2348. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  2349. OPT_BOOLEAN('T', "time", &trace.full_time,
  2350. "Show full timestamp, not time relative to first start"),
  2351. OPT_BOOLEAN('s', "summary", &trace.summary_only,
  2352. "Show only syscall summary with statistics"),
  2353. OPT_BOOLEAN('S', "with-summary", &trace.summary,
  2354. "Show all syscalls and summary with statistics"),
  2355. OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
  2356. "Trace pagefaults", parse_pagefaults, "maj"),
  2357. OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
  2358. OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
  2359. OPT_CALLBACK(0, "call-graph", &trace.opts,
  2360. "record_mode[,record_size]", record_callchain_help,
  2361. &record_parse_callchain_opt),
  2362. OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
  2363. "Show the kernel callchains on the syscall exit path"),
  2364. OPT_UINTEGER(0, "min-stack", &trace.min_stack,
  2365. "Set the minimum stack depth when parsing the callchain, "
  2366. "anything below the specified depth will be ignored."),
  2367. OPT_UINTEGER(0, "max-stack", &trace.max_stack,
  2368. "Set the maximum stack depth when parsing the callchain, "
  2369. "anything beyond the specified depth will be ignored. "
  2370. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  2371. OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
  2372. "per thread proc mmap processing timeout in ms"),
  2373. OPT_END()
  2374. };
  2375. bool __maybe_unused max_stack_user_set = true;
  2376. bool mmap_pages_user_set = true;
  2377. const char * const trace_subcommands[] = { "record", NULL };
  2378. int err;
  2379. char bf[BUFSIZ];
  2380. signal(SIGSEGV, sighandler_dump_stack);
  2381. signal(SIGFPE, sighandler_dump_stack);
  2382. trace.evlist = perf_evlist__new();
  2383. trace.sctbl = syscalltbl__new();
  2384. if (trace.evlist == NULL || trace.sctbl == NULL) {
  2385. pr_err("Not enough memory to run!\n");
  2386. err = -ENOMEM;
  2387. goto out;
  2388. }
  2389. argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
  2390. trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  2391. err = bpf__setup_stdout(trace.evlist);
  2392. if (err) {
  2393. bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
  2394. pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
  2395. goto out;
  2396. }
  2397. err = -1;
  2398. if (trace.trace_pgfaults) {
  2399. trace.opts.sample_address = true;
  2400. trace.opts.sample_time = true;
  2401. }
  2402. if (trace.opts.mmap_pages == UINT_MAX)
  2403. mmap_pages_user_set = false;
  2404. if (trace.max_stack == UINT_MAX) {
  2405. trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl_perf_event_max_stack;
  2406. max_stack_user_set = false;
  2407. }
  2408. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  2409. if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled && trace.trace_syscalls)
  2410. record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
  2411. #endif
  2412. if (callchain_param.enabled) {
  2413. if (!mmap_pages_user_set && geteuid() == 0)
  2414. trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
  2415. symbol_conf.use_callchain = true;
  2416. }
  2417. if (trace.evlist->nr_entries > 0)
  2418. evlist__set_evsel_handler(trace.evlist, trace__event_handler);
  2419. if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
  2420. return trace__record(&trace, argc-1, &argv[1]);
  2421. /* summary_only implies summary option, but don't overwrite summary if set */
  2422. if (trace.summary_only)
  2423. trace.summary = trace.summary_only;
  2424. if (!trace.trace_syscalls && !trace.trace_pgfaults &&
  2425. trace.evlist->nr_entries == 0 /* Was --events used? */) {
  2426. pr_err("Please specify something to trace.\n");
  2427. return -1;
  2428. }
  2429. if (!trace.trace_syscalls && ev_qualifier_str) {
  2430. pr_err("The -e option can't be used with --no-syscalls.\n");
  2431. goto out;
  2432. }
  2433. if (output_name != NULL) {
  2434. err = trace__open_output(&trace, output_name);
  2435. if (err < 0) {
  2436. perror("failed to create output file");
  2437. goto out;
  2438. }
  2439. }
  2440. trace.open_id = syscalltbl__id(trace.sctbl, "open");
  2441. if (ev_qualifier_str != NULL) {
  2442. const char *s = ev_qualifier_str;
  2443. struct strlist_config slist_config = {
  2444. .dirname = system_path(STRACE_GROUPS_DIR),
  2445. };
  2446. trace.not_ev_qualifier = *s == '!';
  2447. if (trace.not_ev_qualifier)
  2448. ++s;
  2449. trace.ev_qualifier = strlist__new(s, &slist_config);
  2450. if (trace.ev_qualifier == NULL) {
  2451. fputs("Not enough memory to parse event qualifier",
  2452. trace.output);
  2453. err = -ENOMEM;
  2454. goto out_close;
  2455. }
  2456. err = trace__validate_ev_qualifier(&trace);
  2457. if (err)
  2458. goto out_close;
  2459. }
  2460. err = target__validate(&trace.opts.target);
  2461. if (err) {
  2462. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2463. fprintf(trace.output, "%s", bf);
  2464. goto out_close;
  2465. }
  2466. err = target__parse_uid(&trace.opts.target);
  2467. if (err) {
  2468. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2469. fprintf(trace.output, "%s", bf);
  2470. goto out_close;
  2471. }
  2472. if (!argc && target__none(&trace.opts.target))
  2473. trace.opts.target.system_wide = true;
  2474. if (input_name)
  2475. err = trace__replay(&trace);
  2476. else
  2477. err = trace__run(&trace, argc, argv);
  2478. out_close:
  2479. if (output_name != NULL)
  2480. fclose(trace.output);
  2481. out:
  2482. return err;
  2483. }