parse-events.c 44 KB

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