annotate.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  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 "config.h"
  17. #include "cache.h"
  18. #include "symbol.h"
  19. #include "debug.h"
  20. #include "annotate.h"
  21. #include "evsel.h"
  22. #include "block-range.h"
  23. #include "string2.h"
  24. #include "arch/common.h"
  25. #include <regex.h>
  26. #include <pthread.h>
  27. #include <linux/bitops.h>
  28. #include <linux/kernel.h>
  29. /* FIXME: For the HE_COLORSET */
  30. #include "ui/browser.h"
  31. /*
  32. * FIXME: Using the same values as slang.h,
  33. * but that header may not be available everywhere
  34. */
  35. #define LARROW_CHAR ((unsigned char)',')
  36. #define RARROW_CHAR ((unsigned char)'+')
  37. #define DARROW_CHAR ((unsigned char)'.')
  38. #define UARROW_CHAR ((unsigned char)'-')
  39. #include "sane_ctype.h"
  40. struct annotation_options annotation__default_options = {
  41. .use_offset = true,
  42. .jump_arrows = true,
  43. };
  44. const char *disassembler_style;
  45. const char *objdump_path;
  46. static regex_t file_lineno;
  47. static struct ins_ops *ins__find(struct arch *arch, const char *name);
  48. static void ins__sort(struct arch *arch);
  49. static int disasm_line__parse(char *line, const char **namep, char **rawp);
  50. struct arch {
  51. const char *name;
  52. struct ins *instructions;
  53. size_t nr_instructions;
  54. size_t nr_instructions_allocated;
  55. struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
  56. bool sorted_instructions;
  57. bool initialized;
  58. void *priv;
  59. unsigned int model;
  60. unsigned int family;
  61. int (*init)(struct arch *arch, char *cpuid);
  62. bool (*ins_is_fused)(struct arch *arch, const char *ins1,
  63. const char *ins2);
  64. struct {
  65. char comment_char;
  66. char skip_functions_char;
  67. } objdump;
  68. };
  69. static struct ins_ops call_ops;
  70. static struct ins_ops dec_ops;
  71. static struct ins_ops jump_ops;
  72. static struct ins_ops mov_ops;
  73. static struct ins_ops nop_ops;
  74. static struct ins_ops lock_ops;
  75. static struct ins_ops ret_ops;
  76. static int arch__grow_instructions(struct arch *arch)
  77. {
  78. struct ins *new_instructions;
  79. size_t new_nr_allocated;
  80. if (arch->nr_instructions_allocated == 0 && arch->instructions)
  81. goto grow_from_non_allocated_table;
  82. new_nr_allocated = arch->nr_instructions_allocated + 128;
  83. new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
  84. if (new_instructions == NULL)
  85. return -1;
  86. out_update_instructions:
  87. arch->instructions = new_instructions;
  88. arch->nr_instructions_allocated = new_nr_allocated;
  89. return 0;
  90. grow_from_non_allocated_table:
  91. new_nr_allocated = arch->nr_instructions + 128;
  92. new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
  93. if (new_instructions == NULL)
  94. return -1;
  95. memcpy(new_instructions, arch->instructions, arch->nr_instructions);
  96. goto out_update_instructions;
  97. }
  98. static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
  99. {
  100. struct ins *ins;
  101. if (arch->nr_instructions == arch->nr_instructions_allocated &&
  102. arch__grow_instructions(arch))
  103. return -1;
  104. ins = &arch->instructions[arch->nr_instructions];
  105. ins->name = strdup(name);
  106. if (!ins->name)
  107. return -1;
  108. ins->ops = ops;
  109. arch->nr_instructions++;
  110. ins__sort(arch);
  111. return 0;
  112. }
  113. #include "arch/arm/annotate/instructions.c"
  114. #include "arch/arm64/annotate/instructions.c"
  115. #include "arch/x86/annotate/instructions.c"
  116. #include "arch/powerpc/annotate/instructions.c"
  117. #include "arch/s390/annotate/instructions.c"
  118. static struct arch architectures[] = {
  119. {
  120. .name = "arm",
  121. .init = arm__annotate_init,
  122. },
  123. {
  124. .name = "arm64",
  125. .init = arm64__annotate_init,
  126. },
  127. {
  128. .name = "x86",
  129. .init = x86__annotate_init,
  130. .instructions = x86__instructions,
  131. .nr_instructions = ARRAY_SIZE(x86__instructions),
  132. .ins_is_fused = x86__ins_is_fused,
  133. .objdump = {
  134. .comment_char = '#',
  135. },
  136. },
  137. {
  138. .name = "powerpc",
  139. .init = powerpc__annotate_init,
  140. },
  141. {
  142. .name = "s390",
  143. .init = s390__annotate_init,
  144. .objdump = {
  145. .comment_char = '#',
  146. },
  147. },
  148. };
  149. static void ins__delete(struct ins_operands *ops)
  150. {
  151. if (ops == NULL)
  152. return;
  153. zfree(&ops->source.raw);
  154. zfree(&ops->source.name);
  155. zfree(&ops->target.raw);
  156. zfree(&ops->target.name);
  157. }
  158. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  159. struct ins_operands *ops)
  160. {
  161. return scnprintf(bf, size, "%-6s %s", ins->name, ops->raw);
  162. }
  163. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  164. struct ins_operands *ops)
  165. {
  166. if (ins->ops->scnprintf)
  167. return ins->ops->scnprintf(ins, bf, size, ops);
  168. return ins__raw_scnprintf(ins, bf, size, ops);
  169. }
  170. bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
  171. {
  172. if (!arch || !arch->ins_is_fused)
  173. return false;
  174. return arch->ins_is_fused(arch, ins1, ins2);
  175. }
  176. static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
  177. {
  178. char *endptr, *tok, *name;
  179. struct map *map = ms->map;
  180. struct addr_map_symbol target = {
  181. .map = map,
  182. };
  183. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  184. name = strchr(endptr, '<');
  185. if (name == NULL)
  186. goto indirect_call;
  187. name++;
  188. if (arch->objdump.skip_functions_char &&
  189. strchr(name, arch->objdump.skip_functions_char))
  190. return -1;
  191. tok = strchr(name, '>');
  192. if (tok == NULL)
  193. return -1;
  194. *tok = '\0';
  195. ops->target.name = strdup(name);
  196. *tok = '>';
  197. if (ops->target.name == NULL)
  198. return -1;
  199. find_target:
  200. target.addr = map__objdump_2mem(map, ops->target.addr);
  201. if (map_groups__find_ams(&target) == 0 &&
  202. map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
  203. ops->target.sym = target.sym;
  204. return 0;
  205. indirect_call:
  206. tok = strchr(endptr, '*');
  207. if (tok != NULL)
  208. ops->target.addr = strtoull(tok + 1, NULL, 16);
  209. goto find_target;
  210. }
  211. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  212. struct ins_operands *ops)
  213. {
  214. if (ops->target.sym)
  215. return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name);
  216. if (ops->target.addr == 0)
  217. return ins__raw_scnprintf(ins, bf, size, ops);
  218. if (ops->target.name)
  219. return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.name);
  220. return scnprintf(bf, size, "%-6s *%" PRIx64, ins->name, ops->target.addr);
  221. }
  222. static struct ins_ops call_ops = {
  223. .parse = call__parse,
  224. .scnprintf = call__scnprintf,
  225. };
  226. bool ins__is_call(const struct ins *ins)
  227. {
  228. return ins->ops == &call_ops || ins->ops == &s390_call_ops;
  229. }
  230. static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms)
  231. {
  232. struct map *map = ms->map;
  233. struct symbol *sym = ms->sym;
  234. struct addr_map_symbol target = {
  235. .map = map,
  236. };
  237. const char *s = strchr(ops->raw, '+');
  238. const char *c = strchr(ops->raw, ',');
  239. u64 start, end;
  240. /*
  241. * Examples of lines to parse for the _cpp_lex_token@@Base
  242. * function:
  243. *
  244. * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
  245. * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
  246. *
  247. * The first is a jump to an offset inside the same function,
  248. * the second is to another function, i.e. that 0xa72 is an
  249. * offset in the cpp_named_operator2name@@base function.
  250. */
  251. /*
  252. * skip over possible up to 2 operands to get to address, e.g.:
  253. * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
  254. */
  255. if (c++ != NULL) {
  256. ops->target.addr = strtoull(c, NULL, 16);
  257. if (!ops->target.addr) {
  258. c = strchr(c, ',');
  259. if (c++ != NULL)
  260. ops->target.addr = strtoull(c, NULL, 16);
  261. }
  262. } else {
  263. ops->target.addr = strtoull(ops->raw, NULL, 16);
  264. }
  265. target.addr = map__objdump_2mem(map, ops->target.addr);
  266. start = map->unmap_ip(map, sym->start),
  267. end = map->unmap_ip(map, sym->end);
  268. ops->target.outside = target.addr < start || target.addr > end;
  269. /*
  270. * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
  271. cpp_named_operator2name@@Base+0xa72
  272. * Point to a place that is after the cpp_named_operator2name
  273. * boundaries, i.e. in the ELF symbol table for cc1
  274. * cpp_named_operator2name is marked as being 32-bytes long, but it in
  275. * fact is much larger than that, so we seem to need a symbols__find()
  276. * routine that looks for >= current->start and < next_symbol->start,
  277. * possibly just for C++ objects?
  278. *
  279. * For now lets just make some progress by marking jumps to outside the
  280. * current function as call like.
  281. *
  282. * Actual navigation will come next, with further understanding of how
  283. * the symbol searching and disassembly should be done.
  284. if (map_groups__find_ams(&target) == 0 &&
  285. map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
  286. ops->target.sym = target.sym;
  287. */
  288. if (s++ != NULL) {
  289. ops->target.offset = strtoull(s, NULL, 16);
  290. ops->target.offset_avail = true;
  291. } else {
  292. ops->target.offset_avail = false;
  293. }
  294. return 0;
  295. }
  296. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  297. struct ins_operands *ops)
  298. {
  299. const char *c = strchr(ops->raw, ',');
  300. if (!ops->target.addr || ops->target.offset < 0)
  301. return ins__raw_scnprintf(ins, bf, size, ops);
  302. if (c != NULL) {
  303. const char *c2 = strchr(c + 1, ',');
  304. /* check for 3-op insn */
  305. if (c2 != NULL)
  306. c = c2;
  307. c++;
  308. /* mirror arch objdump's space-after-comma style */
  309. if (*c == ' ')
  310. c++;
  311. }
  312. return scnprintf(bf, size, "%-6s %.*s%" PRIx64,
  313. ins->name, c ? c - ops->raw : 0, ops->raw,
  314. ops->target.offset);
  315. }
  316. static struct ins_ops jump_ops = {
  317. .parse = jump__parse,
  318. .scnprintf = jump__scnprintf,
  319. };
  320. bool ins__is_jump(const struct ins *ins)
  321. {
  322. return ins->ops == &jump_ops;
  323. }
  324. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  325. {
  326. char *endptr, *name, *t;
  327. if (strstr(raw, "(%rip)") == NULL)
  328. return 0;
  329. *addrp = strtoull(comment, &endptr, 16);
  330. if (endptr == comment)
  331. return 0;
  332. name = strchr(endptr, '<');
  333. if (name == NULL)
  334. return -1;
  335. name++;
  336. t = strchr(name, '>');
  337. if (t == NULL)
  338. return 0;
  339. *t = '\0';
  340. *namep = strdup(name);
  341. *t = '>';
  342. return 0;
  343. }
  344. static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
  345. {
  346. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  347. if (ops->locked.ops == NULL)
  348. return 0;
  349. if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
  350. goto out_free_ops;
  351. ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
  352. if (ops->locked.ins.ops == NULL)
  353. goto out_free_ops;
  354. if (ops->locked.ins.ops->parse &&
  355. ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
  356. goto out_free_ops;
  357. return 0;
  358. out_free_ops:
  359. zfree(&ops->locked.ops);
  360. return 0;
  361. }
  362. static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
  363. struct ins_operands *ops)
  364. {
  365. int printed;
  366. if (ops->locked.ins.ops == NULL)
  367. return ins__raw_scnprintf(ins, bf, size, ops);
  368. printed = scnprintf(bf, size, "%-6s ", ins->name);
  369. return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
  370. size - printed, ops->locked.ops);
  371. }
  372. static void lock__delete(struct ins_operands *ops)
  373. {
  374. struct ins *ins = &ops->locked.ins;
  375. if (ins->ops && ins->ops->free)
  376. ins->ops->free(ops->locked.ops);
  377. else
  378. ins__delete(ops->locked.ops);
  379. zfree(&ops->locked.ops);
  380. zfree(&ops->target.raw);
  381. zfree(&ops->target.name);
  382. }
  383. static struct ins_ops lock_ops = {
  384. .free = lock__delete,
  385. .parse = lock__parse,
  386. .scnprintf = lock__scnprintf,
  387. };
  388. static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
  389. {
  390. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  391. if (s == NULL)
  392. return -1;
  393. *s = '\0';
  394. ops->source.raw = strdup(ops->raw);
  395. *s = ',';
  396. if (ops->source.raw == NULL)
  397. return -1;
  398. target = ++s;
  399. comment = strchr(s, arch->objdump.comment_char);
  400. if (comment != NULL)
  401. s = comment - 1;
  402. else
  403. s = strchr(s, '\0') - 1;
  404. while (s > target && isspace(s[0]))
  405. --s;
  406. s++;
  407. prev = *s;
  408. *s = '\0';
  409. ops->target.raw = strdup(target);
  410. *s = prev;
  411. if (ops->target.raw == NULL)
  412. goto out_free_source;
  413. if (comment == NULL)
  414. return 0;
  415. comment = ltrim(comment);
  416. comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
  417. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  418. return 0;
  419. out_free_source:
  420. zfree(&ops->source.raw);
  421. return -1;
  422. }
  423. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  424. struct ins_operands *ops)
  425. {
  426. return scnprintf(bf, size, "%-6s %s,%s", ins->name,
  427. ops->source.name ?: ops->source.raw,
  428. ops->target.name ?: ops->target.raw);
  429. }
  430. static struct ins_ops mov_ops = {
  431. .parse = mov__parse,
  432. .scnprintf = mov__scnprintf,
  433. };
  434. static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
  435. {
  436. char *target, *comment, *s, prev;
  437. target = s = ops->raw;
  438. while (s[0] != '\0' && !isspace(s[0]))
  439. ++s;
  440. prev = *s;
  441. *s = '\0';
  442. ops->target.raw = strdup(target);
  443. *s = prev;
  444. if (ops->target.raw == NULL)
  445. return -1;
  446. comment = strchr(s, arch->objdump.comment_char);
  447. if (comment == NULL)
  448. return 0;
  449. comment = ltrim(comment);
  450. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  451. return 0;
  452. }
  453. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  454. struct ins_operands *ops)
  455. {
  456. return scnprintf(bf, size, "%-6s %s", ins->name,
  457. ops->target.name ?: ops->target.raw);
  458. }
  459. static struct ins_ops dec_ops = {
  460. .parse = dec__parse,
  461. .scnprintf = dec__scnprintf,
  462. };
  463. static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
  464. struct ins_operands *ops __maybe_unused)
  465. {
  466. return scnprintf(bf, size, "%-6s", "nop");
  467. }
  468. static struct ins_ops nop_ops = {
  469. .scnprintf = nop__scnprintf,
  470. };
  471. static struct ins_ops ret_ops = {
  472. .scnprintf = ins__raw_scnprintf,
  473. };
  474. bool ins__is_ret(const struct ins *ins)
  475. {
  476. return ins->ops == &ret_ops;
  477. }
  478. bool ins__is_lock(const struct ins *ins)
  479. {
  480. return ins->ops == &lock_ops;
  481. }
  482. static int ins__key_cmp(const void *name, const void *insp)
  483. {
  484. const struct ins *ins = insp;
  485. return strcmp(name, ins->name);
  486. }
  487. static int ins__cmp(const void *a, const void *b)
  488. {
  489. const struct ins *ia = a;
  490. const struct ins *ib = b;
  491. return strcmp(ia->name, ib->name);
  492. }
  493. static void ins__sort(struct arch *arch)
  494. {
  495. const int nmemb = arch->nr_instructions;
  496. qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
  497. }
  498. static struct ins_ops *__ins__find(struct arch *arch, const char *name)
  499. {
  500. struct ins *ins;
  501. const int nmemb = arch->nr_instructions;
  502. if (!arch->sorted_instructions) {
  503. ins__sort(arch);
  504. arch->sorted_instructions = true;
  505. }
  506. ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
  507. return ins ? ins->ops : NULL;
  508. }
  509. static struct ins_ops *ins__find(struct arch *arch, const char *name)
  510. {
  511. struct ins_ops *ops = __ins__find(arch, name);
  512. if (!ops && arch->associate_instruction_ops)
  513. ops = arch->associate_instruction_ops(arch, name);
  514. return ops;
  515. }
  516. static int arch__key_cmp(const void *name, const void *archp)
  517. {
  518. const struct arch *arch = archp;
  519. return strcmp(name, arch->name);
  520. }
  521. static int arch__cmp(const void *a, const void *b)
  522. {
  523. const struct arch *aa = a;
  524. const struct arch *ab = b;
  525. return strcmp(aa->name, ab->name);
  526. }
  527. static void arch__sort(void)
  528. {
  529. const int nmemb = ARRAY_SIZE(architectures);
  530. qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
  531. }
  532. static struct arch *arch__find(const char *name)
  533. {
  534. const int nmemb = ARRAY_SIZE(architectures);
  535. static bool sorted;
  536. if (!sorted) {
  537. arch__sort();
  538. sorted = true;
  539. }
  540. return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
  541. }
  542. int symbol__alloc_hist(struct symbol *sym)
  543. {
  544. struct annotation *notes = symbol__annotation(sym);
  545. size_t size = symbol__size(sym);
  546. size_t sizeof_sym_hist;
  547. /*
  548. * Add buffer of one element for zero length symbol.
  549. * When sample is taken from first instruction of
  550. * zero length symbol, perf still resolves it and
  551. * shows symbol name in perf report and allows to
  552. * annotate it.
  553. */
  554. if (size == 0)
  555. size = 1;
  556. /* Check for overflow when calculating sizeof_sym_hist */
  557. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
  558. return -1;
  559. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
  560. /* Check for overflow in zalloc argument */
  561. if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
  562. / symbol_conf.nr_events)
  563. return -1;
  564. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  565. if (notes->src == NULL)
  566. return -1;
  567. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  568. notes->src->nr_histograms = symbol_conf.nr_events;
  569. INIT_LIST_HEAD(&notes->src->source);
  570. return 0;
  571. }
  572. /* The cycles histogram is lazily allocated. */
  573. static int symbol__alloc_hist_cycles(struct symbol *sym)
  574. {
  575. struct annotation *notes = symbol__annotation(sym);
  576. const size_t size = symbol__size(sym);
  577. notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
  578. if (notes->src->cycles_hist == NULL)
  579. return -1;
  580. return 0;
  581. }
  582. void symbol__annotate_zero_histograms(struct symbol *sym)
  583. {
  584. struct annotation *notes = symbol__annotation(sym);
  585. pthread_mutex_lock(&notes->lock);
  586. if (notes->src != NULL) {
  587. memset(notes->src->histograms, 0,
  588. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  589. if (notes->src->cycles_hist)
  590. memset(notes->src->cycles_hist, 0,
  591. symbol__size(sym) * sizeof(struct cyc_hist));
  592. }
  593. pthread_mutex_unlock(&notes->lock);
  594. }
  595. static int __symbol__account_cycles(struct annotation *notes,
  596. u64 start,
  597. unsigned offset, unsigned cycles,
  598. unsigned have_start)
  599. {
  600. struct cyc_hist *ch;
  601. ch = notes->src->cycles_hist;
  602. /*
  603. * For now we can only account one basic block per
  604. * final jump. But multiple could be overlapping.
  605. * Always account the longest one. So when
  606. * a shorter one has been already seen throw it away.
  607. *
  608. * We separately always account the full cycles.
  609. */
  610. ch[offset].num_aggr++;
  611. ch[offset].cycles_aggr += cycles;
  612. if (!have_start && ch[offset].have_start)
  613. return 0;
  614. if (ch[offset].num) {
  615. if (have_start && (!ch[offset].have_start ||
  616. ch[offset].start > start)) {
  617. ch[offset].have_start = 0;
  618. ch[offset].cycles = 0;
  619. ch[offset].num = 0;
  620. if (ch[offset].reset < 0xffff)
  621. ch[offset].reset++;
  622. } else if (have_start &&
  623. ch[offset].start < start)
  624. return 0;
  625. }
  626. ch[offset].have_start = have_start;
  627. ch[offset].start = start;
  628. ch[offset].cycles += cycles;
  629. ch[offset].num++;
  630. return 0;
  631. }
  632. static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  633. struct annotation *notes, int evidx, u64 addr,
  634. struct perf_sample *sample)
  635. {
  636. unsigned offset;
  637. struct sym_hist *h;
  638. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  639. if ((addr < sym->start || addr >= sym->end) &&
  640. (addr != sym->end || sym->start != sym->end)) {
  641. pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
  642. __func__, __LINE__, sym->name, sym->start, addr, sym->end);
  643. return -ERANGE;
  644. }
  645. offset = addr - sym->start;
  646. h = annotation__histogram(notes, evidx);
  647. h->nr_samples++;
  648. h->addr[offset].nr_samples++;
  649. h->period += sample->period;
  650. h->addr[offset].period += sample->period;
  651. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  652. ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
  653. sym->start, sym->name, addr, addr - sym->start, evidx,
  654. h->addr[offset].nr_samples, h->addr[offset].period);
  655. return 0;
  656. }
  657. static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
  658. {
  659. struct annotation *notes = symbol__annotation(sym);
  660. if (notes->src == NULL) {
  661. if (symbol__alloc_hist(sym) < 0)
  662. return NULL;
  663. }
  664. if (!notes->src->cycles_hist && cycles) {
  665. if (symbol__alloc_hist_cycles(sym) < 0)
  666. return NULL;
  667. }
  668. return notes;
  669. }
  670. static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  671. int evidx, u64 addr,
  672. struct perf_sample *sample)
  673. {
  674. struct annotation *notes;
  675. if (sym == NULL)
  676. return 0;
  677. notes = symbol__get_annotation(sym, false);
  678. if (notes == NULL)
  679. return -ENOMEM;
  680. return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample);
  681. }
  682. static int symbol__account_cycles(u64 addr, u64 start,
  683. struct symbol *sym, unsigned cycles)
  684. {
  685. struct annotation *notes;
  686. unsigned offset;
  687. if (sym == NULL)
  688. return 0;
  689. notes = symbol__get_annotation(sym, true);
  690. if (notes == NULL)
  691. return -ENOMEM;
  692. if (addr < sym->start || addr >= sym->end)
  693. return -ERANGE;
  694. if (start) {
  695. if (start < sym->start || start >= sym->end)
  696. return -ERANGE;
  697. if (start >= addr)
  698. start = 0;
  699. }
  700. offset = addr - sym->start;
  701. return __symbol__account_cycles(notes,
  702. start ? start - sym->start : 0,
  703. offset, cycles,
  704. !!start);
  705. }
  706. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  707. struct addr_map_symbol *start,
  708. unsigned cycles)
  709. {
  710. u64 saddr = 0;
  711. int err;
  712. if (!cycles)
  713. return 0;
  714. /*
  715. * Only set start when IPC can be computed. We can only
  716. * compute it when the basic block is completely in a single
  717. * function.
  718. * Special case the case when the jump is elsewhere, but
  719. * it starts on the function start.
  720. */
  721. if (start &&
  722. (start->sym == ams->sym ||
  723. (ams->sym &&
  724. start->addr == ams->sym->start + ams->map->start)))
  725. saddr = start->al_addr;
  726. if (saddr == 0)
  727. pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
  728. ams->addr,
  729. start ? start->addr : 0,
  730. ams->sym ? ams->sym->start + ams->map->start : 0,
  731. saddr);
  732. err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
  733. if (err)
  734. pr_debug2("account_cycles failed %d\n", err);
  735. return err;
  736. }
  737. static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
  738. {
  739. unsigned n_insn = 0;
  740. u64 offset;
  741. for (offset = start; offset <= end; offset++) {
  742. if (notes->offsets[offset])
  743. n_insn++;
  744. }
  745. return n_insn;
  746. }
  747. static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
  748. {
  749. unsigned n_insn;
  750. u64 offset;
  751. n_insn = annotation__count_insn(notes, start, end);
  752. if (n_insn && ch->num && ch->cycles) {
  753. float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
  754. /* Hide data when there are too many overlaps. */
  755. if (ch->reset >= 0x7fff || ch->reset >= ch->num / 2)
  756. return;
  757. for (offset = start; offset <= end; offset++) {
  758. struct annotation_line *al = notes->offsets[offset];
  759. if (al)
  760. al->ipc = ipc;
  761. }
  762. }
  763. }
  764. void annotation__compute_ipc(struct annotation *notes, size_t size)
  765. {
  766. u64 offset;
  767. if (!notes->src || !notes->src->cycles_hist)
  768. return;
  769. pthread_mutex_lock(&notes->lock);
  770. for (offset = 0; offset < size; ++offset) {
  771. struct cyc_hist *ch;
  772. ch = &notes->src->cycles_hist[offset];
  773. if (ch && ch->cycles) {
  774. struct annotation_line *al;
  775. if (ch->have_start)
  776. annotation__count_and_fill(notes, ch->start, offset, ch);
  777. al = notes->offsets[offset];
  778. if (al && ch->num_aggr)
  779. al->cycles = ch->cycles_aggr / ch->num_aggr;
  780. notes->have_cycles = true;
  781. }
  782. }
  783. pthread_mutex_unlock(&notes->lock);
  784. }
  785. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  786. int evidx)
  787. {
  788. return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample);
  789. }
  790. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  791. int evidx, u64 ip)
  792. {
  793. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample);
  794. }
  795. static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
  796. {
  797. dl->ins.ops = ins__find(arch, dl->ins.name);
  798. if (!dl->ins.ops)
  799. return;
  800. if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
  801. dl->ins.ops = NULL;
  802. }
  803. static int disasm_line__parse(char *line, const char **namep, char **rawp)
  804. {
  805. char tmp, *name = ltrim(line);
  806. if (name[0] == '\0')
  807. return -1;
  808. *rawp = name + 1;
  809. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  810. ++*rawp;
  811. tmp = (*rawp)[0];
  812. (*rawp)[0] = '\0';
  813. *namep = strdup(name);
  814. if (*namep == NULL)
  815. goto out_free_name;
  816. (*rawp)[0] = tmp;
  817. *rawp = ltrim(*rawp);
  818. return 0;
  819. out_free_name:
  820. free((void *)namep);
  821. *namep = NULL;
  822. return -1;
  823. }
  824. struct annotate_args {
  825. size_t privsize;
  826. struct arch *arch;
  827. struct map_symbol ms;
  828. struct perf_evsel *evsel;
  829. s64 offset;
  830. char *line;
  831. int line_nr;
  832. };
  833. static void annotation_line__delete(struct annotation_line *al)
  834. {
  835. void *ptr = (void *) al - al->privsize;
  836. free_srcline(al->path);
  837. zfree(&al->line);
  838. free(ptr);
  839. }
  840. /*
  841. * Allocating the annotation line data with following
  842. * structure:
  843. *
  844. * --------------------------------------
  845. * private space | struct annotation_line
  846. * --------------------------------------
  847. *
  848. * Size of the private space is stored in 'struct annotation_line'.
  849. *
  850. */
  851. static struct annotation_line *
  852. annotation_line__new(struct annotate_args *args, size_t privsize)
  853. {
  854. struct annotation_line *al;
  855. struct perf_evsel *evsel = args->evsel;
  856. size_t size = privsize + sizeof(*al);
  857. int nr = 1;
  858. if (perf_evsel__is_group_event(evsel))
  859. nr = evsel->nr_members;
  860. size += sizeof(al->samples[0]) * nr;
  861. al = zalloc(size);
  862. if (al) {
  863. al = (void *) al + privsize;
  864. al->privsize = privsize;
  865. al->offset = args->offset;
  866. al->line = strdup(args->line);
  867. al->line_nr = args->line_nr;
  868. al->samples_nr = nr;
  869. }
  870. return al;
  871. }
  872. /*
  873. * Allocating the disasm annotation line data with
  874. * following structure:
  875. *
  876. * ------------------------------------------------------------
  877. * privsize space | struct disasm_line | struct annotation_line
  878. * ------------------------------------------------------------
  879. *
  880. * We have 'struct annotation_line' member as last member
  881. * of 'struct disasm_line' to have an easy access.
  882. *
  883. */
  884. static struct disasm_line *disasm_line__new(struct annotate_args *args)
  885. {
  886. struct disasm_line *dl = NULL;
  887. struct annotation_line *al;
  888. size_t privsize = args->privsize + offsetof(struct disasm_line, al);
  889. al = annotation_line__new(args, privsize);
  890. if (al != NULL) {
  891. dl = disasm_line(al);
  892. if (dl->al.line == NULL)
  893. goto out_delete;
  894. if (args->offset != -1) {
  895. if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
  896. goto out_free_line;
  897. disasm_line__init_ins(dl, args->arch, &args->ms);
  898. }
  899. }
  900. return dl;
  901. out_free_line:
  902. zfree(&dl->al.line);
  903. out_delete:
  904. free(dl);
  905. return NULL;
  906. }
  907. void disasm_line__free(struct disasm_line *dl)
  908. {
  909. if (dl->ins.ops && dl->ins.ops->free)
  910. dl->ins.ops->free(&dl->ops);
  911. else
  912. ins__delete(&dl->ops);
  913. free((void *)dl->ins.name);
  914. dl->ins.name = NULL;
  915. annotation_line__delete(&dl->al);
  916. }
  917. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  918. {
  919. if (raw || !dl->ins.ops)
  920. return scnprintf(bf, size, "%-6s %s", dl->ins.name, dl->ops.raw);
  921. return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
  922. }
  923. static void annotation_line__add(struct annotation_line *al, struct list_head *head)
  924. {
  925. list_add_tail(&al->node, head);
  926. }
  927. struct annotation_line *
  928. annotation_line__next(struct annotation_line *pos, struct list_head *head)
  929. {
  930. list_for_each_entry_continue(pos, head, node)
  931. if (pos->offset >= 0)
  932. return pos;
  933. return NULL;
  934. }
  935. static const char *annotate__address_color(struct block_range *br)
  936. {
  937. double cov = block_range__coverage(br);
  938. if (cov >= 0) {
  939. /* mark red for >75% coverage */
  940. if (cov > 0.75)
  941. return PERF_COLOR_RED;
  942. /* mark dull for <1% coverage */
  943. if (cov < 0.01)
  944. return PERF_COLOR_NORMAL;
  945. }
  946. return PERF_COLOR_MAGENTA;
  947. }
  948. static const char *annotate__asm_color(struct block_range *br)
  949. {
  950. double cov = block_range__coverage(br);
  951. if (cov >= 0) {
  952. /* mark dull for <1% coverage */
  953. if (cov < 0.01)
  954. return PERF_COLOR_NORMAL;
  955. }
  956. return PERF_COLOR_BLUE;
  957. }
  958. static void annotate__branch_printf(struct block_range *br, u64 addr)
  959. {
  960. bool emit_comment = true;
  961. if (!br)
  962. return;
  963. #if 1
  964. if (br->is_target && br->start == addr) {
  965. struct block_range *branch = br;
  966. double p;
  967. /*
  968. * Find matching branch to our target.
  969. */
  970. while (!branch->is_branch)
  971. branch = block_range__next(branch);
  972. p = 100 *(double)br->entry / branch->coverage;
  973. if (p > 0.1) {
  974. if (emit_comment) {
  975. emit_comment = false;
  976. printf("\t#");
  977. }
  978. /*
  979. * The percentage of coverage joined at this target in relation
  980. * to the next branch.
  981. */
  982. printf(" +%.2f%%", p);
  983. }
  984. }
  985. #endif
  986. if (br->is_branch && br->end == addr) {
  987. double p = 100*(double)br->taken / br->coverage;
  988. if (p > 0.1) {
  989. if (emit_comment) {
  990. emit_comment = false;
  991. printf("\t#");
  992. }
  993. /*
  994. * The percentage of coverage leaving at this branch, and
  995. * its prediction ratio.
  996. */
  997. printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
  998. }
  999. }
  1000. }
  1001. static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
  1002. {
  1003. s64 offset = dl->al.offset;
  1004. const u64 addr = start + offset;
  1005. struct block_range *br;
  1006. br = block_range__find(addr);
  1007. color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
  1008. color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
  1009. annotate__branch_printf(br, addr);
  1010. return 0;
  1011. }
  1012. static int
  1013. annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
  1014. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  1015. int max_lines, struct annotation_line *queue, int addr_fmt_width)
  1016. {
  1017. struct disasm_line *dl = container_of(al, struct disasm_line, al);
  1018. static const char *prev_line;
  1019. static const char *prev_color;
  1020. if (al->offset != -1) {
  1021. double max_percent = 0.0;
  1022. int i, nr_percent = 1;
  1023. const char *color;
  1024. struct annotation *notes = symbol__annotation(sym);
  1025. for (i = 0; i < al->samples_nr; i++) {
  1026. struct annotation_data *sample = &al->samples[i];
  1027. if (sample->percent > max_percent)
  1028. max_percent = sample->percent;
  1029. }
  1030. if (max_percent < min_pcnt)
  1031. return -1;
  1032. if (max_lines && printed >= max_lines)
  1033. return 1;
  1034. if (queue != NULL) {
  1035. list_for_each_entry_from(queue, &notes->src->source, node) {
  1036. if (queue == al)
  1037. break;
  1038. annotation_line__print(queue, sym, start, evsel, len,
  1039. 0, 0, 1, NULL, addr_fmt_width);
  1040. }
  1041. }
  1042. color = get_percent_color(max_percent);
  1043. /*
  1044. * Also color the filename and line if needed, with
  1045. * the same color than the percentage. Don't print it
  1046. * twice for close colored addr with the same filename:line
  1047. */
  1048. if (al->path) {
  1049. if (!prev_line || strcmp(prev_line, al->path)
  1050. || color != prev_color) {
  1051. color_fprintf(stdout, color, " %s", al->path);
  1052. prev_line = al->path;
  1053. prev_color = color;
  1054. }
  1055. }
  1056. for (i = 0; i < nr_percent; i++) {
  1057. struct annotation_data *sample = &al->samples[i];
  1058. color = get_percent_color(sample->percent);
  1059. if (symbol_conf.show_total_period)
  1060. color_fprintf(stdout, color, " %11" PRIu64,
  1061. sample->he.period);
  1062. else if (symbol_conf.show_nr_samples)
  1063. color_fprintf(stdout, color, " %7" PRIu64,
  1064. sample->he.nr_samples);
  1065. else
  1066. color_fprintf(stdout, color, " %7.2f", sample->percent);
  1067. }
  1068. printf(" : ");
  1069. disasm_line__print(dl, start, addr_fmt_width);
  1070. printf("\n");
  1071. } else if (max_lines && printed >= max_lines)
  1072. return 1;
  1073. else {
  1074. int width = symbol_conf.show_total_period ? 12 : 8;
  1075. if (queue)
  1076. return -1;
  1077. if (perf_evsel__is_group_event(evsel))
  1078. width *= evsel->nr_members;
  1079. if (!*al->line)
  1080. printf(" %*s:\n", width, " ");
  1081. else
  1082. printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line);
  1083. }
  1084. return 0;
  1085. }
  1086. /*
  1087. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  1088. * which looks like following
  1089. *
  1090. * 0000000000415500 <_init>:
  1091. * 415500: sub $0x8,%rsp
  1092. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  1093. * 41550b: test %rax,%rax
  1094. * 41550e: je 415515 <_init+0x15>
  1095. * 415510: callq 416e70 <__gmon_start__@plt>
  1096. * 415515: add $0x8,%rsp
  1097. * 415519: retq
  1098. *
  1099. * it will be parsed and saved into struct disasm_line as
  1100. * <offset> <name> <ops.raw>
  1101. *
  1102. * The offset will be a relative offset from the start of the symbol and -1
  1103. * means that it's not a disassembly line so should be treated differently.
  1104. * The ops.raw part will be parsed further according to type of the instruction.
  1105. */
  1106. static int symbol__parse_objdump_line(struct symbol *sym, FILE *file,
  1107. struct annotate_args *args,
  1108. int *line_nr)
  1109. {
  1110. struct map *map = args->ms.map;
  1111. struct annotation *notes = symbol__annotation(sym);
  1112. struct disasm_line *dl;
  1113. char *line = NULL, *parsed_line, *tmp, *tmp2;
  1114. size_t line_len;
  1115. s64 line_ip, offset = -1;
  1116. regmatch_t match[2];
  1117. if (getline(&line, &line_len, file) < 0)
  1118. return -1;
  1119. if (!line)
  1120. return -1;
  1121. line_ip = -1;
  1122. parsed_line = rtrim(line);
  1123. /* /filename:linenr ? Save line number and ignore. */
  1124. if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
  1125. *line_nr = atoi(parsed_line + match[1].rm_so);
  1126. return 0;
  1127. }
  1128. tmp = ltrim(parsed_line);
  1129. if (*tmp) {
  1130. /*
  1131. * Parse hexa addresses followed by ':'
  1132. */
  1133. line_ip = strtoull(tmp, &tmp2, 16);
  1134. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  1135. line_ip = -1;
  1136. }
  1137. if (line_ip != -1) {
  1138. u64 start = map__rip_2objdump(map, sym->start),
  1139. end = map__rip_2objdump(map, sym->end);
  1140. offset = line_ip - start;
  1141. if ((u64)line_ip < start || (u64)line_ip >= end)
  1142. offset = -1;
  1143. else
  1144. parsed_line = tmp2 + 1;
  1145. }
  1146. args->offset = offset;
  1147. args->line = parsed_line;
  1148. args->line_nr = *line_nr;
  1149. args->ms.sym = sym;
  1150. dl = disasm_line__new(args);
  1151. free(line);
  1152. (*line_nr)++;
  1153. if (dl == NULL)
  1154. return -1;
  1155. if (!disasm_line__has_local_offset(dl)) {
  1156. dl->ops.target.offset = dl->ops.target.addr -
  1157. map__rip_2objdump(map, sym->start);
  1158. dl->ops.target.offset_avail = true;
  1159. }
  1160. /* kcore has no symbols, so add the call target symbol */
  1161. if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
  1162. struct addr_map_symbol target = {
  1163. .map = map,
  1164. .addr = dl->ops.target.addr,
  1165. };
  1166. if (!map_groups__find_ams(&target) &&
  1167. target.sym->start == target.al_addr)
  1168. dl->ops.target.sym = target.sym;
  1169. }
  1170. annotation_line__add(&dl->al, &notes->src->source);
  1171. return 0;
  1172. }
  1173. static __attribute__((constructor)) void symbol__init_regexpr(void)
  1174. {
  1175. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  1176. }
  1177. static void delete_last_nop(struct symbol *sym)
  1178. {
  1179. struct annotation *notes = symbol__annotation(sym);
  1180. struct list_head *list = &notes->src->source;
  1181. struct disasm_line *dl;
  1182. while (!list_empty(list)) {
  1183. dl = list_entry(list->prev, struct disasm_line, al.node);
  1184. if (dl->ins.ops) {
  1185. if (dl->ins.ops != &nop_ops)
  1186. return;
  1187. } else {
  1188. if (!strstr(dl->al.line, " nop ") &&
  1189. !strstr(dl->al.line, " nopl ") &&
  1190. !strstr(dl->al.line, " nopw "))
  1191. return;
  1192. }
  1193. list_del(&dl->al.node);
  1194. disasm_line__free(dl);
  1195. }
  1196. }
  1197. int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
  1198. int errnum, char *buf, size_t buflen)
  1199. {
  1200. struct dso *dso = map->dso;
  1201. BUG_ON(buflen == 0);
  1202. if (errnum >= 0) {
  1203. str_error_r(errnum, buf, buflen);
  1204. return 0;
  1205. }
  1206. switch (errnum) {
  1207. case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
  1208. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  1209. char *build_id_msg = NULL;
  1210. if (dso->has_build_id) {
  1211. build_id__sprintf(dso->build_id,
  1212. sizeof(dso->build_id), bf + 15);
  1213. build_id_msg = bf;
  1214. }
  1215. scnprintf(buf, buflen,
  1216. "No vmlinux file%s\nwas found in the path.\n\n"
  1217. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  1218. "Please use:\n\n"
  1219. " perf buildid-cache -vu vmlinux\n\n"
  1220. "or:\n\n"
  1221. " --vmlinux vmlinux\n", build_id_msg ?: "");
  1222. }
  1223. break;
  1224. default:
  1225. scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
  1226. break;
  1227. }
  1228. return 0;
  1229. }
  1230. static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
  1231. {
  1232. char linkname[PATH_MAX];
  1233. char *build_id_filename;
  1234. char *build_id_path = NULL;
  1235. char *pos;
  1236. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  1237. !dso__is_kcore(dso))
  1238. return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
  1239. build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
  1240. if (build_id_filename) {
  1241. __symbol__join_symfs(filename, filename_size, build_id_filename);
  1242. free(build_id_filename);
  1243. } else {
  1244. if (dso->has_build_id)
  1245. return ENOMEM;
  1246. goto fallback;
  1247. }
  1248. build_id_path = strdup(filename);
  1249. if (!build_id_path)
  1250. return -1;
  1251. /*
  1252. * old style build-id cache has name of XX/XXXXXXX.. while
  1253. * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
  1254. * extract the build-id part of dirname in the new style only.
  1255. */
  1256. pos = strrchr(build_id_path, '/');
  1257. if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
  1258. dirname(build_id_path);
  1259. if (dso__is_kcore(dso) ||
  1260. readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
  1261. strstr(linkname, DSO__NAME_KALLSYMS) ||
  1262. access(filename, R_OK)) {
  1263. fallback:
  1264. /*
  1265. * If we don't have build-ids or the build-id file isn't in the
  1266. * cache, or is just a kallsyms file, well, lets hope that this
  1267. * DSO is the same as when 'perf record' ran.
  1268. */
  1269. __symbol__join_symfs(filename, filename_size, dso->long_name);
  1270. }
  1271. free(build_id_path);
  1272. return 0;
  1273. }
  1274. static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
  1275. {
  1276. struct map *map = args->ms.map;
  1277. struct dso *dso = map->dso;
  1278. char *command;
  1279. FILE *file;
  1280. char symfs_filename[PATH_MAX];
  1281. struct kcore_extract kce;
  1282. bool delete_extract = false;
  1283. int stdout_fd[2];
  1284. int lineno = 0;
  1285. int nline;
  1286. pid_t pid;
  1287. int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
  1288. if (err)
  1289. return err;
  1290. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  1291. symfs_filename, sym->name, map->unmap_ip(map, sym->start),
  1292. map->unmap_ip(map, sym->end));
  1293. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  1294. dso, dso->long_name, sym, sym->name);
  1295. if (dso__is_kcore(dso)) {
  1296. kce.kcore_filename = symfs_filename;
  1297. kce.addr = map__rip_2objdump(map, sym->start);
  1298. kce.offs = sym->start;
  1299. kce.len = sym->end - sym->start;
  1300. if (!kcore_extract__create(&kce)) {
  1301. delete_extract = true;
  1302. strlcpy(symfs_filename, kce.extract_filename,
  1303. sizeof(symfs_filename));
  1304. }
  1305. } else if (dso__needs_decompress(dso)) {
  1306. char tmp[KMOD_DECOMP_LEN];
  1307. if (dso__decompress_kmodule_path(dso, symfs_filename,
  1308. tmp, sizeof(tmp)) < 0)
  1309. goto out;
  1310. strcpy(symfs_filename, tmp);
  1311. }
  1312. err = asprintf(&command,
  1313. "%s %s%s --start-address=0x%016" PRIx64
  1314. " --stop-address=0x%016" PRIx64
  1315. " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
  1316. objdump_path ? objdump_path : "objdump",
  1317. disassembler_style ? "-M " : "",
  1318. disassembler_style ? disassembler_style : "",
  1319. map__rip_2objdump(map, sym->start),
  1320. map__rip_2objdump(map, sym->end),
  1321. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  1322. symbol_conf.annotate_src ? "-S" : "",
  1323. symfs_filename, symfs_filename);
  1324. if (err < 0) {
  1325. pr_err("Failure allocating memory for the command to run\n");
  1326. goto out_remove_tmp;
  1327. }
  1328. pr_debug("Executing: %s\n", command);
  1329. err = -1;
  1330. if (pipe(stdout_fd) < 0) {
  1331. pr_err("Failure creating the pipe to run %s\n", command);
  1332. goto out_free_command;
  1333. }
  1334. pid = fork();
  1335. if (pid < 0) {
  1336. pr_err("Failure forking to run %s\n", command);
  1337. goto out_close_stdout;
  1338. }
  1339. if (pid == 0) {
  1340. close(stdout_fd[0]);
  1341. dup2(stdout_fd[1], 1);
  1342. close(stdout_fd[1]);
  1343. execl("/bin/sh", "sh", "-c", command, NULL);
  1344. perror(command);
  1345. exit(-1);
  1346. }
  1347. close(stdout_fd[1]);
  1348. file = fdopen(stdout_fd[0], "r");
  1349. if (!file) {
  1350. pr_err("Failure creating FILE stream for %s\n", command);
  1351. /*
  1352. * If we were using debug info should retry with
  1353. * original binary.
  1354. */
  1355. goto out_free_command;
  1356. }
  1357. nline = 0;
  1358. while (!feof(file)) {
  1359. /*
  1360. * The source code line number (lineno) needs to be kept in
  1361. * accross calls to symbol__parse_objdump_line(), so that it
  1362. * can associate it with the instructions till the next one.
  1363. * See disasm_line__new() and struct disasm_line::line_nr.
  1364. */
  1365. if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0)
  1366. break;
  1367. nline++;
  1368. }
  1369. if (nline == 0)
  1370. pr_err("No output from %s\n", command);
  1371. /*
  1372. * kallsyms does not have symbol sizes so there may a nop at the end.
  1373. * Remove it.
  1374. */
  1375. if (dso__is_kcore(dso))
  1376. delete_last_nop(sym);
  1377. fclose(file);
  1378. err = 0;
  1379. out_free_command:
  1380. free(command);
  1381. out_remove_tmp:
  1382. close(stdout_fd[0]);
  1383. if (dso__needs_decompress(dso))
  1384. unlink(symfs_filename);
  1385. if (delete_extract)
  1386. kcore_extract__delete(&kce);
  1387. out:
  1388. return err;
  1389. out_close_stdout:
  1390. close(stdout_fd[1]);
  1391. goto out_free_command;
  1392. }
  1393. static void calc_percent(struct sym_hist *hist,
  1394. struct annotation_data *sample,
  1395. s64 offset, s64 end)
  1396. {
  1397. unsigned int hits = 0;
  1398. u64 period = 0;
  1399. while (offset < end) {
  1400. hits += hist->addr[offset].nr_samples;
  1401. period += hist->addr[offset].period;
  1402. ++offset;
  1403. }
  1404. if (hist->nr_samples) {
  1405. sample->he.period = period;
  1406. sample->he.nr_samples = hits;
  1407. sample->percent = 100.0 * hits / hist->nr_samples;
  1408. }
  1409. }
  1410. static void annotation__calc_percent(struct annotation *notes,
  1411. struct perf_evsel *evsel, s64 len)
  1412. {
  1413. struct annotation_line *al, *next;
  1414. list_for_each_entry(al, &notes->src->source, node) {
  1415. s64 end;
  1416. int i;
  1417. if (al->offset == -1)
  1418. continue;
  1419. next = annotation_line__next(al, &notes->src->source);
  1420. end = next ? next->offset : len;
  1421. for (i = 0; i < al->samples_nr; i++) {
  1422. struct annotation_data *sample;
  1423. struct sym_hist *hist;
  1424. hist = annotation__histogram(notes, evsel->idx + i);
  1425. sample = &al->samples[i];
  1426. calc_percent(hist, sample, al->offset, end);
  1427. }
  1428. }
  1429. }
  1430. void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
  1431. {
  1432. struct annotation *notes = symbol__annotation(sym);
  1433. annotation__calc_percent(notes, evsel, symbol__size(sym));
  1434. }
  1435. int symbol__annotate(struct symbol *sym, struct map *map,
  1436. struct perf_evsel *evsel, size_t privsize,
  1437. struct arch **parch)
  1438. {
  1439. struct annotate_args args = {
  1440. .privsize = privsize,
  1441. .evsel = evsel,
  1442. };
  1443. struct perf_env *env = perf_evsel__env(evsel);
  1444. const char *arch_name = perf_env__arch(env);
  1445. struct arch *arch;
  1446. int err;
  1447. if (!arch_name)
  1448. return -1;
  1449. args.arch = arch = arch__find(arch_name);
  1450. if (arch == NULL)
  1451. return -ENOTSUP;
  1452. if (parch)
  1453. *parch = arch;
  1454. if (arch->init) {
  1455. err = arch->init(arch, env ? env->cpuid : NULL);
  1456. if (err) {
  1457. pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
  1458. return err;
  1459. }
  1460. }
  1461. args.ms.map = map;
  1462. args.ms.sym = sym;
  1463. return symbol__disassemble(sym, &args);
  1464. }
  1465. static void insert_source_line(struct rb_root *root, struct annotation_line *al)
  1466. {
  1467. struct annotation_line *iter;
  1468. struct rb_node **p = &root->rb_node;
  1469. struct rb_node *parent = NULL;
  1470. int i, ret;
  1471. while (*p != NULL) {
  1472. parent = *p;
  1473. iter = rb_entry(parent, struct annotation_line, rb_node);
  1474. ret = strcmp(iter->path, al->path);
  1475. if (ret == 0) {
  1476. for (i = 0; i < al->samples_nr; i++)
  1477. iter->samples[i].percent_sum += al->samples[i].percent;
  1478. return;
  1479. }
  1480. if (ret < 0)
  1481. p = &(*p)->rb_left;
  1482. else
  1483. p = &(*p)->rb_right;
  1484. }
  1485. for (i = 0; i < al->samples_nr; i++)
  1486. al->samples[i].percent_sum = al->samples[i].percent;
  1487. rb_link_node(&al->rb_node, parent, p);
  1488. rb_insert_color(&al->rb_node, root);
  1489. }
  1490. static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
  1491. {
  1492. int i;
  1493. for (i = 0; i < a->samples_nr; i++) {
  1494. if (a->samples[i].percent_sum == b->samples[i].percent_sum)
  1495. continue;
  1496. return a->samples[i].percent_sum > b->samples[i].percent_sum;
  1497. }
  1498. return 0;
  1499. }
  1500. static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
  1501. {
  1502. struct annotation_line *iter;
  1503. struct rb_node **p = &root->rb_node;
  1504. struct rb_node *parent = NULL;
  1505. while (*p != NULL) {
  1506. parent = *p;
  1507. iter = rb_entry(parent, struct annotation_line, rb_node);
  1508. if (cmp_source_line(al, iter))
  1509. p = &(*p)->rb_left;
  1510. else
  1511. p = &(*p)->rb_right;
  1512. }
  1513. rb_link_node(&al->rb_node, parent, p);
  1514. rb_insert_color(&al->rb_node, root);
  1515. }
  1516. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1517. {
  1518. struct annotation_line *al;
  1519. struct rb_node *node;
  1520. node = rb_first(src_root);
  1521. while (node) {
  1522. struct rb_node *next;
  1523. al = rb_entry(node, struct annotation_line, rb_node);
  1524. next = rb_next(node);
  1525. rb_erase(node, src_root);
  1526. __resort_source_line(dest_root, al);
  1527. node = next;
  1528. }
  1529. }
  1530. static void print_summary(struct rb_root *root, const char *filename)
  1531. {
  1532. struct annotation_line *al;
  1533. struct rb_node *node;
  1534. printf("\nSorted summary for file %s\n", filename);
  1535. printf("----------------------------------------------\n\n");
  1536. if (RB_EMPTY_ROOT(root)) {
  1537. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1538. return;
  1539. }
  1540. node = rb_first(root);
  1541. while (node) {
  1542. double percent, percent_max = 0.0;
  1543. const char *color;
  1544. char *path;
  1545. int i;
  1546. al = rb_entry(node, struct annotation_line, rb_node);
  1547. for (i = 0; i < al->samples_nr; i++) {
  1548. percent = al->samples[i].percent_sum;
  1549. color = get_percent_color(percent);
  1550. color_fprintf(stdout, color, " %7.2f", percent);
  1551. if (percent > percent_max)
  1552. percent_max = percent;
  1553. }
  1554. path = al->path;
  1555. color = get_percent_color(percent_max);
  1556. color_fprintf(stdout, color, " %s\n", path);
  1557. node = rb_next(node);
  1558. }
  1559. }
  1560. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1561. {
  1562. struct annotation *notes = symbol__annotation(sym);
  1563. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1564. u64 len = symbol__size(sym), offset;
  1565. for (offset = 0; offset < len; ++offset)
  1566. if (h->addr[offset].nr_samples != 0)
  1567. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1568. sym->start + offset, h->addr[offset].nr_samples);
  1569. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
  1570. }
  1571. static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
  1572. {
  1573. char bf[32];
  1574. struct annotation_line *line;
  1575. list_for_each_entry_reverse(line, lines, node) {
  1576. if (line->offset != -1)
  1577. return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
  1578. }
  1579. return 0;
  1580. }
  1581. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1582. struct perf_evsel *evsel, bool full_paths,
  1583. int min_pcnt, int max_lines, int context)
  1584. {
  1585. struct dso *dso = map->dso;
  1586. char *filename;
  1587. const char *d_filename;
  1588. const char *evsel_name = perf_evsel__name(evsel);
  1589. struct annotation *notes = symbol__annotation(sym);
  1590. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1591. struct annotation_line *pos, *queue = NULL;
  1592. u64 start = map__rip_2objdump(map, sym->start);
  1593. int printed = 2, queue_len = 0, addr_fmt_width;
  1594. int more = 0;
  1595. u64 len;
  1596. int width = symbol_conf.show_total_period ? 12 : 8;
  1597. int graph_dotted_len;
  1598. filename = strdup(dso->long_name);
  1599. if (!filename)
  1600. return -ENOMEM;
  1601. if (full_paths)
  1602. d_filename = filename;
  1603. else
  1604. d_filename = basename(filename);
  1605. len = symbol__size(sym);
  1606. if (perf_evsel__is_group_event(evsel))
  1607. width *= evsel->nr_members;
  1608. graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
  1609. width, width, symbol_conf.show_total_period ? "Period" :
  1610. symbol_conf.show_nr_samples ? "Samples" : "Percent",
  1611. d_filename, evsel_name, h->nr_samples);
  1612. printf("%-*.*s----\n",
  1613. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1614. if (verbose > 0)
  1615. symbol__annotate_hits(sym, evsel);
  1616. addr_fmt_width = annotated_source__addr_fmt_width(&notes->src->source, start);
  1617. list_for_each_entry(pos, &notes->src->source, node) {
  1618. int err;
  1619. if (context && queue == NULL) {
  1620. queue = pos;
  1621. queue_len = 0;
  1622. }
  1623. err = annotation_line__print(pos, sym, start, evsel, len,
  1624. min_pcnt, printed, max_lines,
  1625. queue, addr_fmt_width);
  1626. switch (err) {
  1627. case 0:
  1628. ++printed;
  1629. if (context) {
  1630. printed += queue_len;
  1631. queue = NULL;
  1632. queue_len = 0;
  1633. }
  1634. break;
  1635. case 1:
  1636. /* filtered by max_lines */
  1637. ++more;
  1638. break;
  1639. case -1:
  1640. default:
  1641. /*
  1642. * Filtered by min_pcnt or non IP lines when
  1643. * context != 0
  1644. */
  1645. if (!context)
  1646. break;
  1647. if (queue_len == context)
  1648. queue = list_entry(queue->node.next, typeof(*queue), node);
  1649. else
  1650. ++queue_len;
  1651. break;
  1652. }
  1653. }
  1654. free(filename);
  1655. return more;
  1656. }
  1657. static void FILE__set_percent_color(void *fp __maybe_unused,
  1658. double percent __maybe_unused,
  1659. bool current __maybe_unused)
  1660. {
  1661. }
  1662. static int FILE__set_jumps_percent_color(void *fp __maybe_unused,
  1663. int nr __maybe_unused, bool current __maybe_unused)
  1664. {
  1665. return 0;
  1666. }
  1667. static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused)
  1668. {
  1669. return 0;
  1670. }
  1671. static void FILE__printf(void *fp, const char *fmt, ...)
  1672. {
  1673. va_list args;
  1674. va_start(args, fmt);
  1675. vfprintf(fp, fmt, args);
  1676. va_end(args);
  1677. }
  1678. static void FILE__write_graph(void *fp, int graph)
  1679. {
  1680. const char *s;
  1681. switch (graph) {
  1682. case DARROW_CHAR: s = "↓"; break;
  1683. case UARROW_CHAR: s = "↑"; break;
  1684. case LARROW_CHAR: s = "←"; break;
  1685. case RARROW_CHAR: s = "→"; break;
  1686. default: s = "?"; break;
  1687. }
  1688. fputs(s, fp);
  1689. }
  1690. int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp)
  1691. {
  1692. struct annotation *notes = symbol__annotation(sym);
  1693. struct annotation_write_ops ops = {
  1694. .first_line = true,
  1695. .obj = fp,
  1696. .set_color = FILE__set_color,
  1697. .set_percent_color = FILE__set_percent_color,
  1698. .set_jumps_percent_color = FILE__set_jumps_percent_color,
  1699. .printf = FILE__printf,
  1700. .write_graph = FILE__write_graph,
  1701. };
  1702. struct annotation_line *al;
  1703. list_for_each_entry(al, &notes->src->source, node) {
  1704. if (annotation_line__filter(al, notes))
  1705. continue;
  1706. annotation_line__write(al, notes, &ops);
  1707. fputc('\n', fp);
  1708. ops.first_line = false;
  1709. }
  1710. return 0;
  1711. }
  1712. int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel)
  1713. {
  1714. const char *ev_name = perf_evsel__name(evsel);
  1715. char buf[1024];
  1716. char *filename;
  1717. int err = -1;
  1718. FILE *fp;
  1719. if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0)
  1720. return -1;
  1721. fp = fopen(filename, "w");
  1722. if (fp == NULL)
  1723. goto out_free_filename;
  1724. if (perf_evsel__is_group_event(evsel)) {
  1725. perf_evsel__group_desc(evsel, buf, sizeof(buf));
  1726. ev_name = buf;
  1727. }
  1728. fprintf(fp, "%s() %s\nEvent: %s\n\n",
  1729. ms->sym->name, ms->map->dso->long_name, ev_name);
  1730. symbol__annotate_fprintf2(ms->sym, fp);
  1731. fclose(fp);
  1732. err = 0;
  1733. out_free_filename:
  1734. free(filename);
  1735. return err;
  1736. }
  1737. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1738. {
  1739. struct annotation *notes = symbol__annotation(sym);
  1740. struct sym_hist *h = annotation__histogram(notes, evidx);
  1741. memset(h, 0, notes->src->sizeof_sym_hist);
  1742. }
  1743. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1744. {
  1745. struct annotation *notes = symbol__annotation(sym);
  1746. struct sym_hist *h = annotation__histogram(notes, evidx);
  1747. int len = symbol__size(sym), offset;
  1748. h->nr_samples = 0;
  1749. for (offset = 0; offset < len; ++offset) {
  1750. h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
  1751. h->nr_samples += h->addr[offset].nr_samples;
  1752. }
  1753. }
  1754. void annotated_source__purge(struct annotated_source *as)
  1755. {
  1756. struct annotation_line *al, *n;
  1757. list_for_each_entry_safe(al, n, &as->source, node) {
  1758. list_del(&al->node);
  1759. disasm_line__free(disasm_line(al));
  1760. }
  1761. }
  1762. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1763. {
  1764. size_t printed;
  1765. if (dl->al.offset == -1)
  1766. return fprintf(fp, "%s\n", dl->al.line);
  1767. printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
  1768. if (dl->ops.raw[0] != '\0') {
  1769. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1770. dl->ops.raw);
  1771. }
  1772. return printed + fprintf(fp, "\n");
  1773. }
  1774. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1775. {
  1776. struct disasm_line *pos;
  1777. size_t printed = 0;
  1778. list_for_each_entry(pos, head, al.node)
  1779. printed += disasm_line__fprintf(pos, fp);
  1780. return printed;
  1781. }
  1782. bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym)
  1783. {
  1784. if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) ||
  1785. !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 ||
  1786. dl->ops.target.offset >= (s64)symbol__size(sym))
  1787. return false;
  1788. return true;
  1789. }
  1790. void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym)
  1791. {
  1792. u64 offset, size = symbol__size(sym);
  1793. /* PLT symbols contain external offsets */
  1794. if (strstr(sym->name, "@plt"))
  1795. return;
  1796. for (offset = 0; offset < size; ++offset) {
  1797. struct annotation_line *al = notes->offsets[offset];
  1798. struct disasm_line *dl;
  1799. dl = disasm_line(al);
  1800. if (!disasm_line__is_valid_local_jump(dl, sym))
  1801. continue;
  1802. al = notes->offsets[dl->ops.target.offset];
  1803. /*
  1804. * FIXME: Oops, no jump target? Buggy disassembler? Or do we
  1805. * have to adjust to the previous offset?
  1806. */
  1807. if (al == NULL)
  1808. continue;
  1809. if (++al->jump_sources > notes->max_jump_sources)
  1810. notes->max_jump_sources = al->jump_sources;
  1811. ++notes->nr_jumps;
  1812. }
  1813. }
  1814. void annotation__set_offsets(struct annotation *notes, s64 size)
  1815. {
  1816. struct annotation_line *al;
  1817. notes->max_line_len = 0;
  1818. list_for_each_entry(al, &notes->src->source, node) {
  1819. size_t line_len = strlen(al->line);
  1820. if (notes->max_line_len < line_len)
  1821. notes->max_line_len = line_len;
  1822. al->idx = notes->nr_entries++;
  1823. if (al->offset != -1) {
  1824. al->idx_asm = notes->nr_asm_entries++;
  1825. /*
  1826. * FIXME: short term bandaid to cope with assembly
  1827. * routines that comes with labels in the same column
  1828. * as the address in objdump, sigh.
  1829. *
  1830. * E.g. copy_user_generic_unrolled
  1831. */
  1832. if (al->offset < size)
  1833. notes->offsets[al->offset] = al;
  1834. } else
  1835. al->idx_asm = -1;
  1836. }
  1837. }
  1838. static inline int width_jumps(int n)
  1839. {
  1840. if (n >= 100)
  1841. return 5;
  1842. if (n / 10)
  1843. return 2;
  1844. return 1;
  1845. }
  1846. void annotation__init_column_widths(struct annotation *notes, struct symbol *sym)
  1847. {
  1848. notes->widths.addr = notes->widths.target =
  1849. notes->widths.min_addr = hex_width(symbol__size(sym));
  1850. notes->widths.max_addr = hex_width(sym->end);
  1851. notes->widths.jumps = width_jumps(notes->max_jump_sources);
  1852. }
  1853. void annotation__update_column_widths(struct annotation *notes)
  1854. {
  1855. if (notes->options->use_offset)
  1856. notes->widths.target = notes->widths.min_addr;
  1857. else
  1858. notes->widths.target = notes->widths.max_addr;
  1859. notes->widths.addr = notes->widths.target;
  1860. if (notes->options->show_nr_jumps)
  1861. notes->widths.addr += notes->widths.jumps + 1;
  1862. }
  1863. static void annotation__calc_lines(struct annotation *notes, struct map *map,
  1864. struct rb_root *root)
  1865. {
  1866. struct annotation_line *al;
  1867. struct rb_root tmp_root = RB_ROOT;
  1868. list_for_each_entry(al, &notes->src->source, node) {
  1869. double percent_max = 0.0;
  1870. int i;
  1871. for (i = 0; i < al->samples_nr; i++) {
  1872. struct annotation_data *sample;
  1873. sample = &al->samples[i];
  1874. if (sample->percent > percent_max)
  1875. percent_max = sample->percent;
  1876. }
  1877. if (percent_max <= 0.5)
  1878. continue;
  1879. al->path = get_srcline(map->dso, notes->start + al->offset, NULL,
  1880. false, true, notes->start + al->offset);
  1881. insert_source_line(&tmp_root, al);
  1882. }
  1883. resort_source_line(root, &tmp_root);
  1884. }
  1885. static void symbol__calc_lines(struct symbol *sym, struct map *map,
  1886. struct rb_root *root)
  1887. {
  1888. struct annotation *notes = symbol__annotation(sym);
  1889. annotation__calc_lines(notes, map, root);
  1890. }
  1891. int symbol__tty_annotate2(struct symbol *sym, struct map *map,
  1892. struct perf_evsel *evsel, bool print_lines,
  1893. bool full_paths)
  1894. {
  1895. struct dso *dso = map->dso;
  1896. struct rb_root source_line = RB_ROOT;
  1897. struct annotation_options opts = annotation__default_options;
  1898. const char *ev_name = perf_evsel__name(evsel);
  1899. char buf[1024];
  1900. if (symbol__annotate2(sym, map, evsel, &opts, NULL) < 0)
  1901. return -1;
  1902. if (print_lines) {
  1903. srcline_full_filename = full_paths;
  1904. symbol__calc_lines(sym, map, &source_line);
  1905. print_summary(&source_line, dso->long_name);
  1906. }
  1907. if (perf_evsel__is_group_event(evsel)) {
  1908. perf_evsel__group_desc(evsel, buf, sizeof(buf));
  1909. ev_name = buf;
  1910. }
  1911. fprintf(stdout, "%s() %s\nEvent: %s\n\n", sym->name, dso->long_name, ev_name);
  1912. symbol__annotate_fprintf2(sym, stdout);
  1913. annotated_source__purge(symbol__annotation(sym)->src);
  1914. return 0;
  1915. }
  1916. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1917. struct perf_evsel *evsel, bool print_lines,
  1918. bool full_paths, int min_pcnt, int max_lines)
  1919. {
  1920. struct dso *dso = map->dso;
  1921. struct rb_root source_line = RB_ROOT;
  1922. if (symbol__annotate(sym, map, evsel, 0, NULL) < 0)
  1923. return -1;
  1924. symbol__calc_percent(sym, evsel);
  1925. if (print_lines) {
  1926. srcline_full_filename = full_paths;
  1927. symbol__calc_lines(sym, map, &source_line);
  1928. print_summary(&source_line, dso->long_name);
  1929. }
  1930. symbol__annotate_printf(sym, map, evsel, full_paths,
  1931. min_pcnt, max_lines, 0);
  1932. annotated_source__purge(symbol__annotation(sym)->src);
  1933. return 0;
  1934. }
  1935. bool ui__has_annotation(void)
  1936. {
  1937. return use_browser == 1 && perf_hpp_list.sym;
  1938. }
  1939. double annotation_line__max_percent(struct annotation_line *al, struct annotation *notes)
  1940. {
  1941. double percent_max = 0.0;
  1942. int i;
  1943. for (i = 0; i < notes->nr_events; i++) {
  1944. if (al->samples[i].percent > percent_max)
  1945. percent_max = al->samples[i].percent;
  1946. }
  1947. return percent_max;
  1948. }
  1949. static void disasm_line__write(struct disasm_line *dl, struct annotation *notes,
  1950. void *obj, char *bf, size_t size,
  1951. void (*obj__printf)(void *obj, const char *fmt, ...),
  1952. void (*obj__write_graph)(void *obj, int graph))
  1953. {
  1954. if (dl->ins.ops && dl->ins.ops->scnprintf) {
  1955. if (ins__is_jump(&dl->ins)) {
  1956. bool fwd;
  1957. if (dl->ops.target.outside)
  1958. goto call_like;
  1959. fwd = dl->ops.target.offset > dl->al.offset;
  1960. obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR);
  1961. obj__printf(obj, " ");
  1962. } else if (ins__is_call(&dl->ins)) {
  1963. call_like:
  1964. obj__write_graph(obj, RARROW_CHAR);
  1965. obj__printf(obj, " ");
  1966. } else if (ins__is_ret(&dl->ins)) {
  1967. obj__write_graph(obj, LARROW_CHAR);
  1968. obj__printf(obj, " ");
  1969. } else {
  1970. obj__printf(obj, " ");
  1971. }
  1972. } else {
  1973. obj__printf(obj, " ");
  1974. }
  1975. disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset);
  1976. }
  1977. static void __annotation_line__write(struct annotation_line *al, struct annotation *notes,
  1978. bool first_line, bool current_entry, bool change_color, int width,
  1979. void *obj,
  1980. int (*obj__set_color)(void *obj, int color),
  1981. void (*obj__set_percent_color)(void *obj, double percent, bool current),
  1982. int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current),
  1983. void (*obj__printf)(void *obj, const char *fmt, ...),
  1984. void (*obj__write_graph)(void *obj, int graph))
  1985. {
  1986. double percent_max = annotation_line__max_percent(al, notes);
  1987. int pcnt_width = annotation__pcnt_width(notes),
  1988. cycles_width = annotation__cycles_width(notes);
  1989. bool show_title = false;
  1990. char bf[256];
  1991. int printed;
  1992. if (first_line && (al->offset == -1 || percent_max == 0.0)) {
  1993. if (notes->have_cycles) {
  1994. if (al->ipc == 0.0 && al->cycles == 0)
  1995. show_title = true;
  1996. } else
  1997. show_title = true;
  1998. }
  1999. if (al->offset != -1 && percent_max != 0.0) {
  2000. int i;
  2001. for (i = 0; i < notes->nr_events; i++) {
  2002. obj__set_percent_color(obj, al->samples[i].percent, current_entry);
  2003. if (notes->options->show_total_period) {
  2004. obj__printf(obj, "%11" PRIu64 " ", al->samples[i].he.period);
  2005. } else if (notes->options->show_nr_samples) {
  2006. obj__printf(obj, "%6" PRIu64 " ",
  2007. al->samples[i].he.nr_samples);
  2008. } else {
  2009. obj__printf(obj, "%6.2f ",
  2010. al->samples[i].percent);
  2011. }
  2012. }
  2013. } else {
  2014. obj__set_percent_color(obj, 0, current_entry);
  2015. if (!show_title)
  2016. obj__printf(obj, "%-*s", pcnt_width, " ");
  2017. else {
  2018. obj__printf(obj, "%-*s", pcnt_width,
  2019. notes->options->show_total_period ? "Period" :
  2020. notes->options->show_nr_samples ? "Samples" : "Percent");
  2021. }
  2022. }
  2023. if (notes->have_cycles) {
  2024. if (al->ipc)
  2025. obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc);
  2026. else if (!show_title)
  2027. obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " ");
  2028. else
  2029. obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC");
  2030. if (al->cycles)
  2031. obj__printf(obj, "%*" PRIu64 " ",
  2032. ANNOTATION__CYCLES_WIDTH - 1, al->cycles);
  2033. else if (!show_title)
  2034. obj__printf(obj, "%*s", ANNOTATION__CYCLES_WIDTH, " ");
  2035. else
  2036. obj__printf(obj, "%*s ", ANNOTATION__CYCLES_WIDTH - 1, "Cycle");
  2037. }
  2038. obj__printf(obj, " ");
  2039. if (!*al->line)
  2040. obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " ");
  2041. else if (al->offset == -1) {
  2042. if (al->line_nr && notes->options->show_linenr)
  2043. printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr);
  2044. else
  2045. printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " ");
  2046. obj__printf(obj, bf);
  2047. obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line);
  2048. } else {
  2049. u64 addr = al->offset;
  2050. int color = -1;
  2051. if (!notes->options->use_offset)
  2052. addr += notes->start;
  2053. if (!notes->options->use_offset) {
  2054. printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
  2055. } else {
  2056. if (al->jump_sources) {
  2057. if (notes->options->show_nr_jumps) {
  2058. int prev;
  2059. printed = scnprintf(bf, sizeof(bf), "%*d ",
  2060. notes->widths.jumps,
  2061. al->jump_sources);
  2062. prev = obj__set_jumps_percent_color(obj, al->jump_sources,
  2063. current_entry);
  2064. obj__printf(obj, bf);
  2065. obj__set_color(obj, prev);
  2066. }
  2067. printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
  2068. notes->widths.target, addr);
  2069. } else {
  2070. printed = scnprintf(bf, sizeof(bf), "%-*s ",
  2071. notes->widths.addr, " ");
  2072. }
  2073. }
  2074. if (change_color)
  2075. color = obj__set_color(obj, HE_COLORSET_ADDR);
  2076. obj__printf(obj, bf);
  2077. if (change_color)
  2078. obj__set_color(obj, color);
  2079. disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph);
  2080. obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf);
  2081. }
  2082. }
  2083. void annotation_line__write(struct annotation_line *al, struct annotation *notes,
  2084. struct annotation_write_ops *ops)
  2085. {
  2086. __annotation_line__write(al, notes, ops->first_line, ops->current_entry,
  2087. ops->change_color, ops->width, ops->obj,
  2088. ops->set_color, ops->set_percent_color,
  2089. ops->set_jumps_percent_color, ops->printf,
  2090. ops->write_graph);
  2091. }
  2092. int symbol__annotate2(struct symbol *sym, struct map *map, struct perf_evsel *evsel,
  2093. struct annotation_options *options, struct arch **parch)
  2094. {
  2095. struct annotation *notes = symbol__annotation(sym);
  2096. size_t size = symbol__size(sym);
  2097. int nr_pcnt = 1, err;
  2098. notes->offsets = zalloc(size * sizeof(struct annotation_line *));
  2099. if (notes->offsets == NULL)
  2100. return -1;
  2101. if (perf_evsel__is_group_event(evsel))
  2102. nr_pcnt = evsel->nr_members;
  2103. err = symbol__annotate(sym, map, evsel, 0, parch);
  2104. if (err)
  2105. goto out_free_offsets;
  2106. notes->options = options;
  2107. symbol__calc_percent(sym, evsel);
  2108. notes->start = map__rip_2objdump(map, sym->start);
  2109. annotation__set_offsets(notes, size);
  2110. annotation__mark_jump_targets(notes, sym);
  2111. annotation__compute_ipc(notes, size);
  2112. annotation__init_column_widths(notes, sym);
  2113. notes->nr_events = nr_pcnt;
  2114. annotation__update_column_widths(notes);
  2115. return 0;
  2116. out_free_offsets:
  2117. zfree(&notes->offsets);
  2118. return -1;
  2119. }
  2120. #define ANNOTATION__CFG(n) \
  2121. { .name = #n, .value = &annotation__default_options.n, }
  2122. /*
  2123. * Keep the entries sorted, they are bsearch'ed
  2124. */
  2125. static struct annotation_config {
  2126. const char *name;
  2127. bool *value;
  2128. } annotation__configs[] = {
  2129. ANNOTATION__CFG(hide_src_code),
  2130. ANNOTATION__CFG(jump_arrows),
  2131. ANNOTATION__CFG(show_linenr),
  2132. ANNOTATION__CFG(show_nr_jumps),
  2133. ANNOTATION__CFG(show_nr_samples),
  2134. ANNOTATION__CFG(show_total_period),
  2135. ANNOTATION__CFG(use_offset),
  2136. };
  2137. #undef ANNOTATION__CFG
  2138. static int annotation_config__cmp(const void *name, const void *cfgp)
  2139. {
  2140. const struct annotation_config *cfg = cfgp;
  2141. return strcmp(name, cfg->name);
  2142. }
  2143. static int annotation__config(const char *var, const char *value,
  2144. void *data __maybe_unused)
  2145. {
  2146. struct annotation_config *cfg;
  2147. const char *name;
  2148. if (!strstarts(var, "annotate."))
  2149. return 0;
  2150. name = var + 9;
  2151. cfg = bsearch(name, annotation__configs, ARRAY_SIZE(annotation__configs),
  2152. sizeof(struct annotation_config), annotation_config__cmp);
  2153. if (cfg == NULL)
  2154. pr_debug("%s variable unknown, ignoring...", var);
  2155. else
  2156. *cfg->value = perf_config_bool(name, value);
  2157. return 0;
  2158. }
  2159. void annotation_config__init(void)
  2160. {
  2161. perf_config(annotation__config, NULL);
  2162. annotation__default_options.show_total_period = symbol_conf.show_total_period;
  2163. annotation__default_options.show_nr_samples = symbol_conf.show_nr_samples;
  2164. }