machine.c 34 KB

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