annotate.c 59 KB

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