annotate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Parts came from builtin-annotate.c, see those files for further
  5. * copyright notes.
  6. *
  7. * Released under the GPL v2. (and only v2, not any later version)
  8. */
  9. #include "util.h"
  10. #include "build-id.h"
  11. #include "color.h"
  12. #include "cache.h"
  13. #include "symbol.h"
  14. #include "debug.h"
  15. #include "annotate.h"
  16. int symbol__alloc_hist(struct symbol *sym, int nevents)
  17. {
  18. struct annotation *notes = symbol__annotation(sym);
  19. notes->sizeof_sym_hist = (sizeof(*notes->histograms) +
  20. (sym->end - sym->start) * sizeof(u64));
  21. notes->histograms = calloc(nevents, notes->sizeof_sym_hist);
  22. notes->nr_histograms = nevents;
  23. return notes->histograms == NULL ? -1 : 0;
  24. }
  25. void symbol__annotate_zero_histograms(struct symbol *sym)
  26. {
  27. struct annotation *notes = symbol__annotation(sym);
  28. if (notes->histograms != NULL)
  29. memset(notes->histograms, 0,
  30. notes->nr_histograms * notes->sizeof_sym_hist);
  31. }
  32. int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  33. int evidx, u64 addr)
  34. {
  35. unsigned offset;
  36. struct annotation *notes;
  37. struct sym_hist *h;
  38. notes = symbol__annotation(sym);
  39. if (notes->histograms == NULL)
  40. return -ENOMEM;
  41. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  42. if (addr >= sym->end)
  43. return 0;
  44. offset = addr - sym->start;
  45. h = annotation__histogram(notes, evidx);
  46. h->sum++;
  47. h->addr[offset]++;
  48. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  49. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  50. addr, addr - sym->start, evidx, h->addr[offset]);
  51. return 0;
  52. }
  53. static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
  54. {
  55. struct objdump_line *self = malloc(sizeof(*self) + privsize);
  56. if (self != NULL) {
  57. self->offset = offset;
  58. self->line = line;
  59. }
  60. return self;
  61. }
  62. void objdump_line__free(struct objdump_line *self)
  63. {
  64. free(self->line);
  65. free(self);
  66. }
  67. static void objdump__add_line(struct list_head *head, struct objdump_line *line)
  68. {
  69. list_add_tail(&line->node, head);
  70. }
  71. struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
  72. struct objdump_line *pos)
  73. {
  74. list_for_each_entry_continue(pos, head, node)
  75. if (pos->offset >= 0)
  76. return pos;
  77. return NULL;
  78. }
  79. static int objdump_line__print(struct objdump_line *oline,
  80. struct list_head *head, struct symbol *sym,
  81. int evidx, u64 len, int min_pcnt,
  82. int printed, int max_lines)
  83. {
  84. static const char *prev_line;
  85. static const char *prev_color;
  86. if (oline->offset != -1) {
  87. const char *path = NULL;
  88. unsigned int hits = 0;
  89. double percent = 0.0;
  90. const char *color;
  91. struct annotation *notes = symbol__annotation(sym);
  92. struct source_line *src_line = notes->src_line;
  93. struct sym_hist *h = annotation__histogram(notes, evidx);
  94. s64 offset = oline->offset;
  95. struct objdump_line *next = objdump__get_next_ip_line(head, oline);
  96. while (offset < (s64)len &&
  97. (next == NULL || offset < next->offset)) {
  98. if (src_line) {
  99. if (path == NULL)
  100. path = src_line[offset].path;
  101. percent += src_line[offset].percent;
  102. } else
  103. hits += h->addr[offset];
  104. ++offset;
  105. }
  106. if (src_line == NULL && h->sum)
  107. percent = 100.0 * hits / h->sum;
  108. if (percent < min_pcnt)
  109. return -1;
  110. if (max_lines && printed >= max_lines)
  111. return 1;
  112. color = get_percent_color(percent);
  113. /*
  114. * Also color the filename and line if needed, with
  115. * the same color than the percentage. Don't print it
  116. * twice for close colored addr with the same filename:line
  117. */
  118. if (path) {
  119. if (!prev_line || strcmp(prev_line, path)
  120. || color != prev_color) {
  121. color_fprintf(stdout, color, " %s", path);
  122. prev_line = path;
  123. prev_color = color;
  124. }
  125. }
  126. color_fprintf(stdout, color, " %7.2f", percent);
  127. printf(" : ");
  128. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
  129. } else if (max_lines && printed >= max_lines)
  130. return 1;
  131. else {
  132. if (!*oline->line)
  133. printf(" :\n");
  134. else
  135. printf(" : %s\n", oline->line);
  136. }
  137. return 0;
  138. }
  139. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, FILE *file,
  140. struct list_head *head, size_t privsize)
  141. {
  142. struct objdump_line *objdump_line;
  143. char *line = NULL, *tmp, *tmp2, *c;
  144. size_t line_len;
  145. s64 line_ip, offset = -1;
  146. if (getline(&line, &line_len, file) < 0)
  147. return -1;
  148. if (!line)
  149. return -1;
  150. while (line_len != 0 && isspace(line[line_len - 1]))
  151. line[--line_len] = '\0';
  152. c = strchr(line, '\n');
  153. if (c)
  154. *c = 0;
  155. line_ip = -1;
  156. /*
  157. * Strip leading spaces:
  158. */
  159. tmp = line;
  160. while (*tmp) {
  161. if (*tmp != ' ')
  162. break;
  163. tmp++;
  164. }
  165. if (*tmp) {
  166. /*
  167. * Parse hexa addresses followed by ':'
  168. */
  169. line_ip = strtoull(tmp, &tmp2, 16);
  170. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  171. line_ip = -1;
  172. }
  173. if (line_ip != -1) {
  174. u64 start = map__rip_2objdump(map, sym->start),
  175. end = map__rip_2objdump(map, sym->end);
  176. offset = line_ip - start;
  177. if (offset < 0 || (u64)line_ip > end)
  178. offset = -1;
  179. }
  180. objdump_line = objdump_line__new(offset, line, privsize);
  181. if (objdump_line == NULL) {
  182. free(line);
  183. return -1;
  184. }
  185. objdump__add_line(head, objdump_line);
  186. return 0;
  187. }
  188. int symbol__annotate(struct symbol *sym, struct map *map,
  189. struct list_head *head, size_t privsize)
  190. {
  191. struct dso *dso = map->dso;
  192. char *filename = dso__build_id_filename(dso, NULL, 0);
  193. bool free_filename = true;
  194. char command[PATH_MAX * 2];
  195. FILE *file;
  196. int err = 0;
  197. char symfs_filename[PATH_MAX];
  198. if (filename) {
  199. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  200. symbol_conf.symfs, filename);
  201. }
  202. if (filename == NULL) {
  203. if (dso->has_build_id) {
  204. pr_err("Can't annotate %s: not enough memory\n",
  205. sym->name);
  206. return -ENOMEM;
  207. }
  208. goto fallback;
  209. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  210. strstr(command, "[kernel.kallsyms]") ||
  211. access(symfs_filename, R_OK)) {
  212. free(filename);
  213. fallback:
  214. /*
  215. * If we don't have build-ids or the build-id file isn't in the
  216. * cache, or is just a kallsyms file, well, lets hope that this
  217. * DSO is the same as when 'perf record' ran.
  218. */
  219. filename = dso->long_name;
  220. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  221. symbol_conf.symfs, filename);
  222. free_filename = false;
  223. }
  224. if (dso->origin == DSO__ORIG_KERNEL) {
  225. if (dso->annotate_warned)
  226. goto out_free_filename;
  227. err = -ENOENT;
  228. dso->annotate_warned = 1;
  229. pr_err("Can't annotate %s: No vmlinux file was found in the "
  230. "path\n", sym->name);
  231. goto out_free_filename;
  232. }
  233. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  234. filename, sym->name, map->unmap_ip(map, sym->start),
  235. map->unmap_ip(map, sym->end));
  236. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  237. dso, dso->long_name, sym, sym->name);
  238. snprintf(command, sizeof(command),
  239. "objdump --start-address=0x%016" PRIx64
  240. " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
  241. map__rip_2objdump(map, sym->start),
  242. map__rip_2objdump(map, sym->end),
  243. symfs_filename, filename);
  244. pr_debug("Executing: %s\n", command);
  245. file = popen(command, "r");
  246. if (!file)
  247. goto out_free_filename;
  248. while (!feof(file))
  249. if (symbol__parse_objdump_line(sym, map, file, head, privsize) < 0)
  250. break;
  251. pclose(file);
  252. out_free_filename:
  253. if (free_filename)
  254. free(filename);
  255. return err;
  256. }
  257. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  258. {
  259. struct source_line *iter;
  260. struct rb_node **p = &root->rb_node;
  261. struct rb_node *parent = NULL;
  262. while (*p != NULL) {
  263. parent = *p;
  264. iter = rb_entry(parent, struct source_line, node);
  265. if (src_line->percent > iter->percent)
  266. p = &(*p)->rb_left;
  267. else
  268. p = &(*p)->rb_right;
  269. }
  270. rb_link_node(&src_line->node, parent, p);
  271. rb_insert_color(&src_line->node, root);
  272. }
  273. static void symbol__free_source_line(struct symbol *sym, int len)
  274. {
  275. struct annotation *notes = symbol__annotation(sym);
  276. struct source_line *src_line = notes->src_line;
  277. int i;
  278. for (i = 0; i < len; i++)
  279. free(src_line[i].path);
  280. free(src_line);
  281. notes->src_line = NULL;
  282. }
  283. /* Get the filename:line for the colored entries */
  284. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  285. int evidx, struct rb_root *root, int len,
  286. const char *filename)
  287. {
  288. u64 start;
  289. int i;
  290. char cmd[PATH_MAX * 2];
  291. struct source_line *src_line;
  292. struct annotation *notes = symbol__annotation(sym);
  293. struct sym_hist *h = annotation__histogram(notes, evidx);
  294. if (!h->sum)
  295. return 0;
  296. src_line = notes->src_line = calloc(len, sizeof(struct source_line));
  297. if (!notes->src_line)
  298. return -1;
  299. start = map->unmap_ip(map, sym->start);
  300. for (i = 0; i < len; i++) {
  301. char *path = NULL;
  302. size_t line_len;
  303. u64 offset;
  304. FILE *fp;
  305. src_line[i].percent = 100.0 * h->addr[i] / h->sum;
  306. if (src_line[i].percent <= 0.5)
  307. continue;
  308. offset = start + i;
  309. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  310. fp = popen(cmd, "r");
  311. if (!fp)
  312. continue;
  313. if (getline(&path, &line_len, fp) < 0 || !line_len)
  314. goto next;
  315. src_line[i].path = malloc(sizeof(char) * line_len + 1);
  316. if (!src_line[i].path)
  317. goto next;
  318. strcpy(src_line[i].path, path);
  319. insert_source_line(root, &src_line[i]);
  320. next:
  321. pclose(fp);
  322. }
  323. return 0;
  324. }
  325. static void print_summary(struct rb_root *root, const char *filename)
  326. {
  327. struct source_line *src_line;
  328. struct rb_node *node;
  329. printf("\nSorted summary for file %s\n", filename);
  330. printf("----------------------------------------------\n\n");
  331. if (RB_EMPTY_ROOT(root)) {
  332. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  333. return;
  334. }
  335. node = rb_first(root);
  336. while (node) {
  337. double percent;
  338. const char *color;
  339. char *path;
  340. src_line = rb_entry(node, struct source_line, node);
  341. percent = src_line->percent;
  342. color = get_percent_color(percent);
  343. path = src_line->path;
  344. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  345. node = rb_next(node);
  346. }
  347. }
  348. static void symbol__annotate_hits(struct symbol *sym, int evidx)
  349. {
  350. struct annotation *notes = symbol__annotation(sym);
  351. struct sym_hist *h = annotation__histogram(notes, evidx);
  352. u64 len = sym->end - sym->start, offset;
  353. for (offset = 0; offset < len; ++offset)
  354. if (h->addr[offset] != 0)
  355. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  356. sym->start + offset, h->addr[offset]);
  357. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  358. }
  359. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  360. struct list_head *head, int evidx, bool full_paths,
  361. int min_pcnt, int max_lines)
  362. {
  363. struct dso *dso = map->dso;
  364. const char *filename = dso->long_name, *d_filename;
  365. struct objdump_line *pos;
  366. int printed = 2;
  367. int more = 0;
  368. u64 len;
  369. if (full_paths)
  370. d_filename = filename;
  371. else
  372. d_filename = basename(filename);
  373. len = sym->end - sym->start;
  374. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  375. printf("------------------------------------------------\n");
  376. if (verbose)
  377. symbol__annotate_hits(sym, evidx);
  378. list_for_each_entry(pos, head, node) {
  379. switch (objdump_line__print(pos, head, sym, evidx, len, min_pcnt,
  380. printed, max_lines)) {
  381. case 0:
  382. ++printed;
  383. break;
  384. case 1:
  385. /* filtered by max_lines */
  386. ++more;
  387. break;
  388. case -1:
  389. default:
  390. /* filtered by min_pcnt */
  391. break;
  392. }
  393. }
  394. return more;
  395. }
  396. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  397. {
  398. struct annotation *notes = symbol__annotation(sym);
  399. struct sym_hist *h = annotation__histogram(notes, evidx);
  400. memset(h, 0, notes->sizeof_sym_hist);
  401. }
  402. void symbol__annotate_decay_histogram(struct symbol *sym,
  403. struct list_head *head, int evidx)
  404. {
  405. struct annotation *notes = symbol__annotation(sym);
  406. struct sym_hist *h = annotation__histogram(notes, evidx);
  407. struct objdump_line *pos;
  408. h->sum = 0;
  409. list_for_each_entry(pos, head, node) {
  410. if (pos->offset != -1) {
  411. h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8;
  412. h->sum += h->addr[pos->offset];
  413. }
  414. }
  415. }
  416. void objdump_line_list__purge(struct list_head *head)
  417. {
  418. struct objdump_line *pos, *n;
  419. list_for_each_entry_safe(pos, n, head, node) {
  420. list_del(&pos->node);
  421. objdump_line__free(pos);
  422. }
  423. }
  424. int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
  425. bool print_lines, bool full_paths, int min_pcnt,
  426. int max_lines)
  427. {
  428. struct dso *dso = map->dso;
  429. const char *filename = dso->long_name;
  430. struct rb_root source_line = RB_ROOT;
  431. LIST_HEAD(head);
  432. u64 len;
  433. if (symbol__annotate(sym, map, &head, 0) < 0)
  434. return -1;
  435. len = sym->end - sym->start;
  436. if (print_lines) {
  437. symbol__get_source_line(sym, map, evidx, &source_line,
  438. len, filename);
  439. print_summary(&source_line, filename);
  440. }
  441. symbol__annotate_printf(sym, map, &head, evidx, full_paths,
  442. min_pcnt, max_lines);
  443. if (print_lines)
  444. symbol__free_source_line(sym, len);
  445. objdump_line_list__purge(&head);
  446. return 0;
  447. }