machine.c 61 KB

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