annotate.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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 "util.h"
  10. #include "build-id.h"
  11. #include "color.h"
  12. #include "cache.h"
  13. #include "symbol.h"
  14. #include "debug.h"
  15. #include "annotate.h"
  16. #include "evsel.h"
  17. #include <pthread.h>
  18. #include <linux/bitops.h>
  19. const char *disassembler_style;
  20. const char *objdump_path;
  21. static struct ins *ins__find(const char *name);
  22. static int disasm_line__parse(char *line, char **namep, char **rawp);
  23. static void ins__delete(struct ins_operands *ops)
  24. {
  25. free(ops->source.raw);
  26. free(ops->source.name);
  27. free(ops->target.raw);
  28. free(ops->target.name);
  29. }
  30. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  31. struct ins_operands *ops)
  32. {
  33. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
  34. }
  35. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  36. struct ins_operands *ops)
  37. {
  38. if (ins->ops->scnprintf)
  39. return ins->ops->scnprintf(ins, bf, size, ops);
  40. return ins__raw_scnprintf(ins, bf, size, ops);
  41. }
  42. static int call__parse(struct ins_operands *ops)
  43. {
  44. char *endptr, *tok, *name;
  45. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  46. name = strchr(endptr, '<');
  47. if (name == NULL)
  48. goto indirect_call;
  49. name++;
  50. tok = strchr(name, '>');
  51. if (tok == NULL)
  52. return -1;
  53. *tok = '\0';
  54. ops->target.name = strdup(name);
  55. *tok = '>';
  56. return ops->target.name == NULL ? -1 : 0;
  57. indirect_call:
  58. tok = strchr(endptr, '(');
  59. if (tok != NULL) {
  60. ops->target.addr = 0;
  61. return 0;
  62. }
  63. tok = strchr(endptr, '*');
  64. if (tok == NULL)
  65. return -1;
  66. ops->target.addr = strtoull(tok + 1, NULL, 16);
  67. return 0;
  68. }
  69. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  70. struct ins_operands *ops)
  71. {
  72. if (ops->target.name)
  73. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
  74. if (ops->target.addr == 0)
  75. return ins__raw_scnprintf(ins, bf, size, ops);
  76. return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
  77. }
  78. static struct ins_ops call_ops = {
  79. .parse = call__parse,
  80. .scnprintf = call__scnprintf,
  81. };
  82. bool ins__is_call(const struct ins *ins)
  83. {
  84. return ins->ops == &call_ops;
  85. }
  86. static int jump__parse(struct ins_operands *ops)
  87. {
  88. const char *s = strchr(ops->raw, '+');
  89. ops->target.addr = strtoull(ops->raw, NULL, 16);
  90. if (s++ != NULL)
  91. ops->target.offset = strtoull(s, NULL, 16);
  92. else
  93. ops->target.offset = UINT64_MAX;
  94. return 0;
  95. }
  96. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  97. struct ins_operands *ops)
  98. {
  99. return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
  100. }
  101. static struct ins_ops jump_ops = {
  102. .parse = jump__parse,
  103. .scnprintf = jump__scnprintf,
  104. };
  105. bool ins__is_jump(const struct ins *ins)
  106. {
  107. return ins->ops == &jump_ops;
  108. }
  109. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  110. {
  111. char *endptr, *name, *t;
  112. if (strstr(raw, "(%rip)") == NULL)
  113. return 0;
  114. *addrp = strtoull(comment, &endptr, 16);
  115. name = strchr(endptr, '<');
  116. if (name == NULL)
  117. return -1;
  118. name++;
  119. t = strchr(name, '>');
  120. if (t == NULL)
  121. return 0;
  122. *t = '\0';
  123. *namep = strdup(name);
  124. *t = '>';
  125. return 0;
  126. }
  127. static int lock__parse(struct ins_operands *ops)
  128. {
  129. char *name;
  130. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  131. if (ops->locked.ops == NULL)
  132. return 0;
  133. if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
  134. goto out_free_ops;
  135. ops->locked.ins = ins__find(name);
  136. if (ops->locked.ins == NULL)
  137. goto out_free_ops;
  138. if (!ops->locked.ins->ops)
  139. return 0;
  140. if (ops->locked.ins->ops->parse)
  141. ops->locked.ins->ops->parse(ops->locked.ops);
  142. return 0;
  143. out_free_ops:
  144. free(ops->locked.ops);
  145. ops->locked.ops = NULL;
  146. return 0;
  147. }
  148. static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
  149. struct ins_operands *ops)
  150. {
  151. int printed;
  152. if (ops->locked.ins == NULL)
  153. return ins__raw_scnprintf(ins, bf, size, ops);
  154. printed = scnprintf(bf, size, "%-6.6s ", ins->name);
  155. return printed + ins__scnprintf(ops->locked.ins, bf + printed,
  156. size - printed, ops->locked.ops);
  157. }
  158. static void lock__delete(struct ins_operands *ops)
  159. {
  160. free(ops->locked.ops);
  161. free(ops->target.raw);
  162. free(ops->target.name);
  163. }
  164. static struct ins_ops lock_ops = {
  165. .free = lock__delete,
  166. .parse = lock__parse,
  167. .scnprintf = lock__scnprintf,
  168. };
  169. static int mov__parse(struct ins_operands *ops)
  170. {
  171. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  172. if (s == NULL)
  173. return -1;
  174. *s = '\0';
  175. ops->source.raw = strdup(ops->raw);
  176. *s = ',';
  177. if (ops->source.raw == NULL)
  178. return -1;
  179. target = ++s;
  180. while (s[0] != '\0' && !isspace(s[0]))
  181. ++s;
  182. prev = *s;
  183. *s = '\0';
  184. ops->target.raw = strdup(target);
  185. *s = prev;
  186. if (ops->target.raw == NULL)
  187. goto out_free_source;
  188. comment = strchr(s, '#');
  189. if (comment == NULL)
  190. return 0;
  191. while (comment[0] != '\0' && isspace(comment[0]))
  192. ++comment;
  193. comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
  194. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  195. return 0;
  196. out_free_source:
  197. free(ops->source.raw);
  198. ops->source.raw = NULL;
  199. return -1;
  200. }
  201. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  202. struct ins_operands *ops)
  203. {
  204. return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
  205. ops->source.name ?: ops->source.raw,
  206. ops->target.name ?: ops->target.raw);
  207. }
  208. static struct ins_ops mov_ops = {
  209. .parse = mov__parse,
  210. .scnprintf = mov__scnprintf,
  211. };
  212. static int dec__parse(struct ins_operands *ops)
  213. {
  214. char *target, *comment, *s, prev;
  215. target = s = ops->raw;
  216. while (s[0] != '\0' && !isspace(s[0]))
  217. ++s;
  218. prev = *s;
  219. *s = '\0';
  220. ops->target.raw = strdup(target);
  221. *s = prev;
  222. if (ops->target.raw == NULL)
  223. return -1;
  224. comment = strchr(s, '#');
  225. if (comment == NULL)
  226. return 0;
  227. while (comment[0] != '\0' && isspace(comment[0]))
  228. ++comment;
  229. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  230. return 0;
  231. }
  232. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  233. struct ins_operands *ops)
  234. {
  235. return scnprintf(bf, size, "%-6.6s %s", ins->name,
  236. ops->target.name ?: ops->target.raw);
  237. }
  238. static struct ins_ops dec_ops = {
  239. .parse = dec__parse,
  240. .scnprintf = dec__scnprintf,
  241. };
  242. static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
  243. struct ins_operands *ops __maybe_unused)
  244. {
  245. return scnprintf(bf, size, "%-6.6s", "nop");
  246. }
  247. static struct ins_ops nop_ops = {
  248. .scnprintf = nop__scnprintf,
  249. };
  250. /*
  251. * Must be sorted by name!
  252. */
  253. static struct ins instructions[] = {
  254. { .name = "add", .ops = &mov_ops, },
  255. { .name = "addl", .ops = &mov_ops, },
  256. { .name = "addq", .ops = &mov_ops, },
  257. { .name = "addw", .ops = &mov_ops, },
  258. { .name = "and", .ops = &mov_ops, },
  259. { .name = "bts", .ops = &mov_ops, },
  260. { .name = "call", .ops = &call_ops, },
  261. { .name = "callq", .ops = &call_ops, },
  262. { .name = "cmp", .ops = &mov_ops, },
  263. { .name = "cmpb", .ops = &mov_ops, },
  264. { .name = "cmpl", .ops = &mov_ops, },
  265. { .name = "cmpq", .ops = &mov_ops, },
  266. { .name = "cmpw", .ops = &mov_ops, },
  267. { .name = "cmpxch", .ops = &mov_ops, },
  268. { .name = "dec", .ops = &dec_ops, },
  269. { .name = "decl", .ops = &dec_ops, },
  270. { .name = "imul", .ops = &mov_ops, },
  271. { .name = "inc", .ops = &dec_ops, },
  272. { .name = "incl", .ops = &dec_ops, },
  273. { .name = "ja", .ops = &jump_ops, },
  274. { .name = "jae", .ops = &jump_ops, },
  275. { .name = "jb", .ops = &jump_ops, },
  276. { .name = "jbe", .ops = &jump_ops, },
  277. { .name = "jc", .ops = &jump_ops, },
  278. { .name = "jcxz", .ops = &jump_ops, },
  279. { .name = "je", .ops = &jump_ops, },
  280. { .name = "jecxz", .ops = &jump_ops, },
  281. { .name = "jg", .ops = &jump_ops, },
  282. { .name = "jge", .ops = &jump_ops, },
  283. { .name = "jl", .ops = &jump_ops, },
  284. { .name = "jle", .ops = &jump_ops, },
  285. { .name = "jmp", .ops = &jump_ops, },
  286. { .name = "jmpq", .ops = &jump_ops, },
  287. { .name = "jna", .ops = &jump_ops, },
  288. { .name = "jnae", .ops = &jump_ops, },
  289. { .name = "jnb", .ops = &jump_ops, },
  290. { .name = "jnbe", .ops = &jump_ops, },
  291. { .name = "jnc", .ops = &jump_ops, },
  292. { .name = "jne", .ops = &jump_ops, },
  293. { .name = "jng", .ops = &jump_ops, },
  294. { .name = "jnge", .ops = &jump_ops, },
  295. { .name = "jnl", .ops = &jump_ops, },
  296. { .name = "jnle", .ops = &jump_ops, },
  297. { .name = "jno", .ops = &jump_ops, },
  298. { .name = "jnp", .ops = &jump_ops, },
  299. { .name = "jns", .ops = &jump_ops, },
  300. { .name = "jnz", .ops = &jump_ops, },
  301. { .name = "jo", .ops = &jump_ops, },
  302. { .name = "jp", .ops = &jump_ops, },
  303. { .name = "jpe", .ops = &jump_ops, },
  304. { .name = "jpo", .ops = &jump_ops, },
  305. { .name = "jrcxz", .ops = &jump_ops, },
  306. { .name = "js", .ops = &jump_ops, },
  307. { .name = "jz", .ops = &jump_ops, },
  308. { .name = "lea", .ops = &mov_ops, },
  309. { .name = "lock", .ops = &lock_ops, },
  310. { .name = "mov", .ops = &mov_ops, },
  311. { .name = "movb", .ops = &mov_ops, },
  312. { .name = "movdqa",.ops = &mov_ops, },
  313. { .name = "movl", .ops = &mov_ops, },
  314. { .name = "movq", .ops = &mov_ops, },
  315. { .name = "movslq", .ops = &mov_ops, },
  316. { .name = "movzbl", .ops = &mov_ops, },
  317. { .name = "movzwl", .ops = &mov_ops, },
  318. { .name = "nop", .ops = &nop_ops, },
  319. { .name = "nopl", .ops = &nop_ops, },
  320. { .name = "nopw", .ops = &nop_ops, },
  321. { .name = "or", .ops = &mov_ops, },
  322. { .name = "orl", .ops = &mov_ops, },
  323. { .name = "test", .ops = &mov_ops, },
  324. { .name = "testb", .ops = &mov_ops, },
  325. { .name = "testl", .ops = &mov_ops, },
  326. { .name = "xadd", .ops = &mov_ops, },
  327. { .name = "xbeginl", .ops = &jump_ops, },
  328. { .name = "xbeginq", .ops = &jump_ops, },
  329. };
  330. static int ins__cmp(const void *name, const void *insp)
  331. {
  332. const struct ins *ins = insp;
  333. return strcmp(name, ins->name);
  334. }
  335. static struct ins *ins__find(const char *name)
  336. {
  337. const int nmemb = ARRAY_SIZE(instructions);
  338. return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
  339. }
  340. int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
  341. {
  342. struct annotation *notes = symbol__annotation(sym);
  343. pthread_mutex_init(&notes->lock, NULL);
  344. return 0;
  345. }
  346. int symbol__alloc_hist(struct symbol *sym)
  347. {
  348. struct annotation *notes = symbol__annotation(sym);
  349. const size_t size = symbol__size(sym);
  350. size_t sizeof_sym_hist;
  351. /* Check for overflow when calculating sizeof_sym_hist */
  352. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
  353. return -1;
  354. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
  355. /* Check for overflow in zalloc argument */
  356. if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
  357. / symbol_conf.nr_events)
  358. return -1;
  359. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  360. if (notes->src == NULL)
  361. return -1;
  362. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  363. notes->src->nr_histograms = symbol_conf.nr_events;
  364. INIT_LIST_HEAD(&notes->src->source);
  365. return 0;
  366. }
  367. void symbol__annotate_zero_histograms(struct symbol *sym)
  368. {
  369. struct annotation *notes = symbol__annotation(sym);
  370. pthread_mutex_lock(&notes->lock);
  371. if (notes->src != NULL)
  372. memset(notes->src->histograms, 0,
  373. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  374. pthread_mutex_unlock(&notes->lock);
  375. }
  376. int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  377. int evidx, u64 addr)
  378. {
  379. unsigned offset;
  380. struct annotation *notes;
  381. struct sym_hist *h;
  382. notes = symbol__annotation(sym);
  383. if (notes->src == NULL)
  384. return -ENOMEM;
  385. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  386. if (addr < sym->start || addr > sym->end)
  387. return -ERANGE;
  388. offset = addr - sym->start;
  389. h = annotation__histogram(notes, evidx);
  390. h->sum++;
  391. h->addr[offset]++;
  392. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  393. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  394. addr, addr - sym->start, evidx, h->addr[offset]);
  395. return 0;
  396. }
  397. static void disasm_line__init_ins(struct disasm_line *dl)
  398. {
  399. dl->ins = ins__find(dl->name);
  400. if (dl->ins == NULL)
  401. return;
  402. if (!dl->ins->ops)
  403. return;
  404. if (dl->ins->ops->parse)
  405. dl->ins->ops->parse(&dl->ops);
  406. }
  407. static int disasm_line__parse(char *line, char **namep, char **rawp)
  408. {
  409. char *name = line, tmp;
  410. while (isspace(name[0]))
  411. ++name;
  412. if (name[0] == '\0')
  413. return -1;
  414. *rawp = name + 1;
  415. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  416. ++*rawp;
  417. tmp = (*rawp)[0];
  418. (*rawp)[0] = '\0';
  419. *namep = strdup(name);
  420. if (*namep == NULL)
  421. goto out_free_name;
  422. (*rawp)[0] = tmp;
  423. if ((*rawp)[0] != '\0') {
  424. (*rawp)++;
  425. while (isspace((*rawp)[0]))
  426. ++(*rawp);
  427. }
  428. return 0;
  429. out_free_name:
  430. free(*namep);
  431. *namep = NULL;
  432. return -1;
  433. }
  434. static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
  435. {
  436. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  437. if (dl != NULL) {
  438. dl->offset = offset;
  439. dl->line = strdup(line);
  440. if (dl->line == NULL)
  441. goto out_delete;
  442. if (offset != -1) {
  443. if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
  444. goto out_free_line;
  445. disasm_line__init_ins(dl);
  446. }
  447. }
  448. return dl;
  449. out_free_line:
  450. free(dl->line);
  451. out_delete:
  452. free(dl);
  453. return NULL;
  454. }
  455. void disasm_line__free(struct disasm_line *dl)
  456. {
  457. free(dl->line);
  458. free(dl->name);
  459. if (dl->ins && dl->ins->ops->free)
  460. dl->ins->ops->free(&dl->ops);
  461. else
  462. ins__delete(&dl->ops);
  463. free(dl);
  464. }
  465. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  466. {
  467. if (raw || !dl->ins)
  468. return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
  469. return ins__scnprintf(dl->ins, bf, size, &dl->ops);
  470. }
  471. static void disasm__add(struct list_head *head, struct disasm_line *line)
  472. {
  473. list_add_tail(&line->node, head);
  474. }
  475. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  476. {
  477. list_for_each_entry_continue(pos, head, node)
  478. if (pos->offset >= 0)
  479. return pos;
  480. return NULL;
  481. }
  482. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  483. s64 end, const char **path)
  484. {
  485. struct source_line *src_line = notes->src->lines;
  486. double percent = 0.0;
  487. if (src_line) {
  488. size_t sizeof_src_line = sizeof(*src_line) +
  489. sizeof(src_line->p) * (src_line->nr_pcnt - 1);
  490. while (offset < end) {
  491. src_line = (void *)notes->src->lines +
  492. (sizeof_src_line * offset);
  493. if (*path == NULL)
  494. *path = src_line->path;
  495. percent += src_line->p[evidx].percent;
  496. offset++;
  497. }
  498. } else {
  499. struct sym_hist *h = annotation__histogram(notes, evidx);
  500. unsigned int hits = 0;
  501. while (offset < end)
  502. hits += h->addr[offset++];
  503. if (h->sum)
  504. percent = 100.0 * hits / h->sum;
  505. }
  506. return percent;
  507. }
  508. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  509. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  510. int max_lines, struct disasm_line *queue)
  511. {
  512. static const char *prev_line;
  513. static const char *prev_color;
  514. if (dl->offset != -1) {
  515. const char *path = NULL;
  516. double percent, max_percent = 0.0;
  517. double *ppercents = &percent;
  518. int i, nr_percent = 1;
  519. const char *color;
  520. struct annotation *notes = symbol__annotation(sym);
  521. s64 offset = dl->offset;
  522. const u64 addr = start + offset;
  523. struct disasm_line *next;
  524. next = disasm__get_next_ip_line(&notes->src->source, dl);
  525. if (perf_evsel__is_group_event(evsel)) {
  526. nr_percent = evsel->nr_members;
  527. ppercents = calloc(nr_percent, sizeof(double));
  528. if (ppercents == NULL)
  529. return -1;
  530. }
  531. for (i = 0; i < nr_percent; i++) {
  532. percent = disasm__calc_percent(notes,
  533. notes->src->lines ? i : evsel->idx + i,
  534. offset,
  535. next ? next->offset : (s64) len,
  536. &path);
  537. ppercents[i] = percent;
  538. if (percent > max_percent)
  539. max_percent = percent;
  540. }
  541. if (max_percent < min_pcnt)
  542. return -1;
  543. if (max_lines && printed >= max_lines)
  544. return 1;
  545. if (queue != NULL) {
  546. list_for_each_entry_from(queue, &notes->src->source, node) {
  547. if (queue == dl)
  548. break;
  549. disasm_line__print(queue, sym, start, evsel, len,
  550. 0, 0, 1, NULL);
  551. }
  552. }
  553. color = get_percent_color(max_percent);
  554. /*
  555. * Also color the filename and line if needed, with
  556. * the same color than the percentage. Don't print it
  557. * twice for close colored addr with the same filename:line
  558. */
  559. if (path) {
  560. if (!prev_line || strcmp(prev_line, path)
  561. || color != prev_color) {
  562. color_fprintf(stdout, color, " %s", path);
  563. prev_line = path;
  564. prev_color = color;
  565. }
  566. }
  567. for (i = 0; i < nr_percent; i++) {
  568. percent = ppercents[i];
  569. color = get_percent_color(percent);
  570. color_fprintf(stdout, color, " %7.2f", percent);
  571. }
  572. printf(" : ");
  573. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  574. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  575. if (ppercents != &percent)
  576. free(ppercents);
  577. } else if (max_lines && printed >= max_lines)
  578. return 1;
  579. else {
  580. int width = 8;
  581. if (queue)
  582. return -1;
  583. if (perf_evsel__is_group_event(evsel))
  584. width *= evsel->nr_members;
  585. if (!*dl->line)
  586. printf(" %*s:\n", width, " ");
  587. else
  588. printf(" %*s: %s\n", width, " ", dl->line);
  589. }
  590. return 0;
  591. }
  592. /*
  593. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  594. * which looks like following
  595. *
  596. * 0000000000415500 <_init>:
  597. * 415500: sub $0x8,%rsp
  598. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  599. * 41550b: test %rax,%rax
  600. * 41550e: je 415515 <_init+0x15>
  601. * 415510: callq 416e70 <__gmon_start__@plt>
  602. * 415515: add $0x8,%rsp
  603. * 415519: retq
  604. *
  605. * it will be parsed and saved into struct disasm_line as
  606. * <offset> <name> <ops.raw>
  607. *
  608. * The offset will be a relative offset from the start of the symbol and -1
  609. * means that it's not a disassembly line so should be treated differently.
  610. * The ops.raw part will be parsed further according to type of the instruction.
  611. */
  612. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  613. FILE *file, size_t privsize)
  614. {
  615. struct annotation *notes = symbol__annotation(sym);
  616. struct disasm_line *dl;
  617. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  618. size_t line_len;
  619. s64 line_ip, offset = -1;
  620. if (getline(&line, &line_len, file) < 0)
  621. return -1;
  622. if (!line)
  623. return -1;
  624. while (line_len != 0 && isspace(line[line_len - 1]))
  625. line[--line_len] = '\0';
  626. c = strchr(line, '\n');
  627. if (c)
  628. *c = 0;
  629. line_ip = -1;
  630. parsed_line = line;
  631. /*
  632. * Strip leading spaces:
  633. */
  634. tmp = line;
  635. while (*tmp) {
  636. if (*tmp != ' ')
  637. break;
  638. tmp++;
  639. }
  640. if (*tmp) {
  641. /*
  642. * Parse hexa addresses followed by ':'
  643. */
  644. line_ip = strtoull(tmp, &tmp2, 16);
  645. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  646. line_ip = -1;
  647. }
  648. if (line_ip != -1) {
  649. u64 start = map__rip_2objdump(map, sym->start),
  650. end = map__rip_2objdump(map, sym->end);
  651. offset = line_ip - start;
  652. if ((u64)line_ip < start || (u64)line_ip > end)
  653. offset = -1;
  654. else
  655. parsed_line = tmp2 + 1;
  656. }
  657. dl = disasm_line__new(offset, parsed_line, privsize);
  658. free(line);
  659. if (dl == NULL)
  660. return -1;
  661. if (dl->ops.target.offset == UINT64_MAX)
  662. dl->ops.target.offset = dl->ops.target.addr -
  663. map__rip_2objdump(map, sym->start);
  664. /*
  665. * kcore has no symbols, so add the call target name if it is on the
  666. * same map.
  667. */
  668. if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
  669. struct symbol *s;
  670. u64 ip = dl->ops.target.addr;
  671. if (ip >= map->start && ip <= map->end) {
  672. ip = map->map_ip(map, ip);
  673. s = map__find_symbol(map, ip, NULL);
  674. if (s && s->start == ip)
  675. dl->ops.target.name = strdup(s->name);
  676. }
  677. }
  678. disasm__add(&notes->src->source, dl);
  679. return 0;
  680. }
  681. static void delete_last_nop(struct symbol *sym)
  682. {
  683. struct annotation *notes = symbol__annotation(sym);
  684. struct list_head *list = &notes->src->source;
  685. struct disasm_line *dl;
  686. while (!list_empty(list)) {
  687. dl = list_entry(list->prev, struct disasm_line, node);
  688. if (dl->ins && dl->ins->ops) {
  689. if (dl->ins->ops != &nop_ops)
  690. return;
  691. } else {
  692. if (!strstr(dl->line, " nop ") &&
  693. !strstr(dl->line, " nopl ") &&
  694. !strstr(dl->line, " nopw "))
  695. return;
  696. }
  697. list_del(&dl->node);
  698. disasm_line__free(dl);
  699. }
  700. }
  701. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  702. {
  703. struct dso *dso = map->dso;
  704. char *filename = dso__build_id_filename(dso, NULL, 0);
  705. bool free_filename = true;
  706. char command[PATH_MAX * 2];
  707. FILE *file;
  708. int err = 0;
  709. char symfs_filename[PATH_MAX];
  710. if (filename) {
  711. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  712. symbol_conf.symfs, filename);
  713. }
  714. if (filename == NULL) {
  715. if (dso->has_build_id) {
  716. pr_err("Can't annotate %s: not enough memory\n",
  717. sym->name);
  718. return -ENOMEM;
  719. }
  720. goto fallback;
  721. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  722. strstr(command, "[kernel.kallsyms]") ||
  723. access(symfs_filename, R_OK)) {
  724. free(filename);
  725. fallback:
  726. /*
  727. * If we don't have build-ids or the build-id file isn't in the
  728. * cache, or is just a kallsyms file, well, lets hope that this
  729. * DSO is the same as when 'perf record' ran.
  730. */
  731. filename = dso->long_name;
  732. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  733. symbol_conf.symfs, filename);
  734. free_filename = false;
  735. }
  736. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  737. !dso__is_kcore(dso)) {
  738. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  739. char *build_id_msg = NULL;
  740. if (dso->annotate_warned)
  741. goto out_free_filename;
  742. if (dso->has_build_id) {
  743. build_id__sprintf(dso->build_id,
  744. sizeof(dso->build_id), bf + 15);
  745. build_id_msg = bf;
  746. }
  747. err = -ENOENT;
  748. dso->annotate_warned = 1;
  749. pr_err("Can't annotate %s:\n\n"
  750. "No vmlinux file%s\nwas found in the path.\n\n"
  751. "Please use:\n\n"
  752. " perf buildid-cache -vu vmlinux\n\n"
  753. "or:\n\n"
  754. " --vmlinux vmlinux\n",
  755. sym->name, build_id_msg ?: "");
  756. goto out_free_filename;
  757. }
  758. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  759. filename, sym->name, map->unmap_ip(map, sym->start),
  760. map->unmap_ip(map, sym->end));
  761. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  762. dso, dso->long_name, sym, sym->name);
  763. snprintf(command, sizeof(command),
  764. "%s %s%s --start-address=0x%016" PRIx64
  765. " --stop-address=0x%016" PRIx64
  766. " -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
  767. objdump_path ? objdump_path : "objdump",
  768. disassembler_style ? "-M " : "",
  769. disassembler_style ? disassembler_style : "",
  770. map__rip_2objdump(map, sym->start),
  771. map__rip_2objdump(map, sym->end+1),
  772. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  773. symbol_conf.annotate_src ? "-S" : "",
  774. symfs_filename, filename);
  775. pr_debug("Executing: %s\n", command);
  776. file = popen(command, "r");
  777. if (!file)
  778. goto out_free_filename;
  779. while (!feof(file))
  780. if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
  781. break;
  782. /*
  783. * kallsyms does not have symbol sizes so there may a nop at the end.
  784. * Remove it.
  785. */
  786. if (dso__is_kcore(dso))
  787. delete_last_nop(sym);
  788. pclose(file);
  789. out_free_filename:
  790. if (free_filename)
  791. free(filename);
  792. return err;
  793. }
  794. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  795. {
  796. struct source_line *iter;
  797. struct rb_node **p = &root->rb_node;
  798. struct rb_node *parent = NULL;
  799. int i, ret;
  800. while (*p != NULL) {
  801. parent = *p;
  802. iter = rb_entry(parent, struct source_line, node);
  803. ret = strcmp(iter->path, src_line->path);
  804. if (ret == 0) {
  805. for (i = 0; i < src_line->nr_pcnt; i++)
  806. iter->p[i].percent_sum += src_line->p[i].percent;
  807. return;
  808. }
  809. if (ret < 0)
  810. p = &(*p)->rb_left;
  811. else
  812. p = &(*p)->rb_right;
  813. }
  814. for (i = 0; i < src_line->nr_pcnt; i++)
  815. src_line->p[i].percent_sum = src_line->p[i].percent;
  816. rb_link_node(&src_line->node, parent, p);
  817. rb_insert_color(&src_line->node, root);
  818. }
  819. static int cmp_source_line(struct source_line *a, struct source_line *b)
  820. {
  821. int i;
  822. for (i = 0; i < a->nr_pcnt; i++) {
  823. if (a->p[i].percent_sum == b->p[i].percent_sum)
  824. continue;
  825. return a->p[i].percent_sum > b->p[i].percent_sum;
  826. }
  827. return 0;
  828. }
  829. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  830. {
  831. struct source_line *iter;
  832. struct rb_node **p = &root->rb_node;
  833. struct rb_node *parent = NULL;
  834. while (*p != NULL) {
  835. parent = *p;
  836. iter = rb_entry(parent, struct source_line, node);
  837. if (cmp_source_line(src_line, iter))
  838. p = &(*p)->rb_left;
  839. else
  840. p = &(*p)->rb_right;
  841. }
  842. rb_link_node(&src_line->node, parent, p);
  843. rb_insert_color(&src_line->node, root);
  844. }
  845. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  846. {
  847. struct source_line *src_line;
  848. struct rb_node *node;
  849. node = rb_first(src_root);
  850. while (node) {
  851. struct rb_node *next;
  852. src_line = rb_entry(node, struct source_line, node);
  853. next = rb_next(node);
  854. rb_erase(node, src_root);
  855. __resort_source_line(dest_root, src_line);
  856. node = next;
  857. }
  858. }
  859. static void symbol__free_source_line(struct symbol *sym, int len)
  860. {
  861. struct annotation *notes = symbol__annotation(sym);
  862. struct source_line *src_line = notes->src->lines;
  863. size_t sizeof_src_line;
  864. int i;
  865. sizeof_src_line = sizeof(*src_line) +
  866. (sizeof(src_line->p) * (src_line->nr_pcnt - 1));
  867. for (i = 0; i < len; i++) {
  868. free(src_line->path);
  869. src_line = (void *)src_line + sizeof_src_line;
  870. }
  871. free(notes->src->lines);
  872. notes->src->lines = NULL;
  873. }
  874. /* Get the filename:line for the colored entries */
  875. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  876. struct perf_evsel *evsel,
  877. struct rb_root *root, int len,
  878. const char *filename)
  879. {
  880. u64 start;
  881. int i, k;
  882. int evidx = evsel->idx;
  883. char cmd[PATH_MAX * 2];
  884. struct source_line *src_line;
  885. struct annotation *notes = symbol__annotation(sym);
  886. struct sym_hist *h = annotation__histogram(notes, evidx);
  887. struct rb_root tmp_root = RB_ROOT;
  888. int nr_pcnt = 1;
  889. u64 h_sum = h->sum;
  890. size_t sizeof_src_line = sizeof(struct source_line);
  891. if (perf_evsel__is_group_event(evsel)) {
  892. for (i = 1; i < evsel->nr_members; i++) {
  893. h = annotation__histogram(notes, evidx + i);
  894. h_sum += h->sum;
  895. }
  896. nr_pcnt = evsel->nr_members;
  897. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->p);
  898. }
  899. if (!h_sum)
  900. return 0;
  901. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  902. if (!notes->src->lines)
  903. return -1;
  904. start = map__rip_2objdump(map, sym->start);
  905. for (i = 0; i < len; i++) {
  906. char *path = NULL;
  907. size_t line_len;
  908. u64 offset;
  909. FILE *fp;
  910. double percent_max = 0.0;
  911. src_line->nr_pcnt = nr_pcnt;
  912. for (k = 0; k < nr_pcnt; k++) {
  913. h = annotation__histogram(notes, evidx + k);
  914. src_line->p[k].percent = 100.0 * h->addr[i] / h->sum;
  915. if (src_line->p[k].percent > percent_max)
  916. percent_max = src_line->p[k].percent;
  917. }
  918. if (percent_max <= 0.5)
  919. goto next;
  920. offset = start + i;
  921. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  922. fp = popen(cmd, "r");
  923. if (!fp)
  924. goto next;
  925. if (getline(&path, &line_len, fp) < 0 || !line_len)
  926. goto next_close;
  927. src_line->path = malloc(sizeof(char) * line_len + 1);
  928. if (!src_line->path)
  929. goto next_close;
  930. strcpy(src_line->path, path);
  931. insert_source_line(&tmp_root, src_line);
  932. next_close:
  933. pclose(fp);
  934. next:
  935. src_line = (void *)src_line + sizeof_src_line;
  936. }
  937. resort_source_line(root, &tmp_root);
  938. return 0;
  939. }
  940. static void print_summary(struct rb_root *root, const char *filename)
  941. {
  942. struct source_line *src_line;
  943. struct rb_node *node;
  944. printf("\nSorted summary for file %s\n", filename);
  945. printf("----------------------------------------------\n\n");
  946. if (RB_EMPTY_ROOT(root)) {
  947. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  948. return;
  949. }
  950. node = rb_first(root);
  951. while (node) {
  952. double percent, percent_max = 0.0;
  953. const char *color;
  954. char *path;
  955. int i;
  956. src_line = rb_entry(node, struct source_line, node);
  957. for (i = 0; i < src_line->nr_pcnt; i++) {
  958. percent = src_line->p[i].percent_sum;
  959. color = get_percent_color(percent);
  960. color_fprintf(stdout, color, " %7.2f", percent);
  961. if (percent > percent_max)
  962. percent_max = percent;
  963. }
  964. path = src_line->path;
  965. color = get_percent_color(percent_max);
  966. color_fprintf(stdout, color, " %s", path);
  967. node = rb_next(node);
  968. }
  969. }
  970. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  971. {
  972. struct annotation *notes = symbol__annotation(sym);
  973. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  974. u64 len = symbol__size(sym), offset;
  975. for (offset = 0; offset < len; ++offset)
  976. if (h->addr[offset] != 0)
  977. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  978. sym->start + offset, h->addr[offset]);
  979. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  980. }
  981. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  982. struct perf_evsel *evsel, bool full_paths,
  983. int min_pcnt, int max_lines, int context)
  984. {
  985. struct dso *dso = map->dso;
  986. char *filename;
  987. const char *d_filename;
  988. struct annotation *notes = symbol__annotation(sym);
  989. struct disasm_line *pos, *queue = NULL;
  990. u64 start = map__rip_2objdump(map, sym->start);
  991. int printed = 2, queue_len = 0;
  992. int more = 0;
  993. u64 len;
  994. int width = 8;
  995. int namelen;
  996. filename = strdup(dso->long_name);
  997. if (!filename)
  998. return -ENOMEM;
  999. if (full_paths)
  1000. d_filename = filename;
  1001. else
  1002. d_filename = basename(filename);
  1003. len = symbol__size(sym);
  1004. namelen = strlen(d_filename);
  1005. if (perf_evsel__is_group_event(evsel))
  1006. width *= evsel->nr_members;
  1007. printf(" %-*.*s| Source code & Disassembly of %s\n",
  1008. width, width, "Percent", d_filename);
  1009. printf("-%-*.*s-------------------------------------\n",
  1010. width+namelen, width+namelen, graph_dotted_line);
  1011. if (verbose)
  1012. symbol__annotate_hits(sym, evsel);
  1013. list_for_each_entry(pos, &notes->src->source, node) {
  1014. if (context && queue == NULL) {
  1015. queue = pos;
  1016. queue_len = 0;
  1017. }
  1018. switch (disasm_line__print(pos, sym, start, evsel, len,
  1019. min_pcnt, printed, max_lines,
  1020. queue)) {
  1021. case 0:
  1022. ++printed;
  1023. if (context) {
  1024. printed += queue_len;
  1025. queue = NULL;
  1026. queue_len = 0;
  1027. }
  1028. break;
  1029. case 1:
  1030. /* filtered by max_lines */
  1031. ++more;
  1032. break;
  1033. case -1:
  1034. default:
  1035. /*
  1036. * Filtered by min_pcnt or non IP lines when
  1037. * context != 0
  1038. */
  1039. if (!context)
  1040. break;
  1041. if (queue_len == context)
  1042. queue = list_entry(queue->node.next, typeof(*queue), node);
  1043. else
  1044. ++queue_len;
  1045. break;
  1046. }
  1047. }
  1048. free(filename);
  1049. return more;
  1050. }
  1051. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1052. {
  1053. struct annotation *notes = symbol__annotation(sym);
  1054. struct sym_hist *h = annotation__histogram(notes, evidx);
  1055. memset(h, 0, notes->src->sizeof_sym_hist);
  1056. }
  1057. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1058. {
  1059. struct annotation *notes = symbol__annotation(sym);
  1060. struct sym_hist *h = annotation__histogram(notes, evidx);
  1061. int len = symbol__size(sym), offset;
  1062. h->sum = 0;
  1063. for (offset = 0; offset < len; ++offset) {
  1064. h->addr[offset] = h->addr[offset] * 7 / 8;
  1065. h->sum += h->addr[offset];
  1066. }
  1067. }
  1068. void disasm__purge(struct list_head *head)
  1069. {
  1070. struct disasm_line *pos, *n;
  1071. list_for_each_entry_safe(pos, n, head, node) {
  1072. list_del(&pos->node);
  1073. disasm_line__free(pos);
  1074. }
  1075. }
  1076. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1077. {
  1078. size_t printed;
  1079. if (dl->offset == -1)
  1080. return fprintf(fp, "%s\n", dl->line);
  1081. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  1082. if (dl->ops.raw[0] != '\0') {
  1083. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1084. dl->ops.raw);
  1085. }
  1086. return printed + fprintf(fp, "\n");
  1087. }
  1088. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1089. {
  1090. struct disasm_line *pos;
  1091. size_t printed = 0;
  1092. list_for_each_entry(pos, head, node)
  1093. printed += disasm_line__fprintf(pos, fp);
  1094. return printed;
  1095. }
  1096. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1097. struct perf_evsel *evsel, bool print_lines,
  1098. bool full_paths, int min_pcnt, int max_lines)
  1099. {
  1100. struct dso *dso = map->dso;
  1101. const char *filename = dso->long_name;
  1102. struct rb_root source_line = RB_ROOT;
  1103. u64 len;
  1104. if (symbol__annotate(sym, map, 0) < 0)
  1105. return -1;
  1106. len = symbol__size(sym);
  1107. if (print_lines) {
  1108. symbol__get_source_line(sym, map, evsel, &source_line,
  1109. len, filename);
  1110. print_summary(&source_line, filename);
  1111. }
  1112. symbol__annotate_printf(sym, map, evsel, full_paths,
  1113. min_pcnt, max_lines, 0);
  1114. if (print_lines)
  1115. symbol__free_source_line(sym, len);
  1116. disasm__purge(&symbol__annotation(sym)->src->source);
  1117. return 0;
  1118. }