parse-events.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  1. #include <linux/hw_breakpoint.h>
  2. #include <linux/err.h>
  3. #include "util.h"
  4. #include "../perf.h"
  5. #include "evlist.h"
  6. #include "evsel.h"
  7. #include <subcmd/parse-options.h>
  8. #include "parse-events.h"
  9. #include <subcmd/exec-cmd.h>
  10. #include "string.h"
  11. #include "symbol.h"
  12. #include "cache.h"
  13. #include "header.h"
  14. #include "bpf-loader.h"
  15. #include "debug.h"
  16. #include <api/fs/tracing_path.h>
  17. #include "parse-events-bison.h"
  18. #define YY_EXTRA_TYPE int
  19. #include "parse-events-flex.h"
  20. #include "pmu.h"
  21. #include "thread_map.h"
  22. #include "cpumap.h"
  23. #include "asm/bug.h"
  24. #define MAX_NAME_LEN 100
  25. #ifdef PARSER_DEBUG
  26. extern int parse_events_debug;
  27. #endif
  28. int parse_events_parse(void *data, void *scanner);
  29. static int get_config_terms(struct list_head *head_config,
  30. struct list_head *head_terms __maybe_unused);
  31. static struct perf_pmu_event_symbol *perf_pmu_events_list;
  32. /*
  33. * The variable indicates the number of supported pmu event symbols.
  34. * 0 means not initialized and ready to init
  35. * -1 means failed to init, don't try anymore
  36. * >0 is the number of supported pmu event symbols
  37. */
  38. static int perf_pmu_events_list_num;
  39. struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
  40. [PERF_COUNT_HW_CPU_CYCLES] = {
  41. .symbol = "cpu-cycles",
  42. .alias = "cycles",
  43. },
  44. [PERF_COUNT_HW_INSTRUCTIONS] = {
  45. .symbol = "instructions",
  46. .alias = "",
  47. },
  48. [PERF_COUNT_HW_CACHE_REFERENCES] = {
  49. .symbol = "cache-references",
  50. .alias = "",
  51. },
  52. [PERF_COUNT_HW_CACHE_MISSES] = {
  53. .symbol = "cache-misses",
  54. .alias = "",
  55. },
  56. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
  57. .symbol = "branch-instructions",
  58. .alias = "branches",
  59. },
  60. [PERF_COUNT_HW_BRANCH_MISSES] = {
  61. .symbol = "branch-misses",
  62. .alias = "",
  63. },
  64. [PERF_COUNT_HW_BUS_CYCLES] = {
  65. .symbol = "bus-cycles",
  66. .alias = "",
  67. },
  68. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
  69. .symbol = "stalled-cycles-frontend",
  70. .alias = "idle-cycles-frontend",
  71. },
  72. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
  73. .symbol = "stalled-cycles-backend",
  74. .alias = "idle-cycles-backend",
  75. },
  76. [PERF_COUNT_HW_REF_CPU_CYCLES] = {
  77. .symbol = "ref-cycles",
  78. .alias = "",
  79. },
  80. };
  81. struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
  82. [PERF_COUNT_SW_CPU_CLOCK] = {
  83. .symbol = "cpu-clock",
  84. .alias = "",
  85. },
  86. [PERF_COUNT_SW_TASK_CLOCK] = {
  87. .symbol = "task-clock",
  88. .alias = "",
  89. },
  90. [PERF_COUNT_SW_PAGE_FAULTS] = {
  91. .symbol = "page-faults",
  92. .alias = "faults",
  93. },
  94. [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
  95. .symbol = "context-switches",
  96. .alias = "cs",
  97. },
  98. [PERF_COUNT_SW_CPU_MIGRATIONS] = {
  99. .symbol = "cpu-migrations",
  100. .alias = "migrations",
  101. },
  102. [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
  103. .symbol = "minor-faults",
  104. .alias = "",
  105. },
  106. [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
  107. .symbol = "major-faults",
  108. .alias = "",
  109. },
  110. [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
  111. .symbol = "alignment-faults",
  112. .alias = "",
  113. },
  114. [PERF_COUNT_SW_EMULATION_FAULTS] = {
  115. .symbol = "emulation-faults",
  116. .alias = "",
  117. },
  118. [PERF_COUNT_SW_DUMMY] = {
  119. .symbol = "dummy",
  120. .alias = "",
  121. },
  122. [PERF_COUNT_SW_BPF_OUTPUT] = {
  123. .symbol = "bpf-output",
  124. .alias = "",
  125. },
  126. };
  127. #define __PERF_EVENT_FIELD(config, name) \
  128. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  129. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  130. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  131. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  132. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  133. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  134. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  135. if (sys_dirent.d_type == DT_DIR && \
  136. (strcmp(sys_dirent.d_name, ".")) && \
  137. (strcmp(sys_dirent.d_name, "..")))
  138. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  139. {
  140. char evt_path[MAXPATHLEN];
  141. int fd;
  142. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  143. sys_dir->d_name, evt_dir->d_name);
  144. fd = open(evt_path, O_RDONLY);
  145. if (fd < 0)
  146. return -EINVAL;
  147. close(fd);
  148. return 0;
  149. }
  150. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  151. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  152. if (evt_dirent.d_type == DT_DIR && \
  153. (strcmp(evt_dirent.d_name, ".")) && \
  154. (strcmp(evt_dirent.d_name, "..")) && \
  155. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  156. #define MAX_EVENT_LENGTH 512
  157. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  158. {
  159. struct tracepoint_path *path = NULL;
  160. DIR *sys_dir, *evt_dir;
  161. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  162. char id_buf[24];
  163. int fd;
  164. u64 id;
  165. char evt_path[MAXPATHLEN];
  166. char dir_path[MAXPATHLEN];
  167. sys_dir = opendir(tracing_events_path);
  168. if (!sys_dir)
  169. return NULL;
  170. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  171. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  172. sys_dirent.d_name);
  173. evt_dir = opendir(dir_path);
  174. if (!evt_dir)
  175. continue;
  176. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  177. snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
  178. evt_dirent.d_name);
  179. fd = open(evt_path, O_RDONLY);
  180. if (fd < 0)
  181. continue;
  182. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  183. close(fd);
  184. continue;
  185. }
  186. close(fd);
  187. id = atoll(id_buf);
  188. if (id == config) {
  189. closedir(evt_dir);
  190. closedir(sys_dir);
  191. path = zalloc(sizeof(*path));
  192. path->system = malloc(MAX_EVENT_LENGTH);
  193. if (!path->system) {
  194. free(path);
  195. return NULL;
  196. }
  197. path->name = malloc(MAX_EVENT_LENGTH);
  198. if (!path->name) {
  199. zfree(&path->system);
  200. free(path);
  201. return NULL;
  202. }
  203. strncpy(path->system, sys_dirent.d_name,
  204. MAX_EVENT_LENGTH);
  205. strncpy(path->name, evt_dirent.d_name,
  206. MAX_EVENT_LENGTH);
  207. return path;
  208. }
  209. }
  210. closedir(evt_dir);
  211. }
  212. closedir(sys_dir);
  213. return NULL;
  214. }
  215. struct tracepoint_path *tracepoint_name_to_path(const char *name)
  216. {
  217. struct tracepoint_path *path = zalloc(sizeof(*path));
  218. char *str = strchr(name, ':');
  219. if (path == NULL || str == NULL) {
  220. free(path);
  221. return NULL;
  222. }
  223. path->system = strndup(name, str - name);
  224. path->name = strdup(str+1);
  225. if (path->system == NULL || path->name == NULL) {
  226. zfree(&path->system);
  227. zfree(&path->name);
  228. free(path);
  229. path = NULL;
  230. }
  231. return path;
  232. }
  233. const char *event_type(int type)
  234. {
  235. switch (type) {
  236. case PERF_TYPE_HARDWARE:
  237. return "hardware";
  238. case PERF_TYPE_SOFTWARE:
  239. return "software";
  240. case PERF_TYPE_TRACEPOINT:
  241. return "tracepoint";
  242. case PERF_TYPE_HW_CACHE:
  243. return "hardware-cache";
  244. default:
  245. break;
  246. }
  247. return "unknown";
  248. }
  249. static struct perf_evsel *
  250. __add_event(struct list_head *list, int *idx,
  251. struct perf_event_attr *attr,
  252. char *name, struct cpu_map *cpus,
  253. struct list_head *config_terms)
  254. {
  255. struct perf_evsel *evsel;
  256. event_attr_init(attr);
  257. evsel = perf_evsel__new_idx(attr, (*idx)++);
  258. if (!evsel)
  259. return NULL;
  260. evsel->cpus = cpu_map__get(cpus);
  261. evsel->own_cpus = cpu_map__get(cpus);
  262. if (name)
  263. evsel->name = strdup(name);
  264. if (config_terms)
  265. list_splice(config_terms, &evsel->config_terms);
  266. list_add_tail(&evsel->node, list);
  267. return evsel;
  268. }
  269. static int add_event(struct list_head *list, int *idx,
  270. struct perf_event_attr *attr, char *name,
  271. struct list_head *config_terms)
  272. {
  273. return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
  274. }
  275. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  276. {
  277. int i, j;
  278. int n, longest = -1;
  279. for (i = 0; i < size; i++) {
  280. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  281. n = strlen(names[i][j]);
  282. if (n > longest && !strncasecmp(str, names[i][j], n))
  283. longest = n;
  284. }
  285. if (longest > 0)
  286. return i;
  287. }
  288. return -1;
  289. }
  290. int parse_events_add_cache(struct list_head *list, int *idx,
  291. char *type, char *op_result1, char *op_result2)
  292. {
  293. struct perf_event_attr attr;
  294. char name[MAX_NAME_LEN];
  295. int cache_type = -1, cache_op = -1, cache_result = -1;
  296. char *op_result[2] = { op_result1, op_result2 };
  297. int i, n;
  298. /*
  299. * No fallback - if we cannot get a clear cache type
  300. * then bail out:
  301. */
  302. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  303. PERF_COUNT_HW_CACHE_MAX);
  304. if (cache_type == -1)
  305. return -EINVAL;
  306. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  307. for (i = 0; (i < 2) && (op_result[i]); i++) {
  308. char *str = op_result[i];
  309. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  310. if (cache_op == -1) {
  311. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  312. PERF_COUNT_HW_CACHE_OP_MAX);
  313. if (cache_op >= 0) {
  314. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  315. return -EINVAL;
  316. continue;
  317. }
  318. }
  319. if (cache_result == -1) {
  320. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  321. PERF_COUNT_HW_CACHE_RESULT_MAX);
  322. if (cache_result >= 0)
  323. continue;
  324. }
  325. }
  326. /*
  327. * Fall back to reads:
  328. */
  329. if (cache_op == -1)
  330. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  331. /*
  332. * Fall back to accesses:
  333. */
  334. if (cache_result == -1)
  335. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  336. memset(&attr, 0, sizeof(attr));
  337. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  338. attr.type = PERF_TYPE_HW_CACHE;
  339. return add_event(list, idx, &attr, name, NULL);
  340. }
  341. static void tracepoint_error(struct parse_events_error *e, int err,
  342. char *sys, char *name)
  343. {
  344. char help[BUFSIZ];
  345. /*
  346. * We get error directly from syscall errno ( > 0),
  347. * or from encoded pointer's error ( < 0).
  348. */
  349. err = abs(err);
  350. switch (err) {
  351. case EACCES:
  352. e->str = strdup("can't access trace events");
  353. break;
  354. case ENOENT:
  355. e->str = strdup("unknown tracepoint");
  356. break;
  357. default:
  358. e->str = strdup("failed to add tracepoint");
  359. break;
  360. }
  361. tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
  362. e->help = strdup(help);
  363. }
  364. static int add_tracepoint(struct list_head *list, int *idx,
  365. char *sys_name, char *evt_name,
  366. struct parse_events_error *err,
  367. struct list_head *head_config)
  368. {
  369. struct perf_evsel *evsel;
  370. evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
  371. if (IS_ERR(evsel)) {
  372. tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
  373. return PTR_ERR(evsel);
  374. }
  375. if (head_config) {
  376. LIST_HEAD(config_terms);
  377. if (get_config_terms(head_config, &config_terms))
  378. return -ENOMEM;
  379. list_splice(&config_terms, &evsel->config_terms);
  380. }
  381. list_add_tail(&evsel->node, list);
  382. return 0;
  383. }
  384. static int add_tracepoint_multi_event(struct list_head *list, int *idx,
  385. char *sys_name, char *evt_name,
  386. struct parse_events_error *err,
  387. struct list_head *head_config)
  388. {
  389. char evt_path[MAXPATHLEN];
  390. struct dirent *evt_ent;
  391. DIR *evt_dir;
  392. int ret = 0, found = 0;
  393. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  394. evt_dir = opendir(evt_path);
  395. if (!evt_dir) {
  396. tracepoint_error(err, errno, sys_name, evt_name);
  397. return -1;
  398. }
  399. while (!ret && (evt_ent = readdir(evt_dir))) {
  400. if (!strcmp(evt_ent->d_name, ".")
  401. || !strcmp(evt_ent->d_name, "..")
  402. || !strcmp(evt_ent->d_name, "enable")
  403. || !strcmp(evt_ent->d_name, "filter"))
  404. continue;
  405. if (!strglobmatch(evt_ent->d_name, evt_name))
  406. continue;
  407. found++;
  408. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
  409. err, head_config);
  410. }
  411. if (!found) {
  412. tracepoint_error(err, ENOENT, sys_name, evt_name);
  413. ret = -1;
  414. }
  415. closedir(evt_dir);
  416. return ret;
  417. }
  418. static int add_tracepoint_event(struct list_head *list, int *idx,
  419. char *sys_name, char *evt_name,
  420. struct parse_events_error *err,
  421. struct list_head *head_config)
  422. {
  423. return strpbrk(evt_name, "*?") ?
  424. add_tracepoint_multi_event(list, idx, sys_name, evt_name,
  425. err, head_config) :
  426. add_tracepoint(list, idx, sys_name, evt_name,
  427. err, head_config);
  428. }
  429. static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
  430. char *sys_name, char *evt_name,
  431. struct parse_events_error *err,
  432. struct list_head *head_config)
  433. {
  434. struct dirent *events_ent;
  435. DIR *events_dir;
  436. int ret = 0;
  437. events_dir = opendir(tracing_events_path);
  438. if (!events_dir) {
  439. tracepoint_error(err, errno, sys_name, evt_name);
  440. return -1;
  441. }
  442. while (!ret && (events_ent = readdir(events_dir))) {
  443. if (!strcmp(events_ent->d_name, ".")
  444. || !strcmp(events_ent->d_name, "..")
  445. || !strcmp(events_ent->d_name, "enable")
  446. || !strcmp(events_ent->d_name, "header_event")
  447. || !strcmp(events_ent->d_name, "header_page"))
  448. continue;
  449. if (!strglobmatch(events_ent->d_name, sys_name))
  450. continue;
  451. ret = add_tracepoint_event(list, idx, events_ent->d_name,
  452. evt_name, err, head_config);
  453. }
  454. closedir(events_dir);
  455. return ret;
  456. }
  457. struct __add_bpf_event_param {
  458. struct parse_events_evlist *data;
  459. struct list_head *list;
  460. };
  461. static int add_bpf_event(struct probe_trace_event *tev, int fd,
  462. void *_param)
  463. {
  464. LIST_HEAD(new_evsels);
  465. struct __add_bpf_event_param *param = _param;
  466. struct parse_events_evlist *evlist = param->data;
  467. struct list_head *list = param->list;
  468. struct perf_evsel *pos;
  469. int err;
  470. pr_debug("add bpf event %s:%s and attach bpf program %d\n",
  471. tev->group, tev->event, fd);
  472. err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, tev->group,
  473. tev->event, evlist->error, NULL);
  474. if (err) {
  475. struct perf_evsel *evsel, *tmp;
  476. pr_debug("Failed to add BPF event %s:%s\n",
  477. tev->group, tev->event);
  478. list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
  479. list_del(&evsel->node);
  480. perf_evsel__delete(evsel);
  481. }
  482. return err;
  483. }
  484. pr_debug("adding %s:%s\n", tev->group, tev->event);
  485. list_for_each_entry(pos, &new_evsels, node) {
  486. pr_debug("adding %s:%s to %p\n",
  487. tev->group, tev->event, pos);
  488. pos->bpf_fd = fd;
  489. }
  490. list_splice(&new_evsels, list);
  491. return 0;
  492. }
  493. int parse_events_load_bpf_obj(struct parse_events_evlist *data,
  494. struct list_head *list,
  495. struct bpf_object *obj)
  496. {
  497. int err;
  498. char errbuf[BUFSIZ];
  499. struct __add_bpf_event_param param = {data, list};
  500. static bool registered_unprobe_atexit = false;
  501. if (IS_ERR(obj) || !obj) {
  502. snprintf(errbuf, sizeof(errbuf),
  503. "Internal error: load bpf obj with NULL");
  504. err = -EINVAL;
  505. goto errout;
  506. }
  507. /*
  508. * Register atexit handler before calling bpf__probe() so
  509. * bpf__probe() don't need to unprobe probe points its already
  510. * created when failure.
  511. */
  512. if (!registered_unprobe_atexit) {
  513. atexit(bpf__clear);
  514. registered_unprobe_atexit = true;
  515. }
  516. err = bpf__probe(obj);
  517. if (err) {
  518. bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
  519. goto errout;
  520. }
  521. err = bpf__load(obj);
  522. if (err) {
  523. bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
  524. goto errout;
  525. }
  526. err = bpf__foreach_tev(obj, add_bpf_event, &param);
  527. if (err) {
  528. snprintf(errbuf, sizeof(errbuf),
  529. "Attach events in BPF object failed");
  530. goto errout;
  531. }
  532. return 0;
  533. errout:
  534. data->error->help = strdup("(add -v to see detail)");
  535. data->error->str = strdup(errbuf);
  536. return err;
  537. }
  538. int parse_events_load_bpf(struct parse_events_evlist *data,
  539. struct list_head *list,
  540. char *bpf_file_name,
  541. bool source)
  542. {
  543. struct bpf_object *obj;
  544. obj = bpf__prepare_load(bpf_file_name, source);
  545. if (IS_ERR(obj)) {
  546. char errbuf[BUFSIZ];
  547. int err;
  548. err = PTR_ERR(obj);
  549. if (err == -ENOTSUP)
  550. snprintf(errbuf, sizeof(errbuf),
  551. "BPF support is not compiled");
  552. else
  553. bpf__strerror_prepare_load(bpf_file_name,
  554. source,
  555. -err, errbuf,
  556. sizeof(errbuf));
  557. data->error->help = strdup("(add -v to see detail)");
  558. data->error->str = strdup(errbuf);
  559. return err;
  560. }
  561. return parse_events_load_bpf_obj(data, list, obj);
  562. }
  563. static int
  564. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  565. {
  566. int i;
  567. for (i = 0; i < 3; i++) {
  568. if (!type || !type[i])
  569. break;
  570. #define CHECK_SET_TYPE(bit) \
  571. do { \
  572. if (attr->bp_type & bit) \
  573. return -EINVAL; \
  574. else \
  575. attr->bp_type |= bit; \
  576. } while (0)
  577. switch (type[i]) {
  578. case 'r':
  579. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  580. break;
  581. case 'w':
  582. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  583. break;
  584. case 'x':
  585. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  586. break;
  587. default:
  588. return -EINVAL;
  589. }
  590. }
  591. #undef CHECK_SET_TYPE
  592. if (!attr->bp_type) /* Default */
  593. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  594. return 0;
  595. }
  596. int parse_events_add_breakpoint(struct list_head *list, int *idx,
  597. void *ptr, char *type, u64 len)
  598. {
  599. struct perf_event_attr attr;
  600. memset(&attr, 0, sizeof(attr));
  601. attr.bp_addr = (unsigned long) ptr;
  602. if (parse_breakpoint_type(type, &attr))
  603. return -EINVAL;
  604. /* Provide some defaults if len is not specified */
  605. if (!len) {
  606. if (attr.bp_type == HW_BREAKPOINT_X)
  607. len = sizeof(long);
  608. else
  609. len = HW_BREAKPOINT_LEN_4;
  610. }
  611. attr.bp_len = len;
  612. attr.type = PERF_TYPE_BREAKPOINT;
  613. attr.sample_period = 1;
  614. return add_event(list, idx, &attr, NULL, NULL);
  615. }
  616. static int check_type_val(struct parse_events_term *term,
  617. struct parse_events_error *err,
  618. int type)
  619. {
  620. if (type == term->type_val)
  621. return 0;
  622. if (err) {
  623. err->idx = term->err_val;
  624. if (type == PARSE_EVENTS__TERM_TYPE_NUM)
  625. err->str = strdup("expected numeric value");
  626. else
  627. err->str = strdup("expected string value");
  628. }
  629. return -EINVAL;
  630. }
  631. typedef int config_term_func_t(struct perf_event_attr *attr,
  632. struct parse_events_term *term,
  633. struct parse_events_error *err);
  634. static int config_term_common(struct perf_event_attr *attr,
  635. struct parse_events_term *term,
  636. struct parse_events_error *err)
  637. {
  638. #define CHECK_TYPE_VAL(type) \
  639. do { \
  640. if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
  641. return -EINVAL; \
  642. } while (0)
  643. switch (term->type_term) {
  644. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  645. CHECK_TYPE_VAL(NUM);
  646. attr->config = term->val.num;
  647. break;
  648. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  649. CHECK_TYPE_VAL(NUM);
  650. attr->config1 = term->val.num;
  651. break;
  652. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  653. CHECK_TYPE_VAL(NUM);
  654. attr->config2 = term->val.num;
  655. break;
  656. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  657. CHECK_TYPE_VAL(NUM);
  658. break;
  659. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  660. CHECK_TYPE_VAL(NUM);
  661. break;
  662. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  663. /*
  664. * TODO uncomment when the field is available
  665. * attr->branch_sample_type = term->val.num;
  666. */
  667. break;
  668. case PARSE_EVENTS__TERM_TYPE_TIME:
  669. CHECK_TYPE_VAL(NUM);
  670. if (term->val.num > 1) {
  671. err->str = strdup("expected 0 or 1");
  672. err->idx = term->err_val;
  673. return -EINVAL;
  674. }
  675. break;
  676. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  677. CHECK_TYPE_VAL(STR);
  678. break;
  679. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  680. CHECK_TYPE_VAL(NUM);
  681. break;
  682. case PARSE_EVENTS__TERM_TYPE_INHERIT:
  683. CHECK_TYPE_VAL(NUM);
  684. break;
  685. case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  686. CHECK_TYPE_VAL(NUM);
  687. break;
  688. case PARSE_EVENTS__TERM_TYPE_NAME:
  689. CHECK_TYPE_VAL(STR);
  690. break;
  691. default:
  692. err->str = strdup("unknown term");
  693. err->idx = term->err_term;
  694. err->help = parse_events_formats_error_string(NULL);
  695. return -EINVAL;
  696. }
  697. return 0;
  698. #undef CHECK_TYPE_VAL
  699. }
  700. static int config_term_pmu(struct perf_event_attr *attr,
  701. struct parse_events_term *term,
  702. struct parse_events_error *err)
  703. {
  704. if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
  705. /*
  706. * Always succeed for sysfs terms, as we dont know
  707. * at this point what type they need to have.
  708. */
  709. return 0;
  710. else
  711. return config_term_common(attr, term, err);
  712. }
  713. static int config_term_tracepoint(struct perf_event_attr *attr,
  714. struct parse_events_term *term,
  715. struct parse_events_error *err)
  716. {
  717. switch (term->type_term) {
  718. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  719. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  720. case PARSE_EVENTS__TERM_TYPE_INHERIT:
  721. case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  722. return config_term_common(attr, term, err);
  723. default:
  724. if (err) {
  725. err->idx = term->err_term;
  726. err->str = strdup("unknown term");
  727. err->help = strdup("valid terms: call-graph,stack-size\n");
  728. }
  729. return -EINVAL;
  730. }
  731. return 0;
  732. }
  733. static int config_attr(struct perf_event_attr *attr,
  734. struct list_head *head,
  735. struct parse_events_error *err,
  736. config_term_func_t config_term)
  737. {
  738. struct parse_events_term *term;
  739. list_for_each_entry(term, head, list)
  740. if (config_term(attr, term, err))
  741. return -EINVAL;
  742. return 0;
  743. }
  744. static int get_config_terms(struct list_head *head_config,
  745. struct list_head *head_terms __maybe_unused)
  746. {
  747. #define ADD_CONFIG_TERM(__type, __name, __val) \
  748. do { \
  749. struct perf_evsel_config_term *__t; \
  750. \
  751. __t = zalloc(sizeof(*__t)); \
  752. if (!__t) \
  753. return -ENOMEM; \
  754. \
  755. INIT_LIST_HEAD(&__t->list); \
  756. __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
  757. __t->val.__name = __val; \
  758. list_add_tail(&__t->list, head_terms); \
  759. } while (0)
  760. struct parse_events_term *term;
  761. list_for_each_entry(term, head_config, list) {
  762. switch (term->type_term) {
  763. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  764. ADD_CONFIG_TERM(PERIOD, period, term->val.num);
  765. break;
  766. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  767. ADD_CONFIG_TERM(FREQ, freq, term->val.num);
  768. break;
  769. case PARSE_EVENTS__TERM_TYPE_TIME:
  770. ADD_CONFIG_TERM(TIME, time, term->val.num);
  771. break;
  772. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  773. ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
  774. break;
  775. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  776. ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
  777. break;
  778. case PARSE_EVENTS__TERM_TYPE_INHERIT:
  779. ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
  780. break;
  781. case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  782. ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
  783. break;
  784. default:
  785. break;
  786. }
  787. }
  788. #undef ADD_EVSEL_CONFIG
  789. return 0;
  790. }
  791. int parse_events_add_tracepoint(struct list_head *list, int *idx,
  792. char *sys, char *event,
  793. struct parse_events_error *err,
  794. struct list_head *head_config)
  795. {
  796. if (head_config) {
  797. struct perf_event_attr attr;
  798. if (config_attr(&attr, head_config, err,
  799. config_term_tracepoint))
  800. return -EINVAL;
  801. }
  802. if (strpbrk(sys, "*?"))
  803. return add_tracepoint_multi_sys(list, idx, sys, event,
  804. err, head_config);
  805. else
  806. return add_tracepoint_event(list, idx, sys, event,
  807. err, head_config);
  808. }
  809. int parse_events_add_numeric(struct parse_events_evlist *data,
  810. struct list_head *list,
  811. u32 type, u64 config,
  812. struct list_head *head_config)
  813. {
  814. struct perf_event_attr attr;
  815. LIST_HEAD(config_terms);
  816. memset(&attr, 0, sizeof(attr));
  817. attr.type = type;
  818. attr.config = config;
  819. if (head_config) {
  820. if (config_attr(&attr, head_config, data->error,
  821. config_term_common))
  822. return -EINVAL;
  823. if (get_config_terms(head_config, &config_terms))
  824. return -ENOMEM;
  825. }
  826. return add_event(list, &data->idx, &attr, NULL, &config_terms);
  827. }
  828. static int parse_events__is_name_term(struct parse_events_term *term)
  829. {
  830. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  831. }
  832. static char *pmu_event_name(struct list_head *head_terms)
  833. {
  834. struct parse_events_term *term;
  835. list_for_each_entry(term, head_terms, list)
  836. if (parse_events__is_name_term(term))
  837. return term->val.str;
  838. return NULL;
  839. }
  840. int parse_events_add_pmu(struct parse_events_evlist *data,
  841. struct list_head *list, char *name,
  842. struct list_head *head_config)
  843. {
  844. struct perf_event_attr attr;
  845. struct perf_pmu_info info;
  846. struct perf_pmu *pmu;
  847. struct perf_evsel *evsel;
  848. LIST_HEAD(config_terms);
  849. pmu = perf_pmu__find(name);
  850. if (!pmu)
  851. return -EINVAL;
  852. if (pmu->default_config) {
  853. memcpy(&attr, pmu->default_config,
  854. sizeof(struct perf_event_attr));
  855. } else {
  856. memset(&attr, 0, sizeof(attr));
  857. }
  858. if (!head_config) {
  859. attr.type = pmu->type;
  860. evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
  861. return evsel ? 0 : -ENOMEM;
  862. }
  863. if (perf_pmu__check_alias(pmu, head_config, &info))
  864. return -EINVAL;
  865. /*
  866. * Configure hardcoded terms first, no need to check
  867. * return value when called with fail == 0 ;)
  868. */
  869. if (config_attr(&attr, head_config, data->error, config_term_pmu))
  870. return -EINVAL;
  871. if (get_config_terms(head_config, &config_terms))
  872. return -ENOMEM;
  873. if (perf_pmu__config(pmu, &attr, head_config, data->error))
  874. return -EINVAL;
  875. evsel = __add_event(list, &data->idx, &attr,
  876. pmu_event_name(head_config), pmu->cpus,
  877. &config_terms);
  878. if (evsel) {
  879. evsel->unit = info.unit;
  880. evsel->scale = info.scale;
  881. evsel->per_pkg = info.per_pkg;
  882. evsel->snapshot = info.snapshot;
  883. }
  884. return evsel ? 0 : -ENOMEM;
  885. }
  886. int parse_events__modifier_group(struct list_head *list,
  887. char *event_mod)
  888. {
  889. return parse_events__modifier_event(list, event_mod, true);
  890. }
  891. void parse_events__set_leader(char *name, struct list_head *list)
  892. {
  893. struct perf_evsel *leader;
  894. if (list_empty(list)) {
  895. WARN_ONCE(true, "WARNING: failed to set leader: empty list");
  896. return;
  897. }
  898. __perf_evlist__set_leader(list);
  899. leader = list_entry(list->next, struct perf_evsel, node);
  900. leader->group_name = name ? strdup(name) : NULL;
  901. }
  902. /* list_event is assumed to point to malloc'ed memory */
  903. void parse_events_update_lists(struct list_head *list_event,
  904. struct list_head *list_all)
  905. {
  906. /*
  907. * Called for single event definition. Update the
  908. * 'all event' list, and reinit the 'single event'
  909. * list, for next event definition.
  910. */
  911. list_splice_tail(list_event, list_all);
  912. free(list_event);
  913. }
  914. struct event_modifier {
  915. int eu;
  916. int ek;
  917. int eh;
  918. int eH;
  919. int eG;
  920. int eI;
  921. int precise;
  922. int precise_max;
  923. int exclude_GH;
  924. int sample_read;
  925. int pinned;
  926. };
  927. static int get_event_modifier(struct event_modifier *mod, char *str,
  928. struct perf_evsel *evsel)
  929. {
  930. int eu = evsel ? evsel->attr.exclude_user : 0;
  931. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  932. int eh = evsel ? evsel->attr.exclude_hv : 0;
  933. int eH = evsel ? evsel->attr.exclude_host : 0;
  934. int eG = evsel ? evsel->attr.exclude_guest : 0;
  935. int eI = evsel ? evsel->attr.exclude_idle : 0;
  936. int precise = evsel ? evsel->attr.precise_ip : 0;
  937. int precise_max = 0;
  938. int sample_read = 0;
  939. int pinned = evsel ? evsel->attr.pinned : 0;
  940. int exclude = eu | ek | eh;
  941. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  942. memset(mod, 0, sizeof(*mod));
  943. while (*str) {
  944. if (*str == 'u') {
  945. if (!exclude)
  946. exclude = eu = ek = eh = 1;
  947. eu = 0;
  948. } else if (*str == 'k') {
  949. if (!exclude)
  950. exclude = eu = ek = eh = 1;
  951. ek = 0;
  952. } else if (*str == 'h') {
  953. if (!exclude)
  954. exclude = eu = ek = eh = 1;
  955. eh = 0;
  956. } else if (*str == 'G') {
  957. if (!exclude_GH)
  958. exclude_GH = eG = eH = 1;
  959. eG = 0;
  960. } else if (*str == 'H') {
  961. if (!exclude_GH)
  962. exclude_GH = eG = eH = 1;
  963. eH = 0;
  964. } else if (*str == 'I') {
  965. eI = 1;
  966. } else if (*str == 'p') {
  967. precise++;
  968. /* use of precise requires exclude_guest */
  969. if (!exclude_GH)
  970. eG = 1;
  971. } else if (*str == 'P') {
  972. precise_max = 1;
  973. } else if (*str == 'S') {
  974. sample_read = 1;
  975. } else if (*str == 'D') {
  976. pinned = 1;
  977. } else
  978. break;
  979. ++str;
  980. }
  981. /*
  982. * precise ip:
  983. *
  984. * 0 - SAMPLE_IP can have arbitrary skid
  985. * 1 - SAMPLE_IP must have constant skid
  986. * 2 - SAMPLE_IP requested to have 0 skid
  987. * 3 - SAMPLE_IP must have 0 skid
  988. *
  989. * See also PERF_RECORD_MISC_EXACT_IP
  990. */
  991. if (precise > 3)
  992. return -EINVAL;
  993. mod->eu = eu;
  994. mod->ek = ek;
  995. mod->eh = eh;
  996. mod->eH = eH;
  997. mod->eG = eG;
  998. mod->eI = eI;
  999. mod->precise = precise;
  1000. mod->precise_max = precise_max;
  1001. mod->exclude_GH = exclude_GH;
  1002. mod->sample_read = sample_read;
  1003. mod->pinned = pinned;
  1004. return 0;
  1005. }
  1006. /*
  1007. * Basic modifier sanity check to validate it contains only one
  1008. * instance of any modifier (apart from 'p') present.
  1009. */
  1010. static int check_modifier(char *str)
  1011. {
  1012. char *p = str;
  1013. /* The sizeof includes 0 byte as well. */
  1014. if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
  1015. return -1;
  1016. while (*p) {
  1017. if (*p != 'p' && strchr(p + 1, *p))
  1018. return -1;
  1019. p++;
  1020. }
  1021. return 0;
  1022. }
  1023. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  1024. {
  1025. struct perf_evsel *evsel;
  1026. struct event_modifier mod;
  1027. if (str == NULL)
  1028. return 0;
  1029. if (check_modifier(str))
  1030. return -EINVAL;
  1031. if (!add && get_event_modifier(&mod, str, NULL))
  1032. return -EINVAL;
  1033. __evlist__for_each(list, evsel) {
  1034. if (add && get_event_modifier(&mod, str, evsel))
  1035. return -EINVAL;
  1036. evsel->attr.exclude_user = mod.eu;
  1037. evsel->attr.exclude_kernel = mod.ek;
  1038. evsel->attr.exclude_hv = mod.eh;
  1039. evsel->attr.precise_ip = mod.precise;
  1040. evsel->attr.exclude_host = mod.eH;
  1041. evsel->attr.exclude_guest = mod.eG;
  1042. evsel->attr.exclude_idle = mod.eI;
  1043. evsel->exclude_GH = mod.exclude_GH;
  1044. evsel->sample_read = mod.sample_read;
  1045. evsel->precise_max = mod.precise_max;
  1046. if (perf_evsel__is_group_leader(evsel))
  1047. evsel->attr.pinned = mod.pinned;
  1048. }
  1049. return 0;
  1050. }
  1051. int parse_events_name(struct list_head *list, char *name)
  1052. {
  1053. struct perf_evsel *evsel;
  1054. __evlist__for_each(list, evsel) {
  1055. if (!evsel->name)
  1056. evsel->name = strdup(name);
  1057. }
  1058. return 0;
  1059. }
  1060. static int
  1061. comp_pmu(const void *p1, const void *p2)
  1062. {
  1063. struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
  1064. struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
  1065. return strcmp(pmu1->symbol, pmu2->symbol);
  1066. }
  1067. static void perf_pmu__parse_cleanup(void)
  1068. {
  1069. if (perf_pmu_events_list_num > 0) {
  1070. struct perf_pmu_event_symbol *p;
  1071. int i;
  1072. for (i = 0; i < perf_pmu_events_list_num; i++) {
  1073. p = perf_pmu_events_list + i;
  1074. free(p->symbol);
  1075. }
  1076. free(perf_pmu_events_list);
  1077. perf_pmu_events_list = NULL;
  1078. perf_pmu_events_list_num = 0;
  1079. }
  1080. }
  1081. #define SET_SYMBOL(str, stype) \
  1082. do { \
  1083. p->symbol = str; \
  1084. if (!p->symbol) \
  1085. goto err; \
  1086. p->type = stype; \
  1087. } while (0)
  1088. /*
  1089. * Read the pmu events list from sysfs
  1090. * Save it into perf_pmu_events_list
  1091. */
  1092. static void perf_pmu__parse_init(void)
  1093. {
  1094. struct perf_pmu *pmu = NULL;
  1095. struct perf_pmu_alias *alias;
  1096. int len = 0;
  1097. pmu = perf_pmu__find("cpu");
  1098. if ((pmu == NULL) || list_empty(&pmu->aliases)) {
  1099. perf_pmu_events_list_num = -1;
  1100. return;
  1101. }
  1102. list_for_each_entry(alias, &pmu->aliases, list) {
  1103. if (strchr(alias->name, '-'))
  1104. len++;
  1105. len++;
  1106. }
  1107. perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
  1108. if (!perf_pmu_events_list)
  1109. return;
  1110. perf_pmu_events_list_num = len;
  1111. len = 0;
  1112. list_for_each_entry(alias, &pmu->aliases, list) {
  1113. struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
  1114. char *tmp = strchr(alias->name, '-');
  1115. if (tmp != NULL) {
  1116. SET_SYMBOL(strndup(alias->name, tmp - alias->name),
  1117. PMU_EVENT_SYMBOL_PREFIX);
  1118. p++;
  1119. SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
  1120. len += 2;
  1121. } else {
  1122. SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
  1123. len++;
  1124. }
  1125. }
  1126. qsort(perf_pmu_events_list, len,
  1127. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  1128. return;
  1129. err:
  1130. perf_pmu__parse_cleanup();
  1131. }
  1132. enum perf_pmu_event_symbol_type
  1133. perf_pmu__parse_check(const char *name)
  1134. {
  1135. struct perf_pmu_event_symbol p, *r;
  1136. /* scan kernel pmu events from sysfs if needed */
  1137. if (perf_pmu_events_list_num == 0)
  1138. perf_pmu__parse_init();
  1139. /*
  1140. * name "cpu" could be prefix of cpu-cycles or cpu// events.
  1141. * cpu-cycles has been handled by hardcode.
  1142. * So it must be cpu// events, not kernel pmu event.
  1143. */
  1144. if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
  1145. return PMU_EVENT_SYMBOL_ERR;
  1146. p.symbol = strdup(name);
  1147. r = bsearch(&p, perf_pmu_events_list,
  1148. (size_t) perf_pmu_events_list_num,
  1149. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  1150. free(p.symbol);
  1151. return r ? r->type : PMU_EVENT_SYMBOL_ERR;
  1152. }
  1153. static int parse_events__scanner(const char *str, void *data, int start_token)
  1154. {
  1155. YY_BUFFER_STATE buffer;
  1156. void *scanner;
  1157. int ret;
  1158. ret = parse_events_lex_init_extra(start_token, &scanner);
  1159. if (ret)
  1160. return ret;
  1161. buffer = parse_events__scan_string(str, scanner);
  1162. #ifdef PARSER_DEBUG
  1163. parse_events_debug = 1;
  1164. #endif
  1165. ret = parse_events_parse(data, scanner);
  1166. parse_events__flush_buffer(buffer, scanner);
  1167. parse_events__delete_buffer(buffer, scanner);
  1168. parse_events_lex_destroy(scanner);
  1169. return ret;
  1170. }
  1171. /*
  1172. * parse event config string, return a list of event terms.
  1173. */
  1174. int parse_events_terms(struct list_head *terms, const char *str)
  1175. {
  1176. struct parse_events_terms data = {
  1177. .terms = NULL,
  1178. };
  1179. int ret;
  1180. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  1181. if (!ret) {
  1182. list_splice(data.terms, terms);
  1183. zfree(&data.terms);
  1184. return 0;
  1185. }
  1186. if (data.terms)
  1187. parse_events__free_terms(data.terms);
  1188. return ret;
  1189. }
  1190. int parse_events(struct perf_evlist *evlist, const char *str,
  1191. struct parse_events_error *err)
  1192. {
  1193. struct parse_events_evlist data = {
  1194. .list = LIST_HEAD_INIT(data.list),
  1195. .idx = evlist->nr_entries,
  1196. .error = err,
  1197. };
  1198. int ret;
  1199. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  1200. perf_pmu__parse_cleanup();
  1201. if (!ret) {
  1202. struct perf_evsel *last;
  1203. if (list_empty(&data.list)) {
  1204. WARN_ONCE(true, "WARNING: event parser found nothing");
  1205. return -1;
  1206. }
  1207. perf_evlist__splice_list_tail(evlist, &data.list);
  1208. evlist->nr_groups += data.nr_groups;
  1209. last = perf_evlist__last(evlist);
  1210. last->cmdline_group_boundary = true;
  1211. return 0;
  1212. }
  1213. /*
  1214. * There are 2 users - builtin-record and builtin-test objects.
  1215. * Both call perf_evlist__delete in case of error, so we dont
  1216. * need to bother.
  1217. */
  1218. return ret;
  1219. }
  1220. #define MAX_WIDTH 1000
  1221. static int get_term_width(void)
  1222. {
  1223. struct winsize ws;
  1224. get_term_dimensions(&ws);
  1225. return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
  1226. }
  1227. static void parse_events_print_error(struct parse_events_error *err,
  1228. const char *event)
  1229. {
  1230. const char *str = "invalid or unsupported event: ";
  1231. char _buf[MAX_WIDTH];
  1232. char *buf = (char *) event;
  1233. int idx = 0;
  1234. if (err->str) {
  1235. /* -2 for extra '' in the final fprintf */
  1236. int width = get_term_width() - 2;
  1237. int len_event = strlen(event);
  1238. int len_str, max_len, cut = 0;
  1239. /*
  1240. * Maximum error index indent, we will cut
  1241. * the event string if it's bigger.
  1242. */
  1243. int max_err_idx = 13;
  1244. /*
  1245. * Let's be specific with the message when
  1246. * we have the precise error.
  1247. */
  1248. str = "event syntax error: ";
  1249. len_str = strlen(str);
  1250. max_len = width - len_str;
  1251. buf = _buf;
  1252. /* We're cutting from the beggining. */
  1253. if (err->idx > max_err_idx)
  1254. cut = err->idx - max_err_idx;
  1255. strncpy(buf, event + cut, max_len);
  1256. /* Mark cut parts with '..' on both sides. */
  1257. if (cut)
  1258. buf[0] = buf[1] = '.';
  1259. if ((len_event - cut) > max_len) {
  1260. buf[max_len - 1] = buf[max_len - 2] = '.';
  1261. buf[max_len] = 0;
  1262. }
  1263. idx = len_str + err->idx - cut;
  1264. }
  1265. fprintf(stderr, "%s'%s'\n", str, buf);
  1266. if (idx) {
  1267. fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
  1268. if (err->help)
  1269. fprintf(stderr, "\n%s\n", err->help);
  1270. free(err->str);
  1271. free(err->help);
  1272. }
  1273. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  1274. }
  1275. #undef MAX_WIDTH
  1276. int parse_events_option(const struct option *opt, const char *str,
  1277. int unset __maybe_unused)
  1278. {
  1279. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1280. struct parse_events_error err = { .idx = 0, };
  1281. int ret = parse_events(evlist, str, &err);
  1282. if (ret)
  1283. parse_events_print_error(&err, str);
  1284. return ret;
  1285. }
  1286. static int
  1287. foreach_evsel_in_last_glob(struct perf_evlist *evlist,
  1288. int (*func)(struct perf_evsel *evsel,
  1289. const void *arg),
  1290. const void *arg)
  1291. {
  1292. struct perf_evsel *last = NULL;
  1293. int err;
  1294. /*
  1295. * Don't return when list_empty, give func a chance to report
  1296. * error when it found last == NULL.
  1297. *
  1298. * So no need to WARN here, let *func do this.
  1299. */
  1300. if (evlist->nr_entries > 0)
  1301. last = perf_evlist__last(evlist);
  1302. do {
  1303. err = (*func)(last, arg);
  1304. if (err)
  1305. return -1;
  1306. if (!last)
  1307. return 0;
  1308. if (last->node.prev == &evlist->entries)
  1309. return 0;
  1310. last = list_entry(last->node.prev, struct perf_evsel, node);
  1311. } while (!last->cmdline_group_boundary);
  1312. return 0;
  1313. }
  1314. static int set_filter(struct perf_evsel *evsel, const void *arg)
  1315. {
  1316. const char *str = arg;
  1317. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1318. fprintf(stderr,
  1319. "--filter option should follow a -e tracepoint option\n");
  1320. return -1;
  1321. }
  1322. if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
  1323. fprintf(stderr,
  1324. "not enough memory to hold filter string\n");
  1325. return -1;
  1326. }
  1327. return 0;
  1328. }
  1329. int parse_filter(const struct option *opt, const char *str,
  1330. int unset __maybe_unused)
  1331. {
  1332. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1333. return foreach_evsel_in_last_glob(evlist, set_filter,
  1334. (const void *)str);
  1335. }
  1336. static int add_exclude_perf_filter(struct perf_evsel *evsel,
  1337. const void *arg __maybe_unused)
  1338. {
  1339. char new_filter[64];
  1340. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1341. fprintf(stderr,
  1342. "--exclude-perf option should follow a -e tracepoint option\n");
  1343. return -1;
  1344. }
  1345. snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
  1346. if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
  1347. fprintf(stderr,
  1348. "not enough memory to hold filter string\n");
  1349. return -1;
  1350. }
  1351. return 0;
  1352. }
  1353. int exclude_perf(const struct option *opt,
  1354. const char *arg __maybe_unused,
  1355. int unset __maybe_unused)
  1356. {
  1357. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1358. return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
  1359. NULL);
  1360. }
  1361. static const char * const event_type_descriptors[] = {
  1362. "Hardware event",
  1363. "Software event",
  1364. "Tracepoint event",
  1365. "Hardware cache event",
  1366. "Raw hardware event descriptor",
  1367. "Hardware breakpoint",
  1368. };
  1369. static int cmp_string(const void *a, const void *b)
  1370. {
  1371. const char * const *as = a;
  1372. const char * const *bs = b;
  1373. return strcmp(*as, *bs);
  1374. }
  1375. /*
  1376. * Print the events from <debugfs_mount_point>/tracing/events
  1377. */
  1378. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  1379. bool name_only)
  1380. {
  1381. DIR *sys_dir, *evt_dir;
  1382. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1383. char evt_path[MAXPATHLEN];
  1384. char dir_path[MAXPATHLEN];
  1385. char **evt_list = NULL;
  1386. unsigned int evt_i = 0, evt_num = 0;
  1387. bool evt_num_known = false;
  1388. restart:
  1389. sys_dir = opendir(tracing_events_path);
  1390. if (!sys_dir)
  1391. return;
  1392. if (evt_num_known) {
  1393. evt_list = zalloc(sizeof(char *) * evt_num);
  1394. if (!evt_list)
  1395. goto out_close_sys_dir;
  1396. }
  1397. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1398. if (subsys_glob != NULL &&
  1399. !strglobmatch(sys_dirent.d_name, subsys_glob))
  1400. continue;
  1401. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1402. sys_dirent.d_name);
  1403. evt_dir = opendir(dir_path);
  1404. if (!evt_dir)
  1405. continue;
  1406. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1407. if (event_glob != NULL &&
  1408. !strglobmatch(evt_dirent.d_name, event_glob))
  1409. continue;
  1410. if (!evt_num_known) {
  1411. evt_num++;
  1412. continue;
  1413. }
  1414. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1415. sys_dirent.d_name, evt_dirent.d_name);
  1416. evt_list[evt_i] = strdup(evt_path);
  1417. if (evt_list[evt_i] == NULL)
  1418. goto out_close_evt_dir;
  1419. evt_i++;
  1420. }
  1421. closedir(evt_dir);
  1422. }
  1423. closedir(sys_dir);
  1424. if (!evt_num_known) {
  1425. evt_num_known = true;
  1426. goto restart;
  1427. }
  1428. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1429. evt_i = 0;
  1430. while (evt_i < evt_num) {
  1431. if (name_only) {
  1432. printf("%s ", evt_list[evt_i++]);
  1433. continue;
  1434. }
  1435. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1436. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1437. }
  1438. if (evt_num && pager_in_use())
  1439. printf("\n");
  1440. out_free:
  1441. evt_num = evt_i;
  1442. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1443. zfree(&evt_list[evt_i]);
  1444. zfree(&evt_list);
  1445. return;
  1446. out_close_evt_dir:
  1447. closedir(evt_dir);
  1448. out_close_sys_dir:
  1449. closedir(sys_dir);
  1450. printf("FATAL: not enough memory to print %s\n",
  1451. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1452. if (evt_list)
  1453. goto out_free;
  1454. }
  1455. /*
  1456. * Check whether event is in <debugfs_mount_point>/tracing/events
  1457. */
  1458. int is_valid_tracepoint(const char *event_string)
  1459. {
  1460. DIR *sys_dir, *evt_dir;
  1461. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1462. char evt_path[MAXPATHLEN];
  1463. char dir_path[MAXPATHLEN];
  1464. sys_dir = opendir(tracing_events_path);
  1465. if (!sys_dir)
  1466. return 0;
  1467. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1468. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1469. sys_dirent.d_name);
  1470. evt_dir = opendir(dir_path);
  1471. if (!evt_dir)
  1472. continue;
  1473. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1474. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1475. sys_dirent.d_name, evt_dirent.d_name);
  1476. if (!strcmp(evt_path, event_string)) {
  1477. closedir(evt_dir);
  1478. closedir(sys_dir);
  1479. return 1;
  1480. }
  1481. }
  1482. closedir(evt_dir);
  1483. }
  1484. closedir(sys_dir);
  1485. return 0;
  1486. }
  1487. static bool is_event_supported(u8 type, unsigned config)
  1488. {
  1489. bool ret = true;
  1490. int open_return;
  1491. struct perf_evsel *evsel;
  1492. struct perf_event_attr attr = {
  1493. .type = type,
  1494. .config = config,
  1495. .disabled = 1,
  1496. };
  1497. struct {
  1498. struct thread_map map;
  1499. int threads[1];
  1500. } tmap = {
  1501. .map.nr = 1,
  1502. .threads = { 0 },
  1503. };
  1504. evsel = perf_evsel__new(&attr);
  1505. if (evsel) {
  1506. open_return = perf_evsel__open(evsel, NULL, &tmap.map);
  1507. ret = open_return >= 0;
  1508. if (open_return == -EACCES) {
  1509. /*
  1510. * This happens if the paranoid value
  1511. * /proc/sys/kernel/perf_event_paranoid is set to 2
  1512. * Re-run with exclude_kernel set; we don't do that
  1513. * by default as some ARM machines do not support it.
  1514. *
  1515. */
  1516. evsel->attr.exclude_kernel = 1;
  1517. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  1518. }
  1519. perf_evsel__delete(evsel);
  1520. }
  1521. return ret;
  1522. }
  1523. int print_hwcache_events(const char *event_glob, bool name_only)
  1524. {
  1525. unsigned int type, op, i, evt_i = 0, evt_num = 0;
  1526. char name[64];
  1527. char **evt_list = NULL;
  1528. bool evt_num_known = false;
  1529. restart:
  1530. if (evt_num_known) {
  1531. evt_list = zalloc(sizeof(char *) * evt_num);
  1532. if (!evt_list)
  1533. goto out_enomem;
  1534. }
  1535. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  1536. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  1537. /* skip invalid cache type */
  1538. if (!perf_evsel__is_cache_op_valid(type, op))
  1539. continue;
  1540. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  1541. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  1542. name, sizeof(name));
  1543. if (event_glob != NULL && !strglobmatch(name, event_glob))
  1544. continue;
  1545. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  1546. type | (op << 8) | (i << 16)))
  1547. continue;
  1548. if (!evt_num_known) {
  1549. evt_num++;
  1550. continue;
  1551. }
  1552. evt_list[evt_i] = strdup(name);
  1553. if (evt_list[evt_i] == NULL)
  1554. goto out_enomem;
  1555. evt_i++;
  1556. }
  1557. }
  1558. }
  1559. if (!evt_num_known) {
  1560. evt_num_known = true;
  1561. goto restart;
  1562. }
  1563. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1564. evt_i = 0;
  1565. while (evt_i < evt_num) {
  1566. if (name_only) {
  1567. printf("%s ", evt_list[evt_i++]);
  1568. continue;
  1569. }
  1570. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1571. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1572. }
  1573. if (evt_num && pager_in_use())
  1574. printf("\n");
  1575. out_free:
  1576. evt_num = evt_i;
  1577. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1578. zfree(&evt_list[evt_i]);
  1579. zfree(&evt_list);
  1580. return evt_num;
  1581. out_enomem:
  1582. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1583. if (evt_list)
  1584. goto out_free;
  1585. return evt_num;
  1586. }
  1587. void print_symbol_events(const char *event_glob, unsigned type,
  1588. struct event_symbol *syms, unsigned max,
  1589. bool name_only)
  1590. {
  1591. unsigned int i, evt_i = 0, evt_num = 0;
  1592. char name[MAX_NAME_LEN];
  1593. char **evt_list = NULL;
  1594. bool evt_num_known = false;
  1595. restart:
  1596. if (evt_num_known) {
  1597. evt_list = zalloc(sizeof(char *) * evt_num);
  1598. if (!evt_list)
  1599. goto out_enomem;
  1600. syms -= max;
  1601. }
  1602. for (i = 0; i < max; i++, syms++) {
  1603. if (event_glob != NULL && syms->symbol != NULL &&
  1604. !(strglobmatch(syms->symbol, event_glob) ||
  1605. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1606. continue;
  1607. if (!is_event_supported(type, i))
  1608. continue;
  1609. if (!evt_num_known) {
  1610. evt_num++;
  1611. continue;
  1612. }
  1613. if (!name_only && strlen(syms->alias))
  1614. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1615. else
  1616. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1617. evt_list[evt_i] = strdup(name);
  1618. if (evt_list[evt_i] == NULL)
  1619. goto out_enomem;
  1620. evt_i++;
  1621. }
  1622. if (!evt_num_known) {
  1623. evt_num_known = true;
  1624. goto restart;
  1625. }
  1626. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1627. evt_i = 0;
  1628. while (evt_i < evt_num) {
  1629. if (name_only) {
  1630. printf("%s ", evt_list[evt_i++]);
  1631. continue;
  1632. }
  1633. printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
  1634. }
  1635. if (evt_num && pager_in_use())
  1636. printf("\n");
  1637. out_free:
  1638. evt_num = evt_i;
  1639. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1640. zfree(&evt_list[evt_i]);
  1641. zfree(&evt_list);
  1642. return;
  1643. out_enomem:
  1644. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
  1645. if (evt_list)
  1646. goto out_free;
  1647. }
  1648. /*
  1649. * Print the help text for the event symbols:
  1650. */
  1651. void print_events(const char *event_glob, bool name_only)
  1652. {
  1653. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1654. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1655. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1656. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1657. print_hwcache_events(event_glob, name_only);
  1658. print_pmu_events(event_glob, name_only);
  1659. if (event_glob != NULL)
  1660. return;
  1661. if (!name_only) {
  1662. printf(" %-50s [%s]\n",
  1663. "rNNN",
  1664. event_type_descriptors[PERF_TYPE_RAW]);
  1665. printf(" %-50s [%s]\n",
  1666. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1667. event_type_descriptors[PERF_TYPE_RAW]);
  1668. if (pager_in_use())
  1669. printf(" (see 'man perf-list' on how to encode it)\n\n");
  1670. printf(" %-50s [%s]\n",
  1671. "mem:<addr>[/len][:access]",
  1672. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1673. if (pager_in_use())
  1674. printf("\n");
  1675. }
  1676. print_tracepoint_events(NULL, NULL, name_only);
  1677. }
  1678. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1679. {
  1680. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1681. }
  1682. static int new_term(struct parse_events_term **_term, int type_val,
  1683. int type_term, char *config,
  1684. char *str, u64 num, int err_term, int err_val)
  1685. {
  1686. struct parse_events_term *term;
  1687. term = zalloc(sizeof(*term));
  1688. if (!term)
  1689. return -ENOMEM;
  1690. INIT_LIST_HEAD(&term->list);
  1691. term->type_val = type_val;
  1692. term->type_term = type_term;
  1693. term->config = config;
  1694. term->err_term = err_term;
  1695. term->err_val = err_val;
  1696. switch (type_val) {
  1697. case PARSE_EVENTS__TERM_TYPE_NUM:
  1698. term->val.num = num;
  1699. break;
  1700. case PARSE_EVENTS__TERM_TYPE_STR:
  1701. term->val.str = str;
  1702. break;
  1703. default:
  1704. free(term);
  1705. return -EINVAL;
  1706. }
  1707. *_term = term;
  1708. return 0;
  1709. }
  1710. int parse_events_term__num(struct parse_events_term **term,
  1711. int type_term, char *config, u64 num,
  1712. void *loc_term_, void *loc_val_)
  1713. {
  1714. YYLTYPE *loc_term = loc_term_;
  1715. YYLTYPE *loc_val = loc_val_;
  1716. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1717. config, NULL, num,
  1718. loc_term ? loc_term->first_column : 0,
  1719. loc_val ? loc_val->first_column : 0);
  1720. }
  1721. int parse_events_term__str(struct parse_events_term **term,
  1722. int type_term, char *config, char *str,
  1723. void *loc_term_, void *loc_val_)
  1724. {
  1725. YYLTYPE *loc_term = loc_term_;
  1726. YYLTYPE *loc_val = loc_val_;
  1727. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1728. config, str, 0,
  1729. loc_term ? loc_term->first_column : 0,
  1730. loc_val ? loc_val->first_column : 0);
  1731. }
  1732. int parse_events_term__sym_hw(struct parse_events_term **term,
  1733. char *config, unsigned idx)
  1734. {
  1735. struct event_symbol *sym;
  1736. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1737. sym = &event_symbols_hw[idx];
  1738. if (config)
  1739. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1740. PARSE_EVENTS__TERM_TYPE_USER, config,
  1741. (char *) sym->symbol, 0, 0, 0);
  1742. else
  1743. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1744. PARSE_EVENTS__TERM_TYPE_USER,
  1745. (char *) "event", (char *) sym->symbol,
  1746. 0, 0, 0);
  1747. }
  1748. int parse_events_term__clone(struct parse_events_term **new,
  1749. struct parse_events_term *term)
  1750. {
  1751. return new_term(new, term->type_val, term->type_term, term->config,
  1752. term->val.str, term->val.num,
  1753. term->err_term, term->err_val);
  1754. }
  1755. void parse_events__free_terms(struct list_head *terms)
  1756. {
  1757. struct parse_events_term *term, *h;
  1758. list_for_each_entry_safe(term, h, terms, list)
  1759. free(term);
  1760. }
  1761. void parse_events_evlist_error(struct parse_events_evlist *data,
  1762. int idx, const char *str)
  1763. {
  1764. struct parse_events_error *err = data->error;
  1765. if (!err)
  1766. return;
  1767. err->idx = idx;
  1768. err->str = strdup(str);
  1769. WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
  1770. }
  1771. /*
  1772. * Return string contains valid config terms of an event.
  1773. * @additional_terms: For terms such as PMU sysfs terms.
  1774. */
  1775. char *parse_events_formats_error_string(char *additional_terms)
  1776. {
  1777. char *str;
  1778. static const char *static_terms = "config,config1,config2,name,"
  1779. "period,freq,branch_type,time,"
  1780. "call-graph,stack-size\n";
  1781. /* valid terms */
  1782. if (additional_terms) {
  1783. if (!asprintf(&str, "valid terms: %s,%s",
  1784. additional_terms, static_terms))
  1785. goto fail;
  1786. } else {
  1787. if (!asprintf(&str, "valid terms: %s", static_terms))
  1788. goto fail;
  1789. }
  1790. return str;
  1791. fail:
  1792. return NULL;
  1793. }