annotate.c 40 KB

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