machine.c 49 KB

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