parse-events.c 58 KB

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