annotate.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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 "ui/ui.h"
  11. #include "sort.h"
  12. #include "build-id.h"
  13. #include "color.h"
  14. #include "cache.h"
  15. #include "symbol.h"
  16. #include "debug.h"
  17. #include "annotate.h"
  18. #include "evsel.h"
  19. #include <regex.h>
  20. #include <pthread.h>
  21. #include <linux/bitops.h>
  22. const char *disassembler_style;
  23. const char *objdump_path;
  24. static regex_t file_lineno;
  25. static struct ins *ins__find(const char *name);
  26. static int disasm_line__parse(char *line, char **namep, char **rawp);
  27. static void ins__delete(struct ins_operands *ops)
  28. {
  29. if (ops == NULL)
  30. return;
  31. zfree(&ops->source.raw);
  32. zfree(&ops->source.name);
  33. zfree(&ops->target.raw);
  34. zfree(&ops->target.name);
  35. }
  36. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  37. struct ins_operands *ops)
  38. {
  39. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
  40. }
  41. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  42. struct ins_operands *ops)
  43. {
  44. if (ins->ops->scnprintf)
  45. return ins->ops->scnprintf(ins, bf, size, ops);
  46. return ins__raw_scnprintf(ins, bf, size, ops);
  47. }
  48. static int call__parse(struct ins_operands *ops)
  49. {
  50. char *endptr, *tok, *name;
  51. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  52. name = strchr(endptr, '<');
  53. if (name == NULL)
  54. goto indirect_call;
  55. name++;
  56. tok = strchr(name, '>');
  57. if (tok == NULL)
  58. return -1;
  59. *tok = '\0';
  60. ops->target.name = strdup(name);
  61. *tok = '>';
  62. return ops->target.name == NULL ? -1 : 0;
  63. indirect_call:
  64. tok = strchr(endptr, '(');
  65. if (tok != NULL) {
  66. ops->target.addr = 0;
  67. return 0;
  68. }
  69. tok = strchr(endptr, '*');
  70. if (tok == NULL)
  71. return -1;
  72. ops->target.addr = strtoull(tok + 1, NULL, 16);
  73. return 0;
  74. }
  75. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  76. struct ins_operands *ops)
  77. {
  78. if (ops->target.name)
  79. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
  80. if (ops->target.addr == 0)
  81. return ins__raw_scnprintf(ins, bf, size, ops);
  82. return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
  83. }
  84. static struct ins_ops call_ops = {
  85. .parse = call__parse,
  86. .scnprintf = call__scnprintf,
  87. };
  88. bool ins__is_call(const struct ins *ins)
  89. {
  90. return ins->ops == &call_ops;
  91. }
  92. static int jump__parse(struct ins_operands *ops)
  93. {
  94. const char *s = strchr(ops->raw, '+');
  95. ops->target.addr = strtoull(ops->raw, NULL, 16);
  96. if (s++ != NULL)
  97. ops->target.offset = strtoull(s, NULL, 16);
  98. else
  99. ops->target.offset = UINT64_MAX;
  100. return 0;
  101. }
  102. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  103. struct ins_operands *ops)
  104. {
  105. return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
  106. }
  107. static struct ins_ops jump_ops = {
  108. .parse = jump__parse,
  109. .scnprintf = jump__scnprintf,
  110. };
  111. bool ins__is_jump(const struct ins *ins)
  112. {
  113. return ins->ops == &jump_ops;
  114. }
  115. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  116. {
  117. char *endptr, *name, *t;
  118. if (strstr(raw, "(%rip)") == NULL)
  119. return 0;
  120. *addrp = strtoull(comment, &endptr, 16);
  121. name = strchr(endptr, '<');
  122. if (name == NULL)
  123. return -1;
  124. name++;
  125. t = strchr(name, '>');
  126. if (t == NULL)
  127. return 0;
  128. *t = '\0';
  129. *namep = strdup(name);
  130. *t = '>';
  131. return 0;
  132. }
  133. static int lock__parse(struct ins_operands *ops)
  134. {
  135. char *name;
  136. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  137. if (ops->locked.ops == NULL)
  138. return 0;
  139. if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
  140. goto out_free_ops;
  141. ops->locked.ins = ins__find(name);
  142. free(name);
  143. if (ops->locked.ins == NULL)
  144. goto out_free_ops;
  145. if (!ops->locked.ins->ops)
  146. return 0;
  147. if (ops->locked.ins->ops->parse &&
  148. ops->locked.ins->ops->parse(ops->locked.ops) < 0)
  149. goto out_free_ops;
  150. return 0;
  151. out_free_ops:
  152. zfree(&ops->locked.ops);
  153. return 0;
  154. }
  155. static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
  156. struct ins_operands *ops)
  157. {
  158. int printed;
  159. if (ops->locked.ins == NULL)
  160. return ins__raw_scnprintf(ins, bf, size, ops);
  161. printed = scnprintf(bf, size, "%-6.6s ", ins->name);
  162. return printed + ins__scnprintf(ops->locked.ins, bf + printed,
  163. size - printed, ops->locked.ops);
  164. }
  165. static void lock__delete(struct ins_operands *ops)
  166. {
  167. struct ins *ins = ops->locked.ins;
  168. if (ins && ins->ops->free)
  169. ins->ops->free(ops->locked.ops);
  170. else
  171. ins__delete(ops->locked.ops);
  172. zfree(&ops->locked.ops);
  173. zfree(&ops->target.raw);
  174. zfree(&ops->target.name);
  175. }
  176. static struct ins_ops lock_ops = {
  177. .free = lock__delete,
  178. .parse = lock__parse,
  179. .scnprintf = lock__scnprintf,
  180. };
  181. static int mov__parse(struct ins_operands *ops)
  182. {
  183. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  184. if (s == NULL)
  185. return -1;
  186. *s = '\0';
  187. ops->source.raw = strdup(ops->raw);
  188. *s = ',';
  189. if (ops->source.raw == NULL)
  190. return -1;
  191. target = ++s;
  192. comment = strchr(s, '#');
  193. if (comment != NULL)
  194. s = comment - 1;
  195. else
  196. s = strchr(s, '\0') - 1;
  197. while (s > target && isspace(s[0]))
  198. --s;
  199. s++;
  200. prev = *s;
  201. *s = '\0';
  202. ops->target.raw = strdup(target);
  203. *s = prev;
  204. if (ops->target.raw == NULL)
  205. goto out_free_source;
  206. if (comment == NULL)
  207. return 0;
  208. while (comment[0] != '\0' && isspace(comment[0]))
  209. ++comment;
  210. comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
  211. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  212. return 0;
  213. out_free_source:
  214. zfree(&ops->source.raw);
  215. return -1;
  216. }
  217. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  218. struct ins_operands *ops)
  219. {
  220. return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
  221. ops->source.name ?: ops->source.raw,
  222. ops->target.name ?: ops->target.raw);
  223. }
  224. static struct ins_ops mov_ops = {
  225. .parse = mov__parse,
  226. .scnprintf = mov__scnprintf,
  227. };
  228. static int dec__parse(struct ins_operands *ops)
  229. {
  230. char *target, *comment, *s, prev;
  231. target = s = ops->raw;
  232. while (s[0] != '\0' && !isspace(s[0]))
  233. ++s;
  234. prev = *s;
  235. *s = '\0';
  236. ops->target.raw = strdup(target);
  237. *s = prev;
  238. if (ops->target.raw == NULL)
  239. return -1;
  240. comment = strchr(s, '#');
  241. if (comment == NULL)
  242. return 0;
  243. while (comment[0] != '\0' && isspace(comment[0]))
  244. ++comment;
  245. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  246. return 0;
  247. }
  248. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  249. struct ins_operands *ops)
  250. {
  251. return scnprintf(bf, size, "%-6.6s %s", ins->name,
  252. ops->target.name ?: ops->target.raw);
  253. }
  254. static struct ins_ops dec_ops = {
  255. .parse = dec__parse,
  256. .scnprintf = dec__scnprintf,
  257. };
  258. static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
  259. struct ins_operands *ops __maybe_unused)
  260. {
  261. return scnprintf(bf, size, "%-6.6s", "nop");
  262. }
  263. static struct ins_ops nop_ops = {
  264. .scnprintf = nop__scnprintf,
  265. };
  266. /*
  267. * Must be sorted by name!
  268. */
  269. static struct ins instructions[] = {
  270. { .name = "add", .ops = &mov_ops, },
  271. { .name = "addl", .ops = &mov_ops, },
  272. { .name = "addq", .ops = &mov_ops, },
  273. { .name = "addw", .ops = &mov_ops, },
  274. { .name = "and", .ops = &mov_ops, },
  275. { .name = "bts", .ops = &mov_ops, },
  276. { .name = "call", .ops = &call_ops, },
  277. { .name = "callq", .ops = &call_ops, },
  278. { .name = "cmp", .ops = &mov_ops, },
  279. { .name = "cmpb", .ops = &mov_ops, },
  280. { .name = "cmpl", .ops = &mov_ops, },
  281. { .name = "cmpq", .ops = &mov_ops, },
  282. { .name = "cmpw", .ops = &mov_ops, },
  283. { .name = "cmpxch", .ops = &mov_ops, },
  284. { .name = "dec", .ops = &dec_ops, },
  285. { .name = "decl", .ops = &dec_ops, },
  286. { .name = "imul", .ops = &mov_ops, },
  287. { .name = "inc", .ops = &dec_ops, },
  288. { .name = "incl", .ops = &dec_ops, },
  289. { .name = "ja", .ops = &jump_ops, },
  290. { .name = "jae", .ops = &jump_ops, },
  291. { .name = "jb", .ops = &jump_ops, },
  292. { .name = "jbe", .ops = &jump_ops, },
  293. { .name = "jc", .ops = &jump_ops, },
  294. { .name = "jcxz", .ops = &jump_ops, },
  295. { .name = "je", .ops = &jump_ops, },
  296. { .name = "jecxz", .ops = &jump_ops, },
  297. { .name = "jg", .ops = &jump_ops, },
  298. { .name = "jge", .ops = &jump_ops, },
  299. { .name = "jl", .ops = &jump_ops, },
  300. { .name = "jle", .ops = &jump_ops, },
  301. { .name = "jmp", .ops = &jump_ops, },
  302. { .name = "jmpq", .ops = &jump_ops, },
  303. { .name = "jna", .ops = &jump_ops, },
  304. { .name = "jnae", .ops = &jump_ops, },
  305. { .name = "jnb", .ops = &jump_ops, },
  306. { .name = "jnbe", .ops = &jump_ops, },
  307. { .name = "jnc", .ops = &jump_ops, },
  308. { .name = "jne", .ops = &jump_ops, },
  309. { .name = "jng", .ops = &jump_ops, },
  310. { .name = "jnge", .ops = &jump_ops, },
  311. { .name = "jnl", .ops = &jump_ops, },
  312. { .name = "jnle", .ops = &jump_ops, },
  313. { .name = "jno", .ops = &jump_ops, },
  314. { .name = "jnp", .ops = &jump_ops, },
  315. { .name = "jns", .ops = &jump_ops, },
  316. { .name = "jnz", .ops = &jump_ops, },
  317. { .name = "jo", .ops = &jump_ops, },
  318. { .name = "jp", .ops = &jump_ops, },
  319. { .name = "jpe", .ops = &jump_ops, },
  320. { .name = "jpo", .ops = &jump_ops, },
  321. { .name = "jrcxz", .ops = &jump_ops, },
  322. { .name = "js", .ops = &jump_ops, },
  323. { .name = "jz", .ops = &jump_ops, },
  324. { .name = "lea", .ops = &mov_ops, },
  325. { .name = "lock", .ops = &lock_ops, },
  326. { .name = "mov", .ops = &mov_ops, },
  327. { .name = "movb", .ops = &mov_ops, },
  328. { .name = "movdqa",.ops = &mov_ops, },
  329. { .name = "movl", .ops = &mov_ops, },
  330. { .name = "movq", .ops = &mov_ops, },
  331. { .name = "movslq", .ops = &mov_ops, },
  332. { .name = "movzbl", .ops = &mov_ops, },
  333. { .name = "movzwl", .ops = &mov_ops, },
  334. { .name = "nop", .ops = &nop_ops, },
  335. { .name = "nopl", .ops = &nop_ops, },
  336. { .name = "nopw", .ops = &nop_ops, },
  337. { .name = "or", .ops = &mov_ops, },
  338. { .name = "orl", .ops = &mov_ops, },
  339. { .name = "test", .ops = &mov_ops, },
  340. { .name = "testb", .ops = &mov_ops, },
  341. { .name = "testl", .ops = &mov_ops, },
  342. { .name = "xadd", .ops = &mov_ops, },
  343. { .name = "xbeginl", .ops = &jump_ops, },
  344. { .name = "xbeginq", .ops = &jump_ops, },
  345. };
  346. static int ins__cmp(const void *name, const void *insp)
  347. {
  348. const struct ins *ins = insp;
  349. return strcmp(name, ins->name);
  350. }
  351. static struct ins *ins__find(const char *name)
  352. {
  353. const int nmemb = ARRAY_SIZE(instructions);
  354. return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
  355. }
  356. int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
  357. {
  358. struct annotation *notes = symbol__annotation(sym);
  359. pthread_mutex_init(&notes->lock, NULL);
  360. return 0;
  361. }
  362. int symbol__alloc_hist(struct symbol *sym)
  363. {
  364. struct annotation *notes = symbol__annotation(sym);
  365. const size_t size = symbol__size(sym);
  366. size_t sizeof_sym_hist;
  367. /* Check for overflow when calculating sizeof_sym_hist */
  368. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
  369. return -1;
  370. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
  371. /* Check for overflow in zalloc argument */
  372. if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
  373. / symbol_conf.nr_events)
  374. return -1;
  375. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  376. if (notes->src == NULL)
  377. return -1;
  378. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  379. notes->src->nr_histograms = symbol_conf.nr_events;
  380. INIT_LIST_HEAD(&notes->src->source);
  381. return 0;
  382. }
  383. /* The cycles histogram is lazily allocated. */
  384. static int symbol__alloc_hist_cycles(struct symbol *sym)
  385. {
  386. struct annotation *notes = symbol__annotation(sym);
  387. const size_t size = symbol__size(sym);
  388. notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
  389. if (notes->src->cycles_hist == NULL)
  390. return -1;
  391. return 0;
  392. }
  393. void symbol__annotate_zero_histograms(struct symbol *sym)
  394. {
  395. struct annotation *notes = symbol__annotation(sym);
  396. pthread_mutex_lock(&notes->lock);
  397. if (notes->src != NULL) {
  398. memset(notes->src->histograms, 0,
  399. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  400. if (notes->src->cycles_hist)
  401. memset(notes->src->cycles_hist, 0,
  402. symbol__size(sym) * sizeof(struct cyc_hist));
  403. }
  404. pthread_mutex_unlock(&notes->lock);
  405. }
  406. static int __symbol__account_cycles(struct annotation *notes,
  407. u64 start,
  408. unsigned offset, unsigned cycles,
  409. unsigned have_start)
  410. {
  411. struct cyc_hist *ch;
  412. ch = notes->src->cycles_hist;
  413. /*
  414. * For now we can only account one basic block per
  415. * final jump. But multiple could be overlapping.
  416. * Always account the longest one. So when
  417. * a shorter one has been already seen throw it away.
  418. *
  419. * We separately always account the full cycles.
  420. */
  421. ch[offset].num_aggr++;
  422. ch[offset].cycles_aggr += cycles;
  423. if (!have_start && ch[offset].have_start)
  424. return 0;
  425. if (ch[offset].num) {
  426. if (have_start && (!ch[offset].have_start ||
  427. ch[offset].start > start)) {
  428. ch[offset].have_start = 0;
  429. ch[offset].cycles = 0;
  430. ch[offset].num = 0;
  431. if (ch[offset].reset < 0xffff)
  432. ch[offset].reset++;
  433. } else if (have_start &&
  434. ch[offset].start < start)
  435. return 0;
  436. }
  437. ch[offset].have_start = have_start;
  438. ch[offset].start = start;
  439. ch[offset].cycles += cycles;
  440. ch[offset].num++;
  441. return 0;
  442. }
  443. static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  444. struct annotation *notes, int evidx, u64 addr)
  445. {
  446. unsigned offset;
  447. struct sym_hist *h;
  448. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  449. if (addr < sym->start || addr >= sym->end)
  450. return -ERANGE;
  451. offset = addr - sym->start;
  452. h = annotation__histogram(notes, evidx);
  453. h->sum++;
  454. h->addr[offset]++;
  455. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  456. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  457. addr, addr - sym->start, evidx, h->addr[offset]);
  458. return 0;
  459. }
  460. static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
  461. {
  462. struct annotation *notes = symbol__annotation(sym);
  463. if (notes->src == NULL) {
  464. if (symbol__alloc_hist(sym) < 0)
  465. return NULL;
  466. }
  467. if (!notes->src->cycles_hist && cycles) {
  468. if (symbol__alloc_hist_cycles(sym) < 0)
  469. return NULL;
  470. }
  471. return notes;
  472. }
  473. static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  474. int evidx, u64 addr)
  475. {
  476. struct annotation *notes;
  477. if (sym == NULL)
  478. return 0;
  479. notes = symbol__get_annotation(sym, false);
  480. if (notes == NULL)
  481. return -ENOMEM;
  482. return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
  483. }
  484. static int symbol__account_cycles(u64 addr, u64 start,
  485. struct symbol *sym, unsigned cycles)
  486. {
  487. struct annotation *notes;
  488. unsigned offset;
  489. if (sym == NULL)
  490. return 0;
  491. notes = symbol__get_annotation(sym, true);
  492. if (notes == NULL)
  493. return -ENOMEM;
  494. if (addr < sym->start || addr >= sym->end)
  495. return -ERANGE;
  496. if (start) {
  497. if (start < sym->start || start >= sym->end)
  498. return -ERANGE;
  499. if (start >= addr)
  500. start = 0;
  501. }
  502. offset = addr - sym->start;
  503. return __symbol__account_cycles(notes,
  504. start ? start - sym->start : 0,
  505. offset, cycles,
  506. !!start);
  507. }
  508. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  509. struct addr_map_symbol *start,
  510. unsigned cycles)
  511. {
  512. u64 saddr = 0;
  513. int err;
  514. if (!cycles)
  515. return 0;
  516. /*
  517. * Only set start when IPC can be computed. We can only
  518. * compute it when the basic block is completely in a single
  519. * function.
  520. * Special case the case when the jump is elsewhere, but
  521. * it starts on the function start.
  522. */
  523. if (start &&
  524. (start->sym == ams->sym ||
  525. (ams->sym &&
  526. start->addr == ams->sym->start + ams->map->start)))
  527. saddr = start->al_addr;
  528. if (saddr == 0)
  529. pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
  530. ams->addr,
  531. start ? start->addr : 0,
  532. ams->sym ? ams->sym->start + ams->map->start : 0,
  533. saddr);
  534. err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
  535. if (err)
  536. pr_debug2("account_cycles failed %d\n", err);
  537. return err;
  538. }
  539. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
  540. {
  541. return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
  542. }
  543. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  544. {
  545. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  546. }
  547. static void disasm_line__init_ins(struct disasm_line *dl)
  548. {
  549. dl->ins = ins__find(dl->name);
  550. if (dl->ins == NULL)
  551. return;
  552. if (!dl->ins->ops)
  553. return;
  554. if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops) < 0)
  555. dl->ins = NULL;
  556. }
  557. static int disasm_line__parse(char *line, char **namep, char **rawp)
  558. {
  559. char *name = line, tmp;
  560. while (isspace(name[0]))
  561. ++name;
  562. if (name[0] == '\0')
  563. return -1;
  564. *rawp = name + 1;
  565. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  566. ++*rawp;
  567. tmp = (*rawp)[0];
  568. (*rawp)[0] = '\0';
  569. *namep = strdup(name);
  570. if (*namep == NULL)
  571. goto out_free_name;
  572. (*rawp)[0] = tmp;
  573. if ((*rawp)[0] != '\0') {
  574. (*rawp)++;
  575. while (isspace((*rawp)[0]))
  576. ++(*rawp);
  577. }
  578. return 0;
  579. out_free_name:
  580. zfree(namep);
  581. return -1;
  582. }
  583. static struct disasm_line *disasm_line__new(s64 offset, char *line,
  584. size_t privsize, int line_nr)
  585. {
  586. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  587. if (dl != NULL) {
  588. dl->offset = offset;
  589. dl->line = strdup(line);
  590. dl->line_nr = line_nr;
  591. if (dl->line == NULL)
  592. goto out_delete;
  593. if (offset != -1) {
  594. if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
  595. goto out_free_line;
  596. disasm_line__init_ins(dl);
  597. }
  598. }
  599. return dl;
  600. out_free_line:
  601. zfree(&dl->line);
  602. out_delete:
  603. free(dl);
  604. return NULL;
  605. }
  606. void disasm_line__free(struct disasm_line *dl)
  607. {
  608. zfree(&dl->line);
  609. zfree(&dl->name);
  610. if (dl->ins && dl->ins->ops->free)
  611. dl->ins->ops->free(&dl->ops);
  612. else
  613. ins__delete(&dl->ops);
  614. free(dl);
  615. }
  616. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  617. {
  618. if (raw || !dl->ins)
  619. return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
  620. return ins__scnprintf(dl->ins, bf, size, &dl->ops);
  621. }
  622. static void disasm__add(struct list_head *head, struct disasm_line *line)
  623. {
  624. list_add_tail(&line->node, head);
  625. }
  626. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  627. {
  628. list_for_each_entry_continue(pos, head, node)
  629. if (pos->offset >= 0)
  630. return pos;
  631. return NULL;
  632. }
  633. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  634. s64 end, const char **path, u64 *nr_samples)
  635. {
  636. struct source_line *src_line = notes->src->lines;
  637. double percent = 0.0;
  638. *nr_samples = 0;
  639. if (src_line) {
  640. size_t sizeof_src_line = sizeof(*src_line) +
  641. sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
  642. while (offset < end) {
  643. src_line = (void *)notes->src->lines +
  644. (sizeof_src_line * offset);
  645. if (*path == NULL)
  646. *path = src_line->path;
  647. percent += src_line->samples[evidx].percent;
  648. *nr_samples += src_line->samples[evidx].nr;
  649. offset++;
  650. }
  651. } else {
  652. struct sym_hist *h = annotation__histogram(notes, evidx);
  653. unsigned int hits = 0;
  654. while (offset < end)
  655. hits += h->addr[offset++];
  656. if (h->sum) {
  657. *nr_samples = hits;
  658. percent = 100.0 * hits / h->sum;
  659. }
  660. }
  661. return percent;
  662. }
  663. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  664. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  665. int max_lines, struct disasm_line *queue)
  666. {
  667. static const char *prev_line;
  668. static const char *prev_color;
  669. if (dl->offset != -1) {
  670. const char *path = NULL;
  671. u64 nr_samples;
  672. double percent, max_percent = 0.0;
  673. double *ppercents = &percent;
  674. u64 *psamples = &nr_samples;
  675. int i, nr_percent = 1;
  676. const char *color;
  677. struct annotation *notes = symbol__annotation(sym);
  678. s64 offset = dl->offset;
  679. const u64 addr = start + offset;
  680. struct disasm_line *next;
  681. next = disasm__get_next_ip_line(&notes->src->source, dl);
  682. if (perf_evsel__is_group_event(evsel)) {
  683. nr_percent = evsel->nr_members;
  684. ppercents = calloc(nr_percent, sizeof(double));
  685. psamples = calloc(nr_percent, sizeof(u64));
  686. if (ppercents == NULL || psamples == NULL) {
  687. return -1;
  688. }
  689. }
  690. for (i = 0; i < nr_percent; i++) {
  691. percent = disasm__calc_percent(notes,
  692. notes->src->lines ? i : evsel->idx + i,
  693. offset,
  694. next ? next->offset : (s64) len,
  695. &path, &nr_samples);
  696. ppercents[i] = percent;
  697. psamples[i] = nr_samples;
  698. if (percent > max_percent)
  699. max_percent = percent;
  700. }
  701. if (max_percent < min_pcnt)
  702. return -1;
  703. if (max_lines && printed >= max_lines)
  704. return 1;
  705. if (queue != NULL) {
  706. list_for_each_entry_from(queue, &notes->src->source, node) {
  707. if (queue == dl)
  708. break;
  709. disasm_line__print(queue, sym, start, evsel, len,
  710. 0, 0, 1, NULL);
  711. }
  712. }
  713. color = get_percent_color(max_percent);
  714. /*
  715. * Also color the filename and line if needed, with
  716. * the same color than the percentage. Don't print it
  717. * twice for close colored addr with the same filename:line
  718. */
  719. if (path) {
  720. if (!prev_line || strcmp(prev_line, path)
  721. || color != prev_color) {
  722. color_fprintf(stdout, color, " %s", path);
  723. prev_line = path;
  724. prev_color = color;
  725. }
  726. }
  727. for (i = 0; i < nr_percent; i++) {
  728. percent = ppercents[i];
  729. nr_samples = psamples[i];
  730. color = get_percent_color(percent);
  731. if (symbol_conf.show_total_period)
  732. color_fprintf(stdout, color, " %7" PRIu64,
  733. nr_samples);
  734. else
  735. color_fprintf(stdout, color, " %7.2f", percent);
  736. }
  737. printf(" : ");
  738. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  739. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  740. if (ppercents != &percent)
  741. free(ppercents);
  742. if (psamples != &nr_samples)
  743. free(psamples);
  744. } else if (max_lines && printed >= max_lines)
  745. return 1;
  746. else {
  747. int width = 8;
  748. if (queue)
  749. return -1;
  750. if (perf_evsel__is_group_event(evsel))
  751. width *= evsel->nr_members;
  752. if (!*dl->line)
  753. printf(" %*s:\n", width, " ");
  754. else
  755. printf(" %*s: %s\n", width, " ", dl->line);
  756. }
  757. return 0;
  758. }
  759. /*
  760. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  761. * which looks like following
  762. *
  763. * 0000000000415500 <_init>:
  764. * 415500: sub $0x8,%rsp
  765. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  766. * 41550b: test %rax,%rax
  767. * 41550e: je 415515 <_init+0x15>
  768. * 415510: callq 416e70 <__gmon_start__@plt>
  769. * 415515: add $0x8,%rsp
  770. * 415519: retq
  771. *
  772. * it will be parsed and saved into struct disasm_line as
  773. * <offset> <name> <ops.raw>
  774. *
  775. * The offset will be a relative offset from the start of the symbol and -1
  776. * means that it's not a disassembly line so should be treated differently.
  777. * The ops.raw part will be parsed further according to type of the instruction.
  778. */
  779. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  780. FILE *file, size_t privsize,
  781. int *line_nr)
  782. {
  783. struct annotation *notes = symbol__annotation(sym);
  784. struct disasm_line *dl;
  785. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  786. size_t line_len;
  787. s64 line_ip, offset = -1;
  788. regmatch_t match[2];
  789. if (getline(&line, &line_len, file) < 0)
  790. return -1;
  791. if (!line)
  792. return -1;
  793. while (line_len != 0 && isspace(line[line_len - 1]))
  794. line[--line_len] = '\0';
  795. c = strchr(line, '\n');
  796. if (c)
  797. *c = 0;
  798. line_ip = -1;
  799. parsed_line = line;
  800. /* /filename:linenr ? Save line number and ignore. */
  801. if (regexec(&file_lineno, line, 2, match, 0) == 0) {
  802. *line_nr = atoi(line + match[1].rm_so);
  803. return 0;
  804. }
  805. /*
  806. * Strip leading spaces:
  807. */
  808. tmp = line;
  809. while (*tmp) {
  810. if (*tmp != ' ')
  811. break;
  812. tmp++;
  813. }
  814. if (*tmp) {
  815. /*
  816. * Parse hexa addresses followed by ':'
  817. */
  818. line_ip = strtoull(tmp, &tmp2, 16);
  819. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  820. line_ip = -1;
  821. }
  822. if (line_ip != -1) {
  823. u64 start = map__rip_2objdump(map, sym->start),
  824. end = map__rip_2objdump(map, sym->end);
  825. offset = line_ip - start;
  826. if ((u64)line_ip < start || (u64)line_ip >= end)
  827. offset = -1;
  828. else
  829. parsed_line = tmp2 + 1;
  830. }
  831. dl = disasm_line__new(offset, parsed_line, privsize, *line_nr);
  832. free(line);
  833. (*line_nr)++;
  834. if (dl == NULL)
  835. return -1;
  836. if (dl->ops.target.offset == UINT64_MAX)
  837. dl->ops.target.offset = dl->ops.target.addr -
  838. map__rip_2objdump(map, sym->start);
  839. /* kcore has no symbols, so add the call target name */
  840. if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
  841. struct addr_map_symbol target = {
  842. .map = map,
  843. .addr = dl->ops.target.addr,
  844. };
  845. if (!map_groups__find_ams(&target, NULL) &&
  846. target.sym->start == target.al_addr)
  847. dl->ops.target.name = strdup(target.sym->name);
  848. }
  849. disasm__add(&notes->src->source, dl);
  850. return 0;
  851. }
  852. static __attribute__((constructor)) void symbol__init_regexpr(void)
  853. {
  854. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  855. }
  856. static void delete_last_nop(struct symbol *sym)
  857. {
  858. struct annotation *notes = symbol__annotation(sym);
  859. struct list_head *list = &notes->src->source;
  860. struct disasm_line *dl;
  861. while (!list_empty(list)) {
  862. dl = list_entry(list->prev, struct disasm_line, node);
  863. if (dl->ins && dl->ins->ops) {
  864. if (dl->ins->ops != &nop_ops)
  865. return;
  866. } else {
  867. if (!strstr(dl->line, " nop ") &&
  868. !strstr(dl->line, " nopl ") &&
  869. !strstr(dl->line, " nopw "))
  870. return;
  871. }
  872. list_del(&dl->node);
  873. disasm_line__free(dl);
  874. }
  875. }
  876. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  877. {
  878. struct dso *dso = map->dso;
  879. char *filename = dso__build_id_filename(dso, NULL, 0);
  880. bool free_filename = true;
  881. char command[PATH_MAX * 2];
  882. FILE *file;
  883. int err = 0;
  884. char symfs_filename[PATH_MAX];
  885. struct kcore_extract kce;
  886. bool delete_extract = false;
  887. int lineno = 0;
  888. if (filename)
  889. symbol__join_symfs(symfs_filename, filename);
  890. if (filename == NULL) {
  891. if (dso->has_build_id) {
  892. pr_err("Can't annotate %s: not enough memory\n",
  893. sym->name);
  894. return -ENOMEM;
  895. }
  896. goto fallback;
  897. } else if (dso__is_kcore(dso)) {
  898. goto fallback;
  899. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  900. strstr(command, "[kernel.kallsyms]") ||
  901. access(symfs_filename, R_OK)) {
  902. free(filename);
  903. fallback:
  904. /*
  905. * If we don't have build-ids or the build-id file isn't in the
  906. * cache, or is just a kallsyms file, well, lets hope that this
  907. * DSO is the same as when 'perf record' ran.
  908. */
  909. filename = (char *)dso->long_name;
  910. symbol__join_symfs(symfs_filename, filename);
  911. free_filename = false;
  912. }
  913. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  914. !dso__is_kcore(dso)) {
  915. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  916. char *build_id_msg = NULL;
  917. if (dso->annotate_warned)
  918. goto out_free_filename;
  919. if (dso->has_build_id) {
  920. build_id__sprintf(dso->build_id,
  921. sizeof(dso->build_id), bf + 15);
  922. build_id_msg = bf;
  923. }
  924. err = -ENOENT;
  925. dso->annotate_warned = 1;
  926. pr_err("Can't annotate %s:\n\n"
  927. "No vmlinux file%s\nwas found in the path.\n\n"
  928. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  929. "Please use:\n\n"
  930. " perf buildid-cache -vu vmlinux\n\n"
  931. "or:\n\n"
  932. " --vmlinux vmlinux\n",
  933. sym->name, build_id_msg ?: "");
  934. goto out_free_filename;
  935. }
  936. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  937. filename, sym->name, map->unmap_ip(map, sym->start),
  938. map->unmap_ip(map, sym->end));
  939. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  940. dso, dso->long_name, sym, sym->name);
  941. if (dso__is_kcore(dso)) {
  942. kce.kcore_filename = symfs_filename;
  943. kce.addr = map__rip_2objdump(map, sym->start);
  944. kce.offs = sym->start;
  945. kce.len = sym->end - sym->start;
  946. if (!kcore_extract__create(&kce)) {
  947. delete_extract = true;
  948. strlcpy(symfs_filename, kce.extract_filename,
  949. sizeof(symfs_filename));
  950. if (free_filename) {
  951. free(filename);
  952. free_filename = false;
  953. }
  954. filename = symfs_filename;
  955. }
  956. } else if (dso__needs_decompress(dso)) {
  957. char tmp[PATH_MAX];
  958. struct kmod_path m;
  959. int fd;
  960. bool ret;
  961. if (kmod_path__parse_ext(&m, symfs_filename))
  962. goto out_free_filename;
  963. snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
  964. fd = mkstemp(tmp);
  965. if (fd < 0) {
  966. free(m.ext);
  967. goto out_free_filename;
  968. }
  969. ret = decompress_to_file(m.ext, symfs_filename, fd);
  970. free(m.ext);
  971. close(fd);
  972. if (!ret)
  973. goto out_free_filename;
  974. strcpy(symfs_filename, tmp);
  975. }
  976. snprintf(command, sizeof(command),
  977. "%s %s%s --start-address=0x%016" PRIx64
  978. " --stop-address=0x%016" PRIx64
  979. " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
  980. objdump_path ? objdump_path : "objdump",
  981. disassembler_style ? "-M " : "",
  982. disassembler_style ? disassembler_style : "",
  983. map__rip_2objdump(map, sym->start),
  984. map__rip_2objdump(map, sym->end),
  985. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  986. symbol_conf.annotate_src ? "-S" : "",
  987. symfs_filename, filename);
  988. pr_debug("Executing: %s\n", command);
  989. file = popen(command, "r");
  990. if (!file)
  991. goto out_remove_tmp;
  992. while (!feof(file))
  993. if (symbol__parse_objdump_line(sym, map, file, privsize,
  994. &lineno) < 0)
  995. break;
  996. /*
  997. * kallsyms does not have symbol sizes so there may a nop at the end.
  998. * Remove it.
  999. */
  1000. if (dso__is_kcore(dso))
  1001. delete_last_nop(sym);
  1002. pclose(file);
  1003. out_remove_tmp:
  1004. if (dso__needs_decompress(dso))
  1005. unlink(symfs_filename);
  1006. out_free_filename:
  1007. if (delete_extract)
  1008. kcore_extract__delete(&kce);
  1009. if (free_filename)
  1010. free(filename);
  1011. return err;
  1012. }
  1013. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  1014. {
  1015. struct source_line *iter;
  1016. struct rb_node **p = &root->rb_node;
  1017. struct rb_node *parent = NULL;
  1018. int i, ret;
  1019. while (*p != NULL) {
  1020. parent = *p;
  1021. iter = rb_entry(parent, struct source_line, node);
  1022. ret = strcmp(iter->path, src_line->path);
  1023. if (ret == 0) {
  1024. for (i = 0; i < src_line->nr_pcnt; i++)
  1025. iter->samples[i].percent_sum += src_line->samples[i].percent;
  1026. return;
  1027. }
  1028. if (ret < 0)
  1029. p = &(*p)->rb_left;
  1030. else
  1031. p = &(*p)->rb_right;
  1032. }
  1033. for (i = 0; i < src_line->nr_pcnt; i++)
  1034. src_line->samples[i].percent_sum = src_line->samples[i].percent;
  1035. rb_link_node(&src_line->node, parent, p);
  1036. rb_insert_color(&src_line->node, root);
  1037. }
  1038. static int cmp_source_line(struct source_line *a, struct source_line *b)
  1039. {
  1040. int i;
  1041. for (i = 0; i < a->nr_pcnt; i++) {
  1042. if (a->samples[i].percent_sum == b->samples[i].percent_sum)
  1043. continue;
  1044. return a->samples[i].percent_sum > b->samples[i].percent_sum;
  1045. }
  1046. return 0;
  1047. }
  1048. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  1049. {
  1050. struct source_line *iter;
  1051. struct rb_node **p = &root->rb_node;
  1052. struct rb_node *parent = NULL;
  1053. while (*p != NULL) {
  1054. parent = *p;
  1055. iter = rb_entry(parent, struct source_line, node);
  1056. if (cmp_source_line(src_line, iter))
  1057. p = &(*p)->rb_left;
  1058. else
  1059. p = &(*p)->rb_right;
  1060. }
  1061. rb_link_node(&src_line->node, parent, p);
  1062. rb_insert_color(&src_line->node, root);
  1063. }
  1064. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1065. {
  1066. struct source_line *src_line;
  1067. struct rb_node *node;
  1068. node = rb_first(src_root);
  1069. while (node) {
  1070. struct rb_node *next;
  1071. src_line = rb_entry(node, struct source_line, node);
  1072. next = rb_next(node);
  1073. rb_erase(node, src_root);
  1074. __resort_source_line(dest_root, src_line);
  1075. node = next;
  1076. }
  1077. }
  1078. static void symbol__free_source_line(struct symbol *sym, int len)
  1079. {
  1080. struct annotation *notes = symbol__annotation(sym);
  1081. struct source_line *src_line = notes->src->lines;
  1082. size_t sizeof_src_line;
  1083. int i;
  1084. sizeof_src_line = sizeof(*src_line) +
  1085. (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
  1086. for (i = 0; i < len; i++) {
  1087. free_srcline(src_line->path);
  1088. src_line = (void *)src_line + sizeof_src_line;
  1089. }
  1090. zfree(&notes->src->lines);
  1091. }
  1092. /* Get the filename:line for the colored entries */
  1093. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  1094. struct perf_evsel *evsel,
  1095. struct rb_root *root, int len)
  1096. {
  1097. u64 start;
  1098. int i, k;
  1099. int evidx = evsel->idx;
  1100. struct source_line *src_line;
  1101. struct annotation *notes = symbol__annotation(sym);
  1102. struct sym_hist *h = annotation__histogram(notes, evidx);
  1103. struct rb_root tmp_root = RB_ROOT;
  1104. int nr_pcnt = 1;
  1105. u64 h_sum = h->sum;
  1106. size_t sizeof_src_line = sizeof(struct source_line);
  1107. if (perf_evsel__is_group_event(evsel)) {
  1108. for (i = 1; i < evsel->nr_members; i++) {
  1109. h = annotation__histogram(notes, evidx + i);
  1110. h_sum += h->sum;
  1111. }
  1112. nr_pcnt = evsel->nr_members;
  1113. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
  1114. }
  1115. if (!h_sum)
  1116. return 0;
  1117. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  1118. if (!notes->src->lines)
  1119. return -1;
  1120. start = map__rip_2objdump(map, sym->start);
  1121. for (i = 0; i < len; i++) {
  1122. u64 offset;
  1123. double percent_max = 0.0;
  1124. src_line->nr_pcnt = nr_pcnt;
  1125. for (k = 0; k < nr_pcnt; k++) {
  1126. h = annotation__histogram(notes, evidx + k);
  1127. src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
  1128. if (src_line->samples[k].percent > percent_max)
  1129. percent_max = src_line->samples[k].percent;
  1130. }
  1131. if (percent_max <= 0.5)
  1132. goto next;
  1133. offset = start + i;
  1134. src_line->path = get_srcline(map->dso, offset, NULL, false);
  1135. insert_source_line(&tmp_root, src_line);
  1136. next:
  1137. src_line = (void *)src_line + sizeof_src_line;
  1138. }
  1139. resort_source_line(root, &tmp_root);
  1140. return 0;
  1141. }
  1142. static void print_summary(struct rb_root *root, const char *filename)
  1143. {
  1144. struct source_line *src_line;
  1145. struct rb_node *node;
  1146. printf("\nSorted summary for file %s\n", filename);
  1147. printf("----------------------------------------------\n\n");
  1148. if (RB_EMPTY_ROOT(root)) {
  1149. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1150. return;
  1151. }
  1152. node = rb_first(root);
  1153. while (node) {
  1154. double percent, percent_max = 0.0;
  1155. const char *color;
  1156. char *path;
  1157. int i;
  1158. src_line = rb_entry(node, struct source_line, node);
  1159. for (i = 0; i < src_line->nr_pcnt; i++) {
  1160. percent = src_line->samples[i].percent_sum;
  1161. color = get_percent_color(percent);
  1162. color_fprintf(stdout, color, " %7.2f", percent);
  1163. if (percent > percent_max)
  1164. percent_max = percent;
  1165. }
  1166. path = src_line->path;
  1167. color = get_percent_color(percent_max);
  1168. color_fprintf(stdout, color, " %s\n", path);
  1169. node = rb_next(node);
  1170. }
  1171. }
  1172. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1173. {
  1174. struct annotation *notes = symbol__annotation(sym);
  1175. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1176. u64 len = symbol__size(sym), offset;
  1177. for (offset = 0; offset < len; ++offset)
  1178. if (h->addr[offset] != 0)
  1179. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1180. sym->start + offset, h->addr[offset]);
  1181. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  1182. }
  1183. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1184. struct perf_evsel *evsel, bool full_paths,
  1185. int min_pcnt, int max_lines, int context)
  1186. {
  1187. struct dso *dso = map->dso;
  1188. char *filename;
  1189. const char *d_filename;
  1190. const char *evsel_name = perf_evsel__name(evsel);
  1191. struct annotation *notes = symbol__annotation(sym);
  1192. struct disasm_line *pos, *queue = NULL;
  1193. u64 start = map__rip_2objdump(map, sym->start);
  1194. int printed = 2, queue_len = 0;
  1195. int more = 0;
  1196. u64 len;
  1197. int width = 8;
  1198. int namelen, evsel_name_len, graph_dotted_len;
  1199. filename = strdup(dso->long_name);
  1200. if (!filename)
  1201. return -ENOMEM;
  1202. if (full_paths)
  1203. d_filename = filename;
  1204. else
  1205. d_filename = basename(filename);
  1206. len = symbol__size(sym);
  1207. namelen = strlen(d_filename);
  1208. evsel_name_len = strlen(evsel_name);
  1209. if (perf_evsel__is_group_event(evsel))
  1210. width *= evsel->nr_members;
  1211. printf(" %-*.*s| Source code & Disassembly of %s for %s\n",
  1212. width, width, "Percent", d_filename, evsel_name);
  1213. graph_dotted_len = width + namelen + evsel_name_len;
  1214. printf("-%-*.*s-----------------------------------------\n",
  1215. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1216. if (verbose)
  1217. symbol__annotate_hits(sym, evsel);
  1218. list_for_each_entry(pos, &notes->src->source, node) {
  1219. if (context && queue == NULL) {
  1220. queue = pos;
  1221. queue_len = 0;
  1222. }
  1223. switch (disasm_line__print(pos, sym, start, evsel, len,
  1224. min_pcnt, printed, max_lines,
  1225. queue)) {
  1226. case 0:
  1227. ++printed;
  1228. if (context) {
  1229. printed += queue_len;
  1230. queue = NULL;
  1231. queue_len = 0;
  1232. }
  1233. break;
  1234. case 1:
  1235. /* filtered by max_lines */
  1236. ++more;
  1237. break;
  1238. case -1:
  1239. default:
  1240. /*
  1241. * Filtered by min_pcnt or non IP lines when
  1242. * context != 0
  1243. */
  1244. if (!context)
  1245. break;
  1246. if (queue_len == context)
  1247. queue = list_entry(queue->node.next, typeof(*queue), node);
  1248. else
  1249. ++queue_len;
  1250. break;
  1251. }
  1252. }
  1253. free(filename);
  1254. return more;
  1255. }
  1256. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1257. {
  1258. struct annotation *notes = symbol__annotation(sym);
  1259. struct sym_hist *h = annotation__histogram(notes, evidx);
  1260. memset(h, 0, notes->src->sizeof_sym_hist);
  1261. }
  1262. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1263. {
  1264. struct annotation *notes = symbol__annotation(sym);
  1265. struct sym_hist *h = annotation__histogram(notes, evidx);
  1266. int len = symbol__size(sym), offset;
  1267. h->sum = 0;
  1268. for (offset = 0; offset < len; ++offset) {
  1269. h->addr[offset] = h->addr[offset] * 7 / 8;
  1270. h->sum += h->addr[offset];
  1271. }
  1272. }
  1273. void disasm__purge(struct list_head *head)
  1274. {
  1275. struct disasm_line *pos, *n;
  1276. list_for_each_entry_safe(pos, n, head, node) {
  1277. list_del(&pos->node);
  1278. disasm_line__free(pos);
  1279. }
  1280. }
  1281. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1282. {
  1283. size_t printed;
  1284. if (dl->offset == -1)
  1285. return fprintf(fp, "%s\n", dl->line);
  1286. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  1287. if (dl->ops.raw[0] != '\0') {
  1288. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1289. dl->ops.raw);
  1290. }
  1291. return printed + fprintf(fp, "\n");
  1292. }
  1293. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1294. {
  1295. struct disasm_line *pos;
  1296. size_t printed = 0;
  1297. list_for_each_entry(pos, head, node)
  1298. printed += disasm_line__fprintf(pos, fp);
  1299. return printed;
  1300. }
  1301. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1302. struct perf_evsel *evsel, bool print_lines,
  1303. bool full_paths, int min_pcnt, int max_lines)
  1304. {
  1305. struct dso *dso = map->dso;
  1306. struct rb_root source_line = RB_ROOT;
  1307. u64 len;
  1308. if (symbol__annotate(sym, map, 0) < 0)
  1309. return -1;
  1310. len = symbol__size(sym);
  1311. if (print_lines) {
  1312. symbol__get_source_line(sym, map, evsel, &source_line, len);
  1313. print_summary(&source_line, dso->long_name);
  1314. }
  1315. symbol__annotate_printf(sym, map, evsel, full_paths,
  1316. min_pcnt, max_lines, 0);
  1317. if (print_lines)
  1318. symbol__free_source_line(sym, len);
  1319. disasm__purge(&symbol__annotation(sym)->src->source);
  1320. return 0;
  1321. }
  1322. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  1323. {
  1324. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  1325. }
  1326. bool ui__has_annotation(void)
  1327. {
  1328. return use_browser == 1 && sort__has_sym;
  1329. }