parse-events.c 23 KB

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