parse-events.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343
  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) \
  134. while ((sys_dirent = readdir(sys_dir)) != NULL) \
  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) \
  151. while ((evt_dirent = readdir(evt_dir)) != NULL) \
  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_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) {
  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) {
  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 int parse_events__is_name_term(struct parse_events_term *term)
  250. {
  251. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  252. }
  253. static char *get_config_name(struct list_head *head_terms)
  254. {
  255. struct parse_events_term *term;
  256. if (!head_terms)
  257. return NULL;
  258. list_for_each_entry(term, head_terms, list)
  259. if (parse_events__is_name_term(term))
  260. return term->val.str;
  261. return NULL;
  262. }
  263. static struct perf_evsel *
  264. __add_event(struct list_head *list, int *idx,
  265. struct perf_event_attr *attr,
  266. char *name, struct cpu_map *cpus,
  267. struct list_head *config_terms)
  268. {
  269. struct perf_evsel *evsel;
  270. event_attr_init(attr);
  271. evsel = perf_evsel__new_idx(attr, (*idx)++);
  272. if (!evsel)
  273. return NULL;
  274. evsel->cpus = cpu_map__get(cpus);
  275. evsel->own_cpus = cpu_map__get(cpus);
  276. if (name)
  277. evsel->name = strdup(name);
  278. if (config_terms)
  279. list_splice(config_terms, &evsel->config_terms);
  280. list_add_tail(&evsel->node, list);
  281. return evsel;
  282. }
  283. static int add_event(struct list_head *list, int *idx,
  284. struct perf_event_attr *attr, char *name,
  285. struct list_head *config_terms)
  286. {
  287. return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
  288. }
  289. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  290. {
  291. int i, j;
  292. int n, longest = -1;
  293. for (i = 0; i < size; i++) {
  294. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  295. n = strlen(names[i][j]);
  296. if (n > longest && !strncasecmp(str, names[i][j], n))
  297. longest = n;
  298. }
  299. if (longest > 0)
  300. return i;
  301. }
  302. return -1;
  303. }
  304. typedef int config_term_func_t(struct perf_event_attr *attr,
  305. struct parse_events_term *term,
  306. struct parse_events_error *err);
  307. static int config_term_common(struct perf_event_attr *attr,
  308. struct parse_events_term *term,
  309. struct parse_events_error *err);
  310. static int config_attr(struct perf_event_attr *attr,
  311. struct list_head *head,
  312. struct parse_events_error *err,
  313. config_term_func_t config_term);
  314. int parse_events_add_cache(struct list_head *list, int *idx,
  315. char *type, char *op_result1, char *op_result2,
  316. struct parse_events_error *err,
  317. struct list_head *head_config)
  318. {
  319. struct perf_event_attr attr;
  320. LIST_HEAD(config_terms);
  321. char name[MAX_NAME_LEN], *config_name;
  322. int cache_type = -1, cache_op = -1, cache_result = -1;
  323. char *op_result[2] = { op_result1, op_result2 };
  324. int i, n;
  325. /*
  326. * No fallback - if we cannot get a clear cache type
  327. * then bail out:
  328. */
  329. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  330. PERF_COUNT_HW_CACHE_MAX);
  331. if (cache_type == -1)
  332. return -EINVAL;
  333. config_name = get_config_name(head_config);
  334. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  335. for (i = 0; (i < 2) && (op_result[i]); i++) {
  336. char *str = op_result[i];
  337. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  338. if (cache_op == -1) {
  339. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  340. PERF_COUNT_HW_CACHE_OP_MAX);
  341. if (cache_op >= 0) {
  342. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  343. return -EINVAL;
  344. continue;
  345. }
  346. }
  347. if (cache_result == -1) {
  348. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  349. PERF_COUNT_HW_CACHE_RESULT_MAX);
  350. if (cache_result >= 0)
  351. continue;
  352. }
  353. }
  354. /*
  355. * Fall back to reads:
  356. */
  357. if (cache_op == -1)
  358. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  359. /*
  360. * Fall back to accesses:
  361. */
  362. if (cache_result == -1)
  363. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  364. memset(&attr, 0, sizeof(attr));
  365. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  366. attr.type = PERF_TYPE_HW_CACHE;
  367. if (head_config) {
  368. if (config_attr(&attr, head_config, err,
  369. config_term_common))
  370. return -EINVAL;
  371. if (get_config_terms(head_config, &config_terms))
  372. return -ENOMEM;
  373. }
  374. return add_event(list, idx, &attr, config_name ? : name, &config_terms);
  375. }
  376. static void tracepoint_error(struct parse_events_error *e, int err,
  377. char *sys, char *name)
  378. {
  379. char help[BUFSIZ];
  380. if (!e)
  381. return;
  382. /*
  383. * We get error directly from syscall errno ( > 0),
  384. * or from encoded pointer's error ( < 0).
  385. */
  386. err = abs(err);
  387. switch (err) {
  388. case EACCES:
  389. e->str = strdup("can't access trace events");
  390. break;
  391. case ENOENT:
  392. e->str = strdup("unknown tracepoint");
  393. break;
  394. default:
  395. e->str = strdup("failed to add tracepoint");
  396. break;
  397. }
  398. tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
  399. e->help = strdup(help);
  400. }
  401. static int add_tracepoint(struct list_head *list, int *idx,
  402. char *sys_name, char *evt_name,
  403. struct parse_events_error *err,
  404. struct list_head *head_config)
  405. {
  406. struct perf_evsel *evsel;
  407. evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
  408. if (IS_ERR(evsel)) {
  409. tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
  410. return PTR_ERR(evsel);
  411. }
  412. if (head_config) {
  413. LIST_HEAD(config_terms);
  414. if (get_config_terms(head_config, &config_terms))
  415. return -ENOMEM;
  416. list_splice(&config_terms, &evsel->config_terms);
  417. }
  418. list_add_tail(&evsel->node, list);
  419. return 0;
  420. }
  421. static int add_tracepoint_multi_event(struct list_head *list, int *idx,
  422. char *sys_name, char *evt_name,
  423. struct parse_events_error *err,
  424. struct list_head *head_config)
  425. {
  426. char evt_path[MAXPATHLEN];
  427. struct dirent *evt_ent;
  428. DIR *evt_dir;
  429. int ret = 0, found = 0;
  430. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  431. evt_dir = opendir(evt_path);
  432. if (!evt_dir) {
  433. tracepoint_error(err, errno, sys_name, evt_name);
  434. return -1;
  435. }
  436. while (!ret && (evt_ent = readdir(evt_dir))) {
  437. if (!strcmp(evt_ent->d_name, ".")
  438. || !strcmp(evt_ent->d_name, "..")
  439. || !strcmp(evt_ent->d_name, "enable")
  440. || !strcmp(evt_ent->d_name, "filter"))
  441. continue;
  442. if (!strglobmatch(evt_ent->d_name, evt_name))
  443. continue;
  444. found++;
  445. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
  446. err, head_config);
  447. }
  448. if (!found) {
  449. tracepoint_error(err, ENOENT, sys_name, evt_name);
  450. ret = -1;
  451. }
  452. closedir(evt_dir);
  453. return ret;
  454. }
  455. static int add_tracepoint_event(struct list_head *list, int *idx,
  456. char *sys_name, char *evt_name,
  457. struct parse_events_error *err,
  458. struct list_head *head_config)
  459. {
  460. return strpbrk(evt_name, "*?") ?
  461. add_tracepoint_multi_event(list, idx, sys_name, evt_name,
  462. err, head_config) :
  463. add_tracepoint(list, idx, sys_name, evt_name,
  464. err, head_config);
  465. }
  466. static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
  467. char *sys_name, char *evt_name,
  468. struct parse_events_error *err,
  469. struct list_head *head_config)
  470. {
  471. struct dirent *events_ent;
  472. DIR *events_dir;
  473. int ret = 0;
  474. events_dir = opendir(tracing_events_path);
  475. if (!events_dir) {
  476. tracepoint_error(err, errno, sys_name, evt_name);
  477. return -1;
  478. }
  479. while (!ret && (events_ent = readdir(events_dir))) {
  480. if (!strcmp(events_ent->d_name, ".")
  481. || !strcmp(events_ent->d_name, "..")
  482. || !strcmp(events_ent->d_name, "enable")
  483. || !strcmp(events_ent->d_name, "header_event")
  484. || !strcmp(events_ent->d_name, "header_page"))
  485. continue;
  486. if (!strglobmatch(events_ent->d_name, sys_name))
  487. continue;
  488. ret = add_tracepoint_event(list, idx, events_ent->d_name,
  489. evt_name, err, head_config);
  490. }
  491. closedir(events_dir);
  492. return ret;
  493. }
  494. struct __add_bpf_event_param {
  495. struct parse_events_evlist *data;
  496. struct list_head *list;
  497. struct list_head *head_config;
  498. };
  499. static int add_bpf_event(struct probe_trace_event *tev, int fd,
  500. void *_param)
  501. {
  502. LIST_HEAD(new_evsels);
  503. struct __add_bpf_event_param *param = _param;
  504. struct parse_events_evlist *evlist = param->data;
  505. struct list_head *list = param->list;
  506. struct perf_evsel *pos;
  507. int err;
  508. pr_debug("add bpf event %s:%s and attach bpf program %d\n",
  509. tev->group, tev->event, fd);
  510. err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, tev->group,
  511. tev->event, evlist->error,
  512. param->head_config);
  513. if (err) {
  514. struct perf_evsel *evsel, *tmp;
  515. pr_debug("Failed to add BPF event %s:%s\n",
  516. tev->group, tev->event);
  517. list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
  518. list_del(&evsel->node);
  519. perf_evsel__delete(evsel);
  520. }
  521. return err;
  522. }
  523. pr_debug("adding %s:%s\n", tev->group, tev->event);
  524. list_for_each_entry(pos, &new_evsels, node) {
  525. pr_debug("adding %s:%s to %p\n",
  526. tev->group, tev->event, pos);
  527. pos->bpf_fd = fd;
  528. }
  529. list_splice(&new_evsels, list);
  530. return 0;
  531. }
  532. int parse_events_load_bpf_obj(struct parse_events_evlist *data,
  533. struct list_head *list,
  534. struct bpf_object *obj,
  535. struct list_head *head_config)
  536. {
  537. int err;
  538. char errbuf[BUFSIZ];
  539. struct __add_bpf_event_param param = {data, list, head_config};
  540. static bool registered_unprobe_atexit = false;
  541. if (IS_ERR(obj) || !obj) {
  542. snprintf(errbuf, sizeof(errbuf),
  543. "Internal error: load bpf obj with NULL");
  544. err = -EINVAL;
  545. goto errout;
  546. }
  547. /*
  548. * Register atexit handler before calling bpf__probe() so
  549. * bpf__probe() don't need to unprobe probe points its already
  550. * created when failure.
  551. */
  552. if (!registered_unprobe_atexit) {
  553. atexit(bpf__clear);
  554. registered_unprobe_atexit = true;
  555. }
  556. err = bpf__probe(obj);
  557. if (err) {
  558. bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
  559. goto errout;
  560. }
  561. err = bpf__load(obj);
  562. if (err) {
  563. bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
  564. goto errout;
  565. }
  566. err = bpf__foreach_tev(obj, add_bpf_event, &param);
  567. if (err) {
  568. snprintf(errbuf, sizeof(errbuf),
  569. "Attach events in BPF object failed");
  570. goto errout;
  571. }
  572. return 0;
  573. errout:
  574. data->error->help = strdup("(add -v to see detail)");
  575. data->error->str = strdup(errbuf);
  576. return err;
  577. }
  578. static int
  579. parse_events_config_bpf(struct parse_events_evlist *data,
  580. struct bpf_object *obj,
  581. struct list_head *head_config)
  582. {
  583. struct parse_events_term *term;
  584. int error_pos;
  585. if (!head_config || list_empty(head_config))
  586. return 0;
  587. list_for_each_entry(term, head_config, list) {
  588. char errbuf[BUFSIZ];
  589. int err;
  590. if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
  591. snprintf(errbuf, sizeof(errbuf),
  592. "Invalid config term for BPF object");
  593. errbuf[BUFSIZ - 1] = '\0';
  594. data->error->idx = term->err_term;
  595. data->error->str = strdup(errbuf);
  596. return -EINVAL;
  597. }
  598. err = bpf__config_obj(obj, term, data->evlist, &error_pos);
  599. if (err) {
  600. bpf__strerror_config_obj(obj, term, data->evlist,
  601. &error_pos, err, errbuf,
  602. sizeof(errbuf));
  603. data->error->help = strdup(
  604. "Hint:\tValid config terms:\n"
  605. " \tmap:[<arraymap>].value<indices>=[value]\n"
  606. " \tmap:[<eventmap>].event<indices>=[event]\n"
  607. "\n"
  608. " \twhere <indices> is something like [0,3...5] or [all]\n"
  609. " \t(add -v to see detail)");
  610. data->error->str = strdup(errbuf);
  611. if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
  612. data->error->idx = term->err_val;
  613. else
  614. data->error->idx = term->err_term + error_pos;
  615. return err;
  616. }
  617. }
  618. return 0;
  619. }
  620. /*
  621. * Split config terms:
  622. * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
  623. * 'call-graph=fp' is 'evt config', should be applied to each
  624. * events in bpf.c.
  625. * 'map:array.value[0]=1' is 'obj config', should be processed
  626. * with parse_events_config_bpf.
  627. *
  628. * Move object config terms from the first list to obj_head_config.
  629. */
  630. static void
  631. split_bpf_config_terms(struct list_head *evt_head_config,
  632. struct list_head *obj_head_config)
  633. {
  634. struct parse_events_term *term, *temp;
  635. /*
  636. * Currectly, all possible user config term
  637. * belong to bpf object. parse_events__is_hardcoded_term()
  638. * happends to be a good flag.
  639. *
  640. * See parse_events_config_bpf() and
  641. * config_term_tracepoint().
  642. */
  643. list_for_each_entry_safe(term, temp, evt_head_config, list)
  644. if (!parse_events__is_hardcoded_term(term))
  645. list_move_tail(&term->list, obj_head_config);
  646. }
  647. int parse_events_load_bpf(struct parse_events_evlist *data,
  648. struct list_head *list,
  649. char *bpf_file_name,
  650. bool source,
  651. struct list_head *head_config)
  652. {
  653. int err;
  654. struct bpf_object *obj;
  655. LIST_HEAD(obj_head_config);
  656. if (head_config)
  657. split_bpf_config_terms(head_config, &obj_head_config);
  658. obj = bpf__prepare_load(bpf_file_name, source);
  659. if (IS_ERR(obj)) {
  660. char errbuf[BUFSIZ];
  661. err = PTR_ERR(obj);
  662. if (err == -ENOTSUP)
  663. snprintf(errbuf, sizeof(errbuf),
  664. "BPF support is not compiled");
  665. else
  666. bpf__strerror_prepare_load(bpf_file_name,
  667. source,
  668. -err, errbuf,
  669. sizeof(errbuf));
  670. data->error->help = strdup("(add -v to see detail)");
  671. data->error->str = strdup(errbuf);
  672. return err;
  673. }
  674. err = parse_events_load_bpf_obj(data, list, obj, head_config);
  675. if (err)
  676. return err;
  677. err = parse_events_config_bpf(data, obj, &obj_head_config);
  678. /*
  679. * Caller doesn't know anything about obj_head_config,
  680. * so combine them together again before returnning.
  681. */
  682. if (head_config)
  683. list_splice_tail(&obj_head_config, head_config);
  684. return err;
  685. }
  686. static int
  687. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  688. {
  689. int i;
  690. for (i = 0; i < 3; i++) {
  691. if (!type || !type[i])
  692. break;
  693. #define CHECK_SET_TYPE(bit) \
  694. do { \
  695. if (attr->bp_type & bit) \
  696. return -EINVAL; \
  697. else \
  698. attr->bp_type |= bit; \
  699. } while (0)
  700. switch (type[i]) {
  701. case 'r':
  702. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  703. break;
  704. case 'w':
  705. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  706. break;
  707. case 'x':
  708. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  709. break;
  710. default:
  711. return -EINVAL;
  712. }
  713. }
  714. #undef CHECK_SET_TYPE
  715. if (!attr->bp_type) /* Default */
  716. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  717. return 0;
  718. }
  719. int parse_events_add_breakpoint(struct list_head *list, int *idx,
  720. void *ptr, char *type, u64 len)
  721. {
  722. struct perf_event_attr attr;
  723. memset(&attr, 0, sizeof(attr));
  724. attr.bp_addr = (unsigned long) ptr;
  725. if (parse_breakpoint_type(type, &attr))
  726. return -EINVAL;
  727. /* Provide some defaults if len is not specified */
  728. if (!len) {
  729. if (attr.bp_type == HW_BREAKPOINT_X)
  730. len = sizeof(long);
  731. else
  732. len = HW_BREAKPOINT_LEN_4;
  733. }
  734. attr.bp_len = len;
  735. attr.type = PERF_TYPE_BREAKPOINT;
  736. attr.sample_period = 1;
  737. return add_event(list, idx, &attr, NULL, NULL);
  738. }
  739. static int check_type_val(struct parse_events_term *term,
  740. struct parse_events_error *err,
  741. int type)
  742. {
  743. if (type == term->type_val)
  744. return 0;
  745. if (err) {
  746. err->idx = term->err_val;
  747. if (type == PARSE_EVENTS__TERM_TYPE_NUM)
  748. err->str = strdup("expected numeric value");
  749. else
  750. err->str = strdup("expected string value");
  751. }
  752. return -EINVAL;
  753. }
  754. /*
  755. * Update according to parse-events.l
  756. */
  757. static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
  758. [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
  759. [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
  760. [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
  761. [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
  762. [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
  763. [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
  764. [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
  765. [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
  766. [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
  767. [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
  768. [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
  769. [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
  770. [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
  771. };
  772. static bool config_term_shrinked;
  773. static bool
  774. config_term_avail(int term_type, struct parse_events_error *err)
  775. {
  776. if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
  777. err->str = strdup("Invalid term_type");
  778. return false;
  779. }
  780. if (!config_term_shrinked)
  781. return true;
  782. switch (term_type) {
  783. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  784. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  785. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  786. case PARSE_EVENTS__TERM_TYPE_NAME:
  787. return true;
  788. default:
  789. if (!err)
  790. return false;
  791. /* term_type is validated so indexing is safe */
  792. if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
  793. config_term_names[term_type]) < 0)
  794. err->str = NULL;
  795. return false;
  796. }
  797. }
  798. void parse_events__shrink_config_terms(void)
  799. {
  800. config_term_shrinked = true;
  801. }
  802. static int config_term_common(struct perf_event_attr *attr,
  803. struct parse_events_term *term,
  804. struct parse_events_error *err)
  805. {
  806. #define CHECK_TYPE_VAL(type) \
  807. do { \
  808. if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
  809. return -EINVAL; \
  810. } while (0)
  811. switch (term->type_term) {
  812. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  813. CHECK_TYPE_VAL(NUM);
  814. attr->config = term->val.num;
  815. break;
  816. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  817. CHECK_TYPE_VAL(NUM);
  818. attr->config1 = term->val.num;
  819. break;
  820. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  821. CHECK_TYPE_VAL(NUM);
  822. attr->config2 = term->val.num;
  823. break;
  824. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  825. CHECK_TYPE_VAL(NUM);
  826. break;
  827. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  828. CHECK_TYPE_VAL(NUM);
  829. break;
  830. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  831. /*
  832. * TODO uncomment when the field is available
  833. * attr->branch_sample_type = term->val.num;
  834. */
  835. break;
  836. case PARSE_EVENTS__TERM_TYPE_TIME:
  837. CHECK_TYPE_VAL(NUM);
  838. if (term->val.num > 1) {
  839. err->str = strdup("expected 0 or 1");
  840. err->idx = term->err_val;
  841. return -EINVAL;
  842. }
  843. break;
  844. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  845. CHECK_TYPE_VAL(STR);
  846. break;
  847. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  848. CHECK_TYPE_VAL(NUM);
  849. break;
  850. case PARSE_EVENTS__TERM_TYPE_INHERIT:
  851. CHECK_TYPE_VAL(NUM);
  852. break;
  853. case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  854. CHECK_TYPE_VAL(NUM);
  855. break;
  856. case PARSE_EVENTS__TERM_TYPE_NAME:
  857. CHECK_TYPE_VAL(STR);
  858. break;
  859. default:
  860. err->str = strdup("unknown term");
  861. err->idx = term->err_term;
  862. err->help = parse_events_formats_error_string(NULL);
  863. return -EINVAL;
  864. }
  865. /*
  866. * Check term availbility after basic checking so
  867. * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
  868. *
  869. * If check availbility at the entry of this function,
  870. * user will see "'<sysfs term>' is not usable in 'perf stat'"
  871. * if an invalid config term is provided for legacy events
  872. * (for example, instructions/badterm/...), which is confusing.
  873. */
  874. if (!config_term_avail(term->type_term, err))
  875. return -EINVAL;
  876. return 0;
  877. #undef CHECK_TYPE_VAL
  878. }
  879. static int config_term_pmu(struct perf_event_attr *attr,
  880. struct parse_events_term *term,
  881. struct parse_events_error *err)
  882. {
  883. if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
  884. /*
  885. * Always succeed for sysfs terms, as we dont know
  886. * at this point what type they need to have.
  887. */
  888. return 0;
  889. else
  890. return config_term_common(attr, term, err);
  891. }
  892. static int config_term_tracepoint(struct perf_event_attr *attr,
  893. struct parse_events_term *term,
  894. struct parse_events_error *err)
  895. {
  896. switch (term->type_term) {
  897. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  898. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  899. case PARSE_EVENTS__TERM_TYPE_INHERIT:
  900. case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  901. return config_term_common(attr, term, err);
  902. default:
  903. if (err) {
  904. err->idx = term->err_term;
  905. err->str = strdup("unknown term");
  906. err->help = strdup("valid terms: call-graph,stack-size\n");
  907. }
  908. return -EINVAL;
  909. }
  910. return 0;
  911. }
  912. static int config_attr(struct perf_event_attr *attr,
  913. struct list_head *head,
  914. struct parse_events_error *err,
  915. config_term_func_t config_term)
  916. {
  917. struct parse_events_term *term;
  918. list_for_each_entry(term, head, list)
  919. if (config_term(attr, term, err))
  920. return -EINVAL;
  921. return 0;
  922. }
  923. static int get_config_terms(struct list_head *head_config,
  924. struct list_head *head_terms __maybe_unused)
  925. {
  926. #define ADD_CONFIG_TERM(__type, __name, __val) \
  927. do { \
  928. struct perf_evsel_config_term *__t; \
  929. \
  930. __t = zalloc(sizeof(*__t)); \
  931. if (!__t) \
  932. return -ENOMEM; \
  933. \
  934. INIT_LIST_HEAD(&__t->list); \
  935. __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
  936. __t->val.__name = __val; \
  937. list_add_tail(&__t->list, head_terms); \
  938. } while (0)
  939. struct parse_events_term *term;
  940. list_for_each_entry(term, head_config, list) {
  941. switch (term->type_term) {
  942. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  943. ADD_CONFIG_TERM(PERIOD, period, term->val.num);
  944. break;
  945. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  946. ADD_CONFIG_TERM(FREQ, freq, term->val.num);
  947. break;
  948. case PARSE_EVENTS__TERM_TYPE_TIME:
  949. ADD_CONFIG_TERM(TIME, time, term->val.num);
  950. break;
  951. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  952. ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
  953. break;
  954. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  955. ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
  956. break;
  957. case PARSE_EVENTS__TERM_TYPE_INHERIT:
  958. ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
  959. break;
  960. case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  961. ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
  962. break;
  963. default:
  964. break;
  965. }
  966. }
  967. #undef ADD_EVSEL_CONFIG
  968. return 0;
  969. }
  970. int parse_events_add_tracepoint(struct list_head *list, int *idx,
  971. char *sys, char *event,
  972. struct parse_events_error *err,
  973. struct list_head *head_config)
  974. {
  975. if (head_config) {
  976. struct perf_event_attr attr;
  977. if (config_attr(&attr, head_config, err,
  978. config_term_tracepoint))
  979. return -EINVAL;
  980. }
  981. if (strpbrk(sys, "*?"))
  982. return add_tracepoint_multi_sys(list, idx, sys, event,
  983. err, head_config);
  984. else
  985. return add_tracepoint_event(list, idx, sys, event,
  986. err, head_config);
  987. }
  988. int parse_events_add_numeric(struct parse_events_evlist *data,
  989. struct list_head *list,
  990. u32 type, u64 config,
  991. struct list_head *head_config)
  992. {
  993. struct perf_event_attr attr;
  994. LIST_HEAD(config_terms);
  995. memset(&attr, 0, sizeof(attr));
  996. attr.type = type;
  997. attr.config = config;
  998. if (head_config) {
  999. if (config_attr(&attr, head_config, data->error,
  1000. config_term_common))
  1001. return -EINVAL;
  1002. if (get_config_terms(head_config, &config_terms))
  1003. return -ENOMEM;
  1004. }
  1005. return add_event(list, &data->idx, &attr,
  1006. get_config_name(head_config), &config_terms);
  1007. }
  1008. int parse_events_add_pmu(struct parse_events_evlist *data,
  1009. struct list_head *list, char *name,
  1010. struct list_head *head_config)
  1011. {
  1012. struct perf_event_attr attr;
  1013. struct perf_pmu_info info;
  1014. struct perf_pmu *pmu;
  1015. struct perf_evsel *evsel;
  1016. LIST_HEAD(config_terms);
  1017. pmu = perf_pmu__find(name);
  1018. if (!pmu)
  1019. return -EINVAL;
  1020. if (pmu->default_config) {
  1021. memcpy(&attr, pmu->default_config,
  1022. sizeof(struct perf_event_attr));
  1023. } else {
  1024. memset(&attr, 0, sizeof(attr));
  1025. }
  1026. if (!head_config) {
  1027. attr.type = pmu->type;
  1028. evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
  1029. return evsel ? 0 : -ENOMEM;
  1030. }
  1031. if (perf_pmu__check_alias(pmu, head_config, &info))
  1032. return -EINVAL;
  1033. /*
  1034. * Configure hardcoded terms first, no need to check
  1035. * return value when called with fail == 0 ;)
  1036. */
  1037. if (config_attr(&attr, head_config, data->error, config_term_pmu))
  1038. return -EINVAL;
  1039. if (get_config_terms(head_config, &config_terms))
  1040. return -ENOMEM;
  1041. if (perf_pmu__config(pmu, &attr, head_config, data->error))
  1042. return -EINVAL;
  1043. evsel = __add_event(list, &data->idx, &attr,
  1044. get_config_name(head_config), pmu->cpus,
  1045. &config_terms);
  1046. if (evsel) {
  1047. evsel->unit = info.unit;
  1048. evsel->scale = info.scale;
  1049. evsel->per_pkg = info.per_pkg;
  1050. evsel->snapshot = info.snapshot;
  1051. }
  1052. return evsel ? 0 : -ENOMEM;
  1053. }
  1054. int parse_events__modifier_group(struct list_head *list,
  1055. char *event_mod)
  1056. {
  1057. return parse_events__modifier_event(list, event_mod, true);
  1058. }
  1059. void parse_events__set_leader(char *name, struct list_head *list)
  1060. {
  1061. struct perf_evsel *leader;
  1062. if (list_empty(list)) {
  1063. WARN_ONCE(true, "WARNING: failed to set leader: empty list");
  1064. return;
  1065. }
  1066. __perf_evlist__set_leader(list);
  1067. leader = list_entry(list->next, struct perf_evsel, node);
  1068. leader->group_name = name ? strdup(name) : NULL;
  1069. }
  1070. /* list_event is assumed to point to malloc'ed memory */
  1071. void parse_events_update_lists(struct list_head *list_event,
  1072. struct list_head *list_all)
  1073. {
  1074. /*
  1075. * Called for single event definition. Update the
  1076. * 'all event' list, and reinit the 'single event'
  1077. * list, for next event definition.
  1078. */
  1079. list_splice_tail(list_event, list_all);
  1080. free(list_event);
  1081. }
  1082. struct event_modifier {
  1083. int eu;
  1084. int ek;
  1085. int eh;
  1086. int eH;
  1087. int eG;
  1088. int eI;
  1089. int precise;
  1090. int precise_max;
  1091. int exclude_GH;
  1092. int sample_read;
  1093. int pinned;
  1094. };
  1095. static int get_event_modifier(struct event_modifier *mod, char *str,
  1096. struct perf_evsel *evsel)
  1097. {
  1098. int eu = evsel ? evsel->attr.exclude_user : 0;
  1099. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  1100. int eh = evsel ? evsel->attr.exclude_hv : 0;
  1101. int eH = evsel ? evsel->attr.exclude_host : 0;
  1102. int eG = evsel ? evsel->attr.exclude_guest : 0;
  1103. int eI = evsel ? evsel->attr.exclude_idle : 0;
  1104. int precise = evsel ? evsel->attr.precise_ip : 0;
  1105. int precise_max = 0;
  1106. int sample_read = 0;
  1107. int pinned = evsel ? evsel->attr.pinned : 0;
  1108. int exclude = eu | ek | eh;
  1109. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  1110. memset(mod, 0, sizeof(*mod));
  1111. while (*str) {
  1112. if (*str == 'u') {
  1113. if (!exclude)
  1114. exclude = eu = ek = eh = 1;
  1115. eu = 0;
  1116. } else if (*str == 'k') {
  1117. if (!exclude)
  1118. exclude = eu = ek = eh = 1;
  1119. ek = 0;
  1120. } else if (*str == 'h') {
  1121. if (!exclude)
  1122. exclude = eu = ek = eh = 1;
  1123. eh = 0;
  1124. } else if (*str == 'G') {
  1125. if (!exclude_GH)
  1126. exclude_GH = eG = eH = 1;
  1127. eG = 0;
  1128. } else if (*str == 'H') {
  1129. if (!exclude_GH)
  1130. exclude_GH = eG = eH = 1;
  1131. eH = 0;
  1132. } else if (*str == 'I') {
  1133. eI = 1;
  1134. } else if (*str == 'p') {
  1135. precise++;
  1136. /* use of precise requires exclude_guest */
  1137. if (!exclude_GH)
  1138. eG = 1;
  1139. } else if (*str == 'P') {
  1140. precise_max = 1;
  1141. } else if (*str == 'S') {
  1142. sample_read = 1;
  1143. } else if (*str == 'D') {
  1144. pinned = 1;
  1145. } else
  1146. break;
  1147. ++str;
  1148. }
  1149. /*
  1150. * precise ip:
  1151. *
  1152. * 0 - SAMPLE_IP can have arbitrary skid
  1153. * 1 - SAMPLE_IP must have constant skid
  1154. * 2 - SAMPLE_IP requested to have 0 skid
  1155. * 3 - SAMPLE_IP must have 0 skid
  1156. *
  1157. * See also PERF_RECORD_MISC_EXACT_IP
  1158. */
  1159. if (precise > 3)
  1160. return -EINVAL;
  1161. mod->eu = eu;
  1162. mod->ek = ek;
  1163. mod->eh = eh;
  1164. mod->eH = eH;
  1165. mod->eG = eG;
  1166. mod->eI = eI;
  1167. mod->precise = precise;
  1168. mod->precise_max = precise_max;
  1169. mod->exclude_GH = exclude_GH;
  1170. mod->sample_read = sample_read;
  1171. mod->pinned = pinned;
  1172. return 0;
  1173. }
  1174. /*
  1175. * Basic modifier sanity check to validate it contains only one
  1176. * instance of any modifier (apart from 'p') present.
  1177. */
  1178. static int check_modifier(char *str)
  1179. {
  1180. char *p = str;
  1181. /* The sizeof includes 0 byte as well. */
  1182. if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
  1183. return -1;
  1184. while (*p) {
  1185. if (*p != 'p' && strchr(p + 1, *p))
  1186. return -1;
  1187. p++;
  1188. }
  1189. return 0;
  1190. }
  1191. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  1192. {
  1193. struct perf_evsel *evsel;
  1194. struct event_modifier mod;
  1195. if (str == NULL)
  1196. return 0;
  1197. if (check_modifier(str))
  1198. return -EINVAL;
  1199. if (!add && get_event_modifier(&mod, str, NULL))
  1200. return -EINVAL;
  1201. __evlist__for_each(list, evsel) {
  1202. if (add && get_event_modifier(&mod, str, evsel))
  1203. return -EINVAL;
  1204. evsel->attr.exclude_user = mod.eu;
  1205. evsel->attr.exclude_kernel = mod.ek;
  1206. evsel->attr.exclude_hv = mod.eh;
  1207. evsel->attr.precise_ip = mod.precise;
  1208. evsel->attr.exclude_host = mod.eH;
  1209. evsel->attr.exclude_guest = mod.eG;
  1210. evsel->attr.exclude_idle = mod.eI;
  1211. evsel->exclude_GH = mod.exclude_GH;
  1212. evsel->sample_read = mod.sample_read;
  1213. evsel->precise_max = mod.precise_max;
  1214. if (perf_evsel__is_group_leader(evsel))
  1215. evsel->attr.pinned = mod.pinned;
  1216. }
  1217. return 0;
  1218. }
  1219. int parse_events_name(struct list_head *list, char *name)
  1220. {
  1221. struct perf_evsel *evsel;
  1222. __evlist__for_each(list, evsel) {
  1223. if (!evsel->name)
  1224. evsel->name = strdup(name);
  1225. }
  1226. return 0;
  1227. }
  1228. static int
  1229. comp_pmu(const void *p1, const void *p2)
  1230. {
  1231. struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
  1232. struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
  1233. return strcmp(pmu1->symbol, pmu2->symbol);
  1234. }
  1235. static void perf_pmu__parse_cleanup(void)
  1236. {
  1237. if (perf_pmu_events_list_num > 0) {
  1238. struct perf_pmu_event_symbol *p;
  1239. int i;
  1240. for (i = 0; i < perf_pmu_events_list_num; i++) {
  1241. p = perf_pmu_events_list + i;
  1242. free(p->symbol);
  1243. }
  1244. free(perf_pmu_events_list);
  1245. perf_pmu_events_list = NULL;
  1246. perf_pmu_events_list_num = 0;
  1247. }
  1248. }
  1249. #define SET_SYMBOL(str, stype) \
  1250. do { \
  1251. p->symbol = str; \
  1252. if (!p->symbol) \
  1253. goto err; \
  1254. p->type = stype; \
  1255. } while (0)
  1256. /*
  1257. * Read the pmu events list from sysfs
  1258. * Save it into perf_pmu_events_list
  1259. */
  1260. static void perf_pmu__parse_init(void)
  1261. {
  1262. struct perf_pmu *pmu = NULL;
  1263. struct perf_pmu_alias *alias;
  1264. int len = 0;
  1265. pmu = perf_pmu__find("cpu");
  1266. if ((pmu == NULL) || list_empty(&pmu->aliases)) {
  1267. perf_pmu_events_list_num = -1;
  1268. return;
  1269. }
  1270. list_for_each_entry(alias, &pmu->aliases, list) {
  1271. if (strchr(alias->name, '-'))
  1272. len++;
  1273. len++;
  1274. }
  1275. perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
  1276. if (!perf_pmu_events_list)
  1277. return;
  1278. perf_pmu_events_list_num = len;
  1279. len = 0;
  1280. list_for_each_entry(alias, &pmu->aliases, list) {
  1281. struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
  1282. char *tmp = strchr(alias->name, '-');
  1283. if (tmp != NULL) {
  1284. SET_SYMBOL(strndup(alias->name, tmp - alias->name),
  1285. PMU_EVENT_SYMBOL_PREFIX);
  1286. p++;
  1287. SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
  1288. len += 2;
  1289. } else {
  1290. SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
  1291. len++;
  1292. }
  1293. }
  1294. qsort(perf_pmu_events_list, len,
  1295. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  1296. return;
  1297. err:
  1298. perf_pmu__parse_cleanup();
  1299. }
  1300. enum perf_pmu_event_symbol_type
  1301. perf_pmu__parse_check(const char *name)
  1302. {
  1303. struct perf_pmu_event_symbol p, *r;
  1304. /* scan kernel pmu events from sysfs if needed */
  1305. if (perf_pmu_events_list_num == 0)
  1306. perf_pmu__parse_init();
  1307. /*
  1308. * name "cpu" could be prefix of cpu-cycles or cpu// events.
  1309. * cpu-cycles has been handled by hardcode.
  1310. * So it must be cpu// events, not kernel pmu event.
  1311. */
  1312. if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
  1313. return PMU_EVENT_SYMBOL_ERR;
  1314. p.symbol = strdup(name);
  1315. r = bsearch(&p, perf_pmu_events_list,
  1316. (size_t) perf_pmu_events_list_num,
  1317. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  1318. free(p.symbol);
  1319. return r ? r->type : PMU_EVENT_SYMBOL_ERR;
  1320. }
  1321. static int parse_events__scanner(const char *str, void *data, int start_token)
  1322. {
  1323. YY_BUFFER_STATE buffer;
  1324. void *scanner;
  1325. int ret;
  1326. ret = parse_events_lex_init_extra(start_token, &scanner);
  1327. if (ret)
  1328. return ret;
  1329. buffer = parse_events__scan_string(str, scanner);
  1330. #ifdef PARSER_DEBUG
  1331. parse_events_debug = 1;
  1332. #endif
  1333. ret = parse_events_parse(data, scanner);
  1334. parse_events__flush_buffer(buffer, scanner);
  1335. parse_events__delete_buffer(buffer, scanner);
  1336. parse_events_lex_destroy(scanner);
  1337. return ret;
  1338. }
  1339. /*
  1340. * parse event config string, return a list of event terms.
  1341. */
  1342. int parse_events_terms(struct list_head *terms, const char *str)
  1343. {
  1344. struct parse_events_terms data = {
  1345. .terms = NULL,
  1346. };
  1347. int ret;
  1348. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  1349. if (!ret) {
  1350. list_splice(data.terms, terms);
  1351. zfree(&data.terms);
  1352. return 0;
  1353. }
  1354. parse_events_terms__delete(data.terms);
  1355. return ret;
  1356. }
  1357. int parse_events(struct perf_evlist *evlist, const char *str,
  1358. struct parse_events_error *err)
  1359. {
  1360. struct parse_events_evlist data = {
  1361. .list = LIST_HEAD_INIT(data.list),
  1362. .idx = evlist->nr_entries,
  1363. .error = err,
  1364. .evlist = evlist,
  1365. };
  1366. int ret;
  1367. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  1368. perf_pmu__parse_cleanup();
  1369. if (!ret) {
  1370. struct perf_evsel *last;
  1371. if (list_empty(&data.list)) {
  1372. WARN_ONCE(true, "WARNING: event parser found nothing");
  1373. return -1;
  1374. }
  1375. perf_evlist__splice_list_tail(evlist, &data.list);
  1376. evlist->nr_groups += data.nr_groups;
  1377. last = perf_evlist__last(evlist);
  1378. last->cmdline_group_boundary = true;
  1379. return 0;
  1380. }
  1381. /*
  1382. * There are 2 users - builtin-record and builtin-test objects.
  1383. * Both call perf_evlist__delete in case of error, so we dont
  1384. * need to bother.
  1385. */
  1386. return ret;
  1387. }
  1388. #define MAX_WIDTH 1000
  1389. static int get_term_width(void)
  1390. {
  1391. struct winsize ws;
  1392. get_term_dimensions(&ws);
  1393. return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
  1394. }
  1395. static void parse_events_print_error(struct parse_events_error *err,
  1396. const char *event)
  1397. {
  1398. const char *str = "invalid or unsupported event: ";
  1399. char _buf[MAX_WIDTH];
  1400. char *buf = (char *) event;
  1401. int idx = 0;
  1402. if (err->str) {
  1403. /* -2 for extra '' in the final fprintf */
  1404. int width = get_term_width() - 2;
  1405. int len_event = strlen(event);
  1406. int len_str, max_len, cut = 0;
  1407. /*
  1408. * Maximum error index indent, we will cut
  1409. * the event string if it's bigger.
  1410. */
  1411. int max_err_idx = 13;
  1412. /*
  1413. * Let's be specific with the message when
  1414. * we have the precise error.
  1415. */
  1416. str = "event syntax error: ";
  1417. len_str = strlen(str);
  1418. max_len = width - len_str;
  1419. buf = _buf;
  1420. /* We're cutting from the beginning. */
  1421. if (err->idx > max_err_idx)
  1422. cut = err->idx - max_err_idx;
  1423. strncpy(buf, event + cut, max_len);
  1424. /* Mark cut parts with '..' on both sides. */
  1425. if (cut)
  1426. buf[0] = buf[1] = '.';
  1427. if ((len_event - cut) > max_len) {
  1428. buf[max_len - 1] = buf[max_len - 2] = '.';
  1429. buf[max_len] = 0;
  1430. }
  1431. idx = len_str + err->idx - cut;
  1432. }
  1433. fprintf(stderr, "%s'%s'\n", str, buf);
  1434. if (idx) {
  1435. fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
  1436. if (err->help)
  1437. fprintf(stderr, "\n%s\n", err->help);
  1438. free(err->str);
  1439. free(err->help);
  1440. }
  1441. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  1442. }
  1443. #undef MAX_WIDTH
  1444. int parse_events_option(const struct option *opt, const char *str,
  1445. int unset __maybe_unused)
  1446. {
  1447. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1448. struct parse_events_error err = { .idx = 0, };
  1449. int ret = parse_events(evlist, str, &err);
  1450. if (ret)
  1451. parse_events_print_error(&err, str);
  1452. return ret;
  1453. }
  1454. static int
  1455. foreach_evsel_in_last_glob(struct perf_evlist *evlist,
  1456. int (*func)(struct perf_evsel *evsel,
  1457. const void *arg),
  1458. const void *arg)
  1459. {
  1460. struct perf_evsel *last = NULL;
  1461. int err;
  1462. /*
  1463. * Don't return when list_empty, give func a chance to report
  1464. * error when it found last == NULL.
  1465. *
  1466. * So no need to WARN here, let *func do this.
  1467. */
  1468. if (evlist->nr_entries > 0)
  1469. last = perf_evlist__last(evlist);
  1470. do {
  1471. err = (*func)(last, arg);
  1472. if (err)
  1473. return -1;
  1474. if (!last)
  1475. return 0;
  1476. if (last->node.prev == &evlist->entries)
  1477. return 0;
  1478. last = list_entry(last->node.prev, struct perf_evsel, node);
  1479. } while (!last->cmdline_group_boundary);
  1480. return 0;
  1481. }
  1482. static int set_filter(struct perf_evsel *evsel, const void *arg)
  1483. {
  1484. const char *str = arg;
  1485. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1486. fprintf(stderr,
  1487. "--filter option should follow a -e tracepoint option\n");
  1488. return -1;
  1489. }
  1490. if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
  1491. fprintf(stderr,
  1492. "not enough memory to hold filter string\n");
  1493. return -1;
  1494. }
  1495. return 0;
  1496. }
  1497. int parse_filter(const struct option *opt, const char *str,
  1498. int unset __maybe_unused)
  1499. {
  1500. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1501. return foreach_evsel_in_last_glob(evlist, set_filter,
  1502. (const void *)str);
  1503. }
  1504. static int add_exclude_perf_filter(struct perf_evsel *evsel,
  1505. const void *arg __maybe_unused)
  1506. {
  1507. char new_filter[64];
  1508. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1509. fprintf(stderr,
  1510. "--exclude-perf option should follow a -e tracepoint option\n");
  1511. return -1;
  1512. }
  1513. snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
  1514. if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
  1515. fprintf(stderr,
  1516. "not enough memory to hold filter string\n");
  1517. return -1;
  1518. }
  1519. return 0;
  1520. }
  1521. int exclude_perf(const struct option *opt,
  1522. const char *arg __maybe_unused,
  1523. int unset __maybe_unused)
  1524. {
  1525. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1526. return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
  1527. NULL);
  1528. }
  1529. static const char * const event_type_descriptors[] = {
  1530. "Hardware event",
  1531. "Software event",
  1532. "Tracepoint event",
  1533. "Hardware cache event",
  1534. "Raw hardware event descriptor",
  1535. "Hardware breakpoint",
  1536. };
  1537. static int cmp_string(const void *a, const void *b)
  1538. {
  1539. const char * const *as = a;
  1540. const char * const *bs = b;
  1541. return strcmp(*as, *bs);
  1542. }
  1543. /*
  1544. * Print the events from <debugfs_mount_point>/tracing/events
  1545. */
  1546. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  1547. bool name_only)
  1548. {
  1549. DIR *sys_dir, *evt_dir;
  1550. struct dirent *sys_dirent, *evt_dirent;
  1551. char evt_path[MAXPATHLEN];
  1552. char dir_path[MAXPATHLEN];
  1553. char **evt_list = NULL;
  1554. unsigned int evt_i = 0, evt_num = 0;
  1555. bool evt_num_known = false;
  1556. restart:
  1557. sys_dir = opendir(tracing_events_path);
  1558. if (!sys_dir)
  1559. return;
  1560. if (evt_num_known) {
  1561. evt_list = zalloc(sizeof(char *) * evt_num);
  1562. if (!evt_list)
  1563. goto out_close_sys_dir;
  1564. }
  1565. for_each_subsystem(sys_dir, sys_dirent) {
  1566. if (subsys_glob != NULL &&
  1567. !strglobmatch(sys_dirent->d_name, subsys_glob))
  1568. continue;
  1569. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1570. sys_dirent->d_name);
  1571. evt_dir = opendir(dir_path);
  1572. if (!evt_dir)
  1573. continue;
  1574. for_each_event(sys_dirent, evt_dir, evt_dirent) {
  1575. if (event_glob != NULL &&
  1576. !strglobmatch(evt_dirent->d_name, event_glob))
  1577. continue;
  1578. if (!evt_num_known) {
  1579. evt_num++;
  1580. continue;
  1581. }
  1582. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1583. sys_dirent->d_name, evt_dirent->d_name);
  1584. evt_list[evt_i] = strdup(evt_path);
  1585. if (evt_list[evt_i] == NULL)
  1586. goto out_close_evt_dir;
  1587. evt_i++;
  1588. }
  1589. closedir(evt_dir);
  1590. }
  1591. closedir(sys_dir);
  1592. if (!evt_num_known) {
  1593. evt_num_known = true;
  1594. goto restart;
  1595. }
  1596. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1597. evt_i = 0;
  1598. while (evt_i < evt_num) {
  1599. if (name_only) {
  1600. printf("%s ", evt_list[evt_i++]);
  1601. continue;
  1602. }
  1603. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1604. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1605. }
  1606. if (evt_num && pager_in_use())
  1607. printf("\n");
  1608. out_free:
  1609. evt_num = evt_i;
  1610. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1611. zfree(&evt_list[evt_i]);
  1612. zfree(&evt_list);
  1613. return;
  1614. out_close_evt_dir:
  1615. closedir(evt_dir);
  1616. out_close_sys_dir:
  1617. closedir(sys_dir);
  1618. printf("FATAL: not enough memory to print %s\n",
  1619. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1620. if (evt_list)
  1621. goto out_free;
  1622. }
  1623. /*
  1624. * Check whether event is in <debugfs_mount_point>/tracing/events
  1625. */
  1626. int is_valid_tracepoint(const char *event_string)
  1627. {
  1628. DIR *sys_dir, *evt_dir;
  1629. struct dirent *sys_dirent, *evt_dirent;
  1630. char evt_path[MAXPATHLEN];
  1631. char dir_path[MAXPATHLEN];
  1632. sys_dir = opendir(tracing_events_path);
  1633. if (!sys_dir)
  1634. return 0;
  1635. for_each_subsystem(sys_dir, sys_dirent) {
  1636. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1637. sys_dirent->d_name);
  1638. evt_dir = opendir(dir_path);
  1639. if (!evt_dir)
  1640. continue;
  1641. for_each_event(sys_dirent, evt_dir, evt_dirent) {
  1642. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1643. sys_dirent->d_name, evt_dirent->d_name);
  1644. if (!strcmp(evt_path, event_string)) {
  1645. closedir(evt_dir);
  1646. closedir(sys_dir);
  1647. return 1;
  1648. }
  1649. }
  1650. closedir(evt_dir);
  1651. }
  1652. closedir(sys_dir);
  1653. return 0;
  1654. }
  1655. static bool is_event_supported(u8 type, unsigned config)
  1656. {
  1657. bool ret = true;
  1658. int open_return;
  1659. struct perf_evsel *evsel;
  1660. struct perf_event_attr attr = {
  1661. .type = type,
  1662. .config = config,
  1663. .disabled = 1,
  1664. };
  1665. struct {
  1666. struct thread_map map;
  1667. int threads[1];
  1668. } tmap = {
  1669. .map.nr = 1,
  1670. .threads = { 0 },
  1671. };
  1672. evsel = perf_evsel__new(&attr);
  1673. if (evsel) {
  1674. open_return = perf_evsel__open(evsel, NULL, &tmap.map);
  1675. ret = open_return >= 0;
  1676. if (open_return == -EACCES) {
  1677. /*
  1678. * This happens if the paranoid value
  1679. * /proc/sys/kernel/perf_event_paranoid is set to 2
  1680. * Re-run with exclude_kernel set; we don't do that
  1681. * by default as some ARM machines do not support it.
  1682. *
  1683. */
  1684. evsel->attr.exclude_kernel = 1;
  1685. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  1686. }
  1687. perf_evsel__delete(evsel);
  1688. }
  1689. return ret;
  1690. }
  1691. int print_hwcache_events(const char *event_glob, bool name_only)
  1692. {
  1693. unsigned int type, op, i, evt_i = 0, evt_num = 0;
  1694. char name[64];
  1695. char **evt_list = NULL;
  1696. bool evt_num_known = false;
  1697. restart:
  1698. if (evt_num_known) {
  1699. evt_list = zalloc(sizeof(char *) * evt_num);
  1700. if (!evt_list)
  1701. goto out_enomem;
  1702. }
  1703. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  1704. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  1705. /* skip invalid cache type */
  1706. if (!perf_evsel__is_cache_op_valid(type, op))
  1707. continue;
  1708. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  1709. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  1710. name, sizeof(name));
  1711. if (event_glob != NULL && !strglobmatch(name, event_glob))
  1712. continue;
  1713. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  1714. type | (op << 8) | (i << 16)))
  1715. continue;
  1716. if (!evt_num_known) {
  1717. evt_num++;
  1718. continue;
  1719. }
  1720. evt_list[evt_i] = strdup(name);
  1721. if (evt_list[evt_i] == NULL)
  1722. goto out_enomem;
  1723. evt_i++;
  1724. }
  1725. }
  1726. }
  1727. if (!evt_num_known) {
  1728. evt_num_known = true;
  1729. goto restart;
  1730. }
  1731. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1732. evt_i = 0;
  1733. while (evt_i < evt_num) {
  1734. if (name_only) {
  1735. printf("%s ", evt_list[evt_i++]);
  1736. continue;
  1737. }
  1738. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1739. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1740. }
  1741. if (evt_num && pager_in_use())
  1742. printf("\n");
  1743. out_free:
  1744. evt_num = evt_i;
  1745. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1746. zfree(&evt_list[evt_i]);
  1747. zfree(&evt_list);
  1748. return evt_num;
  1749. out_enomem:
  1750. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1751. if (evt_list)
  1752. goto out_free;
  1753. return evt_num;
  1754. }
  1755. void print_symbol_events(const char *event_glob, unsigned type,
  1756. struct event_symbol *syms, unsigned max,
  1757. bool name_only)
  1758. {
  1759. unsigned int i, evt_i = 0, evt_num = 0;
  1760. char name[MAX_NAME_LEN];
  1761. char **evt_list = NULL;
  1762. bool evt_num_known = false;
  1763. restart:
  1764. if (evt_num_known) {
  1765. evt_list = zalloc(sizeof(char *) * evt_num);
  1766. if (!evt_list)
  1767. goto out_enomem;
  1768. syms -= max;
  1769. }
  1770. for (i = 0; i < max; i++, syms++) {
  1771. if (event_glob != NULL && syms->symbol != NULL &&
  1772. !(strglobmatch(syms->symbol, event_glob) ||
  1773. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1774. continue;
  1775. if (!is_event_supported(type, i))
  1776. continue;
  1777. if (!evt_num_known) {
  1778. evt_num++;
  1779. continue;
  1780. }
  1781. if (!name_only && strlen(syms->alias))
  1782. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1783. else
  1784. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1785. evt_list[evt_i] = strdup(name);
  1786. if (evt_list[evt_i] == NULL)
  1787. goto out_enomem;
  1788. evt_i++;
  1789. }
  1790. if (!evt_num_known) {
  1791. evt_num_known = true;
  1792. goto restart;
  1793. }
  1794. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1795. evt_i = 0;
  1796. while (evt_i < evt_num) {
  1797. if (name_only) {
  1798. printf("%s ", evt_list[evt_i++]);
  1799. continue;
  1800. }
  1801. printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
  1802. }
  1803. if (evt_num && pager_in_use())
  1804. printf("\n");
  1805. out_free:
  1806. evt_num = evt_i;
  1807. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1808. zfree(&evt_list[evt_i]);
  1809. zfree(&evt_list);
  1810. return;
  1811. out_enomem:
  1812. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
  1813. if (evt_list)
  1814. goto out_free;
  1815. }
  1816. /*
  1817. * Print the help text for the event symbols:
  1818. */
  1819. void print_events(const char *event_glob, bool name_only)
  1820. {
  1821. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1822. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1823. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1824. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1825. print_hwcache_events(event_glob, name_only);
  1826. print_pmu_events(event_glob, name_only);
  1827. if (event_glob != NULL)
  1828. return;
  1829. if (!name_only) {
  1830. printf(" %-50s [%s]\n",
  1831. "rNNN",
  1832. event_type_descriptors[PERF_TYPE_RAW]);
  1833. printf(" %-50s [%s]\n",
  1834. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1835. event_type_descriptors[PERF_TYPE_RAW]);
  1836. if (pager_in_use())
  1837. printf(" (see 'man perf-list' on how to encode it)\n\n");
  1838. printf(" %-50s [%s]\n",
  1839. "mem:<addr>[/len][:access]",
  1840. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1841. if (pager_in_use())
  1842. printf("\n");
  1843. }
  1844. print_tracepoint_events(NULL, NULL, name_only);
  1845. }
  1846. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1847. {
  1848. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1849. }
  1850. static int new_term(struct parse_events_term **_term, int type_val,
  1851. int type_term, char *config,
  1852. char *str, u64 num, int err_term, int err_val)
  1853. {
  1854. struct parse_events_term *term;
  1855. term = zalloc(sizeof(*term));
  1856. if (!term)
  1857. return -ENOMEM;
  1858. INIT_LIST_HEAD(&term->list);
  1859. term->type_val = type_val;
  1860. term->type_term = type_term;
  1861. term->config = config;
  1862. term->err_term = err_term;
  1863. term->err_val = err_val;
  1864. switch (type_val) {
  1865. case PARSE_EVENTS__TERM_TYPE_NUM:
  1866. term->val.num = num;
  1867. break;
  1868. case PARSE_EVENTS__TERM_TYPE_STR:
  1869. term->val.str = str;
  1870. break;
  1871. default:
  1872. free(term);
  1873. return -EINVAL;
  1874. }
  1875. *_term = term;
  1876. return 0;
  1877. }
  1878. int parse_events_term__num(struct parse_events_term **term,
  1879. int type_term, char *config, u64 num,
  1880. void *loc_term_, void *loc_val_)
  1881. {
  1882. YYLTYPE *loc_term = loc_term_;
  1883. YYLTYPE *loc_val = loc_val_;
  1884. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1885. config, NULL, num,
  1886. loc_term ? loc_term->first_column : 0,
  1887. loc_val ? loc_val->first_column : 0);
  1888. }
  1889. int parse_events_term__str(struct parse_events_term **term,
  1890. int type_term, char *config, char *str,
  1891. void *loc_term_, void *loc_val_)
  1892. {
  1893. YYLTYPE *loc_term = loc_term_;
  1894. YYLTYPE *loc_val = loc_val_;
  1895. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1896. config, str, 0,
  1897. loc_term ? loc_term->first_column : 0,
  1898. loc_val ? loc_val->first_column : 0);
  1899. }
  1900. int parse_events_term__sym_hw(struct parse_events_term **term,
  1901. char *config, unsigned idx)
  1902. {
  1903. struct event_symbol *sym;
  1904. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1905. sym = &event_symbols_hw[idx];
  1906. if (config)
  1907. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1908. PARSE_EVENTS__TERM_TYPE_USER, config,
  1909. (char *) sym->symbol, 0, 0, 0);
  1910. else
  1911. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1912. PARSE_EVENTS__TERM_TYPE_USER,
  1913. (char *) "event", (char *) sym->symbol,
  1914. 0, 0, 0);
  1915. }
  1916. int parse_events_term__clone(struct parse_events_term **new,
  1917. struct parse_events_term *term)
  1918. {
  1919. return new_term(new, term->type_val, term->type_term, term->config,
  1920. term->val.str, term->val.num,
  1921. term->err_term, term->err_val);
  1922. }
  1923. void parse_events_terms__purge(struct list_head *terms)
  1924. {
  1925. struct parse_events_term *term, *h;
  1926. list_for_each_entry_safe(term, h, terms, list) {
  1927. if (term->array.nr_ranges)
  1928. free(term->array.ranges);
  1929. list_del_init(&term->list);
  1930. free(term);
  1931. }
  1932. }
  1933. void parse_events_terms__delete(struct list_head *terms)
  1934. {
  1935. if (!terms)
  1936. return;
  1937. parse_events_terms__purge(terms);
  1938. free(terms);
  1939. }
  1940. void parse_events__clear_array(struct parse_events_array *a)
  1941. {
  1942. free(a->ranges);
  1943. }
  1944. void parse_events_evlist_error(struct parse_events_evlist *data,
  1945. int idx, const char *str)
  1946. {
  1947. struct parse_events_error *err = data->error;
  1948. if (!err)
  1949. return;
  1950. err->idx = idx;
  1951. err->str = strdup(str);
  1952. WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
  1953. }
  1954. static void config_terms_list(char *buf, size_t buf_sz)
  1955. {
  1956. int i;
  1957. bool first = true;
  1958. buf[0] = '\0';
  1959. for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
  1960. const char *name = config_term_names[i];
  1961. if (!config_term_avail(i, NULL))
  1962. continue;
  1963. if (!name)
  1964. continue;
  1965. if (name[0] == '<')
  1966. continue;
  1967. if (strlen(buf) + strlen(name) + 2 >= buf_sz)
  1968. return;
  1969. if (!first)
  1970. strcat(buf, ",");
  1971. else
  1972. first = false;
  1973. strcat(buf, name);
  1974. }
  1975. }
  1976. /*
  1977. * Return string contains valid config terms of an event.
  1978. * @additional_terms: For terms such as PMU sysfs terms.
  1979. */
  1980. char *parse_events_formats_error_string(char *additional_terms)
  1981. {
  1982. char *str;
  1983. /* "branch_type" is the longest name */
  1984. char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
  1985. (sizeof("branch_type") - 1)];
  1986. config_terms_list(static_terms, sizeof(static_terms));
  1987. /* valid terms */
  1988. if (additional_terms) {
  1989. if (asprintf(&str, "valid terms: %s,%s",
  1990. additional_terms, static_terms) < 0)
  1991. goto fail;
  1992. } else {
  1993. if (asprintf(&str, "valid terms: %s", static_terms) < 0)
  1994. goto fail;
  1995. }
  1996. return str;
  1997. fail:
  1998. return NULL;
  1999. }