builtin-script.c 49 KB

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