annotate.c 62 KB

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