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