parse-events.c 57 KB

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