machine.c 43 KB

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