parse-events.c 57 KB

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