parse-events.c 29 KB

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