annotate.c 34 KB

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