parse-events.c 24 KB

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