machine.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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_module(void *arg, const char *name, u64 start)
  647. {
  648. struct machine *machine = arg;
  649. struct map *map;
  650. map = machine__new_module(machine, start, name);
  651. if (map == NULL)
  652. return -1;
  653. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  654. return 0;
  655. }
  656. static int machine__create_modules(struct machine *machine)
  657. {
  658. const char *modules;
  659. char path[PATH_MAX];
  660. if (machine__is_default_guest(machine)) {
  661. modules = symbol_conf.default_guest_modules;
  662. } else {
  663. snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
  664. modules = path;
  665. }
  666. if (symbol__restricted_filename(modules, "/proc/modules"))
  667. return -1;
  668. if (modules__parse(modules, machine, machine__create_module))
  669. return -1;
  670. if (!machine__set_modules_path(machine))
  671. return 0;
  672. pr_debug("Problems setting modules path maps, continuing anyway...\n");
  673. return 0;
  674. }
  675. int machine__create_kernel_maps(struct machine *machine)
  676. {
  677. struct dso *kernel = machine__get_kernel(machine);
  678. if (kernel == NULL ||
  679. __machine__create_kernel_maps(machine, kernel) < 0)
  680. return -1;
  681. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  682. if (machine__is_host(machine))
  683. pr_debug("Problems creating module maps, "
  684. "continuing anyway...\n");
  685. else
  686. pr_debug("Problems creating module maps for guest %d, "
  687. "continuing anyway...\n", machine->pid);
  688. }
  689. /*
  690. * Now that we have all the maps created, just set the ->end of them:
  691. */
  692. map_groups__fixup_end(&machine->kmaps);
  693. return 0;
  694. }
  695. static void machine__set_kernel_mmap_len(struct machine *machine,
  696. union perf_event *event)
  697. {
  698. int i;
  699. for (i = 0; i < MAP__NR_TYPES; i++) {
  700. machine->vmlinux_maps[i]->start = event->mmap.start;
  701. machine->vmlinux_maps[i]->end = (event->mmap.start +
  702. event->mmap.len);
  703. /*
  704. * Be a bit paranoid here, some perf.data file came with
  705. * a zero sized synthesized MMAP event for the kernel.
  706. */
  707. if (machine->vmlinux_maps[i]->end == 0)
  708. machine->vmlinux_maps[i]->end = ~0ULL;
  709. }
  710. }
  711. static bool machine__uses_kcore(struct machine *machine)
  712. {
  713. struct dso *dso;
  714. list_for_each_entry(dso, &machine->kernel_dsos, node) {
  715. if (dso__is_kcore(dso))
  716. return true;
  717. }
  718. return false;
  719. }
  720. static int machine__process_kernel_mmap_event(struct machine *machine,
  721. union perf_event *event)
  722. {
  723. struct map *map;
  724. char kmmap_prefix[PATH_MAX];
  725. enum dso_kernel_type kernel_type;
  726. bool is_kernel_mmap;
  727. /* If we have maps from kcore then we do not need or want any others */
  728. if (machine__uses_kcore(machine))
  729. return 0;
  730. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  731. if (machine__is_host(machine))
  732. kernel_type = DSO_TYPE_KERNEL;
  733. else
  734. kernel_type = DSO_TYPE_GUEST_KERNEL;
  735. is_kernel_mmap = memcmp(event->mmap.filename,
  736. kmmap_prefix,
  737. strlen(kmmap_prefix) - 1) == 0;
  738. if (event->mmap.filename[0] == '/' ||
  739. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  740. char short_module_name[1024];
  741. char *name, *dot;
  742. if (event->mmap.filename[0] == '/') {
  743. name = strrchr(event->mmap.filename, '/');
  744. if (name == NULL)
  745. goto out_problem;
  746. ++name; /* skip / */
  747. dot = strrchr(name, '.');
  748. if (dot == NULL)
  749. goto out_problem;
  750. snprintf(short_module_name, sizeof(short_module_name),
  751. "[%.*s]", (int)(dot - name), name);
  752. strxfrchar(short_module_name, '-', '_');
  753. } else
  754. strcpy(short_module_name, event->mmap.filename);
  755. map = machine__new_module(machine, event->mmap.start,
  756. event->mmap.filename);
  757. if (map == NULL)
  758. goto out_problem;
  759. name = strdup(short_module_name);
  760. if (name == NULL)
  761. goto out_problem;
  762. map->dso->short_name = name;
  763. map->dso->sname_alloc = 1;
  764. map->end = map->start + event->mmap.len;
  765. } else if (is_kernel_mmap) {
  766. const char *symbol_name = (event->mmap.filename +
  767. strlen(kmmap_prefix));
  768. /*
  769. * Should be there already, from the build-id table in
  770. * the header.
  771. */
  772. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  773. kmmap_prefix);
  774. if (kernel == NULL)
  775. goto out_problem;
  776. kernel->kernel = kernel_type;
  777. if (__machine__create_kernel_maps(machine, kernel) < 0)
  778. goto out_problem;
  779. machine__set_kernel_mmap_len(machine, event);
  780. /*
  781. * Avoid using a zero address (kptr_restrict) for the ref reloc
  782. * symbol. Effectively having zero here means that at record
  783. * time /proc/sys/kernel/kptr_restrict was non zero.
  784. */
  785. if (event->mmap.pgoff != 0) {
  786. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  787. symbol_name,
  788. event->mmap.pgoff);
  789. }
  790. if (machine__is_default_guest(machine)) {
  791. /*
  792. * preload dso of guest kernel and modules
  793. */
  794. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  795. NULL);
  796. }
  797. }
  798. return 0;
  799. out_problem:
  800. return -1;
  801. }
  802. int machine__process_mmap2_event(struct machine *machine,
  803. union perf_event *event)
  804. {
  805. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  806. struct thread *thread;
  807. struct map *map;
  808. enum map_type type;
  809. int ret = 0;
  810. if (dump_trace)
  811. perf_event__fprintf_mmap2(event, stdout);
  812. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  813. cpumode == PERF_RECORD_MISC_KERNEL) {
  814. ret = machine__process_kernel_mmap_event(machine, event);
  815. if (ret < 0)
  816. goto out_problem;
  817. return 0;
  818. }
  819. thread = machine__findnew_thread(machine, event->mmap2.pid,
  820. event->mmap2.pid);
  821. if (thread == NULL)
  822. goto out_problem;
  823. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  824. type = MAP__VARIABLE;
  825. else
  826. type = MAP__FUNCTION;
  827. map = map__new(&machine->user_dsos, event->mmap2.start,
  828. event->mmap2.len, event->mmap2.pgoff,
  829. event->mmap2.pid, event->mmap2.maj,
  830. event->mmap2.min, event->mmap2.ino,
  831. event->mmap2.ino_generation,
  832. event->mmap2.filename, type);
  833. if (map == NULL)
  834. goto out_problem;
  835. thread__insert_map(thread, map);
  836. return 0;
  837. out_problem:
  838. dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
  839. return 0;
  840. }
  841. int machine__process_mmap_event(struct machine *machine, union perf_event *event)
  842. {
  843. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  844. struct thread *thread;
  845. struct map *map;
  846. enum map_type type;
  847. int ret = 0;
  848. if (dump_trace)
  849. perf_event__fprintf_mmap(event, stdout);
  850. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  851. cpumode == PERF_RECORD_MISC_KERNEL) {
  852. ret = machine__process_kernel_mmap_event(machine, event);
  853. if (ret < 0)
  854. goto out_problem;
  855. return 0;
  856. }
  857. thread = machine__findnew_thread(machine, event->mmap.pid,
  858. event->mmap.pid);
  859. if (thread == NULL)
  860. goto out_problem;
  861. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  862. type = MAP__VARIABLE;
  863. else
  864. type = MAP__FUNCTION;
  865. map = map__new(&machine->user_dsos, event->mmap.start,
  866. event->mmap.len, event->mmap.pgoff,
  867. event->mmap.pid, 0, 0, 0, 0,
  868. event->mmap.filename,
  869. type);
  870. if (map == NULL)
  871. goto out_problem;
  872. thread__insert_map(thread, map);
  873. return 0;
  874. out_problem:
  875. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  876. return 0;
  877. }
  878. static void machine__remove_thread(struct machine *machine, struct thread *th)
  879. {
  880. machine->last_match = NULL;
  881. rb_erase(&th->rb_node, &machine->threads);
  882. /*
  883. * We may have references to this thread, for instance in some hist_entry
  884. * instances, so just move them to a separate list.
  885. */
  886. list_add_tail(&th->node, &machine->dead_threads);
  887. }
  888. int machine__process_fork_event(struct machine *machine, union perf_event *event)
  889. {
  890. struct thread *thread = machine__find_thread(machine, event->fork.tid);
  891. struct thread *parent = machine__findnew_thread(machine,
  892. event->fork.ppid,
  893. event->fork.ptid);
  894. /* if a thread currently exists for the thread id remove it */
  895. if (thread != NULL)
  896. machine__remove_thread(machine, thread);
  897. thread = machine__findnew_thread(machine, event->fork.pid,
  898. event->fork.tid);
  899. if (dump_trace)
  900. perf_event__fprintf_task(event, stdout);
  901. if (thread == NULL || parent == NULL ||
  902. thread__fork(thread, parent) < 0) {
  903. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  904. return -1;
  905. }
  906. return 0;
  907. }
  908. int machine__process_exit_event(struct machine *machine __maybe_unused,
  909. union perf_event *event)
  910. {
  911. struct thread *thread = machine__find_thread(machine, event->fork.tid);
  912. if (dump_trace)
  913. perf_event__fprintf_task(event, stdout);
  914. if (thread != NULL)
  915. thread__exited(thread);
  916. return 0;
  917. }
  918. int machine__process_event(struct machine *machine, union perf_event *event)
  919. {
  920. int ret;
  921. switch (event->header.type) {
  922. case PERF_RECORD_COMM:
  923. ret = machine__process_comm_event(machine, event); break;
  924. case PERF_RECORD_MMAP:
  925. ret = machine__process_mmap_event(machine, event); break;
  926. case PERF_RECORD_MMAP2:
  927. ret = machine__process_mmap2_event(machine, event); break;
  928. case PERF_RECORD_FORK:
  929. ret = machine__process_fork_event(machine, event); break;
  930. case PERF_RECORD_EXIT:
  931. ret = machine__process_exit_event(machine, event); break;
  932. case PERF_RECORD_LOST:
  933. ret = machine__process_lost_event(machine, event); break;
  934. default:
  935. ret = -1;
  936. break;
  937. }
  938. return ret;
  939. }
  940. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  941. {
  942. if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
  943. return 1;
  944. return 0;
  945. }
  946. static const u8 cpumodes[] = {
  947. PERF_RECORD_MISC_USER,
  948. PERF_RECORD_MISC_KERNEL,
  949. PERF_RECORD_MISC_GUEST_USER,
  950. PERF_RECORD_MISC_GUEST_KERNEL
  951. };
  952. #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
  953. static void ip__resolve_ams(struct machine *machine, struct thread *thread,
  954. struct addr_map_symbol *ams,
  955. u64 ip)
  956. {
  957. struct addr_location al;
  958. size_t i;
  959. u8 m;
  960. memset(&al, 0, sizeof(al));
  961. for (i = 0; i < NCPUMODES; i++) {
  962. m = cpumodes[i];
  963. /*
  964. * We cannot use the header.misc hint to determine whether a
  965. * branch stack address is user, kernel, guest, hypervisor.
  966. * Branches may straddle the kernel/user/hypervisor boundaries.
  967. * Thus, we have to try consecutively until we find a match
  968. * or else, the symbol is unknown
  969. */
  970. thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
  971. ip, &al);
  972. if (al.sym)
  973. goto found;
  974. }
  975. found:
  976. ams->addr = ip;
  977. ams->al_addr = al.addr;
  978. ams->sym = al.sym;
  979. ams->map = al.map;
  980. }
  981. static void ip__resolve_data(struct machine *machine, struct thread *thread,
  982. u8 m, struct addr_map_symbol *ams, u64 addr)
  983. {
  984. struct addr_location al;
  985. memset(&al, 0, sizeof(al));
  986. thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
  987. &al);
  988. ams->addr = addr;
  989. ams->al_addr = al.addr;
  990. ams->sym = al.sym;
  991. ams->map = al.map;
  992. }
  993. struct mem_info *machine__resolve_mem(struct machine *machine,
  994. struct thread *thr,
  995. struct perf_sample *sample,
  996. u8 cpumode)
  997. {
  998. struct mem_info *mi = zalloc(sizeof(*mi));
  999. if (!mi)
  1000. return NULL;
  1001. ip__resolve_ams(machine, thr, &mi->iaddr, sample->ip);
  1002. ip__resolve_data(machine, thr, cpumode, &mi->daddr, sample->addr);
  1003. mi->data_src.val = sample->data_src;
  1004. return mi;
  1005. }
  1006. struct branch_info *machine__resolve_bstack(struct machine *machine,
  1007. struct thread *thr,
  1008. struct branch_stack *bs)
  1009. {
  1010. struct branch_info *bi;
  1011. unsigned int i;
  1012. bi = calloc(bs->nr, sizeof(struct branch_info));
  1013. if (!bi)
  1014. return NULL;
  1015. for (i = 0; i < bs->nr; i++) {
  1016. ip__resolve_ams(machine, thr, &bi[i].to, bs->entries[i].to);
  1017. ip__resolve_ams(machine, thr, &bi[i].from, bs->entries[i].from);
  1018. bi[i].flags = bs->entries[i].flags;
  1019. }
  1020. return bi;
  1021. }
  1022. static int machine__resolve_callchain_sample(struct machine *machine,
  1023. struct thread *thread,
  1024. struct ip_callchain *chain,
  1025. struct symbol **parent,
  1026. struct addr_location *root_al,
  1027. int max_stack)
  1028. {
  1029. u8 cpumode = PERF_RECORD_MISC_USER;
  1030. int chain_nr = min(max_stack, (int)chain->nr);
  1031. int i;
  1032. int err;
  1033. callchain_cursor_reset(&callchain_cursor);
  1034. if (chain->nr > PERF_MAX_STACK_DEPTH) {
  1035. pr_warning("corrupted callchain. skipping...\n");
  1036. return 0;
  1037. }
  1038. for (i = 0; i < chain_nr; i++) {
  1039. u64 ip;
  1040. struct addr_location al;
  1041. if (callchain_param.order == ORDER_CALLEE)
  1042. ip = chain->ips[i];
  1043. else
  1044. ip = chain->ips[chain->nr - i - 1];
  1045. if (ip >= PERF_CONTEXT_MAX) {
  1046. switch (ip) {
  1047. case PERF_CONTEXT_HV:
  1048. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1049. break;
  1050. case PERF_CONTEXT_KERNEL:
  1051. cpumode = PERF_RECORD_MISC_KERNEL;
  1052. break;
  1053. case PERF_CONTEXT_USER:
  1054. cpumode = PERF_RECORD_MISC_USER;
  1055. break;
  1056. default:
  1057. pr_debug("invalid callchain context: "
  1058. "%"PRId64"\n", (s64) ip);
  1059. /*
  1060. * It seems the callchain is corrupted.
  1061. * Discard all.
  1062. */
  1063. callchain_cursor_reset(&callchain_cursor);
  1064. return 0;
  1065. }
  1066. continue;
  1067. }
  1068. al.filtered = false;
  1069. thread__find_addr_location(thread, machine, cpumode,
  1070. MAP__FUNCTION, ip, &al);
  1071. if (al.sym != NULL) {
  1072. if (sort__has_parent && !*parent &&
  1073. symbol__match_regex(al.sym, &parent_regex))
  1074. *parent = al.sym;
  1075. else if (have_ignore_callees && root_al &&
  1076. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1077. /* Treat this symbol as the root,
  1078. forgetting its callees. */
  1079. *root_al = al;
  1080. callchain_cursor_reset(&callchain_cursor);
  1081. }
  1082. if (!symbol_conf.use_callchain)
  1083. break;
  1084. }
  1085. err = callchain_cursor_append(&callchain_cursor,
  1086. ip, al.map, al.sym);
  1087. if (err)
  1088. return err;
  1089. }
  1090. return 0;
  1091. }
  1092. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1093. {
  1094. struct callchain_cursor *cursor = arg;
  1095. return callchain_cursor_append(cursor, entry->ip,
  1096. entry->map, entry->sym);
  1097. }
  1098. int machine__resolve_callchain(struct machine *machine,
  1099. struct perf_evsel *evsel,
  1100. struct thread *thread,
  1101. struct perf_sample *sample,
  1102. struct symbol **parent,
  1103. struct addr_location *root_al,
  1104. int max_stack)
  1105. {
  1106. int ret;
  1107. ret = machine__resolve_callchain_sample(machine, thread,
  1108. sample->callchain, parent,
  1109. root_al, max_stack);
  1110. if (ret)
  1111. return ret;
  1112. /* Can we do dwarf post unwind? */
  1113. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1114. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1115. return 0;
  1116. /* Bail out if nothing was captured. */
  1117. if ((!sample->user_regs.regs) ||
  1118. (!sample->user_stack.size))
  1119. return 0;
  1120. return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
  1121. thread, evsel->attr.sample_regs_user,
  1122. sample);
  1123. }
  1124. int machine__for_each_thread(struct machine *machine,
  1125. int (*fn)(struct thread *thread, void *p),
  1126. void *priv)
  1127. {
  1128. struct rb_node *nd;
  1129. struct thread *thread;
  1130. int rc = 0;
  1131. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  1132. thread = rb_entry(nd, struct thread, rb_node);
  1133. rc = fn(thread, priv);
  1134. if (rc != 0)
  1135. return rc;
  1136. }
  1137. list_for_each_entry(thread, &machine->dead_threads, node) {
  1138. rc = fn(thread, priv);
  1139. if (rc != 0)
  1140. return rc;
  1141. }
  1142. return rc;
  1143. }