machine.c 34 KB

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