parse-events.c 37 KB

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