parse-events.c 37 KB

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