evsel.c 48 KB

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