machine.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  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. #include "linux/hash.h"
  16. static void dsos__init(struct dsos *dsos)
  17. {
  18. INIT_LIST_HEAD(&dsos->head);
  19. dsos->root = RB_ROOT;
  20. }
  21. int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
  22. {
  23. map_groups__init(&machine->kmaps, machine);
  24. RB_CLEAR_NODE(&machine->rb_node);
  25. dsos__init(&machine->user_dsos);
  26. dsos__init(&machine->kernel_dsos);
  27. machine->threads = RB_ROOT;
  28. INIT_LIST_HEAD(&machine->dead_threads);
  29. machine->last_match = NULL;
  30. machine->vdso_info = NULL;
  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(machine);
  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. /*
  314. * We have to initialize map_groups separately
  315. * after rb tree is updated.
  316. *
  317. * The reason is that we call machine__findnew_thread
  318. * within thread__init_map_groups to find the thread
  319. * leader and that would screwed the rb tree.
  320. */
  321. if (thread__init_map_groups(th, machine)) {
  322. rb_erase(&th->rb_node, &machine->threads);
  323. thread__delete(th);
  324. return NULL;
  325. }
  326. machine->last_match = th;
  327. }
  328. return th;
  329. }
  330. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
  331. pid_t tid)
  332. {
  333. return __machine__findnew_thread(machine, pid, tid, true);
  334. }
  335. struct thread *machine__find_thread(struct machine *machine, pid_t pid,
  336. pid_t tid)
  337. {
  338. return __machine__findnew_thread(machine, pid, tid, false);
  339. }
  340. struct comm *machine__thread_exec_comm(struct machine *machine,
  341. struct thread *thread)
  342. {
  343. if (machine->comm_exec)
  344. return thread__exec_comm(thread);
  345. else
  346. return thread__comm(thread);
  347. }
  348. int machine__process_comm_event(struct machine *machine, union perf_event *event,
  349. struct perf_sample *sample)
  350. {
  351. struct thread *thread = machine__findnew_thread(machine,
  352. event->comm.pid,
  353. event->comm.tid);
  354. bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
  355. if (exec)
  356. machine->comm_exec = true;
  357. if (dump_trace)
  358. perf_event__fprintf_comm(event, stdout);
  359. if (thread == NULL ||
  360. __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
  361. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  362. return -1;
  363. }
  364. return 0;
  365. }
  366. int machine__process_lost_event(struct machine *machine __maybe_unused,
  367. union perf_event *event, struct perf_sample *sample __maybe_unused)
  368. {
  369. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  370. event->lost.id, event->lost.lost);
  371. return 0;
  372. }
  373. struct map *machine__new_module(struct machine *machine, u64 start,
  374. const char *filename)
  375. {
  376. struct map *map;
  377. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  378. bool compressed;
  379. if (dso == NULL)
  380. return NULL;
  381. map = map__new2(start, dso, MAP__FUNCTION);
  382. if (map == NULL)
  383. return NULL;
  384. if (machine__is_host(machine))
  385. dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
  386. else
  387. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
  388. /* _KMODULE_COMP should be next to _KMODULE */
  389. if (is_kernel_module(filename, &compressed) && compressed)
  390. dso->symtab_type++;
  391. map_groups__insert(&machine->kmaps, map);
  392. return map;
  393. }
  394. size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
  395. {
  396. struct rb_node *nd;
  397. size_t ret = __dsos__fprintf(&machines->host.kernel_dsos.head, fp) +
  398. __dsos__fprintf(&machines->host.user_dsos.head, fp);
  399. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  400. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  401. ret += __dsos__fprintf(&pos->kernel_dsos.head, fp);
  402. ret += __dsos__fprintf(&pos->user_dsos.head, fp);
  403. }
  404. return ret;
  405. }
  406. size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
  407. bool (skip)(struct dso *dso, int parm), int parm)
  408. {
  409. return __dsos__fprintf_buildid(&m->kernel_dsos.head, fp, skip, parm) +
  410. __dsos__fprintf_buildid(&m->user_dsos.head, fp, skip, parm);
  411. }
  412. size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
  413. bool (skip)(struct dso *dso, int parm), int parm)
  414. {
  415. struct rb_node *nd;
  416. size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
  417. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  418. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  419. ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
  420. }
  421. return ret;
  422. }
  423. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  424. {
  425. int i;
  426. size_t printed = 0;
  427. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  428. if (kdso->has_build_id) {
  429. char filename[PATH_MAX];
  430. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  431. printed += fprintf(fp, "[0] %s\n", filename);
  432. }
  433. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  434. printed += fprintf(fp, "[%d] %s\n",
  435. i + kdso->has_build_id, vmlinux_path[i]);
  436. return printed;
  437. }
  438. size_t machine__fprintf(struct machine *machine, FILE *fp)
  439. {
  440. size_t ret = 0;
  441. struct rb_node *nd;
  442. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  443. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  444. ret += thread__fprintf(pos, fp);
  445. }
  446. return ret;
  447. }
  448. static struct dso *machine__get_kernel(struct machine *machine)
  449. {
  450. const char *vmlinux_name = NULL;
  451. struct dso *kernel;
  452. if (machine__is_host(machine)) {
  453. vmlinux_name = symbol_conf.vmlinux_name;
  454. if (!vmlinux_name)
  455. vmlinux_name = "[kernel.kallsyms]";
  456. kernel = dso__kernel_findnew(machine, vmlinux_name,
  457. "[kernel]",
  458. DSO_TYPE_KERNEL);
  459. } else {
  460. char bf[PATH_MAX];
  461. if (machine__is_default_guest(machine))
  462. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  463. if (!vmlinux_name)
  464. vmlinux_name = machine__mmap_name(machine, bf,
  465. sizeof(bf));
  466. kernel = dso__kernel_findnew(machine, vmlinux_name,
  467. "[guest.kernel]",
  468. DSO_TYPE_GUEST_KERNEL);
  469. }
  470. if (kernel != NULL && (!kernel->has_build_id))
  471. dso__read_running_kernel_build_id(kernel, machine);
  472. return kernel;
  473. }
  474. struct process_args {
  475. u64 start;
  476. };
  477. static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
  478. size_t bufsz)
  479. {
  480. if (machine__is_default_guest(machine))
  481. scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
  482. else
  483. scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
  484. }
  485. const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
  486. /* Figure out the start address of kernel map from /proc/kallsyms.
  487. * Returns the name of the start symbol in *symbol_name. Pass in NULL as
  488. * symbol_name if it's not that important.
  489. */
  490. static u64 machine__get_running_kernel_start(struct machine *machine,
  491. const char **symbol_name)
  492. {
  493. char filename[PATH_MAX];
  494. int i;
  495. const char *name;
  496. u64 addr = 0;
  497. machine__get_kallsyms_filename(machine, filename, PATH_MAX);
  498. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  499. return 0;
  500. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  501. addr = kallsyms__get_function_start(filename, name);
  502. if (addr)
  503. break;
  504. }
  505. if (symbol_name)
  506. *symbol_name = name;
  507. return addr;
  508. }
  509. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  510. {
  511. enum map_type type;
  512. u64 start = machine__get_running_kernel_start(machine, NULL);
  513. for (type = 0; type < MAP__NR_TYPES; ++type) {
  514. struct kmap *kmap;
  515. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  516. if (machine->vmlinux_maps[type] == NULL)
  517. return -1;
  518. machine->vmlinux_maps[type]->map_ip =
  519. machine->vmlinux_maps[type]->unmap_ip =
  520. identity__map_ip;
  521. kmap = map__kmap(machine->vmlinux_maps[type]);
  522. kmap->kmaps = &machine->kmaps;
  523. map_groups__insert(&machine->kmaps,
  524. machine->vmlinux_maps[type]);
  525. }
  526. return 0;
  527. }
  528. void machine__destroy_kernel_maps(struct machine *machine)
  529. {
  530. enum map_type type;
  531. for (type = 0; type < MAP__NR_TYPES; ++type) {
  532. struct kmap *kmap;
  533. if (machine->vmlinux_maps[type] == NULL)
  534. continue;
  535. kmap = map__kmap(machine->vmlinux_maps[type]);
  536. map_groups__remove(&machine->kmaps,
  537. machine->vmlinux_maps[type]);
  538. if (kmap->ref_reloc_sym) {
  539. /*
  540. * ref_reloc_sym is shared among all maps, so free just
  541. * on one of them.
  542. */
  543. if (type == MAP__FUNCTION) {
  544. zfree((char **)&kmap->ref_reloc_sym->name);
  545. zfree(&kmap->ref_reloc_sym);
  546. } else
  547. kmap->ref_reloc_sym = NULL;
  548. }
  549. map__delete(machine->vmlinux_maps[type]);
  550. machine->vmlinux_maps[type] = NULL;
  551. }
  552. }
  553. int machines__create_guest_kernel_maps(struct machines *machines)
  554. {
  555. int ret = 0;
  556. struct dirent **namelist = NULL;
  557. int i, items = 0;
  558. char path[PATH_MAX];
  559. pid_t pid;
  560. char *endp;
  561. if (symbol_conf.default_guest_vmlinux_name ||
  562. symbol_conf.default_guest_modules ||
  563. symbol_conf.default_guest_kallsyms) {
  564. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  565. }
  566. if (symbol_conf.guestmount) {
  567. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  568. if (items <= 0)
  569. return -ENOENT;
  570. for (i = 0; i < items; i++) {
  571. if (!isdigit(namelist[i]->d_name[0])) {
  572. /* Filter out . and .. */
  573. continue;
  574. }
  575. pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
  576. if ((*endp != '\0') ||
  577. (endp == namelist[i]->d_name) ||
  578. (errno == ERANGE)) {
  579. pr_debug("invalid directory (%s). Skipping.\n",
  580. namelist[i]->d_name);
  581. continue;
  582. }
  583. sprintf(path, "%s/%s/proc/kallsyms",
  584. symbol_conf.guestmount,
  585. namelist[i]->d_name);
  586. ret = access(path, R_OK);
  587. if (ret) {
  588. pr_debug("Can't access file %s\n", path);
  589. goto failure;
  590. }
  591. machines__create_kernel_maps(machines, pid);
  592. }
  593. failure:
  594. free(namelist);
  595. }
  596. return ret;
  597. }
  598. void machines__destroy_kernel_maps(struct machines *machines)
  599. {
  600. struct rb_node *next = rb_first(&machines->guests);
  601. machine__destroy_kernel_maps(&machines->host);
  602. while (next) {
  603. struct machine *pos = rb_entry(next, struct machine, rb_node);
  604. next = rb_next(&pos->rb_node);
  605. rb_erase(&pos->rb_node, &machines->guests);
  606. machine__delete(pos);
  607. }
  608. }
  609. int machines__create_kernel_maps(struct machines *machines, pid_t pid)
  610. {
  611. struct machine *machine = machines__findnew(machines, pid);
  612. if (machine == NULL)
  613. return -1;
  614. return machine__create_kernel_maps(machine);
  615. }
  616. int machine__load_kallsyms(struct machine *machine, const char *filename,
  617. enum map_type type, symbol_filter_t filter)
  618. {
  619. struct map *map = machine->vmlinux_maps[type];
  620. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  621. if (ret > 0) {
  622. dso__set_loaded(map->dso, type);
  623. /*
  624. * Since /proc/kallsyms will have multiple sessions for the
  625. * kernel, with modules between them, fixup the end of all
  626. * sections.
  627. */
  628. __map_groups__fixup_end(&machine->kmaps, type);
  629. }
  630. return ret;
  631. }
  632. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  633. symbol_filter_t filter)
  634. {
  635. struct map *map = machine->vmlinux_maps[type];
  636. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  637. if (ret > 0)
  638. dso__set_loaded(map->dso, type);
  639. return ret;
  640. }
  641. static void map_groups__fixup_end(struct map_groups *mg)
  642. {
  643. int i;
  644. for (i = 0; i < MAP__NR_TYPES; ++i)
  645. __map_groups__fixup_end(mg, i);
  646. }
  647. static char *get_kernel_version(const char *root_dir)
  648. {
  649. char version[PATH_MAX];
  650. FILE *file;
  651. char *name, *tmp;
  652. const char *prefix = "Linux version ";
  653. sprintf(version, "%s/proc/version", root_dir);
  654. file = fopen(version, "r");
  655. if (!file)
  656. return NULL;
  657. version[0] = '\0';
  658. tmp = fgets(version, sizeof(version), file);
  659. fclose(file);
  660. name = strstr(version, prefix);
  661. if (!name)
  662. return NULL;
  663. name += strlen(prefix);
  664. tmp = strchr(name, ' ');
  665. if (tmp)
  666. *tmp = '\0';
  667. return strdup(name);
  668. }
  669. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  670. const char *dir_name, int depth)
  671. {
  672. struct dirent *dent;
  673. DIR *dir = opendir(dir_name);
  674. int ret = 0;
  675. if (!dir) {
  676. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  677. return -1;
  678. }
  679. while ((dent = readdir(dir)) != NULL) {
  680. char path[PATH_MAX];
  681. struct stat st;
  682. /*sshfs might return bad dent->d_type, so we have to stat*/
  683. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  684. if (stat(path, &st))
  685. continue;
  686. if (S_ISDIR(st.st_mode)) {
  687. if (!strcmp(dent->d_name, ".") ||
  688. !strcmp(dent->d_name, ".."))
  689. continue;
  690. /* Do not follow top-level source and build symlinks */
  691. if (depth == 0) {
  692. if (!strcmp(dent->d_name, "source") ||
  693. !strcmp(dent->d_name, "build"))
  694. continue;
  695. }
  696. ret = map_groups__set_modules_path_dir(mg, path,
  697. depth + 1);
  698. if (ret < 0)
  699. goto out;
  700. } else {
  701. char *dot = strrchr(dent->d_name, '.'),
  702. dso_name[PATH_MAX];
  703. struct map *map;
  704. char *long_name;
  705. if (dot == NULL)
  706. continue;
  707. /* On some system, modules are compressed like .ko.gz */
  708. if (is_supported_compression(dot + 1) &&
  709. is_kmodule_extension(dot - 2))
  710. dot -= 3;
  711. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  712. (int)(dot - dent->d_name), dent->d_name);
  713. strxfrchar(dso_name, '-', '_');
  714. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  715. dso_name);
  716. if (map == NULL)
  717. continue;
  718. long_name = strdup(path);
  719. if (long_name == NULL) {
  720. ret = -1;
  721. goto out;
  722. }
  723. dso__set_long_name(map->dso, long_name, true);
  724. dso__kernel_module_get_build_id(map->dso, "");
  725. }
  726. }
  727. out:
  728. closedir(dir);
  729. return ret;
  730. }
  731. static int machine__set_modules_path(struct machine *machine)
  732. {
  733. char *version;
  734. char modules_path[PATH_MAX];
  735. version = get_kernel_version(machine->root_dir);
  736. if (!version)
  737. return -1;
  738. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
  739. machine->root_dir, version);
  740. free(version);
  741. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
  742. }
  743. static int machine__create_module(void *arg, const char *name, u64 start)
  744. {
  745. struct machine *machine = arg;
  746. struct map *map;
  747. map = machine__new_module(machine, start, name);
  748. if (map == NULL)
  749. return -1;
  750. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  751. return 0;
  752. }
  753. static int machine__create_modules(struct machine *machine)
  754. {
  755. const char *modules;
  756. char path[PATH_MAX];
  757. if (machine__is_default_guest(machine)) {
  758. modules = symbol_conf.default_guest_modules;
  759. } else {
  760. snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
  761. modules = path;
  762. }
  763. if (symbol__restricted_filename(modules, "/proc/modules"))
  764. return -1;
  765. if (modules__parse(modules, machine, machine__create_module))
  766. return -1;
  767. if (!machine__set_modules_path(machine))
  768. return 0;
  769. pr_debug("Problems setting modules path maps, continuing anyway...\n");
  770. return 0;
  771. }
  772. int machine__create_kernel_maps(struct machine *machine)
  773. {
  774. struct dso *kernel = machine__get_kernel(machine);
  775. const char *name;
  776. u64 addr = machine__get_running_kernel_start(machine, &name);
  777. if (!addr)
  778. return -1;
  779. if (kernel == NULL ||
  780. __machine__create_kernel_maps(machine, kernel) < 0)
  781. return -1;
  782. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  783. if (machine__is_host(machine))
  784. pr_debug("Problems creating module maps, "
  785. "continuing anyway...\n");
  786. else
  787. pr_debug("Problems creating module maps for guest %d, "
  788. "continuing anyway...\n", machine->pid);
  789. }
  790. /*
  791. * Now that we have all the maps created, just set the ->end of them:
  792. */
  793. map_groups__fixup_end(&machine->kmaps);
  794. if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
  795. addr)) {
  796. machine__destroy_kernel_maps(machine);
  797. return -1;
  798. }
  799. return 0;
  800. }
  801. static void machine__set_kernel_mmap_len(struct machine *machine,
  802. union perf_event *event)
  803. {
  804. int i;
  805. for (i = 0; i < MAP__NR_TYPES; i++) {
  806. machine->vmlinux_maps[i]->start = event->mmap.start;
  807. machine->vmlinux_maps[i]->end = (event->mmap.start +
  808. event->mmap.len);
  809. /*
  810. * Be a bit paranoid here, some perf.data file came with
  811. * a zero sized synthesized MMAP event for the kernel.
  812. */
  813. if (machine->vmlinux_maps[i]->end == 0)
  814. machine->vmlinux_maps[i]->end = ~0ULL;
  815. }
  816. }
  817. static bool machine__uses_kcore(struct machine *machine)
  818. {
  819. struct dso *dso;
  820. list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
  821. if (dso__is_kcore(dso))
  822. return true;
  823. }
  824. return false;
  825. }
  826. static int machine__process_kernel_mmap_event(struct machine *machine,
  827. union perf_event *event)
  828. {
  829. struct map *map;
  830. char kmmap_prefix[PATH_MAX];
  831. enum dso_kernel_type kernel_type;
  832. bool is_kernel_mmap;
  833. /* If we have maps from kcore then we do not need or want any others */
  834. if (machine__uses_kcore(machine))
  835. return 0;
  836. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  837. if (machine__is_host(machine))
  838. kernel_type = DSO_TYPE_KERNEL;
  839. else
  840. kernel_type = DSO_TYPE_GUEST_KERNEL;
  841. is_kernel_mmap = memcmp(event->mmap.filename,
  842. kmmap_prefix,
  843. strlen(kmmap_prefix) - 1) == 0;
  844. if (event->mmap.filename[0] == '/' ||
  845. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  846. char short_module_name[1024];
  847. char *name, *dot;
  848. if (event->mmap.filename[0] == '/') {
  849. name = strrchr(event->mmap.filename, '/');
  850. if (name == NULL)
  851. goto out_problem;
  852. ++name; /* skip / */
  853. dot = strrchr(name, '.');
  854. if (dot == NULL)
  855. goto out_problem;
  856. /* On some system, modules are compressed like .ko.gz */
  857. if (is_supported_compression(dot + 1))
  858. dot -= 3;
  859. if (!is_kmodule_extension(dot + 1))
  860. goto out_problem;
  861. snprintf(short_module_name, sizeof(short_module_name),
  862. "[%.*s]", (int)(dot - name), name);
  863. strxfrchar(short_module_name, '-', '_');
  864. } else
  865. strcpy(short_module_name, event->mmap.filename);
  866. map = machine__new_module(machine, event->mmap.start,
  867. event->mmap.filename);
  868. if (map == NULL)
  869. goto out_problem;
  870. name = strdup(short_module_name);
  871. if (name == NULL)
  872. goto out_problem;
  873. dso__set_short_name(map->dso, name, true);
  874. map->end = map->start + event->mmap.len;
  875. } else if (is_kernel_mmap) {
  876. const char *symbol_name = (event->mmap.filename +
  877. strlen(kmmap_prefix));
  878. /*
  879. * Should be there already, from the build-id table in
  880. * the header.
  881. */
  882. struct dso *kernel = NULL;
  883. struct dso *dso;
  884. list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
  885. if (is_kernel_module(dso->long_name, NULL))
  886. continue;
  887. kernel = dso;
  888. break;
  889. }
  890. if (kernel == NULL)
  891. kernel = __dsos__findnew(&machine->kernel_dsos,
  892. kmmap_prefix);
  893. if (kernel == NULL)
  894. goto out_problem;
  895. kernel->kernel = kernel_type;
  896. if (__machine__create_kernel_maps(machine, kernel) < 0)
  897. goto out_problem;
  898. if (strstr(kernel->long_name, "vmlinux"))
  899. dso__set_short_name(kernel, "[kernel.vmlinux]", false);
  900. machine__set_kernel_mmap_len(machine, event);
  901. /*
  902. * Avoid using a zero address (kptr_restrict) for the ref reloc
  903. * symbol. Effectively having zero here means that at record
  904. * time /proc/sys/kernel/kptr_restrict was non zero.
  905. */
  906. if (event->mmap.pgoff != 0) {
  907. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  908. symbol_name,
  909. event->mmap.pgoff);
  910. }
  911. if (machine__is_default_guest(machine)) {
  912. /*
  913. * preload dso of guest kernel and modules
  914. */
  915. dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
  916. NULL);
  917. }
  918. }
  919. return 0;
  920. out_problem:
  921. return -1;
  922. }
  923. int machine__process_mmap2_event(struct machine *machine,
  924. union perf_event *event,
  925. struct perf_sample *sample __maybe_unused)
  926. {
  927. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  928. struct thread *thread;
  929. struct map *map;
  930. enum map_type type;
  931. int ret = 0;
  932. if (dump_trace)
  933. perf_event__fprintf_mmap2(event, stdout);
  934. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  935. cpumode == PERF_RECORD_MISC_KERNEL) {
  936. ret = machine__process_kernel_mmap_event(machine, event);
  937. if (ret < 0)
  938. goto out_problem;
  939. return 0;
  940. }
  941. thread = machine__findnew_thread(machine, event->mmap2.pid,
  942. event->mmap2.tid);
  943. if (thread == NULL)
  944. goto out_problem;
  945. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  946. type = MAP__VARIABLE;
  947. else
  948. type = MAP__FUNCTION;
  949. map = map__new(machine, event->mmap2.start,
  950. event->mmap2.len, event->mmap2.pgoff,
  951. event->mmap2.pid, event->mmap2.maj,
  952. event->mmap2.min, event->mmap2.ino,
  953. event->mmap2.ino_generation,
  954. event->mmap2.prot,
  955. event->mmap2.flags,
  956. event->mmap2.filename, type, thread);
  957. if (map == NULL)
  958. goto out_problem;
  959. thread__insert_map(thread, map);
  960. return 0;
  961. out_problem:
  962. dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
  963. return 0;
  964. }
  965. int machine__process_mmap_event(struct machine *machine, union perf_event *event,
  966. struct perf_sample *sample __maybe_unused)
  967. {
  968. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  969. struct thread *thread;
  970. struct map *map;
  971. enum map_type type;
  972. int ret = 0;
  973. if (dump_trace)
  974. perf_event__fprintf_mmap(event, stdout);
  975. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  976. cpumode == PERF_RECORD_MISC_KERNEL) {
  977. ret = machine__process_kernel_mmap_event(machine, event);
  978. if (ret < 0)
  979. goto out_problem;
  980. return 0;
  981. }
  982. thread = machine__findnew_thread(machine, event->mmap.pid,
  983. event->mmap.tid);
  984. if (thread == NULL)
  985. goto out_problem;
  986. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  987. type = MAP__VARIABLE;
  988. else
  989. type = MAP__FUNCTION;
  990. map = map__new(machine, event->mmap.start,
  991. event->mmap.len, event->mmap.pgoff,
  992. event->mmap.pid, 0, 0, 0, 0, 0, 0,
  993. event->mmap.filename,
  994. type, thread);
  995. if (map == NULL)
  996. goto out_problem;
  997. thread__insert_map(thread, map);
  998. return 0;
  999. out_problem:
  1000. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  1001. return 0;
  1002. }
  1003. static void machine__remove_thread(struct machine *machine, struct thread *th)
  1004. {
  1005. machine->last_match = NULL;
  1006. rb_erase(&th->rb_node, &machine->threads);
  1007. /*
  1008. * We may have references to this thread, for instance in some hist_entry
  1009. * instances, so just move them to a separate list.
  1010. */
  1011. list_add_tail(&th->node, &machine->dead_threads);
  1012. }
  1013. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  1014. struct perf_sample *sample)
  1015. {
  1016. struct thread *thread = machine__find_thread(machine,
  1017. event->fork.pid,
  1018. event->fork.tid);
  1019. struct thread *parent = machine__findnew_thread(machine,
  1020. event->fork.ppid,
  1021. event->fork.ptid);
  1022. /* if a thread currently exists for the thread id remove it */
  1023. if (thread != NULL)
  1024. machine__remove_thread(machine, thread);
  1025. thread = machine__findnew_thread(machine, event->fork.pid,
  1026. event->fork.tid);
  1027. if (dump_trace)
  1028. perf_event__fprintf_task(event, stdout);
  1029. if (thread == NULL || parent == NULL ||
  1030. thread__fork(thread, parent, sample->time) < 0) {
  1031. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  1032. return -1;
  1033. }
  1034. return 0;
  1035. }
  1036. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  1037. struct perf_sample *sample __maybe_unused)
  1038. {
  1039. struct thread *thread = machine__find_thread(machine,
  1040. event->fork.pid,
  1041. event->fork.tid);
  1042. if (dump_trace)
  1043. perf_event__fprintf_task(event, stdout);
  1044. if (thread != NULL)
  1045. thread__exited(thread);
  1046. return 0;
  1047. }
  1048. int machine__process_event(struct machine *machine, union perf_event *event,
  1049. struct perf_sample *sample)
  1050. {
  1051. int ret;
  1052. switch (event->header.type) {
  1053. case PERF_RECORD_COMM:
  1054. ret = machine__process_comm_event(machine, event, sample); break;
  1055. case PERF_RECORD_MMAP:
  1056. ret = machine__process_mmap_event(machine, event, sample); break;
  1057. case PERF_RECORD_MMAP2:
  1058. ret = machine__process_mmap2_event(machine, event, sample); break;
  1059. case PERF_RECORD_FORK:
  1060. ret = machine__process_fork_event(machine, event, sample); break;
  1061. case PERF_RECORD_EXIT:
  1062. ret = machine__process_exit_event(machine, event, sample); break;
  1063. case PERF_RECORD_LOST:
  1064. ret = machine__process_lost_event(machine, event, sample); break;
  1065. default:
  1066. ret = -1;
  1067. break;
  1068. }
  1069. return ret;
  1070. }
  1071. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  1072. {
  1073. if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
  1074. return 1;
  1075. return 0;
  1076. }
  1077. static void ip__resolve_ams(struct thread *thread,
  1078. struct addr_map_symbol *ams,
  1079. u64 ip)
  1080. {
  1081. struct addr_location al;
  1082. memset(&al, 0, sizeof(al));
  1083. /*
  1084. * We cannot use the header.misc hint to determine whether a
  1085. * branch stack address is user, kernel, guest, hypervisor.
  1086. * Branches may straddle the kernel/user/hypervisor boundaries.
  1087. * Thus, we have to try consecutively until we find a match
  1088. * or else, the symbol is unknown
  1089. */
  1090. thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
  1091. ams->addr = ip;
  1092. ams->al_addr = al.addr;
  1093. ams->sym = al.sym;
  1094. ams->map = al.map;
  1095. }
  1096. static void ip__resolve_data(struct thread *thread,
  1097. u8 m, struct addr_map_symbol *ams, u64 addr)
  1098. {
  1099. struct addr_location al;
  1100. memset(&al, 0, sizeof(al));
  1101. thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
  1102. if (al.map == NULL) {
  1103. /*
  1104. * some shared data regions have execute bit set which puts
  1105. * their mapping in the MAP__FUNCTION type array.
  1106. * Check there as a fallback option before dropping the sample.
  1107. */
  1108. thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
  1109. }
  1110. ams->addr = addr;
  1111. ams->al_addr = al.addr;
  1112. ams->sym = al.sym;
  1113. ams->map = al.map;
  1114. }
  1115. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  1116. struct addr_location *al)
  1117. {
  1118. struct mem_info *mi = zalloc(sizeof(*mi));
  1119. if (!mi)
  1120. return NULL;
  1121. ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
  1122. ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
  1123. mi->data_src.val = sample->data_src;
  1124. return mi;
  1125. }
  1126. static int add_callchain_ip(struct thread *thread,
  1127. struct symbol **parent,
  1128. struct addr_location *root_al,
  1129. bool branch_history,
  1130. u64 ip)
  1131. {
  1132. struct addr_location al;
  1133. al.filtered = 0;
  1134. al.sym = NULL;
  1135. if (branch_history)
  1136. thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
  1137. ip, &al);
  1138. else {
  1139. u8 cpumode = PERF_RECORD_MISC_USER;
  1140. if (ip >= PERF_CONTEXT_MAX) {
  1141. switch (ip) {
  1142. case PERF_CONTEXT_HV:
  1143. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1144. break;
  1145. case PERF_CONTEXT_KERNEL:
  1146. cpumode = PERF_RECORD_MISC_KERNEL;
  1147. break;
  1148. case PERF_CONTEXT_USER:
  1149. cpumode = PERF_RECORD_MISC_USER;
  1150. break;
  1151. default:
  1152. pr_debug("invalid callchain context: "
  1153. "%"PRId64"\n", (s64) ip);
  1154. /*
  1155. * It seems the callchain is corrupted.
  1156. * Discard all.
  1157. */
  1158. callchain_cursor_reset(&callchain_cursor);
  1159. return 1;
  1160. }
  1161. return 0;
  1162. }
  1163. thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
  1164. ip, &al);
  1165. }
  1166. if (al.sym != NULL) {
  1167. if (sort__has_parent && !*parent &&
  1168. symbol__match_regex(al.sym, &parent_regex))
  1169. *parent = al.sym;
  1170. else if (have_ignore_callees && root_al &&
  1171. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1172. /* Treat this symbol as the root,
  1173. forgetting its callees. */
  1174. *root_al = al;
  1175. callchain_cursor_reset(&callchain_cursor);
  1176. }
  1177. }
  1178. return callchain_cursor_append(&callchain_cursor, al.addr, al.map, al.sym);
  1179. }
  1180. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  1181. struct addr_location *al)
  1182. {
  1183. unsigned int i;
  1184. const struct branch_stack *bs = sample->branch_stack;
  1185. struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
  1186. if (!bi)
  1187. return NULL;
  1188. for (i = 0; i < bs->nr; i++) {
  1189. ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
  1190. ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
  1191. bi[i].flags = bs->entries[i].flags;
  1192. }
  1193. return bi;
  1194. }
  1195. #define CHASHSZ 127
  1196. #define CHASHBITS 7
  1197. #define NO_ENTRY 0xff
  1198. #define PERF_MAX_BRANCH_DEPTH 127
  1199. /* Remove loops. */
  1200. static int remove_loops(struct branch_entry *l, int nr)
  1201. {
  1202. int i, j, off;
  1203. unsigned char chash[CHASHSZ];
  1204. memset(chash, NO_ENTRY, sizeof(chash));
  1205. BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
  1206. for (i = 0; i < nr; i++) {
  1207. int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
  1208. /* no collision handling for now */
  1209. if (chash[h] == NO_ENTRY) {
  1210. chash[h] = i;
  1211. } else if (l[chash[h]].from == l[i].from) {
  1212. bool is_loop = true;
  1213. /* check if it is a real loop */
  1214. off = 0;
  1215. for (j = chash[h]; j < i && i + off < nr; j++, off++)
  1216. if (l[j].from != l[i + off].from) {
  1217. is_loop = false;
  1218. break;
  1219. }
  1220. if (is_loop) {
  1221. memmove(l + i, l + i + off,
  1222. (nr - (i + off)) * sizeof(*l));
  1223. nr -= off;
  1224. }
  1225. }
  1226. }
  1227. return nr;
  1228. }
  1229. static int thread__resolve_callchain_sample(struct thread *thread,
  1230. struct ip_callchain *chain,
  1231. struct branch_stack *branch,
  1232. struct symbol **parent,
  1233. struct addr_location *root_al,
  1234. int max_stack)
  1235. {
  1236. int chain_nr = min(max_stack, (int)chain->nr);
  1237. int i, j, err;
  1238. int skip_idx = -1;
  1239. int first_call = 0;
  1240. /*
  1241. * Based on DWARF debug information, some architectures skip
  1242. * a callchain entry saved by the kernel.
  1243. */
  1244. if (chain->nr < PERF_MAX_STACK_DEPTH)
  1245. skip_idx = arch_skip_callchain_idx(thread, chain);
  1246. callchain_cursor_reset(&callchain_cursor);
  1247. /*
  1248. * Add branches to call stack for easier browsing. This gives
  1249. * more context for a sample than just the callers.
  1250. *
  1251. * This uses individual histograms of paths compared to the
  1252. * aggregated histograms the normal LBR mode uses.
  1253. *
  1254. * Limitations for now:
  1255. * - No extra filters
  1256. * - No annotations (should annotate somehow)
  1257. */
  1258. if (branch && callchain_param.branch_callstack) {
  1259. int nr = min(max_stack, (int)branch->nr);
  1260. struct branch_entry be[nr];
  1261. if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
  1262. pr_warning("corrupted branch chain. skipping...\n");
  1263. goto check_calls;
  1264. }
  1265. for (i = 0; i < nr; i++) {
  1266. if (callchain_param.order == ORDER_CALLEE) {
  1267. be[i] = branch->entries[i];
  1268. /*
  1269. * Check for overlap into the callchain.
  1270. * The return address is one off compared to
  1271. * the branch entry. To adjust for this
  1272. * assume the calling instruction is not longer
  1273. * than 8 bytes.
  1274. */
  1275. if (i == skip_idx ||
  1276. chain->ips[first_call] >= PERF_CONTEXT_MAX)
  1277. first_call++;
  1278. else if (be[i].from < chain->ips[first_call] &&
  1279. be[i].from >= chain->ips[first_call] - 8)
  1280. first_call++;
  1281. } else
  1282. be[i] = branch->entries[branch->nr - i - 1];
  1283. }
  1284. nr = remove_loops(be, nr);
  1285. for (i = 0; i < nr; i++) {
  1286. err = add_callchain_ip(thread, parent, root_al,
  1287. true, be[i].to);
  1288. if (!err)
  1289. err = add_callchain_ip(thread, parent, root_al,
  1290. true, be[i].from);
  1291. if (err == -EINVAL)
  1292. break;
  1293. if (err)
  1294. return err;
  1295. }
  1296. chain_nr -= nr;
  1297. }
  1298. check_calls:
  1299. if (chain->nr > PERF_MAX_STACK_DEPTH) {
  1300. pr_warning("corrupted callchain. skipping...\n");
  1301. return 0;
  1302. }
  1303. for (i = first_call; i < chain_nr; i++) {
  1304. u64 ip;
  1305. if (callchain_param.order == ORDER_CALLEE)
  1306. j = i;
  1307. else
  1308. j = chain->nr - i - 1;
  1309. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  1310. if (j == skip_idx)
  1311. continue;
  1312. #endif
  1313. ip = chain->ips[j];
  1314. err = add_callchain_ip(thread, parent, root_al, false, ip);
  1315. if (err)
  1316. return (err < 0) ? err : 0;
  1317. }
  1318. return 0;
  1319. }
  1320. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1321. {
  1322. struct callchain_cursor *cursor = arg;
  1323. return callchain_cursor_append(cursor, entry->ip,
  1324. entry->map, entry->sym);
  1325. }
  1326. int thread__resolve_callchain(struct thread *thread,
  1327. struct perf_evsel *evsel,
  1328. struct perf_sample *sample,
  1329. struct symbol **parent,
  1330. struct addr_location *root_al,
  1331. int max_stack)
  1332. {
  1333. int ret = thread__resolve_callchain_sample(thread, sample->callchain,
  1334. sample->branch_stack,
  1335. parent, root_al, max_stack);
  1336. if (ret)
  1337. return ret;
  1338. /* Can we do dwarf post unwind? */
  1339. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1340. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1341. return 0;
  1342. /* Bail out if nothing was captured. */
  1343. if ((!sample->user_regs.regs) ||
  1344. (!sample->user_stack.size))
  1345. return 0;
  1346. return unwind__get_entries(unwind_entry, &callchain_cursor,
  1347. thread, sample, max_stack);
  1348. }
  1349. int machine__for_each_thread(struct machine *machine,
  1350. int (*fn)(struct thread *thread, void *p),
  1351. void *priv)
  1352. {
  1353. struct rb_node *nd;
  1354. struct thread *thread;
  1355. int rc = 0;
  1356. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  1357. thread = rb_entry(nd, struct thread, rb_node);
  1358. rc = fn(thread, priv);
  1359. if (rc != 0)
  1360. return rc;
  1361. }
  1362. list_for_each_entry(thread, &machine->dead_threads, node) {
  1363. rc = fn(thread, priv);
  1364. if (rc != 0)
  1365. return rc;
  1366. }
  1367. return rc;
  1368. }
  1369. int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
  1370. struct target *target, struct thread_map *threads,
  1371. perf_event__handler_t process, bool data_mmap)
  1372. {
  1373. if (target__has_task(target))
  1374. return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
  1375. else if (target__has_cpu(target))
  1376. return perf_event__synthesize_threads(tool, process, machine, data_mmap);
  1377. /* command specified */
  1378. return 0;
  1379. }
  1380. pid_t machine__get_current_tid(struct machine *machine, int cpu)
  1381. {
  1382. if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
  1383. return -1;
  1384. return machine->current_tid[cpu];
  1385. }
  1386. int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
  1387. pid_t tid)
  1388. {
  1389. struct thread *thread;
  1390. if (cpu < 0)
  1391. return -EINVAL;
  1392. if (!machine->current_tid) {
  1393. int i;
  1394. machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
  1395. if (!machine->current_tid)
  1396. return -ENOMEM;
  1397. for (i = 0; i < MAX_NR_CPUS; i++)
  1398. machine->current_tid[i] = -1;
  1399. }
  1400. if (cpu >= MAX_NR_CPUS) {
  1401. pr_err("Requested CPU %d too large. ", cpu);
  1402. pr_err("Consider raising MAX_NR_CPUS\n");
  1403. return -EINVAL;
  1404. }
  1405. machine->current_tid[cpu] = tid;
  1406. thread = machine__findnew_thread(machine, pid, tid);
  1407. if (!thread)
  1408. return -ENOMEM;
  1409. thread->cpu = cpu;
  1410. return 0;
  1411. }
  1412. int machine__get_kernel_start(struct machine *machine)
  1413. {
  1414. struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
  1415. int err = 0;
  1416. /*
  1417. * The only addresses above 2^63 are kernel addresses of a 64-bit
  1418. * kernel. Note that addresses are unsigned so that on a 32-bit system
  1419. * all addresses including kernel addresses are less than 2^32. In
  1420. * that case (32-bit system), if the kernel mapping is unknown, all
  1421. * addresses will be assumed to be in user space - see
  1422. * machine__kernel_ip().
  1423. */
  1424. machine->kernel_start = 1ULL << 63;
  1425. if (map) {
  1426. err = map__load(map, machine->symbol_filter);
  1427. if (map->start)
  1428. machine->kernel_start = map->start;
  1429. }
  1430. return err;
  1431. }