annotate.c 15 KB

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