annotate.c 69 KB

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