machine.c 51 KB

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