header.c 51 KB

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