machine.c 46 KB

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