builtin-inject.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-inject.c
  4. *
  5. * Builtin inject command: Examine the live mode (stdin) event stream
  6. * and repipe it to stdout while optionally injecting additional
  7. * events into it.
  8. */
  9. #include "builtin.h"
  10. #include "util/color.h"
  11. #include "util/dso.h"
  12. #include "util/vdso.h"
  13. #include "util/evlist.h"
  14. #include "util/evsel.h"
  15. #include "util/map.h"
  16. #include "util/session.h"
  17. #include "util/tool.h"
  18. #include "util/debug.h"
  19. #include "util/build-id.h"
  20. #include "util/data.h"
  21. #include "util/auxtrace.h"
  22. #include "util/jit.h"
  23. #include "util/string2.h"
  24. #include "util/symbol.h"
  25. #include "util/synthetic-events.h"
  26. #include "util/thread.h"
  27. #include "util/namespaces.h"
  28. #include "util/util.h"
  29. #include "util/tsc.h"
  30. #include <internal/lib.h>
  31. #include <linux/err.h>
  32. #include <subcmd/parse-options.h>
  33. #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
  34. #include <linux/list.h>
  35. #include <linux/string.h>
  36. #include <linux/zalloc.h>
  37. #include <linux/hash.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. #include <signal.h>
  41. #include <inttypes.h>
  42. struct guest_event {
  43. struct perf_sample sample;
  44. union perf_event *event;
  45. char *event_buf;
  46. };
  47. struct guest_id {
  48. /* hlist_node must be first, see free_hlist() */
  49. struct hlist_node node;
  50. u64 id;
  51. u64 host_id;
  52. u32 vcpu;
  53. };
  54. struct guest_tid {
  55. /* hlist_node must be first, see free_hlist() */
  56. struct hlist_node node;
  57. /* Thread ID of QEMU thread */
  58. u32 tid;
  59. u32 vcpu;
  60. };
  61. struct guest_vcpu {
  62. /* Current host CPU */
  63. u32 cpu;
  64. /* Thread ID of QEMU thread */
  65. u32 tid;
  66. };
  67. struct guest_session {
  68. char *perf_data_file;
  69. u32 machine_pid;
  70. u64 time_offset;
  71. double time_scale;
  72. struct perf_tool tool;
  73. struct perf_data data;
  74. struct perf_session *session;
  75. char *tmp_file_name;
  76. int tmp_fd;
  77. struct perf_tsc_conversion host_tc;
  78. struct perf_tsc_conversion guest_tc;
  79. bool copy_kcore_dir;
  80. bool have_tc;
  81. bool fetched;
  82. bool ready;
  83. u16 dflt_id_hdr_size;
  84. u64 dflt_id;
  85. u64 highest_id;
  86. /* Array of guest_vcpu */
  87. struct guest_vcpu *vcpu;
  88. size_t vcpu_cnt;
  89. /* Hash table for guest_id */
  90. struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
  91. /* Hash table for guest_tid */
  92. struct hlist_head tids[PERF_EVLIST__HLIST_SIZE];
  93. /* Place to stash next guest event */
  94. struct guest_event ev;
  95. };
  96. enum build_id_rewrite_style {
  97. BID_RWS__NONE = 0,
  98. BID_RWS__INJECT_HEADER_LAZY,
  99. BID_RWS__INJECT_HEADER_ALL,
  100. BID_RWS__MMAP2_BUILDID_ALL,
  101. BID_RWS__MMAP2_BUILDID_LAZY,
  102. };
  103. struct perf_inject {
  104. struct perf_tool tool;
  105. struct perf_session *session;
  106. enum build_id_rewrite_style build_id_style;
  107. bool sched_stat;
  108. bool have_auxtrace;
  109. bool strip;
  110. bool jit_mode;
  111. bool in_place_update;
  112. bool in_place_update_dry_run;
  113. bool copy_kcore_dir;
  114. bool convert_callchain;
  115. const char *input_name;
  116. struct perf_data output;
  117. u64 bytes_written;
  118. u64 aux_id;
  119. struct list_head samples;
  120. struct itrace_synth_opts itrace_synth_opts;
  121. char *event_copy;
  122. struct perf_file_section secs[HEADER_FEAT_BITS];
  123. struct guest_session guest_session;
  124. struct strlist *known_build_ids;
  125. const struct evsel *mmap_evsel;
  126. struct ip_callchain *raw_callchain;
  127. };
  128. struct event_entry {
  129. struct list_head node;
  130. u32 tid;
  131. union perf_event event[];
  132. };
  133. static int tool__inject_build_id(const struct perf_tool *tool,
  134. struct perf_sample *sample,
  135. struct machine *machine,
  136. const struct evsel *evsel,
  137. __u16 misc,
  138. const char *filename,
  139. struct dso *dso, u32 flags);
  140. static int tool__inject_mmap2_build_id(const struct perf_tool *tool,
  141. struct perf_sample *sample,
  142. struct machine *machine,
  143. const struct evsel *evsel,
  144. __u16 misc,
  145. __u32 pid, __u32 tid,
  146. __u64 start, __u64 len, __u64 pgoff,
  147. struct dso *dso,
  148. __u32 prot, __u32 flags,
  149. const char *filename);
  150. static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
  151. {
  152. ssize_t size;
  153. size = perf_data__write(&inject->output, buf, sz);
  154. if (size < 0)
  155. return -errno;
  156. inject->bytes_written += size;
  157. return 0;
  158. }
  159. static int perf_event__repipe_synth(const struct perf_tool *tool,
  160. union perf_event *event)
  161. {
  162. struct perf_inject *inject = container_of(tool, struct perf_inject,
  163. tool);
  164. return output_bytes(inject, event, event->header.size);
  165. }
  166. static int perf_event__repipe_oe_synth(const struct perf_tool *tool,
  167. union perf_event *event,
  168. struct ordered_events *oe __maybe_unused)
  169. {
  170. return perf_event__repipe_synth(tool, event);
  171. }
  172. #ifdef HAVE_JITDUMP
  173. static int perf_event__drop_oe(const struct perf_tool *tool __maybe_unused,
  174. union perf_event *event __maybe_unused,
  175. struct ordered_events *oe __maybe_unused)
  176. {
  177. return 0;
  178. }
  179. #endif
  180. static int perf_event__repipe_op2_synth(const struct perf_tool *tool,
  181. struct perf_session *session __maybe_unused,
  182. union perf_event *event)
  183. {
  184. return perf_event__repipe_synth(tool, event);
  185. }
  186. static int perf_event__repipe_op4_synth(const struct perf_tool *tool,
  187. struct perf_session *session __maybe_unused,
  188. union perf_event *event,
  189. u64 data __maybe_unused,
  190. const char *str __maybe_unused)
  191. {
  192. return perf_event__repipe_synth(tool, event);
  193. }
  194. static int perf_event__repipe_attr(const struct perf_tool *tool,
  195. union perf_event *event,
  196. struct evlist **pevlist)
  197. {
  198. struct perf_inject *inject = container_of(tool, struct perf_inject,
  199. tool);
  200. int ret;
  201. ret = perf_event__process_attr(tool, event, pevlist);
  202. if (ret)
  203. return ret;
  204. /* If the output isn't a pipe then the attributes will be written as part of the header. */
  205. if (!inject->output.is_pipe)
  206. return 0;
  207. return perf_event__repipe_synth(tool, event);
  208. }
  209. static int perf_event__repipe_event_update(const struct perf_tool *tool,
  210. union perf_event *event,
  211. struct evlist **pevlist __maybe_unused)
  212. {
  213. return perf_event__repipe_synth(tool, event);
  214. }
  215. static int copy_bytes(struct perf_inject *inject, struct perf_data *data, off_t size)
  216. {
  217. char buf[4096];
  218. ssize_t ssz;
  219. int ret;
  220. while (size > 0) {
  221. ssz = perf_data__read(data, buf, min(size, (off_t)sizeof(buf)));
  222. if (ssz < 0)
  223. return -errno;
  224. ret = output_bytes(inject, buf, ssz);
  225. if (ret)
  226. return ret;
  227. size -= ssz;
  228. }
  229. return 0;
  230. }
  231. static s64 perf_event__repipe_auxtrace(const struct perf_tool *tool,
  232. struct perf_session *session,
  233. union perf_event *event)
  234. {
  235. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  236. int ret;
  237. inject->have_auxtrace = true;
  238. if (!inject->output.is_pipe) {
  239. off_t offset;
  240. offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
  241. if (offset == -1)
  242. return -errno;
  243. ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
  244. event, offset);
  245. if (ret < 0)
  246. return ret;
  247. }
  248. if (perf_data__is_pipe(session->data) || !session->one_mmap) {
  249. ret = output_bytes(inject, event, event->header.size);
  250. if (ret < 0)
  251. return ret;
  252. ret = copy_bytes(inject, session->data,
  253. event->auxtrace.size);
  254. } else {
  255. ret = output_bytes(inject, event,
  256. event->header.size + event->auxtrace.size);
  257. }
  258. if (ret < 0)
  259. return ret;
  260. return event->auxtrace.size;
  261. }
  262. static int perf_event__repipe(const struct perf_tool *tool,
  263. union perf_event *event,
  264. struct perf_sample *sample __maybe_unused,
  265. struct machine *machine __maybe_unused)
  266. {
  267. return perf_event__repipe_synth(tool, event);
  268. }
  269. static int perf_event__drop(const struct perf_tool *tool __maybe_unused,
  270. union perf_event *event __maybe_unused,
  271. struct perf_sample *sample __maybe_unused,
  272. struct machine *machine __maybe_unused)
  273. {
  274. return 0;
  275. }
  276. static int perf_event__drop_aux(const struct perf_tool *tool,
  277. union perf_event *event __maybe_unused,
  278. struct perf_sample *sample,
  279. struct machine *machine __maybe_unused)
  280. {
  281. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  282. if (!inject->aux_id)
  283. inject->aux_id = sample->id;
  284. return 0;
  285. }
  286. static union perf_event *
  287. perf_inject__cut_auxtrace_sample(struct perf_inject *inject,
  288. union perf_event *event,
  289. struct perf_sample *sample)
  290. {
  291. size_t sz1 = sample->aux_sample.data - (void *)event;
  292. size_t sz2 = event->header.size - sample->aux_sample.size - sz1;
  293. union perf_event *ev;
  294. if (inject->event_copy == NULL) {
  295. inject->event_copy = malloc(PERF_SAMPLE_MAX_SIZE);
  296. if (!inject->event_copy)
  297. return ERR_PTR(-ENOMEM);
  298. }
  299. ev = (union perf_event *)inject->event_copy;
  300. if (sz1 > event->header.size || sz2 > event->header.size ||
  301. sz1 + sz2 > event->header.size ||
  302. sz1 < sizeof(struct perf_event_header) + sizeof(u64))
  303. return event;
  304. memcpy(ev, event, sz1);
  305. memcpy((void *)ev + sz1, (void *)event + event->header.size - sz2, sz2);
  306. ev->header.size = sz1 + sz2;
  307. ((u64 *)((void *)ev + sz1))[-1] = 0;
  308. return ev;
  309. }
  310. typedef int (*inject_handler)(const struct perf_tool *tool,
  311. union perf_event *event,
  312. struct perf_sample *sample,
  313. struct evsel *evsel,
  314. struct machine *machine);
  315. static int perf_event__repipe_sample(const struct perf_tool *tool,
  316. union perf_event *event,
  317. struct perf_sample *sample,
  318. struct evsel *evsel,
  319. struct machine *machine)
  320. {
  321. struct perf_inject *inject = container_of(tool, struct perf_inject,
  322. tool);
  323. if (evsel && evsel->handler) {
  324. inject_handler f = evsel->handler;
  325. return f(tool, event, sample, evsel, machine);
  326. }
  327. build_id__mark_dso_hit(tool, event, sample, evsel, machine);
  328. if (inject->itrace_synth_opts.set && sample->aux_sample.size) {
  329. event = perf_inject__cut_auxtrace_sample(inject, event, sample);
  330. if (IS_ERR(event))
  331. return PTR_ERR(event);
  332. }
  333. return perf_event__repipe_synth(tool, event);
  334. }
  335. static int perf_event__convert_sample_callchain(const struct perf_tool *tool,
  336. union perf_event *event,
  337. struct perf_sample *sample,
  338. struct evsel *evsel,
  339. struct machine *machine)
  340. {
  341. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  342. struct callchain_cursor *cursor = get_tls_callchain_cursor();
  343. union perf_event *event_copy = (void *)inject->event_copy;
  344. struct callchain_cursor_node *node;
  345. struct thread *thread;
  346. u64 sample_type = evsel->core.attr.sample_type;
  347. u32 sample_size = event->header.size;
  348. u64 i, k;
  349. int ret;
  350. if (event_copy == NULL) {
  351. inject->event_copy = malloc(PERF_SAMPLE_MAX_SIZE);
  352. if (!inject->event_copy)
  353. return -ENOMEM;
  354. event_copy = (void *)inject->event_copy;
  355. }
  356. if (cursor == NULL)
  357. return -ENOMEM;
  358. callchain_cursor_reset(cursor);
  359. thread = machine__find_thread(machine, sample->tid, sample->pid);
  360. if (thread == NULL)
  361. goto out;
  362. /* this will parse DWARF using stack and register data */
  363. ret = thread__resolve_callchain(thread, cursor, evsel, sample,
  364. /*parent=*/NULL, /*root_al=*/NULL,
  365. PERF_MAX_STACK_DEPTH);
  366. thread__put(thread);
  367. if (ret != 0)
  368. goto out;
  369. /* copy kernel callchain and context entries */
  370. for (i = 0; i < sample->callchain->nr; i++) {
  371. inject->raw_callchain->ips[i] = sample->callchain->ips[i];
  372. if (sample->callchain->ips[i] == PERF_CONTEXT_USER) {
  373. i++;
  374. break;
  375. }
  376. }
  377. if (i == 0 || inject->raw_callchain->ips[i - 1] != PERF_CONTEXT_USER)
  378. inject->raw_callchain->ips[i++] = PERF_CONTEXT_USER;
  379. node = cursor->first;
  380. for (k = 0; k < cursor->nr && i < PERF_MAX_STACK_DEPTH; k++) {
  381. if (machine__kernel_ip(machine, node->ip))
  382. /* kernel IPs were added already */;
  383. else if (node->ms.sym && node->ms.sym->inlined)
  384. /* we can't handle inlined callchains */;
  385. else
  386. inject->raw_callchain->ips[i++] = node->ip;
  387. node = node->next;
  388. }
  389. inject->raw_callchain->nr = i;
  390. sample->callchain = inject->raw_callchain;
  391. out:
  392. memcpy(event_copy, event, sizeof(event->header));
  393. /* adjust sample size for stack and regs */
  394. sample_size -= sample->user_stack.size;
  395. sample_size -= (hweight64(evsel->core.attr.sample_regs_user) + 1) * sizeof(u64);
  396. sample_size += (sample->callchain->nr + 1) * sizeof(u64);
  397. event_copy->header.size = sample_size;
  398. /* remove sample_type {STACK,REGS}_USER for synthesize */
  399. sample_type &= ~(PERF_SAMPLE_STACK_USER | PERF_SAMPLE_REGS_USER);
  400. perf_event__synthesize_sample(event_copy, sample_type,
  401. evsel->core.attr.read_format, sample);
  402. return perf_event__repipe_synth(tool, event_copy);
  403. }
  404. static struct dso *findnew_dso(int pid, int tid, const char *filename,
  405. const struct dso_id *id, struct machine *machine)
  406. {
  407. struct thread *thread;
  408. struct nsinfo *nsi = NULL;
  409. struct nsinfo *nnsi;
  410. struct dso *dso;
  411. bool vdso;
  412. thread = machine__findnew_thread(machine, pid, tid);
  413. if (thread == NULL) {
  414. pr_err("cannot find or create a task %d/%d.\n", tid, pid);
  415. return NULL;
  416. }
  417. vdso = is_vdso_map(filename);
  418. nsi = nsinfo__get(thread__nsinfo(thread));
  419. if (vdso) {
  420. /* The vdso maps are always on the host and not the
  421. * container. Ensure that we don't use setns to look
  422. * them up.
  423. */
  424. nnsi = nsinfo__copy(nsi);
  425. if (nnsi) {
  426. nsinfo__put(nsi);
  427. nsinfo__clear_need_setns(nnsi);
  428. nsi = nnsi;
  429. }
  430. dso = machine__findnew_vdso(machine, thread);
  431. } else {
  432. dso = machine__findnew_dso_id(machine, filename, id);
  433. }
  434. if (dso) {
  435. mutex_lock(dso__lock(dso));
  436. dso__set_nsinfo(dso, nsi);
  437. mutex_unlock(dso__lock(dso));
  438. } else
  439. nsinfo__put(nsi);
  440. thread__put(thread);
  441. return dso;
  442. }
  443. /*
  444. * The evsel used for the sample ID for mmap events. Typically stashed when
  445. * processing mmap events. If not stashed, search the evlist for the first mmap
  446. * gathering event.
  447. */
  448. static const struct evsel *inject__mmap_evsel(struct perf_inject *inject)
  449. {
  450. struct evsel *pos;
  451. if (inject->mmap_evsel)
  452. return inject->mmap_evsel;
  453. evlist__for_each_entry(inject->session->evlist, pos) {
  454. if (pos->core.attr.mmap) {
  455. inject->mmap_evsel = pos;
  456. return pos;
  457. }
  458. }
  459. pr_err("No mmap events found\n");
  460. return NULL;
  461. }
  462. static int perf_event__repipe_common_mmap(const struct perf_tool *tool,
  463. union perf_event *event,
  464. struct perf_sample *sample,
  465. struct machine *machine,
  466. __u32 pid, __u32 tid,
  467. __u64 start, __u64 len, __u64 pgoff,
  468. __u32 flags, __u32 prot,
  469. const char *filename,
  470. const struct dso_id *dso_id,
  471. int (*perf_event_process)(const struct perf_tool *tool,
  472. union perf_event *event,
  473. struct perf_sample *sample,
  474. struct machine *machine))
  475. {
  476. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  477. struct dso *dso = NULL;
  478. bool dso_sought = false;
  479. #ifdef HAVE_JITDUMP
  480. if (inject->jit_mode) {
  481. u64 n = 0;
  482. int ret;
  483. /* If jit marker, then inject jit mmaps and generate ELF images. */
  484. ret = jit_process(inject->session, &inject->output, machine,
  485. filename, pid, tid, &n);
  486. if (ret < 0)
  487. return ret;
  488. if (ret) {
  489. inject->bytes_written += n;
  490. return 0;
  491. }
  492. }
  493. #endif
  494. if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
  495. dso = findnew_dso(pid, tid, filename, dso_id, machine);
  496. dso_sought = true;
  497. if (dso) {
  498. /* mark it not to inject build-id */
  499. dso__set_hit(dso);
  500. }
  501. }
  502. if (inject->build_id_style == BID_RWS__INJECT_HEADER_ALL) {
  503. if (!dso_sought) {
  504. dso = findnew_dso(pid, tid, filename, dso_id, machine);
  505. dso_sought = true;
  506. }
  507. if (dso && !dso__hit(dso)) {
  508. struct evsel *evsel = evlist__event2evsel(inject->session->evlist, event);
  509. if (evsel) {
  510. dso__set_hit(dso);
  511. tool__inject_build_id(tool, sample, machine, evsel,
  512. /*misc=*/sample->cpumode,
  513. filename, dso, flags);
  514. }
  515. }
  516. } else {
  517. int err;
  518. /*
  519. * Remember the evsel for lazy build id generation. It is used
  520. * for the sample id header type.
  521. */
  522. if ((inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
  523. inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) &&
  524. !inject->mmap_evsel)
  525. inject->mmap_evsel = evlist__event2evsel(inject->session->evlist, event);
  526. /* Create the thread, map, etc. Not done for the unordered inject all case. */
  527. err = perf_event_process(tool, event, sample, machine);
  528. if (err) {
  529. dso__put(dso);
  530. return err;
  531. }
  532. }
  533. if ((inject->build_id_style == BID_RWS__MMAP2_BUILDID_ALL) &&
  534. !(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) {
  535. struct evsel *evsel = evlist__event2evsel(inject->session->evlist, event);
  536. if (evsel && !dso_sought) {
  537. dso = findnew_dso(pid, tid, filename, dso_id, machine);
  538. dso_sought = true;
  539. }
  540. if (evsel && dso &&
  541. !tool__inject_mmap2_build_id(tool, sample, machine, evsel,
  542. sample->cpumode | PERF_RECORD_MISC_MMAP_BUILD_ID,
  543. pid, tid, start, len, pgoff,
  544. dso,
  545. prot, flags,
  546. filename)) {
  547. /* Injected mmap2 so no need to repipe. */
  548. dso__put(dso);
  549. return 0;
  550. }
  551. }
  552. dso__put(dso);
  553. if (inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY)
  554. return 0;
  555. return perf_event__repipe(tool, event, sample, machine);
  556. }
  557. static int perf_event__repipe_mmap(const struct perf_tool *tool,
  558. union perf_event *event,
  559. struct perf_sample *sample,
  560. struct machine *machine)
  561. {
  562. return perf_event__repipe_common_mmap(
  563. tool, event, sample, machine,
  564. event->mmap.pid, event->mmap.tid,
  565. event->mmap.start, event->mmap.len, event->mmap.pgoff,
  566. /*flags=*/0, PROT_EXEC,
  567. event->mmap.filename, /*dso_id=*/NULL,
  568. perf_event__process_mmap);
  569. }
  570. static int perf_event__repipe_mmap2(const struct perf_tool *tool,
  571. union perf_event *event,
  572. struct perf_sample *sample,
  573. struct machine *machine)
  574. {
  575. struct dso_id id = dso_id_empty;
  576. if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
  577. build_id__init(&id.build_id, event->mmap2.build_id, event->mmap2.build_id_size);
  578. } else {
  579. id.maj = event->mmap2.maj;
  580. id.min = event->mmap2.min;
  581. id.ino = event->mmap2.ino;
  582. id.ino_generation = event->mmap2.ino_generation;
  583. id.mmap2_valid = true;
  584. id.mmap2_ino_generation_valid = true;
  585. }
  586. return perf_event__repipe_common_mmap(
  587. tool, event, sample, machine,
  588. event->mmap2.pid, event->mmap2.tid,
  589. event->mmap2.start, event->mmap2.len, event->mmap2.pgoff,
  590. event->mmap2.flags, event->mmap2.prot,
  591. event->mmap2.filename, &id,
  592. perf_event__process_mmap2);
  593. }
  594. static int perf_event__repipe_fork(const struct perf_tool *tool,
  595. union perf_event *event,
  596. struct perf_sample *sample,
  597. struct machine *machine)
  598. {
  599. int err;
  600. err = perf_event__process_fork(tool, event, sample, machine);
  601. perf_event__repipe(tool, event, sample, machine);
  602. return err;
  603. }
  604. static int perf_event__repipe_comm(const struct perf_tool *tool,
  605. union perf_event *event,
  606. struct perf_sample *sample,
  607. struct machine *machine)
  608. {
  609. int err;
  610. err = perf_event__process_comm(tool, event, sample, machine);
  611. perf_event__repipe(tool, event, sample, machine);
  612. return err;
  613. }
  614. static int perf_event__repipe_namespaces(const struct perf_tool *tool,
  615. union perf_event *event,
  616. struct perf_sample *sample,
  617. struct machine *machine)
  618. {
  619. int err = perf_event__process_namespaces(tool, event, sample, machine);
  620. perf_event__repipe(tool, event, sample, machine);
  621. return err;
  622. }
  623. static int perf_event__repipe_exit(const struct perf_tool *tool,
  624. union perf_event *event,
  625. struct perf_sample *sample,
  626. struct machine *machine)
  627. {
  628. int err;
  629. err = perf_event__process_exit(tool, event, sample, machine);
  630. perf_event__repipe(tool, event, sample, machine);
  631. return err;
  632. }
  633. #ifdef HAVE_LIBTRACEEVENT
  634. static int perf_event__repipe_tracing_data(const struct perf_tool *tool,
  635. struct perf_session *session,
  636. union perf_event *event)
  637. {
  638. perf_event__repipe_synth(tool, event);
  639. return perf_event__process_tracing_data(tool, session, event);
  640. }
  641. #endif
  642. static int dso__read_build_id(struct dso *dso)
  643. {
  644. struct nscookie nsc;
  645. struct build_id bid = { .size = 0, };
  646. if (dso__has_build_id(dso))
  647. return 0;
  648. mutex_lock(dso__lock(dso));
  649. nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
  650. if (filename__read_build_id(dso__long_name(dso), &bid) > 0)
  651. dso__set_build_id(dso, &bid);
  652. else if (dso__nsinfo(dso)) {
  653. char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso));
  654. if (new_name && filename__read_build_id(new_name, &bid) > 0)
  655. dso__set_build_id(dso, &bid);
  656. free(new_name);
  657. }
  658. nsinfo__mountns_exit(&nsc);
  659. mutex_unlock(dso__lock(dso));
  660. return dso__has_build_id(dso) ? 0 : -1;
  661. }
  662. static struct strlist *perf_inject__parse_known_build_ids(
  663. const char *known_build_ids_string)
  664. {
  665. struct str_node *pos, *tmp;
  666. struct strlist *known_build_ids;
  667. int bid_len;
  668. known_build_ids = strlist__new(known_build_ids_string, NULL);
  669. if (known_build_ids == NULL)
  670. return NULL;
  671. strlist__for_each_entry_safe(pos, tmp, known_build_ids) {
  672. const char *build_id, *dso_name;
  673. build_id = skip_spaces(pos->s);
  674. dso_name = strchr(build_id, ' ');
  675. if (dso_name == NULL) {
  676. strlist__remove(known_build_ids, pos);
  677. continue;
  678. }
  679. bid_len = dso_name - pos->s;
  680. dso_name = skip_spaces(dso_name);
  681. if (bid_len % 2 != 0 || bid_len >= SBUILD_ID_SIZE) {
  682. strlist__remove(known_build_ids, pos);
  683. continue;
  684. }
  685. for (int ix = 0; 2 * ix + 1 < bid_len; ++ix) {
  686. if (!isxdigit(build_id[2 * ix]) ||
  687. !isxdigit(build_id[2 * ix + 1])) {
  688. strlist__remove(known_build_ids, pos);
  689. break;
  690. }
  691. }
  692. }
  693. return known_build_ids;
  694. }
  695. static bool perf_inject__lookup_known_build_id(struct perf_inject *inject,
  696. struct dso *dso)
  697. {
  698. struct str_node *pos;
  699. strlist__for_each_entry(pos, inject->known_build_ids) {
  700. struct build_id bid;
  701. const char *build_id, *dso_name;
  702. size_t bid_len;
  703. build_id = skip_spaces(pos->s);
  704. dso_name = strchr(build_id, ' ');
  705. bid_len = dso_name - pos->s;
  706. if (bid_len > sizeof(bid.data))
  707. bid_len = sizeof(bid.data);
  708. dso_name = skip_spaces(dso_name);
  709. if (strcmp(dso__long_name(dso), dso_name))
  710. continue;
  711. for (size_t ix = 0; 2 * ix + 1 < bid_len; ++ix) {
  712. bid.data[ix] = (hex(build_id[2 * ix]) << 4 |
  713. hex(build_id[2 * ix + 1]));
  714. }
  715. bid.size = bid_len / 2;
  716. dso__set_build_id(dso, &bid);
  717. return true;
  718. }
  719. return false;
  720. }
  721. static int tool__inject_build_id(const struct perf_tool *tool,
  722. struct perf_sample *sample,
  723. struct machine *machine,
  724. const struct evsel *evsel,
  725. __u16 misc,
  726. const char *filename,
  727. struct dso *dso, u32 flags)
  728. {
  729. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  730. int err;
  731. if (is_anon_memory(filename) || flags & MAP_HUGETLB)
  732. return 0;
  733. if (is_no_dso_memory(filename))
  734. return 0;
  735. if (inject->known_build_ids != NULL &&
  736. perf_inject__lookup_known_build_id(inject, dso))
  737. return 1;
  738. if (dso__read_build_id(dso) < 0) {
  739. pr_debug("no build_id found for %s\n", filename);
  740. return -1;
  741. }
  742. err = perf_event__synthesize_build_id(tool, sample, machine,
  743. perf_event__repipe,
  744. evsel, misc, dso__bid(dso),
  745. filename);
  746. if (err) {
  747. pr_err("Can't synthesize build_id event for %s\n", filename);
  748. return -1;
  749. }
  750. return 0;
  751. }
  752. static int tool__inject_mmap2_build_id(const struct perf_tool *tool,
  753. struct perf_sample *sample,
  754. struct machine *machine,
  755. const struct evsel *evsel,
  756. __u16 misc,
  757. __u32 pid, __u32 tid,
  758. __u64 start, __u64 len, __u64 pgoff,
  759. struct dso *dso,
  760. __u32 prot, __u32 flags,
  761. const char *filename)
  762. {
  763. int err;
  764. /* Return to repipe anonymous maps. */
  765. if (is_anon_memory(filename) || flags & MAP_HUGETLB)
  766. return 1;
  767. if (is_no_dso_memory(filename))
  768. return 1;
  769. if (dso__read_build_id(dso)) {
  770. pr_debug("no build_id found for %s\n", filename);
  771. return -1;
  772. }
  773. err = perf_event__synthesize_mmap2_build_id(tool, sample, machine,
  774. perf_event__repipe,
  775. evsel,
  776. misc, pid, tid,
  777. start, len, pgoff,
  778. dso__bid(dso),
  779. prot, flags,
  780. filename);
  781. if (err) {
  782. pr_err("Can't synthesize build_id event for %s\n", filename);
  783. return -1;
  784. }
  785. return 0;
  786. }
  787. static int mark_dso_hit(const struct perf_inject *inject,
  788. const struct perf_tool *tool,
  789. struct perf_sample *sample,
  790. struct machine *machine,
  791. const struct evsel *mmap_evsel,
  792. struct map *map, bool sample_in_dso)
  793. {
  794. struct dso *dso;
  795. u16 misc = sample->cpumode;
  796. if (!map)
  797. return 0;
  798. if (!sample_in_dso) {
  799. u16 guest_mask = PERF_RECORD_MISC_GUEST_KERNEL |
  800. PERF_RECORD_MISC_GUEST_USER;
  801. if ((misc & guest_mask) != 0) {
  802. misc &= PERF_RECORD_MISC_HYPERVISOR;
  803. misc |= __map__is_kernel(map)
  804. ? PERF_RECORD_MISC_GUEST_KERNEL
  805. : PERF_RECORD_MISC_GUEST_USER;
  806. } else {
  807. misc &= PERF_RECORD_MISC_HYPERVISOR;
  808. misc |= __map__is_kernel(map)
  809. ? PERF_RECORD_MISC_KERNEL
  810. : PERF_RECORD_MISC_USER;
  811. }
  812. }
  813. dso = map__dso(map);
  814. if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY) {
  815. if (dso && !dso__hit(dso)) {
  816. dso__set_hit(dso);
  817. tool__inject_build_id(tool, sample, machine,
  818. mmap_evsel, misc, dso__long_name(dso), dso,
  819. map__flags(map));
  820. }
  821. } else if (inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) {
  822. if (!map__hit(map)) {
  823. const struct build_id null_bid = { .size = 0 };
  824. const struct build_id *bid = dso ? dso__bid(dso) : &null_bid;
  825. const char *filename = dso ? dso__long_name(dso) : "";
  826. map__set_hit(map);
  827. perf_event__synthesize_mmap2_build_id(tool, sample, machine,
  828. perf_event__repipe,
  829. mmap_evsel,
  830. misc,
  831. sample->pid, sample->tid,
  832. map__start(map),
  833. map__end(map) - map__start(map),
  834. map__pgoff(map),
  835. bid,
  836. map__prot(map),
  837. map__flags(map),
  838. filename);
  839. }
  840. }
  841. return 0;
  842. }
  843. struct mark_dso_hit_args {
  844. const struct perf_inject *inject;
  845. const struct perf_tool *tool;
  846. struct perf_sample *sample;
  847. struct machine *machine;
  848. const struct evsel *mmap_evsel;
  849. };
  850. static int mark_dso_hit_callback(struct callchain_cursor_node *node, void *data)
  851. {
  852. struct mark_dso_hit_args *args = data;
  853. struct map *map = node->ms.map;
  854. return mark_dso_hit(args->inject, args->tool, args->sample, args->machine,
  855. args->mmap_evsel, map, /*sample_in_dso=*/false);
  856. }
  857. int perf_event__inject_buildid(const struct perf_tool *tool, union perf_event *event,
  858. struct perf_sample *sample,
  859. struct evsel *evsel __maybe_unused,
  860. struct machine *machine)
  861. {
  862. struct addr_location al;
  863. struct thread *thread;
  864. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  865. struct mark_dso_hit_args args = {
  866. .inject = inject,
  867. .tool = tool,
  868. /*
  869. * Use the parsed sample data of the sample event, which will
  870. * have a later timestamp than the mmap event.
  871. */
  872. .sample = sample,
  873. .machine = machine,
  874. .mmap_evsel = inject__mmap_evsel(inject),
  875. };
  876. addr_location__init(&al);
  877. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  878. if (thread == NULL) {
  879. pr_err("problem processing %d event, skipping it.\n",
  880. event->header.type);
  881. goto repipe;
  882. }
  883. if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
  884. mark_dso_hit(inject, tool, sample, machine, args.mmap_evsel, al.map,
  885. /*sample_in_dso=*/true);
  886. }
  887. sample__for_each_callchain_node(thread, evsel, sample, PERF_MAX_STACK_DEPTH,
  888. /*symbols=*/false, mark_dso_hit_callback, &args);
  889. thread__put(thread);
  890. repipe:
  891. perf_event__repipe(tool, event, sample, machine);
  892. addr_location__exit(&al);
  893. return 0;
  894. }
  895. static int perf_inject__sched_process_exit(const struct perf_tool *tool,
  896. union perf_event *event __maybe_unused,
  897. struct perf_sample *sample,
  898. struct evsel *evsel __maybe_unused,
  899. struct machine *machine __maybe_unused)
  900. {
  901. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  902. struct event_entry *ent;
  903. list_for_each_entry(ent, &inject->samples, node) {
  904. if (sample->tid == ent->tid) {
  905. list_del_init(&ent->node);
  906. free(ent);
  907. break;
  908. }
  909. }
  910. return 0;
  911. }
  912. static int perf_inject__sched_switch(const struct perf_tool *tool,
  913. union perf_event *event,
  914. struct perf_sample *sample,
  915. struct evsel *evsel,
  916. struct machine *machine)
  917. {
  918. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  919. struct event_entry *ent;
  920. perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
  921. ent = malloc(event->header.size + sizeof(struct event_entry));
  922. if (ent == NULL) {
  923. color_fprintf(stderr, PERF_COLOR_RED,
  924. "Not enough memory to process sched switch event!");
  925. return -1;
  926. }
  927. ent->tid = sample->tid;
  928. memcpy(&ent->event, event, event->header.size);
  929. list_add(&ent->node, &inject->samples);
  930. return 0;
  931. }
  932. #ifdef HAVE_LIBTRACEEVENT
  933. static int perf_inject__sched_stat(const struct perf_tool *tool,
  934. union perf_event *event __maybe_unused,
  935. struct perf_sample *sample,
  936. struct evsel *evsel,
  937. struct machine *machine)
  938. {
  939. struct event_entry *ent;
  940. union perf_event *event_sw;
  941. struct perf_sample sample_sw;
  942. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  943. u32 pid = evsel__intval(evsel, sample, "pid");
  944. list_for_each_entry(ent, &inject->samples, node) {
  945. if (pid == ent->tid)
  946. goto found;
  947. }
  948. return 0;
  949. found:
  950. event_sw = &ent->event[0];
  951. evsel__parse_sample(evsel, event_sw, &sample_sw);
  952. sample_sw.period = sample->period;
  953. sample_sw.time = sample->time;
  954. perf_event__synthesize_sample(event_sw, evsel->core.attr.sample_type,
  955. evsel->core.attr.read_format, &sample_sw);
  956. build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
  957. return perf_event__repipe(tool, event_sw, &sample_sw, machine);
  958. }
  959. #endif
  960. static struct guest_vcpu *guest_session__vcpu(struct guest_session *gs, u32 vcpu)
  961. {
  962. if (realloc_array_as_needed(gs->vcpu, gs->vcpu_cnt, vcpu, NULL))
  963. return NULL;
  964. return &gs->vcpu[vcpu];
  965. }
  966. static int guest_session__output_bytes(struct guest_session *gs, void *buf, size_t sz)
  967. {
  968. ssize_t ret = writen(gs->tmp_fd, buf, sz);
  969. return ret < 0 ? ret : 0;
  970. }
  971. static int guest_session__repipe(const struct perf_tool *tool,
  972. union perf_event *event,
  973. struct perf_sample *sample __maybe_unused,
  974. struct machine *machine __maybe_unused)
  975. {
  976. struct guest_session *gs = container_of(tool, struct guest_session, tool);
  977. return guest_session__output_bytes(gs, event, event->header.size);
  978. }
  979. static int guest_session__map_tid(struct guest_session *gs, u32 tid, u32 vcpu)
  980. {
  981. struct guest_tid *guest_tid = zalloc(sizeof(*guest_tid));
  982. int hash;
  983. if (!guest_tid)
  984. return -ENOMEM;
  985. guest_tid->tid = tid;
  986. guest_tid->vcpu = vcpu;
  987. hash = hash_32(guest_tid->tid, PERF_EVLIST__HLIST_BITS);
  988. hlist_add_head(&guest_tid->node, &gs->tids[hash]);
  989. return 0;
  990. }
  991. static int host_peek_vm_comms_cb(struct perf_session *session __maybe_unused,
  992. union perf_event *event,
  993. u64 offset __maybe_unused, void *data)
  994. {
  995. struct guest_session *gs = data;
  996. unsigned int vcpu;
  997. struct guest_vcpu *guest_vcpu;
  998. int ret;
  999. if (event->header.type != PERF_RECORD_COMM ||
  1000. event->comm.pid != gs->machine_pid)
  1001. return 0;
  1002. /*
  1003. * QEMU option -name debug-threads=on, causes thread names formatted as
  1004. * below, although it is not an ABI. Also libvirt seems to use this by
  1005. * default. Here we rely on it to tell us which thread is which VCPU.
  1006. */
  1007. ret = sscanf(event->comm.comm, "CPU %u/KVM", &vcpu);
  1008. if (ret <= 0)
  1009. return ret;
  1010. pr_debug("Found VCPU: tid %u comm %s vcpu %u\n",
  1011. event->comm.tid, event->comm.comm, vcpu);
  1012. if (vcpu > INT_MAX) {
  1013. pr_err("Invalid VCPU %u\n", vcpu);
  1014. return -EINVAL;
  1015. }
  1016. guest_vcpu = guest_session__vcpu(gs, vcpu);
  1017. if (!guest_vcpu)
  1018. return -ENOMEM;
  1019. if (guest_vcpu->tid && guest_vcpu->tid != event->comm.tid) {
  1020. pr_err("Fatal error: Two threads found with the same VCPU\n");
  1021. return -EINVAL;
  1022. }
  1023. guest_vcpu->tid = event->comm.tid;
  1024. return guest_session__map_tid(gs, event->comm.tid, vcpu);
  1025. }
  1026. static int host_peek_vm_comms(struct perf_session *session, struct guest_session *gs)
  1027. {
  1028. return perf_session__peek_events(session, session->header.data_offset,
  1029. session->header.data_size,
  1030. host_peek_vm_comms_cb, gs);
  1031. }
  1032. static bool evlist__is_id_used(struct evlist *evlist, u64 id)
  1033. {
  1034. return evlist__id2sid(evlist, id);
  1035. }
  1036. static u64 guest_session__allocate_new_id(struct guest_session *gs, struct evlist *host_evlist)
  1037. {
  1038. do {
  1039. gs->highest_id += 1;
  1040. } while (!gs->highest_id || evlist__is_id_used(host_evlist, gs->highest_id));
  1041. return gs->highest_id;
  1042. }
  1043. static int guest_session__map_id(struct guest_session *gs, u64 id, u64 host_id, u32 vcpu)
  1044. {
  1045. struct guest_id *guest_id = zalloc(sizeof(*guest_id));
  1046. int hash;
  1047. if (!guest_id)
  1048. return -ENOMEM;
  1049. guest_id->id = id;
  1050. guest_id->host_id = host_id;
  1051. guest_id->vcpu = vcpu;
  1052. hash = hash_64(guest_id->id, PERF_EVLIST__HLIST_BITS);
  1053. hlist_add_head(&guest_id->node, &gs->heads[hash]);
  1054. return 0;
  1055. }
  1056. static u64 evlist__find_highest_id(struct evlist *evlist)
  1057. {
  1058. struct evsel *evsel;
  1059. u64 highest_id = 1;
  1060. evlist__for_each_entry(evlist, evsel) {
  1061. u32 j;
  1062. for (j = 0; j < evsel->core.ids; j++) {
  1063. u64 id = evsel->core.id[j];
  1064. if (id > highest_id)
  1065. highest_id = id;
  1066. }
  1067. }
  1068. return highest_id;
  1069. }
  1070. static int guest_session__map_ids(struct guest_session *gs, struct evlist *host_evlist)
  1071. {
  1072. struct evlist *evlist = gs->session->evlist;
  1073. struct evsel *evsel;
  1074. int ret;
  1075. evlist__for_each_entry(evlist, evsel) {
  1076. u32 j;
  1077. for (j = 0; j < evsel->core.ids; j++) {
  1078. struct perf_sample_id *sid;
  1079. u64 host_id;
  1080. u64 id;
  1081. id = evsel->core.id[j];
  1082. sid = evlist__id2sid(evlist, id);
  1083. if (!sid || sid->cpu.cpu == -1)
  1084. continue;
  1085. host_id = guest_session__allocate_new_id(gs, host_evlist);
  1086. ret = guest_session__map_id(gs, id, host_id, sid->cpu.cpu);
  1087. if (ret)
  1088. return ret;
  1089. }
  1090. }
  1091. return 0;
  1092. }
  1093. static struct guest_id *guest_session__lookup_id(struct guest_session *gs, u64 id)
  1094. {
  1095. struct hlist_head *head;
  1096. struct guest_id *guest_id;
  1097. int hash;
  1098. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  1099. head = &gs->heads[hash];
  1100. hlist_for_each_entry(guest_id, head, node)
  1101. if (guest_id->id == id)
  1102. return guest_id;
  1103. return NULL;
  1104. }
  1105. static int process_attr(const struct perf_tool *tool, union perf_event *event,
  1106. struct perf_sample *sample __maybe_unused,
  1107. struct machine *machine __maybe_unused)
  1108. {
  1109. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  1110. return perf_event__process_attr(tool, event, &inject->session->evlist);
  1111. }
  1112. static int guest_session__add_attr(struct guest_session *gs, struct evsel *evsel)
  1113. {
  1114. struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
  1115. struct perf_event_attr attr = evsel->core.attr;
  1116. u64 *id_array;
  1117. u32 *vcpu_array;
  1118. int ret = -ENOMEM;
  1119. u32 i;
  1120. id_array = calloc(evsel->core.ids, sizeof(*id_array));
  1121. if (!id_array)
  1122. return -ENOMEM;
  1123. vcpu_array = calloc(evsel->core.ids, sizeof(*vcpu_array));
  1124. if (!vcpu_array)
  1125. goto out;
  1126. for (i = 0; i < evsel->core.ids; i++) {
  1127. u64 id = evsel->core.id[i];
  1128. struct guest_id *guest_id = guest_session__lookup_id(gs, id);
  1129. if (!guest_id) {
  1130. pr_err("Failed to find guest id %"PRIu64"\n", id);
  1131. ret = -EINVAL;
  1132. goto out;
  1133. }
  1134. id_array[i] = guest_id->host_id;
  1135. vcpu_array[i] = guest_id->vcpu;
  1136. }
  1137. attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
  1138. attr.exclude_host = 1;
  1139. attr.exclude_guest = 0;
  1140. ret = perf_event__synthesize_attr(&inject->tool, &attr, evsel->core.ids,
  1141. id_array, process_attr);
  1142. if (ret)
  1143. pr_err("Failed to add guest attr.\n");
  1144. for (i = 0; i < evsel->core.ids; i++) {
  1145. struct perf_sample_id *sid;
  1146. u32 vcpu = vcpu_array[i];
  1147. sid = evlist__id2sid(inject->session->evlist, id_array[i]);
  1148. /* Guest event is per-thread from the host point of view */
  1149. sid->cpu.cpu = -1;
  1150. sid->tid = gs->vcpu[vcpu].tid;
  1151. sid->machine_pid = gs->machine_pid;
  1152. sid->vcpu.cpu = vcpu;
  1153. }
  1154. out:
  1155. free(vcpu_array);
  1156. free(id_array);
  1157. return ret;
  1158. }
  1159. static int guest_session__add_attrs(struct guest_session *gs)
  1160. {
  1161. struct evlist *evlist = gs->session->evlist;
  1162. struct evsel *evsel;
  1163. int ret;
  1164. evlist__for_each_entry(evlist, evsel) {
  1165. ret = guest_session__add_attr(gs, evsel);
  1166. if (ret)
  1167. return ret;
  1168. }
  1169. return 0;
  1170. }
  1171. static int synthesize_id_index(struct perf_inject *inject, size_t new_cnt)
  1172. {
  1173. struct perf_session *session = inject->session;
  1174. struct evlist *evlist = session->evlist;
  1175. struct machine *machine = &session->machines.host;
  1176. size_t from = evlist->core.nr_entries - new_cnt;
  1177. return __perf_event__synthesize_id_index(&inject->tool, perf_event__repipe,
  1178. evlist, machine, from);
  1179. }
  1180. static struct guest_tid *guest_session__lookup_tid(struct guest_session *gs, u32 tid)
  1181. {
  1182. struct hlist_head *head;
  1183. struct guest_tid *guest_tid;
  1184. int hash;
  1185. hash = hash_32(tid, PERF_EVLIST__HLIST_BITS);
  1186. head = &gs->tids[hash];
  1187. hlist_for_each_entry(guest_tid, head, node)
  1188. if (guest_tid->tid == tid)
  1189. return guest_tid;
  1190. return NULL;
  1191. }
  1192. static bool dso__is_in_kernel_space(struct dso *dso)
  1193. {
  1194. if (dso__is_vdso(dso))
  1195. return false;
  1196. return dso__is_kcore(dso) ||
  1197. dso__kernel(dso) ||
  1198. is_kernel_module(dso__long_name(dso), PERF_RECORD_MISC_CPUMODE_UNKNOWN);
  1199. }
  1200. static u64 evlist__first_id(struct evlist *evlist)
  1201. {
  1202. struct evsel *evsel;
  1203. evlist__for_each_entry(evlist, evsel) {
  1204. if (evsel->core.ids)
  1205. return evsel->core.id[0];
  1206. }
  1207. return 0;
  1208. }
  1209. static int process_build_id(const struct perf_tool *tool,
  1210. union perf_event *event,
  1211. struct perf_sample *sample __maybe_unused,
  1212. struct machine *machine __maybe_unused)
  1213. {
  1214. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  1215. return perf_event__process_build_id(tool, inject->session, event);
  1216. }
  1217. static int synthesize_build_id(struct perf_inject *inject, struct dso *dso, pid_t machine_pid)
  1218. {
  1219. struct machine *machine = perf_session__findnew_machine(inject->session, machine_pid);
  1220. struct perf_sample synth_sample = {
  1221. .pid = -1,
  1222. .tid = -1,
  1223. .time = -1,
  1224. .stream_id = -1,
  1225. .cpu = -1,
  1226. .period = 1,
  1227. .cpumode = dso__is_in_kernel_space(dso)
  1228. ? PERF_RECORD_MISC_GUEST_KERNEL
  1229. : PERF_RECORD_MISC_GUEST_USER,
  1230. };
  1231. if (!machine)
  1232. return -ENOMEM;
  1233. dso__set_hit(dso);
  1234. return perf_event__synthesize_build_id(&inject->tool, &synth_sample, machine,
  1235. process_build_id, inject__mmap_evsel(inject),
  1236. /*misc=*/synth_sample.cpumode,
  1237. dso__bid(dso), dso__long_name(dso));
  1238. }
  1239. static int guest_session__add_build_ids_cb(struct dso *dso, void *data)
  1240. {
  1241. struct guest_session *gs = data;
  1242. struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
  1243. if (!dso__has_build_id(dso))
  1244. return 0;
  1245. return synthesize_build_id(inject, dso, gs->machine_pid);
  1246. }
  1247. static int guest_session__add_build_ids(struct guest_session *gs)
  1248. {
  1249. struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
  1250. /* Build IDs will be put in the Build ID feature section */
  1251. perf_header__set_feat(&inject->session->header, HEADER_BUILD_ID);
  1252. return dsos__for_each_dso(&gs->session->machines.host.dsos,
  1253. guest_session__add_build_ids_cb,
  1254. gs);
  1255. }
  1256. static int guest_session__ksymbol_event(const struct perf_tool *tool,
  1257. union perf_event *event,
  1258. struct perf_sample *sample __maybe_unused,
  1259. struct machine *machine __maybe_unused)
  1260. {
  1261. struct guest_session *gs = container_of(tool, struct guest_session, tool);
  1262. /* Only support out-of-line i.e. no BPF support */
  1263. if (event->ksymbol.ksym_type != PERF_RECORD_KSYMBOL_TYPE_OOL)
  1264. return 0;
  1265. return guest_session__output_bytes(gs, event, event->header.size);
  1266. }
  1267. static int guest_session__start(struct guest_session *gs, const char *name, bool force)
  1268. {
  1269. char tmp_file_name[] = "/tmp/perf-inject-guest_session-XXXXXX";
  1270. struct perf_session *session;
  1271. int ret;
  1272. /* Only these events will be injected */
  1273. gs->tool.mmap = guest_session__repipe;
  1274. gs->tool.mmap2 = guest_session__repipe;
  1275. gs->tool.comm = guest_session__repipe;
  1276. gs->tool.fork = guest_session__repipe;
  1277. gs->tool.exit = guest_session__repipe;
  1278. gs->tool.lost = guest_session__repipe;
  1279. gs->tool.context_switch = guest_session__repipe;
  1280. gs->tool.ksymbol = guest_session__ksymbol_event;
  1281. gs->tool.text_poke = guest_session__repipe;
  1282. /*
  1283. * Processing a build ID creates a struct dso with that build ID. Later,
  1284. * all guest dsos are iterated and the build IDs processed into the host
  1285. * session where they will be output to the Build ID feature section
  1286. * when the perf.data file header is written.
  1287. */
  1288. gs->tool.build_id = perf_event__process_build_id;
  1289. /* Process the id index to know what VCPU an ID belongs to */
  1290. gs->tool.id_index = perf_event__process_id_index;
  1291. gs->tool.ordered_events = true;
  1292. gs->tool.ordering_requires_timestamps = true;
  1293. gs->data.path = name;
  1294. gs->data.force = force;
  1295. gs->data.mode = PERF_DATA_MODE_READ;
  1296. session = perf_session__new(&gs->data, &gs->tool);
  1297. if (IS_ERR(session))
  1298. return PTR_ERR(session);
  1299. gs->session = session;
  1300. /*
  1301. * Initial events have zero'd ID samples. Get default ID sample size
  1302. * used for removing them.
  1303. */
  1304. gs->dflt_id_hdr_size = session->machines.host.id_hdr_size;
  1305. /* And default ID for adding back a host-compatible ID sample */
  1306. gs->dflt_id = evlist__first_id(session->evlist);
  1307. if (!gs->dflt_id) {
  1308. pr_err("Guest data has no sample IDs");
  1309. return -EINVAL;
  1310. }
  1311. /* Temporary file for guest events */
  1312. gs->tmp_file_name = strdup(tmp_file_name);
  1313. if (!gs->tmp_file_name)
  1314. return -ENOMEM;
  1315. gs->tmp_fd = mkstemp(gs->tmp_file_name);
  1316. if (gs->tmp_fd < 0)
  1317. return -errno;
  1318. if (zstd_init(&gs->session->zstd_data, 0) < 0)
  1319. pr_warning("Guest session decompression initialization failed.\n");
  1320. /*
  1321. * perf does not support processing 2 sessions simultaneously, so output
  1322. * guest events to a temporary file.
  1323. */
  1324. ret = perf_session__process_events(gs->session);
  1325. if (ret)
  1326. return ret;
  1327. if (lseek(gs->tmp_fd, 0, SEEK_SET))
  1328. return -errno;
  1329. return 0;
  1330. }
  1331. /* Free hlist nodes assuming hlist_node is the first member of hlist entries */
  1332. static void free_hlist(struct hlist_head *heads, size_t hlist_sz)
  1333. {
  1334. struct hlist_node *pos, *n;
  1335. size_t i;
  1336. for (i = 0; i < hlist_sz; ++i) {
  1337. hlist_for_each_safe(pos, n, &heads[i]) {
  1338. hlist_del(pos);
  1339. free(pos);
  1340. }
  1341. }
  1342. }
  1343. static void guest_session__exit(struct guest_session *gs)
  1344. {
  1345. if (gs->session) {
  1346. perf_session__delete(gs->session);
  1347. free_hlist(gs->heads, PERF_EVLIST__HLIST_SIZE);
  1348. free_hlist(gs->tids, PERF_EVLIST__HLIST_SIZE);
  1349. }
  1350. if (gs->tmp_file_name) {
  1351. if (gs->tmp_fd >= 0)
  1352. close(gs->tmp_fd);
  1353. unlink(gs->tmp_file_name);
  1354. zfree(&gs->tmp_file_name);
  1355. }
  1356. zfree(&gs->vcpu);
  1357. zfree(&gs->perf_data_file);
  1358. }
  1359. static void get_tsc_conv(struct perf_tsc_conversion *tc, struct perf_record_time_conv *time_conv)
  1360. {
  1361. tc->time_shift = time_conv->time_shift;
  1362. tc->time_mult = time_conv->time_mult;
  1363. tc->time_zero = time_conv->time_zero;
  1364. tc->time_cycles = time_conv->time_cycles;
  1365. tc->time_mask = time_conv->time_mask;
  1366. tc->cap_user_time_zero = time_conv->cap_user_time_zero;
  1367. tc->cap_user_time_short = time_conv->cap_user_time_short;
  1368. }
  1369. static void guest_session__get_tc(struct guest_session *gs)
  1370. {
  1371. struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
  1372. get_tsc_conv(&gs->host_tc, &inject->session->time_conv);
  1373. get_tsc_conv(&gs->guest_tc, &gs->session->time_conv);
  1374. }
  1375. static void guest_session__convert_time(struct guest_session *gs, u64 guest_time, u64 *host_time)
  1376. {
  1377. u64 tsc;
  1378. if (!guest_time) {
  1379. *host_time = 0;
  1380. return;
  1381. }
  1382. if (gs->guest_tc.cap_user_time_zero)
  1383. tsc = perf_time_to_tsc(guest_time, &gs->guest_tc);
  1384. else
  1385. tsc = guest_time;
  1386. /*
  1387. * This is the correct order of operations for x86 if the TSC Offset and
  1388. * Multiplier values are used.
  1389. */
  1390. tsc -= gs->time_offset;
  1391. tsc /= gs->time_scale;
  1392. if (gs->host_tc.cap_user_time_zero)
  1393. *host_time = tsc_to_perf_time(tsc, &gs->host_tc);
  1394. else
  1395. *host_time = tsc;
  1396. }
  1397. static int guest_session__fetch(struct guest_session *gs)
  1398. {
  1399. void *buf;
  1400. struct perf_event_header *hdr;
  1401. size_t hdr_sz = sizeof(*hdr);
  1402. ssize_t ret;
  1403. buf = gs->ev.event_buf;
  1404. if (!buf) {
  1405. buf = malloc(PERF_SAMPLE_MAX_SIZE);
  1406. if (!buf)
  1407. return -ENOMEM;
  1408. gs->ev.event_buf = buf;
  1409. }
  1410. hdr = buf;
  1411. ret = readn(gs->tmp_fd, buf, hdr_sz);
  1412. if (ret < 0)
  1413. return ret;
  1414. if (!ret) {
  1415. /* Zero size means EOF */
  1416. hdr->size = 0;
  1417. return 0;
  1418. }
  1419. buf += hdr_sz;
  1420. ret = readn(gs->tmp_fd, buf, hdr->size - hdr_sz);
  1421. if (ret < 0)
  1422. return ret;
  1423. gs->ev.event = (union perf_event *)gs->ev.event_buf;
  1424. gs->ev.sample.time = 0;
  1425. if (hdr->type >= PERF_RECORD_USER_TYPE_START) {
  1426. pr_err("Unexpected type fetching guest event");
  1427. return 0;
  1428. }
  1429. ret = evlist__parse_sample(gs->session->evlist, gs->ev.event, &gs->ev.sample);
  1430. if (ret) {
  1431. pr_err("Parse failed fetching guest event");
  1432. return ret;
  1433. }
  1434. if (!gs->have_tc) {
  1435. guest_session__get_tc(gs);
  1436. gs->have_tc = true;
  1437. }
  1438. guest_session__convert_time(gs, gs->ev.sample.time, &gs->ev.sample.time);
  1439. return 0;
  1440. }
  1441. static int evlist__append_id_sample(struct evlist *evlist, union perf_event *ev,
  1442. const struct perf_sample *sample)
  1443. {
  1444. struct evsel *evsel;
  1445. void *array;
  1446. int ret;
  1447. evsel = evlist__id2evsel(evlist, sample->id);
  1448. array = ev;
  1449. if (!evsel) {
  1450. pr_err("No evsel for id %"PRIu64"\n", sample->id);
  1451. return -EINVAL;
  1452. }
  1453. array += ev->header.size;
  1454. ret = perf_event__synthesize_id_sample(array, evsel->core.attr.sample_type, sample);
  1455. if (ret < 0)
  1456. return ret;
  1457. if (ret & 7) {
  1458. pr_err("Bad id sample size %d\n", ret);
  1459. return -EINVAL;
  1460. }
  1461. ev->header.size += ret;
  1462. return 0;
  1463. }
  1464. static int guest_session__inject_events(struct guest_session *gs, u64 timestamp)
  1465. {
  1466. struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
  1467. int ret;
  1468. if (!gs->ready)
  1469. return 0;
  1470. while (1) {
  1471. struct perf_sample *sample;
  1472. struct guest_id *guest_id;
  1473. union perf_event *ev;
  1474. u16 id_hdr_size;
  1475. u8 cpumode;
  1476. u64 id;
  1477. if (!gs->fetched) {
  1478. ret = guest_session__fetch(gs);
  1479. if (ret)
  1480. return ret;
  1481. gs->fetched = true;
  1482. }
  1483. ev = gs->ev.event;
  1484. sample = &gs->ev.sample;
  1485. if (!ev->header.size)
  1486. return 0; /* EOF */
  1487. if (sample->time > timestamp)
  1488. return 0;
  1489. /* Change cpumode to guest */
  1490. cpumode = ev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  1491. if (cpumode & PERF_RECORD_MISC_USER)
  1492. cpumode = PERF_RECORD_MISC_GUEST_USER;
  1493. else
  1494. cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
  1495. ev->header.misc &= ~PERF_RECORD_MISC_CPUMODE_MASK;
  1496. ev->header.misc |= cpumode;
  1497. id = sample->id;
  1498. if (!id) {
  1499. id = gs->dflt_id;
  1500. id_hdr_size = gs->dflt_id_hdr_size;
  1501. } else {
  1502. struct evsel *evsel = evlist__id2evsel(gs->session->evlist, id);
  1503. id_hdr_size = evsel__id_hdr_size(evsel);
  1504. }
  1505. if (id_hdr_size & 7) {
  1506. pr_err("Bad id_hdr_size %u\n", id_hdr_size);
  1507. return -EINVAL;
  1508. }
  1509. if (ev->header.size & 7) {
  1510. pr_err("Bad event size %u\n", ev->header.size);
  1511. return -EINVAL;
  1512. }
  1513. /* Remove guest id sample */
  1514. ev->header.size -= id_hdr_size;
  1515. if (ev->header.size & 7) {
  1516. pr_err("Bad raw event size %u\n", ev->header.size);
  1517. return -EINVAL;
  1518. }
  1519. guest_id = guest_session__lookup_id(gs, id);
  1520. if (!guest_id) {
  1521. pr_err("Guest event with unknown id %llu\n",
  1522. (unsigned long long)id);
  1523. return -EINVAL;
  1524. }
  1525. /* Change to host ID to avoid conflicting ID values */
  1526. sample->id = guest_id->host_id;
  1527. sample->stream_id = guest_id->host_id;
  1528. if (sample->cpu != (u32)-1) {
  1529. if (sample->cpu >= gs->vcpu_cnt) {
  1530. pr_err("Guest event with unknown VCPU %u\n",
  1531. sample->cpu);
  1532. return -EINVAL;
  1533. }
  1534. /* Change to host CPU instead of guest VCPU */
  1535. sample->cpu = gs->vcpu[sample->cpu].cpu;
  1536. }
  1537. /* New id sample with new ID and CPU */
  1538. ret = evlist__append_id_sample(inject->session->evlist, ev, sample);
  1539. if (ret)
  1540. return ret;
  1541. if (ev->header.size & 7) {
  1542. pr_err("Bad new event size %u\n", ev->header.size);
  1543. return -EINVAL;
  1544. }
  1545. gs->fetched = false;
  1546. ret = output_bytes(inject, ev, ev->header.size);
  1547. if (ret)
  1548. return ret;
  1549. }
  1550. }
  1551. static int guest_session__flush_events(struct guest_session *gs)
  1552. {
  1553. return guest_session__inject_events(gs, -1);
  1554. }
  1555. static int host__repipe(const struct perf_tool *tool,
  1556. union perf_event *event,
  1557. struct perf_sample *sample,
  1558. struct machine *machine)
  1559. {
  1560. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  1561. int ret;
  1562. ret = guest_session__inject_events(&inject->guest_session, sample->time);
  1563. if (ret)
  1564. return ret;
  1565. return perf_event__repipe(tool, event, sample, machine);
  1566. }
  1567. static int host__finished_init(const struct perf_tool *tool, struct perf_session *session,
  1568. union perf_event *event)
  1569. {
  1570. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  1571. struct guest_session *gs = &inject->guest_session;
  1572. int ret;
  1573. /*
  1574. * Peek through host COMM events to find QEMU threads and the VCPU they
  1575. * are running.
  1576. */
  1577. ret = host_peek_vm_comms(session, gs);
  1578. if (ret)
  1579. return ret;
  1580. if (!gs->vcpu_cnt) {
  1581. pr_err("No VCPU threads found for pid %u\n", gs->machine_pid);
  1582. return -EINVAL;
  1583. }
  1584. /*
  1585. * Allocate new (unused) host sample IDs and map them to the guest IDs.
  1586. */
  1587. gs->highest_id = evlist__find_highest_id(session->evlist);
  1588. ret = guest_session__map_ids(gs, session->evlist);
  1589. if (ret)
  1590. return ret;
  1591. ret = guest_session__add_attrs(gs);
  1592. if (ret)
  1593. return ret;
  1594. ret = synthesize_id_index(inject, gs->session->evlist->core.nr_entries);
  1595. if (ret) {
  1596. pr_err("Failed to synthesize id_index\n");
  1597. return ret;
  1598. }
  1599. ret = guest_session__add_build_ids(gs);
  1600. if (ret) {
  1601. pr_err("Failed to add guest build IDs\n");
  1602. return ret;
  1603. }
  1604. gs->ready = true;
  1605. ret = guest_session__inject_events(gs, 0);
  1606. if (ret)
  1607. return ret;
  1608. return perf_event__repipe_op2_synth(tool, session, event);
  1609. }
  1610. /*
  1611. * Obey finished-round ordering. The FINISHED_ROUND event is first processed
  1612. * which flushes host events to file up until the last flush time. Then inject
  1613. * guest events up to the same time. Finally write out the FINISHED_ROUND event
  1614. * itself.
  1615. */
  1616. static int host__finished_round(const struct perf_tool *tool,
  1617. union perf_event *event,
  1618. struct ordered_events *oe)
  1619. {
  1620. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  1621. int ret = perf_event__process_finished_round(tool, event, oe);
  1622. u64 timestamp = ordered_events__last_flush_time(oe);
  1623. if (ret)
  1624. return ret;
  1625. ret = guest_session__inject_events(&inject->guest_session, timestamp);
  1626. if (ret)
  1627. return ret;
  1628. return perf_event__repipe_oe_synth(tool, event, oe);
  1629. }
  1630. static int host__context_switch(const struct perf_tool *tool,
  1631. union perf_event *event,
  1632. struct perf_sample *sample,
  1633. struct machine *machine)
  1634. {
  1635. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  1636. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  1637. struct guest_session *gs = &inject->guest_session;
  1638. u32 pid = event->context_switch.next_prev_pid;
  1639. u32 tid = event->context_switch.next_prev_tid;
  1640. struct guest_tid *guest_tid;
  1641. u32 vcpu;
  1642. if (out || pid != gs->machine_pid)
  1643. goto out;
  1644. guest_tid = guest_session__lookup_tid(gs, tid);
  1645. if (!guest_tid)
  1646. goto out;
  1647. if (sample->cpu == (u32)-1) {
  1648. pr_err("Switch event does not have CPU\n");
  1649. return -EINVAL;
  1650. }
  1651. vcpu = guest_tid->vcpu;
  1652. if (vcpu >= gs->vcpu_cnt)
  1653. return -EINVAL;
  1654. /* Guest is switching in, record which CPU the VCPU is now running on */
  1655. gs->vcpu[vcpu].cpu = sample->cpu;
  1656. out:
  1657. return host__repipe(tool, event, sample, machine);
  1658. }
  1659. static void sig_handler(int sig __maybe_unused)
  1660. {
  1661. session_done = 1;
  1662. }
  1663. static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg)
  1664. {
  1665. struct perf_event_attr *attr = &evsel->core.attr;
  1666. const char *name = evsel__name(evsel);
  1667. if (!(attr->sample_type & sample_type)) {
  1668. pr_err("Samples for %s event do not have %s attribute set.",
  1669. name, sample_msg);
  1670. return -EINVAL;
  1671. }
  1672. return 0;
  1673. }
  1674. static int drop_sample(const struct perf_tool *tool __maybe_unused,
  1675. union perf_event *event __maybe_unused,
  1676. struct perf_sample *sample __maybe_unused,
  1677. struct evsel *evsel __maybe_unused,
  1678. struct machine *machine __maybe_unused)
  1679. {
  1680. return 0;
  1681. }
  1682. static void strip_init(struct perf_inject *inject)
  1683. {
  1684. struct evlist *evlist = inject->session->evlist;
  1685. struct evsel *evsel;
  1686. inject->tool.context_switch = perf_event__drop;
  1687. evlist__for_each_entry(evlist, evsel)
  1688. evsel->handler = drop_sample;
  1689. }
  1690. static int parse_vm_time_correlation(const struct option *opt, const char *str, int unset)
  1691. {
  1692. struct perf_inject *inject = opt->value;
  1693. const char *args;
  1694. char *dry_run;
  1695. if (unset)
  1696. return 0;
  1697. inject->itrace_synth_opts.set = true;
  1698. inject->itrace_synth_opts.vm_time_correlation = true;
  1699. inject->in_place_update = true;
  1700. if (!str)
  1701. return 0;
  1702. dry_run = skip_spaces(str);
  1703. if (!strncmp(dry_run, "dry-run", strlen("dry-run"))) {
  1704. inject->itrace_synth_opts.vm_tm_corr_dry_run = true;
  1705. inject->in_place_update_dry_run = true;
  1706. args = dry_run + strlen("dry-run");
  1707. } else {
  1708. args = str;
  1709. }
  1710. inject->itrace_synth_opts.vm_tm_corr_args = strdup(args);
  1711. return inject->itrace_synth_opts.vm_tm_corr_args ? 0 : -ENOMEM;
  1712. }
  1713. static int parse_guest_data(const struct option *opt, const char *str, int unset)
  1714. {
  1715. struct perf_inject *inject = opt->value;
  1716. struct guest_session *gs = &inject->guest_session;
  1717. char *tok;
  1718. char *s;
  1719. if (unset)
  1720. return 0;
  1721. if (!str)
  1722. goto bad_args;
  1723. s = strdup(str);
  1724. if (!s)
  1725. return -ENOMEM;
  1726. gs->perf_data_file = strsep(&s, ",");
  1727. if (!gs->perf_data_file)
  1728. goto bad_args;
  1729. gs->copy_kcore_dir = has_kcore_dir(gs->perf_data_file);
  1730. if (gs->copy_kcore_dir)
  1731. inject->output.is_dir = true;
  1732. tok = strsep(&s, ",");
  1733. if (!tok)
  1734. goto bad_args;
  1735. gs->machine_pid = strtoul(tok, NULL, 0);
  1736. if (!inject->guest_session.machine_pid)
  1737. goto bad_args;
  1738. gs->time_scale = 1;
  1739. tok = strsep(&s, ",");
  1740. if (!tok)
  1741. goto out;
  1742. gs->time_offset = strtoull(tok, NULL, 0);
  1743. tok = strsep(&s, ",");
  1744. if (!tok)
  1745. goto out;
  1746. gs->time_scale = strtod(tok, NULL);
  1747. if (!gs->time_scale)
  1748. goto bad_args;
  1749. out:
  1750. return 0;
  1751. bad_args:
  1752. pr_err("--guest-data option requires guest perf.data file name, "
  1753. "guest machine PID, and optionally guest timestamp offset, "
  1754. "and guest timestamp scale factor, separated by commas.\n");
  1755. return -1;
  1756. }
  1757. static int save_section_info_cb(struct perf_file_section *section,
  1758. struct perf_header *ph __maybe_unused,
  1759. int feat, int fd __maybe_unused, void *data)
  1760. {
  1761. struct perf_inject *inject = data;
  1762. inject->secs[feat] = *section;
  1763. return 0;
  1764. }
  1765. static int save_section_info(struct perf_inject *inject)
  1766. {
  1767. struct perf_header *header = &inject->session->header;
  1768. int fd = perf_data__fd(inject->session->data);
  1769. return perf_header__process_sections(header, fd, inject, save_section_info_cb);
  1770. }
  1771. static bool keep_feat(struct perf_inject *inject, int feat)
  1772. {
  1773. switch (feat) {
  1774. /* Keep original information that describes the machine or software */
  1775. case HEADER_TRACING_DATA:
  1776. case HEADER_HOSTNAME:
  1777. case HEADER_OSRELEASE:
  1778. case HEADER_VERSION:
  1779. case HEADER_ARCH:
  1780. case HEADER_NRCPUS:
  1781. case HEADER_CPUDESC:
  1782. case HEADER_CPUID:
  1783. case HEADER_TOTAL_MEM:
  1784. case HEADER_CPU_TOPOLOGY:
  1785. case HEADER_NUMA_TOPOLOGY:
  1786. case HEADER_PMU_MAPPINGS:
  1787. case HEADER_CACHE:
  1788. case HEADER_MEM_TOPOLOGY:
  1789. case HEADER_CLOCKID:
  1790. case HEADER_BPF_PROG_INFO:
  1791. case HEADER_BPF_BTF:
  1792. case HEADER_CPU_PMU_CAPS:
  1793. case HEADER_CLOCK_DATA:
  1794. case HEADER_HYBRID_TOPOLOGY:
  1795. case HEADER_PMU_CAPS:
  1796. case HEADER_CPU_DOMAIN_INFO:
  1797. return true;
  1798. /* Information that can be updated */
  1799. case HEADER_BUILD_ID:
  1800. return inject->build_id_style == BID_RWS__NONE;
  1801. case HEADER_CMDLINE:
  1802. case HEADER_EVENT_DESC:
  1803. case HEADER_BRANCH_STACK:
  1804. case HEADER_GROUP_DESC:
  1805. case HEADER_AUXTRACE:
  1806. case HEADER_STAT:
  1807. case HEADER_SAMPLE_TIME:
  1808. case HEADER_DIR_FORMAT:
  1809. case HEADER_COMPRESSED:
  1810. default:
  1811. return false;
  1812. };
  1813. }
  1814. static int read_file(int fd, u64 offs, void *buf, size_t sz)
  1815. {
  1816. ssize_t ret = preadn(fd, buf, sz, offs);
  1817. if (ret < 0)
  1818. return -errno;
  1819. if ((size_t)ret != sz)
  1820. return -EINVAL;
  1821. return 0;
  1822. }
  1823. static int feat_copy(struct perf_inject *inject, int feat, struct feat_writer *fw)
  1824. {
  1825. int fd = perf_data__fd(inject->session->data);
  1826. u64 offs = inject->secs[feat].offset;
  1827. size_t sz = inject->secs[feat].size;
  1828. void *buf = malloc(sz);
  1829. int ret;
  1830. if (!buf)
  1831. return -ENOMEM;
  1832. ret = read_file(fd, offs, buf, sz);
  1833. if (ret)
  1834. goto out_free;
  1835. ret = fw->write(fw, buf, sz);
  1836. out_free:
  1837. free(buf);
  1838. return ret;
  1839. }
  1840. struct inject_fc {
  1841. struct feat_copier fc;
  1842. struct perf_inject *inject;
  1843. };
  1844. static int feat_copy_cb(struct feat_copier *fc, int feat, struct feat_writer *fw)
  1845. {
  1846. struct inject_fc *inj_fc = container_of(fc, struct inject_fc, fc);
  1847. struct perf_inject *inject = inj_fc->inject;
  1848. int ret;
  1849. if (!inject->secs[feat].offset ||
  1850. !keep_feat(inject, feat))
  1851. return 0;
  1852. ret = feat_copy(inject, feat, fw);
  1853. if (ret < 0)
  1854. return ret;
  1855. return 1; /* Feature section copied */
  1856. }
  1857. static int copy_kcore_dir(struct perf_inject *inject)
  1858. {
  1859. char *cmd;
  1860. int ret;
  1861. ret = asprintf(&cmd, "cp -r -n %s/kcore_dir* %s >/dev/null 2>&1",
  1862. inject->input_name, inject->output.path);
  1863. if (ret < 0)
  1864. return ret;
  1865. pr_debug("%s\n", cmd);
  1866. ret = system(cmd);
  1867. free(cmd);
  1868. return ret;
  1869. }
  1870. static int guest_session__copy_kcore_dir(struct guest_session *gs)
  1871. {
  1872. struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
  1873. char *cmd;
  1874. int ret;
  1875. ret = asprintf(&cmd, "cp -r -n %s/kcore_dir %s/kcore_dir__%u >/dev/null 2>&1",
  1876. gs->perf_data_file, inject->output.path, gs->machine_pid);
  1877. if (ret < 0)
  1878. return ret;
  1879. pr_debug("%s\n", cmd);
  1880. ret = system(cmd);
  1881. free(cmd);
  1882. return ret;
  1883. }
  1884. static int output_fd(struct perf_inject *inject)
  1885. {
  1886. return inject->in_place_update ? -1 : perf_data__fd(&inject->output);
  1887. }
  1888. static int __cmd_inject(struct perf_inject *inject)
  1889. {
  1890. int ret = -EINVAL;
  1891. struct guest_session *gs = &inject->guest_session;
  1892. struct perf_session *session = inject->session;
  1893. int fd = output_fd(inject);
  1894. u64 output_data_offset = perf_session__data_offset(session->evlist);
  1895. /*
  1896. * Pipe input hasn't loaded the attributes and will handle them as
  1897. * events. So that the attributes don't overlap the data, write the
  1898. * attributes after the data.
  1899. */
  1900. bool write_attrs_after_data = !inject->output.is_pipe && inject->session->data->is_pipe;
  1901. signal(SIGINT, sig_handler);
  1902. if (inject->build_id_style != BID_RWS__NONE || inject->sched_stat ||
  1903. inject->itrace_synth_opts.set) {
  1904. inject->tool.mmap = perf_event__repipe_mmap;
  1905. inject->tool.mmap2 = perf_event__repipe_mmap2;
  1906. inject->tool.fork = perf_event__repipe_fork;
  1907. #ifdef HAVE_LIBTRACEEVENT
  1908. inject->tool.tracing_data = perf_event__repipe_tracing_data;
  1909. #endif
  1910. }
  1911. if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
  1912. inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) {
  1913. inject->tool.sample = perf_event__inject_buildid;
  1914. } else if (inject->sched_stat) {
  1915. struct evsel *evsel;
  1916. evlist__for_each_entry(session->evlist, evsel) {
  1917. const char *name = evsel__name(evsel);
  1918. if (!strcmp(name, "sched:sched_switch")) {
  1919. if (evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
  1920. return -EINVAL;
  1921. evsel->handler = perf_inject__sched_switch;
  1922. } else if (!strcmp(name, "sched:sched_process_exit"))
  1923. evsel->handler = perf_inject__sched_process_exit;
  1924. #ifdef HAVE_LIBTRACEEVENT
  1925. else if (!strncmp(name, "sched:sched_stat_", 17))
  1926. evsel->handler = perf_inject__sched_stat;
  1927. #endif
  1928. }
  1929. } else if (inject->itrace_synth_opts.vm_time_correlation) {
  1930. session->itrace_synth_opts = &inject->itrace_synth_opts;
  1931. memset(&inject->tool, 0, sizeof(inject->tool));
  1932. inject->tool.id_index = perf_event__process_id_index;
  1933. inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
  1934. inject->tool.auxtrace = perf_event__process_auxtrace;
  1935. inject->tool.auxtrace_error = perf_event__process_auxtrace_error;
  1936. inject->tool.ordered_events = true;
  1937. inject->tool.ordering_requires_timestamps = true;
  1938. } else if (inject->itrace_synth_opts.set) {
  1939. session->itrace_synth_opts = &inject->itrace_synth_opts;
  1940. inject->itrace_synth_opts.inject = true;
  1941. inject->tool.comm = perf_event__repipe_comm;
  1942. inject->tool.namespaces = perf_event__repipe_namespaces;
  1943. inject->tool.exit = perf_event__repipe_exit;
  1944. inject->tool.id_index = perf_event__process_id_index;
  1945. inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
  1946. inject->tool.auxtrace = perf_event__process_auxtrace;
  1947. inject->tool.aux = perf_event__drop_aux;
  1948. inject->tool.itrace_start = perf_event__drop_aux;
  1949. inject->tool.aux_output_hw_id = perf_event__drop_aux;
  1950. inject->tool.ordered_events = true;
  1951. inject->tool.ordering_requires_timestamps = true;
  1952. /* Allow space in the header for new attributes */
  1953. output_data_offset = roundup(8192 + session->header.data_offset, 4096);
  1954. if (inject->strip)
  1955. strip_init(inject);
  1956. } else if (gs->perf_data_file) {
  1957. char *name = gs->perf_data_file;
  1958. /*
  1959. * Not strictly necessary, but keep these events in order wrt
  1960. * guest events.
  1961. */
  1962. inject->tool.mmap = host__repipe;
  1963. inject->tool.mmap2 = host__repipe;
  1964. inject->tool.comm = host__repipe;
  1965. inject->tool.fork = host__repipe;
  1966. inject->tool.exit = host__repipe;
  1967. inject->tool.lost = host__repipe;
  1968. inject->tool.context_switch = host__repipe;
  1969. inject->tool.ksymbol = host__repipe;
  1970. inject->tool.text_poke = host__repipe;
  1971. /*
  1972. * Once the host session has initialized, set up sample ID
  1973. * mapping and feed in guest attrs, build IDs and initial
  1974. * events.
  1975. */
  1976. inject->tool.finished_init = host__finished_init;
  1977. /* Obey finished round ordering */
  1978. inject->tool.finished_round = host__finished_round;
  1979. /* Keep track of which CPU a VCPU is runnng on */
  1980. inject->tool.context_switch = host__context_switch;
  1981. /*
  1982. * Must order events to be able to obey finished round
  1983. * ordering.
  1984. */
  1985. inject->tool.ordered_events = true;
  1986. inject->tool.ordering_requires_timestamps = true;
  1987. /* Set up a separate session to process guest perf.data file */
  1988. ret = guest_session__start(gs, name, session->data->force);
  1989. if (ret) {
  1990. pr_err("Failed to process %s, error %d\n", name, ret);
  1991. return ret;
  1992. }
  1993. /* Allow space in the header for guest attributes */
  1994. output_data_offset += gs->session->header.data_offset;
  1995. output_data_offset = roundup(output_data_offset, 4096);
  1996. } else if (inject->convert_callchain) {
  1997. inject->tool.sample = perf_event__convert_sample_callchain;
  1998. inject->tool.fork = perf_event__repipe_fork;
  1999. inject->tool.comm = perf_event__repipe_comm;
  2000. inject->tool.exit = perf_event__repipe_exit;
  2001. inject->tool.mmap = perf_event__repipe_mmap;
  2002. inject->tool.mmap2 = perf_event__repipe_mmap2;
  2003. inject->tool.ordered_events = true;
  2004. inject->tool.ordering_requires_timestamps = true;
  2005. }
  2006. if (!inject->itrace_synth_opts.set)
  2007. auxtrace_index__free(&session->auxtrace_index);
  2008. if (!inject->output.is_pipe && !inject->in_place_update)
  2009. lseek(fd, output_data_offset, SEEK_SET);
  2010. ret = perf_session__process_events(session);
  2011. if (ret)
  2012. return ret;
  2013. if (gs->session) {
  2014. /*
  2015. * Remaining guest events have later timestamps. Flush them
  2016. * out to file.
  2017. */
  2018. ret = guest_session__flush_events(gs);
  2019. if (ret) {
  2020. pr_err("Failed to flush guest events\n");
  2021. return ret;
  2022. }
  2023. }
  2024. if (!inject->output.is_pipe && !inject->in_place_update) {
  2025. struct inject_fc inj_fc = {
  2026. .fc.copy = feat_copy_cb,
  2027. .inject = inject,
  2028. };
  2029. if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
  2030. inject->build_id_style == BID_RWS__INJECT_HEADER_ALL)
  2031. perf_header__set_feat(&session->header, HEADER_BUILD_ID);
  2032. /*
  2033. * Keep all buildids when there is unprocessed AUX data because
  2034. * it is not known which ones the AUX trace hits.
  2035. */
  2036. if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
  2037. inject->have_auxtrace && !inject->itrace_synth_opts.set)
  2038. perf_session__dsos_hit_all(session);
  2039. /*
  2040. * The AUX areas have been removed and replaced with
  2041. * synthesized hardware events, so clear the feature flag.
  2042. */
  2043. if (inject->itrace_synth_opts.set) {
  2044. perf_header__clear_feat(&session->header,
  2045. HEADER_AUXTRACE);
  2046. if (inject->itrace_synth_opts.last_branch ||
  2047. inject->itrace_synth_opts.add_last_branch)
  2048. perf_header__set_feat(&session->header,
  2049. HEADER_BRANCH_STACK);
  2050. }
  2051. /*
  2052. * The converted data file won't have stack and registers.
  2053. * Update the perf_event_attr to remove them before writing.
  2054. */
  2055. if (inject->convert_callchain) {
  2056. struct evsel *evsel;
  2057. evlist__for_each_entry(session->evlist, evsel) {
  2058. evsel__reset_sample_bit(evsel, REGS_USER);
  2059. evsel__reset_sample_bit(evsel, STACK_USER);
  2060. evsel->core.attr.sample_regs_user = 0;
  2061. evsel->core.attr.sample_stack_user = 0;
  2062. evsel->core.attr.exclude_callchain_user = 0;
  2063. }
  2064. }
  2065. session->header.data_offset = output_data_offset;
  2066. session->header.data_size = inject->bytes_written;
  2067. perf_session__inject_header(session, session->evlist, fd, &inj_fc.fc,
  2068. write_attrs_after_data);
  2069. if (inject->copy_kcore_dir) {
  2070. ret = copy_kcore_dir(inject);
  2071. if (ret) {
  2072. pr_err("Failed to copy kcore\n");
  2073. return ret;
  2074. }
  2075. }
  2076. if (gs->copy_kcore_dir) {
  2077. ret = guest_session__copy_kcore_dir(gs);
  2078. if (ret) {
  2079. pr_err("Failed to copy guest kcore\n");
  2080. return ret;
  2081. }
  2082. }
  2083. }
  2084. return ret;
  2085. }
  2086. static bool evsel__has_dwarf_callchain(struct evsel *evsel)
  2087. {
  2088. struct perf_event_attr *attr = &evsel->core.attr;
  2089. const u64 dwarf_callchain_flags =
  2090. PERF_SAMPLE_STACK_USER | PERF_SAMPLE_REGS_USER | PERF_SAMPLE_CALLCHAIN;
  2091. if (!attr->exclude_callchain_user)
  2092. return false;
  2093. return (attr->sample_type & dwarf_callchain_flags) == dwarf_callchain_flags;
  2094. }
  2095. int cmd_inject(int argc, const char **argv)
  2096. {
  2097. struct perf_inject inject = {
  2098. .input_name = "-",
  2099. .samples = LIST_HEAD_INIT(inject.samples),
  2100. .output = {
  2101. .path = "-",
  2102. .mode = PERF_DATA_MODE_WRITE,
  2103. .use_stdio = true,
  2104. },
  2105. };
  2106. struct perf_data data = {
  2107. .mode = PERF_DATA_MODE_READ,
  2108. .use_stdio = true,
  2109. };
  2110. int ret;
  2111. const char *known_build_ids = NULL;
  2112. bool build_ids = false;
  2113. bool build_id_all = false;
  2114. bool mmap2_build_ids = false;
  2115. bool mmap2_build_id_all = false;
  2116. struct option options[] = {
  2117. OPT_BOOLEAN('b', "build-ids", &build_ids,
  2118. "Inject build-ids into the output stream"),
  2119. OPT_BOOLEAN(0, "buildid-all", &build_id_all,
  2120. "Inject build-ids of all DSOs into the output stream"),
  2121. OPT_BOOLEAN('B', "mmap2-buildids", &mmap2_build_ids,
  2122. "Drop unused mmap events, make others mmap2 with build IDs"),
  2123. OPT_BOOLEAN(0, "mmap2-buildid-all", &mmap2_build_id_all,
  2124. "Rewrite all mmap events as mmap2 events with build IDs"),
  2125. OPT_STRING(0, "known-build-ids", &known_build_ids,
  2126. "buildid path [,buildid path...]",
  2127. "build-ids to use for given paths"),
  2128. OPT_STRING('i', "input", &inject.input_name, "file",
  2129. "input file name"),
  2130. OPT_STRING('o', "output", &inject.output.path, "file",
  2131. "output file name"),
  2132. OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
  2133. "Merge sched-stat and sched-switch for getting events "
  2134. "where and how long tasks slept"),
  2135. #ifdef HAVE_JITDUMP
  2136. OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
  2137. #endif
  2138. OPT_INCR('v', "verbose", &verbose,
  2139. "be more verbose (show build ids, etc)"),
  2140. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  2141. "file", "vmlinux pathname"),
  2142. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  2143. "don't load vmlinux even if found"),
  2144. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
  2145. "kallsyms pathname"),
  2146. OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
  2147. OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
  2148. NULL, "opts", "Instruction Tracing options\n"
  2149. ITRACE_HELP,
  2150. itrace_parse_synth_opts),
  2151. OPT_BOOLEAN(0, "strip", &inject.strip,
  2152. "strip non-synthesized events (use with --itrace)"),
  2153. OPT_CALLBACK_OPTARG(0, "vm-time-correlation", &inject, NULL, "opts",
  2154. "correlate time between VM guests and the host",
  2155. parse_vm_time_correlation),
  2156. OPT_CALLBACK_OPTARG(0, "guest-data", &inject, NULL, "opts",
  2157. "inject events from a guest perf.data file",
  2158. parse_guest_data),
  2159. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  2160. "guest mount directory under which every guest os"
  2161. " instance has a subdir"),
  2162. OPT_BOOLEAN(0, "convert-callchain", &inject.convert_callchain,
  2163. "Generate callchains using DWARF and drop register/stack data"),
  2164. OPT_END()
  2165. };
  2166. const char * const inject_usage[] = {
  2167. "perf inject [<options>]",
  2168. NULL
  2169. };
  2170. bool ordered_events;
  2171. if (!inject.itrace_synth_opts.set) {
  2172. /* Disable eager loading of kernel symbols that adds overhead to perf inject. */
  2173. symbol_conf.lazy_load_kernel_maps = true;
  2174. }
  2175. #ifndef HAVE_JITDUMP
  2176. set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
  2177. #endif
  2178. #ifndef HAVE_LIBDW_SUPPORT
  2179. set_option_nobuild(options, 0, "convert-callchain", "NO_LIBDW=1", true);
  2180. #endif
  2181. argc = parse_options(argc, argv, options, inject_usage, 0);
  2182. /*
  2183. * Any (unrecognized) arguments left?
  2184. */
  2185. if (argc)
  2186. usage_with_options(inject_usage, options);
  2187. if (inject.strip && !inject.itrace_synth_opts.set) {
  2188. pr_err("--strip option requires --itrace option\n");
  2189. return -1;
  2190. }
  2191. if (symbol__validate_sym_arguments())
  2192. return -1;
  2193. if (inject.in_place_update) {
  2194. if (!strcmp(inject.input_name, "-")) {
  2195. pr_err("Input file name required for in-place updating\n");
  2196. return -1;
  2197. }
  2198. if (strcmp(inject.output.path, "-")) {
  2199. pr_err("Output file name must not be specified for in-place updating\n");
  2200. return -1;
  2201. }
  2202. if (!data.force && !inject.in_place_update_dry_run) {
  2203. pr_err("The input file would be updated in place, "
  2204. "the --force option is required.\n");
  2205. return -1;
  2206. }
  2207. if (!inject.in_place_update_dry_run)
  2208. data.in_place_update = true;
  2209. } else {
  2210. if (strcmp(inject.output.path, "-") && !inject.strip &&
  2211. has_kcore_dir(inject.input_name)) {
  2212. inject.output.is_dir = true;
  2213. inject.copy_kcore_dir = true;
  2214. }
  2215. if (perf_data__open(&inject.output)) {
  2216. perror("failed to create output file");
  2217. return -1;
  2218. }
  2219. }
  2220. if (mmap2_build_ids)
  2221. inject.build_id_style = BID_RWS__MMAP2_BUILDID_LAZY;
  2222. if (mmap2_build_id_all)
  2223. inject.build_id_style = BID_RWS__MMAP2_BUILDID_ALL;
  2224. if (build_ids)
  2225. inject.build_id_style = BID_RWS__INJECT_HEADER_LAZY;
  2226. if (build_id_all)
  2227. inject.build_id_style = BID_RWS__INJECT_HEADER_ALL;
  2228. data.path = inject.input_name;
  2229. ordered_events = inject.jit_mode || inject.sched_stat ||
  2230. inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
  2231. inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY;
  2232. perf_tool__init(&inject.tool, ordered_events);
  2233. inject.tool.sample = perf_event__repipe_sample;
  2234. inject.tool.read = perf_event__repipe_sample;
  2235. inject.tool.mmap = perf_event__repipe;
  2236. inject.tool.mmap2 = perf_event__repipe;
  2237. inject.tool.comm = perf_event__repipe;
  2238. inject.tool.namespaces = perf_event__repipe;
  2239. inject.tool.cgroup = perf_event__repipe;
  2240. inject.tool.fork = perf_event__repipe;
  2241. inject.tool.exit = perf_event__repipe;
  2242. inject.tool.lost = perf_event__repipe;
  2243. inject.tool.lost_samples = perf_event__repipe;
  2244. inject.tool.aux = perf_event__repipe;
  2245. inject.tool.itrace_start = perf_event__repipe;
  2246. inject.tool.aux_output_hw_id = perf_event__repipe;
  2247. inject.tool.context_switch = perf_event__repipe;
  2248. inject.tool.throttle = perf_event__repipe;
  2249. inject.tool.unthrottle = perf_event__repipe;
  2250. inject.tool.ksymbol = perf_event__repipe;
  2251. inject.tool.bpf = perf_event__repipe;
  2252. inject.tool.text_poke = perf_event__repipe;
  2253. inject.tool.attr = perf_event__repipe_attr;
  2254. inject.tool.event_update = perf_event__repipe_event_update;
  2255. inject.tool.tracing_data = perf_event__repipe_op2_synth;
  2256. inject.tool.finished_round = perf_event__repipe_oe_synth;
  2257. inject.tool.build_id = perf_event__repipe_op2_synth;
  2258. inject.tool.id_index = perf_event__repipe_op2_synth;
  2259. inject.tool.auxtrace_info = perf_event__repipe_op2_synth;
  2260. inject.tool.auxtrace_error = perf_event__repipe_op2_synth;
  2261. inject.tool.time_conv = perf_event__repipe_op2_synth;
  2262. inject.tool.thread_map = perf_event__repipe_op2_synth;
  2263. inject.tool.cpu_map = perf_event__repipe_op2_synth;
  2264. inject.tool.stat_config = perf_event__repipe_op2_synth;
  2265. inject.tool.stat = perf_event__repipe_op2_synth;
  2266. inject.tool.stat_round = perf_event__repipe_op2_synth;
  2267. inject.tool.feature = perf_event__repipe_op2_synth;
  2268. inject.tool.finished_init = perf_event__repipe_op2_synth;
  2269. inject.tool.compressed = perf_event__repipe_op4_synth;
  2270. inject.tool.auxtrace = perf_event__repipe_auxtrace;
  2271. inject.tool.bpf_metadata = perf_event__repipe_op2_synth;
  2272. inject.tool.schedstat_cpu = perf_event__repipe_op2_synth;
  2273. inject.tool.schedstat_domain = perf_event__repipe_op2_synth;
  2274. inject.tool.dont_split_sample_group = true;
  2275. inject.tool.merge_deferred_callchains = false;
  2276. inject.session = __perf_session__new(&data, &inject.tool,
  2277. /*trace_event_repipe=*/inject.output.is_pipe,
  2278. /*host_env=*/NULL);
  2279. if (IS_ERR(inject.session)) {
  2280. ret = PTR_ERR(inject.session);
  2281. goto out_close_output;
  2282. }
  2283. if (zstd_init(&(inject.session->zstd_data), 0) < 0)
  2284. pr_warning("Decompression initialization failed.\n");
  2285. /* Save original section info before feature bits change */
  2286. ret = save_section_info(&inject);
  2287. if (ret)
  2288. goto out_delete;
  2289. if (inject.output.is_pipe) {
  2290. ret = perf_header__write_pipe(perf_data__fd(&inject.output));
  2291. if (ret < 0) {
  2292. pr_err("Couldn't write a new pipe header.\n");
  2293. goto out_delete;
  2294. }
  2295. /*
  2296. * If the input is already a pipe then the features and
  2297. * attributes don't need synthesizing, they will be present in
  2298. * the input.
  2299. */
  2300. if (!data.is_pipe) {
  2301. ret = perf_event__synthesize_for_pipe(&inject.tool,
  2302. inject.session,
  2303. &inject.output,
  2304. perf_event__repipe);
  2305. if (ret < 0)
  2306. goto out_delete;
  2307. }
  2308. }
  2309. if (inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
  2310. inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) {
  2311. /*
  2312. * to make sure the mmap records are ordered correctly
  2313. * and so that the correct especially due to jitted code
  2314. * mmaps. We cannot generate the buildid hit list and
  2315. * inject the jit mmaps at the same time for now.
  2316. */
  2317. inject.tool.ordering_requires_timestamps = true;
  2318. }
  2319. if (inject.build_id_style != BID_RWS__NONE && known_build_ids != NULL) {
  2320. inject.known_build_ids =
  2321. perf_inject__parse_known_build_ids(known_build_ids);
  2322. if (inject.known_build_ids == NULL) {
  2323. pr_err("Couldn't parse known build ids.\n");
  2324. goto out_delete;
  2325. }
  2326. }
  2327. if (inject.convert_callchain) {
  2328. struct evsel *evsel;
  2329. if (inject.output.is_pipe || inject.session->data->is_pipe) {
  2330. pr_err("--convert-callchain cannot work with pipe\n");
  2331. goto out_delete;
  2332. }
  2333. evlist__for_each_entry(inject.session->evlist, evsel) {
  2334. if (!evsel__has_dwarf_callchain(evsel) && !evsel__is_dummy_event(evsel)) {
  2335. pr_err("--convert-callchain requires DWARF call graph.\n");
  2336. goto out_delete;
  2337. }
  2338. }
  2339. inject.raw_callchain = calloc(PERF_MAX_STACK_DEPTH, sizeof(u64));
  2340. if (inject.raw_callchain == NULL) {
  2341. pr_err("callchain allocation failed\n");
  2342. goto out_delete;
  2343. }
  2344. }
  2345. #ifdef HAVE_JITDUMP
  2346. if (inject.jit_mode) {
  2347. inject.tool.mmap2 = perf_event__repipe_mmap2;
  2348. inject.tool.mmap = perf_event__repipe_mmap;
  2349. inject.tool.ordering_requires_timestamps = true;
  2350. /*
  2351. * JIT MMAP injection injects all MMAP events in one go, so it
  2352. * does not obey finished_round semantics.
  2353. */
  2354. inject.tool.finished_round = perf_event__drop_oe;
  2355. }
  2356. #endif
  2357. ret = symbol__init(perf_session__env(inject.session));
  2358. if (ret < 0)
  2359. goto out_delete;
  2360. ret = __cmd_inject(&inject);
  2361. guest_session__exit(&inject.guest_session);
  2362. out_delete:
  2363. strlist__delete(inject.known_build_ids);
  2364. zstd_fini(&(inject.session->zstd_data));
  2365. perf_session__delete(inject.session);
  2366. out_close_output:
  2367. if (!inject.in_place_update)
  2368. perf_data__close(&inject.output);
  2369. free(inject.itrace_synth_opts.vm_tm_corr_args);
  2370. free(inject.event_copy);
  2371. free(inject.guest_session.ev.event_buf);
  2372. free(inject.raw_callchain);
  2373. return ret;
  2374. }