machine.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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 dsos__init(struct dsos *dsos)
  17. {
  18. INIT_LIST_HEAD(&dsos->head);
  19. dsos->root = RB_ROOT;
  20. }
  21. int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
  22. {
  23. map_groups__init(&machine->kmaps, machine);
  24. RB_CLEAR_NODE(&machine->rb_node);
  25. dsos__init(&machine->user_dsos);
  26. dsos__init(&machine->kernel_dsos);
  27. machine->threads = RB_ROOT;
  28. INIT_LIST_HEAD(&machine->dead_threads);
  29. machine->last_match = NULL;
  30. machine->vdso_info = NULL;
  31. machine->pid = pid;
  32. machine->symbol_filter = NULL;
  33. machine->id_hdr_size = 0;
  34. machine->comm_exec = false;
  35. machine->kernel_start = 0;
  36. machine->root_dir = strdup(root_dir);
  37. if (machine->root_dir == NULL)
  38. return -ENOMEM;
  39. if (pid != HOST_KERNEL_ID) {
  40. struct thread *thread = machine__findnew_thread(machine, -1,
  41. pid);
  42. char comm[64];
  43. if (thread == NULL)
  44. return -ENOMEM;
  45. snprintf(comm, sizeof(comm), "[guest/%d]", pid);
  46. thread__set_comm(thread, comm, 0);
  47. }
  48. machine->current_tid = NULL;
  49. return 0;
  50. }
  51. struct machine *machine__new_host(void)
  52. {
  53. struct machine *machine = malloc(sizeof(*machine));
  54. if (machine != NULL) {
  55. machine__init(machine, "", HOST_KERNEL_ID);
  56. if (machine__create_kernel_maps(machine) < 0)
  57. goto out_delete;
  58. }
  59. return machine;
  60. out_delete:
  61. free(machine);
  62. return NULL;
  63. }
  64. static void dsos__delete(struct dsos *dsos)
  65. {
  66. struct dso *pos, *n;
  67. list_for_each_entry_safe(pos, n, &dsos->head, node) {
  68. RB_CLEAR_NODE(&pos->rb_node);
  69. list_del(&pos->node);
  70. dso__delete(pos);
  71. }
  72. }
  73. void machine__delete_dead_threads(struct machine *machine)
  74. {
  75. struct thread *n, *t;
  76. list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
  77. list_del(&t->node);
  78. thread__delete(t);
  79. }
  80. }
  81. void machine__delete_threads(struct machine *machine)
  82. {
  83. struct rb_node *nd = rb_first(&machine->threads);
  84. while (nd) {
  85. struct thread *t = rb_entry(nd, struct thread, rb_node);
  86. rb_erase(&t->rb_node, &machine->threads);
  87. nd = rb_next(nd);
  88. thread__delete(t);
  89. }
  90. }
  91. void machine__exit(struct machine *machine)
  92. {
  93. map_groups__exit(&machine->kmaps);
  94. dsos__delete(&machine->user_dsos);
  95. dsos__delete(&machine->kernel_dsos);
  96. vdso__exit(machine);
  97. zfree(&machine->root_dir);
  98. zfree(&machine->current_tid);
  99. }
  100. void machine__delete(struct machine *machine)
  101. {
  102. machine__exit(machine);
  103. free(machine);
  104. }
  105. void machines__init(struct machines *machines)
  106. {
  107. machine__init(&machines->host, "", HOST_KERNEL_ID);
  108. machines->guests = RB_ROOT;
  109. machines->symbol_filter = NULL;
  110. }
  111. void machines__exit(struct machines *machines)
  112. {
  113. machine__exit(&machines->host);
  114. /* XXX exit guest */
  115. }
  116. struct machine *machines__add(struct machines *machines, pid_t pid,
  117. const char *root_dir)
  118. {
  119. struct rb_node **p = &machines->guests.rb_node;
  120. struct rb_node *parent = NULL;
  121. struct machine *pos, *machine = malloc(sizeof(*machine));
  122. if (machine == NULL)
  123. return NULL;
  124. if (machine__init(machine, root_dir, pid) != 0) {
  125. free(machine);
  126. return NULL;
  127. }
  128. machine->symbol_filter = machines->symbol_filter;
  129. while (*p != NULL) {
  130. parent = *p;
  131. pos = rb_entry(parent, struct machine, rb_node);
  132. if (pid < pos->pid)
  133. p = &(*p)->rb_left;
  134. else
  135. p = &(*p)->rb_right;
  136. }
  137. rb_link_node(&machine->rb_node, parent, p);
  138. rb_insert_color(&machine->rb_node, &machines->guests);
  139. return machine;
  140. }
  141. void machines__set_symbol_filter(struct machines *machines,
  142. symbol_filter_t symbol_filter)
  143. {
  144. struct rb_node *nd;
  145. machines->symbol_filter = symbol_filter;
  146. machines->host.symbol_filter = symbol_filter;
  147. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  148. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  149. machine->symbol_filter = symbol_filter;
  150. }
  151. }
  152. void machines__set_comm_exec(struct machines *machines, bool comm_exec)
  153. {
  154. struct rb_node *nd;
  155. machines->host.comm_exec = comm_exec;
  156. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  157. struct machine *machine = rb_entry(nd, struct machine, rb_node);
  158. machine->comm_exec = comm_exec;
  159. }
  160. }
  161. struct machine *machines__find(struct machines *machines, pid_t pid)
  162. {
  163. struct rb_node **p = &machines->guests.rb_node;
  164. struct rb_node *parent = NULL;
  165. struct machine *machine;
  166. struct machine *default_machine = NULL;
  167. if (pid == HOST_KERNEL_ID)
  168. return &machines->host;
  169. while (*p != NULL) {
  170. parent = *p;
  171. machine = rb_entry(parent, struct machine, rb_node);
  172. if (pid < machine->pid)
  173. p = &(*p)->rb_left;
  174. else if (pid > machine->pid)
  175. p = &(*p)->rb_right;
  176. else
  177. return machine;
  178. if (!machine->pid)
  179. default_machine = machine;
  180. }
  181. return default_machine;
  182. }
  183. struct machine *machines__findnew(struct machines *machines, pid_t pid)
  184. {
  185. char path[PATH_MAX];
  186. const char *root_dir = "";
  187. struct machine *machine = machines__find(machines, pid);
  188. if (machine && (machine->pid == pid))
  189. goto out;
  190. if ((pid != HOST_KERNEL_ID) &&
  191. (pid != DEFAULT_GUEST_KERNEL_ID) &&
  192. (symbol_conf.guestmount)) {
  193. sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
  194. if (access(path, R_OK)) {
  195. static struct strlist *seen;
  196. if (!seen)
  197. seen = strlist__new(true, NULL);
  198. if (!strlist__has_entry(seen, path)) {
  199. pr_err("Can't access file %s\n", path);
  200. strlist__add(seen, path);
  201. }
  202. machine = NULL;
  203. goto out;
  204. }
  205. root_dir = path;
  206. }
  207. machine = machines__add(machines, pid, root_dir);
  208. out:
  209. return machine;
  210. }
  211. void machines__process_guests(struct machines *machines,
  212. machine__process_t process, void *data)
  213. {
  214. struct rb_node *nd;
  215. for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
  216. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  217. process(pos, data);
  218. }
  219. }
  220. char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
  221. {
  222. if (machine__is_host(machine))
  223. snprintf(bf, size, "[%s]", "kernel.kallsyms");
  224. else if (machine__is_default_guest(machine))
  225. snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
  226. else {
  227. snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
  228. machine->pid);
  229. }
  230. return bf;
  231. }
  232. void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
  233. {
  234. struct rb_node *node;
  235. struct machine *machine;
  236. machines->host.id_hdr_size = id_hdr_size;
  237. for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
  238. machine = rb_entry(node, struct machine, rb_node);
  239. machine->id_hdr_size = id_hdr_size;
  240. }
  241. return;
  242. }
  243. static void machine__update_thread_pid(struct machine *machine,
  244. struct thread *th, pid_t pid)
  245. {
  246. struct thread *leader;
  247. if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
  248. return;
  249. th->pid_ = pid;
  250. if (th->pid_ == th->tid)
  251. return;
  252. leader = machine__findnew_thread(machine, th->pid_, th->pid_);
  253. if (!leader)
  254. goto out_err;
  255. if (!leader->mg)
  256. leader->mg = map_groups__new(machine);
  257. if (!leader->mg)
  258. goto out_err;
  259. if (th->mg == leader->mg)
  260. return;
  261. if (th->mg) {
  262. /*
  263. * Maps are created from MMAP events which provide the pid and
  264. * tid. Consequently there never should be any maps on a thread
  265. * with an unknown pid. Just print an error if there are.
  266. */
  267. if (!map_groups__empty(th->mg))
  268. pr_err("Discarding thread maps for %d:%d\n",
  269. th->pid_, th->tid);
  270. map_groups__delete(th->mg);
  271. }
  272. th->mg = map_groups__get(leader->mg);
  273. return;
  274. out_err:
  275. pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
  276. }
  277. static struct thread *__machine__findnew_thread(struct machine *machine,
  278. pid_t pid, pid_t tid,
  279. bool create)
  280. {
  281. struct rb_node **p = &machine->threads.rb_node;
  282. struct rb_node *parent = NULL;
  283. struct thread *th;
  284. /*
  285. * Front-end cache - TID lookups come in blocks,
  286. * so most of the time we dont have to look up
  287. * the full rbtree:
  288. */
  289. th = machine->last_match;
  290. if (th && th->tid == tid) {
  291. machine__update_thread_pid(machine, th, pid);
  292. return th;
  293. }
  294. while (*p != NULL) {
  295. parent = *p;
  296. th = rb_entry(parent, struct thread, rb_node);
  297. if (th->tid == tid) {
  298. machine->last_match = th;
  299. machine__update_thread_pid(machine, th, pid);
  300. return th;
  301. }
  302. if (tid < th->tid)
  303. p = &(*p)->rb_left;
  304. else
  305. p = &(*p)->rb_right;
  306. }
  307. if (!create)
  308. return NULL;
  309. th = thread__new(pid, tid);
  310. if (th != NULL) {
  311. rb_link_node(&th->rb_node, parent, p);
  312. rb_insert_color(&th->rb_node, &machine->threads);
  313. machine->last_match = th;
  314. /*
  315. * We have to initialize map_groups separately
  316. * after rb tree is updated.
  317. *
  318. * The reason is that we call machine__findnew_thread
  319. * within thread__init_map_groups to find the thread
  320. * leader and that would screwed the rb tree.
  321. */
  322. if (thread__init_map_groups(th, machine)) {
  323. thread__delete(th);
  324. return NULL;
  325. }
  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. machine->last_match = NULL;
  1005. rb_erase(&th->rb_node, &machine->threads);
  1006. /*
  1007. * We may have references to this thread, for instance in some hist_entry
  1008. * instances, so just move them to a separate list.
  1009. */
  1010. list_add_tail(&th->node, &machine->dead_threads);
  1011. }
  1012. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  1013. struct perf_sample *sample)
  1014. {
  1015. struct thread *thread = machine__find_thread(machine,
  1016. event->fork.pid,
  1017. event->fork.tid);
  1018. struct thread *parent = machine__findnew_thread(machine,
  1019. event->fork.ppid,
  1020. event->fork.ptid);
  1021. /* if a thread currently exists for the thread id remove it */
  1022. if (thread != NULL)
  1023. machine__remove_thread(machine, thread);
  1024. thread = machine__findnew_thread(machine, event->fork.pid,
  1025. event->fork.tid);
  1026. if (dump_trace)
  1027. perf_event__fprintf_task(event, stdout);
  1028. if (thread == NULL || parent == NULL ||
  1029. thread__fork(thread, parent, sample->time) < 0) {
  1030. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  1031. return -1;
  1032. }
  1033. return 0;
  1034. }
  1035. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  1036. struct perf_sample *sample __maybe_unused)
  1037. {
  1038. struct thread *thread = machine__find_thread(machine,
  1039. event->fork.pid,
  1040. event->fork.tid);
  1041. if (dump_trace)
  1042. perf_event__fprintf_task(event, stdout);
  1043. if (thread != NULL)
  1044. thread__exited(thread);
  1045. return 0;
  1046. }
  1047. int machine__process_event(struct machine *machine, union perf_event *event,
  1048. struct perf_sample *sample)
  1049. {
  1050. int ret;
  1051. switch (event->header.type) {
  1052. case PERF_RECORD_COMM:
  1053. ret = machine__process_comm_event(machine, event, sample); break;
  1054. case PERF_RECORD_MMAP:
  1055. ret = machine__process_mmap_event(machine, event, sample); break;
  1056. case PERF_RECORD_MMAP2:
  1057. ret = machine__process_mmap2_event(machine, event, sample); break;
  1058. case PERF_RECORD_FORK:
  1059. ret = machine__process_fork_event(machine, event, sample); break;
  1060. case PERF_RECORD_EXIT:
  1061. ret = machine__process_exit_event(machine, event, sample); break;
  1062. case PERF_RECORD_LOST:
  1063. ret = machine__process_lost_event(machine, event, sample); break;
  1064. default:
  1065. ret = -1;
  1066. break;
  1067. }
  1068. return ret;
  1069. }
  1070. static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
  1071. {
  1072. if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
  1073. return 1;
  1074. return 0;
  1075. }
  1076. static void ip__resolve_ams(struct thread *thread,
  1077. struct addr_map_symbol *ams,
  1078. u64 ip)
  1079. {
  1080. struct addr_location al;
  1081. memset(&al, 0, sizeof(al));
  1082. /*
  1083. * We cannot use the header.misc hint to determine whether a
  1084. * branch stack address is user, kernel, guest, hypervisor.
  1085. * Branches may straddle the kernel/user/hypervisor boundaries.
  1086. * Thus, we have to try consecutively until we find a match
  1087. * or else, the symbol is unknown
  1088. */
  1089. thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
  1090. ams->addr = ip;
  1091. ams->al_addr = al.addr;
  1092. ams->sym = al.sym;
  1093. ams->map = al.map;
  1094. }
  1095. static void ip__resolve_data(struct thread *thread,
  1096. u8 m, struct addr_map_symbol *ams, u64 addr)
  1097. {
  1098. struct addr_location al;
  1099. memset(&al, 0, sizeof(al));
  1100. thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
  1101. if (al.map == NULL) {
  1102. /*
  1103. * some shared data regions have execute bit set which puts
  1104. * their mapping in the MAP__FUNCTION type array.
  1105. * Check there as a fallback option before dropping the sample.
  1106. */
  1107. thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
  1108. }
  1109. ams->addr = addr;
  1110. ams->al_addr = al.addr;
  1111. ams->sym = al.sym;
  1112. ams->map = al.map;
  1113. }
  1114. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  1115. struct addr_location *al)
  1116. {
  1117. struct mem_info *mi = zalloc(sizeof(*mi));
  1118. if (!mi)
  1119. return NULL;
  1120. ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
  1121. ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
  1122. mi->data_src.val = sample->data_src;
  1123. return mi;
  1124. }
  1125. static int add_callchain_ip(struct thread *thread,
  1126. struct symbol **parent,
  1127. struct addr_location *root_al,
  1128. bool branch_history,
  1129. u64 ip)
  1130. {
  1131. struct addr_location al;
  1132. al.filtered = 0;
  1133. al.sym = NULL;
  1134. if (branch_history)
  1135. thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
  1136. ip, &al);
  1137. else {
  1138. u8 cpumode = PERF_RECORD_MISC_USER;
  1139. if (ip >= PERF_CONTEXT_MAX) {
  1140. switch (ip) {
  1141. case PERF_CONTEXT_HV:
  1142. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  1143. break;
  1144. case PERF_CONTEXT_KERNEL:
  1145. cpumode = PERF_RECORD_MISC_KERNEL;
  1146. break;
  1147. case PERF_CONTEXT_USER:
  1148. cpumode = PERF_RECORD_MISC_USER;
  1149. break;
  1150. default:
  1151. pr_debug("invalid callchain context: "
  1152. "%"PRId64"\n", (s64) ip);
  1153. /*
  1154. * It seems the callchain is corrupted.
  1155. * Discard all.
  1156. */
  1157. callchain_cursor_reset(&callchain_cursor);
  1158. return 1;
  1159. }
  1160. return 0;
  1161. }
  1162. thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
  1163. ip, &al);
  1164. }
  1165. if (al.sym != NULL) {
  1166. if (sort__has_parent && !*parent &&
  1167. symbol__match_regex(al.sym, &parent_regex))
  1168. *parent = al.sym;
  1169. else if (have_ignore_callees && root_al &&
  1170. symbol__match_regex(al.sym, &ignore_callees_regex)) {
  1171. /* Treat this symbol as the root,
  1172. forgetting its callees. */
  1173. *root_al = al;
  1174. callchain_cursor_reset(&callchain_cursor);
  1175. }
  1176. }
  1177. return callchain_cursor_append(&callchain_cursor, al.addr, al.map, al.sym);
  1178. }
  1179. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  1180. struct addr_location *al)
  1181. {
  1182. unsigned int i;
  1183. const struct branch_stack *bs = sample->branch_stack;
  1184. struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
  1185. if (!bi)
  1186. return NULL;
  1187. for (i = 0; i < bs->nr; i++) {
  1188. ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
  1189. ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
  1190. bi[i].flags = bs->entries[i].flags;
  1191. }
  1192. return bi;
  1193. }
  1194. #define CHASHSZ 127
  1195. #define CHASHBITS 7
  1196. #define NO_ENTRY 0xff
  1197. #define PERF_MAX_BRANCH_DEPTH 127
  1198. /* Remove loops. */
  1199. static int remove_loops(struct branch_entry *l, int nr)
  1200. {
  1201. int i, j, off;
  1202. unsigned char chash[CHASHSZ];
  1203. memset(chash, NO_ENTRY, sizeof(chash));
  1204. BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
  1205. for (i = 0; i < nr; i++) {
  1206. int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
  1207. /* no collision handling for now */
  1208. if (chash[h] == NO_ENTRY) {
  1209. chash[h] = i;
  1210. } else if (l[chash[h]].from == l[i].from) {
  1211. bool is_loop = true;
  1212. /* check if it is a real loop */
  1213. off = 0;
  1214. for (j = chash[h]; j < i && i + off < nr; j++, off++)
  1215. if (l[j].from != l[i + off].from) {
  1216. is_loop = false;
  1217. break;
  1218. }
  1219. if (is_loop) {
  1220. memmove(l + i, l + i + off,
  1221. (nr - (i + off)) * sizeof(*l));
  1222. nr -= off;
  1223. }
  1224. }
  1225. }
  1226. return nr;
  1227. }
  1228. static int thread__resolve_callchain_sample(struct thread *thread,
  1229. struct ip_callchain *chain,
  1230. struct branch_stack *branch,
  1231. struct symbol **parent,
  1232. struct addr_location *root_al,
  1233. int max_stack)
  1234. {
  1235. int chain_nr = min(max_stack, (int)chain->nr);
  1236. int i, j, err;
  1237. int skip_idx = -1;
  1238. int first_call = 0;
  1239. /*
  1240. * Based on DWARF debug information, some architectures skip
  1241. * a callchain entry saved by the kernel.
  1242. */
  1243. if (chain->nr < PERF_MAX_STACK_DEPTH)
  1244. skip_idx = arch_skip_callchain_idx(thread, chain);
  1245. callchain_cursor_reset(&callchain_cursor);
  1246. /*
  1247. * Add branches to call stack for easier browsing. This gives
  1248. * more context for a sample than just the callers.
  1249. *
  1250. * This uses individual histograms of paths compared to the
  1251. * aggregated histograms the normal LBR mode uses.
  1252. *
  1253. * Limitations for now:
  1254. * - No extra filters
  1255. * - No annotations (should annotate somehow)
  1256. */
  1257. if (branch && callchain_param.branch_callstack) {
  1258. int nr = min(max_stack, (int)branch->nr);
  1259. struct branch_entry be[nr];
  1260. if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
  1261. pr_warning("corrupted branch chain. skipping...\n");
  1262. goto check_calls;
  1263. }
  1264. for (i = 0; i < nr; i++) {
  1265. if (callchain_param.order == ORDER_CALLEE) {
  1266. be[i] = branch->entries[i];
  1267. /*
  1268. * Check for overlap into the callchain.
  1269. * The return address is one off compared to
  1270. * the branch entry. To adjust for this
  1271. * assume the calling instruction is not longer
  1272. * than 8 bytes.
  1273. */
  1274. if (i == skip_idx ||
  1275. chain->ips[first_call] >= PERF_CONTEXT_MAX)
  1276. first_call++;
  1277. else if (be[i].from < chain->ips[first_call] &&
  1278. be[i].from >= chain->ips[first_call] - 8)
  1279. first_call++;
  1280. } else
  1281. be[i] = branch->entries[branch->nr - i - 1];
  1282. }
  1283. nr = remove_loops(be, nr);
  1284. for (i = 0; i < nr; i++) {
  1285. err = add_callchain_ip(thread, parent, root_al,
  1286. true, be[i].to);
  1287. if (!err)
  1288. err = add_callchain_ip(thread, parent, root_al,
  1289. true, be[i].from);
  1290. if (err == -EINVAL)
  1291. break;
  1292. if (err)
  1293. return err;
  1294. }
  1295. chain_nr -= nr;
  1296. }
  1297. check_calls:
  1298. if (chain->nr > PERF_MAX_STACK_DEPTH) {
  1299. pr_warning("corrupted callchain. skipping...\n");
  1300. return 0;
  1301. }
  1302. for (i = first_call; i < chain_nr; i++) {
  1303. u64 ip;
  1304. if (callchain_param.order == ORDER_CALLEE)
  1305. j = i;
  1306. else
  1307. j = chain->nr - i - 1;
  1308. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  1309. if (j == skip_idx)
  1310. continue;
  1311. #endif
  1312. ip = chain->ips[j];
  1313. err = add_callchain_ip(thread, parent, root_al, false, ip);
  1314. if (err)
  1315. return (err < 0) ? err : 0;
  1316. }
  1317. return 0;
  1318. }
  1319. static int unwind_entry(struct unwind_entry *entry, void *arg)
  1320. {
  1321. struct callchain_cursor *cursor = arg;
  1322. return callchain_cursor_append(cursor, entry->ip,
  1323. entry->map, entry->sym);
  1324. }
  1325. int thread__resolve_callchain(struct thread *thread,
  1326. struct perf_evsel *evsel,
  1327. struct perf_sample *sample,
  1328. struct symbol **parent,
  1329. struct addr_location *root_al,
  1330. int max_stack)
  1331. {
  1332. int ret = thread__resolve_callchain_sample(thread, sample->callchain,
  1333. sample->branch_stack,
  1334. parent, root_al, max_stack);
  1335. if (ret)
  1336. return ret;
  1337. /* Can we do dwarf post unwind? */
  1338. if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
  1339. (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
  1340. return 0;
  1341. /* Bail out if nothing was captured. */
  1342. if ((!sample->user_regs.regs) ||
  1343. (!sample->user_stack.size))
  1344. return 0;
  1345. return unwind__get_entries(unwind_entry, &callchain_cursor,
  1346. thread, sample, max_stack);
  1347. }
  1348. int machine__for_each_thread(struct machine *machine,
  1349. int (*fn)(struct thread *thread, void *p),
  1350. void *priv)
  1351. {
  1352. struct rb_node *nd;
  1353. struct thread *thread;
  1354. int rc = 0;
  1355. for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
  1356. thread = rb_entry(nd, struct thread, rb_node);
  1357. rc = fn(thread, priv);
  1358. if (rc != 0)
  1359. return rc;
  1360. }
  1361. list_for_each_entry(thread, &machine->dead_threads, node) {
  1362. rc = fn(thread, priv);
  1363. if (rc != 0)
  1364. return rc;
  1365. }
  1366. return rc;
  1367. }
  1368. int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
  1369. struct target *target, struct thread_map *threads,
  1370. perf_event__handler_t process, bool data_mmap)
  1371. {
  1372. if (target__has_task(target))
  1373. return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
  1374. else if (target__has_cpu(target))
  1375. return perf_event__synthesize_threads(tool, process, machine, data_mmap);
  1376. /* command specified */
  1377. return 0;
  1378. }
  1379. pid_t machine__get_current_tid(struct machine *machine, int cpu)
  1380. {
  1381. if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
  1382. return -1;
  1383. return machine->current_tid[cpu];
  1384. }
  1385. int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
  1386. pid_t tid)
  1387. {
  1388. struct thread *thread;
  1389. if (cpu < 0)
  1390. return -EINVAL;
  1391. if (!machine->current_tid) {
  1392. int i;
  1393. machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
  1394. if (!machine->current_tid)
  1395. return -ENOMEM;
  1396. for (i = 0; i < MAX_NR_CPUS; i++)
  1397. machine->current_tid[i] = -1;
  1398. }
  1399. if (cpu >= MAX_NR_CPUS) {
  1400. pr_err("Requested CPU %d too large. ", cpu);
  1401. pr_err("Consider raising MAX_NR_CPUS\n");
  1402. return -EINVAL;
  1403. }
  1404. machine->current_tid[cpu] = tid;
  1405. thread = machine__findnew_thread(machine, pid, tid);
  1406. if (!thread)
  1407. return -ENOMEM;
  1408. thread->cpu = cpu;
  1409. return 0;
  1410. }
  1411. int machine__get_kernel_start(struct machine *machine)
  1412. {
  1413. struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
  1414. int err = 0;
  1415. /*
  1416. * The only addresses above 2^63 are kernel addresses of a 64-bit
  1417. * kernel. Note that addresses are unsigned so that on a 32-bit system
  1418. * all addresses including kernel addresses are less than 2^32. In
  1419. * that case (32-bit system), if the kernel mapping is unknown, all
  1420. * addresses will be assumed to be in user space - see
  1421. * machine__kernel_ip().
  1422. */
  1423. machine->kernel_start = 1ULL << 63;
  1424. if (map) {
  1425. err = map__load(map, machine->symbol_filter);
  1426. if (map->start)
  1427. machine->kernel_start = map->start;
  1428. }
  1429. return err;
  1430. }