annotate.c 47 KB

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