parse-events.c 57 KB

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