parse-events.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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. struct list_head *config_terms)
  246. {
  247. struct perf_evsel *evsel;
  248. event_attr_init(attr);
  249. evsel = perf_evsel__new_idx(attr, (*idx)++);
  250. if (!evsel)
  251. return NULL;
  252. if (cpus)
  253. evsel->cpus = cpu_map__get(cpus);
  254. if (name)
  255. evsel->name = strdup(name);
  256. if (config_terms)
  257. list_splice(config_terms, &evsel->config_terms);
  258. list_add_tail(&evsel->node, list);
  259. return evsel;
  260. }
  261. static int add_event(struct list_head *list, int *idx,
  262. struct perf_event_attr *attr, char *name,
  263. struct list_head *config_terms)
  264. {
  265. return __add_event(list, idx, attr, name, NULL, config_terms) ? 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, NULL);
  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, 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. break;
  509. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  510. CHECK_TYPE_VAL(NUM);
  511. break;
  512. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  513. /*
  514. * TODO uncomment when the field is available
  515. * attr->branch_sample_type = term->val.num;
  516. */
  517. break;
  518. case PARSE_EVENTS__TERM_TYPE_TIME:
  519. CHECK_TYPE_VAL(NUM);
  520. if (term->val.num > 1) {
  521. err->str = strdup("expected 0 or 1");
  522. err->idx = term->err_val;
  523. return -EINVAL;
  524. }
  525. break;
  526. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  527. CHECK_TYPE_VAL(STR);
  528. break;
  529. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  530. CHECK_TYPE_VAL(NUM);
  531. break;
  532. case PARSE_EVENTS__TERM_TYPE_NAME:
  533. CHECK_TYPE_VAL(STR);
  534. break;
  535. default:
  536. return -EINVAL;
  537. }
  538. return 0;
  539. #undef CHECK_TYPE_VAL
  540. }
  541. static int config_attr(struct perf_event_attr *attr,
  542. struct list_head *head,
  543. struct parse_events_error *err)
  544. {
  545. struct parse_events_term *term;
  546. list_for_each_entry(term, head, list)
  547. if (config_term(attr, term, err))
  548. return -EINVAL;
  549. return 0;
  550. }
  551. static int get_config_terms(struct list_head *head_config,
  552. struct list_head *head_terms __maybe_unused)
  553. {
  554. #define ADD_CONFIG_TERM(__type, __name, __val) \
  555. do { \
  556. struct perf_evsel_config_term *__t; \
  557. \
  558. __t = zalloc(sizeof(*__t)); \
  559. if (!__t) \
  560. return -ENOMEM; \
  561. \
  562. INIT_LIST_HEAD(&__t->list); \
  563. __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
  564. __t->val.__name = __val; \
  565. list_add_tail(&__t->list, head_terms); \
  566. } while (0)
  567. struct parse_events_term *term;
  568. list_for_each_entry(term, head_config, list) {
  569. switch (term->type_term) {
  570. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  571. ADD_CONFIG_TERM(PERIOD, period, term->val.num);
  572. break;
  573. case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
  574. ADD_CONFIG_TERM(FREQ, freq, term->val.num);
  575. break;
  576. case PARSE_EVENTS__TERM_TYPE_TIME:
  577. ADD_CONFIG_TERM(TIME, time, term->val.num);
  578. break;
  579. case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
  580. ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
  581. break;
  582. case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  583. ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. #undef ADD_EVSEL_CONFIG
  590. return 0;
  591. }
  592. int parse_events_add_numeric(struct parse_events_evlist *data,
  593. struct list_head *list,
  594. u32 type, u64 config,
  595. struct list_head *head_config)
  596. {
  597. struct perf_event_attr attr;
  598. LIST_HEAD(config_terms);
  599. memset(&attr, 0, sizeof(attr));
  600. attr.type = type;
  601. attr.config = config;
  602. if (head_config) {
  603. if (config_attr(&attr, head_config, data->error))
  604. return -EINVAL;
  605. if (get_config_terms(head_config, &config_terms))
  606. return -ENOMEM;
  607. }
  608. return add_event(list, &data->idx, &attr, NULL, &config_terms);
  609. }
  610. static int parse_events__is_name_term(struct parse_events_term *term)
  611. {
  612. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  613. }
  614. static char *pmu_event_name(struct list_head *head_terms)
  615. {
  616. struct parse_events_term *term;
  617. list_for_each_entry(term, head_terms, list)
  618. if (parse_events__is_name_term(term))
  619. return term->val.str;
  620. return NULL;
  621. }
  622. int parse_events_add_pmu(struct parse_events_evlist *data,
  623. struct list_head *list, char *name,
  624. struct list_head *head_config)
  625. {
  626. struct perf_event_attr attr;
  627. struct perf_pmu_info info;
  628. struct perf_pmu *pmu;
  629. struct perf_evsel *evsel;
  630. LIST_HEAD(config_terms);
  631. pmu = perf_pmu__find(name);
  632. if (!pmu)
  633. return -EINVAL;
  634. if (pmu->default_config) {
  635. memcpy(&attr, pmu->default_config,
  636. sizeof(struct perf_event_attr));
  637. } else {
  638. memset(&attr, 0, sizeof(attr));
  639. }
  640. if (!head_config) {
  641. attr.type = pmu->type;
  642. evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
  643. return evsel ? 0 : -ENOMEM;
  644. }
  645. if (perf_pmu__check_alias(pmu, head_config, &info))
  646. return -EINVAL;
  647. /*
  648. * Configure hardcoded terms first, no need to check
  649. * return value when called with fail == 0 ;)
  650. */
  651. if (config_attr(&attr, head_config, data->error))
  652. return -EINVAL;
  653. if (get_config_terms(head_config, &config_terms))
  654. return -ENOMEM;
  655. if (perf_pmu__config(pmu, &attr, head_config, data->error))
  656. return -EINVAL;
  657. evsel = __add_event(list, &data->idx, &attr,
  658. pmu_event_name(head_config), pmu->cpus,
  659. &config_terms);
  660. if (evsel) {
  661. evsel->unit = info.unit;
  662. evsel->scale = info.scale;
  663. evsel->per_pkg = info.per_pkg;
  664. evsel->snapshot = info.snapshot;
  665. }
  666. return evsel ? 0 : -ENOMEM;
  667. }
  668. int parse_events__modifier_group(struct list_head *list,
  669. char *event_mod)
  670. {
  671. return parse_events__modifier_event(list, event_mod, true);
  672. }
  673. void parse_events__set_leader(char *name, struct list_head *list)
  674. {
  675. struct perf_evsel *leader;
  676. __perf_evlist__set_leader(list);
  677. leader = list_entry(list->next, struct perf_evsel, node);
  678. leader->group_name = name ? strdup(name) : NULL;
  679. }
  680. /* list_event is assumed to point to malloc'ed memory */
  681. void parse_events_update_lists(struct list_head *list_event,
  682. struct list_head *list_all)
  683. {
  684. /*
  685. * Called for single event definition. Update the
  686. * 'all event' list, and reinit the 'single event'
  687. * list, for next event definition.
  688. */
  689. list_splice_tail(list_event, list_all);
  690. free(list_event);
  691. }
  692. struct event_modifier {
  693. int eu;
  694. int ek;
  695. int eh;
  696. int eH;
  697. int eG;
  698. int eI;
  699. int precise;
  700. int exclude_GH;
  701. int sample_read;
  702. int pinned;
  703. };
  704. static int get_event_modifier(struct event_modifier *mod, char *str,
  705. struct perf_evsel *evsel)
  706. {
  707. int eu = evsel ? evsel->attr.exclude_user : 0;
  708. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  709. int eh = evsel ? evsel->attr.exclude_hv : 0;
  710. int eH = evsel ? evsel->attr.exclude_host : 0;
  711. int eG = evsel ? evsel->attr.exclude_guest : 0;
  712. int eI = evsel ? evsel->attr.exclude_idle : 0;
  713. int precise = evsel ? evsel->attr.precise_ip : 0;
  714. int sample_read = 0;
  715. int pinned = evsel ? evsel->attr.pinned : 0;
  716. int exclude = eu | ek | eh;
  717. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  718. memset(mod, 0, sizeof(*mod));
  719. while (*str) {
  720. if (*str == 'u') {
  721. if (!exclude)
  722. exclude = eu = ek = eh = 1;
  723. eu = 0;
  724. } else if (*str == 'k') {
  725. if (!exclude)
  726. exclude = eu = ek = eh = 1;
  727. ek = 0;
  728. } else if (*str == 'h') {
  729. if (!exclude)
  730. exclude = eu = ek = eh = 1;
  731. eh = 0;
  732. } else if (*str == 'G') {
  733. if (!exclude_GH)
  734. exclude_GH = eG = eH = 1;
  735. eG = 0;
  736. } else if (*str == 'H') {
  737. if (!exclude_GH)
  738. exclude_GH = eG = eH = 1;
  739. eH = 0;
  740. } else if (*str == 'I') {
  741. eI = 1;
  742. } else if (*str == 'p') {
  743. precise++;
  744. /* use of precise requires exclude_guest */
  745. if (!exclude_GH)
  746. eG = 1;
  747. } else if (*str == 'S') {
  748. sample_read = 1;
  749. } else if (*str == 'D') {
  750. pinned = 1;
  751. } else
  752. break;
  753. ++str;
  754. }
  755. /*
  756. * precise ip:
  757. *
  758. * 0 - SAMPLE_IP can have arbitrary skid
  759. * 1 - SAMPLE_IP must have constant skid
  760. * 2 - SAMPLE_IP requested to have 0 skid
  761. * 3 - SAMPLE_IP must have 0 skid
  762. *
  763. * See also PERF_RECORD_MISC_EXACT_IP
  764. */
  765. if (precise > 3)
  766. return -EINVAL;
  767. mod->eu = eu;
  768. mod->ek = ek;
  769. mod->eh = eh;
  770. mod->eH = eH;
  771. mod->eG = eG;
  772. mod->eI = eI;
  773. mod->precise = precise;
  774. mod->exclude_GH = exclude_GH;
  775. mod->sample_read = sample_read;
  776. mod->pinned = pinned;
  777. return 0;
  778. }
  779. /*
  780. * Basic modifier sanity check to validate it contains only one
  781. * instance of any modifier (apart from 'p') present.
  782. */
  783. static int check_modifier(char *str)
  784. {
  785. char *p = str;
  786. /* The sizeof includes 0 byte as well. */
  787. if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
  788. return -1;
  789. while (*p) {
  790. if (*p != 'p' && strchr(p + 1, *p))
  791. return -1;
  792. p++;
  793. }
  794. return 0;
  795. }
  796. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  797. {
  798. struct perf_evsel *evsel;
  799. struct event_modifier mod;
  800. if (str == NULL)
  801. return 0;
  802. if (check_modifier(str))
  803. return -EINVAL;
  804. if (!add && get_event_modifier(&mod, str, NULL))
  805. return -EINVAL;
  806. __evlist__for_each(list, evsel) {
  807. if (add && get_event_modifier(&mod, str, evsel))
  808. return -EINVAL;
  809. evsel->attr.exclude_user = mod.eu;
  810. evsel->attr.exclude_kernel = mod.ek;
  811. evsel->attr.exclude_hv = mod.eh;
  812. evsel->attr.precise_ip = mod.precise;
  813. evsel->attr.exclude_host = mod.eH;
  814. evsel->attr.exclude_guest = mod.eG;
  815. evsel->attr.exclude_idle = mod.eI;
  816. evsel->exclude_GH = mod.exclude_GH;
  817. evsel->sample_read = mod.sample_read;
  818. if (perf_evsel__is_group_leader(evsel))
  819. evsel->attr.pinned = mod.pinned;
  820. }
  821. return 0;
  822. }
  823. int parse_events_name(struct list_head *list, char *name)
  824. {
  825. struct perf_evsel *evsel;
  826. __evlist__for_each(list, evsel) {
  827. if (!evsel->name)
  828. evsel->name = strdup(name);
  829. }
  830. return 0;
  831. }
  832. static int
  833. comp_pmu(const void *p1, const void *p2)
  834. {
  835. struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
  836. struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
  837. return strcmp(pmu1->symbol, pmu2->symbol);
  838. }
  839. static void perf_pmu__parse_cleanup(void)
  840. {
  841. if (perf_pmu_events_list_num > 0) {
  842. struct perf_pmu_event_symbol *p;
  843. int i;
  844. for (i = 0; i < perf_pmu_events_list_num; i++) {
  845. p = perf_pmu_events_list + i;
  846. free(p->symbol);
  847. }
  848. free(perf_pmu_events_list);
  849. perf_pmu_events_list = NULL;
  850. perf_pmu_events_list_num = 0;
  851. }
  852. }
  853. #define SET_SYMBOL(str, stype) \
  854. do { \
  855. p->symbol = str; \
  856. if (!p->symbol) \
  857. goto err; \
  858. p->type = stype; \
  859. } while (0)
  860. /*
  861. * Read the pmu events list from sysfs
  862. * Save it into perf_pmu_events_list
  863. */
  864. static void perf_pmu__parse_init(void)
  865. {
  866. struct perf_pmu *pmu = NULL;
  867. struct perf_pmu_alias *alias;
  868. int len = 0;
  869. pmu = perf_pmu__find("cpu");
  870. if ((pmu == NULL) || list_empty(&pmu->aliases)) {
  871. perf_pmu_events_list_num = -1;
  872. return;
  873. }
  874. list_for_each_entry(alias, &pmu->aliases, list) {
  875. if (strchr(alias->name, '-'))
  876. len++;
  877. len++;
  878. }
  879. perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
  880. if (!perf_pmu_events_list)
  881. return;
  882. perf_pmu_events_list_num = len;
  883. len = 0;
  884. list_for_each_entry(alias, &pmu->aliases, list) {
  885. struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
  886. char *tmp = strchr(alias->name, '-');
  887. if (tmp != NULL) {
  888. SET_SYMBOL(strndup(alias->name, tmp - alias->name),
  889. PMU_EVENT_SYMBOL_PREFIX);
  890. p++;
  891. SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
  892. len += 2;
  893. } else {
  894. SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
  895. len++;
  896. }
  897. }
  898. qsort(perf_pmu_events_list, len,
  899. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  900. return;
  901. err:
  902. perf_pmu__parse_cleanup();
  903. }
  904. enum perf_pmu_event_symbol_type
  905. perf_pmu__parse_check(const char *name)
  906. {
  907. struct perf_pmu_event_symbol p, *r;
  908. /* scan kernel pmu events from sysfs if needed */
  909. if (perf_pmu_events_list_num == 0)
  910. perf_pmu__parse_init();
  911. /*
  912. * name "cpu" could be prefix of cpu-cycles or cpu// events.
  913. * cpu-cycles has been handled by hardcode.
  914. * So it must be cpu// events, not kernel pmu event.
  915. */
  916. if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
  917. return PMU_EVENT_SYMBOL_ERR;
  918. p.symbol = strdup(name);
  919. r = bsearch(&p, perf_pmu_events_list,
  920. (size_t) perf_pmu_events_list_num,
  921. sizeof(struct perf_pmu_event_symbol), comp_pmu);
  922. free(p.symbol);
  923. return r ? r->type : PMU_EVENT_SYMBOL_ERR;
  924. }
  925. static int parse_events__scanner(const char *str, void *data, int start_token)
  926. {
  927. YY_BUFFER_STATE buffer;
  928. void *scanner;
  929. int ret;
  930. ret = parse_events_lex_init_extra(start_token, &scanner);
  931. if (ret)
  932. return ret;
  933. buffer = parse_events__scan_string(str, scanner);
  934. #ifdef PARSER_DEBUG
  935. parse_events_debug = 1;
  936. #endif
  937. ret = parse_events_parse(data, scanner);
  938. parse_events__flush_buffer(buffer, scanner);
  939. parse_events__delete_buffer(buffer, scanner);
  940. parse_events_lex_destroy(scanner);
  941. return ret;
  942. }
  943. /*
  944. * parse event config string, return a list of event terms.
  945. */
  946. int parse_events_terms(struct list_head *terms, const char *str)
  947. {
  948. struct parse_events_terms data = {
  949. .terms = NULL,
  950. };
  951. int ret;
  952. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  953. if (!ret) {
  954. list_splice(data.terms, terms);
  955. zfree(&data.terms);
  956. return 0;
  957. }
  958. if (data.terms)
  959. parse_events__free_terms(data.terms);
  960. return ret;
  961. }
  962. int parse_events(struct perf_evlist *evlist, const char *str,
  963. struct parse_events_error *err)
  964. {
  965. struct parse_events_evlist data = {
  966. .list = LIST_HEAD_INIT(data.list),
  967. .idx = evlist->nr_entries,
  968. .error = err,
  969. };
  970. int ret;
  971. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  972. perf_pmu__parse_cleanup();
  973. if (!ret) {
  974. int entries = data.idx - evlist->nr_entries;
  975. struct perf_evsel *last;
  976. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  977. evlist->nr_groups += data.nr_groups;
  978. last = perf_evlist__last(evlist);
  979. last->cmdline_group_boundary = true;
  980. return 0;
  981. }
  982. /*
  983. * There are 2 users - builtin-record and builtin-test objects.
  984. * Both call perf_evlist__delete in case of error, so we dont
  985. * need to bother.
  986. */
  987. return ret;
  988. }
  989. #define MAX_WIDTH 1000
  990. static int get_term_width(void)
  991. {
  992. struct winsize ws;
  993. get_term_dimensions(&ws);
  994. return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
  995. }
  996. static void parse_events_print_error(struct parse_events_error *err,
  997. const char *event)
  998. {
  999. const char *str = "invalid or unsupported event: ";
  1000. char _buf[MAX_WIDTH];
  1001. char *buf = (char *) event;
  1002. int idx = 0;
  1003. if (err->str) {
  1004. /* -2 for extra '' in the final fprintf */
  1005. int width = get_term_width() - 2;
  1006. int len_event = strlen(event);
  1007. int len_str, max_len, cut = 0;
  1008. /*
  1009. * Maximum error index indent, we will cut
  1010. * the event string if it's bigger.
  1011. */
  1012. int max_err_idx = 13;
  1013. /*
  1014. * Let's be specific with the message when
  1015. * we have the precise error.
  1016. */
  1017. str = "event syntax error: ";
  1018. len_str = strlen(str);
  1019. max_len = width - len_str;
  1020. buf = _buf;
  1021. /* We're cutting from the beggining. */
  1022. if (err->idx > max_err_idx)
  1023. cut = err->idx - max_err_idx;
  1024. strncpy(buf, event + cut, max_len);
  1025. /* Mark cut parts with '..' on both sides. */
  1026. if (cut)
  1027. buf[0] = buf[1] = '.';
  1028. if ((len_event - cut) > max_len) {
  1029. buf[max_len - 1] = buf[max_len - 2] = '.';
  1030. buf[max_len] = 0;
  1031. }
  1032. idx = len_str + err->idx - cut;
  1033. }
  1034. fprintf(stderr, "%s'%s'\n", str, buf);
  1035. if (idx) {
  1036. fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
  1037. if (err->help)
  1038. fprintf(stderr, "\n%s\n", err->help);
  1039. free(err->str);
  1040. free(err->help);
  1041. }
  1042. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  1043. }
  1044. #undef MAX_WIDTH
  1045. int parse_events_option(const struct option *opt, const char *str,
  1046. int unset __maybe_unused)
  1047. {
  1048. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1049. struct parse_events_error err = { .idx = 0, };
  1050. int ret = parse_events(evlist, str, &err);
  1051. if (ret)
  1052. parse_events_print_error(&err, str);
  1053. return ret;
  1054. }
  1055. static int
  1056. foreach_evsel_in_last_glob(struct perf_evlist *evlist,
  1057. int (*func)(struct perf_evsel *evsel,
  1058. const void *arg),
  1059. const void *arg)
  1060. {
  1061. struct perf_evsel *last = NULL;
  1062. int err;
  1063. if (evlist->nr_entries > 0)
  1064. last = perf_evlist__last(evlist);
  1065. do {
  1066. err = (*func)(last, arg);
  1067. if (err)
  1068. return -1;
  1069. if (!last)
  1070. return 0;
  1071. if (last->node.prev == &evlist->entries)
  1072. return 0;
  1073. last = list_entry(last->node.prev, struct perf_evsel, node);
  1074. } while (!last->cmdline_group_boundary);
  1075. return 0;
  1076. }
  1077. static int set_filter(struct perf_evsel *evsel, const void *arg)
  1078. {
  1079. const char *str = arg;
  1080. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1081. fprintf(stderr,
  1082. "--filter option should follow a -e tracepoint option\n");
  1083. return -1;
  1084. }
  1085. if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
  1086. fprintf(stderr,
  1087. "not enough memory to hold filter string\n");
  1088. return -1;
  1089. }
  1090. return 0;
  1091. }
  1092. int parse_filter(const struct option *opt, const char *str,
  1093. int unset __maybe_unused)
  1094. {
  1095. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1096. return foreach_evsel_in_last_glob(evlist, set_filter,
  1097. (const void *)str);
  1098. }
  1099. static int add_exclude_perf_filter(struct perf_evsel *evsel,
  1100. const void *arg __maybe_unused)
  1101. {
  1102. char new_filter[64];
  1103. if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1104. fprintf(stderr,
  1105. "--exclude-perf option should follow a -e tracepoint option\n");
  1106. return -1;
  1107. }
  1108. snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
  1109. if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
  1110. fprintf(stderr,
  1111. "not enough memory to hold filter string\n");
  1112. return -1;
  1113. }
  1114. return 0;
  1115. }
  1116. int exclude_perf(const struct option *opt,
  1117. const char *arg __maybe_unused,
  1118. int unset __maybe_unused)
  1119. {
  1120. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  1121. return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
  1122. NULL);
  1123. }
  1124. static const char * const event_type_descriptors[] = {
  1125. "Hardware event",
  1126. "Software event",
  1127. "Tracepoint event",
  1128. "Hardware cache event",
  1129. "Raw hardware event descriptor",
  1130. "Hardware breakpoint",
  1131. };
  1132. static int cmp_string(const void *a, const void *b)
  1133. {
  1134. const char * const *as = a;
  1135. const char * const *bs = b;
  1136. return strcmp(*as, *bs);
  1137. }
  1138. /*
  1139. * Print the events from <debugfs_mount_point>/tracing/events
  1140. */
  1141. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  1142. bool name_only)
  1143. {
  1144. DIR *sys_dir, *evt_dir;
  1145. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1146. char evt_path[MAXPATHLEN];
  1147. char dir_path[MAXPATHLEN];
  1148. char **evt_list = NULL;
  1149. unsigned int evt_i = 0, evt_num = 0;
  1150. bool evt_num_known = false;
  1151. restart:
  1152. sys_dir = opendir(tracing_events_path);
  1153. if (!sys_dir)
  1154. return;
  1155. if (evt_num_known) {
  1156. evt_list = zalloc(sizeof(char *) * evt_num);
  1157. if (!evt_list)
  1158. goto out_close_sys_dir;
  1159. }
  1160. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1161. if (subsys_glob != NULL &&
  1162. !strglobmatch(sys_dirent.d_name, subsys_glob))
  1163. continue;
  1164. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1165. sys_dirent.d_name);
  1166. evt_dir = opendir(dir_path);
  1167. if (!evt_dir)
  1168. continue;
  1169. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1170. if (event_glob != NULL &&
  1171. !strglobmatch(evt_dirent.d_name, event_glob))
  1172. continue;
  1173. if (!evt_num_known) {
  1174. evt_num++;
  1175. continue;
  1176. }
  1177. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1178. sys_dirent.d_name, evt_dirent.d_name);
  1179. evt_list[evt_i] = strdup(evt_path);
  1180. if (evt_list[evt_i] == NULL)
  1181. goto out_close_evt_dir;
  1182. evt_i++;
  1183. }
  1184. closedir(evt_dir);
  1185. }
  1186. closedir(sys_dir);
  1187. if (!evt_num_known) {
  1188. evt_num_known = true;
  1189. goto restart;
  1190. }
  1191. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1192. evt_i = 0;
  1193. while (evt_i < evt_num) {
  1194. if (name_only) {
  1195. printf("%s ", evt_list[evt_i++]);
  1196. continue;
  1197. }
  1198. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1199. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1200. }
  1201. if (evt_num)
  1202. printf("\n");
  1203. out_free:
  1204. evt_num = evt_i;
  1205. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1206. zfree(&evt_list[evt_i]);
  1207. zfree(&evt_list);
  1208. return;
  1209. out_close_evt_dir:
  1210. closedir(evt_dir);
  1211. out_close_sys_dir:
  1212. closedir(sys_dir);
  1213. printf("FATAL: not enough memory to print %s\n",
  1214. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  1215. if (evt_list)
  1216. goto out_free;
  1217. }
  1218. /*
  1219. * Check whether event is in <debugfs_mount_point>/tracing/events
  1220. */
  1221. int is_valid_tracepoint(const char *event_string)
  1222. {
  1223. DIR *sys_dir, *evt_dir;
  1224. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  1225. char evt_path[MAXPATHLEN];
  1226. char dir_path[MAXPATHLEN];
  1227. sys_dir = opendir(tracing_events_path);
  1228. if (!sys_dir)
  1229. return 0;
  1230. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  1231. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  1232. sys_dirent.d_name);
  1233. evt_dir = opendir(dir_path);
  1234. if (!evt_dir)
  1235. continue;
  1236. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  1237. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  1238. sys_dirent.d_name, evt_dirent.d_name);
  1239. if (!strcmp(evt_path, event_string)) {
  1240. closedir(evt_dir);
  1241. closedir(sys_dir);
  1242. return 1;
  1243. }
  1244. }
  1245. closedir(evt_dir);
  1246. }
  1247. closedir(sys_dir);
  1248. return 0;
  1249. }
  1250. static bool is_event_supported(u8 type, unsigned config)
  1251. {
  1252. bool ret = true;
  1253. int open_return;
  1254. struct perf_evsel *evsel;
  1255. struct perf_event_attr attr = {
  1256. .type = type,
  1257. .config = config,
  1258. .disabled = 1,
  1259. };
  1260. struct {
  1261. struct thread_map map;
  1262. int threads[1];
  1263. } tmap = {
  1264. .map.nr = 1,
  1265. .threads = { 0 },
  1266. };
  1267. evsel = perf_evsel__new(&attr);
  1268. if (evsel) {
  1269. open_return = perf_evsel__open(evsel, NULL, &tmap.map);
  1270. ret = open_return >= 0;
  1271. if (open_return == -EACCES) {
  1272. /*
  1273. * This happens if the paranoid value
  1274. * /proc/sys/kernel/perf_event_paranoid is set to 2
  1275. * Re-run with exclude_kernel set; we don't do that
  1276. * by default as some ARM machines do not support it.
  1277. *
  1278. */
  1279. evsel->attr.exclude_kernel = 1;
  1280. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  1281. }
  1282. perf_evsel__delete(evsel);
  1283. }
  1284. return ret;
  1285. }
  1286. int print_hwcache_events(const char *event_glob, bool name_only)
  1287. {
  1288. unsigned int type, op, i, evt_i = 0, evt_num = 0;
  1289. char name[64];
  1290. char **evt_list = NULL;
  1291. bool evt_num_known = false;
  1292. restart:
  1293. if (evt_num_known) {
  1294. evt_list = zalloc(sizeof(char *) * evt_num);
  1295. if (!evt_list)
  1296. goto out_enomem;
  1297. }
  1298. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  1299. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  1300. /* skip invalid cache type */
  1301. if (!perf_evsel__is_cache_op_valid(type, op))
  1302. continue;
  1303. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  1304. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  1305. name, sizeof(name));
  1306. if (event_glob != NULL && !strglobmatch(name, event_glob))
  1307. continue;
  1308. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  1309. type | (op << 8) | (i << 16)))
  1310. continue;
  1311. if (!evt_num_known) {
  1312. evt_num++;
  1313. continue;
  1314. }
  1315. evt_list[evt_i] = strdup(name);
  1316. if (evt_list[evt_i] == NULL)
  1317. goto out_enomem;
  1318. evt_i++;
  1319. }
  1320. }
  1321. }
  1322. if (!evt_num_known) {
  1323. evt_num_known = true;
  1324. goto restart;
  1325. }
  1326. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1327. evt_i = 0;
  1328. while (evt_i < evt_num) {
  1329. if (name_only) {
  1330. printf("%s ", evt_list[evt_i++]);
  1331. continue;
  1332. }
  1333. printf(" %-50s [%s]\n", evt_list[evt_i++],
  1334. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1335. }
  1336. if (evt_num)
  1337. printf("\n");
  1338. out_free:
  1339. evt_num = evt_i;
  1340. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1341. zfree(&evt_list[evt_i]);
  1342. zfree(&evt_list);
  1343. return evt_num;
  1344. out_enomem:
  1345. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
  1346. if (evt_list)
  1347. goto out_free;
  1348. return evt_num;
  1349. }
  1350. void print_symbol_events(const char *event_glob, unsigned type,
  1351. struct event_symbol *syms, unsigned max,
  1352. bool name_only)
  1353. {
  1354. unsigned int i, evt_i = 0, evt_num = 0;
  1355. char name[MAX_NAME_LEN];
  1356. char **evt_list = NULL;
  1357. bool evt_num_known = false;
  1358. restart:
  1359. if (evt_num_known) {
  1360. evt_list = zalloc(sizeof(char *) * evt_num);
  1361. if (!evt_list)
  1362. goto out_enomem;
  1363. syms -= max;
  1364. }
  1365. for (i = 0; i < max; i++, syms++) {
  1366. if (event_glob != NULL &&
  1367. !(strglobmatch(syms->symbol, event_glob) ||
  1368. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1369. continue;
  1370. if (!is_event_supported(type, i))
  1371. continue;
  1372. if (!evt_num_known) {
  1373. evt_num++;
  1374. continue;
  1375. }
  1376. if (!name_only && strlen(syms->alias))
  1377. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1378. else
  1379. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1380. evt_list[evt_i] = strdup(name);
  1381. if (evt_list[evt_i] == NULL)
  1382. goto out_enomem;
  1383. evt_i++;
  1384. }
  1385. if (!evt_num_known) {
  1386. evt_num_known = true;
  1387. goto restart;
  1388. }
  1389. qsort(evt_list, evt_num, sizeof(char *), cmp_string);
  1390. evt_i = 0;
  1391. while (evt_i < evt_num) {
  1392. if (name_only) {
  1393. printf("%s ", evt_list[evt_i++]);
  1394. continue;
  1395. }
  1396. printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
  1397. }
  1398. if (evt_num)
  1399. printf("\n");
  1400. out_free:
  1401. evt_num = evt_i;
  1402. for (evt_i = 0; evt_i < evt_num; evt_i++)
  1403. zfree(&evt_list[evt_i]);
  1404. zfree(&evt_list);
  1405. return;
  1406. out_enomem:
  1407. printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
  1408. if (evt_list)
  1409. goto out_free;
  1410. }
  1411. /*
  1412. * Print the help text for the event symbols:
  1413. */
  1414. void print_events(const char *event_glob, bool name_only)
  1415. {
  1416. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1417. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1418. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1419. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1420. print_hwcache_events(event_glob, name_only);
  1421. print_pmu_events(event_glob, name_only);
  1422. if (event_glob != NULL)
  1423. return;
  1424. if (!name_only) {
  1425. printf(" %-50s [%s]\n",
  1426. "rNNN",
  1427. event_type_descriptors[PERF_TYPE_RAW]);
  1428. printf(" %-50s [%s]\n",
  1429. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1430. event_type_descriptors[PERF_TYPE_RAW]);
  1431. printf(" (see 'man perf-list' on how to encode it)\n");
  1432. printf("\n");
  1433. printf(" %-50s [%s]\n",
  1434. "mem:<addr>[/len][:access]",
  1435. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1436. printf("\n");
  1437. }
  1438. print_tracepoint_events(NULL, NULL, name_only);
  1439. }
  1440. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1441. {
  1442. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1443. }
  1444. static int new_term(struct parse_events_term **_term, int type_val,
  1445. int type_term, char *config,
  1446. char *str, u64 num, int err_term, int err_val)
  1447. {
  1448. struct parse_events_term *term;
  1449. term = zalloc(sizeof(*term));
  1450. if (!term)
  1451. return -ENOMEM;
  1452. INIT_LIST_HEAD(&term->list);
  1453. term->type_val = type_val;
  1454. term->type_term = type_term;
  1455. term->config = config;
  1456. term->err_term = err_term;
  1457. term->err_val = err_val;
  1458. switch (type_val) {
  1459. case PARSE_EVENTS__TERM_TYPE_NUM:
  1460. term->val.num = num;
  1461. break;
  1462. case PARSE_EVENTS__TERM_TYPE_STR:
  1463. term->val.str = str;
  1464. break;
  1465. default:
  1466. free(term);
  1467. return -EINVAL;
  1468. }
  1469. *_term = term;
  1470. return 0;
  1471. }
  1472. int parse_events_term__num(struct parse_events_term **term,
  1473. int type_term, char *config, u64 num,
  1474. void *loc_term_, void *loc_val_)
  1475. {
  1476. YYLTYPE *loc_term = loc_term_;
  1477. YYLTYPE *loc_val = loc_val_;
  1478. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1479. config, NULL, num,
  1480. loc_term ? loc_term->first_column : 0,
  1481. loc_val ? loc_val->first_column : 0);
  1482. }
  1483. int parse_events_term__str(struct parse_events_term **term,
  1484. int type_term, char *config, char *str,
  1485. void *loc_term_, void *loc_val_)
  1486. {
  1487. YYLTYPE *loc_term = loc_term_;
  1488. YYLTYPE *loc_val = loc_val_;
  1489. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1490. config, str, 0,
  1491. loc_term ? loc_term->first_column : 0,
  1492. loc_val ? loc_val->first_column : 0);
  1493. }
  1494. int parse_events_term__sym_hw(struct parse_events_term **term,
  1495. char *config, unsigned idx)
  1496. {
  1497. struct event_symbol *sym;
  1498. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1499. sym = &event_symbols_hw[idx];
  1500. if (config)
  1501. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1502. PARSE_EVENTS__TERM_TYPE_USER, config,
  1503. (char *) sym->symbol, 0, 0, 0);
  1504. else
  1505. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1506. PARSE_EVENTS__TERM_TYPE_USER,
  1507. (char *) "event", (char *) sym->symbol,
  1508. 0, 0, 0);
  1509. }
  1510. int parse_events_term__clone(struct parse_events_term **new,
  1511. struct parse_events_term *term)
  1512. {
  1513. return new_term(new, term->type_val, term->type_term, term->config,
  1514. term->val.str, term->val.num,
  1515. term->err_term, term->err_val);
  1516. }
  1517. void parse_events__free_terms(struct list_head *terms)
  1518. {
  1519. struct parse_events_term *term, *h;
  1520. list_for_each_entry_safe(term, h, terms, list)
  1521. free(term);
  1522. }
  1523. void parse_events_evlist_error(struct parse_events_evlist *data,
  1524. int idx, const char *str)
  1525. {
  1526. struct parse_events_error *err = data->error;
  1527. if (!err)
  1528. return;
  1529. err->idx = idx;
  1530. err->str = strdup(str);
  1531. WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
  1532. }