M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions

View File

@@ -0,0 +1,597 @@
#include "../util.h"
#include "../cache.h"
#include "../../perf.h"
#include "libslang.h"
#include <newt.h>
#include "ui.h"
#include "util.h"
#include <linux/compiler.h>
#include <linux/list.h>
#include <linux/rbtree.h>
#include <stdlib.h>
#include <sys/ttydefaults.h>
#include "browser.h"
#include "helpline.h"
#include "keysyms.h"
#include "../color.h"
static int ui_browser__percent_color(struct ui_browser *browser,
double percent, bool current)
{
if (current && (!browser->use_navkeypressed || browser->navkeypressed))
return HE_COLORSET_SELECTED;
if (percent >= MIN_RED)
return HE_COLORSET_TOP;
if (percent >= MIN_GREEN)
return HE_COLORSET_MEDIUM;
return HE_COLORSET_NORMAL;
}
void ui_browser__set_color(struct ui_browser *self __used, int color)
{
SLsmg_set_color(color);
}
void ui_browser__set_percent_color(struct ui_browser *self,
double percent, bool current)
{
int color = ui_browser__percent_color(self, percent, current);
ui_browser__set_color(self, color);
}
void ui_browser__gotorc(struct ui_browser *self, int y, int x)
{
SLsmg_gotorc(self->y + y, self->x + x);
}
static struct list_head *
ui_browser__list_head_filter_entries(struct ui_browser *browser,
struct list_head *pos)
{
do {
if (!browser->filter || !browser->filter(browser, pos))
return pos;
pos = pos->next;
} while (pos != browser->entries);
return NULL;
}
static struct list_head *
ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
struct list_head *pos)
{
do {
if (!browser->filter || !browser->filter(browser, pos))
return pos;
pos = pos->prev;
} while (pos != browser->entries);
return NULL;
}
void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
{
struct list_head *head = self->entries;
struct list_head *pos;
if (self->nr_entries == 0)
return;
switch (whence) {
case SEEK_SET:
pos = ui_browser__list_head_filter_entries(self, head->next);
break;
case SEEK_CUR:
pos = self->top;
break;
case SEEK_END:
pos = ui_browser__list_head_filter_prev_entries(self, head->prev);
break;
default:
return;
}
assert(pos != NULL);
if (offset > 0) {
while (offset-- != 0)
pos = ui_browser__list_head_filter_entries(self, pos->next);
} else {
while (offset++ != 0)
pos = ui_browser__list_head_filter_prev_entries(self, pos->prev);
}
self->top = pos;
}
void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
{
struct rb_root *root = self->entries;
struct rb_node *nd;
switch (whence) {
case SEEK_SET:
nd = rb_first(root);
break;
case SEEK_CUR:
nd = self->top;
break;
case SEEK_END:
nd = rb_last(root);
break;
default:
return;
}
if (offset > 0) {
while (offset-- != 0)
nd = rb_next(nd);
} else {
while (offset++ != 0)
nd = rb_prev(nd);
}
self->top = nd;
}
unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
{
struct rb_node *nd;
int row = 0;
if (self->top == NULL)
self->top = rb_first(self->entries);
nd = self->top;
while (nd != NULL) {
ui_browser__gotorc(self, row, 0);
self->write(self, nd, row);
if (++row == self->height)
break;
nd = rb_next(nd);
}
return row;
}
bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
{
return self->top_idx + row == self->index;
}
void ui_browser__refresh_dimensions(struct ui_browser *self)
{
self->width = SLtt_Screen_Cols - 1;
self->height = SLtt_Screen_Rows - 2;
self->y = 1;
self->x = 0;
}
void ui_browser__handle_resize(struct ui_browser *browser)
{
ui__refresh_dimensions(false);
ui_browser__show(browser, browser->title, ui_helpline__current);
ui_browser__refresh(browser);
}
int ui_browser__warning(struct ui_browser *browser, int timeout,
const char *format, ...)
{
va_list args;
char *text;
int key = 0, err;
va_start(args, format);
err = vasprintf(&text, format, args);
va_end(args);
if (err < 0) {
va_start(args, format);
ui_helpline__vpush(format, args);
va_end(args);
} else {
while ((key == ui__question_window("Warning!", text,
"Press any key...",
timeout)) == K_RESIZE)
ui_browser__handle_resize(browser);
free(text);
}
return key;
}
int ui_browser__help_window(struct ui_browser *browser, const char *text)
{
int key;
while ((key = ui__help_window(text)) == K_RESIZE)
ui_browser__handle_resize(browser);
return key;
}
bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
{
int key;
while ((key = ui__dialog_yesno(text)) == K_RESIZE)
ui_browser__handle_resize(browser);
return key == K_ENTER || toupper(key) == 'Y';
}
void ui_browser__reset_index(struct ui_browser *self)
{
self->index = self->top_idx = 0;
self->seek(self, 0, SEEK_SET);
}
void __ui_browser__show_title(struct ui_browser *browser, const char *title)
{
SLsmg_gotorc(0, 0);
ui_browser__set_color(browser, NEWT_COLORSET_ROOT);
slsmg_write_nstring(title, browser->width + 1);
}
void ui_browser__show_title(struct ui_browser *browser, const char *title)
{
pthread_mutex_lock(&ui__lock);
__ui_browser__show_title(browser, title);
pthread_mutex_unlock(&ui__lock);
}
int ui_browser__show(struct ui_browser *self, const char *title,
const char *helpline, ...)
{
int err;
va_list ap;
ui_browser__refresh_dimensions(self);
pthread_mutex_lock(&ui__lock);
__ui_browser__show_title(self, title);
self->title = title;
free(self->helpline);
self->helpline = NULL;
va_start(ap, helpline);
err = vasprintf(&self->helpline, helpline, ap);
va_end(ap);
if (err > 0)
ui_helpline__push(self->helpline);
pthread_mutex_unlock(&ui__lock);
return err ? 0 : -1;
}
void ui_browser__hide(struct ui_browser *browser __used)
{
pthread_mutex_lock(&ui__lock);
ui_helpline__pop();
pthread_mutex_unlock(&ui__lock);
}
static void ui_browser__scrollbar_set(struct ui_browser *browser)
{
int height = browser->height, h = 0, pct = 0,
col = browser->width,
row = browser->y - 1;
if (browser->nr_entries > 1) {
pct = ((browser->index * (browser->height - 1)) /
(browser->nr_entries - 1));
}
SLsmg_set_char_set(1);
while (h < height) {
ui_browser__gotorc(browser, row++, col);
SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
++h;
}
SLsmg_set_char_set(0);
}
static int __ui_browser__refresh(struct ui_browser *browser)
{
int row;
int width = browser->width;
row = browser->refresh(browser);
ui_browser__set_color(browser, HE_COLORSET_NORMAL);
if (!browser->use_navkeypressed || browser->navkeypressed)
ui_browser__scrollbar_set(browser);
else
width += 1;
SLsmg_fill_region(browser->y + row, browser->x,
browser->height - row, width, ' ');
return 0;
}
int ui_browser__refresh(struct ui_browser *browser)
{
pthread_mutex_lock(&ui__lock);
__ui_browser__refresh(browser);
pthread_mutex_unlock(&ui__lock);
return 0;
}
/*
* Here we're updating nr_entries _after_ we started browsing, i.e. we have to
* forget about any reference to any entry in the underlying data structure,
* that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
* after an output_resort and hist decay.
*/
void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
{
off_t offset = nr_entries - browser->nr_entries;
browser->nr_entries = nr_entries;
if (offset < 0) {
if (browser->top_idx < (u64)-offset)
offset = -browser->top_idx;
browser->index += offset;
browser->top_idx += offset;
}
browser->top = NULL;
browser->seek(browser, browser->top_idx, SEEK_SET);
}
int ui_browser__run(struct ui_browser *self, int delay_secs)
{
int err, key;
while (1) {
off_t offset;
pthread_mutex_lock(&ui__lock);
err = __ui_browser__refresh(self);
SLsmg_refresh();
pthread_mutex_unlock(&ui__lock);
if (err < 0)
break;
key = ui__getch(delay_secs);
if (key == K_RESIZE) {
ui__refresh_dimensions(false);
ui_browser__refresh_dimensions(self);
__ui_browser__show_title(self, self->title);
ui_helpline__puts(self->helpline);
continue;
}
if (self->use_navkeypressed && !self->navkeypressed) {
if (key == K_DOWN || key == K_UP ||
key == K_PGDN || key == K_PGUP ||
key == K_HOME || key == K_END ||
key == ' ') {
self->navkeypressed = true;
continue;
} else
return key;
}
switch (key) {
case K_DOWN:
if (self->index == self->nr_entries - 1)
break;
++self->index;
if (self->index == self->top_idx + self->height) {
++self->top_idx;
self->seek(self, +1, SEEK_CUR);
}
break;
case K_UP:
if (self->index == 0)
break;
--self->index;
if (self->index < self->top_idx) {
--self->top_idx;
self->seek(self, -1, SEEK_CUR);
}
break;
case K_PGDN:
case ' ':
if (self->top_idx + self->height > self->nr_entries - 1)
break;
offset = self->height;
if (self->index + offset > self->nr_entries - 1)
offset = self->nr_entries - 1 - self->index;
self->index += offset;
self->top_idx += offset;
self->seek(self, +offset, SEEK_CUR);
break;
case K_PGUP:
if (self->top_idx == 0)
break;
if (self->top_idx < self->height)
offset = self->top_idx;
else
offset = self->height;
self->index -= offset;
self->top_idx -= offset;
self->seek(self, -offset, SEEK_CUR);
break;
case K_HOME:
ui_browser__reset_index(self);
break;
case K_END:
offset = self->height - 1;
if (offset >= self->nr_entries)
offset = self->nr_entries - 1;
self->index = self->nr_entries - 1;
self->top_idx = self->index - offset;
self->seek(self, -offset, SEEK_END);
break;
default:
return key;
}
}
return -1;
}
unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
{
struct list_head *pos;
struct list_head *head = self->entries;
int row = 0;
if (self->top == NULL || self->top == self->entries)
self->top = ui_browser__list_head_filter_entries(self, head->next);
pos = self->top;
list_for_each_from(pos, head) {
if (!self->filter || !self->filter(self, pos)) {
ui_browser__gotorc(self, row, 0);
self->write(self, pos, row);
if (++row == self->height)
break;
}
}
return row;
}
static struct ui_browser__colorset {
const char *name, *fg, *bg;
int colorset;
} ui_browser__colorsets[] = {
{
.colorset = HE_COLORSET_TOP,
.name = "top",
.fg = "red",
.bg = "default",
},
{
.colorset = HE_COLORSET_MEDIUM,
.name = "medium",
.fg = "green",
.bg = "default",
},
{
.colorset = HE_COLORSET_NORMAL,
.name = "normal",
.fg = "default",
.bg = "default",
},
{
.colorset = HE_COLORSET_SELECTED,
.name = "selected",
.fg = "black",
.bg = "lightgray",
},
{
.colorset = HE_COLORSET_CODE,
.name = "code",
.fg = "blue",
.bg = "default",
},
{
.name = NULL,
}
};
static int ui_browser__color_config(const char *var, const char *value,
void *data __used)
{
char *fg = NULL, *bg;
int i;
/* same dir for all commands */
if (prefixcmp(var, "colors.") != 0)
return 0;
for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
const char *name = var + 7;
if (strcmp(ui_browser__colorsets[i].name, name) != 0)
continue;
fg = strdup(value);
if (fg == NULL)
break;
bg = strchr(fg, ',');
if (bg == NULL)
break;
*bg = '\0';
while (isspace(*++bg));
ui_browser__colorsets[i].bg = bg;
ui_browser__colorsets[i].fg = fg;
return 0;
}
free(fg);
return -1;
}
void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
{
switch (whence) {
case SEEK_SET:
browser->top = browser->entries;
break;
case SEEK_CUR:
browser->top = browser->top + browser->top_idx + offset;
break;
case SEEK_END:
browser->top = browser->top + browser->nr_entries + offset;
break;
default:
return;
}
}
unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
{
unsigned int row = 0, idx = browser->top_idx;
char **pos;
if (browser->top == NULL)
browser->top = browser->entries;
pos = (char **)browser->top;
while (idx < browser->nr_entries) {
if (!browser->filter || !browser->filter(browser, *pos)) {
ui_browser__gotorc(browser, row, 0);
browser->write(browser, pos, row);
if (++row == browser->height)
break;
}
++idx;
++pos;
}
return row;
}
void ui_browser__init(void)
{
int i = 0;
perf_config(ui_browser__color_config, NULL);
while (ui_browser__colorsets[i].name) {
struct ui_browser__colorset *c = &ui_browser__colorsets[i++];
sltt_set_color(c->colorset, c->name, c->fg, c->bg);
}
}

View File

@@ -0,0 +1,65 @@
#ifndef _PERF_UI_BROWSER_H_
#define _PERF_UI_BROWSER_H_ 1
#include <stdbool.h>
#include <sys/types.h>
#include "../types.h"
#define HE_COLORSET_TOP 50
#define HE_COLORSET_MEDIUM 51
#define HE_COLORSET_NORMAL 52
#define HE_COLORSET_SELECTED 53
#define HE_COLORSET_CODE 54
struct ui_browser {
u64 index, top_idx;
void *top, *entries;
u16 y, x, width, height;
void *priv;
const char *title;
char *helpline;
unsigned int (*refresh)(struct ui_browser *self);
void (*write)(struct ui_browser *self, void *entry, int row);
void (*seek)(struct ui_browser *self, off_t offset, int whence);
bool (*filter)(struct ui_browser *self, void *entry);
u32 nr_entries;
bool navkeypressed;
bool use_navkeypressed;
};
void ui_browser__set_color(struct ui_browser *self, int color);
void ui_browser__set_percent_color(struct ui_browser *self,
double percent, bool current);
bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row);
void ui_browser__refresh_dimensions(struct ui_browser *self);
void ui_browser__reset_index(struct ui_browser *self);
void ui_browser__gotorc(struct ui_browser *self, int y, int x);
void __ui_browser__show_title(struct ui_browser *browser, const char *title);
void ui_browser__show_title(struct ui_browser *browser, const char *title);
int ui_browser__show(struct ui_browser *self, const char *title,
const char *helpline, ...);
void ui_browser__hide(struct ui_browser *self);
int ui_browser__refresh(struct ui_browser *self);
int ui_browser__run(struct ui_browser *browser, int delay_secs);
void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries);
void ui_browser__handle_resize(struct ui_browser *browser);
int ui_browser__warning(struct ui_browser *browser, int timeout,
const char *format, ...);
int ui_browser__help_window(struct ui_browser *browser, const char *text);
bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text);
int ui_browser__input_window(const char *title, const char *text, char *input,
const char *exit_msg, int delay_sec);
void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence);
unsigned int ui_browser__argv_refresh(struct ui_browser *browser);
void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence);
unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self);
void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence);
unsigned int ui_browser__list_head_refresh(struct ui_browser *self);
void ui_browser__init(void);
#endif /* _PERF_UI_BROWSER_H_ */

View File

@@ -0,0 +1,433 @@
#include "../../util.h"
#include "../browser.h"
#include "../helpline.h"
#include "../libslang.h"
#include "../ui.h"
#include "../util.h"
#include "../../annotate.h"
#include "../../hist.h"
#include "../../sort.h"
#include "../../symbol.h"
#include <pthread.h>
#include <newt.h>
struct annotate_browser {
struct ui_browser b;
struct rb_root entries;
struct rb_node *curr_hot;
struct objdump_line *selection;
int nr_asm_entries;
int nr_entries;
bool hide_src_code;
};
struct objdump_line_rb_node {
struct rb_node rb_node;
double percent;
u32 idx;
int idx_asm;
};
static inline
struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
{
return (struct objdump_line_rb_node *)(self + 1);
}
static bool objdump_line__filter(struct ui_browser *browser, void *entry)
{
struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
if (ab->hide_src_code) {
struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
return ol->offset == -1;
}
return false;
}
static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
{
struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
bool current_entry = ui_browser__is_current_entry(self, row);
int width = self->width;
if (ol->offset != -1) {
struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
ui_browser__set_percent_color(self, olrb->percent, current_entry);
slsmg_printf(" %7.2f ", olrb->percent);
} else {
ui_browser__set_percent_color(self, 0, current_entry);
slsmg_write_nstring(" ", 9);
}
SLsmg_write_char(':');
slsmg_write_nstring(" ", 8);
/* The scroll bar isn't being used */
if (!self->navkeypressed)
width += 1;
if (!ab->hide_src_code && ol->offset != -1)
if (!current_entry || (self->use_navkeypressed &&
!self->navkeypressed))
ui_browser__set_color(self, HE_COLORSET_CODE);
if (!*ol->line)
slsmg_write_nstring(" ", width - 18);
else
slsmg_write_nstring(ol->line, width - 18);
if (current_entry)
ab->selection = ol;
}
static double objdump_line__calc_percent(struct objdump_line *self,
struct symbol *sym, int evidx)
{
double percent = 0.0;
if (self->offset != -1) {
int len = sym->end - sym->start;
unsigned int hits = 0;
struct annotation *notes = symbol__annotation(sym);
struct source_line *src_line = notes->src->lines;
struct sym_hist *h = annotation__histogram(notes, evidx);
s64 offset = self->offset;
struct objdump_line *next;
next = objdump__get_next_ip_line(&notes->src->source, self);
while (offset < (s64)len &&
(next == NULL || offset < next->offset)) {
if (src_line) {
percent += src_line[offset].percent;
} else
hits += h->addr[offset];
++offset;
}
/*
* If the percentage wasn't already calculated in
* symbol__get_source_line, do it now:
*/
if (src_line == NULL && h->sum)
percent = 100.0 * hits / h->sum;
}
return percent;
}
static void objdump__insert_line(struct rb_root *self,
struct objdump_line_rb_node *line)
{
struct rb_node **p = &self->rb_node;
struct rb_node *parent = NULL;
struct objdump_line_rb_node *l;
while (*p != NULL) {
parent = *p;
l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
if (line->percent < l->percent)
p = &(*p)->rb_left;
else
p = &(*p)->rb_right;
}
rb_link_node(&line->rb_node, parent, p);
rb_insert_color(&line->rb_node, self);
}
static void annotate_browser__set_top(struct annotate_browser *self,
struct rb_node *nd)
{
struct objdump_line_rb_node *rbpos;
struct objdump_line *pos;
unsigned back;
ui_browser__refresh_dimensions(&self->b);
back = self->b.height / 2;
rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
pos = ((struct objdump_line *)rbpos) - 1;
self->b.top_idx = self->b.index = rbpos->idx;
while (self->b.top_idx != 0 && back != 0) {
pos = list_entry(pos->node.prev, struct objdump_line, node);
--self->b.top_idx;
--back;
}
self->b.top = pos;
self->curr_hot = nd;
}
static void annotate_browser__calc_percent(struct annotate_browser *browser,
int evidx)
{
struct map_symbol *ms = browser->b.priv;
struct symbol *sym = ms->sym;
struct annotation *notes = symbol__annotation(sym);
struct objdump_line *pos;
browser->entries = RB_ROOT;
pthread_mutex_lock(&notes->lock);
list_for_each_entry(pos, &notes->src->source, node) {
struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
if (rbpos->percent < 0.01) {
RB_CLEAR_NODE(&rbpos->rb_node);
continue;
}
objdump__insert_line(&browser->entries, rbpos);
}
pthread_mutex_unlock(&notes->lock);
browser->curr_hot = rb_last(&browser->entries);
}
static bool annotate_browser__toggle_source(struct annotate_browser *browser)
{
struct objdump_line *ol;
struct objdump_line_rb_node *olrb;
off_t offset = browser->b.index - browser->b.top_idx;
browser->b.seek(&browser->b, offset, SEEK_CUR);
ol = list_entry(browser->b.top, struct objdump_line, node);
olrb = objdump_line__rb(ol);
if (browser->hide_src_code) {
if (olrb->idx_asm < offset)
offset = olrb->idx;
browser->b.nr_entries = browser->nr_entries;
browser->hide_src_code = false;
browser->b.seek(&browser->b, -offset, SEEK_CUR);
browser->b.top_idx = olrb->idx - offset;
browser->b.index = olrb->idx;
} else {
if (olrb->idx_asm < 0) {
ui_helpline__puts("Only available for assembly lines.");
browser->b.seek(&browser->b, -offset, SEEK_CUR);
return false;
}
if (olrb->idx_asm < offset)
offset = olrb->idx_asm;
browser->b.nr_entries = browser->nr_asm_entries;
browser->hide_src_code = true;
browser->b.seek(&browser->b, -offset, SEEK_CUR);
browser->b.top_idx = olrb->idx_asm - offset;
browser->b.index = olrb->idx_asm;
}
return true;
}
static int annotate_browser__run(struct annotate_browser *self, int evidx,
void(*timer)(void *arg),
void *arg, int delay_secs)
{
struct rb_node *nd = NULL;
struct map_symbol *ms = self->b.priv;
struct symbol *sym = ms->sym;
const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
"H: Go to hottest line, ->/ENTER: Line action, "
"S: Toggle source code view";
int key;
if (ui_browser__show(&self->b, sym->name, help) < 0)
return -1;
annotate_browser__calc_percent(self, evidx);
if (self->curr_hot)
annotate_browser__set_top(self, self->curr_hot);
nd = self->curr_hot;
while (1) {
key = ui_browser__run(&self->b, delay_secs);
if (delay_secs != 0) {
annotate_browser__calc_percent(self, evidx);
/*
* Current line focus got out of the list of most active
* lines, NULL it so that if TAB|UNTAB is pressed, we
* move to curr_hot (current hottest line).
*/
if (nd != NULL && RB_EMPTY_NODE(nd))
nd = NULL;
}
switch (key) {
case K_TIMER:
if (timer != NULL)
timer(arg);
if (delay_secs != 0)
symbol__annotate_decay_histogram(sym, evidx);
continue;
case K_TAB:
if (nd != NULL) {
nd = rb_prev(nd);
if (nd == NULL)
nd = rb_last(&self->entries);
} else
nd = self->curr_hot;
break;
case K_UNTAB:
if (nd != NULL)
nd = rb_next(nd);
if (nd == NULL)
nd = rb_first(&self->entries);
else
nd = self->curr_hot;
break;
case 'H':
case 'h':
nd = self->curr_hot;
break;
case 'S':
case 's':
if (annotate_browser__toggle_source(self))
ui_helpline__puts(help);
continue;
case K_ENTER:
case K_RIGHT:
if (self->selection == NULL) {
ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
continue;
}
if (self->selection->offset == -1) {
ui_helpline__puts("Actions are only available for assembly lines.");
continue;
} else {
char *s = strstr(self->selection->line, "callq ");
struct annotation *notes;
struct symbol *target;
u64 ip;
if (s == NULL) {
ui_helpline__puts("Actions are only available for the 'callq' instruction.");
continue;
}
s = strchr(s, ' ');
if (s++ == NULL) {
ui_helpline__puts("Invallid callq instruction.");
continue;
}
ip = strtoull(s, NULL, 16);
ip = ms->map->map_ip(ms->map, ip);
target = map__find_symbol(ms->map, ip, NULL);
if (target == NULL) {
ui_helpline__puts("The called function was not found.");
continue;
}
notes = symbol__annotation(target);
pthread_mutex_lock(&notes->lock);
if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
pthread_mutex_unlock(&notes->lock);
ui__warning("Not enough memory for annotating '%s' symbol!\n",
target->name);
continue;
}
pthread_mutex_unlock(&notes->lock);
symbol__tui_annotate(target, ms->map, evidx,
timer, arg, delay_secs);
ui_browser__show_title(&self->b, sym->name);
}
continue;
case K_LEFT:
case K_ESC:
case 'q':
case CTRL('c'):
goto out;
default:
continue;
}
if (nd != NULL)
annotate_browser__set_top(self, nd);
}
out:
ui_browser__hide(&self->b);
return key;
}
int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
void(*timer)(void *arg), void *arg, int delay_secs)
{
return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
timer, arg, delay_secs);
}
int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
void(*timer)(void *arg), void *arg,
int delay_secs)
{
struct objdump_line *pos, *n;
struct annotation *notes;
struct map_symbol ms = {
.map = map,
.sym = sym,
};
struct annotate_browser browser = {
.b = {
.refresh = ui_browser__list_head_refresh,
.seek = ui_browser__list_head_seek,
.write = annotate_browser__write,
.filter = objdump_line__filter,
.priv = &ms,
.use_navkeypressed = true,
},
};
int ret;
if (sym == NULL)
return -1;
if (map->dso->annotate_warned)
return -1;
if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
ui__error("%s", ui_helpline__last_msg);
return -1;
}
ui_helpline__push("Press <- or ESC to exit");
notes = symbol__annotation(sym);
list_for_each_entry(pos, &notes->src->source, node) {
struct objdump_line_rb_node *rbpos;
size_t line_len = strlen(pos->line);
if (browser.b.width < line_len)
browser.b.width = line_len;
rbpos = objdump_line__rb(pos);
rbpos->idx = browser.nr_entries++;
if (pos->offset != -1)
rbpos->idx_asm = browser.nr_asm_entries++;
else
rbpos->idx_asm = -1;
}
browser.b.nr_entries = browser.nr_entries;
browser.b.entries = &notes->src->source,
browser.b.width += 18; /* Percentage */
ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
list_for_each_entry_safe(pos, n, &notes->src->source, node) {
list_del(&pos->node);
objdump_line__free(pos);
}
return ret;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,154 @@
#include "../libslang.h"
#include <elf.h>
#include <newt.h>
#include <inttypes.h>
#include <sys/ttydefaults.h>
#include <string.h>
#include <linux/bitops.h>
#include "../../util.h"
#include "../../debug.h"
#include "../../symbol.h"
#include "../browser.h"
#include "../helpline.h"
#include "map.h"
static int ui_entry__read(const char *title, char *bf, size_t size, int width)
{
struct newtExitStruct es;
newtComponent form, entry;
const char *result;
int err = -1;
newtCenteredWindow(width, 1, title);
form = newtForm(NULL, NULL, 0);
if (form == NULL)
return -1;
entry = newtEntry(0, 0, "0x", width, &result, NEWT_FLAG_SCROLL);
if (entry == NULL)
goto out_free_form;
newtFormAddComponent(form, entry);
newtFormAddHotKey(form, NEWT_KEY_ENTER);
newtFormAddHotKey(form, NEWT_KEY_ESCAPE);
newtFormAddHotKey(form, NEWT_KEY_LEFT);
newtFormAddHotKey(form, CTRL('c'));
newtFormRun(form, &es);
if (result != NULL) {
strncpy(bf, result, size);
err = 0;
}
out_free_form:
newtPopWindow();
newtFormDestroy(form);
return err;
}
struct map_browser {
struct ui_browser b;
struct map *map;
u8 addrlen;
};
static void map_browser__write(struct ui_browser *self, void *nd, int row)
{
struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
struct map_browser *mb = container_of(self, struct map_browser, b);
bool current_entry = ui_browser__is_current_entry(self, row);
int width;
ui_browser__set_percent_color(self, 0, current_entry);
slsmg_printf("%*" PRIx64 " %*" PRIx64 " %c ",
mb->addrlen, sym->start, mb->addrlen, sym->end,
sym->binding == STB_GLOBAL ? 'g' :
sym->binding == STB_LOCAL ? 'l' : 'w');
width = self->width - ((mb->addrlen * 2) + 4);
if (width > 0)
slsmg_write_nstring(sym->name, width);
}
/* FIXME uber-kludgy, see comment on cmd_report... */
static u32 *symbol__browser_index(struct symbol *self)
{
return ((void *)self) - sizeof(struct rb_node) - sizeof(u32);
}
static int map_browser__search(struct map_browser *self)
{
char target[512];
struct symbol *sym;
int err = ui_entry__read("Search by name/addr", target, sizeof(target), 40);
if (err)
return err;
if (target[0] == '0' && tolower(target[1]) == 'x') {
u64 addr = strtoull(target, NULL, 16);
sym = map__find_symbol(self->map, addr, NULL);
} else
sym = map__find_symbol_by_name(self->map, target, NULL);
if (sym != NULL) {
u32 *idx = symbol__browser_index(sym);
self->b.top = &sym->rb_node;
self->b.index = self->b.top_idx = *idx;
} else
ui_helpline__fpush("%s not found!", target);
return 0;
}
static int map_browser__run(struct map_browser *self)
{
int key;
if (ui_browser__show(&self->b, self->map->dso->long_name,
"Press <- or ESC to exit, %s / to search",
verbose ? "" : "restart with -v to use") < 0)
return -1;
while (1) {
key = ui_browser__run(&self->b, 0);
if (verbose && key == '/')
map_browser__search(self);
else
break;
}
ui_browser__hide(&self->b);
return key;
}
int map__browse(struct map *self)
{
struct map_browser mb = {
.b = {
.entries = &self->dso->symbols[self->type],
.refresh = ui_browser__rb_tree_refresh,
.seek = ui_browser__rb_tree_seek,
.write = map_browser__write,
},
.map = self,
};
struct rb_node *nd;
char tmp[BITS_PER_LONG / 4];
u64 maxaddr = 0;
for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
if (maxaddr < pos->end)
maxaddr = pos->end;
if (verbose) {
u32 *idx = symbol__browser_index(pos);
*idx = mb.b.nr_entries;
}
++mb.b.nr_entries;
}
mb.addrlen = snprintf(tmp, sizeof(tmp), "%" PRIx64, maxaddr);
return map_browser__run(&mb);
}

View File

@@ -0,0 +1,6 @@
#ifndef _PERF_UI_MAP_BROWSER_H_
#define _PERF_UI_MAP_BROWSER_H_ 1
struct map;
int map__browse(struct map *self);
#endif /* _PERF_UI_MAP_BROWSER_H_ */

View File

@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../debug.h"
#include "helpline.h"
#include "ui.h"
#include "libslang.h"
void ui_helpline__pop(void)
{
}
char ui_helpline__current[512];
void ui_helpline__push(const char *msg)
{
const size_t sz = sizeof(ui_helpline__current);
SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
SLsmg_set_color(0);
SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols);
SLsmg_refresh();
strncpy(ui_helpline__current, msg, sz)[sz - 1] = '\0';
}
void ui_helpline__vpush(const char *fmt, va_list ap)
{
char *s;
if (vasprintf(&s, fmt, ap) < 0)
vfprintf(stderr, fmt, ap);
else {
ui_helpline__push(s);
free(s);
}
}
void ui_helpline__fpush(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
ui_helpline__vpush(fmt, ap);
va_end(ap);
}
void ui_helpline__puts(const char *msg)
{
ui_helpline__pop();
ui_helpline__push(msg);
}
void ui_helpline__init(void)
{
ui_helpline__puts(" ");
}
char ui_helpline__last_msg[1024];
int ui_helpline__show_help(const char *format, va_list ap)
{
int ret;
static int backlog;
pthread_mutex_lock(&ui__lock);
ret = vscnprintf(ui_helpline__last_msg + backlog,
sizeof(ui_helpline__last_msg) - backlog, format, ap);
backlog += ret;
if (ui_helpline__last_msg[backlog - 1] == '\n') {
ui_helpline__puts(ui_helpline__last_msg);
SLsmg_refresh();
backlog = 0;
}
pthread_mutex_unlock(&ui__lock);
return ret;
}

View File

@@ -0,0 +1,16 @@
#ifndef _PERF_UI_HELPLINE_H_
#define _PERF_UI_HELPLINE_H_ 1
#include <stdio.h>
#include <stdarg.h>
void ui_helpline__init(void);
void ui_helpline__pop(void);
void ui_helpline__push(const char *msg);
void ui_helpline__vpush(const char *fmt, va_list ap);
void ui_helpline__fpush(const char *fmt, ...);
void ui_helpline__puts(const char *msg);
extern char ui_helpline__current[];
#endif /* _PERF_UI_HELPLINE_H_ */

View File

@@ -0,0 +1,27 @@
#ifndef _PERF_KEYSYMS_H_
#define _PERF_KEYSYMS_H_ 1
#include "libslang.h"
#define K_DOWN SL_KEY_DOWN
#define K_END SL_KEY_END
#define K_ENTER '\r'
#define K_ESC 033
#define K_F1 SL_KEY_F(1)
#define K_HOME SL_KEY_HOME
#define K_LEFT SL_KEY_LEFT
#define K_PGDN SL_KEY_NPAGE
#define K_PGUP SL_KEY_PPAGE
#define K_RIGHT SL_KEY_RIGHT
#define K_TAB '\t'
#define K_UNTAB SL_KEY_UNTAB
#define K_UP SL_KEY_UP
#define K_BKSPC 0x7f
#define K_DEL SL_KEY_DELETE
/* Not really keys */
#define K_TIMER -1
#define K_ERROR -2
#define K_RESIZE -3
#endif /* _PERF_KEYSYMS_H_ */

View File

@@ -0,0 +1,29 @@
#ifndef _PERF_UI_SLANG_H_
#define _PERF_UI_SLANG_H_ 1
/*
* slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
* the build if it isn't defined. Use the equivalent one that glibc
* has on features.h.
*/
#include <features.h>
#ifndef HAVE_LONG_LONG
#define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
#endif
#include <slang.h>
#if SLANG_VERSION < 20104
#define slsmg_printf(msg, args...) \
SLsmg_printf((char *)(msg), ##args)
#define slsmg_write_nstring(msg, len) \
SLsmg_write_nstring((char *)(msg), len)
#define sltt_set_color(obj, name, fg, bg) \
SLtt_set_color(obj,(char *)(name), (char *)(fg), (char *)(bg))
#else
#define slsmg_printf SLsmg_printf
#define slsmg_write_nstring SLsmg_write_nstring
#define sltt_set_color SLtt_set_color
#endif
#define SL_KEY_UNTAB 0x1000
#endif /* _PERF_UI_SLANG_H_ */

View File

@@ -0,0 +1,32 @@
#include "../cache.h"
#include "progress.h"
#include "libslang.h"
#include "ui.h"
#include "browser.h"
void ui_progress__update(u64 curr, u64 total, const char *title)
{
int bar, y;
/*
* FIXME: We should have a per UI backend way of showing progress,
* stdio will just show a percentage as NN%, etc.
*/
if (use_browser <= 0)
return;
if (total == 0)
return;
ui__refresh_dimensions(true);
pthread_mutex_lock(&ui__lock);
y = SLtt_Screen_Rows / 2 - 2;
SLsmg_set_color(0);
SLsmg_draw_box(y, 0, 3, SLtt_Screen_Cols);
SLsmg_gotorc(y++, 1);
SLsmg_write_string((char *)title);
SLsmg_set_color(HE_COLORSET_SELECTED);
bar = ((SLtt_Screen_Cols - 2) * curr) / total;
SLsmg_fill_region(y, 1, 1, bar, ' ');
SLsmg_refresh();
pthread_mutex_unlock(&ui__lock);
}

View File

@@ -0,0 +1,8 @@
#ifndef _PERF_UI_PROGRESS_H_
#define _PERF_UI_PROGRESS_H_ 1
#include <../types.h>
void ui_progress__update(u64 curr, u64 total, const char *title);
#endif

View File

@@ -0,0 +1,155 @@
#include <newt.h>
#include <signal.h>
#include <stdbool.h>
#include "../cache.h"
#include "../debug.h"
#include "browser.h"
#include "helpline.h"
#include "ui.h"
#include "util.h"
#include "libslang.h"
#include "keysyms.h"
pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
static volatile int ui__need_resize;
void ui__refresh_dimensions(bool force)
{
if (force || ui__need_resize) {
ui__need_resize = 0;
pthread_mutex_lock(&ui__lock);
SLtt_get_screen_size();
SLsmg_reinit_smg();
pthread_mutex_unlock(&ui__lock);
}
}
static void ui__sigwinch(int sig __used)
{
ui__need_resize = 1;
}
static void ui__setup_sigwinch(void)
{
static bool done;
if (done)
return;
done = true;
pthread__unblock_sigwinch();
signal(SIGWINCH, ui__sigwinch);
}
int ui__getch(int delay_secs)
{
struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
fd_set read_set;
int err, key;
ui__setup_sigwinch();
FD_ZERO(&read_set);
FD_SET(0, &read_set);
if (delay_secs) {
timeout.tv_sec = delay_secs;
timeout.tv_usec = 0;
}
err = select(1, &read_set, NULL, NULL, ptimeout);
if (err == 0)
return K_TIMER;
if (err == -1) {
if (errno == EINTR)
return K_RESIZE;
return K_ERROR;
}
key = SLang_getkey();
if (key != K_ESC)
return key;
FD_ZERO(&read_set);
FD_SET(0, &read_set);
timeout.tv_sec = 0;
timeout.tv_usec = 20;
err = select(1, &read_set, NULL, NULL, &timeout);
if (err == 0)
return K_ESC;
SLang_ungetkey(key);
return SLkp_getkey();
}
static void newt_suspend(void *d __used)
{
newtSuspend();
raise(SIGTSTP);
newtResume();
}
static int ui__init(void)
{
int err = SLkp_init();
if (err < 0)
goto out;
SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
out:
return err;
}
static void ui__exit(void)
{
SLtt_set_cursor_visibility(1);
SLsmg_refresh();
SLsmg_reset_smg();
SLang_reset_tty();
}
static void ui__signal(int sig)
{
ui__exit();
psignal(sig, "perf");
exit(0);
}
void setup_browser(bool fallback_to_pager)
{
if (!isatty(1) || !use_browser || dump_trace) {
use_browser = 0;
if (fallback_to_pager)
setup_pager();
return;
}
use_browser = 1;
newtInit();
ui__init();
newtSetSuspendCallback(newt_suspend, NULL);
ui_helpline__init();
ui_browser__init();
signal(SIGSEGV, ui__signal);
signal(SIGFPE, ui__signal);
signal(SIGINT, ui__signal);
signal(SIGQUIT, ui__signal);
signal(SIGTERM, ui__signal);
}
void exit_browser(bool wait_for_ok)
{
if (use_browser > 0) {
if (wait_for_ok)
ui__question_window("Fatal Error",
ui_helpline__last_msg,
"Press any key...", 0);
ui__exit();
}
}

View File

@@ -0,0 +1,11 @@
#ifndef _PERF_UI_H_
#define _PERF_UI_H_ 1
#include <pthread.h>
#include <stdbool.h>
extern pthread_mutex_t ui__lock;
void ui__refresh_dimensions(bool force);
#endif /* _PERF_UI_H_ */

View File

@@ -0,0 +1,250 @@
#include "../util.h"
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <sys/ttydefaults.h>
#include "../cache.h"
#include "../debug.h"
#include "browser.h"
#include "keysyms.h"
#include "helpline.h"
#include "ui.h"
#include "util.h"
#include "libslang.h"
static void ui_browser__argv_write(struct ui_browser *browser,
void *entry, int row)
{
char **arg = entry;
bool current_entry = ui_browser__is_current_entry(browser, row);
ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
HE_COLORSET_NORMAL);
slsmg_write_nstring(*arg, browser->width);
}
static int popup_menu__run(struct ui_browser *menu)
{
int key;
if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
return -1;
while (1) {
key = ui_browser__run(menu, 0);
switch (key) {
case K_RIGHT:
case K_ENTER:
key = menu->index;
break;
case K_LEFT:
case K_ESC:
case 'q':
case CTRL('c'):
key = -1;
break;
default:
continue;
}
break;
}
ui_browser__hide(menu);
return key;
}
int ui__popup_menu(int argc, char * const argv[])
{
struct ui_browser menu = {
.entries = (void *)argv,
.refresh = ui_browser__argv_refresh,
.seek = ui_browser__argv_seek,
.write = ui_browser__argv_write,
.nr_entries = argc,
};
return popup_menu__run(&menu);
}
int ui_browser__input_window(const char *title, const char *text, char *input,
const char *exit_msg, int delay_secs)
{
int x, y, len, key;
int max_len = 60, nr_lines = 0;
static char buf[50];
const char *t;
t = text;
while (1) {
const char *sep = strchr(t, '\n');
if (sep == NULL)
sep = strchr(t, '\0');
len = sep - t;
if (max_len < len)
max_len = len;
++nr_lines;
if (*sep == '\0')
break;
t = sep + 1;
}
max_len += 2;
nr_lines += 8;
y = SLtt_Screen_Rows / 2 - nr_lines / 2;
x = SLtt_Screen_Cols / 2 - max_len / 2;
SLsmg_set_color(0);
SLsmg_draw_box(y, x++, nr_lines, max_len);
if (title) {
SLsmg_gotorc(y, x + 1);
SLsmg_write_string((char *)title);
}
SLsmg_gotorc(++y, x);
nr_lines -= 7;
max_len -= 2;
SLsmg_write_wrapped_string((unsigned char *)text, y, x,
nr_lines, max_len, 1);
y += nr_lines;
len = 5;
while (len--) {
SLsmg_gotorc(y + len - 1, x);
SLsmg_write_nstring((char *)" ", max_len);
}
SLsmg_draw_box(y++, x + 1, 3, max_len - 2);
SLsmg_gotorc(y + 3, x);
SLsmg_write_nstring((char *)exit_msg, max_len);
SLsmg_refresh();
x += 2;
len = 0;
key = ui__getch(delay_secs);
while (key != K_TIMER && key != K_ENTER && key != K_ESC) {
if (key == K_BKSPC) {
if (len == 0)
goto next_key;
SLsmg_gotorc(y, x + --len);
SLsmg_write_char(' ');
} else {
buf[len] = key;
SLsmg_gotorc(y, x + len++);
SLsmg_write_char(key);
}
SLsmg_refresh();
/* XXX more graceful overflow handling needed */
if (len == sizeof(buf) - 1) {
ui_helpline__push("maximum size of symbol name reached!");
key = K_ENTER;
break;
}
next_key:
key = ui__getch(delay_secs);
}
buf[len] = '\0';
strncpy(input, buf, len+1);
return key;
}
int ui__question_window(const char *title, const char *text,
const char *exit_msg, int delay_secs)
{
int x, y;
int max_len = 0, nr_lines = 0;
const char *t;
t = text;
while (1) {
const char *sep = strchr(t, '\n');
int len;
if (sep == NULL)
sep = strchr(t, '\0');
len = sep - t;
if (max_len < len)
max_len = len;
++nr_lines;
if (*sep == '\0')
break;
t = sep + 1;
}
max_len += 2;
nr_lines += 4;
y = SLtt_Screen_Rows / 2 - nr_lines / 2,
x = SLtt_Screen_Cols / 2 - max_len / 2;
SLsmg_set_color(0);
SLsmg_draw_box(y, x++, nr_lines, max_len);
if (title) {
SLsmg_gotorc(y, x + 1);
SLsmg_write_string((char *)title);
}
SLsmg_gotorc(++y, x);
nr_lines -= 2;
max_len -= 2;
SLsmg_write_wrapped_string((unsigned char *)text, y, x,
nr_lines, max_len, 1);
SLsmg_gotorc(y + nr_lines - 2, x);
SLsmg_write_nstring((char *)" ", max_len);
SLsmg_gotorc(y + nr_lines - 1, x);
SLsmg_write_nstring((char *)exit_msg, max_len);
SLsmg_refresh();
return ui__getch(delay_secs);
}
int ui__help_window(const char *text)
{
return ui__question_window("Help", text, "Press any key...", 0);
}
int ui__dialog_yesno(const char *msg)
{
return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
}
int __ui__warning(const char *title, const char *format, va_list args)
{
char *s;
if (use_browser > 0 && vasprintf(&s, format, args) > 0) {
int key;
pthread_mutex_lock(&ui__lock);
key = ui__question_window(title, s, "Press any key...", 0);
pthread_mutex_unlock(&ui__lock);
free(s);
return key;
}
fprintf(stderr, "%s:\n", title);
vfprintf(stderr, format, args);
return K_ESC;
}
int ui__warning(const char *format, ...)
{
int key;
va_list args;
va_start(args, format);
key = __ui__warning("Warning", format, args);
va_end(args);
return key;
}
int ui__error(const char *format, ...)
{
int key;
va_list args;
va_start(args, format);
key = __ui__warning("Error", format, args);
va_end(args);
return key;
}

View File

@@ -0,0 +1,14 @@
#ifndef _PERF_UI_UTIL_H_
#define _PERF_UI_UTIL_H_ 1
#include <stdarg.h>
int ui__getch(int delay_secs);
int ui__popup_menu(int argc, char * const argv[]);
int ui__help_window(const char *text);
int ui__dialog_yesno(const char *msg);
int ui__question_window(const char *title, const char *text,
const char *exit_msg, int delay_secs);
int __ui__warning(const char *title, const char *format, va_list args);
#endif /* _PERF_UI_UTIL_H_ */