annotate.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  19. struct ins_operands *ops)
  20. {
  21. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
  22. }
  23. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  24. struct ins_operands *ops)
  25. {
  26. if (ins->ops->scnprintf)
  27. return ins->ops->scnprintf(ins, bf, size, ops);
  28. return ins__raw_scnprintf(ins, bf, size, ops);
  29. }
  30. static int call__parse(struct ins_operands *ops)
  31. {
  32. char *endptr, *tok, *name;
  33. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  34. name = strchr(endptr, '<');
  35. if (name == NULL)
  36. goto indirect_call;
  37. name++;
  38. tok = strchr(name, '>');
  39. if (tok == NULL)
  40. return -1;
  41. *tok = '\0';
  42. ops->target.name = strdup(name);
  43. *tok = '>';
  44. return ops->target.name == NULL ? -1 : 0;
  45. indirect_call:
  46. tok = strchr(endptr, '(');
  47. if (tok != NULL) {
  48. ops->target.addr = 0;
  49. return 0;
  50. }
  51. tok = strchr(endptr, '*');
  52. if (tok == NULL)
  53. return -1;
  54. ops->target.addr = strtoull(tok + 1, NULL, 16);
  55. return 0;
  56. }
  57. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  58. struct ins_operands *ops)
  59. {
  60. if (ops->target.name)
  61. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
  62. if (ops->target.addr == 0)
  63. return ins__raw_scnprintf(ins, bf, size, ops);
  64. return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
  65. }
  66. static struct ins_ops call_ops = {
  67. .parse = call__parse,
  68. .scnprintf = call__scnprintf,
  69. };
  70. bool ins__is_call(const struct ins *ins)
  71. {
  72. return ins->ops == &call_ops;
  73. }
  74. static int jump__parse(struct ins_operands *ops)
  75. {
  76. const char *s = strchr(ops->raw, '+');
  77. ops->target.addr = strtoll(ops->raw, NULL, 16);
  78. if (s++ != NULL)
  79. ops->target.offset = strtoll(s, NULL, 16);
  80. else
  81. ops->target.offset = UINT64_MAX;
  82. return 0;
  83. }
  84. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  85. struct ins_operands *ops)
  86. {
  87. return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
  88. }
  89. static struct ins_ops jump_ops = {
  90. .parse = jump__parse,
  91. .scnprintf = jump__scnprintf,
  92. };
  93. bool ins__is_jump(const struct ins *ins)
  94. {
  95. return ins->ops == &jump_ops;
  96. }
  97. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  98. {
  99. char *endptr, *name, *t;
  100. if (strstr(raw, "(%rip)") == NULL)
  101. return 0;
  102. *addrp = strtoull(comment, &endptr, 16);
  103. name = strchr(endptr, '<');
  104. if (name == NULL)
  105. return -1;
  106. name++;
  107. t = strchr(name, '>');
  108. if (t == NULL)
  109. return 0;
  110. *t = '\0';
  111. *namep = strdup(name);
  112. *t = '>';
  113. return 0;
  114. }
  115. static int mov__parse(struct ins_operands *ops)
  116. {
  117. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  118. if (s == NULL)
  119. return -1;
  120. *s = '\0';
  121. ops->source.raw = strdup(ops->raw);
  122. *s = ',';
  123. if (ops->source.raw == NULL)
  124. return -1;
  125. target = ++s;
  126. while (s[0] != '\0' && !isspace(s[0]))
  127. ++s;
  128. prev = *s;
  129. *s = '\0';
  130. ops->target.raw = strdup(target);
  131. *s = prev;
  132. if (ops->target.raw == NULL)
  133. goto out_free_source;
  134. comment = strchr(s, '#');
  135. if (comment == NULL)
  136. return 0;
  137. while (comment[0] != '\0' && isspace(comment[0]))
  138. ++comment;
  139. comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
  140. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  141. return 0;
  142. out_free_source:
  143. free(ops->source.raw);
  144. ops->source.raw = NULL;
  145. return -1;
  146. }
  147. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  148. struct ins_operands *ops)
  149. {
  150. return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
  151. ops->source.name ?: ops->source.raw,
  152. ops->target.name ?: ops->target.raw);
  153. }
  154. static struct ins_ops mov_ops = {
  155. .parse = mov__parse,
  156. .scnprintf = mov__scnprintf,
  157. };
  158. static int dec__parse(struct ins_operands *ops)
  159. {
  160. char *target, *comment, *s, prev;
  161. target = s = ops->raw;
  162. while (s[0] != '\0' && !isspace(s[0]))
  163. ++s;
  164. prev = *s;
  165. *s = '\0';
  166. ops->target.raw = strdup(target);
  167. *s = prev;
  168. if (ops->target.raw == NULL)
  169. return -1;
  170. comment = strchr(s, '#');
  171. if (comment == NULL)
  172. return 0;
  173. while (comment[0] != '\0' && isspace(comment[0]))
  174. ++comment;
  175. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  176. return 0;
  177. }
  178. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  179. struct ins_operands *ops)
  180. {
  181. return scnprintf(bf, size, "%-6.6s %s", ins->name,
  182. ops->target.name ?: ops->target.raw);
  183. }
  184. static struct ins_ops dec_ops = {
  185. .parse = dec__parse,
  186. .scnprintf = dec__scnprintf,
  187. };
  188. static int nop__scnprintf(struct ins *ins __used, char *bf, size_t size,
  189. struct ins_operands *ops __used)
  190. {
  191. return scnprintf(bf, size, "%-6.6s", "nop");
  192. }
  193. static struct ins_ops nop_ops = {
  194. .scnprintf = nop__scnprintf,
  195. };
  196. /*
  197. * Must be sorted by name!
  198. */
  199. static struct ins instructions[] = {
  200. { .name = "add", .ops = &mov_ops, },
  201. { .name = "addl", .ops = &mov_ops, },
  202. { .name = "addq", .ops = &mov_ops, },
  203. { .name = "addw", .ops = &mov_ops, },
  204. { .name = "and", .ops = &mov_ops, },
  205. { .name = "call", .ops = &call_ops, },
  206. { .name = "callq", .ops = &call_ops, },
  207. { .name = "cmp", .ops = &mov_ops, },
  208. { .name = "cmpb", .ops = &mov_ops, },
  209. { .name = "cmpl", .ops = &mov_ops, },
  210. { .name = "cmpq", .ops = &mov_ops, },
  211. { .name = "cmpw", .ops = &mov_ops, },
  212. { .name = "cmpxch", .ops = &mov_ops, },
  213. { .name = "dec", .ops = &dec_ops, },
  214. { .name = "decl", .ops = &dec_ops, },
  215. { .name = "imul", .ops = &mov_ops, },
  216. { .name = "inc", .ops = &dec_ops, },
  217. { .name = "incl", .ops = &dec_ops, },
  218. { .name = "ja", .ops = &jump_ops, },
  219. { .name = "jae", .ops = &jump_ops, },
  220. { .name = "jb", .ops = &jump_ops, },
  221. { .name = "jbe", .ops = &jump_ops, },
  222. { .name = "jc", .ops = &jump_ops, },
  223. { .name = "jcxz", .ops = &jump_ops, },
  224. { .name = "je", .ops = &jump_ops, },
  225. { .name = "jecxz", .ops = &jump_ops, },
  226. { .name = "jg", .ops = &jump_ops, },
  227. { .name = "jge", .ops = &jump_ops, },
  228. { .name = "jl", .ops = &jump_ops, },
  229. { .name = "jle", .ops = &jump_ops, },
  230. { .name = "jmp", .ops = &jump_ops, },
  231. { .name = "jmpq", .ops = &jump_ops, },
  232. { .name = "jna", .ops = &jump_ops, },
  233. { .name = "jnae", .ops = &jump_ops, },
  234. { .name = "jnb", .ops = &jump_ops, },
  235. { .name = "jnbe", .ops = &jump_ops, },
  236. { .name = "jnc", .ops = &jump_ops, },
  237. { .name = "jne", .ops = &jump_ops, },
  238. { .name = "jng", .ops = &jump_ops, },
  239. { .name = "jnge", .ops = &jump_ops, },
  240. { .name = "jnl", .ops = &jump_ops, },
  241. { .name = "jnle", .ops = &jump_ops, },
  242. { .name = "jno", .ops = &jump_ops, },
  243. { .name = "jnp", .ops = &jump_ops, },
  244. { .name = "jns", .ops = &jump_ops, },
  245. { .name = "jnz", .ops = &jump_ops, },
  246. { .name = "jo", .ops = &jump_ops, },
  247. { .name = "jp", .ops = &jump_ops, },
  248. { .name = "jpe", .ops = &jump_ops, },
  249. { .name = "jpo", .ops = &jump_ops, },
  250. { .name = "jrcxz", .ops = &jump_ops, },
  251. { .name = "js", .ops = &jump_ops, },
  252. { .name = "jz", .ops = &jump_ops, },
  253. { .name = "lea", .ops = &mov_ops, },
  254. { .name = "mov", .ops = &mov_ops, },
  255. { .name = "movb", .ops = &mov_ops, },
  256. { .name = "movdqa",.ops = &mov_ops, },
  257. { .name = "movl", .ops = &mov_ops, },
  258. { .name = "movq", .ops = &mov_ops, },
  259. { .name = "movslq", .ops = &mov_ops, },
  260. { .name = "movzbl", .ops = &mov_ops, },
  261. { .name = "movzwl", .ops = &mov_ops, },
  262. { .name = "nop", .ops = &nop_ops, },
  263. { .name = "nopl", .ops = &nop_ops, },
  264. { .name = "nopw", .ops = &nop_ops, },
  265. { .name = "or", .ops = &mov_ops, },
  266. { .name = "orl", .ops = &mov_ops, },
  267. { .name = "test", .ops = &mov_ops, },
  268. { .name = "testb", .ops = &mov_ops, },
  269. { .name = "testl", .ops = &mov_ops, },
  270. };
  271. static int ins__cmp(const void *name, const void *insp)
  272. {
  273. const struct ins *ins = insp;
  274. return strcmp(name, ins->name);
  275. }
  276. static struct ins *ins__find(const char *name)
  277. {
  278. const int nmemb = ARRAY_SIZE(instructions);
  279. return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
  280. }
  281. int symbol__annotate_init(struct map *map __used, struct symbol *sym)
  282. {
  283. struct annotation *notes = symbol__annotation(sym);
  284. pthread_mutex_init(&notes->lock, NULL);
  285. return 0;
  286. }
  287. int symbol__alloc_hist(struct symbol *sym)
  288. {
  289. struct annotation *notes = symbol__annotation(sym);
  290. const size_t size = symbol__size(sym);
  291. size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
  292. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  293. if (notes->src == NULL)
  294. return -1;
  295. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  296. notes->src->nr_histograms = symbol_conf.nr_events;
  297. INIT_LIST_HEAD(&notes->src->source);
  298. return 0;
  299. }
  300. void symbol__annotate_zero_histograms(struct symbol *sym)
  301. {
  302. struct annotation *notes = symbol__annotation(sym);
  303. pthread_mutex_lock(&notes->lock);
  304. if (notes->src != NULL)
  305. memset(notes->src->histograms, 0,
  306. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  307. pthread_mutex_unlock(&notes->lock);
  308. }
  309. int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  310. int evidx, u64 addr)
  311. {
  312. unsigned offset;
  313. struct annotation *notes;
  314. struct sym_hist *h;
  315. notes = symbol__annotation(sym);
  316. if (notes->src == NULL)
  317. return -ENOMEM;
  318. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  319. if (addr < sym->start || addr > sym->end)
  320. return -ERANGE;
  321. offset = addr - sym->start;
  322. h = annotation__histogram(notes, evidx);
  323. h->sum++;
  324. h->addr[offset]++;
  325. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  326. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  327. addr, addr - sym->start, evidx, h->addr[offset]);
  328. return 0;
  329. }
  330. static void disasm_line__init_ins(struct disasm_line *dl)
  331. {
  332. dl->ins = ins__find(dl->name);
  333. if (dl->ins == NULL)
  334. return;
  335. if (!dl->ins->ops)
  336. return;
  337. if (dl->ins->ops->parse)
  338. dl->ins->ops->parse(&dl->ops);
  339. }
  340. static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
  341. {
  342. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  343. if (dl != NULL) {
  344. dl->offset = offset;
  345. dl->line = strdup(line);
  346. if (dl->line == NULL)
  347. goto out_delete;
  348. if (offset != -1) {
  349. char *name = dl->line, tmp;
  350. while (isspace(name[0]))
  351. ++name;
  352. if (name[0] == '\0')
  353. goto out_delete;
  354. dl->ops.raw = name + 1;
  355. while (dl->ops.raw[0] != '\0' &&
  356. !isspace(dl->ops.raw[0]))
  357. ++dl->ops.raw;
  358. tmp = dl->ops.raw[0];
  359. dl->ops.raw[0] = '\0';
  360. dl->name = strdup(name);
  361. if (dl->name == NULL)
  362. goto out_free_line;
  363. dl->ops.raw[0] = tmp;
  364. if (dl->ops.raw[0] != '\0') {
  365. dl->ops.raw++;
  366. while (isspace(dl->ops.raw[0]))
  367. ++dl->ops.raw;
  368. }
  369. disasm_line__init_ins(dl);
  370. }
  371. }
  372. return dl;
  373. out_free_line:
  374. free(dl->line);
  375. out_delete:
  376. free(dl);
  377. return NULL;
  378. }
  379. void disasm_line__free(struct disasm_line *dl)
  380. {
  381. free(dl->line);
  382. free(dl->name);
  383. free(dl->ops.source.raw);
  384. free(dl->ops.source.name);
  385. free(dl->ops.target.raw);
  386. free(dl->ops.target.name);
  387. free(dl);
  388. }
  389. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  390. {
  391. if (raw || !dl->ins)
  392. return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
  393. return ins__scnprintf(dl->ins, bf, size, &dl->ops);
  394. }
  395. static void disasm__add(struct list_head *head, struct disasm_line *line)
  396. {
  397. list_add_tail(&line->node, head);
  398. }
  399. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  400. {
  401. list_for_each_entry_continue(pos, head, node)
  402. if (pos->offset >= 0)
  403. return pos;
  404. return NULL;
  405. }
  406. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  407. int evidx, u64 len, int min_pcnt, int printed,
  408. int max_lines, struct disasm_line *queue)
  409. {
  410. static const char *prev_line;
  411. static const char *prev_color;
  412. if (dl->offset != -1) {
  413. const char *path = NULL;
  414. unsigned int hits = 0;
  415. double percent = 0.0;
  416. const char *color;
  417. struct annotation *notes = symbol__annotation(sym);
  418. struct source_line *src_line = notes->src->lines;
  419. struct sym_hist *h = annotation__histogram(notes, evidx);
  420. s64 offset = dl->offset;
  421. const u64 addr = start + offset;
  422. struct disasm_line *next;
  423. next = disasm__get_next_ip_line(&notes->src->source, dl);
  424. while (offset < (s64)len &&
  425. (next == NULL || offset < next->offset)) {
  426. if (src_line) {
  427. if (path == NULL)
  428. path = src_line[offset].path;
  429. percent += src_line[offset].percent;
  430. } else
  431. hits += h->addr[offset];
  432. ++offset;
  433. }
  434. if (src_line == NULL && h->sum)
  435. percent = 100.0 * hits / h->sum;
  436. if (percent < min_pcnt)
  437. return -1;
  438. if (max_lines && printed >= max_lines)
  439. return 1;
  440. if (queue != NULL) {
  441. list_for_each_entry_from(queue, &notes->src->source, node) {
  442. if (queue == dl)
  443. break;
  444. disasm_line__print(queue, sym, start, evidx, len,
  445. 0, 0, 1, NULL);
  446. }
  447. }
  448. color = get_percent_color(percent);
  449. /*
  450. * Also color the filename and line if needed, with
  451. * the same color than the percentage. Don't print it
  452. * twice for close colored addr with the same filename:line
  453. */
  454. if (path) {
  455. if (!prev_line || strcmp(prev_line, path)
  456. || color != prev_color) {
  457. color_fprintf(stdout, color, " %s", path);
  458. prev_line = path;
  459. prev_color = color;
  460. }
  461. }
  462. color_fprintf(stdout, color, " %7.2f", percent);
  463. printf(" : ");
  464. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  465. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  466. } else if (max_lines && printed >= max_lines)
  467. return 1;
  468. else {
  469. if (queue)
  470. return -1;
  471. if (!*dl->line)
  472. printf(" :\n");
  473. else
  474. printf(" : %s\n", dl->line);
  475. }
  476. return 0;
  477. }
  478. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  479. FILE *file, size_t privsize)
  480. {
  481. struct annotation *notes = symbol__annotation(sym);
  482. struct disasm_line *dl;
  483. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  484. size_t line_len;
  485. s64 line_ip, offset = -1;
  486. if (getline(&line, &line_len, file) < 0)
  487. return -1;
  488. if (!line)
  489. return -1;
  490. while (line_len != 0 && isspace(line[line_len - 1]))
  491. line[--line_len] = '\0';
  492. c = strchr(line, '\n');
  493. if (c)
  494. *c = 0;
  495. line_ip = -1;
  496. parsed_line = line;
  497. /*
  498. * Strip leading spaces:
  499. */
  500. tmp = line;
  501. while (*tmp) {
  502. if (*tmp != ' ')
  503. break;
  504. tmp++;
  505. }
  506. if (*tmp) {
  507. /*
  508. * Parse hexa addresses followed by ':'
  509. */
  510. line_ip = strtoull(tmp, &tmp2, 16);
  511. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  512. line_ip = -1;
  513. }
  514. if (line_ip != -1) {
  515. u64 start = map__rip_2objdump(map, sym->start),
  516. end = map__rip_2objdump(map, sym->end);
  517. offset = line_ip - start;
  518. if (offset < 0 || (u64)line_ip > end)
  519. offset = -1;
  520. else
  521. parsed_line = tmp2 + 1;
  522. }
  523. dl = disasm_line__new(offset, parsed_line, privsize);
  524. free(line);
  525. if (dl == NULL)
  526. return -1;
  527. disasm__add(&notes->src->source, dl);
  528. return 0;
  529. }
  530. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  531. {
  532. struct dso *dso = map->dso;
  533. char *filename = dso__build_id_filename(dso, NULL, 0);
  534. bool free_filename = true;
  535. char command[PATH_MAX * 2];
  536. FILE *file;
  537. int err = 0;
  538. char symfs_filename[PATH_MAX];
  539. if (filename) {
  540. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  541. symbol_conf.symfs, filename);
  542. }
  543. if (filename == NULL) {
  544. if (dso->has_build_id) {
  545. pr_err("Can't annotate %s: not enough memory\n",
  546. sym->name);
  547. return -ENOMEM;
  548. }
  549. goto fallback;
  550. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  551. strstr(command, "[kernel.kallsyms]") ||
  552. access(symfs_filename, R_OK)) {
  553. free(filename);
  554. fallback:
  555. /*
  556. * If we don't have build-ids or the build-id file isn't in the
  557. * cache, or is just a kallsyms file, well, lets hope that this
  558. * DSO is the same as when 'perf record' ran.
  559. */
  560. filename = dso->long_name;
  561. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  562. symbol_conf.symfs, filename);
  563. free_filename = false;
  564. }
  565. if (dso->symtab_type == SYMTAB__KALLSYMS) {
  566. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  567. char *build_id_msg = NULL;
  568. if (dso->annotate_warned)
  569. goto out_free_filename;
  570. if (dso->has_build_id) {
  571. build_id__sprintf(dso->build_id,
  572. sizeof(dso->build_id), bf + 15);
  573. build_id_msg = bf;
  574. }
  575. err = -ENOENT;
  576. dso->annotate_warned = 1;
  577. pr_err("Can't annotate %s:\n\n"
  578. "No vmlinux file%s\nwas found in the path.\n\n"
  579. "Please use:\n\n"
  580. " perf buildid-cache -av vmlinux\n\n"
  581. "or:\n\n"
  582. " --vmlinux vmlinux\n",
  583. sym->name, build_id_msg ?: "");
  584. goto out_free_filename;
  585. }
  586. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  587. filename, sym->name, map->unmap_ip(map, sym->start),
  588. map->unmap_ip(map, sym->end));
  589. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  590. dso, dso->long_name, sym, sym->name);
  591. snprintf(command, sizeof(command),
  592. "objdump %s%s --start-address=0x%016" PRIx64
  593. " --stop-address=0x%016" PRIx64
  594. " -d %s %s -C %s|grep -v %s|expand",
  595. disassembler_style ? "-M " : "",
  596. disassembler_style ? disassembler_style : "",
  597. map__rip_2objdump(map, sym->start),
  598. map__rip_2objdump(map, sym->end+1),
  599. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  600. symbol_conf.annotate_src ? "-S" : "",
  601. symfs_filename, filename);
  602. pr_debug("Executing: %s\n", command);
  603. file = popen(command, "r");
  604. if (!file)
  605. goto out_free_filename;
  606. while (!feof(file))
  607. if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
  608. break;
  609. pclose(file);
  610. out_free_filename:
  611. if (free_filename)
  612. free(filename);
  613. return err;
  614. }
  615. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  616. {
  617. struct source_line *iter;
  618. struct rb_node **p = &root->rb_node;
  619. struct rb_node *parent = NULL;
  620. while (*p != NULL) {
  621. parent = *p;
  622. iter = rb_entry(parent, struct source_line, node);
  623. if (src_line->percent > iter->percent)
  624. p = &(*p)->rb_left;
  625. else
  626. p = &(*p)->rb_right;
  627. }
  628. rb_link_node(&src_line->node, parent, p);
  629. rb_insert_color(&src_line->node, root);
  630. }
  631. static void symbol__free_source_line(struct symbol *sym, int len)
  632. {
  633. struct annotation *notes = symbol__annotation(sym);
  634. struct source_line *src_line = notes->src->lines;
  635. int i;
  636. for (i = 0; i < len; i++)
  637. free(src_line[i].path);
  638. free(src_line);
  639. notes->src->lines = NULL;
  640. }
  641. /* Get the filename:line for the colored entries */
  642. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  643. int evidx, struct rb_root *root, int len,
  644. const char *filename)
  645. {
  646. u64 start;
  647. int i;
  648. char cmd[PATH_MAX * 2];
  649. struct source_line *src_line;
  650. struct annotation *notes = symbol__annotation(sym);
  651. struct sym_hist *h = annotation__histogram(notes, evidx);
  652. if (!h->sum)
  653. return 0;
  654. src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
  655. if (!notes->src->lines)
  656. return -1;
  657. start = map__rip_2objdump(map, sym->start);
  658. for (i = 0; i < len; i++) {
  659. char *path = NULL;
  660. size_t line_len;
  661. u64 offset;
  662. FILE *fp;
  663. src_line[i].percent = 100.0 * h->addr[i] / h->sum;
  664. if (src_line[i].percent <= 0.5)
  665. continue;
  666. offset = start + i;
  667. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  668. fp = popen(cmd, "r");
  669. if (!fp)
  670. continue;
  671. if (getline(&path, &line_len, fp) < 0 || !line_len)
  672. goto next;
  673. src_line[i].path = malloc(sizeof(char) * line_len + 1);
  674. if (!src_line[i].path)
  675. goto next;
  676. strcpy(src_line[i].path, path);
  677. insert_source_line(root, &src_line[i]);
  678. next:
  679. pclose(fp);
  680. }
  681. return 0;
  682. }
  683. static void print_summary(struct rb_root *root, const char *filename)
  684. {
  685. struct source_line *src_line;
  686. struct rb_node *node;
  687. printf("\nSorted summary for file %s\n", filename);
  688. printf("----------------------------------------------\n\n");
  689. if (RB_EMPTY_ROOT(root)) {
  690. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  691. return;
  692. }
  693. node = rb_first(root);
  694. while (node) {
  695. double percent;
  696. const char *color;
  697. char *path;
  698. src_line = rb_entry(node, struct source_line, node);
  699. percent = src_line->percent;
  700. color = get_percent_color(percent);
  701. path = src_line->path;
  702. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  703. node = rb_next(node);
  704. }
  705. }
  706. static void symbol__annotate_hits(struct symbol *sym, int evidx)
  707. {
  708. struct annotation *notes = symbol__annotation(sym);
  709. struct sym_hist *h = annotation__histogram(notes, evidx);
  710. u64 len = symbol__size(sym), offset;
  711. for (offset = 0; offset < len; ++offset)
  712. if (h->addr[offset] != 0)
  713. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  714. sym->start + offset, h->addr[offset]);
  715. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  716. }
  717. int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
  718. bool full_paths, int min_pcnt, int max_lines,
  719. int context)
  720. {
  721. struct dso *dso = map->dso;
  722. const char *filename = dso->long_name, *d_filename;
  723. struct annotation *notes = symbol__annotation(sym);
  724. struct disasm_line *pos, *queue = NULL;
  725. u64 start = map__rip_2objdump(map, sym->start);
  726. int printed = 2, queue_len = 0;
  727. int more = 0;
  728. u64 len;
  729. if (full_paths)
  730. d_filename = filename;
  731. else
  732. d_filename = basename(filename);
  733. len = symbol__size(sym);
  734. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  735. printf("------------------------------------------------\n");
  736. if (verbose)
  737. symbol__annotate_hits(sym, evidx);
  738. list_for_each_entry(pos, &notes->src->source, node) {
  739. if (context && queue == NULL) {
  740. queue = pos;
  741. queue_len = 0;
  742. }
  743. switch (disasm_line__print(pos, sym, start, evidx, len,
  744. min_pcnt, printed, max_lines,
  745. queue)) {
  746. case 0:
  747. ++printed;
  748. if (context) {
  749. printed += queue_len;
  750. queue = NULL;
  751. queue_len = 0;
  752. }
  753. break;
  754. case 1:
  755. /* filtered by max_lines */
  756. ++more;
  757. break;
  758. case -1:
  759. default:
  760. /*
  761. * Filtered by min_pcnt or non IP lines when
  762. * context != 0
  763. */
  764. if (!context)
  765. break;
  766. if (queue_len == context)
  767. queue = list_entry(queue->node.next, typeof(*queue), node);
  768. else
  769. ++queue_len;
  770. break;
  771. }
  772. }
  773. return more;
  774. }
  775. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  776. {
  777. struct annotation *notes = symbol__annotation(sym);
  778. struct sym_hist *h = annotation__histogram(notes, evidx);
  779. memset(h, 0, notes->src->sizeof_sym_hist);
  780. }
  781. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  782. {
  783. struct annotation *notes = symbol__annotation(sym);
  784. struct sym_hist *h = annotation__histogram(notes, evidx);
  785. int len = symbol__size(sym), offset;
  786. h->sum = 0;
  787. for (offset = 0; offset < len; ++offset) {
  788. h->addr[offset] = h->addr[offset] * 7 / 8;
  789. h->sum += h->addr[offset];
  790. }
  791. }
  792. void disasm__purge(struct list_head *head)
  793. {
  794. struct disasm_line *pos, *n;
  795. list_for_each_entry_safe(pos, n, head, node) {
  796. list_del(&pos->node);
  797. disasm_line__free(pos);
  798. }
  799. }
  800. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  801. {
  802. size_t printed;
  803. if (dl->offset == -1)
  804. return fprintf(fp, "%s\n", dl->line);
  805. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  806. if (dl->ops.raw[0] != '\0') {
  807. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  808. dl->ops.raw);
  809. }
  810. return printed + fprintf(fp, "\n");
  811. }
  812. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  813. {
  814. struct disasm_line *pos;
  815. size_t printed = 0;
  816. list_for_each_entry(pos, head, node)
  817. printed += disasm_line__fprintf(pos, fp);
  818. return printed;
  819. }
  820. int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
  821. bool print_lines, bool full_paths, int min_pcnt,
  822. int max_lines)
  823. {
  824. struct dso *dso = map->dso;
  825. const char *filename = dso->long_name;
  826. struct rb_root source_line = RB_ROOT;
  827. u64 len;
  828. if (symbol__annotate(sym, map, 0) < 0)
  829. return -1;
  830. len = symbol__size(sym);
  831. if (print_lines) {
  832. symbol__get_source_line(sym, map, evidx, &source_line,
  833. len, filename);
  834. print_summary(&source_line, filename);
  835. }
  836. symbol__annotate_printf(sym, map, evidx, full_paths,
  837. min_pcnt, max_lines, 0);
  838. if (print_lines)
  839. symbol__free_source_line(sym, len);
  840. disasm__purge(&symbol__annotation(sym)->src->source);
  841. return 0;
  842. }