machine.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <inttypes.h>
  5. #include <regex.h>
  6. #include <stdlib.h>
  7. #include "callchain.h"
  8. #include "debug.h"
  9. #include "dso.h"
  10. #include "env.h"
  11. #include "event.h"
  12. #include "evsel.h"
  13. #include "hist.h"
  14. #include "machine.h"
  15. #include "map.h"
  16. #include "map_symbol.h"
  17. #include "branch.h"
  18. #include "mem-events.h"
  19. #include "mem-info.h"
  20. #include "path.h"
  21. #include "srcline.h"
  22. #include "symbol.h"
  23. #include "synthetic-events.h"
  24. #include "sort.h"
  25. #include "strlist.h"
  26. #include "target.h"
  27. #include "thread.h"
  28. #include "util.h"
  29. #include "vdso.h"
  30. #include <stdbool.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. #include "unwind.h"
  35. #include "linux/hash.h"
  36. #include "asm/bug.h"
  37. #include "bpf-event.h"
  38. #include <internal/lib.h> // page_size
  39. #include "cgroup.h"
  40. #include "arm64-frame-pointer-unwind-support.h"
  41. #include <api/io_dir.h>
  42. #include <linux/ctype.h>
  43. #include <symbol/kallsyms.h>
  44. #include <linux/mman.h>
  45. #include <linux/string.h>
  46. #include <linux/zalloc.h>
  47. static struct dso *machine__kernel_dso(struct machine *machine)
  48. {
  49. return map__dso(machine->vmlinux_map);
  50. }
  51. static int machine__set_mmap_name(struct machine *machine)
  52. {
  53. if (machine__is_host(machine))
  54. machine->mmap_name = strdup("[kernel.kallsyms]");
  55. else if (machine__is_default_guest(machine))
  56. machine->mmap_name = strdup("[guest.kernel.kallsyms]");
  57. else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
  58. machine->pid) < 0)
  59. machine->mmap_name = NULL;
  60. return machine->mmap_name ? 0 : -ENOMEM;
  61. }
  62. static void thread__set_guest_comm(struct thread *thread, pid_t pid)
  63. {
  64. char comm[64];
  65. snprintf(comm, sizeof(comm), "[guest/%d]", pid);
  66. thread__set_comm(thread, comm, 0);
  67. }
  68. int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
  69. {
  70. int err = -ENOMEM;
  71. memset(machine, 0, sizeof(*machine));
  72. machine->kmaps = maps__new(machine);
  73. if (machine->kmaps == NULL)
  74. return -ENOMEM;
  75. RB_CLEAR_NODE(&machine->rb_node);
  76. dsos__init(&machine->dsos);
  77. threads__init(&machine->threads);
  78. machine->vdso_info = NULL;
  79. machine->env = NULL;
  80. machine->pid = pid;
  81. machine->id_hdr_size = 0;
  82. machine->kptr_restrict_warned = false;
  83. machine->comm_exec = false;
  84. machine->kernel_start = 0;
  85. machine->vmlinux_map = NULL;
  86. /* There is no initial context switch in, so we start at 1. */
  87. machine->parallelism = 1;
  88. machine->root_dir = strdup(root_dir);
  89. if (machine->root_dir == NULL)
  90. goto out;
  91. if (machine__set_mmap_name(machine))
  92. goto out;
  93. if (pid != HOST_KERNEL_ID) {
  94. struct thread *thread = machine__findnew_thread(machine, -1,
  95. pid);
  96. if (thread == NULL)
  97. goto out;
  98. thread__set_guest_comm(thread, pid);
  99. thread__put(thread);
  100. }
  101. machine->current_tid = NULL;
  102. err = 0;
  103. out:
  104. if (err) {
  105. zfree(&machine->kmaps);
  106. zfree(&machine->root_dir);
  107. zfree(&machine->mmap_name);
  108. }
  109. return 0;
  110. }
  111. static struct machine *__machine__new_host(struct perf_env *host_env, bool kernel_maps)
  112. {
  113. struct machine *machine = malloc(sizeof(*machine));
  114. if (!machine)
  115. return NULL;
  116. machine__init(machine, "", HOST_KERNEL_ID);
  117. if (kernel_maps && machine__create_kernel_maps(machine) < 0) {
  118. free(machine);
  119. return NULL;
  120. }
  121. machine->env = host_env;
  122. return machine;
  123. }
  124. struct machine *machine__new_host(struct perf_env *host_env)
  125. {
  126. return __machine__new_host(host_env, /*kernel_maps=*/true);
  127. }
  128. static int mmap_handler(const struct perf_tool *tool __maybe_unused,
  129. union perf_event *event,
  130. struct perf_sample *sample,
  131. struct machine *machine)
  132. {
  133. return machine__process_mmap2_event(machine, event, sample);
  134. }
  135. static int machine__init_live(struct machine *machine, pid_t pid)
  136. {
  137. union perf_event event;
  138. memset(&event, 0, sizeof(event));
  139. return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
  140. mmap_handler, machine, true);
  141. }
  142. struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid)
  143. {
  144. struct machine *machine = __machine__new_host(host_env, kernel_maps);
  145. if (!machine)
  146. return NULL;
  147. if (machine__init_live(machine, pid)) {
  148. machine__delete(machine);
  149. return NULL;
  150. }
  151. return machine;
  152. }
  153. struct machine *machine__new_kallsyms(struct perf_env *host_env)
  154. {
  155. struct machine *machine = machine__new_host(host_env);
  156. /*
  157. * FIXME:
  158. * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
  159. * ask for not using the kcore parsing code, once this one is fixed
  160. * to create a map per module.
  161. */
  162. if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
  163. machine__delete(machine);
  164. machine = NULL;
  165. }
  166. return machine;
  167. }
  168. void machine__delete_threads(struct machine *machine)
  169. {
  170. threads__remove_all_threads(&machine->threads);
  171. }
  172. void machine__exit(struct machine *machine)
  173. {
  174. if (machine == NULL)
  175. return;
  176. machine__destroy_kernel_maps(machine);
  177. maps__zput(machine->kmaps);
  178. dsos__exit(&machine->dsos);
  179. machine__exit_vdso(machine);
  180. zfree(&machine->root_dir);
  181. zfree(&machine->mmap_name);
  182. zfree(&machine->current_tid);
  183. zfree(&machine->kallsyms_filename);
  184. threads__exit(&machine->threads);
  185. }
  186. void machine__delete(struct machine *machine)
  187. {
  188. if (machine) {
  189. machine__exit(machine);
  190. free(machine);
  191. }
  192. }
  193. void machines__init(struct machines *machines)
  194. {
  195. machine__init(&machines->host, "", HOST_KERNEL_ID);
  196. machines->guests = RB_ROOT_CACHED;
  197. }
  198. void machines__exit(struct machines *machines)
  199. {
  200. machine__exit(&machines->host);
  201. /* XXX exit guest */
  202. }
  203. struct machine *machines__add(struct machines *machines, pid_t pid,
  204. const char *root_dir)
  205. {
  206. struct rb_node **p = &machines->guests.rb_root.rb_node;
  207. struct rb_node *parent = NULL;
  208. struct machine *pos, *machine = malloc(sizeof(*machine));
  209. bool leftmost = true;
  210. if (machine == NULL)
  211. return NULL;
  212. if (machine__init(machine, root_dir, pid) != 0) {
  213. free(machine);
  214. return NULL;
  215. }
  216. while (*p != NULL) {
  217. parent = *p;
  218. pos = rb_entry(parent, struct machine, rb_node);
  219. if (pid < pos->pid)
  220. p = &(*p)->rb_left;
  221. else {
  222. p = &(*p)->rb_right;
  223. leftmost = false;
  224. }
  225. }
  226. rb_link_node(&machine->rb_node, parent, p);
  227. rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
  228. machine->machines = machines;
  229. return machine;
  230. }
  231. void machines__set_comm_exec(struct machines *machines, bool comm_exec)
  232. {
  233. struct rb_node *nd;
  234. machines->host.comm_exec = comm_exec;
  235. for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
  236. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  237. machine->comm_exec = comm_exec;
  238. }
  239. }
  240. struct machine *machines__find(struct machines *machines, pid_t pid)
  241. {
  242. struct rb_node **p = &machines->guests.rb_root.rb_node;
  243. struct rb_node *parent = NULL;
  244. struct machine *machine;
  245. struct machine *default_machine = NULL;
  246. if (pid == HOST_KERNEL_ID)
  247. return &machines->host;
  248. while (*p != NULL) {
  249. parent = *p;
  250. machine = rb_entry(parent, struct machine, rb_node);
  251. if (pid < machine->pid)
  252. p = &(*p)->rb_left;
  253. else if (pid > machine->pid)
  254. p = &(*p)->rb_right;
  255. else
  256. return machine;
  257. if (!machine->pid)
  258. default_machine = machine;
  259. }
  260. return default_machine;
  261. }
  262. struct machine *machines__findnew(struct machines *machines, pid_t pid)
  263. {
  264. char path[PATH_MAX];
  265. const char *root_dir = "";
  266. struct machine *machine = machines__find(machines, pid);
  267. if (machine && (machine->pid == pid))
  268. goto out;
  269. if ((pid != HOST_KERNEL_ID) &&
  270. (pid != DEFAULT_GUEST_KERNEL_ID) &&
  271. (symbol_conf.guestmount)) {
  272. sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
  273. if (access(path, R_OK)) {
  274. static struct strlist *seen;
  275. if (!seen)
  276. seen = strlist__new(NULL, NULL);
  277. if (!strlist__has_entry(seen, path)) {
  278. pr_err("Can't access file %s\n", path);
  279. strlist__add(seen, path);
  280. }
  281. machine = NULL;
  282. goto out;
  283. }
  284. root_dir = path;
  285. }
  286. machine = machines__add(machines, pid, root_dir);
  287. out:
  288. return machine;
  289. }
  290. struct machine *machines__find_guest(struct machines *machines, pid_t pid)
  291. {
  292. struct machine *machine = machines__find(machines, pid);
  293. if (!machine)
  294. machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
  295. return machine;
  296. }
  297. /*
  298. * A common case for KVM test programs is that the test program acts as the
  299. * hypervisor, creating, running and destroying the virtual machine, and
  300. * providing the guest object code from its own object code. In this case,
  301. * the VM is not running an OS, but only the functions loaded into it by the
  302. * hypervisor test program, and conveniently, loaded at the same virtual
  303. * addresses.
  304. *
  305. * Normally to resolve addresses, MMAP events are needed to map addresses
  306. * back to the object code and debug symbols for that object code.
  307. *
  308. * Currently, there is no way to get such mapping information from guests
  309. * but, in the scenario described above, the guest has the same mappings
  310. * as the hypervisor, so support for that scenario can be achieved.
  311. *
  312. * To support that, copy the host thread's maps to the guest thread's maps.
  313. * Note, we do not discover the guest until we encounter a guest event,
  314. * which works well because it is not until then that we know that the host
  315. * thread's maps have been set up.
  316. *
  317. * This function returns the guest thread. Apart from keeping the data
  318. * structures sane, using a thread belonging to the guest machine, instead
  319. * of the host thread, allows it to have its own comm (refer
  320. * thread__set_guest_comm()).
  321. */
  322. static struct thread *findnew_guest_code(struct machine *machine,
  323. struct machine *host_machine,
  324. pid_t pid)
  325. {
  326. struct thread *host_thread;
  327. struct thread *thread;
  328. int err;
  329. if (!machine)
  330. return NULL;
  331. thread = machine__findnew_thread(machine, -1, pid);
  332. if (!thread)
  333. return NULL;
  334. /* Assume maps are set up if there are any */
  335. if (!maps__empty(thread__maps(thread)))
  336. return thread;
  337. host_thread = machine__find_thread(host_machine, -1, pid);
  338. if (!host_thread)
  339. goto out_err;
  340. thread__set_guest_comm(thread, pid);
  341. /*
  342. * Guest code can be found in hypervisor process at the same address
  343. * so copy host maps.
  344. */
  345. err = maps__copy_from(thread__maps(thread), thread__maps(host_thread));
  346. thread__put(host_thread);
  347. if (err)
  348. goto out_err;
  349. return thread;
  350. out_err:
  351. thread__zput(thread);
  352. return NULL;
  353. }
  354. struct thread *machines__findnew_guest_code(struct machines *machines, pid_t pid)
  355. {
  356. struct machine *host_machine = machines__find(machines, HOST_KERNEL_ID);
  357. struct machine *machine = machines__findnew(machines, pid);
  358. return findnew_guest_code(machine, host_machine, pid);
  359. }
  360. struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid)
  361. {
  362. struct machines *machines = machine->machines;
  363. struct machine *host_machine;
  364. if (!machines)
  365. return NULL;
  366. host_machine = machines__find(machines, HOST_KERNEL_ID);
  367. return findnew_guest_code(machine, host_machine, pid);
  368. }
  369. void machines__process_guests(struct machines *machines,
  370. machine__process_t process, void *data)
  371. {
  372. struct rb_node *nd;
  373. for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
  374. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  375. process(pos, data);
  376. }
  377. }
  378. void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
  379. {
  380. struct rb_node *node;
  381. struct machine *machine;
  382. machines->host.id_hdr_size = id_hdr_size;
  383. for (node = rb_first_cached(&machines->guests); node;
  384. node = rb_next(node)) {
  385. machine = rb_entry(node, struct machine, rb_node);
  386. machine->id_hdr_size = id_hdr_size;
  387. }
  388. return;
  389. }
  390. static void machine__update_thread_pid(struct machine *machine,
  391. struct thread *th, pid_t pid)
  392. {
  393. struct thread *leader;
  394. if (pid == thread__pid(th) || pid == -1 || thread__pid(th) != -1)
  395. return;
  396. thread__set_pid(th, pid);
  397. if (thread__pid(th) == thread__tid(th))
  398. return;
  399. leader = machine__findnew_thread(machine, thread__pid(th), thread__pid(th));
  400. if (!leader)
  401. goto out_err;
  402. if (!thread__maps(leader))
  403. thread__set_maps(leader, maps__new(machine));
  404. if (!thread__maps(leader))
  405. goto out_err;
  406. if (thread__maps(th) == thread__maps(leader))
  407. goto out_put;
  408. if (thread__maps(th)) {
  409. /*
  410. * Maps are created from MMAP events which provide the pid and
  411. * tid. Consequently there never should be any maps on a thread
  412. * with an unknown pid. Just print an error if there are.
  413. */
  414. if (!maps__empty(thread__maps(th)))
  415. pr_err("Discarding thread maps for %d:%d\n",
  416. thread__pid(th), thread__tid(th));
  417. maps__put(thread__maps(th));
  418. }
  419. thread__set_maps(th, maps__get(thread__maps(leader)));
  420. out_put:
  421. thread__put(leader);
  422. return;
  423. out_err:
  424. pr_err("Failed to join map groups for %d:%d\n", thread__pid(th), thread__tid(th));
  425. goto out_put;
  426. }
  427. /*
  428. * Caller must eventually drop thread->refcnt returned with a successful
  429. * lookup/new thread inserted.
  430. */
  431. static struct thread *__machine__findnew_thread(struct machine *machine,
  432. pid_t pid,
  433. pid_t tid,
  434. bool create)
  435. {
  436. struct thread *th = threads__find(&machine->threads, tid);
  437. bool created;
  438. if (th) {
  439. machine__update_thread_pid(machine, th, pid);
  440. return th;
  441. }
  442. if (!create)
  443. return NULL;
  444. th = threads__findnew(&machine->threads, pid, tid, &created);
  445. if (created) {
  446. /*
  447. * We have to initialize maps separately after rb tree is
  448. * updated.
  449. *
  450. * The reason is that we call machine__findnew_thread within
  451. * thread__init_maps to find the thread leader and that would
  452. * screwed the rb tree.
  453. */
  454. if (thread__init_maps(th, machine)) {
  455. pr_err("Thread init failed thread %d\n", pid);
  456. threads__remove(&machine->threads, th);
  457. thread__put(th);
  458. return NULL;
  459. }
  460. } else
  461. machine__update_thread_pid(machine, th, pid);
  462. return th;
  463. }
  464. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
  465. {
  466. return __machine__findnew_thread(machine, pid, tid, /*create=*/true);
  467. }
  468. struct thread *machine__find_thread(struct machine *machine, pid_t pid,
  469. pid_t tid)
  470. {
  471. return __machine__findnew_thread(machine, pid, tid, /*create=*/false);
  472. }
  473. /*
  474. * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
  475. * So here a single thread is created for that, but actually there is a separate
  476. * idle task per cpu, so there should be one 'struct thread' per cpu, but there
  477. * is only 1. That causes problems for some tools, requiring workarounds. For
  478. * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
  479. */
  480. struct thread *machine__idle_thread(struct machine *machine)
  481. {
  482. struct thread *thread = machine__findnew_thread(machine, 0, 0);
  483. if (!thread || thread__set_comm(thread, "swapper", 0) ||
  484. thread__set_namespaces(thread, 0, NULL))
  485. pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
  486. return thread;
  487. }
  488. struct comm *machine__thread_exec_comm(struct machine *machine,
  489. struct thread *thread)
  490. {
  491. if (machine->comm_exec)
  492. return thread__exec_comm(thread);
  493. else
  494. return thread__comm(thread);
  495. }
  496. int machine__process_comm_event(struct machine *machine, union perf_event *event,
  497. struct perf_sample *sample)
  498. {
  499. struct thread *thread = machine__findnew_thread(machine,
  500. event->comm.pid,
  501. event->comm.tid);
  502. bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
  503. int err = 0;
  504. if (exec)
  505. machine->comm_exec = true;
  506. if (dump_trace)
  507. perf_event__fprintf_comm(event, stdout);
  508. if (thread == NULL ||
  509. __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
  510. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  511. err = -1;
  512. }
  513. thread__put(thread);
  514. return err;
  515. }
  516. int machine__process_namespaces_event(struct machine *machine __maybe_unused,
  517. union perf_event *event,
  518. struct perf_sample *sample __maybe_unused)
  519. {
  520. struct thread *thread = machine__findnew_thread(machine,
  521. event->namespaces.pid,
  522. event->namespaces.tid);
  523. int err = 0;
  524. WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
  525. "\nWARNING: kernel seems to support more namespaces than perf"
  526. " tool.\nTry updating the perf tool..\n\n");
  527. WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
  528. "\nWARNING: perf tool seems to support more namespaces than"
  529. " the kernel.\nTry updating the kernel..\n\n");
  530. if (dump_trace)
  531. perf_event__fprintf_namespaces(event, stdout);
  532. if (thread == NULL ||
  533. thread__set_namespaces(thread, sample->time, &event->namespaces)) {
  534. dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
  535. err = -1;
  536. }
  537. thread__put(thread);
  538. return err;
  539. }
  540. int machine__process_cgroup_event(struct machine *machine,
  541. union perf_event *event,
  542. struct perf_sample *sample __maybe_unused)
  543. {
  544. struct cgroup *cgrp;
  545. if (dump_trace)
  546. perf_event__fprintf_cgroup(event, stdout);
  547. cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
  548. if (cgrp == NULL)
  549. return -ENOMEM;
  550. return 0;
  551. }
  552. int machine__process_lost_event(struct machine *machine __maybe_unused,
  553. union perf_event *event, struct perf_sample *sample __maybe_unused)
  554. {
  555. dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
  556. event->lost.id, event->lost.lost);
  557. return 0;
  558. }
  559. int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
  560. union perf_event *event, struct perf_sample *sample)
  561. {
  562. dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "%s\n",
  563. sample->id, event->lost_samples.lost,
  564. event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF ? " (BPF)" : "");
  565. return 0;
  566. }
  567. int machine__process_aux_event(struct machine *machine __maybe_unused,
  568. union perf_event *event)
  569. {
  570. if (dump_trace)
  571. perf_event__fprintf_aux(event, stdout);
  572. return 0;
  573. }
  574. int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
  575. union perf_event *event)
  576. {
  577. if (dump_trace)
  578. perf_event__fprintf_itrace_start(event, stdout);
  579. return 0;
  580. }
  581. int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
  582. union perf_event *event)
  583. {
  584. if (dump_trace)
  585. perf_event__fprintf_aux_output_hw_id(event, stdout);
  586. return 0;
  587. }
  588. int machine__process_switch_event(struct machine *machine __maybe_unused,
  589. union perf_event *event)
  590. {
  591. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  592. if (dump_trace)
  593. perf_event__fprintf_switch(event, stdout);
  594. machine->parallelism += out ? -1 : 1;
  595. return 0;
  596. }
  597. static int machine__process_ksymbol_register(struct machine *machine,
  598. union perf_event *event,
  599. struct perf_sample *sample __maybe_unused)
  600. {
  601. struct symbol *sym;
  602. struct dso *dso = NULL;
  603. struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
  604. int err = 0;
  605. if (!map) {
  606. dso = dso__new(event->ksymbol.name);
  607. if (!dso) {
  608. err = -ENOMEM;
  609. goto out;
  610. }
  611. dso__set_kernel(dso, DSO_SPACE__KERNEL);
  612. map = map__new2(0, dso);
  613. if (!map) {
  614. err = -ENOMEM;
  615. goto out;
  616. }
  617. if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
  618. dso__set_binary_type(dso, DSO_BINARY_TYPE__OOL);
  619. dso__data(dso)->file_size = event->ksymbol.len;
  620. dso__set_loaded(dso);
  621. }
  622. map__set_start(map, event->ksymbol.addr);
  623. map__set_end(map, map__start(map) + event->ksymbol.len);
  624. err = maps__fixup_overlap_and_insert(machine__kernel_maps(machine), map);
  625. if (err) {
  626. err = -ENOMEM;
  627. goto out;
  628. }
  629. dso__set_loaded(dso);
  630. if (is_bpf_image(event->ksymbol.name)) {
  631. dso__set_binary_type(dso, DSO_BINARY_TYPE__BPF_IMAGE);
  632. dso__set_long_name(dso, "", false);
  633. }
  634. } else {
  635. dso = dso__get(map__dso(map));
  636. }
  637. sym = symbol__new(map__map_ip(map, map__start(map)),
  638. event->ksymbol.len,
  639. 0, 0, event->ksymbol.name);
  640. if (!sym) {
  641. err = -ENOMEM;
  642. goto out;
  643. }
  644. dso__insert_symbol(dso, sym);
  645. out:
  646. map__put(map);
  647. dso__put(dso);
  648. return err;
  649. }
  650. static int machine__process_ksymbol_unregister(struct machine *machine,
  651. union perf_event *event,
  652. struct perf_sample *sample __maybe_unused)
  653. {
  654. struct symbol *sym;
  655. struct map *map;
  656. map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
  657. if (!map)
  658. return 0;
  659. if (!RC_CHK_EQUAL(map, machine->vmlinux_map))
  660. maps__remove(machine__kernel_maps(machine), map);
  661. else {
  662. struct dso *dso = map__dso(map);
  663. sym = dso__find_symbol(dso, map__map_ip(map, map__start(map)));
  664. if (sym)
  665. dso__delete_symbol(dso, sym);
  666. }
  667. map__put(map);
  668. return 0;
  669. }
  670. int machine__process_ksymbol(struct machine *machine __maybe_unused,
  671. union perf_event *event,
  672. struct perf_sample *sample)
  673. {
  674. if (dump_trace)
  675. perf_event__fprintf_ksymbol(event, stdout);
  676. /* no need to process non-JIT BPF as it cannot get samples */
  677. if (event->ksymbol.len == 0)
  678. return 0;
  679. if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
  680. return machine__process_ksymbol_unregister(machine, event,
  681. sample);
  682. return machine__process_ksymbol_register(machine, event, sample);
  683. }
  684. int machine__process_text_poke(struct machine *machine, union perf_event *event,
  685. struct perf_sample *sample __maybe_unused)
  686. {
  687. struct map *map = maps__find(machine__kernel_maps(machine), event->text_poke.addr);
  688. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  689. struct dso *dso = map ? map__dso(map) : NULL;
  690. if (dump_trace)
  691. perf_event__fprintf_text_poke(event, machine, stdout);
  692. if (!event->text_poke.new_len)
  693. goto out;
  694. if (cpumode != PERF_RECORD_MISC_KERNEL) {
  695. pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
  696. goto out;
  697. }
  698. if (dso) {
  699. u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
  700. int ret;
  701. /*
  702. * Kernel maps might be changed when loading symbols so loading
  703. * must be done prior to using kernel maps.
  704. */
  705. map__load(map);
  706. ret = dso__data_write_cache_addr(dso, map, machine,
  707. event->text_poke.addr,
  708. new_bytes,
  709. event->text_poke.new_len);
  710. if (ret != event->text_poke.new_len)
  711. pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
  712. event->text_poke.addr);
  713. } else {
  714. pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
  715. event->text_poke.addr);
  716. }
  717. out:
  718. map__put(map);
  719. return 0;
  720. }
  721. static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
  722. const char *filename)
  723. {
  724. struct map *map = NULL;
  725. struct kmod_path m;
  726. struct dso *dso;
  727. int err;
  728. if (kmod_path__parse_name(&m, filename))
  729. return NULL;
  730. dso = dsos__findnew_module_dso(&machine->dsos, machine, &m, filename);
  731. if (dso == NULL)
  732. goto out;
  733. map = map__new2(start, dso);
  734. if (map == NULL)
  735. goto out;
  736. err = maps__insert(machine__kernel_maps(machine), map);
  737. /* If maps__insert failed, return NULL. */
  738. if (err) {
  739. map__put(map);
  740. map = NULL;
  741. }
  742. out:
  743. /* put the dso here, corresponding to machine__findnew_module_dso */
  744. dso__put(dso);
  745. zfree(&m.name);
  746. return map;
  747. }
  748. size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
  749. {
  750. struct rb_node *nd;
  751. size_t ret = dsos__fprintf(&machines->host.dsos, fp);
  752. for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
  753. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  754. ret += dsos__fprintf(&pos->dsos, fp);
  755. }
  756. return ret;
  757. }
  758. size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
  759. bool (skip)(struct dso *dso, int parm), int parm)
  760. {
  761. return dsos__fprintf_buildid(&m->dsos, fp, skip, parm);
  762. }
  763. size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
  764. bool (skip)(struct dso *dso, int parm), int parm)
  765. {
  766. struct rb_node *nd;
  767. size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
  768. for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
  769. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  770. ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
  771. }
  772. return ret;
  773. }
  774. struct machine_fprintf_cb_args {
  775. FILE *fp;
  776. size_t printed;
  777. };
  778. static int machine_fprintf_cb(struct thread *thread, void *data)
  779. {
  780. struct machine_fprintf_cb_args *args = data;
  781. /* TODO: handle fprintf errors. */
  782. args->printed += thread__fprintf(thread, args->fp);
  783. return 0;
  784. }
  785. size_t machine__fprintf(struct machine *machine, FILE *fp)
  786. {
  787. struct machine_fprintf_cb_args args = {
  788. .fp = fp,
  789. .printed = 0,
  790. };
  791. size_t ret = fprintf(fp, "Threads: %zu\n", threads__nr(&machine->threads));
  792. machine__for_each_thread(machine, machine_fprintf_cb, &args);
  793. return ret + args.printed;
  794. }
  795. static struct dso *machine__get_kernel(struct machine *machine)
  796. {
  797. const char *vmlinux_name = machine->mmap_name;
  798. struct dso *kernel;
  799. if (machine__is_host(machine)) {
  800. if (symbol_conf.vmlinux_name)
  801. vmlinux_name = symbol_conf.vmlinux_name;
  802. kernel = machine__findnew_kernel(machine, vmlinux_name,
  803. "[kernel]", DSO_SPACE__KERNEL);
  804. } else {
  805. if (symbol_conf.default_guest_vmlinux_name)
  806. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  807. kernel = machine__findnew_kernel(machine, vmlinux_name,
  808. "[guest.kernel]",
  809. DSO_SPACE__KERNEL_GUEST);
  810. }
  811. if (kernel != NULL && (!dso__has_build_id(kernel)))
  812. dso__read_running_kernel_build_id(kernel, machine);
  813. return kernel;
  814. }
  815. void machine__get_kallsyms_filename(struct machine *machine, char *buf,
  816. size_t bufsz)
  817. {
  818. if (machine__is_default_guest(machine))
  819. scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
  820. else
  821. scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
  822. }
  823. const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
  824. /* Figure out the start address of kernel map from /proc/kallsyms.
  825. * Returns the name of the start symbol in *symbol_name. Pass in NULL as
  826. * symbol_name if it's not that important.
  827. */
  828. static int machine__get_running_kernel_start(struct machine *machine,
  829. const char **symbol_name,
  830. u64 *start, u64 *end)
  831. {
  832. char filename[PATH_MAX];
  833. int i, err = -1;
  834. const char *name;
  835. u64 addr = 0;
  836. machine__get_kallsyms_filename(machine, filename, PATH_MAX);
  837. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  838. return 0;
  839. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  840. err = kallsyms__get_function_start(filename, name, &addr);
  841. if (!err)
  842. break;
  843. }
  844. if (err)
  845. return -1;
  846. if (symbol_name)
  847. *symbol_name = name;
  848. *start = addr;
  849. err = kallsyms__get_symbol_start(filename, "_edata", &addr);
  850. if (err)
  851. err = kallsyms__get_symbol_start(filename, "_etext", &addr);
  852. if (!err)
  853. *end = addr;
  854. return 0;
  855. }
  856. int machine__create_extra_kernel_map(struct machine *machine,
  857. struct dso *kernel,
  858. struct extra_kernel_map *xm)
  859. {
  860. struct kmap *kmap;
  861. struct map *map;
  862. int err;
  863. map = map__new2(xm->start, kernel);
  864. if (!map)
  865. return -ENOMEM;
  866. map__set_end(map, xm->end);
  867. map__set_pgoff(map, xm->pgoff);
  868. kmap = map__kmap(map);
  869. strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
  870. err = maps__insert(machine__kernel_maps(machine), map);
  871. if (!err) {
  872. pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
  873. kmap->name, map__start(map), map__end(map));
  874. }
  875. map__put(map);
  876. return err;
  877. }
  878. static u64 find_entry_trampoline(struct dso *dso)
  879. {
  880. /* Duplicates are removed so lookup all aliases */
  881. const char *syms[] = {
  882. "_entry_trampoline",
  883. "__entry_trampoline_start",
  884. "entry_SYSCALL_64_trampoline",
  885. };
  886. struct symbol *sym = dso__first_symbol(dso);
  887. unsigned int i;
  888. for (; sym; sym = dso__next_symbol(sym)) {
  889. if (sym->binding != STB_GLOBAL)
  890. continue;
  891. for (i = 0; i < ARRAY_SIZE(syms); i++) {
  892. if (!strcmp(sym->name, syms[i]))
  893. return sym->start;
  894. }
  895. }
  896. return 0;
  897. }
  898. /*
  899. * These values can be used for kernels that do not have symbols for the entry
  900. * trampolines in kallsyms.
  901. */
  902. #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
  903. #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
  904. #define X86_64_ENTRY_TRAMPOLINE 0x6000
  905. struct machine__map_x86_64_entry_trampolines_args {
  906. struct maps *kmaps;
  907. bool found;
  908. };
  909. static int machine__map_x86_64_entry_trampolines_cb(struct map *map, void *data)
  910. {
  911. struct machine__map_x86_64_entry_trampolines_args *args = data;
  912. struct map *dest_map;
  913. struct kmap *kmap = __map__kmap(map);
  914. if (!kmap || !is_entry_trampoline(kmap->name))
  915. return 0;
  916. dest_map = maps__find(args->kmaps, map__pgoff(map));
  917. if (RC_CHK_ACCESS(dest_map) != RC_CHK_ACCESS(map))
  918. map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map)));
  919. map__put(dest_map);
  920. args->found = true;
  921. return 0;
  922. }
  923. /* Map x86_64 PTI entry trampolines */
  924. int machine__map_x86_64_entry_trampolines(struct machine *machine,
  925. struct dso *kernel)
  926. {
  927. struct machine__map_x86_64_entry_trampolines_args args = {
  928. .kmaps = machine__kernel_maps(machine),
  929. .found = false,
  930. };
  931. int nr_cpus_avail, cpu;
  932. u64 pgoff;
  933. /*
  934. * In the vmlinux case, pgoff is a virtual address which must now be
  935. * mapped to a vmlinux offset.
  936. */
  937. maps__for_each_map(args.kmaps, machine__map_x86_64_entry_trampolines_cb, &args);
  938. if (args.found || machine->trampolines_mapped)
  939. return 0;
  940. pgoff = find_entry_trampoline(kernel);
  941. if (!pgoff)
  942. return 0;
  943. nr_cpus_avail = machine__nr_cpus_avail(machine);
  944. /* Add a 1 page map for each CPU's entry trampoline */
  945. for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
  946. u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
  947. cpu * X86_64_CPU_ENTRY_AREA_SIZE +
  948. X86_64_ENTRY_TRAMPOLINE;
  949. struct extra_kernel_map xm = {
  950. .start = va,
  951. .end = va + page_size,
  952. .pgoff = pgoff,
  953. };
  954. strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
  955. if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
  956. return -1;
  957. }
  958. machine->trampolines_mapped = nr_cpus_avail;
  959. return 0;
  960. }
  961. int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
  962. struct dso *kernel __maybe_unused)
  963. {
  964. return 0;
  965. }
  966. static int
  967. __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  968. {
  969. /* In case of renewal the kernel map, destroy previous one */
  970. machine__destroy_kernel_maps(machine);
  971. map__put(machine->vmlinux_map);
  972. machine->vmlinux_map = map__new2(0, kernel);
  973. if (machine->vmlinux_map == NULL)
  974. return -ENOMEM;
  975. map__set_mapping_type(machine->vmlinux_map, MAPPING_TYPE__IDENTITY);
  976. return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
  977. }
  978. void machine__destroy_kernel_maps(struct machine *machine)
  979. {
  980. struct kmap *kmap;
  981. struct map *map = machine__kernel_map(machine);
  982. if (map == NULL)
  983. return;
  984. kmap = map__kmap(map);
  985. maps__remove(machine__kernel_maps(machine), map);
  986. if (kmap && kmap->ref_reloc_sym) {
  987. zfree((char **)&kmap->ref_reloc_sym->name);
  988. zfree(&kmap->ref_reloc_sym);
  989. }
  990. map__zput(machine->vmlinux_map);
  991. }
  992. int machines__create_guest_kernel_maps(struct machines *machines)
  993. {
  994. int ret = 0;
  995. struct dirent **namelist = NULL;
  996. int i, items = 0;
  997. char path[PATH_MAX];
  998. pid_t pid;
  999. char *endp;
  1000. if (symbol_conf.default_guest_vmlinux_name ||
  1001. symbol_conf.default_guest_modules ||
  1002. symbol_conf.default_guest_kallsyms) {
  1003. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  1004. }
  1005. if (symbol_conf.guestmount) {
  1006. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  1007. if (items <= 0)
  1008. return -ENOENT;
  1009. for (i = 0; i < items; i++) {
  1010. if (!isdigit(namelist[i]->d_name[0])) {
  1011. /* Filter out . and .. */
  1012. continue;
  1013. }
  1014. pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
  1015. if ((*endp != '\0') ||
  1016. (endp == namelist[i]->d_name) ||
  1017. (errno == ERANGE)) {
  1018. pr_debug("invalid directory (%s). Skipping.\n",
  1019. namelist[i]->d_name);
  1020. continue;
  1021. }
  1022. sprintf(path, "%s/%s/proc/kallsyms",
  1023. symbol_conf.guestmount,
  1024. namelist[i]->d_name);
  1025. ret = access(path, R_OK);
  1026. if (ret) {
  1027. pr_debug("Can't access file %s\n", path);
  1028. goto failure;
  1029. }
  1030. machines__create_kernel_maps(machines, pid);
  1031. }
  1032. failure:
  1033. free(namelist);
  1034. }
  1035. return ret;
  1036. }
  1037. void machines__destroy_kernel_maps(struct machines *machines)
  1038. {
  1039. struct rb_node *next = rb_first_cached(&machines->guests);
  1040. machine__destroy_kernel_maps(&machines->host);
  1041. while (next) {
  1042. struct machine *pos = rb_entry(next, struct machine, rb_node);
  1043. next = rb_next(&pos->rb_node);
  1044. rb_erase_cached(&pos->rb_node, &machines->guests);
  1045. machine__delete(pos);
  1046. }
  1047. }
  1048. int machines__create_kernel_maps(struct machines *machines, pid_t pid)
  1049. {
  1050. struct machine *machine = machines__findnew(machines, pid);
  1051. if (machine == NULL)
  1052. return -1;
  1053. return machine__create_kernel_maps(machine);
  1054. }
  1055. int machine__load_kallsyms(struct machine *machine, const char *filename)
  1056. {
  1057. struct map *map = machine__kernel_map(machine);
  1058. struct dso *dso = map__dso(map);
  1059. int ret = __dso__load_kallsyms(dso, filename, map, true);
  1060. if (ret > 0) {
  1061. dso__set_loaded(dso);
  1062. /*
  1063. * Since /proc/kallsyms will have multiple sessions for the
  1064. * kernel, with modules between them, fixup the end of all
  1065. * sections.
  1066. */
  1067. maps__fixup_end(machine__kernel_maps(machine));
  1068. }
  1069. return ret;
  1070. }
  1071. int machine__load_vmlinux_path(struct machine *machine)
  1072. {
  1073. struct map *map = machine__kernel_map(machine);
  1074. struct dso *dso = map__dso(map);
  1075. int ret = dso__load_vmlinux_path(dso, map);
  1076. if (ret > 0)
  1077. dso__set_loaded(dso);
  1078. return ret;
  1079. }
  1080. static char *get_kernel_version(const char *root_dir)
  1081. {
  1082. char version[PATH_MAX];
  1083. FILE *file;
  1084. char *name, *tmp;
  1085. const char *prefix = "Linux version ";
  1086. sprintf(version, "%s/proc/version", root_dir);
  1087. file = fopen(version, "r");
  1088. if (!file)
  1089. return NULL;
  1090. tmp = fgets(version, sizeof(version), file);
  1091. fclose(file);
  1092. if (!tmp)
  1093. return NULL;
  1094. name = strstr(version, prefix);
  1095. if (!name)
  1096. return NULL;
  1097. name += strlen(prefix);
  1098. tmp = strchr(name, ' ');
  1099. if (tmp)
  1100. *tmp = '\0';
  1101. return strdup(name);
  1102. }
  1103. static bool is_kmod_dso(struct dso *dso)
  1104. {
  1105. return dso__symtab_type(dso) == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  1106. dso__symtab_type(dso) == DSO_BINARY_TYPE__GUEST_KMODULE;
  1107. }
  1108. static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
  1109. {
  1110. char *long_name;
  1111. struct dso *dso;
  1112. struct map *map = maps__find_by_name(maps, m->name);
  1113. if (map == NULL)
  1114. return 0;
  1115. long_name = strdup(path);
  1116. if (long_name == NULL) {
  1117. map__put(map);
  1118. return -ENOMEM;
  1119. }
  1120. dso = map__dso(map);
  1121. dso__set_long_name(dso, long_name, true);
  1122. dso__kernel_module_get_build_id(dso, "");
  1123. /*
  1124. * Full name could reveal us kmod compression, so
  1125. * we need to update the symtab_type if needed.
  1126. */
  1127. if (m->comp && is_kmod_dso(dso)) {
  1128. dso__set_symtab_type(dso, dso__symtab_type(dso)+1);
  1129. dso__set_comp(dso, m->comp);
  1130. }
  1131. map__put(map);
  1132. return 0;
  1133. }
  1134. static int maps__set_modules_path_dir(struct maps *maps, char *path, size_t path_size, int depth)
  1135. {
  1136. struct io_dirent64 *dent;
  1137. struct io_dir iod;
  1138. size_t root_len = strlen(path);
  1139. int ret = 0;
  1140. io_dir__init(&iod, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY));
  1141. if (iod.dirfd < 0) {
  1142. pr_debug("%s: cannot open %s dir\n", __func__, path);
  1143. return -1;
  1144. }
  1145. /* Bounds check, should never happen. */
  1146. if (root_len >= path_size)
  1147. return -1;
  1148. path[root_len++] = '/';
  1149. while ((dent = io_dir__readdir(&iod)) != NULL) {
  1150. if (io_dir__is_dir(&iod, dent)) {
  1151. if (!strcmp(dent->d_name, ".") ||
  1152. !strcmp(dent->d_name, ".."))
  1153. continue;
  1154. /* Do not follow top-level source and build symlinks */
  1155. if (depth == 0) {
  1156. if (!strcmp(dent->d_name, "source") ||
  1157. !strcmp(dent->d_name, "build"))
  1158. continue;
  1159. }
  1160. /* Bounds check, should never happen. */
  1161. if (root_len + strlen(dent->d_name) >= path_size)
  1162. continue;
  1163. strcpy(path + root_len, dent->d_name);
  1164. ret = maps__set_modules_path_dir(maps, path, path_size, depth + 1);
  1165. if (ret < 0)
  1166. goto out;
  1167. } else {
  1168. struct kmod_path m;
  1169. ret = kmod_path__parse_name(&m, dent->d_name);
  1170. if (ret)
  1171. goto out;
  1172. if (m.kmod) {
  1173. /* Bounds check, should never happen. */
  1174. if (root_len + strlen(dent->d_name) < path_size) {
  1175. strcpy(path + root_len, dent->d_name);
  1176. ret = maps__set_module_path(maps, path, &m);
  1177. }
  1178. }
  1179. zfree(&m.name);
  1180. if (ret)
  1181. goto out;
  1182. }
  1183. }
  1184. out:
  1185. close(iod.dirfd);
  1186. return ret;
  1187. }
  1188. static int machine__set_modules_path(struct machine *machine)
  1189. {
  1190. char *version;
  1191. char modules_path[PATH_MAX];
  1192. version = get_kernel_version(machine->root_dir);
  1193. if (!version)
  1194. return -1;
  1195. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
  1196. machine->root_dir, version);
  1197. free(version);
  1198. return maps__set_modules_path_dir(machine__kernel_maps(machine),
  1199. modules_path, sizeof(modules_path), 0);
  1200. }
  1201. int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
  1202. u64 *size __maybe_unused,
  1203. const char *name __maybe_unused)
  1204. {
  1205. return 0;
  1206. }
  1207. static int machine__create_module(void *arg, const char *name, u64 start,
  1208. u64 size)
  1209. {
  1210. struct machine *machine = arg;
  1211. struct map *map;
  1212. if (arch__fix_module_text_start(&start, &size, name) < 0)
  1213. return -1;
  1214. map = machine__addnew_module_map(machine, start, name);
  1215. if (map == NULL)
  1216. return -1;
  1217. map__set_end(map, start + size);
  1218. dso__kernel_module_get_build_id(map__dso(map), machine->root_dir);
  1219. map__put(map);
  1220. return 0;
  1221. }
  1222. static int machine__create_modules(struct machine *machine)
  1223. {
  1224. const char *modules;
  1225. char path[PATH_MAX];
  1226. if (machine__is_default_guest(machine)) {
  1227. modules = symbol_conf.default_guest_modules;
  1228. } else {
  1229. snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
  1230. modules = path;
  1231. }
  1232. if (symbol__restricted_filename(modules, "/proc/modules"))
  1233. return -1;
  1234. if (modules__parse(modules, machine, machine__create_module))
  1235. return -1;
  1236. if (!machine__set_modules_path(machine))
  1237. return 0;
  1238. pr_debug("Problems setting modules path maps, continuing anyway...\n");
  1239. return 0;
  1240. }
  1241. static void machine__set_kernel_mmap(struct machine *machine,
  1242. u64 start, u64 end)
  1243. {
  1244. map__set_start(machine->vmlinux_map, start);
  1245. map__set_end(machine->vmlinux_map, end);
  1246. /*
  1247. * Be a bit paranoid here, some perf.data file came with
  1248. * a zero sized synthesized MMAP event for the kernel.
  1249. */
  1250. if (start == 0 && end == 0)
  1251. map__set_end(machine->vmlinux_map, ~0ULL);
  1252. }
  1253. static int machine__update_kernel_mmap(struct machine *machine,
  1254. u64 start, u64 end)
  1255. {
  1256. struct map *orig, *updated;
  1257. int err;
  1258. orig = machine->vmlinux_map;
  1259. updated = map__get(orig);
  1260. machine->vmlinux_map = updated;
  1261. maps__remove(machine__kernel_maps(machine), orig);
  1262. machine__set_kernel_mmap(machine, start, end);
  1263. err = maps__insert(machine__kernel_maps(machine), updated);
  1264. map__put(orig);
  1265. return err;
  1266. }
  1267. int machine__create_kernel_maps(struct machine *machine)
  1268. {
  1269. struct dso *kernel = machine__get_kernel(machine);
  1270. const char *name = NULL;
  1271. u64 start = 0, end = ~0ULL;
  1272. int ret;
  1273. if (kernel == NULL)
  1274. return -1;
  1275. ret = __machine__create_kernel_maps(machine, kernel);
  1276. if (ret < 0)
  1277. goto out_put;
  1278. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  1279. if (machine__is_host(machine))
  1280. pr_debug("Problems creating module maps, "
  1281. "continuing anyway...\n");
  1282. else
  1283. pr_debug("Problems creating module maps for guest %d, "
  1284. "continuing anyway...\n", machine->pid);
  1285. }
  1286. if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
  1287. if (name &&
  1288. map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
  1289. machine__destroy_kernel_maps(machine);
  1290. ret = -1;
  1291. goto out_put;
  1292. }
  1293. /*
  1294. * we have a real start address now, so re-order the kmaps
  1295. * assume it's the last in the kmaps
  1296. */
  1297. ret = machine__update_kernel_mmap(machine, start, end);
  1298. if (ret < 0)
  1299. goto out_put;
  1300. }
  1301. if (machine__create_extra_kernel_maps(machine, kernel))
  1302. pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
  1303. if (end == ~0ULL) {
  1304. /* update end address of the kernel map using adjacent module address */
  1305. struct map *next = maps__find_next_entry(machine__kernel_maps(machine),
  1306. machine__kernel_map(machine));
  1307. if (next) {
  1308. machine__set_kernel_mmap(machine, start, map__start(next));
  1309. map__put(next);
  1310. }
  1311. }
  1312. maps__fixup_end(machine__kernel_maps(machine));
  1313. out_put:
  1314. dso__put(kernel);
  1315. return ret;
  1316. }
  1317. static int machine__uses_kcore_cb(struct dso *dso, void *data __maybe_unused)
  1318. {
  1319. return dso__is_kcore(dso) ? 1 : 0;
  1320. }
  1321. static bool machine__uses_kcore(struct machine *machine)
  1322. {
  1323. return dsos__for_each_dso(&machine->dsos, machine__uses_kcore_cb, NULL) != 0 ? true : false;
  1324. }
  1325. static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
  1326. struct extra_kernel_map *xm)
  1327. {
  1328. return machine__is(machine, "x86_64") &&
  1329. is_entry_trampoline(xm->name);
  1330. }
  1331. static int machine__process_extra_kernel_map(struct machine *machine,
  1332. struct extra_kernel_map *xm)
  1333. {
  1334. struct dso *kernel = machine__kernel_dso(machine);
  1335. if (kernel == NULL)
  1336. return -1;
  1337. return machine__create_extra_kernel_map(machine, kernel, xm);
  1338. }
  1339. static int machine__process_kernel_mmap_event(struct machine *machine,
  1340. struct extra_kernel_map *xm,
  1341. struct build_id *bid)
  1342. {
  1343. enum dso_space_type dso_space;
  1344. bool is_kernel_mmap;
  1345. const char *mmap_name = machine->mmap_name;
  1346. /* If we have maps from kcore then we do not need or want any others */
  1347. if (machine__uses_kcore(machine))
  1348. return 0;
  1349. if (machine__is_host(machine))
  1350. dso_space = DSO_SPACE__KERNEL;
  1351. else
  1352. dso_space = DSO_SPACE__KERNEL_GUEST;
  1353. is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0;
  1354. if (!is_kernel_mmap && !machine__is_host(machine)) {
  1355. /*
  1356. * If the event was recorded inside the guest and injected into
  1357. * the host perf.data file, then it will match a host mmap_name,
  1358. * so try that - see machine__set_mmap_name().
  1359. */
  1360. mmap_name = "[kernel.kallsyms]";
  1361. is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0;
  1362. }
  1363. if (xm->name[0] == '/' ||
  1364. (!is_kernel_mmap && xm->name[0] == '[')) {
  1365. struct map *map = machine__addnew_module_map(machine, xm->start, xm->name);
  1366. if (map == NULL)
  1367. goto out_problem;
  1368. map__set_end(map, map__start(map) + xm->end - xm->start);
  1369. if (build_id__is_defined(bid))
  1370. dso__set_build_id(map__dso(map), bid);
  1371. map__put(map);
  1372. } else if (is_kernel_mmap) {
  1373. const char *symbol_name = xm->name + strlen(mmap_name);
  1374. /*
  1375. * Should be there already, from the build-id table in
  1376. * the header.
  1377. */
  1378. struct dso *kernel = dsos__find_kernel_dso(&machine->dsos);
  1379. if (kernel == NULL)
  1380. kernel = machine__findnew_dso(machine, machine->mmap_name);
  1381. if (kernel == NULL)
  1382. goto out_problem;
  1383. dso__set_kernel(kernel, dso_space);
  1384. if (__machine__create_kernel_maps(machine, kernel) < 0) {
  1385. dso__put(kernel);
  1386. goto out_problem;
  1387. }
  1388. if (strstr(dso__long_name(kernel), "vmlinux"))
  1389. dso__set_short_name(kernel, "[kernel.vmlinux]", false);
  1390. if (machine__update_kernel_mmap(machine, xm->start, xm->end) < 0) {
  1391. dso__put(kernel);
  1392. goto out_problem;
  1393. }
  1394. if (build_id__is_defined(bid))
  1395. dso__set_build_id(kernel, bid);
  1396. /*
  1397. * Avoid using a zero address (kptr_restrict) for the ref reloc
  1398. * symbol. Effectively having zero here means that at record
  1399. * time /proc/sys/kernel/kptr_restrict was non zero.
  1400. */
  1401. if (xm->pgoff != 0) {
  1402. map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
  1403. symbol_name,
  1404. xm->pgoff);
  1405. }
  1406. if (machine__is_default_guest(machine)) {
  1407. /*
  1408. * preload dso of guest kernel and modules
  1409. */
  1410. dso__load(kernel, machine__kernel_map(machine));
  1411. }
  1412. dso__put(kernel);
  1413. } else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
  1414. return machine__process_extra_kernel_map(machine, xm);
  1415. }
  1416. return 0;
  1417. out_problem:
  1418. return -1;
  1419. }
  1420. int machine__process_mmap2_event(struct machine *machine,
  1421. union perf_event *event,
  1422. struct perf_sample *sample)
  1423. {
  1424. struct thread *thread;
  1425. struct map *map;
  1426. struct dso_id dso_id = dso_id_empty;
  1427. int ret = 0;
  1428. if (dump_trace)
  1429. perf_event__fprintf_mmap2(event, stdout);
  1430. if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
  1431. build_id__init(&dso_id.build_id, event->mmap2.build_id, event->mmap2.build_id_size);
  1432. } else {
  1433. dso_id.maj = event->mmap2.maj;
  1434. dso_id.min = event->mmap2.min;
  1435. dso_id.ino = event->mmap2.ino;
  1436. dso_id.ino_generation = event->mmap2.ino_generation;
  1437. dso_id.mmap2_valid = true;
  1438. dso_id.mmap2_ino_generation_valid = true;
  1439. }
  1440. if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  1441. sample->cpumode == PERF_RECORD_MISC_KERNEL) {
  1442. struct extra_kernel_map xm = {
  1443. .start = event->mmap2.start,
  1444. .end = event->mmap2.start + event->mmap2.len,
  1445. .pgoff = event->mmap2.pgoff,
  1446. };
  1447. strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
  1448. ret = machine__process_kernel_mmap_event(machine, &xm, &dso_id.build_id);
  1449. if (ret < 0)
  1450. goto out_problem;
  1451. return 0;
  1452. }
  1453. thread = machine__findnew_thread(machine, event->mmap2.pid,
  1454. event->mmap2.tid);
  1455. if (thread == NULL)
  1456. goto out_problem;
  1457. map = map__new(machine, event->mmap2.start,
  1458. event->mmap2.len, event->mmap2.pgoff,
  1459. &dso_id, event->mmap2.prot,
  1460. event->mmap2.flags,
  1461. event->mmap2.filename, thread);
  1462. if (map == NULL)
  1463. goto out_problem_map;
  1464. ret = thread__insert_map(thread, map);
  1465. if (ret)
  1466. goto out_problem_insert;
  1467. thread__put(thread);
  1468. map__put(map);
  1469. return 0;
  1470. out_problem_insert:
  1471. map__put(map);
  1472. out_problem_map:
  1473. thread__put(thread);
  1474. out_problem:
  1475. dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
  1476. return 0;
  1477. }
  1478. int machine__process_mmap_event(struct machine *machine, union perf_event *event,
  1479. struct perf_sample *sample)
  1480. {
  1481. struct thread *thread;
  1482. struct map *map;
  1483. u32 prot = 0;
  1484. int ret = 0;
  1485. if (dump_trace)
  1486. perf_event__fprintf_mmap(event, stdout);
  1487. if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  1488. sample->cpumode == PERF_RECORD_MISC_KERNEL) {
  1489. struct extra_kernel_map xm = {
  1490. .start = event->mmap.start,
  1491. .end = event->mmap.start + event->mmap.len,
  1492. .pgoff = event->mmap.pgoff,
  1493. };
  1494. strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
  1495. ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
  1496. if (ret < 0)
  1497. goto out_problem;
  1498. return 0;
  1499. }
  1500. thread = machine__findnew_thread(machine, event->mmap.pid,
  1501. event->mmap.tid);
  1502. if (thread == NULL)
  1503. goto out_problem;
  1504. if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
  1505. prot = PROT_EXEC;
  1506. map = map__new(machine, event->mmap.start,
  1507. event->mmap.len, event->mmap.pgoff,
  1508. &dso_id_empty, prot, /*flags=*/0, event->mmap.filename, thread);
  1509. if (map == NULL)
  1510. goto out_problem_map;
  1511. ret = thread__insert_map(thread, map);
  1512. if (ret)
  1513. goto out_problem_insert;
  1514. thread__put(thread);
  1515. map__put(map);
  1516. return 0;
  1517. out_problem_insert:
  1518. map__put(map);
  1519. out_problem_map:
  1520. thread__put(thread);
  1521. out_problem:
  1522. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  1523. return 0;
  1524. }
  1525. void machine__remove_thread(struct machine *machine, struct thread *th)
  1526. {
  1527. return threads__remove(&machine->threads, th);
  1528. }
  1529. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  1530. struct perf_sample *sample)
  1531. {
  1532. struct thread *thread = machine__find_thread(machine,
  1533. event->fork.pid,
  1534. event->fork.tid);
  1535. struct thread *parent = machine__findnew_thread(machine,
  1536. event->fork.ppid,
  1537. event->fork.ptid);
  1538. bool do_maps_clone = true;
  1539. int err = 0;
  1540. if (dump_trace)
  1541. perf_event__fprintf_task(event, stdout);
  1542. /*
  1543. * There may be an existing thread that is not actually the parent,
  1544. * either because we are processing events out of order, or because the
  1545. * (fork) event that would have removed the thread was lost. Assume the
  1546. * latter case and continue on as best we can.
  1547. */
  1548. if (thread__pid(parent) != (pid_t)event->fork.ppid) {
  1549. dump_printf("removing erroneous parent thread %d/%d\n",
  1550. thread__pid(parent), thread__tid(parent));
  1551. machine__remove_thread(machine, parent);
  1552. thread__put(parent);
  1553. parent = machine__findnew_thread(machine, event->fork.ppid,
  1554. event->fork.ptid);
  1555. }
  1556. /* if a thread currently exists for the thread id remove it */
  1557. if (thread != NULL) {
  1558. machine__remove_thread(machine, thread);
  1559. thread__put(thread);
  1560. }
  1561. thread = machine__findnew_thread(machine, event->fork.pid,
  1562. event->fork.tid);
  1563. /*
  1564. * When synthesizing FORK events, we are trying to create thread
  1565. * objects for the already running tasks on the machine.
  1566. *
  1567. * Normally, for a kernel FORK event, we want to clone the parent's
  1568. * maps because that is what the kernel just did.
  1569. *
  1570. * But when synthesizing, this should not be done. If we do, we end up
  1571. * with overlapping maps as we process the synthesized MMAP2 events that
  1572. * get delivered shortly thereafter.
  1573. *
  1574. * Use the FORK event misc flags in an internal way to signal this
  1575. * situation, so we can elide the map clone when appropriate.
  1576. */
  1577. if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
  1578. do_maps_clone = false;
  1579. if (thread == NULL || parent == NULL ||
  1580. thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
  1581. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  1582. err = -1;
  1583. }
  1584. thread__put(thread);
  1585. thread__put(parent);
  1586. return err;
  1587. }
  1588. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  1589. struct perf_sample *sample __maybe_unused)
  1590. {
  1591. struct thread *thread = machine__find_thread(machine,
  1592. event->fork.pid,
  1593. event->fork.tid);
  1594. if (dump_trace)
  1595. perf_event__fprintf_task(event, stdout);
  1596. /* There is no context switch out before exit, so we decrement here. */
  1597. machine->parallelism--;
  1598. if (thread != NULL) {
  1599. if (symbol_conf.keep_exited_threads)
  1600. thread__set_exited(thread, /*exited=*/true);
  1601. else
  1602. machine__remove_thread(machine, thread);
  1603. }
  1604. thread__put(thread);
  1605. return 0;
  1606. }
  1607. int machine__process_event(struct machine *machine, union perf_event *event,
  1608. struct perf_sample *sample)
  1609. {
  1610. int ret;
  1611. switch (event->header.type) {
  1612. case PERF_RECORD_COMM:
  1613. ret = machine__process_comm_event(machine, event, sample); break;
  1614. case PERF_RECORD_MMAP:
  1615. ret = machine__process_mmap_event(machine, event, sample); break;
  1616. case PERF_RECORD_NAMESPACES:
  1617. ret = machine__process_namespaces_event(machine, event, sample); break;
  1618. case PERF_RECORD_CGROUP:
  1619. ret = machine__process_cgroup_event(machine, event, sample); break;
  1620. case PERF_RECORD_MMAP2:
  1621. ret = machine__process_mmap2_event(machine, event, sample); break;
  1622. case PERF_RECORD_FORK:
  1623. ret = machine__process_fork_event(machine, event, sample); break;
  1624. case PERF_RECORD_EXIT:
  1625. ret = machine__process_exit_event(machine, event, sample); break;
  1626. case PERF_RECORD_LOST:
  1627. ret = machine__process_lost_event(machine, event, sample); break;
  1628. case PERF_RECORD_AUX:
  1629. ret = machine__process_aux_event(machine, event); break;
  1630. case PERF_RECORD_ITRACE_START:
  1631. ret = machine__process_itrace_start_event(machine, event); break;
  1632. case PERF_RECORD_LOST_SAMPLES:
  1633. ret = machine__process_lost_samples_event(machine, event, sample); break;
  1634. case PERF_RECORD_SWITCH:
  1635. case PERF_RECORD_SWITCH_CPU_WIDE:
  1636. ret = machine__process_switch_event(machine, event); break;
  1637. case PERF_RECORD_KSYMBOL:
  1638. ret = machine__process_ksymbol(machine, event, sample); break;
  1639. case PERF_RECORD_BPF_EVENT:
  1640. ret = machine__process_bpf(machine, event, sample); break;
  1641. case PERF_RECORD_TEXT_POKE:
  1642. ret = machine__process_text_poke(machine, event, sample); break;
  1643. case PERF_RECORD_AUX_OUTPUT_HW_ID:
  1644. ret = machine__process_aux_output_hw_id_event(machine, event); break;
  1645. default:
  1646. ret = -1;
  1647. break;
  1648. }
  1649. return ret;
  1650. }
  1651. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  1652. {
  1653. return regexec(regex, sym->name, 0, NULL, 0) == 0;
  1654. }
  1655. static void ip__resolve_ams(struct thread *thread,
  1656. struct addr_map_symbol *ams,
  1657. u64 ip)
  1658. {
  1659. struct addr_location al;
  1660. addr_location__init(&al);
  1661. /*
  1662. * We cannot use the header.misc hint to determine whether a
  1663. * branch stack address is user, kernel, guest, hypervisor.
  1664. * Branches may straddle the kernel/user/hypervisor boundaries.
  1665. * Thus, we have to try consecutively until we find a match
  1666. * or else, the symbol is unknown
  1667. */
  1668. thread__find_cpumode_addr_location(thread, ip, /*symbols=*/true, &al);
  1669. ams->addr = ip;
  1670. ams->al_addr = al.addr;
  1671. ams->al_level = al.level;
  1672. ams->ms.thread = thread__get(al.thread);
  1673. ams->ms.sym = al.sym;
  1674. ams->ms.map = map__get(al.map);
  1675. ams->phys_addr = 0;
  1676. ams->data_page_size = 0;
  1677. addr_location__exit(&al);
  1678. }
  1679. static void ip__resolve_data(struct thread *thread,
  1680. u8 m, struct addr_map_symbol *ams,
  1681. u64 addr, u64 phys_addr, u64 daddr_page_size)
  1682. {
  1683. struct addr_location al;
  1684. addr_location__init(&al);
  1685. thread__find_symbol(thread, m, addr, &al);
  1686. ams->addr = addr;
  1687. ams->al_addr = al.addr;
  1688. ams->al_level = al.level;
  1689. ams->ms.thread = thread__get(al.thread);
  1690. ams->ms.sym = al.sym;
  1691. ams->ms.map = map__get(al.map);
  1692. ams->phys_addr = phys_addr;
  1693. ams->data_page_size = daddr_page_size;
  1694. addr_location__exit(&al);
  1695. }
  1696. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  1697. struct addr_location *al)
  1698. {
  1699. struct mem_info *mi = mem_info__new();
  1700. if (!mi)
  1701. return NULL;
  1702. ip__resolve_ams(al->thread, mem_info__iaddr(mi), sample->ip);
  1703. ip__resolve_data(al->thread, al->cpumode, mem_info__daddr(mi),
  1704. sample->addr, sample->phys_addr,
  1705. sample->data_page_size);
  1706. mem_info__data_src(mi)->val = sample->data_src;
  1707. return mi;
  1708. }
  1709. static char *callchain_srcline(struct map_symbol *ms, u64 ip)
  1710. {
  1711. struct map *map = ms->map;
  1712. char *srcline = NULL;
  1713. struct dso *dso;
  1714. if (!map || callchain_param.key == CCKEY_FUNCTION)
  1715. return srcline;
  1716. dso = map__dso(map);
  1717. srcline = srcline__tree_find(dso__srclines(dso), ip);
  1718. if (!srcline) {
  1719. bool show_sym = false;
  1720. bool show_addr = callchain_param.key == CCKEY_ADDRESS;
  1721. srcline = get_srcline(dso, map__rip_2objdump(map, ip),
  1722. ms->sym, show_sym, show_addr, ip);
  1723. srcline__tree_insert(dso__srclines(dso), ip, srcline);
  1724. }
  1725. return srcline;
  1726. }
  1727. struct iterations {
  1728. int nr_loop_iter;
  1729. u64 cycles;
  1730. };
  1731. static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip,
  1732. bool branch, struct branch_flags *flags, int nr_loop_iter,
  1733. u64 iter_cycles, u64 branch_from)
  1734. {
  1735. struct symbol *sym = ms->sym;
  1736. struct map *map = ms->map;
  1737. struct inline_node *inline_node;
  1738. struct inline_list *ilist;
  1739. struct dso *dso;
  1740. u64 addr;
  1741. int ret = 1;
  1742. struct map_symbol ilist_ms;
  1743. bool first = true;
  1744. if (!symbol_conf.inline_name || !map || !sym)
  1745. return ret;
  1746. addr = map__dso_map_ip(map, ip);
  1747. addr = map__rip_2objdump(map, addr);
  1748. dso = map__dso(map);
  1749. inline_node = inlines__tree_find(dso__inlined_nodes(dso), addr);
  1750. if (!inline_node) {
  1751. inline_node = dso__parse_addr_inlines(dso, addr, sym);
  1752. if (!inline_node)
  1753. return ret;
  1754. inlines__tree_insert(dso__inlined_nodes(dso), inline_node);
  1755. }
  1756. ilist_ms = (struct map_symbol) {
  1757. .thread = thread__get(ms->thread),
  1758. .map = map__get(map),
  1759. };
  1760. list_for_each_entry(ilist, &inline_node->val, list) {
  1761. ilist_ms.sym = ilist->symbol;
  1762. if (first) {
  1763. ret = callchain_cursor_append(cursor, ip, &ilist_ms,
  1764. branch, flags, nr_loop_iter,
  1765. iter_cycles, branch_from, ilist->srcline);
  1766. } else {
  1767. ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
  1768. NULL, 0, 0, 0, ilist->srcline);
  1769. }
  1770. first = false;
  1771. if (ret != 0)
  1772. return ret;
  1773. }
  1774. map_symbol__exit(&ilist_ms);
  1775. return ret;
  1776. }
  1777. static int add_callchain_ip(struct thread *thread,
  1778. struct callchain_cursor *cursor,
  1779. struct symbol **parent,
  1780. struct addr_location *root_al,
  1781. u8 *cpumode,
  1782. u64 ip,
  1783. bool branch,
  1784. struct branch_flags *flags,
  1785. struct iterations *iter,
  1786. u64 branch_from,
  1787. bool symbols)
  1788. {
  1789. struct map_symbol ms = {};
  1790. struct addr_location al;
  1791. int nr_loop_iter = 0, err = 0;
  1792. u64 iter_cycles = 0;
  1793. const char *srcline = NULL;
  1794. addr_location__init(&al);
  1795. al.filtered = 0;
  1796. al.sym = NULL;
  1797. al.srcline = NULL;
  1798. if (!cpumode) {
  1799. thread__find_cpumode_addr_location(thread, ip, symbols, &al);
  1800. } else {
  1801. if (ip >= PERF_CONTEXT_MAX) {
  1802. switch (ip) {
  1803. case PERF_CONTEXT_HV:
  1804. *cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1805. break;
  1806. case PERF_CONTEXT_KERNEL:
  1807. *cpumode = PERF_RECORD_MISC_KERNEL;
  1808. break;
  1809. case PERF_CONTEXT_USER:
  1810. case PERF_CONTEXT_USER_DEFERRED:
  1811. *cpumode = PERF_RECORD_MISC_USER;
  1812. break;
  1813. default:
  1814. pr_debug("invalid callchain context: "
  1815. "%"PRId64"\n", (s64) ip);
  1816. /*
  1817. * It seems the callchain is corrupted.
  1818. * Discard all.
  1819. */
  1820. callchain_cursor_reset(cursor);
  1821. err = 1;
  1822. goto out;
  1823. }
  1824. goto out;
  1825. }
  1826. if (symbols)
  1827. thread__find_symbol(thread, *cpumode, ip, &al);
  1828. else
  1829. thread__find_map(thread, *cpumode, ip, &al);
  1830. }
  1831. if (al.sym != NULL) {
  1832. if (perf_hpp_list.parent && !*parent &&
  1833. symbol__match_regex(al.sym, &parent_regex))
  1834. *parent = al.sym;
  1835. else if (have_ignore_callees && root_al &&
  1836. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1837. /* Treat this symbol as the root,
  1838. forgetting its callees. */
  1839. addr_location__copy(root_al, &al);
  1840. callchain_cursor_reset(cursor);
  1841. }
  1842. }
  1843. if (symbol_conf.hide_unresolved && al.sym == NULL)
  1844. goto out;
  1845. if (iter) {
  1846. nr_loop_iter = iter->nr_loop_iter;
  1847. iter_cycles = iter->cycles;
  1848. }
  1849. ms.thread = thread__get(al.thread);
  1850. ms.map = map__get(al.map);
  1851. ms.sym = al.sym;
  1852. if (append_inlines(cursor, &ms, ip, branch, flags, nr_loop_iter,
  1853. iter_cycles, branch_from) == 0)
  1854. goto out;
  1855. srcline = callchain_srcline(&ms, al.addr);
  1856. err = callchain_cursor_append(cursor, ip, &ms,
  1857. branch, flags, nr_loop_iter,
  1858. iter_cycles, branch_from, srcline);
  1859. out:
  1860. addr_location__exit(&al);
  1861. map_symbol__exit(&ms);
  1862. return err;
  1863. }
  1864. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  1865. struct addr_location *al)
  1866. {
  1867. unsigned int i;
  1868. const struct branch_stack *bs = sample->branch_stack;
  1869. struct branch_entry *entries = perf_sample__branch_entries(sample);
  1870. u64 *branch_stack_cntr = sample->branch_stack_cntr;
  1871. struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
  1872. if (!bi)
  1873. return NULL;
  1874. for (i = 0; i < bs->nr; i++) {
  1875. ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
  1876. ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
  1877. bi[i].flags = entries[i].flags;
  1878. if (branch_stack_cntr)
  1879. bi[i].branch_stack_cntr = branch_stack_cntr[i];
  1880. }
  1881. return bi;
  1882. }
  1883. static void save_iterations(struct iterations *iter,
  1884. struct branch_entry *be, int nr)
  1885. {
  1886. int i;
  1887. iter->nr_loop_iter++;
  1888. iter->cycles = 0;
  1889. for (i = 0; i < nr; i++)
  1890. iter->cycles += be[i].flags.cycles;
  1891. }
  1892. #define CHASHSZ 127
  1893. #define CHASHBITS 7
  1894. #define NO_ENTRY 0xff
  1895. #define PERF_MAX_BRANCH_DEPTH 127
  1896. /* Remove loops. */
  1897. static int remove_loops(struct branch_entry *l, int nr,
  1898. struct iterations *iter)
  1899. {
  1900. int i, j, off;
  1901. unsigned char chash[CHASHSZ];
  1902. memset(chash, NO_ENTRY, sizeof(chash));
  1903. BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
  1904. for (i = 0; i < nr; i++) {
  1905. int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
  1906. /* no collision handling for now */
  1907. if (chash[h] == NO_ENTRY) {
  1908. chash[h] = i;
  1909. } else if (l[chash[h]].from == l[i].from) {
  1910. bool is_loop = true;
  1911. /* check if it is a real loop */
  1912. off = 0;
  1913. for (j = chash[h]; j < i && i + off < nr; j++, off++)
  1914. if (l[j].from != l[i + off].from) {
  1915. is_loop = false;
  1916. break;
  1917. }
  1918. if (is_loop) {
  1919. j = nr - (i + off);
  1920. if (j > 0) {
  1921. save_iterations(iter + i + off,
  1922. l + i, off);
  1923. memmove(iter + i, iter + i + off,
  1924. j * sizeof(*iter));
  1925. memmove(l + i, l + i + off,
  1926. j * sizeof(*l));
  1927. }
  1928. nr -= off;
  1929. }
  1930. }
  1931. }
  1932. return nr;
  1933. }
  1934. static int lbr_callchain_add_kernel_ip(struct thread *thread,
  1935. struct callchain_cursor *cursor,
  1936. struct perf_sample *sample,
  1937. struct symbol **parent,
  1938. struct addr_location *root_al,
  1939. u64 branch_from,
  1940. bool callee, int end,
  1941. bool symbols)
  1942. {
  1943. struct ip_callchain *chain = sample->callchain;
  1944. u8 cpumode = PERF_RECORD_MISC_USER;
  1945. int err, i;
  1946. if (callee) {
  1947. for (i = 0; i < end + 1; i++) {
  1948. err = add_callchain_ip(thread, cursor, parent,
  1949. root_al, &cpumode, chain->ips[i],
  1950. false, NULL, NULL, branch_from,
  1951. symbols);
  1952. if (err)
  1953. return err;
  1954. }
  1955. return 0;
  1956. }
  1957. for (i = end; i >= 0; i--) {
  1958. err = add_callchain_ip(thread, cursor, parent,
  1959. root_al, &cpumode, chain->ips[i],
  1960. false, NULL, NULL, branch_from,
  1961. symbols);
  1962. if (err)
  1963. return err;
  1964. }
  1965. return 0;
  1966. }
  1967. static void save_lbr_cursor_node(struct thread *thread,
  1968. struct callchain_cursor *cursor,
  1969. int idx)
  1970. {
  1971. struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
  1972. if (!lbr_stitch)
  1973. return;
  1974. if (cursor->pos == cursor->nr) {
  1975. lbr_stitch->prev_lbr_cursor[idx].valid = false;
  1976. return;
  1977. }
  1978. if (!cursor->curr)
  1979. cursor->curr = cursor->first;
  1980. else
  1981. cursor->curr = cursor->curr->next;
  1982. map_symbol__exit(&lbr_stitch->prev_lbr_cursor[idx].ms);
  1983. memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
  1984. sizeof(struct callchain_cursor_node));
  1985. lbr_stitch->prev_lbr_cursor[idx].ms.thread = thread__get(cursor->curr->ms.thread);
  1986. lbr_stitch->prev_lbr_cursor[idx].ms.map = map__get(cursor->curr->ms.map);
  1987. lbr_stitch->prev_lbr_cursor[idx].valid = true;
  1988. cursor->pos++;
  1989. }
  1990. static int lbr_callchain_add_lbr_ip(struct thread *thread,
  1991. struct callchain_cursor *cursor,
  1992. struct perf_sample *sample,
  1993. struct symbol **parent,
  1994. struct addr_location *root_al,
  1995. u64 *branch_from,
  1996. bool callee,
  1997. bool symbols)
  1998. {
  1999. struct branch_stack *lbr_stack = sample->branch_stack;
  2000. struct branch_entry *entries = perf_sample__branch_entries(sample);
  2001. u8 cpumode = PERF_RECORD_MISC_USER;
  2002. int lbr_nr = lbr_stack->nr;
  2003. struct branch_flags *flags;
  2004. int err, i;
  2005. u64 ip;
  2006. /*
  2007. * The curr and pos are not used in writing session. They are cleared
  2008. * in callchain_cursor_commit() when the writing session is closed.
  2009. * Using curr and pos to track the current cursor node.
  2010. */
  2011. if (thread__lbr_stitch(thread)) {
  2012. cursor->curr = NULL;
  2013. cursor->pos = cursor->nr;
  2014. if (cursor->nr) {
  2015. cursor->curr = cursor->first;
  2016. for (i = 0; i < (int)(cursor->nr - 1); i++)
  2017. cursor->curr = cursor->curr->next;
  2018. }
  2019. }
  2020. if (callee) {
  2021. /*
  2022. * Set the (first) leaf function's IP to sample->ip (the
  2023. * location of the sample) but if not recorded use entries.to
  2024. */
  2025. if (sample->ip)
  2026. ip = sample->ip;
  2027. else
  2028. ip = entries[0].to;
  2029. flags = &entries[0].flags;
  2030. *branch_from = entries[0].from;
  2031. err = add_callchain_ip(thread, cursor, parent,
  2032. root_al, &cpumode, ip,
  2033. true, flags, NULL,
  2034. *branch_from, symbols);
  2035. if (err)
  2036. return err;
  2037. /*
  2038. * The number of cursor node increases.
  2039. * Move the current cursor node.
  2040. * But does not need to save current cursor node for entry 0.
  2041. * It's impossible to stitch the whole LBRs of previous sample.
  2042. */
  2043. if (thread__lbr_stitch(thread) && (cursor->pos != cursor->nr)) {
  2044. if (!cursor->curr)
  2045. cursor->curr = cursor->first;
  2046. else
  2047. cursor->curr = cursor->curr->next;
  2048. cursor->pos++;
  2049. }
  2050. /* Add LBR ip from entries.from one by one. */
  2051. for (i = 0; i < lbr_nr; i++) {
  2052. ip = entries[i].from;
  2053. flags = &entries[i].flags;
  2054. err = add_callchain_ip(thread, cursor, parent,
  2055. root_al, &cpumode, ip,
  2056. true, flags, NULL,
  2057. *branch_from, symbols);
  2058. if (err)
  2059. return err;
  2060. save_lbr_cursor_node(thread, cursor, i);
  2061. }
  2062. return 0;
  2063. }
  2064. /* Add LBR ip from entries.from one by one. */
  2065. for (i = lbr_nr - 1; i >= 0; i--) {
  2066. ip = entries[i].from;
  2067. flags = &entries[i].flags;
  2068. err = add_callchain_ip(thread, cursor, parent,
  2069. root_al, &cpumode, ip,
  2070. true, flags, NULL,
  2071. *branch_from, symbols);
  2072. if (err)
  2073. return err;
  2074. save_lbr_cursor_node(thread, cursor, i);
  2075. }
  2076. if (lbr_nr > 0) {
  2077. /*
  2078. * Set the (first) leaf function's IP to sample->ip (the
  2079. * location of the sample) but if not recorded use entries.to
  2080. */
  2081. if (sample->ip)
  2082. ip = sample->ip;
  2083. else
  2084. ip = entries[0].to;
  2085. flags = &entries[0].flags;
  2086. *branch_from = entries[0].from;
  2087. err = add_callchain_ip(thread, cursor, parent,
  2088. root_al, &cpumode, ip,
  2089. true, flags, NULL,
  2090. *branch_from, symbols);
  2091. if (err)
  2092. return err;
  2093. }
  2094. return 0;
  2095. }
  2096. static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
  2097. struct callchain_cursor *cursor)
  2098. {
  2099. struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
  2100. struct callchain_cursor_node *cnode;
  2101. struct stitch_list *stitch_node;
  2102. int err;
  2103. list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
  2104. cnode = &stitch_node->cursor;
  2105. err = callchain_cursor_append(cursor, cnode->ip,
  2106. &cnode->ms,
  2107. cnode->branch,
  2108. &cnode->branch_flags,
  2109. cnode->nr_loop_iter,
  2110. cnode->iter_cycles,
  2111. cnode->branch_from,
  2112. cnode->srcline);
  2113. if (err)
  2114. return err;
  2115. }
  2116. return 0;
  2117. }
  2118. static struct stitch_list *get_stitch_node(struct thread *thread)
  2119. {
  2120. struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
  2121. struct stitch_list *stitch_node;
  2122. if (!list_empty(&lbr_stitch->free_lists)) {
  2123. stitch_node = list_first_entry(&lbr_stitch->free_lists,
  2124. struct stitch_list, node);
  2125. list_del(&stitch_node->node);
  2126. return stitch_node;
  2127. }
  2128. return malloc(sizeof(struct stitch_list));
  2129. }
  2130. static bool has_stitched_lbr(struct thread *thread,
  2131. struct perf_sample *cur,
  2132. struct perf_sample *prev,
  2133. unsigned int max_lbr,
  2134. bool callee)
  2135. {
  2136. struct branch_stack *cur_stack = cur->branch_stack;
  2137. struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
  2138. struct branch_stack *prev_stack = prev->branch_stack;
  2139. struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
  2140. struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
  2141. int i, j, nr_identical_branches = 0;
  2142. struct stitch_list *stitch_node;
  2143. u64 cur_base, distance;
  2144. if (!cur_stack || !prev_stack)
  2145. return false;
  2146. /* Find the physical index of the base-of-stack for current sample. */
  2147. cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
  2148. distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
  2149. (max_lbr + prev_stack->hw_idx - cur_base);
  2150. /* Previous sample has shorter stack. Nothing can be stitched. */
  2151. if (distance + 1 > prev_stack->nr)
  2152. return false;
  2153. /*
  2154. * Check if there are identical LBRs between two samples.
  2155. * Identical LBRs must have same from, to and flags values. Also,
  2156. * they have to be saved in the same LBR registers (same physical
  2157. * index).
  2158. *
  2159. * Starts from the base-of-stack of current sample.
  2160. */
  2161. for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
  2162. if ((prev_entries[i].from != cur_entries[j].from) ||
  2163. (prev_entries[i].to != cur_entries[j].to) ||
  2164. (prev_entries[i].flags.value != cur_entries[j].flags.value))
  2165. break;
  2166. nr_identical_branches++;
  2167. }
  2168. if (!nr_identical_branches)
  2169. return false;
  2170. /*
  2171. * Save the LBRs between the base-of-stack of previous sample
  2172. * and the base-of-stack of current sample into lbr_stitch->lists.
  2173. * These LBRs will be stitched later.
  2174. */
  2175. for (i = prev_stack->nr - 1; i > (int)distance; i--) {
  2176. if (!lbr_stitch->prev_lbr_cursor[i].valid)
  2177. continue;
  2178. stitch_node = get_stitch_node(thread);
  2179. if (!stitch_node)
  2180. return false;
  2181. memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
  2182. sizeof(struct callchain_cursor_node));
  2183. stitch_node->cursor.ms.thread =
  2184. thread__get(lbr_stitch->prev_lbr_cursor[i].ms.thread);
  2185. stitch_node->cursor.ms.map = map__get(lbr_stitch->prev_lbr_cursor[i].ms.map);
  2186. if (callee)
  2187. list_add(&stitch_node->node, &lbr_stitch->lists);
  2188. else
  2189. list_add_tail(&stitch_node->node, &lbr_stitch->lists);
  2190. }
  2191. return true;
  2192. }
  2193. static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
  2194. {
  2195. if (thread__lbr_stitch(thread))
  2196. return true;
  2197. thread__set_lbr_stitch(thread, zalloc(sizeof(struct lbr_stitch)));
  2198. if (!thread__lbr_stitch(thread))
  2199. goto err;
  2200. thread__lbr_stitch(thread)->prev_lbr_cursor =
  2201. calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
  2202. if (!thread__lbr_stitch(thread)->prev_lbr_cursor)
  2203. goto free_lbr_stitch;
  2204. thread__lbr_stitch(thread)->prev_lbr_cursor_size = max_lbr + 1;
  2205. INIT_LIST_HEAD(&thread__lbr_stitch(thread)->lists);
  2206. INIT_LIST_HEAD(&thread__lbr_stitch(thread)->free_lists);
  2207. return true;
  2208. free_lbr_stitch:
  2209. free(thread__lbr_stitch(thread));
  2210. thread__set_lbr_stitch(thread, NULL);
  2211. err:
  2212. pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
  2213. thread__set_lbr_stitch_enable(thread, false);
  2214. return false;
  2215. }
  2216. /*
  2217. * Resolve LBR callstack chain sample
  2218. * Return:
  2219. * 1 on success get LBR callchain information
  2220. * 0 no available LBR callchain information, should try fp
  2221. * negative error code on other errors.
  2222. */
  2223. static int resolve_lbr_callchain_sample(struct thread *thread,
  2224. struct callchain_cursor *cursor,
  2225. struct perf_sample *sample,
  2226. struct symbol **parent,
  2227. struct addr_location *root_al,
  2228. int max_stack,
  2229. unsigned int max_lbr,
  2230. bool symbols)
  2231. {
  2232. bool callee = (callchain_param.order == ORDER_CALLEE);
  2233. struct ip_callchain *chain = sample->callchain;
  2234. int chain_nr = min(max_stack, (int)chain->nr), i;
  2235. struct lbr_stitch *lbr_stitch;
  2236. bool stitched_lbr = false;
  2237. u64 branch_from = 0;
  2238. int err;
  2239. for (i = 0; i < chain_nr; i++) {
  2240. if (chain->ips[i] == PERF_CONTEXT_USER)
  2241. break;
  2242. }
  2243. /* LBR only affects the user callchain */
  2244. if (i == chain_nr)
  2245. return 0;
  2246. if (thread__lbr_stitch_enable(thread) && !sample->no_hw_idx &&
  2247. (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
  2248. lbr_stitch = thread__lbr_stitch(thread);
  2249. stitched_lbr = has_stitched_lbr(thread, sample,
  2250. &lbr_stitch->prev_sample,
  2251. max_lbr, callee);
  2252. if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
  2253. struct stitch_list *stitch_node;
  2254. list_for_each_entry(stitch_node, &lbr_stitch->lists, node)
  2255. map_symbol__exit(&stitch_node->cursor.ms);
  2256. list_splice_init(&lbr_stitch->lists, &lbr_stitch->free_lists);
  2257. }
  2258. memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
  2259. }
  2260. if (callee) {
  2261. /* Add kernel ip */
  2262. err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
  2263. parent, root_al, branch_from,
  2264. true, i, symbols);
  2265. if (err)
  2266. goto error;
  2267. err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
  2268. root_al, &branch_from, true, symbols);
  2269. if (err)
  2270. goto error;
  2271. if (stitched_lbr) {
  2272. err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
  2273. if (err)
  2274. goto error;
  2275. }
  2276. } else {
  2277. if (stitched_lbr) {
  2278. err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
  2279. if (err)
  2280. goto error;
  2281. }
  2282. err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
  2283. root_al, &branch_from, false, symbols);
  2284. if (err)
  2285. goto error;
  2286. /* Add kernel ip */
  2287. err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
  2288. parent, root_al, branch_from,
  2289. false, i, symbols);
  2290. if (err)
  2291. goto error;
  2292. }
  2293. return 1;
  2294. error:
  2295. return (err < 0) ? err : 0;
  2296. }
  2297. static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
  2298. struct callchain_cursor *cursor,
  2299. struct symbol **parent,
  2300. struct addr_location *root_al,
  2301. u8 *cpumode, int ent, bool symbols)
  2302. {
  2303. int err = 0;
  2304. while (--ent >= 0) {
  2305. u64 ip = chain->ips[ent];
  2306. if (ip >= PERF_CONTEXT_MAX) {
  2307. err = add_callchain_ip(thread, cursor, parent,
  2308. root_al, cpumode, ip,
  2309. false, NULL, NULL, 0, symbols);
  2310. break;
  2311. }
  2312. }
  2313. return err;
  2314. }
  2315. static u64 get_leaf_frame_caller(struct perf_sample *sample,
  2316. struct thread *thread, int usr_idx)
  2317. {
  2318. if (machine__normalized_is(maps__machine(thread__maps(thread)), "arm64"))
  2319. return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
  2320. else
  2321. return 0;
  2322. }
  2323. static int thread__resolve_callchain_sample(struct thread *thread,
  2324. struct callchain_cursor *cursor,
  2325. struct evsel *evsel,
  2326. struct perf_sample *sample,
  2327. struct symbol **parent,
  2328. struct addr_location *root_al,
  2329. int max_stack,
  2330. bool symbols)
  2331. {
  2332. struct branch_stack *branch = sample->branch_stack;
  2333. struct branch_entry *entries = perf_sample__branch_entries(sample);
  2334. struct ip_callchain *chain = sample->callchain;
  2335. int chain_nr = 0;
  2336. u8 cpumode = PERF_RECORD_MISC_USER;
  2337. int i, j, err, nr_entries, usr_idx;
  2338. int skip_idx = -1;
  2339. int first_call = 0;
  2340. u64 leaf_frame_caller;
  2341. if (chain)
  2342. chain_nr = chain->nr;
  2343. if (evsel__has_branch_callstack(evsel)) {
  2344. struct perf_env *env = evsel__env(evsel);
  2345. err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
  2346. root_al, max_stack,
  2347. !env ? 0 : env->max_branches,
  2348. symbols);
  2349. if (err)
  2350. return (err < 0) ? err : 0;
  2351. }
  2352. /*
  2353. * Based on DWARF debug information, some architectures skip
  2354. * a callchain entry saved by the kernel.
  2355. */
  2356. skip_idx = arch_skip_callchain_idx(thread, chain);
  2357. /*
  2358. * Add branches to call stack for easier browsing. This gives
  2359. * more context for a sample than just the callers.
  2360. *
  2361. * This uses individual histograms of paths compared to the
  2362. * aggregated histograms the normal LBR mode uses.
  2363. *
  2364. * Limitations for now:
  2365. * - No extra filters
  2366. * - No annotations (should annotate somehow)
  2367. */
  2368. if (branch && callchain_param.branch_callstack) {
  2369. int nr = min(max_stack, (int)branch->nr);
  2370. struct branch_entry be[nr];
  2371. struct iterations iter[nr];
  2372. if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
  2373. pr_warning("corrupted branch chain. skipping...\n");
  2374. goto check_calls;
  2375. }
  2376. for (i = 0; i < nr; i++) {
  2377. if (callchain_param.order == ORDER_CALLEE) {
  2378. be[i] = entries[i];
  2379. if (chain == NULL)
  2380. continue;
  2381. /*
  2382. * Check for overlap into the callchain.
  2383. * The return address is one off compared to
  2384. * the branch entry. To adjust for this
  2385. * assume the calling instruction is not longer
  2386. * than 8 bytes.
  2387. */
  2388. if (i == skip_idx ||
  2389. chain->ips[first_call] >= PERF_CONTEXT_MAX)
  2390. first_call++;
  2391. else if (be[i].from < chain->ips[first_call] &&
  2392. be[i].from >= chain->ips[first_call] - 8)
  2393. first_call++;
  2394. } else
  2395. be[i] = entries[branch->nr - i - 1];
  2396. }
  2397. memset(iter, 0, sizeof(struct iterations) * nr);
  2398. nr = remove_loops(be, nr, iter);
  2399. for (i = 0; i < nr; i++) {
  2400. err = add_callchain_ip(thread, cursor, parent,
  2401. root_al,
  2402. NULL, be[i].to,
  2403. true, &be[i].flags,
  2404. NULL, be[i].from, symbols);
  2405. if (!err) {
  2406. err = add_callchain_ip(thread, cursor, parent, root_al,
  2407. NULL, be[i].from,
  2408. true, &be[i].flags,
  2409. &iter[i], 0, symbols);
  2410. }
  2411. if (err == -EINVAL)
  2412. break;
  2413. if (err)
  2414. return err;
  2415. }
  2416. if (chain_nr == 0)
  2417. return 0;
  2418. chain_nr -= nr;
  2419. }
  2420. check_calls:
  2421. if (chain && callchain_param.order != ORDER_CALLEE) {
  2422. err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
  2423. &cpumode, chain->nr - first_call, symbols);
  2424. if (err)
  2425. return (err < 0) ? err : 0;
  2426. }
  2427. for (i = first_call, nr_entries = 0;
  2428. i < chain_nr && nr_entries < max_stack; i++) {
  2429. u64 ip;
  2430. if (callchain_param.order == ORDER_CALLEE)
  2431. j = i;
  2432. else
  2433. j = chain->nr - i - 1;
  2434. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  2435. if (j == skip_idx)
  2436. continue;
  2437. #endif
  2438. ip = chain->ips[j];
  2439. if (ip < PERF_CONTEXT_MAX)
  2440. ++nr_entries;
  2441. else if (callchain_param.order != ORDER_CALLEE) {
  2442. err = find_prev_cpumode(chain, thread, cursor, parent,
  2443. root_al, &cpumode, j, symbols);
  2444. if (err)
  2445. return (err < 0) ? err : 0;
  2446. continue;
  2447. }
  2448. /*
  2449. * PERF_CONTEXT_USER allows us to locate where the user stack ends.
  2450. * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
  2451. * the index will be different in order to add the missing frame
  2452. * at the right place.
  2453. */
  2454. usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
  2455. if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
  2456. leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
  2457. /*
  2458. * check if leaf_frame_Caller != ip to not add the same
  2459. * value twice.
  2460. */
  2461. if (leaf_frame_caller && leaf_frame_caller != ip) {
  2462. err = add_callchain_ip(thread, cursor, parent,
  2463. root_al, &cpumode, leaf_frame_caller,
  2464. false, NULL, NULL, 0, symbols);
  2465. if (err)
  2466. return (err < 0) ? err : 0;
  2467. }
  2468. }
  2469. err = add_callchain_ip(thread, cursor, parent,
  2470. root_al, &cpumode, ip,
  2471. false, NULL, NULL, 0, symbols);
  2472. if (err)
  2473. return (err < 0) ? err : 0;
  2474. }
  2475. return 0;
  2476. }
  2477. static int unwind_entry(struct unwind_entry *entry, void *arg)
  2478. {
  2479. struct callchain_cursor *cursor = arg;
  2480. const char *srcline = NULL;
  2481. u64 addr = entry->ip;
  2482. if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
  2483. return 0;
  2484. if (append_inlines(cursor, &entry->ms, entry->ip, /*branch=*/false, /*branch_flags=*/NULL,
  2485. /*nr_loop_iter=*/0, /*iter_cycles=*/0, /*branch_from=*/0) == 0)
  2486. return 0;
  2487. /*
  2488. * Convert entry->ip from a virtual address to an offset in
  2489. * its corresponding binary.
  2490. */
  2491. if (entry->ms.map)
  2492. addr = map__dso_map_ip(entry->ms.map, entry->ip);
  2493. srcline = callchain_srcline(&entry->ms, addr);
  2494. return callchain_cursor_append(cursor, entry->ip, &entry->ms,
  2495. false, NULL, 0, 0, 0, srcline);
  2496. }
  2497. static int thread__resolve_callchain_unwind(struct thread *thread,
  2498. struct callchain_cursor *cursor,
  2499. struct evsel *evsel,
  2500. struct perf_sample *sample,
  2501. int max_stack, bool symbols)
  2502. {
  2503. /* Can we do dwarf post unwind? */
  2504. if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  2505. (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
  2506. return 0;
  2507. /* Bail out if nothing was captured. */
  2508. if (!sample->user_regs || !sample->user_regs->regs ||
  2509. !sample->user_stack.size)
  2510. return 0;
  2511. if (!symbols)
  2512. pr_debug("Not resolving symbols with an unwinder isn't currently supported\n");
  2513. return unwind__get_entries(unwind_entry, cursor,
  2514. thread, sample, max_stack, false);
  2515. }
  2516. int __thread__resolve_callchain(struct thread *thread,
  2517. struct callchain_cursor *cursor,
  2518. struct evsel *evsel,
  2519. struct perf_sample *sample,
  2520. struct symbol **parent,
  2521. struct addr_location *root_al,
  2522. int max_stack,
  2523. bool symbols)
  2524. {
  2525. int ret = 0;
  2526. if (cursor == NULL)
  2527. return -ENOMEM;
  2528. callchain_cursor_reset(cursor);
  2529. if (callchain_param.order == ORDER_CALLEE) {
  2530. ret = thread__resolve_callchain_sample(thread, cursor,
  2531. evsel, sample,
  2532. parent, root_al,
  2533. max_stack, symbols);
  2534. if (ret)
  2535. return ret;
  2536. ret = thread__resolve_callchain_unwind(thread, cursor,
  2537. evsel, sample,
  2538. max_stack, symbols);
  2539. } else {
  2540. ret = thread__resolve_callchain_unwind(thread, cursor,
  2541. evsel, sample,
  2542. max_stack, symbols);
  2543. if (ret)
  2544. return ret;
  2545. ret = thread__resolve_callchain_sample(thread, cursor,
  2546. evsel, sample,
  2547. parent, root_al,
  2548. max_stack, symbols);
  2549. }
  2550. return ret;
  2551. }
  2552. int machine__for_each_thread(struct machine *machine,
  2553. int (*fn)(struct thread *thread, void *p),
  2554. void *priv)
  2555. {
  2556. return threads__for_each_thread(&machine->threads, fn, priv);
  2557. }
  2558. int machines__for_each_thread(struct machines *machines,
  2559. int (*fn)(struct thread *thread, void *p),
  2560. void *priv)
  2561. {
  2562. struct rb_node *nd;
  2563. int rc = 0;
  2564. rc = machine__for_each_thread(&machines->host, fn, priv);
  2565. if (rc != 0)
  2566. return rc;
  2567. for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
  2568. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  2569. rc = machine__for_each_thread(machine, fn, priv);
  2570. if (rc != 0)
  2571. return rc;
  2572. }
  2573. return rc;
  2574. }
  2575. static int thread_list_cb(struct thread *thread, void *data)
  2576. {
  2577. struct list_head *list = data;
  2578. struct thread_list *entry = malloc(sizeof(*entry));
  2579. if (!entry)
  2580. return -ENOMEM;
  2581. entry->thread = thread__get(thread);
  2582. list_add_tail(&entry->list, list);
  2583. return 0;
  2584. }
  2585. int machine__thread_list(struct machine *machine, struct list_head *list)
  2586. {
  2587. return machine__for_each_thread(machine, thread_list_cb, list);
  2588. }
  2589. void thread_list__delete(struct list_head *list)
  2590. {
  2591. struct thread_list *pos, *next;
  2592. list_for_each_entry_safe(pos, next, list, list) {
  2593. thread__zput(pos->thread);
  2594. list_del(&pos->list);
  2595. free(pos);
  2596. }
  2597. }
  2598. pid_t machine__get_current_tid(struct machine *machine, int cpu)
  2599. {
  2600. if (cpu < 0 || (size_t)cpu >= machine->current_tid_sz)
  2601. return -1;
  2602. return machine->current_tid[cpu];
  2603. }
  2604. int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
  2605. pid_t tid)
  2606. {
  2607. struct thread *thread;
  2608. const pid_t init_val = -1;
  2609. if (cpu < 0)
  2610. return -EINVAL;
  2611. if (realloc_array_as_needed(machine->current_tid,
  2612. machine->current_tid_sz,
  2613. (unsigned int)cpu,
  2614. &init_val))
  2615. return -ENOMEM;
  2616. machine->current_tid[cpu] = tid;
  2617. thread = machine__findnew_thread(machine, pid, tid);
  2618. if (!thread)
  2619. return -ENOMEM;
  2620. thread__set_cpu(thread, cpu);
  2621. thread__put(thread);
  2622. return 0;
  2623. }
  2624. /*
  2625. * Compares the raw arch string. N.B. see instead perf_env__arch() or
  2626. * machine__normalized_is() if a normalized arch is needed.
  2627. */
  2628. bool machine__is(struct machine *machine, const char *arch)
  2629. {
  2630. return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
  2631. }
  2632. bool machine__normalized_is(struct machine *machine, const char *arch)
  2633. {
  2634. return machine && !strcmp(perf_env__arch(machine->env), arch);
  2635. }
  2636. int machine__nr_cpus_avail(struct machine *machine)
  2637. {
  2638. return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
  2639. }
  2640. int machine__get_kernel_start(struct machine *machine)
  2641. {
  2642. struct map *map = machine__kernel_map(machine);
  2643. int err = 0;
  2644. /*
  2645. * The only addresses above 2^63 are kernel addresses of a 64-bit
  2646. * kernel. Note that addresses are unsigned so that on a 32-bit system
  2647. * all addresses including kernel addresses are less than 2^32. In
  2648. * that case (32-bit system), if the kernel mapping is unknown, all
  2649. * addresses will be assumed to be in user space - see
  2650. * machine__kernel_ip().
  2651. */
  2652. machine->kernel_start = 1ULL << 63;
  2653. if (map) {
  2654. err = map__load(map);
  2655. /*
  2656. * On x86_64, PTI entry trampolines are less than the
  2657. * start of kernel text, but still above 2^63. So leave
  2658. * kernel_start = 1ULL << 63 for x86_64.
  2659. */
  2660. if (!err && !machine__is(machine, "x86_64"))
  2661. machine->kernel_start = map__start(map);
  2662. }
  2663. return err;
  2664. }
  2665. u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
  2666. {
  2667. u8 addr_cpumode = cpumode;
  2668. bool kernel_ip;
  2669. if (!machine->single_address_space)
  2670. goto out;
  2671. kernel_ip = machine__kernel_ip(machine, addr);
  2672. switch (cpumode) {
  2673. case PERF_RECORD_MISC_KERNEL:
  2674. case PERF_RECORD_MISC_USER:
  2675. addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
  2676. PERF_RECORD_MISC_USER;
  2677. break;
  2678. case PERF_RECORD_MISC_GUEST_KERNEL:
  2679. case PERF_RECORD_MISC_GUEST_USER:
  2680. addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
  2681. PERF_RECORD_MISC_GUEST_USER;
  2682. break;
  2683. default:
  2684. break;
  2685. }
  2686. out:
  2687. return addr_cpumode;
  2688. }
  2689. struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename,
  2690. const struct dso_id *id)
  2691. {
  2692. return dsos__findnew_id(&machine->dsos, filename, id);
  2693. }
  2694. struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
  2695. {
  2696. return machine__findnew_dso_id(machine, filename, &dso_id_empty);
  2697. }
  2698. char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  2699. {
  2700. struct machine *machine = vmachine;
  2701. struct map *map;
  2702. struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
  2703. if (sym == NULL)
  2704. return NULL;
  2705. *modp = __map__is_kmodule(map) ? (char *)dso__short_name(map__dso(map)) : NULL;
  2706. *addrp = map__unmap_ip(map, sym->start);
  2707. return sym->name;
  2708. }
  2709. struct machine__for_each_dso_cb_args {
  2710. struct machine *machine;
  2711. machine__dso_t fn;
  2712. void *priv;
  2713. };
  2714. static int machine__for_each_dso_cb(struct dso *dso, void *data)
  2715. {
  2716. struct machine__for_each_dso_cb_args *args = data;
  2717. return args->fn(dso, args->machine, args->priv);
  2718. }
  2719. int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
  2720. {
  2721. struct machine__for_each_dso_cb_args args = {
  2722. .machine = machine,
  2723. .fn = fn,
  2724. .priv = priv,
  2725. };
  2726. return dsos__for_each_dso(&machine->dsos, machine__for_each_dso_cb, &args);
  2727. }
  2728. int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn, void *priv)
  2729. {
  2730. struct maps *maps = machine__kernel_maps(machine);
  2731. return maps__for_each_map(maps, fn, priv);
  2732. }
  2733. bool machine__is_lock_function(struct machine *machine, u64 addr)
  2734. {
  2735. if (!machine->sched.text_start) {
  2736. struct map *kmap;
  2737. struct symbol *sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_start", &kmap);
  2738. if (!sym) {
  2739. /* to avoid retry */
  2740. machine->sched.text_start = 1;
  2741. return false;
  2742. }
  2743. machine->sched.text_start = map__unmap_ip(kmap, sym->start);
  2744. /* should not fail from here */
  2745. sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_end", &kmap);
  2746. machine->sched.text_end = map__unmap_ip(kmap, sym->start);
  2747. sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_start", &kmap);
  2748. machine->lock.text_start = map__unmap_ip(kmap, sym->start);
  2749. sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_end", &kmap);
  2750. machine->lock.text_end = map__unmap_ip(kmap, sym->start);
  2751. sym = machine__find_kernel_symbol_by_name(machine, "__traceiter_contention_begin", &kmap);
  2752. if (sym) {
  2753. machine->traceiter.text_start = map__unmap_ip(kmap, sym->start);
  2754. machine->traceiter.text_end = map__unmap_ip(kmap, sym->end);
  2755. }
  2756. sym = machine__find_kernel_symbol_by_name(machine, "trace_contention_begin", &kmap);
  2757. if (sym) {
  2758. machine->trace.text_start = map__unmap_ip(kmap, sym->start);
  2759. machine->trace.text_end = map__unmap_ip(kmap, sym->end);
  2760. }
  2761. }
  2762. /* failed to get kernel symbols */
  2763. if (machine->sched.text_start == 1)
  2764. return false;
  2765. /* mutex and rwsem functions are in sched text section */
  2766. if (machine->sched.text_start <= addr && addr < machine->sched.text_end)
  2767. return true;
  2768. /* spinlock functions are in lock text section */
  2769. if (machine->lock.text_start <= addr && addr < machine->lock.text_end)
  2770. return true;
  2771. /* traceiter functions currently don't have their own section
  2772. * but we consider them lock functions
  2773. */
  2774. if (machine->traceiter.text_start != 0) {
  2775. if (machine->traceiter.text_start <= addr && addr < machine->traceiter.text_end)
  2776. return true;
  2777. }
  2778. if (machine->trace.text_start != 0) {
  2779. if (machine->trace.text_start <= addr && addr < machine->trace.text_end)
  2780. return true;
  2781. }
  2782. return false;
  2783. }
  2784. int machine__hit_all_dsos(struct machine *machine)
  2785. {
  2786. return dsos__hit_all(&machine->dsos);
  2787. }