header.c 53 KB

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