parse-events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", 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,
  518. char *event_mod)
  519. {
  520. return parse_events__modifier_event(list, event_mod, true);
  521. }
  522. void parse_events__set_leader(char *name, struct list_head *list)
  523. {
  524. struct perf_evsel *leader;
  525. __perf_evlist__set_leader(list);
  526. leader = list_entry(list->next, struct perf_evsel, node);
  527. leader->group_name = name ? strdup(name) : NULL;
  528. }
  529. void parse_events_update_lists(struct list_head *list_event,
  530. struct list_head *list_all)
  531. {
  532. /*
  533. * Called for single event definition. Update the
  534. * 'all event' list, and reinit the 'single event'
  535. * list, for next event definition.
  536. */
  537. list_splice_tail(list_event, list_all);
  538. free(list_event);
  539. }
  540. struct event_modifier {
  541. int eu;
  542. int ek;
  543. int eh;
  544. int eH;
  545. int eG;
  546. int precise;
  547. int exclude_GH;
  548. };
  549. static int get_event_modifier(struct event_modifier *mod, char *str,
  550. struct perf_evsel *evsel)
  551. {
  552. int eu = evsel ? evsel->attr.exclude_user : 0;
  553. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  554. int eh = evsel ? evsel->attr.exclude_hv : 0;
  555. int eH = evsel ? evsel->attr.exclude_host : 0;
  556. int eG = evsel ? evsel->attr.exclude_guest : 0;
  557. int precise = evsel ? evsel->attr.precise_ip : 0;
  558. int exclude = eu | ek | eh;
  559. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  560. /*
  561. * We are here for group and 'GH' was not set as event
  562. * modifier and whatever event/group modifier override
  563. * default 'GH' setup.
  564. */
  565. if (evsel && !exclude_GH)
  566. eH = eG = 0;
  567. memset(mod, 0, sizeof(*mod));
  568. while (*str) {
  569. if (*str == 'u') {
  570. if (!exclude)
  571. exclude = eu = ek = eh = 1;
  572. eu = 0;
  573. } else if (*str == 'k') {
  574. if (!exclude)
  575. exclude = eu = ek = eh = 1;
  576. ek = 0;
  577. } else if (*str == 'h') {
  578. if (!exclude)
  579. exclude = eu = ek = eh = 1;
  580. eh = 0;
  581. } else if (*str == 'G') {
  582. if (!exclude_GH)
  583. exclude_GH = eG = eH = 1;
  584. eG = 0;
  585. } else if (*str == 'H') {
  586. if (!exclude_GH)
  587. exclude_GH = eG = eH = 1;
  588. eH = 0;
  589. } else if (*str == 'p') {
  590. precise++;
  591. } else
  592. break;
  593. ++str;
  594. }
  595. /*
  596. * precise ip:
  597. *
  598. * 0 - SAMPLE_IP can have arbitrary skid
  599. * 1 - SAMPLE_IP must have constant skid
  600. * 2 - SAMPLE_IP requested to have 0 skid
  601. * 3 - SAMPLE_IP must have 0 skid
  602. *
  603. * See also PERF_RECORD_MISC_EXACT_IP
  604. */
  605. if (precise > 3)
  606. return -EINVAL;
  607. mod->eu = eu;
  608. mod->ek = ek;
  609. mod->eh = eh;
  610. mod->eH = eH;
  611. mod->eG = eG;
  612. mod->precise = precise;
  613. mod->exclude_GH = exclude_GH;
  614. return 0;
  615. }
  616. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  617. {
  618. struct perf_evsel *evsel;
  619. struct event_modifier mod;
  620. if (str == NULL)
  621. return 0;
  622. if (!add && get_event_modifier(&mod, str, NULL))
  623. return -EINVAL;
  624. list_for_each_entry(evsel, list, node) {
  625. if (add && get_event_modifier(&mod, str, evsel))
  626. return -EINVAL;
  627. evsel->attr.exclude_user = mod.eu;
  628. evsel->attr.exclude_kernel = mod.ek;
  629. evsel->attr.exclude_hv = mod.eh;
  630. evsel->attr.precise_ip = mod.precise;
  631. evsel->attr.exclude_host = mod.eH;
  632. evsel->attr.exclude_guest = mod.eG;
  633. evsel->exclude_GH = mod.exclude_GH;
  634. }
  635. return 0;
  636. }
  637. int parse_events_name(struct list_head *list, char *name)
  638. {
  639. struct perf_evsel *evsel;
  640. list_for_each_entry(evsel, list, node) {
  641. if (!evsel->name)
  642. evsel->name = strdup(name);
  643. }
  644. return 0;
  645. }
  646. static int parse_events__scanner(const char *str, void *data, int start_token)
  647. {
  648. YY_BUFFER_STATE buffer;
  649. void *scanner;
  650. int ret;
  651. ret = parse_events_lex_init_extra(start_token, &scanner);
  652. if (ret)
  653. return ret;
  654. buffer = parse_events__scan_string(str, scanner);
  655. #ifdef PARSER_DEBUG
  656. parse_events_debug = 1;
  657. #endif
  658. ret = parse_events_parse(data, scanner);
  659. parse_events__flush_buffer(buffer, scanner);
  660. parse_events__delete_buffer(buffer, scanner);
  661. parse_events_lex_destroy(scanner);
  662. return ret;
  663. }
  664. /*
  665. * parse event config string, return a list of event terms.
  666. */
  667. int parse_events_terms(struct list_head *terms, const char *str)
  668. {
  669. struct parse_events_data__terms data = {
  670. .terms = NULL,
  671. };
  672. int ret;
  673. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  674. if (!ret) {
  675. list_splice(data.terms, terms);
  676. free(data.terms);
  677. return 0;
  678. }
  679. parse_events__free_terms(data.terms);
  680. return ret;
  681. }
  682. int parse_events(struct perf_evlist *evlist, const char *str,
  683. int unset __maybe_unused)
  684. {
  685. struct parse_events_data__events data = {
  686. .list = LIST_HEAD_INIT(data.list),
  687. .idx = evlist->nr_entries,
  688. };
  689. int ret;
  690. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  691. if (!ret) {
  692. int entries = data.idx - evlist->nr_entries;
  693. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  694. return 0;
  695. }
  696. /*
  697. * There are 2 users - builtin-record and builtin-test objects.
  698. * Both call perf_evlist__delete in case of error, so we dont
  699. * need to bother.
  700. */
  701. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  702. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  703. return ret;
  704. }
  705. int parse_events_option(const struct option *opt, const char *str,
  706. int unset __maybe_unused)
  707. {
  708. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  709. return parse_events(evlist, str, unset);
  710. }
  711. int parse_filter(const struct option *opt, const char *str,
  712. int unset __maybe_unused)
  713. {
  714. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  715. struct perf_evsel *last = NULL;
  716. if (evlist->nr_entries > 0)
  717. last = perf_evlist__last(evlist);
  718. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  719. fprintf(stderr,
  720. "-F option should follow a -e tracepoint option\n");
  721. return -1;
  722. }
  723. last->filter = strdup(str);
  724. if (last->filter == NULL) {
  725. fprintf(stderr, "not enough memory to hold filter string\n");
  726. return -1;
  727. }
  728. return 0;
  729. }
  730. static const char * const event_type_descriptors[] = {
  731. "Hardware event",
  732. "Software event",
  733. "Tracepoint event",
  734. "Hardware cache event",
  735. "Raw hardware event descriptor",
  736. "Hardware breakpoint",
  737. };
  738. /*
  739. * Print the events from <debugfs_mount_point>/tracing/events
  740. */
  741. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  742. bool name_only)
  743. {
  744. DIR *sys_dir, *evt_dir;
  745. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  746. char evt_path[MAXPATHLEN];
  747. char dir_path[MAXPATHLEN];
  748. if (debugfs_valid_mountpoint(tracing_events_path))
  749. return;
  750. sys_dir = opendir(tracing_events_path);
  751. if (!sys_dir)
  752. return;
  753. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  754. if (subsys_glob != NULL &&
  755. !strglobmatch(sys_dirent.d_name, subsys_glob))
  756. continue;
  757. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  758. sys_dirent.d_name);
  759. evt_dir = opendir(dir_path);
  760. if (!evt_dir)
  761. continue;
  762. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  763. if (event_glob != NULL &&
  764. !strglobmatch(evt_dirent.d_name, event_glob))
  765. continue;
  766. if (name_only) {
  767. printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
  768. continue;
  769. }
  770. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  771. sys_dirent.d_name, evt_dirent.d_name);
  772. printf(" %-50s [%s]\n", evt_path,
  773. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  774. }
  775. closedir(evt_dir);
  776. }
  777. closedir(sys_dir);
  778. }
  779. /*
  780. * Check whether event is in <debugfs_mount_point>/tracing/events
  781. */
  782. int is_valid_tracepoint(const char *event_string)
  783. {
  784. DIR *sys_dir, *evt_dir;
  785. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  786. char evt_path[MAXPATHLEN];
  787. char dir_path[MAXPATHLEN];
  788. if (debugfs_valid_mountpoint(tracing_events_path))
  789. return 0;
  790. sys_dir = opendir(tracing_events_path);
  791. if (!sys_dir)
  792. return 0;
  793. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  794. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  795. sys_dirent.d_name);
  796. evt_dir = opendir(dir_path);
  797. if (!evt_dir)
  798. continue;
  799. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  800. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  801. sys_dirent.d_name, evt_dirent.d_name);
  802. if (!strcmp(evt_path, event_string)) {
  803. closedir(evt_dir);
  804. closedir(sys_dir);
  805. return 1;
  806. }
  807. }
  808. closedir(evt_dir);
  809. }
  810. closedir(sys_dir);
  811. return 0;
  812. }
  813. static void __print_events_type(u8 type, struct event_symbol *syms,
  814. unsigned max)
  815. {
  816. char name[64];
  817. unsigned i;
  818. for (i = 0; i < max ; i++, syms++) {
  819. if (strlen(syms->alias))
  820. snprintf(name, sizeof(name), "%s OR %s",
  821. syms->symbol, syms->alias);
  822. else
  823. snprintf(name, sizeof(name), "%s", syms->symbol);
  824. printf(" %-50s [%s]\n", name,
  825. event_type_descriptors[type]);
  826. }
  827. }
  828. void print_events_type(u8 type)
  829. {
  830. if (type == PERF_TYPE_SOFTWARE)
  831. __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
  832. else
  833. __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
  834. }
  835. int print_hwcache_events(const char *event_glob, bool name_only)
  836. {
  837. unsigned int type, op, i, printed = 0;
  838. char name[64];
  839. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  840. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  841. /* skip invalid cache type */
  842. if (!perf_evsel__is_cache_op_valid(type, op))
  843. continue;
  844. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  845. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  846. name, sizeof(name));
  847. if (event_glob != NULL && !strglobmatch(name, event_glob))
  848. continue;
  849. if (name_only)
  850. printf("%s ", name);
  851. else
  852. printf(" %-50s [%s]\n", name,
  853. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  854. ++printed;
  855. }
  856. }
  857. }
  858. return printed;
  859. }
  860. static void print_symbol_events(const char *event_glob, unsigned type,
  861. struct event_symbol *syms, unsigned max,
  862. bool name_only)
  863. {
  864. unsigned i, printed = 0;
  865. char name[MAX_NAME_LEN];
  866. for (i = 0; i < max; i++, syms++) {
  867. if (event_glob != NULL &&
  868. !(strglobmatch(syms->symbol, event_glob) ||
  869. (syms->alias && strglobmatch(syms->alias, event_glob))))
  870. continue;
  871. if (name_only) {
  872. printf("%s ", syms->symbol);
  873. continue;
  874. }
  875. if (strlen(syms->alias))
  876. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  877. else
  878. strncpy(name, syms->symbol, MAX_NAME_LEN);
  879. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  880. printed++;
  881. }
  882. if (printed)
  883. printf("\n");
  884. }
  885. /*
  886. * Print the help text for the event symbols:
  887. */
  888. void print_events(const char *event_glob, bool name_only)
  889. {
  890. if (!name_only) {
  891. printf("\n");
  892. printf("List of pre-defined events (to be used in -e):\n");
  893. }
  894. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  895. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  896. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  897. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  898. print_hwcache_events(event_glob, name_only);
  899. if (event_glob != NULL)
  900. return;
  901. if (!name_only) {
  902. printf("\n");
  903. printf(" %-50s [%s]\n",
  904. "rNNN",
  905. event_type_descriptors[PERF_TYPE_RAW]);
  906. printf(" %-50s [%s]\n",
  907. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  908. event_type_descriptors[PERF_TYPE_RAW]);
  909. printf(" (see 'perf list --help' on how to encode it)\n");
  910. printf("\n");
  911. printf(" %-50s [%s]\n",
  912. "mem:<addr>[:access]",
  913. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  914. printf("\n");
  915. }
  916. print_tracepoint_events(NULL, NULL, name_only);
  917. }
  918. int parse_events__is_hardcoded_term(struct parse_events__term *term)
  919. {
  920. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  921. }
  922. static int new_term(struct parse_events__term **_term, int type_val,
  923. int type_term, char *config,
  924. char *str, u64 num)
  925. {
  926. struct parse_events__term *term;
  927. term = zalloc(sizeof(*term));
  928. if (!term)
  929. return -ENOMEM;
  930. INIT_LIST_HEAD(&term->list);
  931. term->type_val = type_val;
  932. term->type_term = type_term;
  933. term->config = config;
  934. switch (type_val) {
  935. case PARSE_EVENTS__TERM_TYPE_NUM:
  936. term->val.num = num;
  937. break;
  938. case PARSE_EVENTS__TERM_TYPE_STR:
  939. term->val.str = str;
  940. break;
  941. default:
  942. return -EINVAL;
  943. }
  944. *_term = term;
  945. return 0;
  946. }
  947. int parse_events__term_num(struct parse_events__term **term,
  948. int type_term, char *config, u64 num)
  949. {
  950. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  951. config, NULL, num);
  952. }
  953. int parse_events__term_str(struct parse_events__term **term,
  954. int type_term, char *config, char *str)
  955. {
  956. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  957. config, str, 0);
  958. }
  959. int parse_events__term_clone(struct parse_events__term **new,
  960. struct parse_events__term *term)
  961. {
  962. return new_term(new, term->type_val, term->type_term, term->config,
  963. term->val.str, term->val.num);
  964. }
  965. void parse_events__free_terms(struct list_head *terms)
  966. {
  967. struct parse_events__term *term, *h;
  968. list_for_each_entry_safe(term, h, terms, list)
  969. free(term);
  970. free(terms);
  971. }