annotate.c 46 KB

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