annotate.c 16 KB

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