annotate.c 29 KB

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