builtin-annotate.c 25 KB

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