parse-events.c 25 KB

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