annotate.c 26 KB

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