annotate.c 26 KB

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