machine.c 36 KB

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