annotate.c 66 KB

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