annotate.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "gtk.h"
  2. #include "util/debug.h"
  3. #include "util/annotate.h"
  4. #include "util/evsel.h"
  5. #include "ui/helpline.h"
  6. enum {
  7. ANN_COL__PERCENT,
  8. ANN_COL__OFFSET,
  9. ANN_COL__LINE,
  10. MAX_ANN_COLS
  11. };
  12. static const char *const col_names[] = {
  13. "Overhead",
  14. "Offset",
  15. "Line"
  16. };
  17. static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
  18. struct disasm_line *dl, int evidx)
  19. {
  20. struct sym_hist *symhist;
  21. double percent = 0.0;
  22. const char *markup;
  23. int ret = 0;
  24. strcpy(buf, "");
  25. if (dl->offset == (s64) -1)
  26. return 0;
  27. symhist = annotation__histogram(symbol__annotation(sym), evidx);
  28. if (!symbol_conf.event_group && !symhist->addr[dl->offset])
  29. return 0;
  30. percent = 100.0 * symhist->addr[dl->offset] / symhist->sum;
  31. markup = perf_gtk__get_percent_color(percent);
  32. if (markup)
  33. ret += scnprintf(buf, size, "%s", markup);
  34. ret += scnprintf(buf + ret, size - ret, "%6.2f%%", percent);
  35. if (markup)
  36. ret += scnprintf(buf + ret, size - ret, "</span>");
  37. return ret;
  38. }
  39. static int perf_gtk__get_offset(char *buf, size_t size, struct symbol *sym,
  40. struct map *map, struct disasm_line *dl)
  41. {
  42. u64 start = map__rip_2objdump(map, sym->start);
  43. strcpy(buf, "");
  44. if (dl->offset == (s64) -1)
  45. return 0;
  46. return scnprintf(buf, size, "%"PRIx64, start + dl->offset);
  47. }
  48. static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
  49. {
  50. int ret = 0;
  51. char *line = g_markup_escape_text(dl->line, -1);
  52. const char *markup = "<span fgcolor='gray'>";
  53. strcpy(buf, "");
  54. if (!line)
  55. return 0;
  56. if (dl->offset != (s64) -1)
  57. markup = NULL;
  58. if (markup)
  59. ret += scnprintf(buf, size, "%s", markup);
  60. ret += scnprintf(buf + ret, size - ret, "%s", line);
  61. if (markup)
  62. ret += scnprintf(buf + ret, size - ret, "</span>");
  63. g_free(line);
  64. return ret;
  65. }
  66. static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym,
  67. struct map *map, struct perf_evsel *evsel,
  68. struct hist_browser_timer *hbt __maybe_unused)
  69. {
  70. struct disasm_line *pos, *n;
  71. struct annotation *notes;
  72. GType col_types[MAX_ANN_COLS];
  73. GtkCellRenderer *renderer;
  74. GtkListStore *store;
  75. GtkWidget *view;
  76. int i;
  77. char s[512];
  78. notes = symbol__annotation(sym);
  79. for (i = 0; i < MAX_ANN_COLS; i++) {
  80. col_types[i] = G_TYPE_STRING;
  81. }
  82. store = gtk_list_store_newv(MAX_ANN_COLS, col_types);
  83. view = gtk_tree_view_new();
  84. renderer = gtk_cell_renderer_text_new();
  85. for (i = 0; i < MAX_ANN_COLS; i++) {
  86. gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
  87. -1, col_names[i], renderer, "markup",
  88. i, NULL);
  89. }
  90. gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
  91. g_object_unref(GTK_TREE_MODEL(store));
  92. list_for_each_entry(pos, &notes->src->source, node) {
  93. GtkTreeIter iter;
  94. int ret = 0;
  95. gtk_list_store_append(store, &iter);
  96. if (perf_evsel__is_group_event(evsel)) {
  97. for (i = 0; i < evsel->nr_members; i++) {
  98. ret += perf_gtk__get_percent(s + ret,
  99. sizeof(s) - ret,
  100. sym, pos,
  101. evsel->idx + i);
  102. ret += scnprintf(s + ret, sizeof(s) - ret, " ");
  103. }
  104. } else {
  105. ret = perf_gtk__get_percent(s, sizeof(s), sym, pos,
  106. evsel->idx);
  107. }
  108. if (ret)
  109. gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
  110. if (perf_gtk__get_offset(s, sizeof(s), sym, map, pos))
  111. gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
  112. if (perf_gtk__get_line(s, sizeof(s), pos))
  113. gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
  114. }
  115. gtk_container_add(GTK_CONTAINER(window), view);
  116. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  117. list_del(&pos->node);
  118. disasm_line__free(pos);
  119. }
  120. return 0;
  121. }
  122. static int symbol__gtk_annotate(struct symbol *sym, struct map *map,
  123. struct perf_evsel *evsel,
  124. struct hist_browser_timer *hbt)
  125. {
  126. GtkWidget *window;
  127. GtkWidget *notebook;
  128. GtkWidget *scrolled_window;
  129. GtkWidget *tab_label;
  130. if (map->dso->annotate_warned)
  131. return -1;
  132. if (symbol__annotate(sym, map, 0) < 0) {
  133. ui__error("%s", ui_helpline__current);
  134. return -1;
  135. }
  136. if (perf_gtk__is_active_context(pgctx)) {
  137. window = pgctx->main_window;
  138. notebook = pgctx->notebook;
  139. } else {
  140. GtkWidget *vbox;
  141. GtkWidget *infobar;
  142. GtkWidget *statbar;
  143. signal(SIGSEGV, perf_gtk__signal);
  144. signal(SIGFPE, perf_gtk__signal);
  145. signal(SIGINT, perf_gtk__signal);
  146. signal(SIGQUIT, perf_gtk__signal);
  147. signal(SIGTERM, perf_gtk__signal);
  148. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  149. gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
  150. g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
  151. pgctx = perf_gtk__activate_context(window);
  152. if (!pgctx)
  153. return -1;
  154. vbox = gtk_vbox_new(FALSE, 0);
  155. notebook = gtk_notebook_new();
  156. pgctx->notebook = notebook;
  157. gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
  158. infobar = perf_gtk__setup_info_bar();
  159. if (infobar) {
  160. gtk_box_pack_start(GTK_BOX(vbox), infobar,
  161. FALSE, FALSE, 0);
  162. }
  163. statbar = perf_gtk__setup_statusbar();
  164. gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
  165. gtk_container_add(GTK_CONTAINER(window), vbox);
  166. }
  167. scrolled_window = gtk_scrolled_window_new(NULL, NULL);
  168. tab_label = gtk_label_new(sym->name);
  169. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
  170. GTK_POLICY_AUTOMATIC,
  171. GTK_POLICY_AUTOMATIC);
  172. gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
  173. tab_label);
  174. perf_gtk__annotate_symbol(scrolled_window, sym, map, evsel, hbt);
  175. return 0;
  176. }
  177. int hist_entry__gtk_annotate(struct hist_entry *he,
  178. struct perf_evsel *evsel,
  179. struct hist_browser_timer *hbt)
  180. {
  181. return symbol__gtk_annotate(he->ms.sym, he->ms.map, evsel, hbt);
  182. }
  183. void perf_gtk__show_annotations(void)
  184. {
  185. GtkWidget *window;
  186. if (!perf_gtk__is_active_context(pgctx))
  187. return;
  188. window = pgctx->main_window;
  189. gtk_widget_show_all(window);
  190. perf_gtk__resize_window(window);
  191. gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  192. gtk_main();
  193. perf_gtk__deactivate_context(&pgctx);
  194. }