annotate.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777
  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 "config.h"
  17. #include "cache.h"
  18. #include "symbol.h"
  19. #include "units.h"
  20. #include "debug.h"
  21. #include "annotate.h"
  22. #include "evsel.h"
  23. #include "evlist.h"
  24. #include "block-range.h"
  25. #include "string2.h"
  26. #include "arch/common.h"
  27. #include <regex.h>
  28. #include <pthread.h>
  29. #include <linux/bitops.h>
  30. #include <linux/kernel.h>
  31. /* FIXME: For the HE_COLORSET */
  32. #include "ui/browser.h"
  33. /*
  34. * FIXME: Using the same values as slang.h,
  35. * but that header may not be available everywhere
  36. */
  37. #define LARROW_CHAR ((unsigned char)',')
  38. #define RARROW_CHAR ((unsigned char)'+')
  39. #define DARROW_CHAR ((unsigned char)'.')
  40. #define UARROW_CHAR ((unsigned char)'-')
  41. #include "sane_ctype.h"
  42. struct annotation_options annotation__default_options = {
  43. .use_offset = true,
  44. .jump_arrows = true,
  45. .annotate_src = true,
  46. .offset_level = ANNOTATION__OFFSET_JUMP_TARGETS,
  47. };
  48. static regex_t file_lineno;
  49. static struct ins_ops *ins__find(struct arch *arch, const char *name);
  50. static void ins__sort(struct arch *arch);
  51. static int disasm_line__parse(char *line, const char **namep, char **rawp);
  52. struct arch {
  53. const char *name;
  54. struct ins *instructions;
  55. size_t nr_instructions;
  56. size_t nr_instructions_allocated;
  57. struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
  58. bool sorted_instructions;
  59. bool initialized;
  60. void *priv;
  61. unsigned int model;
  62. unsigned int family;
  63. int (*init)(struct arch *arch, char *cpuid);
  64. bool (*ins_is_fused)(struct arch *arch, const char *ins1,
  65. const char *ins2);
  66. struct {
  67. char comment_char;
  68. char skip_functions_char;
  69. } objdump;
  70. };
  71. static struct ins_ops call_ops;
  72. static struct ins_ops dec_ops;
  73. static struct ins_ops jump_ops;
  74. static struct ins_ops mov_ops;
  75. static struct ins_ops nop_ops;
  76. static struct ins_ops lock_ops;
  77. static struct ins_ops ret_ops;
  78. static int arch__grow_instructions(struct arch *arch)
  79. {
  80. struct ins *new_instructions;
  81. size_t new_nr_allocated;
  82. if (arch->nr_instructions_allocated == 0 && arch->instructions)
  83. goto grow_from_non_allocated_table;
  84. new_nr_allocated = arch->nr_instructions_allocated + 128;
  85. new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
  86. if (new_instructions == NULL)
  87. return -1;
  88. out_update_instructions:
  89. arch->instructions = new_instructions;
  90. arch->nr_instructions_allocated = new_nr_allocated;
  91. return 0;
  92. grow_from_non_allocated_table:
  93. new_nr_allocated = arch->nr_instructions + 128;
  94. new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
  95. if (new_instructions == NULL)
  96. return -1;
  97. memcpy(new_instructions, arch->instructions, arch->nr_instructions);
  98. goto out_update_instructions;
  99. }
  100. static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
  101. {
  102. struct ins *ins;
  103. if (arch->nr_instructions == arch->nr_instructions_allocated &&
  104. arch__grow_instructions(arch))
  105. return -1;
  106. ins = &arch->instructions[arch->nr_instructions];
  107. ins->name = strdup(name);
  108. if (!ins->name)
  109. return -1;
  110. ins->ops = ops;
  111. arch->nr_instructions++;
  112. ins__sort(arch);
  113. return 0;
  114. }
  115. #include "arch/arm/annotate/instructions.c"
  116. #include "arch/arm64/annotate/instructions.c"
  117. #include "arch/x86/annotate/instructions.c"
  118. #include "arch/powerpc/annotate/instructions.c"
  119. #include "arch/s390/annotate/instructions.c"
  120. static struct arch architectures[] = {
  121. {
  122. .name = "arm",
  123. .init = arm__annotate_init,
  124. },
  125. {
  126. .name = "arm64",
  127. .init = arm64__annotate_init,
  128. },
  129. {
  130. .name = "x86",
  131. .init = x86__annotate_init,
  132. .instructions = x86__instructions,
  133. .nr_instructions = ARRAY_SIZE(x86__instructions),
  134. .ins_is_fused = x86__ins_is_fused,
  135. .objdump = {
  136. .comment_char = '#',
  137. },
  138. },
  139. {
  140. .name = "powerpc",
  141. .init = powerpc__annotate_init,
  142. },
  143. {
  144. .name = "s390",
  145. .init = s390__annotate_init,
  146. .objdump = {
  147. .comment_char = '#',
  148. },
  149. },
  150. };
  151. static void ins__delete(struct ins_operands *ops)
  152. {
  153. if (ops == NULL)
  154. return;
  155. zfree(&ops->source.raw);
  156. zfree(&ops->source.name);
  157. zfree(&ops->target.raw);
  158. zfree(&ops->target.name);
  159. }
  160. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  161. struct ins_operands *ops)
  162. {
  163. return scnprintf(bf, size, "%-6s %s", ins->name, ops->raw);
  164. }
  165. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  166. struct ins_operands *ops)
  167. {
  168. if (ins->ops->scnprintf)
  169. return ins->ops->scnprintf(ins, bf, size, ops);
  170. return ins__raw_scnprintf(ins, bf, size, ops);
  171. }
  172. bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
  173. {
  174. if (!arch || !arch->ins_is_fused)
  175. return false;
  176. return arch->ins_is_fused(arch, ins1, ins2);
  177. }
  178. static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
  179. {
  180. char *endptr, *tok, *name;
  181. struct map *map = ms->map;
  182. struct addr_map_symbol target = {
  183. .map = map,
  184. };
  185. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  186. name = strchr(endptr, '<');
  187. if (name == NULL)
  188. goto indirect_call;
  189. name++;
  190. if (arch->objdump.skip_functions_char &&
  191. strchr(name, arch->objdump.skip_functions_char))
  192. return -1;
  193. tok = strchr(name, '>');
  194. if (tok == NULL)
  195. return -1;
  196. *tok = '\0';
  197. ops->target.name = strdup(name);
  198. *tok = '>';
  199. if (ops->target.name == NULL)
  200. return -1;
  201. find_target:
  202. target.addr = map__objdump_2mem(map, ops->target.addr);
  203. if (map_groups__find_ams(&target) == 0 &&
  204. map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
  205. ops->target.sym = target.sym;
  206. return 0;
  207. indirect_call:
  208. tok = strchr(endptr, '*');
  209. if (tok != NULL)
  210. ops->target.addr = strtoull(tok + 1, NULL, 16);
  211. goto find_target;
  212. }
  213. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  214. struct ins_operands *ops)
  215. {
  216. if (ops->target.sym)
  217. return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name);
  218. if (ops->target.addr == 0)
  219. return ins__raw_scnprintf(ins, bf, size, ops);
  220. if (ops->target.name)
  221. return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.name);
  222. return scnprintf(bf, size, "%-6s *%" PRIx64, ins->name, ops->target.addr);
  223. }
  224. static struct ins_ops call_ops = {
  225. .parse = call__parse,
  226. .scnprintf = call__scnprintf,
  227. };
  228. bool ins__is_call(const struct ins *ins)
  229. {
  230. return ins->ops == &call_ops || ins->ops == &s390_call_ops;
  231. }
  232. static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms)
  233. {
  234. struct map *map = ms->map;
  235. struct symbol *sym = ms->sym;
  236. struct addr_map_symbol target = {
  237. .map = map,
  238. };
  239. const char *c = strchr(ops->raw, ',');
  240. u64 start, end;
  241. /*
  242. * Examples of lines to parse for the _cpp_lex_token@@Base
  243. * function:
  244. *
  245. * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
  246. * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
  247. *
  248. * The first is a jump to an offset inside the same function,
  249. * the second is to another function, i.e. that 0xa72 is an
  250. * offset in the cpp_named_operator2name@@base function.
  251. */
  252. /*
  253. * skip over possible up to 2 operands to get to address, e.g.:
  254. * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
  255. */
  256. if (c++ != NULL) {
  257. ops->target.addr = strtoull(c, NULL, 16);
  258. if (!ops->target.addr) {
  259. c = strchr(c, ',');
  260. if (c++ != NULL)
  261. ops->target.addr = strtoull(c, NULL, 16);
  262. }
  263. } else {
  264. ops->target.addr = strtoull(ops->raw, NULL, 16);
  265. }
  266. target.addr = map__objdump_2mem(map, ops->target.addr);
  267. start = map->unmap_ip(map, sym->start),
  268. end = map->unmap_ip(map, sym->end);
  269. ops->target.outside = target.addr < start || target.addr > end;
  270. /*
  271. * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
  272. cpp_named_operator2name@@Base+0xa72
  273. * Point to a place that is after the cpp_named_operator2name
  274. * boundaries, i.e. in the ELF symbol table for cc1
  275. * cpp_named_operator2name is marked as being 32-bytes long, but it in
  276. * fact is much larger than that, so we seem to need a symbols__find()
  277. * routine that looks for >= current->start and < next_symbol->start,
  278. * possibly just for C++ objects?
  279. *
  280. * For now lets just make some progress by marking jumps to outside the
  281. * current function as call like.
  282. *
  283. * Actual navigation will come next, with further understanding of how
  284. * the symbol searching and disassembly should be done.
  285. */
  286. if (map_groups__find_ams(&target) == 0 &&
  287. map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
  288. ops->target.sym = target.sym;
  289. if (!ops->target.outside) {
  290. ops->target.offset = target.addr - start;
  291. ops->target.offset_avail = true;
  292. } else {
  293. ops->target.offset_avail = false;
  294. }
  295. return 0;
  296. }
  297. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  298. struct ins_operands *ops)
  299. {
  300. const char *c;
  301. if (!ops->target.addr || ops->target.offset < 0)
  302. return ins__raw_scnprintf(ins, bf, size, ops);
  303. if (ops->target.outside && ops->target.sym != NULL)
  304. return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name);
  305. c = strchr(ops->raw, ',');
  306. if (c != NULL) {
  307. const char *c2 = strchr(c + 1, ',');
  308. /* check for 3-op insn */
  309. if (c2 != NULL)
  310. c = c2;
  311. c++;
  312. /* mirror arch objdump's space-after-comma style */
  313. if (*c == ' ')
  314. c++;
  315. }
  316. return scnprintf(bf, size, "%-6s %.*s%" PRIx64,
  317. ins->name, c ? c - ops->raw : 0, ops->raw,
  318. ops->target.offset);
  319. }
  320. static struct ins_ops jump_ops = {
  321. .parse = jump__parse,
  322. .scnprintf = jump__scnprintf,
  323. };
  324. bool ins__is_jump(const struct ins *ins)
  325. {
  326. return ins->ops == &jump_ops;
  327. }
  328. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  329. {
  330. char *endptr, *name, *t;
  331. if (strstr(raw, "(%rip)") == NULL)
  332. return 0;
  333. *addrp = strtoull(comment, &endptr, 16);
  334. if (endptr == comment)
  335. return 0;
  336. name = strchr(endptr, '<');
  337. if (name == NULL)
  338. return -1;
  339. name++;
  340. t = strchr(name, '>');
  341. if (t == NULL)
  342. return 0;
  343. *t = '\0';
  344. *namep = strdup(name);
  345. *t = '>';
  346. return 0;
  347. }
  348. static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
  349. {
  350. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  351. if (ops->locked.ops == NULL)
  352. return 0;
  353. if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
  354. goto out_free_ops;
  355. ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
  356. if (ops->locked.ins.ops == NULL)
  357. goto out_free_ops;
  358. if (ops->locked.ins.ops->parse &&
  359. ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
  360. goto out_free_ops;
  361. return 0;
  362. out_free_ops:
  363. zfree(&ops->locked.ops);
  364. return 0;
  365. }
  366. static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
  367. struct ins_operands *ops)
  368. {
  369. int printed;
  370. if (ops->locked.ins.ops == NULL)
  371. return ins__raw_scnprintf(ins, bf, size, ops);
  372. printed = scnprintf(bf, size, "%-6s ", ins->name);
  373. return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
  374. size - printed, ops->locked.ops);
  375. }
  376. static void lock__delete(struct ins_operands *ops)
  377. {
  378. struct ins *ins = &ops->locked.ins;
  379. if (ins->ops && ins->ops->free)
  380. ins->ops->free(ops->locked.ops);
  381. else
  382. ins__delete(ops->locked.ops);
  383. zfree(&ops->locked.ops);
  384. zfree(&ops->target.raw);
  385. zfree(&ops->target.name);
  386. }
  387. static struct ins_ops lock_ops = {
  388. .free = lock__delete,
  389. .parse = lock__parse,
  390. .scnprintf = lock__scnprintf,
  391. };
  392. static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
  393. {
  394. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  395. if (s == NULL)
  396. return -1;
  397. *s = '\0';
  398. ops->source.raw = strdup(ops->raw);
  399. *s = ',';
  400. if (ops->source.raw == NULL)
  401. return -1;
  402. target = ++s;
  403. comment = strchr(s, arch->objdump.comment_char);
  404. if (comment != NULL)
  405. s = comment - 1;
  406. else
  407. s = strchr(s, '\0') - 1;
  408. while (s > target && isspace(s[0]))
  409. --s;
  410. s++;
  411. prev = *s;
  412. *s = '\0';
  413. ops->target.raw = strdup(target);
  414. *s = prev;
  415. if (ops->target.raw == NULL)
  416. goto out_free_source;
  417. if (comment == NULL)
  418. return 0;
  419. comment = ltrim(comment);
  420. comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
  421. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  422. return 0;
  423. out_free_source:
  424. zfree(&ops->source.raw);
  425. return -1;
  426. }
  427. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  428. struct ins_operands *ops)
  429. {
  430. return scnprintf(bf, size, "%-6s %s,%s", ins->name,
  431. ops->source.name ?: ops->source.raw,
  432. ops->target.name ?: ops->target.raw);
  433. }
  434. static struct ins_ops mov_ops = {
  435. .parse = mov__parse,
  436. .scnprintf = mov__scnprintf,
  437. };
  438. static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
  439. {
  440. char *target, *comment, *s, prev;
  441. target = s = ops->raw;
  442. while (s[0] != '\0' && !isspace(s[0]))
  443. ++s;
  444. prev = *s;
  445. *s = '\0';
  446. ops->target.raw = strdup(target);
  447. *s = prev;
  448. if (ops->target.raw == NULL)
  449. return -1;
  450. comment = strchr(s, arch->objdump.comment_char);
  451. if (comment == NULL)
  452. return 0;
  453. comment = ltrim(comment);
  454. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  455. return 0;
  456. }
  457. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  458. struct ins_operands *ops)
  459. {
  460. return scnprintf(bf, size, "%-6s %s", ins->name,
  461. ops->target.name ?: ops->target.raw);
  462. }
  463. static struct ins_ops dec_ops = {
  464. .parse = dec__parse,
  465. .scnprintf = dec__scnprintf,
  466. };
  467. static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
  468. struct ins_operands *ops __maybe_unused)
  469. {
  470. return scnprintf(bf, size, "%-6s", "nop");
  471. }
  472. static struct ins_ops nop_ops = {
  473. .scnprintf = nop__scnprintf,
  474. };
  475. static struct ins_ops ret_ops = {
  476. .scnprintf = ins__raw_scnprintf,
  477. };
  478. bool ins__is_ret(const struct ins *ins)
  479. {
  480. return ins->ops == &ret_ops;
  481. }
  482. bool ins__is_lock(const struct ins *ins)
  483. {
  484. return ins->ops == &lock_ops;
  485. }
  486. static int ins__key_cmp(const void *name, const void *insp)
  487. {
  488. const struct ins *ins = insp;
  489. return strcmp(name, ins->name);
  490. }
  491. static int ins__cmp(const void *a, const void *b)
  492. {
  493. const struct ins *ia = a;
  494. const struct ins *ib = b;
  495. return strcmp(ia->name, ib->name);
  496. }
  497. static void ins__sort(struct arch *arch)
  498. {
  499. const int nmemb = arch->nr_instructions;
  500. qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
  501. }
  502. static struct ins_ops *__ins__find(struct arch *arch, const char *name)
  503. {
  504. struct ins *ins;
  505. const int nmemb = arch->nr_instructions;
  506. if (!arch->sorted_instructions) {
  507. ins__sort(arch);
  508. arch->sorted_instructions = true;
  509. }
  510. ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
  511. return ins ? ins->ops : NULL;
  512. }
  513. static struct ins_ops *ins__find(struct arch *arch, const char *name)
  514. {
  515. struct ins_ops *ops = __ins__find(arch, name);
  516. if (!ops && arch->associate_instruction_ops)
  517. ops = arch->associate_instruction_ops(arch, name);
  518. return ops;
  519. }
  520. static int arch__key_cmp(const void *name, const void *archp)
  521. {
  522. const struct arch *arch = archp;
  523. return strcmp(name, arch->name);
  524. }
  525. static int arch__cmp(const void *a, const void *b)
  526. {
  527. const struct arch *aa = a;
  528. const struct arch *ab = b;
  529. return strcmp(aa->name, ab->name);
  530. }
  531. static void arch__sort(void)
  532. {
  533. const int nmemb = ARRAY_SIZE(architectures);
  534. qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
  535. }
  536. static struct arch *arch__find(const char *name)
  537. {
  538. const int nmemb = ARRAY_SIZE(architectures);
  539. static bool sorted;
  540. if (!sorted) {
  541. arch__sort();
  542. sorted = true;
  543. }
  544. return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
  545. }
  546. static struct annotated_source *annotated_source__new(void)
  547. {
  548. struct annotated_source *src = zalloc(sizeof(*src));
  549. if (src != NULL)
  550. INIT_LIST_HEAD(&src->source);
  551. return src;
  552. }
  553. static __maybe_unused void annotated_source__delete(struct annotated_source *src)
  554. {
  555. if (src == NULL)
  556. return;
  557. zfree(&src->histograms);
  558. zfree(&src->cycles_hist);
  559. free(src);
  560. }
  561. static int annotated_source__alloc_histograms(struct annotated_source *src,
  562. size_t size, int nr_hists)
  563. {
  564. size_t sizeof_sym_hist;
  565. /*
  566. * Add buffer of one element for zero length symbol.
  567. * When sample is taken from first instruction of
  568. * zero length symbol, perf still resolves it and
  569. * shows symbol name in perf report and allows to
  570. * annotate it.
  571. */
  572. if (size == 0)
  573. size = 1;
  574. /* Check for overflow when calculating sizeof_sym_hist */
  575. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
  576. return -1;
  577. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
  578. /* Check for overflow in zalloc argument */
  579. if (sizeof_sym_hist > SIZE_MAX / nr_hists)
  580. return -1;
  581. src->sizeof_sym_hist = sizeof_sym_hist;
  582. src->nr_histograms = nr_hists;
  583. src->histograms = calloc(nr_hists, sizeof_sym_hist) ;
  584. return src->histograms ? 0 : -1;
  585. }
  586. /* The cycles histogram is lazily allocated. */
  587. static int symbol__alloc_hist_cycles(struct symbol *sym)
  588. {
  589. struct annotation *notes = symbol__annotation(sym);
  590. const size_t size = symbol__size(sym);
  591. notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
  592. if (notes->src->cycles_hist == NULL)
  593. return -1;
  594. return 0;
  595. }
  596. void symbol__annotate_zero_histograms(struct symbol *sym)
  597. {
  598. struct annotation *notes = symbol__annotation(sym);
  599. pthread_mutex_lock(&notes->lock);
  600. if (notes->src != NULL) {
  601. memset(notes->src->histograms, 0,
  602. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  603. if (notes->src->cycles_hist)
  604. memset(notes->src->cycles_hist, 0,
  605. symbol__size(sym) * sizeof(struct cyc_hist));
  606. }
  607. pthread_mutex_unlock(&notes->lock);
  608. }
  609. static int __symbol__account_cycles(struct cyc_hist *ch,
  610. u64 start,
  611. unsigned offset, unsigned cycles,
  612. unsigned have_start)
  613. {
  614. /*
  615. * For now we can only account one basic block per
  616. * final jump. But multiple could be overlapping.
  617. * Always account the longest one. So when
  618. * a shorter one has been already seen throw it away.
  619. *
  620. * We separately always account the full cycles.
  621. */
  622. ch[offset].num_aggr++;
  623. ch[offset].cycles_aggr += cycles;
  624. if (cycles > ch[offset].cycles_max)
  625. ch[offset].cycles_max = cycles;
  626. if (ch[offset].cycles_min) {
  627. if (cycles && cycles < ch[offset].cycles_min)
  628. ch[offset].cycles_min = cycles;
  629. } else
  630. ch[offset].cycles_min = cycles;
  631. if (!have_start && ch[offset].have_start)
  632. return 0;
  633. if (ch[offset].num) {
  634. if (have_start && (!ch[offset].have_start ||
  635. ch[offset].start > start)) {
  636. ch[offset].have_start = 0;
  637. ch[offset].cycles = 0;
  638. ch[offset].num = 0;
  639. if (ch[offset].reset < 0xffff)
  640. ch[offset].reset++;
  641. } else if (have_start &&
  642. ch[offset].start < start)
  643. return 0;
  644. }
  645. ch[offset].have_start = have_start;
  646. ch[offset].start = start;
  647. ch[offset].cycles += cycles;
  648. ch[offset].num++;
  649. return 0;
  650. }
  651. static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  652. struct annotated_source *src, int evidx, u64 addr,
  653. struct perf_sample *sample)
  654. {
  655. unsigned offset;
  656. struct sym_hist *h;
  657. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  658. if ((addr < sym->start || addr >= sym->end) &&
  659. (addr != sym->end || sym->start != sym->end)) {
  660. pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
  661. __func__, __LINE__, sym->name, sym->start, addr, sym->end);
  662. return -ERANGE;
  663. }
  664. offset = addr - sym->start;
  665. h = annotated_source__histogram(src, evidx);
  666. if (h == NULL) {
  667. pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
  668. __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC);
  669. return -ENOMEM;
  670. }
  671. h->nr_samples++;
  672. h->addr[offset].nr_samples++;
  673. h->period += sample->period;
  674. h->addr[offset].period += sample->period;
  675. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  676. ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
  677. sym->start, sym->name, addr, addr - sym->start, evidx,
  678. h->addr[offset].nr_samples, h->addr[offset].period);
  679. return 0;
  680. }
  681. static struct cyc_hist *symbol__cycles_hist(struct symbol *sym)
  682. {
  683. struct annotation *notes = symbol__annotation(sym);
  684. if (notes->src == NULL) {
  685. notes->src = annotated_source__new();
  686. if (notes->src == NULL)
  687. return NULL;
  688. goto alloc_cycles_hist;
  689. }
  690. if (!notes->src->cycles_hist) {
  691. alloc_cycles_hist:
  692. symbol__alloc_hist_cycles(sym);
  693. }
  694. return notes->src->cycles_hist;
  695. }
  696. struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists)
  697. {
  698. struct annotation *notes = symbol__annotation(sym);
  699. if (notes->src == NULL) {
  700. notes->src = annotated_source__new();
  701. if (notes->src == NULL)
  702. return NULL;
  703. goto alloc_histograms;
  704. }
  705. if (notes->src->histograms == NULL) {
  706. alloc_histograms:
  707. annotated_source__alloc_histograms(notes->src, symbol__size(sym),
  708. nr_hists);
  709. }
  710. return notes->src;
  711. }
  712. static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  713. struct perf_evsel *evsel, u64 addr,
  714. struct perf_sample *sample)
  715. {
  716. struct annotated_source *src;
  717. if (sym == NULL)
  718. return 0;
  719. src = symbol__hists(sym, evsel->evlist->nr_entries);
  720. if (src == NULL)
  721. return -ENOMEM;
  722. return __symbol__inc_addr_samples(sym, map, src, evsel->idx, addr, sample);
  723. }
  724. static int symbol__account_cycles(u64 addr, u64 start,
  725. struct symbol *sym, unsigned cycles)
  726. {
  727. struct cyc_hist *cycles_hist;
  728. unsigned offset;
  729. if (sym == NULL)
  730. return 0;
  731. cycles_hist = symbol__cycles_hist(sym);
  732. if (cycles_hist == NULL)
  733. return -ENOMEM;
  734. if (addr < sym->start || addr >= sym->end)
  735. return -ERANGE;
  736. if (start) {
  737. if (start < sym->start || start >= sym->end)
  738. return -ERANGE;
  739. if (start >= addr)
  740. start = 0;
  741. }
  742. offset = addr - sym->start;
  743. return __symbol__account_cycles(cycles_hist,
  744. start ? start - sym->start : 0,
  745. offset, cycles,
  746. !!start);
  747. }
  748. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  749. struct addr_map_symbol *start,
  750. unsigned cycles)
  751. {
  752. u64 saddr = 0;
  753. int err;
  754. if (!cycles)
  755. return 0;
  756. /*
  757. * Only set start when IPC can be computed. We can only
  758. * compute it when the basic block is completely in a single
  759. * function.
  760. * Special case the case when the jump is elsewhere, but
  761. * it starts on the function start.
  762. */
  763. if (start &&
  764. (start->sym == ams->sym ||
  765. (ams->sym &&
  766. start->addr == ams->sym->start + ams->map->start)))
  767. saddr = start->al_addr;
  768. if (saddr == 0)
  769. pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
  770. ams->addr,
  771. start ? start->addr : 0,
  772. ams->sym ? ams->sym->start + ams->map->start : 0,
  773. saddr);
  774. err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
  775. if (err)
  776. pr_debug2("account_cycles failed %d\n", err);
  777. return err;
  778. }
  779. static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
  780. {
  781. unsigned n_insn = 0;
  782. u64 offset;
  783. for (offset = start; offset <= end; offset++) {
  784. if (notes->offsets[offset])
  785. n_insn++;
  786. }
  787. return n_insn;
  788. }
  789. static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
  790. {
  791. unsigned n_insn;
  792. u64 offset;
  793. n_insn = annotation__count_insn(notes, start, end);
  794. if (n_insn && ch->num && ch->cycles) {
  795. float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
  796. /* Hide data when there are too many overlaps. */
  797. if (ch->reset >= 0x7fff || ch->reset >= ch->num / 2)
  798. return;
  799. for (offset = start; offset <= end; offset++) {
  800. struct annotation_line *al = notes->offsets[offset];
  801. if (al)
  802. al->ipc = ipc;
  803. }
  804. }
  805. }
  806. void annotation__compute_ipc(struct annotation *notes, size_t size)
  807. {
  808. u64 offset;
  809. if (!notes->src || !notes->src->cycles_hist)
  810. return;
  811. pthread_mutex_lock(&notes->lock);
  812. for (offset = 0; offset < size; ++offset) {
  813. struct cyc_hist *ch;
  814. ch = &notes->src->cycles_hist[offset];
  815. if (ch && ch->cycles) {
  816. struct annotation_line *al;
  817. if (ch->have_start)
  818. annotation__count_and_fill(notes, ch->start, offset, ch);
  819. al = notes->offsets[offset];
  820. if (al && ch->num_aggr) {
  821. al->cycles = ch->cycles_aggr / ch->num_aggr;
  822. al->cycles_max = ch->cycles_max;
  823. al->cycles_min = ch->cycles_min;
  824. }
  825. notes->have_cycles = true;
  826. }
  827. }
  828. pthread_mutex_unlock(&notes->lock);
  829. }
  830. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  831. struct perf_evsel *evsel)
  832. {
  833. return symbol__inc_addr_samples(ams->sym, ams->map, evsel, ams->al_addr, sample);
  834. }
  835. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  836. struct perf_evsel *evsel, u64 ip)
  837. {
  838. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evsel, ip, sample);
  839. }
  840. static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
  841. {
  842. dl->ins.ops = ins__find(arch, dl->ins.name);
  843. if (!dl->ins.ops)
  844. return;
  845. if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
  846. dl->ins.ops = NULL;
  847. }
  848. static int disasm_line__parse(char *line, const char **namep, char **rawp)
  849. {
  850. char tmp, *name = ltrim(line);
  851. if (name[0] == '\0')
  852. return -1;
  853. *rawp = name + 1;
  854. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  855. ++*rawp;
  856. tmp = (*rawp)[0];
  857. (*rawp)[0] = '\0';
  858. *namep = strdup(name);
  859. if (*namep == NULL)
  860. goto out_free_name;
  861. (*rawp)[0] = tmp;
  862. *rawp = ltrim(*rawp);
  863. return 0;
  864. out_free_name:
  865. free((void *)namep);
  866. *namep = NULL;
  867. return -1;
  868. }
  869. struct annotate_args {
  870. size_t privsize;
  871. struct arch *arch;
  872. struct map_symbol ms;
  873. struct perf_evsel *evsel;
  874. struct annotation_options *options;
  875. s64 offset;
  876. char *line;
  877. int line_nr;
  878. };
  879. static void annotation_line__delete(struct annotation_line *al)
  880. {
  881. void *ptr = (void *) al - al->privsize;
  882. free_srcline(al->path);
  883. zfree(&al->line);
  884. free(ptr);
  885. }
  886. /*
  887. * Allocating the annotation line data with following
  888. * structure:
  889. *
  890. * --------------------------------------
  891. * private space | struct annotation_line
  892. * --------------------------------------
  893. *
  894. * Size of the private space is stored in 'struct annotation_line'.
  895. *
  896. */
  897. static struct annotation_line *
  898. annotation_line__new(struct annotate_args *args, size_t privsize)
  899. {
  900. struct annotation_line *al;
  901. struct perf_evsel *evsel = args->evsel;
  902. size_t size = privsize + sizeof(*al);
  903. int nr = 1;
  904. if (perf_evsel__is_group_event(evsel))
  905. nr = evsel->nr_members;
  906. size += sizeof(al->data[0]) * nr;
  907. al = zalloc(size);
  908. if (al) {
  909. al = (void *) al + privsize;
  910. al->privsize = privsize;
  911. al->offset = args->offset;
  912. al->line = strdup(args->line);
  913. al->line_nr = args->line_nr;
  914. al->data_nr = nr;
  915. }
  916. return al;
  917. }
  918. /*
  919. * Allocating the disasm annotation line data with
  920. * following structure:
  921. *
  922. * ------------------------------------------------------------
  923. * privsize space | struct disasm_line | struct annotation_line
  924. * ------------------------------------------------------------
  925. *
  926. * We have 'struct annotation_line' member as last member
  927. * of 'struct disasm_line' to have an easy access.
  928. *
  929. */
  930. static struct disasm_line *disasm_line__new(struct annotate_args *args)
  931. {
  932. struct disasm_line *dl = NULL;
  933. struct annotation_line *al;
  934. size_t privsize = args->privsize + offsetof(struct disasm_line, al);
  935. al = annotation_line__new(args, privsize);
  936. if (al != NULL) {
  937. dl = disasm_line(al);
  938. if (dl->al.line == NULL)
  939. goto out_delete;
  940. if (args->offset != -1) {
  941. if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
  942. goto out_free_line;
  943. disasm_line__init_ins(dl, args->arch, &args->ms);
  944. }
  945. }
  946. return dl;
  947. out_free_line:
  948. zfree(&dl->al.line);
  949. out_delete:
  950. free(dl);
  951. return NULL;
  952. }
  953. void disasm_line__free(struct disasm_line *dl)
  954. {
  955. if (dl->ins.ops && dl->ins.ops->free)
  956. dl->ins.ops->free(&dl->ops);
  957. else
  958. ins__delete(&dl->ops);
  959. free((void *)dl->ins.name);
  960. dl->ins.name = NULL;
  961. annotation_line__delete(&dl->al);
  962. }
  963. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  964. {
  965. if (raw || !dl->ins.ops)
  966. return scnprintf(bf, size, "%-6s %s", dl->ins.name, dl->ops.raw);
  967. return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
  968. }
  969. static void annotation_line__add(struct annotation_line *al, struct list_head *head)
  970. {
  971. list_add_tail(&al->node, head);
  972. }
  973. struct annotation_line *
  974. annotation_line__next(struct annotation_line *pos, struct list_head *head)
  975. {
  976. list_for_each_entry_continue(pos, head, node)
  977. if (pos->offset >= 0)
  978. return pos;
  979. return NULL;
  980. }
  981. static const char *annotate__address_color(struct block_range *br)
  982. {
  983. double cov = block_range__coverage(br);
  984. if (cov >= 0) {
  985. /* mark red for >75% coverage */
  986. if (cov > 0.75)
  987. return PERF_COLOR_RED;
  988. /* mark dull for <1% coverage */
  989. if (cov < 0.01)
  990. return PERF_COLOR_NORMAL;
  991. }
  992. return PERF_COLOR_MAGENTA;
  993. }
  994. static const char *annotate__asm_color(struct block_range *br)
  995. {
  996. double cov = block_range__coverage(br);
  997. if (cov >= 0) {
  998. /* mark dull for <1% coverage */
  999. if (cov < 0.01)
  1000. return PERF_COLOR_NORMAL;
  1001. }
  1002. return PERF_COLOR_BLUE;
  1003. }
  1004. static void annotate__branch_printf(struct block_range *br, u64 addr)
  1005. {
  1006. bool emit_comment = true;
  1007. if (!br)
  1008. return;
  1009. #if 1
  1010. if (br->is_target && br->start == addr) {
  1011. struct block_range *branch = br;
  1012. double p;
  1013. /*
  1014. * Find matching branch to our target.
  1015. */
  1016. while (!branch->is_branch)
  1017. branch = block_range__next(branch);
  1018. p = 100 *(double)br->entry / branch->coverage;
  1019. if (p > 0.1) {
  1020. if (emit_comment) {
  1021. emit_comment = false;
  1022. printf("\t#");
  1023. }
  1024. /*
  1025. * The percentage of coverage joined at this target in relation
  1026. * to the next branch.
  1027. */
  1028. printf(" +%.2f%%", p);
  1029. }
  1030. }
  1031. #endif
  1032. if (br->is_branch && br->end == addr) {
  1033. double p = 100*(double)br->taken / br->coverage;
  1034. if (p > 0.1) {
  1035. if (emit_comment) {
  1036. emit_comment = false;
  1037. printf("\t#");
  1038. }
  1039. /*
  1040. * The percentage of coverage leaving at this branch, and
  1041. * its prediction ratio.
  1042. */
  1043. printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
  1044. }
  1045. }
  1046. }
  1047. static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
  1048. {
  1049. s64 offset = dl->al.offset;
  1050. const u64 addr = start + offset;
  1051. struct block_range *br;
  1052. br = block_range__find(addr);
  1053. color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
  1054. color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
  1055. annotate__branch_printf(br, addr);
  1056. return 0;
  1057. }
  1058. static int
  1059. annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
  1060. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  1061. int max_lines, struct annotation_line *queue, int addr_fmt_width)
  1062. {
  1063. struct disasm_line *dl = container_of(al, struct disasm_line, al);
  1064. static const char *prev_line;
  1065. static const char *prev_color;
  1066. if (al->offset != -1) {
  1067. double max_percent = 0.0;
  1068. int i, nr_percent = 1;
  1069. const char *color;
  1070. struct annotation *notes = symbol__annotation(sym);
  1071. for (i = 0; i < al->data_nr; i++) {
  1072. double percent;
  1073. percent = annotation_data__percent(&al->data[i],
  1074. PERCENT_HITS_LOCAL);
  1075. if (percent > max_percent)
  1076. max_percent = percent;
  1077. }
  1078. if (al->data_nr > nr_percent)
  1079. nr_percent = al->data_nr;
  1080. if (max_percent < min_pcnt)
  1081. return -1;
  1082. if (max_lines && printed >= max_lines)
  1083. return 1;
  1084. if (queue != NULL) {
  1085. list_for_each_entry_from(queue, &notes->src->source, node) {
  1086. if (queue == al)
  1087. break;
  1088. annotation_line__print(queue, sym, start, evsel, len,
  1089. 0, 0, 1, NULL, addr_fmt_width);
  1090. }
  1091. }
  1092. color = get_percent_color(max_percent);
  1093. /*
  1094. * Also color the filename and line if needed, with
  1095. * the same color than the percentage. Don't print it
  1096. * twice for close colored addr with the same filename:line
  1097. */
  1098. if (al->path) {
  1099. if (!prev_line || strcmp(prev_line, al->path)
  1100. || color != prev_color) {
  1101. color_fprintf(stdout, color, " %s", al->path);
  1102. prev_line = al->path;
  1103. prev_color = color;
  1104. }
  1105. }
  1106. for (i = 0; i < nr_percent; i++) {
  1107. struct annotation_data *data = &al->data[i];
  1108. double percent;
  1109. percent = annotation_data__percent(data, PERCENT_HITS_LOCAL);
  1110. color = get_percent_color(percent);
  1111. if (symbol_conf.show_total_period)
  1112. color_fprintf(stdout, color, " %11" PRIu64,
  1113. data->he.period);
  1114. else if (symbol_conf.show_nr_samples)
  1115. color_fprintf(stdout, color, " %7" PRIu64,
  1116. data->he.nr_samples);
  1117. else
  1118. color_fprintf(stdout, color, " %7.2f", percent);
  1119. }
  1120. printf(" : ");
  1121. disasm_line__print(dl, start, addr_fmt_width);
  1122. printf("\n");
  1123. } else if (max_lines && printed >= max_lines)
  1124. return 1;
  1125. else {
  1126. int width = symbol_conf.show_total_period ? 12 : 8;
  1127. if (queue)
  1128. return -1;
  1129. if (perf_evsel__is_group_event(evsel))
  1130. width *= evsel->nr_members;
  1131. if (!*al->line)
  1132. printf(" %*s:\n", width, " ");
  1133. else
  1134. printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line);
  1135. }
  1136. return 0;
  1137. }
  1138. /*
  1139. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  1140. * which looks like following
  1141. *
  1142. * 0000000000415500 <_init>:
  1143. * 415500: sub $0x8,%rsp
  1144. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  1145. * 41550b: test %rax,%rax
  1146. * 41550e: je 415515 <_init+0x15>
  1147. * 415510: callq 416e70 <__gmon_start__@plt>
  1148. * 415515: add $0x8,%rsp
  1149. * 415519: retq
  1150. *
  1151. * it will be parsed and saved into struct disasm_line as
  1152. * <offset> <name> <ops.raw>
  1153. *
  1154. * The offset will be a relative offset from the start of the symbol and -1
  1155. * means that it's not a disassembly line so should be treated differently.
  1156. * The ops.raw part will be parsed further according to type of the instruction.
  1157. */
  1158. static int symbol__parse_objdump_line(struct symbol *sym, FILE *file,
  1159. struct annotate_args *args,
  1160. int *line_nr)
  1161. {
  1162. struct map *map = args->ms.map;
  1163. struct annotation *notes = symbol__annotation(sym);
  1164. struct disasm_line *dl;
  1165. char *line = NULL, *parsed_line, *tmp, *tmp2;
  1166. size_t line_len;
  1167. s64 line_ip, offset = -1;
  1168. regmatch_t match[2];
  1169. if (getline(&line, &line_len, file) < 0)
  1170. return -1;
  1171. if (!line)
  1172. return -1;
  1173. line_ip = -1;
  1174. parsed_line = rtrim(line);
  1175. /* /filename:linenr ? Save line number and ignore. */
  1176. if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
  1177. *line_nr = atoi(parsed_line + match[1].rm_so);
  1178. return 0;
  1179. }
  1180. tmp = ltrim(parsed_line);
  1181. if (*tmp) {
  1182. /*
  1183. * Parse hexa addresses followed by ':'
  1184. */
  1185. line_ip = strtoull(tmp, &tmp2, 16);
  1186. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  1187. line_ip = -1;
  1188. }
  1189. if (line_ip != -1) {
  1190. u64 start = map__rip_2objdump(map, sym->start),
  1191. end = map__rip_2objdump(map, sym->end);
  1192. offset = line_ip - start;
  1193. if ((u64)line_ip < start || (u64)line_ip >= end)
  1194. offset = -1;
  1195. else
  1196. parsed_line = tmp2 + 1;
  1197. }
  1198. args->offset = offset;
  1199. args->line = parsed_line;
  1200. args->line_nr = *line_nr;
  1201. args->ms.sym = sym;
  1202. dl = disasm_line__new(args);
  1203. free(line);
  1204. (*line_nr)++;
  1205. if (dl == NULL)
  1206. return -1;
  1207. if (!disasm_line__has_local_offset(dl)) {
  1208. dl->ops.target.offset = dl->ops.target.addr -
  1209. map__rip_2objdump(map, sym->start);
  1210. dl->ops.target.offset_avail = true;
  1211. }
  1212. /* kcore has no symbols, so add the call target symbol */
  1213. if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
  1214. struct addr_map_symbol target = {
  1215. .map = map,
  1216. .addr = dl->ops.target.addr,
  1217. };
  1218. if (!map_groups__find_ams(&target) &&
  1219. target.sym->start == target.al_addr)
  1220. dl->ops.target.sym = target.sym;
  1221. }
  1222. annotation_line__add(&dl->al, &notes->src->source);
  1223. return 0;
  1224. }
  1225. static __attribute__((constructor)) void symbol__init_regexpr(void)
  1226. {
  1227. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  1228. }
  1229. static void delete_last_nop(struct symbol *sym)
  1230. {
  1231. struct annotation *notes = symbol__annotation(sym);
  1232. struct list_head *list = &notes->src->source;
  1233. struct disasm_line *dl;
  1234. while (!list_empty(list)) {
  1235. dl = list_entry(list->prev, struct disasm_line, al.node);
  1236. if (dl->ins.ops) {
  1237. if (dl->ins.ops != &nop_ops)
  1238. return;
  1239. } else {
  1240. if (!strstr(dl->al.line, " nop ") &&
  1241. !strstr(dl->al.line, " nopl ") &&
  1242. !strstr(dl->al.line, " nopw "))
  1243. return;
  1244. }
  1245. list_del(&dl->al.node);
  1246. disasm_line__free(dl);
  1247. }
  1248. }
  1249. int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
  1250. int errnum, char *buf, size_t buflen)
  1251. {
  1252. struct dso *dso = map->dso;
  1253. BUG_ON(buflen == 0);
  1254. if (errnum >= 0) {
  1255. str_error_r(errnum, buf, buflen);
  1256. return 0;
  1257. }
  1258. switch (errnum) {
  1259. case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
  1260. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  1261. char *build_id_msg = NULL;
  1262. if (dso->has_build_id) {
  1263. build_id__sprintf(dso->build_id,
  1264. sizeof(dso->build_id), bf + 15);
  1265. build_id_msg = bf;
  1266. }
  1267. scnprintf(buf, buflen,
  1268. "No vmlinux file%s\nwas found in the path.\n\n"
  1269. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  1270. "Please use:\n\n"
  1271. " perf buildid-cache -vu vmlinux\n\n"
  1272. "or:\n\n"
  1273. " --vmlinux vmlinux\n", build_id_msg ?: "");
  1274. }
  1275. break;
  1276. default:
  1277. scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
  1278. break;
  1279. }
  1280. return 0;
  1281. }
  1282. static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
  1283. {
  1284. char linkname[PATH_MAX];
  1285. char *build_id_filename;
  1286. char *build_id_path = NULL;
  1287. char *pos;
  1288. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  1289. !dso__is_kcore(dso))
  1290. return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
  1291. build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
  1292. if (build_id_filename) {
  1293. __symbol__join_symfs(filename, filename_size, build_id_filename);
  1294. free(build_id_filename);
  1295. } else {
  1296. if (dso->has_build_id)
  1297. return ENOMEM;
  1298. goto fallback;
  1299. }
  1300. build_id_path = strdup(filename);
  1301. if (!build_id_path)
  1302. return -1;
  1303. /*
  1304. * old style build-id cache has name of XX/XXXXXXX.. while
  1305. * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
  1306. * extract the build-id part of dirname in the new style only.
  1307. */
  1308. pos = strrchr(build_id_path, '/');
  1309. if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
  1310. dirname(build_id_path);
  1311. if (dso__is_kcore(dso) ||
  1312. readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
  1313. strstr(linkname, DSO__NAME_KALLSYMS) ||
  1314. access(filename, R_OK)) {
  1315. fallback:
  1316. /*
  1317. * If we don't have build-ids or the build-id file isn't in the
  1318. * cache, or is just a kallsyms file, well, lets hope that this
  1319. * DSO is the same as when 'perf record' ran.
  1320. */
  1321. __symbol__join_symfs(filename, filename_size, dso->long_name);
  1322. }
  1323. free(build_id_path);
  1324. return 0;
  1325. }
  1326. static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
  1327. {
  1328. struct annotation_options *opts = args->options;
  1329. struct map *map = args->ms.map;
  1330. struct dso *dso = map->dso;
  1331. char *command;
  1332. FILE *file;
  1333. char symfs_filename[PATH_MAX];
  1334. struct kcore_extract kce;
  1335. bool delete_extract = false;
  1336. int stdout_fd[2];
  1337. int lineno = 0;
  1338. int nline;
  1339. pid_t pid;
  1340. int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
  1341. if (err)
  1342. return err;
  1343. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  1344. symfs_filename, sym->name, map->unmap_ip(map, sym->start),
  1345. map->unmap_ip(map, sym->end));
  1346. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  1347. dso, dso->long_name, sym, sym->name);
  1348. if (dso__is_kcore(dso)) {
  1349. kce.kcore_filename = symfs_filename;
  1350. kce.addr = map__rip_2objdump(map, sym->start);
  1351. kce.offs = sym->start;
  1352. kce.len = sym->end - sym->start;
  1353. if (!kcore_extract__create(&kce)) {
  1354. delete_extract = true;
  1355. strlcpy(symfs_filename, kce.extract_filename,
  1356. sizeof(symfs_filename));
  1357. }
  1358. } else if (dso__needs_decompress(dso)) {
  1359. char tmp[KMOD_DECOMP_LEN];
  1360. if (dso__decompress_kmodule_path(dso, symfs_filename,
  1361. tmp, sizeof(tmp)) < 0)
  1362. goto out;
  1363. strcpy(symfs_filename, tmp);
  1364. }
  1365. err = asprintf(&command,
  1366. "%s %s%s --start-address=0x%016" PRIx64
  1367. " --stop-address=0x%016" PRIx64
  1368. " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
  1369. opts->objdump_path ?: "objdump",
  1370. opts->disassembler_style ? "-M " : "",
  1371. opts->disassembler_style ?: "",
  1372. map__rip_2objdump(map, sym->start),
  1373. map__rip_2objdump(map, sym->end),
  1374. opts->show_asm_raw ? "" : "--no-show-raw",
  1375. opts->annotate_src ? "-S" : "",
  1376. symfs_filename, symfs_filename);
  1377. if (err < 0) {
  1378. pr_err("Failure allocating memory for the command to run\n");
  1379. goto out_remove_tmp;
  1380. }
  1381. pr_debug("Executing: %s\n", command);
  1382. err = -1;
  1383. if (pipe(stdout_fd) < 0) {
  1384. pr_err("Failure creating the pipe to run %s\n", command);
  1385. goto out_free_command;
  1386. }
  1387. pid = fork();
  1388. if (pid < 0) {
  1389. pr_err("Failure forking to run %s\n", command);
  1390. goto out_close_stdout;
  1391. }
  1392. if (pid == 0) {
  1393. close(stdout_fd[0]);
  1394. dup2(stdout_fd[1], 1);
  1395. close(stdout_fd[1]);
  1396. execl("/bin/sh", "sh", "-c", command, NULL);
  1397. perror(command);
  1398. exit(-1);
  1399. }
  1400. close(stdout_fd[1]);
  1401. file = fdopen(stdout_fd[0], "r");
  1402. if (!file) {
  1403. pr_err("Failure creating FILE stream for %s\n", command);
  1404. /*
  1405. * If we were using debug info should retry with
  1406. * original binary.
  1407. */
  1408. goto out_free_command;
  1409. }
  1410. nline = 0;
  1411. while (!feof(file)) {
  1412. /*
  1413. * The source code line number (lineno) needs to be kept in
  1414. * accross calls to symbol__parse_objdump_line(), so that it
  1415. * can associate it with the instructions till the next one.
  1416. * See disasm_line__new() and struct disasm_line::line_nr.
  1417. */
  1418. if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0)
  1419. break;
  1420. nline++;
  1421. }
  1422. if (nline == 0)
  1423. pr_err("No output from %s\n", command);
  1424. /*
  1425. * kallsyms does not have symbol sizes so there may a nop at the end.
  1426. * Remove it.
  1427. */
  1428. if (dso__is_kcore(dso))
  1429. delete_last_nop(sym);
  1430. fclose(file);
  1431. err = 0;
  1432. out_free_command:
  1433. free(command);
  1434. out_remove_tmp:
  1435. close(stdout_fd[0]);
  1436. if (dso__needs_decompress(dso))
  1437. unlink(symfs_filename);
  1438. if (delete_extract)
  1439. kcore_extract__delete(&kce);
  1440. out:
  1441. return err;
  1442. out_close_stdout:
  1443. close(stdout_fd[1]);
  1444. goto out_free_command;
  1445. }
  1446. static void calc_percent(struct sym_hist *sym_hist,
  1447. struct annotation_data *data,
  1448. s64 offset, s64 end)
  1449. {
  1450. unsigned int hits = 0;
  1451. u64 period = 0;
  1452. while (offset < end) {
  1453. hits += sym_hist->addr[offset].nr_samples;
  1454. period += sym_hist->addr[offset].period;
  1455. ++offset;
  1456. }
  1457. if (sym_hist->nr_samples) {
  1458. data->he.period = period;
  1459. data->he.nr_samples = hits;
  1460. data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples;
  1461. }
  1462. }
  1463. static void annotation__calc_percent(struct annotation *notes,
  1464. struct perf_evsel *leader, s64 len)
  1465. {
  1466. struct annotation_line *al, *next;
  1467. struct perf_evsel *evsel;
  1468. list_for_each_entry(al, &notes->src->source, node) {
  1469. s64 end;
  1470. int i = 0;
  1471. if (al->offset == -1)
  1472. continue;
  1473. next = annotation_line__next(al, &notes->src->source);
  1474. end = next ? next->offset : len;
  1475. for_each_group_evsel(evsel, leader) {
  1476. struct annotation_data *data;
  1477. struct sym_hist *sym_hist;
  1478. BUG_ON(i >= al->data_nr);
  1479. sym_hist = annotation__histogram(notes, evsel->idx);
  1480. data = &al->data[i++];
  1481. calc_percent(sym_hist, data, al->offset, end);
  1482. }
  1483. }
  1484. }
  1485. void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
  1486. {
  1487. struct annotation *notes = symbol__annotation(sym);
  1488. annotation__calc_percent(notes, evsel, symbol__size(sym));
  1489. }
  1490. int symbol__annotate(struct symbol *sym, struct map *map,
  1491. struct perf_evsel *evsel, size_t privsize,
  1492. struct annotation_options *options,
  1493. struct arch **parch)
  1494. {
  1495. struct annotate_args args = {
  1496. .privsize = privsize,
  1497. .evsel = evsel,
  1498. .options = options,
  1499. };
  1500. struct perf_env *env = perf_evsel__env(evsel);
  1501. const char *arch_name = perf_env__arch(env);
  1502. struct arch *arch;
  1503. int err;
  1504. if (!arch_name)
  1505. return -1;
  1506. args.arch = arch = arch__find(arch_name);
  1507. if (arch == NULL)
  1508. return -ENOTSUP;
  1509. if (parch)
  1510. *parch = arch;
  1511. if (arch->init) {
  1512. err = arch->init(arch, env ? env->cpuid : NULL);
  1513. if (err) {
  1514. pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
  1515. return err;
  1516. }
  1517. }
  1518. args.ms.map = map;
  1519. args.ms.sym = sym;
  1520. return symbol__disassemble(sym, &args);
  1521. }
  1522. static void insert_source_line(struct rb_root *root, struct annotation_line *al)
  1523. {
  1524. struct annotation_line *iter;
  1525. struct rb_node **p = &root->rb_node;
  1526. struct rb_node *parent = NULL;
  1527. int i, ret;
  1528. while (*p != NULL) {
  1529. parent = *p;
  1530. iter = rb_entry(parent, struct annotation_line, rb_node);
  1531. ret = strcmp(iter->path, al->path);
  1532. if (ret == 0) {
  1533. for (i = 0; i < al->data_nr; i++) {
  1534. iter->data[i].percent_sum += annotation_data__percent(&al->data[i],
  1535. PERCENT_HITS_LOCAL);
  1536. }
  1537. return;
  1538. }
  1539. if (ret < 0)
  1540. p = &(*p)->rb_left;
  1541. else
  1542. p = &(*p)->rb_right;
  1543. }
  1544. for (i = 0; i < al->data_nr; i++) {
  1545. al->data[i].percent_sum = annotation_data__percent(&al->data[i],
  1546. PERCENT_HITS_LOCAL);
  1547. }
  1548. rb_link_node(&al->rb_node, parent, p);
  1549. rb_insert_color(&al->rb_node, root);
  1550. }
  1551. static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
  1552. {
  1553. int i;
  1554. for (i = 0; i < a->data_nr; i++) {
  1555. if (a->data[i].percent_sum == b->data[i].percent_sum)
  1556. continue;
  1557. return a->data[i].percent_sum > b->data[i].percent_sum;
  1558. }
  1559. return 0;
  1560. }
  1561. static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
  1562. {
  1563. struct annotation_line *iter;
  1564. struct rb_node **p = &root->rb_node;
  1565. struct rb_node *parent = NULL;
  1566. while (*p != NULL) {
  1567. parent = *p;
  1568. iter = rb_entry(parent, struct annotation_line, rb_node);
  1569. if (cmp_source_line(al, iter))
  1570. p = &(*p)->rb_left;
  1571. else
  1572. p = &(*p)->rb_right;
  1573. }
  1574. rb_link_node(&al->rb_node, parent, p);
  1575. rb_insert_color(&al->rb_node, root);
  1576. }
  1577. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1578. {
  1579. struct annotation_line *al;
  1580. struct rb_node *node;
  1581. node = rb_first(src_root);
  1582. while (node) {
  1583. struct rb_node *next;
  1584. al = rb_entry(node, struct annotation_line, rb_node);
  1585. next = rb_next(node);
  1586. rb_erase(node, src_root);
  1587. __resort_source_line(dest_root, al);
  1588. node = next;
  1589. }
  1590. }
  1591. static void print_summary(struct rb_root *root, const char *filename)
  1592. {
  1593. struct annotation_line *al;
  1594. struct rb_node *node;
  1595. printf("\nSorted summary for file %s\n", filename);
  1596. printf("----------------------------------------------\n\n");
  1597. if (RB_EMPTY_ROOT(root)) {
  1598. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1599. return;
  1600. }
  1601. node = rb_first(root);
  1602. while (node) {
  1603. double percent, percent_max = 0.0;
  1604. const char *color;
  1605. char *path;
  1606. int i;
  1607. al = rb_entry(node, struct annotation_line, rb_node);
  1608. for (i = 0; i < al->data_nr; i++) {
  1609. percent = al->data[i].percent_sum;
  1610. color = get_percent_color(percent);
  1611. color_fprintf(stdout, color, " %7.2f", percent);
  1612. if (percent > percent_max)
  1613. percent_max = percent;
  1614. }
  1615. path = al->path;
  1616. color = get_percent_color(percent_max);
  1617. color_fprintf(stdout, color, " %s\n", path);
  1618. node = rb_next(node);
  1619. }
  1620. }
  1621. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1622. {
  1623. struct annotation *notes = symbol__annotation(sym);
  1624. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1625. u64 len = symbol__size(sym), offset;
  1626. for (offset = 0; offset < len; ++offset)
  1627. if (h->addr[offset].nr_samples != 0)
  1628. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1629. sym->start + offset, h->addr[offset].nr_samples);
  1630. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
  1631. }
  1632. static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
  1633. {
  1634. char bf[32];
  1635. struct annotation_line *line;
  1636. list_for_each_entry_reverse(line, lines, node) {
  1637. if (line->offset != -1)
  1638. return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
  1639. }
  1640. return 0;
  1641. }
  1642. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1643. struct perf_evsel *evsel,
  1644. struct annotation_options *opts)
  1645. {
  1646. struct dso *dso = map->dso;
  1647. char *filename;
  1648. const char *d_filename;
  1649. const char *evsel_name = perf_evsel__name(evsel);
  1650. struct annotation *notes = symbol__annotation(sym);
  1651. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1652. struct annotation_line *pos, *queue = NULL;
  1653. u64 start = map__rip_2objdump(map, sym->start);
  1654. int printed = 2, queue_len = 0, addr_fmt_width;
  1655. int more = 0;
  1656. bool context = opts->context;
  1657. u64 len;
  1658. int width = symbol_conf.show_total_period ? 12 : 8;
  1659. int graph_dotted_len;
  1660. char buf[512];
  1661. filename = strdup(dso->long_name);
  1662. if (!filename)
  1663. return -ENOMEM;
  1664. if (opts->full_path)
  1665. d_filename = filename;
  1666. else
  1667. d_filename = basename(filename);
  1668. len = symbol__size(sym);
  1669. if (perf_evsel__is_group_event(evsel)) {
  1670. width *= evsel->nr_members;
  1671. perf_evsel__group_desc(evsel, buf, sizeof(buf));
  1672. evsel_name = buf;
  1673. }
  1674. graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
  1675. width, width, symbol_conf.show_total_period ? "Period" :
  1676. symbol_conf.show_nr_samples ? "Samples" : "Percent",
  1677. d_filename, evsel_name, h->nr_samples);
  1678. printf("%-*.*s----\n",
  1679. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1680. if (verbose > 0)
  1681. symbol__annotate_hits(sym, evsel);
  1682. addr_fmt_width = annotated_source__addr_fmt_width(&notes->src->source, start);
  1683. list_for_each_entry(pos, &notes->src->source, node) {
  1684. int err;
  1685. if (context && queue == NULL) {
  1686. queue = pos;
  1687. queue_len = 0;
  1688. }
  1689. err = annotation_line__print(pos, sym, start, evsel, len,
  1690. opts->min_pcnt, printed, opts->max_lines,
  1691. queue, addr_fmt_width);
  1692. switch (err) {
  1693. case 0:
  1694. ++printed;
  1695. if (context) {
  1696. printed += queue_len;
  1697. queue = NULL;
  1698. queue_len = 0;
  1699. }
  1700. break;
  1701. case 1:
  1702. /* filtered by max_lines */
  1703. ++more;
  1704. break;
  1705. case -1:
  1706. default:
  1707. /*
  1708. * Filtered by min_pcnt or non IP lines when
  1709. * context != 0
  1710. */
  1711. if (!context)
  1712. break;
  1713. if (queue_len == context)
  1714. queue = list_entry(queue->node.next, typeof(*queue), node);
  1715. else
  1716. ++queue_len;
  1717. break;
  1718. }
  1719. }
  1720. free(filename);
  1721. return more;
  1722. }
  1723. static void FILE__set_percent_color(void *fp __maybe_unused,
  1724. double percent __maybe_unused,
  1725. bool current __maybe_unused)
  1726. {
  1727. }
  1728. static int FILE__set_jumps_percent_color(void *fp __maybe_unused,
  1729. int nr __maybe_unused, bool current __maybe_unused)
  1730. {
  1731. return 0;
  1732. }
  1733. static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused)
  1734. {
  1735. return 0;
  1736. }
  1737. static void FILE__printf(void *fp, const char *fmt, ...)
  1738. {
  1739. va_list args;
  1740. va_start(args, fmt);
  1741. vfprintf(fp, fmt, args);
  1742. va_end(args);
  1743. }
  1744. static void FILE__write_graph(void *fp, int graph)
  1745. {
  1746. const char *s;
  1747. switch (graph) {
  1748. case DARROW_CHAR: s = "↓"; break;
  1749. case UARROW_CHAR: s = "↑"; break;
  1750. case LARROW_CHAR: s = "←"; break;
  1751. case RARROW_CHAR: s = "→"; break;
  1752. default: s = "?"; break;
  1753. }
  1754. fputs(s, fp);
  1755. }
  1756. static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp)
  1757. {
  1758. struct annotation *notes = symbol__annotation(sym);
  1759. struct annotation_write_ops ops = {
  1760. .first_line = true,
  1761. .obj = fp,
  1762. .set_color = FILE__set_color,
  1763. .set_percent_color = FILE__set_percent_color,
  1764. .set_jumps_percent_color = FILE__set_jumps_percent_color,
  1765. .printf = FILE__printf,
  1766. .write_graph = FILE__write_graph,
  1767. };
  1768. struct annotation_line *al;
  1769. list_for_each_entry(al, &notes->src->source, node) {
  1770. if (annotation_line__filter(al, notes))
  1771. continue;
  1772. annotation_line__write(al, notes, &ops);
  1773. fputc('\n', fp);
  1774. ops.first_line = false;
  1775. }
  1776. return 0;
  1777. }
  1778. int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel)
  1779. {
  1780. const char *ev_name = perf_evsel__name(evsel);
  1781. char buf[1024];
  1782. char *filename;
  1783. int err = -1;
  1784. FILE *fp;
  1785. if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0)
  1786. return -1;
  1787. fp = fopen(filename, "w");
  1788. if (fp == NULL)
  1789. goto out_free_filename;
  1790. if (perf_evsel__is_group_event(evsel)) {
  1791. perf_evsel__group_desc(evsel, buf, sizeof(buf));
  1792. ev_name = buf;
  1793. }
  1794. fprintf(fp, "%s() %s\nEvent: %s\n\n",
  1795. ms->sym->name, ms->map->dso->long_name, ev_name);
  1796. symbol__annotate_fprintf2(ms->sym, fp);
  1797. fclose(fp);
  1798. err = 0;
  1799. out_free_filename:
  1800. free(filename);
  1801. return err;
  1802. }
  1803. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1804. {
  1805. struct annotation *notes = symbol__annotation(sym);
  1806. struct sym_hist *h = annotation__histogram(notes, evidx);
  1807. memset(h, 0, notes->src->sizeof_sym_hist);
  1808. }
  1809. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1810. {
  1811. struct annotation *notes = symbol__annotation(sym);
  1812. struct sym_hist *h = annotation__histogram(notes, evidx);
  1813. int len = symbol__size(sym), offset;
  1814. h->nr_samples = 0;
  1815. for (offset = 0; offset < len; ++offset) {
  1816. h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
  1817. h->nr_samples += h->addr[offset].nr_samples;
  1818. }
  1819. }
  1820. void annotated_source__purge(struct annotated_source *as)
  1821. {
  1822. struct annotation_line *al, *n;
  1823. list_for_each_entry_safe(al, n, &as->source, node) {
  1824. list_del(&al->node);
  1825. disasm_line__free(disasm_line(al));
  1826. }
  1827. }
  1828. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1829. {
  1830. size_t printed;
  1831. if (dl->al.offset == -1)
  1832. return fprintf(fp, "%s\n", dl->al.line);
  1833. printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
  1834. if (dl->ops.raw[0] != '\0') {
  1835. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1836. dl->ops.raw);
  1837. }
  1838. return printed + fprintf(fp, "\n");
  1839. }
  1840. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1841. {
  1842. struct disasm_line *pos;
  1843. size_t printed = 0;
  1844. list_for_each_entry(pos, head, al.node)
  1845. printed += disasm_line__fprintf(pos, fp);
  1846. return printed;
  1847. }
  1848. bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym)
  1849. {
  1850. if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) ||
  1851. !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 ||
  1852. dl->ops.target.offset >= (s64)symbol__size(sym))
  1853. return false;
  1854. return true;
  1855. }
  1856. void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym)
  1857. {
  1858. u64 offset, size = symbol__size(sym);
  1859. /* PLT symbols contain external offsets */
  1860. if (strstr(sym->name, "@plt"))
  1861. return;
  1862. for (offset = 0; offset < size; ++offset) {
  1863. struct annotation_line *al = notes->offsets[offset];
  1864. struct disasm_line *dl;
  1865. dl = disasm_line(al);
  1866. if (!disasm_line__is_valid_local_jump(dl, sym))
  1867. continue;
  1868. al = notes->offsets[dl->ops.target.offset];
  1869. /*
  1870. * FIXME: Oops, no jump target? Buggy disassembler? Or do we
  1871. * have to adjust to the previous offset?
  1872. */
  1873. if (al == NULL)
  1874. continue;
  1875. if (++al->jump_sources > notes->max_jump_sources)
  1876. notes->max_jump_sources = al->jump_sources;
  1877. ++notes->nr_jumps;
  1878. }
  1879. }
  1880. void annotation__set_offsets(struct annotation *notes, s64 size)
  1881. {
  1882. struct annotation_line *al;
  1883. notes->max_line_len = 0;
  1884. list_for_each_entry(al, &notes->src->source, node) {
  1885. size_t line_len = strlen(al->line);
  1886. if (notes->max_line_len < line_len)
  1887. notes->max_line_len = line_len;
  1888. al->idx = notes->nr_entries++;
  1889. if (al->offset != -1) {
  1890. al->idx_asm = notes->nr_asm_entries++;
  1891. /*
  1892. * FIXME: short term bandaid to cope with assembly
  1893. * routines that comes with labels in the same column
  1894. * as the address in objdump, sigh.
  1895. *
  1896. * E.g. copy_user_generic_unrolled
  1897. */
  1898. if (al->offset < size)
  1899. notes->offsets[al->offset] = al;
  1900. } else
  1901. al->idx_asm = -1;
  1902. }
  1903. }
  1904. static inline int width_jumps(int n)
  1905. {
  1906. if (n >= 100)
  1907. return 5;
  1908. if (n / 10)
  1909. return 2;
  1910. return 1;
  1911. }
  1912. void annotation__init_column_widths(struct annotation *notes, struct symbol *sym)
  1913. {
  1914. notes->widths.addr = notes->widths.target =
  1915. notes->widths.min_addr = hex_width(symbol__size(sym));
  1916. notes->widths.max_addr = hex_width(sym->end);
  1917. notes->widths.jumps = width_jumps(notes->max_jump_sources);
  1918. }
  1919. void annotation__update_column_widths(struct annotation *notes)
  1920. {
  1921. if (notes->options->use_offset)
  1922. notes->widths.target = notes->widths.min_addr;
  1923. else
  1924. notes->widths.target = notes->widths.max_addr;
  1925. notes->widths.addr = notes->widths.target;
  1926. if (notes->options->show_nr_jumps)
  1927. notes->widths.addr += notes->widths.jumps + 1;
  1928. }
  1929. static void annotation__calc_lines(struct annotation *notes, struct map *map,
  1930. struct rb_root *root)
  1931. {
  1932. struct annotation_line *al;
  1933. struct rb_root tmp_root = RB_ROOT;
  1934. list_for_each_entry(al, &notes->src->source, node) {
  1935. double percent_max = 0.0;
  1936. int i;
  1937. for (i = 0; i < al->data_nr; i++) {
  1938. double percent;
  1939. percent = annotation_data__percent(&al->data[i],
  1940. PERCENT_HITS_LOCAL);
  1941. if (percent > percent_max)
  1942. percent_max = percent;
  1943. }
  1944. if (percent_max <= 0.5)
  1945. continue;
  1946. al->path = get_srcline(map->dso, notes->start + al->offset, NULL,
  1947. false, true, notes->start + al->offset);
  1948. insert_source_line(&tmp_root, al);
  1949. }
  1950. resort_source_line(root, &tmp_root);
  1951. }
  1952. static void symbol__calc_lines(struct symbol *sym, struct map *map,
  1953. struct rb_root *root)
  1954. {
  1955. struct annotation *notes = symbol__annotation(sym);
  1956. annotation__calc_lines(notes, map, root);
  1957. }
  1958. int symbol__tty_annotate2(struct symbol *sym, struct map *map,
  1959. struct perf_evsel *evsel,
  1960. struct annotation_options *opts)
  1961. {
  1962. struct dso *dso = map->dso;
  1963. struct rb_root source_line = RB_ROOT;
  1964. struct hists *hists = evsel__hists(evsel);
  1965. char buf[1024];
  1966. if (symbol__annotate2(sym, map, evsel, opts, NULL) < 0)
  1967. return -1;
  1968. if (opts->print_lines) {
  1969. srcline_full_filename = opts->full_path;
  1970. symbol__calc_lines(sym, map, &source_line);
  1971. print_summary(&source_line, dso->long_name);
  1972. }
  1973. hists__scnprintf_title(hists, buf, sizeof(buf));
  1974. fprintf(stdout, "%s\n%s() %s\n", buf, sym->name, dso->long_name);
  1975. symbol__annotate_fprintf2(sym, stdout);
  1976. annotated_source__purge(symbol__annotation(sym)->src);
  1977. return 0;
  1978. }
  1979. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1980. struct perf_evsel *evsel,
  1981. struct annotation_options *opts)
  1982. {
  1983. struct dso *dso = map->dso;
  1984. struct rb_root source_line = RB_ROOT;
  1985. if (symbol__annotate(sym, map, evsel, 0, opts, NULL) < 0)
  1986. return -1;
  1987. symbol__calc_percent(sym, evsel);
  1988. if (opts->print_lines) {
  1989. srcline_full_filename = opts->full_path;
  1990. symbol__calc_lines(sym, map, &source_line);
  1991. print_summary(&source_line, dso->long_name);
  1992. }
  1993. symbol__annotate_printf(sym, map, evsel, opts);
  1994. annotated_source__purge(symbol__annotation(sym)->src);
  1995. return 0;
  1996. }
  1997. bool ui__has_annotation(void)
  1998. {
  1999. return use_browser == 1 && perf_hpp_list.sym;
  2000. }
  2001. static double annotation_line__max_percent(struct annotation_line *al,
  2002. struct annotation *notes)
  2003. {
  2004. double percent_max = 0.0;
  2005. int i;
  2006. for (i = 0; i < notes->nr_events; i++) {
  2007. double percent;
  2008. percent = annotation_data__percent(&al->data[i],
  2009. PERCENT_HITS_LOCAL);
  2010. if (percent > percent_max)
  2011. percent_max = percent;
  2012. }
  2013. return percent_max;
  2014. }
  2015. static void disasm_line__write(struct disasm_line *dl, struct annotation *notes,
  2016. void *obj, char *bf, size_t size,
  2017. void (*obj__printf)(void *obj, const char *fmt, ...),
  2018. void (*obj__write_graph)(void *obj, int graph))
  2019. {
  2020. if (dl->ins.ops && dl->ins.ops->scnprintf) {
  2021. if (ins__is_jump(&dl->ins)) {
  2022. bool fwd;
  2023. if (dl->ops.target.outside)
  2024. goto call_like;
  2025. fwd = dl->ops.target.offset > dl->al.offset;
  2026. obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR);
  2027. obj__printf(obj, " ");
  2028. } else if (ins__is_call(&dl->ins)) {
  2029. call_like:
  2030. obj__write_graph(obj, RARROW_CHAR);
  2031. obj__printf(obj, " ");
  2032. } else if (ins__is_ret(&dl->ins)) {
  2033. obj__write_graph(obj, LARROW_CHAR);
  2034. obj__printf(obj, " ");
  2035. } else {
  2036. obj__printf(obj, " ");
  2037. }
  2038. } else {
  2039. obj__printf(obj, " ");
  2040. }
  2041. disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset);
  2042. }
  2043. static void __annotation_line__write(struct annotation_line *al, struct annotation *notes,
  2044. bool first_line, bool current_entry, bool change_color, int width,
  2045. void *obj,
  2046. int (*obj__set_color)(void *obj, int color),
  2047. void (*obj__set_percent_color)(void *obj, double percent, bool current),
  2048. int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current),
  2049. void (*obj__printf)(void *obj, const char *fmt, ...),
  2050. void (*obj__write_graph)(void *obj, int graph))
  2051. {
  2052. double percent_max = annotation_line__max_percent(al, notes);
  2053. int pcnt_width = annotation__pcnt_width(notes),
  2054. cycles_width = annotation__cycles_width(notes);
  2055. bool show_title = false;
  2056. char bf[256];
  2057. int printed;
  2058. if (first_line && (al->offset == -1 || percent_max == 0.0)) {
  2059. if (notes->have_cycles) {
  2060. if (al->ipc == 0.0 && al->cycles == 0)
  2061. show_title = true;
  2062. } else
  2063. show_title = true;
  2064. }
  2065. if (al->offset != -1 && percent_max != 0.0) {
  2066. int i;
  2067. for (i = 0; i < notes->nr_events; i++) {
  2068. double percent;
  2069. percent = annotation_data__percent(&al->data[i],
  2070. PERCENT_HITS_LOCAL);
  2071. obj__set_percent_color(obj, percent, current_entry);
  2072. if (notes->options->show_total_period) {
  2073. obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period);
  2074. } else if (notes->options->show_nr_samples) {
  2075. obj__printf(obj, "%6" PRIu64 " ",
  2076. al->data[i].he.nr_samples);
  2077. } else {
  2078. obj__printf(obj, "%6.2f ", percent);
  2079. }
  2080. }
  2081. } else {
  2082. obj__set_percent_color(obj, 0, current_entry);
  2083. if (!show_title)
  2084. obj__printf(obj, "%-*s", pcnt_width, " ");
  2085. else {
  2086. obj__printf(obj, "%-*s", pcnt_width,
  2087. notes->options->show_total_period ? "Period" :
  2088. notes->options->show_nr_samples ? "Samples" : "Percent");
  2089. }
  2090. }
  2091. if (notes->have_cycles) {
  2092. if (al->ipc)
  2093. obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc);
  2094. else if (!show_title)
  2095. obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " ");
  2096. else
  2097. obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC");
  2098. if (!notes->options->show_minmax_cycle) {
  2099. if (al->cycles)
  2100. obj__printf(obj, "%*" PRIu64 " ",
  2101. ANNOTATION__CYCLES_WIDTH - 1, al->cycles);
  2102. else if (!show_title)
  2103. obj__printf(obj, "%*s",
  2104. ANNOTATION__CYCLES_WIDTH, " ");
  2105. else
  2106. obj__printf(obj, "%*s ",
  2107. ANNOTATION__CYCLES_WIDTH - 1,
  2108. "Cycle");
  2109. } else {
  2110. if (al->cycles) {
  2111. char str[32];
  2112. scnprintf(str, sizeof(str),
  2113. "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")",
  2114. al->cycles, al->cycles_min,
  2115. al->cycles_max);
  2116. obj__printf(obj, "%*s ",
  2117. ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
  2118. str);
  2119. } else if (!show_title)
  2120. obj__printf(obj, "%*s",
  2121. ANNOTATION__MINMAX_CYCLES_WIDTH,
  2122. " ");
  2123. else
  2124. obj__printf(obj, "%*s ",
  2125. ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
  2126. "Cycle(min/max)");
  2127. }
  2128. }
  2129. obj__printf(obj, " ");
  2130. if (!*al->line)
  2131. obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " ");
  2132. else if (al->offset == -1) {
  2133. if (al->line_nr && notes->options->show_linenr)
  2134. printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr);
  2135. else
  2136. printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " ");
  2137. obj__printf(obj, bf);
  2138. obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line);
  2139. } else {
  2140. u64 addr = al->offset;
  2141. int color = -1;
  2142. if (!notes->options->use_offset)
  2143. addr += notes->start;
  2144. if (!notes->options->use_offset) {
  2145. printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
  2146. } else {
  2147. if (al->jump_sources &&
  2148. notes->options->offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) {
  2149. if (notes->options->show_nr_jumps) {
  2150. int prev;
  2151. printed = scnprintf(bf, sizeof(bf), "%*d ",
  2152. notes->widths.jumps,
  2153. al->jump_sources);
  2154. prev = obj__set_jumps_percent_color(obj, al->jump_sources,
  2155. current_entry);
  2156. obj__printf(obj, bf);
  2157. obj__set_color(obj, prev);
  2158. }
  2159. print_addr:
  2160. printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
  2161. notes->widths.target, addr);
  2162. } else if (ins__is_call(&disasm_line(al)->ins) &&
  2163. notes->options->offset_level >= ANNOTATION__OFFSET_CALL) {
  2164. goto print_addr;
  2165. } else if (notes->options->offset_level == ANNOTATION__MAX_OFFSET_LEVEL) {
  2166. goto print_addr;
  2167. } else {
  2168. printed = scnprintf(bf, sizeof(bf), "%-*s ",
  2169. notes->widths.addr, " ");
  2170. }
  2171. }
  2172. if (change_color)
  2173. color = obj__set_color(obj, HE_COLORSET_ADDR);
  2174. obj__printf(obj, bf);
  2175. if (change_color)
  2176. obj__set_color(obj, color);
  2177. disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph);
  2178. obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf);
  2179. }
  2180. }
  2181. void annotation_line__write(struct annotation_line *al, struct annotation *notes,
  2182. struct annotation_write_ops *ops)
  2183. {
  2184. __annotation_line__write(al, notes, ops->first_line, ops->current_entry,
  2185. ops->change_color, ops->width, ops->obj,
  2186. ops->set_color, ops->set_percent_color,
  2187. ops->set_jumps_percent_color, ops->printf,
  2188. ops->write_graph);
  2189. }
  2190. int symbol__annotate2(struct symbol *sym, struct map *map, struct perf_evsel *evsel,
  2191. struct annotation_options *options, struct arch **parch)
  2192. {
  2193. struct annotation *notes = symbol__annotation(sym);
  2194. size_t size = symbol__size(sym);
  2195. int nr_pcnt = 1, err;
  2196. notes->offsets = zalloc(size * sizeof(struct annotation_line *));
  2197. if (notes->offsets == NULL)
  2198. return -1;
  2199. if (perf_evsel__is_group_event(evsel))
  2200. nr_pcnt = evsel->nr_members;
  2201. err = symbol__annotate(sym, map, evsel, 0, options, parch);
  2202. if (err)
  2203. goto out_free_offsets;
  2204. notes->options = options;
  2205. symbol__calc_percent(sym, evsel);
  2206. notes->start = map__rip_2objdump(map, sym->start);
  2207. annotation__set_offsets(notes, size);
  2208. annotation__mark_jump_targets(notes, sym);
  2209. annotation__compute_ipc(notes, size);
  2210. annotation__init_column_widths(notes, sym);
  2211. notes->nr_events = nr_pcnt;
  2212. annotation__update_column_widths(notes);
  2213. return 0;
  2214. out_free_offsets:
  2215. zfree(&notes->offsets);
  2216. return -1;
  2217. }
  2218. #define ANNOTATION__CFG(n) \
  2219. { .name = #n, .value = &annotation__default_options.n, }
  2220. /*
  2221. * Keep the entries sorted, they are bsearch'ed
  2222. */
  2223. static struct annotation_config {
  2224. const char *name;
  2225. void *value;
  2226. } annotation__configs[] = {
  2227. ANNOTATION__CFG(hide_src_code),
  2228. ANNOTATION__CFG(jump_arrows),
  2229. ANNOTATION__CFG(offset_level),
  2230. ANNOTATION__CFG(show_linenr),
  2231. ANNOTATION__CFG(show_nr_jumps),
  2232. ANNOTATION__CFG(show_nr_samples),
  2233. ANNOTATION__CFG(show_total_period),
  2234. ANNOTATION__CFG(use_offset),
  2235. };
  2236. #undef ANNOTATION__CFG
  2237. static int annotation_config__cmp(const void *name, const void *cfgp)
  2238. {
  2239. const struct annotation_config *cfg = cfgp;
  2240. return strcmp(name, cfg->name);
  2241. }
  2242. static int annotation__config(const char *var, const char *value,
  2243. void *data __maybe_unused)
  2244. {
  2245. struct annotation_config *cfg;
  2246. const char *name;
  2247. if (!strstarts(var, "annotate."))
  2248. return 0;
  2249. name = var + 9;
  2250. cfg = bsearch(name, annotation__configs, ARRAY_SIZE(annotation__configs),
  2251. sizeof(struct annotation_config), annotation_config__cmp);
  2252. if (cfg == NULL)
  2253. pr_debug("%s variable unknown, ignoring...", var);
  2254. else if (strcmp(var, "annotate.offset_level") == 0) {
  2255. perf_config_int(cfg->value, name, value);
  2256. if (*(int *)cfg->value > ANNOTATION__MAX_OFFSET_LEVEL)
  2257. *(int *)cfg->value = ANNOTATION__MAX_OFFSET_LEVEL;
  2258. else if (*(int *)cfg->value < ANNOTATION__MIN_OFFSET_LEVEL)
  2259. *(int *)cfg->value = ANNOTATION__MIN_OFFSET_LEVEL;
  2260. } else {
  2261. *(bool *)cfg->value = perf_config_bool(name, value);
  2262. }
  2263. return 0;
  2264. }
  2265. void annotation_config__init(void)
  2266. {
  2267. perf_config(annotation__config, NULL);
  2268. annotation__default_options.show_total_period = symbol_conf.show_total_period;
  2269. annotation__default_options.show_nr_samples = symbol_conf.show_nr_samples;
  2270. }