parse-events.c 24 KB

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