annotate.c 65 KB

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