builtin-script.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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 <linux/bitmap.h>
  17. static char const *script_name;
  18. static char const *generate_script_lang;
  19. static bool debug_mode;
  20. static u64 last_timestamp;
  21. static u64 nr_unordered;
  22. extern const struct option record_options[];
  23. static bool no_callchain;
  24. static bool show_full_info;
  25. static bool system_wide;
  26. static const char *cpu_list;
  27. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  28. enum perf_output_field {
  29. PERF_OUTPUT_COMM = 1U << 0,
  30. PERF_OUTPUT_TID = 1U << 1,
  31. PERF_OUTPUT_PID = 1U << 2,
  32. PERF_OUTPUT_TIME = 1U << 3,
  33. PERF_OUTPUT_CPU = 1U << 4,
  34. PERF_OUTPUT_EVNAME = 1U << 5,
  35. PERF_OUTPUT_TRACE = 1U << 6,
  36. PERF_OUTPUT_IP = 1U << 7,
  37. PERF_OUTPUT_SYM = 1U << 8,
  38. PERF_OUTPUT_DSO = 1U << 9,
  39. PERF_OUTPUT_ADDR = 1U << 10,
  40. PERF_OUTPUT_SYMOFFSET = 1U << 11,
  41. };
  42. struct output_option {
  43. const char *str;
  44. enum perf_output_field field;
  45. } all_output_options[] = {
  46. {.str = "comm", .field = PERF_OUTPUT_COMM},
  47. {.str = "tid", .field = PERF_OUTPUT_TID},
  48. {.str = "pid", .field = PERF_OUTPUT_PID},
  49. {.str = "time", .field = PERF_OUTPUT_TIME},
  50. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  51. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  52. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  53. {.str = "ip", .field = PERF_OUTPUT_IP},
  54. {.str = "sym", .field = PERF_OUTPUT_SYM},
  55. {.str = "dso", .field = PERF_OUTPUT_DSO},
  56. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  57. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  58. };
  59. /* default set to maintain compatibility with current format */
  60. static struct {
  61. bool user_set;
  62. bool wildcard_set;
  63. u64 fields;
  64. u64 invalid_fields;
  65. } output[PERF_TYPE_MAX] = {
  66. [PERF_TYPE_HARDWARE] = {
  67. .user_set = false,
  68. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  69. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  70. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  71. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
  72. .invalid_fields = PERF_OUTPUT_TRACE,
  73. },
  74. [PERF_TYPE_SOFTWARE] = {
  75. .user_set = false,
  76. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  77. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  78. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  79. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
  80. .invalid_fields = PERF_OUTPUT_TRACE,
  81. },
  82. [PERF_TYPE_TRACEPOINT] = {
  83. .user_set = false,
  84. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  85. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  86. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
  87. },
  88. [PERF_TYPE_RAW] = {
  89. .user_set = false,
  90. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  91. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  92. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  93. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
  94. .invalid_fields = PERF_OUTPUT_TRACE,
  95. },
  96. };
  97. static bool output_set_by_user(void)
  98. {
  99. int j;
  100. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  101. if (output[j].user_set)
  102. return true;
  103. }
  104. return false;
  105. }
  106. static const char *output_field2str(enum perf_output_field field)
  107. {
  108. int i, imax = ARRAY_SIZE(all_output_options);
  109. const char *str = "";
  110. for (i = 0; i < imax; ++i) {
  111. if (all_output_options[i].field == field) {
  112. str = all_output_options[i].str;
  113. break;
  114. }
  115. }
  116. return str;
  117. }
  118. #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
  119. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  120. u64 sample_type, const char *sample_msg,
  121. enum perf_output_field field)
  122. {
  123. struct perf_event_attr *attr = &evsel->attr;
  124. int type = attr->type;
  125. const char *evname;
  126. if (attr->sample_type & sample_type)
  127. return 0;
  128. if (output[type].user_set) {
  129. evname = perf_evsel__name(evsel);
  130. pr_err("Samples for '%s' event do not have %s attribute set. "
  131. "Cannot print '%s' field.\n",
  132. evname, sample_msg, output_field2str(field));
  133. return -1;
  134. }
  135. /* user did not ask for it explicitly so remove from the default list */
  136. output[type].fields &= ~field;
  137. evname = perf_evsel__name(evsel);
  138. pr_debug("Samples for '%s' event do not have %s attribute set. "
  139. "Skipping '%s' field.\n",
  140. evname, sample_msg, output_field2str(field));
  141. return 0;
  142. }
  143. static int perf_evsel__check_attr(struct perf_evsel *evsel,
  144. struct perf_session *session)
  145. {
  146. struct perf_event_attr *attr = &evsel->attr;
  147. if (PRINT_FIELD(TRACE) &&
  148. !perf_session__has_traces(session, "record -R"))
  149. return -EINVAL;
  150. if (PRINT_FIELD(IP)) {
  151. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
  152. PERF_OUTPUT_IP))
  153. return -EINVAL;
  154. if (!no_callchain &&
  155. !(attr->sample_type & PERF_SAMPLE_CALLCHAIN))
  156. symbol_conf.use_callchain = false;
  157. }
  158. if (PRINT_FIELD(ADDR) &&
  159. perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
  160. PERF_OUTPUT_ADDR))
  161. return -EINVAL;
  162. if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  163. pr_err("Display of symbols requested but neither sample IP nor "
  164. "sample address\nis selected. Hence, no addresses to convert "
  165. "to symbols.\n");
  166. return -EINVAL;
  167. }
  168. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  169. pr_err("Display of offsets requested but symbol is not"
  170. "selected.\n");
  171. return -EINVAL;
  172. }
  173. if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  174. pr_err("Display of DSO requested but neither sample IP nor "
  175. "sample address\nis selected. Hence, no addresses to convert "
  176. "to DSO.\n");
  177. return -EINVAL;
  178. }
  179. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  180. perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
  181. PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  182. return -EINVAL;
  183. if (PRINT_FIELD(TIME) &&
  184. perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
  185. PERF_OUTPUT_TIME))
  186. return -EINVAL;
  187. if (PRINT_FIELD(CPU) &&
  188. perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
  189. PERF_OUTPUT_CPU))
  190. return -EINVAL;
  191. return 0;
  192. }
  193. /*
  194. * verify all user requested events exist and the samples
  195. * have the expected data
  196. */
  197. static int perf_session__check_output_opt(struct perf_session *session)
  198. {
  199. int j;
  200. struct perf_evsel *evsel;
  201. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  202. evsel = perf_session__find_first_evtype(session, j);
  203. /*
  204. * even if fields is set to 0 (ie., show nothing) event must
  205. * exist if user explicitly includes it on the command line
  206. */
  207. if (!evsel && output[j].user_set && !output[j].wildcard_set) {
  208. pr_err("%s events do not exist. "
  209. "Remove corresponding -f option to proceed.\n",
  210. event_type(j));
  211. return -1;
  212. }
  213. if (evsel && output[j].fields &&
  214. perf_evsel__check_attr(evsel, session))
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. static void print_sample_start(struct perf_sample *sample,
  220. struct thread *thread,
  221. struct perf_evsel *evsel)
  222. {
  223. struct perf_event_attr *attr = &evsel->attr;
  224. const char *evname = NULL;
  225. unsigned long secs;
  226. unsigned long usecs;
  227. unsigned long long nsecs;
  228. if (PRINT_FIELD(COMM)) {
  229. if (latency_format)
  230. printf("%8.8s ", thread->comm);
  231. else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
  232. printf("%s ", thread->comm);
  233. else
  234. printf("%16s ", thread->comm);
  235. }
  236. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  237. printf("%5d/%-5d ", sample->pid, sample->tid);
  238. else if (PRINT_FIELD(PID))
  239. printf("%5d ", sample->pid);
  240. else if (PRINT_FIELD(TID))
  241. printf("%5d ", sample->tid);
  242. if (PRINT_FIELD(CPU)) {
  243. if (latency_format)
  244. printf("%3d ", sample->cpu);
  245. else
  246. printf("[%03d] ", sample->cpu);
  247. }
  248. if (PRINT_FIELD(TIME)) {
  249. nsecs = sample->time;
  250. secs = nsecs / NSECS_PER_SEC;
  251. nsecs -= secs * NSECS_PER_SEC;
  252. usecs = nsecs / NSECS_PER_USEC;
  253. printf("%5lu.%06lu: ", secs, usecs);
  254. }
  255. if (PRINT_FIELD(EVNAME)) {
  256. evname = perf_evsel__name(evsel);
  257. printf("%s: ", evname ? evname : "[unknown]");
  258. }
  259. }
  260. static bool is_bts_event(struct perf_event_attr *attr)
  261. {
  262. return ((attr->type == PERF_TYPE_HARDWARE) &&
  263. (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  264. (attr->sample_period == 1));
  265. }
  266. static bool sample_addr_correlates_sym(struct perf_event_attr *attr)
  267. {
  268. if ((attr->type == PERF_TYPE_SOFTWARE) &&
  269. ((attr->config == PERF_COUNT_SW_PAGE_FAULTS) ||
  270. (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN) ||
  271. (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)))
  272. return true;
  273. if (is_bts_event(attr))
  274. return true;
  275. return false;
  276. }
  277. static void print_sample_addr(union perf_event *event,
  278. struct perf_sample *sample,
  279. struct machine *machine,
  280. struct thread *thread,
  281. struct perf_event_attr *attr)
  282. {
  283. struct addr_location al;
  284. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  285. printf("%16" PRIx64, sample->addr);
  286. if (!sample_addr_correlates_sym(attr))
  287. return;
  288. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  289. sample->addr, &al);
  290. if (!al.map)
  291. thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
  292. sample->addr, &al);
  293. al.cpu = sample->cpu;
  294. al.sym = NULL;
  295. if (al.map)
  296. al.sym = map__find_symbol(al.map, al.addr, NULL);
  297. if (PRINT_FIELD(SYM)) {
  298. printf(" ");
  299. if (PRINT_FIELD(SYMOFFSET))
  300. symbol__fprintf_symname_offs(al.sym, &al, stdout);
  301. else
  302. symbol__fprintf_symname(al.sym, stdout);
  303. }
  304. if (PRINT_FIELD(DSO)) {
  305. printf(" (");
  306. map__fprintf_dsoname(al.map, stdout);
  307. printf(")");
  308. }
  309. }
  310. static void print_sample_bts(union perf_event *event,
  311. struct perf_sample *sample,
  312. struct perf_evsel *evsel,
  313. struct machine *machine,
  314. struct thread *thread)
  315. {
  316. struct perf_event_attr *attr = &evsel->attr;
  317. /* print branch_from information */
  318. if (PRINT_FIELD(IP)) {
  319. if (!symbol_conf.use_callchain)
  320. printf(" ");
  321. else
  322. printf("\n");
  323. perf_event__print_ip(event, sample, machine,
  324. PRINT_FIELD(SYM), PRINT_FIELD(DSO),
  325. PRINT_FIELD(SYMOFFSET));
  326. }
  327. printf(" => ");
  328. /* print branch_to information */
  329. if (PRINT_FIELD(ADDR))
  330. print_sample_addr(event, sample, machine, thread, attr);
  331. printf("\n");
  332. }
  333. static void process_event(union perf_event *event, struct perf_sample *sample,
  334. struct perf_evsel *evsel, struct machine *machine,
  335. struct thread *thread)
  336. {
  337. struct perf_event_attr *attr = &evsel->attr;
  338. if (output[attr->type].fields == 0)
  339. return;
  340. print_sample_start(sample, thread, evsel);
  341. if (is_bts_event(attr)) {
  342. print_sample_bts(event, sample, evsel, machine, thread);
  343. return;
  344. }
  345. if (PRINT_FIELD(TRACE))
  346. event_format__print(evsel->tp_format, sample->cpu,
  347. sample->raw_data, sample->raw_size);
  348. if (PRINT_FIELD(ADDR))
  349. print_sample_addr(event, sample, machine, thread, attr);
  350. if (PRINT_FIELD(IP)) {
  351. if (!symbol_conf.use_callchain)
  352. printf(" ");
  353. else
  354. printf("\n");
  355. perf_event__print_ip(event, sample, machine,
  356. PRINT_FIELD(SYM), PRINT_FIELD(DSO),
  357. PRINT_FIELD(SYMOFFSET));
  358. }
  359. printf("\n");
  360. }
  361. static int default_start_script(const char *script __unused,
  362. int argc __unused,
  363. const char **argv __unused)
  364. {
  365. return 0;
  366. }
  367. static int default_stop_script(void)
  368. {
  369. return 0;
  370. }
  371. static int default_generate_script(struct pevent *pevent __unused,
  372. const char *outfile __unused)
  373. {
  374. return 0;
  375. }
  376. static struct scripting_ops default_scripting_ops = {
  377. .start_script = default_start_script,
  378. .stop_script = default_stop_script,
  379. .process_event = process_event,
  380. .generate_script = default_generate_script,
  381. };
  382. static struct scripting_ops *scripting_ops;
  383. static void setup_scripting(void)
  384. {
  385. setup_perl_scripting();
  386. setup_python_scripting();
  387. scripting_ops = &default_scripting_ops;
  388. }
  389. static int cleanup_scripting(void)
  390. {
  391. pr_debug("\nperf script stopped\n");
  392. return scripting_ops->stop_script();
  393. }
  394. static const char *input_name;
  395. static int process_sample_event(struct perf_tool *tool __used,
  396. union perf_event *event,
  397. struct perf_sample *sample,
  398. struct perf_evsel *evsel,
  399. struct machine *machine)
  400. {
  401. struct addr_location al;
  402. struct thread *thread = machine__findnew_thread(machine, event->ip.tid);
  403. if (thread == NULL) {
  404. pr_debug("problem processing %d event, skipping it.\n",
  405. event->header.type);
  406. return -1;
  407. }
  408. if (debug_mode) {
  409. if (sample->time < last_timestamp) {
  410. pr_err("Samples misordered, previous: %" PRIu64
  411. " this: %" PRIu64 "\n", last_timestamp,
  412. sample->time);
  413. nr_unordered++;
  414. }
  415. last_timestamp = sample->time;
  416. return 0;
  417. }
  418. if (perf_event__preprocess_sample(event, machine, &al, sample, 0) < 0) {
  419. pr_err("problem processing %d event, skipping it.\n",
  420. event->header.type);
  421. return -1;
  422. }
  423. if (al.filtered)
  424. return 0;
  425. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  426. return 0;
  427. scripting_ops->process_event(event, sample, evsel, machine, thread);
  428. evsel->hists.stats.total_period += sample->period;
  429. return 0;
  430. }
  431. static struct perf_tool perf_script = {
  432. .sample = process_sample_event,
  433. .mmap = perf_event__process_mmap,
  434. .comm = perf_event__process_comm,
  435. .exit = perf_event__process_task,
  436. .fork = perf_event__process_task,
  437. .attr = perf_event__process_attr,
  438. .event_type = perf_event__process_event_type,
  439. .tracing_data = perf_event__process_tracing_data,
  440. .build_id = perf_event__process_build_id,
  441. .ordered_samples = true,
  442. .ordering_requires_timestamps = true,
  443. };
  444. extern volatile int session_done;
  445. static void sig_handler(int sig __unused)
  446. {
  447. session_done = 1;
  448. }
  449. static int __cmd_script(struct perf_session *session)
  450. {
  451. int ret;
  452. signal(SIGINT, sig_handler);
  453. ret = perf_session__process_events(session, &perf_script);
  454. if (debug_mode)
  455. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  456. return ret;
  457. }
  458. struct script_spec {
  459. struct list_head node;
  460. struct scripting_ops *ops;
  461. char spec[0];
  462. };
  463. static LIST_HEAD(script_specs);
  464. static struct script_spec *script_spec__new(const char *spec,
  465. struct scripting_ops *ops)
  466. {
  467. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  468. if (s != NULL) {
  469. strcpy(s->spec, spec);
  470. s->ops = ops;
  471. }
  472. return s;
  473. }
  474. static void script_spec__add(struct script_spec *s)
  475. {
  476. list_add_tail(&s->node, &script_specs);
  477. }
  478. static struct script_spec *script_spec__find(const char *spec)
  479. {
  480. struct script_spec *s;
  481. list_for_each_entry(s, &script_specs, node)
  482. if (strcasecmp(s->spec, spec) == 0)
  483. return s;
  484. return NULL;
  485. }
  486. static struct script_spec *script_spec__findnew(const char *spec,
  487. struct scripting_ops *ops)
  488. {
  489. struct script_spec *s = script_spec__find(spec);
  490. if (s)
  491. return s;
  492. s = script_spec__new(spec, ops);
  493. if (!s)
  494. return NULL;
  495. script_spec__add(s);
  496. return s;
  497. }
  498. int script_spec_register(const char *spec, struct scripting_ops *ops)
  499. {
  500. struct script_spec *s;
  501. s = script_spec__find(spec);
  502. if (s)
  503. return -1;
  504. s = script_spec__findnew(spec, ops);
  505. if (!s)
  506. return -1;
  507. return 0;
  508. }
  509. static struct scripting_ops *script_spec__lookup(const char *spec)
  510. {
  511. struct script_spec *s = script_spec__find(spec);
  512. if (!s)
  513. return NULL;
  514. return s->ops;
  515. }
  516. static void list_available_languages(void)
  517. {
  518. struct script_spec *s;
  519. fprintf(stderr, "\n");
  520. fprintf(stderr, "Scripting language extensions (used in "
  521. "perf script -s [spec:]script.[spec]):\n\n");
  522. list_for_each_entry(s, &script_specs, node)
  523. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  524. fprintf(stderr, "\n");
  525. }
  526. static int parse_scriptname(const struct option *opt __used,
  527. const char *str, int unset __used)
  528. {
  529. char spec[PATH_MAX];
  530. const char *script, *ext;
  531. int len;
  532. if (strcmp(str, "lang") == 0) {
  533. list_available_languages();
  534. exit(0);
  535. }
  536. script = strchr(str, ':');
  537. if (script) {
  538. len = script - str;
  539. if (len >= PATH_MAX) {
  540. fprintf(stderr, "invalid language specifier");
  541. return -1;
  542. }
  543. strncpy(spec, str, len);
  544. spec[len] = '\0';
  545. scripting_ops = script_spec__lookup(spec);
  546. if (!scripting_ops) {
  547. fprintf(stderr, "invalid language specifier");
  548. return -1;
  549. }
  550. script++;
  551. } else {
  552. script = str;
  553. ext = strrchr(script, '.');
  554. if (!ext) {
  555. fprintf(stderr, "invalid script extension");
  556. return -1;
  557. }
  558. scripting_ops = script_spec__lookup(++ext);
  559. if (!scripting_ops) {
  560. fprintf(stderr, "invalid script extension");
  561. return -1;
  562. }
  563. }
  564. script_name = strdup(script);
  565. return 0;
  566. }
  567. static int parse_output_fields(const struct option *opt __used,
  568. const char *arg, int unset __used)
  569. {
  570. char *tok;
  571. int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
  572. int j;
  573. int rc = 0;
  574. char *str = strdup(arg);
  575. int type = -1;
  576. if (!str)
  577. return -ENOMEM;
  578. /* first word can state for which event type the user is specifying
  579. * the fields. If no type exists, the specified fields apply to all
  580. * event types found in the file minus the invalid fields for a type.
  581. */
  582. tok = strchr(str, ':');
  583. if (tok) {
  584. *tok = '\0';
  585. tok++;
  586. if (!strcmp(str, "hw"))
  587. type = PERF_TYPE_HARDWARE;
  588. else if (!strcmp(str, "sw"))
  589. type = PERF_TYPE_SOFTWARE;
  590. else if (!strcmp(str, "trace"))
  591. type = PERF_TYPE_TRACEPOINT;
  592. else if (!strcmp(str, "raw"))
  593. type = PERF_TYPE_RAW;
  594. else {
  595. fprintf(stderr, "Invalid event type in field string.\n");
  596. rc = -EINVAL;
  597. goto out;
  598. }
  599. if (output[type].user_set)
  600. pr_warning("Overriding previous field request for %s events.\n",
  601. event_type(type));
  602. output[type].fields = 0;
  603. output[type].user_set = true;
  604. output[type].wildcard_set = false;
  605. } else {
  606. tok = str;
  607. if (strlen(str) == 0) {
  608. fprintf(stderr,
  609. "Cannot set fields to 'none' for all event types.\n");
  610. rc = -EINVAL;
  611. goto out;
  612. }
  613. if (output_set_by_user())
  614. pr_warning("Overriding previous field request for all events.\n");
  615. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  616. output[j].fields = 0;
  617. output[j].user_set = true;
  618. output[j].wildcard_set = true;
  619. }
  620. }
  621. tok = strtok(tok, ",");
  622. while (tok) {
  623. for (i = 0; i < imax; ++i) {
  624. if (strcmp(tok, all_output_options[i].str) == 0)
  625. break;
  626. }
  627. if (i == imax) {
  628. fprintf(stderr, "Invalid field requested.\n");
  629. rc = -EINVAL;
  630. goto out;
  631. }
  632. if (type == -1) {
  633. /* add user option to all events types for
  634. * which it is valid
  635. */
  636. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  637. if (output[j].invalid_fields & all_output_options[i].field) {
  638. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  639. all_output_options[i].str, event_type(j));
  640. } else
  641. output[j].fields |= all_output_options[i].field;
  642. }
  643. } else {
  644. if (output[type].invalid_fields & all_output_options[i].field) {
  645. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  646. all_output_options[i].str, event_type(type));
  647. rc = -EINVAL;
  648. goto out;
  649. }
  650. output[type].fields |= all_output_options[i].field;
  651. }
  652. tok = strtok(NULL, ",");
  653. }
  654. if (type >= 0) {
  655. if (output[type].fields == 0) {
  656. pr_debug("No fields requested for %s type. "
  657. "Events will not be displayed.\n", event_type(type));
  658. }
  659. }
  660. out:
  661. free(str);
  662. return rc;
  663. }
  664. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  665. static int is_directory(const char *base_path, const struct dirent *dent)
  666. {
  667. char path[PATH_MAX];
  668. struct stat st;
  669. sprintf(path, "%s/%s", base_path, dent->d_name);
  670. if (stat(path, &st))
  671. return 0;
  672. return S_ISDIR(st.st_mode);
  673. }
  674. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  675. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  676. lang_next) \
  677. if ((lang_dirent.d_type == DT_DIR || \
  678. (lang_dirent.d_type == DT_UNKNOWN && \
  679. is_directory(scripts_path, &lang_dirent))) && \
  680. (strcmp(lang_dirent.d_name, ".")) && \
  681. (strcmp(lang_dirent.d_name, "..")))
  682. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  683. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  684. script_next) \
  685. if (script_dirent.d_type != DT_DIR && \
  686. (script_dirent.d_type != DT_UNKNOWN || \
  687. !is_directory(lang_path, &script_dirent)))
  688. #define RECORD_SUFFIX "-record"
  689. #define REPORT_SUFFIX "-report"
  690. struct script_desc {
  691. struct list_head node;
  692. char *name;
  693. char *half_liner;
  694. char *args;
  695. };
  696. static LIST_HEAD(script_descs);
  697. static struct script_desc *script_desc__new(const char *name)
  698. {
  699. struct script_desc *s = zalloc(sizeof(*s));
  700. if (s != NULL && name)
  701. s->name = strdup(name);
  702. return s;
  703. }
  704. static void script_desc__delete(struct script_desc *s)
  705. {
  706. free(s->name);
  707. free(s->half_liner);
  708. free(s->args);
  709. free(s);
  710. }
  711. static void script_desc__add(struct script_desc *s)
  712. {
  713. list_add_tail(&s->node, &script_descs);
  714. }
  715. static struct script_desc *script_desc__find(const char *name)
  716. {
  717. struct script_desc *s;
  718. list_for_each_entry(s, &script_descs, node)
  719. if (strcasecmp(s->name, name) == 0)
  720. return s;
  721. return NULL;
  722. }
  723. static struct script_desc *script_desc__findnew(const char *name)
  724. {
  725. struct script_desc *s = script_desc__find(name);
  726. if (s)
  727. return s;
  728. s = script_desc__new(name);
  729. if (!s)
  730. goto out_delete_desc;
  731. script_desc__add(s);
  732. return s;
  733. out_delete_desc:
  734. script_desc__delete(s);
  735. return NULL;
  736. }
  737. static const char *ends_with(const char *str, const char *suffix)
  738. {
  739. size_t suffix_len = strlen(suffix);
  740. const char *p = str;
  741. if (strlen(str) > suffix_len) {
  742. p = str + strlen(str) - suffix_len;
  743. if (!strncmp(p, suffix, suffix_len))
  744. return p;
  745. }
  746. return NULL;
  747. }
  748. static char *ltrim(char *str)
  749. {
  750. int len = strlen(str);
  751. while (len && isspace(*str)) {
  752. len--;
  753. str++;
  754. }
  755. return str;
  756. }
  757. static int read_script_info(struct script_desc *desc, const char *filename)
  758. {
  759. char line[BUFSIZ], *p;
  760. FILE *fp;
  761. fp = fopen(filename, "r");
  762. if (!fp)
  763. return -1;
  764. while (fgets(line, sizeof(line), fp)) {
  765. p = ltrim(line);
  766. if (strlen(p) == 0)
  767. continue;
  768. if (*p != '#')
  769. continue;
  770. p++;
  771. if (strlen(p) && *p == '!')
  772. continue;
  773. p = ltrim(p);
  774. if (strlen(p) && p[strlen(p) - 1] == '\n')
  775. p[strlen(p) - 1] = '\0';
  776. if (!strncmp(p, "description:", strlen("description:"))) {
  777. p += strlen("description:");
  778. desc->half_liner = strdup(ltrim(p));
  779. continue;
  780. }
  781. if (!strncmp(p, "args:", strlen("args:"))) {
  782. p += strlen("args:");
  783. desc->args = strdup(ltrim(p));
  784. continue;
  785. }
  786. }
  787. fclose(fp);
  788. return 0;
  789. }
  790. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  791. {
  792. char *script_root, *str;
  793. script_root = strdup(script_dirent->d_name);
  794. if (!script_root)
  795. return NULL;
  796. str = (char *)ends_with(script_root, suffix);
  797. if (!str) {
  798. free(script_root);
  799. return NULL;
  800. }
  801. *str = '\0';
  802. return script_root;
  803. }
  804. static int list_available_scripts(const struct option *opt __used,
  805. const char *s __used, int unset __used)
  806. {
  807. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  808. char scripts_path[MAXPATHLEN];
  809. DIR *scripts_dir, *lang_dir;
  810. char script_path[MAXPATHLEN];
  811. char lang_path[MAXPATHLEN];
  812. struct script_desc *desc;
  813. char first_half[BUFSIZ];
  814. char *script_root;
  815. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  816. scripts_dir = opendir(scripts_path);
  817. if (!scripts_dir)
  818. return -1;
  819. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  820. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  821. lang_dirent.d_name);
  822. lang_dir = opendir(lang_path);
  823. if (!lang_dir)
  824. continue;
  825. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  826. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  827. if (script_root) {
  828. desc = script_desc__findnew(script_root);
  829. snprintf(script_path, MAXPATHLEN, "%s/%s",
  830. lang_path, script_dirent.d_name);
  831. read_script_info(desc, script_path);
  832. free(script_root);
  833. }
  834. }
  835. }
  836. fprintf(stdout, "List of available trace scripts:\n");
  837. list_for_each_entry(desc, &script_descs, node) {
  838. sprintf(first_half, "%s %s", desc->name,
  839. desc->args ? desc->args : "");
  840. fprintf(stdout, " %-36s %s\n", first_half,
  841. desc->half_liner ? desc->half_liner : "");
  842. }
  843. exit(0);
  844. }
  845. static char *get_script_path(const char *script_root, const char *suffix)
  846. {
  847. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  848. char scripts_path[MAXPATHLEN];
  849. char script_path[MAXPATHLEN];
  850. DIR *scripts_dir, *lang_dir;
  851. char lang_path[MAXPATHLEN];
  852. char *__script_root;
  853. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  854. scripts_dir = opendir(scripts_path);
  855. if (!scripts_dir)
  856. return NULL;
  857. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  858. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  859. lang_dirent.d_name);
  860. lang_dir = opendir(lang_path);
  861. if (!lang_dir)
  862. continue;
  863. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  864. __script_root = get_script_root(&script_dirent, suffix);
  865. if (__script_root && !strcmp(script_root, __script_root)) {
  866. free(__script_root);
  867. closedir(lang_dir);
  868. closedir(scripts_dir);
  869. snprintf(script_path, MAXPATHLEN, "%s/%s",
  870. lang_path, script_dirent.d_name);
  871. return strdup(script_path);
  872. }
  873. free(__script_root);
  874. }
  875. closedir(lang_dir);
  876. }
  877. closedir(scripts_dir);
  878. return NULL;
  879. }
  880. static bool is_top_script(const char *script_path)
  881. {
  882. return ends_with(script_path, "top") == NULL ? false : true;
  883. }
  884. static int has_required_arg(char *script_path)
  885. {
  886. struct script_desc *desc;
  887. int n_args = 0;
  888. char *p;
  889. desc = script_desc__new(NULL);
  890. if (read_script_info(desc, script_path))
  891. goto out;
  892. if (!desc->args)
  893. goto out;
  894. for (p = desc->args; *p; p++)
  895. if (*p == '<')
  896. n_args++;
  897. out:
  898. script_desc__delete(desc);
  899. return n_args;
  900. }
  901. static const char * const script_usage[] = {
  902. "perf script [<options>]",
  903. "perf script [<options>] record <script> [<record-options>] <command>",
  904. "perf script [<options>] report <script> [script-args]",
  905. "perf script [<options>] <script> [<record-options>] <command>",
  906. "perf script [<options>] <top-script> [script-args]",
  907. NULL
  908. };
  909. static const struct option options[] = {
  910. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  911. "dump raw trace in ASCII"),
  912. OPT_INCR('v', "verbose", &verbose,
  913. "be more verbose (show symbol address, etc)"),
  914. OPT_BOOLEAN('L', "Latency", &latency_format,
  915. "show latency attributes (irqs/preemption disabled, etc)"),
  916. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  917. list_available_scripts),
  918. OPT_CALLBACK('s', "script", NULL, "name",
  919. "script file name (lang:script name, script name, or *)",
  920. parse_scriptname),
  921. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  922. "generate perf-script.xx script in specified language"),
  923. OPT_STRING('i', "input", &input_name, "file",
  924. "input file name"),
  925. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  926. "do various checks like samples ordering and lost events"),
  927. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  928. "file", "vmlinux pathname"),
  929. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  930. "file", "kallsyms pathname"),
  931. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  932. "When printing symbols do not display call chain"),
  933. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  934. "Look for files with symbols relative to this directory"),
  935. OPT_CALLBACK('f', "fields", NULL, "str",
  936. "comma separated output fields prepend with 'type:'. "
  937. "Valid types: hw,sw,trace,raw. "
  938. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  939. "addr,symoff",
  940. parse_output_fields),
  941. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  942. "system-wide collection from all CPUs"),
  943. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  944. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  945. "only display events for these comms"),
  946. OPT_BOOLEAN('I', "show-info", &show_full_info,
  947. "display extended information from perf.data file"),
  948. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  949. "Show the path of [kernel.kallsyms]"),
  950. OPT_END()
  951. };
  952. static bool have_cmd(int argc, const char **argv)
  953. {
  954. char **__argv = malloc(sizeof(const char *) * argc);
  955. if (!__argv)
  956. die("malloc");
  957. memcpy(__argv, argv, sizeof(const char *) * argc);
  958. argc = parse_options(argc, (const char **)__argv, record_options,
  959. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  960. free(__argv);
  961. return argc != 0;
  962. }
  963. int cmd_script(int argc, const char **argv, const char *prefix __used)
  964. {
  965. char *rec_script_path = NULL;
  966. char *rep_script_path = NULL;
  967. struct perf_session *session;
  968. char *script_path = NULL;
  969. const char **__argv;
  970. int i, j, err;
  971. setup_scripting();
  972. argc = parse_options(argc, argv, options, script_usage,
  973. PARSE_OPT_STOP_AT_NON_OPTION);
  974. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  975. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  976. if (!rec_script_path)
  977. return cmd_record(argc, argv, NULL);
  978. }
  979. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  980. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  981. if (!rep_script_path) {
  982. fprintf(stderr,
  983. "Please specify a valid report script"
  984. "(see 'perf script -l' for listing)\n");
  985. return -1;
  986. }
  987. }
  988. /* make sure PERF_EXEC_PATH is set for scripts */
  989. perf_set_argv_exec_path(perf_exec_path());
  990. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  991. int live_pipe[2];
  992. int rep_args;
  993. pid_t pid;
  994. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  995. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  996. if (!rec_script_path && !rep_script_path) {
  997. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  998. " script -l for available scripts.\n", argv[0]);
  999. usage_with_options(script_usage, options);
  1000. }
  1001. if (is_top_script(argv[0])) {
  1002. rep_args = argc - 1;
  1003. } else {
  1004. int rec_args;
  1005. rep_args = has_required_arg(rep_script_path);
  1006. rec_args = (argc - 1) - rep_args;
  1007. if (rec_args < 0) {
  1008. fprintf(stderr, " %s script requires options."
  1009. "\n\n See perf script -l for available "
  1010. "scripts and options.\n", argv[0]);
  1011. usage_with_options(script_usage, options);
  1012. }
  1013. }
  1014. if (pipe(live_pipe) < 0) {
  1015. perror("failed to create pipe");
  1016. exit(-1);
  1017. }
  1018. pid = fork();
  1019. if (pid < 0) {
  1020. perror("failed to fork");
  1021. exit(-1);
  1022. }
  1023. if (!pid) {
  1024. j = 0;
  1025. dup2(live_pipe[1], 1);
  1026. close(live_pipe[0]);
  1027. if (is_top_script(argv[0])) {
  1028. system_wide = true;
  1029. } else if (!system_wide) {
  1030. system_wide = !have_cmd(argc - rep_args,
  1031. &argv[rep_args]);
  1032. }
  1033. __argv = malloc((argc + 6) * sizeof(const char *));
  1034. if (!__argv)
  1035. die("malloc");
  1036. __argv[j++] = "/bin/sh";
  1037. __argv[j++] = rec_script_path;
  1038. if (system_wide)
  1039. __argv[j++] = "-a";
  1040. __argv[j++] = "-q";
  1041. __argv[j++] = "-o";
  1042. __argv[j++] = "-";
  1043. for (i = rep_args + 1; i < argc; i++)
  1044. __argv[j++] = argv[i];
  1045. __argv[j++] = NULL;
  1046. execvp("/bin/sh", (char **)__argv);
  1047. free(__argv);
  1048. exit(-1);
  1049. }
  1050. dup2(live_pipe[0], 0);
  1051. close(live_pipe[1]);
  1052. __argv = malloc((argc + 4) * sizeof(const char *));
  1053. if (!__argv)
  1054. die("malloc");
  1055. j = 0;
  1056. __argv[j++] = "/bin/sh";
  1057. __argv[j++] = rep_script_path;
  1058. for (i = 1; i < rep_args + 1; i++)
  1059. __argv[j++] = argv[i];
  1060. __argv[j++] = "-i";
  1061. __argv[j++] = "-";
  1062. __argv[j++] = NULL;
  1063. execvp("/bin/sh", (char **)__argv);
  1064. free(__argv);
  1065. exit(-1);
  1066. }
  1067. if (rec_script_path)
  1068. script_path = rec_script_path;
  1069. if (rep_script_path)
  1070. script_path = rep_script_path;
  1071. if (script_path) {
  1072. j = 0;
  1073. if (!rec_script_path)
  1074. system_wide = false;
  1075. else if (!system_wide)
  1076. system_wide = !have_cmd(argc - 1, &argv[1]);
  1077. __argv = malloc((argc + 2) * sizeof(const char *));
  1078. if (!__argv)
  1079. die("malloc");
  1080. __argv[j++] = "/bin/sh";
  1081. __argv[j++] = script_path;
  1082. if (system_wide)
  1083. __argv[j++] = "-a";
  1084. for (i = 2; i < argc; i++)
  1085. __argv[j++] = argv[i];
  1086. __argv[j++] = NULL;
  1087. execvp("/bin/sh", (char **)__argv);
  1088. free(__argv);
  1089. exit(-1);
  1090. }
  1091. if (symbol__init() < 0)
  1092. return -1;
  1093. if (!script_name)
  1094. setup_pager();
  1095. session = perf_session__new(input_name, O_RDONLY, 0, false,
  1096. &perf_script);
  1097. if (session == NULL)
  1098. return -ENOMEM;
  1099. if (cpu_list) {
  1100. if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
  1101. return -1;
  1102. }
  1103. perf_session__fprintf_info(session, stdout, show_full_info);
  1104. if (!no_callchain)
  1105. symbol_conf.use_callchain = true;
  1106. else
  1107. symbol_conf.use_callchain = false;
  1108. if (generate_script_lang) {
  1109. struct stat perf_stat;
  1110. int input;
  1111. if (output_set_by_user()) {
  1112. fprintf(stderr,
  1113. "custom fields not supported for generated scripts");
  1114. return -1;
  1115. }
  1116. input = open(session->filename, O_RDONLY); /* input_name */
  1117. if (input < 0) {
  1118. perror("failed to open file");
  1119. exit(-1);
  1120. }
  1121. err = fstat(input, &perf_stat);
  1122. if (err < 0) {
  1123. perror("failed to stat file");
  1124. exit(-1);
  1125. }
  1126. if (!perf_stat.st_size) {
  1127. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1128. exit(0);
  1129. }
  1130. scripting_ops = script_spec__lookup(generate_script_lang);
  1131. if (!scripting_ops) {
  1132. fprintf(stderr, "invalid language specifier");
  1133. return -1;
  1134. }
  1135. err = scripting_ops->generate_script(session->pevent,
  1136. "perf-script");
  1137. goto out;
  1138. }
  1139. if (script_name) {
  1140. err = scripting_ops->start_script(script_name, argc, argv);
  1141. if (err)
  1142. goto out;
  1143. pr_debug("perf script started with script %s\n\n", script_name);
  1144. }
  1145. err = perf_session__check_output_opt(session);
  1146. if (err < 0)
  1147. goto out;
  1148. err = __cmd_script(session);
  1149. perf_session__delete(session);
  1150. cleanup_scripting();
  1151. out:
  1152. return err;
  1153. }