annotate.c 45 KB

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