annotate.c 42 KB

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