builtin-annotate.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * builtin-annotate.c
  3. *
  4. * Builtin annotate command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/color.h"
  11. #include <linux/list.h>
  12. #include "util/cache.h"
  13. #include <linux/rbtree.h>
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "perf.h"
  17. #include "util/debug.h"
  18. #include "util/parse-options.h"
  19. #include "util/parse-events.h"
  20. #include "util/thread.h"
  21. #include "util/sort.h"
  22. static char const *input_name = "perf.data";
  23. static int force;
  24. static int input;
  25. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  26. static int full_paths;
  27. static int print_line;
  28. static unsigned long page_size;
  29. static unsigned long mmap_window = 32;
  30. static struct rb_root threads;
  31. static struct thread *last_match;
  32. struct sym_ext {
  33. struct rb_node node;
  34. double percent;
  35. char *path;
  36. };
  37. /*
  38. * histogram, sorted on item, collects counts
  39. */
  40. static struct rb_root hist;
  41. static int64_t
  42. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  43. {
  44. struct sort_entry *se;
  45. int64_t cmp = 0;
  46. list_for_each_entry(se, &hist_entry__sort_list, list) {
  47. cmp = se->cmp(left, right);
  48. if (cmp)
  49. break;
  50. }
  51. return cmp;
  52. }
  53. static int64_t
  54. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  55. {
  56. struct sort_entry *se;
  57. int64_t cmp = 0;
  58. list_for_each_entry(se, &hist_entry__sort_list, list) {
  59. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  60. f = se->collapse ?: se->cmp;
  61. cmp = f(left, right);
  62. if (cmp)
  63. break;
  64. }
  65. return cmp;
  66. }
  67. /*
  68. * collect histogram counts
  69. */
  70. static void hist_hit(struct hist_entry *he, u64 ip)
  71. {
  72. unsigned int sym_size, offset;
  73. struct symbol *sym = he->sym;
  74. he->count++;
  75. if (!sym || !sym->hist)
  76. return;
  77. sym_size = sym->end - sym->start;
  78. offset = ip - sym->start;
  79. if (offset >= sym_size)
  80. return;
  81. sym->hist_sum++;
  82. sym->hist[offset]++;
  83. if (verbose >= 3)
  84. printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
  85. (void *)(unsigned long)he->sym->start,
  86. he->sym->name,
  87. (void *)(unsigned long)ip, ip - he->sym->start,
  88. sym->hist[offset]);
  89. }
  90. static int
  91. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  92. struct symbol *sym, u64 ip, char level)
  93. {
  94. struct rb_node **p = &hist.rb_node;
  95. struct rb_node *parent = NULL;
  96. struct hist_entry *he;
  97. struct hist_entry entry = {
  98. .thread = thread,
  99. .map = map,
  100. .dso = dso,
  101. .sym = sym,
  102. .ip = ip,
  103. .level = level,
  104. .count = 1,
  105. };
  106. int cmp;
  107. while (*p != NULL) {
  108. parent = *p;
  109. he = rb_entry(parent, struct hist_entry, rb_node);
  110. cmp = hist_entry__cmp(&entry, he);
  111. if (!cmp) {
  112. hist_hit(he, ip);
  113. return 0;
  114. }
  115. if (cmp < 0)
  116. p = &(*p)->rb_left;
  117. else
  118. p = &(*p)->rb_right;
  119. }
  120. he = malloc(sizeof(*he));
  121. if (!he)
  122. return -ENOMEM;
  123. *he = entry;
  124. rb_link_node(&he->rb_node, parent, p);
  125. rb_insert_color(&he->rb_node, &hist);
  126. return 0;
  127. }
  128. static void hist_entry__free(struct hist_entry *he)
  129. {
  130. free(he);
  131. }
  132. /*
  133. * collapse the histogram
  134. */
  135. static struct rb_root collapse_hists;
  136. static void collapse__insert_entry(struct hist_entry *he)
  137. {
  138. struct rb_node **p = &collapse_hists.rb_node;
  139. struct rb_node *parent = NULL;
  140. struct hist_entry *iter;
  141. int64_t cmp;
  142. while (*p != NULL) {
  143. parent = *p;
  144. iter = rb_entry(parent, struct hist_entry, rb_node);
  145. cmp = hist_entry__collapse(iter, he);
  146. if (!cmp) {
  147. iter->count += he->count;
  148. hist_entry__free(he);
  149. return;
  150. }
  151. if (cmp < 0)
  152. p = &(*p)->rb_left;
  153. else
  154. p = &(*p)->rb_right;
  155. }
  156. rb_link_node(&he->rb_node, parent, p);
  157. rb_insert_color(&he->rb_node, &collapse_hists);
  158. }
  159. static void collapse__resort(void)
  160. {
  161. struct rb_node *next;
  162. struct hist_entry *n;
  163. if (!sort__need_collapse)
  164. return;
  165. next = rb_first(&hist);
  166. while (next) {
  167. n = rb_entry(next, struct hist_entry, rb_node);
  168. next = rb_next(&n->rb_node);
  169. rb_erase(&n->rb_node, &hist);
  170. collapse__insert_entry(n);
  171. }
  172. }
  173. /*
  174. * reverse the map, sort on count.
  175. */
  176. static struct rb_root output_hists;
  177. static void output__insert_entry(struct hist_entry *he)
  178. {
  179. struct rb_node **p = &output_hists.rb_node;
  180. struct rb_node *parent = NULL;
  181. struct hist_entry *iter;
  182. while (*p != NULL) {
  183. parent = *p;
  184. iter = rb_entry(parent, struct hist_entry, rb_node);
  185. if (he->count > iter->count)
  186. p = &(*p)->rb_left;
  187. else
  188. p = &(*p)->rb_right;
  189. }
  190. rb_link_node(&he->rb_node, parent, p);
  191. rb_insert_color(&he->rb_node, &output_hists);
  192. }
  193. static void output__resort(void)
  194. {
  195. struct rb_node *next;
  196. struct hist_entry *n;
  197. struct rb_root *tree = &hist;
  198. if (sort__need_collapse)
  199. tree = &collapse_hists;
  200. next = rb_first(tree);
  201. while (next) {
  202. n = rb_entry(next, struct hist_entry, rb_node);
  203. next = rb_next(&n->rb_node);
  204. rb_erase(&n->rb_node, tree);
  205. output__insert_entry(n);
  206. }
  207. }
  208. static unsigned long total = 0,
  209. total_mmap = 0,
  210. total_comm = 0,
  211. total_fork = 0,
  212. total_unknown = 0;
  213. static int
  214. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  215. {
  216. char level;
  217. int show = 0;
  218. struct dso *dso = NULL;
  219. struct thread *thread;
  220. u64 ip = event->ip.ip;
  221. struct map *map = NULL;
  222. thread = threads__findnew(event->ip.pid, &threads, &last_match);
  223. dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  224. (void *)(offset + head),
  225. (void *)(long)(event->header.size),
  226. event->header.misc,
  227. event->ip.pid,
  228. (void *)(long)ip);
  229. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  230. if (thread == NULL) {
  231. fprintf(stderr, "problem processing %d event, skipping it.\n",
  232. event->header.type);
  233. return -1;
  234. }
  235. if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
  236. show = SHOW_KERNEL;
  237. level = 'k';
  238. dso = kernel_dso;
  239. dump_printf(" ...... dso: %s\n", dso->name);
  240. } else if (event->header.misc & PERF_RECORD_MISC_USER) {
  241. show = SHOW_USER;
  242. level = '.';
  243. map = thread__find_map(thread, ip);
  244. if (map != NULL) {
  245. ip = map->map_ip(map, ip);
  246. dso = map->dso;
  247. } else {
  248. /*
  249. * If this is outside of all known maps,
  250. * and is a negative address, try to look it
  251. * up in the kernel dso, as it might be a
  252. * vsyscall (which executes in user-mode):
  253. */
  254. if ((long long)ip < 0)
  255. dso = kernel_dso;
  256. }
  257. dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  258. } else {
  259. show = SHOW_HV;
  260. level = 'H';
  261. dump_printf(" ...... dso: [hypervisor]\n");
  262. }
  263. if (show & show_mask) {
  264. struct symbol *sym = NULL;
  265. if (dso)
  266. sym = dso->find_symbol(dso, ip);
  267. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  268. fprintf(stderr,
  269. "problem incrementing symbol count, skipping event\n");
  270. return -1;
  271. }
  272. }
  273. total++;
  274. return 0;
  275. }
  276. static int
  277. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  278. {
  279. struct thread *thread;
  280. struct map *map = map__new(&event->mmap, NULL, 0);
  281. thread = threads__findnew(event->mmap.pid, &threads, &last_match);
  282. dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
  283. (void *)(offset + head),
  284. (void *)(long)(event->header.size),
  285. event->mmap.pid,
  286. (void *)(long)event->mmap.start,
  287. (void *)(long)event->mmap.len,
  288. (void *)(long)event->mmap.pgoff,
  289. event->mmap.filename);
  290. if (thread == NULL || map == NULL) {
  291. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  292. return 0;
  293. }
  294. thread__insert_map(thread, map);
  295. total_mmap++;
  296. return 0;
  297. }
  298. static int
  299. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  300. {
  301. struct thread *thread;
  302. thread = threads__findnew(event->comm.pid, &threads, &last_match);
  303. dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
  304. (void *)(offset + head),
  305. (void *)(long)(event->header.size),
  306. event->comm.comm, event->comm.pid);
  307. if (thread == NULL ||
  308. thread__set_comm(thread, event->comm.comm)) {
  309. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  310. return -1;
  311. }
  312. total_comm++;
  313. return 0;
  314. }
  315. static int
  316. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  317. {
  318. struct thread *thread;
  319. struct thread *parent;
  320. thread = threads__findnew(event->fork.pid, &threads, &last_match);
  321. parent = threads__findnew(event->fork.ppid, &threads, &last_match);
  322. dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
  323. (void *)(offset + head),
  324. (void *)(long)(event->header.size),
  325. event->fork.pid, event->fork.ppid);
  326. /*
  327. * A thread clone will have the same PID for both
  328. * parent and child.
  329. */
  330. if (thread == parent)
  331. return 0;
  332. if (!thread || !parent || thread__fork(thread, parent)) {
  333. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  334. return -1;
  335. }
  336. total_fork++;
  337. return 0;
  338. }
  339. static int
  340. process_event(event_t *event, unsigned long offset, unsigned long head)
  341. {
  342. switch (event->header.type) {
  343. case PERF_RECORD_SAMPLE:
  344. return process_sample_event(event, offset, head);
  345. case PERF_RECORD_MMAP:
  346. return process_mmap_event(event, offset, head);
  347. case PERF_RECORD_COMM:
  348. return process_comm_event(event, offset, head);
  349. case PERF_RECORD_FORK:
  350. return process_fork_event(event, offset, head);
  351. /*
  352. * We dont process them right now but they are fine:
  353. */
  354. case PERF_RECORD_THROTTLE:
  355. case PERF_RECORD_UNTHROTTLE:
  356. return 0;
  357. default:
  358. return -1;
  359. }
  360. return 0;
  361. }
  362. static int
  363. parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
  364. {
  365. char *line = NULL, *tmp, *tmp2;
  366. static const char *prev_line;
  367. static const char *prev_color;
  368. unsigned int offset;
  369. size_t line_len;
  370. s64 line_ip;
  371. int ret;
  372. char *c;
  373. if (getline(&line, &line_len, file) < 0)
  374. return -1;
  375. if (!line)
  376. return -1;
  377. c = strchr(line, '\n');
  378. if (c)
  379. *c = 0;
  380. line_ip = -1;
  381. offset = 0;
  382. ret = -2;
  383. /*
  384. * Strip leading spaces:
  385. */
  386. tmp = line;
  387. while (*tmp) {
  388. if (*tmp != ' ')
  389. break;
  390. tmp++;
  391. }
  392. if (*tmp) {
  393. /*
  394. * Parse hexa addresses followed by ':'
  395. */
  396. line_ip = strtoull(tmp, &tmp2, 16);
  397. if (*tmp2 != ':')
  398. line_ip = -1;
  399. }
  400. if (line_ip != -1) {
  401. const char *path = NULL;
  402. unsigned int hits = 0;
  403. double percent = 0.0;
  404. const char *color;
  405. struct sym_ext *sym_ext = sym->priv;
  406. offset = line_ip - start;
  407. if (offset < len)
  408. hits = sym->hist[offset];
  409. if (offset < len && sym_ext) {
  410. path = sym_ext[offset].path;
  411. percent = sym_ext[offset].percent;
  412. } else if (sym->hist_sum)
  413. percent = 100.0 * hits / sym->hist_sum;
  414. color = get_percent_color(percent);
  415. /*
  416. * Also color the filename and line if needed, with
  417. * the same color than the percentage. Don't print it
  418. * twice for close colored ip with the same filename:line
  419. */
  420. if (path) {
  421. if (!prev_line || strcmp(prev_line, path)
  422. || color != prev_color) {
  423. color_fprintf(stdout, color, " %s", path);
  424. prev_line = path;
  425. prev_color = color;
  426. }
  427. }
  428. color_fprintf(stdout, color, " %7.2f", percent);
  429. printf(" : ");
  430. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  431. } else {
  432. if (!*line)
  433. printf(" :\n");
  434. else
  435. printf(" : %s\n", line);
  436. }
  437. return 0;
  438. }
  439. static struct rb_root root_sym_ext;
  440. static void insert_source_line(struct sym_ext *sym_ext)
  441. {
  442. struct sym_ext *iter;
  443. struct rb_node **p = &root_sym_ext.rb_node;
  444. struct rb_node *parent = NULL;
  445. while (*p != NULL) {
  446. parent = *p;
  447. iter = rb_entry(parent, struct sym_ext, node);
  448. if (sym_ext->percent > iter->percent)
  449. p = &(*p)->rb_left;
  450. else
  451. p = &(*p)->rb_right;
  452. }
  453. rb_link_node(&sym_ext->node, parent, p);
  454. rb_insert_color(&sym_ext->node, &root_sym_ext);
  455. }
  456. static void free_source_line(struct symbol *sym, int len)
  457. {
  458. struct sym_ext *sym_ext = sym->priv;
  459. int i;
  460. if (!sym_ext)
  461. return;
  462. for (i = 0; i < len; i++)
  463. free(sym_ext[i].path);
  464. free(sym_ext);
  465. sym->priv = NULL;
  466. root_sym_ext = RB_ROOT;
  467. }
  468. /* Get the filename:line for the colored entries */
  469. static void
  470. get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
  471. {
  472. int i;
  473. char cmd[PATH_MAX * 2];
  474. struct sym_ext *sym_ext;
  475. if (!sym->hist_sum)
  476. return;
  477. sym->priv = calloc(len, sizeof(struct sym_ext));
  478. if (!sym->priv)
  479. return;
  480. sym_ext = sym->priv;
  481. for (i = 0; i < len; i++) {
  482. char *path = NULL;
  483. size_t line_len;
  484. u64 offset;
  485. FILE *fp;
  486. sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
  487. if (sym_ext[i].percent <= 0.5)
  488. continue;
  489. offset = start + i;
  490. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  491. fp = popen(cmd, "r");
  492. if (!fp)
  493. continue;
  494. if (getline(&path, &line_len, fp) < 0 || !line_len)
  495. goto next;
  496. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  497. if (!sym_ext[i].path)
  498. goto next;
  499. strcpy(sym_ext[i].path, path);
  500. insert_source_line(&sym_ext[i]);
  501. next:
  502. pclose(fp);
  503. }
  504. }
  505. static void print_summary(const char *filename)
  506. {
  507. struct sym_ext *sym_ext;
  508. struct rb_node *node;
  509. printf("\nSorted summary for file %s\n", filename);
  510. printf("----------------------------------------------\n\n");
  511. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  512. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  513. return;
  514. }
  515. node = rb_first(&root_sym_ext);
  516. while (node) {
  517. double percent;
  518. const char *color;
  519. char *path;
  520. sym_ext = rb_entry(node, struct sym_ext, node);
  521. percent = sym_ext->percent;
  522. color = get_percent_color(percent);
  523. path = sym_ext->path;
  524. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  525. node = rb_next(node);
  526. }
  527. }
  528. static void annotate_sym(struct dso *dso, struct symbol *sym)
  529. {
  530. const char *filename = dso->name, *d_filename;
  531. u64 start, end, len;
  532. char command[PATH_MAX*2];
  533. FILE *file;
  534. if (!filename)
  535. return;
  536. if (sym->module)
  537. filename = sym->module->path;
  538. else if (dso == kernel_dso)
  539. filename = vmlinux_name;
  540. start = sym->obj_start;
  541. if (!start)
  542. start = sym->start;
  543. if (full_paths)
  544. d_filename = filename;
  545. else
  546. d_filename = basename(filename);
  547. end = start + sym->end - sym->start + 1;
  548. len = sym->end - sym->start;
  549. if (print_line) {
  550. get_source_line(sym, start, len, filename);
  551. print_summary(filename);
  552. }
  553. printf("\n\n------------------------------------------------\n");
  554. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  555. printf("------------------------------------------------\n");
  556. if (verbose >= 2)
  557. printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
  558. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
  559. (u64)start, (u64)end, filename, filename);
  560. if (verbose >= 3)
  561. printf("doing: %s\n", command);
  562. file = popen(command, "r");
  563. if (!file)
  564. return;
  565. while (!feof(file)) {
  566. if (parse_line(file, sym, start, len) < 0)
  567. break;
  568. }
  569. pclose(file);
  570. if (print_line)
  571. free_source_line(sym, len);
  572. }
  573. static void find_annotations(void)
  574. {
  575. struct rb_node *nd;
  576. struct dso *dso;
  577. int count = 0;
  578. list_for_each_entry(dso, &dsos, node) {
  579. for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
  580. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  581. if (sym->hist) {
  582. annotate_sym(dso, sym);
  583. count++;
  584. }
  585. }
  586. }
  587. if (!count)
  588. printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
  589. }
  590. static int __cmd_annotate(void)
  591. {
  592. int ret, rc = EXIT_FAILURE;
  593. unsigned long offset = 0;
  594. unsigned long head = 0;
  595. struct stat input_stat;
  596. event_t *event;
  597. uint32_t size;
  598. char *buf;
  599. register_idle_thread(&threads, &last_match);
  600. input = open(input_name, O_RDONLY);
  601. if (input < 0) {
  602. perror("failed to open file");
  603. exit(-1);
  604. }
  605. ret = fstat(input, &input_stat);
  606. if (ret < 0) {
  607. perror("failed to stat file");
  608. exit(-1);
  609. }
  610. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  611. fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
  612. exit(-1);
  613. }
  614. if (!input_stat.st_size) {
  615. fprintf(stderr, "zero-sized file, nothing to do!\n");
  616. exit(0);
  617. }
  618. if (load_kernel() < 0) {
  619. perror("failed to load kernel symbols");
  620. return EXIT_FAILURE;
  621. }
  622. remap:
  623. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  624. MAP_SHARED, input, offset);
  625. if (buf == MAP_FAILED) {
  626. perror("failed to mmap file");
  627. exit(-1);
  628. }
  629. more:
  630. event = (event_t *)(buf + head);
  631. size = event->header.size;
  632. if (!size)
  633. size = 8;
  634. if (head + event->header.size >= page_size * mmap_window) {
  635. unsigned long shift = page_size * (head / page_size);
  636. int munmap_ret;
  637. munmap_ret = munmap(buf, page_size * mmap_window);
  638. assert(munmap_ret == 0);
  639. offset += shift;
  640. head -= shift;
  641. goto remap;
  642. }
  643. size = event->header.size;
  644. dump_printf("%p [%p]: event: %d\n",
  645. (void *)(offset + head),
  646. (void *)(long)event->header.size,
  647. event->header.type);
  648. if (!size || process_event(event, offset, head) < 0) {
  649. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  650. (void *)(offset + head),
  651. (void *)(long)(event->header.size),
  652. event->header.type);
  653. total_unknown++;
  654. /*
  655. * assume we lost track of the stream, check alignment, and
  656. * increment a single u64 in the hope to catch on again 'soon'.
  657. */
  658. if (unlikely(head & 7))
  659. head &= ~7ULL;
  660. size = 8;
  661. }
  662. head += size;
  663. if (offset + head < (unsigned long)input_stat.st_size)
  664. goto more;
  665. rc = EXIT_SUCCESS;
  666. close(input);
  667. dump_printf(" IP events: %10ld\n", total);
  668. dump_printf(" mmap events: %10ld\n", total_mmap);
  669. dump_printf(" comm events: %10ld\n", total_comm);
  670. dump_printf(" fork events: %10ld\n", total_fork);
  671. dump_printf(" unknown events: %10ld\n", total_unknown);
  672. if (dump_trace)
  673. return 0;
  674. if (verbose >= 3)
  675. threads__fprintf(stdout, &threads);
  676. if (verbose >= 2)
  677. dsos__fprintf(stdout);
  678. collapse__resort();
  679. output__resort();
  680. find_annotations();
  681. return rc;
  682. }
  683. static const char * const annotate_usage[] = {
  684. "perf annotate [<options>] <command>",
  685. NULL
  686. };
  687. static const struct option options[] = {
  688. OPT_STRING('i', "input", &input_name, "file",
  689. "input file name"),
  690. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  691. "symbol to annotate"),
  692. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  693. OPT_BOOLEAN('v', "verbose", &verbose,
  694. "be more verbose (show symbol address, etc)"),
  695. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  696. "dump raw trace in ASCII"),
  697. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  698. OPT_BOOLEAN('m', "modules", &modules,
  699. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  700. OPT_BOOLEAN('l', "print-line", &print_line,
  701. "print matching source lines (may be slow)"),
  702. OPT_BOOLEAN('P', "full-paths", &full_paths,
  703. "Don't shorten the displayed pathnames"),
  704. OPT_END()
  705. };
  706. static void setup_sorting(void)
  707. {
  708. char *tmp, *tok, *str = strdup(sort_order);
  709. for (tok = strtok_r(str, ", ", &tmp);
  710. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  711. if (sort_dimension__add(tok) < 0) {
  712. error("Unknown --sort key: `%s'", tok);
  713. usage_with_options(annotate_usage, options);
  714. }
  715. }
  716. free(str);
  717. }
  718. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  719. {
  720. symbol__init();
  721. page_size = getpagesize();
  722. argc = parse_options(argc, argv, options, annotate_usage, 0);
  723. setup_sorting();
  724. if (argc) {
  725. /*
  726. * Special case: if there's an argument left then assume tha
  727. * it's a symbol filter:
  728. */
  729. if (argc > 1)
  730. usage_with_options(annotate_usage, options);
  731. sym_hist_filter = argv[0];
  732. }
  733. if (!sym_hist_filter)
  734. usage_with_options(annotate_usage, options);
  735. setup_pager();
  736. if (field_sep && *field_sep == '.') {
  737. fputs("'.' is the only non valid --field-separator argument\n",
  738. stderr);
  739. exit(129);
  740. }
  741. return __cmd_annotate();
  742. }