header.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878
  1. #include "util.h"
  2. #include <sys/types.h>
  3. #include <byteswap.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <linux/list.h>
  8. #include <linux/kernel.h>
  9. #include <linux/bitops.h>
  10. #include <sys/utsname.h>
  11. #include "evlist.h"
  12. #include "evsel.h"
  13. #include "header.h"
  14. #include "../perf.h"
  15. #include "trace-event.h"
  16. #include "session.h"
  17. #include "symbol.h"
  18. #include "debug.h"
  19. #include "cpumap.h"
  20. #include "pmu.h"
  21. #include "vdso.h"
  22. #include "strbuf.h"
  23. #include "build-id.h"
  24. #include "data.h"
  25. /*
  26. * magic2 = "PERFILE2"
  27. * must be a numerical value to let the endianness
  28. * determine the memory layout. That way we are able
  29. * to detect endianness when reading the perf.data file
  30. * back.
  31. *
  32. * we check for legacy (PERFFILE) format.
  33. */
  34. static const char *__perf_magic1 = "PERFFILE";
  35. static const u64 __perf_magic2 = 0x32454c4946524550ULL;
  36. static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
  37. #define PERF_MAGIC __perf_magic2
  38. struct perf_file_attr {
  39. struct perf_event_attr attr;
  40. struct perf_file_section ids;
  41. };
  42. void perf_header__set_feat(struct perf_header *header, int feat)
  43. {
  44. set_bit(feat, header->adds_features);
  45. }
  46. void perf_header__clear_feat(struct perf_header *header, int feat)
  47. {
  48. clear_bit(feat, header->adds_features);
  49. }
  50. bool perf_header__has_feat(const struct perf_header *header, int feat)
  51. {
  52. return test_bit(feat, header->adds_features);
  53. }
  54. static int do_write(int fd, const void *buf, size_t size)
  55. {
  56. while (size) {
  57. int ret = write(fd, buf, size);
  58. if (ret < 0)
  59. return -errno;
  60. size -= ret;
  61. buf += ret;
  62. }
  63. return 0;
  64. }
  65. int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
  66. {
  67. static const char zero_buf[NAME_ALIGN];
  68. int err = do_write(fd, bf, count);
  69. if (!err)
  70. err = do_write(fd, zero_buf, count_aligned - count);
  71. return err;
  72. }
  73. #define string_size(str) \
  74. (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
  75. static int do_write_string(int fd, const char *str)
  76. {
  77. u32 len, olen;
  78. int ret;
  79. olen = strlen(str) + 1;
  80. len = PERF_ALIGN(olen, NAME_ALIGN);
  81. /* write len, incl. \0 */
  82. ret = do_write(fd, &len, sizeof(len));
  83. if (ret < 0)
  84. return ret;
  85. return write_padded(fd, str, olen, len);
  86. }
  87. static char *do_read_string(int fd, struct perf_header *ph)
  88. {
  89. ssize_t sz, ret;
  90. u32 len;
  91. char *buf;
  92. sz = readn(fd, &len, sizeof(len));
  93. if (sz < (ssize_t)sizeof(len))
  94. return NULL;
  95. if (ph->needs_swap)
  96. len = bswap_32(len);
  97. buf = malloc(len);
  98. if (!buf)
  99. return NULL;
  100. ret = readn(fd, buf, len);
  101. if (ret == (ssize_t)len) {
  102. /*
  103. * strings are padded by zeroes
  104. * thus the actual strlen of buf
  105. * may be less than len
  106. */
  107. return buf;
  108. }
  109. free(buf);
  110. return NULL;
  111. }
  112. static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
  113. struct perf_evlist *evlist)
  114. {
  115. return read_tracing_data(fd, &evlist->entries);
  116. }
  117. static int write_build_id(int fd, struct perf_header *h,
  118. struct perf_evlist *evlist __maybe_unused)
  119. {
  120. struct perf_session *session;
  121. int err;
  122. session = container_of(h, struct perf_session, header);
  123. if (!perf_session__read_build_ids(session, true))
  124. return -1;
  125. err = perf_session__write_buildid_table(session, fd);
  126. if (err < 0) {
  127. pr_debug("failed to write buildid table\n");
  128. return err;
  129. }
  130. perf_session__cache_build_ids(session);
  131. return 0;
  132. }
  133. static int write_hostname(int fd, struct perf_header *h __maybe_unused,
  134. struct perf_evlist *evlist __maybe_unused)
  135. {
  136. struct utsname uts;
  137. int ret;
  138. ret = uname(&uts);
  139. if (ret < 0)
  140. return -1;
  141. return do_write_string(fd, uts.nodename);
  142. }
  143. static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
  144. struct perf_evlist *evlist __maybe_unused)
  145. {
  146. struct utsname uts;
  147. int ret;
  148. ret = uname(&uts);
  149. if (ret < 0)
  150. return -1;
  151. return do_write_string(fd, uts.release);
  152. }
  153. static int write_arch(int fd, struct perf_header *h __maybe_unused,
  154. struct perf_evlist *evlist __maybe_unused)
  155. {
  156. struct utsname uts;
  157. int ret;
  158. ret = uname(&uts);
  159. if (ret < 0)
  160. return -1;
  161. return do_write_string(fd, uts.machine);
  162. }
  163. static int write_version(int fd, struct perf_header *h __maybe_unused,
  164. struct perf_evlist *evlist __maybe_unused)
  165. {
  166. return do_write_string(fd, perf_version_string);
  167. }
  168. static int __write_cpudesc(int fd, const char *cpuinfo_proc)
  169. {
  170. FILE *file;
  171. char *buf = NULL;
  172. char *s, *p;
  173. const char *search = cpuinfo_proc;
  174. size_t len = 0;
  175. int ret = -1;
  176. if (!search)
  177. return -1;
  178. file = fopen("/proc/cpuinfo", "r");
  179. if (!file)
  180. return -1;
  181. while (getline(&buf, &len, file) > 0) {
  182. ret = strncmp(buf, search, strlen(search));
  183. if (!ret)
  184. break;
  185. }
  186. if (ret) {
  187. ret = -1;
  188. goto done;
  189. }
  190. s = buf;
  191. p = strchr(buf, ':');
  192. if (p && *(p+1) == ' ' && *(p+2))
  193. s = p + 2;
  194. p = strchr(s, '\n');
  195. if (p)
  196. *p = '\0';
  197. /* squash extra space characters (branding string) */
  198. p = s;
  199. while (*p) {
  200. if (isspace(*p)) {
  201. char *r = p + 1;
  202. char *q = r;
  203. *p = ' ';
  204. while (*q && isspace(*q))
  205. q++;
  206. if (q != (p+1))
  207. while ((*r++ = *q++));
  208. }
  209. p++;
  210. }
  211. ret = do_write_string(fd, s);
  212. done:
  213. free(buf);
  214. fclose(file);
  215. return ret;
  216. }
  217. static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
  218. struct perf_evlist *evlist __maybe_unused)
  219. {
  220. #ifndef CPUINFO_PROC
  221. #define CPUINFO_PROC {"model name", }
  222. #endif
  223. const char *cpuinfo_procs[] = CPUINFO_PROC;
  224. unsigned int i;
  225. for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
  226. int ret;
  227. ret = __write_cpudesc(fd, cpuinfo_procs[i]);
  228. if (ret >= 0)
  229. return ret;
  230. }
  231. return -1;
  232. }
  233. static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
  234. struct perf_evlist *evlist __maybe_unused)
  235. {
  236. long nr;
  237. u32 nrc, nra;
  238. int ret;
  239. nr = sysconf(_SC_NPROCESSORS_CONF);
  240. if (nr < 0)
  241. return -1;
  242. nrc = (u32)(nr & UINT_MAX);
  243. nr = sysconf(_SC_NPROCESSORS_ONLN);
  244. if (nr < 0)
  245. return -1;
  246. nra = (u32)(nr & UINT_MAX);
  247. ret = do_write(fd, &nrc, sizeof(nrc));
  248. if (ret < 0)
  249. return ret;
  250. return do_write(fd, &nra, sizeof(nra));
  251. }
  252. static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
  253. struct perf_evlist *evlist)
  254. {
  255. struct perf_evsel *evsel;
  256. u32 nre, nri, sz;
  257. int ret;
  258. nre = evlist->nr_entries;
  259. /*
  260. * write number of events
  261. */
  262. ret = do_write(fd, &nre, sizeof(nre));
  263. if (ret < 0)
  264. return ret;
  265. /*
  266. * size of perf_event_attr struct
  267. */
  268. sz = (u32)sizeof(evsel->attr);
  269. ret = do_write(fd, &sz, sizeof(sz));
  270. if (ret < 0)
  271. return ret;
  272. evlist__for_each(evlist, evsel) {
  273. ret = do_write(fd, &evsel->attr, sz);
  274. if (ret < 0)
  275. return ret;
  276. /*
  277. * write number of unique id per event
  278. * there is one id per instance of an event
  279. *
  280. * copy into an nri to be independent of the
  281. * type of ids,
  282. */
  283. nri = evsel->ids;
  284. ret = do_write(fd, &nri, sizeof(nri));
  285. if (ret < 0)
  286. return ret;
  287. /*
  288. * write event string as passed on cmdline
  289. */
  290. ret = do_write_string(fd, perf_evsel__name(evsel));
  291. if (ret < 0)
  292. return ret;
  293. /*
  294. * write unique ids for this event
  295. */
  296. ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
  297. if (ret < 0)
  298. return ret;
  299. }
  300. return 0;
  301. }
  302. static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
  303. struct perf_evlist *evlist __maybe_unused)
  304. {
  305. char buf[MAXPATHLEN];
  306. char proc[32];
  307. u32 n;
  308. int i, ret;
  309. /*
  310. * actual atual path to perf binary
  311. */
  312. sprintf(proc, "/proc/%d/exe", getpid());
  313. ret = readlink(proc, buf, sizeof(buf));
  314. if (ret <= 0)
  315. return -1;
  316. /* readlink() does not add null termination */
  317. buf[ret] = '\0';
  318. /* account for binary path */
  319. n = perf_env.nr_cmdline + 1;
  320. ret = do_write(fd, &n, sizeof(n));
  321. if (ret < 0)
  322. return ret;
  323. ret = do_write_string(fd, buf);
  324. if (ret < 0)
  325. return ret;
  326. for (i = 0 ; i < perf_env.nr_cmdline; i++) {
  327. ret = do_write_string(fd, perf_env.cmdline_argv[i]);
  328. if (ret < 0)
  329. return ret;
  330. }
  331. return 0;
  332. }
  333. #define CORE_SIB_FMT \
  334. "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
  335. #define THRD_SIB_FMT \
  336. "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
  337. struct cpu_topo {
  338. u32 cpu_nr;
  339. u32 core_sib;
  340. u32 thread_sib;
  341. char **core_siblings;
  342. char **thread_siblings;
  343. };
  344. static int build_cpu_topo(struct cpu_topo *tp, int cpu)
  345. {
  346. FILE *fp;
  347. char filename[MAXPATHLEN];
  348. char *buf = NULL, *p;
  349. size_t len = 0;
  350. ssize_t sret;
  351. u32 i = 0;
  352. int ret = -1;
  353. sprintf(filename, CORE_SIB_FMT, cpu);
  354. fp = fopen(filename, "r");
  355. if (!fp)
  356. goto try_threads;
  357. sret = getline(&buf, &len, fp);
  358. fclose(fp);
  359. if (sret <= 0)
  360. goto try_threads;
  361. p = strchr(buf, '\n');
  362. if (p)
  363. *p = '\0';
  364. for (i = 0; i < tp->core_sib; i++) {
  365. if (!strcmp(buf, tp->core_siblings[i]))
  366. break;
  367. }
  368. if (i == tp->core_sib) {
  369. tp->core_siblings[i] = buf;
  370. tp->core_sib++;
  371. buf = NULL;
  372. len = 0;
  373. }
  374. ret = 0;
  375. try_threads:
  376. sprintf(filename, THRD_SIB_FMT, cpu);
  377. fp = fopen(filename, "r");
  378. if (!fp)
  379. goto done;
  380. if (getline(&buf, &len, fp) <= 0)
  381. goto done;
  382. p = strchr(buf, '\n');
  383. if (p)
  384. *p = '\0';
  385. for (i = 0; i < tp->thread_sib; i++) {
  386. if (!strcmp(buf, tp->thread_siblings[i]))
  387. break;
  388. }
  389. if (i == tp->thread_sib) {
  390. tp->thread_siblings[i] = buf;
  391. tp->thread_sib++;
  392. buf = NULL;
  393. }
  394. ret = 0;
  395. done:
  396. if(fp)
  397. fclose(fp);
  398. free(buf);
  399. return ret;
  400. }
  401. static void free_cpu_topo(struct cpu_topo *tp)
  402. {
  403. u32 i;
  404. if (!tp)
  405. return;
  406. for (i = 0 ; i < tp->core_sib; i++)
  407. zfree(&tp->core_siblings[i]);
  408. for (i = 0 ; i < tp->thread_sib; i++)
  409. zfree(&tp->thread_siblings[i]);
  410. free(tp);
  411. }
  412. static struct cpu_topo *build_cpu_topology(void)
  413. {
  414. struct cpu_topo *tp;
  415. void *addr;
  416. u32 nr, i;
  417. size_t sz;
  418. long ncpus;
  419. int ret = -1;
  420. ncpus = sysconf(_SC_NPROCESSORS_CONF);
  421. if (ncpus < 0)
  422. return NULL;
  423. nr = (u32)(ncpus & UINT_MAX);
  424. sz = nr * sizeof(char *);
  425. addr = calloc(1, sizeof(*tp) + 2 * sz);
  426. if (!addr)
  427. return NULL;
  428. tp = addr;
  429. tp->cpu_nr = nr;
  430. addr += sizeof(*tp);
  431. tp->core_siblings = addr;
  432. addr += sz;
  433. tp->thread_siblings = addr;
  434. for (i = 0; i < nr; i++) {
  435. ret = build_cpu_topo(tp, i);
  436. if (ret < 0)
  437. break;
  438. }
  439. if (ret) {
  440. free_cpu_topo(tp);
  441. tp = NULL;
  442. }
  443. return tp;
  444. }
  445. static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
  446. struct perf_evlist *evlist __maybe_unused)
  447. {
  448. struct cpu_topo *tp;
  449. u32 i;
  450. int ret, j;
  451. tp = build_cpu_topology();
  452. if (!tp)
  453. return -1;
  454. ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
  455. if (ret < 0)
  456. goto done;
  457. for (i = 0; i < tp->core_sib; i++) {
  458. ret = do_write_string(fd, tp->core_siblings[i]);
  459. if (ret < 0)
  460. goto done;
  461. }
  462. ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
  463. if (ret < 0)
  464. goto done;
  465. for (i = 0; i < tp->thread_sib; i++) {
  466. ret = do_write_string(fd, tp->thread_siblings[i]);
  467. if (ret < 0)
  468. break;
  469. }
  470. ret = perf_env__read_cpu_topology_map(&perf_env);
  471. if (ret < 0)
  472. goto done;
  473. for (j = 0; j < perf_env.nr_cpus_avail; j++) {
  474. ret = do_write(fd, &perf_env.cpu[j].core_id,
  475. sizeof(perf_env.cpu[j].core_id));
  476. if (ret < 0)
  477. return ret;
  478. ret = do_write(fd, &perf_env.cpu[j].socket_id,
  479. sizeof(perf_env.cpu[j].socket_id));
  480. if (ret < 0)
  481. return ret;
  482. }
  483. done:
  484. free_cpu_topo(tp);
  485. return ret;
  486. }
  487. static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
  488. struct perf_evlist *evlist __maybe_unused)
  489. {
  490. char *buf = NULL;
  491. FILE *fp;
  492. size_t len = 0;
  493. int ret = -1, n;
  494. uint64_t mem;
  495. fp = fopen("/proc/meminfo", "r");
  496. if (!fp)
  497. return -1;
  498. while (getline(&buf, &len, fp) > 0) {
  499. ret = strncmp(buf, "MemTotal:", 9);
  500. if (!ret)
  501. break;
  502. }
  503. if (!ret) {
  504. n = sscanf(buf, "%*s %"PRIu64, &mem);
  505. if (n == 1)
  506. ret = do_write(fd, &mem, sizeof(mem));
  507. } else
  508. ret = -1;
  509. free(buf);
  510. fclose(fp);
  511. return ret;
  512. }
  513. static int write_topo_node(int fd, int node)
  514. {
  515. char str[MAXPATHLEN];
  516. char field[32];
  517. char *buf = NULL, *p;
  518. size_t len = 0;
  519. FILE *fp;
  520. u64 mem_total, mem_free, mem;
  521. int ret = -1;
  522. sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
  523. fp = fopen(str, "r");
  524. if (!fp)
  525. return -1;
  526. while (getline(&buf, &len, fp) > 0) {
  527. /* skip over invalid lines */
  528. if (!strchr(buf, ':'))
  529. continue;
  530. if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
  531. goto done;
  532. if (!strcmp(field, "MemTotal:"))
  533. mem_total = mem;
  534. if (!strcmp(field, "MemFree:"))
  535. mem_free = mem;
  536. }
  537. fclose(fp);
  538. fp = NULL;
  539. ret = do_write(fd, &mem_total, sizeof(u64));
  540. if (ret)
  541. goto done;
  542. ret = do_write(fd, &mem_free, sizeof(u64));
  543. if (ret)
  544. goto done;
  545. ret = -1;
  546. sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
  547. fp = fopen(str, "r");
  548. if (!fp)
  549. goto done;
  550. if (getline(&buf, &len, fp) <= 0)
  551. goto done;
  552. p = strchr(buf, '\n');
  553. if (p)
  554. *p = '\0';
  555. ret = do_write_string(fd, buf);
  556. done:
  557. free(buf);
  558. if (fp)
  559. fclose(fp);
  560. return ret;
  561. }
  562. static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
  563. struct perf_evlist *evlist __maybe_unused)
  564. {
  565. char *buf = NULL;
  566. size_t len = 0;
  567. FILE *fp;
  568. struct cpu_map *node_map = NULL;
  569. char *c;
  570. u32 nr, i, j;
  571. int ret = -1;
  572. fp = fopen("/sys/devices/system/node/online", "r");
  573. if (!fp)
  574. return -1;
  575. if (getline(&buf, &len, fp) <= 0)
  576. goto done;
  577. c = strchr(buf, '\n');
  578. if (c)
  579. *c = '\0';
  580. node_map = cpu_map__new(buf);
  581. if (!node_map)
  582. goto done;
  583. nr = (u32)node_map->nr;
  584. ret = do_write(fd, &nr, sizeof(nr));
  585. if (ret < 0)
  586. goto done;
  587. for (i = 0; i < nr; i++) {
  588. j = (u32)node_map->map[i];
  589. ret = do_write(fd, &j, sizeof(j));
  590. if (ret < 0)
  591. break;
  592. ret = write_topo_node(fd, i);
  593. if (ret < 0)
  594. break;
  595. }
  596. done:
  597. free(buf);
  598. fclose(fp);
  599. free(node_map);
  600. return ret;
  601. }
  602. /*
  603. * File format:
  604. *
  605. * struct pmu_mappings {
  606. * u32 pmu_num;
  607. * struct pmu_map {
  608. * u32 type;
  609. * char name[];
  610. * }[pmu_num];
  611. * };
  612. */
  613. static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
  614. struct perf_evlist *evlist __maybe_unused)
  615. {
  616. struct perf_pmu *pmu = NULL;
  617. off_t offset = lseek(fd, 0, SEEK_CUR);
  618. __u32 pmu_num = 0;
  619. int ret;
  620. /* write real pmu_num later */
  621. ret = do_write(fd, &pmu_num, sizeof(pmu_num));
  622. if (ret < 0)
  623. return ret;
  624. while ((pmu = perf_pmu__scan(pmu))) {
  625. if (!pmu->name)
  626. continue;
  627. pmu_num++;
  628. ret = do_write(fd, &pmu->type, sizeof(pmu->type));
  629. if (ret < 0)
  630. return ret;
  631. ret = do_write_string(fd, pmu->name);
  632. if (ret < 0)
  633. return ret;
  634. }
  635. if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
  636. /* discard all */
  637. lseek(fd, offset, SEEK_SET);
  638. return -1;
  639. }
  640. return 0;
  641. }
  642. /*
  643. * File format:
  644. *
  645. * struct group_descs {
  646. * u32 nr_groups;
  647. * struct group_desc {
  648. * char name[];
  649. * u32 leader_idx;
  650. * u32 nr_members;
  651. * }[nr_groups];
  652. * };
  653. */
  654. static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
  655. struct perf_evlist *evlist)
  656. {
  657. u32 nr_groups = evlist->nr_groups;
  658. struct perf_evsel *evsel;
  659. int ret;
  660. ret = do_write(fd, &nr_groups, sizeof(nr_groups));
  661. if (ret < 0)
  662. return ret;
  663. evlist__for_each(evlist, evsel) {
  664. if (perf_evsel__is_group_leader(evsel) &&
  665. evsel->nr_members > 1) {
  666. const char *name = evsel->group_name ?: "{anon_group}";
  667. u32 leader_idx = evsel->idx;
  668. u32 nr_members = evsel->nr_members;
  669. ret = do_write_string(fd, name);
  670. if (ret < 0)
  671. return ret;
  672. ret = do_write(fd, &leader_idx, sizeof(leader_idx));
  673. if (ret < 0)
  674. return ret;
  675. ret = do_write(fd, &nr_members, sizeof(nr_members));
  676. if (ret < 0)
  677. return ret;
  678. }
  679. }
  680. return 0;
  681. }
  682. /*
  683. * default get_cpuid(): nothing gets recorded
  684. * actual implementation must be in arch/$(ARCH)/util/header.c
  685. */
  686. int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
  687. size_t sz __maybe_unused)
  688. {
  689. return -1;
  690. }
  691. static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
  692. struct perf_evlist *evlist __maybe_unused)
  693. {
  694. char buffer[64];
  695. int ret;
  696. ret = get_cpuid(buffer, sizeof(buffer));
  697. if (!ret)
  698. goto write_it;
  699. return -1;
  700. write_it:
  701. return do_write_string(fd, buffer);
  702. }
  703. static int write_branch_stack(int fd __maybe_unused,
  704. struct perf_header *h __maybe_unused,
  705. struct perf_evlist *evlist __maybe_unused)
  706. {
  707. return 0;
  708. }
  709. static int write_auxtrace(int fd, struct perf_header *h,
  710. struct perf_evlist *evlist __maybe_unused)
  711. {
  712. struct perf_session *session;
  713. int err;
  714. session = container_of(h, struct perf_session, header);
  715. err = auxtrace_index__write(fd, &session->auxtrace_index);
  716. if (err < 0)
  717. pr_err("Failed to write auxtrace index\n");
  718. return err;
  719. }
  720. static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
  721. FILE *fp)
  722. {
  723. fprintf(fp, "# hostname : %s\n", ph->env.hostname);
  724. }
  725. static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
  726. FILE *fp)
  727. {
  728. fprintf(fp, "# os release : %s\n", ph->env.os_release);
  729. }
  730. static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
  731. {
  732. fprintf(fp, "# arch : %s\n", ph->env.arch);
  733. }
  734. static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
  735. FILE *fp)
  736. {
  737. fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
  738. }
  739. static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
  740. FILE *fp)
  741. {
  742. fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
  743. fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
  744. }
  745. static void print_version(struct perf_header *ph, int fd __maybe_unused,
  746. FILE *fp)
  747. {
  748. fprintf(fp, "# perf version : %s\n", ph->env.version);
  749. }
  750. static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
  751. FILE *fp)
  752. {
  753. int nr, i;
  754. nr = ph->env.nr_cmdline;
  755. fprintf(fp, "# cmdline : ");
  756. for (i = 0; i < nr; i++)
  757. fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
  758. fputc('\n', fp);
  759. }
  760. static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
  761. FILE *fp)
  762. {
  763. int nr, i;
  764. char *str;
  765. int cpu_nr = ph->env.nr_cpus_online;
  766. nr = ph->env.nr_sibling_cores;
  767. str = ph->env.sibling_cores;
  768. for (i = 0; i < nr; i++) {
  769. fprintf(fp, "# sibling cores : %s\n", str);
  770. str += strlen(str) + 1;
  771. }
  772. nr = ph->env.nr_sibling_threads;
  773. str = ph->env.sibling_threads;
  774. for (i = 0; i < nr; i++) {
  775. fprintf(fp, "# sibling threads : %s\n", str);
  776. str += strlen(str) + 1;
  777. }
  778. if (ph->env.cpu != NULL) {
  779. for (i = 0; i < cpu_nr; i++)
  780. fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
  781. ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
  782. } else
  783. fprintf(fp, "# Core ID and Socket ID information is not available\n");
  784. }
  785. static void free_event_desc(struct perf_evsel *events)
  786. {
  787. struct perf_evsel *evsel;
  788. if (!events)
  789. return;
  790. for (evsel = events; evsel->attr.size; evsel++) {
  791. zfree(&evsel->name);
  792. zfree(&evsel->id);
  793. }
  794. free(events);
  795. }
  796. static struct perf_evsel *
  797. read_event_desc(struct perf_header *ph, int fd)
  798. {
  799. struct perf_evsel *evsel, *events = NULL;
  800. u64 *id;
  801. void *buf = NULL;
  802. u32 nre, sz, nr, i, j;
  803. ssize_t ret;
  804. size_t msz;
  805. /* number of events */
  806. ret = readn(fd, &nre, sizeof(nre));
  807. if (ret != (ssize_t)sizeof(nre))
  808. goto error;
  809. if (ph->needs_swap)
  810. nre = bswap_32(nre);
  811. ret = readn(fd, &sz, sizeof(sz));
  812. if (ret != (ssize_t)sizeof(sz))
  813. goto error;
  814. if (ph->needs_swap)
  815. sz = bswap_32(sz);
  816. /* buffer to hold on file attr struct */
  817. buf = malloc(sz);
  818. if (!buf)
  819. goto error;
  820. /* the last event terminates with evsel->attr.size == 0: */
  821. events = calloc(nre + 1, sizeof(*events));
  822. if (!events)
  823. goto error;
  824. msz = sizeof(evsel->attr);
  825. if (sz < msz)
  826. msz = sz;
  827. for (i = 0, evsel = events; i < nre; evsel++, i++) {
  828. evsel->idx = i;
  829. /*
  830. * must read entire on-file attr struct to
  831. * sync up with layout.
  832. */
  833. ret = readn(fd, buf, sz);
  834. if (ret != (ssize_t)sz)
  835. goto error;
  836. if (ph->needs_swap)
  837. perf_event__attr_swap(buf);
  838. memcpy(&evsel->attr, buf, msz);
  839. ret = readn(fd, &nr, sizeof(nr));
  840. if (ret != (ssize_t)sizeof(nr))
  841. goto error;
  842. if (ph->needs_swap) {
  843. nr = bswap_32(nr);
  844. evsel->needs_swap = true;
  845. }
  846. evsel->name = do_read_string(fd, ph);
  847. if (!nr)
  848. continue;
  849. id = calloc(nr, sizeof(*id));
  850. if (!id)
  851. goto error;
  852. evsel->ids = nr;
  853. evsel->id = id;
  854. for (j = 0 ; j < nr; j++) {
  855. ret = readn(fd, id, sizeof(*id));
  856. if (ret != (ssize_t)sizeof(*id))
  857. goto error;
  858. if (ph->needs_swap)
  859. *id = bswap_64(*id);
  860. id++;
  861. }
  862. }
  863. out:
  864. free(buf);
  865. return events;
  866. error:
  867. free_event_desc(events);
  868. events = NULL;
  869. goto out;
  870. }
  871. static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
  872. void *priv __attribute__((unused)))
  873. {
  874. return fprintf(fp, ", %s = %s", name, val);
  875. }
  876. static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
  877. {
  878. struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
  879. u32 j;
  880. u64 *id;
  881. if (!events) {
  882. fprintf(fp, "# event desc: not available or unable to read\n");
  883. return;
  884. }
  885. for (evsel = events; evsel->attr.size; evsel++) {
  886. fprintf(fp, "# event : name = %s, ", evsel->name);
  887. if (evsel->ids) {
  888. fprintf(fp, ", id = {");
  889. for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
  890. if (j)
  891. fputc(',', fp);
  892. fprintf(fp, " %"PRIu64, *id);
  893. }
  894. fprintf(fp, " }");
  895. }
  896. perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
  897. fputc('\n', fp);
  898. }
  899. free_event_desc(events);
  900. }
  901. static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
  902. FILE *fp)
  903. {
  904. fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
  905. }
  906. static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
  907. FILE *fp)
  908. {
  909. u32 nr, c, i;
  910. char *str, *tmp;
  911. uint64_t mem_total, mem_free;
  912. /* nr nodes */
  913. nr = ph->env.nr_numa_nodes;
  914. str = ph->env.numa_nodes;
  915. for (i = 0; i < nr; i++) {
  916. /* node number */
  917. c = strtoul(str, &tmp, 0);
  918. if (*tmp != ':')
  919. goto error;
  920. str = tmp + 1;
  921. mem_total = strtoull(str, &tmp, 0);
  922. if (*tmp != ':')
  923. goto error;
  924. str = tmp + 1;
  925. mem_free = strtoull(str, &tmp, 0);
  926. if (*tmp != ':')
  927. goto error;
  928. fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
  929. " free = %"PRIu64" kB\n",
  930. c, mem_total, mem_free);
  931. str = tmp + 1;
  932. fprintf(fp, "# node%u cpu list : %s\n", c, str);
  933. str += strlen(str) + 1;
  934. }
  935. return;
  936. error:
  937. fprintf(fp, "# numa topology : not available\n");
  938. }
  939. static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
  940. {
  941. fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
  942. }
  943. static void print_branch_stack(struct perf_header *ph __maybe_unused,
  944. int fd __maybe_unused, FILE *fp)
  945. {
  946. fprintf(fp, "# contains samples with branch stack\n");
  947. }
  948. static void print_auxtrace(struct perf_header *ph __maybe_unused,
  949. int fd __maybe_unused, FILE *fp)
  950. {
  951. fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
  952. }
  953. static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
  954. FILE *fp)
  955. {
  956. const char *delimiter = "# pmu mappings: ";
  957. char *str, *tmp;
  958. u32 pmu_num;
  959. u32 type;
  960. pmu_num = ph->env.nr_pmu_mappings;
  961. if (!pmu_num) {
  962. fprintf(fp, "# pmu mappings: not available\n");
  963. return;
  964. }
  965. str = ph->env.pmu_mappings;
  966. while (pmu_num) {
  967. type = strtoul(str, &tmp, 0);
  968. if (*tmp != ':')
  969. goto error;
  970. str = tmp + 1;
  971. fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
  972. delimiter = ", ";
  973. str += strlen(str) + 1;
  974. pmu_num--;
  975. }
  976. fprintf(fp, "\n");
  977. if (!pmu_num)
  978. return;
  979. error:
  980. fprintf(fp, "# pmu mappings: unable to read\n");
  981. }
  982. static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
  983. FILE *fp)
  984. {
  985. struct perf_session *session;
  986. struct perf_evsel *evsel;
  987. u32 nr = 0;
  988. session = container_of(ph, struct perf_session, header);
  989. evlist__for_each(session->evlist, evsel) {
  990. if (perf_evsel__is_group_leader(evsel) &&
  991. evsel->nr_members > 1) {
  992. fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
  993. perf_evsel__name(evsel));
  994. nr = evsel->nr_members - 1;
  995. } else if (nr) {
  996. fprintf(fp, ",%s", perf_evsel__name(evsel));
  997. if (--nr == 0)
  998. fprintf(fp, "}\n");
  999. }
  1000. }
  1001. }
  1002. static int __event_process_build_id(struct build_id_event *bev,
  1003. char *filename,
  1004. struct perf_session *session)
  1005. {
  1006. int err = -1;
  1007. struct machine *machine;
  1008. u16 cpumode;
  1009. struct dso *dso;
  1010. enum dso_kernel_type dso_type;
  1011. machine = perf_session__findnew_machine(session, bev->pid);
  1012. if (!machine)
  1013. goto out;
  1014. cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  1015. switch (cpumode) {
  1016. case PERF_RECORD_MISC_KERNEL:
  1017. dso_type = DSO_TYPE_KERNEL;
  1018. break;
  1019. case PERF_RECORD_MISC_GUEST_KERNEL:
  1020. dso_type = DSO_TYPE_GUEST_KERNEL;
  1021. break;
  1022. case PERF_RECORD_MISC_USER:
  1023. case PERF_RECORD_MISC_GUEST_USER:
  1024. dso_type = DSO_TYPE_USER;
  1025. break;
  1026. default:
  1027. goto out;
  1028. }
  1029. dso = machine__findnew_dso(machine, filename);
  1030. if (dso != NULL) {
  1031. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1032. dso__set_build_id(dso, &bev->build_id);
  1033. if (dso_type != DSO_TYPE_USER) {
  1034. struct kmod_path m = { .name = NULL, };
  1035. if (!kmod_path__parse_name(&m, filename) && m.kmod)
  1036. dso__set_short_name(dso, strdup(m.name), true);
  1037. else
  1038. dso->kernel = dso_type;
  1039. free(m.name);
  1040. }
  1041. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1042. sbuild_id);
  1043. pr_debug("build id event received for %s: %s\n",
  1044. dso->long_name, sbuild_id);
  1045. dso__put(dso);
  1046. }
  1047. err = 0;
  1048. out:
  1049. return err;
  1050. }
  1051. static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
  1052. int input, u64 offset, u64 size)
  1053. {
  1054. struct perf_session *session = container_of(header, struct perf_session, header);
  1055. struct {
  1056. struct perf_event_header header;
  1057. u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
  1058. char filename[0];
  1059. } old_bev;
  1060. struct build_id_event bev;
  1061. char filename[PATH_MAX];
  1062. u64 limit = offset + size;
  1063. while (offset < limit) {
  1064. ssize_t len;
  1065. if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
  1066. return -1;
  1067. if (header->needs_swap)
  1068. perf_event_header__bswap(&old_bev.header);
  1069. len = old_bev.header.size - sizeof(old_bev);
  1070. if (readn(input, filename, len) != len)
  1071. return -1;
  1072. bev.header = old_bev.header;
  1073. /*
  1074. * As the pid is the missing value, we need to fill
  1075. * it properly. The header.misc value give us nice hint.
  1076. */
  1077. bev.pid = HOST_KERNEL_ID;
  1078. if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
  1079. bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
  1080. bev.pid = DEFAULT_GUEST_KERNEL_ID;
  1081. memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
  1082. __event_process_build_id(&bev, filename, session);
  1083. offset += bev.header.size;
  1084. }
  1085. return 0;
  1086. }
  1087. static int perf_header__read_build_ids(struct perf_header *header,
  1088. int input, u64 offset, u64 size)
  1089. {
  1090. struct perf_session *session = container_of(header, struct perf_session, header);
  1091. struct build_id_event bev;
  1092. char filename[PATH_MAX];
  1093. u64 limit = offset + size, orig_offset = offset;
  1094. int err = -1;
  1095. while (offset < limit) {
  1096. ssize_t len;
  1097. if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
  1098. goto out;
  1099. if (header->needs_swap)
  1100. perf_event_header__bswap(&bev.header);
  1101. len = bev.header.size - sizeof(bev);
  1102. if (readn(input, filename, len) != len)
  1103. goto out;
  1104. /*
  1105. * The a1645ce1 changeset:
  1106. *
  1107. * "perf: 'perf kvm' tool for monitoring guest performance from host"
  1108. *
  1109. * Added a field to struct build_id_event that broke the file
  1110. * format.
  1111. *
  1112. * Since the kernel build-id is the first entry, process the
  1113. * table using the old format if the well known
  1114. * '[kernel.kallsyms]' string for the kernel build-id has the
  1115. * first 4 characters chopped off (where the pid_t sits).
  1116. */
  1117. if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
  1118. if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
  1119. return -1;
  1120. return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
  1121. }
  1122. __event_process_build_id(&bev, filename, session);
  1123. offset += bev.header.size;
  1124. }
  1125. err = 0;
  1126. out:
  1127. return err;
  1128. }
  1129. static int process_tracing_data(struct perf_file_section *section __maybe_unused,
  1130. struct perf_header *ph __maybe_unused,
  1131. int fd, void *data)
  1132. {
  1133. ssize_t ret = trace_report(fd, data, false);
  1134. return ret < 0 ? -1 : 0;
  1135. }
  1136. static int process_build_id(struct perf_file_section *section,
  1137. struct perf_header *ph, int fd,
  1138. void *data __maybe_unused)
  1139. {
  1140. if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
  1141. pr_debug("Failed to read buildids, continuing...\n");
  1142. return 0;
  1143. }
  1144. static int process_hostname(struct perf_file_section *section __maybe_unused,
  1145. struct perf_header *ph, int fd,
  1146. void *data __maybe_unused)
  1147. {
  1148. ph->env.hostname = do_read_string(fd, ph);
  1149. return ph->env.hostname ? 0 : -ENOMEM;
  1150. }
  1151. static int process_osrelease(struct perf_file_section *section __maybe_unused,
  1152. struct perf_header *ph, int fd,
  1153. void *data __maybe_unused)
  1154. {
  1155. ph->env.os_release = do_read_string(fd, ph);
  1156. return ph->env.os_release ? 0 : -ENOMEM;
  1157. }
  1158. static int process_version(struct perf_file_section *section __maybe_unused,
  1159. struct perf_header *ph, int fd,
  1160. void *data __maybe_unused)
  1161. {
  1162. ph->env.version = do_read_string(fd, ph);
  1163. return ph->env.version ? 0 : -ENOMEM;
  1164. }
  1165. static int process_arch(struct perf_file_section *section __maybe_unused,
  1166. struct perf_header *ph, int fd,
  1167. void *data __maybe_unused)
  1168. {
  1169. ph->env.arch = do_read_string(fd, ph);
  1170. return ph->env.arch ? 0 : -ENOMEM;
  1171. }
  1172. static int process_nrcpus(struct perf_file_section *section __maybe_unused,
  1173. struct perf_header *ph, int fd,
  1174. void *data __maybe_unused)
  1175. {
  1176. ssize_t ret;
  1177. u32 nr;
  1178. ret = readn(fd, &nr, sizeof(nr));
  1179. if (ret != sizeof(nr))
  1180. return -1;
  1181. if (ph->needs_swap)
  1182. nr = bswap_32(nr);
  1183. ph->env.nr_cpus_avail = nr;
  1184. ret = readn(fd, &nr, sizeof(nr));
  1185. if (ret != sizeof(nr))
  1186. return -1;
  1187. if (ph->needs_swap)
  1188. nr = bswap_32(nr);
  1189. ph->env.nr_cpus_online = nr;
  1190. return 0;
  1191. }
  1192. static int process_cpudesc(struct perf_file_section *section __maybe_unused,
  1193. struct perf_header *ph, int fd,
  1194. void *data __maybe_unused)
  1195. {
  1196. ph->env.cpu_desc = do_read_string(fd, ph);
  1197. return ph->env.cpu_desc ? 0 : -ENOMEM;
  1198. }
  1199. static int process_cpuid(struct perf_file_section *section __maybe_unused,
  1200. struct perf_header *ph, int fd,
  1201. void *data __maybe_unused)
  1202. {
  1203. ph->env.cpuid = do_read_string(fd, ph);
  1204. return ph->env.cpuid ? 0 : -ENOMEM;
  1205. }
  1206. static int process_total_mem(struct perf_file_section *section __maybe_unused,
  1207. struct perf_header *ph, int fd,
  1208. void *data __maybe_unused)
  1209. {
  1210. uint64_t mem;
  1211. ssize_t ret;
  1212. ret = readn(fd, &mem, sizeof(mem));
  1213. if (ret != sizeof(mem))
  1214. return -1;
  1215. if (ph->needs_swap)
  1216. mem = bswap_64(mem);
  1217. ph->env.total_mem = mem;
  1218. return 0;
  1219. }
  1220. static struct perf_evsel *
  1221. perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
  1222. {
  1223. struct perf_evsel *evsel;
  1224. evlist__for_each(evlist, evsel) {
  1225. if (evsel->idx == idx)
  1226. return evsel;
  1227. }
  1228. return NULL;
  1229. }
  1230. static void
  1231. perf_evlist__set_event_name(struct perf_evlist *evlist,
  1232. struct perf_evsel *event)
  1233. {
  1234. struct perf_evsel *evsel;
  1235. if (!event->name)
  1236. return;
  1237. evsel = perf_evlist__find_by_index(evlist, event->idx);
  1238. if (!evsel)
  1239. return;
  1240. if (evsel->name)
  1241. return;
  1242. evsel->name = strdup(event->name);
  1243. }
  1244. static int
  1245. process_event_desc(struct perf_file_section *section __maybe_unused,
  1246. struct perf_header *header, int fd,
  1247. void *data __maybe_unused)
  1248. {
  1249. struct perf_session *session;
  1250. struct perf_evsel *evsel, *events = read_event_desc(header, fd);
  1251. if (!events)
  1252. return 0;
  1253. session = container_of(header, struct perf_session, header);
  1254. for (evsel = events; evsel->attr.size; evsel++)
  1255. perf_evlist__set_event_name(session->evlist, evsel);
  1256. free_event_desc(events);
  1257. return 0;
  1258. }
  1259. static int process_cmdline(struct perf_file_section *section,
  1260. struct perf_header *ph, int fd,
  1261. void *data __maybe_unused)
  1262. {
  1263. ssize_t ret;
  1264. char *str, *cmdline = NULL, **argv = NULL;
  1265. u32 nr, i, len = 0;
  1266. ret = readn(fd, &nr, sizeof(nr));
  1267. if (ret != sizeof(nr))
  1268. return -1;
  1269. if (ph->needs_swap)
  1270. nr = bswap_32(nr);
  1271. ph->env.nr_cmdline = nr;
  1272. cmdline = zalloc(section->size + nr + 1);
  1273. if (!cmdline)
  1274. return -1;
  1275. argv = zalloc(sizeof(char *) * (nr + 1));
  1276. if (!argv)
  1277. goto error;
  1278. for (i = 0; i < nr; i++) {
  1279. str = do_read_string(fd, ph);
  1280. if (!str)
  1281. goto error;
  1282. argv[i] = cmdline + len;
  1283. memcpy(argv[i], str, strlen(str) + 1);
  1284. len += strlen(str) + 1;
  1285. free(str);
  1286. }
  1287. ph->env.cmdline = cmdline;
  1288. ph->env.cmdline_argv = (const char **) argv;
  1289. return 0;
  1290. error:
  1291. free(argv);
  1292. free(cmdline);
  1293. return -1;
  1294. }
  1295. static int process_cpu_topology(struct perf_file_section *section,
  1296. struct perf_header *ph, int fd,
  1297. void *data __maybe_unused)
  1298. {
  1299. ssize_t ret;
  1300. u32 nr, i;
  1301. char *str;
  1302. struct strbuf sb;
  1303. int cpu_nr = ph->env.nr_cpus_online;
  1304. u64 size = 0;
  1305. ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
  1306. if (!ph->env.cpu)
  1307. return -1;
  1308. ret = readn(fd, &nr, sizeof(nr));
  1309. if (ret != sizeof(nr))
  1310. goto free_cpu;
  1311. if (ph->needs_swap)
  1312. nr = bswap_32(nr);
  1313. ph->env.nr_sibling_cores = nr;
  1314. size += sizeof(u32);
  1315. strbuf_init(&sb, 128);
  1316. for (i = 0; i < nr; i++) {
  1317. str = do_read_string(fd, ph);
  1318. if (!str)
  1319. goto error;
  1320. /* include a NULL character at the end */
  1321. strbuf_add(&sb, str, strlen(str) + 1);
  1322. size += string_size(str);
  1323. free(str);
  1324. }
  1325. ph->env.sibling_cores = strbuf_detach(&sb, NULL);
  1326. ret = readn(fd, &nr, sizeof(nr));
  1327. if (ret != sizeof(nr))
  1328. return -1;
  1329. if (ph->needs_swap)
  1330. nr = bswap_32(nr);
  1331. ph->env.nr_sibling_threads = nr;
  1332. size += sizeof(u32);
  1333. for (i = 0; i < nr; i++) {
  1334. str = do_read_string(fd, ph);
  1335. if (!str)
  1336. goto error;
  1337. /* include a NULL character at the end */
  1338. strbuf_add(&sb, str, strlen(str) + 1);
  1339. size += string_size(str);
  1340. free(str);
  1341. }
  1342. ph->env.sibling_threads = strbuf_detach(&sb, NULL);
  1343. /*
  1344. * The header may be from old perf,
  1345. * which doesn't include core id and socket id information.
  1346. */
  1347. if (section->size <= size) {
  1348. zfree(&ph->env.cpu);
  1349. return 0;
  1350. }
  1351. for (i = 0; i < (u32)cpu_nr; i++) {
  1352. ret = readn(fd, &nr, sizeof(nr));
  1353. if (ret != sizeof(nr))
  1354. goto free_cpu;
  1355. if (ph->needs_swap)
  1356. nr = bswap_32(nr);
  1357. if (nr > (u32)cpu_nr) {
  1358. pr_debug("core_id number is too big."
  1359. "You may need to upgrade the perf tool.\n");
  1360. goto free_cpu;
  1361. }
  1362. ph->env.cpu[i].core_id = nr;
  1363. ret = readn(fd, &nr, sizeof(nr));
  1364. if (ret != sizeof(nr))
  1365. goto free_cpu;
  1366. if (ph->needs_swap)
  1367. nr = bswap_32(nr);
  1368. if (nr > (u32)cpu_nr) {
  1369. pr_debug("socket_id number is too big."
  1370. "You may need to upgrade the perf tool.\n");
  1371. goto free_cpu;
  1372. }
  1373. ph->env.cpu[i].socket_id = nr;
  1374. }
  1375. return 0;
  1376. error:
  1377. strbuf_release(&sb);
  1378. free_cpu:
  1379. zfree(&ph->env.cpu);
  1380. return -1;
  1381. }
  1382. static int process_numa_topology(struct perf_file_section *section __maybe_unused,
  1383. struct perf_header *ph, int fd,
  1384. void *data __maybe_unused)
  1385. {
  1386. ssize_t ret;
  1387. u32 nr, node, i;
  1388. char *str;
  1389. uint64_t mem_total, mem_free;
  1390. struct strbuf sb;
  1391. /* nr nodes */
  1392. ret = readn(fd, &nr, sizeof(nr));
  1393. if (ret != sizeof(nr))
  1394. goto error;
  1395. if (ph->needs_swap)
  1396. nr = bswap_32(nr);
  1397. ph->env.nr_numa_nodes = nr;
  1398. strbuf_init(&sb, 256);
  1399. for (i = 0; i < nr; i++) {
  1400. /* node number */
  1401. ret = readn(fd, &node, sizeof(node));
  1402. if (ret != sizeof(node))
  1403. goto error;
  1404. ret = readn(fd, &mem_total, sizeof(u64));
  1405. if (ret != sizeof(u64))
  1406. goto error;
  1407. ret = readn(fd, &mem_free, sizeof(u64));
  1408. if (ret != sizeof(u64))
  1409. goto error;
  1410. if (ph->needs_swap) {
  1411. node = bswap_32(node);
  1412. mem_total = bswap_64(mem_total);
  1413. mem_free = bswap_64(mem_free);
  1414. }
  1415. strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
  1416. node, mem_total, mem_free);
  1417. str = do_read_string(fd, ph);
  1418. if (!str)
  1419. goto error;
  1420. /* include a NULL character at the end */
  1421. strbuf_add(&sb, str, strlen(str) + 1);
  1422. free(str);
  1423. }
  1424. ph->env.numa_nodes = strbuf_detach(&sb, NULL);
  1425. return 0;
  1426. error:
  1427. strbuf_release(&sb);
  1428. return -1;
  1429. }
  1430. static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
  1431. struct perf_header *ph, int fd,
  1432. void *data __maybe_unused)
  1433. {
  1434. ssize_t ret;
  1435. char *name;
  1436. u32 pmu_num;
  1437. u32 type;
  1438. struct strbuf sb;
  1439. ret = readn(fd, &pmu_num, sizeof(pmu_num));
  1440. if (ret != sizeof(pmu_num))
  1441. return -1;
  1442. if (ph->needs_swap)
  1443. pmu_num = bswap_32(pmu_num);
  1444. if (!pmu_num) {
  1445. pr_debug("pmu mappings not available\n");
  1446. return 0;
  1447. }
  1448. ph->env.nr_pmu_mappings = pmu_num;
  1449. strbuf_init(&sb, 128);
  1450. while (pmu_num) {
  1451. if (readn(fd, &type, sizeof(type)) != sizeof(type))
  1452. goto error;
  1453. if (ph->needs_swap)
  1454. type = bswap_32(type);
  1455. name = do_read_string(fd, ph);
  1456. if (!name)
  1457. goto error;
  1458. strbuf_addf(&sb, "%u:%s", type, name);
  1459. /* include a NULL character at the end */
  1460. strbuf_add(&sb, "", 1);
  1461. if (!strcmp(name, "msr"))
  1462. ph->env.msr_pmu_type = type;
  1463. free(name);
  1464. pmu_num--;
  1465. }
  1466. ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
  1467. return 0;
  1468. error:
  1469. strbuf_release(&sb);
  1470. return -1;
  1471. }
  1472. static int process_group_desc(struct perf_file_section *section __maybe_unused,
  1473. struct perf_header *ph, int fd,
  1474. void *data __maybe_unused)
  1475. {
  1476. size_t ret = -1;
  1477. u32 i, nr, nr_groups;
  1478. struct perf_session *session;
  1479. struct perf_evsel *evsel, *leader = NULL;
  1480. struct group_desc {
  1481. char *name;
  1482. u32 leader_idx;
  1483. u32 nr_members;
  1484. } *desc;
  1485. if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
  1486. return -1;
  1487. if (ph->needs_swap)
  1488. nr_groups = bswap_32(nr_groups);
  1489. ph->env.nr_groups = nr_groups;
  1490. if (!nr_groups) {
  1491. pr_debug("group desc not available\n");
  1492. return 0;
  1493. }
  1494. desc = calloc(nr_groups, sizeof(*desc));
  1495. if (!desc)
  1496. return -1;
  1497. for (i = 0; i < nr_groups; i++) {
  1498. desc[i].name = do_read_string(fd, ph);
  1499. if (!desc[i].name)
  1500. goto out_free;
  1501. if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
  1502. goto out_free;
  1503. if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
  1504. goto out_free;
  1505. if (ph->needs_swap) {
  1506. desc[i].leader_idx = bswap_32(desc[i].leader_idx);
  1507. desc[i].nr_members = bswap_32(desc[i].nr_members);
  1508. }
  1509. }
  1510. /*
  1511. * Rebuild group relationship based on the group_desc
  1512. */
  1513. session = container_of(ph, struct perf_session, header);
  1514. session->evlist->nr_groups = nr_groups;
  1515. i = nr = 0;
  1516. evlist__for_each(session->evlist, evsel) {
  1517. if (evsel->idx == (int) desc[i].leader_idx) {
  1518. evsel->leader = evsel;
  1519. /* {anon_group} is a dummy name */
  1520. if (strcmp(desc[i].name, "{anon_group}")) {
  1521. evsel->group_name = desc[i].name;
  1522. desc[i].name = NULL;
  1523. }
  1524. evsel->nr_members = desc[i].nr_members;
  1525. if (i >= nr_groups || nr > 0) {
  1526. pr_debug("invalid group desc\n");
  1527. goto out_free;
  1528. }
  1529. leader = evsel;
  1530. nr = evsel->nr_members - 1;
  1531. i++;
  1532. } else if (nr) {
  1533. /* This is a group member */
  1534. evsel->leader = leader;
  1535. nr--;
  1536. }
  1537. }
  1538. if (i != nr_groups || nr != 0) {
  1539. pr_debug("invalid group desc\n");
  1540. goto out_free;
  1541. }
  1542. ret = 0;
  1543. out_free:
  1544. for (i = 0; i < nr_groups; i++)
  1545. zfree(&desc[i].name);
  1546. free(desc);
  1547. return ret;
  1548. }
  1549. static int process_auxtrace(struct perf_file_section *section,
  1550. struct perf_header *ph, int fd,
  1551. void *data __maybe_unused)
  1552. {
  1553. struct perf_session *session;
  1554. int err;
  1555. session = container_of(ph, struct perf_session, header);
  1556. err = auxtrace_index__process(fd, section->size, session,
  1557. ph->needs_swap);
  1558. if (err < 0)
  1559. pr_err("Failed to process auxtrace index\n");
  1560. return err;
  1561. }
  1562. struct feature_ops {
  1563. int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
  1564. void (*print)(struct perf_header *h, int fd, FILE *fp);
  1565. int (*process)(struct perf_file_section *section,
  1566. struct perf_header *h, int fd, void *data);
  1567. const char *name;
  1568. bool full_only;
  1569. };
  1570. #define FEAT_OPA(n, func) \
  1571. [n] = { .name = #n, .write = write_##func, .print = print_##func }
  1572. #define FEAT_OPP(n, func) \
  1573. [n] = { .name = #n, .write = write_##func, .print = print_##func, \
  1574. .process = process_##func }
  1575. #define FEAT_OPF(n, func) \
  1576. [n] = { .name = #n, .write = write_##func, .print = print_##func, \
  1577. .process = process_##func, .full_only = true }
  1578. /* feature_ops not implemented: */
  1579. #define print_tracing_data NULL
  1580. #define print_build_id NULL
  1581. static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
  1582. FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
  1583. FEAT_OPP(HEADER_BUILD_ID, build_id),
  1584. FEAT_OPP(HEADER_HOSTNAME, hostname),
  1585. FEAT_OPP(HEADER_OSRELEASE, osrelease),
  1586. FEAT_OPP(HEADER_VERSION, version),
  1587. FEAT_OPP(HEADER_ARCH, arch),
  1588. FEAT_OPP(HEADER_NRCPUS, nrcpus),
  1589. FEAT_OPP(HEADER_CPUDESC, cpudesc),
  1590. FEAT_OPP(HEADER_CPUID, cpuid),
  1591. FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
  1592. FEAT_OPP(HEADER_EVENT_DESC, event_desc),
  1593. FEAT_OPP(HEADER_CMDLINE, cmdline),
  1594. FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
  1595. FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
  1596. FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
  1597. FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
  1598. FEAT_OPP(HEADER_GROUP_DESC, group_desc),
  1599. FEAT_OPP(HEADER_AUXTRACE, auxtrace),
  1600. };
  1601. struct header_print_data {
  1602. FILE *fp;
  1603. bool full; /* extended list of headers */
  1604. };
  1605. static int perf_file_section__fprintf_info(struct perf_file_section *section,
  1606. struct perf_header *ph,
  1607. int feat, int fd, void *data)
  1608. {
  1609. struct header_print_data *hd = data;
  1610. if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
  1611. pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
  1612. "%d, continuing...\n", section->offset, feat);
  1613. return 0;
  1614. }
  1615. if (feat >= HEADER_LAST_FEATURE) {
  1616. pr_warning("unknown feature %d\n", feat);
  1617. return 0;
  1618. }
  1619. if (!feat_ops[feat].print)
  1620. return 0;
  1621. if (!feat_ops[feat].full_only || hd->full)
  1622. feat_ops[feat].print(ph, fd, hd->fp);
  1623. else
  1624. fprintf(hd->fp, "# %s info available, use -I to display\n",
  1625. feat_ops[feat].name);
  1626. return 0;
  1627. }
  1628. int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
  1629. {
  1630. struct header_print_data hd;
  1631. struct perf_header *header = &session->header;
  1632. int fd = perf_data_file__fd(session->file);
  1633. hd.fp = fp;
  1634. hd.full = full;
  1635. perf_header__process_sections(header, fd, &hd,
  1636. perf_file_section__fprintf_info);
  1637. return 0;
  1638. }
  1639. static int do_write_feat(int fd, struct perf_header *h, int type,
  1640. struct perf_file_section **p,
  1641. struct perf_evlist *evlist)
  1642. {
  1643. int err;
  1644. int ret = 0;
  1645. if (perf_header__has_feat(h, type)) {
  1646. if (!feat_ops[type].write)
  1647. return -1;
  1648. (*p)->offset = lseek(fd, 0, SEEK_CUR);
  1649. err = feat_ops[type].write(fd, h, evlist);
  1650. if (err < 0) {
  1651. pr_debug("failed to write feature %d\n", type);
  1652. /* undo anything written */
  1653. lseek(fd, (*p)->offset, SEEK_SET);
  1654. return -1;
  1655. }
  1656. (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
  1657. (*p)++;
  1658. }
  1659. return ret;
  1660. }
  1661. static int perf_header__adds_write(struct perf_header *header,
  1662. struct perf_evlist *evlist, int fd)
  1663. {
  1664. int nr_sections;
  1665. struct perf_file_section *feat_sec, *p;
  1666. int sec_size;
  1667. u64 sec_start;
  1668. int feat;
  1669. int err;
  1670. nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
  1671. if (!nr_sections)
  1672. return 0;
  1673. feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
  1674. if (feat_sec == NULL)
  1675. return -ENOMEM;
  1676. sec_size = sizeof(*feat_sec) * nr_sections;
  1677. sec_start = header->feat_offset;
  1678. lseek(fd, sec_start + sec_size, SEEK_SET);
  1679. for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
  1680. if (do_write_feat(fd, header, feat, &p, evlist))
  1681. perf_header__clear_feat(header, feat);
  1682. }
  1683. lseek(fd, sec_start, SEEK_SET);
  1684. /*
  1685. * may write more than needed due to dropped feature, but
  1686. * this is okay, reader will skip the mising entries
  1687. */
  1688. err = do_write(fd, feat_sec, sec_size);
  1689. if (err < 0)
  1690. pr_debug("failed to write feature section\n");
  1691. free(feat_sec);
  1692. return err;
  1693. }
  1694. int perf_header__write_pipe(int fd)
  1695. {
  1696. struct perf_pipe_file_header f_header;
  1697. int err;
  1698. f_header = (struct perf_pipe_file_header){
  1699. .magic = PERF_MAGIC,
  1700. .size = sizeof(f_header),
  1701. };
  1702. err = do_write(fd, &f_header, sizeof(f_header));
  1703. if (err < 0) {
  1704. pr_debug("failed to write perf pipe header\n");
  1705. return err;
  1706. }
  1707. return 0;
  1708. }
  1709. int perf_session__write_header(struct perf_session *session,
  1710. struct perf_evlist *evlist,
  1711. int fd, bool at_exit)
  1712. {
  1713. struct perf_file_header f_header;
  1714. struct perf_file_attr f_attr;
  1715. struct perf_header *header = &session->header;
  1716. struct perf_evsel *evsel;
  1717. u64 attr_offset;
  1718. int err;
  1719. lseek(fd, sizeof(f_header), SEEK_SET);
  1720. evlist__for_each(session->evlist, evsel) {
  1721. evsel->id_offset = lseek(fd, 0, SEEK_CUR);
  1722. err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
  1723. if (err < 0) {
  1724. pr_debug("failed to write perf header\n");
  1725. return err;
  1726. }
  1727. }
  1728. attr_offset = lseek(fd, 0, SEEK_CUR);
  1729. evlist__for_each(evlist, evsel) {
  1730. f_attr = (struct perf_file_attr){
  1731. .attr = evsel->attr,
  1732. .ids = {
  1733. .offset = evsel->id_offset,
  1734. .size = evsel->ids * sizeof(u64),
  1735. }
  1736. };
  1737. err = do_write(fd, &f_attr, sizeof(f_attr));
  1738. if (err < 0) {
  1739. pr_debug("failed to write perf header attribute\n");
  1740. return err;
  1741. }
  1742. }
  1743. if (!header->data_offset)
  1744. header->data_offset = lseek(fd, 0, SEEK_CUR);
  1745. header->feat_offset = header->data_offset + header->data_size;
  1746. if (at_exit) {
  1747. err = perf_header__adds_write(header, evlist, fd);
  1748. if (err < 0)
  1749. return err;
  1750. }
  1751. f_header = (struct perf_file_header){
  1752. .magic = PERF_MAGIC,
  1753. .size = sizeof(f_header),
  1754. .attr_size = sizeof(f_attr),
  1755. .attrs = {
  1756. .offset = attr_offset,
  1757. .size = evlist->nr_entries * sizeof(f_attr),
  1758. },
  1759. .data = {
  1760. .offset = header->data_offset,
  1761. .size = header->data_size,
  1762. },
  1763. /* event_types is ignored, store zeros */
  1764. };
  1765. memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
  1766. lseek(fd, 0, SEEK_SET);
  1767. err = do_write(fd, &f_header, sizeof(f_header));
  1768. if (err < 0) {
  1769. pr_debug("failed to write perf header\n");
  1770. return err;
  1771. }
  1772. lseek(fd, header->data_offset + header->data_size, SEEK_SET);
  1773. return 0;
  1774. }
  1775. static int perf_header__getbuffer64(struct perf_header *header,
  1776. int fd, void *buf, size_t size)
  1777. {
  1778. if (readn(fd, buf, size) <= 0)
  1779. return -1;
  1780. if (header->needs_swap)
  1781. mem_bswap_64(buf, size);
  1782. return 0;
  1783. }
  1784. int perf_header__process_sections(struct perf_header *header, int fd,
  1785. void *data,
  1786. int (*process)(struct perf_file_section *section,
  1787. struct perf_header *ph,
  1788. int feat, int fd, void *data))
  1789. {
  1790. struct perf_file_section *feat_sec, *sec;
  1791. int nr_sections;
  1792. int sec_size;
  1793. int feat;
  1794. int err;
  1795. nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
  1796. if (!nr_sections)
  1797. return 0;
  1798. feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
  1799. if (!feat_sec)
  1800. return -1;
  1801. sec_size = sizeof(*feat_sec) * nr_sections;
  1802. lseek(fd, header->feat_offset, SEEK_SET);
  1803. err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
  1804. if (err < 0)
  1805. goto out_free;
  1806. for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
  1807. err = process(sec++, header, feat, fd, data);
  1808. if (err < 0)
  1809. goto out_free;
  1810. }
  1811. err = 0;
  1812. out_free:
  1813. free(feat_sec);
  1814. return err;
  1815. }
  1816. static const int attr_file_abi_sizes[] = {
  1817. [0] = PERF_ATTR_SIZE_VER0,
  1818. [1] = PERF_ATTR_SIZE_VER1,
  1819. [2] = PERF_ATTR_SIZE_VER2,
  1820. [3] = PERF_ATTR_SIZE_VER3,
  1821. [4] = PERF_ATTR_SIZE_VER4,
  1822. 0,
  1823. };
  1824. /*
  1825. * In the legacy file format, the magic number is not used to encode endianness.
  1826. * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
  1827. * on ABI revisions, we need to try all combinations for all endianness to
  1828. * detect the endianness.
  1829. */
  1830. static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
  1831. {
  1832. uint64_t ref_size, attr_size;
  1833. int i;
  1834. for (i = 0 ; attr_file_abi_sizes[i]; i++) {
  1835. ref_size = attr_file_abi_sizes[i]
  1836. + sizeof(struct perf_file_section);
  1837. if (hdr_sz != ref_size) {
  1838. attr_size = bswap_64(hdr_sz);
  1839. if (attr_size != ref_size)
  1840. continue;
  1841. ph->needs_swap = true;
  1842. }
  1843. pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
  1844. i,
  1845. ph->needs_swap);
  1846. return 0;
  1847. }
  1848. /* could not determine endianness */
  1849. return -1;
  1850. }
  1851. #define PERF_PIPE_HDR_VER0 16
  1852. static const size_t attr_pipe_abi_sizes[] = {
  1853. [0] = PERF_PIPE_HDR_VER0,
  1854. 0,
  1855. };
  1856. /*
  1857. * In the legacy pipe format, there is an implicit assumption that endiannesss
  1858. * between host recording the samples, and host parsing the samples is the
  1859. * same. This is not always the case given that the pipe output may always be
  1860. * redirected into a file and analyzed on a different machine with possibly a
  1861. * different endianness and perf_event ABI revsions in the perf tool itself.
  1862. */
  1863. static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
  1864. {
  1865. u64 attr_size;
  1866. int i;
  1867. for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
  1868. if (hdr_sz != attr_pipe_abi_sizes[i]) {
  1869. attr_size = bswap_64(hdr_sz);
  1870. if (attr_size != hdr_sz)
  1871. continue;
  1872. ph->needs_swap = true;
  1873. }
  1874. pr_debug("Pipe ABI%d perf.data file detected\n", i);
  1875. return 0;
  1876. }
  1877. return -1;
  1878. }
  1879. bool is_perf_magic(u64 magic)
  1880. {
  1881. if (!memcmp(&magic, __perf_magic1, sizeof(magic))
  1882. || magic == __perf_magic2
  1883. || magic == __perf_magic2_sw)
  1884. return true;
  1885. return false;
  1886. }
  1887. static int check_magic_endian(u64 magic, uint64_t hdr_sz,
  1888. bool is_pipe, struct perf_header *ph)
  1889. {
  1890. int ret;
  1891. /* check for legacy format */
  1892. ret = memcmp(&magic, __perf_magic1, sizeof(magic));
  1893. if (ret == 0) {
  1894. ph->version = PERF_HEADER_VERSION_1;
  1895. pr_debug("legacy perf.data format\n");
  1896. if (is_pipe)
  1897. return try_all_pipe_abis(hdr_sz, ph);
  1898. return try_all_file_abis(hdr_sz, ph);
  1899. }
  1900. /*
  1901. * the new magic number serves two purposes:
  1902. * - unique number to identify actual perf.data files
  1903. * - encode endianness of file
  1904. */
  1905. ph->version = PERF_HEADER_VERSION_2;
  1906. /* check magic number with one endianness */
  1907. if (magic == __perf_magic2)
  1908. return 0;
  1909. /* check magic number with opposite endianness */
  1910. if (magic != __perf_magic2_sw)
  1911. return -1;
  1912. ph->needs_swap = true;
  1913. return 0;
  1914. }
  1915. int perf_file_header__read(struct perf_file_header *header,
  1916. struct perf_header *ph, int fd)
  1917. {
  1918. ssize_t ret;
  1919. lseek(fd, 0, SEEK_SET);
  1920. ret = readn(fd, header, sizeof(*header));
  1921. if (ret <= 0)
  1922. return -1;
  1923. if (check_magic_endian(header->magic,
  1924. header->attr_size, false, ph) < 0) {
  1925. pr_debug("magic/endian check failed\n");
  1926. return -1;
  1927. }
  1928. if (ph->needs_swap) {
  1929. mem_bswap_64(header, offsetof(struct perf_file_header,
  1930. adds_features));
  1931. }
  1932. if (header->size != sizeof(*header)) {
  1933. /* Support the previous format */
  1934. if (header->size == offsetof(typeof(*header), adds_features))
  1935. bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
  1936. else
  1937. return -1;
  1938. } else if (ph->needs_swap) {
  1939. /*
  1940. * feature bitmap is declared as an array of unsigned longs --
  1941. * not good since its size can differ between the host that
  1942. * generated the data file and the host analyzing the file.
  1943. *
  1944. * We need to handle endianness, but we don't know the size of
  1945. * the unsigned long where the file was generated. Take a best
  1946. * guess at determining it: try 64-bit swap first (ie., file
  1947. * created on a 64-bit host), and check if the hostname feature
  1948. * bit is set (this feature bit is forced on as of fbe96f2).
  1949. * If the bit is not, undo the 64-bit swap and try a 32-bit
  1950. * swap. If the hostname bit is still not set (e.g., older data
  1951. * file), punt and fallback to the original behavior --
  1952. * clearing all feature bits and setting buildid.
  1953. */
  1954. mem_bswap_64(&header->adds_features,
  1955. BITS_TO_U64(HEADER_FEAT_BITS));
  1956. if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
  1957. /* unswap as u64 */
  1958. mem_bswap_64(&header->adds_features,
  1959. BITS_TO_U64(HEADER_FEAT_BITS));
  1960. /* unswap as u32 */
  1961. mem_bswap_32(&header->adds_features,
  1962. BITS_TO_U32(HEADER_FEAT_BITS));
  1963. }
  1964. if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
  1965. bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
  1966. set_bit(HEADER_BUILD_ID, header->adds_features);
  1967. }
  1968. }
  1969. memcpy(&ph->adds_features, &header->adds_features,
  1970. sizeof(ph->adds_features));
  1971. ph->data_offset = header->data.offset;
  1972. ph->data_size = header->data.size;
  1973. ph->feat_offset = header->data.offset + header->data.size;
  1974. return 0;
  1975. }
  1976. static int perf_file_section__process(struct perf_file_section *section,
  1977. struct perf_header *ph,
  1978. int feat, int fd, void *data)
  1979. {
  1980. if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
  1981. pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
  1982. "%d, continuing...\n", section->offset, feat);
  1983. return 0;
  1984. }
  1985. if (feat >= HEADER_LAST_FEATURE) {
  1986. pr_debug("unknown feature %d, continuing...\n", feat);
  1987. return 0;
  1988. }
  1989. if (!feat_ops[feat].process)
  1990. return 0;
  1991. return feat_ops[feat].process(section, ph, fd, data);
  1992. }
  1993. static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
  1994. struct perf_header *ph, int fd,
  1995. bool repipe)
  1996. {
  1997. ssize_t ret;
  1998. ret = readn(fd, header, sizeof(*header));
  1999. if (ret <= 0)
  2000. return -1;
  2001. if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
  2002. pr_debug("endian/magic failed\n");
  2003. return -1;
  2004. }
  2005. if (ph->needs_swap)
  2006. header->size = bswap_64(header->size);
  2007. if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
  2008. return -1;
  2009. return 0;
  2010. }
  2011. static int perf_header__read_pipe(struct perf_session *session)
  2012. {
  2013. struct perf_header *header = &session->header;
  2014. struct perf_pipe_file_header f_header;
  2015. if (perf_file_header__read_pipe(&f_header, header,
  2016. perf_data_file__fd(session->file),
  2017. session->repipe) < 0) {
  2018. pr_debug("incompatible file format\n");
  2019. return -EINVAL;
  2020. }
  2021. return 0;
  2022. }
  2023. static int read_attr(int fd, struct perf_header *ph,
  2024. struct perf_file_attr *f_attr)
  2025. {
  2026. struct perf_event_attr *attr = &f_attr->attr;
  2027. size_t sz, left;
  2028. size_t our_sz = sizeof(f_attr->attr);
  2029. ssize_t ret;
  2030. memset(f_attr, 0, sizeof(*f_attr));
  2031. /* read minimal guaranteed structure */
  2032. ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
  2033. if (ret <= 0) {
  2034. pr_debug("cannot read %d bytes of header attr\n",
  2035. PERF_ATTR_SIZE_VER0);
  2036. return -1;
  2037. }
  2038. /* on file perf_event_attr size */
  2039. sz = attr->size;
  2040. if (ph->needs_swap)
  2041. sz = bswap_32(sz);
  2042. if (sz == 0) {
  2043. /* assume ABI0 */
  2044. sz = PERF_ATTR_SIZE_VER0;
  2045. } else if (sz > our_sz) {
  2046. pr_debug("file uses a more recent and unsupported ABI"
  2047. " (%zu bytes extra)\n", sz - our_sz);
  2048. return -1;
  2049. }
  2050. /* what we have not yet read and that we know about */
  2051. left = sz - PERF_ATTR_SIZE_VER0;
  2052. if (left) {
  2053. void *ptr = attr;
  2054. ptr += PERF_ATTR_SIZE_VER0;
  2055. ret = readn(fd, ptr, left);
  2056. }
  2057. /* read perf_file_section, ids are read in caller */
  2058. ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
  2059. return ret <= 0 ? -1 : 0;
  2060. }
  2061. static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
  2062. struct pevent *pevent)
  2063. {
  2064. struct event_format *event;
  2065. char bf[128];
  2066. /* already prepared */
  2067. if (evsel->tp_format)
  2068. return 0;
  2069. if (pevent == NULL) {
  2070. pr_debug("broken or missing trace data\n");
  2071. return -1;
  2072. }
  2073. event = pevent_find_event(pevent, evsel->attr.config);
  2074. if (event == NULL)
  2075. return -1;
  2076. if (!evsel->name) {
  2077. snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
  2078. evsel->name = strdup(bf);
  2079. if (evsel->name == NULL)
  2080. return -1;
  2081. }
  2082. evsel->tp_format = event;
  2083. return 0;
  2084. }
  2085. static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
  2086. struct pevent *pevent)
  2087. {
  2088. struct perf_evsel *pos;
  2089. evlist__for_each(evlist, pos) {
  2090. if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
  2091. perf_evsel__prepare_tracepoint_event(pos, pevent))
  2092. return -1;
  2093. }
  2094. return 0;
  2095. }
  2096. int perf_session__read_header(struct perf_session *session)
  2097. {
  2098. struct perf_data_file *file = session->file;
  2099. struct perf_header *header = &session->header;
  2100. struct perf_file_header f_header;
  2101. struct perf_file_attr f_attr;
  2102. u64 f_id;
  2103. int nr_attrs, nr_ids, i, j;
  2104. int fd = perf_data_file__fd(file);
  2105. session->evlist = perf_evlist__new();
  2106. if (session->evlist == NULL)
  2107. return -ENOMEM;
  2108. session->evlist->env = &header->env;
  2109. session->machines.host.env = &header->env;
  2110. if (perf_data_file__is_pipe(file))
  2111. return perf_header__read_pipe(session);
  2112. if (perf_file_header__read(&f_header, header, fd) < 0)
  2113. return -EINVAL;
  2114. /*
  2115. * Sanity check that perf.data was written cleanly; data size is
  2116. * initialized to 0 and updated only if the on_exit function is run.
  2117. * If data size is still 0 then the file contains only partial
  2118. * information. Just warn user and process it as much as it can.
  2119. */
  2120. if (f_header.data.size == 0) {
  2121. pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
  2122. "Was the 'perf record' command properly terminated?\n",
  2123. file->path);
  2124. }
  2125. nr_attrs = f_header.attrs.size / f_header.attr_size;
  2126. lseek(fd, f_header.attrs.offset, SEEK_SET);
  2127. for (i = 0; i < nr_attrs; i++) {
  2128. struct perf_evsel *evsel;
  2129. off_t tmp;
  2130. if (read_attr(fd, header, &f_attr) < 0)
  2131. goto out_errno;
  2132. if (header->needs_swap) {
  2133. f_attr.ids.size = bswap_64(f_attr.ids.size);
  2134. f_attr.ids.offset = bswap_64(f_attr.ids.offset);
  2135. perf_event__attr_swap(&f_attr.attr);
  2136. }
  2137. tmp = lseek(fd, 0, SEEK_CUR);
  2138. evsel = perf_evsel__new(&f_attr.attr);
  2139. if (evsel == NULL)
  2140. goto out_delete_evlist;
  2141. evsel->needs_swap = header->needs_swap;
  2142. /*
  2143. * Do it before so that if perf_evsel__alloc_id fails, this
  2144. * entry gets purged too at perf_evlist__delete().
  2145. */
  2146. perf_evlist__add(session->evlist, evsel);
  2147. nr_ids = f_attr.ids.size / sizeof(u64);
  2148. /*
  2149. * We don't have the cpu and thread maps on the header, so
  2150. * for allocating the perf_sample_id table we fake 1 cpu and
  2151. * hattr->ids threads.
  2152. */
  2153. if (perf_evsel__alloc_id(evsel, 1, nr_ids))
  2154. goto out_delete_evlist;
  2155. lseek(fd, f_attr.ids.offset, SEEK_SET);
  2156. for (j = 0; j < nr_ids; j++) {
  2157. if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
  2158. goto out_errno;
  2159. perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
  2160. }
  2161. lseek(fd, tmp, SEEK_SET);
  2162. }
  2163. symbol_conf.nr_events = nr_attrs;
  2164. perf_header__process_sections(header, fd, &session->tevent,
  2165. perf_file_section__process);
  2166. if (perf_evlist__prepare_tracepoint_events(session->evlist,
  2167. session->tevent.pevent))
  2168. goto out_delete_evlist;
  2169. return 0;
  2170. out_errno:
  2171. return -errno;
  2172. out_delete_evlist:
  2173. perf_evlist__delete(session->evlist);
  2174. session->evlist = NULL;
  2175. return -ENOMEM;
  2176. }
  2177. int perf_event__synthesize_attr(struct perf_tool *tool,
  2178. struct perf_event_attr *attr, u32 ids, u64 *id,
  2179. perf_event__handler_t process)
  2180. {
  2181. union perf_event *ev;
  2182. size_t size;
  2183. int err;
  2184. size = sizeof(struct perf_event_attr);
  2185. size = PERF_ALIGN(size, sizeof(u64));
  2186. size += sizeof(struct perf_event_header);
  2187. size += ids * sizeof(u64);
  2188. ev = malloc(size);
  2189. if (ev == NULL)
  2190. return -ENOMEM;
  2191. ev->attr.attr = *attr;
  2192. memcpy(ev->attr.id, id, ids * sizeof(u64));
  2193. ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
  2194. ev->attr.header.size = (u16)size;
  2195. if (ev->attr.header.size == size)
  2196. err = process(tool, ev, NULL, NULL);
  2197. else
  2198. err = -E2BIG;
  2199. free(ev);
  2200. return err;
  2201. }
  2202. int perf_event__synthesize_attrs(struct perf_tool *tool,
  2203. struct perf_session *session,
  2204. perf_event__handler_t process)
  2205. {
  2206. struct perf_evsel *evsel;
  2207. int err = 0;
  2208. evlist__for_each(session->evlist, evsel) {
  2209. err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
  2210. evsel->id, process);
  2211. if (err) {
  2212. pr_debug("failed to create perf header attribute\n");
  2213. return err;
  2214. }
  2215. }
  2216. return err;
  2217. }
  2218. int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
  2219. union perf_event *event,
  2220. struct perf_evlist **pevlist)
  2221. {
  2222. u32 i, ids, n_ids;
  2223. struct perf_evsel *evsel;
  2224. struct perf_evlist *evlist = *pevlist;
  2225. if (evlist == NULL) {
  2226. *pevlist = evlist = perf_evlist__new();
  2227. if (evlist == NULL)
  2228. return -ENOMEM;
  2229. }
  2230. evsel = perf_evsel__new(&event->attr.attr);
  2231. if (evsel == NULL)
  2232. return -ENOMEM;
  2233. perf_evlist__add(evlist, evsel);
  2234. ids = event->header.size;
  2235. ids -= (void *)&event->attr.id - (void *)event;
  2236. n_ids = ids / sizeof(u64);
  2237. /*
  2238. * We don't have the cpu and thread maps on the header, so
  2239. * for allocating the perf_sample_id table we fake 1 cpu and
  2240. * hattr->ids threads.
  2241. */
  2242. if (perf_evsel__alloc_id(evsel, 1, n_ids))
  2243. return -ENOMEM;
  2244. for (i = 0; i < n_ids; i++) {
  2245. perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
  2246. }
  2247. symbol_conf.nr_events = evlist->nr_entries;
  2248. return 0;
  2249. }
  2250. int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
  2251. struct perf_evlist *evlist,
  2252. perf_event__handler_t process)
  2253. {
  2254. union perf_event ev;
  2255. struct tracing_data *tdata;
  2256. ssize_t size = 0, aligned_size = 0, padding;
  2257. int err __maybe_unused = 0;
  2258. /*
  2259. * We are going to store the size of the data followed
  2260. * by the data contents. Since the fd descriptor is a pipe,
  2261. * we cannot seek back to store the size of the data once
  2262. * we know it. Instead we:
  2263. *
  2264. * - write the tracing data to the temp file
  2265. * - get/write the data size to pipe
  2266. * - write the tracing data from the temp file
  2267. * to the pipe
  2268. */
  2269. tdata = tracing_data_get(&evlist->entries, fd, true);
  2270. if (!tdata)
  2271. return -1;
  2272. memset(&ev, 0, sizeof(ev));
  2273. ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
  2274. size = tdata->size;
  2275. aligned_size = PERF_ALIGN(size, sizeof(u64));
  2276. padding = aligned_size - size;
  2277. ev.tracing_data.header.size = sizeof(ev.tracing_data);
  2278. ev.tracing_data.size = aligned_size;
  2279. process(tool, &ev, NULL, NULL);
  2280. /*
  2281. * The put function will copy all the tracing data
  2282. * stored in temp file to the pipe.
  2283. */
  2284. tracing_data_put(tdata);
  2285. write_padded(fd, NULL, 0, padding);
  2286. return aligned_size;
  2287. }
  2288. int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
  2289. union perf_event *event,
  2290. struct perf_session *session)
  2291. {
  2292. ssize_t size_read, padding, size = event->tracing_data.size;
  2293. int fd = perf_data_file__fd(session->file);
  2294. off_t offset = lseek(fd, 0, SEEK_CUR);
  2295. char buf[BUFSIZ];
  2296. /* setup for reading amidst mmap */
  2297. lseek(fd, offset + sizeof(struct tracing_data_event),
  2298. SEEK_SET);
  2299. size_read = trace_report(fd, &session->tevent,
  2300. session->repipe);
  2301. padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
  2302. if (readn(fd, buf, padding) < 0) {
  2303. pr_err("%s: reading input file", __func__);
  2304. return -1;
  2305. }
  2306. if (session->repipe) {
  2307. int retw = write(STDOUT_FILENO, buf, padding);
  2308. if (retw <= 0 || retw != padding) {
  2309. pr_err("%s: repiping tracing data padding", __func__);
  2310. return -1;
  2311. }
  2312. }
  2313. if (size_read + padding != size) {
  2314. pr_err("%s: tracing data size mismatch", __func__);
  2315. return -1;
  2316. }
  2317. perf_evlist__prepare_tracepoint_events(session->evlist,
  2318. session->tevent.pevent);
  2319. return size_read + padding;
  2320. }
  2321. int perf_event__synthesize_build_id(struct perf_tool *tool,
  2322. struct dso *pos, u16 misc,
  2323. perf_event__handler_t process,
  2324. struct machine *machine)
  2325. {
  2326. union perf_event ev;
  2327. size_t len;
  2328. int err = 0;
  2329. if (!pos->hit)
  2330. return err;
  2331. memset(&ev, 0, sizeof(ev));
  2332. len = pos->long_name_len + 1;
  2333. len = PERF_ALIGN(len, NAME_ALIGN);
  2334. memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
  2335. ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
  2336. ev.build_id.header.misc = misc;
  2337. ev.build_id.pid = machine->pid;
  2338. ev.build_id.header.size = sizeof(ev.build_id) + len;
  2339. memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
  2340. err = process(tool, &ev, NULL, machine);
  2341. return err;
  2342. }
  2343. int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
  2344. union perf_event *event,
  2345. struct perf_session *session)
  2346. {
  2347. __event_process_build_id(&event->build_id,
  2348. event->build_id.filename,
  2349. session);
  2350. return 0;
  2351. }