annotate.c 14 KB

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