parse-events.c 37 KB

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