parse-events.c 23 KB

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