annotate.c 69 KB

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