annotate.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. { .name = "xbeginl", .ops = &jump_ops, },
  327. { .name = "xbeginq", .ops = &jump_ops, },
  328. };
  329. static int ins__cmp(const void *name, const void *insp)
  330. {
  331. const struct ins *ins = insp;
  332. return strcmp(name, ins->name);
  333. }
  334. static struct ins *ins__find(const char *name)
  335. {
  336. const int nmemb = ARRAY_SIZE(instructions);
  337. return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
  338. }
  339. int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
  340. {
  341. struct annotation *notes = symbol__annotation(sym);
  342. pthread_mutex_init(&notes->lock, NULL);
  343. return 0;
  344. }
  345. int symbol__alloc_hist(struct symbol *sym)
  346. {
  347. struct annotation *notes = symbol__annotation(sym);
  348. const size_t size = symbol__size(sym);
  349. size_t sizeof_sym_hist;
  350. /* Check for overflow when calculating sizeof_sym_hist */
  351. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
  352. return -1;
  353. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
  354. /* Check for overflow in zalloc argument */
  355. if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
  356. / symbol_conf.nr_events)
  357. return -1;
  358. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  359. if (notes->src == NULL)
  360. return -1;
  361. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  362. notes->src->nr_histograms = symbol_conf.nr_events;
  363. INIT_LIST_HEAD(&notes->src->source);
  364. return 0;
  365. }
  366. void symbol__annotate_zero_histograms(struct symbol *sym)
  367. {
  368. struct annotation *notes = symbol__annotation(sym);
  369. pthread_mutex_lock(&notes->lock);
  370. if (notes->src != NULL)
  371. memset(notes->src->histograms, 0,
  372. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  373. pthread_mutex_unlock(&notes->lock);
  374. }
  375. int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  376. int evidx, u64 addr)
  377. {
  378. unsigned offset;
  379. struct annotation *notes;
  380. struct sym_hist *h;
  381. notes = symbol__annotation(sym);
  382. if (notes->src == NULL)
  383. return -ENOMEM;
  384. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  385. if (addr < sym->start || addr > sym->end)
  386. return -ERANGE;
  387. offset = addr - sym->start;
  388. h = annotation__histogram(notes, evidx);
  389. h->sum++;
  390. h->addr[offset]++;
  391. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  392. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  393. addr, addr - sym->start, evidx, h->addr[offset]);
  394. return 0;
  395. }
  396. static void disasm_line__init_ins(struct disasm_line *dl)
  397. {
  398. dl->ins = ins__find(dl->name);
  399. if (dl->ins == NULL)
  400. return;
  401. if (!dl->ins->ops)
  402. return;
  403. if (dl->ins->ops->parse)
  404. dl->ins->ops->parse(&dl->ops);
  405. }
  406. static int disasm_line__parse(char *line, char **namep, char **rawp)
  407. {
  408. char *name = line, tmp;
  409. while (isspace(name[0]))
  410. ++name;
  411. if (name[0] == '\0')
  412. return -1;
  413. *rawp = name + 1;
  414. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  415. ++*rawp;
  416. tmp = (*rawp)[0];
  417. (*rawp)[0] = '\0';
  418. *namep = strdup(name);
  419. if (*namep == NULL)
  420. goto out_free_name;
  421. (*rawp)[0] = tmp;
  422. if ((*rawp)[0] != '\0') {
  423. (*rawp)++;
  424. while (isspace((*rawp)[0]))
  425. ++(*rawp);
  426. }
  427. return 0;
  428. out_free_name:
  429. free(*namep);
  430. *namep = NULL;
  431. return -1;
  432. }
  433. static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
  434. {
  435. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  436. if (dl != NULL) {
  437. dl->offset = offset;
  438. dl->line = strdup(line);
  439. if (dl->line == NULL)
  440. goto out_delete;
  441. if (offset != -1) {
  442. if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
  443. goto out_free_line;
  444. disasm_line__init_ins(dl);
  445. }
  446. }
  447. return dl;
  448. out_free_line:
  449. free(dl->line);
  450. out_delete:
  451. free(dl);
  452. return NULL;
  453. }
  454. void disasm_line__free(struct disasm_line *dl)
  455. {
  456. free(dl->line);
  457. free(dl->name);
  458. if (dl->ins && dl->ins->ops->free)
  459. dl->ins->ops->free(&dl->ops);
  460. else
  461. ins__delete(&dl->ops);
  462. free(dl);
  463. }
  464. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  465. {
  466. if (raw || !dl->ins)
  467. return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
  468. return ins__scnprintf(dl->ins, bf, size, &dl->ops);
  469. }
  470. static void disasm__add(struct list_head *head, struct disasm_line *line)
  471. {
  472. list_add_tail(&line->node, head);
  473. }
  474. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  475. {
  476. list_for_each_entry_continue(pos, head, node)
  477. if (pos->offset >= 0)
  478. return pos;
  479. return NULL;
  480. }
  481. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  482. int evidx, u64 len, int min_pcnt, int printed,
  483. int max_lines, struct disasm_line *queue)
  484. {
  485. static const char *prev_line;
  486. static const char *prev_color;
  487. if (dl->offset != -1) {
  488. const char *path = NULL;
  489. unsigned int hits = 0;
  490. double percent = 0.0;
  491. const char *color;
  492. struct annotation *notes = symbol__annotation(sym);
  493. struct source_line *src_line = notes->src->lines;
  494. struct sym_hist *h = annotation__histogram(notes, evidx);
  495. s64 offset = dl->offset;
  496. const u64 addr = start + offset;
  497. struct disasm_line *next;
  498. next = disasm__get_next_ip_line(&notes->src->source, dl);
  499. while (offset < (s64)len &&
  500. (next == NULL || offset < next->offset)) {
  501. if (src_line) {
  502. if (path == NULL)
  503. path = src_line[offset].path;
  504. percent += src_line[offset].percent;
  505. } else
  506. hits += h->addr[offset];
  507. ++offset;
  508. }
  509. if (src_line == NULL && h->sum)
  510. percent = 100.0 * hits / h->sum;
  511. if (percent < min_pcnt)
  512. return -1;
  513. if (max_lines && printed >= max_lines)
  514. return 1;
  515. if (queue != NULL) {
  516. list_for_each_entry_from(queue, &notes->src->source, node) {
  517. if (queue == dl)
  518. break;
  519. disasm_line__print(queue, sym, start, evidx, len,
  520. 0, 0, 1, NULL);
  521. }
  522. }
  523. color = get_percent_color(percent);
  524. /*
  525. * Also color the filename and line if needed, with
  526. * the same color than the percentage. Don't print it
  527. * twice for close colored addr with the same filename:line
  528. */
  529. if (path) {
  530. if (!prev_line || strcmp(prev_line, path)
  531. || color != prev_color) {
  532. color_fprintf(stdout, color, " %s", path);
  533. prev_line = path;
  534. prev_color = color;
  535. }
  536. }
  537. color_fprintf(stdout, color, " %7.2f", percent);
  538. printf(" : ");
  539. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  540. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  541. } else if (max_lines && printed >= max_lines)
  542. return 1;
  543. else {
  544. if (queue)
  545. return -1;
  546. if (!*dl->line)
  547. printf(" :\n");
  548. else
  549. printf(" : %s\n", dl->line);
  550. }
  551. return 0;
  552. }
  553. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  554. FILE *file, size_t privsize)
  555. {
  556. struct annotation *notes = symbol__annotation(sym);
  557. struct disasm_line *dl;
  558. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  559. size_t line_len;
  560. s64 line_ip, offset = -1;
  561. if (getline(&line, &line_len, file) < 0)
  562. return -1;
  563. if (!line)
  564. return -1;
  565. while (line_len != 0 && isspace(line[line_len - 1]))
  566. line[--line_len] = '\0';
  567. c = strchr(line, '\n');
  568. if (c)
  569. *c = 0;
  570. line_ip = -1;
  571. parsed_line = line;
  572. /*
  573. * Strip leading spaces:
  574. */
  575. tmp = line;
  576. while (*tmp) {
  577. if (*tmp != ' ')
  578. break;
  579. tmp++;
  580. }
  581. if (*tmp) {
  582. /*
  583. * Parse hexa addresses followed by ':'
  584. */
  585. line_ip = strtoull(tmp, &tmp2, 16);
  586. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  587. line_ip = -1;
  588. }
  589. if (line_ip != -1) {
  590. u64 start = map__rip_2objdump(map, sym->start),
  591. end = map__rip_2objdump(map, sym->end);
  592. offset = line_ip - start;
  593. if (offset < 0 || (u64)line_ip > end)
  594. offset = -1;
  595. else
  596. parsed_line = tmp2 + 1;
  597. }
  598. dl = disasm_line__new(offset, parsed_line, privsize);
  599. free(line);
  600. if (dl == NULL)
  601. return -1;
  602. disasm__add(&notes->src->source, dl);
  603. return 0;
  604. }
  605. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  606. {
  607. struct dso *dso = map->dso;
  608. char *filename = dso__build_id_filename(dso, NULL, 0);
  609. bool free_filename = true;
  610. char command[PATH_MAX * 2];
  611. FILE *file;
  612. int err = 0;
  613. char symfs_filename[PATH_MAX];
  614. if (filename) {
  615. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  616. symbol_conf.symfs, filename);
  617. }
  618. if (filename == NULL) {
  619. if (dso->has_build_id) {
  620. pr_err("Can't annotate %s: not enough memory\n",
  621. sym->name);
  622. return -ENOMEM;
  623. }
  624. goto fallback;
  625. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  626. strstr(command, "[kernel.kallsyms]") ||
  627. access(symfs_filename, R_OK)) {
  628. free(filename);
  629. fallback:
  630. /*
  631. * If we don't have build-ids or the build-id file isn't in the
  632. * cache, or is just a kallsyms file, well, lets hope that this
  633. * DSO is the same as when 'perf record' ran.
  634. */
  635. filename = dso->long_name;
  636. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  637. symbol_conf.symfs, filename);
  638. free_filename = false;
  639. }
  640. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS) {
  641. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  642. char *build_id_msg = NULL;
  643. if (dso->annotate_warned)
  644. goto out_free_filename;
  645. if (dso->has_build_id) {
  646. build_id__sprintf(dso->build_id,
  647. sizeof(dso->build_id), bf + 15);
  648. build_id_msg = bf;
  649. }
  650. err = -ENOENT;
  651. dso->annotate_warned = 1;
  652. pr_err("Can't annotate %s:\n\n"
  653. "No vmlinux file%s\nwas found in the path.\n\n"
  654. "Please use:\n\n"
  655. " perf buildid-cache -av vmlinux\n\n"
  656. "or:\n\n"
  657. " --vmlinux vmlinux\n",
  658. sym->name, build_id_msg ?: "");
  659. goto out_free_filename;
  660. }
  661. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  662. filename, sym->name, map->unmap_ip(map, sym->start),
  663. map->unmap_ip(map, sym->end));
  664. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  665. dso, dso->long_name, sym, sym->name);
  666. snprintf(command, sizeof(command),
  667. "%s %s%s --start-address=0x%016" PRIx64
  668. " --stop-address=0x%016" PRIx64
  669. " -d %s %s -C %s|grep -v %s|expand",
  670. objdump_path ? objdump_path : "objdump",
  671. disassembler_style ? "-M " : "",
  672. disassembler_style ? disassembler_style : "",
  673. map__rip_2objdump(map, sym->start),
  674. map__rip_2objdump(map, sym->end+1),
  675. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  676. symbol_conf.annotate_src ? "-S" : "",
  677. symfs_filename, filename);
  678. pr_debug("Executing: %s\n", command);
  679. file = popen(command, "r");
  680. if (!file)
  681. goto out_free_filename;
  682. while (!feof(file))
  683. if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
  684. break;
  685. pclose(file);
  686. out_free_filename:
  687. if (free_filename)
  688. free(filename);
  689. return err;
  690. }
  691. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  692. {
  693. struct source_line *iter;
  694. struct rb_node **p = &root->rb_node;
  695. struct rb_node *parent = NULL;
  696. int ret;
  697. while (*p != NULL) {
  698. parent = *p;
  699. iter = rb_entry(parent, struct source_line, node);
  700. ret = strcmp(iter->path, src_line->path);
  701. if (ret == 0) {
  702. iter->percent_sum += src_line->percent;
  703. return;
  704. }
  705. if (ret < 0)
  706. p = &(*p)->rb_left;
  707. else
  708. p = &(*p)->rb_right;
  709. }
  710. src_line->percent_sum = src_line->percent;
  711. rb_link_node(&src_line->node, parent, p);
  712. rb_insert_color(&src_line->node, root);
  713. }
  714. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  715. {
  716. struct source_line *iter;
  717. struct rb_node **p = &root->rb_node;
  718. struct rb_node *parent = NULL;
  719. while (*p != NULL) {
  720. parent = *p;
  721. iter = rb_entry(parent, struct source_line, node);
  722. if (src_line->percent_sum > iter->percent_sum)
  723. p = &(*p)->rb_left;
  724. else
  725. p = &(*p)->rb_right;
  726. }
  727. rb_link_node(&src_line->node, parent, p);
  728. rb_insert_color(&src_line->node, root);
  729. }
  730. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  731. {
  732. struct source_line *src_line;
  733. struct rb_node *node;
  734. node = rb_first(src_root);
  735. while (node) {
  736. struct rb_node *next;
  737. src_line = rb_entry(node, struct source_line, node);
  738. next = rb_next(node);
  739. rb_erase(node, src_root);
  740. __resort_source_line(dest_root, src_line);
  741. node = next;
  742. }
  743. }
  744. static void symbol__free_source_line(struct symbol *sym, int len)
  745. {
  746. struct annotation *notes = symbol__annotation(sym);
  747. struct source_line *src_line = notes->src->lines;
  748. int i;
  749. for (i = 0; i < len; i++)
  750. free(src_line[i].path);
  751. free(src_line);
  752. notes->src->lines = NULL;
  753. }
  754. /* Get the filename:line for the colored entries */
  755. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  756. int evidx, struct rb_root *root, int len,
  757. const char *filename)
  758. {
  759. u64 start;
  760. int i;
  761. char cmd[PATH_MAX * 2];
  762. struct source_line *src_line;
  763. struct annotation *notes = symbol__annotation(sym);
  764. struct sym_hist *h = annotation__histogram(notes, evidx);
  765. struct rb_root tmp_root = RB_ROOT;
  766. if (!h->sum)
  767. return 0;
  768. src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
  769. if (!notes->src->lines)
  770. return -1;
  771. start = map__rip_2objdump(map, sym->start);
  772. for (i = 0; i < len; i++) {
  773. char *path = NULL;
  774. size_t line_len;
  775. u64 offset;
  776. FILE *fp;
  777. src_line[i].percent = 100.0 * h->addr[i] / h->sum;
  778. if (src_line[i].percent <= 0.5)
  779. continue;
  780. offset = start + i;
  781. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  782. fp = popen(cmd, "r");
  783. if (!fp)
  784. continue;
  785. if (getline(&path, &line_len, fp) < 0 || !line_len)
  786. goto next;
  787. src_line[i].path = malloc(sizeof(char) * line_len + 1);
  788. if (!src_line[i].path)
  789. goto next;
  790. strcpy(src_line[i].path, path);
  791. insert_source_line(&tmp_root, &src_line[i]);
  792. next:
  793. pclose(fp);
  794. }
  795. resort_source_line(root, &tmp_root);
  796. return 0;
  797. }
  798. static void print_summary(struct rb_root *root, const char *filename)
  799. {
  800. struct source_line *src_line;
  801. struct rb_node *node;
  802. printf("\nSorted summary for file %s\n", filename);
  803. printf("----------------------------------------------\n\n");
  804. if (RB_EMPTY_ROOT(root)) {
  805. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  806. return;
  807. }
  808. node = rb_first(root);
  809. while (node) {
  810. double percent;
  811. const char *color;
  812. char *path;
  813. src_line = rb_entry(node, struct source_line, node);
  814. percent = src_line->percent_sum;
  815. color = get_percent_color(percent);
  816. path = src_line->path;
  817. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  818. node = rb_next(node);
  819. }
  820. }
  821. static void symbol__annotate_hits(struct symbol *sym, int evidx)
  822. {
  823. struct annotation *notes = symbol__annotation(sym);
  824. struct sym_hist *h = annotation__histogram(notes, evidx);
  825. u64 len = symbol__size(sym), offset;
  826. for (offset = 0; offset < len; ++offset)
  827. if (h->addr[offset] != 0)
  828. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  829. sym->start + offset, h->addr[offset]);
  830. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  831. }
  832. int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
  833. bool full_paths, int min_pcnt, int max_lines,
  834. int context)
  835. {
  836. struct dso *dso = map->dso;
  837. char *filename;
  838. const char *d_filename;
  839. struct annotation *notes = symbol__annotation(sym);
  840. struct disasm_line *pos, *queue = NULL;
  841. u64 start = map__rip_2objdump(map, sym->start);
  842. int printed = 2, queue_len = 0;
  843. int more = 0;
  844. u64 len;
  845. filename = strdup(dso->long_name);
  846. if (!filename)
  847. return -ENOMEM;
  848. if (full_paths)
  849. d_filename = filename;
  850. else
  851. d_filename = basename(filename);
  852. len = symbol__size(sym);
  853. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  854. printf("------------------------------------------------\n");
  855. if (verbose)
  856. symbol__annotate_hits(sym, evidx);
  857. list_for_each_entry(pos, &notes->src->source, node) {
  858. if (context && queue == NULL) {
  859. queue = pos;
  860. queue_len = 0;
  861. }
  862. switch (disasm_line__print(pos, sym, start, evidx, len,
  863. min_pcnt, printed, max_lines,
  864. queue)) {
  865. case 0:
  866. ++printed;
  867. if (context) {
  868. printed += queue_len;
  869. queue = NULL;
  870. queue_len = 0;
  871. }
  872. break;
  873. case 1:
  874. /* filtered by max_lines */
  875. ++more;
  876. break;
  877. case -1:
  878. default:
  879. /*
  880. * Filtered by min_pcnt or non IP lines when
  881. * context != 0
  882. */
  883. if (!context)
  884. break;
  885. if (queue_len == context)
  886. queue = list_entry(queue->node.next, typeof(*queue), node);
  887. else
  888. ++queue_len;
  889. break;
  890. }
  891. }
  892. free(filename);
  893. return more;
  894. }
  895. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  896. {
  897. struct annotation *notes = symbol__annotation(sym);
  898. struct sym_hist *h = annotation__histogram(notes, evidx);
  899. memset(h, 0, notes->src->sizeof_sym_hist);
  900. }
  901. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  902. {
  903. struct annotation *notes = symbol__annotation(sym);
  904. struct sym_hist *h = annotation__histogram(notes, evidx);
  905. int len = symbol__size(sym), offset;
  906. h->sum = 0;
  907. for (offset = 0; offset < len; ++offset) {
  908. h->addr[offset] = h->addr[offset] * 7 / 8;
  909. h->sum += h->addr[offset];
  910. }
  911. }
  912. void disasm__purge(struct list_head *head)
  913. {
  914. struct disasm_line *pos, *n;
  915. list_for_each_entry_safe(pos, n, head, node) {
  916. list_del(&pos->node);
  917. disasm_line__free(pos);
  918. }
  919. }
  920. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  921. {
  922. size_t printed;
  923. if (dl->offset == -1)
  924. return fprintf(fp, "%s\n", dl->line);
  925. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  926. if (dl->ops.raw[0] != '\0') {
  927. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  928. dl->ops.raw);
  929. }
  930. return printed + fprintf(fp, "\n");
  931. }
  932. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  933. {
  934. struct disasm_line *pos;
  935. size_t printed = 0;
  936. list_for_each_entry(pos, head, node)
  937. printed += disasm_line__fprintf(pos, fp);
  938. return printed;
  939. }
  940. int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
  941. bool print_lines, bool full_paths, int min_pcnt,
  942. int max_lines)
  943. {
  944. struct dso *dso = map->dso;
  945. const char *filename = dso->long_name;
  946. struct rb_root source_line = RB_ROOT;
  947. u64 len;
  948. if (symbol__annotate(sym, map, 0) < 0)
  949. return -1;
  950. len = symbol__size(sym);
  951. if (print_lines) {
  952. symbol__get_source_line(sym, map, evidx, &source_line,
  953. len, filename);
  954. print_summary(&source_line, filename);
  955. }
  956. symbol__annotate_printf(sym, map, evidx, full_paths,
  957. min_pcnt, max_lines, 0);
  958. if (print_lines)
  959. symbol__free_source_line(sym, len);
  960. disasm__purge(&symbol__annotation(sym)->src->source);
  961. return 0;
  962. }