machine.c 31 KB

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