parse-events.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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. struct list_head *config_terms)
  246. {
  247. struct perf_evsel *evsel;
  248. event_attr_init(attr);
  249. evsel = perf_evsel__new_idx(attr, (*idx)++);
  250. if (!evsel)
  251. return NULL;
  252. evsel->cpus = cpu_map__get(cpus);
  253. evsel->own_cpus = cpu_map__get(cpus);
  254. if (name)
  255. evsel->name = strdup(name);
  256. if (config_terms)
  257. list_splice(config_terms, &evsel->config_terms);
  258. list_add_tail(&evsel->node, list);
  259. return evsel;
  260. }
  261. static int add_event(struct list_head *list, int *idx,
  262. struct perf_event_attr *attr, char *name,
  263. struct list_head *config_terms)
  264. {
  265. return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
  266. }
  267. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  268. {
  269. int i, j;
  270. int n, longest = -1;
  271. for (i = 0; i < size; i++) {
  272. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  273. n = strlen(names[i][j]);
  274. if (n > longest && !strncasecmp(str, names[i][j], n))
  275. longest = n;
  276. }
  277. if (longest > 0)
  278. return i;
  279. }
  280. return -1;
  281. }
  282. int parse_events_add_cache(struct list_head *list, int *idx,
  283. char *type, char *op_result1, char *op_result2)
  284. {
  285. struct perf_event_attr attr;
  286. char name[MAX_NAME_LEN];
  287. int cache_type = -1, cache_op = -1, cache_result = -1;
  288. char *op_result[2] = { op_result1, op_result2 };
  289. int i, n;
  290. /*
  291. * No fallback - if we cannot get a clear cache type
  292. * then bail out:
  293. */
  294. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  295. PERF_COUNT_HW_CACHE_MAX);
  296. if (cache_type == -1)
  297. return -EINVAL;
  298. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  299. for (i = 0; (i < 2) && (op_result[i]); i++) {
  300. char *str = op_result[i];
  301. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  302. if (cache_op == -1) {
  303. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  304. PERF_COUNT_HW_CACHE_OP_MAX);
  305. if (cache_op >= 0) {
  306. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  307. return -EINVAL;
  308. continue;
  309. }
  310. }
  311. if (cache_result == -1) {
  312. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  313. PERF_COUNT_HW_CACHE_RESULT_MAX);
  314. if (cache_result >= 0)
  315. continue;
  316. }
  317. }
  318. /*
  319. * Fall back to reads:
  320. */
  321. if (cache_op == -1)
  322. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  323. /*
  324. * Fall back to accesses:
  325. */
  326. if (cache_result == -1)
  327. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  328. memset(&attr, 0, sizeof(attr));
  329. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  330. attr.type = PERF_TYPE_HW_CACHE;
  331. return add_event(list, idx, &attr, name, NULL);
  332. }
  333. static int add_tracepoint(struct list_head *list, int *idx,
  334. char *sys_name, char *evt_name)
  335. {
  336. struct perf_evsel *evsel;
  337. evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
  338. if (!evsel)
  339. return -ENOMEM;
  340. list_add_tail(&evsel->node, list);
  341. return 0;
  342. }
  343. static int add_tracepoint_multi_event(struct list_head *list, int *idx,
  344. char *sys_name, char *evt_name)
  345. {
  346. char evt_path[MAXPATHLEN];
  347. struct dirent *evt_ent;
  348. DIR *evt_dir;
  349. int ret = 0;
  350. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  351. evt_dir = opendir(evt_path);
  352. if (!evt_dir) {
  353. perror("Can't open event dir");
  354. return -1;
  355. }
  356. while (!ret && (evt_ent = readdir(evt_dir))) {
  357. if (!strcmp(evt_ent->d_name, ".")
  358. || !strcmp(evt_ent->d_name, "..")
  359. || !strcmp(evt_ent->d_name, "enable")
  360. || !strcmp(evt_ent->d_name, "filter"))
  361. continue;
  362. if (!strglobmatch(evt_ent->d_name, evt_name))
  363. continue;
  364. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  365. }
  366. closedir(evt_dir);
  367. return ret;
  368. }
  369. static int add_tracepoint_event(struct list_head *list, int *idx,
  370. char *sys_name, char *evt_name)
  371. {
  372. return strpbrk(evt_name, "*?") ?
  373. add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
  374. add_tracepoint(list, idx, sys_name, evt_name);
  375. }
  376. static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
  377. char *sys_name, char *evt_name)
  378. {
  379. struct dirent *events_ent;
  380. DIR *events_dir;
  381. int ret = 0;
  382. events_dir = opendir(tracing_events_path);
  383. if (!events_dir) {
  384. perror("Can't open event dir");
  385. return -1;
  386. }
  387. while (!ret && (events_ent = readdir(events_dir))) {
  388. if (!strcmp(events_ent->d_name, ".")
  389. || !strcmp(events_ent->d_name, "..")
  390. || !strcmp(events_ent->d_name, "enable")
  391. || !strcmp(events_ent->d_name, "header_event")
  392. || !strcmp(events_ent->d_name, "header_page"))
  393. continue;
  394. if (!strglobmatch(events_ent->d_name, sys_name))
  395. continue;
  396. ret = add_tracepoint_event(list, idx, events_ent->d_name,
  397. evt_name);
  398. }
  399. closedir(events_dir);
  400. return ret;
  401. }
  402. int parse_events_add_tracepoint(struct list_head *list, int *idx,
  403. char *sys, char *event)
  404. {
  405. if (strpbrk(sys, "*?"))
  406. return add_tracepoint_multi_sys(list, idx, sys, event);
  407. else
  408. return add_tracepoint_event(list, idx, sys, event);
  409. }
  410. static int
  411. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  412. {
  413. int i;
  414. for (i = 0; i < 3; i++) {
  415. if (!type || !type[i])
  416. break;
  417. #define CHECK_SET_TYPE(bit) \
  418. do { \
  419. if (attr->bp_type & bit) \
  420. return -EINVAL; \
  421. else \
  422. attr->bp_type |= bit; \
  423. } while (0)
  424. switch (type[i]) {
  425. case 'r':
  426. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  427. break;
  428. case 'w':
  429. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  430. break;
  431. case 'x':
  432. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. }
  438. #undef CHECK_SET_TYPE
  439. if (!attr->bp_type) /* Default */
  440. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  441. return 0;
  442. }
  443. int parse_events_add_breakpoint(struct list_head *list, int *idx,
  444. void *ptr, char *type, u64 len)
  445. {
  446. struct perf_event_attr attr;
  447. memset(&attr, 0, sizeof(attr));
  448. attr.bp_addr = (unsigned long) ptr;
  449. if (parse_breakpoint_type(type, &attr))
  450. return -EINVAL;
  451. /* Provide some defaults if len is not specified */
  452. if (!len) {
  453. if (attr.bp_type == HW_BREAKPOINT_X)
  454. len = sizeof(long);
  455. else
  456. len = HW_BREAKPOINT_LEN_4;
  457. }
  458. attr.bp_len = len;
  459. attr.type = PERF_TYPE_BREAKPOINT;
  460. attr.sample_period = 1;
  461. return add_event(list, idx, &attr, NULL, NULL);
  462. }
  463. static int check_type_val(struct parse_events_term *term,
  464. struct parse_events_error *err,
  465. int type)
  466. {
  467. if (type == term->type_val)
  468. return 0;
  469. if (err) {
  470. err->idx = term->err_val;
  471. if (type == PARSE_EVENTS__TERM_TYPE_NUM)
  472. err->str = strdup("expected numeric value");
  473. else
  474. err->str = strdup("expected string value");
  475. }
  476. return -EINVAL;
  477. }
  478. static int config_term(struct perf_event_attr *attr,
  479. struct parse_events_term *term,
  480. struct parse_events_error *err)
  481. {
  482. #define CHECK_TYPE_VAL(type) \
  483. do { \
  484. if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
  485. return -EINVAL; \
  486. } while (0)
  487. switch (term->type_term) {
  488. case PARSE_EVENTS__TERM_TYPE_USER:
  489. /*
  490. * Always succeed for sysfs terms, as we dont know
  491. * at this point what type they need to have.
  492. */
  493. return 0;
  494. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  495. CHECK_TYPE_VAL(NUM);
  496. attr->config = term->val.num;
  497. break;
  498. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  499. CHECK_TYPE_VAL(NUM);
  500. attr->config1 = term->val.num;
  501. break;
  502. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  503. CHECK_TYPE_VAL(NUM);
  504. attr->config2 = term->val.num;
  505. break;
  506. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  507. CHECK_TYPE_VAL(NUM);
  508. break;
  509. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  510. CHECK_TYPE_VAL(NUM);
  511. break;
  512. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  513. /*
  514. * TODO uncomment when the field is available
  515. * attr->branch_sample_type = term->val.num;
  516. */
  517. break;
  518. case PARSE_EVENTS__TERM_TYPE_TIME:
  519. CHECK_TYPE_VAL(NUM);
  520. if (term->val.num > 1) {
  521. err->str = strdup("expected 0 or 1");
  522. err->idx = term->err_val;
  523. return -EINVAL;
  524. }
  525. break;
  526. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  527. CHECK_TYPE_VAL(STR);
  528. break;
  529. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  530. CHECK_TYPE_VAL(NUM);
  531. break;
  532. case PARSE_EVENTS__TERM_TYPE_NAME:
  533. CHECK_TYPE_VAL(STR);
  534. break;
  535. default:
  536. return -EINVAL;
  537. }
  538. return 0;
  539. #undef CHECK_TYPE_VAL
  540. }
  541. static int config_attr(struct perf_event_attr *attr,
  542. struct list_head *head,
  543. struct parse_events_error *err)
  544. {
  545. struct parse_events_term *term;
  546. list_for_each_entry(term, head, list)
  547. if (config_term(attr, term, err))
  548. return -EINVAL;
  549. return 0;
  550. }
  551. static int get_config_terms(struct list_head *head_config,
  552. struct list_head *head_terms __maybe_unused)
  553. {
  554. #define ADD_CONFIG_TERM(__type, __name, __val) \
  555. do { \
  556. struct perf_evsel_config_term *__t; \
  557. \
  558. __t = zalloc(sizeof(*__t)); \
  559. if (!__t) \
  560. return -ENOMEM; \
  561. \
  562. INIT_LIST_HEAD(&__t->list); \
  563. __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
  564. __t->val.__name = __val; \
  565. list_add_tail(&__t->list, head_terms); \
  566. } while (0)
  567. struct parse_events_term *term;
  568. list_for_each_entry(term, head_config, list) {
  569. switch (term->type_term) {
  570. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  571. ADD_CONFIG_TERM(PERIOD, period, term->val.num);
  572. break;
  573. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  574. ADD_CONFIG_TERM(FREQ, freq, term->val.num);
  575. break;
  576. case PARSE_EVENTS__TERM_TYPE_TIME:
  577. ADD_CONFIG_TERM(TIME, time, term->val.num);
  578. break;
  579. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  580. ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
  581. break;
  582. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  583. ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. #undef ADD_EVSEL_CONFIG
  590. return 0;
  591. }
  592. int parse_events_add_numeric(struct parse_events_evlist *data,
  593. struct list_head *list,
  594. u32 type, u64 config,
  595. struct list_head *head_config)
  596. {
  597. struct perf_event_attr attr;
  598. LIST_HEAD(config_terms);
  599. memset(&attr, 0, sizeof(attr));
  600. attr.type = type;
  601. attr.config = config;
  602. if (head_config) {
  603. if (config_attr(&attr, head_config, data->error))
  604. return -EINVAL;
  605. if (get_config_terms(head_config, &config_terms))
  606. return -ENOMEM;
  607. }
  608. return add_event(list, &data->idx, &attr, NULL, &config_terms);
  609. }
  610. static int parse_events__is_name_term(struct parse_events_term *term)
  611. {
  612. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  613. }
  614. static char *pmu_event_name(struct list_head *head_terms)
  615. {
  616. struct parse_events_term *term;
  617. list_for_each_entry(term, head_terms, list)
  618. if (parse_events__is_name_term(term))
  619. return term->val.str;
  620. return NULL;
  621. }
  622. int parse_events_add_pmu(struct parse_events_evlist *data,
  623. struct list_head *list, char *name,
  624. struct list_head *head_config)
  625. {
  626. struct perf_event_attr attr;
  627. struct perf_pmu_info info;
  628. struct perf_pmu *pmu;
  629. struct perf_evsel *evsel;
  630. LIST_HEAD(config_terms);
  631. pmu = perf_pmu__find(name);
  632. if (!pmu)
  633. return -EINVAL;
  634. if (pmu->default_config) {
  635. memcpy(&attr, pmu->default_config,
  636. sizeof(struct perf_event_attr));
  637. } else {
  638. memset(&attr, 0, sizeof(attr));
  639. }
  640. if (!head_config) {
  641. attr.type = pmu->type;
  642. evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
  643. return evsel ? 0 : -ENOMEM;
  644. }
  645. if (perf_pmu__check_alias(pmu, head_config, &info))
  646. return -EINVAL;
  647. /*
  648. * Configure hardcoded terms first, no need to check
  649. * return value when called with fail == 0 ;)
  650. */
  651. if (config_attr(&attr, head_config, data->error))
  652. return -EINVAL;
  653. if (get_config_terms(head_config, &config_terms))
  654. return -ENOMEM;
  655. if (perf_pmu__config(pmu, &attr, head_config, data->error))
  656. return -EINVAL;
  657. evsel = __add_event(list, &data->idx, &attr,
  658. pmu_event_name(head_config), pmu->cpus,
  659. &config_terms);
  660. if (evsel) {
  661. evsel->unit = info.unit;
  662. evsel->scale = info.scale;
  663. evsel->per_pkg = info.per_pkg;
  664. evsel->snapshot = info.snapshot;
  665. }
  666. return evsel ? 0 : -ENOMEM;
  667. }
  668. int parse_events__modifier_group(struct list_head *list,
  669. char *event_mod)
  670. {
  671. return parse_events__modifier_event(list, event_mod, true);
  672. }
  673. void parse_events__set_leader(char *name, struct list_head *list)
  674. {
  675. struct perf_evsel *leader;
  676. __perf_evlist__set_leader(list);
  677. leader = list_entry(list->next, struct perf_evsel, node);
  678. leader->group_name = name ? strdup(name) : NULL;
  679. }
  680. /* list_event is assumed to point to malloc'ed memory */
  681. void parse_events_update_lists(struct list_head *list_event,
  682. struct list_head *list_all)
  683. {
  684. /*
  685. * Called for single event definition. Update the
  686. * 'all event' list, and reinit the 'single event'
  687. * list, for next event definition.
  688. */
  689. list_splice_tail(list_event, list_all);
  690. free(list_event);
  691. }
  692. struct event_modifier {
  693. int eu;
  694. int ek;
  695. int eh;
  696. int eH;
  697. int eG;
  698. int eI;
  699. int precise;
  700. int exclude_GH;
  701. int sample_read;
  702. int pinned;
  703. };
  704. static int get_event_modifier(struct event_modifier *mod, char *str,
  705. struct perf_evsel *evsel)
  706. {
  707. int eu = evsel ? evsel->attr.exclude_user : 0;
  708. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  709. int eh = evsel ? evsel->attr.exclude_hv : 0;
  710. int eH = evsel ? evsel->attr.exclude_host : 0;
  711. int eG = evsel ? evsel->attr.exclude_guest : 0;
  712. int eI = evsel ? evsel->attr.exclude_idle : 0;
  713. int precise = evsel ? evsel->attr.precise_ip : 0;
  714. int sample_read = 0;
  715. int pinned = evsel ? evsel->attr.pinned : 0;
  716. int exclude = eu | ek | eh;
  717. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  718. memset(mod, 0, sizeof(*mod));
  719. while (*str) {
  720. if (*str == 'u') {
  721. if (!exclude)
  722. exclude = eu = ek = eh = 1;
  723. eu = 0;
  724. } else if (*str == 'k') {
  725. if (!exclude)
  726. exclude = eu = ek = eh = 1;
  727. ek = 0;
  728. } else if (*str == 'h') {
  729. if (!exclude)
  730. exclude = eu = ek = eh = 1;
  731. eh = 0;
  732. } else if (*str == 'G') {
  733. if (!exclude_GH)
  734. exclude_GH = eG = eH = 1;
  735. eG = 0;
  736. } else if (*str == 'H') {
  737. if (!exclude_GH)
  738. exclude_GH = eG = eH = 1;
  739. eH = 0;
  740. } else if (*str == 'I') {
  741. eI = 1;
  742. } else if (*str == 'p') {
  743. precise++;
  744. /* use of precise requires exclude_guest */
  745. if (!exclude_GH)
  746. eG = 1;
  747. } else if (*str == 'S') {
  748. sample_read = 1;
  749. } else if (*str == 'D') {
  750. pinned = 1;
  751. } else
  752. break;
  753. ++str;
  754. }
  755. /*
  756. * precise ip:
  757. *
  758. * 0 - SAMPLE_IP can have arbitrary skid
  759. * 1 - SAMPLE_IP must have constant skid
  760. * 2 - SAMPLE_IP requested to have 0 skid
  761. * 3 - SAMPLE_IP must have 0 skid
  762. *
  763. * See also PERF_RECORD_MISC_EXACT_IP
  764. */
  765. if (precise > 3)
  766. return -EINVAL;
  767. mod->eu = eu;
  768. mod->ek = ek;
  769. mod->eh = eh;
  770. mod->eH = eH;
  771. mod->eG = eG;
  772. mod->eI = eI;
  773. mod->precise = precise;
  774. mod->exclude_GH = exclude_GH;
  775. mod->sample_read = sample_read;
  776. mod->pinned = pinned;
  777. return 0;
  778. }
  779. /*
  780. * Basic modifier sanity check to validate it contains only one
  781. * instance of any modifier (apart from 'p') present.
  782. */
  783. static int check_modifier(char *str)
  784. {
  785. char *p = str;
  786. /* The sizeof includes 0 byte as well. */
  787. if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
  788. return -1;
  789. while (*p) {
  790. if (*p != 'p' && strchr(p + 1, *p))
  791. return -1;
  792. p++;
  793. }
  794. return 0;
  795. }
  796. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  797. {
  798. struct perf_evsel *evsel;
  799. struct event_modifier mod;
  800. if (str == NULL)
  801. return 0;
  802. if (check_modifier(str))
  803. return -EINVAL;
  804. if (!add && get_event_modifier(&mod, str, NULL))
  805. return -EINVAL;
  806. __evlist__for_each(list, evsel) {
  807. if (add && get_event_modifier(&mod, str, evsel))
  808. return -EINVAL;
  809. evsel->attr.exclude_user = mod.eu;
  810. evsel->attr.exclude_kernel = mod.ek;
  811. evsel->attr.exclude_hv = mod.eh;
  812. evsel->attr.precise_ip = mod.precise;
  813. evsel->attr.exclude_host = mod.eH;
  814. evsel->attr.exclude_guest = mod.eG;
  815. evsel->attr.exclude_idle = mod.eI;
  816. evsel->exclude_GH = mod.exclude_GH;
  817. evsel->sample_read = mod.sample_read;
  818. if (perf_evsel__is_group_leader(evsel))
  819. evsel->attr.pinned = mod.pinned;
  820. }
  821. return 0;
  822. }
  823. int parse_events_name(struct list_head *list, char *name)
  824. {
  825. struct perf_evsel *evsel;
  826. __evlist__for_each(list, evsel) {
  827. if (!evsel->name)
  828. evsel->name = strdup(name);
  829. }
  830. return 0;
  831. }
  832. static int
  833. comp_pmu(const void *p1, const void *p2)
  834. {
  835. struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
  836. struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
  837. return strcmp(pmu1->symbol, pmu2->symbol);
  838. }
  839. static void perf_pmu__parse_cleanup(void)
  840. {
  841. if (perf_pmu_events_list_num > 0) {
  842. struct perf_pmu_event_symbol *p;
  843. int i;
  844. for (i = 0; i < perf_pmu_events_list_num; i++) {
  845. p = perf_pmu_events_list + i;
  846. free(p->symbol);
  847. }
  848. free(perf_pmu_events_list);
  849. perf_pmu_events_list = NULL;
  850. perf_pmu_events_list_num = 0;
  851. }
  852. }
  853. #define SET_SYMBOL(str, stype) \
  854. do { \
  855. p->symbol = str; \
  856. if (!p->symbol) \
  857. goto err; \
  858. p->type = stype; \
  859. } while (0)
  860. /*
  861. * Read the pmu events list from sysfs
  862. * Save it into perf_pmu_events_list
  863. */
  864. static void perf_pmu__parse_init(void)
  865. {
  866. struct perf_pmu *pmu = NULL;
  867. struct perf_pmu_alias *alias;
  868. int len = 0;
  869. pmu = perf_pmu__find("cpu");
  870. if ((pmu == NULL) || list_empty(&pmu->aliases)) {
  871. perf_pmu_events_list_num = -1;
  872. return;
  873. }
  874. list_for_each_entry(alias, &pmu->aliases, list) {
  875. if (strchr(alias->name, '-'))
  876. len++;
  877. len++;
  878. }
  879. perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
  880. if (!perf_pmu_events_list)
  881. return;
  882. perf_pmu_events_list_num = len;
  883. len = 0;
  884. list_for_each_entry(alias, &pmu->aliases, list) {
  885. struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
  886. char *tmp = strchr(alias->name, '-');
  887. if (tmp != NULL) {
  888. SET_SYMBOL(strndup(alias->name, tmp - alias->name),
  889. PMU_EVENT_SYMBOL_PREFIX);
  890. p++;
  891. SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
  892. len += 2;
  893. } else {
  894. SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
  895. len++;
  896. }
  897. }
  898. qsort(perf_pmu_events_list, len,
  899. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  900. return;
  901. err:
  902. perf_pmu__parse_cleanup();
  903. }
  904. enum perf_pmu_event_symbol_type
  905. perf_pmu__parse_check(const char *name)
  906. {
  907. struct perf_pmu_event_symbol p, *r;
  908. /* scan kernel pmu events from sysfs if needed */
  909. if (perf_pmu_events_list_num == 0)
  910. perf_pmu__parse_init();
  911. /*
  912. * name "cpu" could be prefix of cpu-cycles or cpu// events.
  913. * cpu-cycles has been handled by hardcode.
  914. * So it must be cpu// events, not kernel pmu event.
  915. */
  916. if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
  917. return PMU_EVENT_SYMBOL_ERR;
  918. p.symbol = strdup(name);
  919. r = bsearch(&p, perf_pmu_events_list,
  920. (size_t) perf_pmu_events_list_num,
  921. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  922. free(p.symbol);
  923. return r ? r->type : PMU_EVENT_SYMBOL_ERR;
  924. }
  925. static int parse_events__scanner(const char *str, void *data, int start_token)
  926. {
  927. YY_BUFFER_STATE buffer;
  928. void *scanner;
  929. int ret;
  930. ret = parse_events_lex_init_extra(start_token, &scanner);
  931. if (ret)
  932. return ret;
  933. buffer = parse_events__scan_string(str, scanner);
  934. #ifdef PARSER_DEBUG
  935. parse_events_debug = 1;
  936. #endif
  937. ret = parse_events_parse(data, scanner);
  938. parse_events__flush_buffer(buffer, scanner);
  939. parse_events__delete_buffer(buffer, scanner);
  940. parse_events_lex_destroy(scanner);
  941. return ret;
  942. }
  943. /*
  944. * parse event config string, return a list of event terms.
  945. */
  946. int parse_events_terms(struct list_head *terms, const char *str)
  947. {
  948. struct parse_events_terms data = {
  949. .terms = NULL,
  950. };
  951. int ret;
  952. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  953. if (!ret) {
  954. list_splice(data.terms, terms);
  955. zfree(&data.terms);
  956. return 0;
  957. }
  958. if (data.terms)
  959. parse_events__free_terms(data.terms);
  960. return ret;
  961. }
  962. int parse_events(struct perf_evlist *evlist, const char *str,
  963. struct parse_events_error *err)
  964. {
  965. struct parse_events_evlist data = {
  966. .list = LIST_HEAD_INIT(data.list),
  967. .idx = evlist->nr_entries,
  968. .error = err,
  969. };
  970. int ret;
  971. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  972. perf_pmu__parse_cleanup();
  973. if (!ret) {
  974. struct perf_evsel *last;
  975. perf_evlist__splice_list_tail(evlist, &data.list);
  976. evlist->nr_groups += data.nr_groups;
  977. last = perf_evlist__last(evlist);
  978. last->cmdline_group_boundary = true;
  979. return 0;
  980. }
  981. /*
  982. * There are 2 users - builtin-record and builtin-test objects.
  983. * Both call perf_evlist__delete in case of error, so we dont
  984. * need to bother.
  985. */
  986. return ret;
  987. }
  988. #define MAX_WIDTH 1000
  989. static int get_term_width(void)
  990. {
  991. struct winsize ws;
  992. get_term_dimensions(&ws);
  993. return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
  994. }
  995. static void parse_events_print_error(struct parse_events_error *err,
  996. const char *event)
  997. {
  998. const char *str = "invalid or unsupported event: ";
  999. char _buf[MAX_WIDTH];
  1000. char *buf = (char *) event;
  1001. int idx = 0;
  1002. if (err->str) {
  1003. /* -2 for extra '' in the final fprintf */
  1004. int width = get_term_width() - 2;
  1005. int len_event = strlen(event);
  1006. int len_str, max_len, cut = 0;
  1007. /*
  1008. * Maximum error index indent, we will cut
  1009. * the event string if it's bigger.
  1010. */
  1011. int max_err_idx = 13;
  1012. /*
  1013. * Let's be specific with the message when
  1014. * we have the precise error.
  1015. */
  1016. str = "event syntax error: ";
  1017. len_str = strlen(str);
  1018. max_len = width - len_str;
  1019. buf = _buf;
  1020. /* We're cutting from the beggining. */
  1021. if (err->idx > max_err_idx)
  1022. cut = err->idx - max_err_idx;
  1023. strncpy(buf, event + cut, max_len);
  1024. /* Mark cut parts with '..' on both sides. */
  1025. if (cut)
  1026. buf[0] = buf[1] = '.';
  1027. if ((len_event - cut) > max_len) {
  1028. buf[max_len - 1] = buf[max_len - 2] = '.';
  1029. buf[max_len] = 0;
  1030. }
  1031. idx = len_str + err->idx - cut;
  1032. }
  1033. fprintf(stderr, "%s'%s'\n", str, buf);
  1034. if (idx) {
  1035. fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
  1036. if (err->help)
  1037. fprintf(stderr, "\n%s\n", err->help);
  1038. free(err->str);
  1039. free(err->help);
  1040. }
  1041. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  1042. }
  1043. #undef MAX_WIDTH
  1044. int parse_events_option(const struct option *opt, const char *str,
  1045. int unset __maybe_unused)
  1046. {
  1047. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1048. struct parse_events_error err = { .idx = 0, };
  1049. int ret = parse_events(evlist, str, &err);
  1050. if (ret)
  1051. parse_events_print_error(&err, str);
  1052. return ret;
  1053. }
  1054. static int
  1055. foreach_evsel_in_last_glob(struct perf_evlist *evlist,
  1056. int (*func)(struct perf_evsel *evsel,
  1057. const void *arg),
  1058. const void *arg)
  1059. {
  1060. struct perf_evsel *last = NULL;
  1061. int err;
  1062. if (evlist->nr_entries > 0)
  1063. last = perf_evlist__last(evlist);
  1064. do {
  1065. err = (*func)(last, arg);
  1066. if (err)
  1067. return -1;
  1068. if (!last)
  1069. return 0;
  1070. if (last->node.prev == &evlist->entries)
  1071. return 0;
  1072. last = list_entry(last->node.prev, struct perf_evsel, node);
  1073. } while (!last->cmdline_group_boundary);
  1074. return 0;
  1075. }
  1076. static int set_filter(struct perf_evsel *evsel, const void *arg)
  1077. {
  1078. const char *str = arg;
  1079. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1080. fprintf(stderr,
  1081. "--filter option should follow a -e tracepoint option\n");
  1082. return -1;
  1083. }
  1084. if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
  1085. fprintf(stderr,
  1086. "not enough memory to hold filter string\n");
  1087. return -1;
  1088. }
  1089. return 0;
  1090. }
  1091. int parse_filter(const struct option *opt, const char *str,
  1092. int unset __maybe_unused)
  1093. {
  1094. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1095. return foreach_evsel_in_last_glob(evlist, set_filter,
  1096. (const void *)str);
  1097. }
  1098. static int add_exclude_perf_filter(struct perf_evsel *evsel,
  1099. const void *arg __maybe_unused)
  1100. {
  1101. char new_filter[64];
  1102. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1103. fprintf(stderr,
  1104. "--exclude-perf option should follow a -e tracepoint option\n");
  1105. return -1;
  1106. }
  1107. snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
  1108. if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
  1109. fprintf(stderr,
  1110. "not enough memory to hold filter string\n");
  1111. return -1;
  1112. }
  1113. return 0;
  1114. }
  1115. int exclude_perf(const struct option *opt,
  1116. const char *arg __maybe_unused,
  1117. int unset __maybe_unused)
  1118. {
  1119. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1120. return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
  1121. NULL);
  1122. }
  1123. static const char * const event_type_descriptors[] = {
  1124. "Hardware event",
  1125. "Software event",
  1126. "Tracepoint event",
  1127. "Hardware cache event",
  1128. "Raw hardware event descriptor",
  1129. "Hardware breakpoint",
  1130. };
  1131. static int cmp_string(const void *a, const void *b)
  1132. {
  1133. const char * const *as = a;
  1134. const char * const *bs = b;
  1135. return strcmp(*as, *bs);
  1136. }
  1137. /*
  1138. * Print the events from <debugfs_mount_point>/tracing/events
  1139. */
  1140. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  1141. bool name_only)
  1142. {
  1143. DIR *sys_dir, *evt_dir;
  1144. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1145. char evt_path[MAXPATHLEN];
  1146. char dir_path[MAXPATHLEN];
  1147. char **evt_list = NULL;
  1148. unsigned int evt_i = 0, evt_num = 0;
  1149. bool evt_num_known = false;
  1150. restart:
  1151. sys_dir = opendir(tracing_events_path);
  1152. if (!sys_dir)
  1153. return;
  1154. if (evt_num_known) {
  1155. evt_list = zalloc(sizeof(char *) * evt_num);
  1156. if (!evt_list)
  1157. goto out_close_sys_dir;
  1158. }
  1159. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1160. if (subsys_glob != NULL &&
  1161. !strglobmatch(sys_dirent.d_name, subsys_glob))
  1162. continue;
  1163. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1164. sys_dirent.d_name);
  1165. evt_dir = opendir(dir_path);
  1166. if (!evt_dir)
  1167. continue;
  1168. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1169. if (event_glob != NULL &&
  1170. !strglobmatch(evt_dirent.d_name, event_glob))
  1171. continue;
  1172. if (!evt_num_known) {
  1173. evt_num++;
  1174. continue;
  1175. }
  1176. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1177. sys_dirent.d_name, evt_dirent.d_name);
  1178. evt_list[evt_i] = strdup(evt_path);
  1179. if (evt_list[evt_i] == NULL)
  1180. goto out_close_evt_dir;
  1181. evt_i++;
  1182. }
  1183. closedir(evt_dir);
  1184. }
  1185. closedir(sys_dir);
  1186. if (!evt_num_known) {
  1187. evt_num_known = true;
  1188. goto restart;
  1189. }
  1190. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1191. evt_i = 0;
  1192. while (evt_i < evt_num) {
  1193. if (name_only) {
  1194. printf("%s ", evt_list[evt_i++]);
  1195. continue;
  1196. }
  1197. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1198. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1199. }
  1200. if (evt_num)
  1201. printf("\n");
  1202. out_free:
  1203. evt_num = evt_i;
  1204. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1205. zfree(&evt_list[evt_i]);
  1206. zfree(&evt_list);
  1207. return;
  1208. out_close_evt_dir:
  1209. closedir(evt_dir);
  1210. out_close_sys_dir:
  1211. closedir(sys_dir);
  1212. printf("FATAL: not enough memory to print %s\n",
  1213. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1214. if (evt_list)
  1215. goto out_free;
  1216. }
  1217. /*
  1218. * Check whether event is in <debugfs_mount_point>/tracing/events
  1219. */
  1220. int is_valid_tracepoint(const char *event_string)
  1221. {
  1222. DIR *sys_dir, *evt_dir;
  1223. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1224. char evt_path[MAXPATHLEN];
  1225. char dir_path[MAXPATHLEN];
  1226. sys_dir = opendir(tracing_events_path);
  1227. if (!sys_dir)
  1228. return 0;
  1229. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1230. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1231. sys_dirent.d_name);
  1232. evt_dir = opendir(dir_path);
  1233. if (!evt_dir)
  1234. continue;
  1235. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1236. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1237. sys_dirent.d_name, evt_dirent.d_name);
  1238. if (!strcmp(evt_path, event_string)) {
  1239. closedir(evt_dir);
  1240. closedir(sys_dir);
  1241. return 1;
  1242. }
  1243. }
  1244. closedir(evt_dir);
  1245. }
  1246. closedir(sys_dir);
  1247. return 0;
  1248. }
  1249. static bool is_event_supported(u8 type, unsigned config)
  1250. {
  1251. bool ret = true;
  1252. int open_return;
  1253. struct perf_evsel *evsel;
  1254. struct perf_event_attr attr = {
  1255. .type = type,
  1256. .config = config,
  1257. .disabled = 1,
  1258. };
  1259. struct {
  1260. struct thread_map map;
  1261. int threads[1];
  1262. } tmap = {
  1263. .map.nr = 1,
  1264. .threads = { 0 },
  1265. };
  1266. evsel = perf_evsel__new(&attr);
  1267. if (evsel) {
  1268. open_return = perf_evsel__open(evsel, NULL, &tmap.map);
  1269. ret = open_return >= 0;
  1270. if (open_return == -EACCES) {
  1271. /*
  1272. * This happens if the paranoid value
  1273. * /proc/sys/kernel/perf_event_paranoid is set to 2
  1274. * Re-run with exclude_kernel set; we don't do that
  1275. * by default as some ARM machines do not support it.
  1276. *
  1277. */
  1278. evsel->attr.exclude_kernel = 1;
  1279. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  1280. }
  1281. perf_evsel__delete(evsel);
  1282. }
  1283. return ret;
  1284. }
  1285. int print_hwcache_events(const char *event_glob, bool name_only)
  1286. {
  1287. unsigned int type, op, i, evt_i = 0, evt_num = 0;
  1288. char name[64];
  1289. char **evt_list = NULL;
  1290. bool evt_num_known = false;
  1291. restart:
  1292. if (evt_num_known) {
  1293. evt_list = zalloc(sizeof(char *) * evt_num);
  1294. if (!evt_list)
  1295. goto out_enomem;
  1296. }
  1297. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  1298. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  1299. /* skip invalid cache type */
  1300. if (!perf_evsel__is_cache_op_valid(type, op))
  1301. continue;
  1302. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  1303. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  1304. name, sizeof(name));
  1305. if (event_glob != NULL && !strglobmatch(name, event_glob))
  1306. continue;
  1307. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  1308. type | (op << 8) | (i << 16)))
  1309. continue;
  1310. if (!evt_num_known) {
  1311. evt_num++;
  1312. continue;
  1313. }
  1314. evt_list[evt_i] = strdup(name);
  1315. if (evt_list[evt_i] == NULL)
  1316. goto out_enomem;
  1317. evt_i++;
  1318. }
  1319. }
  1320. }
  1321. if (!evt_num_known) {
  1322. evt_num_known = true;
  1323. goto restart;
  1324. }
  1325. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1326. evt_i = 0;
  1327. while (evt_i < evt_num) {
  1328. if (name_only) {
  1329. printf("%s ", evt_list[evt_i++]);
  1330. continue;
  1331. }
  1332. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1333. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1334. }
  1335. if (evt_num)
  1336. printf("\n");
  1337. out_free:
  1338. evt_num = evt_i;
  1339. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1340. zfree(&evt_list[evt_i]);
  1341. zfree(&evt_list);
  1342. return evt_num;
  1343. out_enomem:
  1344. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1345. if (evt_list)
  1346. goto out_free;
  1347. return evt_num;
  1348. }
  1349. void print_symbol_events(const char *event_glob, unsigned type,
  1350. struct event_symbol *syms, unsigned max,
  1351. bool name_only)
  1352. {
  1353. unsigned int i, evt_i = 0, evt_num = 0;
  1354. char name[MAX_NAME_LEN];
  1355. char **evt_list = NULL;
  1356. bool evt_num_known = false;
  1357. restart:
  1358. if (evt_num_known) {
  1359. evt_list = zalloc(sizeof(char *) * evt_num);
  1360. if (!evt_list)
  1361. goto out_enomem;
  1362. syms -= max;
  1363. }
  1364. for (i = 0; i < max; i++, syms++) {
  1365. if (event_glob != NULL &&
  1366. !(strglobmatch(syms->symbol, event_glob) ||
  1367. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1368. continue;
  1369. if (!is_event_supported(type, i))
  1370. continue;
  1371. if (!evt_num_known) {
  1372. evt_num++;
  1373. continue;
  1374. }
  1375. if (!name_only && strlen(syms->alias))
  1376. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1377. else
  1378. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1379. evt_list[evt_i] = strdup(name);
  1380. if (evt_list[evt_i] == NULL)
  1381. goto out_enomem;
  1382. evt_i++;
  1383. }
  1384. if (!evt_num_known) {
  1385. evt_num_known = true;
  1386. goto restart;
  1387. }
  1388. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1389. evt_i = 0;
  1390. while (evt_i < evt_num) {
  1391. if (name_only) {
  1392. printf("%s ", evt_list[evt_i++]);
  1393. continue;
  1394. }
  1395. printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
  1396. }
  1397. if (evt_num)
  1398. printf("\n");
  1399. out_free:
  1400. evt_num = evt_i;
  1401. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1402. zfree(&evt_list[evt_i]);
  1403. zfree(&evt_list);
  1404. return;
  1405. out_enomem:
  1406. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
  1407. if (evt_list)
  1408. goto out_free;
  1409. }
  1410. /*
  1411. * Print the help text for the event symbols:
  1412. */
  1413. void print_events(const char *event_glob, bool name_only)
  1414. {
  1415. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1416. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1417. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1418. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1419. print_hwcache_events(event_glob, name_only);
  1420. print_pmu_events(event_glob, name_only);
  1421. if (event_glob != NULL)
  1422. return;
  1423. if (!name_only) {
  1424. printf(" %-50s [%s]\n",
  1425. "rNNN",
  1426. event_type_descriptors[PERF_TYPE_RAW]);
  1427. printf(" %-50s [%s]\n",
  1428. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1429. event_type_descriptors[PERF_TYPE_RAW]);
  1430. printf(" (see 'man perf-list' on how to encode it)\n");
  1431. printf("\n");
  1432. printf(" %-50s [%s]\n",
  1433. "mem:<addr>[/len][:access]",
  1434. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1435. printf("\n");
  1436. }
  1437. print_tracepoint_events(NULL, NULL, name_only);
  1438. }
  1439. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1440. {
  1441. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1442. }
  1443. static int new_term(struct parse_events_term **_term, int type_val,
  1444. int type_term, char *config,
  1445. char *str, u64 num, int err_term, int err_val)
  1446. {
  1447. struct parse_events_term *term;
  1448. term = zalloc(sizeof(*term));
  1449. if (!term)
  1450. return -ENOMEM;
  1451. INIT_LIST_HEAD(&term->list);
  1452. term->type_val = type_val;
  1453. term->type_term = type_term;
  1454. term->config = config;
  1455. term->err_term = err_term;
  1456. term->err_val = err_val;
  1457. switch (type_val) {
  1458. case PARSE_EVENTS__TERM_TYPE_NUM:
  1459. term->val.num = num;
  1460. break;
  1461. case PARSE_EVENTS__TERM_TYPE_STR:
  1462. term->val.str = str;
  1463. break;
  1464. default:
  1465. free(term);
  1466. return -EINVAL;
  1467. }
  1468. *_term = term;
  1469. return 0;
  1470. }
  1471. int parse_events_term__num(struct parse_events_term **term,
  1472. int type_term, char *config, u64 num,
  1473. void *loc_term_, void *loc_val_)
  1474. {
  1475. YYLTYPE *loc_term = loc_term_;
  1476. YYLTYPE *loc_val = loc_val_;
  1477. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1478. config, NULL, num,
  1479. loc_term ? loc_term->first_column : 0,
  1480. loc_val ? loc_val->first_column : 0);
  1481. }
  1482. int parse_events_term__str(struct parse_events_term **term,
  1483. int type_term, char *config, char *str,
  1484. void *loc_term_, void *loc_val_)
  1485. {
  1486. YYLTYPE *loc_term = loc_term_;
  1487. YYLTYPE *loc_val = loc_val_;
  1488. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1489. config, str, 0,
  1490. loc_term ? loc_term->first_column : 0,
  1491. loc_val ? loc_val->first_column : 0);
  1492. }
  1493. int parse_events_term__sym_hw(struct parse_events_term **term,
  1494. char *config, unsigned idx)
  1495. {
  1496. struct event_symbol *sym;
  1497. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1498. sym = &event_symbols_hw[idx];
  1499. if (config)
  1500. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1501. PARSE_EVENTS__TERM_TYPE_USER, config,
  1502. (char *) sym->symbol, 0, 0, 0);
  1503. else
  1504. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1505. PARSE_EVENTS__TERM_TYPE_USER,
  1506. (char *) "event", (char *) sym->symbol,
  1507. 0, 0, 0);
  1508. }
  1509. int parse_events_term__clone(struct parse_events_term **new,
  1510. struct parse_events_term *term)
  1511. {
  1512. return new_term(new, term->type_val, term->type_term, term->config,
  1513. term->val.str, term->val.num,
  1514. term->err_term, term->err_val);
  1515. }
  1516. void parse_events__free_terms(struct list_head *terms)
  1517. {
  1518. struct parse_events_term *term, *h;
  1519. list_for_each_entry_safe(term, h, terms, list)
  1520. free(term);
  1521. }
  1522. void parse_events_evlist_error(struct parse_events_evlist *data,
  1523. int idx, const char *str)
  1524. {
  1525. struct parse_events_error *err = data->error;
  1526. if (!err)
  1527. return;
  1528. err->idx = idx;
  1529. err->str = strdup(str);
  1530. WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
  1531. }