parse-events.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. #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 "debug.h"
  14. #include <api/fs/debugfs.h>
  15. #include "parse-events-bison.h"
  16. #define YY_EXTRA_TYPE int
  17. #include "parse-events-flex.h"
  18. #include "pmu.h"
  19. #include "thread_map.h"
  20. #include "asm/bug.h"
  21. #define MAX_NAME_LEN 100
  22. #ifdef PARSER_DEBUG
  23. extern int parse_events_debug;
  24. #endif
  25. int parse_events_parse(void *data, void *scanner);
  26. static struct perf_pmu_event_symbol *perf_pmu_events_list;
  27. /*
  28. * The variable indicates the number of supported pmu event symbols.
  29. * 0 means not initialized and ready to init
  30. * -1 means failed to init, don't try anymore
  31. * >0 is the number of supported pmu event symbols
  32. */
  33. static int perf_pmu_events_list_num;
  34. struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
  35. [PERF_COUNT_HW_CPU_CYCLES] = {
  36. .symbol = "cpu-cycles",
  37. .alias = "cycles",
  38. },
  39. [PERF_COUNT_HW_INSTRUCTIONS] = {
  40. .symbol = "instructions",
  41. .alias = "",
  42. },
  43. [PERF_COUNT_HW_CACHE_REFERENCES] = {
  44. .symbol = "cache-references",
  45. .alias = "",
  46. },
  47. [PERF_COUNT_HW_CACHE_MISSES] = {
  48. .symbol = "cache-misses",
  49. .alias = "",
  50. },
  51. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
  52. .symbol = "branch-instructions",
  53. .alias = "branches",
  54. },
  55. [PERF_COUNT_HW_BRANCH_MISSES] = {
  56. .symbol = "branch-misses",
  57. .alias = "",
  58. },
  59. [PERF_COUNT_HW_BUS_CYCLES] = {
  60. .symbol = "bus-cycles",
  61. .alias = "",
  62. },
  63. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
  64. .symbol = "stalled-cycles-frontend",
  65. .alias = "idle-cycles-frontend",
  66. },
  67. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
  68. .symbol = "stalled-cycles-backend",
  69. .alias = "idle-cycles-backend",
  70. },
  71. [PERF_COUNT_HW_REF_CPU_CYCLES] = {
  72. .symbol = "ref-cycles",
  73. .alias = "",
  74. },
  75. };
  76. struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
  77. [PERF_COUNT_SW_CPU_CLOCK] = {
  78. .symbol = "cpu-clock",
  79. .alias = "",
  80. },
  81. [PERF_COUNT_SW_TASK_CLOCK] = {
  82. .symbol = "task-clock",
  83. .alias = "",
  84. },
  85. [PERF_COUNT_SW_PAGE_FAULTS] = {
  86. .symbol = "page-faults",
  87. .alias = "faults",
  88. },
  89. [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
  90. .symbol = "context-switches",
  91. .alias = "cs",
  92. },
  93. [PERF_COUNT_SW_CPU_MIGRATIONS] = {
  94. .symbol = "cpu-migrations",
  95. .alias = "migrations",
  96. },
  97. [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
  98. .symbol = "minor-faults",
  99. .alias = "",
  100. },
  101. [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
  102. .symbol = "major-faults",
  103. .alias = "",
  104. },
  105. [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
  106. .symbol = "alignment-faults",
  107. .alias = "",
  108. },
  109. [PERF_COUNT_SW_EMULATION_FAULTS] = {
  110. .symbol = "emulation-faults",
  111. .alias = "",
  112. },
  113. [PERF_COUNT_SW_DUMMY] = {
  114. .symbol = "dummy",
  115. .alias = "",
  116. },
  117. };
  118. #define __PERF_EVENT_FIELD(config, name) \
  119. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  120. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  121. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  122. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  123. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  124. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  125. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  126. if (sys_dirent.d_type == DT_DIR && \
  127. (strcmp(sys_dirent.d_name, ".")) && \
  128. (strcmp(sys_dirent.d_name, "..")))
  129. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  130. {
  131. char evt_path[MAXPATHLEN];
  132. int fd;
  133. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  134. sys_dir->d_name, evt_dir->d_name);
  135. fd = open(evt_path, O_RDONLY);
  136. if (fd < 0)
  137. return -EINVAL;
  138. close(fd);
  139. return 0;
  140. }
  141. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  142. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  143. if (evt_dirent.d_type == DT_DIR && \
  144. (strcmp(evt_dirent.d_name, ".")) && \
  145. (strcmp(evt_dirent.d_name, "..")) && \
  146. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  147. #define MAX_EVENT_LENGTH 512
  148. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  149. {
  150. struct tracepoint_path *path = NULL;
  151. DIR *sys_dir, *evt_dir;
  152. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  153. char id_buf[24];
  154. int fd;
  155. u64 id;
  156. char evt_path[MAXPATHLEN];
  157. char dir_path[MAXPATHLEN];
  158. sys_dir = opendir(tracing_events_path);
  159. if (!sys_dir)
  160. return NULL;
  161. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  162. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  163. sys_dirent.d_name);
  164. evt_dir = opendir(dir_path);
  165. if (!evt_dir)
  166. continue;
  167. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  168. snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
  169. evt_dirent.d_name);
  170. fd = open(evt_path, O_RDONLY);
  171. if (fd < 0)
  172. continue;
  173. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  174. close(fd);
  175. continue;
  176. }
  177. close(fd);
  178. id = atoll(id_buf);
  179. if (id == config) {
  180. closedir(evt_dir);
  181. closedir(sys_dir);
  182. path = zalloc(sizeof(*path));
  183. path->system = malloc(MAX_EVENT_LENGTH);
  184. if (!path->system) {
  185. free(path);
  186. return NULL;
  187. }
  188. path->name = malloc(MAX_EVENT_LENGTH);
  189. if (!path->name) {
  190. zfree(&path->system);
  191. free(path);
  192. return NULL;
  193. }
  194. strncpy(path->system, sys_dirent.d_name,
  195. MAX_EVENT_LENGTH);
  196. strncpy(path->name, evt_dirent.d_name,
  197. MAX_EVENT_LENGTH);
  198. return path;
  199. }
  200. }
  201. closedir(evt_dir);
  202. }
  203. closedir(sys_dir);
  204. return NULL;
  205. }
  206. struct tracepoint_path *tracepoint_name_to_path(const char *name)
  207. {
  208. struct tracepoint_path *path = zalloc(sizeof(*path));
  209. char *str = strchr(name, ':');
  210. if (path == NULL || str == NULL) {
  211. free(path);
  212. return NULL;
  213. }
  214. path->system = strndup(name, str - name);
  215. path->name = strdup(str+1);
  216. if (path->system == NULL || path->name == NULL) {
  217. zfree(&path->system);
  218. zfree(&path->name);
  219. free(path);
  220. path = NULL;
  221. }
  222. return path;
  223. }
  224. const char *event_type(int type)
  225. {
  226. switch (type) {
  227. case PERF_TYPE_HARDWARE:
  228. return "hardware";
  229. case PERF_TYPE_SOFTWARE:
  230. return "software";
  231. case PERF_TYPE_TRACEPOINT:
  232. return "tracepoint";
  233. case PERF_TYPE_HW_CACHE:
  234. return "hardware-cache";
  235. default:
  236. break;
  237. }
  238. return "unknown";
  239. }
  240. static struct perf_evsel *
  241. __add_event(struct list_head *list, int *idx,
  242. struct perf_event_attr *attr,
  243. char *name, struct cpu_map *cpus)
  244. {
  245. struct perf_evsel *evsel;
  246. event_attr_init(attr);
  247. evsel = perf_evsel__new_idx(attr, (*idx)++);
  248. if (!evsel)
  249. return NULL;
  250. evsel->cpus = cpus;
  251. if (name)
  252. evsel->name = strdup(name);
  253. list_add_tail(&evsel->node, list);
  254. return evsel;
  255. }
  256. static int add_event(struct list_head *list, int *idx,
  257. struct perf_event_attr *attr, char *name)
  258. {
  259. return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
  260. }
  261. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  262. {
  263. int i, j;
  264. int n, longest = -1;
  265. for (i = 0; i < size; i++) {
  266. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  267. n = strlen(names[i][j]);
  268. if (n > longest && !strncasecmp(str, names[i][j], n))
  269. longest = n;
  270. }
  271. if (longest > 0)
  272. return i;
  273. }
  274. return -1;
  275. }
  276. int parse_events_add_cache(struct list_head *list, int *idx,
  277. char *type, char *op_result1, char *op_result2)
  278. {
  279. struct perf_event_attr attr;
  280. char name[MAX_NAME_LEN];
  281. int cache_type = -1, cache_op = -1, cache_result = -1;
  282. char *op_result[2] = { op_result1, op_result2 };
  283. int i, n;
  284. /*
  285. * No fallback - if we cannot get a clear cache type
  286. * then bail out:
  287. */
  288. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  289. PERF_COUNT_HW_CACHE_MAX);
  290. if (cache_type == -1)
  291. return -EINVAL;
  292. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  293. for (i = 0; (i < 2) && (op_result[i]); i++) {
  294. char *str = op_result[i];
  295. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  296. if (cache_op == -1) {
  297. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  298. PERF_COUNT_HW_CACHE_OP_MAX);
  299. if (cache_op >= 0) {
  300. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  301. return -EINVAL;
  302. continue;
  303. }
  304. }
  305. if (cache_result == -1) {
  306. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  307. PERF_COUNT_HW_CACHE_RESULT_MAX);
  308. if (cache_result >= 0)
  309. continue;
  310. }
  311. }
  312. /*
  313. * Fall back to reads:
  314. */
  315. if (cache_op == -1)
  316. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  317. /*
  318. * Fall back to accesses:
  319. */
  320. if (cache_result == -1)
  321. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  322. memset(&attr, 0, sizeof(attr));
  323. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  324. attr.type = PERF_TYPE_HW_CACHE;
  325. return add_event(list, idx, &attr, name);
  326. }
  327. static int add_tracepoint(struct list_head *list, int *idx,
  328. char *sys_name, char *evt_name)
  329. {
  330. struct perf_evsel *evsel;
  331. evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
  332. if (!evsel)
  333. return -ENOMEM;
  334. list_add_tail(&evsel->node, list);
  335. return 0;
  336. }
  337. static int add_tracepoint_multi_event(struct list_head *list, int *idx,
  338. char *sys_name, char *evt_name)
  339. {
  340. char evt_path[MAXPATHLEN];
  341. struct dirent *evt_ent;
  342. DIR *evt_dir;
  343. int ret = 0;
  344. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  345. evt_dir = opendir(evt_path);
  346. if (!evt_dir) {
  347. perror("Can't open event dir");
  348. return -1;
  349. }
  350. while (!ret && (evt_ent = readdir(evt_dir))) {
  351. if (!strcmp(evt_ent->d_name, ".")
  352. || !strcmp(evt_ent->d_name, "..")
  353. || !strcmp(evt_ent->d_name, "enable")
  354. || !strcmp(evt_ent->d_name, "filter"))
  355. continue;
  356. if (!strglobmatch(evt_ent->d_name, evt_name))
  357. continue;
  358. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  359. }
  360. closedir(evt_dir);
  361. return ret;
  362. }
  363. static int add_tracepoint_event(struct list_head *list, int *idx,
  364. char *sys_name, char *evt_name)
  365. {
  366. return strpbrk(evt_name, "*?") ?
  367. add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
  368. add_tracepoint(list, idx, sys_name, evt_name);
  369. }
  370. static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
  371. char *sys_name, char *evt_name)
  372. {
  373. struct dirent *events_ent;
  374. DIR *events_dir;
  375. int ret = 0;
  376. events_dir = opendir(tracing_events_path);
  377. if (!events_dir) {
  378. perror("Can't open event dir");
  379. return -1;
  380. }
  381. while (!ret && (events_ent = readdir(events_dir))) {
  382. if (!strcmp(events_ent->d_name, ".")
  383. || !strcmp(events_ent->d_name, "..")
  384. || !strcmp(events_ent->d_name, "enable")
  385. || !strcmp(events_ent->d_name, "header_event")
  386. || !strcmp(events_ent->d_name, "header_page"))
  387. continue;
  388. if (!strglobmatch(events_ent->d_name, sys_name))
  389. continue;
  390. ret = add_tracepoint_event(list, idx, events_ent->d_name,
  391. evt_name);
  392. }
  393. closedir(events_dir);
  394. return ret;
  395. }
  396. int parse_events_add_tracepoint(struct list_head *list, int *idx,
  397. char *sys, char *event)
  398. {
  399. if (strpbrk(sys, "*?"))
  400. return add_tracepoint_multi_sys(list, idx, sys, event);
  401. else
  402. return add_tracepoint_event(list, idx, sys, event);
  403. }
  404. static int
  405. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  406. {
  407. int i;
  408. for (i = 0; i < 3; i++) {
  409. if (!type || !type[i])
  410. break;
  411. #define CHECK_SET_TYPE(bit) \
  412. do { \
  413. if (attr->bp_type & bit) \
  414. return -EINVAL; \
  415. else \
  416. attr->bp_type |= bit; \
  417. } while (0)
  418. switch (type[i]) {
  419. case 'r':
  420. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  421. break;
  422. case 'w':
  423. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  424. break;
  425. case 'x':
  426. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  427. break;
  428. default:
  429. return -EINVAL;
  430. }
  431. }
  432. #undef CHECK_SET_TYPE
  433. if (!attr->bp_type) /* Default */
  434. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  435. return 0;
  436. }
  437. int parse_events_add_breakpoint(struct list_head *list, int *idx,
  438. void *ptr, char *type, u64 len)
  439. {
  440. struct perf_event_attr attr;
  441. memset(&attr, 0, sizeof(attr));
  442. attr.bp_addr = (unsigned long) ptr;
  443. if (parse_breakpoint_type(type, &attr))
  444. return -EINVAL;
  445. /* Provide some defaults if len is not specified */
  446. if (!len) {
  447. if (attr.bp_type == HW_BREAKPOINT_X)
  448. len = sizeof(long);
  449. else
  450. len = HW_BREAKPOINT_LEN_4;
  451. }
  452. attr.bp_len = len;
  453. attr.type = PERF_TYPE_BREAKPOINT;
  454. attr.sample_period = 1;
  455. return add_event(list, idx, &attr, NULL);
  456. }
  457. static int config_term(struct perf_event_attr *attr,
  458. struct parse_events_term *term)
  459. {
  460. #define CHECK_TYPE_VAL(type) \
  461. do { \
  462. if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
  463. return -EINVAL; \
  464. } while (0)
  465. switch (term->type_term) {
  466. case PARSE_EVENTS__TERM_TYPE_USER:
  467. /*
  468. * Always succeed for sysfs terms, as we dont know
  469. * at this point what type they need to have.
  470. */
  471. return 0;
  472. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  473. CHECK_TYPE_VAL(NUM);
  474. attr->config = term->val.num;
  475. break;
  476. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  477. CHECK_TYPE_VAL(NUM);
  478. attr->config1 = term->val.num;
  479. break;
  480. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  481. CHECK_TYPE_VAL(NUM);
  482. attr->config2 = term->val.num;
  483. break;
  484. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  485. CHECK_TYPE_VAL(NUM);
  486. attr->sample_period = term->val.num;
  487. break;
  488. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  489. /*
  490. * TODO uncomment when the field is available
  491. * attr->branch_sample_type = term->val.num;
  492. */
  493. break;
  494. case PARSE_EVENTS__TERM_TYPE_NAME:
  495. CHECK_TYPE_VAL(STR);
  496. break;
  497. default:
  498. return -EINVAL;
  499. }
  500. return 0;
  501. #undef CHECK_TYPE_VAL
  502. }
  503. static int config_attr(struct perf_event_attr *attr,
  504. struct list_head *head)
  505. {
  506. struct parse_events_term *term;
  507. list_for_each_entry(term, head, list)
  508. if (config_term(attr, term))
  509. return -EINVAL;
  510. return 0;
  511. }
  512. int parse_events_add_numeric(struct list_head *list, int *idx,
  513. u32 type, u64 config,
  514. struct list_head *head_config)
  515. {
  516. struct perf_event_attr attr;
  517. memset(&attr, 0, sizeof(attr));
  518. attr.type = type;
  519. attr.config = config;
  520. if (head_config &&
  521. config_attr(&attr, head_config))
  522. return -EINVAL;
  523. return add_event(list, idx, &attr, NULL);
  524. }
  525. static int parse_events__is_name_term(struct parse_events_term *term)
  526. {
  527. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  528. }
  529. static char *pmu_event_name(struct list_head *head_terms)
  530. {
  531. struct parse_events_term *term;
  532. list_for_each_entry(term, head_terms, list)
  533. if (parse_events__is_name_term(term))
  534. return term->val.str;
  535. return NULL;
  536. }
  537. int parse_events_add_pmu(struct parse_events_evlist *data,
  538. struct list_head *list, char *name,
  539. struct list_head *head_config)
  540. {
  541. struct perf_event_attr attr;
  542. struct perf_pmu_info info;
  543. struct perf_pmu *pmu;
  544. struct perf_evsel *evsel;
  545. pmu = perf_pmu__find(name);
  546. if (!pmu)
  547. return -EINVAL;
  548. if (pmu->default_config) {
  549. memcpy(&attr, pmu->default_config,
  550. sizeof(struct perf_event_attr));
  551. } else {
  552. memset(&attr, 0, sizeof(attr));
  553. }
  554. if (!head_config) {
  555. attr.type = pmu->type;
  556. evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus);
  557. return evsel ? 0 : -ENOMEM;
  558. }
  559. if (perf_pmu__check_alias(pmu, head_config, &info))
  560. return -EINVAL;
  561. /*
  562. * Configure hardcoded terms first, no need to check
  563. * return value when called with fail == 0 ;)
  564. */
  565. if (config_attr(&attr, head_config))
  566. return -EINVAL;
  567. if (perf_pmu__config(pmu, &attr, head_config))
  568. return -EINVAL;
  569. evsel = __add_event(list, &data->idx, &attr,
  570. pmu_event_name(head_config), pmu->cpus);
  571. if (evsel) {
  572. evsel->unit = info.unit;
  573. evsel->scale = info.scale;
  574. evsel->per_pkg = info.per_pkg;
  575. evsel->snapshot = info.snapshot;
  576. }
  577. return evsel ? 0 : -ENOMEM;
  578. }
  579. int parse_events__modifier_group(struct list_head *list,
  580. char *event_mod)
  581. {
  582. return parse_events__modifier_event(list, event_mod, true);
  583. }
  584. void parse_events__set_leader(char *name, struct list_head *list)
  585. {
  586. struct perf_evsel *leader;
  587. __perf_evlist__set_leader(list);
  588. leader = list_entry(list->next, struct perf_evsel, node);
  589. leader->group_name = name ? strdup(name) : NULL;
  590. }
  591. /* list_event is assumed to point to malloc'ed memory */
  592. void parse_events_update_lists(struct list_head *list_event,
  593. struct list_head *list_all)
  594. {
  595. /*
  596. * Called for single event definition. Update the
  597. * 'all event' list, and reinit the 'single event'
  598. * list, for next event definition.
  599. */
  600. list_splice_tail(list_event, list_all);
  601. free(list_event);
  602. }
  603. struct event_modifier {
  604. int eu;
  605. int ek;
  606. int eh;
  607. int eH;
  608. int eG;
  609. int eI;
  610. int precise;
  611. int exclude_GH;
  612. int sample_read;
  613. int pinned;
  614. };
  615. static int get_event_modifier(struct event_modifier *mod, char *str,
  616. struct perf_evsel *evsel)
  617. {
  618. int eu = evsel ? evsel->attr.exclude_user : 0;
  619. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  620. int eh = evsel ? evsel->attr.exclude_hv : 0;
  621. int eH = evsel ? evsel->attr.exclude_host : 0;
  622. int eG = evsel ? evsel->attr.exclude_guest : 0;
  623. int eI = evsel ? evsel->attr.exclude_idle : 0;
  624. int precise = evsel ? evsel->attr.precise_ip : 0;
  625. int sample_read = 0;
  626. int pinned = evsel ? evsel->attr.pinned : 0;
  627. int exclude = eu | ek | eh;
  628. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  629. memset(mod, 0, sizeof(*mod));
  630. while (*str) {
  631. if (*str == 'u') {
  632. if (!exclude)
  633. exclude = eu = ek = eh = 1;
  634. eu = 0;
  635. } else if (*str == 'k') {
  636. if (!exclude)
  637. exclude = eu = ek = eh = 1;
  638. ek = 0;
  639. } else if (*str == 'h') {
  640. if (!exclude)
  641. exclude = eu = ek = eh = 1;
  642. eh = 0;
  643. } else if (*str == 'G') {
  644. if (!exclude_GH)
  645. exclude_GH = eG = eH = 1;
  646. eG = 0;
  647. } else if (*str == 'H') {
  648. if (!exclude_GH)
  649. exclude_GH = eG = eH = 1;
  650. eH = 0;
  651. } else if (*str == 'I') {
  652. eI = 1;
  653. } else if (*str == 'p') {
  654. precise++;
  655. /* use of precise requires exclude_guest */
  656. if (!exclude_GH)
  657. eG = 1;
  658. } else if (*str == 'S') {
  659. sample_read = 1;
  660. } else if (*str == 'D') {
  661. pinned = 1;
  662. } else
  663. break;
  664. ++str;
  665. }
  666. /*
  667. * precise ip:
  668. *
  669. * 0 - SAMPLE_IP can have arbitrary skid
  670. * 1 - SAMPLE_IP must have constant skid
  671. * 2 - SAMPLE_IP requested to have 0 skid
  672. * 3 - SAMPLE_IP must have 0 skid
  673. *
  674. * See also PERF_RECORD_MISC_EXACT_IP
  675. */
  676. if (precise > 3)
  677. return -EINVAL;
  678. mod->eu = eu;
  679. mod->ek = ek;
  680. mod->eh = eh;
  681. mod->eH = eH;
  682. mod->eG = eG;
  683. mod->eI = eI;
  684. mod->precise = precise;
  685. mod->exclude_GH = exclude_GH;
  686. mod->sample_read = sample_read;
  687. mod->pinned = pinned;
  688. return 0;
  689. }
  690. /*
  691. * Basic modifier sanity check to validate it contains only one
  692. * instance of any modifier (apart from 'p') present.
  693. */
  694. static int check_modifier(char *str)
  695. {
  696. char *p = str;
  697. /* The sizeof includes 0 byte as well. */
  698. if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
  699. return -1;
  700. while (*p) {
  701. if (*p != 'p' && strchr(p + 1, *p))
  702. return -1;
  703. p++;
  704. }
  705. return 0;
  706. }
  707. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  708. {
  709. struct perf_evsel *evsel;
  710. struct event_modifier mod;
  711. if (str == NULL)
  712. return 0;
  713. if (check_modifier(str))
  714. return -EINVAL;
  715. if (!add && get_event_modifier(&mod, str, NULL))
  716. return -EINVAL;
  717. __evlist__for_each(list, evsel) {
  718. if (add && get_event_modifier(&mod, str, evsel))
  719. return -EINVAL;
  720. evsel->attr.exclude_user = mod.eu;
  721. evsel->attr.exclude_kernel = mod.ek;
  722. evsel->attr.exclude_hv = mod.eh;
  723. evsel->attr.precise_ip = mod.precise;
  724. evsel->attr.exclude_host = mod.eH;
  725. evsel->attr.exclude_guest = mod.eG;
  726. evsel->attr.exclude_idle = mod.eI;
  727. evsel->exclude_GH = mod.exclude_GH;
  728. evsel->sample_read = mod.sample_read;
  729. if (perf_evsel__is_group_leader(evsel))
  730. evsel->attr.pinned = mod.pinned;
  731. }
  732. return 0;
  733. }
  734. int parse_events_name(struct list_head *list, char *name)
  735. {
  736. struct perf_evsel *evsel;
  737. __evlist__for_each(list, evsel) {
  738. if (!evsel->name)
  739. evsel->name = strdup(name);
  740. }
  741. return 0;
  742. }
  743. static int
  744. comp_pmu(const void *p1, const void *p2)
  745. {
  746. struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
  747. struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
  748. return strcmp(pmu1->symbol, pmu2->symbol);
  749. }
  750. static void perf_pmu__parse_cleanup(void)
  751. {
  752. if (perf_pmu_events_list_num > 0) {
  753. struct perf_pmu_event_symbol *p;
  754. int i;
  755. for (i = 0; i < perf_pmu_events_list_num; i++) {
  756. p = perf_pmu_events_list + i;
  757. free(p->symbol);
  758. }
  759. free(perf_pmu_events_list);
  760. perf_pmu_events_list = NULL;
  761. perf_pmu_events_list_num = 0;
  762. }
  763. }
  764. #define SET_SYMBOL(str, stype) \
  765. do { \
  766. p->symbol = str; \
  767. if (!p->symbol) \
  768. goto err; \
  769. p->type = stype; \
  770. } while (0)
  771. /*
  772. * Read the pmu events list from sysfs
  773. * Save it into perf_pmu_events_list
  774. */
  775. static void perf_pmu__parse_init(void)
  776. {
  777. struct perf_pmu *pmu = NULL;
  778. struct perf_pmu_alias *alias;
  779. int len = 0;
  780. pmu = perf_pmu__find("cpu");
  781. if ((pmu == NULL) || list_empty(&pmu->aliases)) {
  782. perf_pmu_events_list_num = -1;
  783. return;
  784. }
  785. list_for_each_entry(alias, &pmu->aliases, list) {
  786. if (strchr(alias->name, '-'))
  787. len++;
  788. len++;
  789. }
  790. perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
  791. if (!perf_pmu_events_list)
  792. return;
  793. perf_pmu_events_list_num = len;
  794. len = 0;
  795. list_for_each_entry(alias, &pmu->aliases, list) {
  796. struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
  797. char *tmp = strchr(alias->name, '-');
  798. if (tmp != NULL) {
  799. SET_SYMBOL(strndup(alias->name, tmp - alias->name),
  800. PMU_EVENT_SYMBOL_PREFIX);
  801. p++;
  802. SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
  803. len += 2;
  804. } else {
  805. SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
  806. len++;
  807. }
  808. }
  809. qsort(perf_pmu_events_list, len,
  810. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  811. return;
  812. err:
  813. perf_pmu__parse_cleanup();
  814. }
  815. enum perf_pmu_event_symbol_type
  816. perf_pmu__parse_check(const char *name)
  817. {
  818. struct perf_pmu_event_symbol p, *r;
  819. /* scan kernel pmu events from sysfs if needed */
  820. if (perf_pmu_events_list_num == 0)
  821. perf_pmu__parse_init();
  822. /*
  823. * name "cpu" could be prefix of cpu-cycles or cpu// events.
  824. * cpu-cycles has been handled by hardcode.
  825. * So it must be cpu// events, not kernel pmu event.
  826. */
  827. if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
  828. return PMU_EVENT_SYMBOL_ERR;
  829. p.symbol = strdup(name);
  830. r = bsearch(&p, perf_pmu_events_list,
  831. (size_t) perf_pmu_events_list_num,
  832. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  833. free(p.symbol);
  834. return r ? r->type : PMU_EVENT_SYMBOL_ERR;
  835. }
  836. static int parse_events__scanner(const char *str, void *data, int start_token)
  837. {
  838. YY_BUFFER_STATE buffer;
  839. void *scanner;
  840. int ret;
  841. ret = parse_events_lex_init_extra(start_token, &scanner);
  842. if (ret)
  843. return ret;
  844. buffer = parse_events__scan_string(str, scanner);
  845. #ifdef PARSER_DEBUG
  846. parse_events_debug = 1;
  847. #endif
  848. ret = parse_events_parse(data, scanner);
  849. parse_events__flush_buffer(buffer, scanner);
  850. parse_events__delete_buffer(buffer, scanner);
  851. parse_events_lex_destroy(scanner);
  852. return ret;
  853. }
  854. /*
  855. * parse event config string, return a list of event terms.
  856. */
  857. int parse_events_terms(struct list_head *terms, const char *str)
  858. {
  859. struct parse_events_terms data = {
  860. .terms = NULL,
  861. };
  862. int ret;
  863. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  864. if (!ret) {
  865. list_splice(data.terms, terms);
  866. zfree(&data.terms);
  867. return 0;
  868. }
  869. if (data.terms)
  870. parse_events__free_terms(data.terms);
  871. return ret;
  872. }
  873. int parse_events(struct perf_evlist *evlist, const char *str,
  874. struct parse_events_error *err)
  875. {
  876. struct parse_events_evlist data = {
  877. .list = LIST_HEAD_INIT(data.list),
  878. .idx = evlist->nr_entries,
  879. .error = err,
  880. };
  881. int ret;
  882. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  883. perf_pmu__parse_cleanup();
  884. if (!ret) {
  885. int entries = data.idx - evlist->nr_entries;
  886. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  887. evlist->nr_groups += data.nr_groups;
  888. return 0;
  889. }
  890. /*
  891. * There are 2 users - builtin-record and builtin-test objects.
  892. * Both call perf_evlist__delete in case of error, so we dont
  893. * need to bother.
  894. */
  895. return ret;
  896. }
  897. #define MAX_WIDTH 1000
  898. static int get_term_width(void)
  899. {
  900. struct winsize ws;
  901. get_term_dimensions(&ws);
  902. return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
  903. }
  904. static void parse_events_print_error(struct parse_events_error *err,
  905. const char *event)
  906. {
  907. const char *str = "invalid or unsupported event: ";
  908. char _buf[MAX_WIDTH];
  909. char *buf = (char *) event;
  910. int idx = 0;
  911. if (err->str) {
  912. /* -2 for extra '' in the final fprintf */
  913. int width = get_term_width() - 2;
  914. int len_event = strlen(event);
  915. int len_str, max_len, cut = 0;
  916. /*
  917. * Maximum error index indent, we will cut
  918. * the event string if it's bigger.
  919. */
  920. int max_err_idx = 10;
  921. /*
  922. * Let's be specific with the message when
  923. * we have the precise error.
  924. */
  925. str = "event syntax error: ";
  926. len_str = strlen(str);
  927. max_len = width - len_str;
  928. buf = _buf;
  929. /* We're cutting from the beggining. */
  930. if (err->idx > max_err_idx)
  931. cut = err->idx - max_err_idx;
  932. strncpy(buf, event + cut, max_len);
  933. /* Mark cut parts with '..' on both sides. */
  934. if (cut)
  935. buf[0] = buf[1] = '.';
  936. if ((len_event - cut) > max_len) {
  937. buf[max_len - 1] = buf[max_len - 2] = '.';
  938. buf[max_len] = 0;
  939. }
  940. idx = len_str + err->idx - cut;
  941. }
  942. fprintf(stderr, "%s'%s'\n", str, buf);
  943. if (idx) {
  944. fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
  945. if (err->help)
  946. fprintf(stderr, "\n%s\n", err->help);
  947. free(err->str);
  948. free(err->help);
  949. }
  950. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  951. }
  952. #undef MAX_WIDTH
  953. int parse_events_option(const struct option *opt, const char *str,
  954. int unset __maybe_unused)
  955. {
  956. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  957. struct parse_events_error err = { .idx = 0, };
  958. int ret = parse_events(evlist, str, &err);
  959. if (ret)
  960. parse_events_print_error(&err, str);
  961. return ret;
  962. }
  963. int parse_filter(const struct option *opt, const char *str,
  964. int unset __maybe_unused)
  965. {
  966. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  967. struct perf_evsel *last = NULL;
  968. if (evlist->nr_entries > 0)
  969. last = perf_evlist__last(evlist);
  970. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  971. fprintf(stderr,
  972. "--filter option should follow a -e tracepoint option\n");
  973. return -1;
  974. }
  975. last->filter = strdup(str);
  976. if (last->filter == NULL) {
  977. fprintf(stderr, "not enough memory to hold filter string\n");
  978. return -1;
  979. }
  980. return 0;
  981. }
  982. static const char * const event_type_descriptors[] = {
  983. "Hardware event",
  984. "Software event",
  985. "Tracepoint event",
  986. "Hardware cache event",
  987. "Raw hardware event descriptor",
  988. "Hardware breakpoint",
  989. };
  990. static int cmp_string(const void *a, const void *b)
  991. {
  992. const char * const *as = a;
  993. const char * const *bs = b;
  994. return strcmp(*as, *bs);
  995. }
  996. /*
  997. * Print the events from <debugfs_mount_point>/tracing/events
  998. */
  999. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  1000. bool name_only)
  1001. {
  1002. DIR *sys_dir, *evt_dir;
  1003. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1004. char evt_path[MAXPATHLEN];
  1005. char dir_path[MAXPATHLEN];
  1006. char **evt_list = NULL;
  1007. unsigned int evt_i = 0, evt_num = 0;
  1008. bool evt_num_known = false;
  1009. restart:
  1010. sys_dir = opendir(tracing_events_path);
  1011. if (!sys_dir)
  1012. return;
  1013. if (evt_num_known) {
  1014. evt_list = zalloc(sizeof(char *) * evt_num);
  1015. if (!evt_list)
  1016. goto out_close_sys_dir;
  1017. }
  1018. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1019. if (subsys_glob != NULL &&
  1020. !strglobmatch(sys_dirent.d_name, subsys_glob))
  1021. continue;
  1022. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1023. sys_dirent.d_name);
  1024. evt_dir = opendir(dir_path);
  1025. if (!evt_dir)
  1026. continue;
  1027. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1028. if (event_glob != NULL &&
  1029. !strglobmatch(evt_dirent.d_name, event_glob))
  1030. continue;
  1031. if (!evt_num_known) {
  1032. evt_num++;
  1033. continue;
  1034. }
  1035. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1036. sys_dirent.d_name, evt_dirent.d_name);
  1037. evt_list[evt_i] = strdup(evt_path);
  1038. if (evt_list[evt_i] == NULL)
  1039. goto out_close_evt_dir;
  1040. evt_i++;
  1041. }
  1042. closedir(evt_dir);
  1043. }
  1044. closedir(sys_dir);
  1045. if (!evt_num_known) {
  1046. evt_num_known = true;
  1047. goto restart;
  1048. }
  1049. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1050. evt_i = 0;
  1051. while (evt_i < evt_num) {
  1052. if (name_only) {
  1053. printf("%s ", evt_list[evt_i++]);
  1054. continue;
  1055. }
  1056. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1057. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1058. }
  1059. if (evt_num)
  1060. printf("\n");
  1061. out_free:
  1062. evt_num = evt_i;
  1063. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1064. zfree(&evt_list[evt_i]);
  1065. zfree(&evt_list);
  1066. return;
  1067. out_close_evt_dir:
  1068. closedir(evt_dir);
  1069. out_close_sys_dir:
  1070. closedir(sys_dir);
  1071. printf("FATAL: not enough memory to print %s\n",
  1072. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1073. if (evt_list)
  1074. goto out_free;
  1075. }
  1076. /*
  1077. * Check whether event is in <debugfs_mount_point>/tracing/events
  1078. */
  1079. int is_valid_tracepoint(const char *event_string)
  1080. {
  1081. DIR *sys_dir, *evt_dir;
  1082. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1083. char evt_path[MAXPATHLEN];
  1084. char dir_path[MAXPATHLEN];
  1085. sys_dir = opendir(tracing_events_path);
  1086. if (!sys_dir)
  1087. return 0;
  1088. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1089. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1090. sys_dirent.d_name);
  1091. evt_dir = opendir(dir_path);
  1092. if (!evt_dir)
  1093. continue;
  1094. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1095. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1096. sys_dirent.d_name, evt_dirent.d_name);
  1097. if (!strcmp(evt_path, event_string)) {
  1098. closedir(evt_dir);
  1099. closedir(sys_dir);
  1100. return 1;
  1101. }
  1102. }
  1103. closedir(evt_dir);
  1104. }
  1105. closedir(sys_dir);
  1106. return 0;
  1107. }
  1108. static bool is_event_supported(u8 type, unsigned config)
  1109. {
  1110. bool ret = true;
  1111. int open_return;
  1112. struct perf_evsel *evsel;
  1113. struct perf_event_attr attr = {
  1114. .type = type,
  1115. .config = config,
  1116. .disabled = 1,
  1117. };
  1118. struct {
  1119. struct thread_map map;
  1120. int threads[1];
  1121. } tmap = {
  1122. .map.nr = 1,
  1123. .threads = { 0 },
  1124. };
  1125. evsel = perf_evsel__new(&attr);
  1126. if (evsel) {
  1127. open_return = perf_evsel__open(evsel, NULL, &tmap.map);
  1128. ret = open_return >= 0;
  1129. if (open_return == -EACCES) {
  1130. /*
  1131. * This happens if the paranoid value
  1132. * /proc/sys/kernel/perf_event_paranoid is set to 2
  1133. * Re-run with exclude_kernel set; we don't do that
  1134. * by default as some ARM machines do not support it.
  1135. *
  1136. */
  1137. evsel->attr.exclude_kernel = 1;
  1138. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  1139. }
  1140. perf_evsel__delete(evsel);
  1141. }
  1142. return ret;
  1143. }
  1144. int print_hwcache_events(const char *event_glob, bool name_only)
  1145. {
  1146. unsigned int type, op, i, evt_i = 0, evt_num = 0;
  1147. char name[64];
  1148. char **evt_list = NULL;
  1149. bool evt_num_known = false;
  1150. restart:
  1151. if (evt_num_known) {
  1152. evt_list = zalloc(sizeof(char *) * evt_num);
  1153. if (!evt_list)
  1154. goto out_enomem;
  1155. }
  1156. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  1157. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  1158. /* skip invalid cache type */
  1159. if (!perf_evsel__is_cache_op_valid(type, op))
  1160. continue;
  1161. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  1162. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  1163. name, sizeof(name));
  1164. if (event_glob != NULL && !strglobmatch(name, event_glob))
  1165. continue;
  1166. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  1167. type | (op << 8) | (i << 16)))
  1168. continue;
  1169. if (!evt_num_known) {
  1170. evt_num++;
  1171. continue;
  1172. }
  1173. evt_list[evt_i] = strdup(name);
  1174. if (evt_list[evt_i] == NULL)
  1175. goto out_enomem;
  1176. evt_i++;
  1177. }
  1178. }
  1179. }
  1180. if (!evt_num_known) {
  1181. evt_num_known = true;
  1182. goto restart;
  1183. }
  1184. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1185. evt_i = 0;
  1186. while (evt_i < evt_num) {
  1187. if (name_only) {
  1188. printf("%s ", evt_list[evt_i++]);
  1189. continue;
  1190. }
  1191. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1192. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1193. }
  1194. if (evt_num)
  1195. printf("\n");
  1196. out_free:
  1197. evt_num = evt_i;
  1198. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1199. zfree(&evt_list[evt_i]);
  1200. zfree(&evt_list);
  1201. return evt_num;
  1202. out_enomem:
  1203. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1204. if (evt_list)
  1205. goto out_free;
  1206. return evt_num;
  1207. }
  1208. void print_symbol_events(const char *event_glob, unsigned type,
  1209. struct event_symbol *syms, unsigned max,
  1210. bool name_only)
  1211. {
  1212. unsigned int i, evt_i = 0, evt_num = 0;
  1213. char name[MAX_NAME_LEN];
  1214. char **evt_list = NULL;
  1215. bool evt_num_known = false;
  1216. restart:
  1217. if (evt_num_known) {
  1218. evt_list = zalloc(sizeof(char *) * evt_num);
  1219. if (!evt_list)
  1220. goto out_enomem;
  1221. syms -= max;
  1222. }
  1223. for (i = 0; i < max; i++, syms++) {
  1224. if (event_glob != NULL &&
  1225. !(strglobmatch(syms->symbol, event_glob) ||
  1226. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1227. continue;
  1228. if (!is_event_supported(type, i))
  1229. continue;
  1230. if (!evt_num_known) {
  1231. evt_num++;
  1232. continue;
  1233. }
  1234. if (!name_only && strlen(syms->alias))
  1235. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1236. else
  1237. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1238. evt_list[evt_i] = strdup(name);
  1239. if (evt_list[evt_i] == NULL)
  1240. goto out_enomem;
  1241. evt_i++;
  1242. }
  1243. if (!evt_num_known) {
  1244. evt_num_known = true;
  1245. goto restart;
  1246. }
  1247. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1248. evt_i = 0;
  1249. while (evt_i < evt_num) {
  1250. if (name_only) {
  1251. printf("%s ", evt_list[evt_i++]);
  1252. continue;
  1253. }
  1254. printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
  1255. }
  1256. if (evt_num)
  1257. printf("\n");
  1258. out_free:
  1259. evt_num = evt_i;
  1260. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1261. zfree(&evt_list[evt_i]);
  1262. zfree(&evt_list);
  1263. return;
  1264. out_enomem:
  1265. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
  1266. if (evt_list)
  1267. goto out_free;
  1268. }
  1269. /*
  1270. * Print the help text for the event symbols:
  1271. */
  1272. void print_events(const char *event_glob, bool name_only)
  1273. {
  1274. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1275. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1276. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1277. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1278. print_hwcache_events(event_glob, name_only);
  1279. print_pmu_events(event_glob, name_only);
  1280. if (event_glob != NULL)
  1281. return;
  1282. if (!name_only) {
  1283. printf(" %-50s [%s]\n",
  1284. "rNNN",
  1285. event_type_descriptors[PERF_TYPE_RAW]);
  1286. printf(" %-50s [%s]\n",
  1287. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1288. event_type_descriptors[PERF_TYPE_RAW]);
  1289. printf(" (see 'man perf-list' on how to encode it)\n");
  1290. printf("\n");
  1291. printf(" %-50s [%s]\n",
  1292. "mem:<addr>[/len][:access]",
  1293. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1294. printf("\n");
  1295. }
  1296. print_tracepoint_events(NULL, NULL, name_only);
  1297. }
  1298. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1299. {
  1300. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1301. }
  1302. static int new_term(struct parse_events_term **_term, int type_val,
  1303. int type_term, char *config,
  1304. char *str, u64 num)
  1305. {
  1306. struct parse_events_term *term;
  1307. term = zalloc(sizeof(*term));
  1308. if (!term)
  1309. return -ENOMEM;
  1310. INIT_LIST_HEAD(&term->list);
  1311. term->type_val = type_val;
  1312. term->type_term = type_term;
  1313. term->config = config;
  1314. switch (type_val) {
  1315. case PARSE_EVENTS__TERM_TYPE_NUM:
  1316. term->val.num = num;
  1317. break;
  1318. case PARSE_EVENTS__TERM_TYPE_STR:
  1319. term->val.str = str;
  1320. break;
  1321. default:
  1322. free(term);
  1323. return -EINVAL;
  1324. }
  1325. *_term = term;
  1326. return 0;
  1327. }
  1328. int parse_events_term__num(struct parse_events_term **term,
  1329. int type_term, char *config, u64 num)
  1330. {
  1331. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1332. config, NULL, num);
  1333. }
  1334. int parse_events_term__str(struct parse_events_term **term,
  1335. int type_term, char *config, char *str)
  1336. {
  1337. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1338. config, str, 0);
  1339. }
  1340. int parse_events_term__sym_hw(struct parse_events_term **term,
  1341. char *config, unsigned idx)
  1342. {
  1343. struct event_symbol *sym;
  1344. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1345. sym = &event_symbols_hw[idx];
  1346. if (config)
  1347. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1348. PARSE_EVENTS__TERM_TYPE_USER, config,
  1349. (char *) sym->symbol, 0);
  1350. else
  1351. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1352. PARSE_EVENTS__TERM_TYPE_USER,
  1353. (char *) "event", (char *) sym->symbol, 0);
  1354. }
  1355. int parse_events_term__clone(struct parse_events_term **new,
  1356. struct parse_events_term *term)
  1357. {
  1358. return new_term(new, term->type_val, term->type_term, term->config,
  1359. term->val.str, term->val.num);
  1360. }
  1361. void parse_events__free_terms(struct list_head *terms)
  1362. {
  1363. struct parse_events_term *term, *h;
  1364. list_for_each_entry_safe(term, h, terms, list)
  1365. free(term);
  1366. }
  1367. void parse_events_evlist_error(struct parse_events_evlist *data,
  1368. int idx, const char *str)
  1369. {
  1370. struct parse_events_error *err = data->error;
  1371. err->idx = idx;
  1372. err->str = strdup(str);
  1373. WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
  1374. }