parse-events.c 29 KB

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