annotate.c 15 KB

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