annotate.c 57 KB

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