machine.c 33 KB

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