annotate.c 26 KB

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