annotate.c 26 KB

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