parse-events.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. #include "../../../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 "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, char *name)
  215. {
  216. struct perf_evsel *evsel;
  217. struct list_head *list = *_list;
  218. if (!list) {
  219. list = malloc(sizeof(*list));
  220. if (!list)
  221. return -ENOMEM;
  222. INIT_LIST_HEAD(list);
  223. }
  224. event_attr_init(attr);
  225. evsel = perf_evsel__new(attr, (*idx)++);
  226. if (!evsel) {
  227. free(list);
  228. return -ENOMEM;
  229. }
  230. if (name)
  231. evsel->name = strdup(name);
  232. list_add_tail(&evsel->node, list);
  233. *_list = list;
  234. return 0;
  235. }
  236. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  237. {
  238. int i, j;
  239. int n, longest = -1;
  240. for (i = 0; i < size; i++) {
  241. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  242. n = strlen(names[i][j]);
  243. if (n > longest && !strncasecmp(str, names[i][j], n))
  244. longest = n;
  245. }
  246. if (longest > 0)
  247. return i;
  248. }
  249. return -1;
  250. }
  251. int parse_events_add_cache(struct list_head **list, int *idx,
  252. char *type, char *op_result1, char *op_result2)
  253. {
  254. struct perf_event_attr attr;
  255. char name[MAX_NAME_LEN];
  256. int cache_type = -1, cache_op = -1, cache_result = -1;
  257. char *op_result[2] = { op_result1, op_result2 };
  258. int i, n;
  259. /*
  260. * No fallback - if we cannot get a clear cache type
  261. * then bail out:
  262. */
  263. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  264. PERF_COUNT_HW_CACHE_MAX);
  265. if (cache_type == -1)
  266. return -EINVAL;
  267. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  268. for (i = 0; (i < 2) && (op_result[i]); i++) {
  269. char *str = op_result[i];
  270. snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
  271. if (cache_op == -1) {
  272. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  273. PERF_COUNT_HW_CACHE_OP_MAX);
  274. if (cache_op >= 0) {
  275. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  276. return -EINVAL;
  277. continue;
  278. }
  279. }
  280. if (cache_result == -1) {
  281. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  282. PERF_COUNT_HW_CACHE_RESULT_MAX);
  283. if (cache_result >= 0)
  284. continue;
  285. }
  286. }
  287. /*
  288. * Fall back to reads:
  289. */
  290. if (cache_op == -1)
  291. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  292. /*
  293. * Fall back to accesses:
  294. */
  295. if (cache_result == -1)
  296. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  297. memset(&attr, 0, sizeof(attr));
  298. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  299. attr.type = PERF_TYPE_HW_CACHE;
  300. return add_event(list, idx, &attr, name);
  301. }
  302. static int add_tracepoint(struct list_head **list, int *idx,
  303. char *sys_name, char *evt_name)
  304. {
  305. struct perf_event_attr attr;
  306. char name[MAX_NAME_LEN];
  307. char evt_path[MAXPATHLEN];
  308. char id_buf[4];
  309. u64 id;
  310. int fd;
  311. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  312. sys_name, evt_name);
  313. fd = open(evt_path, O_RDONLY);
  314. if (fd < 0)
  315. return -1;
  316. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  317. close(fd);
  318. return -1;
  319. }
  320. close(fd);
  321. id = atoll(id_buf);
  322. memset(&attr, 0, sizeof(attr));
  323. attr.config = id;
  324. attr.type = PERF_TYPE_TRACEPOINT;
  325. attr.sample_type |= PERF_SAMPLE_RAW;
  326. attr.sample_type |= PERF_SAMPLE_TIME;
  327. attr.sample_type |= PERF_SAMPLE_CPU;
  328. attr.sample_type |= PERF_SAMPLE_PERIOD;
  329. attr.sample_period = 1;
  330. snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
  331. return add_event(list, idx, &attr, name);
  332. }
  333. static int add_tracepoint_multi(struct list_head **list, int *idx,
  334. char *sys_name, char *evt_name)
  335. {
  336. char evt_path[MAXPATHLEN];
  337. struct dirent *evt_ent;
  338. DIR *evt_dir;
  339. int ret = 0;
  340. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  341. evt_dir = opendir(evt_path);
  342. if (!evt_dir) {
  343. perror("Can't open event dir");
  344. return -1;
  345. }
  346. while (!ret && (evt_ent = readdir(evt_dir))) {
  347. if (!strcmp(evt_ent->d_name, ".")
  348. || !strcmp(evt_ent->d_name, "..")
  349. || !strcmp(evt_ent->d_name, "enable")
  350. || !strcmp(evt_ent->d_name, "filter"))
  351. continue;
  352. if (!strglobmatch(evt_ent->d_name, evt_name))
  353. continue;
  354. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  355. }
  356. return ret;
  357. }
  358. int parse_events_add_tracepoint(struct list_head **list, int *idx,
  359. char *sys, char *event)
  360. {
  361. int ret;
  362. ret = debugfs_valid_mountpoint(tracing_events_path);
  363. if (ret)
  364. return ret;
  365. return strpbrk(event, "*?") ?
  366. add_tracepoint_multi(list, idx, sys, event) :
  367. add_tracepoint(list, idx, sys, event);
  368. }
  369. static int
  370. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  371. {
  372. int i;
  373. for (i = 0; i < 3; i++) {
  374. if (!type || !type[i])
  375. break;
  376. #define CHECK_SET_TYPE(bit) \
  377. do { \
  378. if (attr->bp_type & bit) \
  379. return -EINVAL; \
  380. else \
  381. attr->bp_type |= bit; \
  382. } while (0)
  383. switch (type[i]) {
  384. case 'r':
  385. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  386. break;
  387. case 'w':
  388. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  389. break;
  390. case 'x':
  391. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  392. break;
  393. default:
  394. return -EINVAL;
  395. }
  396. }
  397. #undef CHECK_SET_TYPE
  398. if (!attr->bp_type) /* Default */
  399. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  400. return 0;
  401. }
  402. int parse_events_add_breakpoint(struct list_head **list, int *idx,
  403. void *ptr, char *type)
  404. {
  405. struct perf_event_attr attr;
  406. memset(&attr, 0, sizeof(attr));
  407. attr.bp_addr = (unsigned long) ptr;
  408. if (parse_breakpoint_type(type, &attr))
  409. return -EINVAL;
  410. /*
  411. * We should find a nice way to override the access length
  412. * Provide some defaults for now
  413. */
  414. if (attr.bp_type == HW_BREAKPOINT_X)
  415. attr.bp_len = sizeof(long);
  416. else
  417. attr.bp_len = HW_BREAKPOINT_LEN_4;
  418. attr.type = PERF_TYPE_BREAKPOINT;
  419. attr.sample_period = 1;
  420. return add_event(list, idx, &attr, NULL);
  421. }
  422. static int config_term(struct perf_event_attr *attr,
  423. struct parse_events__term *term)
  424. {
  425. #define CHECK_TYPE_VAL(type) \
  426. do { \
  427. if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
  428. return -EINVAL; \
  429. } while (0)
  430. switch (term->type_term) {
  431. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  432. CHECK_TYPE_VAL(NUM);
  433. attr->config = term->val.num;
  434. break;
  435. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  436. CHECK_TYPE_VAL(NUM);
  437. attr->config1 = term->val.num;
  438. break;
  439. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  440. CHECK_TYPE_VAL(NUM);
  441. attr->config2 = term->val.num;
  442. break;
  443. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  444. CHECK_TYPE_VAL(NUM);
  445. attr->sample_period = term->val.num;
  446. break;
  447. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  448. /*
  449. * TODO uncomment when the field is available
  450. * attr->branch_sample_type = term->val.num;
  451. */
  452. break;
  453. case PARSE_EVENTS__TERM_TYPE_NAME:
  454. CHECK_TYPE_VAL(STR);
  455. break;
  456. default:
  457. return -EINVAL;
  458. }
  459. return 0;
  460. #undef CHECK_TYPE_VAL
  461. }
  462. static int config_attr(struct perf_event_attr *attr,
  463. struct list_head *head, int fail)
  464. {
  465. struct parse_events__term *term;
  466. list_for_each_entry(term, head, list)
  467. if (config_term(attr, term) && fail)
  468. return -EINVAL;
  469. return 0;
  470. }
  471. int parse_events_add_numeric(struct list_head **list, int *idx,
  472. u32 type, u64 config,
  473. struct list_head *head_config)
  474. {
  475. struct perf_event_attr attr;
  476. memset(&attr, 0, sizeof(attr));
  477. attr.type = type;
  478. attr.config = config;
  479. if (head_config &&
  480. config_attr(&attr, head_config, 1))
  481. return -EINVAL;
  482. return add_event(list, idx, &attr, NULL);
  483. }
  484. static int parse_events__is_name_term(struct parse_events__term *term)
  485. {
  486. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  487. }
  488. static char *pmu_event_name(struct list_head *head_terms)
  489. {
  490. struct parse_events__term *term;
  491. list_for_each_entry(term, head_terms, list)
  492. if (parse_events__is_name_term(term))
  493. return term->val.str;
  494. return NULL;
  495. }
  496. int parse_events_add_pmu(struct list_head **list, int *idx,
  497. char *name, struct list_head *head_config)
  498. {
  499. struct perf_event_attr attr;
  500. struct perf_pmu *pmu;
  501. pmu = perf_pmu__find(name);
  502. if (!pmu)
  503. return -EINVAL;
  504. memset(&attr, 0, sizeof(attr));
  505. if (perf_pmu__check_alias(pmu, head_config))
  506. return -EINVAL;
  507. /*
  508. * Configure hardcoded terms first, no need to check
  509. * return value when called with fail == 0 ;)
  510. */
  511. config_attr(&attr, head_config, 0);
  512. if (perf_pmu__config(pmu, &attr, head_config))
  513. return -EINVAL;
  514. return add_event(list, idx, &attr,
  515. pmu_event_name(head_config));
  516. }
  517. int parse_events__modifier_group(struct list_head *list __used,
  518. char *event_mod __used)
  519. {
  520. return 0;
  521. }
  522. void parse_events__group(char *name __used, struct list_head *list __used)
  523. {
  524. }
  525. void parse_events_update_lists(struct list_head *list_event,
  526. struct list_head *list_all)
  527. {
  528. /*
  529. * Called for single event definition. Update the
  530. * 'all event' list, and reinit the 'single event'
  531. * list, for next event definition.
  532. */
  533. list_splice_tail(list_event, list_all);
  534. free(list_event);
  535. }
  536. int parse_events__modifier_event(struct list_head *list, char *str)
  537. {
  538. struct perf_evsel *evsel;
  539. int exclude = 0, exclude_GH = 0;
  540. int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
  541. if (str == NULL)
  542. return 0;
  543. while (*str) {
  544. if (*str == 'u') {
  545. if (!exclude)
  546. exclude = eu = ek = eh = 1;
  547. eu = 0;
  548. } else if (*str == 'k') {
  549. if (!exclude)
  550. exclude = eu = ek = eh = 1;
  551. ek = 0;
  552. } else if (*str == 'h') {
  553. if (!exclude)
  554. exclude = eu = ek = eh = 1;
  555. eh = 0;
  556. } else if (*str == 'G') {
  557. if (!exclude_GH)
  558. exclude_GH = eG = eH = 1;
  559. eG = 0;
  560. } else if (*str == 'H') {
  561. if (!exclude_GH)
  562. exclude_GH = eG = eH = 1;
  563. eH = 0;
  564. } else if (*str == 'p') {
  565. precise++;
  566. } else
  567. break;
  568. ++str;
  569. }
  570. /*
  571. * precise ip:
  572. *
  573. * 0 - SAMPLE_IP can have arbitrary skid
  574. * 1 - SAMPLE_IP must have constant skid
  575. * 2 - SAMPLE_IP requested to have 0 skid
  576. * 3 - SAMPLE_IP must have 0 skid
  577. *
  578. * See also PERF_RECORD_MISC_EXACT_IP
  579. */
  580. if (precise > 3)
  581. return -EINVAL;
  582. list_for_each_entry(evsel, list, node) {
  583. evsel->attr.exclude_user = eu;
  584. evsel->attr.exclude_kernel = ek;
  585. evsel->attr.exclude_hv = eh;
  586. evsel->attr.precise_ip = precise;
  587. evsel->attr.exclude_host = eH;
  588. evsel->attr.exclude_guest = eG;
  589. }
  590. return 0;
  591. }
  592. static int parse_events__scanner(const char *str, void *data, int start_token)
  593. {
  594. YY_BUFFER_STATE buffer;
  595. void *scanner;
  596. int ret;
  597. ret = parse_events_lex_init_extra(start_token, &scanner);
  598. if (ret)
  599. return ret;
  600. buffer = parse_events__scan_string(str, scanner);
  601. #ifdef PARSER_DEBUG
  602. parse_events_debug = 1;
  603. #endif
  604. ret = parse_events_parse(data, scanner);
  605. parse_events__flush_buffer(buffer, scanner);
  606. parse_events__delete_buffer(buffer, scanner);
  607. parse_events_lex_destroy(scanner);
  608. return ret;
  609. }
  610. /*
  611. * parse event config string, return a list of event terms.
  612. */
  613. int parse_events_terms(struct list_head *terms, const char *str)
  614. {
  615. struct parse_events_data__terms data = {
  616. .terms = NULL,
  617. };
  618. int ret;
  619. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  620. if (!ret) {
  621. list_splice(data.terms, terms);
  622. free(data.terms);
  623. return 0;
  624. }
  625. parse_events__free_terms(data.terms);
  626. return ret;
  627. }
  628. int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
  629. {
  630. struct parse_events_data__events data = {
  631. .list = LIST_HEAD_INIT(data.list),
  632. .idx = evlist->nr_entries,
  633. };
  634. int ret;
  635. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  636. if (!ret) {
  637. int entries = data.idx - evlist->nr_entries;
  638. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  639. return 0;
  640. }
  641. /*
  642. * There are 2 users - builtin-record and builtin-test objects.
  643. * Both call perf_evlist__delete in case of error, so we dont
  644. * need to bother.
  645. */
  646. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  647. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  648. return ret;
  649. }
  650. int parse_events_option(const struct option *opt, const char *str,
  651. int unset __used)
  652. {
  653. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  654. return parse_events(evlist, str, unset);
  655. }
  656. int parse_filter(const struct option *opt, const char *str,
  657. int unset __used)
  658. {
  659. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  660. struct perf_evsel *last = NULL;
  661. if (evlist->nr_entries > 0)
  662. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  663. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  664. fprintf(stderr,
  665. "-F option should follow a -e tracepoint option\n");
  666. return -1;
  667. }
  668. last->filter = strdup(str);
  669. if (last->filter == NULL) {
  670. fprintf(stderr, "not enough memory to hold filter string\n");
  671. return -1;
  672. }
  673. return 0;
  674. }
  675. static const char * const event_type_descriptors[] = {
  676. "Hardware event",
  677. "Software event",
  678. "Tracepoint event",
  679. "Hardware cache event",
  680. "Raw hardware event descriptor",
  681. "Hardware breakpoint",
  682. };
  683. /*
  684. * Print the events from <debugfs_mount_point>/tracing/events
  685. */
  686. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  687. bool name_only)
  688. {
  689. DIR *sys_dir, *evt_dir;
  690. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  691. char evt_path[MAXPATHLEN];
  692. char dir_path[MAXPATHLEN];
  693. if (debugfs_valid_mountpoint(tracing_events_path))
  694. return;
  695. sys_dir = opendir(tracing_events_path);
  696. if (!sys_dir)
  697. return;
  698. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  699. if (subsys_glob != NULL &&
  700. !strglobmatch(sys_dirent.d_name, subsys_glob))
  701. continue;
  702. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  703. sys_dirent.d_name);
  704. evt_dir = opendir(dir_path);
  705. if (!evt_dir)
  706. continue;
  707. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  708. if (event_glob != NULL &&
  709. !strglobmatch(evt_dirent.d_name, event_glob))
  710. continue;
  711. if (name_only) {
  712. printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
  713. continue;
  714. }
  715. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  716. sys_dirent.d_name, evt_dirent.d_name);
  717. printf(" %-50s [%s]\n", evt_path,
  718. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  719. }
  720. closedir(evt_dir);
  721. }
  722. closedir(sys_dir);
  723. }
  724. /*
  725. * Check whether event is in <debugfs_mount_point>/tracing/events
  726. */
  727. int is_valid_tracepoint(const char *event_string)
  728. {
  729. DIR *sys_dir, *evt_dir;
  730. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  731. char evt_path[MAXPATHLEN];
  732. char dir_path[MAXPATHLEN];
  733. if (debugfs_valid_mountpoint(tracing_events_path))
  734. return 0;
  735. sys_dir = opendir(tracing_events_path);
  736. if (!sys_dir)
  737. return 0;
  738. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  739. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  740. sys_dirent.d_name);
  741. evt_dir = opendir(dir_path);
  742. if (!evt_dir)
  743. continue;
  744. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  745. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  746. sys_dirent.d_name, evt_dirent.d_name);
  747. if (!strcmp(evt_path, event_string)) {
  748. closedir(evt_dir);
  749. closedir(sys_dir);
  750. return 1;
  751. }
  752. }
  753. closedir(evt_dir);
  754. }
  755. closedir(sys_dir);
  756. return 0;
  757. }
  758. static void __print_events_type(u8 type, struct event_symbol *syms,
  759. unsigned max)
  760. {
  761. char name[64];
  762. unsigned i;
  763. for (i = 0; i < max ; i++, syms++) {
  764. if (strlen(syms->alias))
  765. snprintf(name, sizeof(name), "%s OR %s",
  766. syms->symbol, syms->alias);
  767. else
  768. snprintf(name, sizeof(name), "%s", syms->symbol);
  769. printf(" %-50s [%s]\n", name,
  770. event_type_descriptors[type]);
  771. }
  772. }
  773. void print_events_type(u8 type)
  774. {
  775. if (type == PERF_TYPE_SOFTWARE)
  776. __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
  777. else
  778. __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
  779. }
  780. int print_hwcache_events(const char *event_glob, bool name_only)
  781. {
  782. unsigned int type, op, i, printed = 0;
  783. char name[64];
  784. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  785. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  786. /* skip invalid cache type */
  787. if (!perf_evsel__is_cache_op_valid(type, op))
  788. continue;
  789. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  790. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  791. name, sizeof(name));
  792. if (event_glob != NULL && !strglobmatch(name, event_glob))
  793. continue;
  794. if (name_only)
  795. printf("%s ", name);
  796. else
  797. printf(" %-50s [%s]\n", name,
  798. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  799. ++printed;
  800. }
  801. }
  802. }
  803. return printed;
  804. }
  805. static void print_symbol_events(const char *event_glob, unsigned type,
  806. struct event_symbol *syms, unsigned max,
  807. bool name_only)
  808. {
  809. unsigned i, printed = 0;
  810. char name[MAX_NAME_LEN];
  811. for (i = 0; i < max; i++, syms++) {
  812. if (event_glob != NULL &&
  813. !(strglobmatch(syms->symbol, event_glob) ||
  814. (syms->alias && strglobmatch(syms->alias, event_glob))))
  815. continue;
  816. if (name_only) {
  817. printf("%s ", syms->symbol);
  818. continue;
  819. }
  820. if (strlen(syms->alias))
  821. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  822. else
  823. strncpy(name, syms->symbol, MAX_NAME_LEN);
  824. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  825. printed++;
  826. }
  827. if (printed)
  828. printf("\n");
  829. }
  830. /*
  831. * Print the help text for the event symbols:
  832. */
  833. void print_events(const char *event_glob, bool name_only)
  834. {
  835. if (!name_only) {
  836. printf("\n");
  837. printf("List of pre-defined events (to be used in -e):\n");
  838. }
  839. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  840. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  841. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  842. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  843. print_hwcache_events(event_glob, name_only);
  844. if (event_glob != NULL)
  845. return;
  846. if (!name_only) {
  847. printf("\n");
  848. printf(" %-50s [%s]\n",
  849. "rNNN",
  850. event_type_descriptors[PERF_TYPE_RAW]);
  851. printf(" %-50s [%s]\n",
  852. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  853. event_type_descriptors[PERF_TYPE_RAW]);
  854. printf(" (see 'perf list --help' on how to encode it)\n");
  855. printf("\n");
  856. printf(" %-50s [%s]\n",
  857. "mem:<addr>[:access]",
  858. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  859. printf("\n");
  860. }
  861. print_tracepoint_events(NULL, NULL, name_only);
  862. }
  863. int parse_events__is_hardcoded_term(struct parse_events__term *term)
  864. {
  865. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  866. }
  867. static int new_term(struct parse_events__term **_term, int type_val,
  868. int type_term, char *config,
  869. char *str, u64 num)
  870. {
  871. struct parse_events__term *term;
  872. term = zalloc(sizeof(*term));
  873. if (!term)
  874. return -ENOMEM;
  875. INIT_LIST_HEAD(&term->list);
  876. term->type_val = type_val;
  877. term->type_term = type_term;
  878. term->config = config;
  879. switch (type_val) {
  880. case PARSE_EVENTS__TERM_TYPE_NUM:
  881. term->val.num = num;
  882. break;
  883. case PARSE_EVENTS__TERM_TYPE_STR:
  884. term->val.str = str;
  885. break;
  886. default:
  887. return -EINVAL;
  888. }
  889. *_term = term;
  890. return 0;
  891. }
  892. int parse_events__term_num(struct parse_events__term **term,
  893. int type_term, char *config, u64 num)
  894. {
  895. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  896. config, NULL, num);
  897. }
  898. int parse_events__term_str(struct parse_events__term **term,
  899. int type_term, char *config, char *str)
  900. {
  901. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  902. config, str, 0);
  903. }
  904. int parse_events__term_clone(struct parse_events__term **new,
  905. struct parse_events__term *term)
  906. {
  907. return new_term(new, term->type_val, term->type_term, term->config,
  908. term->val.str, term->val.num);
  909. }
  910. void parse_events__free_terms(struct list_head *terms)
  911. {
  912. struct parse_events__term *term, *h;
  913. list_for_each_entry_safe(term, h, terms, list)
  914. free(term);
  915. free(terms);
  916. }