parse-events.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. #include "parse-events-flex.h"
  15. #include "pmu.h"
  16. #define MAX_NAME_LEN 100
  17. struct event_symbol {
  18. u8 type;
  19. u64 config;
  20. const char *symbol;
  21. const char *alias;
  22. };
  23. #ifdef PARSER_DEBUG
  24. extern int parse_events_debug;
  25. #endif
  26. int parse_events_parse(struct list_head *list, int *idx);
  27. #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
  28. #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
  29. static struct event_symbol event_symbols[] = {
  30. { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
  31. { CHW(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
  32. { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
  33. { CHW(INSTRUCTIONS), "instructions", "" },
  34. { CHW(CACHE_REFERENCES), "cache-references", "" },
  35. { CHW(CACHE_MISSES), "cache-misses", "" },
  36. { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
  37. { CHW(BRANCH_MISSES), "branch-misses", "" },
  38. { CHW(BUS_CYCLES), "bus-cycles", "" },
  39. { CHW(REF_CPU_CYCLES), "ref-cycles", "" },
  40. { CSW(CPU_CLOCK), "cpu-clock", "" },
  41. { CSW(TASK_CLOCK), "task-clock", "" },
  42. { CSW(PAGE_FAULTS), "page-faults", "faults" },
  43. { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
  44. { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
  45. { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
  46. { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
  47. { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
  48. { CSW(EMULATION_FAULTS), "emulation-faults", "" },
  49. };
  50. #define __PERF_EVENT_FIELD(config, name) \
  51. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  52. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  53. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  54. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  55. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  56. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  57. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  58. if (sys_dirent.d_type == DT_DIR && \
  59. (strcmp(sys_dirent.d_name, ".")) && \
  60. (strcmp(sys_dirent.d_name, "..")))
  61. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  62. {
  63. char evt_path[MAXPATHLEN];
  64. int fd;
  65. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  66. sys_dir->d_name, evt_dir->d_name);
  67. fd = open(evt_path, O_RDONLY);
  68. if (fd < 0)
  69. return -EINVAL;
  70. close(fd);
  71. return 0;
  72. }
  73. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  74. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  75. if (evt_dirent.d_type == DT_DIR && \
  76. (strcmp(evt_dirent.d_name, ".")) && \
  77. (strcmp(evt_dirent.d_name, "..")) && \
  78. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  79. #define MAX_EVENT_LENGTH 512
  80. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  81. {
  82. struct tracepoint_path *path = NULL;
  83. DIR *sys_dir, *evt_dir;
  84. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  85. char id_buf[24];
  86. int fd;
  87. u64 id;
  88. char evt_path[MAXPATHLEN];
  89. char dir_path[MAXPATHLEN];
  90. if (debugfs_valid_mountpoint(tracing_events_path))
  91. return NULL;
  92. sys_dir = opendir(tracing_events_path);
  93. if (!sys_dir)
  94. return NULL;
  95. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  96. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  97. sys_dirent.d_name);
  98. evt_dir = opendir(dir_path);
  99. if (!evt_dir)
  100. continue;
  101. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  102. snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
  103. evt_dirent.d_name);
  104. fd = open(evt_path, O_RDONLY);
  105. if (fd < 0)
  106. continue;
  107. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  108. close(fd);
  109. continue;
  110. }
  111. close(fd);
  112. id = atoll(id_buf);
  113. if (id == config) {
  114. closedir(evt_dir);
  115. closedir(sys_dir);
  116. path = zalloc(sizeof(*path));
  117. path->system = malloc(MAX_EVENT_LENGTH);
  118. if (!path->system) {
  119. free(path);
  120. return NULL;
  121. }
  122. path->name = malloc(MAX_EVENT_LENGTH);
  123. if (!path->name) {
  124. free(path->system);
  125. free(path);
  126. return NULL;
  127. }
  128. strncpy(path->system, sys_dirent.d_name,
  129. MAX_EVENT_LENGTH);
  130. strncpy(path->name, evt_dirent.d_name,
  131. MAX_EVENT_LENGTH);
  132. return path;
  133. }
  134. }
  135. closedir(evt_dir);
  136. }
  137. closedir(sys_dir);
  138. return NULL;
  139. }
  140. #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
  141. static const char *tracepoint_id_to_name(u64 config)
  142. {
  143. static char buf[TP_PATH_LEN];
  144. struct tracepoint_path *path;
  145. path = tracepoint_id_to_path(config);
  146. if (path) {
  147. snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
  148. free(path->name);
  149. free(path->system);
  150. free(path);
  151. } else
  152. snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
  153. return buf;
  154. }
  155. const char *event_type(int type)
  156. {
  157. switch (type) {
  158. case PERF_TYPE_HARDWARE:
  159. return "hardware";
  160. case PERF_TYPE_SOFTWARE:
  161. return "software";
  162. case PERF_TYPE_TRACEPOINT:
  163. return "tracepoint";
  164. case PERF_TYPE_HW_CACHE:
  165. return "hardware-cache";
  166. default:
  167. break;
  168. }
  169. return "unknown";
  170. }
  171. const char *event_name(struct perf_evsel *evsel)
  172. {
  173. static char bf[128];
  174. perf_evsel__name(evsel, bf, sizeof(bf));
  175. return bf;
  176. }
  177. const char *__event_name(int type, u64 config)
  178. {
  179. static char buf[32];
  180. if (type == PERF_TYPE_RAW) {
  181. sprintf(buf, "raw 0x%" PRIx64, config);
  182. return buf;
  183. }
  184. switch (type) {
  185. case PERF_TYPE_HARDWARE:
  186. return __perf_evsel__hw_name(config);
  187. case PERF_TYPE_HW_CACHE:
  188. __perf_evsel__hw_cache_name(config, buf, sizeof(buf));
  189. return buf;
  190. case PERF_TYPE_SOFTWARE:
  191. return __perf_evsel__sw_name(config);
  192. case PERF_TYPE_TRACEPOINT:
  193. return tracepoint_id_to_name(config);
  194. default:
  195. break;
  196. }
  197. return "unknown";
  198. }
  199. static int add_event(struct list_head **_list, int *idx,
  200. struct perf_event_attr *attr, char *name)
  201. {
  202. struct perf_evsel *evsel;
  203. struct list_head *list = *_list;
  204. if (!list) {
  205. list = malloc(sizeof(*list));
  206. if (!list)
  207. return -ENOMEM;
  208. INIT_LIST_HEAD(list);
  209. }
  210. event_attr_init(attr);
  211. evsel = perf_evsel__new(attr, (*idx)++);
  212. if (!evsel) {
  213. free(list);
  214. return -ENOMEM;
  215. }
  216. evsel->name = strdup(name);
  217. list_add_tail(&evsel->node, list);
  218. *_list = list;
  219. return 0;
  220. }
  221. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  222. {
  223. int i, j;
  224. int n, longest = -1;
  225. for (i = 0; i < size; i++) {
  226. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  227. n = strlen(names[i][j]);
  228. if (n > longest && !strncasecmp(str, names[i][j], n))
  229. longest = n;
  230. }
  231. if (longest > 0)
  232. return i;
  233. }
  234. return -1;
  235. }
  236. int parse_events_add_cache(struct list_head **list, int *idx,
  237. char *type, char *op_result1, char *op_result2)
  238. {
  239. struct perf_event_attr attr;
  240. char name[MAX_NAME_LEN];
  241. int cache_type = -1, cache_op = -1, cache_result = -1;
  242. char *op_result[2] = { op_result1, op_result2 };
  243. int i, n;
  244. /*
  245. * No fallback - if we cannot get a clear cache type
  246. * then bail out:
  247. */
  248. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  249. PERF_COUNT_HW_CACHE_MAX);
  250. if (cache_type == -1)
  251. return -EINVAL;
  252. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  253. for (i = 0; (i < 2) && (op_result[i]); i++) {
  254. char *str = op_result[i];
  255. snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
  256. if (cache_op == -1) {
  257. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  258. PERF_COUNT_HW_CACHE_OP_MAX);
  259. if (cache_op >= 0) {
  260. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  261. return -EINVAL;
  262. continue;
  263. }
  264. }
  265. if (cache_result == -1) {
  266. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  267. PERF_COUNT_HW_CACHE_RESULT_MAX);
  268. if (cache_result >= 0)
  269. continue;
  270. }
  271. }
  272. /*
  273. * Fall back to reads:
  274. */
  275. if (cache_op == -1)
  276. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  277. /*
  278. * Fall back to accesses:
  279. */
  280. if (cache_result == -1)
  281. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  282. memset(&attr, 0, sizeof(attr));
  283. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  284. attr.type = PERF_TYPE_HW_CACHE;
  285. return add_event(list, idx, &attr, name);
  286. }
  287. static int add_tracepoint(struct list_head **list, int *idx,
  288. char *sys_name, char *evt_name)
  289. {
  290. struct perf_event_attr attr;
  291. char name[MAX_NAME_LEN];
  292. char evt_path[MAXPATHLEN];
  293. char id_buf[4];
  294. u64 id;
  295. int fd;
  296. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  297. sys_name, evt_name);
  298. fd = open(evt_path, O_RDONLY);
  299. if (fd < 0)
  300. return -1;
  301. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  302. close(fd);
  303. return -1;
  304. }
  305. close(fd);
  306. id = atoll(id_buf);
  307. memset(&attr, 0, sizeof(attr));
  308. attr.config = id;
  309. attr.type = PERF_TYPE_TRACEPOINT;
  310. attr.sample_type |= PERF_SAMPLE_RAW;
  311. attr.sample_type |= PERF_SAMPLE_TIME;
  312. attr.sample_type |= PERF_SAMPLE_CPU;
  313. attr.sample_period = 1;
  314. snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
  315. return add_event(list, idx, &attr, name);
  316. }
  317. static int add_tracepoint_multi(struct list_head **list, int *idx,
  318. char *sys_name, char *evt_name)
  319. {
  320. char evt_path[MAXPATHLEN];
  321. struct dirent *evt_ent;
  322. DIR *evt_dir;
  323. int ret = 0;
  324. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  325. evt_dir = opendir(evt_path);
  326. if (!evt_dir) {
  327. perror("Can't open event dir");
  328. return -1;
  329. }
  330. while (!ret && (evt_ent = readdir(evt_dir))) {
  331. if (!strcmp(evt_ent->d_name, ".")
  332. || !strcmp(evt_ent->d_name, "..")
  333. || !strcmp(evt_ent->d_name, "enable")
  334. || !strcmp(evt_ent->d_name, "filter"))
  335. continue;
  336. if (!strglobmatch(evt_ent->d_name, evt_name))
  337. continue;
  338. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  339. }
  340. return ret;
  341. }
  342. int parse_events_add_tracepoint(struct list_head **list, int *idx,
  343. char *sys, char *event)
  344. {
  345. int ret;
  346. ret = debugfs_valid_mountpoint(tracing_events_path);
  347. if (ret)
  348. return ret;
  349. return strpbrk(event, "*?") ?
  350. add_tracepoint_multi(list, idx, sys, event) :
  351. add_tracepoint(list, idx, sys, event);
  352. }
  353. static int
  354. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  355. {
  356. int i;
  357. for (i = 0; i < 3; i++) {
  358. if (!type || !type[i])
  359. break;
  360. switch (type[i]) {
  361. case 'r':
  362. attr->bp_type |= HW_BREAKPOINT_R;
  363. break;
  364. case 'w':
  365. attr->bp_type |= HW_BREAKPOINT_W;
  366. break;
  367. case 'x':
  368. attr->bp_type |= HW_BREAKPOINT_X;
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. }
  374. if (!attr->bp_type) /* Default */
  375. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  376. return 0;
  377. }
  378. int parse_events_add_breakpoint(struct list_head **list, int *idx,
  379. void *ptr, char *type)
  380. {
  381. struct perf_event_attr attr;
  382. char name[MAX_NAME_LEN];
  383. memset(&attr, 0, sizeof(attr));
  384. attr.bp_addr = (unsigned long) ptr;
  385. if (parse_breakpoint_type(type, &attr))
  386. return -EINVAL;
  387. /*
  388. * We should find a nice way to override the access length
  389. * Provide some defaults for now
  390. */
  391. if (attr.bp_type == HW_BREAKPOINT_X)
  392. attr.bp_len = sizeof(long);
  393. else
  394. attr.bp_len = HW_BREAKPOINT_LEN_4;
  395. attr.type = PERF_TYPE_BREAKPOINT;
  396. snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
  397. return add_event(list, idx, &attr, name);
  398. }
  399. static int config_term(struct perf_event_attr *attr,
  400. struct parse_events__term *term)
  401. {
  402. #define CHECK_TYPE_VAL(type) \
  403. do { \
  404. if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
  405. return -EINVAL; \
  406. } while (0)
  407. switch (term->type_term) {
  408. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  409. CHECK_TYPE_VAL(NUM);
  410. attr->config = term->val.num;
  411. break;
  412. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  413. CHECK_TYPE_VAL(NUM);
  414. attr->config1 = term->val.num;
  415. break;
  416. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  417. CHECK_TYPE_VAL(NUM);
  418. attr->config2 = term->val.num;
  419. break;
  420. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  421. CHECK_TYPE_VAL(NUM);
  422. attr->sample_period = term->val.num;
  423. break;
  424. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  425. /*
  426. * TODO uncomment when the field is available
  427. * attr->branch_sample_type = term->val.num;
  428. */
  429. break;
  430. case PARSE_EVENTS__TERM_TYPE_NAME:
  431. CHECK_TYPE_VAL(STR);
  432. break;
  433. default:
  434. return -EINVAL;
  435. }
  436. return 0;
  437. #undef CHECK_TYPE_VAL
  438. }
  439. static int config_attr(struct perf_event_attr *attr,
  440. struct list_head *head, int fail)
  441. {
  442. struct parse_events__term *term;
  443. list_for_each_entry(term, head, list)
  444. if (config_term(attr, term) && fail)
  445. return -EINVAL;
  446. return 0;
  447. }
  448. int parse_events_add_numeric(struct list_head **list, int *idx,
  449. unsigned long type, unsigned long config,
  450. struct list_head *head_config)
  451. {
  452. struct perf_event_attr attr;
  453. memset(&attr, 0, sizeof(attr));
  454. attr.type = type;
  455. attr.config = config;
  456. if (head_config &&
  457. config_attr(&attr, head_config, 1))
  458. return -EINVAL;
  459. return add_event(list, idx, &attr,
  460. (char *) __event_name(type, config));
  461. }
  462. static int parse_events__is_name_term(struct parse_events__term *term)
  463. {
  464. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  465. }
  466. static char *pmu_event_name(struct perf_event_attr *attr,
  467. struct list_head *head_terms)
  468. {
  469. struct parse_events__term *term;
  470. list_for_each_entry(term, head_terms, list)
  471. if (parse_events__is_name_term(term))
  472. return term->val.str;
  473. return (char *) __event_name(PERF_TYPE_RAW, attr->config);
  474. }
  475. int parse_events_add_pmu(struct list_head **list, int *idx,
  476. char *name, struct list_head *head_config)
  477. {
  478. struct perf_event_attr attr;
  479. struct perf_pmu *pmu;
  480. pmu = perf_pmu__find(name);
  481. if (!pmu)
  482. return -EINVAL;
  483. memset(&attr, 0, sizeof(attr));
  484. /*
  485. * Configure hardcoded terms first, no need to check
  486. * return value when called with fail == 0 ;)
  487. */
  488. config_attr(&attr, head_config, 0);
  489. if (perf_pmu__config(pmu, &attr, head_config))
  490. return -EINVAL;
  491. return add_event(list, idx, &attr,
  492. pmu_event_name(&attr, head_config));
  493. }
  494. void parse_events_update_lists(struct list_head *list_event,
  495. struct list_head *list_all)
  496. {
  497. /*
  498. * Called for single event definition. Update the
  499. * 'all event' list, and reinit the 'signle event'
  500. * list, for next event definition.
  501. */
  502. list_splice_tail(list_event, list_all);
  503. free(list_event);
  504. }
  505. int parse_events_modifier(struct list_head *list, char *str)
  506. {
  507. struct perf_evsel *evsel;
  508. int exclude = 0, exclude_GH = 0;
  509. int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
  510. if (str == NULL)
  511. return 0;
  512. while (*str) {
  513. if (*str == 'u') {
  514. if (!exclude)
  515. exclude = eu = ek = eh = 1;
  516. eu = 0;
  517. } else if (*str == 'k') {
  518. if (!exclude)
  519. exclude = eu = ek = eh = 1;
  520. ek = 0;
  521. } else if (*str == 'h') {
  522. if (!exclude)
  523. exclude = eu = ek = eh = 1;
  524. eh = 0;
  525. } else if (*str == 'G') {
  526. if (!exclude_GH)
  527. exclude_GH = eG = eH = 1;
  528. eG = 0;
  529. } else if (*str == 'H') {
  530. if (!exclude_GH)
  531. exclude_GH = eG = eH = 1;
  532. eH = 0;
  533. } else if (*str == 'p') {
  534. precise++;
  535. } else
  536. break;
  537. ++str;
  538. }
  539. /*
  540. * precise ip:
  541. *
  542. * 0 - SAMPLE_IP can have arbitrary skid
  543. * 1 - SAMPLE_IP must have constant skid
  544. * 2 - SAMPLE_IP requested to have 0 skid
  545. * 3 - SAMPLE_IP must have 0 skid
  546. *
  547. * See also PERF_RECORD_MISC_EXACT_IP
  548. */
  549. if (precise > 3)
  550. return -EINVAL;
  551. list_for_each_entry(evsel, list, node) {
  552. evsel->attr.exclude_user = eu;
  553. evsel->attr.exclude_kernel = ek;
  554. evsel->attr.exclude_hv = eh;
  555. evsel->attr.precise_ip = precise;
  556. evsel->attr.exclude_host = eH;
  557. evsel->attr.exclude_guest = eG;
  558. }
  559. return 0;
  560. }
  561. int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
  562. {
  563. LIST_HEAD(list);
  564. LIST_HEAD(list_tmp);
  565. YY_BUFFER_STATE buffer;
  566. int ret, idx = evlist->nr_entries;
  567. buffer = parse_events__scan_string(str);
  568. #ifdef PARSER_DEBUG
  569. parse_events_debug = 1;
  570. #endif
  571. ret = parse_events_parse(&list, &idx);
  572. parse_events__flush_buffer(buffer);
  573. parse_events__delete_buffer(buffer);
  574. parse_events_lex_destroy();
  575. if (!ret) {
  576. int entries = idx - evlist->nr_entries;
  577. perf_evlist__splice_list_tail(evlist, &list, entries);
  578. return 0;
  579. }
  580. /*
  581. * There are 2 users - builtin-record and builtin-test objects.
  582. * Both call perf_evlist__delete in case of error, so we dont
  583. * need to bother.
  584. */
  585. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  586. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  587. return ret;
  588. }
  589. int parse_events_option(const struct option *opt, const char *str,
  590. int unset __used)
  591. {
  592. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  593. return parse_events(evlist, str, unset);
  594. }
  595. int parse_filter(const struct option *opt, const char *str,
  596. int unset __used)
  597. {
  598. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  599. struct perf_evsel *last = NULL;
  600. if (evlist->nr_entries > 0)
  601. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  602. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  603. fprintf(stderr,
  604. "-F option should follow a -e tracepoint option\n");
  605. return -1;
  606. }
  607. last->filter = strdup(str);
  608. if (last->filter == NULL) {
  609. fprintf(stderr, "not enough memory to hold filter string\n");
  610. return -1;
  611. }
  612. return 0;
  613. }
  614. static const char * const event_type_descriptors[] = {
  615. "Hardware event",
  616. "Software event",
  617. "Tracepoint event",
  618. "Hardware cache event",
  619. "Raw hardware event descriptor",
  620. "Hardware breakpoint",
  621. };
  622. /*
  623. * Print the events from <debugfs_mount_point>/tracing/events
  624. */
  625. void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
  626. {
  627. DIR *sys_dir, *evt_dir;
  628. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  629. char evt_path[MAXPATHLEN];
  630. char dir_path[MAXPATHLEN];
  631. if (debugfs_valid_mountpoint(tracing_events_path))
  632. return;
  633. sys_dir = opendir(tracing_events_path);
  634. if (!sys_dir)
  635. return;
  636. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  637. if (subsys_glob != NULL &&
  638. !strglobmatch(sys_dirent.d_name, subsys_glob))
  639. continue;
  640. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  641. sys_dirent.d_name);
  642. evt_dir = opendir(dir_path);
  643. if (!evt_dir)
  644. continue;
  645. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  646. if (event_glob != NULL &&
  647. !strglobmatch(evt_dirent.d_name, event_glob))
  648. continue;
  649. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  650. sys_dirent.d_name, evt_dirent.d_name);
  651. printf(" %-50s [%s]\n", evt_path,
  652. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  653. }
  654. closedir(evt_dir);
  655. }
  656. closedir(sys_dir);
  657. }
  658. /*
  659. * Check whether event is in <debugfs_mount_point>/tracing/events
  660. */
  661. int is_valid_tracepoint(const char *event_string)
  662. {
  663. DIR *sys_dir, *evt_dir;
  664. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  665. char evt_path[MAXPATHLEN];
  666. char dir_path[MAXPATHLEN];
  667. if (debugfs_valid_mountpoint(tracing_events_path))
  668. return 0;
  669. sys_dir = opendir(tracing_events_path);
  670. if (!sys_dir)
  671. return 0;
  672. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  673. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  674. sys_dirent.d_name);
  675. evt_dir = opendir(dir_path);
  676. if (!evt_dir)
  677. continue;
  678. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  679. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  680. sys_dirent.d_name, evt_dirent.d_name);
  681. if (!strcmp(evt_path, event_string)) {
  682. closedir(evt_dir);
  683. closedir(sys_dir);
  684. return 1;
  685. }
  686. }
  687. closedir(evt_dir);
  688. }
  689. closedir(sys_dir);
  690. return 0;
  691. }
  692. void print_events_type(u8 type)
  693. {
  694. struct event_symbol *syms = event_symbols;
  695. unsigned int i;
  696. char name[64];
  697. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  698. if (type != syms->type)
  699. continue;
  700. if (strlen(syms->alias))
  701. snprintf(name, sizeof(name), "%s OR %s",
  702. syms->symbol, syms->alias);
  703. else
  704. snprintf(name, sizeof(name), "%s", syms->symbol);
  705. printf(" %-50s [%s]\n", name,
  706. event_type_descriptors[type]);
  707. }
  708. }
  709. int print_hwcache_events(const char *event_glob)
  710. {
  711. unsigned int type, op, i, printed = 0;
  712. char name[64];
  713. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  714. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  715. /* skip invalid cache type */
  716. if (!perf_evsel__is_cache_op_valid(type, op))
  717. continue;
  718. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  719. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  720. name, sizeof(name));
  721. if (event_glob != NULL && !strglobmatch(name, event_glob))
  722. continue;
  723. printf(" %-50s [%s]\n", name,
  724. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  725. ++printed;
  726. }
  727. }
  728. }
  729. return printed;
  730. }
  731. /*
  732. * Print the help text for the event symbols:
  733. */
  734. void print_events(const char *event_glob)
  735. {
  736. unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
  737. struct event_symbol *syms = event_symbols;
  738. char name[MAX_NAME_LEN];
  739. printf("\n");
  740. printf("List of pre-defined events (to be used in -e):\n");
  741. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  742. type = syms->type;
  743. if (type != prev_type && printed) {
  744. printf("\n");
  745. printed = 0;
  746. ntypes_printed++;
  747. }
  748. if (event_glob != NULL &&
  749. !(strglobmatch(syms->symbol, event_glob) ||
  750. (syms->alias && strglobmatch(syms->alias, event_glob))))
  751. continue;
  752. if (strlen(syms->alias))
  753. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  754. else
  755. strncpy(name, syms->symbol, MAX_NAME_LEN);
  756. printf(" %-50s [%s]\n", name,
  757. event_type_descriptors[type]);
  758. prev_type = type;
  759. ++printed;
  760. }
  761. if (ntypes_printed) {
  762. printed = 0;
  763. printf("\n");
  764. }
  765. print_hwcache_events(event_glob);
  766. if (event_glob != NULL)
  767. return;
  768. printf("\n");
  769. printf(" %-50s [%s]\n",
  770. "rNNN",
  771. event_type_descriptors[PERF_TYPE_RAW]);
  772. printf(" %-50s [%s]\n",
  773. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  774. event_type_descriptors[PERF_TYPE_RAW]);
  775. printf(" (see 'perf list --help' on how to encode it)\n");
  776. printf("\n");
  777. printf(" %-50s [%s]\n",
  778. "mem:<addr>[:access]",
  779. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  780. printf("\n");
  781. print_tracepoint_events(NULL, NULL);
  782. }
  783. int parse_events__is_hardcoded_term(struct parse_events__term *term)
  784. {
  785. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  786. }
  787. static int new_term(struct parse_events__term **_term, int type_val,
  788. int type_term, char *config,
  789. char *str, long num)
  790. {
  791. struct parse_events__term *term;
  792. term = zalloc(sizeof(*term));
  793. if (!term)
  794. return -ENOMEM;
  795. INIT_LIST_HEAD(&term->list);
  796. term->type_val = type_val;
  797. term->type_term = type_term;
  798. term->config = config;
  799. switch (type_val) {
  800. case PARSE_EVENTS__TERM_TYPE_NUM:
  801. term->val.num = num;
  802. break;
  803. case PARSE_EVENTS__TERM_TYPE_STR:
  804. term->val.str = str;
  805. break;
  806. default:
  807. return -EINVAL;
  808. }
  809. *_term = term;
  810. return 0;
  811. }
  812. int parse_events__term_num(struct parse_events__term **term,
  813. int type_term, char *config, long num)
  814. {
  815. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  816. config, NULL, num);
  817. }
  818. int parse_events__term_str(struct parse_events__term **term,
  819. int type_term, char *config, char *str)
  820. {
  821. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  822. config, str, 0);
  823. }
  824. void parse_events__free_terms(struct list_head *terms)
  825. {
  826. struct parse_events__term *term, *h;
  827. list_for_each_entry_safe(term, h, terms, list)
  828. free(term);
  829. free(terms);
  830. }