annotate.c 45 KB

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