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