builtin-trace.c 87 KB

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