annotate.c 26 KB

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