parse-events.c 34 KB

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