annotate.c 52 KB

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