annotate.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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 struct annotation *symbol__get_annotation(struct symbol *sym)
  410. {
  411. struct annotation *notes = symbol__annotation(sym);
  412. if (notes->src == NULL) {
  413. if (symbol__alloc_hist(sym) < 0)
  414. return NULL;
  415. }
  416. return notes;
  417. }
  418. static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  419. int evidx, u64 addr)
  420. {
  421. struct annotation *notes;
  422. if (sym == NULL)
  423. return 0;
  424. notes = symbol__get_annotation(sym);
  425. if (notes == NULL)
  426. return -ENOMEM;
  427. return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
  428. }
  429. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
  430. {
  431. return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
  432. }
  433. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  434. {
  435. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  436. }
  437. static void disasm_line__init_ins(struct disasm_line *dl)
  438. {
  439. dl->ins = ins__find(dl->name);
  440. if (dl->ins == NULL)
  441. return;
  442. if (!dl->ins->ops)
  443. return;
  444. if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops) < 0)
  445. dl->ins = NULL;
  446. }
  447. static int disasm_line__parse(char *line, char **namep, char **rawp)
  448. {
  449. char *name = line, tmp;
  450. while (isspace(name[0]))
  451. ++name;
  452. if (name[0] == '\0')
  453. return -1;
  454. *rawp = name + 1;
  455. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  456. ++*rawp;
  457. tmp = (*rawp)[0];
  458. (*rawp)[0] = '\0';
  459. *namep = strdup(name);
  460. if (*namep == NULL)
  461. goto out_free_name;
  462. (*rawp)[0] = tmp;
  463. if ((*rawp)[0] != '\0') {
  464. (*rawp)++;
  465. while (isspace((*rawp)[0]))
  466. ++(*rawp);
  467. }
  468. return 0;
  469. out_free_name:
  470. zfree(namep);
  471. return -1;
  472. }
  473. static struct disasm_line *disasm_line__new(s64 offset, char *line,
  474. size_t privsize, int line_nr)
  475. {
  476. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  477. if (dl != NULL) {
  478. dl->offset = offset;
  479. dl->line = strdup(line);
  480. dl->line_nr = line_nr;
  481. if (dl->line == NULL)
  482. goto out_delete;
  483. if (offset != -1) {
  484. if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
  485. goto out_free_line;
  486. disasm_line__init_ins(dl);
  487. }
  488. }
  489. return dl;
  490. out_free_line:
  491. zfree(&dl->line);
  492. out_delete:
  493. free(dl);
  494. return NULL;
  495. }
  496. void disasm_line__free(struct disasm_line *dl)
  497. {
  498. zfree(&dl->line);
  499. zfree(&dl->name);
  500. if (dl->ins && dl->ins->ops->free)
  501. dl->ins->ops->free(&dl->ops);
  502. else
  503. ins__delete(&dl->ops);
  504. free(dl);
  505. }
  506. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  507. {
  508. if (raw || !dl->ins)
  509. return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
  510. return ins__scnprintf(dl->ins, bf, size, &dl->ops);
  511. }
  512. static void disasm__add(struct list_head *head, struct disasm_line *line)
  513. {
  514. list_add_tail(&line->node, head);
  515. }
  516. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  517. {
  518. list_for_each_entry_continue(pos, head, node)
  519. if (pos->offset >= 0)
  520. return pos;
  521. return NULL;
  522. }
  523. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  524. s64 end, const char **path, u64 *nr_samples)
  525. {
  526. struct source_line *src_line = notes->src->lines;
  527. double percent = 0.0;
  528. *nr_samples = 0;
  529. if (src_line) {
  530. size_t sizeof_src_line = sizeof(*src_line) +
  531. sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
  532. while (offset < end) {
  533. src_line = (void *)notes->src->lines +
  534. (sizeof_src_line * offset);
  535. if (*path == NULL)
  536. *path = src_line->path;
  537. percent += src_line->samples[evidx].percent;
  538. *nr_samples += src_line->samples[evidx].nr;
  539. offset++;
  540. }
  541. } else {
  542. struct sym_hist *h = annotation__histogram(notes, evidx);
  543. unsigned int hits = 0;
  544. while (offset < end)
  545. hits += h->addr[offset++];
  546. if (h->sum) {
  547. *nr_samples = hits;
  548. percent = 100.0 * hits / h->sum;
  549. }
  550. }
  551. return percent;
  552. }
  553. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  554. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  555. int max_lines, struct disasm_line *queue)
  556. {
  557. static const char *prev_line;
  558. static const char *prev_color;
  559. if (dl->offset != -1) {
  560. const char *path = NULL;
  561. u64 nr_samples;
  562. double percent, max_percent = 0.0;
  563. double *ppercents = &percent;
  564. u64 *psamples = &nr_samples;
  565. int i, nr_percent = 1;
  566. const char *color;
  567. struct annotation *notes = symbol__annotation(sym);
  568. s64 offset = dl->offset;
  569. const u64 addr = start + offset;
  570. struct disasm_line *next;
  571. next = disasm__get_next_ip_line(&notes->src->source, dl);
  572. if (perf_evsel__is_group_event(evsel)) {
  573. nr_percent = evsel->nr_members;
  574. ppercents = calloc(nr_percent, sizeof(double));
  575. psamples = calloc(nr_percent, sizeof(u64));
  576. if (ppercents == NULL || psamples == NULL) {
  577. return -1;
  578. }
  579. }
  580. for (i = 0; i < nr_percent; i++) {
  581. percent = disasm__calc_percent(notes,
  582. notes->src->lines ? i : evsel->idx + i,
  583. offset,
  584. next ? next->offset : (s64) len,
  585. &path, &nr_samples);
  586. ppercents[i] = percent;
  587. psamples[i] = nr_samples;
  588. if (percent > max_percent)
  589. max_percent = percent;
  590. }
  591. if (max_percent < min_pcnt)
  592. return -1;
  593. if (max_lines && printed >= max_lines)
  594. return 1;
  595. if (queue != NULL) {
  596. list_for_each_entry_from(queue, &notes->src->source, node) {
  597. if (queue == dl)
  598. break;
  599. disasm_line__print(queue, sym, start, evsel, len,
  600. 0, 0, 1, NULL);
  601. }
  602. }
  603. color = get_percent_color(max_percent);
  604. /*
  605. * Also color the filename and line if needed, with
  606. * the same color than the percentage. Don't print it
  607. * twice for close colored addr with the same filename:line
  608. */
  609. if (path) {
  610. if (!prev_line || strcmp(prev_line, path)
  611. || color != prev_color) {
  612. color_fprintf(stdout, color, " %s", path);
  613. prev_line = path;
  614. prev_color = color;
  615. }
  616. }
  617. for (i = 0; i < nr_percent; i++) {
  618. percent = ppercents[i];
  619. nr_samples = psamples[i];
  620. color = get_percent_color(percent);
  621. if (symbol_conf.show_total_period)
  622. color_fprintf(stdout, color, " %7" PRIu64,
  623. nr_samples);
  624. else
  625. color_fprintf(stdout, color, " %7.2f", percent);
  626. }
  627. printf(" : ");
  628. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  629. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  630. if (ppercents != &percent)
  631. free(ppercents);
  632. if (psamples != &nr_samples)
  633. free(psamples);
  634. } else if (max_lines && printed >= max_lines)
  635. return 1;
  636. else {
  637. int width = 8;
  638. if (queue)
  639. return -1;
  640. if (perf_evsel__is_group_event(evsel))
  641. width *= evsel->nr_members;
  642. if (!*dl->line)
  643. printf(" %*s:\n", width, " ");
  644. else
  645. printf(" %*s: %s\n", width, " ", dl->line);
  646. }
  647. return 0;
  648. }
  649. /*
  650. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  651. * which looks like following
  652. *
  653. * 0000000000415500 <_init>:
  654. * 415500: sub $0x8,%rsp
  655. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  656. * 41550b: test %rax,%rax
  657. * 41550e: je 415515 <_init+0x15>
  658. * 415510: callq 416e70 <__gmon_start__@plt>
  659. * 415515: add $0x8,%rsp
  660. * 415519: retq
  661. *
  662. * it will be parsed and saved into struct disasm_line as
  663. * <offset> <name> <ops.raw>
  664. *
  665. * The offset will be a relative offset from the start of the symbol and -1
  666. * means that it's not a disassembly line so should be treated differently.
  667. * The ops.raw part will be parsed further according to type of the instruction.
  668. */
  669. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  670. FILE *file, size_t privsize,
  671. int *line_nr)
  672. {
  673. struct annotation *notes = symbol__annotation(sym);
  674. struct disasm_line *dl;
  675. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  676. size_t line_len;
  677. s64 line_ip, offset = -1;
  678. regmatch_t match[2];
  679. if (getline(&line, &line_len, file) < 0)
  680. return -1;
  681. if (!line)
  682. return -1;
  683. while (line_len != 0 && isspace(line[line_len - 1]))
  684. line[--line_len] = '\0';
  685. c = strchr(line, '\n');
  686. if (c)
  687. *c = 0;
  688. line_ip = -1;
  689. parsed_line = line;
  690. /* /filename:linenr ? Save line number and ignore. */
  691. if (regexec(&file_lineno, line, 2, match, 0) == 0) {
  692. *line_nr = atoi(line + match[1].rm_so);
  693. return 0;
  694. }
  695. /*
  696. * Strip leading spaces:
  697. */
  698. tmp = line;
  699. while (*tmp) {
  700. if (*tmp != ' ')
  701. break;
  702. tmp++;
  703. }
  704. if (*tmp) {
  705. /*
  706. * Parse hexa addresses followed by ':'
  707. */
  708. line_ip = strtoull(tmp, &tmp2, 16);
  709. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  710. line_ip = -1;
  711. }
  712. if (line_ip != -1) {
  713. u64 start = map__rip_2objdump(map, sym->start),
  714. end = map__rip_2objdump(map, sym->end);
  715. offset = line_ip - start;
  716. if ((u64)line_ip < start || (u64)line_ip >= end)
  717. offset = -1;
  718. else
  719. parsed_line = tmp2 + 1;
  720. }
  721. dl = disasm_line__new(offset, parsed_line, privsize, *line_nr);
  722. free(line);
  723. (*line_nr)++;
  724. if (dl == NULL)
  725. return -1;
  726. if (dl->ops.target.offset == UINT64_MAX)
  727. dl->ops.target.offset = dl->ops.target.addr -
  728. map__rip_2objdump(map, sym->start);
  729. /* kcore has no symbols, so add the call target name */
  730. if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
  731. struct addr_map_symbol target = {
  732. .map = map,
  733. .addr = dl->ops.target.addr,
  734. };
  735. if (!map_groups__find_ams(&target, NULL) &&
  736. target.sym->start == target.al_addr)
  737. dl->ops.target.name = strdup(target.sym->name);
  738. }
  739. disasm__add(&notes->src->source, dl);
  740. return 0;
  741. }
  742. static __attribute__((constructor)) void symbol__init_regexpr(void)
  743. {
  744. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  745. }
  746. static void delete_last_nop(struct symbol *sym)
  747. {
  748. struct annotation *notes = symbol__annotation(sym);
  749. struct list_head *list = &notes->src->source;
  750. struct disasm_line *dl;
  751. while (!list_empty(list)) {
  752. dl = list_entry(list->prev, struct disasm_line, node);
  753. if (dl->ins && dl->ins->ops) {
  754. if (dl->ins->ops != &nop_ops)
  755. return;
  756. } else {
  757. if (!strstr(dl->line, " nop ") &&
  758. !strstr(dl->line, " nopl ") &&
  759. !strstr(dl->line, " nopw "))
  760. return;
  761. }
  762. list_del(&dl->node);
  763. disasm_line__free(dl);
  764. }
  765. }
  766. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  767. {
  768. struct dso *dso = map->dso;
  769. char *filename = dso__build_id_filename(dso, NULL, 0);
  770. bool free_filename = true;
  771. char command[PATH_MAX * 2];
  772. FILE *file;
  773. int err = 0;
  774. char symfs_filename[PATH_MAX];
  775. struct kcore_extract kce;
  776. bool delete_extract = false;
  777. int lineno = 0;
  778. if (filename)
  779. symbol__join_symfs(symfs_filename, filename);
  780. if (filename == NULL) {
  781. if (dso->has_build_id) {
  782. pr_err("Can't annotate %s: not enough memory\n",
  783. sym->name);
  784. return -ENOMEM;
  785. }
  786. goto fallback;
  787. } else if (dso__is_kcore(dso)) {
  788. goto fallback;
  789. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  790. strstr(command, "[kernel.kallsyms]") ||
  791. access(symfs_filename, R_OK)) {
  792. free(filename);
  793. fallback:
  794. /*
  795. * If we don't have build-ids or the build-id file isn't in the
  796. * cache, or is just a kallsyms file, well, lets hope that this
  797. * DSO is the same as when 'perf record' ran.
  798. */
  799. filename = (char *)dso->long_name;
  800. symbol__join_symfs(symfs_filename, filename);
  801. free_filename = false;
  802. }
  803. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  804. !dso__is_kcore(dso)) {
  805. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  806. char *build_id_msg = NULL;
  807. if (dso->annotate_warned)
  808. goto out_free_filename;
  809. if (dso->has_build_id) {
  810. build_id__sprintf(dso->build_id,
  811. sizeof(dso->build_id), bf + 15);
  812. build_id_msg = bf;
  813. }
  814. err = -ENOENT;
  815. dso->annotate_warned = 1;
  816. pr_err("Can't annotate %s:\n\n"
  817. "No vmlinux file%s\nwas found in the path.\n\n"
  818. "Please use:\n\n"
  819. " perf buildid-cache -vu vmlinux\n\n"
  820. "or:\n\n"
  821. " --vmlinux vmlinux\n",
  822. sym->name, build_id_msg ?: "");
  823. goto out_free_filename;
  824. }
  825. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  826. filename, sym->name, map->unmap_ip(map, sym->start),
  827. map->unmap_ip(map, sym->end));
  828. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  829. dso, dso->long_name, sym, sym->name);
  830. if (dso__is_kcore(dso)) {
  831. kce.kcore_filename = symfs_filename;
  832. kce.addr = map__rip_2objdump(map, sym->start);
  833. kce.offs = sym->start;
  834. kce.len = sym->end - sym->start;
  835. if (!kcore_extract__create(&kce)) {
  836. delete_extract = true;
  837. strlcpy(symfs_filename, kce.extract_filename,
  838. sizeof(symfs_filename));
  839. if (free_filename) {
  840. free(filename);
  841. free_filename = false;
  842. }
  843. filename = symfs_filename;
  844. }
  845. } else if (dso__needs_decompress(dso)) {
  846. char tmp[PATH_MAX];
  847. struct kmod_path m;
  848. int fd;
  849. bool ret;
  850. if (kmod_path__parse_ext(&m, symfs_filename))
  851. goto out_free_filename;
  852. snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
  853. fd = mkstemp(tmp);
  854. if (fd < 0) {
  855. free(m.ext);
  856. goto out_free_filename;
  857. }
  858. ret = decompress_to_file(m.ext, symfs_filename, fd);
  859. free(m.ext);
  860. close(fd);
  861. if (!ret)
  862. goto out_free_filename;
  863. strcpy(symfs_filename, tmp);
  864. }
  865. snprintf(command, sizeof(command),
  866. "%s %s%s --start-address=0x%016" PRIx64
  867. " --stop-address=0x%016" PRIx64
  868. " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
  869. objdump_path ? objdump_path : "objdump",
  870. disassembler_style ? "-M " : "",
  871. disassembler_style ? disassembler_style : "",
  872. map__rip_2objdump(map, sym->start),
  873. map__rip_2objdump(map, sym->end),
  874. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  875. symbol_conf.annotate_src ? "-S" : "",
  876. symfs_filename, filename);
  877. pr_debug("Executing: %s\n", command);
  878. file = popen(command, "r");
  879. if (!file)
  880. goto out_remove_tmp;
  881. while (!feof(file))
  882. if (symbol__parse_objdump_line(sym, map, file, privsize,
  883. &lineno) < 0)
  884. break;
  885. /*
  886. * kallsyms does not have symbol sizes so there may a nop at the end.
  887. * Remove it.
  888. */
  889. if (dso__is_kcore(dso))
  890. delete_last_nop(sym);
  891. pclose(file);
  892. out_remove_tmp:
  893. if (dso__needs_decompress(dso))
  894. unlink(symfs_filename);
  895. out_free_filename:
  896. if (delete_extract)
  897. kcore_extract__delete(&kce);
  898. if (free_filename)
  899. free(filename);
  900. return err;
  901. }
  902. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  903. {
  904. struct source_line *iter;
  905. struct rb_node **p = &root->rb_node;
  906. struct rb_node *parent = NULL;
  907. int i, ret;
  908. while (*p != NULL) {
  909. parent = *p;
  910. iter = rb_entry(parent, struct source_line, node);
  911. ret = strcmp(iter->path, src_line->path);
  912. if (ret == 0) {
  913. for (i = 0; i < src_line->nr_pcnt; i++)
  914. iter->samples[i].percent_sum += src_line->samples[i].percent;
  915. return;
  916. }
  917. if (ret < 0)
  918. p = &(*p)->rb_left;
  919. else
  920. p = &(*p)->rb_right;
  921. }
  922. for (i = 0; i < src_line->nr_pcnt; i++)
  923. src_line->samples[i].percent_sum = src_line->samples[i].percent;
  924. rb_link_node(&src_line->node, parent, p);
  925. rb_insert_color(&src_line->node, root);
  926. }
  927. static int cmp_source_line(struct source_line *a, struct source_line *b)
  928. {
  929. int i;
  930. for (i = 0; i < a->nr_pcnt; i++) {
  931. if (a->samples[i].percent_sum == b->samples[i].percent_sum)
  932. continue;
  933. return a->samples[i].percent_sum > b->samples[i].percent_sum;
  934. }
  935. return 0;
  936. }
  937. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  938. {
  939. struct source_line *iter;
  940. struct rb_node **p = &root->rb_node;
  941. struct rb_node *parent = NULL;
  942. while (*p != NULL) {
  943. parent = *p;
  944. iter = rb_entry(parent, struct source_line, node);
  945. if (cmp_source_line(src_line, iter))
  946. p = &(*p)->rb_left;
  947. else
  948. p = &(*p)->rb_right;
  949. }
  950. rb_link_node(&src_line->node, parent, p);
  951. rb_insert_color(&src_line->node, root);
  952. }
  953. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  954. {
  955. struct source_line *src_line;
  956. struct rb_node *node;
  957. node = rb_first(src_root);
  958. while (node) {
  959. struct rb_node *next;
  960. src_line = rb_entry(node, struct source_line, node);
  961. next = rb_next(node);
  962. rb_erase(node, src_root);
  963. __resort_source_line(dest_root, src_line);
  964. node = next;
  965. }
  966. }
  967. static void symbol__free_source_line(struct symbol *sym, int len)
  968. {
  969. struct annotation *notes = symbol__annotation(sym);
  970. struct source_line *src_line = notes->src->lines;
  971. size_t sizeof_src_line;
  972. int i;
  973. sizeof_src_line = sizeof(*src_line) +
  974. (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
  975. for (i = 0; i < len; i++) {
  976. free_srcline(src_line->path);
  977. src_line = (void *)src_line + sizeof_src_line;
  978. }
  979. zfree(&notes->src->lines);
  980. }
  981. /* Get the filename:line for the colored entries */
  982. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  983. struct perf_evsel *evsel,
  984. struct rb_root *root, int len)
  985. {
  986. u64 start;
  987. int i, k;
  988. int evidx = evsel->idx;
  989. struct source_line *src_line;
  990. struct annotation *notes = symbol__annotation(sym);
  991. struct sym_hist *h = annotation__histogram(notes, evidx);
  992. struct rb_root tmp_root = RB_ROOT;
  993. int nr_pcnt = 1;
  994. u64 h_sum = h->sum;
  995. size_t sizeof_src_line = sizeof(struct source_line);
  996. if (perf_evsel__is_group_event(evsel)) {
  997. for (i = 1; i < evsel->nr_members; i++) {
  998. h = annotation__histogram(notes, evidx + i);
  999. h_sum += h->sum;
  1000. }
  1001. nr_pcnt = evsel->nr_members;
  1002. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
  1003. }
  1004. if (!h_sum)
  1005. return 0;
  1006. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  1007. if (!notes->src->lines)
  1008. return -1;
  1009. start = map__rip_2objdump(map, sym->start);
  1010. for (i = 0; i < len; i++) {
  1011. u64 offset;
  1012. double percent_max = 0.0;
  1013. src_line->nr_pcnt = nr_pcnt;
  1014. for (k = 0; k < nr_pcnt; k++) {
  1015. h = annotation__histogram(notes, evidx + k);
  1016. src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
  1017. if (src_line->samples[k].percent > percent_max)
  1018. percent_max = src_line->samples[k].percent;
  1019. }
  1020. if (percent_max <= 0.5)
  1021. goto next;
  1022. offset = start + i;
  1023. src_line->path = get_srcline(map->dso, offset, NULL, false);
  1024. insert_source_line(&tmp_root, src_line);
  1025. next:
  1026. src_line = (void *)src_line + sizeof_src_line;
  1027. }
  1028. resort_source_line(root, &tmp_root);
  1029. return 0;
  1030. }
  1031. static void print_summary(struct rb_root *root, const char *filename)
  1032. {
  1033. struct source_line *src_line;
  1034. struct rb_node *node;
  1035. printf("\nSorted summary for file %s\n", filename);
  1036. printf("----------------------------------------------\n\n");
  1037. if (RB_EMPTY_ROOT(root)) {
  1038. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1039. return;
  1040. }
  1041. node = rb_first(root);
  1042. while (node) {
  1043. double percent, percent_max = 0.0;
  1044. const char *color;
  1045. char *path;
  1046. int i;
  1047. src_line = rb_entry(node, struct source_line, node);
  1048. for (i = 0; i < src_line->nr_pcnt; i++) {
  1049. percent = src_line->samples[i].percent_sum;
  1050. color = get_percent_color(percent);
  1051. color_fprintf(stdout, color, " %7.2f", percent);
  1052. if (percent > percent_max)
  1053. percent_max = percent;
  1054. }
  1055. path = src_line->path;
  1056. color = get_percent_color(percent_max);
  1057. color_fprintf(stdout, color, " %s\n", path);
  1058. node = rb_next(node);
  1059. }
  1060. }
  1061. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1062. {
  1063. struct annotation *notes = symbol__annotation(sym);
  1064. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1065. u64 len = symbol__size(sym), offset;
  1066. for (offset = 0; offset < len; ++offset)
  1067. if (h->addr[offset] != 0)
  1068. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1069. sym->start + offset, h->addr[offset]);
  1070. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  1071. }
  1072. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1073. struct perf_evsel *evsel, bool full_paths,
  1074. int min_pcnt, int max_lines, int context)
  1075. {
  1076. struct dso *dso = map->dso;
  1077. char *filename;
  1078. const char *d_filename;
  1079. const char *evsel_name = perf_evsel__name(evsel);
  1080. struct annotation *notes = symbol__annotation(sym);
  1081. struct disasm_line *pos, *queue = NULL;
  1082. u64 start = map__rip_2objdump(map, sym->start);
  1083. int printed = 2, queue_len = 0;
  1084. int more = 0;
  1085. u64 len;
  1086. int width = 8;
  1087. int namelen, evsel_name_len, graph_dotted_len;
  1088. filename = strdup(dso->long_name);
  1089. if (!filename)
  1090. return -ENOMEM;
  1091. if (full_paths)
  1092. d_filename = filename;
  1093. else
  1094. d_filename = basename(filename);
  1095. len = symbol__size(sym);
  1096. namelen = strlen(d_filename);
  1097. evsel_name_len = strlen(evsel_name);
  1098. if (perf_evsel__is_group_event(evsel))
  1099. width *= evsel->nr_members;
  1100. printf(" %-*.*s| Source code & Disassembly of %s for %s\n",
  1101. width, width, "Percent", d_filename, evsel_name);
  1102. graph_dotted_len = width + namelen + evsel_name_len;
  1103. printf("-%-*.*s-----------------------------------------\n",
  1104. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1105. if (verbose)
  1106. symbol__annotate_hits(sym, evsel);
  1107. list_for_each_entry(pos, &notes->src->source, node) {
  1108. if (context && queue == NULL) {
  1109. queue = pos;
  1110. queue_len = 0;
  1111. }
  1112. switch (disasm_line__print(pos, sym, start, evsel, len,
  1113. min_pcnt, printed, max_lines,
  1114. queue)) {
  1115. case 0:
  1116. ++printed;
  1117. if (context) {
  1118. printed += queue_len;
  1119. queue = NULL;
  1120. queue_len = 0;
  1121. }
  1122. break;
  1123. case 1:
  1124. /* filtered by max_lines */
  1125. ++more;
  1126. break;
  1127. case -1:
  1128. default:
  1129. /*
  1130. * Filtered by min_pcnt or non IP lines when
  1131. * context != 0
  1132. */
  1133. if (!context)
  1134. break;
  1135. if (queue_len == context)
  1136. queue = list_entry(queue->node.next, typeof(*queue), node);
  1137. else
  1138. ++queue_len;
  1139. break;
  1140. }
  1141. }
  1142. free(filename);
  1143. return more;
  1144. }
  1145. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1146. {
  1147. struct annotation *notes = symbol__annotation(sym);
  1148. struct sym_hist *h = annotation__histogram(notes, evidx);
  1149. memset(h, 0, notes->src->sizeof_sym_hist);
  1150. }
  1151. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1152. {
  1153. struct annotation *notes = symbol__annotation(sym);
  1154. struct sym_hist *h = annotation__histogram(notes, evidx);
  1155. int len = symbol__size(sym), offset;
  1156. h->sum = 0;
  1157. for (offset = 0; offset < len; ++offset) {
  1158. h->addr[offset] = h->addr[offset] * 7 / 8;
  1159. h->sum += h->addr[offset];
  1160. }
  1161. }
  1162. void disasm__purge(struct list_head *head)
  1163. {
  1164. struct disasm_line *pos, *n;
  1165. list_for_each_entry_safe(pos, n, head, node) {
  1166. list_del(&pos->node);
  1167. disasm_line__free(pos);
  1168. }
  1169. }
  1170. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1171. {
  1172. size_t printed;
  1173. if (dl->offset == -1)
  1174. return fprintf(fp, "%s\n", dl->line);
  1175. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  1176. if (dl->ops.raw[0] != '\0') {
  1177. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1178. dl->ops.raw);
  1179. }
  1180. return printed + fprintf(fp, "\n");
  1181. }
  1182. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1183. {
  1184. struct disasm_line *pos;
  1185. size_t printed = 0;
  1186. list_for_each_entry(pos, head, node)
  1187. printed += disasm_line__fprintf(pos, fp);
  1188. return printed;
  1189. }
  1190. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1191. struct perf_evsel *evsel, bool print_lines,
  1192. bool full_paths, int min_pcnt, int max_lines)
  1193. {
  1194. struct dso *dso = map->dso;
  1195. struct rb_root source_line = RB_ROOT;
  1196. u64 len;
  1197. if (symbol__annotate(sym, map, 0) < 0)
  1198. return -1;
  1199. len = symbol__size(sym);
  1200. if (print_lines) {
  1201. symbol__get_source_line(sym, map, evsel, &source_line, len);
  1202. print_summary(&source_line, dso->long_name);
  1203. }
  1204. symbol__annotate_printf(sym, map, evsel, full_paths,
  1205. min_pcnt, max_lines, 0);
  1206. if (print_lines)
  1207. symbol__free_source_line(sym, len);
  1208. disasm__purge(&symbol__annotation(sym)->src->source);
  1209. return 0;
  1210. }
  1211. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  1212. {
  1213. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  1214. }
  1215. bool ui__has_annotation(void)
  1216. {
  1217. return use_browser == 1 && sort__has_sym;
  1218. }