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