machine.c 28 KB

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