annotate.c 65 KB

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