machine.c 56 KB

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