evsel.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. /*
  2. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Parts came from builtin-{top,stat,record}.c, see those files for further
  5. * copyright notes.
  6. *
  7. * Released under the GPL v2. (and only v2, not any later version)
  8. */
  9. #include <byteswap.h>
  10. #include <linux/bitops.h>
  11. #include <api/fs/debugfs.h>
  12. #include <traceevent/event-parse.h>
  13. #include <linux/hw_breakpoint.h>
  14. #include <linux/perf_event.h>
  15. #include <sys/resource.h>
  16. #include "asm/bug.h"
  17. #include "evsel.h"
  18. #include "evlist.h"
  19. #include "util.h"
  20. #include "cpumap.h"
  21. #include "thread_map.h"
  22. #include "target.h"
  23. #include "perf_regs.h"
  24. #include "debug.h"
  25. #include "trace-event.h"
  26. static struct {
  27. bool sample_id_all;
  28. bool exclude_guest;
  29. bool mmap2;
  30. } perf_missing_features;
  31. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  32. int __perf_evsel__sample_size(u64 sample_type)
  33. {
  34. u64 mask = sample_type & PERF_SAMPLE_MASK;
  35. int size = 0;
  36. int i;
  37. for (i = 0; i < 64; i++) {
  38. if (mask & (1ULL << i))
  39. size++;
  40. }
  41. size *= sizeof(u64);
  42. return size;
  43. }
  44. /**
  45. * __perf_evsel__calc_id_pos - calculate id_pos.
  46. * @sample_type: sample type
  47. *
  48. * This function returns the position of the event id (PERF_SAMPLE_ID or
  49. * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
  50. * sample_event.
  51. */
  52. static int __perf_evsel__calc_id_pos(u64 sample_type)
  53. {
  54. int idx = 0;
  55. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  56. return 0;
  57. if (!(sample_type & PERF_SAMPLE_ID))
  58. return -1;
  59. if (sample_type & PERF_SAMPLE_IP)
  60. idx += 1;
  61. if (sample_type & PERF_SAMPLE_TID)
  62. idx += 1;
  63. if (sample_type & PERF_SAMPLE_TIME)
  64. idx += 1;
  65. if (sample_type & PERF_SAMPLE_ADDR)
  66. idx += 1;
  67. return idx;
  68. }
  69. /**
  70. * __perf_evsel__calc_is_pos - calculate is_pos.
  71. * @sample_type: sample type
  72. *
  73. * This function returns the position (counting backwards) of the event id
  74. * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
  75. * sample_id_all is used there is an id sample appended to non-sample events.
  76. */
  77. static int __perf_evsel__calc_is_pos(u64 sample_type)
  78. {
  79. int idx = 1;
  80. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  81. return 1;
  82. if (!(sample_type & PERF_SAMPLE_ID))
  83. return -1;
  84. if (sample_type & PERF_SAMPLE_CPU)
  85. idx += 1;
  86. if (sample_type & PERF_SAMPLE_STREAM_ID)
  87. idx += 1;
  88. return idx;
  89. }
  90. void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
  91. {
  92. evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
  93. evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
  94. }
  95. void hists__init(struct hists *hists)
  96. {
  97. memset(hists, 0, sizeof(*hists));
  98. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  99. hists->entries_in = &hists->entries_in_array[0];
  100. hists->entries_collapsed = RB_ROOT;
  101. hists->entries = RB_ROOT;
  102. pthread_mutex_init(&hists->lock, NULL);
  103. }
  104. void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
  105. enum perf_event_sample_format bit)
  106. {
  107. if (!(evsel->attr.sample_type & bit)) {
  108. evsel->attr.sample_type |= bit;
  109. evsel->sample_size += sizeof(u64);
  110. perf_evsel__calc_id_pos(evsel);
  111. }
  112. }
  113. void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
  114. enum perf_event_sample_format bit)
  115. {
  116. if (evsel->attr.sample_type & bit) {
  117. evsel->attr.sample_type &= ~bit;
  118. evsel->sample_size -= sizeof(u64);
  119. perf_evsel__calc_id_pos(evsel);
  120. }
  121. }
  122. void perf_evsel__set_sample_id(struct perf_evsel *evsel,
  123. bool can_sample_identifier)
  124. {
  125. if (can_sample_identifier) {
  126. perf_evsel__reset_sample_bit(evsel, ID);
  127. perf_evsel__set_sample_bit(evsel, IDENTIFIER);
  128. } else {
  129. perf_evsel__set_sample_bit(evsel, ID);
  130. }
  131. evsel->attr.read_format |= PERF_FORMAT_ID;
  132. }
  133. void perf_evsel__init(struct perf_evsel *evsel,
  134. struct perf_event_attr *attr, int idx)
  135. {
  136. evsel->idx = idx;
  137. evsel->attr = *attr;
  138. evsel->leader = evsel;
  139. evsel->unit = "";
  140. evsel->scale = 1.0;
  141. INIT_LIST_HEAD(&evsel->node);
  142. hists__init(&evsel->hists);
  143. evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
  144. perf_evsel__calc_id_pos(evsel);
  145. }
  146. struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
  147. {
  148. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  149. if (evsel != NULL)
  150. perf_evsel__init(evsel, attr, idx);
  151. return evsel;
  152. }
  153. struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
  154. {
  155. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  156. if (evsel != NULL) {
  157. struct perf_event_attr attr = {
  158. .type = PERF_TYPE_TRACEPOINT,
  159. .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
  160. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
  161. };
  162. if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
  163. goto out_free;
  164. evsel->tp_format = trace_event__tp_format(sys, name);
  165. if (evsel->tp_format == NULL)
  166. goto out_free;
  167. event_attr_init(&attr);
  168. attr.config = evsel->tp_format->id;
  169. attr.sample_period = 1;
  170. perf_evsel__init(evsel, &attr, idx);
  171. }
  172. return evsel;
  173. out_free:
  174. zfree(&evsel->name);
  175. free(evsel);
  176. return NULL;
  177. }
  178. const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
  179. "cycles",
  180. "instructions",
  181. "cache-references",
  182. "cache-misses",
  183. "branches",
  184. "branch-misses",
  185. "bus-cycles",
  186. "stalled-cycles-frontend",
  187. "stalled-cycles-backend",
  188. "ref-cycles",
  189. };
  190. static const char *__perf_evsel__hw_name(u64 config)
  191. {
  192. if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
  193. return perf_evsel__hw_names[config];
  194. return "unknown-hardware";
  195. }
  196. static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
  197. {
  198. int colon = 0, r = 0;
  199. struct perf_event_attr *attr = &evsel->attr;
  200. bool exclude_guest_default = false;
  201. #define MOD_PRINT(context, mod) do { \
  202. if (!attr->exclude_##context) { \
  203. if (!colon) colon = ++r; \
  204. r += scnprintf(bf + r, size - r, "%c", mod); \
  205. } } while(0)
  206. if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
  207. MOD_PRINT(kernel, 'k');
  208. MOD_PRINT(user, 'u');
  209. MOD_PRINT(hv, 'h');
  210. exclude_guest_default = true;
  211. }
  212. if (attr->precise_ip) {
  213. if (!colon)
  214. colon = ++r;
  215. r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
  216. exclude_guest_default = true;
  217. }
  218. if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
  219. MOD_PRINT(host, 'H');
  220. MOD_PRINT(guest, 'G');
  221. }
  222. #undef MOD_PRINT
  223. if (colon)
  224. bf[colon - 1] = ':';
  225. return r;
  226. }
  227. static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
  228. {
  229. int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
  230. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  231. }
  232. const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
  233. "cpu-clock",
  234. "task-clock",
  235. "page-faults",
  236. "context-switches",
  237. "cpu-migrations",
  238. "minor-faults",
  239. "major-faults",
  240. "alignment-faults",
  241. "emulation-faults",
  242. "dummy",
  243. };
  244. static const char *__perf_evsel__sw_name(u64 config)
  245. {
  246. if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
  247. return perf_evsel__sw_names[config];
  248. return "unknown-software";
  249. }
  250. static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
  251. {
  252. int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
  253. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  254. }
  255. static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
  256. {
  257. int r;
  258. r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
  259. if (type & HW_BREAKPOINT_R)
  260. r += scnprintf(bf + r, size - r, "r");
  261. if (type & HW_BREAKPOINT_W)
  262. r += scnprintf(bf + r, size - r, "w");
  263. if (type & HW_BREAKPOINT_X)
  264. r += scnprintf(bf + r, size - r, "x");
  265. return r;
  266. }
  267. static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
  268. {
  269. struct perf_event_attr *attr = &evsel->attr;
  270. int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
  271. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  272. }
  273. const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
  274. [PERF_EVSEL__MAX_ALIASES] = {
  275. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  276. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  277. { "LLC", "L2", },
  278. { "dTLB", "d-tlb", "Data-TLB", },
  279. { "iTLB", "i-tlb", "Instruction-TLB", },
  280. { "branch", "branches", "bpu", "btb", "bpc", },
  281. { "node", },
  282. };
  283. const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
  284. [PERF_EVSEL__MAX_ALIASES] = {
  285. { "load", "loads", "read", },
  286. { "store", "stores", "write", },
  287. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  288. };
  289. const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
  290. [PERF_EVSEL__MAX_ALIASES] = {
  291. { "refs", "Reference", "ops", "access", },
  292. { "misses", "miss", },
  293. };
  294. #define C(x) PERF_COUNT_HW_CACHE_##x
  295. #define CACHE_READ (1 << C(OP_READ))
  296. #define CACHE_WRITE (1 << C(OP_WRITE))
  297. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  298. #define COP(x) (1 << x)
  299. /*
  300. * cache operartion stat
  301. * L1I : Read and prefetch only
  302. * ITLB and BPU : Read-only
  303. */
  304. static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
  305. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  306. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  307. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  308. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  309. [C(ITLB)] = (CACHE_READ),
  310. [C(BPU)] = (CACHE_READ),
  311. [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  312. };
  313. bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
  314. {
  315. if (perf_evsel__hw_cache_stat[type] & COP(op))
  316. return true; /* valid */
  317. else
  318. return false; /* invalid */
  319. }
  320. int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
  321. char *bf, size_t size)
  322. {
  323. if (result) {
  324. return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
  325. perf_evsel__hw_cache_op[op][0],
  326. perf_evsel__hw_cache_result[result][0]);
  327. }
  328. return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
  329. perf_evsel__hw_cache_op[op][1]);
  330. }
  331. static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
  332. {
  333. u8 op, result, type = (config >> 0) & 0xff;
  334. const char *err = "unknown-ext-hardware-cache-type";
  335. if (type > PERF_COUNT_HW_CACHE_MAX)
  336. goto out_err;
  337. op = (config >> 8) & 0xff;
  338. err = "unknown-ext-hardware-cache-op";
  339. if (op > PERF_COUNT_HW_CACHE_OP_MAX)
  340. goto out_err;
  341. result = (config >> 16) & 0xff;
  342. err = "unknown-ext-hardware-cache-result";
  343. if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  344. goto out_err;
  345. err = "invalid-cache";
  346. if (!perf_evsel__is_cache_op_valid(type, op))
  347. goto out_err;
  348. return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
  349. out_err:
  350. return scnprintf(bf, size, "%s", err);
  351. }
  352. static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
  353. {
  354. int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
  355. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  356. }
  357. static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
  358. {
  359. int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
  360. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  361. }
  362. const char *perf_evsel__name(struct perf_evsel *evsel)
  363. {
  364. char bf[128];
  365. if (evsel->name)
  366. return evsel->name;
  367. switch (evsel->attr.type) {
  368. case PERF_TYPE_RAW:
  369. perf_evsel__raw_name(evsel, bf, sizeof(bf));
  370. break;
  371. case PERF_TYPE_HARDWARE:
  372. perf_evsel__hw_name(evsel, bf, sizeof(bf));
  373. break;
  374. case PERF_TYPE_HW_CACHE:
  375. perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
  376. break;
  377. case PERF_TYPE_SOFTWARE:
  378. perf_evsel__sw_name(evsel, bf, sizeof(bf));
  379. break;
  380. case PERF_TYPE_TRACEPOINT:
  381. scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
  382. break;
  383. case PERF_TYPE_BREAKPOINT:
  384. perf_evsel__bp_name(evsel, bf, sizeof(bf));
  385. break;
  386. default:
  387. scnprintf(bf, sizeof(bf), "unknown attr type: %d",
  388. evsel->attr.type);
  389. break;
  390. }
  391. evsel->name = strdup(bf);
  392. return evsel->name ?: "unknown";
  393. }
  394. const char *perf_evsel__group_name(struct perf_evsel *evsel)
  395. {
  396. return evsel->group_name ?: "anon group";
  397. }
  398. int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
  399. {
  400. int ret;
  401. struct perf_evsel *pos;
  402. const char *group_name = perf_evsel__group_name(evsel);
  403. ret = scnprintf(buf, size, "%s", group_name);
  404. ret += scnprintf(buf + ret, size - ret, " { %s",
  405. perf_evsel__name(evsel));
  406. for_each_group_member(pos, evsel)
  407. ret += scnprintf(buf + ret, size - ret, ", %s",
  408. perf_evsel__name(pos));
  409. ret += scnprintf(buf + ret, size - ret, " }");
  410. return ret;
  411. }
  412. static void
  413. perf_evsel__config_callgraph(struct perf_evsel *evsel,
  414. struct record_opts *opts)
  415. {
  416. bool function = perf_evsel__is_function_event(evsel);
  417. struct perf_event_attr *attr = &evsel->attr;
  418. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  419. if (opts->call_graph == CALLCHAIN_DWARF) {
  420. if (!function) {
  421. perf_evsel__set_sample_bit(evsel, REGS_USER);
  422. perf_evsel__set_sample_bit(evsel, STACK_USER);
  423. attr->sample_regs_user = PERF_REGS_MASK;
  424. attr->sample_stack_user = opts->stack_dump_size;
  425. attr->exclude_callchain_user = 1;
  426. } else {
  427. pr_info("Cannot use DWARF unwind for function trace event,"
  428. " falling back to framepointers.\n");
  429. }
  430. }
  431. if (function) {
  432. pr_info("Disabling user space callchains for function trace event.\n");
  433. attr->exclude_callchain_user = 1;
  434. }
  435. }
  436. /*
  437. * The enable_on_exec/disabled value strategy:
  438. *
  439. * 1) For any type of traced program:
  440. * - all independent events and group leaders are disabled
  441. * - all group members are enabled
  442. *
  443. * Group members are ruled by group leaders. They need to
  444. * be enabled, because the group scheduling relies on that.
  445. *
  446. * 2) For traced programs executed by perf:
  447. * - all independent events and group leaders have
  448. * enable_on_exec set
  449. * - we don't specifically enable or disable any event during
  450. * the record command
  451. *
  452. * Independent events and group leaders are initially disabled
  453. * and get enabled by exec. Group members are ruled by group
  454. * leaders as stated in 1).
  455. *
  456. * 3) For traced programs attached by perf (pid/tid):
  457. * - we specifically enable or disable all events during
  458. * the record command
  459. *
  460. * When attaching events to already running traced we
  461. * enable/disable events specifically, as there's no
  462. * initial traced exec call.
  463. */
  464. void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
  465. {
  466. struct perf_evsel *leader = evsel->leader;
  467. struct perf_event_attr *attr = &evsel->attr;
  468. int track = !evsel->idx; /* only the first counter needs these */
  469. bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
  470. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  471. attr->inherit = !opts->no_inherit;
  472. perf_evsel__set_sample_bit(evsel, IP);
  473. perf_evsel__set_sample_bit(evsel, TID);
  474. if (evsel->sample_read) {
  475. perf_evsel__set_sample_bit(evsel, READ);
  476. /*
  477. * We need ID even in case of single event, because
  478. * PERF_SAMPLE_READ process ID specific data.
  479. */
  480. perf_evsel__set_sample_id(evsel, false);
  481. /*
  482. * Apply group format only if we belong to group
  483. * with more than one members.
  484. */
  485. if (leader->nr_members > 1) {
  486. attr->read_format |= PERF_FORMAT_GROUP;
  487. attr->inherit = 0;
  488. }
  489. }
  490. /*
  491. * We default some events to a 1 default interval. But keep
  492. * it a weak assumption overridable by the user.
  493. */
  494. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  495. opts->user_interval != ULLONG_MAX)) {
  496. if (opts->freq) {
  497. perf_evsel__set_sample_bit(evsel, PERIOD);
  498. attr->freq = 1;
  499. attr->sample_freq = opts->freq;
  500. } else {
  501. attr->sample_period = opts->default_interval;
  502. }
  503. }
  504. /*
  505. * Disable sampling for all group members other
  506. * than leader in case leader 'leads' the sampling.
  507. */
  508. if ((leader != evsel) && leader->sample_read) {
  509. attr->sample_freq = 0;
  510. attr->sample_period = 0;
  511. }
  512. if (opts->no_samples)
  513. attr->sample_freq = 0;
  514. if (opts->inherit_stat)
  515. attr->inherit_stat = 1;
  516. if (opts->sample_address) {
  517. perf_evsel__set_sample_bit(evsel, ADDR);
  518. attr->mmap_data = track;
  519. }
  520. if (opts->call_graph_enabled)
  521. perf_evsel__config_callgraph(evsel, opts);
  522. if (target__has_cpu(&opts->target))
  523. perf_evsel__set_sample_bit(evsel, CPU);
  524. if (opts->period)
  525. perf_evsel__set_sample_bit(evsel, PERIOD);
  526. if (!perf_missing_features.sample_id_all &&
  527. (opts->sample_time || !opts->no_inherit ||
  528. target__has_cpu(&opts->target) || per_cpu))
  529. perf_evsel__set_sample_bit(evsel, TIME);
  530. if (opts->raw_samples) {
  531. perf_evsel__set_sample_bit(evsel, TIME);
  532. perf_evsel__set_sample_bit(evsel, RAW);
  533. perf_evsel__set_sample_bit(evsel, CPU);
  534. }
  535. if (opts->sample_address)
  536. perf_evsel__set_sample_bit(evsel, DATA_SRC);
  537. if (opts->no_buffering) {
  538. attr->watermark = 0;
  539. attr->wakeup_events = 1;
  540. }
  541. if (opts->branch_stack) {
  542. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  543. attr->branch_sample_type = opts->branch_stack;
  544. }
  545. if (opts->sample_weight)
  546. perf_evsel__set_sample_bit(evsel, WEIGHT);
  547. attr->mmap = track;
  548. attr->comm = track;
  549. if (opts->sample_transaction)
  550. perf_evsel__set_sample_bit(evsel, TRANSACTION);
  551. /*
  552. * XXX see the function comment above
  553. *
  554. * Disabling only independent events or group leaders,
  555. * keeping group members enabled.
  556. */
  557. if (perf_evsel__is_group_leader(evsel))
  558. attr->disabled = 1;
  559. /*
  560. * Setting enable_on_exec for independent events and
  561. * group leaders for traced executed by perf.
  562. */
  563. if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
  564. !opts->initial_delay)
  565. attr->enable_on_exec = 1;
  566. }
  567. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  568. {
  569. int cpu, thread;
  570. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  571. if (evsel->fd) {
  572. for (cpu = 0; cpu < ncpus; cpu++) {
  573. for (thread = 0; thread < nthreads; thread++) {
  574. FD(evsel, cpu, thread) = -1;
  575. }
  576. }
  577. }
  578. return evsel->fd != NULL ? 0 : -ENOMEM;
  579. }
  580. static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
  581. int ioc, void *arg)
  582. {
  583. int cpu, thread;
  584. for (cpu = 0; cpu < ncpus; cpu++) {
  585. for (thread = 0; thread < nthreads; thread++) {
  586. int fd = FD(evsel, cpu, thread),
  587. err = ioctl(fd, ioc, arg);
  588. if (err)
  589. return err;
  590. }
  591. }
  592. return 0;
  593. }
  594. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  595. const char *filter)
  596. {
  597. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  598. PERF_EVENT_IOC_SET_FILTER,
  599. (void *)filter);
  600. }
  601. int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
  602. {
  603. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  604. PERF_EVENT_IOC_ENABLE,
  605. 0);
  606. }
  607. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  608. {
  609. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  610. if (evsel->sample_id == NULL)
  611. return -ENOMEM;
  612. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  613. if (evsel->id == NULL) {
  614. xyarray__delete(evsel->sample_id);
  615. evsel->sample_id = NULL;
  616. return -ENOMEM;
  617. }
  618. return 0;
  619. }
  620. void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
  621. {
  622. memset(evsel->counts, 0, (sizeof(*evsel->counts) +
  623. (ncpus * sizeof(struct perf_counts_values))));
  624. }
  625. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  626. {
  627. evsel->counts = zalloc((sizeof(*evsel->counts) +
  628. (ncpus * sizeof(struct perf_counts_values))));
  629. return evsel->counts != NULL ? 0 : -ENOMEM;
  630. }
  631. void perf_evsel__free_fd(struct perf_evsel *evsel)
  632. {
  633. xyarray__delete(evsel->fd);
  634. evsel->fd = NULL;
  635. }
  636. void perf_evsel__free_id(struct perf_evsel *evsel)
  637. {
  638. xyarray__delete(evsel->sample_id);
  639. evsel->sample_id = NULL;
  640. zfree(&evsel->id);
  641. }
  642. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  643. {
  644. int cpu, thread;
  645. for (cpu = 0; cpu < ncpus; cpu++)
  646. for (thread = 0; thread < nthreads; ++thread) {
  647. close(FD(evsel, cpu, thread));
  648. FD(evsel, cpu, thread) = -1;
  649. }
  650. }
  651. void perf_evsel__free_counts(struct perf_evsel *evsel)
  652. {
  653. zfree(&evsel->counts);
  654. }
  655. void perf_evsel__exit(struct perf_evsel *evsel)
  656. {
  657. assert(list_empty(&evsel->node));
  658. perf_evsel__free_fd(evsel);
  659. perf_evsel__free_id(evsel);
  660. }
  661. void perf_evsel__delete(struct perf_evsel *evsel)
  662. {
  663. perf_evsel__exit(evsel);
  664. close_cgroup(evsel->cgrp);
  665. zfree(&evsel->group_name);
  666. if (evsel->tp_format)
  667. pevent_free_format(evsel->tp_format);
  668. zfree(&evsel->name);
  669. free(evsel);
  670. }
  671. static inline void compute_deltas(struct perf_evsel *evsel,
  672. int cpu,
  673. struct perf_counts_values *count)
  674. {
  675. struct perf_counts_values tmp;
  676. if (!evsel->prev_raw_counts)
  677. return;
  678. if (cpu == -1) {
  679. tmp = evsel->prev_raw_counts->aggr;
  680. evsel->prev_raw_counts->aggr = *count;
  681. } else {
  682. tmp = evsel->prev_raw_counts->cpu[cpu];
  683. evsel->prev_raw_counts->cpu[cpu] = *count;
  684. }
  685. count->val = count->val - tmp.val;
  686. count->ena = count->ena - tmp.ena;
  687. count->run = count->run - tmp.run;
  688. }
  689. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  690. int cpu, int thread, bool scale)
  691. {
  692. struct perf_counts_values count;
  693. size_t nv = scale ? 3 : 1;
  694. if (FD(evsel, cpu, thread) < 0)
  695. return -EINVAL;
  696. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  697. return -ENOMEM;
  698. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  699. return -errno;
  700. compute_deltas(evsel, cpu, &count);
  701. if (scale) {
  702. if (count.run == 0)
  703. count.val = 0;
  704. else if (count.run < count.ena)
  705. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  706. } else
  707. count.ena = count.run = 0;
  708. evsel->counts->cpu[cpu] = count;
  709. return 0;
  710. }
  711. int __perf_evsel__read(struct perf_evsel *evsel,
  712. int ncpus, int nthreads, bool scale)
  713. {
  714. size_t nv = scale ? 3 : 1;
  715. int cpu, thread;
  716. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  717. aggr->val = aggr->ena = aggr->run = 0;
  718. for (cpu = 0; cpu < ncpus; cpu++) {
  719. for (thread = 0; thread < nthreads; thread++) {
  720. if (FD(evsel, cpu, thread) < 0)
  721. continue;
  722. if (readn(FD(evsel, cpu, thread),
  723. &count, nv * sizeof(u64)) < 0)
  724. return -errno;
  725. aggr->val += count.val;
  726. if (scale) {
  727. aggr->ena += count.ena;
  728. aggr->run += count.run;
  729. }
  730. }
  731. }
  732. compute_deltas(evsel, -1, aggr);
  733. evsel->counts->scaled = 0;
  734. if (scale) {
  735. if (aggr->run == 0) {
  736. evsel->counts->scaled = -1;
  737. aggr->val = 0;
  738. return 0;
  739. }
  740. if (aggr->run < aggr->ena) {
  741. evsel->counts->scaled = 1;
  742. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  743. }
  744. } else
  745. aggr->ena = aggr->run = 0;
  746. return 0;
  747. }
  748. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  749. {
  750. struct perf_evsel *leader = evsel->leader;
  751. int fd;
  752. if (perf_evsel__is_group_leader(evsel))
  753. return -1;
  754. /*
  755. * Leader must be already processed/open,
  756. * if not it's a bug.
  757. */
  758. BUG_ON(!leader->fd);
  759. fd = FD(leader, cpu, thread);
  760. BUG_ON(fd == -1);
  761. return fd;
  762. }
  763. #define __PRINT_ATTR(fmt, cast, field) \
  764. fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
  765. #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
  766. #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
  767. #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
  768. #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
  769. #define PRINT_ATTR2N(name1, field1, name2, field2) \
  770. fprintf(fp, " %-19s %u %-19s %u\n", \
  771. name1, attr->field1, name2, attr->field2)
  772. #define PRINT_ATTR2(field1, field2) \
  773. PRINT_ATTR2N(#field1, field1, #field2, field2)
  774. static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
  775. {
  776. size_t ret = 0;
  777. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  778. ret += fprintf(fp, "perf_event_attr:\n");
  779. ret += PRINT_ATTR_U32(type);
  780. ret += PRINT_ATTR_U32(size);
  781. ret += PRINT_ATTR_X64(config);
  782. ret += PRINT_ATTR_U64(sample_period);
  783. ret += PRINT_ATTR_U64(sample_freq);
  784. ret += PRINT_ATTR_X64(sample_type);
  785. ret += PRINT_ATTR_X64(read_format);
  786. ret += PRINT_ATTR2(disabled, inherit);
  787. ret += PRINT_ATTR2(pinned, exclusive);
  788. ret += PRINT_ATTR2(exclude_user, exclude_kernel);
  789. ret += PRINT_ATTR2(exclude_hv, exclude_idle);
  790. ret += PRINT_ATTR2(mmap, comm);
  791. ret += PRINT_ATTR2(freq, inherit_stat);
  792. ret += PRINT_ATTR2(enable_on_exec, task);
  793. ret += PRINT_ATTR2(watermark, precise_ip);
  794. ret += PRINT_ATTR2(mmap_data, sample_id_all);
  795. ret += PRINT_ATTR2(exclude_host, exclude_guest);
  796. ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
  797. "excl.callchain_user", exclude_callchain_user);
  798. ret += PRINT_ATTR_U32(mmap2);
  799. ret += PRINT_ATTR_U32(wakeup_events);
  800. ret += PRINT_ATTR_U32(wakeup_watermark);
  801. ret += PRINT_ATTR_X32(bp_type);
  802. ret += PRINT_ATTR_X64(bp_addr);
  803. ret += PRINT_ATTR_X64(config1);
  804. ret += PRINT_ATTR_U64(bp_len);
  805. ret += PRINT_ATTR_X64(config2);
  806. ret += PRINT_ATTR_X64(branch_sample_type);
  807. ret += PRINT_ATTR_X64(sample_regs_user);
  808. ret += PRINT_ATTR_U32(sample_stack_user);
  809. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  810. return ret;
  811. }
  812. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  813. struct thread_map *threads)
  814. {
  815. int cpu, thread;
  816. unsigned long flags = 0;
  817. int pid = -1, err;
  818. enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
  819. if (evsel->fd == NULL &&
  820. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  821. return -ENOMEM;
  822. if (evsel->cgrp) {
  823. flags = PERF_FLAG_PID_CGROUP;
  824. pid = evsel->cgrp->fd;
  825. }
  826. fallback_missing_features:
  827. if (perf_missing_features.mmap2)
  828. evsel->attr.mmap2 = 0;
  829. if (perf_missing_features.exclude_guest)
  830. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  831. retry_sample_id:
  832. if (perf_missing_features.sample_id_all)
  833. evsel->attr.sample_id_all = 0;
  834. if (verbose >= 2)
  835. perf_event_attr__fprintf(&evsel->attr, stderr);
  836. for (cpu = 0; cpu < cpus->nr; cpu++) {
  837. for (thread = 0; thread < threads->nr; thread++) {
  838. int group_fd;
  839. if (!evsel->cgrp)
  840. pid = threads->map[thread];
  841. group_fd = get_group_fd(evsel, cpu, thread);
  842. retry_open:
  843. pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
  844. pid, cpus->map[cpu], group_fd, flags);
  845. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  846. pid,
  847. cpus->map[cpu],
  848. group_fd, flags);
  849. if (FD(evsel, cpu, thread) < 0) {
  850. err = -errno;
  851. pr_debug2("sys_perf_event_open failed, error %d\n",
  852. err);
  853. goto try_fallback;
  854. }
  855. set_rlimit = NO_CHANGE;
  856. }
  857. }
  858. return 0;
  859. try_fallback:
  860. /*
  861. * perf stat needs between 5 and 22 fds per CPU. When we run out
  862. * of them try to increase the limits.
  863. */
  864. if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
  865. struct rlimit l;
  866. int old_errno = errno;
  867. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  868. if (set_rlimit == NO_CHANGE)
  869. l.rlim_cur = l.rlim_max;
  870. else {
  871. l.rlim_cur = l.rlim_max + 1000;
  872. l.rlim_max = l.rlim_cur;
  873. }
  874. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  875. set_rlimit++;
  876. errno = old_errno;
  877. goto retry_open;
  878. }
  879. }
  880. errno = old_errno;
  881. }
  882. if (err != -EINVAL || cpu > 0 || thread > 0)
  883. goto out_close;
  884. if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
  885. perf_missing_features.mmap2 = true;
  886. goto fallback_missing_features;
  887. } else if (!perf_missing_features.exclude_guest &&
  888. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  889. perf_missing_features.exclude_guest = true;
  890. goto fallback_missing_features;
  891. } else if (!perf_missing_features.sample_id_all) {
  892. perf_missing_features.sample_id_all = true;
  893. goto retry_sample_id;
  894. }
  895. out_close:
  896. do {
  897. while (--thread >= 0) {
  898. close(FD(evsel, cpu, thread));
  899. FD(evsel, cpu, thread) = -1;
  900. }
  901. thread = threads->nr;
  902. } while (--cpu >= 0);
  903. return err;
  904. }
  905. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  906. {
  907. if (evsel->fd == NULL)
  908. return;
  909. perf_evsel__close_fd(evsel, ncpus, nthreads);
  910. perf_evsel__free_fd(evsel);
  911. }
  912. static struct {
  913. struct cpu_map map;
  914. int cpus[1];
  915. } empty_cpu_map = {
  916. .map.nr = 1,
  917. .cpus = { -1, },
  918. };
  919. static struct {
  920. struct thread_map map;
  921. int threads[1];
  922. } empty_thread_map = {
  923. .map.nr = 1,
  924. .threads = { -1, },
  925. };
  926. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  927. struct thread_map *threads)
  928. {
  929. if (cpus == NULL) {
  930. /* Work around old compiler warnings about strict aliasing */
  931. cpus = &empty_cpu_map.map;
  932. }
  933. if (threads == NULL)
  934. threads = &empty_thread_map.map;
  935. return __perf_evsel__open(evsel, cpus, threads);
  936. }
  937. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  938. struct cpu_map *cpus)
  939. {
  940. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  941. }
  942. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  943. struct thread_map *threads)
  944. {
  945. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  946. }
  947. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  948. const union perf_event *event,
  949. struct perf_sample *sample)
  950. {
  951. u64 type = evsel->attr.sample_type;
  952. const u64 *array = event->sample.array;
  953. bool swapped = evsel->needs_swap;
  954. union u64_swap u;
  955. array += ((event->header.size -
  956. sizeof(event->header)) / sizeof(u64)) - 1;
  957. if (type & PERF_SAMPLE_IDENTIFIER) {
  958. sample->id = *array;
  959. array--;
  960. }
  961. if (type & PERF_SAMPLE_CPU) {
  962. u.val64 = *array;
  963. if (swapped) {
  964. /* undo swap of u64, then swap on individual u32s */
  965. u.val64 = bswap_64(u.val64);
  966. u.val32[0] = bswap_32(u.val32[0]);
  967. }
  968. sample->cpu = u.val32[0];
  969. array--;
  970. }
  971. if (type & PERF_SAMPLE_STREAM_ID) {
  972. sample->stream_id = *array;
  973. array--;
  974. }
  975. if (type & PERF_SAMPLE_ID) {
  976. sample->id = *array;
  977. array--;
  978. }
  979. if (type & PERF_SAMPLE_TIME) {
  980. sample->time = *array;
  981. array--;
  982. }
  983. if (type & PERF_SAMPLE_TID) {
  984. u.val64 = *array;
  985. if (swapped) {
  986. /* undo swap of u64, then swap on individual u32s */
  987. u.val64 = bswap_64(u.val64);
  988. u.val32[0] = bswap_32(u.val32[0]);
  989. u.val32[1] = bswap_32(u.val32[1]);
  990. }
  991. sample->pid = u.val32[0];
  992. sample->tid = u.val32[1];
  993. array--;
  994. }
  995. return 0;
  996. }
  997. static inline bool overflow(const void *endp, u16 max_size, const void *offset,
  998. u64 size)
  999. {
  1000. return size > max_size || offset + size > endp;
  1001. }
  1002. #define OVERFLOW_CHECK(offset, size, max_size) \
  1003. do { \
  1004. if (overflow(endp, (max_size), (offset), (size))) \
  1005. return -EFAULT; \
  1006. } while (0)
  1007. #define OVERFLOW_CHECK_u64(offset) \
  1008. OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
  1009. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  1010. struct perf_sample *data)
  1011. {
  1012. u64 type = evsel->attr.sample_type;
  1013. bool swapped = evsel->needs_swap;
  1014. const u64 *array;
  1015. u16 max_size = event->header.size;
  1016. const void *endp = (void *)event + max_size;
  1017. u64 sz;
  1018. /*
  1019. * used for cross-endian analysis. See git commit 65014ab3
  1020. * for why this goofiness is needed.
  1021. */
  1022. union u64_swap u;
  1023. memset(data, 0, sizeof(*data));
  1024. data->cpu = data->pid = data->tid = -1;
  1025. data->stream_id = data->id = data->time = -1ULL;
  1026. data->period = evsel->attr.sample_period;
  1027. data->weight = 0;
  1028. if (event->header.type != PERF_RECORD_SAMPLE) {
  1029. if (!evsel->attr.sample_id_all)
  1030. return 0;
  1031. return perf_evsel__parse_id_sample(evsel, event, data);
  1032. }
  1033. array = event->sample.array;
  1034. /*
  1035. * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
  1036. * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
  1037. * check the format does not go past the end of the event.
  1038. */
  1039. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  1040. return -EFAULT;
  1041. data->id = -1ULL;
  1042. if (type & PERF_SAMPLE_IDENTIFIER) {
  1043. data->id = *array;
  1044. array++;
  1045. }
  1046. if (type & PERF_SAMPLE_IP) {
  1047. data->ip = *array;
  1048. array++;
  1049. }
  1050. if (type & PERF_SAMPLE_TID) {
  1051. u.val64 = *array;
  1052. if (swapped) {
  1053. /* undo swap of u64, then swap on individual u32s */
  1054. u.val64 = bswap_64(u.val64);
  1055. u.val32[0] = bswap_32(u.val32[0]);
  1056. u.val32[1] = bswap_32(u.val32[1]);
  1057. }
  1058. data->pid = u.val32[0];
  1059. data->tid = u.val32[1];
  1060. array++;
  1061. }
  1062. if (type & PERF_SAMPLE_TIME) {
  1063. data->time = *array;
  1064. array++;
  1065. }
  1066. data->addr = 0;
  1067. if (type & PERF_SAMPLE_ADDR) {
  1068. data->addr = *array;
  1069. array++;
  1070. }
  1071. if (type & PERF_SAMPLE_ID) {
  1072. data->id = *array;
  1073. array++;
  1074. }
  1075. if (type & PERF_SAMPLE_STREAM_ID) {
  1076. data->stream_id = *array;
  1077. array++;
  1078. }
  1079. if (type & PERF_SAMPLE_CPU) {
  1080. u.val64 = *array;
  1081. if (swapped) {
  1082. /* undo swap of u64, then swap on individual u32s */
  1083. u.val64 = bswap_64(u.val64);
  1084. u.val32[0] = bswap_32(u.val32[0]);
  1085. }
  1086. data->cpu = u.val32[0];
  1087. array++;
  1088. }
  1089. if (type & PERF_SAMPLE_PERIOD) {
  1090. data->period = *array;
  1091. array++;
  1092. }
  1093. if (type & PERF_SAMPLE_READ) {
  1094. u64 read_format = evsel->attr.read_format;
  1095. OVERFLOW_CHECK_u64(array);
  1096. if (read_format & PERF_FORMAT_GROUP)
  1097. data->read.group.nr = *array;
  1098. else
  1099. data->read.one.value = *array;
  1100. array++;
  1101. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1102. OVERFLOW_CHECK_u64(array);
  1103. data->read.time_enabled = *array;
  1104. array++;
  1105. }
  1106. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1107. OVERFLOW_CHECK_u64(array);
  1108. data->read.time_running = *array;
  1109. array++;
  1110. }
  1111. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1112. if (read_format & PERF_FORMAT_GROUP) {
  1113. const u64 max_group_nr = UINT64_MAX /
  1114. sizeof(struct sample_read_value);
  1115. if (data->read.group.nr > max_group_nr)
  1116. return -EFAULT;
  1117. sz = data->read.group.nr *
  1118. sizeof(struct sample_read_value);
  1119. OVERFLOW_CHECK(array, sz, max_size);
  1120. data->read.group.values =
  1121. (struct sample_read_value *)array;
  1122. array = (void *)array + sz;
  1123. } else {
  1124. OVERFLOW_CHECK_u64(array);
  1125. data->read.one.id = *array;
  1126. array++;
  1127. }
  1128. }
  1129. if (type & PERF_SAMPLE_CALLCHAIN) {
  1130. const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
  1131. OVERFLOW_CHECK_u64(array);
  1132. data->callchain = (struct ip_callchain *)array++;
  1133. if (data->callchain->nr > max_callchain_nr)
  1134. return -EFAULT;
  1135. sz = data->callchain->nr * sizeof(u64);
  1136. OVERFLOW_CHECK(array, sz, max_size);
  1137. array = (void *)array + sz;
  1138. }
  1139. if (type & PERF_SAMPLE_RAW) {
  1140. OVERFLOW_CHECK_u64(array);
  1141. u.val64 = *array;
  1142. if (WARN_ONCE(swapped,
  1143. "Endianness of raw data not corrected!\n")) {
  1144. /* undo swap of u64, then swap on individual u32s */
  1145. u.val64 = bswap_64(u.val64);
  1146. u.val32[0] = bswap_32(u.val32[0]);
  1147. u.val32[1] = bswap_32(u.val32[1]);
  1148. }
  1149. data->raw_size = u.val32[0];
  1150. array = (void *)array + sizeof(u32);
  1151. OVERFLOW_CHECK(array, data->raw_size, max_size);
  1152. data->raw_data = (void *)array;
  1153. array = (void *)array + data->raw_size;
  1154. }
  1155. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1156. const u64 max_branch_nr = UINT64_MAX /
  1157. sizeof(struct branch_entry);
  1158. OVERFLOW_CHECK_u64(array);
  1159. data->branch_stack = (struct branch_stack *)array++;
  1160. if (data->branch_stack->nr > max_branch_nr)
  1161. return -EFAULT;
  1162. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  1163. OVERFLOW_CHECK(array, sz, max_size);
  1164. array = (void *)array + sz;
  1165. }
  1166. if (type & PERF_SAMPLE_REGS_USER) {
  1167. OVERFLOW_CHECK_u64(array);
  1168. data->user_regs.abi = *array;
  1169. array++;
  1170. if (data->user_regs.abi) {
  1171. u64 mask = evsel->attr.sample_regs_user;
  1172. sz = hweight_long(mask) * sizeof(u64);
  1173. OVERFLOW_CHECK(array, sz, max_size);
  1174. data->user_regs.mask = mask;
  1175. data->user_regs.regs = (u64 *)array;
  1176. array = (void *)array + sz;
  1177. }
  1178. }
  1179. if (type & PERF_SAMPLE_STACK_USER) {
  1180. OVERFLOW_CHECK_u64(array);
  1181. sz = *array++;
  1182. data->user_stack.offset = ((char *)(array - 1)
  1183. - (char *) event);
  1184. if (!sz) {
  1185. data->user_stack.size = 0;
  1186. } else {
  1187. OVERFLOW_CHECK(array, sz, max_size);
  1188. data->user_stack.data = (char *)array;
  1189. array = (void *)array + sz;
  1190. OVERFLOW_CHECK_u64(array);
  1191. data->user_stack.size = *array++;
  1192. if (WARN_ONCE(data->user_stack.size > sz,
  1193. "user stack dump failure\n"))
  1194. return -EFAULT;
  1195. }
  1196. }
  1197. data->weight = 0;
  1198. if (type & PERF_SAMPLE_WEIGHT) {
  1199. OVERFLOW_CHECK_u64(array);
  1200. data->weight = *array;
  1201. array++;
  1202. }
  1203. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1204. if (type & PERF_SAMPLE_DATA_SRC) {
  1205. OVERFLOW_CHECK_u64(array);
  1206. data->data_src = *array;
  1207. array++;
  1208. }
  1209. data->transaction = 0;
  1210. if (type & PERF_SAMPLE_TRANSACTION) {
  1211. OVERFLOW_CHECK_u64(array);
  1212. data->transaction = *array;
  1213. array++;
  1214. }
  1215. return 0;
  1216. }
  1217. size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
  1218. u64 read_format)
  1219. {
  1220. size_t sz, result = sizeof(struct sample_event);
  1221. if (type & PERF_SAMPLE_IDENTIFIER)
  1222. result += sizeof(u64);
  1223. if (type & PERF_SAMPLE_IP)
  1224. result += sizeof(u64);
  1225. if (type & PERF_SAMPLE_TID)
  1226. result += sizeof(u64);
  1227. if (type & PERF_SAMPLE_TIME)
  1228. result += sizeof(u64);
  1229. if (type & PERF_SAMPLE_ADDR)
  1230. result += sizeof(u64);
  1231. if (type & PERF_SAMPLE_ID)
  1232. result += sizeof(u64);
  1233. if (type & PERF_SAMPLE_STREAM_ID)
  1234. result += sizeof(u64);
  1235. if (type & PERF_SAMPLE_CPU)
  1236. result += sizeof(u64);
  1237. if (type & PERF_SAMPLE_PERIOD)
  1238. result += sizeof(u64);
  1239. if (type & PERF_SAMPLE_READ) {
  1240. result += sizeof(u64);
  1241. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1242. result += sizeof(u64);
  1243. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1244. result += sizeof(u64);
  1245. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1246. if (read_format & PERF_FORMAT_GROUP) {
  1247. sz = sample->read.group.nr *
  1248. sizeof(struct sample_read_value);
  1249. result += sz;
  1250. } else {
  1251. result += sizeof(u64);
  1252. }
  1253. }
  1254. if (type & PERF_SAMPLE_CALLCHAIN) {
  1255. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1256. result += sz;
  1257. }
  1258. if (type & PERF_SAMPLE_RAW) {
  1259. result += sizeof(u32);
  1260. result += sample->raw_size;
  1261. }
  1262. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1263. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1264. sz += sizeof(u64);
  1265. result += sz;
  1266. }
  1267. if (type & PERF_SAMPLE_REGS_USER) {
  1268. if (sample->user_regs.abi) {
  1269. result += sizeof(u64);
  1270. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1271. result += sz;
  1272. } else {
  1273. result += sizeof(u64);
  1274. }
  1275. }
  1276. if (type & PERF_SAMPLE_STACK_USER) {
  1277. sz = sample->user_stack.size;
  1278. result += sizeof(u64);
  1279. if (sz) {
  1280. result += sz;
  1281. result += sizeof(u64);
  1282. }
  1283. }
  1284. if (type & PERF_SAMPLE_WEIGHT)
  1285. result += sizeof(u64);
  1286. if (type & PERF_SAMPLE_DATA_SRC)
  1287. result += sizeof(u64);
  1288. if (type & PERF_SAMPLE_TRANSACTION)
  1289. result += sizeof(u64);
  1290. return result;
  1291. }
  1292. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1293. u64 read_format,
  1294. const struct perf_sample *sample,
  1295. bool swapped)
  1296. {
  1297. u64 *array;
  1298. size_t sz;
  1299. /*
  1300. * used for cross-endian analysis. See git commit 65014ab3
  1301. * for why this goofiness is needed.
  1302. */
  1303. union u64_swap u;
  1304. array = event->sample.array;
  1305. if (type & PERF_SAMPLE_IDENTIFIER) {
  1306. *array = sample->id;
  1307. array++;
  1308. }
  1309. if (type & PERF_SAMPLE_IP) {
  1310. *array = sample->ip;
  1311. array++;
  1312. }
  1313. if (type & PERF_SAMPLE_TID) {
  1314. u.val32[0] = sample->pid;
  1315. u.val32[1] = sample->tid;
  1316. if (swapped) {
  1317. /*
  1318. * Inverse of what is done in perf_evsel__parse_sample
  1319. */
  1320. u.val32[0] = bswap_32(u.val32[0]);
  1321. u.val32[1] = bswap_32(u.val32[1]);
  1322. u.val64 = bswap_64(u.val64);
  1323. }
  1324. *array = u.val64;
  1325. array++;
  1326. }
  1327. if (type & PERF_SAMPLE_TIME) {
  1328. *array = sample->time;
  1329. array++;
  1330. }
  1331. if (type & PERF_SAMPLE_ADDR) {
  1332. *array = sample->addr;
  1333. array++;
  1334. }
  1335. if (type & PERF_SAMPLE_ID) {
  1336. *array = sample->id;
  1337. array++;
  1338. }
  1339. if (type & PERF_SAMPLE_STREAM_ID) {
  1340. *array = sample->stream_id;
  1341. array++;
  1342. }
  1343. if (type & PERF_SAMPLE_CPU) {
  1344. u.val32[0] = sample->cpu;
  1345. if (swapped) {
  1346. /*
  1347. * Inverse of what is done in perf_evsel__parse_sample
  1348. */
  1349. u.val32[0] = bswap_32(u.val32[0]);
  1350. u.val64 = bswap_64(u.val64);
  1351. }
  1352. *array = u.val64;
  1353. array++;
  1354. }
  1355. if (type & PERF_SAMPLE_PERIOD) {
  1356. *array = sample->period;
  1357. array++;
  1358. }
  1359. if (type & PERF_SAMPLE_READ) {
  1360. if (read_format & PERF_FORMAT_GROUP)
  1361. *array = sample->read.group.nr;
  1362. else
  1363. *array = sample->read.one.value;
  1364. array++;
  1365. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1366. *array = sample->read.time_enabled;
  1367. array++;
  1368. }
  1369. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1370. *array = sample->read.time_running;
  1371. array++;
  1372. }
  1373. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1374. if (read_format & PERF_FORMAT_GROUP) {
  1375. sz = sample->read.group.nr *
  1376. sizeof(struct sample_read_value);
  1377. memcpy(array, sample->read.group.values, sz);
  1378. array = (void *)array + sz;
  1379. } else {
  1380. *array = sample->read.one.id;
  1381. array++;
  1382. }
  1383. }
  1384. if (type & PERF_SAMPLE_CALLCHAIN) {
  1385. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1386. memcpy(array, sample->callchain, sz);
  1387. array = (void *)array + sz;
  1388. }
  1389. if (type & PERF_SAMPLE_RAW) {
  1390. u.val32[0] = sample->raw_size;
  1391. if (WARN_ONCE(swapped,
  1392. "Endianness of raw data not corrected!\n")) {
  1393. /*
  1394. * Inverse of what is done in perf_evsel__parse_sample
  1395. */
  1396. u.val32[0] = bswap_32(u.val32[0]);
  1397. u.val32[1] = bswap_32(u.val32[1]);
  1398. u.val64 = bswap_64(u.val64);
  1399. }
  1400. *array = u.val64;
  1401. array = (void *)array + sizeof(u32);
  1402. memcpy(array, sample->raw_data, sample->raw_size);
  1403. array = (void *)array + sample->raw_size;
  1404. }
  1405. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1406. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1407. sz += sizeof(u64);
  1408. memcpy(array, sample->branch_stack, sz);
  1409. array = (void *)array + sz;
  1410. }
  1411. if (type & PERF_SAMPLE_REGS_USER) {
  1412. if (sample->user_regs.abi) {
  1413. *array++ = sample->user_regs.abi;
  1414. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1415. memcpy(array, sample->user_regs.regs, sz);
  1416. array = (void *)array + sz;
  1417. } else {
  1418. *array++ = 0;
  1419. }
  1420. }
  1421. if (type & PERF_SAMPLE_STACK_USER) {
  1422. sz = sample->user_stack.size;
  1423. *array++ = sz;
  1424. if (sz) {
  1425. memcpy(array, sample->user_stack.data, sz);
  1426. array = (void *)array + sz;
  1427. *array++ = sz;
  1428. }
  1429. }
  1430. if (type & PERF_SAMPLE_WEIGHT) {
  1431. *array = sample->weight;
  1432. array++;
  1433. }
  1434. if (type & PERF_SAMPLE_DATA_SRC) {
  1435. *array = sample->data_src;
  1436. array++;
  1437. }
  1438. if (type & PERF_SAMPLE_TRANSACTION) {
  1439. *array = sample->transaction;
  1440. array++;
  1441. }
  1442. return 0;
  1443. }
  1444. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1445. {
  1446. return pevent_find_field(evsel->tp_format, name);
  1447. }
  1448. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1449. const char *name)
  1450. {
  1451. struct format_field *field = perf_evsel__field(evsel, name);
  1452. int offset;
  1453. if (!field)
  1454. return NULL;
  1455. offset = field->offset;
  1456. if (field->flags & FIELD_IS_DYNAMIC) {
  1457. offset = *(int *)(sample->raw_data + field->offset);
  1458. offset &= 0xffff;
  1459. }
  1460. return sample->raw_data + offset;
  1461. }
  1462. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1463. const char *name)
  1464. {
  1465. struct format_field *field = perf_evsel__field(evsel, name);
  1466. void *ptr;
  1467. u64 value;
  1468. if (!field)
  1469. return 0;
  1470. ptr = sample->raw_data + field->offset;
  1471. switch (field->size) {
  1472. case 1:
  1473. return *(u8 *)ptr;
  1474. case 2:
  1475. value = *(u16 *)ptr;
  1476. break;
  1477. case 4:
  1478. value = *(u32 *)ptr;
  1479. break;
  1480. case 8:
  1481. value = *(u64 *)ptr;
  1482. break;
  1483. default:
  1484. return 0;
  1485. }
  1486. if (!evsel->needs_swap)
  1487. return value;
  1488. switch (field->size) {
  1489. case 2:
  1490. return bswap_16(value);
  1491. case 4:
  1492. return bswap_32(value);
  1493. case 8:
  1494. return bswap_64(value);
  1495. default:
  1496. return 0;
  1497. }
  1498. return 0;
  1499. }
  1500. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1501. {
  1502. va_list args;
  1503. int ret = 0;
  1504. if (!*first) {
  1505. ret += fprintf(fp, ",");
  1506. } else {
  1507. ret += fprintf(fp, ":");
  1508. *first = false;
  1509. }
  1510. va_start(args, fmt);
  1511. ret += vfprintf(fp, fmt, args);
  1512. va_end(args);
  1513. return ret;
  1514. }
  1515. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1516. {
  1517. if (value == 0)
  1518. return 0;
  1519. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1520. }
  1521. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1522. struct bit_names {
  1523. int bit;
  1524. const char *name;
  1525. };
  1526. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1527. struct bit_names *bits, bool *first)
  1528. {
  1529. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1530. bool first_bit = true;
  1531. do {
  1532. if (value & bits[i].bit) {
  1533. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1534. first_bit = false;
  1535. }
  1536. } while (bits[++i].name != NULL);
  1537. return printed;
  1538. }
  1539. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1540. {
  1541. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1542. struct bit_names bits[] = {
  1543. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1544. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1545. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1546. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1547. bit_name(IDENTIFIER),
  1548. { .name = NULL, }
  1549. };
  1550. #undef bit_name
  1551. return bits__fprintf(fp, "sample_type", value, bits, first);
  1552. }
  1553. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1554. {
  1555. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1556. struct bit_names bits[] = {
  1557. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1558. bit_name(ID), bit_name(GROUP),
  1559. { .name = NULL, }
  1560. };
  1561. #undef bit_name
  1562. return bits__fprintf(fp, "read_format", value, bits, first);
  1563. }
  1564. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1565. struct perf_attr_details *details, FILE *fp)
  1566. {
  1567. bool first = true;
  1568. int printed = 0;
  1569. if (details->event_group) {
  1570. struct perf_evsel *pos;
  1571. if (!perf_evsel__is_group_leader(evsel))
  1572. return 0;
  1573. if (evsel->nr_members > 1)
  1574. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  1575. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1576. for_each_group_member(pos, evsel)
  1577. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  1578. if (evsel->nr_members > 1)
  1579. printed += fprintf(fp, "}");
  1580. goto out;
  1581. }
  1582. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1583. if (details->verbose || details->freq) {
  1584. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1585. (u64)evsel->attr.sample_freq);
  1586. }
  1587. if (details->verbose) {
  1588. if_print(type);
  1589. if_print(config);
  1590. if_print(config1);
  1591. if_print(config2);
  1592. if_print(size);
  1593. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1594. if (evsel->attr.read_format)
  1595. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1596. if_print(disabled);
  1597. if_print(inherit);
  1598. if_print(pinned);
  1599. if_print(exclusive);
  1600. if_print(exclude_user);
  1601. if_print(exclude_kernel);
  1602. if_print(exclude_hv);
  1603. if_print(exclude_idle);
  1604. if_print(mmap);
  1605. if_print(mmap2);
  1606. if_print(comm);
  1607. if_print(freq);
  1608. if_print(inherit_stat);
  1609. if_print(enable_on_exec);
  1610. if_print(task);
  1611. if_print(watermark);
  1612. if_print(precise_ip);
  1613. if_print(mmap_data);
  1614. if_print(sample_id_all);
  1615. if_print(exclude_host);
  1616. if_print(exclude_guest);
  1617. if_print(__reserved_1);
  1618. if_print(wakeup_events);
  1619. if_print(bp_type);
  1620. if_print(branch_sample_type);
  1621. }
  1622. out:
  1623. fputc('\n', fp);
  1624. return ++printed;
  1625. }
  1626. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1627. char *msg, size_t msgsize)
  1628. {
  1629. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1630. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1631. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1632. /*
  1633. * If it's cycles then fall back to hrtimer based
  1634. * cpu-clock-tick sw counter, which is always available even if
  1635. * no PMU support.
  1636. *
  1637. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1638. * b0a873e).
  1639. */
  1640. scnprintf(msg, msgsize, "%s",
  1641. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1642. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1643. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1644. zfree(&evsel->name);
  1645. return true;
  1646. }
  1647. return false;
  1648. }
  1649. int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
  1650. int err, char *msg, size_t size)
  1651. {
  1652. switch (err) {
  1653. case EPERM:
  1654. case EACCES:
  1655. return scnprintf(msg, size,
  1656. "You may not have permission to collect %sstats.\n"
  1657. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  1658. " -1 - Not paranoid at all\n"
  1659. " 0 - Disallow raw tracepoint access for unpriv\n"
  1660. " 1 - Disallow cpu events for unpriv\n"
  1661. " 2 - Disallow kernel profiling for unpriv",
  1662. target->system_wide ? "system-wide " : "");
  1663. case ENOENT:
  1664. return scnprintf(msg, size, "The %s event is not supported.",
  1665. perf_evsel__name(evsel));
  1666. case EMFILE:
  1667. return scnprintf(msg, size, "%s",
  1668. "Too many events are opened.\n"
  1669. "Try again after reducing the number of events.");
  1670. case ENODEV:
  1671. if (target->cpu_list)
  1672. return scnprintf(msg, size, "%s",
  1673. "No such device - did you specify an out-of-range profile CPU?\n");
  1674. break;
  1675. case EOPNOTSUPP:
  1676. if (evsel->attr.precise_ip)
  1677. return scnprintf(msg, size, "%s",
  1678. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  1679. #if defined(__i386__) || defined(__x86_64__)
  1680. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  1681. return scnprintf(msg, size, "%s",
  1682. "No hardware sampling interrupt available.\n"
  1683. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  1684. #endif
  1685. break;
  1686. default:
  1687. break;
  1688. }
  1689. return scnprintf(msg, size,
  1690. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
  1691. "/bin/dmesg may provide additional information.\n"
  1692. "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
  1693. err, strerror(err), perf_evsel__name(evsel));
  1694. }