annotate.c 47 KB

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