parse-events.c 61 KB

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