annotate.c 41 KB

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