parse-events.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. struct event_symbol {
  15. u8 type;
  16. u64 config;
  17. const char *symbol;
  18. const char *alias;
  19. };
  20. enum event_result {
  21. EVT_FAILED,
  22. EVT_HANDLED,
  23. EVT_HANDLED_ALL
  24. };
  25. char debugfs_path[MAXPATHLEN];
  26. #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
  27. #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
  28. static struct event_symbol event_symbols[] = {
  29. { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
  30. { CHW(INSTRUCTIONS), "instructions", "" },
  31. { CHW(CACHE_REFERENCES), "cache-references", "" },
  32. { CHW(CACHE_MISSES), "cache-misses", "" },
  33. { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
  34. { CHW(BRANCH_MISSES), "branch-misses", "" },
  35. { CHW(BUS_CYCLES), "bus-cycles", "" },
  36. { CSW(CPU_CLOCK), "cpu-clock", "" },
  37. { CSW(TASK_CLOCK), "task-clock", "" },
  38. { CSW(PAGE_FAULTS), "page-faults", "faults" },
  39. { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
  40. { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
  41. { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
  42. { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
  43. { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
  44. { CSW(EMULATION_FAULTS), "emulation-faults", "" },
  45. };
  46. #define __PERF_EVENT_FIELD(config, name) \
  47. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  48. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  49. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  50. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  51. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  52. static const char *hw_event_names[] = {
  53. "cycles",
  54. "instructions",
  55. "cache-references",
  56. "cache-misses",
  57. "branches",
  58. "branch-misses",
  59. "bus-cycles",
  60. };
  61. static const char *sw_event_names[] = {
  62. "cpu-clock-msecs",
  63. "task-clock-msecs",
  64. "page-faults",
  65. "context-switches",
  66. "CPU-migrations",
  67. "minor-faults",
  68. "major-faults",
  69. "alignment-faults",
  70. "emulation-faults",
  71. };
  72. #define MAX_ALIASES 8
  73. static const char *hw_cache[][MAX_ALIASES] = {
  74. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  75. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  76. { "LLC", "L2" },
  77. { "dTLB", "d-tlb", "Data-TLB", },
  78. { "iTLB", "i-tlb", "Instruction-TLB", },
  79. { "branch", "branches", "bpu", "btb", "bpc", },
  80. };
  81. static const char *hw_cache_op[][MAX_ALIASES] = {
  82. { "load", "loads", "read", },
  83. { "store", "stores", "write", },
  84. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  85. };
  86. static const char *hw_cache_result[][MAX_ALIASES] = {
  87. { "refs", "Reference", "ops", "access", },
  88. { "misses", "miss", },
  89. };
  90. #define C(x) PERF_COUNT_HW_CACHE_##x
  91. #define CACHE_READ (1 << C(OP_READ))
  92. #define CACHE_WRITE (1 << C(OP_WRITE))
  93. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  94. #define COP(x) (1 << x)
  95. /*
  96. * cache operartion stat
  97. * L1I : Read and prefetch only
  98. * ITLB and BPU : Read-only
  99. */
  100. static unsigned long hw_cache_stat[C(MAX)] = {
  101. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  102. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  103. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  104. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  105. [C(ITLB)] = (CACHE_READ),
  106. [C(BPU)] = (CACHE_READ),
  107. };
  108. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  109. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  110. if (sys_dirent.d_type == DT_DIR && \
  111. (strcmp(sys_dirent.d_name, ".")) && \
  112. (strcmp(sys_dirent.d_name, "..")))
  113. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  114. {
  115. char evt_path[MAXPATHLEN];
  116. int fd;
  117. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  118. sys_dir->d_name, evt_dir->d_name);
  119. fd = open(evt_path, O_RDONLY);
  120. if (fd < 0)
  121. return -EINVAL;
  122. close(fd);
  123. return 0;
  124. }
  125. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  126. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  127. if (evt_dirent.d_type == DT_DIR && \
  128. (strcmp(evt_dirent.d_name, ".")) && \
  129. (strcmp(evt_dirent.d_name, "..")) && \
  130. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  131. #define MAX_EVENT_LENGTH 512
  132. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  133. {
  134. struct tracepoint_path *path = NULL;
  135. DIR *sys_dir, *evt_dir;
  136. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  137. char id_buf[4];
  138. int fd;
  139. u64 id;
  140. char evt_path[MAXPATHLEN];
  141. char dir_path[MAXPATHLEN];
  142. if (debugfs_valid_mountpoint(debugfs_path))
  143. return NULL;
  144. sys_dir = opendir(debugfs_path);
  145. if (!sys_dir)
  146. return NULL;
  147. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  148. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  149. sys_dirent.d_name);
  150. evt_dir = opendir(dir_path);
  151. if (!evt_dir)
  152. continue;
  153. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  154. snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
  155. evt_dirent.d_name);
  156. fd = open(evt_path, O_RDONLY);
  157. if (fd < 0)
  158. continue;
  159. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  160. close(fd);
  161. continue;
  162. }
  163. close(fd);
  164. id = atoll(id_buf);
  165. if (id == config) {
  166. closedir(evt_dir);
  167. closedir(sys_dir);
  168. path = zalloc(sizeof(*path));
  169. path->system = malloc(MAX_EVENT_LENGTH);
  170. if (!path->system) {
  171. free(path);
  172. return NULL;
  173. }
  174. path->name = malloc(MAX_EVENT_LENGTH);
  175. if (!path->name) {
  176. free(path->system);
  177. free(path);
  178. return NULL;
  179. }
  180. strncpy(path->system, sys_dirent.d_name,
  181. MAX_EVENT_LENGTH);
  182. strncpy(path->name, evt_dirent.d_name,
  183. MAX_EVENT_LENGTH);
  184. return path;
  185. }
  186. }
  187. closedir(evt_dir);
  188. }
  189. closedir(sys_dir);
  190. return NULL;
  191. }
  192. #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
  193. static const char *tracepoint_id_to_name(u64 config)
  194. {
  195. static char buf[TP_PATH_LEN];
  196. struct tracepoint_path *path;
  197. path = tracepoint_id_to_path(config);
  198. if (path) {
  199. snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
  200. free(path->name);
  201. free(path->system);
  202. free(path);
  203. } else
  204. snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
  205. return buf;
  206. }
  207. static int is_cache_op_valid(u8 cache_type, u8 cache_op)
  208. {
  209. if (hw_cache_stat[cache_type] & COP(cache_op))
  210. return 1; /* valid */
  211. else
  212. return 0; /* invalid */
  213. }
  214. static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
  215. {
  216. static char name[50];
  217. if (cache_result) {
  218. sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
  219. hw_cache_op[cache_op][0],
  220. hw_cache_result[cache_result][0]);
  221. } else {
  222. sprintf(name, "%s-%s", hw_cache[cache_type][0],
  223. hw_cache_op[cache_op][1]);
  224. }
  225. return name;
  226. }
  227. const char *event_name(struct perf_evsel *evsel)
  228. {
  229. u64 config = evsel->attr.config;
  230. int type = evsel->attr.type;
  231. return __event_name(type, config);
  232. }
  233. const char *__event_name(int type, u64 config)
  234. {
  235. static char buf[32];
  236. if (type == PERF_TYPE_RAW) {
  237. sprintf(buf, "raw 0x%" PRIx64, config);
  238. return buf;
  239. }
  240. switch (type) {
  241. case PERF_TYPE_HARDWARE:
  242. if (config < PERF_COUNT_HW_MAX)
  243. return hw_event_names[config];
  244. return "unknown-hardware";
  245. case PERF_TYPE_HW_CACHE: {
  246. u8 cache_type, cache_op, cache_result;
  247. cache_type = (config >> 0) & 0xff;
  248. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  249. return "unknown-ext-hardware-cache-type";
  250. cache_op = (config >> 8) & 0xff;
  251. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  252. return "unknown-ext-hardware-cache-op";
  253. cache_result = (config >> 16) & 0xff;
  254. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  255. return "unknown-ext-hardware-cache-result";
  256. if (!is_cache_op_valid(cache_type, cache_op))
  257. return "invalid-cache";
  258. return event_cache_name(cache_type, cache_op, cache_result);
  259. }
  260. case PERF_TYPE_SOFTWARE:
  261. if (config < PERF_COUNT_SW_MAX)
  262. return sw_event_names[config];
  263. return "unknown-software";
  264. case PERF_TYPE_TRACEPOINT:
  265. return tracepoint_id_to_name(config);
  266. default:
  267. break;
  268. }
  269. return "unknown";
  270. }
  271. static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
  272. {
  273. int i, j;
  274. int n, longest = -1;
  275. for (i = 0; i < size; i++) {
  276. for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
  277. n = strlen(names[i][j]);
  278. if (n > longest && !strncasecmp(*str, names[i][j], n))
  279. longest = n;
  280. }
  281. if (longest > 0) {
  282. *str += longest;
  283. return i;
  284. }
  285. }
  286. return -1;
  287. }
  288. static enum event_result
  289. parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
  290. {
  291. const char *s = *str;
  292. int cache_type = -1, cache_op = -1, cache_result = -1;
  293. cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  294. /*
  295. * No fallback - if we cannot get a clear cache type
  296. * then bail out:
  297. */
  298. if (cache_type == -1)
  299. return EVT_FAILED;
  300. while ((cache_op == -1 || cache_result == -1) && *s == '-') {
  301. ++s;
  302. if (cache_op == -1) {
  303. cache_op = parse_aliases(&s, hw_cache_op,
  304. PERF_COUNT_HW_CACHE_OP_MAX);
  305. if (cache_op >= 0) {
  306. if (!is_cache_op_valid(cache_type, cache_op))
  307. return 0;
  308. continue;
  309. }
  310. }
  311. if (cache_result == -1) {
  312. cache_result = parse_aliases(&s, hw_cache_result,
  313. PERF_COUNT_HW_CACHE_RESULT_MAX);
  314. if (cache_result >= 0)
  315. continue;
  316. }
  317. /*
  318. * Can't parse this as a cache op or result, so back up
  319. * to the '-'.
  320. */
  321. --s;
  322. break;
  323. }
  324. /*
  325. * Fall back to reads:
  326. */
  327. if (cache_op == -1)
  328. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  329. /*
  330. * Fall back to accesses:
  331. */
  332. if (cache_result == -1)
  333. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  334. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  335. attr->type = PERF_TYPE_HW_CACHE;
  336. *str = s;
  337. return EVT_HANDLED;
  338. }
  339. static enum event_result
  340. parse_single_tracepoint_event(char *sys_name,
  341. const char *evt_name,
  342. unsigned int evt_length,
  343. struct perf_event_attr *attr,
  344. const char **strp)
  345. {
  346. char evt_path[MAXPATHLEN];
  347. char id_buf[4];
  348. u64 id;
  349. int fd;
  350. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  351. sys_name, evt_name);
  352. fd = open(evt_path, O_RDONLY);
  353. if (fd < 0)
  354. return EVT_FAILED;
  355. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  356. close(fd);
  357. return EVT_FAILED;
  358. }
  359. close(fd);
  360. id = atoll(id_buf);
  361. attr->config = id;
  362. attr->type = PERF_TYPE_TRACEPOINT;
  363. *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
  364. attr->sample_type |= PERF_SAMPLE_RAW;
  365. attr->sample_type |= PERF_SAMPLE_TIME;
  366. attr->sample_type |= PERF_SAMPLE_CPU;
  367. attr->sample_period = 1;
  368. return EVT_HANDLED;
  369. }
  370. /* sys + ':' + event + ':' + flags*/
  371. #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
  372. static enum event_result
  373. parse_multiple_tracepoint_event(const struct option *opt, char *sys_name,
  374. const char *evt_exp, char *flags)
  375. {
  376. char evt_path[MAXPATHLEN];
  377. struct dirent *evt_ent;
  378. DIR *evt_dir;
  379. snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
  380. evt_dir = opendir(evt_path);
  381. if (!evt_dir) {
  382. perror("Can't open event dir");
  383. return EVT_FAILED;
  384. }
  385. while ((evt_ent = readdir(evt_dir))) {
  386. char event_opt[MAX_EVOPT_LEN + 1];
  387. int len;
  388. if (!strcmp(evt_ent->d_name, ".")
  389. || !strcmp(evt_ent->d_name, "..")
  390. || !strcmp(evt_ent->d_name, "enable")
  391. || !strcmp(evt_ent->d_name, "filter"))
  392. continue;
  393. if (!strglobmatch(evt_ent->d_name, evt_exp))
  394. continue;
  395. len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
  396. evt_ent->d_name, flags ? ":" : "",
  397. flags ?: "");
  398. if (len < 0)
  399. return EVT_FAILED;
  400. if (parse_events(opt, event_opt, 0))
  401. return EVT_FAILED;
  402. }
  403. return EVT_HANDLED_ALL;
  404. }
  405. static enum event_result
  406. parse_tracepoint_event(const struct option *opt, const char **strp,
  407. struct perf_event_attr *attr)
  408. {
  409. const char *evt_name;
  410. char *flags = NULL, *comma_loc;
  411. char sys_name[MAX_EVENT_LENGTH];
  412. unsigned int sys_length, evt_length;
  413. if (debugfs_valid_mountpoint(debugfs_path))
  414. return 0;
  415. evt_name = strchr(*strp, ':');
  416. if (!evt_name)
  417. return EVT_FAILED;
  418. sys_length = evt_name - *strp;
  419. if (sys_length >= MAX_EVENT_LENGTH)
  420. return 0;
  421. strncpy(sys_name, *strp, sys_length);
  422. sys_name[sys_length] = '\0';
  423. evt_name = evt_name + 1;
  424. comma_loc = strchr(evt_name, ',');
  425. if (comma_loc) {
  426. /* take the event name up to the comma */
  427. evt_name = strndup(evt_name, comma_loc - evt_name);
  428. }
  429. flags = strchr(evt_name, ':');
  430. if (flags) {
  431. /* split it out: */
  432. evt_name = strndup(evt_name, flags - evt_name);
  433. flags++;
  434. }
  435. evt_length = strlen(evt_name);
  436. if (evt_length >= MAX_EVENT_LENGTH)
  437. return EVT_FAILED;
  438. if (strpbrk(evt_name, "*?")) {
  439. *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
  440. return parse_multiple_tracepoint_event(opt, sys_name, evt_name,
  441. flags);
  442. } else {
  443. return parse_single_tracepoint_event(sys_name, evt_name,
  444. evt_length, attr, strp);
  445. }
  446. }
  447. static enum event_result
  448. parse_breakpoint_type(const char *type, const char **strp,
  449. struct perf_event_attr *attr)
  450. {
  451. int i;
  452. for (i = 0; i < 3; i++) {
  453. if (!type[i])
  454. break;
  455. switch (type[i]) {
  456. case 'r':
  457. attr->bp_type |= HW_BREAKPOINT_R;
  458. break;
  459. case 'w':
  460. attr->bp_type |= HW_BREAKPOINT_W;
  461. break;
  462. case 'x':
  463. attr->bp_type |= HW_BREAKPOINT_X;
  464. break;
  465. default:
  466. return EVT_FAILED;
  467. }
  468. }
  469. if (!attr->bp_type) /* Default */
  470. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  471. *strp = type + i;
  472. return EVT_HANDLED;
  473. }
  474. static enum event_result
  475. parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
  476. {
  477. const char *target;
  478. const char *type;
  479. char *endaddr;
  480. u64 addr;
  481. enum event_result err;
  482. target = strchr(*strp, ':');
  483. if (!target)
  484. return EVT_FAILED;
  485. if (strncmp(*strp, "mem", target - *strp) != 0)
  486. return EVT_FAILED;
  487. target++;
  488. addr = strtoull(target, &endaddr, 0);
  489. if (target == endaddr)
  490. return EVT_FAILED;
  491. attr->bp_addr = addr;
  492. *strp = endaddr;
  493. type = strchr(target, ':');
  494. /* If no type is defined, just rw as default */
  495. if (!type) {
  496. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  497. } else {
  498. err = parse_breakpoint_type(++type, strp, attr);
  499. if (err == EVT_FAILED)
  500. return EVT_FAILED;
  501. }
  502. /*
  503. * We should find a nice way to override the access length
  504. * Provide some defaults for now
  505. */
  506. if (attr->bp_type == HW_BREAKPOINT_X)
  507. attr->bp_len = sizeof(long);
  508. else
  509. attr->bp_len = HW_BREAKPOINT_LEN_4;
  510. attr->type = PERF_TYPE_BREAKPOINT;
  511. return EVT_HANDLED;
  512. }
  513. static int check_events(const char *str, unsigned int i)
  514. {
  515. int n;
  516. n = strlen(event_symbols[i].symbol);
  517. if (!strncmp(str, event_symbols[i].symbol, n))
  518. return n;
  519. n = strlen(event_symbols[i].alias);
  520. if (n)
  521. if (!strncmp(str, event_symbols[i].alias, n))
  522. return n;
  523. return 0;
  524. }
  525. static enum event_result
  526. parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
  527. {
  528. const char *str = *strp;
  529. unsigned int i;
  530. int n;
  531. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  532. n = check_events(str, i);
  533. if (n > 0) {
  534. attr->type = event_symbols[i].type;
  535. attr->config = event_symbols[i].config;
  536. *strp = str + n;
  537. return EVT_HANDLED;
  538. }
  539. }
  540. return EVT_FAILED;
  541. }
  542. static enum event_result
  543. parse_raw_event(const char **strp, struct perf_event_attr *attr)
  544. {
  545. const char *str = *strp;
  546. u64 config;
  547. int n;
  548. if (*str != 'r')
  549. return EVT_FAILED;
  550. n = hex2u64(str + 1, &config);
  551. if (n > 0) {
  552. *strp = str + n + 1;
  553. attr->type = PERF_TYPE_RAW;
  554. attr->config = config;
  555. return EVT_HANDLED;
  556. }
  557. return EVT_FAILED;
  558. }
  559. static enum event_result
  560. parse_numeric_event(const char **strp, struct perf_event_attr *attr)
  561. {
  562. const char *str = *strp;
  563. char *endp;
  564. unsigned long type;
  565. u64 config;
  566. type = strtoul(str, &endp, 0);
  567. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  568. str = endp + 1;
  569. config = strtoul(str, &endp, 0);
  570. if (endp > str) {
  571. attr->type = type;
  572. attr->config = config;
  573. *strp = endp;
  574. return EVT_HANDLED;
  575. }
  576. }
  577. return EVT_FAILED;
  578. }
  579. static enum event_result
  580. parse_event_modifier(const char **strp, struct perf_event_attr *attr)
  581. {
  582. const char *str = *strp;
  583. int exclude = 0;
  584. int eu = 0, ek = 0, eh = 0, precise = 0;
  585. if (*str++ != ':')
  586. return 0;
  587. while (*str) {
  588. if (*str == 'u') {
  589. if (!exclude)
  590. exclude = eu = ek = eh = 1;
  591. eu = 0;
  592. } else if (*str == 'k') {
  593. if (!exclude)
  594. exclude = eu = ek = eh = 1;
  595. ek = 0;
  596. } else if (*str == 'h') {
  597. if (!exclude)
  598. exclude = eu = ek = eh = 1;
  599. eh = 0;
  600. } else if (*str == 'p') {
  601. precise++;
  602. } else
  603. break;
  604. ++str;
  605. }
  606. if (str >= *strp + 2) {
  607. *strp = str;
  608. attr->exclude_user = eu;
  609. attr->exclude_kernel = ek;
  610. attr->exclude_hv = eh;
  611. attr->precise_ip = precise;
  612. return 1;
  613. }
  614. return 0;
  615. }
  616. /*
  617. * Each event can have multiple symbolic names.
  618. * Symbolic names are (almost) exactly matched.
  619. */
  620. static enum event_result
  621. parse_event_symbols(const struct option *opt, const char **str,
  622. struct perf_event_attr *attr)
  623. {
  624. enum event_result ret;
  625. ret = parse_tracepoint_event(opt, str, attr);
  626. if (ret != EVT_FAILED)
  627. goto modifier;
  628. ret = parse_raw_event(str, attr);
  629. if (ret != EVT_FAILED)
  630. goto modifier;
  631. ret = parse_numeric_event(str, attr);
  632. if (ret != EVT_FAILED)
  633. goto modifier;
  634. ret = parse_symbolic_event(str, attr);
  635. if (ret != EVT_FAILED)
  636. goto modifier;
  637. ret = parse_generic_hw_event(str, attr);
  638. if (ret != EVT_FAILED)
  639. goto modifier;
  640. ret = parse_breakpoint_event(str, attr);
  641. if (ret != EVT_FAILED)
  642. goto modifier;
  643. fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
  644. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  645. return EVT_FAILED;
  646. modifier:
  647. parse_event_modifier(str, attr);
  648. return ret;
  649. }
  650. int parse_events(const struct option *opt, const char *str, int unset __used)
  651. {
  652. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  653. struct perf_event_attr attr;
  654. enum event_result ret;
  655. for (;;) {
  656. memset(&attr, 0, sizeof(attr));
  657. ret = parse_event_symbols(opt, &str, &attr);
  658. if (ret == EVT_FAILED)
  659. return -1;
  660. if (!(*str == 0 || *str == ',' || isspace(*str)))
  661. return -1;
  662. if (ret != EVT_HANDLED_ALL) {
  663. struct perf_evsel *evsel;
  664. evsel = perf_evsel__new(&attr, evlist->nr_entries);
  665. if (evsel == NULL)
  666. return -1;
  667. perf_evlist__add(evlist, evsel);
  668. }
  669. if (*str == 0)
  670. break;
  671. if (*str == ',')
  672. ++str;
  673. while (isspace(*str))
  674. ++str;
  675. }
  676. return 0;
  677. }
  678. int parse_filter(const struct option *opt, const char *str,
  679. int unset __used)
  680. {
  681. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  682. struct perf_evsel *last = NULL;
  683. if (evlist->nr_entries > 0)
  684. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  685. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  686. fprintf(stderr,
  687. "-F option should follow a -e tracepoint option\n");
  688. return -1;
  689. }
  690. last->filter = strdup(str);
  691. if (last->filter == NULL) {
  692. fprintf(stderr, "not enough memory to hold filter string\n");
  693. return -1;
  694. }
  695. return 0;
  696. }
  697. static const char * const event_type_descriptors[] = {
  698. "Hardware event",
  699. "Software event",
  700. "Tracepoint event",
  701. "Hardware cache event",
  702. "Raw hardware event descriptor",
  703. "Hardware breakpoint",
  704. };
  705. /*
  706. * Print the events from <debugfs_mount_point>/tracing/events
  707. */
  708. static void print_tracepoint_events(void)
  709. {
  710. DIR *sys_dir, *evt_dir;
  711. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  712. char evt_path[MAXPATHLEN];
  713. char dir_path[MAXPATHLEN];
  714. if (debugfs_valid_mountpoint(debugfs_path))
  715. return;
  716. sys_dir = opendir(debugfs_path);
  717. if (!sys_dir)
  718. return;
  719. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  720. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  721. sys_dirent.d_name);
  722. evt_dir = opendir(dir_path);
  723. if (!evt_dir)
  724. continue;
  725. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  726. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  727. sys_dirent.d_name, evt_dirent.d_name);
  728. printf(" %-42s [%s]\n", evt_path,
  729. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  730. }
  731. closedir(evt_dir);
  732. }
  733. closedir(sys_dir);
  734. }
  735. /*
  736. * Check whether event is in <debugfs_mount_point>/tracing/events
  737. */
  738. int is_valid_tracepoint(const char *event_string)
  739. {
  740. DIR *sys_dir, *evt_dir;
  741. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  742. char evt_path[MAXPATHLEN];
  743. char dir_path[MAXPATHLEN];
  744. if (debugfs_valid_mountpoint(debugfs_path))
  745. return 0;
  746. sys_dir = opendir(debugfs_path);
  747. if (!sys_dir)
  748. return 0;
  749. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  750. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  751. sys_dirent.d_name);
  752. evt_dir = opendir(dir_path);
  753. if (!evt_dir)
  754. continue;
  755. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  756. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  757. sys_dirent.d_name, evt_dirent.d_name);
  758. if (!strcmp(evt_path, event_string)) {
  759. closedir(evt_dir);
  760. closedir(sys_dir);
  761. return 1;
  762. }
  763. }
  764. closedir(evt_dir);
  765. }
  766. closedir(sys_dir);
  767. return 0;
  768. }
  769. /*
  770. * Print the help text for the event symbols:
  771. */
  772. void print_events(void)
  773. {
  774. struct event_symbol *syms = event_symbols;
  775. unsigned int i, type, op, prev_type = -1;
  776. char name[40];
  777. printf("\n");
  778. printf("List of pre-defined events (to be used in -e):\n");
  779. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  780. type = syms->type;
  781. if (type != prev_type)
  782. printf("\n");
  783. if (strlen(syms->alias))
  784. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  785. else
  786. strcpy(name, syms->symbol);
  787. printf(" %-42s [%s]\n", name,
  788. event_type_descriptors[type]);
  789. prev_type = type;
  790. }
  791. printf("\n");
  792. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  793. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  794. /* skip invalid cache type */
  795. if (!is_cache_op_valid(type, op))
  796. continue;
  797. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  798. printf(" %-42s [%s]\n",
  799. event_cache_name(type, op, i),
  800. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  801. }
  802. }
  803. }
  804. printf("\n");
  805. printf(" %-42s [%s]\n",
  806. "rNNN (see 'perf list --help' on how to encode it)",
  807. event_type_descriptors[PERF_TYPE_RAW]);
  808. printf("\n");
  809. printf(" %-42s [%s]\n",
  810. "mem:<addr>[:access]",
  811. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  812. printf("\n");
  813. print_tracepoint_events();
  814. exit(129);
  815. }