annotate.c 43 KB

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