annotate.c 34 KB

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