machine.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <inttypes.h>
  5. #include <regex.h>
  6. #include "callchain.h"
  7. #include "debug.h"
  8. #include "event.h"
  9. #include "evsel.h"
  10. #include "hist.h"
  11. #include "machine.h"
  12. #include "map.h"
  13. #include "sort.h"
  14. #include "strlist.h"
  15. #include "thread.h"
  16. #include "vdso.h"
  17. #include <stdbool.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. #include "unwind.h"
  22. #include "linux/hash.h"
  23. #include "asm/bug.h"
  24. #include "sane_ctype.h"
  25. #include <symbol/kallsyms.h>
  26. static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
  27. static void dsos__init(struct dsos *dsos)
  28. {
  29. INIT_LIST_HEAD(&dsos->head);
  30. dsos->root = RB_ROOT;
  31. pthread_rwlock_init(&dsos->lock, NULL);
  32. }
  33. int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
  34. {
  35. memset(machine, 0, sizeof(*machine));
  36. map_groups__init(&machine->kmaps, machine);
  37. RB_CLEAR_NODE(&machine->rb_node);
  38. dsos__init(&machine->dsos);
  39. machine->threads = RB_ROOT;
  40. pthread_rwlock_init(&machine->threads_lock, NULL);
  41. machine->nr_threads = 0;
  42. INIT_LIST_HEAD(&machine->dead_threads);
  43. machine->last_match = NULL;
  44. machine->vdso_info = NULL;
  45. machine->env = NULL;
  46. machine->pid = pid;
  47. machine->id_hdr_size = 0;
  48. machine->kptr_restrict_warned = false;
  49. machine->comm_exec = false;
  50. machine->kernel_start = 0;
  51. memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
  52. machine->root_dir = strdup(root_dir);
  53. if (machine->root_dir == NULL)
  54. return -ENOMEM;
  55. if (pid != HOST_KERNEL_ID) {
  56. struct thread *thread = machine__findnew_thread(machine, -1,
  57. pid);
  58. char comm[64];
  59. if (thread == NULL)
  60. return -ENOMEM;
  61. snprintf(comm, sizeof(comm), "[guest/%d]", pid);
  62. thread__set_comm(thread, comm, 0);
  63. thread__put(thread);
  64. }
  65. machine->current_tid = NULL;
  66. return 0;
  67. }
  68. struct machine *machine__new_host(void)
  69. {
  70. struct machine *machine = malloc(sizeof(*machine));
  71. if (machine != NULL) {
  72. machine__init(machine, "", HOST_KERNEL_ID);
  73. if (machine__create_kernel_maps(machine) < 0)
  74. goto out_delete;
  75. }
  76. return machine;
  77. out_delete:
  78. free(machine);
  79. return NULL;
  80. }
  81. struct machine *machine__new_kallsyms(void)
  82. {
  83. struct machine *machine = machine__new_host();
  84. /*
  85. * FIXME:
  86. * 1) MAP__FUNCTION will go away when we stop loading separate maps for
  87. * functions and data objects.
  88. * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
  89. * ask for not using the kcore parsing code, once this one is fixed
  90. * to create a map per module.
  91. */
  92. if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) {
  93. machine__delete(machine);
  94. machine = NULL;
  95. }
  96. return machine;
  97. }
  98. static void dsos__purge(struct dsos *dsos)
  99. {
  100. struct dso *pos, *n;
  101. pthread_rwlock_wrlock(&dsos->lock);
  102. list_for_each_entry_safe(pos, n, &dsos->head, node) {
  103. RB_CLEAR_NODE(&pos->rb_node);
  104. pos->root = NULL;
  105. list_del_init(&pos->node);
  106. dso__put(pos);
  107. }
  108. pthread_rwlock_unlock(&dsos->lock);
  109. }
  110. static void dsos__exit(struct dsos *dsos)
  111. {
  112. dsos__purge(dsos);
  113. pthread_rwlock_destroy(&dsos->lock);
  114. }
  115. void machine__delete_threads(struct machine *machine)
  116. {
  117. struct rb_node *nd;
  118. pthread_rwlock_wrlock(&machine->threads_lock);
  119. nd = rb_first(&machine->threads);
  120. while (nd) {
  121. struct thread *t = rb_entry(nd, struct thread, rb_node);
  122. nd = rb_next(nd);
  123. __machine__remove_thread(machine, t, false);
  124. }
  125. pthread_rwlock_unlock(&machine->threads_lock);
  126. }
  127. void machine__exit(struct machine *machine)
  128. {
  129. if (machine == NULL)
  130. return;
  131. machine__destroy_kernel_maps(machine);
  132. map_groups__exit(&machine->kmaps);
  133. dsos__exit(&machine->dsos);
  134. machine__exit_vdso(machine);
  135. zfree(&machine->root_dir);
  136. zfree(&machine->current_tid);
  137. pthread_rwlock_destroy(&machine->threads_lock);
  138. }
  139. void machine__delete(struct machine *machine)
  140. {
  141. if (machine) {
  142. machine__exit(machine);
  143. free(machine);
  144. }
  145. }
  146. void machines__init(struct machines *machines)
  147. {
  148. machine__init(&machines->host, "", HOST_KERNEL_ID);
  149. machines->guests = RB_ROOT;
  150. }
  151. void machines__exit(struct machines *machines)
  152. {
  153. machine__exit(&machines->host);
  154. /* XXX exit guest */
  155. }
  156. struct machine *machines__add(struct machines *machines, pid_t pid,
  157. const char *root_dir)
  158. {
  159. struct rb_node **p = &machines->guests.rb_node;
  160. struct rb_node *parent = NULL;
  161. struct machine *pos, *machine = malloc(sizeof(*machine));
  162. if (machine == NULL)
  163. return NULL;
  164. if (machine__init(machine, root_dir, pid) != 0) {
  165. free(machine);
  166. return NULL;
  167. }
  168. while (*p != NULL) {
  169. parent = *p;
  170. pos = rb_entry(parent, struct machine, rb_node);
  171. if (pid < pos->pid)
  172. p = &(*p)->rb_left;
  173. else
  174. p = &(*p)->rb_right;
  175. }
  176. rb_link_node(&machine->rb_node, parent, p);
  177. rb_insert_color(&machine->rb_node, &machines->guests);
  178. return machine;
  179. }
  180. void machines__set_comm_exec(struct machines *machines, bool comm_exec)
  181. {
  182. struct rb_node *nd;
  183. machines->host.comm_exec = comm_exec;
  184. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  185. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  186. machine->comm_exec = comm_exec;
  187. }
  188. }
  189. struct machine *machines__find(struct machines *machines, pid_t pid)
  190. {
  191. struct rb_node **p = &machines->guests.rb_node;
  192. struct rb_node *parent = NULL;
  193. struct machine *machine;
  194. struct machine *default_machine = NULL;
  195. if (pid == HOST_KERNEL_ID)
  196. return &machines->host;
  197. while (*p != NULL) {
  198. parent = *p;
  199. machine = rb_entry(parent, struct machine, rb_node);
  200. if (pid < machine->pid)
  201. p = &(*p)->rb_left;
  202. else if (pid > machine->pid)
  203. p = &(*p)->rb_right;
  204. else
  205. return machine;
  206. if (!machine->pid)
  207. default_machine = machine;
  208. }
  209. return default_machine;
  210. }
  211. struct machine *machines__findnew(struct machines *machines, pid_t pid)
  212. {
  213. char path[PATH_MAX];
  214. const char *root_dir = "";
  215. struct machine *machine = machines__find(machines, pid);
  216. if (machine && (machine->pid == pid))
  217. goto out;
  218. if ((pid != HOST_KERNEL_ID) &&
  219. (pid != DEFAULT_GUEST_KERNEL_ID) &&
  220. (symbol_conf.guestmount)) {
  221. sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
  222. if (access(path, R_OK)) {
  223. static struct strlist *seen;
  224. if (!seen)
  225. seen = strlist__new(NULL, NULL);
  226. if (!strlist__has_entry(seen, path)) {
  227. pr_err("Can't access file %s\n", path);
  228. strlist__add(seen, path);
  229. }
  230. machine = NULL;
  231. goto out;
  232. }
  233. root_dir = path;
  234. }
  235. machine = machines__add(machines, pid, root_dir);
  236. out:
  237. return machine;
  238. }
  239. void machines__process_guests(struct machines *machines,
  240. machine__process_t process, void *data)
  241. {
  242. struct rb_node *nd;
  243. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  244. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  245. process(pos, data);
  246. }
  247. }
  248. char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
  249. {
  250. if (machine__is_host(machine))
  251. snprintf(bf, size, "[%s]", "kernel.kallsyms");
  252. else if (machine__is_default_guest(machine))
  253. snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
  254. else {
  255. snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
  256. machine->pid);
  257. }
  258. return bf;
  259. }
  260. void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
  261. {
  262. struct rb_node *node;
  263. struct machine *machine;
  264. machines->host.id_hdr_size = id_hdr_size;
  265. for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
  266. machine = rb_entry(node, struct machine, rb_node);
  267. machine->id_hdr_size = id_hdr_size;
  268. }
  269. return;
  270. }
  271. static void machine__update_thread_pid(struct machine *machine,
  272. struct thread *th, pid_t pid)
  273. {
  274. struct thread *leader;
  275. if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
  276. return;
  277. th->pid_ = pid;
  278. if (th->pid_ == th->tid)
  279. return;
  280. leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
  281. if (!leader)
  282. goto out_err;
  283. if (!leader->mg)
  284. leader->mg = map_groups__new(machine);
  285. if (!leader->mg)
  286. goto out_err;
  287. if (th->mg == leader->mg)
  288. return;
  289. if (th->mg) {
  290. /*
  291. * Maps are created from MMAP events which provide the pid and
  292. * tid. Consequently there never should be any maps on a thread
  293. * with an unknown pid. Just print an error if there are.
  294. */
  295. if (!map_groups__empty(th->mg))
  296. pr_err("Discarding thread maps for %d:%d\n",
  297. th->pid_, th->tid);
  298. map_groups__put(th->mg);
  299. }
  300. th->mg = map_groups__get(leader->mg);
  301. out_put:
  302. thread__put(leader);
  303. return;
  304. out_err:
  305. pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
  306. goto out_put;
  307. }
  308. /*
  309. * Caller must eventually drop thread->refcnt returned with a successful
  310. * lookup/new thread inserted.
  311. */
  312. static struct thread *____machine__findnew_thread(struct machine *machine,
  313. pid_t pid, pid_t tid,
  314. bool create)
  315. {
  316. struct rb_node **p = &machine->threads.rb_node;
  317. struct rb_node *parent = NULL;
  318. struct thread *th;
  319. /*
  320. * Front-end cache - TID lookups come in blocks,
  321. * so most of the time we dont have to look up
  322. * the full rbtree:
  323. */
  324. th = machine->last_match;
  325. if (th != NULL) {
  326. if (th->tid == tid) {
  327. machine__update_thread_pid(machine, th, pid);
  328. return thread__get(th);
  329. }
  330. machine->last_match = NULL;
  331. }
  332. while (*p != NULL) {
  333. parent = *p;
  334. th = rb_entry(parent, struct thread, rb_node);
  335. if (th->tid == tid) {
  336. machine->last_match = th;
  337. machine__update_thread_pid(machine, th, pid);
  338. return thread__get(th);
  339. }
  340. if (tid < th->tid)
  341. p = &(*p)->rb_left;
  342. else
  343. p = &(*p)->rb_right;
  344. }
  345. if (!create)
  346. return NULL;
  347. th = thread__new(pid, tid);
  348. if (th != NULL) {
  349. rb_link_node(&th->rb_node, parent, p);
  350. rb_insert_color(&th->rb_node, &machine->threads);
  351. /*
  352. * We have to initialize map_groups separately
  353. * after rb tree is updated.
  354. *
  355. * The reason is that we call machine__findnew_thread
  356. * within thread__init_map_groups to find the thread
  357. * leader and that would screwed the rb tree.
  358. */
  359. if (thread__init_map_groups(th, machine)) {
  360. rb_erase_init(&th->rb_node, &machine->threads);
  361. RB_CLEAR_NODE(&th->rb_node);
  362. thread__put(th);
  363. return NULL;
  364. }
  365. /*
  366. * It is now in the rbtree, get a ref
  367. */
  368. thread__get(th);
  369. machine->last_match = th;
  370. ++machine->nr_threads;
  371. }
  372. return th;
  373. }
  374. struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
  375. {
  376. return ____machine__findnew_thread(machine, pid, tid, true);
  377. }
  378. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
  379. pid_t tid)
  380. {
  381. struct thread *th;
  382. pthread_rwlock_wrlock(&machine->threads_lock);
  383. th = __machine__findnew_thread(machine, pid, tid);
  384. pthread_rwlock_unlock(&machine->threads_lock);
  385. return th;
  386. }
  387. struct thread *machine__find_thread(struct machine *machine, pid_t pid,
  388. pid_t tid)
  389. {
  390. struct thread *th;
  391. pthread_rwlock_rdlock(&machine->threads_lock);
  392. th = ____machine__findnew_thread(machine, pid, tid, false);
  393. pthread_rwlock_unlock(&machine->threads_lock);
  394. return th;
  395. }
  396. struct comm *machine__thread_exec_comm(struct machine *machine,
  397. struct thread *thread)
  398. {
  399. if (machine->comm_exec)
  400. return thread__exec_comm(thread);
  401. else
  402. return thread__comm(thread);
  403. }
  404. int machine__process_comm_event(struct machine *machine, union perf_event *event,
  405. struct perf_sample *sample)
  406. {
  407. struct thread *thread = machine__findnew_thread(machine,
  408. event->comm.pid,
  409. event->comm.tid);
  410. bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
  411. int err = 0;
  412. if (exec)
  413. machine->comm_exec = true;
  414. if (dump_trace)
  415. perf_event__fprintf_comm(event, stdout);
  416. if (thread == NULL ||
  417. __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
  418. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  419. err = -1;
  420. }
  421. thread__put(thread);
  422. return err;
  423. }
  424. int machine__process_namespaces_event(struct machine *machine __maybe_unused,
  425. union perf_event *event,
  426. struct perf_sample *sample __maybe_unused)
  427. {
  428. struct thread *thread = machine__findnew_thread(machine,
  429. event->namespaces.pid,
  430. event->namespaces.tid);
  431. int err = 0;
  432. WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
  433. "\nWARNING: kernel seems to support more namespaces than perf"
  434. " tool.\nTry updating the perf tool..\n\n");
  435. WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
  436. "\nWARNING: perf tool seems to support more namespaces than"
  437. " the kernel.\nTry updating the kernel..\n\n");
  438. if (dump_trace)
  439. perf_event__fprintf_namespaces(event, stdout);
  440. if (thread == NULL ||
  441. thread__set_namespaces(thread, sample->time, &event->namespaces)) {
  442. dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
  443. err = -1;
  444. }
  445. thread__put(thread);
  446. return err;
  447. }
  448. int machine__process_lost_event(struct machine *machine __maybe_unused,
  449. union perf_event *event, struct perf_sample *sample __maybe_unused)
  450. {
  451. dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
  452. event->lost.id, event->lost.lost);
  453. return 0;
  454. }
  455. int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
  456. union perf_event *event, struct perf_sample *sample)
  457. {
  458. dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
  459. sample->id, event->lost_samples.lost);
  460. return 0;
  461. }
  462. static struct dso *machine__findnew_module_dso(struct machine *machine,
  463. struct kmod_path *m,
  464. const char *filename)
  465. {
  466. struct dso *dso;
  467. pthread_rwlock_wrlock(&machine->dsos.lock);
  468. dso = __dsos__find(&machine->dsos, m->name, true);
  469. if (!dso) {
  470. dso = __dsos__addnew(&machine->dsos, m->name);
  471. if (dso == NULL)
  472. goto out_unlock;
  473. dso__set_module_info(dso, m, machine);
  474. dso__set_long_name(dso, strdup(filename), true);
  475. }
  476. dso__get(dso);
  477. out_unlock:
  478. pthread_rwlock_unlock(&machine->dsos.lock);
  479. return dso;
  480. }
  481. int machine__process_aux_event(struct machine *machine __maybe_unused,
  482. union perf_event *event)
  483. {
  484. if (dump_trace)
  485. perf_event__fprintf_aux(event, stdout);
  486. return 0;
  487. }
  488. int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
  489. union perf_event *event)
  490. {
  491. if (dump_trace)
  492. perf_event__fprintf_itrace_start(event, stdout);
  493. return 0;
  494. }
  495. int machine__process_switch_event(struct machine *machine __maybe_unused,
  496. union perf_event *event)
  497. {
  498. if (dump_trace)
  499. perf_event__fprintf_switch(event, stdout);
  500. return 0;
  501. }
  502. static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
  503. {
  504. const char *dup_filename;
  505. if (!filename || !dso || !dso->long_name)
  506. return;
  507. if (dso->long_name[0] != '[')
  508. return;
  509. if (!strchr(filename, '/'))
  510. return;
  511. dup_filename = strdup(filename);
  512. if (!dup_filename)
  513. return;
  514. dso__set_long_name(dso, dup_filename, true);
  515. }
  516. struct map *machine__findnew_module_map(struct machine *machine, u64 start,
  517. const char *filename)
  518. {
  519. struct map *map = NULL;
  520. struct dso *dso = NULL;
  521. struct kmod_path m;
  522. if (kmod_path__parse_name(&m, filename))
  523. return NULL;
  524. map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
  525. m.name);
  526. if (map) {
  527. /*
  528. * If the map's dso is an offline module, give dso__load()
  529. * a chance to find the file path of that module by fixing
  530. * long_name.
  531. */
  532. dso__adjust_kmod_long_name(map->dso, filename);
  533. goto out;
  534. }
  535. dso = machine__findnew_module_dso(machine, &m, filename);
  536. if (dso == NULL)
  537. goto out;
  538. map = map__new2(start, dso, MAP__FUNCTION);
  539. if (map == NULL)
  540. goto out;
  541. map_groups__insert(&machine->kmaps, map);
  542. /* Put the map here because map_groups__insert alread got it */
  543. map__put(map);
  544. out:
  545. /* put the dso here, corresponding to machine__findnew_module_dso */
  546. dso__put(dso);
  547. free(m.name);
  548. return map;
  549. }
  550. size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
  551. {
  552. struct rb_node *nd;
  553. size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
  554. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  555. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  556. ret += __dsos__fprintf(&pos->dsos.head, fp);
  557. }
  558. return ret;
  559. }
  560. size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
  561. bool (skip)(struct dso *dso, int parm), int parm)
  562. {
  563. return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
  564. }
  565. size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
  566. bool (skip)(struct dso *dso, int parm), int parm)
  567. {
  568. struct rb_node *nd;
  569. size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
  570. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  571. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  572. ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
  573. }
  574. return ret;
  575. }
  576. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  577. {
  578. int i;
  579. size_t printed = 0;
  580. struct dso *kdso = machine__kernel_map(machine)->dso;
  581. if (kdso->has_build_id) {
  582. char filename[PATH_MAX];
  583. if (dso__build_id_filename(kdso, filename, sizeof(filename),
  584. false))
  585. printed += fprintf(fp, "[0] %s\n", filename);
  586. }
  587. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  588. printed += fprintf(fp, "[%d] %s\n",
  589. i + kdso->has_build_id, vmlinux_path[i]);
  590. return printed;
  591. }
  592. size_t machine__fprintf(struct machine *machine, FILE *fp)
  593. {
  594. size_t ret;
  595. struct rb_node *nd;
  596. pthread_rwlock_rdlock(&machine->threads_lock);
  597. ret = fprintf(fp, "Threads: %u\n", machine->nr_threads);
  598. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  599. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  600. ret += thread__fprintf(pos, fp);
  601. }
  602. pthread_rwlock_unlock(&machine->threads_lock);
  603. return ret;
  604. }
  605. static struct dso *machine__get_kernel(struct machine *machine)
  606. {
  607. const char *vmlinux_name = NULL;
  608. struct dso *kernel;
  609. if (machine__is_host(machine)) {
  610. vmlinux_name = symbol_conf.vmlinux_name;
  611. if (!vmlinux_name)
  612. vmlinux_name = DSO__NAME_KALLSYMS;
  613. kernel = machine__findnew_kernel(machine, vmlinux_name,
  614. "[kernel]", DSO_TYPE_KERNEL);
  615. } else {
  616. char bf[PATH_MAX];
  617. if (machine__is_default_guest(machine))
  618. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  619. if (!vmlinux_name)
  620. vmlinux_name = machine__mmap_name(machine, bf,
  621. sizeof(bf));
  622. kernel = machine__findnew_kernel(machine, vmlinux_name,
  623. "[guest.kernel]",
  624. DSO_TYPE_GUEST_KERNEL);
  625. }
  626. if (kernel != NULL && (!kernel->has_build_id))
  627. dso__read_running_kernel_build_id(kernel, machine);
  628. return kernel;
  629. }
  630. struct process_args {
  631. u64 start;
  632. };
  633. static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
  634. size_t bufsz)
  635. {
  636. if (machine__is_default_guest(machine))
  637. scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
  638. else
  639. scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
  640. }
  641. const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
  642. /* Figure out the start address of kernel map from /proc/kallsyms.
  643. * Returns the name of the start symbol in *symbol_name. Pass in NULL as
  644. * symbol_name if it's not that important.
  645. */
  646. static int machine__get_running_kernel_start(struct machine *machine,
  647. const char **symbol_name, u64 *start)
  648. {
  649. char filename[PATH_MAX];
  650. int i, err = -1;
  651. const char *name;
  652. u64 addr = 0;
  653. machine__get_kallsyms_filename(machine, filename, PATH_MAX);
  654. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  655. return 0;
  656. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  657. err = kallsyms__get_function_start(filename, name, &addr);
  658. if (!err)
  659. break;
  660. }
  661. if (err)
  662. return -1;
  663. if (symbol_name)
  664. *symbol_name = name;
  665. *start = addr;
  666. return 0;
  667. }
  668. /* Kernel-space maps for symbols that are outside the main kernel map and module maps */
  669. struct extra_kernel_map {
  670. u64 start;
  671. u64 end;
  672. u64 pgoff;
  673. };
  674. static int machine__create_extra_kernel_map(struct machine *machine,
  675. struct dso *kernel,
  676. struct extra_kernel_map *xm)
  677. {
  678. struct kmap *kmap;
  679. struct map *map;
  680. map = map__new2(xm->start, kernel, MAP__FUNCTION);
  681. if (!map)
  682. return -1;
  683. map->end = xm->end;
  684. map->pgoff = xm->pgoff;
  685. kmap = map__kmap(map);
  686. kmap->kmaps = &machine->kmaps;
  687. map_groups__insert(&machine->kmaps, map);
  688. pr_debug2("Added extra kernel map %" PRIx64 "-%" PRIx64 "\n",
  689. map->start, map->end);
  690. map__put(map);
  691. return 0;
  692. }
  693. static u64 find_entry_trampoline(struct dso *dso)
  694. {
  695. /* Duplicates are removed so lookup all aliases */
  696. const char *syms[] = {
  697. "_entry_trampoline",
  698. "__entry_trampoline_start",
  699. "entry_SYSCALL_64_trampoline",
  700. };
  701. struct symbol *sym = dso__first_symbol(dso, MAP__FUNCTION);
  702. unsigned int i;
  703. for (; sym; sym = dso__next_symbol(sym)) {
  704. if (sym->binding != STB_GLOBAL)
  705. continue;
  706. for (i = 0; i < ARRAY_SIZE(syms); i++) {
  707. if (!strcmp(sym->name, syms[i]))
  708. return sym->start;
  709. }
  710. }
  711. return 0;
  712. }
  713. /*
  714. * These values can be used for kernels that do not have symbols for the entry
  715. * trampolines in kallsyms.
  716. */
  717. #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
  718. #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
  719. #define X86_64_ENTRY_TRAMPOLINE 0x6000
  720. /* Map x86_64 PTI entry trampolines */
  721. int machine__map_x86_64_entry_trampolines(struct machine *machine,
  722. struct dso *kernel)
  723. {
  724. u64 pgoff = find_entry_trampoline(kernel);
  725. int nr_cpus_avail, cpu;
  726. if (!pgoff)
  727. return 0;
  728. nr_cpus_avail = machine__nr_cpus_avail(machine);
  729. /* Add a 1 page map for each CPU's entry trampoline */
  730. for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
  731. u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
  732. cpu * X86_64_CPU_ENTRY_AREA_SIZE +
  733. X86_64_ENTRY_TRAMPOLINE;
  734. struct extra_kernel_map xm = {
  735. .start = va,
  736. .end = va + page_size,
  737. .pgoff = pgoff,
  738. };
  739. if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
  740. return -1;
  741. }
  742. return 0;
  743. }
  744. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  745. {
  746. int type;
  747. u64 start = 0;
  748. if (machine__get_running_kernel_start(machine, NULL, &start))
  749. return -1;
  750. /* In case of renewal the kernel map, destroy previous one */
  751. machine__destroy_kernel_maps(machine);
  752. for (type = 0; type < MAP__NR_TYPES; ++type) {
  753. struct kmap *kmap;
  754. struct map *map;
  755. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  756. if (machine->vmlinux_maps[type] == NULL)
  757. return -1;
  758. machine->vmlinux_maps[type]->map_ip =
  759. machine->vmlinux_maps[type]->unmap_ip =
  760. identity__map_ip;
  761. map = __machine__kernel_map(machine, type);
  762. kmap = map__kmap(map);
  763. if (!kmap)
  764. return -1;
  765. kmap->kmaps = &machine->kmaps;
  766. map_groups__insert(&machine->kmaps, map);
  767. }
  768. return 0;
  769. }
  770. void machine__destroy_kernel_maps(struct machine *machine)
  771. {
  772. int type;
  773. for (type = 0; type < MAP__NR_TYPES; ++type) {
  774. struct kmap *kmap;
  775. struct map *map = __machine__kernel_map(machine, type);
  776. if (map == NULL)
  777. continue;
  778. kmap = map__kmap(map);
  779. map_groups__remove(&machine->kmaps, map);
  780. if (kmap && kmap->ref_reloc_sym) {
  781. /*
  782. * ref_reloc_sym is shared among all maps, so free just
  783. * on one of them.
  784. */
  785. if (type == MAP__FUNCTION) {
  786. zfree((char **)&kmap->ref_reloc_sym->name);
  787. zfree(&kmap->ref_reloc_sym);
  788. } else
  789. kmap->ref_reloc_sym = NULL;
  790. }
  791. map__put(machine->vmlinux_maps[type]);
  792. machine->vmlinux_maps[type] = NULL;
  793. }
  794. }
  795. int machines__create_guest_kernel_maps(struct machines *machines)
  796. {
  797. int ret = 0;
  798. struct dirent **namelist = NULL;
  799. int i, items = 0;
  800. char path[PATH_MAX];
  801. pid_t pid;
  802. char *endp;
  803. if (symbol_conf.default_guest_vmlinux_name ||
  804. symbol_conf.default_guest_modules ||
  805. symbol_conf.default_guest_kallsyms) {
  806. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  807. }
  808. if (symbol_conf.guestmount) {
  809. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  810. if (items <= 0)
  811. return -ENOENT;
  812. for (i = 0; i < items; i++) {
  813. if (!isdigit(namelist[i]->d_name[0])) {
  814. /* Filter out . and .. */
  815. continue;
  816. }
  817. pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
  818. if ((*endp != '\0') ||
  819. (endp == namelist[i]->d_name) ||
  820. (errno == ERANGE)) {
  821. pr_debug("invalid directory (%s). Skipping.\n",
  822. namelist[i]->d_name);
  823. continue;
  824. }
  825. sprintf(path, "%s/%s/proc/kallsyms",
  826. symbol_conf.guestmount,
  827. namelist[i]->d_name);
  828. ret = access(path, R_OK);
  829. if (ret) {
  830. pr_debug("Can't access file %s\n", path);
  831. goto failure;
  832. }
  833. machines__create_kernel_maps(machines, pid);
  834. }
  835. failure:
  836. free(namelist);
  837. }
  838. return ret;
  839. }
  840. void machines__destroy_kernel_maps(struct machines *machines)
  841. {
  842. struct rb_node *next = rb_first(&machines->guests);
  843. machine__destroy_kernel_maps(&machines->host);
  844. while (next) {
  845. struct machine *pos = rb_entry(next, struct machine, rb_node);
  846. next = rb_next(&pos->rb_node);
  847. rb_erase(&pos->rb_node, &machines->guests);
  848. machine__delete(pos);
  849. }
  850. }
  851. int machines__create_kernel_maps(struct machines *machines, pid_t pid)
  852. {
  853. struct machine *machine = machines__findnew(machines, pid);
  854. if (machine == NULL)
  855. return -1;
  856. return machine__create_kernel_maps(machine);
  857. }
  858. int __machine__load_kallsyms(struct machine *machine, const char *filename,
  859. enum map_type type, bool no_kcore)
  860. {
  861. struct map *map = machine__kernel_map(machine);
  862. int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore);
  863. if (ret > 0) {
  864. dso__set_loaded(map->dso, type);
  865. /*
  866. * Since /proc/kallsyms will have multiple sessions for the
  867. * kernel, with modules between them, fixup the end of all
  868. * sections.
  869. */
  870. __map_groups__fixup_end(&machine->kmaps, type);
  871. }
  872. return ret;
  873. }
  874. int machine__load_kallsyms(struct machine *machine, const char *filename,
  875. enum map_type type)
  876. {
  877. return __machine__load_kallsyms(machine, filename, type, false);
  878. }
  879. int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
  880. {
  881. struct map *map = machine__kernel_map(machine);
  882. int ret = dso__load_vmlinux_path(map->dso, map);
  883. if (ret > 0)
  884. dso__set_loaded(map->dso, type);
  885. return ret;
  886. }
  887. static void map_groups__fixup_end(struct map_groups *mg)
  888. {
  889. int i;
  890. for (i = 0; i < MAP__NR_TYPES; ++i)
  891. __map_groups__fixup_end(mg, i);
  892. }
  893. static char *get_kernel_version(const char *root_dir)
  894. {
  895. char version[PATH_MAX];
  896. FILE *file;
  897. char *name, *tmp;
  898. const char *prefix = "Linux version ";
  899. sprintf(version, "%s/proc/version", root_dir);
  900. file = fopen(version, "r");
  901. if (!file)
  902. return NULL;
  903. version[0] = '\0';
  904. tmp = fgets(version, sizeof(version), file);
  905. fclose(file);
  906. name = strstr(version, prefix);
  907. if (!name)
  908. return NULL;
  909. name += strlen(prefix);
  910. tmp = strchr(name, ' ');
  911. if (tmp)
  912. *tmp = '\0';
  913. return strdup(name);
  914. }
  915. static bool is_kmod_dso(struct dso *dso)
  916. {
  917. return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  918. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
  919. }
  920. static int map_groups__set_module_path(struct map_groups *mg, const char *path,
  921. struct kmod_path *m)
  922. {
  923. struct map *map;
  924. char *long_name;
  925. map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
  926. if (map == NULL)
  927. return 0;
  928. long_name = strdup(path);
  929. if (long_name == NULL)
  930. return -ENOMEM;
  931. dso__set_long_name(map->dso, long_name, true);
  932. dso__kernel_module_get_build_id(map->dso, "");
  933. /*
  934. * Full name could reveal us kmod compression, so
  935. * we need to update the symtab_type if needed.
  936. */
  937. if (m->comp && is_kmod_dso(map->dso))
  938. map->dso->symtab_type++;
  939. return 0;
  940. }
  941. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  942. const char *dir_name, int depth)
  943. {
  944. struct dirent *dent;
  945. DIR *dir = opendir(dir_name);
  946. int ret = 0;
  947. if (!dir) {
  948. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  949. return -1;
  950. }
  951. while ((dent = readdir(dir)) != NULL) {
  952. char path[PATH_MAX];
  953. struct stat st;
  954. /*sshfs might return bad dent->d_type, so we have to stat*/
  955. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  956. if (stat(path, &st))
  957. continue;
  958. if (S_ISDIR(st.st_mode)) {
  959. if (!strcmp(dent->d_name, ".") ||
  960. !strcmp(dent->d_name, ".."))
  961. continue;
  962. /* Do not follow top-level source and build symlinks */
  963. if (depth == 0) {
  964. if (!strcmp(dent->d_name, "source") ||
  965. !strcmp(dent->d_name, "build"))
  966. continue;
  967. }
  968. ret = map_groups__set_modules_path_dir(mg, path,
  969. depth + 1);
  970. if (ret < 0)
  971. goto out;
  972. } else {
  973. struct kmod_path m;
  974. ret = kmod_path__parse_name(&m, dent->d_name);
  975. if (ret)
  976. goto out;
  977. if (m.kmod)
  978. ret = map_groups__set_module_path(mg, path, &m);
  979. free(m.name);
  980. if (ret)
  981. goto out;
  982. }
  983. }
  984. out:
  985. closedir(dir);
  986. return ret;
  987. }
  988. static int machine__set_modules_path(struct machine *machine)
  989. {
  990. char *version;
  991. char modules_path[PATH_MAX];
  992. version = get_kernel_version(machine->root_dir);
  993. if (!version)
  994. return -1;
  995. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
  996. machine->root_dir, version);
  997. free(version);
  998. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
  999. }
  1000. int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
  1001. u64 *size __maybe_unused,
  1002. const char *name __maybe_unused)
  1003. {
  1004. return 0;
  1005. }
  1006. static int machine__create_module(void *arg, const char *name, u64 start,
  1007. u64 size)
  1008. {
  1009. struct machine *machine = arg;
  1010. struct map *map;
  1011. if (arch__fix_module_text_start(&start, &size, name) < 0)
  1012. return -1;
  1013. map = machine__findnew_module_map(machine, start, name);
  1014. if (map == NULL)
  1015. return -1;
  1016. map->end = start + size;
  1017. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1018. return 0;
  1019. }
  1020. static int machine__create_modules(struct machine *machine)
  1021. {
  1022. const char *modules;
  1023. char path[PATH_MAX];
  1024. if (machine__is_default_guest(machine)) {
  1025. modules = symbol_conf.default_guest_modules;
  1026. } else {
  1027. snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
  1028. modules = path;
  1029. }
  1030. if (symbol__restricted_filename(modules, "/proc/modules"))
  1031. return -1;
  1032. if (modules__parse(modules, machine, machine__create_module))
  1033. return -1;
  1034. if (!machine__set_modules_path(machine))
  1035. return 0;
  1036. pr_debug("Problems setting modules path maps, continuing anyway...\n");
  1037. return 0;
  1038. }
  1039. int machine__create_kernel_maps(struct machine *machine)
  1040. {
  1041. struct dso *kernel = machine__get_kernel(machine);
  1042. const char *name = NULL;
  1043. u64 addr = 0;
  1044. int ret;
  1045. if (kernel == NULL)
  1046. return -1;
  1047. ret = __machine__create_kernel_maps(machine, kernel);
  1048. dso__put(kernel);
  1049. if (ret < 0)
  1050. return -1;
  1051. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  1052. if (machine__is_host(machine))
  1053. pr_debug("Problems creating module maps, "
  1054. "continuing anyway...\n");
  1055. else
  1056. pr_debug("Problems creating module maps for guest %d, "
  1057. "continuing anyway...\n", machine->pid);
  1058. }
  1059. /*
  1060. * Now that we have all the maps created, just set the ->end of them:
  1061. */
  1062. map_groups__fixup_end(&machine->kmaps);
  1063. if (!machine__get_running_kernel_start(machine, &name, &addr)) {
  1064. if (name &&
  1065. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
  1066. machine__destroy_kernel_maps(machine);
  1067. return -1;
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. static void machine__set_kernel_mmap_len(struct machine *machine,
  1073. union perf_event *event)
  1074. {
  1075. int i;
  1076. for (i = 0; i < MAP__NR_TYPES; i++) {
  1077. machine->vmlinux_maps[i]->start = event->mmap.start;
  1078. machine->vmlinux_maps[i]->end = (event->mmap.start +
  1079. event->mmap.len);
  1080. /*
  1081. * Be a bit paranoid here, some perf.data file came with
  1082. * a zero sized synthesized MMAP event for the kernel.
  1083. */
  1084. if (machine->vmlinux_maps[i]->end == 0)
  1085. machine->vmlinux_maps[i]->end = ~0ULL;
  1086. }
  1087. }
  1088. static bool machine__uses_kcore(struct machine *machine)
  1089. {
  1090. struct dso *dso;
  1091. list_for_each_entry(dso, &machine->dsos.head, node) {
  1092. if (dso__is_kcore(dso))
  1093. return true;
  1094. }
  1095. return false;
  1096. }
  1097. static int machine__process_kernel_mmap_event(struct machine *machine,
  1098. union perf_event *event)
  1099. {
  1100. struct map *map;
  1101. char kmmap_prefix[PATH_MAX];
  1102. enum dso_kernel_type kernel_type;
  1103. bool is_kernel_mmap;
  1104. /* If we have maps from kcore then we do not need or want any others */
  1105. if (machine__uses_kcore(machine))
  1106. return 0;
  1107. machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
  1108. if (machine__is_host(machine))
  1109. kernel_type = DSO_TYPE_KERNEL;
  1110. else
  1111. kernel_type = DSO_TYPE_GUEST_KERNEL;
  1112. is_kernel_mmap = memcmp(event->mmap.filename,
  1113. kmmap_prefix,
  1114. strlen(kmmap_prefix) - 1) == 0;
  1115. if (event->mmap.filename[0] == '/' ||
  1116. (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
  1117. map = machine__findnew_module_map(machine, event->mmap.start,
  1118. event->mmap.filename);
  1119. if (map == NULL)
  1120. goto out_problem;
  1121. map->end = map->start + event->mmap.len;
  1122. } else if (is_kernel_mmap) {
  1123. const char *symbol_name = (event->mmap.filename +
  1124. strlen(kmmap_prefix));
  1125. /*
  1126. * Should be there already, from the build-id table in
  1127. * the header.
  1128. */
  1129. struct dso *kernel = NULL;
  1130. struct dso *dso;
  1131. pthread_rwlock_rdlock(&machine->dsos.lock);
  1132. list_for_each_entry(dso, &machine->dsos.head, node) {
  1133. /*
  1134. * The cpumode passed to is_kernel_module is not the
  1135. * cpumode of *this* event. If we insist on passing
  1136. * correct cpumode to is_kernel_module, we should
  1137. * record the cpumode when we adding this dso to the
  1138. * linked list.
  1139. *
  1140. * However we don't really need passing correct
  1141. * cpumode. We know the correct cpumode must be kernel
  1142. * mode (if not, we should not link it onto kernel_dsos
  1143. * list).
  1144. *
  1145. * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
  1146. * is_kernel_module() treats it as a kernel cpumode.
  1147. */
  1148. if (!dso->kernel ||
  1149. is_kernel_module(dso->long_name,
  1150. PERF_RECORD_MISC_CPUMODE_UNKNOWN))
  1151. continue;
  1152. kernel = dso;
  1153. break;
  1154. }
  1155. pthread_rwlock_unlock(&machine->dsos.lock);
  1156. if (kernel == NULL)
  1157. kernel = machine__findnew_dso(machine, kmmap_prefix);
  1158. if (kernel == NULL)
  1159. goto out_problem;
  1160. kernel->kernel = kernel_type;
  1161. if (__machine__create_kernel_maps(machine, kernel) < 0) {
  1162. dso__put(kernel);
  1163. goto out_problem;
  1164. }
  1165. if (strstr(kernel->long_name, "vmlinux"))
  1166. dso__set_short_name(kernel, "[kernel.vmlinux]", false);
  1167. machine__set_kernel_mmap_len(machine, event);
  1168. /*
  1169. * Avoid using a zero address (kptr_restrict) for the ref reloc
  1170. * symbol. Effectively having zero here means that at record
  1171. * time /proc/sys/kernel/kptr_restrict was non zero.
  1172. */
  1173. if (event->mmap.pgoff != 0) {
  1174. maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
  1175. symbol_name,
  1176. event->mmap.pgoff);
  1177. }
  1178. if (machine__is_default_guest(machine)) {
  1179. /*
  1180. * preload dso of guest kernel and modules
  1181. */
  1182. dso__load(kernel, machine__kernel_map(machine));
  1183. }
  1184. }
  1185. return 0;
  1186. out_problem:
  1187. return -1;
  1188. }
  1189. int machine__process_mmap2_event(struct machine *machine,
  1190. union perf_event *event,
  1191. struct perf_sample *sample)
  1192. {
  1193. struct thread *thread;
  1194. struct map *map;
  1195. enum map_type type;
  1196. int ret = 0;
  1197. if (dump_trace)
  1198. perf_event__fprintf_mmap2(event, stdout);
  1199. if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  1200. sample->cpumode == PERF_RECORD_MISC_KERNEL) {
  1201. ret = machine__process_kernel_mmap_event(machine, event);
  1202. if (ret < 0)
  1203. goto out_problem;
  1204. return 0;
  1205. }
  1206. thread = machine__findnew_thread(machine, event->mmap2.pid,
  1207. event->mmap2.tid);
  1208. if (thread == NULL)
  1209. goto out_problem;
  1210. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  1211. type = MAP__VARIABLE;
  1212. else
  1213. type = MAP__FUNCTION;
  1214. map = map__new(machine, event->mmap2.start,
  1215. event->mmap2.len, event->mmap2.pgoff,
  1216. event->mmap2.maj,
  1217. event->mmap2.min, event->mmap2.ino,
  1218. event->mmap2.ino_generation,
  1219. event->mmap2.prot,
  1220. event->mmap2.flags,
  1221. event->mmap2.filename, type, thread);
  1222. if (map == NULL)
  1223. goto out_problem_map;
  1224. ret = thread__insert_map(thread, map);
  1225. if (ret)
  1226. goto out_problem_insert;
  1227. thread__put(thread);
  1228. map__put(map);
  1229. return 0;
  1230. out_problem_insert:
  1231. map__put(map);
  1232. out_problem_map:
  1233. thread__put(thread);
  1234. out_problem:
  1235. dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
  1236. return 0;
  1237. }
  1238. int machine__process_mmap_event(struct machine *machine, union perf_event *event,
  1239. struct perf_sample *sample)
  1240. {
  1241. struct thread *thread;
  1242. struct map *map;
  1243. enum map_type type;
  1244. int ret = 0;
  1245. if (dump_trace)
  1246. perf_event__fprintf_mmap(event, stdout);
  1247. if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
  1248. sample->cpumode == PERF_RECORD_MISC_KERNEL) {
  1249. ret = machine__process_kernel_mmap_event(machine, event);
  1250. if (ret < 0)
  1251. goto out_problem;
  1252. return 0;
  1253. }
  1254. thread = machine__findnew_thread(machine, event->mmap.pid,
  1255. event->mmap.tid);
  1256. if (thread == NULL)
  1257. goto out_problem;
  1258. if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
  1259. type = MAP__VARIABLE;
  1260. else
  1261. type = MAP__FUNCTION;
  1262. map = map__new(machine, event->mmap.start,
  1263. event->mmap.len, event->mmap.pgoff,
  1264. 0, 0, 0, 0, 0, 0,
  1265. event->mmap.filename,
  1266. type, thread);
  1267. if (map == NULL)
  1268. goto out_problem_map;
  1269. ret = thread__insert_map(thread, map);
  1270. if (ret)
  1271. goto out_problem_insert;
  1272. thread__put(thread);
  1273. map__put(map);
  1274. return 0;
  1275. out_problem_insert:
  1276. map__put(map);
  1277. out_problem_map:
  1278. thread__put(thread);
  1279. out_problem:
  1280. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  1281. return 0;
  1282. }
  1283. static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
  1284. {
  1285. if (machine->last_match == th)
  1286. machine->last_match = NULL;
  1287. BUG_ON(refcount_read(&th->refcnt) == 0);
  1288. if (lock)
  1289. pthread_rwlock_wrlock(&machine->threads_lock);
  1290. rb_erase_init(&th->rb_node, &machine->threads);
  1291. RB_CLEAR_NODE(&th->rb_node);
  1292. --machine->nr_threads;
  1293. /*
  1294. * Move it first to the dead_threads list, then drop the reference,
  1295. * if this is the last reference, then the thread__delete destructor
  1296. * will be called and we will remove it from the dead_threads list.
  1297. */
  1298. list_add_tail(&th->node, &machine->dead_threads);
  1299. if (lock)
  1300. pthread_rwlock_unlock(&machine->threads_lock);
  1301. thread__put(th);
  1302. }
  1303. void machine__remove_thread(struct machine *machine, struct thread *th)
  1304. {
  1305. return __machine__remove_thread(machine, th, true);
  1306. }
  1307. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  1308. struct perf_sample *sample)
  1309. {
  1310. struct thread *thread = machine__find_thread(machine,
  1311. event->fork.pid,
  1312. event->fork.tid);
  1313. struct thread *parent = machine__findnew_thread(machine,
  1314. event->fork.ppid,
  1315. event->fork.ptid);
  1316. int err = 0;
  1317. if (dump_trace)
  1318. perf_event__fprintf_task(event, stdout);
  1319. /*
  1320. * There may be an existing thread that is not actually the parent,
  1321. * either because we are processing events out of order, or because the
  1322. * (fork) event that would have removed the thread was lost. Assume the
  1323. * latter case and continue on as best we can.
  1324. */
  1325. if (parent->pid_ != (pid_t)event->fork.ppid) {
  1326. dump_printf("removing erroneous parent thread %d/%d\n",
  1327. parent->pid_, parent->tid);
  1328. machine__remove_thread(machine, parent);
  1329. thread__put(parent);
  1330. parent = machine__findnew_thread(machine, event->fork.ppid,
  1331. event->fork.ptid);
  1332. }
  1333. /* if a thread currently exists for the thread id remove it */
  1334. if (thread != NULL) {
  1335. machine__remove_thread(machine, thread);
  1336. thread__put(thread);
  1337. }
  1338. thread = machine__findnew_thread(machine, event->fork.pid,
  1339. event->fork.tid);
  1340. if (thread == NULL || parent == NULL ||
  1341. thread__fork(thread, parent, sample->time) < 0) {
  1342. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  1343. err = -1;
  1344. }
  1345. thread__put(thread);
  1346. thread__put(parent);
  1347. return err;
  1348. }
  1349. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  1350. struct perf_sample *sample __maybe_unused)
  1351. {
  1352. struct thread *thread = machine__find_thread(machine,
  1353. event->fork.pid,
  1354. event->fork.tid);
  1355. if (dump_trace)
  1356. perf_event__fprintf_task(event, stdout);
  1357. if (thread != NULL) {
  1358. thread__exited(thread);
  1359. thread__put(thread);
  1360. }
  1361. return 0;
  1362. }
  1363. int machine__process_event(struct machine *machine, union perf_event *event,
  1364. struct perf_sample *sample)
  1365. {
  1366. int ret;
  1367. switch (event->header.type) {
  1368. case PERF_RECORD_COMM:
  1369. ret = machine__process_comm_event(machine, event, sample); break;
  1370. case PERF_RECORD_MMAP:
  1371. ret = machine__process_mmap_event(machine, event, sample); break;
  1372. case PERF_RECORD_NAMESPACES:
  1373. ret = machine__process_namespaces_event(machine, event, sample); break;
  1374. case PERF_RECORD_MMAP2:
  1375. ret = machine__process_mmap2_event(machine, event, sample); break;
  1376. case PERF_RECORD_FORK:
  1377. ret = machine__process_fork_event(machine, event, sample); break;
  1378. case PERF_RECORD_EXIT:
  1379. ret = machine__process_exit_event(machine, event, sample); break;
  1380. case PERF_RECORD_LOST:
  1381. ret = machine__process_lost_event(machine, event, sample); break;
  1382. case PERF_RECORD_AUX:
  1383. ret = machine__process_aux_event(machine, event); break;
  1384. case PERF_RECORD_ITRACE_START:
  1385. ret = machine__process_itrace_start_event(machine, event); break;
  1386. case PERF_RECORD_LOST_SAMPLES:
  1387. ret = machine__process_lost_samples_event(machine, event, sample); break;
  1388. case PERF_RECORD_SWITCH:
  1389. case PERF_RECORD_SWITCH_CPU_WIDE:
  1390. ret = machine__process_switch_event(machine, event); break;
  1391. default:
  1392. ret = -1;
  1393. break;
  1394. }
  1395. return ret;
  1396. }
  1397. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  1398. {
  1399. if (!regexec(regex, sym->name, 0, NULL, 0))
  1400. return 1;
  1401. return 0;
  1402. }
  1403. static void ip__resolve_ams(struct thread *thread,
  1404. struct addr_map_symbol *ams,
  1405. u64 ip)
  1406. {
  1407. struct addr_location al;
  1408. memset(&al, 0, sizeof(al));
  1409. /*
  1410. * We cannot use the header.misc hint to determine whether a
  1411. * branch stack address is user, kernel, guest, hypervisor.
  1412. * Branches may straddle the kernel/user/hypervisor boundaries.
  1413. * Thus, we have to try consecutively until we find a match
  1414. * or else, the symbol is unknown
  1415. */
  1416. thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
  1417. ams->addr = ip;
  1418. ams->al_addr = al.addr;
  1419. ams->sym = al.sym;
  1420. ams->map = al.map;
  1421. ams->phys_addr = 0;
  1422. }
  1423. static void ip__resolve_data(struct thread *thread,
  1424. u8 m, struct addr_map_symbol *ams,
  1425. u64 addr, u64 phys_addr)
  1426. {
  1427. struct addr_location al;
  1428. memset(&al, 0, sizeof(al));
  1429. thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
  1430. if (al.map == NULL) {
  1431. /*
  1432. * some shared data regions have execute bit set which puts
  1433. * their mapping in the MAP__FUNCTION type array.
  1434. * Check there as a fallback option before dropping the sample.
  1435. */
  1436. thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
  1437. }
  1438. ams->addr = addr;
  1439. ams->al_addr = al.addr;
  1440. ams->sym = al.sym;
  1441. ams->map = al.map;
  1442. ams->phys_addr = phys_addr;
  1443. }
  1444. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  1445. struct addr_location *al)
  1446. {
  1447. struct mem_info *mi = zalloc(sizeof(*mi));
  1448. if (!mi)
  1449. return NULL;
  1450. ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
  1451. ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
  1452. sample->addr, sample->phys_addr);
  1453. mi->data_src.val = sample->data_src;
  1454. return mi;
  1455. }
  1456. struct iterations {
  1457. int nr_loop_iter;
  1458. u64 cycles;
  1459. };
  1460. static int add_callchain_ip(struct thread *thread,
  1461. struct callchain_cursor *cursor,
  1462. struct symbol **parent,
  1463. struct addr_location *root_al,
  1464. u8 *cpumode,
  1465. u64 ip,
  1466. bool branch,
  1467. struct branch_flags *flags,
  1468. struct iterations *iter,
  1469. u64 branch_from)
  1470. {
  1471. struct addr_location al;
  1472. int nr_loop_iter = 0;
  1473. u64 iter_cycles = 0;
  1474. al.filtered = 0;
  1475. al.sym = NULL;
  1476. if (!cpumode) {
  1477. thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
  1478. ip, &al);
  1479. } else {
  1480. if (ip >= PERF_CONTEXT_MAX) {
  1481. switch (ip) {
  1482. case PERF_CONTEXT_HV:
  1483. *cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1484. break;
  1485. case PERF_CONTEXT_KERNEL:
  1486. *cpumode = PERF_RECORD_MISC_KERNEL;
  1487. break;
  1488. case PERF_CONTEXT_USER:
  1489. *cpumode = PERF_RECORD_MISC_USER;
  1490. break;
  1491. default:
  1492. pr_debug("invalid callchain context: "
  1493. "%"PRId64"\n", (s64) ip);
  1494. /*
  1495. * It seems the callchain is corrupted.
  1496. * Discard all.
  1497. */
  1498. callchain_cursor_reset(cursor);
  1499. return 1;
  1500. }
  1501. return 0;
  1502. }
  1503. thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
  1504. ip, &al);
  1505. }
  1506. if (al.sym != NULL) {
  1507. if (perf_hpp_list.parent && !*parent &&
  1508. symbol__match_regex(al.sym, &parent_regex))
  1509. *parent = al.sym;
  1510. else if (have_ignore_callees && root_al &&
  1511. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1512. /* Treat this symbol as the root,
  1513. forgetting its callees. */
  1514. *root_al = al;
  1515. callchain_cursor_reset(cursor);
  1516. }
  1517. }
  1518. if (symbol_conf.hide_unresolved && al.sym == NULL)
  1519. return 0;
  1520. if (iter) {
  1521. nr_loop_iter = iter->nr_loop_iter;
  1522. iter_cycles = iter->cycles;
  1523. }
  1524. return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
  1525. branch, flags, nr_loop_iter,
  1526. iter_cycles, branch_from);
  1527. }
  1528. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  1529. struct addr_location *al)
  1530. {
  1531. unsigned int i;
  1532. const struct branch_stack *bs = sample->branch_stack;
  1533. struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
  1534. if (!bi)
  1535. return NULL;
  1536. for (i = 0; i < bs->nr; i++) {
  1537. ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
  1538. ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
  1539. bi[i].flags = bs->entries[i].flags;
  1540. }
  1541. return bi;
  1542. }
  1543. static void save_iterations(struct iterations *iter,
  1544. struct branch_entry *be, int nr)
  1545. {
  1546. int i;
  1547. iter->nr_loop_iter = nr;
  1548. iter->cycles = 0;
  1549. for (i = 0; i < nr; i++)
  1550. iter->cycles += be[i].flags.cycles;
  1551. }
  1552. #define CHASHSZ 127
  1553. #define CHASHBITS 7
  1554. #define NO_ENTRY 0xff
  1555. #define PERF_MAX_BRANCH_DEPTH 127
  1556. /* Remove loops. */
  1557. static int remove_loops(struct branch_entry *l, int nr,
  1558. struct iterations *iter)
  1559. {
  1560. int i, j, off;
  1561. unsigned char chash[CHASHSZ];
  1562. memset(chash, NO_ENTRY, sizeof(chash));
  1563. BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
  1564. for (i = 0; i < nr; i++) {
  1565. int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
  1566. /* no collision handling for now */
  1567. if (chash[h] == NO_ENTRY) {
  1568. chash[h] = i;
  1569. } else if (l[chash[h]].from == l[i].from) {
  1570. bool is_loop = true;
  1571. /* check if it is a real loop */
  1572. off = 0;
  1573. for (j = chash[h]; j < i && i + off < nr; j++, off++)
  1574. if (l[j].from != l[i + off].from) {
  1575. is_loop = false;
  1576. break;
  1577. }
  1578. if (is_loop) {
  1579. j = nr - (i + off);
  1580. if (j > 0) {
  1581. save_iterations(iter + i + off,
  1582. l + i, off);
  1583. memmove(iter + i, iter + i + off,
  1584. j * sizeof(*iter));
  1585. memmove(l + i, l + i + off,
  1586. j * sizeof(*l));
  1587. }
  1588. nr -= off;
  1589. }
  1590. }
  1591. }
  1592. return nr;
  1593. }
  1594. /*
  1595. * Recolve LBR callstack chain sample
  1596. * Return:
  1597. * 1 on success get LBR callchain information
  1598. * 0 no available LBR callchain information, should try fp
  1599. * negative error code on other errors.
  1600. */
  1601. static int resolve_lbr_callchain_sample(struct thread *thread,
  1602. struct callchain_cursor *cursor,
  1603. struct perf_sample *sample,
  1604. struct symbol **parent,
  1605. struct addr_location *root_al,
  1606. int max_stack)
  1607. {
  1608. struct ip_callchain *chain = sample->callchain;
  1609. int chain_nr = min(max_stack, (int)chain->nr), i;
  1610. u8 cpumode = PERF_RECORD_MISC_USER;
  1611. u64 ip, branch_from = 0;
  1612. for (i = 0; i < chain_nr; i++) {
  1613. if (chain->ips[i] == PERF_CONTEXT_USER)
  1614. break;
  1615. }
  1616. /* LBR only affects the user callchain */
  1617. if (i != chain_nr) {
  1618. struct branch_stack *lbr_stack = sample->branch_stack;
  1619. int lbr_nr = lbr_stack->nr, j, k;
  1620. bool branch;
  1621. struct branch_flags *flags;
  1622. /*
  1623. * LBR callstack can only get user call chain.
  1624. * The mix_chain_nr is kernel call chain
  1625. * number plus LBR user call chain number.
  1626. * i is kernel call chain number,
  1627. * 1 is PERF_CONTEXT_USER,
  1628. * lbr_nr + 1 is the user call chain number.
  1629. * For details, please refer to the comments
  1630. * in callchain__printf
  1631. */
  1632. int mix_chain_nr = i + 1 + lbr_nr + 1;
  1633. for (j = 0; j < mix_chain_nr; j++) {
  1634. int err;
  1635. branch = false;
  1636. flags = NULL;
  1637. if (callchain_param.order == ORDER_CALLEE) {
  1638. if (j < i + 1)
  1639. ip = chain->ips[j];
  1640. else if (j > i + 1) {
  1641. k = j - i - 2;
  1642. ip = lbr_stack->entries[k].from;
  1643. branch = true;
  1644. flags = &lbr_stack->entries[k].flags;
  1645. } else {
  1646. ip = lbr_stack->entries[0].to;
  1647. branch = true;
  1648. flags = &lbr_stack->entries[0].flags;
  1649. branch_from =
  1650. lbr_stack->entries[0].from;
  1651. }
  1652. } else {
  1653. if (j < lbr_nr) {
  1654. k = lbr_nr - j - 1;
  1655. ip = lbr_stack->entries[k].from;
  1656. branch = true;
  1657. flags = &lbr_stack->entries[k].flags;
  1658. }
  1659. else if (j > lbr_nr)
  1660. ip = chain->ips[i + 1 - (j - lbr_nr)];
  1661. else {
  1662. ip = lbr_stack->entries[0].to;
  1663. branch = true;
  1664. flags = &lbr_stack->entries[0].flags;
  1665. branch_from =
  1666. lbr_stack->entries[0].from;
  1667. }
  1668. }
  1669. err = add_callchain_ip(thread, cursor, parent,
  1670. root_al, &cpumode, ip,
  1671. branch, flags, NULL,
  1672. branch_from);
  1673. if (err)
  1674. return (err < 0) ? err : 0;
  1675. }
  1676. return 1;
  1677. }
  1678. return 0;
  1679. }
  1680. static int thread__resolve_callchain_sample(struct thread *thread,
  1681. struct callchain_cursor *cursor,
  1682. struct perf_evsel *evsel,
  1683. struct perf_sample *sample,
  1684. struct symbol **parent,
  1685. struct addr_location *root_al,
  1686. int max_stack)
  1687. {
  1688. struct branch_stack *branch = sample->branch_stack;
  1689. struct ip_callchain *chain = sample->callchain;
  1690. int chain_nr = 0;
  1691. u8 cpumode = PERF_RECORD_MISC_USER;
  1692. int i, j, err, nr_entries;
  1693. int skip_idx = -1;
  1694. int first_call = 0;
  1695. if (chain)
  1696. chain_nr = chain->nr;
  1697. if (perf_evsel__has_branch_callstack(evsel)) {
  1698. err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
  1699. root_al, max_stack);
  1700. if (err)
  1701. return (err < 0) ? err : 0;
  1702. }
  1703. /*
  1704. * Based on DWARF debug information, some architectures skip
  1705. * a callchain entry saved by the kernel.
  1706. */
  1707. skip_idx = arch_skip_callchain_idx(thread, chain);
  1708. /*
  1709. * Add branches to call stack for easier browsing. This gives
  1710. * more context for a sample than just the callers.
  1711. *
  1712. * This uses individual histograms of paths compared to the
  1713. * aggregated histograms the normal LBR mode uses.
  1714. *
  1715. * Limitations for now:
  1716. * - No extra filters
  1717. * - No annotations (should annotate somehow)
  1718. */
  1719. if (branch && callchain_param.branch_callstack) {
  1720. int nr = min(max_stack, (int)branch->nr);
  1721. struct branch_entry be[nr];
  1722. struct iterations iter[nr];
  1723. if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
  1724. pr_warning("corrupted branch chain. skipping...\n");
  1725. goto check_calls;
  1726. }
  1727. for (i = 0; i < nr; i++) {
  1728. if (callchain_param.order == ORDER_CALLEE) {
  1729. be[i] = branch->entries[i];
  1730. if (chain == NULL)
  1731. continue;
  1732. /*
  1733. * Check for overlap into the callchain.
  1734. * The return address is one off compared to
  1735. * the branch entry. To adjust for this
  1736. * assume the calling instruction is not longer
  1737. * than 8 bytes.
  1738. */
  1739. if (i == skip_idx ||
  1740. chain->ips[first_call] >= PERF_CONTEXT_MAX)
  1741. first_call++;
  1742. else if (be[i].from < chain->ips[first_call] &&
  1743. be[i].from >= chain->ips[first_call] - 8)
  1744. first_call++;
  1745. } else
  1746. be[i] = branch->entries[branch->nr - i - 1];
  1747. }
  1748. memset(iter, 0, sizeof(struct iterations) * nr);
  1749. nr = remove_loops(be, nr, iter);
  1750. for (i = 0; i < nr; i++) {
  1751. err = add_callchain_ip(thread, cursor, parent,
  1752. root_al,
  1753. NULL, be[i].to,
  1754. true, &be[i].flags,
  1755. NULL, be[i].from);
  1756. if (!err)
  1757. err = add_callchain_ip(thread, cursor, parent, root_al,
  1758. NULL, be[i].from,
  1759. true, &be[i].flags,
  1760. &iter[i], 0);
  1761. if (err == -EINVAL)
  1762. break;
  1763. if (err)
  1764. return err;
  1765. }
  1766. if (chain_nr == 0)
  1767. return 0;
  1768. chain_nr -= nr;
  1769. }
  1770. check_calls:
  1771. for (i = first_call, nr_entries = 0;
  1772. i < chain_nr && nr_entries < max_stack; i++) {
  1773. u64 ip;
  1774. if (callchain_param.order == ORDER_CALLEE)
  1775. j = i;
  1776. else
  1777. j = chain->nr - i - 1;
  1778. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  1779. if (j == skip_idx)
  1780. continue;
  1781. #endif
  1782. ip = chain->ips[j];
  1783. if (ip < PERF_CONTEXT_MAX)
  1784. ++nr_entries;
  1785. err = add_callchain_ip(thread, cursor, parent,
  1786. root_al, &cpumode, ip,
  1787. false, NULL, NULL, 0);
  1788. if (err)
  1789. return (err < 0) ? err : 0;
  1790. }
  1791. return 0;
  1792. }
  1793. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1794. {
  1795. struct callchain_cursor *cursor = arg;
  1796. if (symbol_conf.hide_unresolved && entry->sym == NULL)
  1797. return 0;
  1798. return callchain_cursor_append(cursor, entry->ip,
  1799. entry->map, entry->sym,
  1800. false, NULL, 0, 0, 0);
  1801. }
  1802. static int thread__resolve_callchain_unwind(struct thread *thread,
  1803. struct callchain_cursor *cursor,
  1804. struct perf_evsel *evsel,
  1805. struct perf_sample *sample,
  1806. int max_stack)
  1807. {
  1808. /* Can we do dwarf post unwind? */
  1809. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1810. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1811. return 0;
  1812. /* Bail out if nothing was captured. */
  1813. if ((!sample->user_regs.regs) ||
  1814. (!sample->user_stack.size))
  1815. return 0;
  1816. return unwind__get_entries(unwind_entry, cursor,
  1817. thread, sample, max_stack);
  1818. }
  1819. int thread__resolve_callchain(struct thread *thread,
  1820. struct callchain_cursor *cursor,
  1821. struct perf_evsel *evsel,
  1822. struct perf_sample *sample,
  1823. struct symbol **parent,
  1824. struct addr_location *root_al,
  1825. int max_stack)
  1826. {
  1827. int ret = 0;
  1828. callchain_cursor_reset(&callchain_cursor);
  1829. if (callchain_param.order == ORDER_CALLEE) {
  1830. ret = thread__resolve_callchain_sample(thread, cursor,
  1831. evsel, sample,
  1832. parent, root_al,
  1833. max_stack);
  1834. if (ret)
  1835. return ret;
  1836. ret = thread__resolve_callchain_unwind(thread, cursor,
  1837. evsel, sample,
  1838. max_stack);
  1839. } else {
  1840. ret = thread__resolve_callchain_unwind(thread, cursor,
  1841. evsel, sample,
  1842. max_stack);
  1843. if (ret)
  1844. return ret;
  1845. ret = thread__resolve_callchain_sample(thread, cursor,
  1846. evsel, sample,
  1847. parent, root_al,
  1848. max_stack);
  1849. }
  1850. return ret;
  1851. }
  1852. int machine__for_each_thread(struct machine *machine,
  1853. int (*fn)(struct thread *thread, void *p),
  1854. void *priv)
  1855. {
  1856. struct rb_node *nd;
  1857. struct thread *thread;
  1858. int rc = 0;
  1859. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  1860. thread = rb_entry(nd, struct thread, rb_node);
  1861. rc = fn(thread, priv);
  1862. if (rc != 0)
  1863. return rc;
  1864. }
  1865. list_for_each_entry(thread, &machine->dead_threads, node) {
  1866. rc = fn(thread, priv);
  1867. if (rc != 0)
  1868. return rc;
  1869. }
  1870. return rc;
  1871. }
  1872. int machines__for_each_thread(struct machines *machines,
  1873. int (*fn)(struct thread *thread, void *p),
  1874. void *priv)
  1875. {
  1876. struct rb_node *nd;
  1877. int rc = 0;
  1878. rc = machine__for_each_thread(&machines->host, fn, priv);
  1879. if (rc != 0)
  1880. return rc;
  1881. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  1882. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  1883. rc = machine__for_each_thread(machine, fn, priv);
  1884. if (rc != 0)
  1885. return rc;
  1886. }
  1887. return rc;
  1888. }
  1889. int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
  1890. struct target *target, struct thread_map *threads,
  1891. perf_event__handler_t process, bool data_mmap,
  1892. unsigned int proc_map_timeout)
  1893. {
  1894. if (target__has_task(target))
  1895. return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
  1896. else if (target__has_cpu(target))
  1897. return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
  1898. /* command specified */
  1899. return 0;
  1900. }
  1901. pid_t machine__get_current_tid(struct machine *machine, int cpu)
  1902. {
  1903. if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
  1904. return -1;
  1905. return machine->current_tid[cpu];
  1906. }
  1907. int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
  1908. pid_t tid)
  1909. {
  1910. struct thread *thread;
  1911. if (cpu < 0)
  1912. return -EINVAL;
  1913. if (!machine->current_tid) {
  1914. int i;
  1915. machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
  1916. if (!machine->current_tid)
  1917. return -ENOMEM;
  1918. for (i = 0; i < MAX_NR_CPUS; i++)
  1919. machine->current_tid[i] = -1;
  1920. }
  1921. if (cpu >= MAX_NR_CPUS) {
  1922. pr_err("Requested CPU %d too large. ", cpu);
  1923. pr_err("Consider raising MAX_NR_CPUS\n");
  1924. return -EINVAL;
  1925. }
  1926. machine->current_tid[cpu] = tid;
  1927. thread = machine__findnew_thread(machine, pid, tid);
  1928. if (!thread)
  1929. return -ENOMEM;
  1930. thread->cpu = cpu;
  1931. thread__put(thread);
  1932. return 0;
  1933. }
  1934. /*
  1935. * Compares the raw arch string. N.B. see instead perf_env__arch() if a
  1936. * normalized arch is needed.
  1937. */
  1938. bool machine__is(struct machine *machine, const char *arch)
  1939. {
  1940. return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
  1941. }
  1942. int machine__nr_cpus_avail(struct machine *machine)
  1943. {
  1944. return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
  1945. }
  1946. int machine__get_kernel_start(struct machine *machine)
  1947. {
  1948. struct map *map = machine__kernel_map(machine);
  1949. int err = 0;
  1950. /*
  1951. * The only addresses above 2^63 are kernel addresses of a 64-bit
  1952. * kernel. Note that addresses are unsigned so that on a 32-bit system
  1953. * all addresses including kernel addresses are less than 2^32. In
  1954. * that case (32-bit system), if the kernel mapping is unknown, all
  1955. * addresses will be assumed to be in user space - see
  1956. * machine__kernel_ip().
  1957. */
  1958. machine->kernel_start = 1ULL << 63;
  1959. if (map) {
  1960. err = map__load(map);
  1961. /*
  1962. * On x86_64, PTI entry trampolines are less than the
  1963. * start of kernel text, but still above 2^63. So leave
  1964. * kernel_start = 1ULL << 63 for x86_64.
  1965. */
  1966. if (!err && !machine__is(machine, "x86_64"))
  1967. machine->kernel_start = map->start;
  1968. }
  1969. return err;
  1970. }
  1971. struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
  1972. {
  1973. return dsos__findnew(&machine->dsos, filename);
  1974. }
  1975. char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  1976. {
  1977. struct machine *machine = vmachine;
  1978. struct map *map;
  1979. struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
  1980. if (sym == NULL)
  1981. return NULL;
  1982. *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
  1983. *addrp = map->unmap_ip(map, sym->start);
  1984. return sym->name;
  1985. }