annotate.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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 <errno.h>
  10. #include <inttypes.h>
  11. #include "util.h"
  12. #include "ui/ui.h"
  13. #include "sort.h"
  14. #include "build-id.h"
  15. #include "color.h"
  16. #include "cache.h"
  17. #include "symbol.h"
  18. #include "debug.h"
  19. #include "annotate.h"
  20. #include "evsel.h"
  21. #include "block-range.h"
  22. #include "string2.h"
  23. #include "arch/common.h"
  24. #include <regex.h>
  25. #include <pthread.h>
  26. #include <linux/bitops.h>
  27. #include <linux/kernel.h>
  28. #include <sys/utsname.h>
  29. #include "sane_ctype.h"
  30. const char *disassembler_style;
  31. const char *objdump_path;
  32. static regex_t file_lineno;
  33. static struct ins_ops *ins__find(struct arch *arch, const char *name);
  34. static void ins__sort(struct arch *arch);
  35. static int disasm_line__parse(char *line, const char **namep, char **rawp);
  36. struct arch {
  37. const char *name;
  38. struct ins *instructions;
  39. size_t nr_instructions;
  40. size_t nr_instructions_allocated;
  41. struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
  42. bool sorted_instructions;
  43. bool initialized;
  44. void *priv;
  45. unsigned int model;
  46. unsigned int family;
  47. int (*init)(struct arch *arch, char *cpuid);
  48. bool (*ins_is_fused)(struct arch *arch, const char *ins1,
  49. const char *ins2);
  50. struct {
  51. char comment_char;
  52. char skip_functions_char;
  53. } objdump;
  54. };
  55. static struct ins_ops call_ops;
  56. static struct ins_ops dec_ops;
  57. static struct ins_ops jump_ops;
  58. static struct ins_ops mov_ops;
  59. static struct ins_ops nop_ops;
  60. static struct ins_ops lock_ops;
  61. static struct ins_ops ret_ops;
  62. static int arch__grow_instructions(struct arch *arch)
  63. {
  64. struct ins *new_instructions;
  65. size_t new_nr_allocated;
  66. if (arch->nr_instructions_allocated == 0 && arch->instructions)
  67. goto grow_from_non_allocated_table;
  68. new_nr_allocated = arch->nr_instructions_allocated + 128;
  69. new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
  70. if (new_instructions == NULL)
  71. return -1;
  72. out_update_instructions:
  73. arch->instructions = new_instructions;
  74. arch->nr_instructions_allocated = new_nr_allocated;
  75. return 0;
  76. grow_from_non_allocated_table:
  77. new_nr_allocated = arch->nr_instructions + 128;
  78. new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
  79. if (new_instructions == NULL)
  80. return -1;
  81. memcpy(new_instructions, arch->instructions, arch->nr_instructions);
  82. goto out_update_instructions;
  83. }
  84. static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
  85. {
  86. struct ins *ins;
  87. if (arch->nr_instructions == arch->nr_instructions_allocated &&
  88. arch__grow_instructions(arch))
  89. return -1;
  90. ins = &arch->instructions[arch->nr_instructions];
  91. ins->name = strdup(name);
  92. if (!ins->name)
  93. return -1;
  94. ins->ops = ops;
  95. arch->nr_instructions++;
  96. ins__sort(arch);
  97. return 0;
  98. }
  99. #include "arch/arm/annotate/instructions.c"
  100. #include "arch/arm64/annotate/instructions.c"
  101. #include "arch/x86/annotate/instructions.c"
  102. #include "arch/powerpc/annotate/instructions.c"
  103. #include "arch/s390/annotate/instructions.c"
  104. static struct arch architectures[] = {
  105. {
  106. .name = "arm",
  107. .init = arm__annotate_init,
  108. },
  109. {
  110. .name = "arm64",
  111. .init = arm64__annotate_init,
  112. },
  113. {
  114. .name = "x86",
  115. .init = x86__annotate_init,
  116. .instructions = x86__instructions,
  117. .nr_instructions = ARRAY_SIZE(x86__instructions),
  118. .ins_is_fused = x86__ins_is_fused,
  119. .objdump = {
  120. .comment_char = '#',
  121. },
  122. },
  123. {
  124. .name = "powerpc",
  125. .init = powerpc__annotate_init,
  126. },
  127. {
  128. .name = "s390",
  129. .init = s390__annotate_init,
  130. .objdump = {
  131. .comment_char = '#',
  132. },
  133. },
  134. };
  135. static void ins__delete(struct ins_operands *ops)
  136. {
  137. if (ops == NULL)
  138. return;
  139. zfree(&ops->source.raw);
  140. zfree(&ops->source.name);
  141. zfree(&ops->target.raw);
  142. zfree(&ops->target.name);
  143. }
  144. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  145. struct ins_operands *ops)
  146. {
  147. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
  148. }
  149. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  150. struct ins_operands *ops)
  151. {
  152. if (ins->ops->scnprintf)
  153. return ins->ops->scnprintf(ins, bf, size, ops);
  154. return ins__raw_scnprintf(ins, bf, size, ops);
  155. }
  156. bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
  157. {
  158. if (!arch || !arch->ins_is_fused)
  159. return false;
  160. return arch->ins_is_fused(arch, ins1, ins2);
  161. }
  162. static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
  163. {
  164. char *endptr, *tok, *name;
  165. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  166. name = strchr(endptr, '<');
  167. if (name == NULL)
  168. goto indirect_call;
  169. name++;
  170. if (arch->objdump.skip_functions_char &&
  171. strchr(name, arch->objdump.skip_functions_char))
  172. return -1;
  173. tok = strchr(name, '>');
  174. if (tok == NULL)
  175. return -1;
  176. *tok = '\0';
  177. ops->target.name = strdup(name);
  178. *tok = '>';
  179. return ops->target.name == NULL ? -1 : 0;
  180. indirect_call:
  181. tok = strchr(endptr, '*');
  182. if (tok == NULL) {
  183. struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
  184. if (sym != NULL)
  185. ops->target.name = strdup(sym->name);
  186. else
  187. ops->target.addr = 0;
  188. return 0;
  189. }
  190. ops->target.addr = strtoull(tok + 1, NULL, 16);
  191. return 0;
  192. }
  193. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  194. struct ins_operands *ops)
  195. {
  196. if (ops->target.name)
  197. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
  198. if (ops->target.addr == 0)
  199. return ins__raw_scnprintf(ins, bf, size, ops);
  200. return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
  201. }
  202. static struct ins_ops call_ops = {
  203. .parse = call__parse,
  204. .scnprintf = call__scnprintf,
  205. };
  206. bool ins__is_call(const struct ins *ins)
  207. {
  208. return ins->ops == &call_ops;
  209. }
  210. static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
  211. {
  212. const char *s = strchr(ops->raw, '+');
  213. const char *c = strchr(ops->raw, ',');
  214. /*
  215. * skip over possible up to 2 operands to get to address, e.g.:
  216. * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
  217. */
  218. if (c++ != NULL) {
  219. ops->target.addr = strtoull(c, NULL, 16);
  220. if (!ops->target.addr) {
  221. c = strchr(c, ',');
  222. if (c++ != NULL)
  223. ops->target.addr = strtoull(c, NULL, 16);
  224. }
  225. } else {
  226. ops->target.addr = strtoull(ops->raw, NULL, 16);
  227. }
  228. if (s++ != NULL) {
  229. ops->target.offset = strtoull(s, NULL, 16);
  230. ops->target.offset_avail = true;
  231. } else {
  232. ops->target.offset_avail = false;
  233. }
  234. return 0;
  235. }
  236. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  237. struct ins_operands *ops)
  238. {
  239. const char *c = strchr(ops->raw, ',');
  240. if (!ops->target.addr || ops->target.offset < 0)
  241. return ins__raw_scnprintf(ins, bf, size, ops);
  242. if (c != NULL) {
  243. const char *c2 = strchr(c + 1, ',');
  244. /* check for 3-op insn */
  245. if (c2 != NULL)
  246. c = c2;
  247. c++;
  248. /* mirror arch objdump's space-after-comma style */
  249. if (*c == ' ')
  250. c++;
  251. }
  252. return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64,
  253. ins->name, c ? c - ops->raw : 0, ops->raw,
  254. ops->target.offset);
  255. }
  256. static struct ins_ops jump_ops = {
  257. .parse = jump__parse,
  258. .scnprintf = jump__scnprintf,
  259. };
  260. bool ins__is_jump(const struct ins *ins)
  261. {
  262. return ins->ops == &jump_ops;
  263. }
  264. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  265. {
  266. char *endptr, *name, *t;
  267. if (strstr(raw, "(%rip)") == NULL)
  268. return 0;
  269. *addrp = strtoull(comment, &endptr, 16);
  270. name = strchr(endptr, '<');
  271. if (name == NULL)
  272. return -1;
  273. name++;
  274. t = strchr(name, '>');
  275. if (t == NULL)
  276. return 0;
  277. *t = '\0';
  278. *namep = strdup(name);
  279. *t = '>';
  280. return 0;
  281. }
  282. static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
  283. {
  284. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  285. if (ops->locked.ops == NULL)
  286. return 0;
  287. if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
  288. goto out_free_ops;
  289. ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
  290. if (ops->locked.ins.ops == NULL)
  291. goto out_free_ops;
  292. if (ops->locked.ins.ops->parse &&
  293. ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
  294. goto out_free_ops;
  295. return 0;
  296. out_free_ops:
  297. zfree(&ops->locked.ops);
  298. return 0;
  299. }
  300. static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
  301. struct ins_operands *ops)
  302. {
  303. int printed;
  304. if (ops->locked.ins.ops == NULL)
  305. return ins__raw_scnprintf(ins, bf, size, ops);
  306. printed = scnprintf(bf, size, "%-6.6s ", ins->name);
  307. return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
  308. size - printed, ops->locked.ops);
  309. }
  310. static void lock__delete(struct ins_operands *ops)
  311. {
  312. struct ins *ins = &ops->locked.ins;
  313. if (ins->ops && ins->ops->free)
  314. ins->ops->free(ops->locked.ops);
  315. else
  316. ins__delete(ops->locked.ops);
  317. zfree(&ops->locked.ops);
  318. zfree(&ops->target.raw);
  319. zfree(&ops->target.name);
  320. }
  321. static struct ins_ops lock_ops = {
  322. .free = lock__delete,
  323. .parse = lock__parse,
  324. .scnprintf = lock__scnprintf,
  325. };
  326. static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
  327. {
  328. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  329. if (s == NULL)
  330. return -1;
  331. *s = '\0';
  332. ops->source.raw = strdup(ops->raw);
  333. *s = ',';
  334. if (ops->source.raw == NULL)
  335. return -1;
  336. target = ++s;
  337. comment = strchr(s, arch->objdump.comment_char);
  338. if (comment != NULL)
  339. s = comment - 1;
  340. else
  341. s = strchr(s, '\0') - 1;
  342. while (s > target && isspace(s[0]))
  343. --s;
  344. s++;
  345. prev = *s;
  346. *s = '\0';
  347. ops->target.raw = strdup(target);
  348. *s = prev;
  349. if (ops->target.raw == NULL)
  350. goto out_free_source;
  351. if (comment == NULL)
  352. return 0;
  353. comment = ltrim(comment);
  354. comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
  355. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  356. return 0;
  357. out_free_source:
  358. zfree(&ops->source.raw);
  359. return -1;
  360. }
  361. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  362. struct ins_operands *ops)
  363. {
  364. return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
  365. ops->source.name ?: ops->source.raw,
  366. ops->target.name ?: ops->target.raw);
  367. }
  368. static struct ins_ops mov_ops = {
  369. .parse = mov__parse,
  370. .scnprintf = mov__scnprintf,
  371. };
  372. static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
  373. {
  374. char *target, *comment, *s, prev;
  375. target = s = ops->raw;
  376. while (s[0] != '\0' && !isspace(s[0]))
  377. ++s;
  378. prev = *s;
  379. *s = '\0';
  380. ops->target.raw = strdup(target);
  381. *s = prev;
  382. if (ops->target.raw == NULL)
  383. return -1;
  384. comment = strchr(s, arch->objdump.comment_char);
  385. if (comment == NULL)
  386. return 0;
  387. comment = ltrim(comment);
  388. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  389. return 0;
  390. }
  391. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  392. struct ins_operands *ops)
  393. {
  394. return scnprintf(bf, size, "%-6.6s %s", ins->name,
  395. ops->target.name ?: ops->target.raw);
  396. }
  397. static struct ins_ops dec_ops = {
  398. .parse = dec__parse,
  399. .scnprintf = dec__scnprintf,
  400. };
  401. static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
  402. struct ins_operands *ops __maybe_unused)
  403. {
  404. return scnprintf(bf, size, "%-6.6s", "nop");
  405. }
  406. static struct ins_ops nop_ops = {
  407. .scnprintf = nop__scnprintf,
  408. };
  409. static struct ins_ops ret_ops = {
  410. .scnprintf = ins__raw_scnprintf,
  411. };
  412. bool ins__is_ret(const struct ins *ins)
  413. {
  414. return ins->ops == &ret_ops;
  415. }
  416. bool ins__is_lock(const struct ins *ins)
  417. {
  418. return ins->ops == &lock_ops;
  419. }
  420. static int ins__key_cmp(const void *name, const void *insp)
  421. {
  422. const struct ins *ins = insp;
  423. return strcmp(name, ins->name);
  424. }
  425. static int ins__cmp(const void *a, const void *b)
  426. {
  427. const struct ins *ia = a;
  428. const struct ins *ib = b;
  429. return strcmp(ia->name, ib->name);
  430. }
  431. static void ins__sort(struct arch *arch)
  432. {
  433. const int nmemb = arch->nr_instructions;
  434. qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
  435. }
  436. static struct ins_ops *__ins__find(struct arch *arch, const char *name)
  437. {
  438. struct ins *ins;
  439. const int nmemb = arch->nr_instructions;
  440. if (!arch->sorted_instructions) {
  441. ins__sort(arch);
  442. arch->sorted_instructions = true;
  443. }
  444. ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
  445. return ins ? ins->ops : NULL;
  446. }
  447. static struct ins_ops *ins__find(struct arch *arch, const char *name)
  448. {
  449. struct ins_ops *ops = __ins__find(arch, name);
  450. if (!ops && arch->associate_instruction_ops)
  451. ops = arch->associate_instruction_ops(arch, name);
  452. return ops;
  453. }
  454. static int arch__key_cmp(const void *name, const void *archp)
  455. {
  456. const struct arch *arch = archp;
  457. return strcmp(name, arch->name);
  458. }
  459. static int arch__cmp(const void *a, const void *b)
  460. {
  461. const struct arch *aa = a;
  462. const struct arch *ab = b;
  463. return strcmp(aa->name, ab->name);
  464. }
  465. static void arch__sort(void)
  466. {
  467. const int nmemb = ARRAY_SIZE(architectures);
  468. qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
  469. }
  470. static struct arch *arch__find(const char *name)
  471. {
  472. const int nmemb = ARRAY_SIZE(architectures);
  473. static bool sorted;
  474. if (!sorted) {
  475. arch__sort();
  476. sorted = true;
  477. }
  478. return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
  479. }
  480. int symbol__alloc_hist(struct symbol *sym)
  481. {
  482. struct annotation *notes = symbol__annotation(sym);
  483. size_t size = symbol__size(sym);
  484. size_t sizeof_sym_hist;
  485. /*
  486. * Add buffer of one element for zero length symbol.
  487. * When sample is taken from first instruction of
  488. * zero length symbol, perf still resolves it and
  489. * shows symbol name in perf report and allows to
  490. * annotate it.
  491. */
  492. if (size == 0)
  493. size = 1;
  494. /* Check for overflow when calculating sizeof_sym_hist */
  495. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
  496. return -1;
  497. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
  498. /* Check for overflow in zalloc argument */
  499. if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
  500. / symbol_conf.nr_events)
  501. return -1;
  502. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  503. if (notes->src == NULL)
  504. return -1;
  505. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  506. notes->src->nr_histograms = symbol_conf.nr_events;
  507. INIT_LIST_HEAD(&notes->src->source);
  508. return 0;
  509. }
  510. /* The cycles histogram is lazily allocated. */
  511. static int symbol__alloc_hist_cycles(struct symbol *sym)
  512. {
  513. struct annotation *notes = symbol__annotation(sym);
  514. const size_t size = symbol__size(sym);
  515. notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
  516. if (notes->src->cycles_hist == NULL)
  517. return -1;
  518. return 0;
  519. }
  520. void symbol__annotate_zero_histograms(struct symbol *sym)
  521. {
  522. struct annotation *notes = symbol__annotation(sym);
  523. pthread_mutex_lock(&notes->lock);
  524. if (notes->src != NULL) {
  525. memset(notes->src->histograms, 0,
  526. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  527. if (notes->src->cycles_hist)
  528. memset(notes->src->cycles_hist, 0,
  529. symbol__size(sym) * sizeof(struct cyc_hist));
  530. }
  531. pthread_mutex_unlock(&notes->lock);
  532. }
  533. static int __symbol__account_cycles(struct annotation *notes,
  534. u64 start,
  535. unsigned offset, unsigned cycles,
  536. unsigned have_start)
  537. {
  538. struct cyc_hist *ch;
  539. ch = notes->src->cycles_hist;
  540. /*
  541. * For now we can only account one basic block per
  542. * final jump. But multiple could be overlapping.
  543. * Always account the longest one. So when
  544. * a shorter one has been already seen throw it away.
  545. *
  546. * We separately always account the full cycles.
  547. */
  548. ch[offset].num_aggr++;
  549. ch[offset].cycles_aggr += cycles;
  550. if (!have_start && ch[offset].have_start)
  551. return 0;
  552. if (ch[offset].num) {
  553. if (have_start && (!ch[offset].have_start ||
  554. ch[offset].start > start)) {
  555. ch[offset].have_start = 0;
  556. ch[offset].cycles = 0;
  557. ch[offset].num = 0;
  558. if (ch[offset].reset < 0xffff)
  559. ch[offset].reset++;
  560. } else if (have_start &&
  561. ch[offset].start < start)
  562. return 0;
  563. }
  564. ch[offset].have_start = have_start;
  565. ch[offset].start = start;
  566. ch[offset].cycles += cycles;
  567. ch[offset].num++;
  568. return 0;
  569. }
  570. static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  571. struct annotation *notes, int evidx, u64 addr,
  572. struct perf_sample *sample)
  573. {
  574. unsigned offset;
  575. struct sym_hist *h;
  576. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  577. if ((addr < sym->start || addr >= sym->end) &&
  578. (addr != sym->end || sym->start != sym->end)) {
  579. pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
  580. __func__, __LINE__, sym->name, sym->start, addr, sym->end);
  581. return -ERANGE;
  582. }
  583. offset = addr - sym->start;
  584. h = annotation__histogram(notes, evidx);
  585. h->nr_samples++;
  586. h->addr[offset].nr_samples++;
  587. h->period += sample->period;
  588. h->addr[offset].period += sample->period;
  589. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  590. ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
  591. sym->start, sym->name, addr, addr - sym->start, evidx,
  592. h->addr[offset].nr_samples, h->addr[offset].period);
  593. return 0;
  594. }
  595. static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
  596. {
  597. struct annotation *notes = symbol__annotation(sym);
  598. if (notes->src == NULL) {
  599. if (symbol__alloc_hist(sym) < 0)
  600. return NULL;
  601. }
  602. if (!notes->src->cycles_hist && cycles) {
  603. if (symbol__alloc_hist_cycles(sym) < 0)
  604. return NULL;
  605. }
  606. return notes;
  607. }
  608. static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  609. int evidx, u64 addr,
  610. struct perf_sample *sample)
  611. {
  612. struct annotation *notes;
  613. if (sym == NULL)
  614. return 0;
  615. notes = symbol__get_annotation(sym, false);
  616. if (notes == NULL)
  617. return -ENOMEM;
  618. return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample);
  619. }
  620. static int symbol__account_cycles(u64 addr, u64 start,
  621. struct symbol *sym, unsigned cycles)
  622. {
  623. struct annotation *notes;
  624. unsigned offset;
  625. if (sym == NULL)
  626. return 0;
  627. notes = symbol__get_annotation(sym, true);
  628. if (notes == NULL)
  629. return -ENOMEM;
  630. if (addr < sym->start || addr >= sym->end)
  631. return -ERANGE;
  632. if (start) {
  633. if (start < sym->start || start >= sym->end)
  634. return -ERANGE;
  635. if (start >= addr)
  636. start = 0;
  637. }
  638. offset = addr - sym->start;
  639. return __symbol__account_cycles(notes,
  640. start ? start - sym->start : 0,
  641. offset, cycles,
  642. !!start);
  643. }
  644. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  645. struct addr_map_symbol *start,
  646. unsigned cycles)
  647. {
  648. u64 saddr = 0;
  649. int err;
  650. if (!cycles)
  651. return 0;
  652. /*
  653. * Only set start when IPC can be computed. We can only
  654. * compute it when the basic block is completely in a single
  655. * function.
  656. * Special case the case when the jump is elsewhere, but
  657. * it starts on the function start.
  658. */
  659. if (start &&
  660. (start->sym == ams->sym ||
  661. (ams->sym &&
  662. start->addr == ams->sym->start + ams->map->start)))
  663. saddr = start->al_addr;
  664. if (saddr == 0)
  665. pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
  666. ams->addr,
  667. start ? start->addr : 0,
  668. ams->sym ? ams->sym->start + ams->map->start : 0,
  669. saddr);
  670. err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
  671. if (err)
  672. pr_debug2("account_cycles failed %d\n", err);
  673. return err;
  674. }
  675. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  676. int evidx)
  677. {
  678. return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample);
  679. }
  680. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  681. int evidx, u64 ip)
  682. {
  683. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample);
  684. }
  685. static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
  686. {
  687. dl->ins.ops = ins__find(arch, dl->ins.name);
  688. if (!dl->ins.ops)
  689. return;
  690. if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
  691. dl->ins.ops = NULL;
  692. }
  693. static int disasm_line__parse(char *line, const char **namep, char **rawp)
  694. {
  695. char tmp, *name = ltrim(line);
  696. if (name[0] == '\0')
  697. return -1;
  698. *rawp = name + 1;
  699. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  700. ++*rawp;
  701. tmp = (*rawp)[0];
  702. (*rawp)[0] = '\0';
  703. *namep = strdup(name);
  704. if (*namep == NULL)
  705. goto out_free_name;
  706. (*rawp)[0] = tmp;
  707. *rawp = ltrim(*rawp);
  708. return 0;
  709. out_free_name:
  710. free((void *)namep);
  711. *namep = NULL;
  712. return -1;
  713. }
  714. struct annotate_args {
  715. size_t privsize;
  716. struct arch *arch;
  717. struct map *map;
  718. struct perf_evsel *evsel;
  719. s64 offset;
  720. char *line;
  721. int line_nr;
  722. };
  723. static void annotation_line__delete(struct annotation_line *al)
  724. {
  725. void *ptr = (void *) al - al->privsize;
  726. zfree(&al->line);
  727. free(ptr);
  728. }
  729. /*
  730. * Allocating the annotation line data with following
  731. * structure:
  732. *
  733. * --------------------------------------
  734. * private space | struct annotation_line
  735. * --------------------------------------
  736. *
  737. * Size of the private space is stored in 'struct annotation_line'.
  738. *
  739. */
  740. static struct annotation_line *
  741. annotation_line__new(struct annotate_args *args, size_t privsize)
  742. {
  743. struct annotation_line *al;
  744. struct perf_evsel *evsel = args->evsel;
  745. size_t size = privsize + sizeof(*al);
  746. int nr = 1;
  747. if (perf_evsel__is_group_event(evsel))
  748. nr = evsel->nr_members;
  749. size += sizeof(al->samples[0]) * nr;
  750. al = zalloc(size);
  751. if (al) {
  752. al = (void *) al + privsize;
  753. al->privsize = privsize;
  754. al->offset = args->offset;
  755. al->line = strdup(args->line);
  756. al->line_nr = args->line_nr;
  757. al->samples_nr = nr;
  758. }
  759. return al;
  760. }
  761. /*
  762. * Allocating the disasm annotation line data with
  763. * following structure:
  764. *
  765. * ------------------------------------------------------------
  766. * privsize space | struct disasm_line | struct annotation_line
  767. * ------------------------------------------------------------
  768. *
  769. * We have 'struct annotation_line' member as last member
  770. * of 'struct disasm_line' to have an easy access.
  771. *
  772. */
  773. static struct disasm_line *disasm_line__new(struct annotate_args *args)
  774. {
  775. struct disasm_line *dl = NULL;
  776. struct annotation_line *al;
  777. size_t privsize = args->privsize + offsetof(struct disasm_line, al);
  778. al = annotation_line__new(args, privsize);
  779. if (al != NULL) {
  780. dl = disasm_line(al);
  781. if (dl->al.line == NULL)
  782. goto out_delete;
  783. if (args->offset != -1) {
  784. if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
  785. goto out_free_line;
  786. disasm_line__init_ins(dl, args->arch, args->map);
  787. }
  788. }
  789. return dl;
  790. out_free_line:
  791. zfree(&dl->al.line);
  792. out_delete:
  793. free(dl);
  794. return NULL;
  795. }
  796. void disasm_line__free(struct disasm_line *dl)
  797. {
  798. if (dl->ins.ops && dl->ins.ops->free)
  799. dl->ins.ops->free(&dl->ops);
  800. else
  801. ins__delete(&dl->ops);
  802. free((void *)dl->ins.name);
  803. dl->ins.name = NULL;
  804. annotation_line__delete(&dl->al);
  805. }
  806. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  807. {
  808. if (raw || !dl->ins.ops)
  809. return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
  810. return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
  811. }
  812. static void annotation_line__add(struct annotation_line *al, struct list_head *head)
  813. {
  814. list_add_tail(&al->node, head);
  815. }
  816. struct annotation_line *
  817. annotation_line__next(struct annotation_line *pos, struct list_head *head)
  818. {
  819. list_for_each_entry_continue(pos, head, node)
  820. if (pos->offset >= 0)
  821. return pos;
  822. return NULL;
  823. }
  824. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  825. s64 end, const char **path, struct sym_hist_entry *sample)
  826. {
  827. struct source_line *src_line = notes->src->lines;
  828. double percent = 0.0;
  829. sample->nr_samples = sample->period = 0;
  830. if (src_line) {
  831. size_t sizeof_src_line = sizeof(*src_line) +
  832. sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
  833. while (offset < end) {
  834. src_line = (void *)notes->src->lines +
  835. (sizeof_src_line * offset);
  836. if (*path == NULL)
  837. *path = src_line->path;
  838. percent += src_line->samples[evidx].percent;
  839. sample->nr_samples += src_line->samples[evidx].nr;
  840. offset++;
  841. }
  842. } else {
  843. struct sym_hist *h = annotation__histogram(notes, evidx);
  844. unsigned int hits = 0;
  845. u64 period = 0;
  846. while (offset < end) {
  847. hits += h->addr[offset].nr_samples;
  848. period += h->addr[offset].period;
  849. ++offset;
  850. }
  851. if (h->nr_samples) {
  852. sample->period = period;
  853. sample->nr_samples = hits;
  854. percent = 100.0 * hits / h->nr_samples;
  855. }
  856. }
  857. return percent;
  858. }
  859. static const char *annotate__address_color(struct block_range *br)
  860. {
  861. double cov = block_range__coverage(br);
  862. if (cov >= 0) {
  863. /* mark red for >75% coverage */
  864. if (cov > 0.75)
  865. return PERF_COLOR_RED;
  866. /* mark dull for <1% coverage */
  867. if (cov < 0.01)
  868. return PERF_COLOR_NORMAL;
  869. }
  870. return PERF_COLOR_MAGENTA;
  871. }
  872. static const char *annotate__asm_color(struct block_range *br)
  873. {
  874. double cov = block_range__coverage(br);
  875. if (cov >= 0) {
  876. /* mark dull for <1% coverage */
  877. if (cov < 0.01)
  878. return PERF_COLOR_NORMAL;
  879. }
  880. return PERF_COLOR_BLUE;
  881. }
  882. static void annotate__branch_printf(struct block_range *br, u64 addr)
  883. {
  884. bool emit_comment = true;
  885. if (!br)
  886. return;
  887. #if 1
  888. if (br->is_target && br->start == addr) {
  889. struct block_range *branch = br;
  890. double p;
  891. /*
  892. * Find matching branch to our target.
  893. */
  894. while (!branch->is_branch)
  895. branch = block_range__next(branch);
  896. p = 100 *(double)br->entry / branch->coverage;
  897. if (p > 0.1) {
  898. if (emit_comment) {
  899. emit_comment = false;
  900. printf("\t#");
  901. }
  902. /*
  903. * The percentage of coverage joined at this target in relation
  904. * to the next branch.
  905. */
  906. printf(" +%.2f%%", p);
  907. }
  908. }
  909. #endif
  910. if (br->is_branch && br->end == addr) {
  911. double p = 100*(double)br->taken / br->coverage;
  912. if (p > 0.1) {
  913. if (emit_comment) {
  914. emit_comment = false;
  915. printf("\t#");
  916. }
  917. /*
  918. * The percentage of coverage leaving at this branch, and
  919. * its prediction ratio.
  920. */
  921. printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
  922. }
  923. }
  924. }
  925. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  926. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  927. int max_lines, struct disasm_line *queue)
  928. {
  929. static const char *prev_line;
  930. static const char *prev_color;
  931. if (dl->al.offset != -1) {
  932. const char *path = NULL;
  933. double percent, max_percent = 0.0;
  934. double *ppercents = &percent;
  935. struct sym_hist_entry sample;
  936. struct sym_hist_entry *psamples = &sample;
  937. int i, nr_percent = 1;
  938. const char *color;
  939. struct annotation *notes = symbol__annotation(sym);
  940. s64 offset = dl->al.offset;
  941. const u64 addr = start + offset;
  942. struct annotation_line *next;
  943. struct block_range *br;
  944. next = annotation_line__next(&dl->al, &notes->src->source);
  945. if (perf_evsel__is_group_event(evsel)) {
  946. nr_percent = evsel->nr_members;
  947. ppercents = calloc(nr_percent, sizeof(double));
  948. psamples = calloc(nr_percent, sizeof(struct sym_hist_entry));
  949. if (ppercents == NULL || psamples == NULL) {
  950. return -1;
  951. }
  952. }
  953. for (i = 0; i < nr_percent; i++) {
  954. percent = disasm__calc_percent(notes,
  955. notes->src->lines ? i : evsel->idx + i,
  956. offset,
  957. next ? next->offset : (s64) len,
  958. &path, &sample);
  959. ppercents[i] = percent;
  960. psamples[i] = sample;
  961. if (percent > max_percent)
  962. max_percent = percent;
  963. }
  964. if (max_percent < min_pcnt)
  965. return -1;
  966. if (max_lines && printed >= max_lines)
  967. return 1;
  968. if (queue != NULL) {
  969. list_for_each_entry_from(queue, &notes->src->source, al.node) {
  970. if (queue == dl)
  971. break;
  972. disasm_line__print(queue, sym, start, evsel, len,
  973. 0, 0, 1, NULL);
  974. }
  975. }
  976. color = get_percent_color(max_percent);
  977. /*
  978. * Also color the filename and line if needed, with
  979. * the same color than the percentage. Don't print it
  980. * twice for close colored addr with the same filename:line
  981. */
  982. if (path) {
  983. if (!prev_line || strcmp(prev_line, path)
  984. || color != prev_color) {
  985. color_fprintf(stdout, color, " %s", path);
  986. prev_line = path;
  987. prev_color = color;
  988. }
  989. }
  990. for (i = 0; i < nr_percent; i++) {
  991. percent = ppercents[i];
  992. sample = psamples[i];
  993. color = get_percent_color(percent);
  994. if (symbol_conf.show_total_period)
  995. color_fprintf(stdout, color, " %11" PRIu64,
  996. sample.period);
  997. else if (symbol_conf.show_nr_samples)
  998. color_fprintf(stdout, color, " %7" PRIu64,
  999. sample.nr_samples);
  1000. else
  1001. color_fprintf(stdout, color, " %7.2f", percent);
  1002. }
  1003. printf(" : ");
  1004. br = block_range__find(addr);
  1005. color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
  1006. color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
  1007. annotate__branch_printf(br, addr);
  1008. printf("\n");
  1009. if (ppercents != &percent)
  1010. free(ppercents);
  1011. if (psamples != &sample)
  1012. free(psamples);
  1013. } else if (max_lines && printed >= max_lines)
  1014. return 1;
  1015. else {
  1016. int width = symbol_conf.show_total_period ? 12 : 8;
  1017. if (queue)
  1018. return -1;
  1019. if (perf_evsel__is_group_event(evsel))
  1020. width *= evsel->nr_members;
  1021. if (!*dl->al.line)
  1022. printf(" %*s:\n", width, " ");
  1023. else
  1024. printf(" %*s: %s\n", width, " ", dl->al.line);
  1025. }
  1026. return 0;
  1027. }
  1028. /*
  1029. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  1030. * which looks like following
  1031. *
  1032. * 0000000000415500 <_init>:
  1033. * 415500: sub $0x8,%rsp
  1034. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  1035. * 41550b: test %rax,%rax
  1036. * 41550e: je 415515 <_init+0x15>
  1037. * 415510: callq 416e70 <__gmon_start__@plt>
  1038. * 415515: add $0x8,%rsp
  1039. * 415519: retq
  1040. *
  1041. * it will be parsed and saved into struct disasm_line as
  1042. * <offset> <name> <ops.raw>
  1043. *
  1044. * The offset will be a relative offset from the start of the symbol and -1
  1045. * means that it's not a disassembly line so should be treated differently.
  1046. * The ops.raw part will be parsed further according to type of the instruction.
  1047. */
  1048. static int symbol__parse_objdump_line(struct symbol *sym, FILE *file,
  1049. struct annotate_args *args,
  1050. int *line_nr)
  1051. {
  1052. struct map *map = args->map;
  1053. struct annotation *notes = symbol__annotation(sym);
  1054. struct disasm_line *dl;
  1055. char *line = NULL, *parsed_line, *tmp, *tmp2;
  1056. size_t line_len;
  1057. s64 line_ip, offset = -1;
  1058. regmatch_t match[2];
  1059. if (getline(&line, &line_len, file) < 0)
  1060. return -1;
  1061. if (!line)
  1062. return -1;
  1063. line_ip = -1;
  1064. parsed_line = rtrim(line);
  1065. /* /filename:linenr ? Save line number and ignore. */
  1066. if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
  1067. *line_nr = atoi(parsed_line + match[1].rm_so);
  1068. return 0;
  1069. }
  1070. tmp = ltrim(parsed_line);
  1071. if (*tmp) {
  1072. /*
  1073. * Parse hexa addresses followed by ':'
  1074. */
  1075. line_ip = strtoull(tmp, &tmp2, 16);
  1076. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  1077. line_ip = -1;
  1078. }
  1079. if (line_ip != -1) {
  1080. u64 start = map__rip_2objdump(map, sym->start),
  1081. end = map__rip_2objdump(map, sym->end);
  1082. offset = line_ip - start;
  1083. if ((u64)line_ip < start || (u64)line_ip >= end)
  1084. offset = -1;
  1085. else
  1086. parsed_line = tmp2 + 1;
  1087. }
  1088. args->offset = offset;
  1089. args->line = parsed_line;
  1090. args->line_nr = *line_nr;
  1091. dl = disasm_line__new(args);
  1092. free(line);
  1093. (*line_nr)++;
  1094. if (dl == NULL)
  1095. return -1;
  1096. if (!disasm_line__has_offset(dl)) {
  1097. dl->ops.target.offset = dl->ops.target.addr -
  1098. map__rip_2objdump(map, sym->start);
  1099. dl->ops.target.offset_avail = true;
  1100. }
  1101. /* kcore has no symbols, so add the call target name */
  1102. if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
  1103. struct addr_map_symbol target = {
  1104. .map = map,
  1105. .addr = dl->ops.target.addr,
  1106. };
  1107. if (!map_groups__find_ams(&target) &&
  1108. target.sym->start == target.al_addr)
  1109. dl->ops.target.name = strdup(target.sym->name);
  1110. }
  1111. annotation_line__add(&dl->al, &notes->src->source);
  1112. return 0;
  1113. }
  1114. static __attribute__((constructor)) void symbol__init_regexpr(void)
  1115. {
  1116. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  1117. }
  1118. static void delete_last_nop(struct symbol *sym)
  1119. {
  1120. struct annotation *notes = symbol__annotation(sym);
  1121. struct list_head *list = &notes->src->source;
  1122. struct disasm_line *dl;
  1123. while (!list_empty(list)) {
  1124. dl = list_entry(list->prev, struct disasm_line, al.node);
  1125. if (dl->ins.ops) {
  1126. if (dl->ins.ops != &nop_ops)
  1127. return;
  1128. } else {
  1129. if (!strstr(dl->al.line, " nop ") &&
  1130. !strstr(dl->al.line, " nopl ") &&
  1131. !strstr(dl->al.line, " nopw "))
  1132. return;
  1133. }
  1134. list_del(&dl->al.node);
  1135. disasm_line__free(dl);
  1136. }
  1137. }
  1138. int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
  1139. int errnum, char *buf, size_t buflen)
  1140. {
  1141. struct dso *dso = map->dso;
  1142. BUG_ON(buflen == 0);
  1143. if (errnum >= 0) {
  1144. str_error_r(errnum, buf, buflen);
  1145. return 0;
  1146. }
  1147. switch (errnum) {
  1148. case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
  1149. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  1150. char *build_id_msg = NULL;
  1151. if (dso->has_build_id) {
  1152. build_id__sprintf(dso->build_id,
  1153. sizeof(dso->build_id), bf + 15);
  1154. build_id_msg = bf;
  1155. }
  1156. scnprintf(buf, buflen,
  1157. "No vmlinux file%s\nwas found in the path.\n\n"
  1158. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  1159. "Please use:\n\n"
  1160. " perf buildid-cache -vu vmlinux\n\n"
  1161. "or:\n\n"
  1162. " --vmlinux vmlinux\n", build_id_msg ?: "");
  1163. }
  1164. break;
  1165. default:
  1166. scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
  1167. break;
  1168. }
  1169. return 0;
  1170. }
  1171. static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
  1172. {
  1173. char linkname[PATH_MAX];
  1174. char *build_id_filename;
  1175. char *build_id_path = NULL;
  1176. char *pos;
  1177. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  1178. !dso__is_kcore(dso))
  1179. return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
  1180. build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
  1181. if (build_id_filename) {
  1182. __symbol__join_symfs(filename, filename_size, build_id_filename);
  1183. free(build_id_filename);
  1184. } else {
  1185. if (dso->has_build_id)
  1186. return ENOMEM;
  1187. goto fallback;
  1188. }
  1189. build_id_path = strdup(filename);
  1190. if (!build_id_path)
  1191. return -1;
  1192. /*
  1193. * old style build-id cache has name of XX/XXXXXXX.. while
  1194. * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
  1195. * extract the build-id part of dirname in the new style only.
  1196. */
  1197. pos = strrchr(build_id_path, '/');
  1198. if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
  1199. dirname(build_id_path);
  1200. if (dso__is_kcore(dso) ||
  1201. readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
  1202. strstr(linkname, DSO__NAME_KALLSYMS) ||
  1203. access(filename, R_OK)) {
  1204. fallback:
  1205. /*
  1206. * If we don't have build-ids or the build-id file isn't in the
  1207. * cache, or is just a kallsyms file, well, lets hope that this
  1208. * DSO is the same as when 'perf record' ran.
  1209. */
  1210. __symbol__join_symfs(filename, filename_size, dso->long_name);
  1211. }
  1212. free(build_id_path);
  1213. return 0;
  1214. }
  1215. static const char *annotate__norm_arch(const char *arch_name)
  1216. {
  1217. struct utsname uts;
  1218. if (!arch_name) { /* Assume we are annotating locally. */
  1219. if (uname(&uts) < 0)
  1220. return NULL;
  1221. arch_name = uts.machine;
  1222. }
  1223. return normalize_arch((char *)arch_name);
  1224. }
  1225. static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
  1226. {
  1227. struct map *map = args->map;
  1228. struct dso *dso = map->dso;
  1229. char command[PATH_MAX * 2];
  1230. FILE *file;
  1231. char symfs_filename[PATH_MAX];
  1232. struct kcore_extract kce;
  1233. bool delete_extract = false;
  1234. int stdout_fd[2];
  1235. int lineno = 0;
  1236. int nline;
  1237. pid_t pid;
  1238. int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
  1239. if (err)
  1240. return err;
  1241. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  1242. symfs_filename, sym->name, map->unmap_ip(map, sym->start),
  1243. map->unmap_ip(map, sym->end));
  1244. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  1245. dso, dso->long_name, sym, sym->name);
  1246. if (dso__is_kcore(dso)) {
  1247. kce.kcore_filename = symfs_filename;
  1248. kce.addr = map__rip_2objdump(map, sym->start);
  1249. kce.offs = sym->start;
  1250. kce.len = sym->end - sym->start;
  1251. if (!kcore_extract__create(&kce)) {
  1252. delete_extract = true;
  1253. strlcpy(symfs_filename, kce.extract_filename,
  1254. sizeof(symfs_filename));
  1255. }
  1256. } else if (dso__needs_decompress(dso)) {
  1257. char tmp[KMOD_DECOMP_LEN];
  1258. if (dso__decompress_kmodule_path(dso, symfs_filename,
  1259. tmp, sizeof(tmp)) < 0)
  1260. goto out;
  1261. strcpy(symfs_filename, tmp);
  1262. }
  1263. snprintf(command, sizeof(command),
  1264. "%s %s%s --start-address=0x%016" PRIx64
  1265. " --stop-address=0x%016" PRIx64
  1266. " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
  1267. objdump_path ? objdump_path : "objdump",
  1268. disassembler_style ? "-M " : "",
  1269. disassembler_style ? disassembler_style : "",
  1270. map__rip_2objdump(map, sym->start),
  1271. map__rip_2objdump(map, sym->end),
  1272. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  1273. symbol_conf.annotate_src ? "-S" : "",
  1274. symfs_filename, symfs_filename);
  1275. pr_debug("Executing: %s\n", command);
  1276. err = -1;
  1277. if (pipe(stdout_fd) < 0) {
  1278. pr_err("Failure creating the pipe to run %s\n", command);
  1279. goto out_remove_tmp;
  1280. }
  1281. pid = fork();
  1282. if (pid < 0) {
  1283. pr_err("Failure forking to run %s\n", command);
  1284. goto out_close_stdout;
  1285. }
  1286. if (pid == 0) {
  1287. close(stdout_fd[0]);
  1288. dup2(stdout_fd[1], 1);
  1289. close(stdout_fd[1]);
  1290. execl("/bin/sh", "sh", "-c", command, NULL);
  1291. perror(command);
  1292. exit(-1);
  1293. }
  1294. close(stdout_fd[1]);
  1295. file = fdopen(stdout_fd[0], "r");
  1296. if (!file) {
  1297. pr_err("Failure creating FILE stream for %s\n", command);
  1298. /*
  1299. * If we were using debug info should retry with
  1300. * original binary.
  1301. */
  1302. goto out_remove_tmp;
  1303. }
  1304. nline = 0;
  1305. while (!feof(file)) {
  1306. /*
  1307. * The source code line number (lineno) needs to be kept in
  1308. * accross calls to symbol__parse_objdump_line(), so that it
  1309. * can associate it with the instructions till the next one.
  1310. * See disasm_line__new() and struct disasm_line::line_nr.
  1311. */
  1312. if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0)
  1313. break;
  1314. nline++;
  1315. }
  1316. if (nline == 0)
  1317. pr_err("No output from %s\n", command);
  1318. /*
  1319. * kallsyms does not have symbol sizes so there may a nop at the end.
  1320. * Remove it.
  1321. */
  1322. if (dso__is_kcore(dso))
  1323. delete_last_nop(sym);
  1324. fclose(file);
  1325. err = 0;
  1326. out_remove_tmp:
  1327. close(stdout_fd[0]);
  1328. if (dso__needs_decompress(dso))
  1329. unlink(symfs_filename);
  1330. if (delete_extract)
  1331. kcore_extract__delete(&kce);
  1332. out:
  1333. return err;
  1334. out_close_stdout:
  1335. close(stdout_fd[1]);
  1336. goto out_remove_tmp;
  1337. }
  1338. static void calc_percent(struct sym_hist *hist,
  1339. struct annotation_data *sample,
  1340. s64 offset, s64 end)
  1341. {
  1342. unsigned int hits = 0;
  1343. u64 period = 0;
  1344. while (offset < end) {
  1345. hits += hist->addr[offset].nr_samples;
  1346. period += hist->addr[offset].period;
  1347. ++offset;
  1348. }
  1349. if (hist->nr_samples) {
  1350. sample->he.period = period;
  1351. sample->he.nr_samples = hits;
  1352. sample->percent = 100.0 * hits / hist->nr_samples;
  1353. }
  1354. }
  1355. static int annotation__calc_percent(struct annotation *notes,
  1356. struct perf_evsel *evsel, s64 len)
  1357. {
  1358. struct annotation_line *al, *next;
  1359. list_for_each_entry(al, &notes->src->source, node) {
  1360. s64 end;
  1361. int i;
  1362. if (al->offset == -1)
  1363. continue;
  1364. next = annotation_line__next(al, &notes->src->source);
  1365. end = next ? next->offset : len;
  1366. for (i = 0; i < al->samples_nr; i++) {
  1367. struct annotation_data *sample;
  1368. struct sym_hist *hist;
  1369. hist = annotation__histogram(notes, evsel->idx + i);
  1370. sample = &al->samples[i];
  1371. calc_percent(hist, sample, al->offset, end);
  1372. }
  1373. }
  1374. return 0;
  1375. }
  1376. int symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
  1377. {
  1378. struct annotation *notes = symbol__annotation(sym);
  1379. return annotation__calc_percent(notes, evsel, symbol__size(sym));
  1380. }
  1381. int symbol__annotate(struct symbol *sym, struct map *map,
  1382. struct perf_evsel *evsel, size_t privsize,
  1383. struct arch **parch, char *cpuid)
  1384. {
  1385. struct annotate_args args = {
  1386. .privsize = privsize,
  1387. .map = map,
  1388. .evsel = evsel,
  1389. };
  1390. const char *arch_name = NULL;
  1391. struct arch *arch;
  1392. int err;
  1393. if (evsel)
  1394. arch_name = perf_evsel__env_arch(evsel);
  1395. arch_name = annotate__norm_arch(arch_name);
  1396. if (!arch_name)
  1397. return -1;
  1398. args.arch = arch = arch__find(arch_name);
  1399. if (arch == NULL)
  1400. return -ENOTSUP;
  1401. if (parch)
  1402. *parch = arch;
  1403. if (arch->init) {
  1404. err = arch->init(arch, cpuid);
  1405. if (err) {
  1406. pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
  1407. return err;
  1408. }
  1409. }
  1410. err = symbol__disassemble(sym, &args);
  1411. if (err)
  1412. return err;
  1413. return symbol__calc_percent(sym, evsel);
  1414. }
  1415. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  1416. {
  1417. struct source_line *iter;
  1418. struct rb_node **p = &root->rb_node;
  1419. struct rb_node *parent = NULL;
  1420. int i, ret;
  1421. while (*p != NULL) {
  1422. parent = *p;
  1423. iter = rb_entry(parent, struct source_line, node);
  1424. ret = strcmp(iter->path, src_line->path);
  1425. if (ret == 0) {
  1426. for (i = 0; i < src_line->nr_pcnt; i++)
  1427. iter->samples[i].percent_sum += src_line->samples[i].percent;
  1428. return;
  1429. }
  1430. if (ret < 0)
  1431. p = &(*p)->rb_left;
  1432. else
  1433. p = &(*p)->rb_right;
  1434. }
  1435. for (i = 0; i < src_line->nr_pcnt; i++)
  1436. src_line->samples[i].percent_sum = src_line->samples[i].percent;
  1437. rb_link_node(&src_line->node, parent, p);
  1438. rb_insert_color(&src_line->node, root);
  1439. }
  1440. static int cmp_source_line(struct source_line *a, struct source_line *b)
  1441. {
  1442. int i;
  1443. for (i = 0; i < a->nr_pcnt; i++) {
  1444. if (a->samples[i].percent_sum == b->samples[i].percent_sum)
  1445. continue;
  1446. return a->samples[i].percent_sum > b->samples[i].percent_sum;
  1447. }
  1448. return 0;
  1449. }
  1450. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  1451. {
  1452. struct source_line *iter;
  1453. struct rb_node **p = &root->rb_node;
  1454. struct rb_node *parent = NULL;
  1455. while (*p != NULL) {
  1456. parent = *p;
  1457. iter = rb_entry(parent, struct source_line, node);
  1458. if (cmp_source_line(src_line, iter))
  1459. p = &(*p)->rb_left;
  1460. else
  1461. p = &(*p)->rb_right;
  1462. }
  1463. rb_link_node(&src_line->node, parent, p);
  1464. rb_insert_color(&src_line->node, root);
  1465. }
  1466. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1467. {
  1468. struct source_line *src_line;
  1469. struct rb_node *node;
  1470. node = rb_first(src_root);
  1471. while (node) {
  1472. struct rb_node *next;
  1473. src_line = rb_entry(node, struct source_line, node);
  1474. next = rb_next(node);
  1475. rb_erase(node, src_root);
  1476. __resort_source_line(dest_root, src_line);
  1477. node = next;
  1478. }
  1479. }
  1480. static void symbol__free_source_line(struct symbol *sym, int len)
  1481. {
  1482. struct annotation *notes = symbol__annotation(sym);
  1483. struct source_line *src_line = notes->src->lines;
  1484. size_t sizeof_src_line;
  1485. int i;
  1486. sizeof_src_line = sizeof(*src_line) +
  1487. (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
  1488. for (i = 0; i < len; i++) {
  1489. free_srcline(src_line->path);
  1490. src_line = (void *)src_line + sizeof_src_line;
  1491. }
  1492. zfree(&notes->src->lines);
  1493. }
  1494. /* Get the filename:line for the colored entries */
  1495. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  1496. struct perf_evsel *evsel,
  1497. struct rb_root *root, int len)
  1498. {
  1499. u64 start;
  1500. int i, k;
  1501. int evidx = evsel->idx;
  1502. struct source_line *src_line;
  1503. struct annotation *notes = symbol__annotation(sym);
  1504. struct sym_hist *h = annotation__histogram(notes, evidx);
  1505. struct rb_root tmp_root = RB_ROOT;
  1506. int nr_pcnt = 1;
  1507. u64 nr_samples = h->nr_samples;
  1508. size_t sizeof_src_line = sizeof(struct source_line);
  1509. if (perf_evsel__is_group_event(evsel)) {
  1510. for (i = 1; i < evsel->nr_members; i++) {
  1511. h = annotation__histogram(notes, evidx + i);
  1512. nr_samples += h->nr_samples;
  1513. }
  1514. nr_pcnt = evsel->nr_members;
  1515. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
  1516. }
  1517. if (!nr_samples)
  1518. return 0;
  1519. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  1520. if (!notes->src->lines)
  1521. return -1;
  1522. start = map__rip_2objdump(map, sym->start);
  1523. for (i = 0; i < len; i++) {
  1524. u64 offset;
  1525. double percent_max = 0.0;
  1526. src_line->nr_pcnt = nr_pcnt;
  1527. for (k = 0; k < nr_pcnt; k++) {
  1528. double percent = 0.0;
  1529. h = annotation__histogram(notes, evidx + k);
  1530. nr_samples = h->addr[i].nr_samples;
  1531. if (h->nr_samples)
  1532. percent = 100.0 * nr_samples / h->nr_samples;
  1533. if (percent > percent_max)
  1534. percent_max = percent;
  1535. src_line->samples[k].percent = percent;
  1536. src_line->samples[k].nr = nr_samples;
  1537. }
  1538. if (percent_max <= 0.5)
  1539. goto next;
  1540. offset = start + i;
  1541. src_line->path = get_srcline(map->dso, offset, NULL,
  1542. false, true);
  1543. insert_source_line(&tmp_root, src_line);
  1544. next:
  1545. src_line = (void *)src_line + sizeof_src_line;
  1546. }
  1547. resort_source_line(root, &tmp_root);
  1548. return 0;
  1549. }
  1550. static void print_summary(struct rb_root *root, const char *filename)
  1551. {
  1552. struct source_line *src_line;
  1553. struct rb_node *node;
  1554. printf("\nSorted summary for file %s\n", filename);
  1555. printf("----------------------------------------------\n\n");
  1556. if (RB_EMPTY_ROOT(root)) {
  1557. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1558. return;
  1559. }
  1560. node = rb_first(root);
  1561. while (node) {
  1562. double percent, percent_max = 0.0;
  1563. const char *color;
  1564. char *path;
  1565. int i;
  1566. src_line = rb_entry(node, struct source_line, node);
  1567. for (i = 0; i < src_line->nr_pcnt; i++) {
  1568. percent = src_line->samples[i].percent_sum;
  1569. color = get_percent_color(percent);
  1570. color_fprintf(stdout, color, " %7.2f", percent);
  1571. if (percent > percent_max)
  1572. percent_max = percent;
  1573. }
  1574. path = src_line->path;
  1575. color = get_percent_color(percent_max);
  1576. color_fprintf(stdout, color, " %s\n", path);
  1577. node = rb_next(node);
  1578. }
  1579. }
  1580. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1581. {
  1582. struct annotation *notes = symbol__annotation(sym);
  1583. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1584. u64 len = symbol__size(sym), offset;
  1585. for (offset = 0; offset < len; ++offset)
  1586. if (h->addr[offset].nr_samples != 0)
  1587. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1588. sym->start + offset, h->addr[offset].nr_samples);
  1589. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
  1590. }
  1591. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1592. struct perf_evsel *evsel, bool full_paths,
  1593. int min_pcnt, int max_lines, int context)
  1594. {
  1595. struct dso *dso = map->dso;
  1596. char *filename;
  1597. const char *d_filename;
  1598. const char *evsel_name = perf_evsel__name(evsel);
  1599. struct annotation *notes = symbol__annotation(sym);
  1600. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1601. struct disasm_line *pos, *queue = NULL;
  1602. u64 start = map__rip_2objdump(map, sym->start);
  1603. int printed = 2, queue_len = 0;
  1604. int more = 0;
  1605. u64 len;
  1606. int width = symbol_conf.show_total_period ? 12 : 8;
  1607. int graph_dotted_len;
  1608. filename = strdup(dso->long_name);
  1609. if (!filename)
  1610. return -ENOMEM;
  1611. if (full_paths)
  1612. d_filename = filename;
  1613. else
  1614. d_filename = basename(filename);
  1615. len = symbol__size(sym);
  1616. if (perf_evsel__is_group_event(evsel))
  1617. width *= evsel->nr_members;
  1618. graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
  1619. width, width, symbol_conf.show_total_period ? "Period" :
  1620. symbol_conf.show_nr_samples ? "Samples" : "Percent",
  1621. d_filename, evsel_name, h->nr_samples);
  1622. printf("%-*.*s----\n",
  1623. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1624. if (verbose > 0)
  1625. symbol__annotate_hits(sym, evsel);
  1626. list_for_each_entry(pos, &notes->src->source, al.node) {
  1627. if (context && queue == NULL) {
  1628. queue = pos;
  1629. queue_len = 0;
  1630. }
  1631. switch (disasm_line__print(pos, sym, start, evsel, len,
  1632. min_pcnt, printed, max_lines,
  1633. queue)) {
  1634. case 0:
  1635. ++printed;
  1636. if (context) {
  1637. printed += queue_len;
  1638. queue = NULL;
  1639. queue_len = 0;
  1640. }
  1641. break;
  1642. case 1:
  1643. /* filtered by max_lines */
  1644. ++more;
  1645. break;
  1646. case -1:
  1647. default:
  1648. /*
  1649. * Filtered by min_pcnt or non IP lines when
  1650. * context != 0
  1651. */
  1652. if (!context)
  1653. break;
  1654. if (queue_len == context)
  1655. queue = list_entry(queue->al.node.next, typeof(*queue), al.node);
  1656. else
  1657. ++queue_len;
  1658. break;
  1659. }
  1660. }
  1661. free(filename);
  1662. return more;
  1663. }
  1664. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1665. {
  1666. struct annotation *notes = symbol__annotation(sym);
  1667. struct sym_hist *h = annotation__histogram(notes, evidx);
  1668. memset(h, 0, notes->src->sizeof_sym_hist);
  1669. }
  1670. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1671. {
  1672. struct annotation *notes = symbol__annotation(sym);
  1673. struct sym_hist *h = annotation__histogram(notes, evidx);
  1674. int len = symbol__size(sym), offset;
  1675. h->nr_samples = 0;
  1676. for (offset = 0; offset < len; ++offset) {
  1677. h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
  1678. h->nr_samples += h->addr[offset].nr_samples;
  1679. }
  1680. }
  1681. void annotated_source__purge(struct annotated_source *as)
  1682. {
  1683. struct annotation_line *al, *n;
  1684. list_for_each_entry_safe(al, n, &as->source, node) {
  1685. list_del(&al->node);
  1686. disasm_line__free(disasm_line(al));
  1687. }
  1688. }
  1689. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1690. {
  1691. size_t printed;
  1692. if (dl->al.offset == -1)
  1693. return fprintf(fp, "%s\n", dl->al.line);
  1694. printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
  1695. if (dl->ops.raw[0] != '\0') {
  1696. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1697. dl->ops.raw);
  1698. }
  1699. return printed + fprintf(fp, "\n");
  1700. }
  1701. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1702. {
  1703. struct disasm_line *pos;
  1704. size_t printed = 0;
  1705. list_for_each_entry(pos, head, al.node)
  1706. printed += disasm_line__fprintf(pos, fp);
  1707. return printed;
  1708. }
  1709. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1710. struct perf_evsel *evsel, bool print_lines,
  1711. bool full_paths, int min_pcnt, int max_lines)
  1712. {
  1713. struct dso *dso = map->dso;
  1714. struct rb_root source_line = RB_ROOT;
  1715. u64 len;
  1716. if (symbol__annotate(sym, map, evsel, 0, NULL, NULL) < 0)
  1717. return -1;
  1718. len = symbol__size(sym);
  1719. if (print_lines) {
  1720. srcline_full_filename = full_paths;
  1721. symbol__get_source_line(sym, map, evsel, &source_line, len);
  1722. print_summary(&source_line, dso->long_name);
  1723. }
  1724. symbol__annotate_printf(sym, map, evsel, full_paths,
  1725. min_pcnt, max_lines, 0);
  1726. if (print_lines)
  1727. symbol__free_source_line(sym, len);
  1728. annotated_source__purge(symbol__annotation(sym)->src);
  1729. return 0;
  1730. }
  1731. bool ui__has_annotation(void)
  1732. {
  1733. return use_browser == 1 && perf_hpp_list.sym;
  1734. }