annotate.c 41 KB

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