annotate.c 11 KB

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