machine.c 29 KB

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