annotate.c 67 KB

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