annotate.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  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. struct annotate_args {
  715. size_t privsize;
  716. };
  717. static struct disasm_line *disasm_line__new(struct annotate_args *args,
  718. s64 offset, char *line,
  719. int line_nr,
  720. struct arch *arch,
  721. struct map *map)
  722. {
  723. struct disasm_line *dl = zalloc(sizeof(*dl) + args->privsize);
  724. if (dl != NULL) {
  725. dl->al.offset = offset;
  726. dl->al.line = strdup(line);
  727. dl->al.line_nr = line_nr;
  728. if (dl->al.line == NULL)
  729. goto out_delete;
  730. if (offset != -1) {
  731. if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
  732. goto out_free_line;
  733. disasm_line__init_ins(dl, arch, map);
  734. }
  735. }
  736. return dl;
  737. out_free_line:
  738. zfree(&dl->al.line);
  739. out_delete:
  740. free(dl);
  741. return NULL;
  742. }
  743. void disasm_line__free(struct disasm_line *dl)
  744. {
  745. zfree(&dl->al.line);
  746. if (dl->ins.ops && dl->ins.ops->free)
  747. dl->ins.ops->free(&dl->ops);
  748. else
  749. ins__delete(&dl->ops);
  750. free((void *)dl->ins.name);
  751. dl->ins.name = NULL;
  752. free(dl);
  753. }
  754. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  755. {
  756. if (raw || !dl->ins.ops)
  757. return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
  758. return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
  759. }
  760. static void disasm__add(struct list_head *head, struct disasm_line *line)
  761. {
  762. list_add_tail(&line->al.node, head);
  763. }
  764. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  765. {
  766. list_for_each_entry_continue(pos, head, al.node)
  767. if (pos->al.offset >= 0)
  768. return pos;
  769. return NULL;
  770. }
  771. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  772. s64 end, const char **path, struct sym_hist_entry *sample)
  773. {
  774. struct source_line *src_line = notes->src->lines;
  775. double percent = 0.0;
  776. sample->nr_samples = sample->period = 0;
  777. if (src_line) {
  778. size_t sizeof_src_line = sizeof(*src_line) +
  779. sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
  780. while (offset < end) {
  781. src_line = (void *)notes->src->lines +
  782. (sizeof_src_line * offset);
  783. if (*path == NULL)
  784. *path = src_line->path;
  785. percent += src_line->samples[evidx].percent;
  786. sample->nr_samples += src_line->samples[evidx].nr;
  787. offset++;
  788. }
  789. } else {
  790. struct sym_hist *h = annotation__histogram(notes, evidx);
  791. unsigned int hits = 0;
  792. u64 period = 0;
  793. while (offset < end) {
  794. hits += h->addr[offset].nr_samples;
  795. period += h->addr[offset].period;
  796. ++offset;
  797. }
  798. if (h->nr_samples) {
  799. sample->period = period;
  800. sample->nr_samples = hits;
  801. percent = 100.0 * hits / h->nr_samples;
  802. }
  803. }
  804. return percent;
  805. }
  806. static const char *annotate__address_color(struct block_range *br)
  807. {
  808. double cov = block_range__coverage(br);
  809. if (cov >= 0) {
  810. /* mark red for >75% coverage */
  811. if (cov > 0.75)
  812. return PERF_COLOR_RED;
  813. /* mark dull for <1% coverage */
  814. if (cov < 0.01)
  815. return PERF_COLOR_NORMAL;
  816. }
  817. return PERF_COLOR_MAGENTA;
  818. }
  819. static const char *annotate__asm_color(struct block_range *br)
  820. {
  821. double cov = block_range__coverage(br);
  822. if (cov >= 0) {
  823. /* mark dull for <1% coverage */
  824. if (cov < 0.01)
  825. return PERF_COLOR_NORMAL;
  826. }
  827. return PERF_COLOR_BLUE;
  828. }
  829. static void annotate__branch_printf(struct block_range *br, u64 addr)
  830. {
  831. bool emit_comment = true;
  832. if (!br)
  833. return;
  834. #if 1
  835. if (br->is_target && br->start == addr) {
  836. struct block_range *branch = br;
  837. double p;
  838. /*
  839. * Find matching branch to our target.
  840. */
  841. while (!branch->is_branch)
  842. branch = block_range__next(branch);
  843. p = 100 *(double)br->entry / branch->coverage;
  844. if (p > 0.1) {
  845. if (emit_comment) {
  846. emit_comment = false;
  847. printf("\t#");
  848. }
  849. /*
  850. * The percentage of coverage joined at this target in relation
  851. * to the next branch.
  852. */
  853. printf(" +%.2f%%", p);
  854. }
  855. }
  856. #endif
  857. if (br->is_branch && br->end == addr) {
  858. double p = 100*(double)br->taken / br->coverage;
  859. if (p > 0.1) {
  860. if (emit_comment) {
  861. emit_comment = false;
  862. printf("\t#");
  863. }
  864. /*
  865. * The percentage of coverage leaving at this branch, and
  866. * its prediction ratio.
  867. */
  868. printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
  869. }
  870. }
  871. }
  872. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  873. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  874. int max_lines, struct disasm_line *queue)
  875. {
  876. static const char *prev_line;
  877. static const char *prev_color;
  878. if (dl->al.offset != -1) {
  879. const char *path = NULL;
  880. double percent, max_percent = 0.0;
  881. double *ppercents = &percent;
  882. struct sym_hist_entry sample;
  883. struct sym_hist_entry *psamples = &sample;
  884. int i, nr_percent = 1;
  885. const char *color;
  886. struct annotation *notes = symbol__annotation(sym);
  887. s64 offset = dl->al.offset;
  888. const u64 addr = start + offset;
  889. struct disasm_line *next;
  890. struct block_range *br;
  891. next = disasm__get_next_ip_line(&notes->src->source, dl);
  892. if (perf_evsel__is_group_event(evsel)) {
  893. nr_percent = evsel->nr_members;
  894. ppercents = calloc(nr_percent, sizeof(double));
  895. psamples = calloc(nr_percent, sizeof(struct sym_hist_entry));
  896. if (ppercents == NULL || psamples == NULL) {
  897. return -1;
  898. }
  899. }
  900. for (i = 0; i < nr_percent; i++) {
  901. percent = disasm__calc_percent(notes,
  902. notes->src->lines ? i : evsel->idx + i,
  903. offset,
  904. next ? next->al.offset : (s64) len,
  905. &path, &sample);
  906. ppercents[i] = percent;
  907. psamples[i] = sample;
  908. if (percent > max_percent)
  909. max_percent = percent;
  910. }
  911. if (max_percent < min_pcnt)
  912. return -1;
  913. if (max_lines && printed >= max_lines)
  914. return 1;
  915. if (queue != NULL) {
  916. list_for_each_entry_from(queue, &notes->src->source, al.node) {
  917. if (queue == dl)
  918. break;
  919. disasm_line__print(queue, sym, start, evsel, len,
  920. 0, 0, 1, NULL);
  921. }
  922. }
  923. color = get_percent_color(max_percent);
  924. /*
  925. * Also color the filename and line if needed, with
  926. * the same color than the percentage. Don't print it
  927. * twice for close colored addr with the same filename:line
  928. */
  929. if (path) {
  930. if (!prev_line || strcmp(prev_line, path)
  931. || color != prev_color) {
  932. color_fprintf(stdout, color, " %s", path);
  933. prev_line = path;
  934. prev_color = color;
  935. }
  936. }
  937. for (i = 0; i < nr_percent; i++) {
  938. percent = ppercents[i];
  939. sample = psamples[i];
  940. color = get_percent_color(percent);
  941. if (symbol_conf.show_total_period)
  942. color_fprintf(stdout, color, " %11" PRIu64,
  943. sample.period);
  944. else if (symbol_conf.show_nr_samples)
  945. color_fprintf(stdout, color, " %7" PRIu64,
  946. sample.nr_samples);
  947. else
  948. color_fprintf(stdout, color, " %7.2f", percent);
  949. }
  950. printf(" : ");
  951. br = block_range__find(addr);
  952. color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
  953. color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
  954. annotate__branch_printf(br, addr);
  955. printf("\n");
  956. if (ppercents != &percent)
  957. free(ppercents);
  958. if (psamples != &sample)
  959. free(psamples);
  960. } else if (max_lines && printed >= max_lines)
  961. return 1;
  962. else {
  963. int width = symbol_conf.show_total_period ? 12 : 8;
  964. if (queue)
  965. return -1;
  966. if (perf_evsel__is_group_event(evsel))
  967. width *= evsel->nr_members;
  968. if (!*dl->al.line)
  969. printf(" %*s:\n", width, " ");
  970. else
  971. printf(" %*s: %s\n", width, " ", dl->al.line);
  972. }
  973. return 0;
  974. }
  975. /*
  976. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  977. * which looks like following
  978. *
  979. * 0000000000415500 <_init>:
  980. * 415500: sub $0x8,%rsp
  981. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  982. * 41550b: test %rax,%rax
  983. * 41550e: je 415515 <_init+0x15>
  984. * 415510: callq 416e70 <__gmon_start__@plt>
  985. * 415515: add $0x8,%rsp
  986. * 415519: retq
  987. *
  988. * it will be parsed and saved into struct disasm_line as
  989. * <offset> <name> <ops.raw>
  990. *
  991. * The offset will be a relative offset from the start of the symbol and -1
  992. * means that it's not a disassembly line so should be treated differently.
  993. * The ops.raw part will be parsed further according to type of the instruction.
  994. */
  995. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  996. struct arch *arch, FILE *file,
  997. struct annotate_args *args,
  998. int *line_nr)
  999. {
  1000. struct annotation *notes = symbol__annotation(sym);
  1001. struct disasm_line *dl;
  1002. char *line = NULL, *parsed_line, *tmp, *tmp2;
  1003. size_t line_len;
  1004. s64 line_ip, offset = -1;
  1005. regmatch_t match[2];
  1006. if (getline(&line, &line_len, file) < 0)
  1007. return -1;
  1008. if (!line)
  1009. return -1;
  1010. line_ip = -1;
  1011. parsed_line = rtrim(line);
  1012. /* /filename:linenr ? Save line number and ignore. */
  1013. if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
  1014. *line_nr = atoi(parsed_line + match[1].rm_so);
  1015. return 0;
  1016. }
  1017. tmp = ltrim(parsed_line);
  1018. if (*tmp) {
  1019. /*
  1020. * Parse hexa addresses followed by ':'
  1021. */
  1022. line_ip = strtoull(tmp, &tmp2, 16);
  1023. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  1024. line_ip = -1;
  1025. }
  1026. if (line_ip != -1) {
  1027. u64 start = map__rip_2objdump(map, sym->start),
  1028. end = map__rip_2objdump(map, sym->end);
  1029. offset = line_ip - start;
  1030. if ((u64)line_ip < start || (u64)line_ip >= end)
  1031. offset = -1;
  1032. else
  1033. parsed_line = tmp2 + 1;
  1034. }
  1035. dl = disasm_line__new(args, offset, parsed_line, *line_nr, arch, map);
  1036. free(line);
  1037. (*line_nr)++;
  1038. if (dl == NULL)
  1039. return -1;
  1040. if (!disasm_line__has_offset(dl)) {
  1041. dl->ops.target.offset = dl->ops.target.addr -
  1042. map__rip_2objdump(map, sym->start);
  1043. dl->ops.target.offset_avail = true;
  1044. }
  1045. /* kcore has no symbols, so add the call target name */
  1046. if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
  1047. struct addr_map_symbol target = {
  1048. .map = map,
  1049. .addr = dl->ops.target.addr,
  1050. };
  1051. if (!map_groups__find_ams(&target) &&
  1052. target.sym->start == target.al_addr)
  1053. dl->ops.target.name = strdup(target.sym->name);
  1054. }
  1055. disasm__add(&notes->src->source, dl);
  1056. return 0;
  1057. }
  1058. static __attribute__((constructor)) void symbol__init_regexpr(void)
  1059. {
  1060. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  1061. }
  1062. static void delete_last_nop(struct symbol *sym)
  1063. {
  1064. struct annotation *notes = symbol__annotation(sym);
  1065. struct list_head *list = &notes->src->source;
  1066. struct disasm_line *dl;
  1067. while (!list_empty(list)) {
  1068. dl = list_entry(list->prev, struct disasm_line, al.node);
  1069. if (dl->ins.ops) {
  1070. if (dl->ins.ops != &nop_ops)
  1071. return;
  1072. } else {
  1073. if (!strstr(dl->al.line, " nop ") &&
  1074. !strstr(dl->al.line, " nopl ") &&
  1075. !strstr(dl->al.line, " nopw "))
  1076. return;
  1077. }
  1078. list_del(&dl->al.node);
  1079. disasm_line__free(dl);
  1080. }
  1081. }
  1082. int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
  1083. int errnum, char *buf, size_t buflen)
  1084. {
  1085. struct dso *dso = map->dso;
  1086. BUG_ON(buflen == 0);
  1087. if (errnum >= 0) {
  1088. str_error_r(errnum, buf, buflen);
  1089. return 0;
  1090. }
  1091. switch (errnum) {
  1092. case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
  1093. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  1094. char *build_id_msg = NULL;
  1095. if (dso->has_build_id) {
  1096. build_id__sprintf(dso->build_id,
  1097. sizeof(dso->build_id), bf + 15);
  1098. build_id_msg = bf;
  1099. }
  1100. scnprintf(buf, buflen,
  1101. "No vmlinux file%s\nwas found in the path.\n\n"
  1102. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  1103. "Please use:\n\n"
  1104. " perf buildid-cache -vu vmlinux\n\n"
  1105. "or:\n\n"
  1106. " --vmlinux vmlinux\n", build_id_msg ?: "");
  1107. }
  1108. break;
  1109. default:
  1110. scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
  1111. break;
  1112. }
  1113. return 0;
  1114. }
  1115. static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
  1116. {
  1117. char linkname[PATH_MAX];
  1118. char *build_id_filename;
  1119. char *build_id_path = NULL;
  1120. char *pos;
  1121. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  1122. !dso__is_kcore(dso))
  1123. return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
  1124. build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
  1125. if (build_id_filename) {
  1126. __symbol__join_symfs(filename, filename_size, build_id_filename);
  1127. free(build_id_filename);
  1128. } else {
  1129. if (dso->has_build_id)
  1130. return ENOMEM;
  1131. goto fallback;
  1132. }
  1133. build_id_path = strdup(filename);
  1134. if (!build_id_path)
  1135. return -1;
  1136. /*
  1137. * old style build-id cache has name of XX/XXXXXXX.. while
  1138. * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
  1139. * extract the build-id part of dirname in the new style only.
  1140. */
  1141. pos = strrchr(build_id_path, '/');
  1142. if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
  1143. dirname(build_id_path);
  1144. if (dso__is_kcore(dso) ||
  1145. readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
  1146. strstr(linkname, DSO__NAME_KALLSYMS) ||
  1147. access(filename, R_OK)) {
  1148. fallback:
  1149. /*
  1150. * If we don't have build-ids or the build-id file isn't in the
  1151. * cache, or is just a kallsyms file, well, lets hope that this
  1152. * DSO is the same as when 'perf record' ran.
  1153. */
  1154. __symbol__join_symfs(filename, filename_size, dso->long_name);
  1155. }
  1156. free(build_id_path);
  1157. return 0;
  1158. }
  1159. static const char *annotate__norm_arch(const char *arch_name)
  1160. {
  1161. struct utsname uts;
  1162. if (!arch_name) { /* Assume we are annotating locally. */
  1163. if (uname(&uts) < 0)
  1164. return NULL;
  1165. arch_name = uts.machine;
  1166. }
  1167. return normalize_arch((char *)arch_name);
  1168. }
  1169. static int symbol__disassemble(struct symbol *sym, struct map *map,
  1170. struct annotate_args *args,
  1171. struct arch *arch)
  1172. {
  1173. struct dso *dso = map->dso;
  1174. char command[PATH_MAX * 2];
  1175. FILE *file;
  1176. char symfs_filename[PATH_MAX];
  1177. struct kcore_extract kce;
  1178. bool delete_extract = false;
  1179. int stdout_fd[2];
  1180. int lineno = 0;
  1181. int nline;
  1182. pid_t pid;
  1183. int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
  1184. if (err)
  1185. return err;
  1186. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  1187. symfs_filename, sym->name, map->unmap_ip(map, sym->start),
  1188. map->unmap_ip(map, sym->end));
  1189. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  1190. dso, dso->long_name, sym, sym->name);
  1191. if (dso__is_kcore(dso)) {
  1192. kce.kcore_filename = symfs_filename;
  1193. kce.addr = map__rip_2objdump(map, sym->start);
  1194. kce.offs = sym->start;
  1195. kce.len = sym->end - sym->start;
  1196. if (!kcore_extract__create(&kce)) {
  1197. delete_extract = true;
  1198. strlcpy(symfs_filename, kce.extract_filename,
  1199. sizeof(symfs_filename));
  1200. }
  1201. } else if (dso__needs_decompress(dso)) {
  1202. char tmp[KMOD_DECOMP_LEN];
  1203. if (dso__decompress_kmodule_path(dso, symfs_filename,
  1204. tmp, sizeof(tmp)) < 0)
  1205. goto out;
  1206. strcpy(symfs_filename, tmp);
  1207. }
  1208. snprintf(command, sizeof(command),
  1209. "%s %s%s --start-address=0x%016" PRIx64
  1210. " --stop-address=0x%016" PRIx64
  1211. " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
  1212. objdump_path ? objdump_path : "objdump",
  1213. disassembler_style ? "-M " : "",
  1214. disassembler_style ? disassembler_style : "",
  1215. map__rip_2objdump(map, sym->start),
  1216. map__rip_2objdump(map, sym->end),
  1217. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  1218. symbol_conf.annotate_src ? "-S" : "",
  1219. symfs_filename, symfs_filename);
  1220. pr_debug("Executing: %s\n", command);
  1221. err = -1;
  1222. if (pipe(stdout_fd) < 0) {
  1223. pr_err("Failure creating the pipe to run %s\n", command);
  1224. goto out_remove_tmp;
  1225. }
  1226. pid = fork();
  1227. if (pid < 0) {
  1228. pr_err("Failure forking to run %s\n", command);
  1229. goto out_close_stdout;
  1230. }
  1231. if (pid == 0) {
  1232. close(stdout_fd[0]);
  1233. dup2(stdout_fd[1], 1);
  1234. close(stdout_fd[1]);
  1235. execl("/bin/sh", "sh", "-c", command, NULL);
  1236. perror(command);
  1237. exit(-1);
  1238. }
  1239. close(stdout_fd[1]);
  1240. file = fdopen(stdout_fd[0], "r");
  1241. if (!file) {
  1242. pr_err("Failure creating FILE stream for %s\n", command);
  1243. /*
  1244. * If we were using debug info should retry with
  1245. * original binary.
  1246. */
  1247. goto out_remove_tmp;
  1248. }
  1249. nline = 0;
  1250. while (!feof(file)) {
  1251. /*
  1252. * The source code line number (lineno) needs to be kept in
  1253. * accross calls to symbol__parse_objdump_line(), so that it
  1254. * can associate it with the instructions till the next one.
  1255. * See disasm_line__new() and struct disasm_line::line_nr.
  1256. */
  1257. if (symbol__parse_objdump_line(sym, map, arch, file, args,
  1258. &lineno) < 0)
  1259. break;
  1260. nline++;
  1261. }
  1262. if (nline == 0)
  1263. pr_err("No output from %s\n", command);
  1264. /*
  1265. * kallsyms does not have symbol sizes so there may a nop at the end.
  1266. * Remove it.
  1267. */
  1268. if (dso__is_kcore(dso))
  1269. delete_last_nop(sym);
  1270. fclose(file);
  1271. err = 0;
  1272. out_remove_tmp:
  1273. close(stdout_fd[0]);
  1274. if (dso__needs_decompress(dso))
  1275. unlink(symfs_filename);
  1276. if (delete_extract)
  1277. kcore_extract__delete(&kce);
  1278. out:
  1279. return err;
  1280. out_close_stdout:
  1281. close(stdout_fd[1]);
  1282. goto out_remove_tmp;
  1283. }
  1284. int symbol__annotate(struct symbol *sym, struct map *map,
  1285. const char *arch_name, size_t privsize,
  1286. struct arch **parch, char *cpuid)
  1287. {
  1288. struct annotate_args args = {
  1289. .privsize = privsize,
  1290. };
  1291. struct arch *arch;
  1292. int err;
  1293. arch_name = annotate__norm_arch(arch_name);
  1294. if (!arch_name)
  1295. return -1;
  1296. arch = arch__find(arch_name);
  1297. if (arch == NULL)
  1298. return -ENOTSUP;
  1299. if (parch)
  1300. *parch = arch;
  1301. if (arch->init) {
  1302. err = arch->init(arch, cpuid);
  1303. if (err) {
  1304. pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
  1305. return err;
  1306. }
  1307. }
  1308. return symbol__disassemble(sym, map, &args, arch);
  1309. }
  1310. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  1311. {
  1312. struct source_line *iter;
  1313. struct rb_node **p = &root->rb_node;
  1314. struct rb_node *parent = NULL;
  1315. int i, ret;
  1316. while (*p != NULL) {
  1317. parent = *p;
  1318. iter = rb_entry(parent, struct source_line, node);
  1319. ret = strcmp(iter->path, src_line->path);
  1320. if (ret == 0) {
  1321. for (i = 0; i < src_line->nr_pcnt; i++)
  1322. iter->samples[i].percent_sum += src_line->samples[i].percent;
  1323. return;
  1324. }
  1325. if (ret < 0)
  1326. p = &(*p)->rb_left;
  1327. else
  1328. p = &(*p)->rb_right;
  1329. }
  1330. for (i = 0; i < src_line->nr_pcnt; i++)
  1331. src_line->samples[i].percent_sum = src_line->samples[i].percent;
  1332. rb_link_node(&src_line->node, parent, p);
  1333. rb_insert_color(&src_line->node, root);
  1334. }
  1335. static int cmp_source_line(struct source_line *a, struct source_line *b)
  1336. {
  1337. int i;
  1338. for (i = 0; i < a->nr_pcnt; i++) {
  1339. if (a->samples[i].percent_sum == b->samples[i].percent_sum)
  1340. continue;
  1341. return a->samples[i].percent_sum > b->samples[i].percent_sum;
  1342. }
  1343. return 0;
  1344. }
  1345. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  1346. {
  1347. struct source_line *iter;
  1348. struct rb_node **p = &root->rb_node;
  1349. struct rb_node *parent = NULL;
  1350. while (*p != NULL) {
  1351. parent = *p;
  1352. iter = rb_entry(parent, struct source_line, node);
  1353. if (cmp_source_line(src_line, iter))
  1354. p = &(*p)->rb_left;
  1355. else
  1356. p = &(*p)->rb_right;
  1357. }
  1358. rb_link_node(&src_line->node, parent, p);
  1359. rb_insert_color(&src_line->node, root);
  1360. }
  1361. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1362. {
  1363. struct source_line *src_line;
  1364. struct rb_node *node;
  1365. node = rb_first(src_root);
  1366. while (node) {
  1367. struct rb_node *next;
  1368. src_line = rb_entry(node, struct source_line, node);
  1369. next = rb_next(node);
  1370. rb_erase(node, src_root);
  1371. __resort_source_line(dest_root, src_line);
  1372. node = next;
  1373. }
  1374. }
  1375. static void symbol__free_source_line(struct symbol *sym, int len)
  1376. {
  1377. struct annotation *notes = symbol__annotation(sym);
  1378. struct source_line *src_line = notes->src->lines;
  1379. size_t sizeof_src_line;
  1380. int i;
  1381. sizeof_src_line = sizeof(*src_line) +
  1382. (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
  1383. for (i = 0; i < len; i++) {
  1384. free_srcline(src_line->path);
  1385. src_line = (void *)src_line + sizeof_src_line;
  1386. }
  1387. zfree(&notes->src->lines);
  1388. }
  1389. /* Get the filename:line for the colored entries */
  1390. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  1391. struct perf_evsel *evsel,
  1392. struct rb_root *root, int len)
  1393. {
  1394. u64 start;
  1395. int i, k;
  1396. int evidx = evsel->idx;
  1397. struct source_line *src_line;
  1398. struct annotation *notes = symbol__annotation(sym);
  1399. struct sym_hist *h = annotation__histogram(notes, evidx);
  1400. struct rb_root tmp_root = RB_ROOT;
  1401. int nr_pcnt = 1;
  1402. u64 nr_samples = h->nr_samples;
  1403. size_t sizeof_src_line = sizeof(struct source_line);
  1404. if (perf_evsel__is_group_event(evsel)) {
  1405. for (i = 1; i < evsel->nr_members; i++) {
  1406. h = annotation__histogram(notes, evidx + i);
  1407. nr_samples += h->nr_samples;
  1408. }
  1409. nr_pcnt = evsel->nr_members;
  1410. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
  1411. }
  1412. if (!nr_samples)
  1413. return 0;
  1414. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  1415. if (!notes->src->lines)
  1416. return -1;
  1417. start = map__rip_2objdump(map, sym->start);
  1418. for (i = 0; i < len; i++) {
  1419. u64 offset;
  1420. double percent_max = 0.0;
  1421. src_line->nr_pcnt = nr_pcnt;
  1422. for (k = 0; k < nr_pcnt; k++) {
  1423. double percent = 0.0;
  1424. h = annotation__histogram(notes, evidx + k);
  1425. nr_samples = h->addr[i].nr_samples;
  1426. if (h->nr_samples)
  1427. percent = 100.0 * nr_samples / h->nr_samples;
  1428. if (percent > percent_max)
  1429. percent_max = percent;
  1430. src_line->samples[k].percent = percent;
  1431. src_line->samples[k].nr = nr_samples;
  1432. }
  1433. if (percent_max <= 0.5)
  1434. goto next;
  1435. offset = start + i;
  1436. src_line->path = get_srcline(map->dso, offset, NULL,
  1437. false, true);
  1438. insert_source_line(&tmp_root, src_line);
  1439. next:
  1440. src_line = (void *)src_line + sizeof_src_line;
  1441. }
  1442. resort_source_line(root, &tmp_root);
  1443. return 0;
  1444. }
  1445. static void print_summary(struct rb_root *root, const char *filename)
  1446. {
  1447. struct source_line *src_line;
  1448. struct rb_node *node;
  1449. printf("\nSorted summary for file %s\n", filename);
  1450. printf("----------------------------------------------\n\n");
  1451. if (RB_EMPTY_ROOT(root)) {
  1452. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1453. return;
  1454. }
  1455. node = rb_first(root);
  1456. while (node) {
  1457. double percent, percent_max = 0.0;
  1458. const char *color;
  1459. char *path;
  1460. int i;
  1461. src_line = rb_entry(node, struct source_line, node);
  1462. for (i = 0; i < src_line->nr_pcnt; i++) {
  1463. percent = src_line->samples[i].percent_sum;
  1464. color = get_percent_color(percent);
  1465. color_fprintf(stdout, color, " %7.2f", percent);
  1466. if (percent > percent_max)
  1467. percent_max = percent;
  1468. }
  1469. path = src_line->path;
  1470. color = get_percent_color(percent_max);
  1471. color_fprintf(stdout, color, " %s\n", path);
  1472. node = rb_next(node);
  1473. }
  1474. }
  1475. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1476. {
  1477. struct annotation *notes = symbol__annotation(sym);
  1478. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1479. u64 len = symbol__size(sym), offset;
  1480. for (offset = 0; offset < len; ++offset)
  1481. if (h->addr[offset].nr_samples != 0)
  1482. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1483. sym->start + offset, h->addr[offset].nr_samples);
  1484. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
  1485. }
  1486. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1487. struct perf_evsel *evsel, bool full_paths,
  1488. int min_pcnt, int max_lines, int context)
  1489. {
  1490. struct dso *dso = map->dso;
  1491. char *filename;
  1492. const char *d_filename;
  1493. const char *evsel_name = perf_evsel__name(evsel);
  1494. struct annotation *notes = symbol__annotation(sym);
  1495. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1496. struct disasm_line *pos, *queue = NULL;
  1497. u64 start = map__rip_2objdump(map, sym->start);
  1498. int printed = 2, queue_len = 0;
  1499. int more = 0;
  1500. u64 len;
  1501. int width = symbol_conf.show_total_period ? 12 : 8;
  1502. int graph_dotted_len;
  1503. filename = strdup(dso->long_name);
  1504. if (!filename)
  1505. return -ENOMEM;
  1506. if (full_paths)
  1507. d_filename = filename;
  1508. else
  1509. d_filename = basename(filename);
  1510. len = symbol__size(sym);
  1511. if (perf_evsel__is_group_event(evsel))
  1512. width *= evsel->nr_members;
  1513. graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
  1514. width, width, symbol_conf.show_total_period ? "Period" :
  1515. symbol_conf.show_nr_samples ? "Samples" : "Percent",
  1516. d_filename, evsel_name, h->nr_samples);
  1517. printf("%-*.*s----\n",
  1518. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1519. if (verbose > 0)
  1520. symbol__annotate_hits(sym, evsel);
  1521. list_for_each_entry(pos, &notes->src->source, al.node) {
  1522. if (context && queue == NULL) {
  1523. queue = pos;
  1524. queue_len = 0;
  1525. }
  1526. switch (disasm_line__print(pos, sym, start, evsel, len,
  1527. min_pcnt, printed, max_lines,
  1528. queue)) {
  1529. case 0:
  1530. ++printed;
  1531. if (context) {
  1532. printed += queue_len;
  1533. queue = NULL;
  1534. queue_len = 0;
  1535. }
  1536. break;
  1537. case 1:
  1538. /* filtered by max_lines */
  1539. ++more;
  1540. break;
  1541. case -1:
  1542. default:
  1543. /*
  1544. * Filtered by min_pcnt or non IP lines when
  1545. * context != 0
  1546. */
  1547. if (!context)
  1548. break;
  1549. if (queue_len == context)
  1550. queue = list_entry(queue->al.node.next, typeof(*queue), al.node);
  1551. else
  1552. ++queue_len;
  1553. break;
  1554. }
  1555. }
  1556. free(filename);
  1557. return more;
  1558. }
  1559. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1560. {
  1561. struct annotation *notes = symbol__annotation(sym);
  1562. struct sym_hist *h = annotation__histogram(notes, evidx);
  1563. memset(h, 0, notes->src->sizeof_sym_hist);
  1564. }
  1565. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1566. {
  1567. struct annotation *notes = symbol__annotation(sym);
  1568. struct sym_hist *h = annotation__histogram(notes, evidx);
  1569. int len = symbol__size(sym), offset;
  1570. h->nr_samples = 0;
  1571. for (offset = 0; offset < len; ++offset) {
  1572. h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
  1573. h->nr_samples += h->addr[offset].nr_samples;
  1574. }
  1575. }
  1576. void disasm__purge(struct list_head *head)
  1577. {
  1578. struct disasm_line *pos, *n;
  1579. list_for_each_entry_safe(pos, n, head, al.node) {
  1580. list_del(&pos->al.node);
  1581. disasm_line__free(pos);
  1582. }
  1583. }
  1584. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1585. {
  1586. size_t printed;
  1587. if (dl->al.offset == -1)
  1588. return fprintf(fp, "%s\n", dl->al.line);
  1589. printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
  1590. if (dl->ops.raw[0] != '\0') {
  1591. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1592. dl->ops.raw);
  1593. }
  1594. return printed + fprintf(fp, "\n");
  1595. }
  1596. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1597. {
  1598. struct disasm_line *pos;
  1599. size_t printed = 0;
  1600. list_for_each_entry(pos, head, al.node)
  1601. printed += disasm_line__fprintf(pos, fp);
  1602. return printed;
  1603. }
  1604. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1605. struct perf_evsel *evsel, bool print_lines,
  1606. bool full_paths, int min_pcnt, int max_lines)
  1607. {
  1608. struct dso *dso = map->dso;
  1609. struct rb_root source_line = RB_ROOT;
  1610. u64 len;
  1611. if (symbol__annotate(sym, map, perf_evsel__env_arch(evsel),
  1612. 0, NULL, NULL) < 0)
  1613. return -1;
  1614. len = symbol__size(sym);
  1615. if (print_lines) {
  1616. srcline_full_filename = full_paths;
  1617. symbol__get_source_line(sym, map, evsel, &source_line, len);
  1618. print_summary(&source_line, dso->long_name);
  1619. }
  1620. symbol__annotate_printf(sym, map, evsel, full_paths,
  1621. min_pcnt, max_lines, 0);
  1622. if (print_lines)
  1623. symbol__free_source_line(sym, len);
  1624. disasm__purge(&symbol__annotation(sym)->src->source);
  1625. return 0;
  1626. }
  1627. bool ui__has_annotation(void)
  1628. {
  1629. return use_browser == 1 && perf_hpp_list.sym;
  1630. }