parse-events.c 33 KB

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