annotate.c 26 KB

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