annotate.c 6.2 KB

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