annotate.c 41 KB

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