annotate.c 46 KB

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