machine.c 46 KB

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