annotate.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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 __maybe_unused)
  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. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  580. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  581. addr, addr - sym->start, evidx, h->addr[offset].nr_samples);
  582. return 0;
  583. }
  584. static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
  585. {
  586. struct annotation *notes = symbol__annotation(sym);
  587. if (notes->src == NULL) {
  588. if (symbol__alloc_hist(sym) < 0)
  589. return NULL;
  590. }
  591. if (!notes->src->cycles_hist && cycles) {
  592. if (symbol__alloc_hist_cycles(sym) < 0)
  593. return NULL;
  594. }
  595. return notes;
  596. }
  597. static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  598. int evidx, u64 addr,
  599. struct perf_sample *sample)
  600. {
  601. struct annotation *notes;
  602. if (sym == NULL)
  603. return 0;
  604. notes = symbol__get_annotation(sym, false);
  605. if (notes == NULL)
  606. return -ENOMEM;
  607. return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample);
  608. }
  609. static int symbol__account_cycles(u64 addr, u64 start,
  610. struct symbol *sym, unsigned cycles)
  611. {
  612. struct annotation *notes;
  613. unsigned offset;
  614. if (sym == NULL)
  615. return 0;
  616. notes = symbol__get_annotation(sym, true);
  617. if (notes == NULL)
  618. return -ENOMEM;
  619. if (addr < sym->start || addr >= sym->end)
  620. return -ERANGE;
  621. if (start) {
  622. if (start < sym->start || start >= sym->end)
  623. return -ERANGE;
  624. if (start >= addr)
  625. start = 0;
  626. }
  627. offset = addr - sym->start;
  628. return __symbol__account_cycles(notes,
  629. start ? start - sym->start : 0,
  630. offset, cycles,
  631. !!start);
  632. }
  633. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  634. struct addr_map_symbol *start,
  635. unsigned cycles)
  636. {
  637. u64 saddr = 0;
  638. int err;
  639. if (!cycles)
  640. return 0;
  641. /*
  642. * Only set start when IPC can be computed. We can only
  643. * compute it when the basic block is completely in a single
  644. * function.
  645. * Special case the case when the jump is elsewhere, but
  646. * it starts on the function start.
  647. */
  648. if (start &&
  649. (start->sym == ams->sym ||
  650. (ams->sym &&
  651. start->addr == ams->sym->start + ams->map->start)))
  652. saddr = start->al_addr;
  653. if (saddr == 0)
  654. pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
  655. ams->addr,
  656. start ? start->addr : 0,
  657. ams->sym ? ams->sym->start + ams->map->start : 0,
  658. saddr);
  659. err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
  660. if (err)
  661. pr_debug2("account_cycles failed %d\n", err);
  662. return err;
  663. }
  664. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  665. int evidx)
  666. {
  667. return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample);
  668. }
  669. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  670. int evidx, u64 ip)
  671. {
  672. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample);
  673. }
  674. static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
  675. {
  676. dl->ins.ops = ins__find(arch, dl->ins.name);
  677. if (!dl->ins.ops)
  678. return;
  679. if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
  680. dl->ins.ops = NULL;
  681. }
  682. static int disasm_line__parse(char *line, const char **namep, char **rawp)
  683. {
  684. char tmp, *name = ltrim(line);
  685. if (name[0] == '\0')
  686. return -1;
  687. *rawp = name + 1;
  688. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  689. ++*rawp;
  690. tmp = (*rawp)[0];
  691. (*rawp)[0] = '\0';
  692. *namep = strdup(name);
  693. if (*namep == NULL)
  694. goto out_free_name;
  695. (*rawp)[0] = tmp;
  696. *rawp = ltrim(*rawp);
  697. return 0;
  698. out_free_name:
  699. free((void *)namep);
  700. *namep = NULL;
  701. return -1;
  702. }
  703. static struct disasm_line *disasm_line__new(s64 offset, char *line,
  704. size_t privsize, int line_nr,
  705. struct arch *arch,
  706. struct map *map)
  707. {
  708. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  709. if (dl != NULL) {
  710. dl->offset = offset;
  711. dl->line = strdup(line);
  712. dl->line_nr = line_nr;
  713. if (dl->line == NULL)
  714. goto out_delete;
  715. if (offset != -1) {
  716. if (disasm_line__parse(dl->line, &dl->ins.name, &dl->ops.raw) < 0)
  717. goto out_free_line;
  718. disasm_line__init_ins(dl, arch, map);
  719. }
  720. }
  721. return dl;
  722. out_free_line:
  723. zfree(&dl->line);
  724. out_delete:
  725. free(dl);
  726. return NULL;
  727. }
  728. void disasm_line__free(struct disasm_line *dl)
  729. {
  730. zfree(&dl->line);
  731. if (dl->ins.ops && dl->ins.ops->free)
  732. dl->ins.ops->free(&dl->ops);
  733. else
  734. ins__delete(&dl->ops);
  735. free((void *)dl->ins.name);
  736. dl->ins.name = NULL;
  737. free(dl);
  738. }
  739. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  740. {
  741. if (raw || !dl->ins.ops)
  742. return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
  743. return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
  744. }
  745. static void disasm__add(struct list_head *head, struct disasm_line *line)
  746. {
  747. list_add_tail(&line->node, head);
  748. }
  749. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  750. {
  751. list_for_each_entry_continue(pos, head, node)
  752. if (pos->offset >= 0)
  753. return pos;
  754. return NULL;
  755. }
  756. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  757. s64 end, const char **path, struct sym_hist_entry *sample)
  758. {
  759. struct source_line *src_line = notes->src->lines;
  760. double percent = 0.0;
  761. sample->nr_samples = 0;
  762. if (src_line) {
  763. size_t sizeof_src_line = sizeof(*src_line) +
  764. sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
  765. while (offset < end) {
  766. src_line = (void *)notes->src->lines +
  767. (sizeof_src_line * offset);
  768. if (*path == NULL)
  769. *path = src_line->path;
  770. percent += src_line->samples[evidx].percent;
  771. sample->nr_samples += src_line->samples[evidx].nr;
  772. offset++;
  773. }
  774. } else {
  775. struct sym_hist *h = annotation__histogram(notes, evidx);
  776. unsigned int hits = 0;
  777. while (offset < end)
  778. hits += h->addr[offset++].nr_samples;
  779. if (h->nr_samples) {
  780. sample->nr_samples = hits;
  781. percent = 100.0 * hits / h->nr_samples;
  782. }
  783. }
  784. return percent;
  785. }
  786. static const char *annotate__address_color(struct block_range *br)
  787. {
  788. double cov = block_range__coverage(br);
  789. if (cov >= 0) {
  790. /* mark red for >75% coverage */
  791. if (cov > 0.75)
  792. return PERF_COLOR_RED;
  793. /* mark dull for <1% coverage */
  794. if (cov < 0.01)
  795. return PERF_COLOR_NORMAL;
  796. }
  797. return PERF_COLOR_MAGENTA;
  798. }
  799. static const char *annotate__asm_color(struct block_range *br)
  800. {
  801. double cov = block_range__coverage(br);
  802. if (cov >= 0) {
  803. /* mark dull for <1% coverage */
  804. if (cov < 0.01)
  805. return PERF_COLOR_NORMAL;
  806. }
  807. return PERF_COLOR_BLUE;
  808. }
  809. static void annotate__branch_printf(struct block_range *br, u64 addr)
  810. {
  811. bool emit_comment = true;
  812. if (!br)
  813. return;
  814. #if 1
  815. if (br->is_target && br->start == addr) {
  816. struct block_range *branch = br;
  817. double p;
  818. /*
  819. * Find matching branch to our target.
  820. */
  821. while (!branch->is_branch)
  822. branch = block_range__next(branch);
  823. p = 100 *(double)br->entry / branch->coverage;
  824. if (p > 0.1) {
  825. if (emit_comment) {
  826. emit_comment = false;
  827. printf("\t#");
  828. }
  829. /*
  830. * The percentage of coverage joined at this target in relation
  831. * to the next branch.
  832. */
  833. printf(" +%.2f%%", p);
  834. }
  835. }
  836. #endif
  837. if (br->is_branch && br->end == addr) {
  838. double p = 100*(double)br->taken / br->coverage;
  839. if (p > 0.1) {
  840. if (emit_comment) {
  841. emit_comment = false;
  842. printf("\t#");
  843. }
  844. /*
  845. * The percentage of coverage leaving at this branch, and
  846. * its prediction ratio.
  847. */
  848. printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
  849. }
  850. }
  851. }
  852. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  853. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  854. int max_lines, struct disasm_line *queue)
  855. {
  856. static const char *prev_line;
  857. static const char *prev_color;
  858. if (dl->offset != -1) {
  859. const char *path = NULL;
  860. double percent, max_percent = 0.0;
  861. double *ppercents = &percent;
  862. struct sym_hist_entry sample;
  863. struct sym_hist_entry *psamples = &sample;
  864. int i, nr_percent = 1;
  865. const char *color;
  866. struct annotation *notes = symbol__annotation(sym);
  867. s64 offset = dl->offset;
  868. const u64 addr = start + offset;
  869. struct disasm_line *next;
  870. struct block_range *br;
  871. next = disasm__get_next_ip_line(&notes->src->source, dl);
  872. if (perf_evsel__is_group_event(evsel)) {
  873. nr_percent = evsel->nr_members;
  874. ppercents = calloc(nr_percent, sizeof(double));
  875. psamples = calloc(nr_percent, sizeof(struct sym_hist_entry));
  876. if (ppercents == NULL || psamples == NULL) {
  877. return -1;
  878. }
  879. }
  880. for (i = 0; i < nr_percent; i++) {
  881. percent = disasm__calc_percent(notes,
  882. notes->src->lines ? i : evsel->idx + i,
  883. offset,
  884. next ? next->offset : (s64) len,
  885. &path, &sample);
  886. ppercents[i] = percent;
  887. psamples[i] = sample;
  888. if (percent > max_percent)
  889. max_percent = percent;
  890. }
  891. if (max_percent < min_pcnt)
  892. return -1;
  893. if (max_lines && printed >= max_lines)
  894. return 1;
  895. if (queue != NULL) {
  896. list_for_each_entry_from(queue, &notes->src->source, node) {
  897. if (queue == dl)
  898. break;
  899. disasm_line__print(queue, sym, start, evsel, len,
  900. 0, 0, 1, NULL);
  901. }
  902. }
  903. color = get_percent_color(max_percent);
  904. /*
  905. * Also color the filename and line if needed, with
  906. * the same color than the percentage. Don't print it
  907. * twice for close colored addr with the same filename:line
  908. */
  909. if (path) {
  910. if (!prev_line || strcmp(prev_line, path)
  911. || color != prev_color) {
  912. color_fprintf(stdout, color, " %s", path);
  913. prev_line = path;
  914. prev_color = color;
  915. }
  916. }
  917. for (i = 0; i < nr_percent; i++) {
  918. percent = ppercents[i];
  919. sample = psamples[i];
  920. color = get_percent_color(percent);
  921. if (symbol_conf.show_total_period)
  922. color_fprintf(stdout, color, " %7" PRIu64,
  923. sample.nr_samples);
  924. else
  925. color_fprintf(stdout, color, " %7.2f", percent);
  926. }
  927. printf(" : ");
  928. br = block_range__find(addr);
  929. color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
  930. color_fprintf(stdout, annotate__asm_color(br), "%s", dl->line);
  931. annotate__branch_printf(br, addr);
  932. printf("\n");
  933. if (ppercents != &percent)
  934. free(ppercents);
  935. if (psamples != &sample)
  936. free(psamples);
  937. } else if (max_lines && printed >= max_lines)
  938. return 1;
  939. else {
  940. int width = 8;
  941. if (queue)
  942. return -1;
  943. if (perf_evsel__is_group_event(evsel))
  944. width *= evsel->nr_members;
  945. if (!*dl->line)
  946. printf(" %*s:\n", width, " ");
  947. else
  948. printf(" %*s: %s\n", width, " ", dl->line);
  949. }
  950. return 0;
  951. }
  952. /*
  953. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  954. * which looks like following
  955. *
  956. * 0000000000415500 <_init>:
  957. * 415500: sub $0x8,%rsp
  958. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  959. * 41550b: test %rax,%rax
  960. * 41550e: je 415515 <_init+0x15>
  961. * 415510: callq 416e70 <__gmon_start__@plt>
  962. * 415515: add $0x8,%rsp
  963. * 415519: retq
  964. *
  965. * it will be parsed and saved into struct disasm_line as
  966. * <offset> <name> <ops.raw>
  967. *
  968. * The offset will be a relative offset from the start of the symbol and -1
  969. * means that it's not a disassembly line so should be treated differently.
  970. * The ops.raw part will be parsed further according to type of the instruction.
  971. */
  972. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  973. struct arch *arch,
  974. FILE *file, size_t privsize,
  975. int *line_nr)
  976. {
  977. struct annotation *notes = symbol__annotation(sym);
  978. struct disasm_line *dl;
  979. char *line = NULL, *parsed_line, *tmp, *tmp2;
  980. size_t line_len;
  981. s64 line_ip, offset = -1;
  982. regmatch_t match[2];
  983. if (getline(&line, &line_len, file) < 0)
  984. return -1;
  985. if (!line)
  986. return -1;
  987. line_ip = -1;
  988. parsed_line = rtrim(line);
  989. /* /filename:linenr ? Save line number and ignore. */
  990. if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
  991. *line_nr = atoi(parsed_line + match[1].rm_so);
  992. return 0;
  993. }
  994. tmp = ltrim(parsed_line);
  995. if (*tmp) {
  996. /*
  997. * Parse hexa addresses followed by ':'
  998. */
  999. line_ip = strtoull(tmp, &tmp2, 16);
  1000. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  1001. line_ip = -1;
  1002. }
  1003. if (line_ip != -1) {
  1004. u64 start = map__rip_2objdump(map, sym->start),
  1005. end = map__rip_2objdump(map, sym->end);
  1006. offset = line_ip - start;
  1007. if ((u64)line_ip < start || (u64)line_ip >= end)
  1008. offset = -1;
  1009. else
  1010. parsed_line = tmp2 + 1;
  1011. }
  1012. dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
  1013. free(line);
  1014. (*line_nr)++;
  1015. if (dl == NULL)
  1016. return -1;
  1017. if (!disasm_line__has_offset(dl)) {
  1018. dl->ops.target.offset = dl->ops.target.addr -
  1019. map__rip_2objdump(map, sym->start);
  1020. dl->ops.target.offset_avail = true;
  1021. }
  1022. /* kcore has no symbols, so add the call target name */
  1023. if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
  1024. struct addr_map_symbol target = {
  1025. .map = map,
  1026. .addr = dl->ops.target.addr,
  1027. };
  1028. if (!map_groups__find_ams(&target) &&
  1029. target.sym->start == target.al_addr)
  1030. dl->ops.target.name = strdup(target.sym->name);
  1031. }
  1032. disasm__add(&notes->src->source, dl);
  1033. return 0;
  1034. }
  1035. static __attribute__((constructor)) void symbol__init_regexpr(void)
  1036. {
  1037. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  1038. }
  1039. static void delete_last_nop(struct symbol *sym)
  1040. {
  1041. struct annotation *notes = symbol__annotation(sym);
  1042. struct list_head *list = &notes->src->source;
  1043. struct disasm_line *dl;
  1044. while (!list_empty(list)) {
  1045. dl = list_entry(list->prev, struct disasm_line, node);
  1046. if (dl->ins.ops) {
  1047. if (dl->ins.ops != &nop_ops)
  1048. return;
  1049. } else {
  1050. if (!strstr(dl->line, " nop ") &&
  1051. !strstr(dl->line, " nopl ") &&
  1052. !strstr(dl->line, " nopw "))
  1053. return;
  1054. }
  1055. list_del(&dl->node);
  1056. disasm_line__free(dl);
  1057. }
  1058. }
  1059. int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
  1060. int errnum, char *buf, size_t buflen)
  1061. {
  1062. struct dso *dso = map->dso;
  1063. BUG_ON(buflen == 0);
  1064. if (errnum >= 0) {
  1065. str_error_r(errnum, buf, buflen);
  1066. return 0;
  1067. }
  1068. switch (errnum) {
  1069. case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
  1070. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  1071. char *build_id_msg = NULL;
  1072. if (dso->has_build_id) {
  1073. build_id__sprintf(dso->build_id,
  1074. sizeof(dso->build_id), bf + 15);
  1075. build_id_msg = bf;
  1076. }
  1077. scnprintf(buf, buflen,
  1078. "No vmlinux file%s\nwas found in the path.\n\n"
  1079. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  1080. "Please use:\n\n"
  1081. " perf buildid-cache -vu vmlinux\n\n"
  1082. "or:\n\n"
  1083. " --vmlinux vmlinux\n", build_id_msg ?: "");
  1084. }
  1085. break;
  1086. default:
  1087. scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
  1088. break;
  1089. }
  1090. return 0;
  1091. }
  1092. static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
  1093. {
  1094. char linkname[PATH_MAX];
  1095. char *build_id_filename;
  1096. char *build_id_path = NULL;
  1097. char *pos;
  1098. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  1099. !dso__is_kcore(dso))
  1100. return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
  1101. build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
  1102. if (build_id_filename) {
  1103. __symbol__join_symfs(filename, filename_size, build_id_filename);
  1104. free(build_id_filename);
  1105. } else {
  1106. if (dso->has_build_id)
  1107. return ENOMEM;
  1108. goto fallback;
  1109. }
  1110. build_id_path = strdup(filename);
  1111. if (!build_id_path)
  1112. return -1;
  1113. /*
  1114. * old style build-id cache has name of XX/XXXXXXX.. while
  1115. * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
  1116. * extract the build-id part of dirname in the new style only.
  1117. */
  1118. pos = strrchr(build_id_path, '/');
  1119. if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
  1120. dirname(build_id_path);
  1121. if (dso__is_kcore(dso) ||
  1122. readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
  1123. strstr(linkname, DSO__NAME_KALLSYMS) ||
  1124. access(filename, R_OK)) {
  1125. fallback:
  1126. /*
  1127. * If we don't have build-ids or the build-id file isn't in the
  1128. * cache, or is just a kallsyms file, well, lets hope that this
  1129. * DSO is the same as when 'perf record' ran.
  1130. */
  1131. __symbol__join_symfs(filename, filename_size, dso->long_name);
  1132. }
  1133. free(build_id_path);
  1134. return 0;
  1135. }
  1136. static const char *annotate__norm_arch(const char *arch_name)
  1137. {
  1138. struct utsname uts;
  1139. if (!arch_name) { /* Assume we are annotating locally. */
  1140. if (uname(&uts) < 0)
  1141. return NULL;
  1142. arch_name = uts.machine;
  1143. }
  1144. return normalize_arch((char *)arch_name);
  1145. }
  1146. int symbol__disassemble(struct symbol *sym, struct map *map,
  1147. const char *arch_name, size_t privsize,
  1148. struct arch **parch, char *cpuid)
  1149. {
  1150. struct dso *dso = map->dso;
  1151. char command[PATH_MAX * 2];
  1152. struct arch *arch = NULL;
  1153. FILE *file;
  1154. char symfs_filename[PATH_MAX];
  1155. struct kcore_extract kce;
  1156. bool delete_extract = false;
  1157. int stdout_fd[2];
  1158. int lineno = 0;
  1159. int nline;
  1160. pid_t pid;
  1161. int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
  1162. if (err)
  1163. return err;
  1164. arch_name = annotate__norm_arch(arch_name);
  1165. if (!arch_name)
  1166. return -1;
  1167. arch = arch__find(arch_name);
  1168. if (arch == NULL)
  1169. return -ENOTSUP;
  1170. if (parch)
  1171. *parch = arch;
  1172. if (arch->init) {
  1173. err = arch->init(arch);
  1174. if (err) {
  1175. pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
  1176. return err;
  1177. }
  1178. }
  1179. if (arch->cpuid_parse && cpuid)
  1180. arch->cpuid_parse(arch, cpuid);
  1181. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  1182. symfs_filename, sym->name, map->unmap_ip(map, sym->start),
  1183. map->unmap_ip(map, sym->end));
  1184. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  1185. dso, dso->long_name, sym, sym->name);
  1186. if (dso__is_kcore(dso)) {
  1187. kce.kcore_filename = symfs_filename;
  1188. kce.addr = map__rip_2objdump(map, sym->start);
  1189. kce.offs = sym->start;
  1190. kce.len = sym->end - sym->start;
  1191. if (!kcore_extract__create(&kce)) {
  1192. delete_extract = true;
  1193. strlcpy(symfs_filename, kce.extract_filename,
  1194. sizeof(symfs_filename));
  1195. }
  1196. } else if (dso__needs_decompress(dso)) {
  1197. char tmp[KMOD_DECOMP_LEN];
  1198. if (dso__decompress_kmodule_path(dso, symfs_filename,
  1199. tmp, sizeof(tmp)) < 0)
  1200. goto out;
  1201. strcpy(symfs_filename, tmp);
  1202. }
  1203. snprintf(command, sizeof(command),
  1204. "%s %s%s --start-address=0x%016" PRIx64
  1205. " --stop-address=0x%016" PRIx64
  1206. " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
  1207. objdump_path ? objdump_path : "objdump",
  1208. disassembler_style ? "-M " : "",
  1209. disassembler_style ? disassembler_style : "",
  1210. map__rip_2objdump(map, sym->start),
  1211. map__rip_2objdump(map, sym->end),
  1212. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  1213. symbol_conf.annotate_src ? "-S" : "",
  1214. symfs_filename, symfs_filename);
  1215. pr_debug("Executing: %s\n", command);
  1216. err = -1;
  1217. if (pipe(stdout_fd) < 0) {
  1218. pr_err("Failure creating the pipe to run %s\n", command);
  1219. goto out_remove_tmp;
  1220. }
  1221. pid = fork();
  1222. if (pid < 0) {
  1223. pr_err("Failure forking to run %s\n", command);
  1224. goto out_close_stdout;
  1225. }
  1226. if (pid == 0) {
  1227. close(stdout_fd[0]);
  1228. dup2(stdout_fd[1], 1);
  1229. close(stdout_fd[1]);
  1230. execl("/bin/sh", "sh", "-c", command, NULL);
  1231. perror(command);
  1232. exit(-1);
  1233. }
  1234. close(stdout_fd[1]);
  1235. file = fdopen(stdout_fd[0], "r");
  1236. if (!file) {
  1237. pr_err("Failure creating FILE stream for %s\n", command);
  1238. /*
  1239. * If we were using debug info should retry with
  1240. * original binary.
  1241. */
  1242. goto out_remove_tmp;
  1243. }
  1244. nline = 0;
  1245. while (!feof(file)) {
  1246. /*
  1247. * The source code line number (lineno) needs to be kept in
  1248. * accross calls to symbol__parse_objdump_line(), so that it
  1249. * can associate it with the instructions till the next one.
  1250. * See disasm_line__new() and struct disasm_line::line_nr.
  1251. */
  1252. if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
  1253. &lineno) < 0)
  1254. break;
  1255. nline++;
  1256. }
  1257. if (nline == 0)
  1258. pr_err("No output from %s\n", command);
  1259. /*
  1260. * kallsyms does not have symbol sizes so there may a nop at the end.
  1261. * Remove it.
  1262. */
  1263. if (dso__is_kcore(dso))
  1264. delete_last_nop(sym);
  1265. fclose(file);
  1266. err = 0;
  1267. out_remove_tmp:
  1268. close(stdout_fd[0]);
  1269. if (dso__needs_decompress(dso))
  1270. unlink(symfs_filename);
  1271. if (delete_extract)
  1272. kcore_extract__delete(&kce);
  1273. out:
  1274. return err;
  1275. out_close_stdout:
  1276. close(stdout_fd[1]);
  1277. goto out_remove_tmp;
  1278. }
  1279. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  1280. {
  1281. struct source_line *iter;
  1282. struct rb_node **p = &root->rb_node;
  1283. struct rb_node *parent = NULL;
  1284. int i, ret;
  1285. while (*p != NULL) {
  1286. parent = *p;
  1287. iter = rb_entry(parent, struct source_line, node);
  1288. ret = strcmp(iter->path, src_line->path);
  1289. if (ret == 0) {
  1290. for (i = 0; i < src_line->nr_pcnt; i++)
  1291. iter->samples[i].percent_sum += src_line->samples[i].percent;
  1292. return;
  1293. }
  1294. if (ret < 0)
  1295. p = &(*p)->rb_left;
  1296. else
  1297. p = &(*p)->rb_right;
  1298. }
  1299. for (i = 0; i < src_line->nr_pcnt; i++)
  1300. src_line->samples[i].percent_sum = src_line->samples[i].percent;
  1301. rb_link_node(&src_line->node, parent, p);
  1302. rb_insert_color(&src_line->node, root);
  1303. }
  1304. static int cmp_source_line(struct source_line *a, struct source_line *b)
  1305. {
  1306. int i;
  1307. for (i = 0; i < a->nr_pcnt; i++) {
  1308. if (a->samples[i].percent_sum == b->samples[i].percent_sum)
  1309. continue;
  1310. return a->samples[i].percent_sum > b->samples[i].percent_sum;
  1311. }
  1312. return 0;
  1313. }
  1314. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  1315. {
  1316. struct source_line *iter;
  1317. struct rb_node **p = &root->rb_node;
  1318. struct rb_node *parent = NULL;
  1319. while (*p != NULL) {
  1320. parent = *p;
  1321. iter = rb_entry(parent, struct source_line, node);
  1322. if (cmp_source_line(src_line, iter))
  1323. p = &(*p)->rb_left;
  1324. else
  1325. p = &(*p)->rb_right;
  1326. }
  1327. rb_link_node(&src_line->node, parent, p);
  1328. rb_insert_color(&src_line->node, root);
  1329. }
  1330. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1331. {
  1332. struct source_line *src_line;
  1333. struct rb_node *node;
  1334. node = rb_first(src_root);
  1335. while (node) {
  1336. struct rb_node *next;
  1337. src_line = rb_entry(node, struct source_line, node);
  1338. next = rb_next(node);
  1339. rb_erase(node, src_root);
  1340. __resort_source_line(dest_root, src_line);
  1341. node = next;
  1342. }
  1343. }
  1344. static void symbol__free_source_line(struct symbol *sym, int len)
  1345. {
  1346. struct annotation *notes = symbol__annotation(sym);
  1347. struct source_line *src_line = notes->src->lines;
  1348. size_t sizeof_src_line;
  1349. int i;
  1350. sizeof_src_line = sizeof(*src_line) +
  1351. (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
  1352. for (i = 0; i < len; i++) {
  1353. free_srcline(src_line->path);
  1354. src_line = (void *)src_line + sizeof_src_line;
  1355. }
  1356. zfree(&notes->src->lines);
  1357. }
  1358. /* Get the filename:line for the colored entries */
  1359. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  1360. struct perf_evsel *evsel,
  1361. struct rb_root *root, int len)
  1362. {
  1363. u64 start;
  1364. int i, k;
  1365. int evidx = evsel->idx;
  1366. struct source_line *src_line;
  1367. struct annotation *notes = symbol__annotation(sym);
  1368. struct sym_hist *h = annotation__histogram(notes, evidx);
  1369. struct rb_root tmp_root = RB_ROOT;
  1370. int nr_pcnt = 1;
  1371. u64 nr_samples = h->nr_samples;
  1372. size_t sizeof_src_line = sizeof(struct source_line);
  1373. if (perf_evsel__is_group_event(evsel)) {
  1374. for (i = 1; i < evsel->nr_members; i++) {
  1375. h = annotation__histogram(notes, evidx + i);
  1376. nr_samples += h->nr_samples;
  1377. }
  1378. nr_pcnt = evsel->nr_members;
  1379. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
  1380. }
  1381. if (!nr_samples)
  1382. return 0;
  1383. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  1384. if (!notes->src->lines)
  1385. return -1;
  1386. start = map__rip_2objdump(map, sym->start);
  1387. for (i = 0; i < len; i++) {
  1388. u64 offset;
  1389. double percent_max = 0.0;
  1390. src_line->nr_pcnt = nr_pcnt;
  1391. for (k = 0; k < nr_pcnt; k++) {
  1392. double percent = 0.0;
  1393. h = annotation__histogram(notes, evidx + k);
  1394. nr_samples = h->addr[i].nr_samples;
  1395. if (h->nr_samples)
  1396. percent = 100.0 * nr_samples / h->nr_samples;
  1397. if (percent > percent_max)
  1398. percent_max = percent;
  1399. src_line->samples[k].percent = percent;
  1400. src_line->samples[k].nr = nr_samples;
  1401. }
  1402. if (percent_max <= 0.5)
  1403. goto next;
  1404. offset = start + i;
  1405. src_line->path = get_srcline(map->dso, offset, NULL,
  1406. false, true);
  1407. insert_source_line(&tmp_root, src_line);
  1408. next:
  1409. src_line = (void *)src_line + sizeof_src_line;
  1410. }
  1411. resort_source_line(root, &tmp_root);
  1412. return 0;
  1413. }
  1414. static void print_summary(struct rb_root *root, const char *filename)
  1415. {
  1416. struct source_line *src_line;
  1417. struct rb_node *node;
  1418. printf("\nSorted summary for file %s\n", filename);
  1419. printf("----------------------------------------------\n\n");
  1420. if (RB_EMPTY_ROOT(root)) {
  1421. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1422. return;
  1423. }
  1424. node = rb_first(root);
  1425. while (node) {
  1426. double percent, percent_max = 0.0;
  1427. const char *color;
  1428. char *path;
  1429. int i;
  1430. src_line = rb_entry(node, struct source_line, node);
  1431. for (i = 0; i < src_line->nr_pcnt; i++) {
  1432. percent = src_line->samples[i].percent_sum;
  1433. color = get_percent_color(percent);
  1434. color_fprintf(stdout, color, " %7.2f", percent);
  1435. if (percent > percent_max)
  1436. percent_max = percent;
  1437. }
  1438. path = src_line->path;
  1439. color = get_percent_color(percent_max);
  1440. color_fprintf(stdout, color, " %s\n", path);
  1441. node = rb_next(node);
  1442. }
  1443. }
  1444. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1445. {
  1446. struct annotation *notes = symbol__annotation(sym);
  1447. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1448. u64 len = symbol__size(sym), offset;
  1449. for (offset = 0; offset < len; ++offset)
  1450. if (h->addr[offset].nr_samples != 0)
  1451. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1452. sym->start + offset, h->addr[offset].nr_samples);
  1453. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
  1454. }
  1455. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1456. struct perf_evsel *evsel, bool full_paths,
  1457. int min_pcnt, int max_lines, int context)
  1458. {
  1459. struct dso *dso = map->dso;
  1460. char *filename;
  1461. const char *d_filename;
  1462. const char *evsel_name = perf_evsel__name(evsel);
  1463. struct annotation *notes = symbol__annotation(sym);
  1464. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1465. struct disasm_line *pos, *queue = NULL;
  1466. u64 start = map__rip_2objdump(map, sym->start);
  1467. int printed = 2, queue_len = 0;
  1468. int more = 0;
  1469. u64 len;
  1470. int width = 8;
  1471. int graph_dotted_len;
  1472. filename = strdup(dso->long_name);
  1473. if (!filename)
  1474. return -ENOMEM;
  1475. if (full_paths)
  1476. d_filename = filename;
  1477. else
  1478. d_filename = basename(filename);
  1479. len = symbol__size(sym);
  1480. if (perf_evsel__is_group_event(evsel))
  1481. width *= evsel->nr_members;
  1482. graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
  1483. width, width, "Percent", d_filename, evsel_name, h->nr_samples);
  1484. printf("%-*.*s----\n",
  1485. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1486. if (verbose > 0)
  1487. symbol__annotate_hits(sym, evsel);
  1488. list_for_each_entry(pos, &notes->src->source, node) {
  1489. if (context && queue == NULL) {
  1490. queue = pos;
  1491. queue_len = 0;
  1492. }
  1493. switch (disasm_line__print(pos, sym, start, evsel, len,
  1494. min_pcnt, printed, max_lines,
  1495. queue)) {
  1496. case 0:
  1497. ++printed;
  1498. if (context) {
  1499. printed += queue_len;
  1500. queue = NULL;
  1501. queue_len = 0;
  1502. }
  1503. break;
  1504. case 1:
  1505. /* filtered by max_lines */
  1506. ++more;
  1507. break;
  1508. case -1:
  1509. default:
  1510. /*
  1511. * Filtered by min_pcnt or non IP lines when
  1512. * context != 0
  1513. */
  1514. if (!context)
  1515. break;
  1516. if (queue_len == context)
  1517. queue = list_entry(queue->node.next, typeof(*queue), node);
  1518. else
  1519. ++queue_len;
  1520. break;
  1521. }
  1522. }
  1523. free(filename);
  1524. return more;
  1525. }
  1526. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1527. {
  1528. struct annotation *notes = symbol__annotation(sym);
  1529. struct sym_hist *h = annotation__histogram(notes, evidx);
  1530. memset(h, 0, notes->src->sizeof_sym_hist);
  1531. }
  1532. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1533. {
  1534. struct annotation *notes = symbol__annotation(sym);
  1535. struct sym_hist *h = annotation__histogram(notes, evidx);
  1536. int len = symbol__size(sym), offset;
  1537. h->nr_samples = 0;
  1538. for (offset = 0; offset < len; ++offset) {
  1539. h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
  1540. h->nr_samples += h->addr[offset].nr_samples;
  1541. }
  1542. }
  1543. void disasm__purge(struct list_head *head)
  1544. {
  1545. struct disasm_line *pos, *n;
  1546. list_for_each_entry_safe(pos, n, head, node) {
  1547. list_del(&pos->node);
  1548. disasm_line__free(pos);
  1549. }
  1550. }
  1551. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1552. {
  1553. size_t printed;
  1554. if (dl->offset == -1)
  1555. return fprintf(fp, "%s\n", dl->line);
  1556. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->ins.name);
  1557. if (dl->ops.raw[0] != '\0') {
  1558. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1559. dl->ops.raw);
  1560. }
  1561. return printed + fprintf(fp, "\n");
  1562. }
  1563. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1564. {
  1565. struct disasm_line *pos;
  1566. size_t printed = 0;
  1567. list_for_each_entry(pos, head, node)
  1568. printed += disasm_line__fprintf(pos, fp);
  1569. return printed;
  1570. }
  1571. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1572. struct perf_evsel *evsel, bool print_lines,
  1573. bool full_paths, int min_pcnt, int max_lines)
  1574. {
  1575. struct dso *dso = map->dso;
  1576. struct rb_root source_line = RB_ROOT;
  1577. u64 len;
  1578. if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel),
  1579. 0, NULL, NULL) < 0)
  1580. return -1;
  1581. len = symbol__size(sym);
  1582. if (print_lines) {
  1583. srcline_full_filename = full_paths;
  1584. symbol__get_source_line(sym, map, evsel, &source_line, len);
  1585. print_summary(&source_line, dso->long_name);
  1586. }
  1587. symbol__annotate_printf(sym, map, evsel, full_paths,
  1588. min_pcnt, max_lines, 0);
  1589. if (print_lines)
  1590. symbol__free_source_line(sym, len);
  1591. disasm__purge(&symbol__annotation(sym)->src->source);
  1592. return 0;
  1593. }
  1594. bool ui__has_annotation(void)
  1595. {
  1596. return use_browser == 1 && perf_hpp_list.sym;
  1597. }