machine.c 43 KB

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