machine.c 52 KB

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