annotate.c 48 KB

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