annotate.c 47 KB

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