annotate.c 46 KB

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