builtin-trace.c 77 KB

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