parse-events.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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, exclude_GH = 0;
  614. int eu = 0, ek = 0, eh = 0, eH = 0, eG = 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 == 'G') {
  635. if (!exclude_GH)
  636. exclude_GH = eG = eH = 1;
  637. eG = 0;
  638. } else if (*str == 'H') {
  639. if (!exclude_GH)
  640. exclude_GH = eG = eH = 1;
  641. eH = 0;
  642. } else if (*str == 'p') {
  643. precise++;
  644. } else
  645. break;
  646. ++str;
  647. }
  648. if (str < *strp + 2)
  649. return -1;
  650. *strp = str;
  651. attr->exclude_user = eu;
  652. attr->exclude_kernel = ek;
  653. attr->exclude_hv = eh;
  654. attr->precise_ip = precise;
  655. attr->exclude_host = eH;
  656. attr->exclude_guest = eG;
  657. return 0;
  658. }
  659. /*
  660. * Each event can have multiple symbolic names.
  661. * Symbolic names are (almost) exactly matched.
  662. */
  663. static enum event_result
  664. parse_event_symbols(struct perf_evlist *evlist, const char **str,
  665. struct perf_event_attr *attr)
  666. {
  667. enum event_result ret;
  668. ret = parse_tracepoint_event(evlist, str, attr);
  669. if (ret != EVT_FAILED)
  670. goto modifier;
  671. ret = parse_raw_event(str, attr);
  672. if (ret != EVT_FAILED)
  673. goto modifier;
  674. ret = parse_numeric_event(str, attr);
  675. if (ret != EVT_FAILED)
  676. goto modifier;
  677. ret = parse_symbolic_event(str, attr);
  678. if (ret != EVT_FAILED)
  679. goto modifier;
  680. ret = parse_generic_hw_event(str, attr);
  681. if (ret != EVT_FAILED)
  682. goto modifier;
  683. ret = parse_breakpoint_event(str, attr);
  684. if (ret != EVT_FAILED)
  685. goto modifier;
  686. fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
  687. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  688. return EVT_FAILED;
  689. modifier:
  690. if (parse_event_modifier(str, attr) < 0) {
  691. fprintf(stderr, "invalid event modifier: '%s'\n", *str);
  692. fprintf(stderr, "Run 'perf list' for a list of valid events and modifiers\n");
  693. return EVT_FAILED;
  694. }
  695. return ret;
  696. }
  697. int parse_events(struct perf_evlist *evlist , const char *str, int unset __used)
  698. {
  699. struct perf_event_attr attr;
  700. enum event_result ret;
  701. const char *ostr;
  702. for (;;) {
  703. ostr = str;
  704. memset(&attr, 0, sizeof(attr));
  705. event_attr_init(&attr);
  706. ret = parse_event_symbols(evlist, &str, &attr);
  707. if (ret == EVT_FAILED)
  708. return -1;
  709. if (!(*str == 0 || *str == ',' || isspace(*str)))
  710. return -1;
  711. if (ret != EVT_HANDLED_ALL) {
  712. struct perf_evsel *evsel;
  713. evsel = perf_evsel__new(&attr, evlist->nr_entries);
  714. if (evsel == NULL)
  715. return -1;
  716. perf_evlist__add(evlist, evsel);
  717. evsel->name = calloc(str - ostr + 1, 1);
  718. if (!evsel->name)
  719. return -1;
  720. strncpy(evsel->name, ostr, str - ostr);
  721. }
  722. if (*str == 0)
  723. break;
  724. if (*str == ',')
  725. ++str;
  726. while (isspace(*str))
  727. ++str;
  728. }
  729. return 0;
  730. }
  731. int parse_events_option(const struct option *opt, const char *str,
  732. int unset __used)
  733. {
  734. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  735. return parse_events(evlist, str, unset);
  736. }
  737. int parse_filter(const struct option *opt, const char *str,
  738. int unset __used)
  739. {
  740. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  741. struct perf_evsel *last = NULL;
  742. if (evlist->nr_entries > 0)
  743. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  744. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  745. fprintf(stderr,
  746. "-F option should follow a -e tracepoint option\n");
  747. return -1;
  748. }
  749. last->filter = strdup(str);
  750. if (last->filter == NULL) {
  751. fprintf(stderr, "not enough memory to hold filter string\n");
  752. return -1;
  753. }
  754. return 0;
  755. }
  756. static const char * const event_type_descriptors[] = {
  757. "Hardware event",
  758. "Software event",
  759. "Tracepoint event",
  760. "Hardware cache event",
  761. "Raw hardware event descriptor",
  762. "Hardware breakpoint",
  763. };
  764. /*
  765. * Print the events from <debugfs_mount_point>/tracing/events
  766. */
  767. void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
  768. {
  769. DIR *sys_dir, *evt_dir;
  770. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  771. char evt_path[MAXPATHLEN];
  772. char dir_path[MAXPATHLEN];
  773. if (debugfs_valid_mountpoint(tracing_events_path))
  774. return;
  775. sys_dir = opendir(tracing_events_path);
  776. if (!sys_dir)
  777. return;
  778. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  779. if (subsys_glob != NULL &&
  780. !strglobmatch(sys_dirent.d_name, subsys_glob))
  781. continue;
  782. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  783. sys_dirent.d_name);
  784. evt_dir = opendir(dir_path);
  785. if (!evt_dir)
  786. continue;
  787. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  788. if (event_glob != NULL &&
  789. !strglobmatch(evt_dirent.d_name, event_glob))
  790. continue;
  791. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  792. sys_dirent.d_name, evt_dirent.d_name);
  793. printf(" %-50s [%s]\n", evt_path,
  794. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  795. }
  796. closedir(evt_dir);
  797. }
  798. closedir(sys_dir);
  799. }
  800. /*
  801. * Check whether event is in <debugfs_mount_point>/tracing/events
  802. */
  803. int is_valid_tracepoint(const char *event_string)
  804. {
  805. DIR *sys_dir, *evt_dir;
  806. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  807. char evt_path[MAXPATHLEN];
  808. char dir_path[MAXPATHLEN];
  809. if (debugfs_valid_mountpoint(tracing_events_path))
  810. return 0;
  811. sys_dir = opendir(tracing_events_path);
  812. if (!sys_dir)
  813. return 0;
  814. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  815. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  816. sys_dirent.d_name);
  817. evt_dir = opendir(dir_path);
  818. if (!evt_dir)
  819. continue;
  820. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  821. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  822. sys_dirent.d_name, evt_dirent.d_name);
  823. if (!strcmp(evt_path, event_string)) {
  824. closedir(evt_dir);
  825. closedir(sys_dir);
  826. return 1;
  827. }
  828. }
  829. closedir(evt_dir);
  830. }
  831. closedir(sys_dir);
  832. return 0;
  833. }
  834. void print_events_type(u8 type)
  835. {
  836. struct event_symbol *syms = event_symbols;
  837. unsigned int i;
  838. char name[64];
  839. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  840. if (type != syms->type)
  841. continue;
  842. if (strlen(syms->alias))
  843. snprintf(name, sizeof(name), "%s OR %s",
  844. syms->symbol, syms->alias);
  845. else
  846. snprintf(name, sizeof(name), "%s", syms->symbol);
  847. printf(" %-50s [%s]\n", name,
  848. event_type_descriptors[type]);
  849. }
  850. }
  851. int print_hwcache_events(const char *event_glob)
  852. {
  853. unsigned int type, op, i, printed = 0;
  854. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  855. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  856. /* skip invalid cache type */
  857. if (!is_cache_op_valid(type, op))
  858. continue;
  859. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  860. char *name = event_cache_name(type, op, i);
  861. if (event_glob != NULL && !strglobmatch(name, event_glob))
  862. continue;
  863. printf(" %-50s [%s]\n", name,
  864. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  865. ++printed;
  866. }
  867. }
  868. }
  869. return printed;
  870. }
  871. #define MAX_NAME_LEN 100
  872. /*
  873. * Print the help text for the event symbols:
  874. */
  875. void print_events(const char *event_glob)
  876. {
  877. unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
  878. struct event_symbol *syms = event_symbols;
  879. char name[MAX_NAME_LEN];
  880. printf("\n");
  881. printf("List of pre-defined events (to be used in -e):\n");
  882. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  883. type = syms->type;
  884. if (type != prev_type && printed) {
  885. printf("\n");
  886. printed = 0;
  887. ntypes_printed++;
  888. }
  889. if (event_glob != NULL &&
  890. !(strglobmatch(syms->symbol, event_glob) ||
  891. (syms->alias && strglobmatch(syms->alias, event_glob))))
  892. continue;
  893. if (strlen(syms->alias))
  894. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  895. else
  896. strncpy(name, syms->symbol, MAX_NAME_LEN);
  897. printf(" %-50s [%s]\n", name,
  898. event_type_descriptors[type]);
  899. prev_type = type;
  900. ++printed;
  901. }
  902. if (ntypes_printed) {
  903. printed = 0;
  904. printf("\n");
  905. }
  906. print_hwcache_events(event_glob);
  907. if (event_glob != NULL)
  908. return;
  909. printf("\n");
  910. printf(" %-50s [%s]\n",
  911. "rNNN (see 'perf list --help' on how to encode it)",
  912. event_type_descriptors[PERF_TYPE_RAW]);
  913. printf("\n");
  914. printf(" %-50s [%s]\n",
  915. "mem:<addr>[:access]",
  916. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  917. printf("\n");
  918. print_tracepoint_events(NULL, NULL);
  919. }