annotate.c 39 KB

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