parse-events.c 24 KB

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