parse-events.c 48 KB

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