annotate.c 67 KB

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