parse-events.c 24 KB

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