builtin-annotate.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. /*
  2. * builtin-annotate.c
  3. *
  4. * Builtin annotate command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/color.h"
  11. #include <linux/list.h>
  12. #include "util/cache.h"
  13. #include <linux/rbtree.h>
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "perf.h"
  17. #include "util/parse-options.h"
  18. #include "util/parse-events.h"
  19. #define SHOW_KERNEL 1
  20. #define SHOW_USER 2
  21. #define SHOW_HV 4
  22. static char const *input_name = "perf.data";
  23. static char default_sort_order[] = "comm,symbol";
  24. static char *sort_order = default_sort_order;
  25. static int input;
  26. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  27. static int dump_trace = 0;
  28. #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
  29. static int full_paths;
  30. static int print_line;
  31. static unsigned long page_size;
  32. static unsigned long mmap_window = 32;
  33. struct ip_event {
  34. struct perf_event_header header;
  35. u64 ip;
  36. u32 pid, tid;
  37. };
  38. struct mmap_event {
  39. struct perf_event_header header;
  40. u32 pid, tid;
  41. u64 start;
  42. u64 len;
  43. u64 pgoff;
  44. char filename[PATH_MAX];
  45. };
  46. struct comm_event {
  47. struct perf_event_header header;
  48. u32 pid, tid;
  49. char comm[16];
  50. };
  51. struct fork_event {
  52. struct perf_event_header header;
  53. u32 pid, ppid;
  54. };
  55. typedef union event_union {
  56. struct perf_event_header header;
  57. struct ip_event ip;
  58. struct mmap_event mmap;
  59. struct comm_event comm;
  60. struct fork_event fork;
  61. } event_t;
  62. struct sym_ext {
  63. struct rb_node node;
  64. double percent;
  65. char *path;
  66. };
  67. struct map {
  68. struct list_head node;
  69. u64 start;
  70. u64 end;
  71. u64 pgoff;
  72. u64 (*map_ip)(struct map *, u64);
  73. struct dso *dso;
  74. };
  75. static u64 map__map_ip(struct map *map, u64 ip)
  76. {
  77. return ip - map->start + map->pgoff;
  78. }
  79. static u64 vdso__map_ip(struct map *map __used, u64 ip)
  80. {
  81. return ip;
  82. }
  83. static struct map *map__new(struct mmap_event *event)
  84. {
  85. struct map *self = malloc(sizeof(*self));
  86. if (self != NULL) {
  87. const char *filename = event->filename;
  88. self->start = event->start;
  89. self->end = event->start + event->len;
  90. self->pgoff = event->pgoff;
  91. self->dso = dsos__findnew(filename);
  92. if (self->dso == NULL)
  93. goto out_delete;
  94. if (self->dso == vdso)
  95. self->map_ip = vdso__map_ip;
  96. else
  97. self->map_ip = map__map_ip;
  98. }
  99. return self;
  100. out_delete:
  101. free(self);
  102. return NULL;
  103. }
  104. static struct map *map__clone(struct map *self)
  105. {
  106. struct map *map = malloc(sizeof(*self));
  107. if (!map)
  108. return NULL;
  109. memcpy(map, self, sizeof(*self));
  110. return map;
  111. }
  112. static int map__overlap(struct map *l, struct map *r)
  113. {
  114. if (l->start > r->start) {
  115. struct map *t = l;
  116. l = r;
  117. r = t;
  118. }
  119. if (l->end > r->start)
  120. return 1;
  121. return 0;
  122. }
  123. static size_t map__fprintf(struct map *self, FILE *fp)
  124. {
  125. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  126. self->start, self->end, self->pgoff, self->dso->name);
  127. }
  128. struct thread {
  129. struct rb_node rb_node;
  130. struct list_head maps;
  131. pid_t pid;
  132. char *comm;
  133. };
  134. static struct thread *thread__new(pid_t pid)
  135. {
  136. struct thread *self = malloc(sizeof(*self));
  137. if (self != NULL) {
  138. self->pid = pid;
  139. self->comm = malloc(32);
  140. if (self->comm)
  141. snprintf(self->comm, 32, ":%d", self->pid);
  142. INIT_LIST_HEAD(&self->maps);
  143. }
  144. return self;
  145. }
  146. static int thread__set_comm(struct thread *self, const char *comm)
  147. {
  148. if (self->comm)
  149. free(self->comm);
  150. self->comm = strdup(comm);
  151. return self->comm ? 0 : -ENOMEM;
  152. }
  153. static size_t thread__fprintf(struct thread *self, FILE *fp)
  154. {
  155. struct map *pos;
  156. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  157. list_for_each_entry(pos, &self->maps, node)
  158. ret += map__fprintf(pos, fp);
  159. return ret;
  160. }
  161. static struct rb_root threads;
  162. static struct thread *last_match;
  163. static struct thread *threads__findnew(pid_t pid)
  164. {
  165. struct rb_node **p = &threads.rb_node;
  166. struct rb_node *parent = NULL;
  167. struct thread *th;
  168. /*
  169. * Font-end cache - PID lookups come in blocks,
  170. * so most of the time we dont have to look up
  171. * the full rbtree:
  172. */
  173. if (last_match && last_match->pid == pid)
  174. return last_match;
  175. while (*p != NULL) {
  176. parent = *p;
  177. th = rb_entry(parent, struct thread, rb_node);
  178. if (th->pid == pid) {
  179. last_match = th;
  180. return th;
  181. }
  182. if (pid < th->pid)
  183. p = &(*p)->rb_left;
  184. else
  185. p = &(*p)->rb_right;
  186. }
  187. th = thread__new(pid);
  188. if (th != NULL) {
  189. rb_link_node(&th->rb_node, parent, p);
  190. rb_insert_color(&th->rb_node, &threads);
  191. last_match = th;
  192. }
  193. return th;
  194. }
  195. static void thread__insert_map(struct thread *self, struct map *map)
  196. {
  197. struct map *pos, *tmp;
  198. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  199. if (map__overlap(pos, map)) {
  200. list_del_init(&pos->node);
  201. /* XXX leaks dsos */
  202. free(pos);
  203. }
  204. }
  205. list_add_tail(&map->node, &self->maps);
  206. }
  207. static int thread__fork(struct thread *self, struct thread *parent)
  208. {
  209. struct map *map;
  210. if (self->comm)
  211. free(self->comm);
  212. self->comm = strdup(parent->comm);
  213. if (!self->comm)
  214. return -ENOMEM;
  215. list_for_each_entry(map, &parent->maps, node) {
  216. struct map *new = map__clone(map);
  217. if (!new)
  218. return -ENOMEM;
  219. thread__insert_map(self, new);
  220. }
  221. return 0;
  222. }
  223. static struct map *thread__find_map(struct thread *self, u64 ip)
  224. {
  225. struct map *pos;
  226. if (self == NULL)
  227. return NULL;
  228. list_for_each_entry(pos, &self->maps, node)
  229. if (ip >= pos->start && ip <= pos->end)
  230. return pos;
  231. return NULL;
  232. }
  233. static size_t threads__fprintf(FILE *fp)
  234. {
  235. size_t ret = 0;
  236. struct rb_node *nd;
  237. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  238. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  239. ret += thread__fprintf(pos, fp);
  240. }
  241. return ret;
  242. }
  243. /*
  244. * histogram, sorted on item, collects counts
  245. */
  246. static struct rb_root hist;
  247. struct hist_entry {
  248. struct rb_node rb_node;
  249. struct thread *thread;
  250. struct map *map;
  251. struct dso *dso;
  252. struct symbol *sym;
  253. u64 ip;
  254. char level;
  255. uint32_t count;
  256. };
  257. /*
  258. * configurable sorting bits
  259. */
  260. struct sort_entry {
  261. struct list_head list;
  262. char *header;
  263. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  264. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  265. size_t (*print)(FILE *fp, struct hist_entry *);
  266. };
  267. /* --sort pid */
  268. static int64_t
  269. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  270. {
  271. return right->thread->pid - left->thread->pid;
  272. }
  273. static size_t
  274. sort__thread_print(FILE *fp, struct hist_entry *self)
  275. {
  276. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  277. }
  278. static struct sort_entry sort_thread = {
  279. .header = " Command: Pid",
  280. .cmp = sort__thread_cmp,
  281. .print = sort__thread_print,
  282. };
  283. /* --sort comm */
  284. static int64_t
  285. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  286. {
  287. return right->thread->pid - left->thread->pid;
  288. }
  289. static int64_t
  290. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  291. {
  292. char *comm_l = left->thread->comm;
  293. char *comm_r = right->thread->comm;
  294. if (!comm_l || !comm_r) {
  295. if (!comm_l && !comm_r)
  296. return 0;
  297. else if (!comm_l)
  298. return -1;
  299. else
  300. return 1;
  301. }
  302. return strcmp(comm_l, comm_r);
  303. }
  304. static size_t
  305. sort__comm_print(FILE *fp, struct hist_entry *self)
  306. {
  307. return fprintf(fp, "%16s", self->thread->comm);
  308. }
  309. static struct sort_entry sort_comm = {
  310. .header = " Command",
  311. .cmp = sort__comm_cmp,
  312. .collapse = sort__comm_collapse,
  313. .print = sort__comm_print,
  314. };
  315. /* --sort dso */
  316. static int64_t
  317. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  318. {
  319. struct dso *dso_l = left->dso;
  320. struct dso *dso_r = right->dso;
  321. if (!dso_l || !dso_r) {
  322. if (!dso_l && !dso_r)
  323. return 0;
  324. else if (!dso_l)
  325. return -1;
  326. else
  327. return 1;
  328. }
  329. return strcmp(dso_l->name, dso_r->name);
  330. }
  331. static size_t
  332. sort__dso_print(FILE *fp, struct hist_entry *self)
  333. {
  334. if (self->dso)
  335. return fprintf(fp, "%-25s", self->dso->name);
  336. return fprintf(fp, "%016llx ", (u64)self->ip);
  337. }
  338. static struct sort_entry sort_dso = {
  339. .header = "Shared Object ",
  340. .cmp = sort__dso_cmp,
  341. .print = sort__dso_print,
  342. };
  343. /* --sort symbol */
  344. static int64_t
  345. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  346. {
  347. u64 ip_l, ip_r;
  348. if (left->sym == right->sym)
  349. return 0;
  350. ip_l = left->sym ? left->sym->start : left->ip;
  351. ip_r = right->sym ? right->sym->start : right->ip;
  352. return (int64_t)(ip_r - ip_l);
  353. }
  354. static size_t
  355. sort__sym_print(FILE *fp, struct hist_entry *self)
  356. {
  357. size_t ret = 0;
  358. if (verbose)
  359. ret += fprintf(fp, "%#018llx ", (u64)self->ip);
  360. if (self->sym) {
  361. ret += fprintf(fp, "[%c] %s",
  362. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  363. } else {
  364. ret += fprintf(fp, "%#016llx", (u64)self->ip);
  365. }
  366. return ret;
  367. }
  368. static struct sort_entry sort_sym = {
  369. .header = "Symbol",
  370. .cmp = sort__sym_cmp,
  371. .print = sort__sym_print,
  372. };
  373. static int sort__need_collapse = 0;
  374. struct sort_dimension {
  375. char *name;
  376. struct sort_entry *entry;
  377. int taken;
  378. };
  379. static struct sort_dimension sort_dimensions[] = {
  380. { .name = "pid", .entry = &sort_thread, },
  381. { .name = "comm", .entry = &sort_comm, },
  382. { .name = "dso", .entry = &sort_dso, },
  383. { .name = "symbol", .entry = &sort_sym, },
  384. };
  385. static LIST_HEAD(hist_entry__sort_list);
  386. static int sort_dimension__add(char *tok)
  387. {
  388. unsigned int i;
  389. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  390. struct sort_dimension *sd = &sort_dimensions[i];
  391. if (sd->taken)
  392. continue;
  393. if (strncasecmp(tok, sd->name, strlen(tok)))
  394. continue;
  395. if (sd->entry->collapse)
  396. sort__need_collapse = 1;
  397. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  398. sd->taken = 1;
  399. return 0;
  400. }
  401. return -ESRCH;
  402. }
  403. static int64_t
  404. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  405. {
  406. struct sort_entry *se;
  407. int64_t cmp = 0;
  408. list_for_each_entry(se, &hist_entry__sort_list, list) {
  409. cmp = se->cmp(left, right);
  410. if (cmp)
  411. break;
  412. }
  413. return cmp;
  414. }
  415. static int64_t
  416. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  417. {
  418. struct sort_entry *se;
  419. int64_t cmp = 0;
  420. list_for_each_entry(se, &hist_entry__sort_list, list) {
  421. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  422. f = se->collapse ?: se->cmp;
  423. cmp = f(left, right);
  424. if (cmp)
  425. break;
  426. }
  427. return cmp;
  428. }
  429. /*
  430. * collect histogram counts
  431. */
  432. static void hist_hit(struct hist_entry *he, u64 ip)
  433. {
  434. unsigned int sym_size, offset;
  435. struct symbol *sym = he->sym;
  436. he->count++;
  437. if (!sym || !sym->hist)
  438. return;
  439. sym_size = sym->end - sym->start;
  440. offset = ip - sym->start;
  441. if (offset >= sym_size)
  442. return;
  443. sym->hist_sum++;
  444. sym->hist[offset]++;
  445. if (verbose >= 3)
  446. printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
  447. (void *)(unsigned long)he->sym->start,
  448. he->sym->name,
  449. (void *)(unsigned long)ip, ip - he->sym->start,
  450. sym->hist[offset]);
  451. }
  452. static int
  453. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  454. struct symbol *sym, u64 ip, char level)
  455. {
  456. struct rb_node **p = &hist.rb_node;
  457. struct rb_node *parent = NULL;
  458. struct hist_entry *he;
  459. struct hist_entry entry = {
  460. .thread = thread,
  461. .map = map,
  462. .dso = dso,
  463. .sym = sym,
  464. .ip = ip,
  465. .level = level,
  466. .count = 1,
  467. };
  468. int cmp;
  469. while (*p != NULL) {
  470. parent = *p;
  471. he = rb_entry(parent, struct hist_entry, rb_node);
  472. cmp = hist_entry__cmp(&entry, he);
  473. if (!cmp) {
  474. hist_hit(he, ip);
  475. return 0;
  476. }
  477. if (cmp < 0)
  478. p = &(*p)->rb_left;
  479. else
  480. p = &(*p)->rb_right;
  481. }
  482. he = malloc(sizeof(*he));
  483. if (!he)
  484. return -ENOMEM;
  485. *he = entry;
  486. rb_link_node(&he->rb_node, parent, p);
  487. rb_insert_color(&he->rb_node, &hist);
  488. return 0;
  489. }
  490. static void hist_entry__free(struct hist_entry *he)
  491. {
  492. free(he);
  493. }
  494. /*
  495. * collapse the histogram
  496. */
  497. static struct rb_root collapse_hists;
  498. static void collapse__insert_entry(struct hist_entry *he)
  499. {
  500. struct rb_node **p = &collapse_hists.rb_node;
  501. struct rb_node *parent = NULL;
  502. struct hist_entry *iter;
  503. int64_t cmp;
  504. while (*p != NULL) {
  505. parent = *p;
  506. iter = rb_entry(parent, struct hist_entry, rb_node);
  507. cmp = hist_entry__collapse(iter, he);
  508. if (!cmp) {
  509. iter->count += he->count;
  510. hist_entry__free(he);
  511. return;
  512. }
  513. if (cmp < 0)
  514. p = &(*p)->rb_left;
  515. else
  516. p = &(*p)->rb_right;
  517. }
  518. rb_link_node(&he->rb_node, parent, p);
  519. rb_insert_color(&he->rb_node, &collapse_hists);
  520. }
  521. static void collapse__resort(void)
  522. {
  523. struct rb_node *next;
  524. struct hist_entry *n;
  525. if (!sort__need_collapse)
  526. return;
  527. next = rb_first(&hist);
  528. while (next) {
  529. n = rb_entry(next, struct hist_entry, rb_node);
  530. next = rb_next(&n->rb_node);
  531. rb_erase(&n->rb_node, &hist);
  532. collapse__insert_entry(n);
  533. }
  534. }
  535. /*
  536. * reverse the map, sort on count.
  537. */
  538. static struct rb_root output_hists;
  539. static void output__insert_entry(struct hist_entry *he)
  540. {
  541. struct rb_node **p = &output_hists.rb_node;
  542. struct rb_node *parent = NULL;
  543. struct hist_entry *iter;
  544. while (*p != NULL) {
  545. parent = *p;
  546. iter = rb_entry(parent, struct hist_entry, rb_node);
  547. if (he->count > iter->count)
  548. p = &(*p)->rb_left;
  549. else
  550. p = &(*p)->rb_right;
  551. }
  552. rb_link_node(&he->rb_node, parent, p);
  553. rb_insert_color(&he->rb_node, &output_hists);
  554. }
  555. static void output__resort(void)
  556. {
  557. struct rb_node *next;
  558. struct hist_entry *n;
  559. struct rb_root *tree = &hist;
  560. if (sort__need_collapse)
  561. tree = &collapse_hists;
  562. next = rb_first(tree);
  563. while (next) {
  564. n = rb_entry(next, struct hist_entry, rb_node);
  565. next = rb_next(&n->rb_node);
  566. rb_erase(&n->rb_node, tree);
  567. output__insert_entry(n);
  568. }
  569. }
  570. static void register_idle_thread(void)
  571. {
  572. struct thread *thread = threads__findnew(0);
  573. if (thread == NULL ||
  574. thread__set_comm(thread, "[idle]")) {
  575. fprintf(stderr, "problem inserting idle task.\n");
  576. exit(-1);
  577. }
  578. }
  579. static unsigned long total = 0,
  580. total_mmap = 0,
  581. total_comm = 0,
  582. total_fork = 0,
  583. total_unknown = 0;
  584. static int
  585. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  586. {
  587. char level;
  588. int show = 0;
  589. struct dso *dso = NULL;
  590. struct thread *thread = threads__findnew(event->ip.pid);
  591. u64 ip = event->ip.ip;
  592. struct map *map = NULL;
  593. dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  594. (void *)(offset + head),
  595. (void *)(long)(event->header.size),
  596. event->header.misc,
  597. event->ip.pid,
  598. (void *)(long)ip);
  599. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  600. if (thread == NULL) {
  601. fprintf(stderr, "problem processing %d event, skipping it.\n",
  602. event->header.type);
  603. return -1;
  604. }
  605. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  606. show = SHOW_KERNEL;
  607. level = 'k';
  608. dso = kernel_dso;
  609. dprintf(" ...... dso: %s\n", dso->name);
  610. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  611. show = SHOW_USER;
  612. level = '.';
  613. map = thread__find_map(thread, ip);
  614. if (map != NULL) {
  615. ip = map->map_ip(map, ip);
  616. dso = map->dso;
  617. } else {
  618. /*
  619. * If this is outside of all known maps,
  620. * and is a negative address, try to look it
  621. * up in the kernel dso, as it might be a
  622. * vsyscall (which executes in user-mode):
  623. */
  624. if ((long long)ip < 0)
  625. dso = kernel_dso;
  626. }
  627. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  628. } else {
  629. show = SHOW_HV;
  630. level = 'H';
  631. dprintf(" ...... dso: [hypervisor]\n");
  632. }
  633. if (show & show_mask) {
  634. struct symbol *sym = NULL;
  635. if (dso)
  636. sym = dso->find_symbol(dso, ip);
  637. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  638. fprintf(stderr,
  639. "problem incrementing symbol count, skipping event\n");
  640. return -1;
  641. }
  642. }
  643. total++;
  644. return 0;
  645. }
  646. static int
  647. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  648. {
  649. struct thread *thread = threads__findnew(event->mmap.pid);
  650. struct map *map = map__new(&event->mmap);
  651. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  652. (void *)(offset + head),
  653. (void *)(long)(event->header.size),
  654. event->mmap.pid,
  655. (void *)(long)event->mmap.start,
  656. (void *)(long)event->mmap.len,
  657. (void *)(long)event->mmap.pgoff,
  658. event->mmap.filename);
  659. if (thread == NULL || map == NULL) {
  660. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  661. return 0;
  662. }
  663. thread__insert_map(thread, map);
  664. total_mmap++;
  665. return 0;
  666. }
  667. static int
  668. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  669. {
  670. struct thread *thread = threads__findnew(event->comm.pid);
  671. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  672. (void *)(offset + head),
  673. (void *)(long)(event->header.size),
  674. event->comm.comm, event->comm.pid);
  675. if (thread == NULL ||
  676. thread__set_comm(thread, event->comm.comm)) {
  677. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  678. return -1;
  679. }
  680. total_comm++;
  681. return 0;
  682. }
  683. static int
  684. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  685. {
  686. struct thread *thread = threads__findnew(event->fork.pid);
  687. struct thread *parent = threads__findnew(event->fork.ppid);
  688. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  689. (void *)(offset + head),
  690. (void *)(long)(event->header.size),
  691. event->fork.pid, event->fork.ppid);
  692. if (!thread || !parent || thread__fork(thread, parent)) {
  693. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  694. return -1;
  695. }
  696. total_fork++;
  697. return 0;
  698. }
  699. static int
  700. process_event(event_t *event, unsigned long offset, unsigned long head)
  701. {
  702. switch (event->header.type) {
  703. case PERF_EVENT_SAMPLE:
  704. return process_sample_event(event, offset, head);
  705. case PERF_EVENT_MMAP:
  706. return process_mmap_event(event, offset, head);
  707. case PERF_EVENT_COMM:
  708. return process_comm_event(event, offset, head);
  709. case PERF_EVENT_FORK:
  710. return process_fork_event(event, offset, head);
  711. /*
  712. * We dont process them right now but they are fine:
  713. */
  714. case PERF_EVENT_THROTTLE:
  715. case PERF_EVENT_UNTHROTTLE:
  716. return 0;
  717. default:
  718. return -1;
  719. }
  720. return 0;
  721. }
  722. static int
  723. parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
  724. {
  725. char *line = NULL, *tmp, *tmp2;
  726. static const char *prev_line;
  727. static const char *prev_color;
  728. unsigned int offset;
  729. size_t line_len;
  730. s64 line_ip;
  731. int ret;
  732. char *c;
  733. if (getline(&line, &line_len, file) < 0)
  734. return -1;
  735. if (!line)
  736. return -1;
  737. c = strchr(line, '\n');
  738. if (c)
  739. *c = 0;
  740. line_ip = -1;
  741. offset = 0;
  742. ret = -2;
  743. /*
  744. * Strip leading spaces:
  745. */
  746. tmp = line;
  747. while (*tmp) {
  748. if (*tmp != ' ')
  749. break;
  750. tmp++;
  751. }
  752. if (*tmp) {
  753. /*
  754. * Parse hexa addresses followed by ':'
  755. */
  756. line_ip = strtoull(tmp, &tmp2, 16);
  757. if (*tmp2 != ':')
  758. line_ip = -1;
  759. }
  760. if (line_ip != -1) {
  761. const char *path = NULL;
  762. unsigned int hits = 0;
  763. double percent = 0.0;
  764. char *color;
  765. struct sym_ext *sym_ext = sym->priv;
  766. offset = line_ip - start;
  767. if (offset < len)
  768. hits = sym->hist[offset];
  769. if (offset < len && sym_ext) {
  770. path = sym_ext[offset].path;
  771. percent = sym_ext[offset].percent;
  772. } else if (sym->hist_sum)
  773. percent = 100.0 * hits / sym->hist_sum;
  774. color = get_percent_color(percent);
  775. /*
  776. * Also color the filename and line if needed, with
  777. * the same color than the percentage. Don't print it
  778. * twice for close colored ip with the same filename:line
  779. */
  780. if (path) {
  781. if (!prev_line || strcmp(prev_line, path)
  782. || color != prev_color) {
  783. color_fprintf(stdout, color, " %s", path);
  784. prev_line = path;
  785. prev_color = color;
  786. }
  787. }
  788. color_fprintf(stdout, color, " %7.2f", percent);
  789. printf(" : ");
  790. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  791. } else {
  792. if (!*line)
  793. printf(" :\n");
  794. else
  795. printf(" : %s\n", line);
  796. }
  797. return 0;
  798. }
  799. static struct rb_root root_sym_ext;
  800. static void insert_source_line(struct sym_ext *sym_ext)
  801. {
  802. struct sym_ext *iter;
  803. struct rb_node **p = &root_sym_ext.rb_node;
  804. struct rb_node *parent = NULL;
  805. while (*p != NULL) {
  806. parent = *p;
  807. iter = rb_entry(parent, struct sym_ext, node);
  808. if (sym_ext->percent > iter->percent)
  809. p = &(*p)->rb_left;
  810. else
  811. p = &(*p)->rb_right;
  812. }
  813. rb_link_node(&sym_ext->node, parent, p);
  814. rb_insert_color(&sym_ext->node, &root_sym_ext);
  815. }
  816. static void free_source_line(struct symbol *sym, int len)
  817. {
  818. struct sym_ext *sym_ext = sym->priv;
  819. int i;
  820. if (!sym_ext)
  821. return;
  822. for (i = 0; i < len; i++)
  823. free(sym_ext[i].path);
  824. free(sym_ext);
  825. sym->priv = NULL;
  826. root_sym_ext = RB_ROOT;
  827. }
  828. /* Get the filename:line for the colored entries */
  829. static void
  830. get_source_line(struct symbol *sym, u64 start, int len, char *filename)
  831. {
  832. int i;
  833. char cmd[PATH_MAX * 2];
  834. struct sym_ext *sym_ext;
  835. if (!sym->hist_sum)
  836. return;
  837. sym->priv = calloc(len, sizeof(struct sym_ext));
  838. if (!sym->priv)
  839. return;
  840. sym_ext = sym->priv;
  841. for (i = 0; i < len; i++) {
  842. char *path = NULL;
  843. size_t line_len;
  844. u64 offset;
  845. FILE *fp;
  846. sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
  847. if (sym_ext[i].percent <= 0.5)
  848. continue;
  849. offset = start + i;
  850. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  851. fp = popen(cmd, "r");
  852. if (!fp)
  853. continue;
  854. if (getline(&path, &line_len, fp) < 0 || !line_len)
  855. goto next;
  856. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  857. if (!sym_ext[i].path)
  858. goto next;
  859. strcpy(sym_ext[i].path, path);
  860. insert_source_line(&sym_ext[i]);
  861. next:
  862. pclose(fp);
  863. }
  864. }
  865. static void print_summary(char *filename)
  866. {
  867. struct sym_ext *sym_ext;
  868. struct rb_node *node;
  869. printf("\nSorted summary for file %s\n", filename);
  870. printf("----------------------------------------------\n\n");
  871. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  872. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  873. return;
  874. }
  875. node = rb_first(&root_sym_ext);
  876. while (node) {
  877. double percent;
  878. char *color;
  879. char *path;
  880. sym_ext = rb_entry(node, struct sym_ext, node);
  881. percent = sym_ext->percent;
  882. color = get_percent_color(percent);
  883. path = sym_ext->path;
  884. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  885. node = rb_next(node);
  886. }
  887. }
  888. static void annotate_sym(struct dso *dso, struct symbol *sym)
  889. {
  890. char *filename = dso->name, *d_filename;
  891. u64 start, end, len;
  892. char command[PATH_MAX*2];
  893. FILE *file;
  894. if (!filename)
  895. return;
  896. if (sym->module)
  897. filename = sym->module->path;
  898. else if (dso == kernel_dso)
  899. filename = vmlinux;
  900. start = sym->obj_start;
  901. if (!start)
  902. start = sym->start;
  903. if (full_paths)
  904. d_filename = filename;
  905. else
  906. d_filename = basename(filename);
  907. end = start + sym->end - sym->start + 1;
  908. len = sym->end - sym->start;
  909. if (print_line) {
  910. get_source_line(sym, start, len, filename);
  911. print_summary(filename);
  912. }
  913. printf("\n\n------------------------------------------------\n");
  914. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  915. printf("------------------------------------------------\n");
  916. if (verbose >= 2)
  917. printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
  918. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
  919. (u64)start, (u64)end, filename, filename);
  920. if (verbose >= 3)
  921. printf("doing: %s\n", command);
  922. file = popen(command, "r");
  923. if (!file)
  924. return;
  925. while (!feof(file)) {
  926. if (parse_line(file, sym, start, len) < 0)
  927. break;
  928. }
  929. pclose(file);
  930. if (print_line)
  931. free_source_line(sym, len);
  932. }
  933. static void find_annotations(void)
  934. {
  935. struct rb_node *nd;
  936. struct dso *dso;
  937. int count = 0;
  938. list_for_each_entry(dso, &dsos, node) {
  939. for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
  940. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  941. if (sym->hist) {
  942. annotate_sym(dso, sym);
  943. count++;
  944. }
  945. }
  946. }
  947. if (!count)
  948. printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
  949. }
  950. static int __cmd_annotate(void)
  951. {
  952. int ret, rc = EXIT_FAILURE;
  953. unsigned long offset = 0;
  954. unsigned long head = 0;
  955. struct stat stat;
  956. event_t *event;
  957. uint32_t size;
  958. char *buf;
  959. register_idle_thread();
  960. input = open(input_name, O_RDONLY);
  961. if (input < 0) {
  962. perror("failed to open file");
  963. exit(-1);
  964. }
  965. ret = fstat(input, &stat);
  966. if (ret < 0) {
  967. perror("failed to stat file");
  968. exit(-1);
  969. }
  970. if (!stat.st_size) {
  971. fprintf(stderr, "zero-sized file, nothing to do!\n");
  972. exit(0);
  973. }
  974. if (load_kernel() < 0) {
  975. perror("failed to load kernel symbols");
  976. return EXIT_FAILURE;
  977. }
  978. remap:
  979. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  980. MAP_SHARED, input, offset);
  981. if (buf == MAP_FAILED) {
  982. perror("failed to mmap file");
  983. exit(-1);
  984. }
  985. more:
  986. event = (event_t *)(buf + head);
  987. size = event->header.size;
  988. if (!size)
  989. size = 8;
  990. if (head + event->header.size >= page_size * mmap_window) {
  991. unsigned long shift = page_size * (head / page_size);
  992. int ret;
  993. ret = munmap(buf, page_size * mmap_window);
  994. assert(ret == 0);
  995. offset += shift;
  996. head -= shift;
  997. goto remap;
  998. }
  999. size = event->header.size;
  1000. dprintf("%p [%p]: event: %d\n",
  1001. (void *)(offset + head),
  1002. (void *)(long)event->header.size,
  1003. event->header.type);
  1004. if (!size || process_event(event, offset, head) < 0) {
  1005. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1006. (void *)(offset + head),
  1007. (void *)(long)(event->header.size),
  1008. event->header.type);
  1009. total_unknown++;
  1010. /*
  1011. * assume we lost track of the stream, check alignment, and
  1012. * increment a single u64 in the hope to catch on again 'soon'.
  1013. */
  1014. if (unlikely(head & 7))
  1015. head &= ~7ULL;
  1016. size = 8;
  1017. }
  1018. head += size;
  1019. if (offset + head < (unsigned long)stat.st_size)
  1020. goto more;
  1021. rc = EXIT_SUCCESS;
  1022. close(input);
  1023. dprintf(" IP events: %10ld\n", total);
  1024. dprintf(" mmap events: %10ld\n", total_mmap);
  1025. dprintf(" comm events: %10ld\n", total_comm);
  1026. dprintf(" fork events: %10ld\n", total_fork);
  1027. dprintf(" unknown events: %10ld\n", total_unknown);
  1028. if (dump_trace)
  1029. return 0;
  1030. if (verbose >= 3)
  1031. threads__fprintf(stdout);
  1032. if (verbose >= 2)
  1033. dsos__fprintf(stdout);
  1034. collapse__resort();
  1035. output__resort();
  1036. find_annotations();
  1037. return rc;
  1038. }
  1039. static const char * const annotate_usage[] = {
  1040. "perf annotate [<options>] <command>",
  1041. NULL
  1042. };
  1043. static const struct option options[] = {
  1044. OPT_STRING('i', "input", &input_name, "file",
  1045. "input file name"),
  1046. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  1047. "symbol to annotate"),
  1048. OPT_BOOLEAN('v', "verbose", &verbose,
  1049. "be more verbose (show symbol address, etc)"),
  1050. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1051. "dump raw trace in ASCII"),
  1052. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1053. OPT_BOOLEAN('m', "modules", &modules,
  1054. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1055. OPT_BOOLEAN('l', "print-line", &print_line,
  1056. "print matching source lines (may be slow)"),
  1057. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1058. "Don't shorten the displayed pathnames"),
  1059. OPT_END()
  1060. };
  1061. static void setup_sorting(void)
  1062. {
  1063. char *tmp, *tok, *str = strdup(sort_order);
  1064. for (tok = strtok_r(str, ", ", &tmp);
  1065. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1066. if (sort_dimension__add(tok) < 0) {
  1067. error("Unknown --sort key: `%s'", tok);
  1068. usage_with_options(annotate_usage, options);
  1069. }
  1070. }
  1071. free(str);
  1072. }
  1073. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  1074. {
  1075. symbol__init();
  1076. page_size = getpagesize();
  1077. argc = parse_options(argc, argv, options, annotate_usage, 0);
  1078. setup_sorting();
  1079. if (argc) {
  1080. /*
  1081. * Special case: if there's an argument left then assume tha
  1082. * it's a symbol filter:
  1083. */
  1084. if (argc > 1)
  1085. usage_with_options(annotate_usage, options);
  1086. sym_hist_filter = argv[0];
  1087. }
  1088. if (!sym_hist_filter)
  1089. usage_with_options(annotate_usage, options);
  1090. setup_pager();
  1091. return __cmd_annotate();
  1092. }