parse-events.c 26 KB

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