parse-events.c 60 KB

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