Linux Perf
annotate.c
Go to the documentation of this file.
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 
10 enum {
14 
16 };
17 
18 static const char *const col_names[] = {
19  "Overhead",
20  "Offset",
21  "Line"
22 };
23 
24 static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
25  struct disasm_line *dl, int evidx)
26 {
27  struct sym_hist *symhist;
28  double percent = 0.0;
29  const char *markup;
30  int ret = 0;
31 
32  strcpy(buf, "");
33 
34  if (dl->al.offset == (s64) -1)
35  return 0;
36 
37  symhist = annotation__histogram(symbol__annotation(sym), evidx);
38  if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples)
39  return 0;
40 
41  percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples;
42 
43  markup = perf_gtk__get_percent_color(percent);
44  if (markup)
45  ret += scnprintf(buf, size, "%s", markup);
46  ret += scnprintf(buf + ret, size - ret, "%6.2f%%", percent);
47  if (markup)
48  ret += scnprintf(buf + ret, size - ret, "</span>");
49 
50  return ret;
51 }
52 
53 static int perf_gtk__get_offset(char *buf, size_t size, struct symbol *sym,
54  struct map *map, struct disasm_line *dl)
55 {
56  u64 start = map__rip_2objdump(map, sym->start);
57 
58  strcpy(buf, "");
59 
60  if (dl->al.offset == (s64) -1)
61  return 0;
62 
63  return scnprintf(buf, size, "%"PRIx64, start + dl->al.offset);
64 }
65 
66 static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
67 {
68  int ret = 0;
69  char *line = g_markup_escape_text(dl->al.line, -1);
70  const char *markup = "<span fgcolor='gray'>";
71 
72  strcpy(buf, "");
73 
74  if (!line)
75  return 0;
76 
77  if (dl->al.offset != (s64) -1)
78  markup = NULL;
79 
80  if (markup)
81  ret += scnprintf(buf, size, "%s", markup);
82  ret += scnprintf(buf + ret, size - ret, "%s", line);
83  if (markup)
84  ret += scnprintf(buf + ret, size - ret, "</span>");
85 
86  g_free(line);
87  return ret;
88 }
89 
90 static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym,
91  struct map *map, struct perf_evsel *evsel,
92  struct hist_browser_timer *hbt __maybe_unused)
93 {
94  struct disasm_line *pos, *n;
95  struct annotation *notes;
96  GType col_types[MAX_ANN_COLS];
97  GtkCellRenderer *renderer;
98  GtkListStore *store;
99  GtkWidget *view;
100  int i;
101  char s[512];
102 
103  notes = symbol__annotation(sym);
104 
105  for (i = 0; i < MAX_ANN_COLS; i++) {
106  col_types[i] = G_TYPE_STRING;
107  }
108  store = gtk_list_store_newv(MAX_ANN_COLS, col_types);
109 
110  view = gtk_tree_view_new();
111  renderer = gtk_cell_renderer_text_new();
112 
113  for (i = 0; i < MAX_ANN_COLS; i++) {
114  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
115  -1, col_names[i], renderer, "markup",
116  i, NULL);
117  }
118 
119  gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
120  g_object_unref(GTK_TREE_MODEL(store));
121 
122  list_for_each_entry(pos, &notes->src->source, al.node) {
123  GtkTreeIter iter;
124  int ret = 0;
125 
126  gtk_list_store_append(store, &iter);
127 
128  if (perf_evsel__is_group_event(evsel)) {
129  for (i = 0; i < evsel->nr_members; i++) {
130  ret += perf_gtk__get_percent(s + ret,
131  sizeof(s) - ret,
132  sym, pos,
133  evsel->idx + i);
134  ret += scnprintf(s + ret, sizeof(s) - ret, " ");
135  }
136  } else {
137  ret = perf_gtk__get_percent(s, sizeof(s), sym, pos,
138  evsel->idx);
139  }
140 
141  if (ret)
142  gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
143  if (perf_gtk__get_offset(s, sizeof(s), sym, map, pos))
144  gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
145  if (perf_gtk__get_line(s, sizeof(s), pos))
146  gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
147  }
148 
149  gtk_container_add(GTK_CONTAINER(window), view);
150 
151  list_for_each_entry_safe(pos, n, &notes->src->source, al.node) {
152  list_del(&pos->al.node);
153  disasm_line__free(pos);
154  }
155 
156  return 0;
157 }
158 
159 static int symbol__gtk_annotate(struct symbol *sym, struct map *map,
160  struct perf_evsel *evsel,
161  struct hist_browser_timer *hbt)
162 {
163  GtkWidget *window;
164  GtkWidget *notebook;
165  GtkWidget *scrolled_window;
166  GtkWidget *tab_label;
167  int err;
168 
169  if (map->dso->annotate_warned)
170  return -1;
171 
172  err = symbol__annotate(sym, map, evsel, 0, &annotation__default_options, NULL);
173  if (err) {
174  char msg[BUFSIZ];
175  symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
176  ui__error("Couldn't annotate %s: %s\n", sym->name, msg);
177  return -1;
178  }
179 
180  symbol__calc_percent(sym, evsel);
181 
183  window = pgctx->main_window;
184  notebook = pgctx->notebook;
185  } else {
186  GtkWidget *vbox;
187  GtkWidget *infobar;
188  GtkWidget *statbar;
189 
190  signal(SIGSEGV, perf_gtk__signal);
191  signal(SIGFPE, perf_gtk__signal);
192  signal(SIGINT, perf_gtk__signal);
193  signal(SIGQUIT, perf_gtk__signal);
194  signal(SIGTERM, perf_gtk__signal);
195 
196  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
197  gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
198 
199  g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
200 
202  if (!pgctx)
203  return -1;
204 
205  vbox = gtk_vbox_new(FALSE, 0);
206  notebook = gtk_notebook_new();
207  pgctx->notebook = notebook;
208 
209  gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
210 
211  infobar = perf_gtk__setup_info_bar();
212  if (infobar) {
213  gtk_box_pack_start(GTK_BOX(vbox), infobar,
214  FALSE, FALSE, 0);
215  }
216 
217  statbar = perf_gtk__setup_statusbar();
218  gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
219 
220  gtk_container_add(GTK_CONTAINER(window), vbox);
221  }
222 
223  scrolled_window = gtk_scrolled_window_new(NULL, NULL);
224  tab_label = gtk_label_new(sym->name);
225 
226  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
227  GTK_POLICY_AUTOMATIC,
228  GTK_POLICY_AUTOMATIC);
229 
230  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
231  tab_label);
232 
233  perf_gtk__annotate_symbol(scrolled_window, sym, map, evsel, hbt);
234  return 0;
235 }
236 
238  struct perf_evsel *evsel,
239  struct hist_browser_timer *hbt)
240 {
241  return symbol__gtk_annotate(he->ms.sym, he->ms.map, evsel, hbt);
242 }
243 
245 {
246  GtkWidget *window;
247 
249  return;
250 
251  window = pgctx->main_window;
252  gtk_widget_show_all(window);
253 
254  perf_gtk__resize_window(window);
255  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
256 
257  gtk_main();
258 
260 }
int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
Definition: util.c:23
bool event_group
Definition: symbol.h:93
void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
Definition: annotate.c:1803
static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
Definition: annotate.c:66
GtkWidget * main_window
Definition: gtk.h:13
size_t size
Definition: evsel.c:60
int ui__error(const char *format,...)
Definition: util.c:32
struct map_symbol ms
Definition: sort.h:98
struct list_head node
Definition: annotate.h:111
struct symbol * sym
Definition: symbol.h:181
static const char *const col_names[]
Definition: annotate.c:18
u64 nr_samples
Definition: annotate.h:100
struct map * map
Definition: symbol.h:180
int hist_entry__gtk_annotate(struct hist_entry *he, struct perf_evsel *evsel, struct hist_browser_timer *hbt)
Definition: annotate.c:237
static GtkWidget * perf_gtk__setup_info_bar(void)
Definition: gtk.h:49
int int err
Definition: 5sec.c:44
struct annotation_line al
Definition: annotate.h:134
u64 nr_samples
Definition: annotate.h:193
u8 annotate_warned
Definition: dso.h:163
int idx
Definition: evsel.h:100
struct list_head source
Definition: annotate.h:229
int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map, int errnum, char *buf, size_t buflen)
Definition: annotate.c:1524
Definition: sort.h:89
void perf_gtk__show_annotations(void)
Definition: annotate.c:244
static struct sym_hist * annotation__histogram(struct annotation *notes, int idx)
Definition: annotate.h:288
u64 start
Definition: symbol.h:57
static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym, struct disasm_line *dl, int evidx)
Definition: annotate.c:24
struct dso * dso
Definition: map.h:45
char name[0]
Definition: symbol.h:66
void perf_gtk__signal(int sig)
Definition: browser.c:12
struct sym_hist_entry addr[0]
Definition: annotate.h:195
static struct annotation * symbol__annotation(struct symbol *sym)
Definition: annotate.h:293
u64 map__rip_2objdump(struct map *map, u64 rip)
Definition: map.c:450
struct perf_gtk_context * perf_gtk__activate_context(GtkWidget *window)
Definition: util.c:12
u64 start
Definition: hists_common.c:25
struct annotation_options annotation__default_options
Definition: annotate.c:47
GtkWidget * perf_gtk__setup_statusbar(void)
Definition: browser.c:75
Definition: jevents.c:228
GtkWidget * notebook
Definition: gtk.h:14
struct annotated_source * src
Definition: annotate.h:256
static int sym(yyscan_t scanner, int type, int config)
const char * perf_gtk__get_percent_color(double percent)
Definition: browser.c:38
static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym, struct map *map, struct perf_evsel *evsel, struct hist_browser_timer *hbt __maybe_unused)
Definition: annotate.c:90
static double percent(int st, int tot)
Definition: builtin-c2c.c:899
struct perf_gtk_context * pgctx
Definition: util.c:10
int nr_members
Definition: evsel.h:133
static bool perf_evsel__is_group_event(struct perf_evsel *evsel)
Definition: evsel.h:393
static bool perf_gtk__is_active_context(struct perf_gtk_context *ctx)
Definition: gtk.h:29
Definition: symbol.h:55
static int perf_gtk__get_offset(char *buf, size_t size, struct symbol *sym, struct map *map, struct disasm_line *dl)
Definition: annotate.c:53
void disasm_line__free(struct disasm_line *dl)
Definition: annotate.c:1168
void perf_gtk__resize_window(GtkWidget *window)
Definition: browser.c:18
int symbol__annotate(struct symbol *sym, struct map *map, struct perf_evsel *evsel, size_t privsize, struct annotation_options *options, struct arch **parch)
Definition: annotate.c:1810
static int symbol__gtk_annotate(struct symbol *sym, struct map *map, struct perf_evsel *evsel, struct hist_browser_timer *hbt)
Definition: annotate.c:159
def store(time, event, cpu, thread, val, ena, run)
Definition: stat-cpi.py:22