parse-events.c 24 KB

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