parse-events.c 29 KB

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