parse-events.c 57 KB

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