machine.c 38 KB

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