machine.c 39 KB

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