annotate.c 39 KB

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