machine.c 47 KB

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