builtin-annotate.c 18 KB

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