machine.c 28 KB

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