parse-events.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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_type(int type)
  228. {
  229. switch (type) {
  230. case PERF_TYPE_HARDWARE:
  231. return "hardware";
  232. case PERF_TYPE_SOFTWARE:
  233. return "software";
  234. case PERF_TYPE_TRACEPOINT:
  235. return "tracepoint";
  236. case PERF_TYPE_HW_CACHE:
  237. return "hardware-cache";
  238. default:
  239. break;
  240. }
  241. return "unknown";
  242. }
  243. const char *event_name(struct perf_evsel *evsel)
  244. {
  245. u64 config = evsel->attr.config;
  246. int type = evsel->attr.type;
  247. if (evsel->name)
  248. return evsel->name;
  249. return __event_name(type, config);
  250. }
  251. const char *__event_name(int type, u64 config)
  252. {
  253. static char buf[32];
  254. if (type == PERF_TYPE_RAW) {
  255. sprintf(buf, "raw 0x%" PRIx64, config);
  256. return buf;
  257. }
  258. switch (type) {
  259. case PERF_TYPE_HARDWARE:
  260. if (config < PERF_COUNT_HW_MAX)
  261. return hw_event_names[config];
  262. return "unknown-hardware";
  263. case PERF_TYPE_HW_CACHE: {
  264. u8 cache_type, cache_op, cache_result;
  265. cache_type = (config >> 0) & 0xff;
  266. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  267. return "unknown-ext-hardware-cache-type";
  268. cache_op = (config >> 8) & 0xff;
  269. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  270. return "unknown-ext-hardware-cache-op";
  271. cache_result = (config >> 16) & 0xff;
  272. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  273. return "unknown-ext-hardware-cache-result";
  274. if (!is_cache_op_valid(cache_type, cache_op))
  275. return "invalid-cache";
  276. return event_cache_name(cache_type, cache_op, cache_result);
  277. }
  278. case PERF_TYPE_SOFTWARE:
  279. if (config < PERF_COUNT_SW_MAX)
  280. return sw_event_names[config];
  281. return "unknown-software";
  282. case PERF_TYPE_TRACEPOINT:
  283. return tracepoint_id_to_name(config);
  284. default:
  285. break;
  286. }
  287. return "unknown";
  288. }
  289. static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
  290. {
  291. int i, j;
  292. int n, longest = -1;
  293. for (i = 0; i < size; i++) {
  294. for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
  295. n = strlen(names[i][j]);
  296. if (n > longest && !strncasecmp(*str, names[i][j], n))
  297. longest = n;
  298. }
  299. if (longest > 0) {
  300. *str += longest;
  301. return i;
  302. }
  303. }
  304. return -1;
  305. }
  306. static enum event_result
  307. parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
  308. {
  309. const char *s = *str;
  310. int cache_type = -1, cache_op = -1, cache_result = -1;
  311. cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  312. /*
  313. * No fallback - if we cannot get a clear cache type
  314. * then bail out:
  315. */
  316. if (cache_type == -1)
  317. return EVT_FAILED;
  318. while ((cache_op == -1 || cache_result == -1) && *s == '-') {
  319. ++s;
  320. if (cache_op == -1) {
  321. cache_op = parse_aliases(&s, hw_cache_op,
  322. PERF_COUNT_HW_CACHE_OP_MAX);
  323. if (cache_op >= 0) {
  324. if (!is_cache_op_valid(cache_type, cache_op))
  325. return 0;
  326. continue;
  327. }
  328. }
  329. if (cache_result == -1) {
  330. cache_result = parse_aliases(&s, hw_cache_result,
  331. PERF_COUNT_HW_CACHE_RESULT_MAX);
  332. if (cache_result >= 0)
  333. continue;
  334. }
  335. /*
  336. * Can't parse this as a cache op or result, so back up
  337. * to the '-'.
  338. */
  339. --s;
  340. break;
  341. }
  342. /*
  343. * Fall back to reads:
  344. */
  345. if (cache_op == -1)
  346. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  347. /*
  348. * Fall back to accesses:
  349. */
  350. if (cache_result == -1)
  351. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  352. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  353. attr->type = PERF_TYPE_HW_CACHE;
  354. *str = s;
  355. return EVT_HANDLED;
  356. }
  357. static enum event_result
  358. parse_single_tracepoint_event(char *sys_name,
  359. const char *evt_name,
  360. unsigned int evt_length,
  361. struct perf_event_attr *attr,
  362. const char **strp)
  363. {
  364. char evt_path[MAXPATHLEN];
  365. char id_buf[4];
  366. u64 id;
  367. int fd;
  368. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  369. sys_name, evt_name);
  370. fd = open(evt_path, O_RDONLY);
  371. if (fd < 0)
  372. return EVT_FAILED;
  373. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  374. close(fd);
  375. return EVT_FAILED;
  376. }
  377. close(fd);
  378. id = atoll(id_buf);
  379. attr->config = id;
  380. attr->type = PERF_TYPE_TRACEPOINT;
  381. *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
  382. attr->sample_type |= PERF_SAMPLE_RAW;
  383. attr->sample_type |= PERF_SAMPLE_TIME;
  384. attr->sample_type |= PERF_SAMPLE_CPU;
  385. attr->sample_period = 1;
  386. return EVT_HANDLED;
  387. }
  388. /* sys + ':' + event + ':' + flags*/
  389. #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
  390. static enum event_result
  391. parse_multiple_tracepoint_event(const struct option *opt, char *sys_name,
  392. const char *evt_exp, char *flags)
  393. {
  394. char evt_path[MAXPATHLEN];
  395. struct dirent *evt_ent;
  396. DIR *evt_dir;
  397. snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
  398. evt_dir = opendir(evt_path);
  399. if (!evt_dir) {
  400. perror("Can't open event dir");
  401. return EVT_FAILED;
  402. }
  403. while ((evt_ent = readdir(evt_dir))) {
  404. char event_opt[MAX_EVOPT_LEN + 1];
  405. int len;
  406. if (!strcmp(evt_ent->d_name, ".")
  407. || !strcmp(evt_ent->d_name, "..")
  408. || !strcmp(evt_ent->d_name, "enable")
  409. || !strcmp(evt_ent->d_name, "filter"))
  410. continue;
  411. if (!strglobmatch(evt_ent->d_name, evt_exp))
  412. continue;
  413. len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
  414. evt_ent->d_name, flags ? ":" : "",
  415. flags ?: "");
  416. if (len < 0)
  417. return EVT_FAILED;
  418. if (parse_events(opt, event_opt, 0))
  419. return EVT_FAILED;
  420. }
  421. return EVT_HANDLED_ALL;
  422. }
  423. static enum event_result
  424. parse_tracepoint_event(const struct option *opt, const char **strp,
  425. struct perf_event_attr *attr)
  426. {
  427. const char *evt_name;
  428. char *flags = NULL, *comma_loc;
  429. char sys_name[MAX_EVENT_LENGTH];
  430. unsigned int sys_length, evt_length;
  431. if (debugfs_valid_mountpoint(debugfs_path))
  432. return 0;
  433. evt_name = strchr(*strp, ':');
  434. if (!evt_name)
  435. return EVT_FAILED;
  436. sys_length = evt_name - *strp;
  437. if (sys_length >= MAX_EVENT_LENGTH)
  438. return 0;
  439. strncpy(sys_name, *strp, sys_length);
  440. sys_name[sys_length] = '\0';
  441. evt_name = evt_name + 1;
  442. comma_loc = strchr(evt_name, ',');
  443. if (comma_loc) {
  444. /* take the event name up to the comma */
  445. evt_name = strndup(evt_name, comma_loc - evt_name);
  446. }
  447. flags = strchr(evt_name, ':');
  448. if (flags) {
  449. /* split it out: */
  450. evt_name = strndup(evt_name, flags - evt_name);
  451. flags++;
  452. }
  453. evt_length = strlen(evt_name);
  454. if (evt_length >= MAX_EVENT_LENGTH)
  455. return EVT_FAILED;
  456. if (strpbrk(evt_name, "*?")) {
  457. *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
  458. return parse_multiple_tracepoint_event(opt, sys_name, evt_name,
  459. flags);
  460. } else {
  461. return parse_single_tracepoint_event(sys_name, evt_name,
  462. evt_length, attr, strp);
  463. }
  464. }
  465. static enum event_result
  466. parse_breakpoint_type(const char *type, const char **strp,
  467. struct perf_event_attr *attr)
  468. {
  469. int i;
  470. for (i = 0; i < 3; i++) {
  471. if (!type[i])
  472. break;
  473. switch (type[i]) {
  474. case 'r':
  475. attr->bp_type |= HW_BREAKPOINT_R;
  476. break;
  477. case 'w':
  478. attr->bp_type |= HW_BREAKPOINT_W;
  479. break;
  480. case 'x':
  481. attr->bp_type |= HW_BREAKPOINT_X;
  482. break;
  483. default:
  484. return EVT_FAILED;
  485. }
  486. }
  487. if (!attr->bp_type) /* Default */
  488. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  489. *strp = type + i;
  490. return EVT_HANDLED;
  491. }
  492. static enum event_result
  493. parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
  494. {
  495. const char *target;
  496. const char *type;
  497. char *endaddr;
  498. u64 addr;
  499. enum event_result err;
  500. target = strchr(*strp, ':');
  501. if (!target)
  502. return EVT_FAILED;
  503. if (strncmp(*strp, "mem", target - *strp) != 0)
  504. return EVT_FAILED;
  505. target++;
  506. addr = strtoull(target, &endaddr, 0);
  507. if (target == endaddr)
  508. return EVT_FAILED;
  509. attr->bp_addr = addr;
  510. *strp = endaddr;
  511. type = strchr(target, ':');
  512. /* If no type is defined, just rw as default */
  513. if (!type) {
  514. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  515. } else {
  516. err = parse_breakpoint_type(++type, strp, attr);
  517. if (err == EVT_FAILED)
  518. return EVT_FAILED;
  519. }
  520. /*
  521. * We should find a nice way to override the access length
  522. * Provide some defaults for now
  523. */
  524. if (attr->bp_type == HW_BREAKPOINT_X)
  525. attr->bp_len = sizeof(long);
  526. else
  527. attr->bp_len = HW_BREAKPOINT_LEN_4;
  528. attr->type = PERF_TYPE_BREAKPOINT;
  529. return EVT_HANDLED;
  530. }
  531. static int check_events(const char *str, unsigned int i)
  532. {
  533. int n;
  534. n = strlen(event_symbols[i].symbol);
  535. if (!strncmp(str, event_symbols[i].symbol, n))
  536. return n;
  537. n = strlen(event_symbols[i].alias);
  538. if (n)
  539. if (!strncmp(str, event_symbols[i].alias, n))
  540. return n;
  541. return 0;
  542. }
  543. static enum event_result
  544. parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
  545. {
  546. const char *str = *strp;
  547. unsigned int i;
  548. int n;
  549. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  550. n = check_events(str, i);
  551. if (n > 0) {
  552. attr->type = event_symbols[i].type;
  553. attr->config = event_symbols[i].config;
  554. *strp = str + n;
  555. return EVT_HANDLED;
  556. }
  557. }
  558. return EVT_FAILED;
  559. }
  560. static enum event_result
  561. parse_raw_event(const char **strp, struct perf_event_attr *attr)
  562. {
  563. const char *str = *strp;
  564. u64 config;
  565. int n;
  566. if (*str != 'r')
  567. return EVT_FAILED;
  568. n = hex2u64(str + 1, &config);
  569. if (n > 0) {
  570. *strp = str + n + 1;
  571. attr->type = PERF_TYPE_RAW;
  572. attr->config = config;
  573. return EVT_HANDLED;
  574. }
  575. return EVT_FAILED;
  576. }
  577. static enum event_result
  578. parse_numeric_event(const char **strp, struct perf_event_attr *attr)
  579. {
  580. const char *str = *strp;
  581. char *endp;
  582. unsigned long type;
  583. u64 config;
  584. type = strtoul(str, &endp, 0);
  585. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  586. str = endp + 1;
  587. config = strtoul(str, &endp, 0);
  588. if (endp > str) {
  589. attr->type = type;
  590. attr->config = config;
  591. *strp = endp;
  592. return EVT_HANDLED;
  593. }
  594. }
  595. return EVT_FAILED;
  596. }
  597. static enum event_result
  598. parse_event_modifier(const char **strp, struct perf_event_attr *attr)
  599. {
  600. const char *str = *strp;
  601. int exclude = 0;
  602. int eu = 0, ek = 0, eh = 0, precise = 0;
  603. if (*str++ != ':')
  604. return 0;
  605. while (*str) {
  606. if (*str == 'u') {
  607. if (!exclude)
  608. exclude = eu = ek = eh = 1;
  609. eu = 0;
  610. } else if (*str == 'k') {
  611. if (!exclude)
  612. exclude = eu = ek = eh = 1;
  613. ek = 0;
  614. } else if (*str == 'h') {
  615. if (!exclude)
  616. exclude = eu = ek = eh = 1;
  617. eh = 0;
  618. } else if (*str == 'p') {
  619. precise++;
  620. } else
  621. break;
  622. ++str;
  623. }
  624. if (str >= *strp + 2) {
  625. *strp = str;
  626. attr->exclude_user = eu;
  627. attr->exclude_kernel = ek;
  628. attr->exclude_hv = eh;
  629. attr->precise_ip = precise;
  630. return 1;
  631. }
  632. return 0;
  633. }
  634. /*
  635. * Each event can have multiple symbolic names.
  636. * Symbolic names are (almost) exactly matched.
  637. */
  638. static enum event_result
  639. parse_event_symbols(const struct option *opt, const char **str,
  640. struct perf_event_attr *attr)
  641. {
  642. enum event_result ret;
  643. ret = parse_tracepoint_event(opt, str, attr);
  644. if (ret != EVT_FAILED)
  645. goto modifier;
  646. ret = parse_raw_event(str, attr);
  647. if (ret != EVT_FAILED)
  648. goto modifier;
  649. ret = parse_numeric_event(str, attr);
  650. if (ret != EVT_FAILED)
  651. goto modifier;
  652. ret = parse_symbolic_event(str, attr);
  653. if (ret != EVT_FAILED)
  654. goto modifier;
  655. ret = parse_generic_hw_event(str, attr);
  656. if (ret != EVT_FAILED)
  657. goto modifier;
  658. ret = parse_breakpoint_event(str, attr);
  659. if (ret != EVT_FAILED)
  660. goto modifier;
  661. fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
  662. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  663. return EVT_FAILED;
  664. modifier:
  665. parse_event_modifier(str, attr);
  666. return ret;
  667. }
  668. int parse_events(const struct option *opt, const char *str, int unset __used)
  669. {
  670. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  671. struct perf_event_attr attr;
  672. enum event_result ret;
  673. const char *ostr;
  674. for (;;) {
  675. ostr = str;
  676. memset(&attr, 0, sizeof(attr));
  677. ret = parse_event_symbols(opt, &str, &attr);
  678. if (ret == EVT_FAILED)
  679. return -1;
  680. if (!(*str == 0 || *str == ',' || isspace(*str)))
  681. return -1;
  682. if (ret != EVT_HANDLED_ALL) {
  683. struct perf_evsel *evsel;
  684. evsel = perf_evsel__new(&attr, evlist->nr_entries);
  685. if (evsel == NULL)
  686. return -1;
  687. perf_evlist__add(evlist, evsel);
  688. evsel->name = calloc(str - ostr + 1, 1);
  689. if (!evsel->name)
  690. return -1;
  691. strncpy(evsel->name, ostr, str - ostr);
  692. }
  693. if (*str == 0)
  694. break;
  695. if (*str == ',')
  696. ++str;
  697. while (isspace(*str))
  698. ++str;
  699. }
  700. return 0;
  701. }
  702. int parse_filter(const struct option *opt, const char *str,
  703. int unset __used)
  704. {
  705. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  706. struct perf_evsel *last = NULL;
  707. if (evlist->nr_entries > 0)
  708. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  709. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  710. fprintf(stderr,
  711. "-F option should follow a -e tracepoint option\n");
  712. return -1;
  713. }
  714. last->filter = strdup(str);
  715. if (last->filter == NULL) {
  716. fprintf(stderr, "not enough memory to hold filter string\n");
  717. return -1;
  718. }
  719. return 0;
  720. }
  721. static const char * const event_type_descriptors[] = {
  722. "Hardware event",
  723. "Software event",
  724. "Tracepoint event",
  725. "Hardware cache event",
  726. "Raw hardware event descriptor",
  727. "Hardware breakpoint",
  728. };
  729. /*
  730. * Print the events from <debugfs_mount_point>/tracing/events
  731. */
  732. void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
  733. {
  734. DIR *sys_dir, *evt_dir;
  735. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  736. char evt_path[MAXPATHLEN];
  737. char dir_path[MAXPATHLEN];
  738. if (debugfs_valid_mountpoint(debugfs_path))
  739. return;
  740. sys_dir = opendir(debugfs_path);
  741. if (!sys_dir)
  742. return;
  743. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  744. if (subsys_glob != NULL &&
  745. !strglobmatch(sys_dirent.d_name, subsys_glob))
  746. continue;
  747. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  748. sys_dirent.d_name);
  749. evt_dir = opendir(dir_path);
  750. if (!evt_dir)
  751. continue;
  752. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  753. if (event_glob != NULL &&
  754. !strglobmatch(evt_dirent.d_name, event_glob))
  755. continue;
  756. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  757. sys_dirent.d_name, evt_dirent.d_name);
  758. printf(" %-42s [%s]\n", evt_path,
  759. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  760. }
  761. closedir(evt_dir);
  762. }
  763. closedir(sys_dir);
  764. }
  765. /*
  766. * Check whether event is in <debugfs_mount_point>/tracing/events
  767. */
  768. int is_valid_tracepoint(const char *event_string)
  769. {
  770. DIR *sys_dir, *evt_dir;
  771. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  772. char evt_path[MAXPATHLEN];
  773. char dir_path[MAXPATHLEN];
  774. if (debugfs_valid_mountpoint(debugfs_path))
  775. return 0;
  776. sys_dir = opendir(debugfs_path);
  777. if (!sys_dir)
  778. return 0;
  779. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  780. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  781. sys_dirent.d_name);
  782. evt_dir = opendir(dir_path);
  783. if (!evt_dir)
  784. continue;
  785. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  786. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  787. sys_dirent.d_name, evt_dirent.d_name);
  788. if (!strcmp(evt_path, event_string)) {
  789. closedir(evt_dir);
  790. closedir(sys_dir);
  791. return 1;
  792. }
  793. }
  794. closedir(evt_dir);
  795. }
  796. closedir(sys_dir);
  797. return 0;
  798. }
  799. void print_events_type(u8 type)
  800. {
  801. struct event_symbol *syms = event_symbols;
  802. unsigned int i;
  803. char name[64];
  804. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  805. if (type != syms->type)
  806. continue;
  807. if (strlen(syms->alias))
  808. snprintf(name, sizeof(name), "%s OR %s",
  809. syms->symbol, syms->alias);
  810. else
  811. snprintf(name, sizeof(name), "%s", syms->symbol);
  812. printf(" %-42s [%s]\n", name,
  813. event_type_descriptors[type]);
  814. }
  815. }
  816. int print_hwcache_events(const char *event_glob)
  817. {
  818. unsigned int type, op, i, printed = 0;
  819. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  820. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  821. /* skip invalid cache type */
  822. if (!is_cache_op_valid(type, op))
  823. continue;
  824. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  825. char *name = event_cache_name(type, op, i);
  826. if (event_glob != NULL &&
  827. !strglobmatch(name, event_glob))
  828. continue;
  829. printf(" %-42s [%s]\n", name,
  830. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  831. ++printed;
  832. }
  833. }
  834. }
  835. return printed;
  836. }
  837. /*
  838. * Print the help text for the event symbols:
  839. */
  840. void print_events(const char *event_glob)
  841. {
  842. struct event_symbol *syms = event_symbols;
  843. unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
  844. char name[40];
  845. printf("\n");
  846. printf("List of pre-defined events (to be used in -e):\n");
  847. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  848. type = syms->type;
  849. if (type != prev_type && printed) {
  850. printf("\n");
  851. printed = 0;
  852. ntypes_printed++;
  853. }
  854. if (event_glob != NULL &&
  855. !(strglobmatch(syms->symbol, event_glob) ||
  856. (syms->alias && strglobmatch(syms->alias, event_glob))))
  857. continue;
  858. if (strlen(syms->alias))
  859. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  860. else
  861. strcpy(name, syms->symbol);
  862. printf(" %-42s [%s]\n", name,
  863. event_type_descriptors[type]);
  864. prev_type = type;
  865. ++printed;
  866. }
  867. if (ntypes_printed) {
  868. printed = 0;
  869. printf("\n");
  870. }
  871. print_hwcache_events(event_glob);
  872. if (event_glob != NULL)
  873. return;
  874. printf("\n");
  875. printf(" %-42s [%s]\n",
  876. "rNNN (see 'perf list --help' on how to encode it)",
  877. event_type_descriptors[PERF_TYPE_RAW]);
  878. printf("\n");
  879. printf(" %-42s [%s]\n",
  880. "mem:<addr>[:access]",
  881. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  882. printf("\n");
  883. print_tracepoint_events(NULL, NULL);
  884. exit(129);
  885. }