machine.c 49 KB

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