annotate.c 47 KB

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