parse-events.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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 "cache.h"
  9. #include "header.h"
  10. #include "debugfs.h"
  11. int nr_counters;
  12. struct perf_event_attr attrs[MAX_COUNTERS];
  13. char *filters[MAX_COUNTERS];
  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(int counter)
  228. {
  229. u64 config = attrs[counter].config;
  230. int type = attrs[counter].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%llx", 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. char *flags,
  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. if (flags) {
  352. if (!strncmp(flags, "record", strlen(flags))) {
  353. attr->sample_type |= PERF_SAMPLE_RAW;
  354. attr->sample_type |= PERF_SAMPLE_TIME;
  355. attr->sample_type |= PERF_SAMPLE_CPU;
  356. }
  357. }
  358. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  359. sys_name, evt_name);
  360. fd = open(evt_path, O_RDONLY);
  361. if (fd < 0)
  362. return EVT_FAILED;
  363. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  364. close(fd);
  365. return EVT_FAILED;
  366. }
  367. close(fd);
  368. id = atoll(id_buf);
  369. attr->config = id;
  370. attr->type = PERF_TYPE_TRACEPOINT;
  371. *strp = evt_name + evt_length;
  372. return EVT_HANDLED;
  373. }
  374. /* sys + ':' + event + ':' + flags*/
  375. #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
  376. static enum event_result
  377. parse_multiple_tracepoint_event(char *sys_name, const char *evt_exp,
  378. char *flags)
  379. {
  380. char evt_path[MAXPATHLEN];
  381. struct dirent *evt_ent;
  382. DIR *evt_dir;
  383. snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
  384. evt_dir = opendir(evt_path);
  385. if (!evt_dir) {
  386. perror("Can't open event dir");
  387. return EVT_FAILED;
  388. }
  389. while ((evt_ent = readdir(evt_dir))) {
  390. char event_opt[MAX_EVOPT_LEN + 1];
  391. int len;
  392. if (!strcmp(evt_ent->d_name, ".")
  393. || !strcmp(evt_ent->d_name, "..")
  394. || !strcmp(evt_ent->d_name, "enable")
  395. || !strcmp(evt_ent->d_name, "filter"))
  396. continue;
  397. if (!strglobmatch(evt_ent->d_name, evt_exp))
  398. continue;
  399. len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
  400. evt_ent->d_name, flags ? ":" : "",
  401. flags ?: "");
  402. if (len < 0)
  403. return EVT_FAILED;
  404. if (parse_events(NULL, event_opt, 0))
  405. return EVT_FAILED;
  406. }
  407. return EVT_HANDLED_ALL;
  408. }
  409. static enum event_result parse_tracepoint_event(const char **strp,
  410. struct perf_event_attr *attr)
  411. {
  412. const char *evt_name;
  413. char *flags;
  414. char sys_name[MAX_EVENT_LENGTH];
  415. unsigned int sys_length, evt_length;
  416. if (debugfs_valid_mountpoint(debugfs_path))
  417. return 0;
  418. evt_name = strchr(*strp, ':');
  419. if (!evt_name)
  420. return EVT_FAILED;
  421. sys_length = evt_name - *strp;
  422. if (sys_length >= MAX_EVENT_LENGTH)
  423. return 0;
  424. strncpy(sys_name, *strp, sys_length);
  425. sys_name[sys_length] = '\0';
  426. evt_name = evt_name + 1;
  427. flags = strchr(evt_name, ':');
  428. if (flags) {
  429. /* split it out: */
  430. evt_name = strndup(evt_name, flags - evt_name);
  431. flags++;
  432. }
  433. evt_length = strlen(evt_name);
  434. if (evt_length >= MAX_EVENT_LENGTH)
  435. return EVT_FAILED;
  436. if (strpbrk(evt_name, "*?")) {
  437. *strp = evt_name + evt_length;
  438. return parse_multiple_tracepoint_event(sys_name, evt_name,
  439. flags);
  440. } else
  441. return parse_single_tracepoint_event(sys_name, evt_name,
  442. evt_length, flags,
  443. attr, strp);
  444. }
  445. static enum event_result
  446. parse_breakpoint_type(const char *type, const char **strp,
  447. struct perf_event_attr *attr)
  448. {
  449. int i;
  450. for (i = 0; i < 3; i++) {
  451. if (!type[i])
  452. break;
  453. switch (type[i]) {
  454. case 'r':
  455. attr->bp_type |= HW_BREAKPOINT_R;
  456. break;
  457. case 'w':
  458. attr->bp_type |= HW_BREAKPOINT_W;
  459. break;
  460. case 'x':
  461. attr->bp_type |= HW_BREAKPOINT_X;
  462. break;
  463. default:
  464. return EVT_FAILED;
  465. }
  466. }
  467. if (!attr->bp_type) /* Default */
  468. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  469. *strp = type + i;
  470. return EVT_HANDLED;
  471. }
  472. static enum event_result
  473. parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
  474. {
  475. const char *target;
  476. const char *type;
  477. char *endaddr;
  478. u64 addr;
  479. enum event_result err;
  480. target = strchr(*strp, ':');
  481. if (!target)
  482. return EVT_FAILED;
  483. if (strncmp(*strp, "mem", target - *strp) != 0)
  484. return EVT_FAILED;
  485. target++;
  486. addr = strtoull(target, &endaddr, 0);
  487. if (target == endaddr)
  488. return EVT_FAILED;
  489. attr->bp_addr = addr;
  490. *strp = endaddr;
  491. type = strchr(target, ':');
  492. /* If no type is defined, just rw as default */
  493. if (!type) {
  494. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  495. } else {
  496. err = parse_breakpoint_type(++type, strp, attr);
  497. if (err == EVT_FAILED)
  498. return EVT_FAILED;
  499. }
  500. /* We should find a nice way to override the access type */
  501. attr->bp_len = HW_BREAKPOINT_LEN_4;
  502. attr->type = PERF_TYPE_BREAKPOINT;
  503. return EVT_HANDLED;
  504. }
  505. static int check_events(const char *str, unsigned int i)
  506. {
  507. int n;
  508. n = strlen(event_symbols[i].symbol);
  509. if (!strncmp(str, event_symbols[i].symbol, n))
  510. return n;
  511. n = strlen(event_symbols[i].alias);
  512. if (n)
  513. if (!strncmp(str, event_symbols[i].alias, n))
  514. return n;
  515. return 0;
  516. }
  517. static enum event_result
  518. parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
  519. {
  520. const char *str = *strp;
  521. unsigned int i;
  522. int n;
  523. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  524. n = check_events(str, i);
  525. if (n > 0) {
  526. attr->type = event_symbols[i].type;
  527. attr->config = event_symbols[i].config;
  528. *strp = str + n;
  529. return EVT_HANDLED;
  530. }
  531. }
  532. return EVT_FAILED;
  533. }
  534. static enum event_result
  535. parse_raw_event(const char **strp, struct perf_event_attr *attr)
  536. {
  537. const char *str = *strp;
  538. u64 config;
  539. int n;
  540. if (*str != 'r')
  541. return EVT_FAILED;
  542. n = hex2u64(str + 1, &config);
  543. if (n > 0) {
  544. *strp = str + n + 1;
  545. attr->type = PERF_TYPE_RAW;
  546. attr->config = config;
  547. return EVT_HANDLED;
  548. }
  549. return EVT_FAILED;
  550. }
  551. static enum event_result
  552. parse_numeric_event(const char **strp, struct perf_event_attr *attr)
  553. {
  554. const char *str = *strp;
  555. char *endp;
  556. unsigned long type;
  557. u64 config;
  558. type = strtoul(str, &endp, 0);
  559. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  560. str = endp + 1;
  561. config = strtoul(str, &endp, 0);
  562. if (endp > str) {
  563. attr->type = type;
  564. attr->config = config;
  565. *strp = endp;
  566. return EVT_HANDLED;
  567. }
  568. }
  569. return EVT_FAILED;
  570. }
  571. static enum event_result
  572. parse_event_modifier(const char **strp, struct perf_event_attr *attr)
  573. {
  574. const char *str = *strp;
  575. int eu = 1, ek = 1, eh = 1;
  576. if (*str++ != ':')
  577. return 0;
  578. while (*str) {
  579. if (*str == 'u')
  580. eu = 0;
  581. else if (*str == 'k')
  582. ek = 0;
  583. else if (*str == 'h')
  584. eh = 0;
  585. else
  586. break;
  587. ++str;
  588. }
  589. if (str >= *strp + 2) {
  590. *strp = str;
  591. attr->exclude_user = eu;
  592. attr->exclude_kernel = ek;
  593. attr->exclude_hv = eh;
  594. return 1;
  595. }
  596. return 0;
  597. }
  598. /*
  599. * Each event can have multiple symbolic names.
  600. * Symbolic names are (almost) exactly matched.
  601. */
  602. static enum event_result
  603. parse_event_symbols(const char **str, struct perf_event_attr *attr)
  604. {
  605. enum event_result ret;
  606. ret = parse_tracepoint_event(str, attr);
  607. if (ret != EVT_FAILED)
  608. goto modifier;
  609. ret = parse_raw_event(str, attr);
  610. if (ret != EVT_FAILED)
  611. goto modifier;
  612. ret = parse_numeric_event(str, attr);
  613. if (ret != EVT_FAILED)
  614. goto modifier;
  615. ret = parse_symbolic_event(str, attr);
  616. if (ret != EVT_FAILED)
  617. goto modifier;
  618. ret = parse_generic_hw_event(str, attr);
  619. if (ret != EVT_FAILED)
  620. goto modifier;
  621. ret = parse_breakpoint_event(str, attr);
  622. if (ret != EVT_FAILED)
  623. goto modifier;
  624. fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
  625. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  626. return EVT_FAILED;
  627. modifier:
  628. parse_event_modifier(str, attr);
  629. return ret;
  630. }
  631. static int store_event_type(const char *orgname)
  632. {
  633. char filename[PATH_MAX], *c;
  634. FILE *file;
  635. int id, n;
  636. sprintf(filename, "%s/", debugfs_path);
  637. strncat(filename, orgname, strlen(orgname));
  638. strcat(filename, "/id");
  639. c = strchr(filename, ':');
  640. if (c)
  641. *c = '/';
  642. file = fopen(filename, "r");
  643. if (!file)
  644. return 0;
  645. n = fscanf(file, "%i", &id);
  646. fclose(file);
  647. if (n < 1) {
  648. pr_err("cannot store event ID\n");
  649. return -EINVAL;
  650. }
  651. return perf_header__push_event(id, orgname);
  652. }
  653. int parse_events(const struct option *opt __used, const char *str, int unset __used)
  654. {
  655. struct perf_event_attr attr;
  656. enum event_result ret;
  657. if (strchr(str, ':'))
  658. if (store_event_type(str) < 0)
  659. return -1;
  660. for (;;) {
  661. if (nr_counters == MAX_COUNTERS)
  662. return -1;
  663. memset(&attr, 0, sizeof(attr));
  664. ret = parse_event_symbols(&str, &attr);
  665. if (ret == EVT_FAILED)
  666. return -1;
  667. if (!(*str == 0 || *str == ',' || isspace(*str)))
  668. return -1;
  669. if (ret != EVT_HANDLED_ALL) {
  670. attrs[nr_counters] = attr;
  671. nr_counters++;
  672. }
  673. if (*str == 0)
  674. break;
  675. if (*str == ',')
  676. ++str;
  677. while (isspace(*str))
  678. ++str;
  679. }
  680. return 0;
  681. }
  682. int parse_filter(const struct option *opt __used, const char *str,
  683. int unset __used)
  684. {
  685. int i = nr_counters - 1;
  686. int len = strlen(str);
  687. if (i < 0 || attrs[i].type != PERF_TYPE_TRACEPOINT) {
  688. fprintf(stderr,
  689. "-F option should follow a -e tracepoint option\n");
  690. return -1;
  691. }
  692. filters[i] = malloc(len + 1);
  693. if (!filters[i]) {
  694. fprintf(stderr, "not enough memory to hold filter string\n");
  695. return -1;
  696. }
  697. strcpy(filters[i], str);
  698. return 0;
  699. }
  700. static const char * const event_type_descriptors[] = {
  701. "Hardware event",
  702. "Software event",
  703. "Tracepoint event",
  704. "Hardware cache event",
  705. "Raw hardware event descriptor",
  706. "Hardware breakpoint",
  707. };
  708. /*
  709. * Print the events from <debugfs_mount_point>/tracing/events
  710. */
  711. static void print_tracepoint_events(void)
  712. {
  713. DIR *sys_dir, *evt_dir;
  714. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  715. char evt_path[MAXPATHLEN];
  716. char dir_path[MAXPATHLEN];
  717. if (debugfs_valid_mountpoint(debugfs_path))
  718. return;
  719. sys_dir = opendir(debugfs_path);
  720. if (!sys_dir)
  721. return;
  722. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  723. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  724. sys_dirent.d_name);
  725. evt_dir = opendir(dir_path);
  726. if (!evt_dir)
  727. continue;
  728. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  729. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  730. sys_dirent.d_name, evt_dirent.d_name);
  731. printf(" %-42s [%s]\n", evt_path,
  732. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  733. }
  734. closedir(evt_dir);
  735. }
  736. closedir(sys_dir);
  737. }
  738. /*
  739. * Print the help text for the event symbols:
  740. */
  741. void print_events(void)
  742. {
  743. struct event_symbol *syms = event_symbols;
  744. unsigned int i, type, op, prev_type = -1;
  745. char name[40];
  746. printf("\n");
  747. printf("List of pre-defined events (to be used in -e):\n");
  748. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  749. type = syms->type;
  750. if (type != prev_type)
  751. printf("\n");
  752. if (strlen(syms->alias))
  753. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  754. else
  755. strcpy(name, syms->symbol);
  756. printf(" %-42s [%s]\n", name,
  757. event_type_descriptors[type]);
  758. prev_type = type;
  759. }
  760. printf("\n");
  761. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  762. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  763. /* skip invalid cache type */
  764. if (!is_cache_op_valid(type, op))
  765. continue;
  766. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  767. printf(" %-42s [%s]\n",
  768. event_cache_name(type, op, i),
  769. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  770. }
  771. }
  772. }
  773. printf("\n");
  774. printf(" %-42s [%s]\n",
  775. "rNNN", event_type_descriptors[PERF_TYPE_RAW]);
  776. printf("\n");
  777. printf(" %-42s [%s]\n",
  778. "mem:<addr>[:access]",
  779. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  780. printf("\n");
  781. print_tracepoint_events();
  782. exit(129);
  783. }