annotate.c 69 KB

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