builtin-trace.c 86 KB

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