annotate.c 42 KB

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