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" |
c97cf422 | 9 | #include <pthread.h> |
211ef127 ACM |
10 | |
11 | static void ui__error_window(const char *fmt, ...) | |
12 | { | |
13 | va_list ap; | |
14 | ||
15 | va_start(ap, fmt); | |
16 | newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap); | |
17 | va_end(ap); | |
18 | } | |
19 | ||
92221162 ACM |
20 | struct annotate_browser { |
21 | struct ui_browser b; | |
22 | struct rb_root entries; | |
f1e9214c | 23 | struct rb_node *curr_hot; |
92221162 ACM |
24 | }; |
25 | ||
26 | struct objdump_line_rb_node { | |
27 | struct rb_node rb_node; | |
28 | double percent; | |
29 | u32 idx; | |
30 | }; | |
31 | ||
32 | static inline | |
33 | struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self) | |
34 | { | |
35 | return (struct objdump_line_rb_node *)(self + 1); | |
36 | } | |
37 | ||
211ef127 ACM |
38 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
39 | { | |
40 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); | |
41 | bool current_entry = ui_browser__is_current_entry(self, row); | |
42 | int width = self->width; | |
43 | ||
44 | if (ol->offset != -1) { | |
92221162 | 45 | struct objdump_line_rb_node *olrb = objdump_line__rb(ol); |
8f9bbc40 | 46 | ui_browser__set_percent_color(self, olrb->percent, current_entry); |
92221162 | 47 | slsmg_printf(" %7.2f ", olrb->percent); |
92221162 | 48 | } else { |
8f9bbc40 | 49 | ui_browser__set_percent_color(self, 0, current_entry); |
92221162 ACM |
50 | slsmg_write_nstring(" ", 9); |
51 | } | |
52 | ||
53 | SLsmg_write_char(':'); | |
54 | slsmg_write_nstring(" ", 8); | |
55 | if (!*ol->line) | |
56 | slsmg_write_nstring(" ", width - 18); | |
57 | else | |
58 | slsmg_write_nstring(ol->line, width - 18); | |
b99976e2 ACM |
59 | |
60 | if (!current_entry) | |
61 | ui_browser__set_color(self, HE_COLORSET_CODE); | |
92221162 ACM |
62 | } |
63 | ||
64 | static double objdump_line__calc_percent(struct objdump_line *self, | |
2f525d01 | 65 | struct symbol *sym, int evidx) |
92221162 ACM |
66 | { |
67 | double percent = 0.0; | |
68 | ||
69 | if (self->offset != -1) { | |
70 | int len = sym->end - sym->start; | |
211ef127 | 71 | unsigned int hits = 0; |
78f7defe | 72 | struct annotation *notes = symbol__annotation(sym); |
ce6f4fab | 73 | struct source_line *src_line = notes->src->lines; |
2f525d01 | 74 | struct sym_hist *h = annotation__histogram(notes, evidx); |
92221162 | 75 | s64 offset = self->offset; |
ce6f4fab | 76 | struct objdump_line *next; |
92221162 | 77 | |
ce6f4fab | 78 | next = objdump__get_next_ip_line(¬es->src->source, self); |
211ef127 ACM |
79 | while (offset < (s64)len && |
80 | (next == NULL || offset < next->offset)) { | |
78f7defe ACM |
81 | if (src_line) { |
82 | percent += src_line[offset].percent; | |
211ef127 | 83 | } else |
78f7defe | 84 | hits += h->addr[offset]; |
211ef127 ACM |
85 | |
86 | ++offset; | |
87 | } | |
78f7defe ACM |
88 | /* |
89 | * If the percentage wasn't already calculated in | |
90 | * symbol__get_source_line, do it now: | |
91 | */ | |
92 | if (src_line == NULL && h->sum) | |
211ef127 | 93 | percent = 100.0 * hits / h->sum; |
211ef127 ACM |
94 | } |
95 | ||
92221162 ACM |
96 | return percent; |
97 | } | |
98 | ||
99 | static void objdump__insert_line(struct rb_root *self, | |
100 | struct objdump_line_rb_node *line) | |
101 | { | |
102 | struct rb_node **p = &self->rb_node; | |
103 | struct rb_node *parent = NULL; | |
104 | struct objdump_line_rb_node *l; | |
105 | ||
106 | while (*p != NULL) { | |
107 | parent = *p; | |
108 | l = rb_entry(parent, struct objdump_line_rb_node, rb_node); | |
109 | if (line->percent < l->percent) | |
110 | p = &(*p)->rb_left; | |
111 | else | |
112 | p = &(*p)->rb_right; | |
113 | } | |
114 | rb_link_node(&line->rb_node, parent, p); | |
115 | rb_insert_color(&line->rb_node, self); | |
211ef127 ACM |
116 | } |
117 | ||
f1e9214c ACM |
118 | static void annotate_browser__set_top(struct annotate_browser *self, |
119 | struct rb_node *nd) | |
120 | { | |
121 | struct objdump_line_rb_node *rbpos; | |
122 | struct objdump_line *pos; | |
123 | unsigned back; | |
124 | ||
125 | ui_browser__refresh_dimensions(&self->b); | |
126 | back = self->b.height / 2; | |
127 | rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node); | |
128 | pos = ((struct objdump_line *)rbpos) - 1; | |
129 | self->b.top_idx = self->b.index = rbpos->idx; | |
130 | ||
131 | while (self->b.top_idx != 0 && back != 0) { | |
132 | pos = list_entry(pos->node.prev, struct objdump_line, node); | |
133 | ||
134 | --self->b.top_idx; | |
135 | --back; | |
136 | } | |
137 | ||
138 | self->b.top = pos; | |
139 | self->curr_hot = nd; | |
140 | } | |
141 | ||
c97cf422 ACM |
142 | static void annotate_browser__calc_percent(struct annotate_browser *browser, |
143 | int evidx) | |
f1e9214c | 144 | { |
c97cf422 ACM |
145 | struct symbol *sym = browser->b.priv; |
146 | struct annotation *notes = symbol__annotation(sym); | |
147 | struct objdump_line *pos; | |
148 | ||
149 | browser->entries = RB_ROOT; | |
150 | ||
151 | pthread_mutex_lock(¬es->lock); | |
152 | ||
153 | list_for_each_entry(pos, ¬es->src->source, node) { | |
154 | struct objdump_line_rb_node *rbpos = objdump_line__rb(pos); | |
155 | rbpos->percent = objdump_line__calc_percent(pos, sym, evidx); | |
156 | if (rbpos->percent < 0.01) { | |
157 | RB_CLEAR_NODE(&rbpos->rb_node); | |
158 | continue; | |
159 | } | |
160 | objdump__insert_line(&browser->entries, rbpos); | |
161 | } | |
162 | pthread_mutex_unlock(¬es->lock); | |
163 | ||
164 | browser->curr_hot = rb_last(&browser->entries); | |
165 | } | |
166 | ||
167 | static int annotate_browser__run(struct annotate_browser *self, int evidx, | |
168 | int refresh) | |
169 | { | |
170 | struct rb_node *nd = NULL; | |
78f7defe | 171 | struct symbol *sym = self->b.priv; |
c97cf422 ACM |
172 | /* |
173 | * RIGHT To allow builtin-annotate to cycle thru multiple symbols by | |
174 | * examining the exit key for this function. | |
175 | */ | |
176 | int exit_keys[] = { 'H', NEWT_KEY_TAB, NEWT_KEY_UNTAB, | |
177 | NEWT_KEY_RIGHT, 0 }; | |
b50e003d | 178 | int key; |
f1e9214c | 179 | |
78f7defe | 180 | if (ui_browser__show(&self->b, sym->name, |
c97cf422 ACM |
181 | "<-, -> or ESC: exit, TAB/shift+TAB: " |
182 | "cycle hottest lines, H: Hottest") < 0) | |
f1e9214c | 183 | return -1; |
c97cf422 ACM |
184 | |
185 | ui_browser__add_exit_keys(&self->b, exit_keys); | |
186 | annotate_browser__calc_percent(self, evidx); | |
187 | ||
188 | if (self->curr_hot) | |
189 | annotate_browser__set_top(self, self->curr_hot); | |
f1e9214c ACM |
190 | |
191 | nd = self->curr_hot; | |
c97cf422 ACM |
192 | |
193 | if (refresh != 0) | |
194 | newtFormSetTimer(self->b.form, refresh); | |
f1e9214c ACM |
195 | |
196 | while (1) { | |
b50e003d | 197 | key = ui_browser__run(&self->b); |
f1e9214c | 198 | |
c97cf422 ACM |
199 | if (refresh != 0) { |
200 | annotate_browser__calc_percent(self, evidx); | |
201 | /* | |
202 | * Current line focus got out of the list of most active | |
203 | * lines, NULL it so that if TAB|UNTAB is pressed, we | |
204 | * move to curr_hot (current hottest line). | |
205 | */ | |
206 | if (nd != NULL && RB_EMPTY_NODE(nd)) | |
207 | nd = NULL; | |
208 | } | |
209 | ||
b50e003d | 210 | switch (key) { |
c97cf422 ACM |
211 | case -1: |
212 | /* | |
213 | * FIXME we need to check if it was | |
214 | * es.reason == NEWT_EXIT_TIMER | |
215 | */ | |
216 | if (refresh != 0) | |
217 | symbol__annotate_decay_histogram(sym, evidx); | |
218 | continue; | |
f1e9214c | 219 | case NEWT_KEY_TAB: |
c97cf422 ACM |
220 | if (nd != NULL) { |
221 | nd = rb_prev(nd); | |
222 | if (nd == NULL) | |
223 | nd = rb_last(&self->entries); | |
224 | } else | |
225 | nd = self->curr_hot; | |
f1e9214c ACM |
226 | break; |
227 | case NEWT_KEY_UNTAB: | |
c97cf422 ACM |
228 | if (nd != NULL) |
229 | nd = rb_next(nd); | |
230 | if (nd == NULL) | |
231 | nd = rb_first(&self->entries); | |
232 | else | |
233 | nd = self->curr_hot; | |
234 | break; | |
235 | case 'H': | |
236 | nd = self->curr_hot; | |
f1e9214c ACM |
237 | break; |
238 | default: | |
239 | goto out; | |
240 | } | |
c97cf422 ACM |
241 | |
242 | if (nd != NULL) | |
243 | annotate_browser__set_top(self, nd); | |
f1e9214c ACM |
244 | } |
245 | out: | |
59e8fe32 | 246 | ui_browser__hide(&self->b); |
b50e003d | 247 | return key; |
f1e9214c ACM |
248 | } |
249 | ||
2f525d01 | 250 | int hist_entry__tui_annotate(struct hist_entry *he, int evidx) |
78f7defe | 251 | { |
c97cf422 | 252 | return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, 0); |
78f7defe ACM |
253 | } |
254 | ||
c97cf422 ACM |
255 | int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx, |
256 | int refresh) | |
211ef127 | 257 | { |
211ef127 | 258 | struct objdump_line *pos, *n; |
db9a9cbc | 259 | struct annotation *notes; |
92221162 ACM |
260 | struct annotate_browser browser = { |
261 | .b = { | |
92221162 ACM |
262 | .refresh = ui_browser__list_head_refresh, |
263 | .seek = ui_browser__list_head_seek, | |
264 | .write = annotate_browser__write, | |
78f7defe | 265 | .priv = sym, |
92221162 | 266 | }, |
211ef127 ACM |
267 | }; |
268 | int ret; | |
269 | ||
78f7defe | 270 | if (sym == NULL) |
211ef127 ACM |
271 | return -1; |
272 | ||
78f7defe | 273 | if (map->dso->annotate_warned) |
211ef127 ACM |
274 | return -1; |
275 | ||
c97cf422 | 276 | if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) { |
1e6dd077 | 277 | ui__error_window(ui_helpline__last_msg); |
211ef127 ACM |
278 | return -1; |
279 | } | |
280 | ||
281 | ui_helpline__push("Press <- or ESC to exit"); | |
282 | ||
db9a9cbc LM |
283 | notes = symbol__annotation(sym); |
284 | ||
ce6f4fab | 285 | list_for_each_entry(pos, ¬es->src->source, node) { |
c97cf422 | 286 | struct objdump_line_rb_node *rbpos; |
211ef127 | 287 | size_t line_len = strlen(pos->line); |
c97cf422 | 288 | |
92221162 ACM |
289 | if (browser.b.width < line_len) |
290 | browser.b.width = line_len; | |
291 | rbpos = objdump_line__rb(pos); | |
292 | rbpos->idx = browser.b.nr_entries++; | |
92221162 ACM |
293 | } |
294 | ||
db9a9cbc | 295 | browser.b.entries = ¬es->src->source, |
92221162 | 296 | browser.b.width += 18; /* Percentage */ |
c97cf422 | 297 | ret = annotate_browser__run(&browser, evidx, refresh); |
ce6f4fab | 298 | list_for_each_entry_safe(pos, n, ¬es->src->source, node) { |
211ef127 ACM |
299 | list_del(&pos->node); |
300 | objdump_line__free(pos); | |
301 | } | |
211ef127 ACM |
302 | return ret; |
303 | } |