annotate.c 44 KB

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