parse-events.c 21 KB

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