parse-events.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. #include <linux/hw_breakpoint.h>
  2. #include "util.h"
  3. #include "../perf.h"
  4. #include "evlist.h"
  5. #include "evsel.h"
  6. #include "parse-options.h"
  7. #include "parse-events.h"
  8. #include "exec_cmd.h"
  9. #include "string.h"
  10. #include "symbol.h"
  11. #include "cache.h"
  12. #include "header.h"
  13. #include <lk/debugfs.h>
  14. #include "parse-events-bison.h"
  15. #define YY_EXTRA_TYPE int
  16. #include "parse-events-flex.h"
  17. #include "pmu.h"
  18. #define MAX_NAME_LEN 100
  19. struct event_symbol {
  20. const char *symbol;
  21. const char *alias;
  22. };
  23. #ifdef PARSER_DEBUG
  24. extern int parse_events_debug;
  25. #endif
  26. int parse_events_parse(void *data, void *scanner);
  27. static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
  28. [PERF_COUNT_HW_CPU_CYCLES] = {
  29. .symbol = "cpu-cycles",
  30. .alias = "cycles",
  31. },
  32. [PERF_COUNT_HW_INSTRUCTIONS] = {
  33. .symbol = "instructions",
  34. .alias = "",
  35. },
  36. [PERF_COUNT_HW_CACHE_REFERENCES] = {
  37. .symbol = "cache-references",
  38. .alias = "",
  39. },
  40. [PERF_COUNT_HW_CACHE_MISSES] = {
  41. .symbol = "cache-misses",
  42. .alias = "",
  43. },
  44. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
  45. .symbol = "branch-instructions",
  46. .alias = "branches",
  47. },
  48. [PERF_COUNT_HW_BRANCH_MISSES] = {
  49. .symbol = "branch-misses",
  50. .alias = "",
  51. },
  52. [PERF_COUNT_HW_BUS_CYCLES] = {
  53. .symbol = "bus-cycles",
  54. .alias = "",
  55. },
  56. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
  57. .symbol = "stalled-cycles-frontend",
  58. .alias = "idle-cycles-frontend",
  59. },
  60. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
  61. .symbol = "stalled-cycles-backend",
  62. .alias = "idle-cycles-backend",
  63. },
  64. [PERF_COUNT_HW_REF_CPU_CYCLES] = {
  65. .symbol = "ref-cycles",
  66. .alias = "",
  67. },
  68. };
  69. static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
  70. [PERF_COUNT_SW_CPU_CLOCK] = {
  71. .symbol = "cpu-clock",
  72. .alias = "",
  73. },
  74. [PERF_COUNT_SW_TASK_CLOCK] = {
  75. .symbol = "task-clock",
  76. .alias = "",
  77. },
  78. [PERF_COUNT_SW_PAGE_FAULTS] = {
  79. .symbol = "page-faults",
  80. .alias = "faults",
  81. },
  82. [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
  83. .symbol = "context-switches",
  84. .alias = "cs",
  85. },
  86. [PERF_COUNT_SW_CPU_MIGRATIONS] = {
  87. .symbol = "cpu-migrations",
  88. .alias = "migrations",
  89. },
  90. [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
  91. .symbol = "minor-faults",
  92. .alias = "",
  93. },
  94. [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
  95. .symbol = "major-faults",
  96. .alias = "",
  97. },
  98. [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
  99. .symbol = "alignment-faults",
  100. .alias = "",
  101. },
  102. [PERF_COUNT_SW_EMULATION_FAULTS] = {
  103. .symbol = "emulation-faults",
  104. .alias = "",
  105. },
  106. };
  107. #define __PERF_EVENT_FIELD(config, name) \
  108. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  109. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  110. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  111. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  112. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  113. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  114. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  115. if (sys_dirent.d_type == DT_DIR && \
  116. (strcmp(sys_dirent.d_name, ".")) && \
  117. (strcmp(sys_dirent.d_name, "..")))
  118. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  119. {
  120. char evt_path[MAXPATHLEN];
  121. int fd;
  122. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  123. sys_dir->d_name, evt_dir->d_name);
  124. fd = open(evt_path, O_RDONLY);
  125. if (fd < 0)
  126. return -EINVAL;
  127. close(fd);
  128. return 0;
  129. }
  130. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  131. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  132. if (evt_dirent.d_type == DT_DIR && \
  133. (strcmp(evt_dirent.d_name, ".")) && \
  134. (strcmp(evt_dirent.d_name, "..")) && \
  135. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  136. #define MAX_EVENT_LENGTH 512
  137. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  138. {
  139. struct tracepoint_path *path = NULL;
  140. DIR *sys_dir, *evt_dir;
  141. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  142. char id_buf[24];
  143. int fd;
  144. u64 id;
  145. char evt_path[MAXPATHLEN];
  146. char dir_path[MAXPATHLEN];
  147. if (debugfs_valid_mountpoint(tracing_events_path))
  148. return NULL;
  149. sys_dir = opendir(tracing_events_path);
  150. if (!sys_dir)
  151. return NULL;
  152. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  153. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  154. sys_dirent.d_name);
  155. evt_dir = opendir(dir_path);
  156. if (!evt_dir)
  157. continue;
  158. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  159. snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
  160. evt_dirent.d_name);
  161. fd = open(evt_path, O_RDONLY);
  162. if (fd < 0)
  163. continue;
  164. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  165. close(fd);
  166. continue;
  167. }
  168. close(fd);
  169. id = atoll(id_buf);
  170. if (id == config) {
  171. closedir(evt_dir);
  172. closedir(sys_dir);
  173. path = zalloc(sizeof(*path));
  174. path->system = malloc(MAX_EVENT_LENGTH);
  175. if (!path->system) {
  176. free(path);
  177. return NULL;
  178. }
  179. path->name = malloc(MAX_EVENT_LENGTH);
  180. if (!path->name) {
  181. free(path->system);
  182. free(path);
  183. return NULL;
  184. }
  185. strncpy(path->system, sys_dirent.d_name,
  186. MAX_EVENT_LENGTH);
  187. strncpy(path->name, evt_dirent.d_name,
  188. MAX_EVENT_LENGTH);
  189. return path;
  190. }
  191. }
  192. closedir(evt_dir);
  193. }
  194. closedir(sys_dir);
  195. return NULL;
  196. }
  197. const char *event_type(int type)
  198. {
  199. switch (type) {
  200. case PERF_TYPE_HARDWARE:
  201. return "hardware";
  202. case PERF_TYPE_SOFTWARE:
  203. return "software";
  204. case PERF_TYPE_TRACEPOINT:
  205. return "tracepoint";
  206. case PERF_TYPE_HW_CACHE:
  207. return "hardware-cache";
  208. default:
  209. break;
  210. }
  211. return "unknown";
  212. }
  213. static int __add_event(struct list_head **_list, int *idx,
  214. struct perf_event_attr *attr,
  215. char *name, struct cpu_map *cpus)
  216. {
  217. struct perf_evsel *evsel;
  218. struct list_head *list = *_list;
  219. if (!list) {
  220. list = malloc(sizeof(*list));
  221. if (!list)
  222. return -ENOMEM;
  223. INIT_LIST_HEAD(list);
  224. }
  225. event_attr_init(attr);
  226. evsel = perf_evsel__new(attr, (*idx)++);
  227. if (!evsel) {
  228. free(list);
  229. return -ENOMEM;
  230. }
  231. evsel->cpus = cpus;
  232. if (name)
  233. evsel->name = strdup(name);
  234. list_add_tail(&evsel->node, list);
  235. *_list = list;
  236. return 0;
  237. }
  238. static int add_event(struct list_head **_list, int *idx,
  239. struct perf_event_attr *attr, char *name)
  240. {
  241. return __add_event(_list, idx, attr, name, NULL);
  242. }
  243. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  244. {
  245. int i, j;
  246. int n, longest = -1;
  247. for (i = 0; i < size; i++) {
  248. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  249. n = strlen(names[i][j]);
  250. if (n > longest && !strncasecmp(str, names[i][j], n))
  251. longest = n;
  252. }
  253. if (longest > 0)
  254. return i;
  255. }
  256. return -1;
  257. }
  258. int parse_events_add_cache(struct list_head **list, int *idx,
  259. char *type, char *op_result1, char *op_result2)
  260. {
  261. struct perf_event_attr attr;
  262. char name[MAX_NAME_LEN];
  263. int cache_type = -1, cache_op = -1, cache_result = -1;
  264. char *op_result[2] = { op_result1, op_result2 };
  265. int i, n;
  266. /*
  267. * No fallback - if we cannot get a clear cache type
  268. * then bail out:
  269. */
  270. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  271. PERF_COUNT_HW_CACHE_MAX);
  272. if (cache_type == -1)
  273. return -EINVAL;
  274. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  275. for (i = 0; (i < 2) && (op_result[i]); i++) {
  276. char *str = op_result[i];
  277. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  278. if (cache_op == -1) {
  279. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  280. PERF_COUNT_HW_CACHE_OP_MAX);
  281. if (cache_op >= 0) {
  282. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  283. return -EINVAL;
  284. continue;
  285. }
  286. }
  287. if (cache_result == -1) {
  288. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  289. PERF_COUNT_HW_CACHE_RESULT_MAX);
  290. if (cache_result >= 0)
  291. continue;
  292. }
  293. }
  294. /*
  295. * Fall back to reads:
  296. */
  297. if (cache_op == -1)
  298. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  299. /*
  300. * Fall back to accesses:
  301. */
  302. if (cache_result == -1)
  303. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  304. memset(&attr, 0, sizeof(attr));
  305. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  306. attr.type = PERF_TYPE_HW_CACHE;
  307. return add_event(list, idx, &attr, name);
  308. }
  309. static int add_tracepoint(struct list_head **listp, int *idx,
  310. char *sys_name, char *evt_name)
  311. {
  312. struct perf_evsel *evsel;
  313. struct list_head *list = *listp;
  314. if (!list) {
  315. list = malloc(sizeof(*list));
  316. if (!list)
  317. return -ENOMEM;
  318. INIT_LIST_HEAD(list);
  319. }
  320. evsel = perf_evsel__newtp(sys_name, evt_name, (*idx)++);
  321. if (!evsel) {
  322. free(list);
  323. return -ENOMEM;
  324. }
  325. list_add_tail(&evsel->node, list);
  326. *listp = list;
  327. return 0;
  328. }
  329. static int add_tracepoint_multi_event(struct list_head **list, int *idx,
  330. char *sys_name, char *evt_name)
  331. {
  332. char evt_path[MAXPATHLEN];
  333. struct dirent *evt_ent;
  334. DIR *evt_dir;
  335. int ret = 0;
  336. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  337. evt_dir = opendir(evt_path);
  338. if (!evt_dir) {
  339. perror("Can't open event dir");
  340. return -1;
  341. }
  342. while (!ret && (evt_ent = readdir(evt_dir))) {
  343. if (!strcmp(evt_ent->d_name, ".")
  344. || !strcmp(evt_ent->d_name, "..")
  345. || !strcmp(evt_ent->d_name, "enable")
  346. || !strcmp(evt_ent->d_name, "filter"))
  347. continue;
  348. if (!strglobmatch(evt_ent->d_name, evt_name))
  349. continue;
  350. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  351. }
  352. closedir(evt_dir);
  353. return ret;
  354. }
  355. static int add_tracepoint_event(struct list_head **list, int *idx,
  356. char *sys_name, char *evt_name)
  357. {
  358. return strpbrk(evt_name, "*?") ?
  359. add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
  360. add_tracepoint(list, idx, sys_name, evt_name);
  361. }
  362. static int add_tracepoint_multi_sys(struct list_head **list, int *idx,
  363. char *sys_name, char *evt_name)
  364. {
  365. struct dirent *events_ent;
  366. DIR *events_dir;
  367. int ret = 0;
  368. events_dir = opendir(tracing_events_path);
  369. if (!events_dir) {
  370. perror("Can't open event dir");
  371. return -1;
  372. }
  373. while (!ret && (events_ent = readdir(events_dir))) {
  374. if (!strcmp(events_ent->d_name, ".")
  375. || !strcmp(events_ent->d_name, "..")
  376. || !strcmp(events_ent->d_name, "enable")
  377. || !strcmp(events_ent->d_name, "header_event")
  378. || !strcmp(events_ent->d_name, "header_page"))
  379. continue;
  380. if (!strglobmatch(events_ent->d_name, sys_name))
  381. continue;
  382. ret = add_tracepoint_event(list, idx, events_ent->d_name,
  383. evt_name);
  384. }
  385. closedir(events_dir);
  386. return ret;
  387. }
  388. int parse_events_add_tracepoint(struct list_head **list, int *idx,
  389. char *sys, char *event)
  390. {
  391. int ret;
  392. ret = debugfs_valid_mountpoint(tracing_events_path);
  393. if (ret)
  394. return ret;
  395. if (strpbrk(sys, "*?"))
  396. return add_tracepoint_multi_sys(list, idx, sys, event);
  397. else
  398. return add_tracepoint_event(list, idx, sys, event);
  399. }
  400. static int
  401. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  402. {
  403. int i;
  404. for (i = 0; i < 3; i++) {
  405. if (!type || !type[i])
  406. break;
  407. #define CHECK_SET_TYPE(bit) \
  408. do { \
  409. if (attr->bp_type & bit) \
  410. return -EINVAL; \
  411. else \
  412. attr->bp_type |= bit; \
  413. } while (0)
  414. switch (type[i]) {
  415. case 'r':
  416. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  417. break;
  418. case 'w':
  419. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  420. break;
  421. case 'x':
  422. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  423. break;
  424. default:
  425. return -EINVAL;
  426. }
  427. }
  428. #undef CHECK_SET_TYPE
  429. if (!attr->bp_type) /* Default */
  430. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  431. return 0;
  432. }
  433. int parse_events_add_breakpoint(struct list_head **list, int *idx,
  434. void *ptr, char *type)
  435. {
  436. struct perf_event_attr attr;
  437. memset(&attr, 0, sizeof(attr));
  438. attr.bp_addr = (unsigned long) ptr;
  439. if (parse_breakpoint_type(type, &attr))
  440. return -EINVAL;
  441. /*
  442. * We should find a nice way to override the access length
  443. * Provide some defaults for now
  444. */
  445. if (attr.bp_type == HW_BREAKPOINT_X)
  446. attr.bp_len = sizeof(long);
  447. else
  448. attr.bp_len = HW_BREAKPOINT_LEN_4;
  449. attr.type = PERF_TYPE_BREAKPOINT;
  450. attr.sample_period = 1;
  451. return add_event(list, idx, &attr, NULL);
  452. }
  453. static int config_term(struct perf_event_attr *attr,
  454. struct parse_events_term *term)
  455. {
  456. #define CHECK_TYPE_VAL(type) \
  457. do { \
  458. if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
  459. return -EINVAL; \
  460. } while (0)
  461. switch (term->type_term) {
  462. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  463. CHECK_TYPE_VAL(NUM);
  464. attr->config = term->val.num;
  465. break;
  466. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  467. CHECK_TYPE_VAL(NUM);
  468. attr->config1 = term->val.num;
  469. break;
  470. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  471. CHECK_TYPE_VAL(NUM);
  472. attr->config2 = term->val.num;
  473. break;
  474. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  475. CHECK_TYPE_VAL(NUM);
  476. attr->sample_period = term->val.num;
  477. break;
  478. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  479. /*
  480. * TODO uncomment when the field is available
  481. * attr->branch_sample_type = term->val.num;
  482. */
  483. break;
  484. case PARSE_EVENTS__TERM_TYPE_NAME:
  485. CHECK_TYPE_VAL(STR);
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. return 0;
  491. #undef CHECK_TYPE_VAL
  492. }
  493. static int config_attr(struct perf_event_attr *attr,
  494. struct list_head *head, int fail)
  495. {
  496. struct parse_events_term *term;
  497. list_for_each_entry(term, head, list)
  498. if (config_term(attr, term) && fail)
  499. return -EINVAL;
  500. return 0;
  501. }
  502. int parse_events_add_numeric(struct list_head **list, int *idx,
  503. u32 type, u64 config,
  504. struct list_head *head_config)
  505. {
  506. struct perf_event_attr attr;
  507. memset(&attr, 0, sizeof(attr));
  508. attr.type = type;
  509. attr.config = config;
  510. if (head_config &&
  511. config_attr(&attr, head_config, 1))
  512. return -EINVAL;
  513. return add_event(list, idx, &attr, NULL);
  514. }
  515. static int parse_events__is_name_term(struct parse_events_term *term)
  516. {
  517. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  518. }
  519. static char *pmu_event_name(struct list_head *head_terms)
  520. {
  521. struct parse_events_term *term;
  522. list_for_each_entry(term, head_terms, list)
  523. if (parse_events__is_name_term(term))
  524. return term->val.str;
  525. return NULL;
  526. }
  527. int parse_events_add_pmu(struct list_head **list, int *idx,
  528. char *name, struct list_head *head_config)
  529. {
  530. struct perf_event_attr attr;
  531. struct perf_pmu *pmu;
  532. pmu = perf_pmu__find(name);
  533. if (!pmu)
  534. return -EINVAL;
  535. memset(&attr, 0, sizeof(attr));
  536. if (perf_pmu__check_alias(pmu, head_config))
  537. return -EINVAL;
  538. /*
  539. * Configure hardcoded terms first, no need to check
  540. * return value when called with fail == 0 ;)
  541. */
  542. config_attr(&attr, head_config, 0);
  543. if (perf_pmu__config(pmu, &attr, head_config))
  544. return -EINVAL;
  545. return __add_event(list, idx, &attr, pmu_event_name(head_config),
  546. pmu->cpus);
  547. }
  548. int parse_events__modifier_group(struct list_head *list,
  549. char *event_mod)
  550. {
  551. return parse_events__modifier_event(list, event_mod, true);
  552. }
  553. void parse_events__set_leader(char *name, struct list_head *list)
  554. {
  555. struct perf_evsel *leader;
  556. __perf_evlist__set_leader(list);
  557. leader = list_entry(list->next, struct perf_evsel, node);
  558. leader->group_name = name ? strdup(name) : NULL;
  559. }
  560. void parse_events_update_lists(struct list_head *list_event,
  561. struct list_head *list_all)
  562. {
  563. /*
  564. * Called for single event definition. Update the
  565. * 'all event' list, and reinit the 'single event'
  566. * list, for next event definition.
  567. */
  568. list_splice_tail(list_event, list_all);
  569. free(list_event);
  570. }
  571. struct event_modifier {
  572. int eu;
  573. int ek;
  574. int eh;
  575. int eH;
  576. int eG;
  577. int precise;
  578. int exclude_GH;
  579. };
  580. static int get_event_modifier(struct event_modifier *mod, char *str,
  581. struct perf_evsel *evsel)
  582. {
  583. int eu = evsel ? evsel->attr.exclude_user : 0;
  584. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  585. int eh = evsel ? evsel->attr.exclude_hv : 0;
  586. int eH = evsel ? evsel->attr.exclude_host : 0;
  587. int eG = evsel ? evsel->attr.exclude_guest : 0;
  588. int precise = evsel ? evsel->attr.precise_ip : 0;
  589. int exclude = eu | ek | eh;
  590. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  591. memset(mod, 0, sizeof(*mod));
  592. while (*str) {
  593. if (*str == 'u') {
  594. if (!exclude)
  595. exclude = eu = ek = eh = 1;
  596. eu = 0;
  597. } else if (*str == 'k') {
  598. if (!exclude)
  599. exclude = eu = ek = eh = 1;
  600. ek = 0;
  601. } else if (*str == 'h') {
  602. if (!exclude)
  603. exclude = eu = ek = eh = 1;
  604. eh = 0;
  605. } else if (*str == 'G') {
  606. if (!exclude_GH)
  607. exclude_GH = eG = eH = 1;
  608. eG = 0;
  609. } else if (*str == 'H') {
  610. if (!exclude_GH)
  611. exclude_GH = eG = eH = 1;
  612. eH = 0;
  613. } else if (*str == 'p') {
  614. precise++;
  615. /* use of precise requires exclude_guest */
  616. if (!exclude_GH)
  617. eG = 1;
  618. } else
  619. break;
  620. ++str;
  621. }
  622. /*
  623. * precise ip:
  624. *
  625. * 0 - SAMPLE_IP can have arbitrary skid
  626. * 1 - SAMPLE_IP must have constant skid
  627. * 2 - SAMPLE_IP requested to have 0 skid
  628. * 3 - SAMPLE_IP must have 0 skid
  629. *
  630. * See also PERF_RECORD_MISC_EXACT_IP
  631. */
  632. if (precise > 3)
  633. return -EINVAL;
  634. mod->eu = eu;
  635. mod->ek = ek;
  636. mod->eh = eh;
  637. mod->eH = eH;
  638. mod->eG = eG;
  639. mod->precise = precise;
  640. mod->exclude_GH = exclude_GH;
  641. return 0;
  642. }
  643. /*
  644. * Basic modifier sanity check to validate it contains only one
  645. * instance of any modifier (apart from 'p') present.
  646. */
  647. static int check_modifier(char *str)
  648. {
  649. char *p = str;
  650. /* The sizeof includes 0 byte as well. */
  651. if (strlen(str) > (sizeof("ukhGHppp") - 1))
  652. return -1;
  653. while (*p) {
  654. if (*p != 'p' && strchr(p + 1, *p))
  655. return -1;
  656. p++;
  657. }
  658. return 0;
  659. }
  660. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  661. {
  662. struct perf_evsel *evsel;
  663. struct event_modifier mod;
  664. if (str == NULL)
  665. return 0;
  666. if (check_modifier(str))
  667. return -EINVAL;
  668. if (!add && get_event_modifier(&mod, str, NULL))
  669. return -EINVAL;
  670. list_for_each_entry(evsel, list, node) {
  671. if (add && get_event_modifier(&mod, str, evsel))
  672. return -EINVAL;
  673. evsel->attr.exclude_user = mod.eu;
  674. evsel->attr.exclude_kernel = mod.ek;
  675. evsel->attr.exclude_hv = mod.eh;
  676. evsel->attr.precise_ip = mod.precise;
  677. evsel->attr.exclude_host = mod.eH;
  678. evsel->attr.exclude_guest = mod.eG;
  679. evsel->exclude_GH = mod.exclude_GH;
  680. }
  681. return 0;
  682. }
  683. int parse_events_name(struct list_head *list, char *name)
  684. {
  685. struct perf_evsel *evsel;
  686. list_for_each_entry(evsel, list, node) {
  687. if (!evsel->name)
  688. evsel->name = strdup(name);
  689. }
  690. return 0;
  691. }
  692. static int parse_events__scanner(const char *str, void *data, int start_token)
  693. {
  694. YY_BUFFER_STATE buffer;
  695. void *scanner;
  696. int ret;
  697. ret = parse_events_lex_init_extra(start_token, &scanner);
  698. if (ret)
  699. return ret;
  700. buffer = parse_events__scan_string(str, scanner);
  701. #ifdef PARSER_DEBUG
  702. parse_events_debug = 1;
  703. #endif
  704. ret = parse_events_parse(data, scanner);
  705. parse_events__flush_buffer(buffer, scanner);
  706. parse_events__delete_buffer(buffer, scanner);
  707. parse_events_lex_destroy(scanner);
  708. return ret;
  709. }
  710. /*
  711. * parse event config string, return a list of event terms.
  712. */
  713. int parse_events_terms(struct list_head *terms, const char *str)
  714. {
  715. struct parse_events_terms data = {
  716. .terms = NULL,
  717. };
  718. int ret;
  719. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  720. if (!ret) {
  721. list_splice(data.terms, terms);
  722. free(data.terms);
  723. return 0;
  724. }
  725. parse_events__free_terms(data.terms);
  726. return ret;
  727. }
  728. int parse_events(struct perf_evlist *evlist, const char *str)
  729. {
  730. struct parse_events_evlist data = {
  731. .list = LIST_HEAD_INIT(data.list),
  732. .idx = evlist->nr_entries,
  733. };
  734. int ret;
  735. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  736. if (!ret) {
  737. int entries = data.idx - evlist->nr_entries;
  738. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  739. evlist->nr_groups += data.nr_groups;
  740. return 0;
  741. }
  742. /*
  743. * There are 2 users - builtin-record and builtin-test objects.
  744. * Both call perf_evlist__delete in case of error, so we dont
  745. * need to bother.
  746. */
  747. return ret;
  748. }
  749. int parse_events_option(const struct option *opt, const char *str,
  750. int unset __maybe_unused)
  751. {
  752. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  753. int ret = parse_events(evlist, str);
  754. if (ret) {
  755. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  756. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  757. }
  758. return ret;
  759. }
  760. int parse_filter(const struct option *opt, const char *str,
  761. int unset __maybe_unused)
  762. {
  763. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  764. struct perf_evsel *last = NULL;
  765. if (evlist->nr_entries > 0)
  766. last = perf_evlist__last(evlist);
  767. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  768. fprintf(stderr,
  769. "-F option should follow a -e tracepoint option\n");
  770. return -1;
  771. }
  772. last->filter = strdup(str);
  773. if (last->filter == NULL) {
  774. fprintf(stderr, "not enough memory to hold filter string\n");
  775. return -1;
  776. }
  777. return 0;
  778. }
  779. static const char * const event_type_descriptors[] = {
  780. "Hardware event",
  781. "Software event",
  782. "Tracepoint event",
  783. "Hardware cache event",
  784. "Raw hardware event descriptor",
  785. "Hardware breakpoint",
  786. };
  787. /*
  788. * Print the events from <debugfs_mount_point>/tracing/events
  789. */
  790. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  791. bool name_only)
  792. {
  793. DIR *sys_dir, *evt_dir;
  794. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  795. char evt_path[MAXPATHLEN];
  796. char dir_path[MAXPATHLEN];
  797. if (debugfs_valid_mountpoint(tracing_events_path))
  798. return;
  799. sys_dir = opendir(tracing_events_path);
  800. if (!sys_dir)
  801. return;
  802. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  803. if (subsys_glob != NULL &&
  804. !strglobmatch(sys_dirent.d_name, subsys_glob))
  805. continue;
  806. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  807. sys_dirent.d_name);
  808. evt_dir = opendir(dir_path);
  809. if (!evt_dir)
  810. continue;
  811. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  812. if (event_glob != NULL &&
  813. !strglobmatch(evt_dirent.d_name, event_glob))
  814. continue;
  815. if (name_only) {
  816. printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
  817. continue;
  818. }
  819. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  820. sys_dirent.d_name, evt_dirent.d_name);
  821. printf(" %-50s [%s]\n", evt_path,
  822. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  823. }
  824. closedir(evt_dir);
  825. }
  826. closedir(sys_dir);
  827. }
  828. /*
  829. * Check whether event is in <debugfs_mount_point>/tracing/events
  830. */
  831. int is_valid_tracepoint(const char *event_string)
  832. {
  833. DIR *sys_dir, *evt_dir;
  834. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  835. char evt_path[MAXPATHLEN];
  836. char dir_path[MAXPATHLEN];
  837. if (debugfs_valid_mountpoint(tracing_events_path))
  838. return 0;
  839. sys_dir = opendir(tracing_events_path);
  840. if (!sys_dir)
  841. return 0;
  842. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  843. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  844. sys_dirent.d_name);
  845. evt_dir = opendir(dir_path);
  846. if (!evt_dir)
  847. continue;
  848. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  849. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  850. sys_dirent.d_name, evt_dirent.d_name);
  851. if (!strcmp(evt_path, event_string)) {
  852. closedir(evt_dir);
  853. closedir(sys_dir);
  854. return 1;
  855. }
  856. }
  857. closedir(evt_dir);
  858. }
  859. closedir(sys_dir);
  860. return 0;
  861. }
  862. static void __print_events_type(u8 type, struct event_symbol *syms,
  863. unsigned max)
  864. {
  865. char name[64];
  866. unsigned i;
  867. for (i = 0; i < max ; i++, syms++) {
  868. if (strlen(syms->alias))
  869. snprintf(name, sizeof(name), "%s OR %s",
  870. syms->symbol, syms->alias);
  871. else
  872. snprintf(name, sizeof(name), "%s", syms->symbol);
  873. printf(" %-50s [%s]\n", name,
  874. event_type_descriptors[type]);
  875. }
  876. }
  877. void print_events_type(u8 type)
  878. {
  879. if (type == PERF_TYPE_SOFTWARE)
  880. __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
  881. else
  882. __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
  883. }
  884. int print_hwcache_events(const char *event_glob, bool name_only)
  885. {
  886. unsigned int type, op, i, printed = 0;
  887. char name[64];
  888. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  889. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  890. /* skip invalid cache type */
  891. if (!perf_evsel__is_cache_op_valid(type, op))
  892. continue;
  893. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  894. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  895. name, sizeof(name));
  896. if (event_glob != NULL && !strglobmatch(name, event_glob))
  897. continue;
  898. if (name_only)
  899. printf("%s ", name);
  900. else
  901. printf(" %-50s [%s]\n", name,
  902. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  903. ++printed;
  904. }
  905. }
  906. }
  907. return printed;
  908. }
  909. static void print_symbol_events(const char *event_glob, unsigned type,
  910. struct event_symbol *syms, unsigned max,
  911. bool name_only)
  912. {
  913. unsigned i, printed = 0;
  914. char name[MAX_NAME_LEN];
  915. for (i = 0; i < max; i++, syms++) {
  916. if (event_glob != NULL &&
  917. !(strglobmatch(syms->symbol, event_glob) ||
  918. (syms->alias && strglobmatch(syms->alias, event_glob))))
  919. continue;
  920. if (name_only) {
  921. printf("%s ", syms->symbol);
  922. continue;
  923. }
  924. if (strlen(syms->alias))
  925. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  926. else
  927. strncpy(name, syms->symbol, MAX_NAME_LEN);
  928. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  929. printed++;
  930. }
  931. if (printed)
  932. printf("\n");
  933. }
  934. /*
  935. * Print the help text for the event symbols:
  936. */
  937. void print_events(const char *event_glob, bool name_only)
  938. {
  939. if (!name_only) {
  940. printf("\n");
  941. printf("List of pre-defined events (to be used in -e):\n");
  942. }
  943. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  944. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  945. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  946. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  947. print_hwcache_events(event_glob, name_only);
  948. if (event_glob != NULL)
  949. return;
  950. if (!name_only) {
  951. printf("\n");
  952. printf(" %-50s [%s]\n",
  953. "rNNN",
  954. event_type_descriptors[PERF_TYPE_RAW]);
  955. printf(" %-50s [%s]\n",
  956. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  957. event_type_descriptors[PERF_TYPE_RAW]);
  958. printf(" (see 'man perf-list' on how to encode it)\n");
  959. printf("\n");
  960. printf(" %-50s [%s]\n",
  961. "mem:<addr>[:access]",
  962. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  963. printf("\n");
  964. }
  965. print_tracepoint_events(NULL, NULL, name_only);
  966. }
  967. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  968. {
  969. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  970. }
  971. static int new_term(struct parse_events_term **_term, int type_val,
  972. int type_term, char *config,
  973. char *str, u64 num)
  974. {
  975. struct parse_events_term *term;
  976. term = zalloc(sizeof(*term));
  977. if (!term)
  978. return -ENOMEM;
  979. INIT_LIST_HEAD(&term->list);
  980. term->type_val = type_val;
  981. term->type_term = type_term;
  982. term->config = config;
  983. switch (type_val) {
  984. case PARSE_EVENTS__TERM_TYPE_NUM:
  985. term->val.num = num;
  986. break;
  987. case PARSE_EVENTS__TERM_TYPE_STR:
  988. term->val.str = str;
  989. break;
  990. default:
  991. return -EINVAL;
  992. }
  993. *_term = term;
  994. return 0;
  995. }
  996. int parse_events_term__num(struct parse_events_term **term,
  997. int type_term, char *config, u64 num)
  998. {
  999. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1000. config, NULL, num);
  1001. }
  1002. int parse_events_term__str(struct parse_events_term **term,
  1003. int type_term, char *config, char *str)
  1004. {
  1005. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1006. config, str, 0);
  1007. }
  1008. int parse_events_term__sym_hw(struct parse_events_term **term,
  1009. char *config, unsigned idx)
  1010. {
  1011. struct event_symbol *sym;
  1012. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1013. sym = &event_symbols_hw[idx];
  1014. if (config)
  1015. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1016. PARSE_EVENTS__TERM_TYPE_USER, config,
  1017. (char *) sym->symbol, 0);
  1018. else
  1019. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1020. PARSE_EVENTS__TERM_TYPE_USER,
  1021. (char *) "event", (char *) sym->symbol, 0);
  1022. }
  1023. int parse_events_term__clone(struct parse_events_term **new,
  1024. struct parse_events_term *term)
  1025. {
  1026. return new_term(new, term->type_val, term->type_term, term->config,
  1027. term->val.str, term->val.num);
  1028. }
  1029. void parse_events__free_terms(struct list_head *terms)
  1030. {
  1031. struct parse_events_term *term, *h;
  1032. list_for_each_entry_safe(term, h, terms, list)
  1033. free(term);
  1034. free(terms);
  1035. }