parse-events.c 21 KB

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