parse-events.c 25 KB

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