parse-events.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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)
  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. /*
  455. * We should find a nice way to override the access length
  456. * Provide some defaults for now
  457. */
  458. if (attr.bp_type == HW_BREAKPOINT_X)
  459. attr.bp_len = sizeof(long);
  460. else
  461. attr.bp_len = HW_BREAKPOINT_LEN_4;
  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. }
  576. return evsel ? 0 : -ENOMEM;
  577. }
  578. int parse_events__modifier_group(struct list_head *list,
  579. char *event_mod)
  580. {
  581. return parse_events__modifier_event(list, event_mod, true);
  582. }
  583. void parse_events__set_leader(char *name, struct list_head *list)
  584. {
  585. struct perf_evsel *leader;
  586. __perf_evlist__set_leader(list);
  587. leader = list_entry(list->next, struct perf_evsel, node);
  588. leader->group_name = name ? strdup(name) : NULL;
  589. }
  590. /* list_event is assumed to point to malloc'ed memory */
  591. void parse_events_update_lists(struct list_head *list_event,
  592. struct list_head *list_all)
  593. {
  594. /*
  595. * Called for single event definition. Update the
  596. * 'all event' list, and reinit the 'single event'
  597. * list, for next event definition.
  598. */
  599. list_splice_tail(list_event, list_all);
  600. free(list_event);
  601. }
  602. struct event_modifier {
  603. int eu;
  604. int ek;
  605. int eh;
  606. int eH;
  607. int eG;
  608. int precise;
  609. int exclude_GH;
  610. int sample_read;
  611. int pinned;
  612. };
  613. static int get_event_modifier(struct event_modifier *mod, char *str,
  614. struct perf_evsel *evsel)
  615. {
  616. int eu = evsel ? evsel->attr.exclude_user : 0;
  617. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  618. int eh = evsel ? evsel->attr.exclude_hv : 0;
  619. int eH = evsel ? evsel->attr.exclude_host : 0;
  620. int eG = evsel ? evsel->attr.exclude_guest : 0;
  621. int precise = evsel ? evsel->attr.precise_ip : 0;
  622. int sample_read = 0;
  623. int pinned = evsel ? evsel->attr.pinned : 0;
  624. int exclude = eu | ek | eh;
  625. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  626. memset(mod, 0, sizeof(*mod));
  627. while (*str) {
  628. if (*str == 'u') {
  629. if (!exclude)
  630. exclude = eu = ek = eh = 1;
  631. eu = 0;
  632. } else if (*str == 'k') {
  633. if (!exclude)
  634. exclude = eu = ek = eh = 1;
  635. ek = 0;
  636. } else if (*str == 'h') {
  637. if (!exclude)
  638. exclude = eu = ek = eh = 1;
  639. eh = 0;
  640. } else if (*str == 'G') {
  641. if (!exclude_GH)
  642. exclude_GH = eG = eH = 1;
  643. eG = 0;
  644. } else if (*str == 'H') {
  645. if (!exclude_GH)
  646. exclude_GH = eG = eH = 1;
  647. eH = 0;
  648. } else if (*str == 'p') {
  649. precise++;
  650. /* use of precise requires exclude_guest */
  651. if (!exclude_GH)
  652. eG = 1;
  653. } else if (*str == 'S') {
  654. sample_read = 1;
  655. } else if (*str == 'D') {
  656. pinned = 1;
  657. } else
  658. break;
  659. ++str;
  660. }
  661. /*
  662. * precise ip:
  663. *
  664. * 0 - SAMPLE_IP can have arbitrary skid
  665. * 1 - SAMPLE_IP must have constant skid
  666. * 2 - SAMPLE_IP requested to have 0 skid
  667. * 3 - SAMPLE_IP must have 0 skid
  668. *
  669. * See also PERF_RECORD_MISC_EXACT_IP
  670. */
  671. if (precise > 3)
  672. return -EINVAL;
  673. mod->eu = eu;
  674. mod->ek = ek;
  675. mod->eh = eh;
  676. mod->eH = eH;
  677. mod->eG = eG;
  678. mod->precise = precise;
  679. mod->exclude_GH = exclude_GH;
  680. mod->sample_read = sample_read;
  681. mod->pinned = pinned;
  682. return 0;
  683. }
  684. /*
  685. * Basic modifier sanity check to validate it contains only one
  686. * instance of any modifier (apart from 'p') present.
  687. */
  688. static int check_modifier(char *str)
  689. {
  690. char *p = str;
  691. /* The sizeof includes 0 byte as well. */
  692. if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
  693. return -1;
  694. while (*p) {
  695. if (*p != 'p' && strchr(p + 1, *p))
  696. return -1;
  697. p++;
  698. }
  699. return 0;
  700. }
  701. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  702. {
  703. struct perf_evsel *evsel;
  704. struct event_modifier mod;
  705. if (str == NULL)
  706. return 0;
  707. if (check_modifier(str))
  708. return -EINVAL;
  709. if (!add && get_event_modifier(&mod, str, NULL))
  710. return -EINVAL;
  711. __evlist__for_each(list, evsel) {
  712. if (add && get_event_modifier(&mod, str, evsel))
  713. return -EINVAL;
  714. evsel->attr.exclude_user = mod.eu;
  715. evsel->attr.exclude_kernel = mod.ek;
  716. evsel->attr.exclude_hv = mod.eh;
  717. evsel->attr.precise_ip = mod.precise;
  718. evsel->attr.exclude_host = mod.eH;
  719. evsel->attr.exclude_guest = mod.eG;
  720. evsel->exclude_GH = mod.exclude_GH;
  721. evsel->sample_read = mod.sample_read;
  722. if (perf_evsel__is_group_leader(evsel))
  723. evsel->attr.pinned = mod.pinned;
  724. }
  725. return 0;
  726. }
  727. int parse_events_name(struct list_head *list, char *name)
  728. {
  729. struct perf_evsel *evsel;
  730. __evlist__for_each(list, evsel) {
  731. if (!evsel->name)
  732. evsel->name = strdup(name);
  733. }
  734. return 0;
  735. }
  736. static int
  737. comp_pmu(const void *p1, const void *p2)
  738. {
  739. struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
  740. struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
  741. return strcmp(pmu1->symbol, pmu2->symbol);
  742. }
  743. static void perf_pmu__parse_cleanup(void)
  744. {
  745. if (perf_pmu_events_list_num > 0) {
  746. struct perf_pmu_event_symbol *p;
  747. int i;
  748. for (i = 0; i < perf_pmu_events_list_num; i++) {
  749. p = perf_pmu_events_list + i;
  750. free(p->symbol);
  751. }
  752. free(perf_pmu_events_list);
  753. perf_pmu_events_list = NULL;
  754. perf_pmu_events_list_num = 0;
  755. }
  756. }
  757. #define SET_SYMBOL(str, stype) \
  758. do { \
  759. p->symbol = str; \
  760. if (!p->symbol) \
  761. goto err; \
  762. p->type = stype; \
  763. } while (0)
  764. /*
  765. * Read the pmu events list from sysfs
  766. * Save it into perf_pmu_events_list
  767. */
  768. static void perf_pmu__parse_init(void)
  769. {
  770. struct perf_pmu *pmu = NULL;
  771. struct perf_pmu_alias *alias;
  772. int len = 0;
  773. pmu = perf_pmu__find("cpu");
  774. if ((pmu == NULL) || list_empty(&pmu->aliases)) {
  775. perf_pmu_events_list_num = -1;
  776. return;
  777. }
  778. list_for_each_entry(alias, &pmu->aliases, list) {
  779. if (strchr(alias->name, '-'))
  780. len++;
  781. len++;
  782. }
  783. perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
  784. if (!perf_pmu_events_list)
  785. return;
  786. perf_pmu_events_list_num = len;
  787. len = 0;
  788. list_for_each_entry(alias, &pmu->aliases, list) {
  789. struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
  790. char *tmp = strchr(alias->name, '-');
  791. if (tmp != NULL) {
  792. SET_SYMBOL(strndup(alias->name, tmp - alias->name),
  793. PMU_EVENT_SYMBOL_PREFIX);
  794. p++;
  795. SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
  796. len += 2;
  797. } else {
  798. SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
  799. len++;
  800. }
  801. }
  802. qsort(perf_pmu_events_list, len,
  803. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  804. return;
  805. err:
  806. perf_pmu__parse_cleanup();
  807. }
  808. enum perf_pmu_event_symbol_type
  809. perf_pmu__parse_check(const char *name)
  810. {
  811. struct perf_pmu_event_symbol p, *r;
  812. /* scan kernel pmu events from sysfs if needed */
  813. if (perf_pmu_events_list_num == 0)
  814. perf_pmu__parse_init();
  815. /*
  816. * name "cpu" could be prefix of cpu-cycles or cpu// events.
  817. * cpu-cycles has been handled by hardcode.
  818. * So it must be cpu// events, not kernel pmu event.
  819. */
  820. if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
  821. return PMU_EVENT_SYMBOL_ERR;
  822. p.symbol = strdup(name);
  823. r = bsearch(&p, perf_pmu_events_list,
  824. (size_t) perf_pmu_events_list_num,
  825. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  826. free(p.symbol);
  827. return r ? r->type : PMU_EVENT_SYMBOL_ERR;
  828. }
  829. static int parse_events__scanner(const char *str, void *data, int start_token)
  830. {
  831. YY_BUFFER_STATE buffer;
  832. void *scanner;
  833. int ret;
  834. ret = parse_events_lex_init_extra(start_token, &scanner);
  835. if (ret)
  836. return ret;
  837. buffer = parse_events__scan_string(str, scanner);
  838. #ifdef PARSER_DEBUG
  839. parse_events_debug = 1;
  840. #endif
  841. ret = parse_events_parse(data, scanner);
  842. parse_events__flush_buffer(buffer, scanner);
  843. parse_events__delete_buffer(buffer, scanner);
  844. parse_events_lex_destroy(scanner);
  845. return ret;
  846. }
  847. /*
  848. * parse event config string, return a list of event terms.
  849. */
  850. int parse_events_terms(struct list_head *terms, const char *str)
  851. {
  852. struct parse_events_terms data = {
  853. .terms = NULL,
  854. };
  855. int ret;
  856. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  857. if (!ret) {
  858. list_splice(data.terms, terms);
  859. zfree(&data.terms);
  860. return 0;
  861. }
  862. if (data.terms)
  863. parse_events__free_terms(data.terms);
  864. return ret;
  865. }
  866. int parse_events(struct perf_evlist *evlist, const char *str)
  867. {
  868. struct parse_events_evlist data = {
  869. .list = LIST_HEAD_INIT(data.list),
  870. .idx = evlist->nr_entries,
  871. };
  872. int ret;
  873. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  874. perf_pmu__parse_cleanup();
  875. if (!ret) {
  876. int entries = data.idx - evlist->nr_entries;
  877. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  878. evlist->nr_groups += data.nr_groups;
  879. return 0;
  880. }
  881. /*
  882. * There are 2 users - builtin-record and builtin-test objects.
  883. * Both call perf_evlist__delete in case of error, so we dont
  884. * need to bother.
  885. */
  886. return ret;
  887. }
  888. int parse_events_option(const struct option *opt, const char *str,
  889. int unset __maybe_unused)
  890. {
  891. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  892. int ret = parse_events(evlist, str);
  893. if (ret) {
  894. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  895. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  896. }
  897. return ret;
  898. }
  899. int parse_filter(const struct option *opt, const char *str,
  900. int unset __maybe_unused)
  901. {
  902. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  903. struct perf_evsel *last = NULL;
  904. if (evlist->nr_entries > 0)
  905. last = perf_evlist__last(evlist);
  906. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  907. fprintf(stderr,
  908. "--filter option should follow a -e tracepoint option\n");
  909. return -1;
  910. }
  911. last->filter = strdup(str);
  912. if (last->filter == NULL) {
  913. fprintf(stderr, "not enough memory to hold filter string\n");
  914. return -1;
  915. }
  916. return 0;
  917. }
  918. static const char * const event_type_descriptors[] = {
  919. "Hardware event",
  920. "Software event",
  921. "Tracepoint event",
  922. "Hardware cache event",
  923. "Raw hardware event descriptor",
  924. "Hardware breakpoint",
  925. };
  926. /*
  927. * Print the events from <debugfs_mount_point>/tracing/events
  928. */
  929. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  930. bool name_only)
  931. {
  932. DIR *sys_dir, *evt_dir;
  933. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  934. char evt_path[MAXPATHLEN];
  935. char dir_path[MAXPATHLEN];
  936. char sbuf[STRERR_BUFSIZE];
  937. if (debugfs_valid_mountpoint(tracing_events_path)) {
  938. printf(" [ Tracepoints not available: %s ]\n",
  939. strerror_r(errno, sbuf, sizeof(sbuf)));
  940. return;
  941. }
  942. sys_dir = opendir(tracing_events_path);
  943. if (!sys_dir)
  944. return;
  945. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  946. if (subsys_glob != NULL &&
  947. !strglobmatch(sys_dirent.d_name, subsys_glob))
  948. continue;
  949. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  950. sys_dirent.d_name);
  951. evt_dir = opendir(dir_path);
  952. if (!evt_dir)
  953. continue;
  954. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  955. if (event_glob != NULL &&
  956. !strglobmatch(evt_dirent.d_name, event_glob))
  957. continue;
  958. if (name_only) {
  959. printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
  960. continue;
  961. }
  962. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  963. sys_dirent.d_name, evt_dirent.d_name);
  964. printf(" %-50s [%s]\n", evt_path,
  965. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  966. }
  967. closedir(evt_dir);
  968. }
  969. closedir(sys_dir);
  970. }
  971. /*
  972. * Check whether event is in <debugfs_mount_point>/tracing/events
  973. */
  974. int is_valid_tracepoint(const char *event_string)
  975. {
  976. DIR *sys_dir, *evt_dir;
  977. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  978. char evt_path[MAXPATHLEN];
  979. char dir_path[MAXPATHLEN];
  980. if (debugfs_valid_mountpoint(tracing_events_path))
  981. return 0;
  982. sys_dir = opendir(tracing_events_path);
  983. if (!sys_dir)
  984. return 0;
  985. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  986. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  987. sys_dirent.d_name);
  988. evt_dir = opendir(dir_path);
  989. if (!evt_dir)
  990. continue;
  991. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  992. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  993. sys_dirent.d_name, evt_dirent.d_name);
  994. if (!strcmp(evt_path, event_string)) {
  995. closedir(evt_dir);
  996. closedir(sys_dir);
  997. return 1;
  998. }
  999. }
  1000. closedir(evt_dir);
  1001. }
  1002. closedir(sys_dir);
  1003. return 0;
  1004. }
  1005. static bool is_event_supported(u8 type, unsigned config)
  1006. {
  1007. bool ret = true;
  1008. int open_return;
  1009. struct perf_evsel *evsel;
  1010. struct perf_event_attr attr = {
  1011. .type = type,
  1012. .config = config,
  1013. .disabled = 1,
  1014. };
  1015. struct {
  1016. struct thread_map map;
  1017. int threads[1];
  1018. } tmap = {
  1019. .map.nr = 1,
  1020. .threads = { 0 },
  1021. };
  1022. evsel = perf_evsel__new(&attr);
  1023. if (evsel) {
  1024. open_return = perf_evsel__open(evsel, NULL, &tmap.map);
  1025. ret = open_return >= 0;
  1026. if (open_return == -EACCES) {
  1027. /*
  1028. * This happens if the paranoid value
  1029. * /proc/sys/kernel/perf_event_paranoid is set to 2
  1030. * Re-run with exclude_kernel set; we don't do that
  1031. * by default as some ARM machines do not support it.
  1032. *
  1033. */
  1034. evsel->attr.exclude_kernel = 1;
  1035. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  1036. }
  1037. perf_evsel__delete(evsel);
  1038. }
  1039. return ret;
  1040. }
  1041. static void __print_events_type(u8 type, struct event_symbol *syms,
  1042. unsigned max)
  1043. {
  1044. char name[64];
  1045. unsigned i;
  1046. for (i = 0; i < max ; i++, syms++) {
  1047. if (!is_event_supported(type, i))
  1048. continue;
  1049. if (strlen(syms->alias))
  1050. snprintf(name, sizeof(name), "%s OR %s",
  1051. syms->symbol, syms->alias);
  1052. else
  1053. snprintf(name, sizeof(name), "%s", syms->symbol);
  1054. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  1055. }
  1056. }
  1057. void print_events_type(u8 type)
  1058. {
  1059. if (type == PERF_TYPE_SOFTWARE)
  1060. __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
  1061. else
  1062. __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
  1063. }
  1064. int print_hwcache_events(const char *event_glob, bool name_only)
  1065. {
  1066. unsigned int type, op, i, printed = 0;
  1067. char name[64];
  1068. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  1069. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  1070. /* skip invalid cache type */
  1071. if (!perf_evsel__is_cache_op_valid(type, op))
  1072. continue;
  1073. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  1074. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  1075. name, sizeof(name));
  1076. if (event_glob != NULL && !strglobmatch(name, event_glob))
  1077. continue;
  1078. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  1079. type | (op << 8) | (i << 16)))
  1080. continue;
  1081. if (name_only)
  1082. printf("%s ", name);
  1083. else
  1084. printf(" %-50s [%s]\n", name,
  1085. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1086. ++printed;
  1087. }
  1088. }
  1089. }
  1090. if (printed)
  1091. printf("\n");
  1092. return printed;
  1093. }
  1094. static void print_symbol_events(const char *event_glob, unsigned type,
  1095. struct event_symbol *syms, unsigned max,
  1096. bool name_only)
  1097. {
  1098. unsigned i, printed = 0;
  1099. char name[MAX_NAME_LEN];
  1100. for (i = 0; i < max; i++, syms++) {
  1101. if (event_glob != NULL &&
  1102. !(strglobmatch(syms->symbol, event_glob) ||
  1103. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1104. continue;
  1105. if (!is_event_supported(type, i))
  1106. continue;
  1107. if (name_only) {
  1108. printf("%s ", syms->symbol);
  1109. continue;
  1110. }
  1111. if (strlen(syms->alias))
  1112. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1113. else
  1114. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1115. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  1116. printed++;
  1117. }
  1118. if (printed)
  1119. printf("\n");
  1120. }
  1121. /*
  1122. * Print the help text for the event symbols:
  1123. */
  1124. void print_events(const char *event_glob, bool name_only)
  1125. {
  1126. if (!name_only) {
  1127. printf("\n");
  1128. printf("List of pre-defined events (to be used in -e):\n");
  1129. }
  1130. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1131. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1132. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1133. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1134. print_hwcache_events(event_glob, name_only);
  1135. print_pmu_events(event_glob, name_only);
  1136. if (event_glob != NULL)
  1137. return;
  1138. if (!name_only) {
  1139. printf(" %-50s [%s]\n",
  1140. "rNNN",
  1141. event_type_descriptors[PERF_TYPE_RAW]);
  1142. printf(" %-50s [%s]\n",
  1143. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1144. event_type_descriptors[PERF_TYPE_RAW]);
  1145. printf(" (see 'man perf-list' on how to encode it)\n");
  1146. printf("\n");
  1147. printf(" %-50s [%s]\n",
  1148. "mem:<addr>[:access]",
  1149. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1150. printf("\n");
  1151. }
  1152. print_tracepoint_events(NULL, NULL, name_only);
  1153. }
  1154. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1155. {
  1156. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1157. }
  1158. static int new_term(struct parse_events_term **_term, int type_val,
  1159. int type_term, char *config,
  1160. char *str, u64 num)
  1161. {
  1162. struct parse_events_term *term;
  1163. term = zalloc(sizeof(*term));
  1164. if (!term)
  1165. return -ENOMEM;
  1166. INIT_LIST_HEAD(&term->list);
  1167. term->type_val = type_val;
  1168. term->type_term = type_term;
  1169. term->config = config;
  1170. switch (type_val) {
  1171. case PARSE_EVENTS__TERM_TYPE_NUM:
  1172. term->val.num = num;
  1173. break;
  1174. case PARSE_EVENTS__TERM_TYPE_STR:
  1175. term->val.str = str;
  1176. break;
  1177. default:
  1178. free(term);
  1179. return -EINVAL;
  1180. }
  1181. *_term = term;
  1182. return 0;
  1183. }
  1184. int parse_events_term__num(struct parse_events_term **term,
  1185. int type_term, char *config, u64 num)
  1186. {
  1187. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1188. config, NULL, num);
  1189. }
  1190. int parse_events_term__str(struct parse_events_term **term,
  1191. int type_term, char *config, char *str)
  1192. {
  1193. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1194. config, str, 0);
  1195. }
  1196. int parse_events_term__sym_hw(struct parse_events_term **term,
  1197. char *config, unsigned idx)
  1198. {
  1199. struct event_symbol *sym;
  1200. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1201. sym = &event_symbols_hw[idx];
  1202. if (config)
  1203. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1204. PARSE_EVENTS__TERM_TYPE_USER, config,
  1205. (char *) sym->symbol, 0);
  1206. else
  1207. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1208. PARSE_EVENTS__TERM_TYPE_USER,
  1209. (char *) "event", (char *) sym->symbol, 0);
  1210. }
  1211. int parse_events_term__clone(struct parse_events_term **new,
  1212. struct parse_events_term *term)
  1213. {
  1214. return new_term(new, term->type_val, term->type_term, term->config,
  1215. term->val.str, term->val.num);
  1216. }
  1217. void parse_events__free_terms(struct list_head *terms)
  1218. {
  1219. struct parse_events_term *term, *h;
  1220. list_for_each_entry_safe(term, h, terms, list)
  1221. free(term);
  1222. }