machine.c 30 KB

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