parse-events.c 48 KB

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