parse-events.c 24 KB

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