parse-events.c 58 KB

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