annotate.c 34 KB

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