parse-events.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. #include "../../../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 "debugfs.h"
  14. #include "parse-events-bison.h"
  15. #define YY_EXTRA_TYPE int
  16. #include "parse-events-flex.h"
  17. #include "pmu.h"
  18. #define MAX_NAME_LEN 100
  19. struct event_symbol {
  20. u8 type;
  21. u64 config;
  22. const char *symbol;
  23. const char *alias;
  24. };
  25. #ifdef PARSER_DEBUG
  26. extern int parse_events_debug;
  27. #endif
  28. int parse_events_parse(void *data, void *scanner);
  29. #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
  30. #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
  31. static struct event_symbol event_symbols[] = {
  32. { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
  33. { CHW(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
  34. { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
  35. { CHW(INSTRUCTIONS), "instructions", "" },
  36. { CHW(CACHE_REFERENCES), "cache-references", "" },
  37. { CHW(CACHE_MISSES), "cache-misses", "" },
  38. { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
  39. { CHW(BRANCH_MISSES), "branch-misses", "" },
  40. { CHW(BUS_CYCLES), "bus-cycles", "" },
  41. { CHW(REF_CPU_CYCLES), "ref-cycles", "" },
  42. { CSW(CPU_CLOCK), "cpu-clock", "" },
  43. { CSW(TASK_CLOCK), "task-clock", "" },
  44. { CSW(PAGE_FAULTS), "page-faults", "faults" },
  45. { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
  46. { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
  47. { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
  48. { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
  49. { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
  50. { CSW(EMULATION_FAULTS), "emulation-faults", "" },
  51. };
  52. #define __PERF_EVENT_FIELD(config, name) \
  53. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  54. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  55. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  56. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  57. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  58. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  59. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  60. if (sys_dirent.d_type == DT_DIR && \
  61. (strcmp(sys_dirent.d_name, ".")) && \
  62. (strcmp(sys_dirent.d_name, "..")))
  63. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  64. {
  65. char evt_path[MAXPATHLEN];
  66. int fd;
  67. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  68. sys_dir->d_name, evt_dir->d_name);
  69. fd = open(evt_path, O_RDONLY);
  70. if (fd < 0)
  71. return -EINVAL;
  72. close(fd);
  73. return 0;
  74. }
  75. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  76. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  77. if (evt_dirent.d_type == DT_DIR && \
  78. (strcmp(evt_dirent.d_name, ".")) && \
  79. (strcmp(evt_dirent.d_name, "..")) && \
  80. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  81. #define MAX_EVENT_LENGTH 512
  82. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  83. {
  84. struct tracepoint_path *path = NULL;
  85. DIR *sys_dir, *evt_dir;
  86. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  87. char id_buf[24];
  88. int fd;
  89. u64 id;
  90. char evt_path[MAXPATHLEN];
  91. char dir_path[MAXPATHLEN];
  92. if (debugfs_valid_mountpoint(tracing_events_path))
  93. return NULL;
  94. sys_dir = opendir(tracing_events_path);
  95. if (!sys_dir)
  96. return NULL;
  97. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  98. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  99. sys_dirent.d_name);
  100. evt_dir = opendir(dir_path);
  101. if (!evt_dir)
  102. continue;
  103. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  104. snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
  105. evt_dirent.d_name);
  106. fd = open(evt_path, O_RDONLY);
  107. if (fd < 0)
  108. continue;
  109. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  110. close(fd);
  111. continue;
  112. }
  113. close(fd);
  114. id = atoll(id_buf);
  115. if (id == config) {
  116. closedir(evt_dir);
  117. closedir(sys_dir);
  118. path = zalloc(sizeof(*path));
  119. path->system = malloc(MAX_EVENT_LENGTH);
  120. if (!path->system) {
  121. free(path);
  122. return NULL;
  123. }
  124. path->name = malloc(MAX_EVENT_LENGTH);
  125. if (!path->name) {
  126. free(path->system);
  127. free(path);
  128. return NULL;
  129. }
  130. strncpy(path->system, sys_dirent.d_name,
  131. MAX_EVENT_LENGTH);
  132. strncpy(path->name, evt_dirent.d_name,
  133. MAX_EVENT_LENGTH);
  134. return path;
  135. }
  136. }
  137. closedir(evt_dir);
  138. }
  139. closedir(sys_dir);
  140. return NULL;
  141. }
  142. const char *event_type(int type)
  143. {
  144. switch (type) {
  145. case PERF_TYPE_HARDWARE:
  146. return "hardware";
  147. case PERF_TYPE_SOFTWARE:
  148. return "software";
  149. case PERF_TYPE_TRACEPOINT:
  150. return "tracepoint";
  151. case PERF_TYPE_HW_CACHE:
  152. return "hardware-cache";
  153. default:
  154. break;
  155. }
  156. return "unknown";
  157. }
  158. static int add_event(struct list_head **_list, int *idx,
  159. struct perf_event_attr *attr, char *name)
  160. {
  161. struct perf_evsel *evsel;
  162. struct list_head *list = *_list;
  163. if (!list) {
  164. list = malloc(sizeof(*list));
  165. if (!list)
  166. return -ENOMEM;
  167. INIT_LIST_HEAD(list);
  168. }
  169. event_attr_init(attr);
  170. evsel = perf_evsel__new(attr, (*idx)++);
  171. if (!evsel) {
  172. free(list);
  173. return -ENOMEM;
  174. }
  175. if (name)
  176. evsel->name = strdup(name);
  177. list_add_tail(&evsel->node, list);
  178. *_list = list;
  179. return 0;
  180. }
  181. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  182. {
  183. int i, j;
  184. int n, longest = -1;
  185. for (i = 0; i < size; i++) {
  186. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  187. n = strlen(names[i][j]);
  188. if (n > longest && !strncasecmp(str, names[i][j], n))
  189. longest = n;
  190. }
  191. if (longest > 0)
  192. return i;
  193. }
  194. return -1;
  195. }
  196. int parse_events_add_cache(struct list_head **list, int *idx,
  197. char *type, char *op_result1, char *op_result2)
  198. {
  199. struct perf_event_attr attr;
  200. char name[MAX_NAME_LEN];
  201. int cache_type = -1, cache_op = -1, cache_result = -1;
  202. char *op_result[2] = { op_result1, op_result2 };
  203. int i, n;
  204. /*
  205. * No fallback - if we cannot get a clear cache type
  206. * then bail out:
  207. */
  208. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  209. PERF_COUNT_HW_CACHE_MAX);
  210. if (cache_type == -1)
  211. return -EINVAL;
  212. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  213. for (i = 0; (i < 2) && (op_result[i]); i++) {
  214. char *str = op_result[i];
  215. snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
  216. if (cache_op == -1) {
  217. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  218. PERF_COUNT_HW_CACHE_OP_MAX);
  219. if (cache_op >= 0) {
  220. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  221. return -EINVAL;
  222. continue;
  223. }
  224. }
  225. if (cache_result == -1) {
  226. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  227. PERF_COUNT_HW_CACHE_RESULT_MAX);
  228. if (cache_result >= 0)
  229. continue;
  230. }
  231. }
  232. /*
  233. * Fall back to reads:
  234. */
  235. if (cache_op == -1)
  236. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  237. /*
  238. * Fall back to accesses:
  239. */
  240. if (cache_result == -1)
  241. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  242. memset(&attr, 0, sizeof(attr));
  243. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  244. attr.type = PERF_TYPE_HW_CACHE;
  245. return add_event(list, idx, &attr, name);
  246. }
  247. static int add_tracepoint(struct list_head **list, int *idx,
  248. char *sys_name, char *evt_name)
  249. {
  250. struct perf_event_attr attr;
  251. char name[MAX_NAME_LEN];
  252. char evt_path[MAXPATHLEN];
  253. char id_buf[4];
  254. u64 id;
  255. int fd;
  256. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  257. sys_name, evt_name);
  258. fd = open(evt_path, O_RDONLY);
  259. if (fd < 0)
  260. return -1;
  261. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  262. close(fd);
  263. return -1;
  264. }
  265. close(fd);
  266. id = atoll(id_buf);
  267. memset(&attr, 0, sizeof(attr));
  268. attr.config = id;
  269. attr.type = PERF_TYPE_TRACEPOINT;
  270. attr.sample_type |= PERF_SAMPLE_RAW;
  271. attr.sample_type |= PERF_SAMPLE_TIME;
  272. attr.sample_type |= PERF_SAMPLE_CPU;
  273. attr.sample_period = 1;
  274. snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
  275. return add_event(list, idx, &attr, name);
  276. }
  277. static int add_tracepoint_multi(struct list_head **list, int *idx,
  278. char *sys_name, char *evt_name)
  279. {
  280. char evt_path[MAXPATHLEN];
  281. struct dirent *evt_ent;
  282. DIR *evt_dir;
  283. int ret = 0;
  284. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  285. evt_dir = opendir(evt_path);
  286. if (!evt_dir) {
  287. perror("Can't open event dir");
  288. return -1;
  289. }
  290. while (!ret && (evt_ent = readdir(evt_dir))) {
  291. if (!strcmp(evt_ent->d_name, ".")
  292. || !strcmp(evt_ent->d_name, "..")
  293. || !strcmp(evt_ent->d_name, "enable")
  294. || !strcmp(evt_ent->d_name, "filter"))
  295. continue;
  296. if (!strglobmatch(evt_ent->d_name, evt_name))
  297. continue;
  298. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  299. }
  300. return ret;
  301. }
  302. int parse_events_add_tracepoint(struct list_head **list, int *idx,
  303. char *sys, char *event)
  304. {
  305. int ret;
  306. ret = debugfs_valid_mountpoint(tracing_events_path);
  307. if (ret)
  308. return ret;
  309. return strpbrk(event, "*?") ?
  310. add_tracepoint_multi(list, idx, sys, event) :
  311. add_tracepoint(list, idx, sys, event);
  312. }
  313. static int
  314. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  315. {
  316. int i;
  317. for (i = 0; i < 3; i++) {
  318. if (!type || !type[i])
  319. break;
  320. #define CHECK_SET_TYPE(bit) \
  321. do { \
  322. if (attr->bp_type & bit) \
  323. return -EINVAL; \
  324. else \
  325. attr->bp_type |= bit; \
  326. } while (0)
  327. switch (type[i]) {
  328. case 'r':
  329. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  330. break;
  331. case 'w':
  332. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  333. break;
  334. case 'x':
  335. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  336. break;
  337. default:
  338. return -EINVAL;
  339. }
  340. }
  341. #undef CHECK_SET_TYPE
  342. if (!attr->bp_type) /* Default */
  343. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  344. return 0;
  345. }
  346. int parse_events_add_breakpoint(struct list_head **list, int *idx,
  347. void *ptr, char *type)
  348. {
  349. struct perf_event_attr attr;
  350. memset(&attr, 0, sizeof(attr));
  351. attr.bp_addr = (unsigned long) ptr;
  352. if (parse_breakpoint_type(type, &attr))
  353. return -EINVAL;
  354. /*
  355. * We should find a nice way to override the access length
  356. * Provide some defaults for now
  357. */
  358. if (attr.bp_type == HW_BREAKPOINT_X)
  359. attr.bp_len = sizeof(long);
  360. else
  361. attr.bp_len = HW_BREAKPOINT_LEN_4;
  362. attr.type = PERF_TYPE_BREAKPOINT;
  363. return add_event(list, idx, &attr, NULL);
  364. }
  365. static int config_term(struct perf_event_attr *attr,
  366. struct parse_events__term *term)
  367. {
  368. #define CHECK_TYPE_VAL(type) \
  369. do { \
  370. if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
  371. return -EINVAL; \
  372. } while (0)
  373. switch (term->type_term) {
  374. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  375. CHECK_TYPE_VAL(NUM);
  376. attr->config = term->val.num;
  377. break;
  378. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  379. CHECK_TYPE_VAL(NUM);
  380. attr->config1 = term->val.num;
  381. break;
  382. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  383. CHECK_TYPE_VAL(NUM);
  384. attr->config2 = term->val.num;
  385. break;
  386. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  387. CHECK_TYPE_VAL(NUM);
  388. attr->sample_period = term->val.num;
  389. break;
  390. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  391. /*
  392. * TODO uncomment when the field is available
  393. * attr->branch_sample_type = term->val.num;
  394. */
  395. break;
  396. case PARSE_EVENTS__TERM_TYPE_NAME:
  397. CHECK_TYPE_VAL(STR);
  398. break;
  399. default:
  400. return -EINVAL;
  401. }
  402. return 0;
  403. #undef CHECK_TYPE_VAL
  404. }
  405. static int config_attr(struct perf_event_attr *attr,
  406. struct list_head *head, int fail)
  407. {
  408. struct parse_events__term *term;
  409. list_for_each_entry(term, head, list)
  410. if (config_term(attr, term) && fail)
  411. return -EINVAL;
  412. return 0;
  413. }
  414. int parse_events_add_numeric(struct list_head **list, int *idx,
  415. unsigned long type, unsigned long config,
  416. struct list_head *head_config)
  417. {
  418. struct perf_event_attr attr;
  419. memset(&attr, 0, sizeof(attr));
  420. attr.type = type;
  421. attr.config = config;
  422. if (head_config &&
  423. config_attr(&attr, head_config, 1))
  424. return -EINVAL;
  425. return add_event(list, idx, &attr, NULL);
  426. }
  427. static int parse_events__is_name_term(struct parse_events__term *term)
  428. {
  429. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  430. }
  431. static char *pmu_event_name(struct list_head *head_terms)
  432. {
  433. struct parse_events__term *term;
  434. list_for_each_entry(term, head_terms, list)
  435. if (parse_events__is_name_term(term))
  436. return term->val.str;
  437. return NULL;
  438. }
  439. int parse_events_add_pmu(struct list_head **list, int *idx,
  440. char *name, struct list_head *head_config)
  441. {
  442. struct perf_event_attr attr;
  443. struct perf_pmu *pmu;
  444. pmu = perf_pmu__find(name);
  445. if (!pmu)
  446. return -EINVAL;
  447. memset(&attr, 0, sizeof(attr));
  448. if (perf_pmu__check_alias(pmu, head_config))
  449. return -EINVAL;
  450. /*
  451. * Configure hardcoded terms first, no need to check
  452. * return value when called with fail == 0 ;)
  453. */
  454. config_attr(&attr, head_config, 0);
  455. if (perf_pmu__config(pmu, &attr, head_config))
  456. return -EINVAL;
  457. return add_event(list, idx, &attr,
  458. pmu_event_name(head_config));
  459. }
  460. void parse_events_update_lists(struct list_head *list_event,
  461. struct list_head *list_all)
  462. {
  463. /*
  464. * Called for single event definition. Update the
  465. * 'all event' list, and reinit the 'signle event'
  466. * list, for next event definition.
  467. */
  468. list_splice_tail(list_event, list_all);
  469. free(list_event);
  470. }
  471. int parse_events_modifier(struct list_head *list, char *str)
  472. {
  473. struct perf_evsel *evsel;
  474. int exclude = 0, exclude_GH = 0;
  475. int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
  476. if (str == NULL)
  477. return 0;
  478. while (*str) {
  479. if (*str == 'u') {
  480. if (!exclude)
  481. exclude = eu = ek = eh = 1;
  482. eu = 0;
  483. } else if (*str == 'k') {
  484. if (!exclude)
  485. exclude = eu = ek = eh = 1;
  486. ek = 0;
  487. } else if (*str == 'h') {
  488. if (!exclude)
  489. exclude = eu = ek = eh = 1;
  490. eh = 0;
  491. } else if (*str == 'G') {
  492. if (!exclude_GH)
  493. exclude_GH = eG = eH = 1;
  494. eG = 0;
  495. } else if (*str == 'H') {
  496. if (!exclude_GH)
  497. exclude_GH = eG = eH = 1;
  498. eH = 0;
  499. } else if (*str == 'p') {
  500. precise++;
  501. } else
  502. break;
  503. ++str;
  504. }
  505. /*
  506. * precise ip:
  507. *
  508. * 0 - SAMPLE_IP can have arbitrary skid
  509. * 1 - SAMPLE_IP must have constant skid
  510. * 2 - SAMPLE_IP requested to have 0 skid
  511. * 3 - SAMPLE_IP must have 0 skid
  512. *
  513. * See also PERF_RECORD_MISC_EXACT_IP
  514. */
  515. if (precise > 3)
  516. return -EINVAL;
  517. list_for_each_entry(evsel, list, node) {
  518. evsel->attr.exclude_user = eu;
  519. evsel->attr.exclude_kernel = ek;
  520. evsel->attr.exclude_hv = eh;
  521. evsel->attr.precise_ip = precise;
  522. evsel->attr.exclude_host = eH;
  523. evsel->attr.exclude_guest = eG;
  524. }
  525. return 0;
  526. }
  527. static int parse_events__scanner(const char *str, void *data, int start_token)
  528. {
  529. YY_BUFFER_STATE buffer;
  530. void *scanner;
  531. int ret;
  532. ret = parse_events_lex_init_extra(start_token, &scanner);
  533. if (ret)
  534. return ret;
  535. buffer = parse_events__scan_string(str, scanner);
  536. #ifdef PARSER_DEBUG
  537. parse_events_debug = 1;
  538. #endif
  539. ret = parse_events_parse(data, scanner);
  540. parse_events__flush_buffer(buffer, scanner);
  541. parse_events__delete_buffer(buffer, scanner);
  542. parse_events_lex_destroy(scanner);
  543. return ret;
  544. }
  545. /*
  546. * parse event config string, return a list of event terms.
  547. */
  548. int parse_events_terms(struct list_head *terms, const char *str)
  549. {
  550. struct parse_events_data__terms data = {
  551. .terms = NULL,
  552. };
  553. int ret;
  554. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  555. if (!ret) {
  556. list_splice(data.terms, terms);
  557. free(data.terms);
  558. return 0;
  559. }
  560. parse_events__free_terms(data.terms);
  561. return ret;
  562. }
  563. int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
  564. {
  565. struct parse_events_data__events data = {
  566. .list = LIST_HEAD_INIT(data.list),
  567. .idx = evlist->nr_entries,
  568. };
  569. int ret;
  570. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  571. if (!ret) {
  572. int entries = data.idx - evlist->nr_entries;
  573. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  574. return 0;
  575. }
  576. /*
  577. * There are 2 users - builtin-record and builtin-test objects.
  578. * Both call perf_evlist__delete in case of error, so we dont
  579. * need to bother.
  580. */
  581. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  582. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  583. return ret;
  584. }
  585. int parse_events_option(const struct option *opt, const char *str,
  586. int unset __used)
  587. {
  588. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  589. return parse_events(evlist, str, unset);
  590. }
  591. int parse_filter(const struct option *opt, const char *str,
  592. int unset __used)
  593. {
  594. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  595. struct perf_evsel *last = NULL;
  596. if (evlist->nr_entries > 0)
  597. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  598. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  599. fprintf(stderr,
  600. "-F option should follow a -e tracepoint option\n");
  601. return -1;
  602. }
  603. last->filter = strdup(str);
  604. if (last->filter == NULL) {
  605. fprintf(stderr, "not enough memory to hold filter string\n");
  606. return -1;
  607. }
  608. return 0;
  609. }
  610. static const char * const event_type_descriptors[] = {
  611. "Hardware event",
  612. "Software event",
  613. "Tracepoint event",
  614. "Hardware cache event",
  615. "Raw hardware event descriptor",
  616. "Hardware breakpoint",
  617. };
  618. /*
  619. * Print the events from <debugfs_mount_point>/tracing/events
  620. */
  621. void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
  622. {
  623. DIR *sys_dir, *evt_dir;
  624. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  625. char evt_path[MAXPATHLEN];
  626. char dir_path[MAXPATHLEN];
  627. if (debugfs_valid_mountpoint(tracing_events_path))
  628. return;
  629. sys_dir = opendir(tracing_events_path);
  630. if (!sys_dir)
  631. return;
  632. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  633. if (subsys_glob != NULL &&
  634. !strglobmatch(sys_dirent.d_name, subsys_glob))
  635. continue;
  636. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  637. sys_dirent.d_name);
  638. evt_dir = opendir(dir_path);
  639. if (!evt_dir)
  640. continue;
  641. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  642. if (event_glob != NULL &&
  643. !strglobmatch(evt_dirent.d_name, event_glob))
  644. continue;
  645. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  646. sys_dirent.d_name, evt_dirent.d_name);
  647. printf(" %-50s [%s]\n", evt_path,
  648. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  649. }
  650. closedir(evt_dir);
  651. }
  652. closedir(sys_dir);
  653. }
  654. /*
  655. * Check whether event is in <debugfs_mount_point>/tracing/events
  656. */
  657. int is_valid_tracepoint(const char *event_string)
  658. {
  659. DIR *sys_dir, *evt_dir;
  660. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  661. char evt_path[MAXPATHLEN];
  662. char dir_path[MAXPATHLEN];
  663. if (debugfs_valid_mountpoint(tracing_events_path))
  664. return 0;
  665. sys_dir = opendir(tracing_events_path);
  666. if (!sys_dir)
  667. return 0;
  668. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  669. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  670. sys_dirent.d_name);
  671. evt_dir = opendir(dir_path);
  672. if (!evt_dir)
  673. continue;
  674. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  675. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  676. sys_dirent.d_name, evt_dirent.d_name);
  677. if (!strcmp(evt_path, event_string)) {
  678. closedir(evt_dir);
  679. closedir(sys_dir);
  680. return 1;
  681. }
  682. }
  683. closedir(evt_dir);
  684. }
  685. closedir(sys_dir);
  686. return 0;
  687. }
  688. void print_events_type(u8 type)
  689. {
  690. struct event_symbol *syms = event_symbols;
  691. unsigned int i;
  692. char name[64];
  693. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  694. if (type != syms->type)
  695. continue;
  696. if (strlen(syms->alias))
  697. snprintf(name, sizeof(name), "%s OR %s",
  698. syms->symbol, syms->alias);
  699. else
  700. snprintf(name, sizeof(name), "%s", syms->symbol);
  701. printf(" %-50s [%s]\n", name,
  702. event_type_descriptors[type]);
  703. }
  704. }
  705. int print_hwcache_events(const char *event_glob)
  706. {
  707. unsigned int type, op, i, printed = 0;
  708. char name[64];
  709. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  710. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  711. /* skip invalid cache type */
  712. if (!perf_evsel__is_cache_op_valid(type, op))
  713. continue;
  714. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  715. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  716. name, sizeof(name));
  717. if (event_glob != NULL && !strglobmatch(name, event_glob))
  718. continue;
  719. printf(" %-50s [%s]\n", name,
  720. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  721. ++printed;
  722. }
  723. }
  724. }
  725. return printed;
  726. }
  727. /*
  728. * Print the help text for the event symbols:
  729. */
  730. void print_events(const char *event_glob)
  731. {
  732. unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
  733. struct event_symbol *syms = event_symbols;
  734. char name[MAX_NAME_LEN];
  735. printf("\n");
  736. printf("List of pre-defined events (to be used in -e):\n");
  737. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  738. type = syms->type;
  739. if (type != prev_type && printed) {
  740. printf("\n");
  741. printed = 0;
  742. ntypes_printed++;
  743. }
  744. if (event_glob != NULL &&
  745. !(strglobmatch(syms->symbol, event_glob) ||
  746. (syms->alias && strglobmatch(syms->alias, event_glob))))
  747. continue;
  748. if (strlen(syms->alias))
  749. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  750. else
  751. strncpy(name, syms->symbol, MAX_NAME_LEN);
  752. printf(" %-50s [%s]\n", name,
  753. event_type_descriptors[type]);
  754. prev_type = type;
  755. ++printed;
  756. }
  757. if (ntypes_printed) {
  758. printed = 0;
  759. printf("\n");
  760. }
  761. print_hwcache_events(event_glob);
  762. if (event_glob != NULL)
  763. return;
  764. printf("\n");
  765. printf(" %-50s [%s]\n",
  766. "rNNN",
  767. event_type_descriptors[PERF_TYPE_RAW]);
  768. printf(" %-50s [%s]\n",
  769. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  770. event_type_descriptors[PERF_TYPE_RAW]);
  771. printf(" (see 'perf list --help' on how to encode it)\n");
  772. printf("\n");
  773. printf(" %-50s [%s]\n",
  774. "mem:<addr>[:access]",
  775. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  776. printf("\n");
  777. print_tracepoint_events(NULL, NULL);
  778. }
  779. int parse_events__is_hardcoded_term(struct parse_events__term *term)
  780. {
  781. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  782. }
  783. static int new_term(struct parse_events__term **_term, int type_val,
  784. int type_term, char *config,
  785. char *str, long num)
  786. {
  787. struct parse_events__term *term;
  788. term = zalloc(sizeof(*term));
  789. if (!term)
  790. return -ENOMEM;
  791. INIT_LIST_HEAD(&term->list);
  792. term->type_val = type_val;
  793. term->type_term = type_term;
  794. term->config = config;
  795. switch (type_val) {
  796. case PARSE_EVENTS__TERM_TYPE_NUM:
  797. term->val.num = num;
  798. break;
  799. case PARSE_EVENTS__TERM_TYPE_STR:
  800. term->val.str = str;
  801. break;
  802. default:
  803. return -EINVAL;
  804. }
  805. *_term = term;
  806. return 0;
  807. }
  808. int parse_events__term_num(struct parse_events__term **term,
  809. int type_term, char *config, long num)
  810. {
  811. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  812. config, NULL, num);
  813. }
  814. int parse_events__term_str(struct parse_events__term **term,
  815. int type_term, char *config, char *str)
  816. {
  817. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  818. config, str, 0);
  819. }
  820. int parse_events__term_clone(struct parse_events__term **new,
  821. struct parse_events__term *term)
  822. {
  823. return new_term(new, term->type_val, term->type_term, term->config,
  824. term->val.str, term->val.num);
  825. }
  826. void parse_events__free_terms(struct list_head *terms)
  827. {
  828. struct parse_events__term *term, *h;
  829. list_for_each_entry_safe(term, h, terms, list)
  830. free(term);
  831. free(terms);
  832. }