builtin-script.c 43 KB

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