]>
Commit | Line | Data |
---|---|---|
211ef127 ACM |
1 | #include "../browser.h" |
2 | #include "../helpline.h" | |
3 | #include "../libslang.h" | |
78f7defe | 4 | #include "../../annotate.h" |
211ef127 ACM |
5 | #include "../../hist.h" |
6 | #include "../../sort.h" | |
7 | #include "../../symbol.h" | |
78f7defe | 8 | #include "../../annotate.h" |
211ef127 ACM |
9 | |
10 | static void ui__error_window(const char *fmt, ...) | |
11 | { | |
12 | va_list ap; | |
13 | ||
14 | va_start(ap, fmt); | |
15 | newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap); | |
16 | va_end(ap); | |
17 | } | |
18 | ||
92221162 ACM |
19 | struct annotate_browser { |
20 | struct ui_browser b; | |
21 | struct rb_root entries; | |
f1e9214c | 22 | struct rb_node *curr_hot; |
92221162 ACM |
23 | }; |
24 | ||
25 | struct objdump_line_rb_node { | |
26 | struct rb_node rb_node; | |
27 | double percent; | |
28 | u32 idx; | |
29 | }; | |
30 | ||
31 | static inline | |
32 | struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self) | |
33 | { | |
34 | return (struct objdump_line_rb_node *)(self + 1); | |
35 | } | |
36 | ||
211ef127 ACM |
37 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
38 | { | |
39 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); | |
40 | bool current_entry = ui_browser__is_current_entry(self, row); | |
41 | int width = self->width; | |
42 | ||
43 | if (ol->offset != -1) { | |
92221162 | 44 | struct objdump_line_rb_node *olrb = objdump_line__rb(ol); |
8f9bbc40 | 45 | ui_browser__set_percent_color(self, olrb->percent, current_entry); |
92221162 ACM |
46 | slsmg_printf(" %7.2f ", olrb->percent); |
47 | if (!current_entry) | |
8f9bbc40 | 48 | ui_browser__set_color(self, HE_COLORSET_CODE); |
92221162 | 49 | } else { |
8f9bbc40 | 50 | ui_browser__set_percent_color(self, 0, current_entry); |
92221162 ACM |
51 | slsmg_write_nstring(" ", 9); |
52 | } | |
53 | ||
54 | SLsmg_write_char(':'); | |
55 | slsmg_write_nstring(" ", 8); | |
56 | if (!*ol->line) | |
57 | slsmg_write_nstring(" ", width - 18); | |
58 | else | |
59 | slsmg_write_nstring(ol->line, width - 18); | |
60 | } | |
61 | ||
62 | static double objdump_line__calc_percent(struct objdump_line *self, | |
63 | struct list_head *head, | |
2f525d01 | 64 | struct symbol *sym, int evidx) |
92221162 ACM |
65 | { |
66 | double percent = 0.0; | |
67 | ||
68 | if (self->offset != -1) { | |
69 | int len = sym->end - sym->start; | |
211ef127 | 70 | unsigned int hits = 0; |
78f7defe ACM |
71 | struct annotation *notes = symbol__annotation(sym); |
72 | struct source_line *src_line = notes->src_line; | |
2f525d01 | 73 | struct sym_hist *h = annotation__histogram(notes, evidx); |
92221162 ACM |
74 | s64 offset = self->offset; |
75 | struct objdump_line *next = objdump__get_next_ip_line(head, self); | |
76 | ||
211ef127 ACM |
77 | while (offset < (s64)len && |
78 | (next == NULL || offset < next->offset)) { | |
78f7defe ACM |
79 | if (src_line) { |
80 | percent += src_line[offset].percent; | |
211ef127 | 81 | } else |
78f7defe | 82 | hits += h->addr[offset]; |
211ef127 ACM |
83 | |
84 | ++offset; | |
85 | } | |
78f7defe ACM |
86 | /* |
87 | * If the percentage wasn't already calculated in | |
88 | * symbol__get_source_line, do it now: | |
89 | */ | |
90 | if (src_line == NULL && h->sum) | |
211ef127 | 91 | percent = 100.0 * hits / h->sum; |
211ef127 ACM |
92 | } |
93 | ||
92221162 ACM |
94 | return percent; |
95 | } | |
96 | ||
97 | static void objdump__insert_line(struct rb_root *self, | |
98 | struct objdump_line_rb_node *line) | |
99 | { | |
100 | struct rb_node **p = &self->rb_node; | |
101 | struct rb_node *parent = NULL; | |
102 | struct objdump_line_rb_node *l; | |
103 | ||
104 | while (*p != NULL) { | |
105 | parent = *p; | |
106 | l = rb_entry(parent, struct objdump_line_rb_node, rb_node); | |
107 | if (line->percent < l->percent) | |
108 | p = &(*p)->rb_left; | |
109 | else | |
110 | p = &(*p)->rb_right; | |
111 | } | |
112 | rb_link_node(&line->rb_node, parent, p); | |
113 | rb_insert_color(&line->rb_node, self); | |
211ef127 ACM |
114 | } |
115 | ||
f1e9214c ACM |
116 | static void annotate_browser__set_top(struct annotate_browser *self, |
117 | struct rb_node *nd) | |
118 | { | |
119 | struct objdump_line_rb_node *rbpos; | |
120 | struct objdump_line *pos; | |
121 | unsigned back; | |
122 | ||
123 | ui_browser__refresh_dimensions(&self->b); | |
124 | back = self->b.height / 2; | |
125 | rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node); | |
126 | pos = ((struct objdump_line *)rbpos) - 1; | |
127 | self->b.top_idx = self->b.index = rbpos->idx; | |
128 | ||
129 | while (self->b.top_idx != 0 && back != 0) { | |
130 | pos = list_entry(pos->node.prev, struct objdump_line, node); | |
131 | ||
132 | --self->b.top_idx; | |
133 | --back; | |
134 | } | |
135 | ||
136 | self->b.top = pos; | |
137 | self->curr_hot = nd; | |
138 | } | |
139 | ||
b50e003d | 140 | static int annotate_browser__run(struct annotate_browser *self) |
f1e9214c ACM |
141 | { |
142 | struct rb_node *nd; | |
78f7defe | 143 | struct symbol *sym = self->b.priv; |
b50e003d | 144 | int key; |
f1e9214c | 145 | |
78f7defe | 146 | if (ui_browser__show(&self->b, sym->name, |
b50e003d | 147 | "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0) |
f1e9214c | 148 | return -1; |
4c1c952e ACM |
149 | /* |
150 | * To allow builtin-annotate to cycle thru multiple symbols by | |
151 | * examining the exit key for this function. | |
152 | */ | |
153 | ui_browser__add_exit_key(&self->b, NEWT_KEY_RIGHT); | |
f1e9214c ACM |
154 | |
155 | nd = self->curr_hot; | |
156 | if (nd) { | |
4c1c952e ACM |
157 | int tabs[] = { NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0 }; |
158 | ui_browser__add_exit_keys(&self->b, tabs); | |
f1e9214c ACM |
159 | } |
160 | ||
161 | while (1) { | |
b50e003d | 162 | key = ui_browser__run(&self->b); |
f1e9214c | 163 | |
b50e003d | 164 | switch (key) { |
f1e9214c ACM |
165 | case NEWT_KEY_TAB: |
166 | nd = rb_prev(nd); | |
167 | if (nd == NULL) | |
168 | nd = rb_last(&self->entries); | |
169 | annotate_browser__set_top(self, nd); | |
170 | break; | |
171 | case NEWT_KEY_UNTAB: | |
172 | nd = rb_next(nd); | |
173 | if (nd == NULL) | |
174 | nd = rb_first(&self->entries); | |
175 | annotate_browser__set_top(self, nd); | |
176 | break; | |
177 | default: | |
178 | goto out; | |
179 | } | |
180 | } | |
181 | out: | |
59e8fe32 | 182 | ui_browser__hide(&self->b); |
b50e003d | 183 | return key; |
f1e9214c ACM |
184 | } |
185 | ||
2f525d01 | 186 | int hist_entry__tui_annotate(struct hist_entry *he, int evidx) |
78f7defe | 187 | { |
2f525d01 | 188 | return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx); |
78f7defe ACM |
189 | } |
190 | ||
2f525d01 | 191 | int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx) |
211ef127 | 192 | { |
211ef127 | 193 | struct objdump_line *pos, *n; |
92221162 | 194 | struct objdump_line_rb_node *rbpos; |
211ef127 | 195 | LIST_HEAD(head); |
92221162 ACM |
196 | struct annotate_browser browser = { |
197 | .b = { | |
198 | .entries = &head, | |
199 | .refresh = ui_browser__list_head_refresh, | |
200 | .seek = ui_browser__list_head_seek, | |
201 | .write = annotate_browser__write, | |
78f7defe | 202 | .priv = sym, |
92221162 | 203 | }, |
211ef127 ACM |
204 | }; |
205 | int ret; | |
206 | ||
78f7defe | 207 | if (sym == NULL) |
211ef127 ACM |
208 | return -1; |
209 | ||
78f7defe | 210 | if (map->dso->annotate_warned) |
211ef127 ACM |
211 | return -1; |
212 | ||
78f7defe | 213 | if (symbol__annotate(sym, map, &head, sizeof(*rbpos)) < 0) { |
1e6dd077 | 214 | ui__error_window(ui_helpline__last_msg); |
211ef127 ACM |
215 | return -1; |
216 | } | |
217 | ||
218 | ui_helpline__push("Press <- or ESC to exit"); | |
219 | ||
220 | list_for_each_entry(pos, &head, node) { | |
221 | size_t line_len = strlen(pos->line); | |
92221162 ACM |
222 | if (browser.b.width < line_len) |
223 | browser.b.width = line_len; | |
224 | rbpos = objdump_line__rb(pos); | |
225 | rbpos->idx = browser.b.nr_entries++; | |
2f525d01 | 226 | rbpos->percent = objdump_line__calc_percent(pos, &head, sym, evidx); |
92221162 ACM |
227 | if (rbpos->percent < 0.01) |
228 | continue; | |
229 | objdump__insert_line(&browser.entries, rbpos); | |
230 | } | |
231 | ||
232 | /* | |
233 | * Position the browser at the hottest line. | |
234 | */ | |
f1e9214c ACM |
235 | browser.curr_hot = rb_last(&browser.entries); |
236 | if (browser.curr_hot) | |
237 | annotate_browser__set_top(&browser, browser.curr_hot); | |
211ef127 | 238 | |
92221162 | 239 | browser.b.width += 18; /* Percentage */ |
b50e003d | 240 | ret = annotate_browser__run(&browser); |
211ef127 ACM |
241 | list_for_each_entry_safe(pos, n, &head, node) { |
242 | list_del(&pos->node); | |
243 | objdump_line__free(pos); | |
244 | } | |
211ef127 ACM |
245 | return ret; |
246 | } |