parse-events.c 48 KB

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