annotate.c 40 KB

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