annotate.c 43 KB

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