machine.c 56 KB

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