parse-events.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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 <lk/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. struct tracepoint_path *tracepoint_name_to_path(const char *name)
  198. {
  199. struct tracepoint_path *path = zalloc(sizeof(*path));
  200. char *str = strchr(name, ':');
  201. if (path == NULL || str == NULL) {
  202. free(path);
  203. return NULL;
  204. }
  205. path->system = strndup(name, str - name);
  206. path->name = strdup(str+1);
  207. if (path->system == NULL || path->name == NULL) {
  208. free(path->system);
  209. free(path->name);
  210. free(path);
  211. path = NULL;
  212. }
  213. return path;
  214. }
  215. const char *event_type(int type)
  216. {
  217. switch (type) {
  218. case PERF_TYPE_HARDWARE:
  219. return "hardware";
  220. case PERF_TYPE_SOFTWARE:
  221. return "software";
  222. case PERF_TYPE_TRACEPOINT:
  223. return "tracepoint";
  224. case PERF_TYPE_HW_CACHE:
  225. return "hardware-cache";
  226. default:
  227. break;
  228. }
  229. return "unknown";
  230. }
  231. static int __add_event(struct list_head **_list, int *idx,
  232. struct perf_event_attr *attr,
  233. char *name, struct cpu_map *cpus)
  234. {
  235. struct perf_evsel *evsel;
  236. struct list_head *list = *_list;
  237. if (!list) {
  238. list = malloc(sizeof(*list));
  239. if (!list)
  240. return -ENOMEM;
  241. INIT_LIST_HEAD(list);
  242. }
  243. event_attr_init(attr);
  244. evsel = perf_evsel__new(attr, (*idx)++);
  245. if (!evsel) {
  246. free(list);
  247. return -ENOMEM;
  248. }
  249. evsel->cpus = cpus;
  250. if (name)
  251. evsel->name = strdup(name);
  252. list_add_tail(&evsel->node, list);
  253. *_list = list;
  254. return 0;
  255. }
  256. static int add_event(struct list_head **_list, int *idx,
  257. struct perf_event_attr *attr, char *name)
  258. {
  259. return __add_event(_list, idx, attr, name, NULL);
  260. }
  261. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  262. {
  263. int i, j;
  264. int n, longest = -1;
  265. for (i = 0; i < size; i++) {
  266. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  267. n = strlen(names[i][j]);
  268. if (n > longest && !strncasecmp(str, names[i][j], n))
  269. longest = n;
  270. }
  271. if (longest > 0)
  272. return i;
  273. }
  274. return -1;
  275. }
  276. int parse_events_add_cache(struct list_head **list, int *idx,
  277. char *type, char *op_result1, char *op_result2)
  278. {
  279. struct perf_event_attr attr;
  280. char name[MAX_NAME_LEN];
  281. int cache_type = -1, cache_op = -1, cache_result = -1;
  282. char *op_result[2] = { op_result1, op_result2 };
  283. int i, n;
  284. /*
  285. * No fallback - if we cannot get a clear cache type
  286. * then bail out:
  287. */
  288. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  289. PERF_COUNT_HW_CACHE_MAX);
  290. if (cache_type == -1)
  291. return -EINVAL;
  292. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  293. for (i = 0; (i < 2) && (op_result[i]); i++) {
  294. char *str = op_result[i];
  295. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  296. if (cache_op == -1) {
  297. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  298. PERF_COUNT_HW_CACHE_OP_MAX);
  299. if (cache_op >= 0) {
  300. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  301. return -EINVAL;
  302. continue;
  303. }
  304. }
  305. if (cache_result == -1) {
  306. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  307. PERF_COUNT_HW_CACHE_RESULT_MAX);
  308. if (cache_result >= 0)
  309. continue;
  310. }
  311. }
  312. /*
  313. * Fall back to reads:
  314. */
  315. if (cache_op == -1)
  316. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  317. /*
  318. * Fall back to accesses:
  319. */
  320. if (cache_result == -1)
  321. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  322. memset(&attr, 0, sizeof(attr));
  323. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  324. attr.type = PERF_TYPE_HW_CACHE;
  325. return add_event(list, idx, &attr, name);
  326. }
  327. static int add_tracepoint(struct list_head **listp, int *idx,
  328. char *sys_name, char *evt_name)
  329. {
  330. struct perf_evsel *evsel;
  331. struct list_head *list = *listp;
  332. if (!list) {
  333. list = malloc(sizeof(*list));
  334. if (!list)
  335. return -ENOMEM;
  336. INIT_LIST_HEAD(list);
  337. }
  338. evsel = perf_evsel__newtp(sys_name, evt_name, (*idx)++);
  339. if (!evsel) {
  340. free(list);
  341. return -ENOMEM;
  342. }
  343. list_add_tail(&evsel->node, list);
  344. *listp = list;
  345. return 0;
  346. }
  347. static int add_tracepoint_multi_event(struct list_head **list, int *idx,
  348. char *sys_name, char *evt_name)
  349. {
  350. char evt_path[MAXPATHLEN];
  351. struct dirent *evt_ent;
  352. DIR *evt_dir;
  353. int ret = 0;
  354. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  355. evt_dir = opendir(evt_path);
  356. if (!evt_dir) {
  357. perror("Can't open event dir");
  358. return -1;
  359. }
  360. while (!ret && (evt_ent = readdir(evt_dir))) {
  361. if (!strcmp(evt_ent->d_name, ".")
  362. || !strcmp(evt_ent->d_name, "..")
  363. || !strcmp(evt_ent->d_name, "enable")
  364. || !strcmp(evt_ent->d_name, "filter"))
  365. continue;
  366. if (!strglobmatch(evt_ent->d_name, evt_name))
  367. continue;
  368. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  369. }
  370. closedir(evt_dir);
  371. return ret;
  372. }
  373. static int add_tracepoint_event(struct list_head **list, int *idx,
  374. char *sys_name, char *evt_name)
  375. {
  376. return strpbrk(evt_name, "*?") ?
  377. add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
  378. add_tracepoint(list, idx, sys_name, evt_name);
  379. }
  380. static int add_tracepoint_multi_sys(struct list_head **list, int *idx,
  381. char *sys_name, char *evt_name)
  382. {
  383. struct dirent *events_ent;
  384. DIR *events_dir;
  385. int ret = 0;
  386. events_dir = opendir(tracing_events_path);
  387. if (!events_dir) {
  388. perror("Can't open event dir");
  389. return -1;
  390. }
  391. while (!ret && (events_ent = readdir(events_dir))) {
  392. if (!strcmp(events_ent->d_name, ".")
  393. || !strcmp(events_ent->d_name, "..")
  394. || !strcmp(events_ent->d_name, "enable")
  395. || !strcmp(events_ent->d_name, "header_event")
  396. || !strcmp(events_ent->d_name, "header_page"))
  397. continue;
  398. if (!strglobmatch(events_ent->d_name, sys_name))
  399. continue;
  400. ret = add_tracepoint_event(list, idx, events_ent->d_name,
  401. evt_name);
  402. }
  403. closedir(events_dir);
  404. return ret;
  405. }
  406. int parse_events_add_tracepoint(struct list_head **list, int *idx,
  407. char *sys, char *event)
  408. {
  409. int ret;
  410. ret = debugfs_valid_mountpoint(tracing_events_path);
  411. if (ret)
  412. return ret;
  413. if (strpbrk(sys, "*?"))
  414. return add_tracepoint_multi_sys(list, idx, sys, event);
  415. else
  416. return add_tracepoint_event(list, idx, sys, event);
  417. }
  418. static int
  419. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  420. {
  421. int i;
  422. for (i = 0; i < 3; i++) {
  423. if (!type || !type[i])
  424. break;
  425. #define CHECK_SET_TYPE(bit) \
  426. do { \
  427. if (attr->bp_type & bit) \
  428. return -EINVAL; \
  429. else \
  430. attr->bp_type |= bit; \
  431. } while (0)
  432. switch (type[i]) {
  433. case 'r':
  434. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  435. break;
  436. case 'w':
  437. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  438. break;
  439. case 'x':
  440. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. }
  446. #undef CHECK_SET_TYPE
  447. if (!attr->bp_type) /* Default */
  448. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  449. return 0;
  450. }
  451. int parse_events_add_breakpoint(struct list_head **list, int *idx,
  452. void *ptr, char *type)
  453. {
  454. struct perf_event_attr attr;
  455. memset(&attr, 0, sizeof(attr));
  456. attr.bp_addr = (unsigned long) ptr;
  457. if (parse_breakpoint_type(type, &attr))
  458. return -EINVAL;
  459. /*
  460. * We should find a nice way to override the access length
  461. * Provide some defaults for now
  462. */
  463. if (attr.bp_type == HW_BREAKPOINT_X)
  464. attr.bp_len = sizeof(long);
  465. else
  466. attr.bp_len = HW_BREAKPOINT_LEN_4;
  467. attr.type = PERF_TYPE_BREAKPOINT;
  468. attr.sample_period = 1;
  469. return add_event(list, idx, &attr, NULL);
  470. }
  471. static int config_term(struct perf_event_attr *attr,
  472. struct parse_events_term *term)
  473. {
  474. #define CHECK_TYPE_VAL(type) \
  475. do { \
  476. if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
  477. return -EINVAL; \
  478. } while (0)
  479. switch (term->type_term) {
  480. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  481. CHECK_TYPE_VAL(NUM);
  482. attr->config = term->val.num;
  483. break;
  484. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  485. CHECK_TYPE_VAL(NUM);
  486. attr->config1 = term->val.num;
  487. break;
  488. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  489. CHECK_TYPE_VAL(NUM);
  490. attr->config2 = term->val.num;
  491. break;
  492. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  493. CHECK_TYPE_VAL(NUM);
  494. attr->sample_period = term->val.num;
  495. break;
  496. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  497. /*
  498. * TODO uncomment when the field is available
  499. * attr->branch_sample_type = term->val.num;
  500. */
  501. break;
  502. case PARSE_EVENTS__TERM_TYPE_NAME:
  503. CHECK_TYPE_VAL(STR);
  504. break;
  505. default:
  506. return -EINVAL;
  507. }
  508. return 0;
  509. #undef CHECK_TYPE_VAL
  510. }
  511. static int config_attr(struct perf_event_attr *attr,
  512. struct list_head *head, int fail)
  513. {
  514. struct parse_events_term *term;
  515. list_for_each_entry(term, head, list)
  516. if (config_term(attr, term) && fail)
  517. return -EINVAL;
  518. return 0;
  519. }
  520. int parse_events_add_numeric(struct list_head **list, int *idx,
  521. u32 type, u64 config,
  522. struct list_head *head_config)
  523. {
  524. struct perf_event_attr attr;
  525. memset(&attr, 0, sizeof(attr));
  526. attr.type = type;
  527. attr.config = config;
  528. if (head_config &&
  529. config_attr(&attr, head_config, 1))
  530. return -EINVAL;
  531. return add_event(list, idx, &attr, NULL);
  532. }
  533. static int parse_events__is_name_term(struct parse_events_term *term)
  534. {
  535. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  536. }
  537. static char *pmu_event_name(struct list_head *head_terms)
  538. {
  539. struct parse_events_term *term;
  540. list_for_each_entry(term, head_terms, list)
  541. if (parse_events__is_name_term(term))
  542. return term->val.str;
  543. return NULL;
  544. }
  545. int parse_events_add_pmu(struct list_head **list, int *idx,
  546. char *name, struct list_head *head_config)
  547. {
  548. struct perf_event_attr attr;
  549. struct perf_pmu *pmu;
  550. pmu = perf_pmu__find(name);
  551. if (!pmu)
  552. return -EINVAL;
  553. memset(&attr, 0, sizeof(attr));
  554. if (perf_pmu__check_alias(pmu, head_config))
  555. return -EINVAL;
  556. /*
  557. * Configure hardcoded terms first, no need to check
  558. * return value when called with fail == 0 ;)
  559. */
  560. config_attr(&attr, head_config, 0);
  561. if (perf_pmu__config(pmu, &attr, head_config))
  562. return -EINVAL;
  563. return __add_event(list, idx, &attr, pmu_event_name(head_config),
  564. pmu->cpus);
  565. }
  566. int parse_events__modifier_group(struct list_head *list,
  567. char *event_mod)
  568. {
  569. return parse_events__modifier_event(list, event_mod, true);
  570. }
  571. void parse_events__set_leader(char *name, struct list_head *list)
  572. {
  573. struct perf_evsel *leader;
  574. __perf_evlist__set_leader(list);
  575. leader = list_entry(list->next, struct perf_evsel, node);
  576. leader->group_name = name ? strdup(name) : NULL;
  577. }
  578. void parse_events_update_lists(struct list_head *list_event,
  579. struct list_head *list_all)
  580. {
  581. /*
  582. * Called for single event definition. Update the
  583. * 'all event' list, and reinit the 'single event'
  584. * list, for next event definition.
  585. */
  586. list_splice_tail(list_event, list_all);
  587. free(list_event);
  588. }
  589. struct event_modifier {
  590. int eu;
  591. int ek;
  592. int eh;
  593. int eH;
  594. int eG;
  595. int precise;
  596. int exclude_GH;
  597. };
  598. static int get_event_modifier(struct event_modifier *mod, char *str,
  599. struct perf_evsel *evsel)
  600. {
  601. int eu = evsel ? evsel->attr.exclude_user : 0;
  602. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  603. int eh = evsel ? evsel->attr.exclude_hv : 0;
  604. int eH = evsel ? evsel->attr.exclude_host : 0;
  605. int eG = evsel ? evsel->attr.exclude_guest : 0;
  606. int precise = evsel ? evsel->attr.precise_ip : 0;
  607. int exclude = eu | ek | eh;
  608. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  609. memset(mod, 0, sizeof(*mod));
  610. while (*str) {
  611. if (*str == 'u') {
  612. if (!exclude)
  613. exclude = eu = ek = eh = 1;
  614. eu = 0;
  615. } else if (*str == 'k') {
  616. if (!exclude)
  617. exclude = eu = ek = eh = 1;
  618. ek = 0;
  619. } else if (*str == 'h') {
  620. if (!exclude)
  621. exclude = eu = ek = eh = 1;
  622. eh = 0;
  623. } else if (*str == 'G') {
  624. if (!exclude_GH)
  625. exclude_GH = eG = eH = 1;
  626. eG = 0;
  627. } else if (*str == 'H') {
  628. if (!exclude_GH)
  629. exclude_GH = eG = eH = 1;
  630. eH = 0;
  631. } else if (*str == 'p') {
  632. precise++;
  633. /* use of precise requires exclude_guest */
  634. if (!exclude_GH)
  635. eG = 1;
  636. } else
  637. break;
  638. ++str;
  639. }
  640. /*
  641. * precise ip:
  642. *
  643. * 0 - SAMPLE_IP can have arbitrary skid
  644. * 1 - SAMPLE_IP must have constant skid
  645. * 2 - SAMPLE_IP requested to have 0 skid
  646. * 3 - SAMPLE_IP must have 0 skid
  647. *
  648. * See also PERF_RECORD_MISC_EXACT_IP
  649. */
  650. if (precise > 3)
  651. return -EINVAL;
  652. mod->eu = eu;
  653. mod->ek = ek;
  654. mod->eh = eh;
  655. mod->eH = eH;
  656. mod->eG = eG;
  657. mod->precise = precise;
  658. mod->exclude_GH = exclude_GH;
  659. return 0;
  660. }
  661. /*
  662. * Basic modifier sanity check to validate it contains only one
  663. * instance of any modifier (apart from 'p') present.
  664. */
  665. static int check_modifier(char *str)
  666. {
  667. char *p = str;
  668. /* The sizeof includes 0 byte as well. */
  669. if (strlen(str) > (sizeof("ukhGHppp") - 1))
  670. return -1;
  671. while (*p) {
  672. if (*p != 'p' && strchr(p + 1, *p))
  673. return -1;
  674. p++;
  675. }
  676. return 0;
  677. }
  678. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  679. {
  680. struct perf_evsel *evsel;
  681. struct event_modifier mod;
  682. if (str == NULL)
  683. return 0;
  684. if (check_modifier(str))
  685. return -EINVAL;
  686. if (!add && get_event_modifier(&mod, str, NULL))
  687. return -EINVAL;
  688. list_for_each_entry(evsel, list, node) {
  689. if (add && get_event_modifier(&mod, str, evsel))
  690. return -EINVAL;
  691. evsel->attr.exclude_user = mod.eu;
  692. evsel->attr.exclude_kernel = mod.ek;
  693. evsel->attr.exclude_hv = mod.eh;
  694. evsel->attr.precise_ip = mod.precise;
  695. evsel->attr.exclude_host = mod.eH;
  696. evsel->attr.exclude_guest = mod.eG;
  697. evsel->exclude_GH = mod.exclude_GH;
  698. }
  699. return 0;
  700. }
  701. int parse_events_name(struct list_head *list, char *name)
  702. {
  703. struct perf_evsel *evsel;
  704. list_for_each_entry(evsel, list, node) {
  705. if (!evsel->name)
  706. evsel->name = strdup(name);
  707. }
  708. return 0;
  709. }
  710. static int parse_events__scanner(const char *str, void *data, int start_token)
  711. {
  712. YY_BUFFER_STATE buffer;
  713. void *scanner;
  714. int ret;
  715. ret = parse_events_lex_init_extra(start_token, &scanner);
  716. if (ret)
  717. return ret;
  718. buffer = parse_events__scan_string(str, scanner);
  719. #ifdef PARSER_DEBUG
  720. parse_events_debug = 1;
  721. #endif
  722. ret = parse_events_parse(data, scanner);
  723. parse_events__flush_buffer(buffer, scanner);
  724. parse_events__delete_buffer(buffer, scanner);
  725. parse_events_lex_destroy(scanner);
  726. return ret;
  727. }
  728. /*
  729. * parse event config string, return a list of event terms.
  730. */
  731. int parse_events_terms(struct list_head *terms, const char *str)
  732. {
  733. struct parse_events_terms data = {
  734. .terms = NULL,
  735. };
  736. int ret;
  737. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  738. if (!ret) {
  739. list_splice(data.terms, terms);
  740. free(data.terms);
  741. return 0;
  742. }
  743. if (data.terms)
  744. parse_events__free_terms(data.terms);
  745. return ret;
  746. }
  747. int parse_events(struct perf_evlist *evlist, const char *str)
  748. {
  749. struct parse_events_evlist data = {
  750. .list = LIST_HEAD_INIT(data.list),
  751. .idx = evlist->nr_entries,
  752. };
  753. int ret;
  754. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  755. if (!ret) {
  756. int entries = data.idx - evlist->nr_entries;
  757. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  758. evlist->nr_groups += data.nr_groups;
  759. return 0;
  760. }
  761. /*
  762. * There are 2 users - builtin-record and builtin-test objects.
  763. * Both call perf_evlist__delete in case of error, so we dont
  764. * need to bother.
  765. */
  766. return ret;
  767. }
  768. int parse_events_option(const struct option *opt, const char *str,
  769. int unset __maybe_unused)
  770. {
  771. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  772. int ret = parse_events(evlist, str);
  773. if (ret) {
  774. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  775. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  776. }
  777. return ret;
  778. }
  779. int parse_filter(const struct option *opt, const char *str,
  780. int unset __maybe_unused)
  781. {
  782. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  783. struct perf_evsel *last = NULL;
  784. if (evlist->nr_entries > 0)
  785. last = perf_evlist__last(evlist);
  786. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  787. fprintf(stderr,
  788. "-F option should follow a -e tracepoint option\n");
  789. return -1;
  790. }
  791. last->filter = strdup(str);
  792. if (last->filter == NULL) {
  793. fprintf(stderr, "not enough memory to hold filter string\n");
  794. return -1;
  795. }
  796. return 0;
  797. }
  798. static const char * const event_type_descriptors[] = {
  799. "Hardware event",
  800. "Software event",
  801. "Tracepoint event",
  802. "Hardware cache event",
  803. "Raw hardware event descriptor",
  804. "Hardware breakpoint",
  805. };
  806. /*
  807. * Print the events from <debugfs_mount_point>/tracing/events
  808. */
  809. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  810. bool name_only)
  811. {
  812. DIR *sys_dir, *evt_dir;
  813. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  814. char evt_path[MAXPATHLEN];
  815. char dir_path[MAXPATHLEN];
  816. if (debugfs_valid_mountpoint(tracing_events_path))
  817. return;
  818. sys_dir = opendir(tracing_events_path);
  819. if (!sys_dir)
  820. return;
  821. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  822. if (subsys_glob != NULL &&
  823. !strglobmatch(sys_dirent.d_name, subsys_glob))
  824. continue;
  825. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  826. sys_dirent.d_name);
  827. evt_dir = opendir(dir_path);
  828. if (!evt_dir)
  829. continue;
  830. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  831. if (event_glob != NULL &&
  832. !strglobmatch(evt_dirent.d_name, event_glob))
  833. continue;
  834. if (name_only) {
  835. printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
  836. continue;
  837. }
  838. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  839. sys_dirent.d_name, evt_dirent.d_name);
  840. printf(" %-50s [%s]\n", evt_path,
  841. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  842. }
  843. closedir(evt_dir);
  844. }
  845. closedir(sys_dir);
  846. }
  847. /*
  848. * Check whether event is in <debugfs_mount_point>/tracing/events
  849. */
  850. int is_valid_tracepoint(const char *event_string)
  851. {
  852. DIR *sys_dir, *evt_dir;
  853. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  854. char evt_path[MAXPATHLEN];
  855. char dir_path[MAXPATHLEN];
  856. if (debugfs_valid_mountpoint(tracing_events_path))
  857. return 0;
  858. sys_dir = opendir(tracing_events_path);
  859. if (!sys_dir)
  860. return 0;
  861. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  862. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  863. sys_dirent.d_name);
  864. evt_dir = opendir(dir_path);
  865. if (!evt_dir)
  866. continue;
  867. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  868. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  869. sys_dirent.d_name, evt_dirent.d_name);
  870. if (!strcmp(evt_path, event_string)) {
  871. closedir(evt_dir);
  872. closedir(sys_dir);
  873. return 1;
  874. }
  875. }
  876. closedir(evt_dir);
  877. }
  878. closedir(sys_dir);
  879. return 0;
  880. }
  881. static void __print_events_type(u8 type, struct event_symbol *syms,
  882. unsigned max)
  883. {
  884. char name[64];
  885. unsigned i;
  886. for (i = 0; i < max ; i++, syms++) {
  887. if (strlen(syms->alias))
  888. snprintf(name, sizeof(name), "%s OR %s",
  889. syms->symbol, syms->alias);
  890. else
  891. snprintf(name, sizeof(name), "%s", syms->symbol);
  892. printf(" %-50s [%s]\n", name,
  893. event_type_descriptors[type]);
  894. }
  895. }
  896. void print_events_type(u8 type)
  897. {
  898. if (type == PERF_TYPE_SOFTWARE)
  899. __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
  900. else
  901. __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
  902. }
  903. int print_hwcache_events(const char *event_glob, bool name_only)
  904. {
  905. unsigned int type, op, i, printed = 0;
  906. char name[64];
  907. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  908. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  909. /* skip invalid cache type */
  910. if (!perf_evsel__is_cache_op_valid(type, op))
  911. continue;
  912. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  913. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  914. name, sizeof(name));
  915. if (event_glob != NULL && !strglobmatch(name, event_glob))
  916. continue;
  917. if (name_only)
  918. printf("%s ", name);
  919. else
  920. printf(" %-50s [%s]\n", name,
  921. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  922. ++printed;
  923. }
  924. }
  925. }
  926. return printed;
  927. }
  928. static void print_symbol_events(const char *event_glob, unsigned type,
  929. struct event_symbol *syms, unsigned max,
  930. bool name_only)
  931. {
  932. unsigned i, printed = 0;
  933. char name[MAX_NAME_LEN];
  934. for (i = 0; i < max; i++, syms++) {
  935. if (event_glob != NULL &&
  936. !(strglobmatch(syms->symbol, event_glob) ||
  937. (syms->alias && strglobmatch(syms->alias, event_glob))))
  938. continue;
  939. if (name_only) {
  940. printf("%s ", syms->symbol);
  941. continue;
  942. }
  943. if (strlen(syms->alias))
  944. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  945. else
  946. strncpy(name, syms->symbol, MAX_NAME_LEN);
  947. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  948. printed++;
  949. }
  950. if (printed)
  951. printf("\n");
  952. }
  953. /*
  954. * Print the help text for the event symbols:
  955. */
  956. void print_events(const char *event_glob, bool name_only)
  957. {
  958. if (!name_only) {
  959. printf("\n");
  960. printf("List of pre-defined events (to be used in -e):\n");
  961. }
  962. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  963. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  964. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  965. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  966. print_hwcache_events(event_glob, name_only);
  967. if (event_glob != NULL)
  968. return;
  969. if (!name_only) {
  970. printf("\n");
  971. printf(" %-50s [%s]\n",
  972. "rNNN",
  973. event_type_descriptors[PERF_TYPE_RAW]);
  974. printf(" %-50s [%s]\n",
  975. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  976. event_type_descriptors[PERF_TYPE_RAW]);
  977. printf(" (see 'man perf-list' on how to encode it)\n");
  978. printf("\n");
  979. printf(" %-50s [%s]\n",
  980. "mem:<addr>[:access]",
  981. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  982. printf("\n");
  983. }
  984. print_tracepoint_events(NULL, NULL, name_only);
  985. }
  986. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  987. {
  988. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  989. }
  990. static int new_term(struct parse_events_term **_term, int type_val,
  991. int type_term, char *config,
  992. char *str, u64 num)
  993. {
  994. struct parse_events_term *term;
  995. term = zalloc(sizeof(*term));
  996. if (!term)
  997. return -ENOMEM;
  998. INIT_LIST_HEAD(&term->list);
  999. term->type_val = type_val;
  1000. term->type_term = type_term;
  1001. term->config = config;
  1002. switch (type_val) {
  1003. case PARSE_EVENTS__TERM_TYPE_NUM:
  1004. term->val.num = num;
  1005. break;
  1006. case PARSE_EVENTS__TERM_TYPE_STR:
  1007. term->val.str = str;
  1008. break;
  1009. default:
  1010. free(term);
  1011. return -EINVAL;
  1012. }
  1013. *_term = term;
  1014. return 0;
  1015. }
  1016. int parse_events_term__num(struct parse_events_term **term,
  1017. int type_term, char *config, u64 num)
  1018. {
  1019. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1020. config, NULL, num);
  1021. }
  1022. int parse_events_term__str(struct parse_events_term **term,
  1023. int type_term, char *config, char *str)
  1024. {
  1025. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1026. config, str, 0);
  1027. }
  1028. int parse_events_term__sym_hw(struct parse_events_term **term,
  1029. char *config, unsigned idx)
  1030. {
  1031. struct event_symbol *sym;
  1032. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1033. sym = &event_symbols_hw[idx];
  1034. if (config)
  1035. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1036. PARSE_EVENTS__TERM_TYPE_USER, config,
  1037. (char *) sym->symbol, 0);
  1038. else
  1039. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1040. PARSE_EVENTS__TERM_TYPE_USER,
  1041. (char *) "event", (char *) sym->symbol, 0);
  1042. }
  1043. int parse_events_term__clone(struct parse_events_term **new,
  1044. struct parse_events_term *term)
  1045. {
  1046. return new_term(new, term->type_val, term->type_term, term->config,
  1047. term->val.str, term->val.num);
  1048. }
  1049. void parse_events__free_terms(struct list_head *terms)
  1050. {
  1051. struct parse_events_term *term, *h;
  1052. list_for_each_entry_safe(term, h, terms, list)
  1053. free(term);
  1054. free(terms);
  1055. }