machine.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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 int symbol__in_kernel(void *arg, const char *name,
  404. char type __maybe_unused, u64 start)
  405. {
  406. struct process_args *args = arg;
  407. if (strchr(name, '['))
  408. return 0;
  409. args->start = start;
  410. return 1;
  411. }
  412. static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
  413. size_t bufsz)
  414. {
  415. if (machine__is_default_guest(machine))
  416. scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
  417. else
  418. scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
  419. }
  420. /* Figure out the start address of kernel map from /proc/kallsyms */
  421. static u64 machine__get_kernel_start_addr(struct machine *machine)
  422. {
  423. char filename[PATH_MAX];
  424. struct process_args args;
  425. machine__get_kallsyms_filename(machine, filename, PATH_MAX);
  426. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  427. return 0;
  428. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  429. return 0;
  430. return args.start;
  431. }
  432. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  433. {
  434. enum map_type type;
  435. u64 start = machine__get_kernel_start_addr(machine);
  436. for (type = 0; type < MAP__NR_TYPES; ++type) {
  437. struct kmap *kmap;
  438. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  439. if (machine->vmlinux_maps[type] == NULL)
  440. return -1;
  441. machine->vmlinux_maps[type]->map_ip =
  442. machine->vmlinux_maps[type]->unmap_ip =
  443. identity__map_ip;
  444. kmap = map__kmap(machine->vmlinux_maps[type]);
  445. kmap->kmaps = &machine->kmaps;
  446. map_groups__insert(&machine->kmaps,
  447. machine->vmlinux_maps[type]);
  448. }
  449. return 0;
  450. }
  451. void machine__destroy_kernel_maps(struct machine *machine)
  452. {
  453. enum map_type type;
  454. for (type = 0; type < MAP__NR_TYPES; ++type) {
  455. struct kmap *kmap;
  456. if (machine->vmlinux_maps[type] == NULL)
  457. continue;
  458. kmap = map__kmap(machine->vmlinux_maps[type]);
  459. map_groups__remove(&machine->kmaps,
  460. machine->vmlinux_maps[type]);
  461. if (kmap->ref_reloc_sym) {
  462. /*
  463. * ref_reloc_sym is shared among all maps, so free just
  464. * on one of them.
  465. */
  466. if (type == MAP__FUNCTION) {
  467. zfree((char **)&kmap->ref_reloc_sym->name);
  468. zfree(&kmap->ref_reloc_sym);
  469. } else
  470. kmap->ref_reloc_sym = NULL;
  471. }
  472. map__delete(machine->vmlinux_maps[type]);
  473. machine->vmlinux_maps[type] = NULL;
  474. }
  475. }
  476. int machines__create_guest_kernel_maps(struct machines *machines)
  477. {
  478. int ret = 0;
  479. struct dirent **namelist = NULL;
  480. int i, items = 0;
  481. char path[PATH_MAX];
  482. pid_t pid;
  483. char *endp;
  484. if (symbol_conf.default_guest_vmlinux_name ||
  485. symbol_conf.default_guest_modules ||
  486. symbol_conf.default_guest_kallsyms) {
  487. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  488. }
  489. if (symbol_conf.guestmount) {
  490. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  491. if (items <= 0)
  492. return -ENOENT;
  493. for (i = 0; i < items; i++) {
  494. if (!isdigit(namelist[i]->d_name[0])) {
  495. /* Filter out . and .. */
  496. continue;
  497. }
  498. pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
  499. if ((*endp != '\0') ||
  500. (endp == namelist[i]->d_name) ||
  501. (errno == ERANGE)) {
  502. pr_debug("invalid directory (%s). Skipping.\n",
  503. namelist[i]->d_name);
  504. continue;
  505. }
  506. sprintf(path, "%s/%s/proc/kallsyms",
  507. symbol_conf.guestmount,
  508. namelist[i]->d_name);
  509. ret = access(path, R_OK);
  510. if (ret) {
  511. pr_debug("Can't access file %s\n", path);
  512. goto failure;
  513. }
  514. machines__create_kernel_maps(machines, pid);
  515. }
  516. failure:
  517. free(namelist);
  518. }
  519. return ret;
  520. }
  521. void machines__destroy_kernel_maps(struct machines *machines)
  522. {
  523. struct rb_node *next = rb_first(&machines->guests);
  524. machine__destroy_kernel_maps(&machines->host);
  525. while (next) {
  526. struct machine *pos = rb_entry(next, struct machine, rb_node);
  527. next = rb_next(&pos->rb_node);
  528. rb_erase(&pos->rb_node, &machines->guests);
  529. machine__delete(pos);
  530. }
  531. }
  532. int machines__create_kernel_maps(struct machines *machines, pid_t pid)
  533. {
  534. struct machine *machine = machines__findnew(machines, pid);
  535. if (machine == NULL)
  536. return -1;
  537. return machine__create_kernel_maps(machine);
  538. }
  539. int machine__load_kallsyms(struct machine *machine, const char *filename,
  540. enum map_type type, symbol_filter_t filter)
  541. {
  542. struct map *map = machine->vmlinux_maps[type];
  543. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  544. if (ret > 0) {
  545. dso__set_loaded(map->dso, type);
  546. /*
  547. * Since /proc/kallsyms will have multiple sessions for the
  548. * kernel, with modules between them, fixup the end of all
  549. * sections.
  550. */
  551. __map_groups__fixup_end(&machine->kmaps, type);
  552. }
  553. return ret;
  554. }
  555. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  556. symbol_filter_t filter)
  557. {
  558. struct map *map = machine->vmlinux_maps[type];
  559. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  560. if (ret > 0)
  561. dso__set_loaded(map->dso, type);
  562. return ret;
  563. }
  564. static void map_groups__fixup_end(struct map_groups *mg)
  565. {
  566. int i;
  567. for (i = 0; i < MAP__NR_TYPES; ++i)
  568. __map_groups__fixup_end(mg, i);
  569. }
  570. static char *get_kernel_version(const char *root_dir)
  571. {
  572. char version[PATH_MAX];
  573. FILE *file;
  574. char *name, *tmp;
  575. const char *prefix = "Linux version ";
  576. sprintf(version, "%s/proc/version", root_dir);
  577. file = fopen(version, "r");
  578. if (!file)
  579. return NULL;
  580. version[0] = '\0';
  581. tmp = fgets(version, sizeof(version), file);
  582. fclose(file);
  583. name = strstr(version, prefix);
  584. if (!name)
  585. return NULL;
  586. name += strlen(prefix);
  587. tmp = strchr(name, ' ');
  588. if (tmp)
  589. *tmp = '\0';
  590. return strdup(name);
  591. }
  592. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  593. const char *dir_name, int depth)
  594. {
  595. struct dirent *dent;
  596. DIR *dir = opendir(dir_name);
  597. int ret = 0;
  598. if (!dir) {
  599. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  600. return -1;
  601. }
  602. while ((dent = readdir(dir)) != NULL) {
  603. char path[PATH_MAX];
  604. struct stat st;
  605. /*sshfs might return bad dent->d_type, so we have to stat*/
  606. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  607. if (stat(path, &st))
  608. continue;
  609. if (S_ISDIR(st.st_mode)) {
  610. if (!strcmp(dent->d_name, ".") ||
  611. !strcmp(dent->d_name, ".."))
  612. continue;
  613. /* Do not follow top-level source and build symlinks */
  614. if (depth == 0) {
  615. if (!strcmp(dent->d_name, "source") ||
  616. !strcmp(dent->d_name, "build"))
  617. continue;
  618. }
  619. ret = map_groups__set_modules_path_dir(mg, path,
  620. depth + 1);
  621. if (ret < 0)
  622. goto out;
  623. } else {
  624. char *dot = strrchr(dent->d_name, '.'),
  625. dso_name[PATH_MAX];
  626. struct map *map;
  627. char *long_name;
  628. if (dot == NULL || strcmp(dot, ".ko"))
  629. continue;
  630. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  631. (int)(dot - dent->d_name), dent->d_name);
  632. strxfrchar(dso_name, '-', '_');
  633. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  634. dso_name);
  635. if (map == NULL)
  636. continue;
  637. long_name = strdup(path);
  638. if (long_name == NULL) {
  639. ret = -1;
  640. goto out;
  641. }
  642. dso__set_long_name(map->dso, long_name, true);
  643. dso__kernel_module_get_build_id(map->dso, "");
  644. }
  645. }
  646. out:
  647. closedir(dir);
  648. return ret;
  649. }
  650. static int machine__set_modules_path(struct machine *machine)
  651. {
  652. char *version;
  653. char modules_path[PATH_MAX];
  654. version = get_kernel_version(machine->root_dir);
  655. if (!version)
  656. return -1;
  657. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
  658. machine->root_dir, version);
  659. free(version);
  660. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
  661. }
  662. static int machine__create_module(void *arg, const char *name, u64 start)
  663. {
  664. struct machine *machine = arg;
  665. struct map *map;
  666. map = machine__new_module(machine, start, name);
  667. if (map == NULL)
  668. return -1;
  669. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  670. return 0;
  671. }
  672. static int machine__create_modules(struct machine *machine)
  673. {
  674. const char *modules;
  675. char path[PATH_MAX];
  676. if (machine__is_default_guest(machine)) {
  677. modules = symbol_conf.default_guest_modules;
  678. } else {
  679. snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
  680. modules = path;
  681. }
  682. if (symbol__restricted_filename(modules, "/proc/modules"))
  683. return -1;
  684. if (modules__parse(modules, machine, machine__create_module))
  685. return -1;
  686. if (!machine__set_modules_path(machine))
  687. return 0;
  688. pr_debug("Problems setting modules path maps, continuing anyway...\n");
  689. return 0;
  690. }
  691. const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
  692. int machine__create_kernel_maps(struct machine *machine)
  693. {
  694. struct dso *kernel = machine__get_kernel(machine);
  695. char filename[PATH_MAX];
  696. const char *name;
  697. u64 addr = 0;
  698. int i;
  699. machine__get_kallsyms_filename(machine, filename, PATH_MAX);
  700. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  701. addr = kallsyms__get_function_start(filename, name);
  702. if (addr)
  703. break;
  704. }
  705. if (!addr)
  706. return -1;
  707. if (kernel == NULL ||
  708. __machine__create_kernel_maps(machine, kernel) < 0)
  709. return -1;
  710. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  711. if (machine__is_host(machine))
  712. pr_debug("Problems creating module maps, "
  713. "continuing anyway...\n");
  714. else
  715. pr_debug("Problems creating module maps for guest %d, "
  716. "continuing anyway...\n", machine->pid);
  717. }
  718. /*
  719. * Now that we have all the maps created, just set the ->end of them:
  720. */
  721. map_groups__fixup_end(&machine->kmaps);
  722. if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
  723. addr)) {
  724. machine__destroy_kernel_maps(machine);
  725. return -1;
  726. }
  727. return 0;
  728. }
  729. static void machine__set_kernel_mmap_len(struct machine *machine,
  730. union perf_event *event)
  731. {
  732. int i;
  733. for (i = 0; i < MAP__NR_TYPES; i++) {
  734. machine->vmlinux_maps[i]->start = event->mmap.start;
  735. machine->vmlinux_maps[i]->end = (event->mmap.start +
  736. event->mmap.len);
  737. /*
  738. * Be a bit paranoid here, some perf.data file came with
  739. * a zero sized synthesized MMAP event for the kernel.
  740. */
  741. if (machine->vmlinux_maps[i]->end == 0)
  742. machine->vmlinux_maps[i]->end = ~0ULL;
  743. }
  744. }
  745. static bool machine__uses_kcore(struct machine *machine)
  746. {
  747. struct dso *dso;
  748. list_for_each_entry(dso, &machine->kernel_dsos, node) {
  749. if (dso__is_kcore(dso))
  750. return true;
  751. }
  752. return false;
  753. }
  754. static int machine__process_kernel_mmap_event(struct machine *machine,
  755. union perf_event *event)
  756. {
  757. struct map *map;
  758. char kmmap_prefix[PATH_MAX];
  759. enum dso_kernel_type kernel_type;
  760. bool is_kernel_mmap;
  761. /* If we have maps from kcore then we do not need or want any others */
  762. if (machine__uses_kcore(machine))
  763. return 0;
  764. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  765. if (machine__is_host(machine))
  766. kernel_type = DSO_TYPE_KERNEL;
  767. else
  768. kernel_type = DSO_TYPE_GUEST_KERNEL;
  769. is_kernel_mmap = memcmp(event->mmap.filename,
  770. kmmap_prefix,
  771. strlen(kmmap_prefix) - 1) == 0;
  772. if (event->mmap.filename[0] == '/' ||
  773. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  774. char short_module_name[1024];
  775. char *name, *dot;
  776. if (event->mmap.filename[0] == '/') {
  777. name = strrchr(event->mmap.filename, '/');
  778. if (name == NULL)
  779. goto out_problem;
  780. ++name; /* skip / */
  781. dot = strrchr(name, '.');
  782. if (dot == NULL)
  783. goto out_problem;
  784. snprintf(short_module_name, sizeof(short_module_name),
  785. "[%.*s]", (int)(dot - name), name);
  786. strxfrchar(short_module_name, '-', '_');
  787. } else
  788. strcpy(short_module_name, event->mmap.filename);
  789. map = machine__new_module(machine, event->mmap.start,
  790. event->mmap.filename);
  791. if (map == NULL)
  792. goto out_problem;
  793. name = strdup(short_module_name);
  794. if (name == NULL)
  795. goto out_problem;
  796. dso__set_short_name(map->dso, name, true);
  797. map->end = map->start + event->mmap.len;
  798. } else if (is_kernel_mmap) {
  799. const char *symbol_name = (event->mmap.filename +
  800. strlen(kmmap_prefix));
  801. /*
  802. * Should be there already, from the build-id table in
  803. * the header.
  804. */
  805. struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
  806. kmmap_prefix);
  807. if (kernel == NULL)
  808. goto out_problem;
  809. kernel->kernel = kernel_type;
  810. if (__machine__create_kernel_maps(machine, kernel) < 0)
  811. goto out_problem;
  812. machine__set_kernel_mmap_len(machine, event);
  813. /*
  814. * Avoid using a zero address (kptr_restrict) for the ref reloc
  815. * symbol. Effectively having zero here means that at record
  816. * time /proc/sys/kernel/kptr_restrict was non zero.
  817. */
  818. if (event->mmap.pgoff != 0) {
  819. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  820. symbol_name,
  821. event->mmap.pgoff);
  822. }
  823. if (machine__is_default_guest(machine)) {
  824. /*
  825. * preload dso of guest kernel and modules
  826. */
  827. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  828. NULL);
  829. }
  830. }
  831. return 0;
  832. out_problem:
  833. return -1;
  834. }
  835. int machine__process_mmap2_event(struct machine *machine,
  836. union perf_event *event,
  837. struct perf_sample *sample __maybe_unused)
  838. {
  839. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  840. struct thread *thread;
  841. struct map *map;
  842. enum map_type type;
  843. int ret = 0;
  844. if (dump_trace)
  845. perf_event__fprintf_mmap2(event, stdout);
  846. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  847. cpumode == PERF_RECORD_MISC_KERNEL) {
  848. ret = machine__process_kernel_mmap_event(machine, event);
  849. if (ret < 0)
  850. goto out_problem;
  851. return 0;
  852. }
  853. thread = machine__findnew_thread(machine, event->mmap2.pid,
  854. event->mmap2.tid);
  855. if (thread == NULL)
  856. goto out_problem;
  857. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  858. type = MAP__VARIABLE;
  859. else
  860. type = MAP__FUNCTION;
  861. map = map__new(&machine->user_dsos, event->mmap2.start,
  862. event->mmap2.len, event->mmap2.pgoff,
  863. event->mmap2.pid, event->mmap2.maj,
  864. event->mmap2.min, event->mmap2.ino,
  865. event->mmap2.ino_generation,
  866. event->mmap2.filename, type);
  867. if (map == NULL)
  868. goto out_problem;
  869. thread__insert_map(thread, map);
  870. return 0;
  871. out_problem:
  872. dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
  873. return 0;
  874. }
  875. int machine__process_mmap_event(struct machine *machine, union perf_event *event,
  876. struct perf_sample *sample __maybe_unused)
  877. {
  878. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  879. struct thread *thread;
  880. struct map *map;
  881. enum map_type type;
  882. int ret = 0;
  883. if (dump_trace)
  884. perf_event__fprintf_mmap(event, stdout);
  885. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  886. cpumode == PERF_RECORD_MISC_KERNEL) {
  887. ret = machine__process_kernel_mmap_event(machine, event);
  888. if (ret < 0)
  889. goto out_problem;
  890. return 0;
  891. }
  892. thread = machine__findnew_thread(machine, event->mmap.pid,
  893. event->mmap.tid);
  894. if (thread == NULL)
  895. goto out_problem;
  896. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  897. type = MAP__VARIABLE;
  898. else
  899. type = MAP__FUNCTION;
  900. map = map__new(&machine->user_dsos, event->mmap.start,
  901. event->mmap.len, event->mmap.pgoff,
  902. event->mmap.pid, 0, 0, 0, 0,
  903. event->mmap.filename,
  904. type);
  905. if (map == NULL)
  906. goto out_problem;
  907. thread__insert_map(thread, map);
  908. return 0;
  909. out_problem:
  910. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  911. return 0;
  912. }
  913. static void machine__remove_thread(struct machine *machine, struct thread *th)
  914. {
  915. machine->last_match = NULL;
  916. rb_erase(&th->rb_node, &machine->threads);
  917. /*
  918. * We may have references to this thread, for instance in some hist_entry
  919. * instances, so just move them to a separate list.
  920. */
  921. list_add_tail(&th->node, &machine->dead_threads);
  922. }
  923. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  924. struct perf_sample *sample)
  925. {
  926. struct thread *thread = machine__find_thread(machine,
  927. event->fork.pid,
  928. event->fork.tid);
  929. struct thread *parent = machine__findnew_thread(machine,
  930. event->fork.ppid,
  931. event->fork.ptid);
  932. /* if a thread currently exists for the thread id remove it */
  933. if (thread != NULL)
  934. machine__remove_thread(machine, thread);
  935. thread = machine__findnew_thread(machine, event->fork.pid,
  936. event->fork.tid);
  937. if (dump_trace)
  938. perf_event__fprintf_task(event, stdout);
  939. if (thread == NULL || parent == NULL ||
  940. thread__fork(thread, parent, sample->time) < 0) {
  941. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  942. return -1;
  943. }
  944. return 0;
  945. }
  946. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  947. struct perf_sample *sample __maybe_unused)
  948. {
  949. struct thread *thread = machine__find_thread(machine,
  950. event->fork.pid,
  951. event->fork.tid);
  952. if (dump_trace)
  953. perf_event__fprintf_task(event, stdout);
  954. if (thread != NULL)
  955. thread__exited(thread);
  956. return 0;
  957. }
  958. int machine__process_event(struct machine *machine, union perf_event *event,
  959. struct perf_sample *sample)
  960. {
  961. int ret;
  962. switch (event->header.type) {
  963. case PERF_RECORD_COMM:
  964. ret = machine__process_comm_event(machine, event, sample); break;
  965. case PERF_RECORD_MMAP:
  966. ret = machine__process_mmap_event(machine, event, sample); break;
  967. case PERF_RECORD_MMAP2:
  968. ret = machine__process_mmap2_event(machine, event, sample); break;
  969. case PERF_RECORD_FORK:
  970. ret = machine__process_fork_event(machine, event, sample); break;
  971. case PERF_RECORD_EXIT:
  972. ret = machine__process_exit_event(machine, event, sample); break;
  973. case PERF_RECORD_LOST:
  974. ret = machine__process_lost_event(machine, event, sample); break;
  975. default:
  976. ret = -1;
  977. break;
  978. }
  979. return ret;
  980. }
  981. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  982. {
  983. if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
  984. return 1;
  985. return 0;
  986. }
  987. static void ip__resolve_ams(struct machine *machine, struct thread *thread,
  988. struct addr_map_symbol *ams,
  989. u64 ip)
  990. {
  991. struct addr_location al;
  992. memset(&al, 0, sizeof(al));
  993. /*
  994. * We cannot use the header.misc hint to determine whether a
  995. * branch stack address is user, kernel, guest, hypervisor.
  996. * Branches may straddle the kernel/user/hypervisor boundaries.
  997. * Thus, we have to try consecutively until we find a match
  998. * or else, the symbol is unknown
  999. */
  1000. thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
  1001. ams->addr = ip;
  1002. ams->al_addr = al.addr;
  1003. ams->sym = al.sym;
  1004. ams->map = al.map;
  1005. }
  1006. static void ip__resolve_data(struct machine *machine, struct thread *thread,
  1007. u8 m, struct addr_map_symbol *ams, u64 addr)
  1008. {
  1009. struct addr_location al;
  1010. memset(&al, 0, sizeof(al));
  1011. thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
  1012. &al);
  1013. ams->addr = addr;
  1014. ams->al_addr = al.addr;
  1015. ams->sym = al.sym;
  1016. ams->map = al.map;
  1017. }
  1018. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  1019. struct addr_location *al)
  1020. {
  1021. struct mem_info *mi = zalloc(sizeof(*mi));
  1022. if (!mi)
  1023. return NULL;
  1024. ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
  1025. ip__resolve_data(al->machine, al->thread, al->cpumode,
  1026. &mi->daddr, sample->addr);
  1027. mi->data_src.val = sample->data_src;
  1028. return mi;
  1029. }
  1030. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  1031. struct addr_location *al)
  1032. {
  1033. unsigned int i;
  1034. const struct branch_stack *bs = sample->branch_stack;
  1035. struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
  1036. if (!bi)
  1037. return NULL;
  1038. for (i = 0; i < bs->nr; i++) {
  1039. ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
  1040. ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
  1041. bi[i].flags = bs->entries[i].flags;
  1042. }
  1043. return bi;
  1044. }
  1045. static int machine__resolve_callchain_sample(struct machine *machine,
  1046. struct thread *thread,
  1047. struct ip_callchain *chain,
  1048. struct symbol **parent,
  1049. struct addr_location *root_al,
  1050. int max_stack)
  1051. {
  1052. u8 cpumode = PERF_RECORD_MISC_USER;
  1053. int chain_nr = min(max_stack, (int)chain->nr);
  1054. int i;
  1055. int err;
  1056. callchain_cursor_reset(&callchain_cursor);
  1057. if (chain->nr > PERF_MAX_STACK_DEPTH) {
  1058. pr_warning("corrupted callchain. skipping...\n");
  1059. return 0;
  1060. }
  1061. for (i = 0; i < chain_nr; i++) {
  1062. u64 ip;
  1063. struct addr_location al;
  1064. if (callchain_param.order == ORDER_CALLEE)
  1065. ip = chain->ips[i];
  1066. else
  1067. ip = chain->ips[chain->nr - i - 1];
  1068. if (ip >= PERF_CONTEXT_MAX) {
  1069. switch (ip) {
  1070. case PERF_CONTEXT_HV:
  1071. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1072. break;
  1073. case PERF_CONTEXT_KERNEL:
  1074. cpumode = PERF_RECORD_MISC_KERNEL;
  1075. break;
  1076. case PERF_CONTEXT_USER:
  1077. cpumode = PERF_RECORD_MISC_USER;
  1078. break;
  1079. default:
  1080. pr_debug("invalid callchain context: "
  1081. "%"PRId64"\n", (s64) ip);
  1082. /*
  1083. * It seems the callchain is corrupted.
  1084. * Discard all.
  1085. */
  1086. callchain_cursor_reset(&callchain_cursor);
  1087. return 0;
  1088. }
  1089. continue;
  1090. }
  1091. al.filtered = 0;
  1092. thread__find_addr_location(thread, machine, cpumode,
  1093. MAP__FUNCTION, ip, &al);
  1094. if (al.sym != NULL) {
  1095. if (sort__has_parent && !*parent &&
  1096. symbol__match_regex(al.sym, &parent_regex))
  1097. *parent = al.sym;
  1098. else if (have_ignore_callees && root_al &&
  1099. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1100. /* Treat this symbol as the root,
  1101. forgetting its callees. */
  1102. *root_al = al;
  1103. callchain_cursor_reset(&callchain_cursor);
  1104. }
  1105. }
  1106. err = callchain_cursor_append(&callchain_cursor,
  1107. ip, al.map, al.sym);
  1108. if (err)
  1109. return err;
  1110. }
  1111. return 0;
  1112. }
  1113. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1114. {
  1115. struct callchain_cursor *cursor = arg;
  1116. return callchain_cursor_append(cursor, entry->ip,
  1117. entry->map, entry->sym);
  1118. }
  1119. int machine__resolve_callchain(struct machine *machine,
  1120. struct perf_evsel *evsel,
  1121. struct thread *thread,
  1122. struct perf_sample *sample,
  1123. struct symbol **parent,
  1124. struct addr_location *root_al,
  1125. int max_stack)
  1126. {
  1127. int ret;
  1128. ret = machine__resolve_callchain_sample(machine, thread,
  1129. sample->callchain, parent,
  1130. root_al, max_stack);
  1131. if (ret)
  1132. return ret;
  1133. /* Can we do dwarf post unwind? */
  1134. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1135. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1136. return 0;
  1137. /* Bail out if nothing was captured. */
  1138. if ((!sample->user_regs.regs) ||
  1139. (!sample->user_stack.size))
  1140. return 0;
  1141. return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
  1142. thread, sample, max_stack);
  1143. }
  1144. int machine__for_each_thread(struct machine *machine,
  1145. int (*fn)(struct thread *thread, void *p),
  1146. void *priv)
  1147. {
  1148. struct rb_node *nd;
  1149. struct thread *thread;
  1150. int rc = 0;
  1151. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  1152. thread = rb_entry(nd, struct thread, rb_node);
  1153. rc = fn(thread, priv);
  1154. if (rc != 0)
  1155. return rc;
  1156. }
  1157. list_for_each_entry(thread, &machine->dead_threads, node) {
  1158. rc = fn(thread, priv);
  1159. if (rc != 0)
  1160. return rc;
  1161. }
  1162. return rc;
  1163. }
  1164. int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
  1165. struct target *target, struct thread_map *threads,
  1166. perf_event__handler_t process, bool data_mmap)
  1167. {
  1168. if (target__has_task(target))
  1169. return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
  1170. else if (target__has_cpu(target))
  1171. return perf_event__synthesize_threads(tool, process, machine, data_mmap);
  1172. /* command specified */
  1173. return 0;
  1174. }