machine.c 49 KB

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