annotate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. struct objdump_line *queue)
  96. {
  97. static const char *prev_line;
  98. static const char *prev_color;
  99. if (oline->offset != -1) {
  100. const char *path = NULL;
  101. unsigned int hits = 0;
  102. double percent = 0.0;
  103. const char *color;
  104. struct annotation *notes = symbol__annotation(sym);
  105. struct source_line *src_line = notes->src->lines;
  106. struct sym_hist *h = annotation__histogram(notes, evidx);
  107. s64 offset = oline->offset;
  108. struct objdump_line *next;
  109. next = objdump__get_next_ip_line(&notes->src->source, oline);
  110. while (offset < (s64)len &&
  111. (next == NULL || offset < next->offset)) {
  112. if (src_line) {
  113. if (path == NULL)
  114. path = src_line[offset].path;
  115. percent += src_line[offset].percent;
  116. } else
  117. hits += h->addr[offset];
  118. ++offset;
  119. }
  120. if (src_line == NULL && h->sum)
  121. percent = 100.0 * hits / h->sum;
  122. if (percent < min_pcnt)
  123. return -1;
  124. if (max_lines && printed >= max_lines)
  125. return 1;
  126. if (queue != NULL) {
  127. list_for_each_entry_from(queue, &notes->src->source, node) {
  128. if (queue == oline)
  129. break;
  130. objdump_line__print(queue, sym, evidx, len,
  131. 0, 0, 1, NULL);
  132. }
  133. }
  134. color = get_percent_color(percent);
  135. /*
  136. * Also color the filename and line if needed, with
  137. * the same color than the percentage. Don't print it
  138. * twice for close colored addr with the same filename:line
  139. */
  140. if (path) {
  141. if (!prev_line || strcmp(prev_line, path)
  142. || color != prev_color) {
  143. color_fprintf(stdout, color, " %s", path);
  144. prev_line = path;
  145. prev_color = color;
  146. }
  147. }
  148. color_fprintf(stdout, color, " %7.2f", percent);
  149. printf(" : ");
  150. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
  151. } else if (max_lines && printed >= max_lines)
  152. return 1;
  153. else {
  154. if (queue)
  155. return -1;
  156. if (!*oline->line)
  157. printf(" :\n");
  158. else
  159. printf(" : %s\n", oline->line);
  160. }
  161. return 0;
  162. }
  163. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  164. FILE *file, size_t privsize)
  165. {
  166. struct annotation *notes = symbol__annotation(sym);
  167. struct objdump_line *objdump_line;
  168. char *line = NULL, *tmp, *tmp2, *c;
  169. size_t line_len;
  170. s64 line_ip, offset = -1;
  171. if (getline(&line, &line_len, file) < 0)
  172. return -1;
  173. if (!line)
  174. return -1;
  175. while (line_len != 0 && isspace(line[line_len - 1]))
  176. line[--line_len] = '\0';
  177. c = strchr(line, '\n');
  178. if (c)
  179. *c = 0;
  180. line_ip = -1;
  181. /*
  182. * Strip leading spaces:
  183. */
  184. tmp = line;
  185. while (*tmp) {
  186. if (*tmp != ' ')
  187. break;
  188. tmp++;
  189. }
  190. if (*tmp) {
  191. /*
  192. * Parse hexa addresses followed by ':'
  193. */
  194. line_ip = strtoull(tmp, &tmp2, 16);
  195. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  196. line_ip = -1;
  197. }
  198. if (line_ip != -1) {
  199. u64 start = map__rip_2objdump(map, sym->start),
  200. end = map__rip_2objdump(map, sym->end);
  201. offset = line_ip - start;
  202. if (offset < 0 || (u64)line_ip > end)
  203. offset = -1;
  204. }
  205. objdump_line = objdump_line__new(offset, line, privsize);
  206. if (objdump_line == NULL) {
  207. free(line);
  208. return -1;
  209. }
  210. objdump__add_line(&notes->src->source, objdump_line);
  211. return 0;
  212. }
  213. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  214. {
  215. struct dso *dso = map->dso;
  216. char *filename = dso__build_id_filename(dso, NULL, 0);
  217. bool free_filename = true;
  218. char command[PATH_MAX * 2];
  219. FILE *file;
  220. int err = 0;
  221. char symfs_filename[PATH_MAX];
  222. if (filename) {
  223. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  224. symbol_conf.symfs, filename);
  225. }
  226. if (filename == NULL) {
  227. if (dso->has_build_id) {
  228. pr_err("Can't annotate %s: not enough memory\n",
  229. sym->name);
  230. return -ENOMEM;
  231. }
  232. goto fallback;
  233. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  234. strstr(command, "[kernel.kallsyms]") ||
  235. access(symfs_filename, R_OK)) {
  236. free(filename);
  237. fallback:
  238. /*
  239. * If we don't have build-ids or the build-id file isn't in the
  240. * cache, or is just a kallsyms file, well, lets hope that this
  241. * DSO is the same as when 'perf record' ran.
  242. */
  243. filename = dso->long_name;
  244. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  245. symbol_conf.symfs, filename);
  246. free_filename = false;
  247. }
  248. if (dso->symtab_type == SYMTAB__KALLSYMS) {
  249. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  250. char *build_id_msg = NULL;
  251. if (dso->annotate_warned)
  252. goto out_free_filename;
  253. if (dso->has_build_id) {
  254. build_id__sprintf(dso->build_id,
  255. sizeof(dso->build_id), bf + 15);
  256. build_id_msg = bf;
  257. }
  258. err = -ENOENT;
  259. dso->annotate_warned = 1;
  260. pr_err("Can't annotate %s: No vmlinux file%s was found in the "
  261. "path.\nPlease use 'perf buildid-cache -av vmlinux' or "
  262. "--vmlinux vmlinux.\n",
  263. sym->name, build_id_msg ?: "");
  264. goto out_free_filename;
  265. }
  266. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  267. filename, sym->name, map->unmap_ip(map, sym->start),
  268. map->unmap_ip(map, sym->end));
  269. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  270. dso, dso->long_name, sym, sym->name);
  271. snprintf(command, sizeof(command),
  272. "objdump --start-address=0x%016" PRIx64
  273. " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
  274. map__rip_2objdump(map, sym->start),
  275. map__rip_2objdump(map, sym->end),
  276. symfs_filename, filename);
  277. pr_debug("Executing: %s\n", command);
  278. file = popen(command, "r");
  279. if (!file)
  280. goto out_free_filename;
  281. while (!feof(file))
  282. if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
  283. break;
  284. pclose(file);
  285. out_free_filename:
  286. if (free_filename)
  287. free(filename);
  288. return err;
  289. }
  290. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  291. {
  292. struct source_line *iter;
  293. struct rb_node **p = &root->rb_node;
  294. struct rb_node *parent = NULL;
  295. while (*p != NULL) {
  296. parent = *p;
  297. iter = rb_entry(parent, struct source_line, node);
  298. if (src_line->percent > iter->percent)
  299. p = &(*p)->rb_left;
  300. else
  301. p = &(*p)->rb_right;
  302. }
  303. rb_link_node(&src_line->node, parent, p);
  304. rb_insert_color(&src_line->node, root);
  305. }
  306. static void symbol__free_source_line(struct symbol *sym, int len)
  307. {
  308. struct annotation *notes = symbol__annotation(sym);
  309. struct source_line *src_line = notes->src->lines;
  310. int i;
  311. for (i = 0; i < len; i++)
  312. free(src_line[i].path);
  313. free(src_line);
  314. notes->src->lines = NULL;
  315. }
  316. /* Get the filename:line for the colored entries */
  317. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  318. int evidx, struct rb_root *root, int len,
  319. const char *filename)
  320. {
  321. u64 start;
  322. int i;
  323. char cmd[PATH_MAX * 2];
  324. struct source_line *src_line;
  325. struct annotation *notes = symbol__annotation(sym);
  326. struct sym_hist *h = annotation__histogram(notes, evidx);
  327. if (!h->sum)
  328. return 0;
  329. src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
  330. if (!notes->src->lines)
  331. return -1;
  332. start = map->unmap_ip(map, sym->start);
  333. for (i = 0; i < len; i++) {
  334. char *path = NULL;
  335. size_t line_len;
  336. u64 offset;
  337. FILE *fp;
  338. src_line[i].percent = 100.0 * h->addr[i] / h->sum;
  339. if (src_line[i].percent <= 0.5)
  340. continue;
  341. offset = start + i;
  342. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  343. fp = popen(cmd, "r");
  344. if (!fp)
  345. continue;
  346. if (getline(&path, &line_len, fp) < 0 || !line_len)
  347. goto next;
  348. src_line[i].path = malloc(sizeof(char) * line_len + 1);
  349. if (!src_line[i].path)
  350. goto next;
  351. strcpy(src_line[i].path, path);
  352. insert_source_line(root, &src_line[i]);
  353. next:
  354. pclose(fp);
  355. }
  356. return 0;
  357. }
  358. static void print_summary(struct rb_root *root, const char *filename)
  359. {
  360. struct source_line *src_line;
  361. struct rb_node *node;
  362. printf("\nSorted summary for file %s\n", filename);
  363. printf("----------------------------------------------\n\n");
  364. if (RB_EMPTY_ROOT(root)) {
  365. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  366. return;
  367. }
  368. node = rb_first(root);
  369. while (node) {
  370. double percent;
  371. const char *color;
  372. char *path;
  373. src_line = rb_entry(node, struct source_line, node);
  374. percent = src_line->percent;
  375. color = get_percent_color(percent);
  376. path = src_line->path;
  377. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  378. node = rb_next(node);
  379. }
  380. }
  381. static void symbol__annotate_hits(struct symbol *sym, int evidx)
  382. {
  383. struct annotation *notes = symbol__annotation(sym);
  384. struct sym_hist *h = annotation__histogram(notes, evidx);
  385. u64 len = sym->end - sym->start, offset;
  386. for (offset = 0; offset < len; ++offset)
  387. if (h->addr[offset] != 0)
  388. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  389. sym->start + offset, h->addr[offset]);
  390. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  391. }
  392. int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
  393. bool full_paths, int min_pcnt, int max_lines,
  394. int context)
  395. {
  396. struct dso *dso = map->dso;
  397. const char *filename = dso->long_name, *d_filename;
  398. struct annotation *notes = symbol__annotation(sym);
  399. struct objdump_line *pos, *queue = NULL;
  400. int printed = 2, queue_len = 0;
  401. int more = 0;
  402. u64 len;
  403. if (full_paths)
  404. d_filename = filename;
  405. else
  406. d_filename = basename(filename);
  407. len = sym->end - sym->start;
  408. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  409. printf("------------------------------------------------\n");
  410. if (verbose)
  411. symbol__annotate_hits(sym, evidx);
  412. list_for_each_entry(pos, &notes->src->source, node) {
  413. if (context && queue == NULL) {
  414. queue = pos;
  415. queue_len = 0;
  416. }
  417. switch (objdump_line__print(pos, sym, evidx, len, min_pcnt,
  418. printed, max_lines, queue)) {
  419. case 0:
  420. ++printed;
  421. if (context) {
  422. printed += queue_len;
  423. queue = NULL;
  424. queue_len = 0;
  425. }
  426. break;
  427. case 1:
  428. /* filtered by max_lines */
  429. ++more;
  430. break;
  431. case -1:
  432. default:
  433. /*
  434. * Filtered by min_pcnt or non IP lines when
  435. * context != 0
  436. */
  437. if (!context)
  438. break;
  439. if (queue_len == context)
  440. queue = list_entry(queue->node.next, typeof(*queue), node);
  441. else
  442. ++queue_len;
  443. break;
  444. }
  445. }
  446. return more;
  447. }
  448. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  449. {
  450. struct annotation *notes = symbol__annotation(sym);
  451. struct sym_hist *h = annotation__histogram(notes, evidx);
  452. memset(h, 0, notes->src->sizeof_sym_hist);
  453. }
  454. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  455. {
  456. struct annotation *notes = symbol__annotation(sym);
  457. struct sym_hist *h = annotation__histogram(notes, evidx);
  458. struct objdump_line *pos;
  459. int len = sym->end - sym->start;
  460. h->sum = 0;
  461. list_for_each_entry(pos, &notes->src->source, node) {
  462. if (pos->offset != -1 && pos->offset < len) {
  463. h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8;
  464. h->sum += h->addr[pos->offset];
  465. }
  466. }
  467. }
  468. void objdump_line_list__purge(struct list_head *head)
  469. {
  470. struct objdump_line *pos, *n;
  471. list_for_each_entry_safe(pos, n, head, node) {
  472. list_del(&pos->node);
  473. objdump_line__free(pos);
  474. }
  475. }
  476. int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
  477. bool print_lines, bool full_paths, int min_pcnt,
  478. int max_lines)
  479. {
  480. struct dso *dso = map->dso;
  481. const char *filename = dso->long_name;
  482. struct rb_root source_line = RB_ROOT;
  483. u64 len;
  484. if (symbol__annotate(sym, map, 0) < 0)
  485. return -1;
  486. len = sym->end - sym->start;
  487. if (print_lines) {
  488. symbol__get_source_line(sym, map, evidx, &source_line,
  489. len, filename);
  490. print_summary(&source_line, filename);
  491. }
  492. symbol__annotate_printf(sym, map, evidx, full_paths,
  493. min_pcnt, max_lines, 0);
  494. if (print_lines)
  495. symbol__free_source_line(sym, len);
  496. objdump_line_list__purge(&symbol__annotation(sym)->src->source);
  497. return 0;
  498. }