machine.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513
  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 "callchain.h"
  7. #include "debug.h"
  8. #include "event.h"
  9. #include "evsel.h"
  10. #include "hist.h"
  11. #include "machine.h"
  12. #include "map.h"
  13. #include "sort.h"
  14. #include "strlist.h"
  15. #include "thread.h"
  16. #include "vdso.h"
  17. #include <stdbool.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. #include "unwind.h"
  22. #include "linux/hash.h"
  23. #include "asm/bug.h"
  24. #include "sane_ctype.h"
  25. #include <symbol/kallsyms.h>
  26. #include <linux/mman.h>
  27. static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
  28. static void dsos__init(struct dsos *dsos)
  29. {
  30. INIT_LIST_HEAD(&dsos->head);
  31. dsos->root = RB_ROOT;
  32. init_rwsem(&dsos->lock);
  33. }
  34. static void machine__threads_init(struct machine *machine)
  35. {
  36. int i;
  37. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  38. struct threads *threads = &machine->threads[i];
  39. threads->entries = RB_ROOT;
  40. init_rwsem(&threads->lock);
  41. threads->nr = 0;
  42. INIT_LIST_HEAD(&threads->dead);
  43. threads->last_match = NULL;
  44. }
  45. }
  46. static int machine__set_mmap_name(struct machine *machine)
  47. {
  48. if (machine__is_host(machine))
  49. machine->mmap_name = strdup("[kernel.kallsyms]");
  50. else if (machine__is_default_guest(machine))
  51. machine->mmap_name = strdup("[guest.kernel.kallsyms]");
  52. else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
  53. machine->pid) < 0)
  54. machine->mmap_name = NULL;
  55. return machine->mmap_name ? 0 : -ENOMEM;
  56. }
  57. int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
  58. {
  59. int err = -ENOMEM;
  60. memset(machine, 0, sizeof(*machine));
  61. map_groups__init(&machine->kmaps, machine);
  62. RB_CLEAR_NODE(&machine->rb_node);
  63. dsos__init(&machine->dsos);
  64. machine__threads_init(machine);
  65. machine->vdso_info = NULL;
  66. machine->env = NULL;
  67. machine->pid = pid;
  68. machine->id_hdr_size = 0;
  69. machine->kptr_restrict_warned = false;
  70. machine->comm_exec = false;
  71. machine->kernel_start = 0;
  72. machine->vmlinux_map = NULL;
  73. machine->root_dir = strdup(root_dir);
  74. if (machine->root_dir == NULL)
  75. return -ENOMEM;
  76. if (machine__set_mmap_name(machine))
  77. goto out;
  78. if (pid != HOST_KERNEL_ID) {
  79. struct thread *thread = machine__findnew_thread(machine, -1,
  80. pid);
  81. char comm[64];
  82. if (thread == NULL)
  83. goto out;
  84. snprintf(comm, sizeof(comm), "[guest/%d]", pid);
  85. thread__set_comm(thread, comm, 0);
  86. thread__put(thread);
  87. }
  88. machine->current_tid = NULL;
  89. err = 0;
  90. out:
  91. if (err) {
  92. zfree(&machine->root_dir);
  93. zfree(&machine->mmap_name);
  94. }
  95. return 0;
  96. }
  97. struct machine *machine__new_host(void)
  98. {
  99. struct machine *machine = malloc(sizeof(*machine));
  100. if (machine != NULL) {
  101. machine__init(machine, "", HOST_KERNEL_ID);
  102. if (machine__create_kernel_maps(machine) < 0)
  103. goto out_delete;
  104. }
  105. return machine;
  106. out_delete:
  107. free(machine);
  108. return NULL;
  109. }
  110. struct machine *machine__new_kallsyms(void)
  111. {
  112. struct machine *machine = machine__new_host();
  113. /*
  114. * FIXME:
  115. * 1) We should switch to machine__load_kallsyms(), i.e. not explicitely
  116. * ask for not using the kcore parsing code, once this one is fixed
  117. * to create a map per module.
  118. */
  119. if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
  120. machine__delete(machine);
  121. machine = NULL;
  122. }
  123. return machine;
  124. }
  125. static void dsos__purge(struct dsos *dsos)
  126. {
  127. struct dso *pos, *n;
  128. down_write(&dsos->lock);
  129. list_for_each_entry_safe(pos, n, &dsos->head, node) {
  130. RB_CLEAR_NODE(&pos->rb_node);
  131. pos->root = NULL;
  132. list_del_init(&pos->node);
  133. dso__put(pos);
  134. }
  135. up_write(&dsos->lock);
  136. }
  137. static void dsos__exit(struct dsos *dsos)
  138. {
  139. dsos__purge(dsos);
  140. exit_rwsem(&dsos->lock);
  141. }
  142. void machine__delete_threads(struct machine *machine)
  143. {
  144. struct rb_node *nd;
  145. int i;
  146. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  147. struct threads *threads = &machine->threads[i];
  148. down_write(&threads->lock);
  149. nd = rb_first(&threads->entries);
  150. while (nd) {
  151. struct thread *t = rb_entry(nd, struct thread, rb_node);
  152. nd = rb_next(nd);
  153. __machine__remove_thread(machine, t, false);
  154. }
  155. up_write(&threads->lock);
  156. }
  157. }
  158. void machine__exit(struct machine *machine)
  159. {
  160. int i;
  161. if (machine == NULL)
  162. return;
  163. machine__destroy_kernel_maps(machine);
  164. map_groups__exit(&machine->kmaps);
  165. dsos__exit(&machine->dsos);
  166. machine__exit_vdso(machine);
  167. zfree(&machine->root_dir);
  168. zfree(&machine->mmap_name);
  169. zfree(&machine->current_tid);
  170. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  171. struct threads *threads = &machine->threads[i];
  172. exit_rwsem(&threads->lock);
  173. }
  174. }
  175. void machine__delete(struct machine *machine)
  176. {
  177. if (machine) {
  178. machine__exit(machine);
  179. free(machine);
  180. }
  181. }
  182. void machines__init(struct machines *machines)
  183. {
  184. machine__init(&machines->host, "", HOST_KERNEL_ID);
  185. machines->guests = RB_ROOT;
  186. }
  187. void machines__exit(struct machines *machines)
  188. {
  189. machine__exit(&machines->host);
  190. /* XXX exit guest */
  191. }
  192. struct machine *machines__add(struct machines *machines, pid_t pid,
  193. const char *root_dir)
  194. {
  195. struct rb_node **p = &machines->guests.rb_node;
  196. struct rb_node *parent = NULL;
  197. struct machine *pos, *machine = malloc(sizeof(*machine));
  198. if (machine == NULL)
  199. return NULL;
  200. if (machine__init(machine, root_dir, pid) != 0) {
  201. free(machine);
  202. return NULL;
  203. }
  204. while (*p != NULL) {
  205. parent = *p;
  206. pos = rb_entry(parent, struct machine, rb_node);
  207. if (pid < pos->pid)
  208. p = &(*p)->rb_left;
  209. else
  210. p = &(*p)->rb_right;
  211. }
  212. rb_link_node(&machine->rb_node, parent, p);
  213. rb_insert_color(&machine->rb_node, &machines->guests);
  214. return machine;
  215. }
  216. void machines__set_comm_exec(struct machines *machines, bool comm_exec)
  217. {
  218. struct rb_node *nd;
  219. machines->host.comm_exec = comm_exec;
  220. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  221. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  222. machine->comm_exec = comm_exec;
  223. }
  224. }
  225. struct machine *machines__find(struct machines *machines, pid_t pid)
  226. {
  227. struct rb_node **p = &machines->guests.rb_node;
  228. struct rb_node *parent = NULL;
  229. struct machine *machine;
  230. struct machine *default_machine = NULL;
  231. if (pid == HOST_KERNEL_ID)
  232. return &machines->host;
  233. while (*p != NULL) {
  234. parent = *p;
  235. machine = rb_entry(parent, struct machine, rb_node);
  236. if (pid < machine->pid)
  237. p = &(*p)->rb_left;
  238. else if (pid > machine->pid)
  239. p = &(*p)->rb_right;
  240. else
  241. return machine;
  242. if (!machine->pid)
  243. default_machine = machine;
  244. }
  245. return default_machine;
  246. }
  247. struct machine *machines__findnew(struct machines *machines, pid_t pid)
  248. {
  249. char path[PATH_MAX];
  250. const char *root_dir = "";
  251. struct machine *machine = machines__find(machines, pid);
  252. if (machine && (machine->pid == pid))
  253. goto out;
  254. if ((pid != HOST_KERNEL_ID) &&
  255. (pid != DEFAULT_GUEST_KERNEL_ID) &&
  256. (symbol_conf.guestmount)) {
  257. sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
  258. if (access(path, R_OK)) {
  259. static struct strlist *seen;
  260. if (!seen)
  261. seen = strlist__new(NULL, NULL);
  262. if (!strlist__has_entry(seen, path)) {
  263. pr_err("Can't access file %s\n", path);
  264. strlist__add(seen, path);
  265. }
  266. machine = NULL;
  267. goto out;
  268. }
  269. root_dir = path;
  270. }
  271. machine = machines__add(machines, pid, root_dir);
  272. out:
  273. return machine;
  274. }
  275. void machines__process_guests(struct machines *machines,
  276. machine__process_t process, void *data)
  277. {
  278. struct rb_node *nd;
  279. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  280. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  281. process(pos, data);
  282. }
  283. }
  284. void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
  285. {
  286. struct rb_node *node;
  287. struct machine *machine;
  288. machines->host.id_hdr_size = id_hdr_size;
  289. for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
  290. machine = rb_entry(node, struct machine, rb_node);
  291. machine->id_hdr_size = id_hdr_size;
  292. }
  293. return;
  294. }
  295. static void machine__update_thread_pid(struct machine *machine,
  296. struct thread *th, pid_t pid)
  297. {
  298. struct thread *leader;
  299. if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
  300. return;
  301. th->pid_ = pid;
  302. if (th->pid_ == th->tid)
  303. return;
  304. leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
  305. if (!leader)
  306. goto out_err;
  307. if (!leader->mg)
  308. leader->mg = map_groups__new(machine);
  309. if (!leader->mg)
  310. goto out_err;
  311. if (th->mg == leader->mg)
  312. return;
  313. if (th->mg) {
  314. /*
  315. * Maps are created from MMAP events which provide the pid and
  316. * tid. Consequently there never should be any maps on a thread
  317. * with an unknown pid. Just print an error if there are.
  318. */
  319. if (!map_groups__empty(th->mg))
  320. pr_err("Discarding thread maps for %d:%d\n",
  321. th->pid_, th->tid);
  322. map_groups__put(th->mg);
  323. }
  324. th->mg = map_groups__get(leader->mg);
  325. out_put:
  326. thread__put(leader);
  327. return;
  328. out_err:
  329. pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
  330. goto out_put;
  331. }
  332. /*
  333. * Caller must eventually drop thread->refcnt returned with a successful
  334. * lookup/new thread inserted.
  335. */
  336. static struct thread *____machine__findnew_thread(struct machine *machine,
  337. struct threads *threads,
  338. pid_t pid, pid_t tid,
  339. bool create)
  340. {
  341. struct rb_node **p = &threads->entries.rb_node;
  342. struct rb_node *parent = NULL;
  343. struct thread *th;
  344. /*
  345. * Front-end cache - TID lookups come in blocks,
  346. * so most of the time we dont have to look up
  347. * the full rbtree:
  348. */
  349. th = threads->last_match;
  350. if (th != NULL) {
  351. if (th->tid == tid) {
  352. machine__update_thread_pid(machine, th, pid);
  353. return thread__get(th);
  354. }
  355. threads->last_match = NULL;
  356. }
  357. while (*p != NULL) {
  358. parent = *p;
  359. th = rb_entry(parent, struct thread, rb_node);
  360. if (th->tid == tid) {
  361. threads->last_match = th;
  362. machine__update_thread_pid(machine, th, pid);
  363. return thread__get(th);
  364. }
  365. if (tid < th->tid)
  366. p = &(*p)->rb_left;
  367. else
  368. p = &(*p)->rb_right;
  369. }
  370. if (!create)
  371. return NULL;
  372. th = thread__new(pid, tid);
  373. if (th != NULL) {
  374. rb_link_node(&th->rb_node, parent, p);
  375. rb_insert_color(&th->rb_node, &threads->entries);
  376. /*
  377. * We have to initialize map_groups separately
  378. * after rb tree is updated.
  379. *
  380. * The reason is that we call machine__findnew_thread
  381. * within thread__init_map_groups to find the thread
  382. * leader and that would screwed the rb tree.
  383. */
  384. if (thread__init_map_groups(th, machine)) {
  385. rb_erase_init(&th->rb_node, &threads->entries);
  386. RB_CLEAR_NODE(&th->rb_node);
  387. thread__put(th);
  388. return NULL;
  389. }
  390. /*
  391. * It is now in the rbtree, get a ref
  392. */
  393. thread__get(th);
  394. threads->last_match = th;
  395. ++threads->nr;
  396. }
  397. return th;
  398. }
  399. struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
  400. {
  401. return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
  402. }
  403. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
  404. pid_t tid)
  405. {
  406. struct threads *threads = machine__threads(machine, tid);
  407. struct thread *th;
  408. down_write(&threads->lock);
  409. th = __machine__findnew_thread(machine, pid, tid);
  410. up_write(&threads->lock);
  411. return th;
  412. }
  413. struct thread *machine__find_thread(struct machine *machine, pid_t pid,
  414. pid_t tid)
  415. {
  416. struct threads *threads = machine__threads(machine, tid);
  417. struct thread *th;
  418. down_read(&threads->lock);
  419. th = ____machine__findnew_thread(machine, threads, pid, tid, false);
  420. up_read(&threads->lock);
  421. return th;
  422. }
  423. struct comm *machine__thread_exec_comm(struct machine *machine,
  424. struct thread *thread)
  425. {
  426. if (machine->comm_exec)
  427. return thread__exec_comm(thread);
  428. else
  429. return thread__comm(thread);
  430. }
  431. int machine__process_comm_event(struct machine *machine, union perf_event *event,
  432. struct perf_sample *sample)
  433. {
  434. struct thread *thread = machine__findnew_thread(machine,
  435. event->comm.pid,
  436. event->comm.tid);
  437. bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
  438. int err = 0;
  439. if (exec)
  440. machine->comm_exec = true;
  441. if (dump_trace)
  442. perf_event__fprintf_comm(event, stdout);
  443. if (thread == NULL ||
  444. __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
  445. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  446. err = -1;
  447. }
  448. thread__put(thread);
  449. return err;
  450. }
  451. int machine__process_namespaces_event(struct machine *machine __maybe_unused,
  452. union perf_event *event,
  453. struct perf_sample *sample __maybe_unused)
  454. {
  455. struct thread *thread = machine__findnew_thread(machine,
  456. event->namespaces.pid,
  457. event->namespaces.tid);
  458. int err = 0;
  459. WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
  460. "\nWARNING: kernel seems to support more namespaces than perf"
  461. " tool.\nTry updating the perf tool..\n\n");
  462. WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
  463. "\nWARNING: perf tool seems to support more namespaces than"
  464. " the kernel.\nTry updating the kernel..\n\n");
  465. if (dump_trace)
  466. perf_event__fprintf_namespaces(event, stdout);
  467. if (thread == NULL ||
  468. thread__set_namespaces(thread, sample->time, &event->namespaces)) {
  469. dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
  470. err = -1;
  471. }
  472. thread__put(thread);
  473. return err;
  474. }
  475. int machine__process_lost_event(struct machine *machine __maybe_unused,
  476. union perf_event *event, struct perf_sample *sample __maybe_unused)
  477. {
  478. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  479. event->lost.id, event->lost.lost);
  480. return 0;
  481. }
  482. int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
  483. union perf_event *event, struct perf_sample *sample)
  484. {
  485. dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
  486. sample->id, event->lost_samples.lost);
  487. return 0;
  488. }
  489. static struct dso *machine__findnew_module_dso(struct machine *machine,
  490. struct kmod_path *m,
  491. const char *filename)
  492. {
  493. struct dso *dso;
  494. down_write(&machine->dsos.lock);
  495. dso = __dsos__find(&machine->dsos, m->name, true);
  496. if (!dso) {
  497. dso = __dsos__addnew(&machine->dsos, m->name);
  498. if (dso == NULL)
  499. goto out_unlock;
  500. dso__set_module_info(dso, m, machine);
  501. dso__set_long_name(dso, strdup(filename), true);
  502. }
  503. dso__get(dso);
  504. out_unlock:
  505. up_write(&machine->dsos.lock);
  506. return dso;
  507. }
  508. int machine__process_aux_event(struct machine *machine __maybe_unused,
  509. union perf_event *event)
  510. {
  511. if (dump_trace)
  512. perf_event__fprintf_aux(event, stdout);
  513. return 0;
  514. }
  515. int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
  516. union perf_event *event)
  517. {
  518. if (dump_trace)
  519. perf_event__fprintf_itrace_start(event, stdout);
  520. return 0;
  521. }
  522. int machine__process_switch_event(struct machine *machine __maybe_unused,
  523. union perf_event *event)
  524. {
  525. if (dump_trace)
  526. perf_event__fprintf_switch(event, stdout);
  527. return 0;
  528. }
  529. static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
  530. {
  531. const char *dup_filename;
  532. if (!filename || !dso || !dso->long_name)
  533. return;
  534. if (dso->long_name[0] != '[')
  535. return;
  536. if (!strchr(filename, '/'))
  537. return;
  538. dup_filename = strdup(filename);
  539. if (!dup_filename)
  540. return;
  541. dso__set_long_name(dso, dup_filename, true);
  542. }
  543. struct map *machine__findnew_module_map(struct machine *machine, u64 start,
  544. const char *filename)
  545. {
  546. struct map *map = NULL;
  547. struct dso *dso = NULL;
  548. struct kmod_path m;
  549. if (kmod_path__parse_name(&m, filename))
  550. return NULL;
  551. map = map_groups__find_by_name(&machine->kmaps, m.name);
  552. if (map) {
  553. /*
  554. * If the map's dso is an offline module, give dso__load()
  555. * a chance to find the file path of that module by fixing
  556. * long_name.
  557. */
  558. dso__adjust_kmod_long_name(map->dso, filename);
  559. goto out;
  560. }
  561. dso = machine__findnew_module_dso(machine, &m, filename);
  562. if (dso == NULL)
  563. goto out;
  564. map = map__new2(start, dso);
  565. if (map == NULL)
  566. goto out;
  567. map_groups__insert(&machine->kmaps, map);
  568. /* Put the map here because map_groups__insert alread got it */
  569. map__put(map);
  570. out:
  571. /* put the dso here, corresponding to machine__findnew_module_dso */
  572. dso__put(dso);
  573. free(m.name);
  574. return map;
  575. }
  576. size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
  577. {
  578. struct rb_node *nd;
  579. size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
  580. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  581. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  582. ret += __dsos__fprintf(&pos->dsos.head, fp);
  583. }
  584. return ret;
  585. }
  586. size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
  587. bool (skip)(struct dso *dso, int parm), int parm)
  588. {
  589. return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
  590. }
  591. size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
  592. bool (skip)(struct dso *dso, int parm), int parm)
  593. {
  594. struct rb_node *nd;
  595. size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
  596. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  597. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  598. ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
  599. }
  600. return ret;
  601. }
  602. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  603. {
  604. int i;
  605. size_t printed = 0;
  606. struct dso *kdso = machine__kernel_map(machine)->dso;
  607. if (kdso->has_build_id) {
  608. char filename[PATH_MAX];
  609. if (dso__build_id_filename(kdso, filename, sizeof(filename),
  610. false))
  611. printed += fprintf(fp, "[0] %s\n", filename);
  612. }
  613. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  614. printed += fprintf(fp, "[%d] %s\n",
  615. i + kdso->has_build_id, vmlinux_path[i]);
  616. return printed;
  617. }
  618. size_t machine__fprintf(struct machine *machine, FILE *fp)
  619. {
  620. struct rb_node *nd;
  621. size_t ret;
  622. int i;
  623. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  624. struct threads *threads = &machine->threads[i];
  625. down_read(&threads->lock);
  626. ret = fprintf(fp, "Threads: %u\n", threads->nr);
  627. for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
  628. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  629. ret += thread__fprintf(pos, fp);
  630. }
  631. up_read(&threads->lock);
  632. }
  633. return ret;
  634. }
  635. static struct dso *machine__get_kernel(struct machine *machine)
  636. {
  637. const char *vmlinux_name = machine->mmap_name;
  638. struct dso *kernel;
  639. if (machine__is_host(machine)) {
  640. if (symbol_conf.vmlinux_name)
  641. vmlinux_name = symbol_conf.vmlinux_name;
  642. kernel = machine__findnew_kernel(machine, vmlinux_name,
  643. "[kernel]", DSO_TYPE_KERNEL);
  644. } else {
  645. if (symbol_conf.default_guest_vmlinux_name)
  646. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  647. kernel = machine__findnew_kernel(machine, vmlinux_name,
  648. "[guest.kernel]",
  649. DSO_TYPE_GUEST_KERNEL);
  650. }
  651. if (kernel != NULL && (!kernel->has_build_id))
  652. dso__read_running_kernel_build_id(kernel, machine);
  653. return kernel;
  654. }
  655. struct process_args {
  656. u64 start;
  657. };
  658. void machine__get_kallsyms_filename(struct machine *machine, char *buf,
  659. size_t bufsz)
  660. {
  661. if (machine__is_default_guest(machine))
  662. scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
  663. else
  664. scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
  665. }
  666. const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
  667. /* Figure out the start address of kernel map from /proc/kallsyms.
  668. * Returns the name of the start symbol in *symbol_name. Pass in NULL as
  669. * symbol_name if it's not that important.
  670. */
  671. static int machine__get_running_kernel_start(struct machine *machine,
  672. const char **symbol_name, u64 *start)
  673. {
  674. char filename[PATH_MAX];
  675. int i, err = -1;
  676. const char *name;
  677. u64 addr = 0;
  678. machine__get_kallsyms_filename(machine, filename, PATH_MAX);
  679. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  680. return 0;
  681. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  682. err = kallsyms__get_function_start(filename, name, &addr);
  683. if (!err)
  684. break;
  685. }
  686. if (err)
  687. return -1;
  688. if (symbol_name)
  689. *symbol_name = name;
  690. *start = addr;
  691. return 0;
  692. }
  693. int machine__create_extra_kernel_map(struct machine *machine,
  694. struct dso *kernel,
  695. struct extra_kernel_map *xm)
  696. {
  697. struct kmap *kmap;
  698. struct map *map;
  699. map = map__new2(xm->start, kernel);
  700. if (!map)
  701. return -1;
  702. map->end = xm->end;
  703. map->pgoff = xm->pgoff;
  704. kmap = map__kmap(map);
  705. kmap->kmaps = &machine->kmaps;
  706. strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
  707. map_groups__insert(&machine->kmaps, map);
  708. pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
  709. kmap->name, map->start, map->end);
  710. map__put(map);
  711. return 0;
  712. }
  713. static u64 find_entry_trampoline(struct dso *dso)
  714. {
  715. /* Duplicates are removed so lookup all aliases */
  716. const char *syms[] = {
  717. "_entry_trampoline",
  718. "__entry_trampoline_start",
  719. "entry_SYSCALL_64_trampoline",
  720. };
  721. struct symbol *sym = dso__first_symbol(dso);
  722. unsigned int i;
  723. for (; sym; sym = dso__next_symbol(sym)) {
  724. if (sym->binding != STB_GLOBAL)
  725. continue;
  726. for (i = 0; i < ARRAY_SIZE(syms); i++) {
  727. if (!strcmp(sym->name, syms[i]))
  728. return sym->start;
  729. }
  730. }
  731. return 0;
  732. }
  733. /*
  734. * These values can be used for kernels that do not have symbols for the entry
  735. * trampolines in kallsyms.
  736. */
  737. #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
  738. #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
  739. #define X86_64_ENTRY_TRAMPOLINE 0x6000
  740. /* Map x86_64 PTI entry trampolines */
  741. int machine__map_x86_64_entry_trampolines(struct machine *machine,
  742. struct dso *kernel)
  743. {
  744. struct map_groups *kmaps = &machine->kmaps;
  745. struct maps *maps = &kmaps->maps;
  746. int nr_cpus_avail, cpu;
  747. bool found = false;
  748. struct map *map;
  749. u64 pgoff;
  750. /*
  751. * In the vmlinux case, pgoff is a virtual address which must now be
  752. * mapped to a vmlinux offset.
  753. */
  754. for (map = maps__first(maps); map; map = map__next(map)) {
  755. struct kmap *kmap = __map__kmap(map);
  756. struct map *dest_map;
  757. if (!kmap || !is_entry_trampoline(kmap->name))
  758. continue;
  759. dest_map = map_groups__find(kmaps, map->pgoff);
  760. if (dest_map != map)
  761. map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
  762. found = true;
  763. }
  764. if (found || machine->trampolines_mapped)
  765. return 0;
  766. pgoff = find_entry_trampoline(kernel);
  767. if (!pgoff)
  768. return 0;
  769. nr_cpus_avail = machine__nr_cpus_avail(machine);
  770. /* Add a 1 page map for each CPU's entry trampoline */
  771. for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
  772. u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
  773. cpu * X86_64_CPU_ENTRY_AREA_SIZE +
  774. X86_64_ENTRY_TRAMPOLINE;
  775. struct extra_kernel_map xm = {
  776. .start = va,
  777. .end = va + page_size,
  778. .pgoff = pgoff,
  779. };
  780. strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
  781. if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
  782. return -1;
  783. }
  784. machine->trampolines_mapped = nr_cpus_avail;
  785. return 0;
  786. }
  787. int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
  788. struct dso *kernel __maybe_unused)
  789. {
  790. return 0;
  791. }
  792. static int
  793. __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  794. {
  795. struct kmap *kmap;
  796. struct map *map;
  797. /* In case of renewal the kernel map, destroy previous one */
  798. machine__destroy_kernel_maps(machine);
  799. machine->vmlinux_map = map__new2(0, kernel);
  800. if (machine->vmlinux_map == NULL)
  801. return -1;
  802. machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
  803. map = machine__kernel_map(machine);
  804. kmap = map__kmap(map);
  805. if (!kmap)
  806. return -1;
  807. kmap->kmaps = &machine->kmaps;
  808. map_groups__insert(&machine->kmaps, map);
  809. return 0;
  810. }
  811. void machine__destroy_kernel_maps(struct machine *machine)
  812. {
  813. struct kmap *kmap;
  814. struct map *map = machine__kernel_map(machine);
  815. if (map == NULL)
  816. return;
  817. kmap = map__kmap(map);
  818. map_groups__remove(&machine->kmaps, map);
  819. if (kmap && kmap->ref_reloc_sym) {
  820. zfree((char **)&kmap->ref_reloc_sym->name);
  821. zfree(&kmap->ref_reloc_sym);
  822. }
  823. map__zput(machine->vmlinux_map);
  824. }
  825. int machines__create_guest_kernel_maps(struct machines *machines)
  826. {
  827. int ret = 0;
  828. struct dirent **namelist = NULL;
  829. int i, items = 0;
  830. char path[PATH_MAX];
  831. pid_t pid;
  832. char *endp;
  833. if (symbol_conf.default_guest_vmlinux_name ||
  834. symbol_conf.default_guest_modules ||
  835. symbol_conf.default_guest_kallsyms) {
  836. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  837. }
  838. if (symbol_conf.guestmount) {
  839. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  840. if (items <= 0)
  841. return -ENOENT;
  842. for (i = 0; i < items; i++) {
  843. if (!isdigit(namelist[i]->d_name[0])) {
  844. /* Filter out . and .. */
  845. continue;
  846. }
  847. pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
  848. if ((*endp != '\0') ||
  849. (endp == namelist[i]->d_name) ||
  850. (errno == ERANGE)) {
  851. pr_debug("invalid directory (%s). Skipping.\n",
  852. namelist[i]->d_name);
  853. continue;
  854. }
  855. sprintf(path, "%s/%s/proc/kallsyms",
  856. symbol_conf.guestmount,
  857. namelist[i]->d_name);
  858. ret = access(path, R_OK);
  859. if (ret) {
  860. pr_debug("Can't access file %s\n", path);
  861. goto failure;
  862. }
  863. machines__create_kernel_maps(machines, pid);
  864. }
  865. failure:
  866. free(namelist);
  867. }
  868. return ret;
  869. }
  870. void machines__destroy_kernel_maps(struct machines *machines)
  871. {
  872. struct rb_node *next = rb_first(&machines->guests);
  873. machine__destroy_kernel_maps(&machines->host);
  874. while (next) {
  875. struct machine *pos = rb_entry(next, struct machine, rb_node);
  876. next = rb_next(&pos->rb_node);
  877. rb_erase(&pos->rb_node, &machines->guests);
  878. machine__delete(pos);
  879. }
  880. }
  881. int machines__create_kernel_maps(struct machines *machines, pid_t pid)
  882. {
  883. struct machine *machine = machines__findnew(machines, pid);
  884. if (machine == NULL)
  885. return -1;
  886. return machine__create_kernel_maps(machine);
  887. }
  888. int machine__load_kallsyms(struct machine *machine, const char *filename)
  889. {
  890. struct map *map = machine__kernel_map(machine);
  891. int ret = __dso__load_kallsyms(map->dso, filename, map, true);
  892. if (ret > 0) {
  893. dso__set_loaded(map->dso);
  894. /*
  895. * Since /proc/kallsyms will have multiple sessions for the
  896. * kernel, with modules between them, fixup the end of all
  897. * sections.
  898. */
  899. map_groups__fixup_end(&machine->kmaps);
  900. }
  901. return ret;
  902. }
  903. int machine__load_vmlinux_path(struct machine *machine)
  904. {
  905. struct map *map = machine__kernel_map(machine);
  906. int ret = dso__load_vmlinux_path(map->dso, map);
  907. if (ret > 0)
  908. dso__set_loaded(map->dso);
  909. return ret;
  910. }
  911. static char *get_kernel_version(const char *root_dir)
  912. {
  913. char version[PATH_MAX];
  914. FILE *file;
  915. char *name, *tmp;
  916. const char *prefix = "Linux version ";
  917. sprintf(version, "%s/proc/version", root_dir);
  918. file = fopen(version, "r");
  919. if (!file)
  920. return NULL;
  921. version[0] = '\0';
  922. tmp = fgets(version, sizeof(version), file);
  923. fclose(file);
  924. name = strstr(version, prefix);
  925. if (!name)
  926. return NULL;
  927. name += strlen(prefix);
  928. tmp = strchr(name, ' ');
  929. if (tmp)
  930. *tmp = '\0';
  931. return strdup(name);
  932. }
  933. static bool is_kmod_dso(struct dso *dso)
  934. {
  935. return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  936. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
  937. }
  938. static int map_groups__set_module_path(struct map_groups *mg, const char *path,
  939. struct kmod_path *m)
  940. {
  941. char *long_name;
  942. struct map *map = map_groups__find_by_name(mg, m->name);
  943. if (map == NULL)
  944. return 0;
  945. long_name = strdup(path);
  946. if (long_name == NULL)
  947. return -ENOMEM;
  948. dso__set_long_name(map->dso, long_name, true);
  949. dso__kernel_module_get_build_id(map->dso, "");
  950. /*
  951. * Full name could reveal us kmod compression, so
  952. * we need to update the symtab_type if needed.
  953. */
  954. if (m->comp && is_kmod_dso(map->dso))
  955. map->dso->symtab_type++;
  956. return 0;
  957. }
  958. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  959. const char *dir_name, int depth)
  960. {
  961. struct dirent *dent;
  962. DIR *dir = opendir(dir_name);
  963. int ret = 0;
  964. if (!dir) {
  965. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  966. return -1;
  967. }
  968. while ((dent = readdir(dir)) != NULL) {
  969. char path[PATH_MAX];
  970. struct stat st;
  971. /*sshfs might return bad dent->d_type, so we have to stat*/
  972. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  973. if (stat(path, &st))
  974. continue;
  975. if (S_ISDIR(st.st_mode)) {
  976. if (!strcmp(dent->d_name, ".") ||
  977. !strcmp(dent->d_name, ".."))
  978. continue;
  979. /* Do not follow top-level source and build symlinks */
  980. if (depth == 0) {
  981. if (!strcmp(dent->d_name, "source") ||
  982. !strcmp(dent->d_name, "build"))
  983. continue;
  984. }
  985. ret = map_groups__set_modules_path_dir(mg, path,
  986. depth + 1);
  987. if (ret < 0)
  988. goto out;
  989. } else {
  990. struct kmod_path m;
  991. ret = kmod_path__parse_name(&m, dent->d_name);
  992. if (ret)
  993. goto out;
  994. if (m.kmod)
  995. ret = map_groups__set_module_path(mg, path, &m);
  996. free(m.name);
  997. if (ret)
  998. goto out;
  999. }
  1000. }
  1001. out:
  1002. closedir(dir);
  1003. return ret;
  1004. }
  1005. static int machine__set_modules_path(struct machine *machine)
  1006. {
  1007. char *version;
  1008. char modules_path[PATH_MAX];
  1009. version = get_kernel_version(machine->root_dir);
  1010. if (!version)
  1011. return -1;
  1012. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
  1013. machine->root_dir, version);
  1014. free(version);
  1015. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
  1016. }
  1017. int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
  1018. const char *name __maybe_unused)
  1019. {
  1020. return 0;
  1021. }
  1022. static int machine__create_module(void *arg, const char *name, u64 start,
  1023. u64 size)
  1024. {
  1025. struct machine *machine = arg;
  1026. struct map *map;
  1027. if (arch__fix_module_text_start(&start, name) < 0)
  1028. return -1;
  1029. map = machine__findnew_module_map(machine, start, name);
  1030. if (map == NULL)
  1031. return -1;
  1032. map->end = start + size;
  1033. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1034. return 0;
  1035. }
  1036. static int machine__create_modules(struct machine *machine)
  1037. {
  1038. const char *modules;
  1039. char path[PATH_MAX];
  1040. if (machine__is_default_guest(machine)) {
  1041. modules = symbol_conf.default_guest_modules;
  1042. } else {
  1043. snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
  1044. modules = path;
  1045. }
  1046. if (symbol__restricted_filename(modules, "/proc/modules"))
  1047. return -1;
  1048. if (modules__parse(modules, machine, machine__create_module))
  1049. return -1;
  1050. if (!machine__set_modules_path(machine))
  1051. return 0;
  1052. pr_debug("Problems setting modules path maps, continuing anyway...\n");
  1053. return 0;
  1054. }
  1055. static void machine__set_kernel_mmap(struct machine *machine,
  1056. u64 start, u64 end)
  1057. {
  1058. machine->vmlinux_map->start = start;
  1059. machine->vmlinux_map->end = end;
  1060. /*
  1061. * Be a bit paranoid here, some perf.data file came with
  1062. * a zero sized synthesized MMAP event for the kernel.
  1063. */
  1064. if (start == 0 && end == 0)
  1065. machine->vmlinux_map->end = ~0ULL;
  1066. }
  1067. int machine__create_kernel_maps(struct machine *machine)
  1068. {
  1069. struct dso *kernel = machine__get_kernel(machine);
  1070. const char *name = NULL;
  1071. struct map *map;
  1072. u64 addr = 0;
  1073. int ret;
  1074. if (kernel == NULL)
  1075. return -1;
  1076. ret = __machine__create_kernel_maps(machine, kernel);
  1077. if (ret < 0)
  1078. goto out_put;
  1079. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  1080. if (machine__is_host(machine))
  1081. pr_debug("Problems creating module maps, "
  1082. "continuing anyway...\n");
  1083. else
  1084. pr_debug("Problems creating module maps for guest %d, "
  1085. "continuing anyway...\n", machine->pid);
  1086. }
  1087. if (!machine__get_running_kernel_start(machine, &name, &addr)) {
  1088. if (name &&
  1089. map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, addr)) {
  1090. machine__destroy_kernel_maps(machine);
  1091. ret = -1;
  1092. goto out_put;
  1093. }
  1094. /* we have a real start address now, so re-order the kmaps */
  1095. map = machine__kernel_map(machine);
  1096. map__get(map);
  1097. map_groups__remove(&machine->kmaps, map);
  1098. /* assume it's the last in the kmaps */
  1099. machine__set_kernel_mmap(machine, addr, ~0ULL);
  1100. map_groups__insert(&machine->kmaps, map);
  1101. map__put(map);
  1102. }
  1103. if (machine__create_extra_kernel_maps(machine, kernel))
  1104. pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
  1105. /* update end address of the kernel map using adjacent module address */
  1106. map = map__next(machine__kernel_map(machine));
  1107. if (map)
  1108. machine__set_kernel_mmap(machine, addr, map->start);
  1109. out_put:
  1110. dso__put(kernel);
  1111. return ret;
  1112. }
  1113. static bool machine__uses_kcore(struct machine *machine)
  1114. {
  1115. struct dso *dso;
  1116. list_for_each_entry(dso, &machine->dsos.head, node) {
  1117. if (dso__is_kcore(dso))
  1118. return true;
  1119. }
  1120. return false;
  1121. }
  1122. static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
  1123. union perf_event *event)
  1124. {
  1125. return machine__is(machine, "x86_64") &&
  1126. is_entry_trampoline(event->mmap.filename);
  1127. }
  1128. static int machine__process_extra_kernel_map(struct machine *machine,
  1129. union perf_event *event)
  1130. {
  1131. struct map *kernel_map = machine__kernel_map(machine);
  1132. struct dso *kernel = kernel_map ? kernel_map->dso : NULL;
  1133. struct extra_kernel_map xm = {
  1134. .start = event->mmap.start,
  1135. .end = event->mmap.start + event->mmap.len,
  1136. .pgoff = event->mmap.pgoff,
  1137. };
  1138. if (kernel == NULL)
  1139. return -1;
  1140. strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
  1141. return machine__create_extra_kernel_map(machine, kernel, &xm);
  1142. }
  1143. static int machine__process_kernel_mmap_event(struct machine *machine,
  1144. union perf_event *event)
  1145. {
  1146. struct map *map;
  1147. enum dso_kernel_type kernel_type;
  1148. bool is_kernel_mmap;
  1149. /* If we have maps from kcore then we do not need or want any others */
  1150. if (machine__uses_kcore(machine))
  1151. return 0;
  1152. if (machine__is_host(machine))
  1153. kernel_type = DSO_TYPE_KERNEL;
  1154. else
  1155. kernel_type = DSO_TYPE_GUEST_KERNEL;
  1156. is_kernel_mmap = memcmp(event->mmap.filename,
  1157. machine->mmap_name,
  1158. strlen(machine->mmap_name) - 1) == 0;
  1159. if (event->mmap.filename[0] == '/' ||
  1160. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  1161. map = machine__findnew_module_map(machine, event->mmap.start,
  1162. event->mmap.filename);
  1163. if (map == NULL)
  1164. goto out_problem;
  1165. map->end = map->start + event->mmap.len;
  1166. } else if (is_kernel_mmap) {
  1167. const char *symbol_name = (event->mmap.filename +
  1168. strlen(machine->mmap_name));
  1169. /*
  1170. * Should be there already, from the build-id table in
  1171. * the header.
  1172. */
  1173. struct dso *kernel = NULL;
  1174. struct dso *dso;
  1175. down_read(&machine->dsos.lock);
  1176. list_for_each_entry(dso, &machine->dsos.head, node) {
  1177. /*
  1178. * The cpumode passed to is_kernel_module is not the
  1179. * cpumode of *this* event. If we insist on passing
  1180. * correct cpumode to is_kernel_module, we should
  1181. * record the cpumode when we adding this dso to the
  1182. * linked list.
  1183. *
  1184. * However we don't really need passing correct
  1185. * cpumode. We know the correct cpumode must be kernel
  1186. * mode (if not, we should not link it onto kernel_dsos
  1187. * list).
  1188. *
  1189. * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
  1190. * is_kernel_module() treats it as a kernel cpumode.
  1191. */
  1192. if (!dso->kernel ||
  1193. is_kernel_module(dso->long_name,
  1194. PERF_RECORD_MISC_CPUMODE_UNKNOWN))
  1195. continue;
  1196. kernel = dso;
  1197. break;
  1198. }
  1199. up_read(&machine->dsos.lock);
  1200. if (kernel == NULL)
  1201. kernel = machine__findnew_dso(machine, machine->mmap_name);
  1202. if (kernel == NULL)
  1203. goto out_problem;
  1204. kernel->kernel = kernel_type;
  1205. if (__machine__create_kernel_maps(machine, kernel) < 0) {
  1206. dso__put(kernel);
  1207. goto out_problem;
  1208. }
  1209. if (strstr(kernel->long_name, "vmlinux"))
  1210. dso__set_short_name(kernel, "[kernel.vmlinux]", false);
  1211. machine__set_kernel_mmap(machine, event->mmap.start,
  1212. event->mmap.start + event->mmap.len);
  1213. /*
  1214. * Avoid using a zero address (kptr_restrict) for the ref reloc
  1215. * symbol. Effectively having zero here means that at record
  1216. * time /proc/sys/kernel/kptr_restrict was non zero.
  1217. */
  1218. if (event->mmap.pgoff != 0) {
  1219. map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
  1220. symbol_name,
  1221. event->mmap.pgoff);
  1222. }
  1223. if (machine__is_default_guest(machine)) {
  1224. /*
  1225. * preload dso of guest kernel and modules
  1226. */
  1227. dso__load(kernel, machine__kernel_map(machine));
  1228. }
  1229. } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
  1230. return machine__process_extra_kernel_map(machine, event);
  1231. }
  1232. return 0;
  1233. out_problem:
  1234. return -1;
  1235. }
  1236. int machine__process_mmap2_event(struct machine *machine,
  1237. union perf_event *event,
  1238. struct perf_sample *sample)
  1239. {
  1240. struct thread *thread;
  1241. struct map *map;
  1242. int ret = 0;
  1243. if (dump_trace)
  1244. perf_event__fprintf_mmap2(event, stdout);
  1245. if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  1246. sample->cpumode == PERF_RECORD_MISC_KERNEL) {
  1247. ret = machine__process_kernel_mmap_event(machine, event);
  1248. if (ret < 0)
  1249. goto out_problem;
  1250. return 0;
  1251. }
  1252. thread = machine__findnew_thread(machine, event->mmap2.pid,
  1253. event->mmap2.tid);
  1254. if (thread == NULL)
  1255. goto out_problem;
  1256. map = map__new(machine, event->mmap2.start,
  1257. event->mmap2.len, event->mmap2.pgoff,
  1258. event->mmap2.maj,
  1259. event->mmap2.min, event->mmap2.ino,
  1260. event->mmap2.ino_generation,
  1261. event->mmap2.prot,
  1262. event->mmap2.flags,
  1263. event->mmap2.filename, thread);
  1264. if (map == NULL)
  1265. goto out_problem_map;
  1266. ret = thread__insert_map(thread, map);
  1267. if (ret)
  1268. goto out_problem_insert;
  1269. thread__put(thread);
  1270. map__put(map);
  1271. return 0;
  1272. out_problem_insert:
  1273. map__put(map);
  1274. out_problem_map:
  1275. thread__put(thread);
  1276. out_problem:
  1277. dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
  1278. return 0;
  1279. }
  1280. int machine__process_mmap_event(struct machine *machine, union perf_event *event,
  1281. struct perf_sample *sample)
  1282. {
  1283. struct thread *thread;
  1284. struct map *map;
  1285. u32 prot = 0;
  1286. int ret = 0;
  1287. if (dump_trace)
  1288. perf_event__fprintf_mmap(event, stdout);
  1289. if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  1290. sample->cpumode == PERF_RECORD_MISC_KERNEL) {
  1291. ret = machine__process_kernel_mmap_event(machine, event);
  1292. if (ret < 0)
  1293. goto out_problem;
  1294. return 0;
  1295. }
  1296. thread = machine__findnew_thread(machine, event->mmap.pid,
  1297. event->mmap.tid);
  1298. if (thread == NULL)
  1299. goto out_problem;
  1300. if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
  1301. prot = PROT_EXEC;
  1302. map = map__new(machine, event->mmap.start,
  1303. event->mmap.len, event->mmap.pgoff,
  1304. 0, 0, 0, 0, prot, 0,
  1305. event->mmap.filename,
  1306. thread);
  1307. if (map == NULL)
  1308. goto out_problem_map;
  1309. ret = thread__insert_map(thread, map);
  1310. if (ret)
  1311. goto out_problem_insert;
  1312. thread__put(thread);
  1313. map__put(map);
  1314. return 0;
  1315. out_problem_insert:
  1316. map__put(map);
  1317. out_problem_map:
  1318. thread__put(thread);
  1319. out_problem:
  1320. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  1321. return 0;
  1322. }
  1323. static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
  1324. {
  1325. struct threads *threads = machine__threads(machine, th->tid);
  1326. if (threads->last_match == th)
  1327. threads->last_match = NULL;
  1328. BUG_ON(refcount_read(&th->refcnt) == 0);
  1329. if (lock)
  1330. down_write(&threads->lock);
  1331. rb_erase_init(&th->rb_node, &threads->entries);
  1332. RB_CLEAR_NODE(&th->rb_node);
  1333. --threads->nr;
  1334. /*
  1335. * Move it first to the dead_threads list, then drop the reference,
  1336. * if this is the last reference, then the thread__delete destructor
  1337. * will be called and we will remove it from the dead_threads list.
  1338. */
  1339. list_add_tail(&th->node, &threads->dead);
  1340. if (lock)
  1341. up_write(&threads->lock);
  1342. thread__put(th);
  1343. }
  1344. void machine__remove_thread(struct machine *machine, struct thread *th)
  1345. {
  1346. return __machine__remove_thread(machine, th, true);
  1347. }
  1348. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  1349. struct perf_sample *sample)
  1350. {
  1351. struct thread *thread = machine__find_thread(machine,
  1352. event->fork.pid,
  1353. event->fork.tid);
  1354. struct thread *parent = machine__findnew_thread(machine,
  1355. event->fork.ppid,
  1356. event->fork.ptid);
  1357. int err = 0;
  1358. if (dump_trace)
  1359. perf_event__fprintf_task(event, stdout);
  1360. /*
  1361. * There may be an existing thread that is not actually the parent,
  1362. * either because we are processing events out of order, or because the
  1363. * (fork) event that would have removed the thread was lost. Assume the
  1364. * latter case and continue on as best we can.
  1365. */
  1366. if (parent->pid_ != (pid_t)event->fork.ppid) {
  1367. dump_printf("removing erroneous parent thread %d/%d\n",
  1368. parent->pid_, parent->tid);
  1369. machine__remove_thread(machine, parent);
  1370. thread__put(parent);
  1371. parent = machine__findnew_thread(machine, event->fork.ppid,
  1372. event->fork.ptid);
  1373. }
  1374. /* if a thread currently exists for the thread id remove it */
  1375. if (thread != NULL) {
  1376. machine__remove_thread(machine, thread);
  1377. thread__put(thread);
  1378. }
  1379. thread = machine__findnew_thread(machine, event->fork.pid,
  1380. event->fork.tid);
  1381. if (thread == NULL || parent == NULL ||
  1382. thread__fork(thread, parent, sample->time) < 0) {
  1383. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  1384. err = -1;
  1385. }
  1386. thread__put(thread);
  1387. thread__put(parent);
  1388. return err;
  1389. }
  1390. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  1391. struct perf_sample *sample __maybe_unused)
  1392. {
  1393. struct thread *thread = machine__find_thread(machine,
  1394. event->fork.pid,
  1395. event->fork.tid);
  1396. if (dump_trace)
  1397. perf_event__fprintf_task(event, stdout);
  1398. if (thread != NULL) {
  1399. thread__exited(thread);
  1400. thread__put(thread);
  1401. }
  1402. return 0;
  1403. }
  1404. int machine__process_event(struct machine *machine, union perf_event *event,
  1405. struct perf_sample *sample)
  1406. {
  1407. int ret;
  1408. switch (event->header.type) {
  1409. case PERF_RECORD_COMM:
  1410. ret = machine__process_comm_event(machine, event, sample); break;
  1411. case PERF_RECORD_MMAP:
  1412. ret = machine__process_mmap_event(machine, event, sample); break;
  1413. case PERF_RECORD_NAMESPACES:
  1414. ret = machine__process_namespaces_event(machine, event, sample); break;
  1415. case PERF_RECORD_MMAP2:
  1416. ret = machine__process_mmap2_event(machine, event, sample); break;
  1417. case PERF_RECORD_FORK:
  1418. ret = machine__process_fork_event(machine, event, sample); break;
  1419. case PERF_RECORD_EXIT:
  1420. ret = machine__process_exit_event(machine, event, sample); break;
  1421. case PERF_RECORD_LOST:
  1422. ret = machine__process_lost_event(machine, event, sample); break;
  1423. case PERF_RECORD_AUX:
  1424. ret = machine__process_aux_event(machine, event); break;
  1425. case PERF_RECORD_ITRACE_START:
  1426. ret = machine__process_itrace_start_event(machine, event); break;
  1427. case PERF_RECORD_LOST_SAMPLES:
  1428. ret = machine__process_lost_samples_event(machine, event, sample); break;
  1429. case PERF_RECORD_SWITCH:
  1430. case PERF_RECORD_SWITCH_CPU_WIDE:
  1431. ret = machine__process_switch_event(machine, event); break;
  1432. default:
  1433. ret = -1;
  1434. break;
  1435. }
  1436. return ret;
  1437. }
  1438. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  1439. {
  1440. if (!regexec(regex, sym->name, 0, NULL, 0))
  1441. return 1;
  1442. return 0;
  1443. }
  1444. static void ip__resolve_ams(struct thread *thread,
  1445. struct addr_map_symbol *ams,
  1446. u64 ip)
  1447. {
  1448. struct addr_location al;
  1449. memset(&al, 0, sizeof(al));
  1450. /*
  1451. * We cannot use the header.misc hint to determine whether a
  1452. * branch stack address is user, kernel, guest, hypervisor.
  1453. * Branches may straddle the kernel/user/hypervisor boundaries.
  1454. * Thus, we have to try consecutively until we find a match
  1455. * or else, the symbol is unknown
  1456. */
  1457. thread__find_cpumode_addr_location(thread, ip, &al);
  1458. ams->addr = ip;
  1459. ams->al_addr = al.addr;
  1460. ams->sym = al.sym;
  1461. ams->map = al.map;
  1462. ams->phys_addr = 0;
  1463. }
  1464. static void ip__resolve_data(struct thread *thread,
  1465. u8 m, struct addr_map_symbol *ams,
  1466. u64 addr, u64 phys_addr)
  1467. {
  1468. struct addr_location al;
  1469. memset(&al, 0, sizeof(al));
  1470. thread__find_symbol(thread, m, addr, &al);
  1471. ams->addr = addr;
  1472. ams->al_addr = al.addr;
  1473. ams->sym = al.sym;
  1474. ams->map = al.map;
  1475. ams->phys_addr = phys_addr;
  1476. }
  1477. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  1478. struct addr_location *al)
  1479. {
  1480. struct mem_info *mi = mem_info__new();
  1481. if (!mi)
  1482. return NULL;
  1483. ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
  1484. ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
  1485. sample->addr, sample->phys_addr);
  1486. mi->data_src.val = sample->data_src;
  1487. return mi;
  1488. }
  1489. static char *callchain_srcline(struct map *map, struct symbol *sym, u64 ip)
  1490. {
  1491. char *srcline = NULL;
  1492. if (!map || callchain_param.key == CCKEY_FUNCTION)
  1493. return srcline;
  1494. srcline = srcline__tree_find(&map->dso->srclines, ip);
  1495. if (!srcline) {
  1496. bool show_sym = false;
  1497. bool show_addr = callchain_param.key == CCKEY_ADDRESS;
  1498. srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
  1499. sym, show_sym, show_addr, ip);
  1500. srcline__tree_insert(&map->dso->srclines, ip, srcline);
  1501. }
  1502. return srcline;
  1503. }
  1504. struct iterations {
  1505. int nr_loop_iter;
  1506. u64 cycles;
  1507. };
  1508. static int add_callchain_ip(struct thread *thread,
  1509. struct callchain_cursor *cursor,
  1510. struct symbol **parent,
  1511. struct addr_location *root_al,
  1512. u8 *cpumode,
  1513. u64 ip,
  1514. bool branch,
  1515. struct branch_flags *flags,
  1516. struct iterations *iter,
  1517. u64 branch_from)
  1518. {
  1519. struct addr_location al;
  1520. int nr_loop_iter = 0;
  1521. u64 iter_cycles = 0;
  1522. const char *srcline = NULL;
  1523. al.filtered = 0;
  1524. al.sym = NULL;
  1525. if (!cpumode) {
  1526. thread__find_cpumode_addr_location(thread, ip, &al);
  1527. } else {
  1528. if (ip >= PERF_CONTEXT_MAX) {
  1529. switch (ip) {
  1530. case PERF_CONTEXT_HV:
  1531. *cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1532. break;
  1533. case PERF_CONTEXT_KERNEL:
  1534. *cpumode = PERF_RECORD_MISC_KERNEL;
  1535. break;
  1536. case PERF_CONTEXT_USER:
  1537. *cpumode = PERF_RECORD_MISC_USER;
  1538. break;
  1539. default:
  1540. pr_debug("invalid callchain context: "
  1541. "%"PRId64"\n", (s64) ip);
  1542. /*
  1543. * It seems the callchain is corrupted.
  1544. * Discard all.
  1545. */
  1546. callchain_cursor_reset(cursor);
  1547. return 1;
  1548. }
  1549. return 0;
  1550. }
  1551. thread__find_symbol(thread, *cpumode, ip, &al);
  1552. }
  1553. if (al.sym != NULL) {
  1554. if (perf_hpp_list.parent && !*parent &&
  1555. symbol__match_regex(al.sym, &parent_regex))
  1556. *parent = al.sym;
  1557. else if (have_ignore_callees && root_al &&
  1558. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1559. /* Treat this symbol as the root,
  1560. forgetting its callees. */
  1561. *root_al = al;
  1562. callchain_cursor_reset(cursor);
  1563. }
  1564. }
  1565. if (symbol_conf.hide_unresolved && al.sym == NULL)
  1566. return 0;
  1567. if (iter) {
  1568. nr_loop_iter = iter->nr_loop_iter;
  1569. iter_cycles = iter->cycles;
  1570. }
  1571. srcline = callchain_srcline(al.map, al.sym, al.addr);
  1572. return callchain_cursor_append(cursor, ip, al.map, al.sym,
  1573. branch, flags, nr_loop_iter,
  1574. iter_cycles, branch_from, srcline);
  1575. }
  1576. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  1577. struct addr_location *al)
  1578. {
  1579. unsigned int i;
  1580. const struct branch_stack *bs = sample->branch_stack;
  1581. struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
  1582. if (!bi)
  1583. return NULL;
  1584. for (i = 0; i < bs->nr; i++) {
  1585. ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
  1586. ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
  1587. bi[i].flags = bs->entries[i].flags;
  1588. }
  1589. return bi;
  1590. }
  1591. static void save_iterations(struct iterations *iter,
  1592. struct branch_entry *be, int nr)
  1593. {
  1594. int i;
  1595. iter->nr_loop_iter = nr;
  1596. iter->cycles = 0;
  1597. for (i = 0; i < nr; i++)
  1598. iter->cycles += be[i].flags.cycles;
  1599. }
  1600. #define CHASHSZ 127
  1601. #define CHASHBITS 7
  1602. #define NO_ENTRY 0xff
  1603. #define PERF_MAX_BRANCH_DEPTH 127
  1604. /* Remove loops. */
  1605. static int remove_loops(struct branch_entry *l, int nr,
  1606. struct iterations *iter)
  1607. {
  1608. int i, j, off;
  1609. unsigned char chash[CHASHSZ];
  1610. memset(chash, NO_ENTRY, sizeof(chash));
  1611. BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
  1612. for (i = 0; i < nr; i++) {
  1613. int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
  1614. /* no collision handling for now */
  1615. if (chash[h] == NO_ENTRY) {
  1616. chash[h] = i;
  1617. } else if (l[chash[h]].from == l[i].from) {
  1618. bool is_loop = true;
  1619. /* check if it is a real loop */
  1620. off = 0;
  1621. for (j = chash[h]; j < i && i + off < nr; j++, off++)
  1622. if (l[j].from != l[i + off].from) {
  1623. is_loop = false;
  1624. break;
  1625. }
  1626. if (is_loop) {
  1627. j = nr - (i + off);
  1628. if (j > 0) {
  1629. save_iterations(iter + i + off,
  1630. l + i, off);
  1631. memmove(iter + i, iter + i + off,
  1632. j * sizeof(*iter));
  1633. memmove(l + i, l + i + off,
  1634. j * sizeof(*l));
  1635. }
  1636. nr -= off;
  1637. }
  1638. }
  1639. }
  1640. return nr;
  1641. }
  1642. /*
  1643. * Recolve LBR callstack chain sample
  1644. * Return:
  1645. * 1 on success get LBR callchain information
  1646. * 0 no available LBR callchain information, should try fp
  1647. * negative error code on other errors.
  1648. */
  1649. static int resolve_lbr_callchain_sample(struct thread *thread,
  1650. struct callchain_cursor *cursor,
  1651. struct perf_sample *sample,
  1652. struct symbol **parent,
  1653. struct addr_location *root_al,
  1654. int max_stack)
  1655. {
  1656. struct ip_callchain *chain = sample->callchain;
  1657. int chain_nr = min(max_stack, (int)chain->nr), i;
  1658. u8 cpumode = PERF_RECORD_MISC_USER;
  1659. u64 ip, branch_from = 0;
  1660. for (i = 0; i < chain_nr; i++) {
  1661. if (chain->ips[i] == PERF_CONTEXT_USER)
  1662. break;
  1663. }
  1664. /* LBR only affects the user callchain */
  1665. if (i != chain_nr) {
  1666. struct branch_stack *lbr_stack = sample->branch_stack;
  1667. int lbr_nr = lbr_stack->nr, j, k;
  1668. bool branch;
  1669. struct branch_flags *flags;
  1670. /*
  1671. * LBR callstack can only get user call chain.
  1672. * The mix_chain_nr is kernel call chain
  1673. * number plus LBR user call chain number.
  1674. * i is kernel call chain number,
  1675. * 1 is PERF_CONTEXT_USER,
  1676. * lbr_nr + 1 is the user call chain number.
  1677. * For details, please refer to the comments
  1678. * in callchain__printf
  1679. */
  1680. int mix_chain_nr = i + 1 + lbr_nr + 1;
  1681. for (j = 0; j < mix_chain_nr; j++) {
  1682. int err;
  1683. branch = false;
  1684. flags = NULL;
  1685. if (callchain_param.order == ORDER_CALLEE) {
  1686. if (j < i + 1)
  1687. ip = chain->ips[j];
  1688. else if (j > i + 1) {
  1689. k = j - i - 2;
  1690. ip = lbr_stack->entries[k].from;
  1691. branch = true;
  1692. flags = &lbr_stack->entries[k].flags;
  1693. } else {
  1694. ip = lbr_stack->entries[0].to;
  1695. branch = true;
  1696. flags = &lbr_stack->entries[0].flags;
  1697. branch_from =
  1698. lbr_stack->entries[0].from;
  1699. }
  1700. } else {
  1701. if (j < lbr_nr) {
  1702. k = lbr_nr - j - 1;
  1703. ip = lbr_stack->entries[k].from;
  1704. branch = true;
  1705. flags = &lbr_stack->entries[k].flags;
  1706. }
  1707. else if (j > lbr_nr)
  1708. ip = chain->ips[i + 1 - (j - lbr_nr)];
  1709. else {
  1710. ip = lbr_stack->entries[0].to;
  1711. branch = true;
  1712. flags = &lbr_stack->entries[0].flags;
  1713. branch_from =
  1714. lbr_stack->entries[0].from;
  1715. }
  1716. }
  1717. err = add_callchain_ip(thread, cursor, parent,
  1718. root_al, &cpumode, ip,
  1719. branch, flags, NULL,
  1720. branch_from);
  1721. if (err)
  1722. return (err < 0) ? err : 0;
  1723. }
  1724. return 1;
  1725. }
  1726. return 0;
  1727. }
  1728. static int thread__resolve_callchain_sample(struct thread *thread,
  1729. struct callchain_cursor *cursor,
  1730. struct perf_evsel *evsel,
  1731. struct perf_sample *sample,
  1732. struct symbol **parent,
  1733. struct addr_location *root_al,
  1734. int max_stack)
  1735. {
  1736. struct branch_stack *branch = sample->branch_stack;
  1737. struct ip_callchain *chain = sample->callchain;
  1738. int chain_nr = 0;
  1739. u8 cpumode = PERF_RECORD_MISC_USER;
  1740. int i, j, err, nr_entries;
  1741. int skip_idx = -1;
  1742. int first_call = 0;
  1743. if (chain)
  1744. chain_nr = chain->nr;
  1745. if (perf_evsel__has_branch_callstack(evsel)) {
  1746. err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
  1747. root_al, max_stack);
  1748. if (err)
  1749. return (err < 0) ? err : 0;
  1750. }
  1751. /*
  1752. * Based on DWARF debug information, some architectures skip
  1753. * a callchain entry saved by the kernel.
  1754. */
  1755. skip_idx = arch_skip_callchain_idx(thread, chain);
  1756. /*
  1757. * Add branches to call stack for easier browsing. This gives
  1758. * more context for a sample than just the callers.
  1759. *
  1760. * This uses individual histograms of paths compared to the
  1761. * aggregated histograms the normal LBR mode uses.
  1762. *
  1763. * Limitations for now:
  1764. * - No extra filters
  1765. * - No annotations (should annotate somehow)
  1766. */
  1767. if (branch && callchain_param.branch_callstack) {
  1768. int nr = min(max_stack, (int)branch->nr);
  1769. struct branch_entry be[nr];
  1770. struct iterations iter[nr];
  1771. if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
  1772. pr_warning("corrupted branch chain. skipping...\n");
  1773. goto check_calls;
  1774. }
  1775. for (i = 0; i < nr; i++) {
  1776. if (callchain_param.order == ORDER_CALLEE) {
  1777. be[i] = branch->entries[i];
  1778. if (chain == NULL)
  1779. continue;
  1780. /*
  1781. * Check for overlap into the callchain.
  1782. * The return address is one off compared to
  1783. * the branch entry. To adjust for this
  1784. * assume the calling instruction is not longer
  1785. * than 8 bytes.
  1786. */
  1787. if (i == skip_idx ||
  1788. chain->ips[first_call] >= PERF_CONTEXT_MAX)
  1789. first_call++;
  1790. else if (be[i].from < chain->ips[first_call] &&
  1791. be[i].from >= chain->ips[first_call] - 8)
  1792. first_call++;
  1793. } else
  1794. be[i] = branch->entries[branch->nr - i - 1];
  1795. }
  1796. memset(iter, 0, sizeof(struct iterations) * nr);
  1797. nr = remove_loops(be, nr, iter);
  1798. for (i = 0; i < nr; i++) {
  1799. err = add_callchain_ip(thread, cursor, parent,
  1800. root_al,
  1801. NULL, be[i].to,
  1802. true, &be[i].flags,
  1803. NULL, be[i].from);
  1804. if (!err)
  1805. err = add_callchain_ip(thread, cursor, parent, root_al,
  1806. NULL, be[i].from,
  1807. true, &be[i].flags,
  1808. &iter[i], 0);
  1809. if (err == -EINVAL)
  1810. break;
  1811. if (err)
  1812. return err;
  1813. }
  1814. if (chain_nr == 0)
  1815. return 0;
  1816. chain_nr -= nr;
  1817. }
  1818. check_calls:
  1819. for (i = first_call, nr_entries = 0;
  1820. i < chain_nr && nr_entries < max_stack; i++) {
  1821. u64 ip;
  1822. if (callchain_param.order == ORDER_CALLEE)
  1823. j = i;
  1824. else
  1825. j = chain->nr - i - 1;
  1826. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  1827. if (j == skip_idx)
  1828. continue;
  1829. #endif
  1830. ip = chain->ips[j];
  1831. if (ip < PERF_CONTEXT_MAX)
  1832. ++nr_entries;
  1833. err = add_callchain_ip(thread, cursor, parent,
  1834. root_al, &cpumode, ip,
  1835. false, NULL, NULL, 0);
  1836. if (err)
  1837. return (err < 0) ? err : 0;
  1838. }
  1839. return 0;
  1840. }
  1841. static int append_inlines(struct callchain_cursor *cursor,
  1842. struct map *map, struct symbol *sym, u64 ip)
  1843. {
  1844. struct inline_node *inline_node;
  1845. struct inline_list *ilist;
  1846. u64 addr;
  1847. int ret = 1;
  1848. if (!symbol_conf.inline_name || !map || !sym)
  1849. return ret;
  1850. addr = map__rip_2objdump(map, ip);
  1851. inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
  1852. if (!inline_node) {
  1853. inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
  1854. if (!inline_node)
  1855. return ret;
  1856. inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
  1857. }
  1858. list_for_each_entry(ilist, &inline_node->val, list) {
  1859. ret = callchain_cursor_append(cursor, ip, map,
  1860. ilist->symbol, false,
  1861. NULL, 0, 0, 0, ilist->srcline);
  1862. if (ret != 0)
  1863. return ret;
  1864. }
  1865. return ret;
  1866. }
  1867. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1868. {
  1869. struct callchain_cursor *cursor = arg;
  1870. const char *srcline = NULL;
  1871. if (symbol_conf.hide_unresolved && entry->sym == NULL)
  1872. return 0;
  1873. if (append_inlines(cursor, entry->map, entry->sym, entry->ip) == 0)
  1874. return 0;
  1875. srcline = callchain_srcline(entry->map, entry->sym, entry->ip);
  1876. return callchain_cursor_append(cursor, entry->ip,
  1877. entry->map, entry->sym,
  1878. false, NULL, 0, 0, 0, srcline);
  1879. }
  1880. static int thread__resolve_callchain_unwind(struct thread *thread,
  1881. struct callchain_cursor *cursor,
  1882. struct perf_evsel *evsel,
  1883. struct perf_sample *sample,
  1884. int max_stack)
  1885. {
  1886. /* Can we do dwarf post unwind? */
  1887. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1888. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1889. return 0;
  1890. /* Bail out if nothing was captured. */
  1891. if ((!sample->user_regs.regs) ||
  1892. (!sample->user_stack.size))
  1893. return 0;
  1894. return unwind__get_entries(unwind_entry, cursor,
  1895. thread, sample, max_stack);
  1896. }
  1897. int thread__resolve_callchain(struct thread *thread,
  1898. struct callchain_cursor *cursor,
  1899. struct perf_evsel *evsel,
  1900. struct perf_sample *sample,
  1901. struct symbol **parent,
  1902. struct addr_location *root_al,
  1903. int max_stack)
  1904. {
  1905. int ret = 0;
  1906. callchain_cursor_reset(cursor);
  1907. if (callchain_param.order == ORDER_CALLEE) {
  1908. ret = thread__resolve_callchain_sample(thread, cursor,
  1909. evsel, sample,
  1910. parent, root_al,
  1911. max_stack);
  1912. if (ret)
  1913. return ret;
  1914. ret = thread__resolve_callchain_unwind(thread, cursor,
  1915. evsel, sample,
  1916. max_stack);
  1917. } else {
  1918. ret = thread__resolve_callchain_unwind(thread, cursor,
  1919. evsel, sample,
  1920. max_stack);
  1921. if (ret)
  1922. return ret;
  1923. ret = thread__resolve_callchain_sample(thread, cursor,
  1924. evsel, sample,
  1925. parent, root_al,
  1926. max_stack);
  1927. }
  1928. return ret;
  1929. }
  1930. int machine__for_each_thread(struct machine *machine,
  1931. int (*fn)(struct thread *thread, void *p),
  1932. void *priv)
  1933. {
  1934. struct threads *threads;
  1935. struct rb_node *nd;
  1936. struct thread *thread;
  1937. int rc = 0;
  1938. int i;
  1939. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  1940. threads = &machine->threads[i];
  1941. for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
  1942. thread = rb_entry(nd, struct thread, rb_node);
  1943. rc = fn(thread, priv);
  1944. if (rc != 0)
  1945. return rc;
  1946. }
  1947. list_for_each_entry(thread, &threads->dead, node) {
  1948. rc = fn(thread, priv);
  1949. if (rc != 0)
  1950. return rc;
  1951. }
  1952. }
  1953. return rc;
  1954. }
  1955. int machines__for_each_thread(struct machines *machines,
  1956. int (*fn)(struct thread *thread, void *p),
  1957. void *priv)
  1958. {
  1959. struct rb_node *nd;
  1960. int rc = 0;
  1961. rc = machine__for_each_thread(&machines->host, fn, priv);
  1962. if (rc != 0)
  1963. return rc;
  1964. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  1965. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  1966. rc = machine__for_each_thread(machine, fn, priv);
  1967. if (rc != 0)
  1968. return rc;
  1969. }
  1970. return rc;
  1971. }
  1972. int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
  1973. struct target *target, struct thread_map *threads,
  1974. perf_event__handler_t process, bool data_mmap,
  1975. unsigned int proc_map_timeout,
  1976. unsigned int nr_threads_synthesize)
  1977. {
  1978. if (target__has_task(target))
  1979. return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
  1980. else if (target__has_cpu(target))
  1981. return perf_event__synthesize_threads(tool, process,
  1982. machine, data_mmap,
  1983. proc_map_timeout,
  1984. nr_threads_synthesize);
  1985. /* command specified */
  1986. return 0;
  1987. }
  1988. pid_t machine__get_current_tid(struct machine *machine, int cpu)
  1989. {
  1990. if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
  1991. return -1;
  1992. return machine->current_tid[cpu];
  1993. }
  1994. int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
  1995. pid_t tid)
  1996. {
  1997. struct thread *thread;
  1998. if (cpu < 0)
  1999. return -EINVAL;
  2000. if (!machine->current_tid) {
  2001. int i;
  2002. machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
  2003. if (!machine->current_tid)
  2004. return -ENOMEM;
  2005. for (i = 0; i < MAX_NR_CPUS; i++)
  2006. machine->current_tid[i] = -1;
  2007. }
  2008. if (cpu >= MAX_NR_CPUS) {
  2009. pr_err("Requested CPU %d too large. ", cpu);
  2010. pr_err("Consider raising MAX_NR_CPUS\n");
  2011. return -EINVAL;
  2012. }
  2013. machine->current_tid[cpu] = tid;
  2014. thread = machine__findnew_thread(machine, pid, tid);
  2015. if (!thread)
  2016. return -ENOMEM;
  2017. thread->cpu = cpu;
  2018. thread__put(thread);
  2019. return 0;
  2020. }
  2021. /*
  2022. * Compares the raw arch string. N.B. see instead perf_env__arch() if a
  2023. * normalized arch is needed.
  2024. */
  2025. bool machine__is(struct machine *machine, const char *arch)
  2026. {
  2027. return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
  2028. }
  2029. int machine__nr_cpus_avail(struct machine *machine)
  2030. {
  2031. return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
  2032. }
  2033. int machine__get_kernel_start(struct machine *machine)
  2034. {
  2035. struct map *map = machine__kernel_map(machine);
  2036. int err = 0;
  2037. /*
  2038. * The only addresses above 2^63 are kernel addresses of a 64-bit
  2039. * kernel. Note that addresses are unsigned so that on a 32-bit system
  2040. * all addresses including kernel addresses are less than 2^32. In
  2041. * that case (32-bit system), if the kernel mapping is unknown, all
  2042. * addresses will be assumed to be in user space - see
  2043. * machine__kernel_ip().
  2044. */
  2045. machine->kernel_start = 1ULL << 63;
  2046. if (map) {
  2047. err = map__load(map);
  2048. /*
  2049. * On x86_64, PTI entry trampolines are less than the
  2050. * start of kernel text, but still above 2^63. So leave
  2051. * kernel_start = 1ULL << 63 for x86_64.
  2052. */
  2053. if (!err && !machine__is(machine, "x86_64"))
  2054. machine->kernel_start = map->start;
  2055. }
  2056. return err;
  2057. }
  2058. struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
  2059. {
  2060. return dsos__findnew(&machine->dsos, filename);
  2061. }
  2062. char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  2063. {
  2064. struct machine *machine = vmachine;
  2065. struct map *map;
  2066. struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
  2067. if (sym == NULL)
  2068. return NULL;
  2069. *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
  2070. *addrp = map->unmap_ip(map, sym->start);
  2071. return sym->name;
  2072. }