annotate.c 46 KB

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