annotate.c 23 KB

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