annotate.c 43 KB

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