annotate.c 11 KB

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