machine.c 55 KB

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