annotate.c 13 KB

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