builtin-trace.c 83 KB

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