annotate.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  483. s64 end, const char **path)
  484. {
  485. struct source_line *src_line = notes->src->lines;
  486. double percent = 0.0;
  487. if (src_line) {
  488. size_t sizeof_src_line = sizeof(*src_line) +
  489. sizeof(src_line->p) * (src_line->nr_pcnt - 1);
  490. while (offset < end) {
  491. src_line = (void *)notes->src->lines +
  492. (sizeof_src_line * offset);
  493. if (*path == NULL)
  494. *path = src_line->path;
  495. percent += src_line->p[evidx].percent;
  496. offset++;
  497. }
  498. } else {
  499. struct sym_hist *h = annotation__histogram(notes, evidx);
  500. unsigned int hits = 0;
  501. while (offset < end)
  502. hits += h->addr[offset++];
  503. if (h->sum)
  504. percent = 100.0 * hits / h->sum;
  505. }
  506. return percent;
  507. }
  508. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  509. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  510. int max_lines, struct disasm_line *queue)
  511. {
  512. static const char *prev_line;
  513. static const char *prev_color;
  514. if (dl->offset != -1) {
  515. const char *path = NULL;
  516. double percent, max_percent = 0.0;
  517. double *ppercents = &percent;
  518. int i, nr_percent = 1;
  519. const char *color;
  520. struct annotation *notes = symbol__annotation(sym);
  521. s64 offset = dl->offset;
  522. const u64 addr = start + offset;
  523. struct disasm_line *next;
  524. next = disasm__get_next_ip_line(&notes->src->source, dl);
  525. if (perf_evsel__is_group_event(evsel)) {
  526. nr_percent = evsel->nr_members;
  527. ppercents = calloc(nr_percent, sizeof(double));
  528. if (ppercents == NULL)
  529. return -1;
  530. }
  531. for (i = 0; i < nr_percent; i++) {
  532. percent = disasm__calc_percent(notes,
  533. notes->src->lines ? i : evsel->idx + i,
  534. offset,
  535. next ? next->offset : (s64) len,
  536. &path);
  537. ppercents[i] = percent;
  538. if (percent > max_percent)
  539. max_percent = percent;
  540. }
  541. if (max_percent < min_pcnt)
  542. return -1;
  543. if (max_lines && printed >= max_lines)
  544. return 1;
  545. if (queue != NULL) {
  546. list_for_each_entry_from(queue, &notes->src->source, node) {
  547. if (queue == dl)
  548. break;
  549. disasm_line__print(queue, sym, start, evsel, len,
  550. 0, 0, 1, NULL);
  551. }
  552. }
  553. color = get_percent_color(max_percent);
  554. /*
  555. * Also color the filename and line if needed, with
  556. * the same color than the percentage. Don't print it
  557. * twice for close colored addr with the same filename:line
  558. */
  559. if (path) {
  560. if (!prev_line || strcmp(prev_line, path)
  561. || color != prev_color) {
  562. color_fprintf(stdout, color, " %s", path);
  563. prev_line = path;
  564. prev_color = color;
  565. }
  566. }
  567. for (i = 0; i < nr_percent; i++) {
  568. percent = ppercents[i];
  569. color = get_percent_color(percent);
  570. color_fprintf(stdout, color, " %7.2f", percent);
  571. }
  572. printf(" : ");
  573. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  574. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  575. if (ppercents != &percent)
  576. free(ppercents);
  577. } else if (max_lines && printed >= max_lines)
  578. return 1;
  579. else {
  580. int width = 8;
  581. if (queue)
  582. return -1;
  583. if (perf_evsel__is_group_event(evsel))
  584. width *= evsel->nr_members;
  585. if (!*dl->line)
  586. printf(" %*s:\n", width, " ");
  587. else
  588. printf(" %*s: %s\n", width, " ", dl->line);
  589. }
  590. return 0;
  591. }
  592. /*
  593. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  594. * which looks like following
  595. *
  596. * 0000000000415500 <_init>:
  597. * 415500: sub $0x8,%rsp
  598. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  599. * 41550b: test %rax,%rax
  600. * 41550e: je 415515 <_init+0x15>
  601. * 415510: callq 416e70 <__gmon_start__@plt>
  602. * 415515: add $0x8,%rsp
  603. * 415519: retq
  604. *
  605. * it will be parsed and saved into struct disasm_line as
  606. * <offset> <name> <ops.raw>
  607. *
  608. * The offset will be a relative offset from the start of the symbol and -1
  609. * means that it's not a disassembly line so should be treated differently.
  610. * The ops.raw part will be parsed further according to type of the instruction.
  611. */
  612. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  613. FILE *file, size_t privsize)
  614. {
  615. struct annotation *notes = symbol__annotation(sym);
  616. struct disasm_line *dl;
  617. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  618. size_t line_len;
  619. s64 line_ip, offset = -1;
  620. if (getline(&line, &line_len, file) < 0)
  621. return -1;
  622. if (!line)
  623. return -1;
  624. while (line_len != 0 && isspace(line[line_len - 1]))
  625. line[--line_len] = '\0';
  626. c = strchr(line, '\n');
  627. if (c)
  628. *c = 0;
  629. line_ip = -1;
  630. parsed_line = line;
  631. /*
  632. * Strip leading spaces:
  633. */
  634. tmp = line;
  635. while (*tmp) {
  636. if (*tmp != ' ')
  637. break;
  638. tmp++;
  639. }
  640. if (*tmp) {
  641. /*
  642. * Parse hexa addresses followed by ':'
  643. */
  644. line_ip = strtoull(tmp, &tmp2, 16);
  645. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  646. line_ip = -1;
  647. }
  648. if (line_ip != -1) {
  649. u64 start = map__rip_2objdump(map, sym->start),
  650. end = map__rip_2objdump(map, sym->end);
  651. offset = line_ip - start;
  652. if (offset < 0 || (u64)line_ip > end)
  653. offset = -1;
  654. else
  655. parsed_line = tmp2 + 1;
  656. }
  657. dl = disasm_line__new(offset, parsed_line, privsize);
  658. free(line);
  659. if (dl == NULL)
  660. return -1;
  661. disasm__add(&notes->src->source, dl);
  662. return 0;
  663. }
  664. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  665. {
  666. struct dso *dso = map->dso;
  667. char *filename = dso__build_id_filename(dso, NULL, 0);
  668. bool free_filename = true;
  669. char command[PATH_MAX * 2];
  670. FILE *file;
  671. int err = 0;
  672. char symfs_filename[PATH_MAX];
  673. if (filename) {
  674. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  675. symbol_conf.symfs, filename);
  676. }
  677. if (filename == NULL) {
  678. if (dso->has_build_id) {
  679. pr_err("Can't annotate %s: not enough memory\n",
  680. sym->name);
  681. return -ENOMEM;
  682. }
  683. goto fallback;
  684. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  685. strstr(command, "[kernel.kallsyms]") ||
  686. access(symfs_filename, R_OK)) {
  687. free(filename);
  688. fallback:
  689. /*
  690. * If we don't have build-ids or the build-id file isn't in the
  691. * cache, or is just a kallsyms file, well, lets hope that this
  692. * DSO is the same as when 'perf record' ran.
  693. */
  694. filename = dso->long_name;
  695. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  696. symbol_conf.symfs, filename);
  697. free_filename = false;
  698. }
  699. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS) {
  700. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  701. char *build_id_msg = NULL;
  702. if (dso->annotate_warned)
  703. goto out_free_filename;
  704. if (dso->has_build_id) {
  705. build_id__sprintf(dso->build_id,
  706. sizeof(dso->build_id), bf + 15);
  707. build_id_msg = bf;
  708. }
  709. err = -ENOENT;
  710. dso->annotate_warned = 1;
  711. pr_err("Can't annotate %s:\n\n"
  712. "No vmlinux file%s\nwas found in the path.\n\n"
  713. "Please use:\n\n"
  714. " perf buildid-cache -vu vmlinux\n\n"
  715. "or:\n\n"
  716. " --vmlinux vmlinux\n",
  717. sym->name, build_id_msg ?: "");
  718. goto out_free_filename;
  719. }
  720. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  721. filename, sym->name, map->unmap_ip(map, sym->start),
  722. map->unmap_ip(map, sym->end));
  723. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  724. dso, dso->long_name, sym, sym->name);
  725. snprintf(command, sizeof(command),
  726. "%s %s%s --start-address=0x%016" PRIx64
  727. " --stop-address=0x%016" PRIx64
  728. " -d %s %s -C %s|grep -v %s|expand",
  729. objdump_path ? objdump_path : "objdump",
  730. disassembler_style ? "-M " : "",
  731. disassembler_style ? disassembler_style : "",
  732. map__rip_2objdump(map, sym->start),
  733. map__rip_2objdump(map, sym->end+1),
  734. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  735. symbol_conf.annotate_src ? "-S" : "",
  736. symfs_filename, filename);
  737. pr_debug("Executing: %s\n", command);
  738. file = popen(command, "r");
  739. if (!file)
  740. goto out_free_filename;
  741. while (!feof(file))
  742. if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
  743. break;
  744. pclose(file);
  745. out_free_filename:
  746. if (free_filename)
  747. free(filename);
  748. return err;
  749. }
  750. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  751. {
  752. struct source_line *iter;
  753. struct rb_node **p = &root->rb_node;
  754. struct rb_node *parent = NULL;
  755. int i, ret;
  756. while (*p != NULL) {
  757. parent = *p;
  758. iter = rb_entry(parent, struct source_line, node);
  759. ret = strcmp(iter->path, src_line->path);
  760. if (ret == 0) {
  761. for (i = 0; i < src_line->nr_pcnt; i++)
  762. iter->p[i].percent_sum += src_line->p[i].percent;
  763. return;
  764. }
  765. if (ret < 0)
  766. p = &(*p)->rb_left;
  767. else
  768. p = &(*p)->rb_right;
  769. }
  770. for (i = 0; i < src_line->nr_pcnt; i++)
  771. src_line->p[i].percent_sum = src_line->p[i].percent;
  772. rb_link_node(&src_line->node, parent, p);
  773. rb_insert_color(&src_line->node, root);
  774. }
  775. static int cmp_source_line(struct source_line *a, struct source_line *b)
  776. {
  777. int i;
  778. for (i = 0; i < a->nr_pcnt; i++) {
  779. if (a->p[i].percent_sum == b->p[i].percent_sum)
  780. continue;
  781. return a->p[i].percent_sum > b->p[i].percent_sum;
  782. }
  783. return 0;
  784. }
  785. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  786. {
  787. struct source_line *iter;
  788. struct rb_node **p = &root->rb_node;
  789. struct rb_node *parent = NULL;
  790. while (*p != NULL) {
  791. parent = *p;
  792. iter = rb_entry(parent, struct source_line, node);
  793. if (cmp_source_line(src_line, iter))
  794. p = &(*p)->rb_left;
  795. else
  796. p = &(*p)->rb_right;
  797. }
  798. rb_link_node(&src_line->node, parent, p);
  799. rb_insert_color(&src_line->node, root);
  800. }
  801. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  802. {
  803. struct source_line *src_line;
  804. struct rb_node *node;
  805. node = rb_first(src_root);
  806. while (node) {
  807. struct rb_node *next;
  808. src_line = rb_entry(node, struct source_line, node);
  809. next = rb_next(node);
  810. rb_erase(node, src_root);
  811. __resort_source_line(dest_root, src_line);
  812. node = next;
  813. }
  814. }
  815. static void symbol__free_source_line(struct symbol *sym, int len)
  816. {
  817. struct annotation *notes = symbol__annotation(sym);
  818. struct source_line *src_line = notes->src->lines;
  819. size_t sizeof_src_line;
  820. int i;
  821. sizeof_src_line = sizeof(*src_line) +
  822. (sizeof(src_line->p) * (src_line->nr_pcnt - 1));
  823. for (i = 0; i < len; i++) {
  824. free(src_line->path);
  825. src_line = (void *)src_line + sizeof_src_line;
  826. }
  827. free(notes->src->lines);
  828. notes->src->lines = NULL;
  829. }
  830. /* Get the filename:line for the colored entries */
  831. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  832. struct perf_evsel *evsel,
  833. struct rb_root *root, int len,
  834. const char *filename)
  835. {
  836. u64 start;
  837. int i, k;
  838. int evidx = evsel->idx;
  839. char cmd[PATH_MAX * 2];
  840. struct source_line *src_line;
  841. struct annotation *notes = symbol__annotation(sym);
  842. struct sym_hist *h = annotation__histogram(notes, evidx);
  843. struct rb_root tmp_root = RB_ROOT;
  844. int nr_pcnt = 1;
  845. u64 h_sum = h->sum;
  846. size_t sizeof_src_line = sizeof(struct source_line);
  847. if (perf_evsel__is_group_event(evsel)) {
  848. for (i = 1; i < evsel->nr_members; i++) {
  849. h = annotation__histogram(notes, evidx + i);
  850. h_sum += h->sum;
  851. }
  852. nr_pcnt = evsel->nr_members;
  853. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->p);
  854. }
  855. if (!h_sum)
  856. return 0;
  857. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  858. if (!notes->src->lines)
  859. return -1;
  860. start = map__rip_2objdump(map, sym->start);
  861. for (i = 0; i < len; i++) {
  862. char *path = NULL;
  863. size_t line_len;
  864. u64 offset;
  865. FILE *fp;
  866. double percent_max = 0.0;
  867. src_line->nr_pcnt = nr_pcnt;
  868. for (k = 0; k < nr_pcnt; k++) {
  869. h = annotation__histogram(notes, evidx + k);
  870. src_line->p[k].percent = 100.0 * h->addr[i] / h->sum;
  871. if (src_line->p[k].percent > percent_max)
  872. percent_max = src_line->p[k].percent;
  873. }
  874. if (percent_max <= 0.5)
  875. goto next;
  876. offset = start + i;
  877. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  878. fp = popen(cmd, "r");
  879. if (!fp)
  880. goto next;
  881. if (getline(&path, &line_len, fp) < 0 || !line_len)
  882. goto next_close;
  883. src_line->path = malloc(sizeof(char) * line_len + 1);
  884. if (!src_line->path)
  885. goto next_close;
  886. strcpy(src_line->path, path);
  887. insert_source_line(&tmp_root, src_line);
  888. next_close:
  889. pclose(fp);
  890. next:
  891. src_line = (void *)src_line + sizeof_src_line;
  892. }
  893. resort_source_line(root, &tmp_root);
  894. return 0;
  895. }
  896. static void print_summary(struct rb_root *root, const char *filename)
  897. {
  898. struct source_line *src_line;
  899. struct rb_node *node;
  900. printf("\nSorted summary for file %s\n", filename);
  901. printf("----------------------------------------------\n\n");
  902. if (RB_EMPTY_ROOT(root)) {
  903. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  904. return;
  905. }
  906. node = rb_first(root);
  907. while (node) {
  908. double percent, percent_max = 0.0;
  909. const char *color;
  910. char *path;
  911. int i;
  912. src_line = rb_entry(node, struct source_line, node);
  913. for (i = 0; i < src_line->nr_pcnt; i++) {
  914. percent = src_line->p[i].percent_sum;
  915. color = get_percent_color(percent);
  916. color_fprintf(stdout, color, " %7.2f", percent);
  917. if (percent > percent_max)
  918. percent_max = percent;
  919. }
  920. path = src_line->path;
  921. color = get_percent_color(percent_max);
  922. color_fprintf(stdout, color, " %s", path);
  923. node = rb_next(node);
  924. }
  925. }
  926. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  927. {
  928. struct annotation *notes = symbol__annotation(sym);
  929. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  930. u64 len = symbol__size(sym), offset;
  931. for (offset = 0; offset < len; ++offset)
  932. if (h->addr[offset] != 0)
  933. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  934. sym->start + offset, h->addr[offset]);
  935. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  936. }
  937. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  938. struct perf_evsel *evsel, bool full_paths,
  939. int min_pcnt, int max_lines, int context)
  940. {
  941. struct dso *dso = map->dso;
  942. char *filename;
  943. const char *d_filename;
  944. struct annotation *notes = symbol__annotation(sym);
  945. struct disasm_line *pos, *queue = NULL;
  946. u64 start = map__rip_2objdump(map, sym->start);
  947. int printed = 2, queue_len = 0;
  948. int more = 0;
  949. u64 len;
  950. int width = 8;
  951. int namelen;
  952. filename = strdup(dso->long_name);
  953. if (!filename)
  954. return -ENOMEM;
  955. if (full_paths)
  956. d_filename = filename;
  957. else
  958. d_filename = basename(filename);
  959. len = symbol__size(sym);
  960. namelen = strlen(d_filename);
  961. if (perf_evsel__is_group_event(evsel))
  962. width *= evsel->nr_members;
  963. printf(" %-*.*s| Source code & Disassembly of %s\n",
  964. width, width, "Percent", d_filename);
  965. printf("-%-*.*s-------------------------------------\n",
  966. width+namelen, width+namelen, graph_dotted_line);
  967. if (verbose)
  968. symbol__annotate_hits(sym, evsel);
  969. list_for_each_entry(pos, &notes->src->source, node) {
  970. if (context && queue == NULL) {
  971. queue = pos;
  972. queue_len = 0;
  973. }
  974. switch (disasm_line__print(pos, sym, start, evsel, len,
  975. min_pcnt, printed, max_lines,
  976. queue)) {
  977. case 0:
  978. ++printed;
  979. if (context) {
  980. printed += queue_len;
  981. queue = NULL;
  982. queue_len = 0;
  983. }
  984. break;
  985. case 1:
  986. /* filtered by max_lines */
  987. ++more;
  988. break;
  989. case -1:
  990. default:
  991. /*
  992. * Filtered by min_pcnt or non IP lines when
  993. * context != 0
  994. */
  995. if (!context)
  996. break;
  997. if (queue_len == context)
  998. queue = list_entry(queue->node.next, typeof(*queue), node);
  999. else
  1000. ++queue_len;
  1001. break;
  1002. }
  1003. }
  1004. free(filename);
  1005. return more;
  1006. }
  1007. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1008. {
  1009. struct annotation *notes = symbol__annotation(sym);
  1010. struct sym_hist *h = annotation__histogram(notes, evidx);
  1011. memset(h, 0, notes->src->sizeof_sym_hist);
  1012. }
  1013. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1014. {
  1015. struct annotation *notes = symbol__annotation(sym);
  1016. struct sym_hist *h = annotation__histogram(notes, evidx);
  1017. int len = symbol__size(sym), offset;
  1018. h->sum = 0;
  1019. for (offset = 0; offset < len; ++offset) {
  1020. h->addr[offset] = h->addr[offset] * 7 / 8;
  1021. h->sum += h->addr[offset];
  1022. }
  1023. }
  1024. void disasm__purge(struct list_head *head)
  1025. {
  1026. struct disasm_line *pos, *n;
  1027. list_for_each_entry_safe(pos, n, head, node) {
  1028. list_del(&pos->node);
  1029. disasm_line__free(pos);
  1030. }
  1031. }
  1032. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1033. {
  1034. size_t printed;
  1035. if (dl->offset == -1)
  1036. return fprintf(fp, "%s\n", dl->line);
  1037. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  1038. if (dl->ops.raw[0] != '\0') {
  1039. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1040. dl->ops.raw);
  1041. }
  1042. return printed + fprintf(fp, "\n");
  1043. }
  1044. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1045. {
  1046. struct disasm_line *pos;
  1047. size_t printed = 0;
  1048. list_for_each_entry(pos, head, node)
  1049. printed += disasm_line__fprintf(pos, fp);
  1050. return printed;
  1051. }
  1052. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1053. struct perf_evsel *evsel, bool print_lines,
  1054. bool full_paths, int min_pcnt, int max_lines)
  1055. {
  1056. struct dso *dso = map->dso;
  1057. const char *filename = dso->long_name;
  1058. struct rb_root source_line = RB_ROOT;
  1059. u64 len;
  1060. if (symbol__annotate(sym, map, 0) < 0)
  1061. return -1;
  1062. len = symbol__size(sym);
  1063. if (print_lines) {
  1064. symbol__get_source_line(sym, map, evsel, &source_line,
  1065. len, filename);
  1066. print_summary(&source_line, filename);
  1067. }
  1068. symbol__annotate_printf(sym, map, evsel, full_paths,
  1069. min_pcnt, max_lines, 0);
  1070. if (print_lines)
  1071. symbol__free_source_line(sym, len);
  1072. disasm__purge(&symbol__annotation(sym)->src->source);
  1073. return 0;
  1074. }