builtin-script.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/cache.h"
  4. #include "util/debug.h"
  5. #include "util/exec_cmd.h"
  6. #include "util/header.h"
  7. #include "util/parse-options.h"
  8. #include "util/perf_regs.h"
  9. #include "util/session.h"
  10. #include "util/tool.h"
  11. #include "util/symbol.h"
  12. #include "util/thread.h"
  13. #include "util/trace-event.h"
  14. #include "util/util.h"
  15. #include "util/evlist.h"
  16. #include "util/evsel.h"
  17. #include "util/sort.h"
  18. #include "util/data.h"
  19. #include "util/auxtrace.h"
  20. #include <linux/bitmap.h>
  21. static char const *script_name;
  22. static char const *generate_script_lang;
  23. static bool debug_mode;
  24. static u64 last_timestamp;
  25. static u64 nr_unordered;
  26. static bool no_callchain;
  27. static bool latency_format;
  28. static bool system_wide;
  29. static bool print_flags;
  30. static bool nanosecs;
  31. static const char *cpu_list;
  32. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  33. unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
  34. enum perf_output_field {
  35. PERF_OUTPUT_COMM = 1U << 0,
  36. PERF_OUTPUT_TID = 1U << 1,
  37. PERF_OUTPUT_PID = 1U << 2,
  38. PERF_OUTPUT_TIME = 1U << 3,
  39. PERF_OUTPUT_CPU = 1U << 4,
  40. PERF_OUTPUT_EVNAME = 1U << 5,
  41. PERF_OUTPUT_TRACE = 1U << 6,
  42. PERF_OUTPUT_IP = 1U << 7,
  43. PERF_OUTPUT_SYM = 1U << 8,
  44. PERF_OUTPUT_DSO = 1U << 9,
  45. PERF_OUTPUT_ADDR = 1U << 10,
  46. PERF_OUTPUT_SYMOFFSET = 1U << 11,
  47. PERF_OUTPUT_SRCLINE = 1U << 12,
  48. PERF_OUTPUT_PERIOD = 1U << 13,
  49. PERF_OUTPUT_IREGS = 1U << 14,
  50. PERF_OUTPUT_BRSTACK = 1U << 15,
  51. PERF_OUTPUT_BRSTACKSYM = 1U << 16,
  52. };
  53. struct output_option {
  54. const char *str;
  55. enum perf_output_field field;
  56. } all_output_options[] = {
  57. {.str = "comm", .field = PERF_OUTPUT_COMM},
  58. {.str = "tid", .field = PERF_OUTPUT_TID},
  59. {.str = "pid", .field = PERF_OUTPUT_PID},
  60. {.str = "time", .field = PERF_OUTPUT_TIME},
  61. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  62. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  63. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  64. {.str = "ip", .field = PERF_OUTPUT_IP},
  65. {.str = "sym", .field = PERF_OUTPUT_SYM},
  66. {.str = "dso", .field = PERF_OUTPUT_DSO},
  67. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  68. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  69. {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  70. {.str = "period", .field = PERF_OUTPUT_PERIOD},
  71. {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  72. {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
  73. {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
  74. };
  75. /* default set to maintain compatibility with current format */
  76. static struct {
  77. bool user_set;
  78. bool wildcard_set;
  79. unsigned int print_ip_opts;
  80. u64 fields;
  81. u64 invalid_fields;
  82. } output[PERF_TYPE_MAX] = {
  83. [PERF_TYPE_HARDWARE] = {
  84. .user_set = false,
  85. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  86. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  87. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  88. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  89. PERF_OUTPUT_PERIOD,
  90. .invalid_fields = PERF_OUTPUT_TRACE,
  91. },
  92. [PERF_TYPE_SOFTWARE] = {
  93. .user_set = false,
  94. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  95. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  96. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  97. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  98. PERF_OUTPUT_PERIOD,
  99. .invalid_fields = PERF_OUTPUT_TRACE,
  100. },
  101. [PERF_TYPE_TRACEPOINT] = {
  102. .user_set = false,
  103. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  104. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  105. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
  106. },
  107. [PERF_TYPE_RAW] = {
  108. .user_set = false,
  109. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  110. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  111. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  112. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  113. PERF_OUTPUT_PERIOD,
  114. .invalid_fields = PERF_OUTPUT_TRACE,
  115. },
  116. [PERF_TYPE_BREAKPOINT] = {
  117. .user_set = false,
  118. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  119. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  120. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  121. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  122. PERF_OUTPUT_PERIOD,
  123. .invalid_fields = PERF_OUTPUT_TRACE,
  124. },
  125. };
  126. static bool output_set_by_user(void)
  127. {
  128. int j;
  129. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  130. if (output[j].user_set)
  131. return true;
  132. }
  133. return false;
  134. }
  135. static const char *output_field2str(enum perf_output_field field)
  136. {
  137. int i, imax = ARRAY_SIZE(all_output_options);
  138. const char *str = "";
  139. for (i = 0; i < imax; ++i) {
  140. if (all_output_options[i].field == field) {
  141. str = all_output_options[i].str;
  142. break;
  143. }
  144. }
  145. return str;
  146. }
  147. #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
  148. static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
  149. u64 sample_type, const char *sample_msg,
  150. enum perf_output_field field,
  151. bool allow_user_set)
  152. {
  153. struct perf_event_attr *attr = &evsel->attr;
  154. int type = attr->type;
  155. const char *evname;
  156. if (attr->sample_type & sample_type)
  157. return 0;
  158. if (output[type].user_set) {
  159. if (allow_user_set)
  160. return 0;
  161. evname = perf_evsel__name(evsel);
  162. pr_err("Samples for '%s' event do not have %s attribute set. "
  163. "Cannot print '%s' field.\n",
  164. evname, sample_msg, output_field2str(field));
  165. return -1;
  166. }
  167. /* user did not ask for it explicitly so remove from the default list */
  168. output[type].fields &= ~field;
  169. evname = perf_evsel__name(evsel);
  170. pr_debug("Samples for '%s' event do not have %s attribute set. "
  171. "Skipping '%s' field.\n",
  172. evname, sample_msg, output_field2str(field));
  173. return 0;
  174. }
  175. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  176. u64 sample_type, const char *sample_msg,
  177. enum perf_output_field field)
  178. {
  179. return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
  180. false);
  181. }
  182. static int perf_evsel__check_attr(struct perf_evsel *evsel,
  183. struct perf_session *session)
  184. {
  185. struct perf_event_attr *attr = &evsel->attr;
  186. bool allow_user_set;
  187. allow_user_set = perf_header__has_feat(&session->header,
  188. HEADER_AUXTRACE);
  189. if (PRINT_FIELD(TRACE) &&
  190. !perf_session__has_traces(session, "record -R"))
  191. return -EINVAL;
  192. if (PRINT_FIELD(IP)) {
  193. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
  194. PERF_OUTPUT_IP))
  195. return -EINVAL;
  196. }
  197. if (PRINT_FIELD(ADDR) &&
  198. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
  199. PERF_OUTPUT_ADDR, allow_user_set))
  200. return -EINVAL;
  201. if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  202. pr_err("Display of symbols requested but neither sample IP nor "
  203. "sample address\nis selected. Hence, no addresses to convert "
  204. "to symbols.\n");
  205. return -EINVAL;
  206. }
  207. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  208. pr_err("Display of offsets requested but symbol is not"
  209. "selected.\n");
  210. return -EINVAL;
  211. }
  212. if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  213. pr_err("Display of DSO requested but neither sample IP nor "
  214. "sample address\nis selected. Hence, no addresses to convert "
  215. "to DSO.\n");
  216. return -EINVAL;
  217. }
  218. if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
  219. pr_err("Display of source line number requested but sample IP is not\n"
  220. "selected. Hence, no address to lookup the source line number.\n");
  221. return -EINVAL;
  222. }
  223. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  224. perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
  225. PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  226. return -EINVAL;
  227. if (PRINT_FIELD(TIME) &&
  228. perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
  229. PERF_OUTPUT_TIME))
  230. return -EINVAL;
  231. if (PRINT_FIELD(CPU) &&
  232. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
  233. PERF_OUTPUT_CPU, allow_user_set))
  234. return -EINVAL;
  235. if (PRINT_FIELD(PERIOD) &&
  236. perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
  237. PERF_OUTPUT_PERIOD))
  238. return -EINVAL;
  239. if (PRINT_FIELD(IREGS) &&
  240. perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
  241. PERF_OUTPUT_IREGS))
  242. return -EINVAL;
  243. return 0;
  244. }
  245. static void set_print_ip_opts(struct perf_event_attr *attr)
  246. {
  247. unsigned int type = attr->type;
  248. output[type].print_ip_opts = 0;
  249. if (PRINT_FIELD(IP))
  250. output[type].print_ip_opts |= PRINT_IP_OPT_IP;
  251. if (PRINT_FIELD(SYM))
  252. output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
  253. if (PRINT_FIELD(DSO))
  254. output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
  255. if (PRINT_FIELD(SYMOFFSET))
  256. output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
  257. if (PRINT_FIELD(SRCLINE))
  258. output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
  259. }
  260. /*
  261. * verify all user requested events exist and the samples
  262. * have the expected data
  263. */
  264. static int perf_session__check_output_opt(struct perf_session *session)
  265. {
  266. int j;
  267. struct perf_evsel *evsel;
  268. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  269. evsel = perf_session__find_first_evtype(session, j);
  270. /*
  271. * even if fields is set to 0 (ie., show nothing) event must
  272. * exist if user explicitly includes it on the command line
  273. */
  274. if (!evsel && output[j].user_set && !output[j].wildcard_set) {
  275. pr_err("%s events do not exist. "
  276. "Remove corresponding -f option to proceed.\n",
  277. event_type(j));
  278. return -1;
  279. }
  280. if (evsel && output[j].fields &&
  281. perf_evsel__check_attr(evsel, session))
  282. return -1;
  283. if (evsel == NULL)
  284. continue;
  285. set_print_ip_opts(&evsel->attr);
  286. }
  287. if (!no_callchain) {
  288. bool use_callchain = false;
  289. evlist__for_each(session->evlist, evsel) {
  290. if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  291. use_callchain = true;
  292. break;
  293. }
  294. }
  295. if (!use_callchain)
  296. symbol_conf.use_callchain = false;
  297. }
  298. /*
  299. * set default for tracepoints to print symbols only
  300. * if callchains are present
  301. */
  302. if (symbol_conf.use_callchain &&
  303. !output[PERF_TYPE_TRACEPOINT].user_set) {
  304. struct perf_event_attr *attr;
  305. j = PERF_TYPE_TRACEPOINT;
  306. evsel = perf_session__find_first_evtype(session, j);
  307. if (evsel == NULL)
  308. goto out;
  309. attr = &evsel->attr;
  310. if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
  311. output[j].fields |= PERF_OUTPUT_IP;
  312. output[j].fields |= PERF_OUTPUT_SYM;
  313. output[j].fields |= PERF_OUTPUT_DSO;
  314. set_print_ip_opts(attr);
  315. }
  316. }
  317. out:
  318. return 0;
  319. }
  320. static void print_sample_iregs(union perf_event *event __maybe_unused,
  321. struct perf_sample *sample,
  322. struct thread *thread __maybe_unused,
  323. struct perf_event_attr *attr)
  324. {
  325. struct regs_dump *regs = &sample->intr_regs;
  326. uint64_t mask = attr->sample_regs_intr;
  327. unsigned i = 0, r;
  328. if (!regs)
  329. return;
  330. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  331. u64 val = regs->regs[i++];
  332. printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
  333. }
  334. }
  335. static void print_sample_start(struct perf_sample *sample,
  336. struct thread *thread,
  337. struct perf_evsel *evsel)
  338. {
  339. struct perf_event_attr *attr = &evsel->attr;
  340. unsigned long secs;
  341. unsigned long usecs;
  342. unsigned long long nsecs;
  343. if (PRINT_FIELD(COMM)) {
  344. if (latency_format)
  345. printf("%8.8s ", thread__comm_str(thread));
  346. else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
  347. printf("%s ", thread__comm_str(thread));
  348. else
  349. printf("%16s ", thread__comm_str(thread));
  350. }
  351. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  352. printf("%5d/%-5d ", sample->pid, sample->tid);
  353. else if (PRINT_FIELD(PID))
  354. printf("%5d ", sample->pid);
  355. else if (PRINT_FIELD(TID))
  356. printf("%5d ", sample->tid);
  357. if (PRINT_FIELD(CPU)) {
  358. if (latency_format)
  359. printf("%3d ", sample->cpu);
  360. else
  361. printf("[%03d] ", sample->cpu);
  362. }
  363. if (PRINT_FIELD(TIME)) {
  364. nsecs = sample->time;
  365. secs = nsecs / NSECS_PER_SEC;
  366. nsecs -= secs * NSECS_PER_SEC;
  367. usecs = nsecs / NSECS_PER_USEC;
  368. if (nanosecs)
  369. printf("%5lu.%09llu: ", secs, nsecs);
  370. else
  371. printf("%5lu.%06lu: ", secs, usecs);
  372. }
  373. }
  374. static inline char
  375. mispred_str(struct branch_entry *br)
  376. {
  377. if (!(br->flags.mispred || br->flags.predicted))
  378. return '-';
  379. return br->flags.predicted ? 'P' : 'M';
  380. }
  381. static void print_sample_brstack(union perf_event *event __maybe_unused,
  382. struct perf_sample *sample,
  383. struct thread *thread __maybe_unused,
  384. struct perf_event_attr *attr __maybe_unused)
  385. {
  386. struct branch_stack *br = sample->branch_stack;
  387. u64 i;
  388. if (!(br && br->nr))
  389. return;
  390. for (i = 0; i < br->nr; i++) {
  391. printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
  392. br->entries[i].from,
  393. br->entries[i].to,
  394. mispred_str( br->entries + i),
  395. br->entries[i].flags.in_tx? 'X' : '-',
  396. br->entries[i].flags.abort? 'A' : '-',
  397. br->entries[i].flags.cycles);
  398. }
  399. }
  400. static void print_sample_brstacksym(union perf_event *event __maybe_unused,
  401. struct perf_sample *sample,
  402. struct thread *thread __maybe_unused,
  403. struct perf_event_attr *attr __maybe_unused)
  404. {
  405. struct branch_stack *br = sample->branch_stack;
  406. struct addr_location alf, alt;
  407. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  408. u64 i, from, to;
  409. if (!(br && br->nr))
  410. return;
  411. for (i = 0; i < br->nr; i++) {
  412. memset(&alf, 0, sizeof(alf));
  413. memset(&alt, 0, sizeof(alt));
  414. from = br->entries[i].from;
  415. to = br->entries[i].to;
  416. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
  417. if (alf.map)
  418. alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
  419. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
  420. if (alt.map)
  421. alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
  422. symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
  423. putchar('/');
  424. symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
  425. printf("/%c/%c/%c/%d ",
  426. mispred_str( br->entries + i),
  427. br->entries[i].flags.in_tx? 'X' : '-',
  428. br->entries[i].flags.abort? 'A' : '-',
  429. br->entries[i].flags.cycles);
  430. }
  431. }
  432. static void print_sample_addr(union perf_event *event,
  433. struct perf_sample *sample,
  434. struct thread *thread,
  435. struct perf_event_attr *attr)
  436. {
  437. struct addr_location al;
  438. printf("%16" PRIx64, sample->addr);
  439. if (!sample_addr_correlates_sym(attr))
  440. return;
  441. perf_event__preprocess_sample_addr(event, sample, thread, &al);
  442. if (PRINT_FIELD(SYM)) {
  443. printf(" ");
  444. if (PRINT_FIELD(SYMOFFSET))
  445. symbol__fprintf_symname_offs(al.sym, &al, stdout);
  446. else
  447. symbol__fprintf_symname(al.sym, stdout);
  448. }
  449. if (PRINT_FIELD(DSO)) {
  450. printf(" (");
  451. map__fprintf_dsoname(al.map, stdout);
  452. printf(")");
  453. }
  454. }
  455. static void print_sample_bts(union perf_event *event,
  456. struct perf_sample *sample,
  457. struct perf_evsel *evsel,
  458. struct thread *thread,
  459. struct addr_location *al)
  460. {
  461. struct perf_event_attr *attr = &evsel->attr;
  462. bool print_srcline_last = false;
  463. /* print branch_from information */
  464. if (PRINT_FIELD(IP)) {
  465. unsigned int print_opts = output[attr->type].print_ip_opts;
  466. if (symbol_conf.use_callchain && sample->callchain) {
  467. printf("\n");
  468. } else {
  469. printf(" ");
  470. if (print_opts & PRINT_IP_OPT_SRCLINE) {
  471. print_srcline_last = true;
  472. print_opts &= ~PRINT_IP_OPT_SRCLINE;
  473. }
  474. }
  475. perf_evsel__print_ip(evsel, sample, al, print_opts,
  476. scripting_max_stack);
  477. }
  478. /* print branch_to information */
  479. if (PRINT_FIELD(ADDR) ||
  480. ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  481. !output[attr->type].user_set)) {
  482. printf(" => ");
  483. print_sample_addr(event, sample, thread, attr);
  484. }
  485. if (print_srcline_last)
  486. map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
  487. printf("\n");
  488. }
  489. static void print_sample_flags(u32 flags)
  490. {
  491. const char *chars = PERF_IP_FLAG_CHARS;
  492. const int n = strlen(PERF_IP_FLAG_CHARS);
  493. char str[33];
  494. int i, pos = 0;
  495. for (i = 0; i < n; i++, flags >>= 1) {
  496. if (flags & 1)
  497. str[pos++] = chars[i];
  498. }
  499. for (; i < 32; i++, flags >>= 1) {
  500. if (flags & 1)
  501. str[pos++] = '?';
  502. }
  503. str[pos] = 0;
  504. printf(" %-4s ", str);
  505. }
  506. struct perf_script {
  507. struct perf_tool tool;
  508. struct perf_session *session;
  509. bool show_task_events;
  510. bool show_mmap_events;
  511. bool show_switch_events;
  512. };
  513. static void process_event(struct perf_script *script __maybe_unused, union perf_event *event,
  514. struct perf_sample *sample, struct perf_evsel *evsel,
  515. struct addr_location *al)
  516. {
  517. struct thread *thread = al->thread;
  518. struct perf_event_attr *attr = &evsel->attr;
  519. if (output[attr->type].fields == 0)
  520. return;
  521. print_sample_start(sample, thread, evsel);
  522. if (PRINT_FIELD(PERIOD))
  523. printf("%10" PRIu64 " ", sample->period);
  524. if (PRINT_FIELD(EVNAME)) {
  525. const char *evname = perf_evsel__name(evsel);
  526. printf("%s: ", evname ? evname : "[unknown]");
  527. }
  528. if (print_flags)
  529. print_sample_flags(sample->flags);
  530. if (is_bts_event(attr)) {
  531. print_sample_bts(event, sample, evsel, thread, al);
  532. return;
  533. }
  534. if (PRINT_FIELD(TRACE))
  535. event_format__print(evsel->tp_format, sample->cpu,
  536. sample->raw_data, sample->raw_size);
  537. if (PRINT_FIELD(ADDR))
  538. print_sample_addr(event, sample, thread, attr);
  539. if (PRINT_FIELD(IP)) {
  540. if (!symbol_conf.use_callchain)
  541. printf(" ");
  542. else
  543. printf("\n");
  544. perf_evsel__print_ip(evsel, sample, al,
  545. output[attr->type].print_ip_opts,
  546. scripting_max_stack);
  547. }
  548. if (PRINT_FIELD(IREGS))
  549. print_sample_iregs(event, sample, thread, attr);
  550. if (PRINT_FIELD(BRSTACK))
  551. print_sample_brstack(event, sample, thread, attr);
  552. else if (PRINT_FIELD(BRSTACKSYM))
  553. print_sample_brstacksym(event, sample, thread, attr);
  554. printf("\n");
  555. }
  556. static struct scripting_ops *scripting_ops;
  557. static void setup_scripting(void)
  558. {
  559. setup_perl_scripting();
  560. setup_python_scripting();
  561. }
  562. static int flush_scripting(void)
  563. {
  564. return scripting_ops ? scripting_ops->flush_script() : 0;
  565. }
  566. static int cleanup_scripting(void)
  567. {
  568. pr_debug("\nperf script stopped\n");
  569. return scripting_ops ? scripting_ops->stop_script() : 0;
  570. }
  571. static int process_sample_event(struct perf_tool *tool,
  572. union perf_event *event,
  573. struct perf_sample *sample,
  574. struct perf_evsel *evsel,
  575. struct machine *machine)
  576. {
  577. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  578. struct addr_location al;
  579. if (debug_mode) {
  580. if (sample->time < last_timestamp) {
  581. pr_err("Samples misordered, previous: %" PRIu64
  582. " this: %" PRIu64 "\n", last_timestamp,
  583. sample->time);
  584. nr_unordered++;
  585. }
  586. last_timestamp = sample->time;
  587. return 0;
  588. }
  589. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  590. pr_err("problem processing %d event, skipping it.\n",
  591. event->header.type);
  592. return -1;
  593. }
  594. if (al.filtered)
  595. goto out_put;
  596. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  597. goto out_put;
  598. if (scripting_ops)
  599. scripting_ops->process_event(event, sample, evsel, &al);
  600. else
  601. process_event(scr, event, sample, evsel, &al);
  602. out_put:
  603. addr_location__put(&al);
  604. return 0;
  605. }
  606. static int process_attr(struct perf_tool *tool, union perf_event *event,
  607. struct perf_evlist **pevlist)
  608. {
  609. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  610. struct perf_evlist *evlist;
  611. struct perf_evsel *evsel, *pos;
  612. int err;
  613. err = perf_event__process_attr(tool, event, pevlist);
  614. if (err)
  615. return err;
  616. evlist = *pevlist;
  617. evsel = perf_evlist__last(*pevlist);
  618. if (evsel->attr.type >= PERF_TYPE_MAX)
  619. return 0;
  620. evlist__for_each(evlist, pos) {
  621. if (pos->attr.type == evsel->attr.type && pos != evsel)
  622. return 0;
  623. }
  624. set_print_ip_opts(&evsel->attr);
  625. if (evsel->attr.sample_type)
  626. err = perf_evsel__check_attr(evsel, scr->session);
  627. return err;
  628. }
  629. static int process_comm_event(struct perf_tool *tool,
  630. union perf_event *event,
  631. struct perf_sample *sample,
  632. struct machine *machine)
  633. {
  634. struct thread *thread;
  635. struct perf_script *script = container_of(tool, struct perf_script, tool);
  636. struct perf_session *session = script->session;
  637. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  638. int ret = -1;
  639. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  640. if (thread == NULL) {
  641. pr_debug("problem processing COMM event, skipping it.\n");
  642. return -1;
  643. }
  644. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  645. goto out;
  646. if (!evsel->attr.sample_id_all) {
  647. sample->cpu = 0;
  648. sample->time = 0;
  649. sample->tid = event->comm.tid;
  650. sample->pid = event->comm.pid;
  651. }
  652. print_sample_start(sample, thread, evsel);
  653. perf_event__fprintf(event, stdout);
  654. ret = 0;
  655. out:
  656. thread__put(thread);
  657. return ret;
  658. }
  659. static int process_fork_event(struct perf_tool *tool,
  660. union perf_event *event,
  661. struct perf_sample *sample,
  662. struct machine *machine)
  663. {
  664. struct thread *thread;
  665. struct perf_script *script = container_of(tool, struct perf_script, tool);
  666. struct perf_session *session = script->session;
  667. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  668. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  669. return -1;
  670. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  671. if (thread == NULL) {
  672. pr_debug("problem processing FORK event, skipping it.\n");
  673. return -1;
  674. }
  675. if (!evsel->attr.sample_id_all) {
  676. sample->cpu = 0;
  677. sample->time = event->fork.time;
  678. sample->tid = event->fork.tid;
  679. sample->pid = event->fork.pid;
  680. }
  681. print_sample_start(sample, thread, evsel);
  682. perf_event__fprintf(event, stdout);
  683. thread__put(thread);
  684. return 0;
  685. }
  686. static int process_exit_event(struct perf_tool *tool,
  687. union perf_event *event,
  688. struct perf_sample *sample,
  689. struct machine *machine)
  690. {
  691. int err = 0;
  692. struct thread *thread;
  693. struct perf_script *script = container_of(tool, struct perf_script, tool);
  694. struct perf_session *session = script->session;
  695. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  696. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  697. if (thread == NULL) {
  698. pr_debug("problem processing EXIT event, skipping it.\n");
  699. return -1;
  700. }
  701. if (!evsel->attr.sample_id_all) {
  702. sample->cpu = 0;
  703. sample->time = 0;
  704. sample->tid = event->fork.tid;
  705. sample->pid = event->fork.pid;
  706. }
  707. print_sample_start(sample, thread, evsel);
  708. perf_event__fprintf(event, stdout);
  709. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  710. err = -1;
  711. thread__put(thread);
  712. return err;
  713. }
  714. static int process_mmap_event(struct perf_tool *tool,
  715. union perf_event *event,
  716. struct perf_sample *sample,
  717. struct machine *machine)
  718. {
  719. struct thread *thread;
  720. struct perf_script *script = container_of(tool, struct perf_script, tool);
  721. struct perf_session *session = script->session;
  722. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  723. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  724. return -1;
  725. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  726. if (thread == NULL) {
  727. pr_debug("problem processing MMAP event, skipping it.\n");
  728. return -1;
  729. }
  730. if (!evsel->attr.sample_id_all) {
  731. sample->cpu = 0;
  732. sample->time = 0;
  733. sample->tid = event->mmap.tid;
  734. sample->pid = event->mmap.pid;
  735. }
  736. print_sample_start(sample, thread, evsel);
  737. perf_event__fprintf(event, stdout);
  738. thread__put(thread);
  739. return 0;
  740. }
  741. static int process_mmap2_event(struct perf_tool *tool,
  742. union perf_event *event,
  743. struct perf_sample *sample,
  744. struct machine *machine)
  745. {
  746. struct thread *thread;
  747. struct perf_script *script = container_of(tool, struct perf_script, tool);
  748. struct perf_session *session = script->session;
  749. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  750. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  751. return -1;
  752. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  753. if (thread == NULL) {
  754. pr_debug("problem processing MMAP2 event, skipping it.\n");
  755. return -1;
  756. }
  757. if (!evsel->attr.sample_id_all) {
  758. sample->cpu = 0;
  759. sample->time = 0;
  760. sample->tid = event->mmap2.tid;
  761. sample->pid = event->mmap2.pid;
  762. }
  763. print_sample_start(sample, thread, evsel);
  764. perf_event__fprintf(event, stdout);
  765. thread__put(thread);
  766. return 0;
  767. }
  768. static int process_switch_event(struct perf_tool *tool,
  769. union perf_event *event,
  770. struct perf_sample *sample,
  771. struct machine *machine)
  772. {
  773. struct thread *thread;
  774. struct perf_script *script = container_of(tool, struct perf_script, tool);
  775. struct perf_session *session = script->session;
  776. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  777. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  778. return -1;
  779. thread = machine__findnew_thread(machine, sample->pid,
  780. sample->tid);
  781. if (thread == NULL) {
  782. pr_debug("problem processing SWITCH event, skipping it.\n");
  783. return -1;
  784. }
  785. print_sample_start(sample, thread, evsel);
  786. perf_event__fprintf(event, stdout);
  787. thread__put(thread);
  788. return 0;
  789. }
  790. static void sig_handler(int sig __maybe_unused)
  791. {
  792. session_done = 1;
  793. }
  794. static int __cmd_script(struct perf_script *script)
  795. {
  796. int ret;
  797. signal(SIGINT, sig_handler);
  798. /* override event processing functions */
  799. if (script->show_task_events) {
  800. script->tool.comm = process_comm_event;
  801. script->tool.fork = process_fork_event;
  802. script->tool.exit = process_exit_event;
  803. }
  804. if (script->show_mmap_events) {
  805. script->tool.mmap = process_mmap_event;
  806. script->tool.mmap2 = process_mmap2_event;
  807. }
  808. if (script->show_switch_events)
  809. script->tool.context_switch = process_switch_event;
  810. ret = perf_session__process_events(script->session);
  811. if (debug_mode)
  812. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  813. return ret;
  814. }
  815. struct script_spec {
  816. struct list_head node;
  817. struct scripting_ops *ops;
  818. char spec[0];
  819. };
  820. static LIST_HEAD(script_specs);
  821. static struct script_spec *script_spec__new(const char *spec,
  822. struct scripting_ops *ops)
  823. {
  824. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  825. if (s != NULL) {
  826. strcpy(s->spec, spec);
  827. s->ops = ops;
  828. }
  829. return s;
  830. }
  831. static void script_spec__add(struct script_spec *s)
  832. {
  833. list_add_tail(&s->node, &script_specs);
  834. }
  835. static struct script_spec *script_spec__find(const char *spec)
  836. {
  837. struct script_spec *s;
  838. list_for_each_entry(s, &script_specs, node)
  839. if (strcasecmp(s->spec, spec) == 0)
  840. return s;
  841. return NULL;
  842. }
  843. static struct script_spec *script_spec__findnew(const char *spec,
  844. struct scripting_ops *ops)
  845. {
  846. struct script_spec *s = script_spec__find(spec);
  847. if (s)
  848. return s;
  849. s = script_spec__new(spec, ops);
  850. if (!s)
  851. return NULL;
  852. script_spec__add(s);
  853. return s;
  854. }
  855. int script_spec_register(const char *spec, struct scripting_ops *ops)
  856. {
  857. struct script_spec *s;
  858. s = script_spec__find(spec);
  859. if (s)
  860. return -1;
  861. s = script_spec__findnew(spec, ops);
  862. if (!s)
  863. return -1;
  864. return 0;
  865. }
  866. static struct scripting_ops *script_spec__lookup(const char *spec)
  867. {
  868. struct script_spec *s = script_spec__find(spec);
  869. if (!s)
  870. return NULL;
  871. return s->ops;
  872. }
  873. static void list_available_languages(void)
  874. {
  875. struct script_spec *s;
  876. fprintf(stderr, "\n");
  877. fprintf(stderr, "Scripting language extensions (used in "
  878. "perf script -s [spec:]script.[spec]):\n\n");
  879. list_for_each_entry(s, &script_specs, node)
  880. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  881. fprintf(stderr, "\n");
  882. }
  883. static int parse_scriptname(const struct option *opt __maybe_unused,
  884. const char *str, int unset __maybe_unused)
  885. {
  886. char spec[PATH_MAX];
  887. const char *script, *ext;
  888. int len;
  889. if (strcmp(str, "lang") == 0) {
  890. list_available_languages();
  891. exit(0);
  892. }
  893. script = strchr(str, ':');
  894. if (script) {
  895. len = script - str;
  896. if (len >= PATH_MAX) {
  897. fprintf(stderr, "invalid language specifier");
  898. return -1;
  899. }
  900. strncpy(spec, str, len);
  901. spec[len] = '\0';
  902. scripting_ops = script_spec__lookup(spec);
  903. if (!scripting_ops) {
  904. fprintf(stderr, "invalid language specifier");
  905. return -1;
  906. }
  907. script++;
  908. } else {
  909. script = str;
  910. ext = strrchr(script, '.');
  911. if (!ext) {
  912. fprintf(stderr, "invalid script extension");
  913. return -1;
  914. }
  915. scripting_ops = script_spec__lookup(++ext);
  916. if (!scripting_ops) {
  917. fprintf(stderr, "invalid script extension");
  918. return -1;
  919. }
  920. }
  921. script_name = strdup(script);
  922. return 0;
  923. }
  924. static int parse_output_fields(const struct option *opt __maybe_unused,
  925. const char *arg, int unset __maybe_unused)
  926. {
  927. char *tok;
  928. int i, imax = ARRAY_SIZE(all_output_options);
  929. int j;
  930. int rc = 0;
  931. char *str = strdup(arg);
  932. int type = -1;
  933. if (!str)
  934. return -ENOMEM;
  935. /* first word can state for which event type the user is specifying
  936. * the fields. If no type exists, the specified fields apply to all
  937. * event types found in the file minus the invalid fields for a type.
  938. */
  939. tok = strchr(str, ':');
  940. if (tok) {
  941. *tok = '\0';
  942. tok++;
  943. if (!strcmp(str, "hw"))
  944. type = PERF_TYPE_HARDWARE;
  945. else if (!strcmp(str, "sw"))
  946. type = PERF_TYPE_SOFTWARE;
  947. else if (!strcmp(str, "trace"))
  948. type = PERF_TYPE_TRACEPOINT;
  949. else if (!strcmp(str, "raw"))
  950. type = PERF_TYPE_RAW;
  951. else if (!strcmp(str, "break"))
  952. type = PERF_TYPE_BREAKPOINT;
  953. else {
  954. fprintf(stderr, "Invalid event type in field string.\n");
  955. rc = -EINVAL;
  956. goto out;
  957. }
  958. if (output[type].user_set)
  959. pr_warning("Overriding previous field request for %s events.\n",
  960. event_type(type));
  961. output[type].fields = 0;
  962. output[type].user_set = true;
  963. output[type].wildcard_set = false;
  964. } else {
  965. tok = str;
  966. if (strlen(str) == 0) {
  967. fprintf(stderr,
  968. "Cannot set fields to 'none' for all event types.\n");
  969. rc = -EINVAL;
  970. goto out;
  971. }
  972. if (output_set_by_user())
  973. pr_warning("Overriding previous field request for all events.\n");
  974. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  975. output[j].fields = 0;
  976. output[j].user_set = true;
  977. output[j].wildcard_set = true;
  978. }
  979. }
  980. for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
  981. for (i = 0; i < imax; ++i) {
  982. if (strcmp(tok, all_output_options[i].str) == 0)
  983. break;
  984. }
  985. if (i == imax && strcmp(tok, "flags") == 0) {
  986. print_flags = true;
  987. continue;
  988. }
  989. if (i == imax) {
  990. fprintf(stderr, "Invalid field requested.\n");
  991. rc = -EINVAL;
  992. goto out;
  993. }
  994. if (type == -1) {
  995. /* add user option to all events types for
  996. * which it is valid
  997. */
  998. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  999. if (output[j].invalid_fields & all_output_options[i].field) {
  1000. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  1001. all_output_options[i].str, event_type(j));
  1002. } else
  1003. output[j].fields |= all_output_options[i].field;
  1004. }
  1005. } else {
  1006. if (output[type].invalid_fields & all_output_options[i].field) {
  1007. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  1008. all_output_options[i].str, event_type(type));
  1009. rc = -EINVAL;
  1010. goto out;
  1011. }
  1012. output[type].fields |= all_output_options[i].field;
  1013. }
  1014. }
  1015. if (type >= 0) {
  1016. if (output[type].fields == 0) {
  1017. pr_debug("No fields requested for %s type. "
  1018. "Events will not be displayed.\n", event_type(type));
  1019. }
  1020. }
  1021. out:
  1022. free(str);
  1023. return rc;
  1024. }
  1025. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  1026. static int is_directory(const char *base_path, const struct dirent *dent)
  1027. {
  1028. char path[PATH_MAX];
  1029. struct stat st;
  1030. sprintf(path, "%s/%s", base_path, dent->d_name);
  1031. if (stat(path, &st))
  1032. return 0;
  1033. return S_ISDIR(st.st_mode);
  1034. }
  1035. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  1036. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  1037. lang_next) \
  1038. if ((lang_dirent.d_type == DT_DIR || \
  1039. (lang_dirent.d_type == DT_UNKNOWN && \
  1040. is_directory(scripts_path, &lang_dirent))) && \
  1041. (strcmp(lang_dirent.d_name, ".")) && \
  1042. (strcmp(lang_dirent.d_name, "..")))
  1043. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  1044. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  1045. script_next) \
  1046. if (script_dirent.d_type != DT_DIR && \
  1047. (script_dirent.d_type != DT_UNKNOWN || \
  1048. !is_directory(lang_path, &script_dirent)))
  1049. #define RECORD_SUFFIX "-record"
  1050. #define REPORT_SUFFIX "-report"
  1051. struct script_desc {
  1052. struct list_head node;
  1053. char *name;
  1054. char *half_liner;
  1055. char *args;
  1056. };
  1057. static LIST_HEAD(script_descs);
  1058. static struct script_desc *script_desc__new(const char *name)
  1059. {
  1060. struct script_desc *s = zalloc(sizeof(*s));
  1061. if (s != NULL && name)
  1062. s->name = strdup(name);
  1063. return s;
  1064. }
  1065. static void script_desc__delete(struct script_desc *s)
  1066. {
  1067. zfree(&s->name);
  1068. zfree(&s->half_liner);
  1069. zfree(&s->args);
  1070. free(s);
  1071. }
  1072. static void script_desc__add(struct script_desc *s)
  1073. {
  1074. list_add_tail(&s->node, &script_descs);
  1075. }
  1076. static struct script_desc *script_desc__find(const char *name)
  1077. {
  1078. struct script_desc *s;
  1079. list_for_each_entry(s, &script_descs, node)
  1080. if (strcasecmp(s->name, name) == 0)
  1081. return s;
  1082. return NULL;
  1083. }
  1084. static struct script_desc *script_desc__findnew(const char *name)
  1085. {
  1086. struct script_desc *s = script_desc__find(name);
  1087. if (s)
  1088. return s;
  1089. s = script_desc__new(name);
  1090. if (!s)
  1091. goto out_delete_desc;
  1092. script_desc__add(s);
  1093. return s;
  1094. out_delete_desc:
  1095. script_desc__delete(s);
  1096. return NULL;
  1097. }
  1098. static const char *ends_with(const char *str, const char *suffix)
  1099. {
  1100. size_t suffix_len = strlen(suffix);
  1101. const char *p = str;
  1102. if (strlen(str) > suffix_len) {
  1103. p = str + strlen(str) - suffix_len;
  1104. if (!strncmp(p, suffix, suffix_len))
  1105. return p;
  1106. }
  1107. return NULL;
  1108. }
  1109. static int read_script_info(struct script_desc *desc, const char *filename)
  1110. {
  1111. char line[BUFSIZ], *p;
  1112. FILE *fp;
  1113. fp = fopen(filename, "r");
  1114. if (!fp)
  1115. return -1;
  1116. while (fgets(line, sizeof(line), fp)) {
  1117. p = ltrim(line);
  1118. if (strlen(p) == 0)
  1119. continue;
  1120. if (*p != '#')
  1121. continue;
  1122. p++;
  1123. if (strlen(p) && *p == '!')
  1124. continue;
  1125. p = ltrim(p);
  1126. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1127. p[strlen(p) - 1] = '\0';
  1128. if (!strncmp(p, "description:", strlen("description:"))) {
  1129. p += strlen("description:");
  1130. desc->half_liner = strdup(ltrim(p));
  1131. continue;
  1132. }
  1133. if (!strncmp(p, "args:", strlen("args:"))) {
  1134. p += strlen("args:");
  1135. desc->args = strdup(ltrim(p));
  1136. continue;
  1137. }
  1138. }
  1139. fclose(fp);
  1140. return 0;
  1141. }
  1142. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1143. {
  1144. char *script_root, *str;
  1145. script_root = strdup(script_dirent->d_name);
  1146. if (!script_root)
  1147. return NULL;
  1148. str = (char *)ends_with(script_root, suffix);
  1149. if (!str) {
  1150. free(script_root);
  1151. return NULL;
  1152. }
  1153. *str = '\0';
  1154. return script_root;
  1155. }
  1156. static int list_available_scripts(const struct option *opt __maybe_unused,
  1157. const char *s __maybe_unused,
  1158. int unset __maybe_unused)
  1159. {
  1160. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1161. char scripts_path[MAXPATHLEN];
  1162. DIR *scripts_dir, *lang_dir;
  1163. char script_path[MAXPATHLEN];
  1164. char lang_path[MAXPATHLEN];
  1165. struct script_desc *desc;
  1166. char first_half[BUFSIZ];
  1167. char *script_root;
  1168. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1169. scripts_dir = opendir(scripts_path);
  1170. if (!scripts_dir)
  1171. return -1;
  1172. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1173. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1174. lang_dirent.d_name);
  1175. lang_dir = opendir(lang_path);
  1176. if (!lang_dir)
  1177. continue;
  1178. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1179. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  1180. if (script_root) {
  1181. desc = script_desc__findnew(script_root);
  1182. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1183. lang_path, script_dirent.d_name);
  1184. read_script_info(desc, script_path);
  1185. free(script_root);
  1186. }
  1187. }
  1188. }
  1189. fprintf(stdout, "List of available trace scripts:\n");
  1190. list_for_each_entry(desc, &script_descs, node) {
  1191. sprintf(first_half, "%s %s", desc->name,
  1192. desc->args ? desc->args : "");
  1193. fprintf(stdout, " %-36s %s\n", first_half,
  1194. desc->half_liner ? desc->half_liner : "");
  1195. }
  1196. exit(0);
  1197. }
  1198. /*
  1199. * Some scripts specify the required events in their "xxx-record" file,
  1200. * this function will check if the events in perf.data match those
  1201. * mentioned in the "xxx-record".
  1202. *
  1203. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  1204. * which is covered well now. And new parsing code should be added to
  1205. * cover the future complexing formats like event groups etc.
  1206. */
  1207. static int check_ev_match(char *dir_name, char *scriptname,
  1208. struct perf_session *session)
  1209. {
  1210. char filename[MAXPATHLEN], evname[128];
  1211. char line[BUFSIZ], *p;
  1212. struct perf_evsel *pos;
  1213. int match, len;
  1214. FILE *fp;
  1215. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  1216. fp = fopen(filename, "r");
  1217. if (!fp)
  1218. return -1;
  1219. while (fgets(line, sizeof(line), fp)) {
  1220. p = ltrim(line);
  1221. if (*p == '#')
  1222. continue;
  1223. while (strlen(p)) {
  1224. p = strstr(p, "-e");
  1225. if (!p)
  1226. break;
  1227. p += 2;
  1228. p = ltrim(p);
  1229. len = strcspn(p, " \t");
  1230. if (!len)
  1231. break;
  1232. snprintf(evname, len + 1, "%s", p);
  1233. match = 0;
  1234. evlist__for_each(session->evlist, pos) {
  1235. if (!strcmp(perf_evsel__name(pos), evname)) {
  1236. match = 1;
  1237. break;
  1238. }
  1239. }
  1240. if (!match) {
  1241. fclose(fp);
  1242. return -1;
  1243. }
  1244. }
  1245. }
  1246. fclose(fp);
  1247. return 0;
  1248. }
  1249. /*
  1250. * Return -1 if none is found, otherwise the actual scripts number.
  1251. *
  1252. * Currently the only user of this function is the script browser, which
  1253. * will list all statically runnable scripts, select one, execute it and
  1254. * show the output in a perf browser.
  1255. */
  1256. int find_scripts(char **scripts_array, char **scripts_path_array)
  1257. {
  1258. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1259. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  1260. DIR *scripts_dir, *lang_dir;
  1261. struct perf_session *session;
  1262. struct perf_data_file file = {
  1263. .path = input_name,
  1264. .mode = PERF_DATA_MODE_READ,
  1265. };
  1266. char *temp;
  1267. int i = 0;
  1268. session = perf_session__new(&file, false, NULL);
  1269. if (!session)
  1270. return -1;
  1271. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1272. scripts_dir = opendir(scripts_path);
  1273. if (!scripts_dir) {
  1274. perf_session__delete(session);
  1275. return -1;
  1276. }
  1277. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1278. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  1279. lang_dirent.d_name);
  1280. #ifdef NO_LIBPERL
  1281. if (strstr(lang_path, "perl"))
  1282. continue;
  1283. #endif
  1284. #ifdef NO_LIBPYTHON
  1285. if (strstr(lang_path, "python"))
  1286. continue;
  1287. #endif
  1288. lang_dir = opendir(lang_path);
  1289. if (!lang_dir)
  1290. continue;
  1291. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1292. /* Skip those real time scripts: xxxtop.p[yl] */
  1293. if (strstr(script_dirent.d_name, "top."))
  1294. continue;
  1295. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  1296. script_dirent.d_name);
  1297. temp = strchr(script_dirent.d_name, '.');
  1298. snprintf(scripts_array[i],
  1299. (temp - script_dirent.d_name) + 1,
  1300. "%s", script_dirent.d_name);
  1301. if (check_ev_match(lang_path,
  1302. scripts_array[i], session))
  1303. continue;
  1304. i++;
  1305. }
  1306. closedir(lang_dir);
  1307. }
  1308. closedir(scripts_dir);
  1309. perf_session__delete(session);
  1310. return i;
  1311. }
  1312. static char *get_script_path(const char *script_root, const char *suffix)
  1313. {
  1314. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1315. char scripts_path[MAXPATHLEN];
  1316. char script_path[MAXPATHLEN];
  1317. DIR *scripts_dir, *lang_dir;
  1318. char lang_path[MAXPATHLEN];
  1319. char *__script_root;
  1320. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1321. scripts_dir = opendir(scripts_path);
  1322. if (!scripts_dir)
  1323. return NULL;
  1324. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1325. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1326. lang_dirent.d_name);
  1327. lang_dir = opendir(lang_path);
  1328. if (!lang_dir)
  1329. continue;
  1330. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1331. __script_root = get_script_root(&script_dirent, suffix);
  1332. if (__script_root && !strcmp(script_root, __script_root)) {
  1333. free(__script_root);
  1334. closedir(lang_dir);
  1335. closedir(scripts_dir);
  1336. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1337. lang_path, script_dirent.d_name);
  1338. return strdup(script_path);
  1339. }
  1340. free(__script_root);
  1341. }
  1342. closedir(lang_dir);
  1343. }
  1344. closedir(scripts_dir);
  1345. return NULL;
  1346. }
  1347. static bool is_top_script(const char *script_path)
  1348. {
  1349. return ends_with(script_path, "top") == NULL ? false : true;
  1350. }
  1351. static int has_required_arg(char *script_path)
  1352. {
  1353. struct script_desc *desc;
  1354. int n_args = 0;
  1355. char *p;
  1356. desc = script_desc__new(NULL);
  1357. if (read_script_info(desc, script_path))
  1358. goto out;
  1359. if (!desc->args)
  1360. goto out;
  1361. for (p = desc->args; *p; p++)
  1362. if (*p == '<')
  1363. n_args++;
  1364. out:
  1365. script_desc__delete(desc);
  1366. return n_args;
  1367. }
  1368. static int have_cmd(int argc, const char **argv)
  1369. {
  1370. char **__argv = malloc(sizeof(const char *) * argc);
  1371. if (!__argv) {
  1372. pr_err("malloc failed\n");
  1373. return -1;
  1374. }
  1375. memcpy(__argv, argv, sizeof(const char *) * argc);
  1376. argc = parse_options(argc, (const char **)__argv, record_options,
  1377. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1378. free(__argv);
  1379. system_wide = (argc == 0);
  1380. return 0;
  1381. }
  1382. static void script__setup_sample_type(struct perf_script *script)
  1383. {
  1384. struct perf_session *session = script->session;
  1385. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  1386. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  1387. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  1388. (sample_type & PERF_SAMPLE_STACK_USER))
  1389. callchain_param.record_mode = CALLCHAIN_DWARF;
  1390. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  1391. callchain_param.record_mode = CALLCHAIN_LBR;
  1392. else
  1393. callchain_param.record_mode = CALLCHAIN_FP;
  1394. }
  1395. }
  1396. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1397. {
  1398. bool show_full_info = false;
  1399. bool header = false;
  1400. bool header_only = false;
  1401. bool script_started = false;
  1402. char *rec_script_path = NULL;
  1403. char *rep_script_path = NULL;
  1404. struct perf_session *session;
  1405. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  1406. char *script_path = NULL;
  1407. const char **__argv;
  1408. int i, j, err = 0;
  1409. struct perf_script script = {
  1410. .tool = {
  1411. .sample = process_sample_event,
  1412. .mmap = perf_event__process_mmap,
  1413. .mmap2 = perf_event__process_mmap2,
  1414. .comm = perf_event__process_comm,
  1415. .exit = perf_event__process_exit,
  1416. .fork = perf_event__process_fork,
  1417. .attr = process_attr,
  1418. .tracing_data = perf_event__process_tracing_data,
  1419. .build_id = perf_event__process_build_id,
  1420. .id_index = perf_event__process_id_index,
  1421. .auxtrace_info = perf_event__process_auxtrace_info,
  1422. .auxtrace = perf_event__process_auxtrace,
  1423. .auxtrace_error = perf_event__process_auxtrace_error,
  1424. .ordered_events = true,
  1425. .ordering_requires_timestamps = true,
  1426. },
  1427. };
  1428. struct perf_data_file file = {
  1429. .mode = PERF_DATA_MODE_READ,
  1430. };
  1431. const struct option options[] = {
  1432. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1433. "dump raw trace in ASCII"),
  1434. OPT_INCR('v', "verbose", &verbose,
  1435. "be more verbose (show symbol address, etc)"),
  1436. OPT_BOOLEAN('L', "Latency", &latency_format,
  1437. "show latency attributes (irqs/preemption disabled, etc)"),
  1438. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1439. list_available_scripts),
  1440. OPT_CALLBACK('s', "script", NULL, "name",
  1441. "script file name (lang:script name, script name, or *)",
  1442. parse_scriptname),
  1443. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1444. "generate perf-script.xx script in specified language"),
  1445. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1446. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1447. "do various checks like samples ordering and lost events"),
  1448. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  1449. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  1450. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1451. "file", "vmlinux pathname"),
  1452. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1453. "file", "kallsyms pathname"),
  1454. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1455. "When printing symbols do not display call chain"),
  1456. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1457. "Look for files with symbols relative to this directory"),
  1458. OPT_CALLBACK('F', "fields", NULL, "str",
  1459. "comma separated output fields prepend with 'type:'. "
  1460. "Valid types: hw,sw,trace,raw. "
  1461. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1462. "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
  1463. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1464. "system-wide collection from all CPUs"),
  1465. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1466. "only consider these symbols"),
  1467. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1468. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1469. "only display events for these comms"),
  1470. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1471. "only consider symbols in these pids"),
  1472. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1473. "only consider symbols in these tids"),
  1474. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1475. "display extended information from perf.data file"),
  1476. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1477. "Show the path of [kernel.kallsyms]"),
  1478. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  1479. "Show the fork/comm/exit events"),
  1480. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  1481. "Show the mmap events"),
  1482. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  1483. "Show context switch events (if recorded)"),
  1484. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1485. OPT_BOOLEAN(0, "ns", &nanosecs,
  1486. "Use 9 decimal places when displaying time"),
  1487. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1488. "Instruction Tracing options",
  1489. itrace_parse_synth_opts),
  1490. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1491. "Show full source file name path for source lines"),
  1492. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  1493. "Enable symbol demangling"),
  1494. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1495. "Enable kernel symbol demangling"),
  1496. OPT_END()
  1497. };
  1498. const char * const script_subcommands[] = { "record", "report", NULL };
  1499. const char *script_usage[] = {
  1500. "perf script [<options>]",
  1501. "perf script [<options>] record <script> [<record-options>] <command>",
  1502. "perf script [<options>] report <script> [script-args]",
  1503. "perf script [<options>] <script> [<record-options>] <command>",
  1504. "perf script [<options>] <top-script> [script-args]",
  1505. NULL
  1506. };
  1507. setup_scripting();
  1508. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  1509. PARSE_OPT_STOP_AT_NON_OPTION);
  1510. file.path = input_name;
  1511. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1512. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1513. if (!rec_script_path)
  1514. return cmd_record(argc, argv, NULL);
  1515. }
  1516. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1517. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1518. if (!rep_script_path) {
  1519. fprintf(stderr,
  1520. "Please specify a valid report script"
  1521. "(see 'perf script -l' for listing)\n");
  1522. return -1;
  1523. }
  1524. }
  1525. if (itrace_synth_opts.callchain &&
  1526. itrace_synth_opts.callchain_sz > scripting_max_stack)
  1527. scripting_max_stack = itrace_synth_opts.callchain_sz;
  1528. /* make sure PERF_EXEC_PATH is set for scripts */
  1529. perf_set_argv_exec_path(perf_exec_path());
  1530. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1531. int live_pipe[2];
  1532. int rep_args;
  1533. pid_t pid;
  1534. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1535. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1536. if (!rec_script_path && !rep_script_path) {
  1537. usage_with_options_msg(script_usage, options,
  1538. "Couldn't find script `%s'\n\n See perf"
  1539. " script -l for available scripts.\n", argv[0]);
  1540. }
  1541. if (is_top_script(argv[0])) {
  1542. rep_args = argc - 1;
  1543. } else {
  1544. int rec_args;
  1545. rep_args = has_required_arg(rep_script_path);
  1546. rec_args = (argc - 1) - rep_args;
  1547. if (rec_args < 0) {
  1548. usage_with_options_msg(script_usage, options,
  1549. "`%s' script requires options."
  1550. "\n\n See perf script -l for available "
  1551. "scripts and options.\n", argv[0]);
  1552. }
  1553. }
  1554. if (pipe(live_pipe) < 0) {
  1555. perror("failed to create pipe");
  1556. return -1;
  1557. }
  1558. pid = fork();
  1559. if (pid < 0) {
  1560. perror("failed to fork");
  1561. return -1;
  1562. }
  1563. if (!pid) {
  1564. j = 0;
  1565. dup2(live_pipe[1], 1);
  1566. close(live_pipe[0]);
  1567. if (is_top_script(argv[0])) {
  1568. system_wide = true;
  1569. } else if (!system_wide) {
  1570. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1571. err = -1;
  1572. goto out;
  1573. }
  1574. }
  1575. __argv = malloc((argc + 6) * sizeof(const char *));
  1576. if (!__argv) {
  1577. pr_err("malloc failed\n");
  1578. err = -ENOMEM;
  1579. goto out;
  1580. }
  1581. __argv[j++] = "/bin/sh";
  1582. __argv[j++] = rec_script_path;
  1583. if (system_wide)
  1584. __argv[j++] = "-a";
  1585. __argv[j++] = "-q";
  1586. __argv[j++] = "-o";
  1587. __argv[j++] = "-";
  1588. for (i = rep_args + 1; i < argc; i++)
  1589. __argv[j++] = argv[i];
  1590. __argv[j++] = NULL;
  1591. execvp("/bin/sh", (char **)__argv);
  1592. free(__argv);
  1593. exit(-1);
  1594. }
  1595. dup2(live_pipe[0], 0);
  1596. close(live_pipe[1]);
  1597. __argv = malloc((argc + 4) * sizeof(const char *));
  1598. if (!__argv) {
  1599. pr_err("malloc failed\n");
  1600. err = -ENOMEM;
  1601. goto out;
  1602. }
  1603. j = 0;
  1604. __argv[j++] = "/bin/sh";
  1605. __argv[j++] = rep_script_path;
  1606. for (i = 1; i < rep_args + 1; i++)
  1607. __argv[j++] = argv[i];
  1608. __argv[j++] = "-i";
  1609. __argv[j++] = "-";
  1610. __argv[j++] = NULL;
  1611. execvp("/bin/sh", (char **)__argv);
  1612. free(__argv);
  1613. exit(-1);
  1614. }
  1615. if (rec_script_path)
  1616. script_path = rec_script_path;
  1617. if (rep_script_path)
  1618. script_path = rep_script_path;
  1619. if (script_path) {
  1620. j = 0;
  1621. if (!rec_script_path)
  1622. system_wide = false;
  1623. else if (!system_wide) {
  1624. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1625. err = -1;
  1626. goto out;
  1627. }
  1628. }
  1629. __argv = malloc((argc + 2) * sizeof(const char *));
  1630. if (!__argv) {
  1631. pr_err("malloc failed\n");
  1632. err = -ENOMEM;
  1633. goto out;
  1634. }
  1635. __argv[j++] = "/bin/sh";
  1636. __argv[j++] = script_path;
  1637. if (system_wide)
  1638. __argv[j++] = "-a";
  1639. for (i = 2; i < argc; i++)
  1640. __argv[j++] = argv[i];
  1641. __argv[j++] = NULL;
  1642. execvp("/bin/sh", (char **)__argv);
  1643. free(__argv);
  1644. exit(-1);
  1645. }
  1646. if (!script_name)
  1647. setup_pager();
  1648. session = perf_session__new(&file, false, &script.tool);
  1649. if (session == NULL)
  1650. return -1;
  1651. if (header || header_only) {
  1652. perf_session__fprintf_info(session, stdout, show_full_info);
  1653. if (header_only)
  1654. goto out_delete;
  1655. }
  1656. if (symbol__init(&session->header.env) < 0)
  1657. goto out_delete;
  1658. script.session = session;
  1659. script__setup_sample_type(&script);
  1660. session->itrace_synth_opts = &itrace_synth_opts;
  1661. if (cpu_list) {
  1662. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  1663. if (err < 0)
  1664. goto out_delete;
  1665. }
  1666. if (!no_callchain)
  1667. symbol_conf.use_callchain = true;
  1668. else
  1669. symbol_conf.use_callchain = false;
  1670. if (session->tevent.pevent &&
  1671. pevent_set_function_resolver(session->tevent.pevent,
  1672. machine__resolve_kernel_addr,
  1673. &session->machines.host) < 0) {
  1674. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  1675. return -1;
  1676. }
  1677. if (generate_script_lang) {
  1678. struct stat perf_stat;
  1679. int input;
  1680. if (output_set_by_user()) {
  1681. fprintf(stderr,
  1682. "custom fields not supported for generated scripts");
  1683. err = -EINVAL;
  1684. goto out_delete;
  1685. }
  1686. input = open(file.path, O_RDONLY); /* input_name */
  1687. if (input < 0) {
  1688. err = -errno;
  1689. perror("failed to open file");
  1690. goto out_delete;
  1691. }
  1692. err = fstat(input, &perf_stat);
  1693. if (err < 0) {
  1694. perror("failed to stat file");
  1695. goto out_delete;
  1696. }
  1697. if (!perf_stat.st_size) {
  1698. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1699. goto out_delete;
  1700. }
  1701. scripting_ops = script_spec__lookup(generate_script_lang);
  1702. if (!scripting_ops) {
  1703. fprintf(stderr, "invalid language specifier");
  1704. err = -ENOENT;
  1705. goto out_delete;
  1706. }
  1707. err = scripting_ops->generate_script(session->tevent.pevent,
  1708. "perf-script");
  1709. goto out_delete;
  1710. }
  1711. if (script_name) {
  1712. err = scripting_ops->start_script(script_name, argc, argv);
  1713. if (err)
  1714. goto out_delete;
  1715. pr_debug("perf script started with script %s\n\n", script_name);
  1716. script_started = true;
  1717. }
  1718. err = perf_session__check_output_opt(session);
  1719. if (err < 0)
  1720. goto out_delete;
  1721. err = __cmd_script(&script);
  1722. flush_scripting();
  1723. out_delete:
  1724. perf_session__delete(session);
  1725. if (script_started)
  1726. cleanup_scripting();
  1727. out:
  1728. return err;
  1729. }