parse-events.c 22 KB

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