machine.c 50 KB

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