parse-events.c 22 KB

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