machine.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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 tid,
  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. * Front-end cache - TID 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->tid == tid)
  204. return machine->last_match;
  205. while (*p != NULL) {
  206. parent = *p;
  207. th = rb_entry(parent, struct thread, rb_node);
  208. if (th->tid == tid) {
  209. machine->last_match = th;
  210. return th;
  211. }
  212. if (tid < th->tid)
  213. p = &(*p)->rb_left;
  214. else
  215. p = &(*p)->rb_right;
  216. }
  217. if (!create)
  218. return NULL;
  219. th = thread__new(tid);
  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 tid)
  228. {
  229. return __machine__findnew_thread(machine, tid, true);
  230. }
  231. struct thread *machine__find_thread(struct machine *machine, pid_t tid)
  232. {
  233. return __machine__findnew_thread(machine, tid, 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. return ret;
  516. }
  517. static void map_groups__fixup_end(struct map_groups *mg)
  518. {
  519. int i;
  520. for (i = 0; i < MAP__NR_TYPES; ++i)
  521. __map_groups__fixup_end(mg, i);
  522. }
  523. static char *get_kernel_version(const char *root_dir)
  524. {
  525. char version[PATH_MAX];
  526. FILE *file;
  527. char *name, *tmp;
  528. const char *prefix = "Linux version ";
  529. sprintf(version, "%s/proc/version", root_dir);
  530. file = fopen(version, "r");
  531. if (!file)
  532. return NULL;
  533. version[0] = '\0';
  534. tmp = fgets(version, sizeof(version), file);
  535. fclose(file);
  536. name = strstr(version, prefix);
  537. if (!name)
  538. return NULL;
  539. name += strlen(prefix);
  540. tmp = strchr(name, ' ');
  541. if (tmp)
  542. *tmp = '\0';
  543. return strdup(name);
  544. }
  545. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  546. const char *dir_name)
  547. {
  548. struct dirent *dent;
  549. DIR *dir = opendir(dir_name);
  550. int ret = 0;
  551. if (!dir) {
  552. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  553. return -1;
  554. }
  555. while ((dent = readdir(dir)) != NULL) {
  556. char path[PATH_MAX];
  557. struct stat st;
  558. /*sshfs might return bad dent->d_type, so we have to stat*/
  559. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  560. if (stat(path, &st))
  561. continue;
  562. if (S_ISDIR(st.st_mode)) {
  563. if (!strcmp(dent->d_name, ".") ||
  564. !strcmp(dent->d_name, ".."))
  565. continue;
  566. ret = map_groups__set_modules_path_dir(mg, path);
  567. if (ret < 0)
  568. goto out;
  569. } else {
  570. char *dot = strrchr(dent->d_name, '.'),
  571. dso_name[PATH_MAX];
  572. struct map *map;
  573. char *long_name;
  574. if (dot == NULL || strcmp(dot, ".ko"))
  575. continue;
  576. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  577. (int)(dot - dent->d_name), dent->d_name);
  578. strxfrchar(dso_name, '-', '_');
  579. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  580. dso_name);
  581. if (map == NULL)
  582. continue;
  583. long_name = strdup(path);
  584. if (long_name == NULL) {
  585. ret = -1;
  586. goto out;
  587. }
  588. dso__set_long_name(map->dso, long_name);
  589. map->dso->lname_alloc = 1;
  590. dso__kernel_module_get_build_id(map->dso, "");
  591. }
  592. }
  593. out:
  594. closedir(dir);
  595. return ret;
  596. }
  597. static int machine__set_modules_path(struct machine *machine)
  598. {
  599. char *version;
  600. char modules_path[PATH_MAX];
  601. version = get_kernel_version(machine->root_dir);
  602. if (!version)
  603. return -1;
  604. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  605. machine->root_dir, version);
  606. free(version);
  607. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  608. }
  609. static int machine__create_modules(struct machine *machine)
  610. {
  611. char *line = NULL;
  612. size_t n;
  613. FILE *file;
  614. struct map *map;
  615. const char *modules;
  616. char path[PATH_MAX];
  617. if (machine__is_default_guest(machine))
  618. modules = symbol_conf.default_guest_modules;
  619. else {
  620. sprintf(path, "%s/proc/modules", machine->root_dir);
  621. modules = path;
  622. }
  623. if (symbol__restricted_filename(path, "/proc/modules"))
  624. return -1;
  625. file = fopen(modules, "r");
  626. if (file == NULL)
  627. return -1;
  628. while (!feof(file)) {
  629. char name[PATH_MAX];
  630. u64 start;
  631. char *sep;
  632. int line_len;
  633. line_len = getline(&line, &n, file);
  634. if (line_len < 0)
  635. break;
  636. if (!line)
  637. goto out_failure;
  638. line[--line_len] = '\0'; /* \n */
  639. sep = strrchr(line, 'x');
  640. if (sep == NULL)
  641. continue;
  642. hex2u64(sep + 1, &start);
  643. sep = strchr(line, ' ');
  644. if (sep == NULL)
  645. continue;
  646. *sep = '\0';
  647. snprintf(name, sizeof(name), "[%s]", line);
  648. map = machine__new_module(machine, start, name);
  649. if (map == NULL)
  650. goto out_delete_line;
  651. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  652. }
  653. free(line);
  654. fclose(file);
  655. return machine__set_modules_path(machine);
  656. out_delete_line:
  657. free(line);
  658. out_failure:
  659. return -1;
  660. }
  661. int machine__create_kernel_maps(struct machine *machine)
  662. {
  663. struct dso *kernel = machine__get_kernel(machine);
  664. if (kernel == NULL ||
  665. __machine__create_kernel_maps(machine, kernel) < 0)
  666. return -1;
  667. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  668. if (machine__is_host(machine))
  669. pr_debug("Problems creating module maps, "
  670. "continuing anyway...\n");
  671. else
  672. pr_debug("Problems creating module maps for guest %d, "
  673. "continuing anyway...\n", machine->pid);
  674. }
  675. /*
  676. * Now that we have all the maps created, just set the ->end of them:
  677. */
  678. map_groups__fixup_end(&machine->kmaps);
  679. return 0;
  680. }
  681. static void machine__set_kernel_mmap_len(struct machine *machine,
  682. union perf_event *event)
  683. {
  684. int i;
  685. for (i = 0; i < MAP__NR_TYPES; i++) {
  686. machine->vmlinux_maps[i]->start = event->mmap.start;
  687. machine->vmlinux_maps[i]->end = (event->mmap.start +
  688. event->mmap.len);
  689. /*
  690. * Be a bit paranoid here, some perf.data file came with
  691. * a zero sized synthesized MMAP event for the kernel.
  692. */
  693. if (machine->vmlinux_maps[i]->end == 0)
  694. machine->vmlinux_maps[i]->end = ~0ULL;
  695. }
  696. }
  697. static bool machine__uses_kcore(struct machine *machine)
  698. {
  699. struct dso *dso;
  700. list_for_each_entry(dso, &machine->kernel_dsos, node) {
  701. if (dso__is_kcore(dso))
  702. return true;
  703. }
  704. return false;
  705. }
  706. static int machine__process_kernel_mmap_event(struct machine *machine,
  707. union perf_event *event)
  708. {
  709. struct map *map;
  710. char kmmap_prefix[PATH_MAX];
  711. enum dso_kernel_type kernel_type;
  712. bool is_kernel_mmap;
  713. /* If we have maps from kcore then we do not need or want any others */
  714. if (machine__uses_kcore(machine))
  715. return 0;
  716. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  717. if (machine__is_host(machine))
  718. kernel_type = DSO_TYPE_KERNEL;
  719. else
  720. kernel_type = DSO_TYPE_GUEST_KERNEL;
  721. is_kernel_mmap = memcmp(event->mmap.filename,
  722. kmmap_prefix,
  723. strlen(kmmap_prefix) - 1) == 0;
  724. if (event->mmap.filename[0] == '/' ||
  725. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  726. char short_module_name[1024];
  727. char *name, *dot;
  728. if (event->mmap.filename[0] == '/') {
  729. name = strrchr(event->mmap.filename, '/');
  730. if (name == NULL)
  731. goto out_problem;
  732. ++name; /* skip / */
  733. dot = strrchr(name, '.');
  734. if (dot == NULL)
  735. goto out_problem;
  736. snprintf(short_module_name, sizeof(short_module_name),
  737. "[%.*s]", (int)(dot - name), name);
  738. strxfrchar(short_module_name, '-', '_');
  739. } else
  740. strcpy(short_module_name, event->mmap.filename);
  741. map = machine__new_module(machine, event->mmap.start,
  742. event->mmap.filename);
  743. if (map == NULL)
  744. goto out_problem;
  745. name = strdup(short_module_name);
  746. if (name == NULL)
  747. goto out_problem;
  748. map->dso->short_name = name;
  749. map->dso->sname_alloc = 1;
  750. map->end = map->start + event->mmap.len;
  751. } else if (is_kernel_mmap) {
  752. const char *symbol_name = (event->mmap.filename +
  753. strlen(kmmap_prefix));
  754. /*
  755. * Should be there already, from the build-id table in
  756. * the header.
  757. */
  758. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  759. kmmap_prefix);
  760. if (kernel == NULL)
  761. goto out_problem;
  762. kernel->kernel = kernel_type;
  763. if (__machine__create_kernel_maps(machine, kernel) < 0)
  764. goto out_problem;
  765. machine__set_kernel_mmap_len(machine, event);
  766. /*
  767. * Avoid using a zero address (kptr_restrict) for the ref reloc
  768. * symbol. Effectively having zero here means that at record
  769. * time /proc/sys/kernel/kptr_restrict was non zero.
  770. */
  771. if (event->mmap.pgoff != 0) {
  772. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  773. symbol_name,
  774. event->mmap.pgoff);
  775. }
  776. if (machine__is_default_guest(machine)) {
  777. /*
  778. * preload dso of guest kernel and modules
  779. */
  780. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  781. NULL);
  782. }
  783. }
  784. return 0;
  785. out_problem:
  786. return -1;
  787. }
  788. int machine__process_mmap_event(struct machine *machine, union perf_event *event)
  789. {
  790. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  791. struct thread *thread;
  792. struct map *map;
  793. enum map_type type;
  794. int ret = 0;
  795. if (dump_trace)
  796. perf_event__fprintf_mmap(event, stdout);
  797. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  798. cpumode == PERF_RECORD_MISC_KERNEL) {
  799. ret = machine__process_kernel_mmap_event(machine, event);
  800. if (ret < 0)
  801. goto out_problem;
  802. return 0;
  803. }
  804. thread = machine__findnew_thread(machine, event->mmap.pid);
  805. if (thread == NULL)
  806. goto out_problem;
  807. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  808. type = MAP__VARIABLE;
  809. else
  810. type = MAP__FUNCTION;
  811. map = map__new(&machine->user_dsos, event->mmap.start,
  812. event->mmap.len, event->mmap.pgoff,
  813. event->mmap.pid, event->mmap.filename,
  814. type);
  815. if (map == NULL)
  816. goto out_problem;
  817. thread__insert_map(thread, map);
  818. return 0;
  819. out_problem:
  820. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  821. return 0;
  822. }
  823. int machine__process_fork_event(struct machine *machine, union perf_event *event)
  824. {
  825. struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
  826. struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
  827. if (dump_trace)
  828. perf_event__fprintf_task(event, stdout);
  829. if (thread == NULL || parent == NULL ||
  830. thread__fork(thread, parent) < 0) {
  831. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  832. return -1;
  833. }
  834. return 0;
  835. }
  836. static void machine__remove_thread(struct machine *machine, struct thread *th)
  837. {
  838. machine->last_match = NULL;
  839. rb_erase(&th->rb_node, &machine->threads);
  840. /*
  841. * We may have references to this thread, for instance in some hist_entry
  842. * instances, so just move them to a separate list.
  843. */
  844. list_add_tail(&th->node, &machine->dead_threads);
  845. }
  846. int machine__process_exit_event(struct machine *machine, union perf_event *event)
  847. {
  848. struct thread *thread = machine__find_thread(machine, event->fork.tid);
  849. if (dump_trace)
  850. perf_event__fprintf_task(event, stdout);
  851. if (thread != NULL)
  852. machine__remove_thread(machine, thread);
  853. return 0;
  854. }
  855. int machine__process_event(struct machine *machine, union perf_event *event)
  856. {
  857. int ret;
  858. switch (event->header.type) {
  859. case PERF_RECORD_COMM:
  860. ret = machine__process_comm_event(machine, event); break;
  861. case PERF_RECORD_MMAP:
  862. ret = machine__process_mmap_event(machine, event); break;
  863. case PERF_RECORD_FORK:
  864. ret = machine__process_fork_event(machine, event); break;
  865. case PERF_RECORD_EXIT:
  866. ret = machine__process_exit_event(machine, event); break;
  867. case PERF_RECORD_LOST:
  868. ret = machine__process_lost_event(machine, event); break;
  869. default:
  870. ret = -1;
  871. break;
  872. }
  873. return ret;
  874. }
  875. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  876. {
  877. if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
  878. return 1;
  879. return 0;
  880. }
  881. static const u8 cpumodes[] = {
  882. PERF_RECORD_MISC_USER,
  883. PERF_RECORD_MISC_KERNEL,
  884. PERF_RECORD_MISC_GUEST_USER,
  885. PERF_RECORD_MISC_GUEST_KERNEL
  886. };
  887. #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
  888. static void ip__resolve_ams(struct machine *machine, struct thread *thread,
  889. struct addr_map_symbol *ams,
  890. u64 ip)
  891. {
  892. struct addr_location al;
  893. size_t i;
  894. u8 m;
  895. memset(&al, 0, sizeof(al));
  896. for (i = 0; i < NCPUMODES; i++) {
  897. m = cpumodes[i];
  898. /*
  899. * We cannot use the header.misc hint to determine whether a
  900. * branch stack address is user, kernel, guest, hypervisor.
  901. * Branches may straddle the kernel/user/hypervisor boundaries.
  902. * Thus, we have to try consecutively until we find a match
  903. * or else, the symbol is unknown
  904. */
  905. thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
  906. ip, &al, NULL);
  907. if (al.sym)
  908. goto found;
  909. }
  910. found:
  911. ams->addr = ip;
  912. ams->al_addr = al.addr;
  913. ams->sym = al.sym;
  914. ams->map = al.map;
  915. }
  916. static void ip__resolve_data(struct machine *machine, struct thread *thread,
  917. u8 m, struct addr_map_symbol *ams, u64 addr)
  918. {
  919. struct addr_location al;
  920. memset(&al, 0, sizeof(al));
  921. thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr, &al,
  922. NULL);
  923. ams->addr = addr;
  924. ams->al_addr = al.addr;
  925. ams->sym = al.sym;
  926. ams->map = al.map;
  927. }
  928. struct mem_info *machine__resolve_mem(struct machine *machine,
  929. struct thread *thr,
  930. struct perf_sample *sample,
  931. u8 cpumode)
  932. {
  933. struct mem_info *mi = zalloc(sizeof(*mi));
  934. if (!mi)
  935. return NULL;
  936. ip__resolve_ams(machine, thr, &mi->iaddr, sample->ip);
  937. ip__resolve_data(machine, thr, cpumode, &mi->daddr, sample->addr);
  938. mi->data_src.val = sample->data_src;
  939. return mi;
  940. }
  941. struct branch_info *machine__resolve_bstack(struct machine *machine,
  942. struct thread *thr,
  943. struct branch_stack *bs)
  944. {
  945. struct branch_info *bi;
  946. unsigned int i;
  947. bi = calloc(bs->nr, sizeof(struct branch_info));
  948. if (!bi)
  949. return NULL;
  950. for (i = 0; i < bs->nr; i++) {
  951. ip__resolve_ams(machine, thr, &bi[i].to, bs->entries[i].to);
  952. ip__resolve_ams(machine, thr, &bi[i].from, bs->entries[i].from);
  953. bi[i].flags = bs->entries[i].flags;
  954. }
  955. return bi;
  956. }
  957. static int machine__resolve_callchain_sample(struct machine *machine,
  958. struct thread *thread,
  959. struct ip_callchain *chain,
  960. struct symbol **parent,
  961. struct addr_location *root_al)
  962. {
  963. u8 cpumode = PERF_RECORD_MISC_USER;
  964. unsigned int i;
  965. int err;
  966. callchain_cursor_reset(&callchain_cursor);
  967. if (chain->nr > PERF_MAX_STACK_DEPTH) {
  968. pr_warning("corrupted callchain. skipping...\n");
  969. return 0;
  970. }
  971. for (i = 0; i < chain->nr; i++) {
  972. u64 ip;
  973. struct addr_location al;
  974. if (callchain_param.order == ORDER_CALLEE)
  975. ip = chain->ips[i];
  976. else
  977. ip = chain->ips[chain->nr - i - 1];
  978. if (ip >= PERF_CONTEXT_MAX) {
  979. switch (ip) {
  980. case PERF_CONTEXT_HV:
  981. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  982. break;
  983. case PERF_CONTEXT_KERNEL:
  984. cpumode = PERF_RECORD_MISC_KERNEL;
  985. break;
  986. case PERF_CONTEXT_USER:
  987. cpumode = PERF_RECORD_MISC_USER;
  988. break;
  989. default:
  990. pr_debug("invalid callchain context: "
  991. "%"PRId64"\n", (s64) ip);
  992. /*
  993. * It seems the callchain is corrupted.
  994. * Discard all.
  995. */
  996. callchain_cursor_reset(&callchain_cursor);
  997. return 0;
  998. }
  999. continue;
  1000. }
  1001. al.filtered = false;
  1002. thread__find_addr_location(thread, machine, cpumode,
  1003. MAP__FUNCTION, ip, &al, NULL);
  1004. if (al.sym != NULL) {
  1005. if (sort__has_parent && !*parent &&
  1006. symbol__match_regex(al.sym, &parent_regex))
  1007. *parent = al.sym;
  1008. else if (have_ignore_callees && root_al &&
  1009. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1010. /* Treat this symbol as the root,
  1011. forgetting its callees. */
  1012. *root_al = al;
  1013. callchain_cursor_reset(&callchain_cursor);
  1014. }
  1015. if (!symbol_conf.use_callchain)
  1016. break;
  1017. }
  1018. err = callchain_cursor_append(&callchain_cursor,
  1019. ip, al.map, al.sym);
  1020. if (err)
  1021. return err;
  1022. }
  1023. return 0;
  1024. }
  1025. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1026. {
  1027. struct callchain_cursor *cursor = arg;
  1028. return callchain_cursor_append(cursor, entry->ip,
  1029. entry->map, entry->sym);
  1030. }
  1031. int machine__resolve_callchain(struct machine *machine,
  1032. struct perf_evsel *evsel,
  1033. struct thread *thread,
  1034. struct perf_sample *sample,
  1035. struct symbol **parent,
  1036. struct addr_location *root_al)
  1037. {
  1038. int ret;
  1039. ret = machine__resolve_callchain_sample(machine, thread,
  1040. sample->callchain, parent, root_al);
  1041. if (ret)
  1042. return ret;
  1043. /* Can we do dwarf post unwind? */
  1044. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1045. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1046. return 0;
  1047. /* Bail out if nothing was captured. */
  1048. if ((!sample->user_regs.regs) ||
  1049. (!sample->user_stack.size))
  1050. return 0;
  1051. return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
  1052. thread, evsel->attr.sample_regs_user,
  1053. sample);
  1054. }