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 "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_subsystem_tracepoint_event(char *sys_name, char *flags)
  378. {
  379. char evt_path[MAXPATHLEN];
  380. struct dirent *evt_ent;
  381. DIR *evt_dir;
  382. snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
  383. evt_dir = opendir(evt_path);
  384. if (!evt_dir) {
  385. perror("Can't open event dir");
  386. return EVT_FAILED;
  387. }
  388. while ((evt_ent = readdir(evt_dir))) {
  389. char event_opt[MAX_EVOPT_LEN + 1];
  390. int len;
  391. unsigned int rem = MAX_EVOPT_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. len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s", sys_name,
  398. evt_ent->d_name);
  399. if (len < 0)
  400. return EVT_FAILED;
  401. rem -= len;
  402. if (flags) {
  403. if (rem < strlen(flags) + 1)
  404. return EVT_FAILED;
  405. strcat(event_opt, ":");
  406. strcat(event_opt, flags);
  407. }
  408. if (parse_events(NULL, event_opt, 0))
  409. return EVT_FAILED;
  410. }
  411. return EVT_HANDLED_ALL;
  412. }
  413. static enum event_result parse_tracepoint_event(const char **strp,
  414. struct perf_event_attr *attr)
  415. {
  416. const char *evt_name;
  417. char *flags;
  418. char sys_name[MAX_EVENT_LENGTH];
  419. unsigned int sys_length, evt_length;
  420. if (debugfs_valid_mountpoint(debugfs_path))
  421. return 0;
  422. evt_name = strchr(*strp, ':');
  423. if (!evt_name)
  424. return EVT_FAILED;
  425. sys_length = evt_name - *strp;
  426. if (sys_length >= MAX_EVENT_LENGTH)
  427. return 0;
  428. strncpy(sys_name, *strp, sys_length);
  429. sys_name[sys_length] = '\0';
  430. evt_name = evt_name + 1;
  431. flags = strchr(evt_name, ':');
  432. if (flags) {
  433. /* split it out: */
  434. evt_name = strndup(evt_name, flags - evt_name);
  435. flags++;
  436. }
  437. evt_length = strlen(evt_name);
  438. if (evt_length >= MAX_EVENT_LENGTH)
  439. return EVT_FAILED;
  440. if (!strcmp(evt_name, "*")) {
  441. *strp = evt_name + evt_length;
  442. return parse_subsystem_tracepoint_event(sys_name, flags);
  443. } else
  444. return parse_single_tracepoint_event(sys_name, evt_name,
  445. evt_length, flags,
  446. attr, strp);
  447. }
  448. static enum event_result
  449. parse_breakpoint_type(const char *type, const char **strp,
  450. struct perf_event_attr *attr)
  451. {
  452. int i;
  453. for (i = 0; i < 3; i++) {
  454. if (!type[i])
  455. break;
  456. switch (type[i]) {
  457. case 'r':
  458. attr->bp_type |= HW_BREAKPOINT_R;
  459. break;
  460. case 'w':
  461. attr->bp_type |= HW_BREAKPOINT_W;
  462. break;
  463. case 'x':
  464. attr->bp_type |= HW_BREAKPOINT_X;
  465. break;
  466. default:
  467. return EVT_FAILED;
  468. }
  469. }
  470. if (!attr->bp_type) /* Default */
  471. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  472. *strp = type + i;
  473. return EVT_HANDLED;
  474. }
  475. static enum event_result
  476. parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
  477. {
  478. const char *target;
  479. const char *type;
  480. char *endaddr;
  481. u64 addr;
  482. enum event_result err;
  483. target = strchr(*strp, ':');
  484. if (!target)
  485. return EVT_FAILED;
  486. if (strncmp(*strp, "mem", target - *strp) != 0)
  487. return EVT_FAILED;
  488. target++;
  489. addr = strtoull(target, &endaddr, 0);
  490. if (target == endaddr)
  491. return EVT_FAILED;
  492. attr->bp_addr = addr;
  493. *strp = endaddr;
  494. type = strchr(target, ':');
  495. /* If no type is defined, just rw as default */
  496. if (!type) {
  497. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  498. } else {
  499. err = parse_breakpoint_type(++type, strp, attr);
  500. if (err == EVT_FAILED)
  501. return EVT_FAILED;
  502. }
  503. /* We should find a nice way to override the access type */
  504. attr->bp_len = HW_BREAKPOINT_LEN_4;
  505. attr->type = PERF_TYPE_BREAKPOINT;
  506. return EVT_HANDLED;
  507. }
  508. static int check_events(const char *str, unsigned int i)
  509. {
  510. int n;
  511. n = strlen(event_symbols[i].symbol);
  512. if (!strncmp(str, event_symbols[i].symbol, n))
  513. return n;
  514. n = strlen(event_symbols[i].alias);
  515. if (n)
  516. if (!strncmp(str, event_symbols[i].alias, n))
  517. return n;
  518. return 0;
  519. }
  520. static enum event_result
  521. parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
  522. {
  523. const char *str = *strp;
  524. unsigned int i;
  525. int n;
  526. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  527. n = check_events(str, i);
  528. if (n > 0) {
  529. attr->type = event_symbols[i].type;
  530. attr->config = event_symbols[i].config;
  531. *strp = str + n;
  532. return EVT_HANDLED;
  533. }
  534. }
  535. return EVT_FAILED;
  536. }
  537. static enum event_result
  538. parse_raw_event(const char **strp, struct perf_event_attr *attr)
  539. {
  540. const char *str = *strp;
  541. u64 config;
  542. int n;
  543. if (*str != 'r')
  544. return EVT_FAILED;
  545. n = hex2u64(str + 1, &config);
  546. if (n > 0) {
  547. *strp = str + n + 1;
  548. attr->type = PERF_TYPE_RAW;
  549. attr->config = config;
  550. return EVT_HANDLED;
  551. }
  552. return EVT_FAILED;
  553. }
  554. static enum event_result
  555. parse_numeric_event(const char **strp, struct perf_event_attr *attr)
  556. {
  557. const char *str = *strp;
  558. char *endp;
  559. unsigned long type;
  560. u64 config;
  561. type = strtoul(str, &endp, 0);
  562. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  563. str = endp + 1;
  564. config = strtoul(str, &endp, 0);
  565. if (endp > str) {
  566. attr->type = type;
  567. attr->config = config;
  568. *strp = endp;
  569. return EVT_HANDLED;
  570. }
  571. }
  572. return EVT_FAILED;
  573. }
  574. static enum event_result
  575. parse_event_modifier(const char **strp, struct perf_event_attr *attr)
  576. {
  577. const char *str = *strp;
  578. int eu = 1, ek = 1, eh = 1;
  579. if (*str++ != ':')
  580. return 0;
  581. while (*str) {
  582. if (*str == 'u')
  583. eu = 0;
  584. else if (*str == 'k')
  585. ek = 0;
  586. else if (*str == 'h')
  587. eh = 0;
  588. else
  589. break;
  590. ++str;
  591. }
  592. if (str >= *strp + 2) {
  593. *strp = str;
  594. attr->exclude_user = eu;
  595. attr->exclude_kernel = ek;
  596. attr->exclude_hv = eh;
  597. return 1;
  598. }
  599. return 0;
  600. }
  601. /*
  602. * Each event can have multiple symbolic names.
  603. * Symbolic names are (almost) exactly matched.
  604. */
  605. static enum event_result
  606. parse_event_symbols(const char **str, struct perf_event_attr *attr)
  607. {
  608. enum event_result ret;
  609. ret = parse_tracepoint_event(str, attr);
  610. if (ret != EVT_FAILED)
  611. goto modifier;
  612. ret = parse_raw_event(str, attr);
  613. if (ret != EVT_FAILED)
  614. goto modifier;
  615. ret = parse_numeric_event(str, attr);
  616. if (ret != EVT_FAILED)
  617. goto modifier;
  618. ret = parse_symbolic_event(str, attr);
  619. if (ret != EVT_FAILED)
  620. goto modifier;
  621. ret = parse_generic_hw_event(str, attr);
  622. if (ret != EVT_FAILED)
  623. goto modifier;
  624. ret = parse_breakpoint_event(str, attr);
  625. if (ret != EVT_FAILED)
  626. goto modifier;
  627. fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
  628. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  629. return EVT_FAILED;
  630. modifier:
  631. parse_event_modifier(str, attr);
  632. return ret;
  633. }
  634. static void store_event_type(const char *orgname)
  635. {
  636. char filename[PATH_MAX], *c;
  637. FILE *file;
  638. int id;
  639. sprintf(filename, "%s/", debugfs_path);
  640. strncat(filename, orgname, strlen(orgname));
  641. strcat(filename, "/id");
  642. c = strchr(filename, ':');
  643. if (c)
  644. *c = '/';
  645. file = fopen(filename, "r");
  646. if (!file)
  647. return;
  648. if (fscanf(file, "%i", &id) < 1)
  649. die("cannot store event ID");
  650. fclose(file);
  651. 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. store_event_type(str);
  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. "",
  701. "Hardware event",
  702. "Software event",
  703. "Tracepoint event",
  704. "Hardware cache event",
  705. };
  706. /*
  707. * Print the events from <debugfs_mount_point>/tracing/events
  708. */
  709. static void print_tracepoint_events(void)
  710. {
  711. DIR *sys_dir, *evt_dir;
  712. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  713. char evt_path[MAXPATHLEN];
  714. char dir_path[MAXPATHLEN];
  715. if (debugfs_valid_mountpoint(debugfs_path))
  716. return;
  717. sys_dir = opendir(debugfs_path);
  718. if (!sys_dir)
  719. return;
  720. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  721. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  722. sys_dirent.d_name);
  723. evt_dir = opendir(dir_path);
  724. if (!evt_dir)
  725. continue;
  726. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  727. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  728. sys_dirent.d_name, evt_dirent.d_name);
  729. printf(" %-42s [%s]\n", evt_path,
  730. event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
  731. }
  732. closedir(evt_dir);
  733. }
  734. closedir(sys_dir);
  735. }
  736. /*
  737. * Print the help text for the event symbols:
  738. */
  739. void print_events(void)
  740. {
  741. struct event_symbol *syms = event_symbols;
  742. unsigned int i, type, op, prev_type = -1;
  743. char name[40];
  744. printf("\n");
  745. printf("List of pre-defined events (to be used in -e):\n");
  746. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  747. type = syms->type + 1;
  748. if (type >= ARRAY_SIZE(event_type_descriptors))
  749. type = 0;
  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[4]);
  770. }
  771. }
  772. }
  773. printf("\n");
  774. printf(" %-42s [raw hardware event descriptor]\n",
  775. "rNNN");
  776. printf("\n");
  777. printf(" %-42s [hardware breakpoint]\n", "mem:<addr>[:access]");
  778. printf("\n");
  779. print_tracepoint_events();
  780. exit(129);
  781. }