map.c 3.2 KB
Newer Older
1
#include <elf.h>
2
#include <inttypes.h>
3 4 5
#include <sys/ttydefaults.h>
#include <string.h>
#include <linux/bitops.h>
6 7 8
#include "../../util/util.h"
#include "../../util/debug.h"
#include "../../util/symbol.h"
9 10
#include "../browser.h"
#include "../helpline.h"
11
#include "../keysyms.h"
12 13 14 15 16 17 18 19
#include "map.h"

struct map_browser {
	struct ui_browser b;
	struct map	  *map;
	u8		  addrlen;
};

20
static void map_browser__write(struct ui_browser *browser, void *nd, int row)
21 22
{
	struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
23 24
	struct map_browser *mb = container_of(browser, struct map_browser, b);
	bool current_entry = ui_browser__is_current_entry(browser, row);
25
	int width;
26

27
	ui_browser__set_percent_color(browser, 0, current_entry);
28 29 30 31
	ui_browser__printf(browser, "%*" PRIx64 " %*" PRIx64 " %c ",
			   mb->addrlen, sym->start, mb->addrlen, sym->end,
			   sym->binding == STB_GLOBAL ? 'g' :
				sym->binding == STB_LOCAL  ? 'l' : 'w');
32
	width = browser->width - ((mb->addrlen * 2) + 4);
33
	if (width > 0)
34
		ui_browser__write_nstring(browser, sym->name, width);
35 36 37
}

/* FIXME uber-kludgy, see comment on cmd_report... */
38
static u32 *symbol__browser_index(struct symbol *browser)
39
{
40
	return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32);
41 42
}

43
static int map_browser__search(struct map_browser *browser)
44 45 46
{
	char target[512];
	struct symbol *sym;
47 48 49 50 51
	int err = ui_browser__input_window("Search by name/addr",
					   "Prefix with 0x to search by address",
					   target, "ENTER: OK, ESC: Cancel", 0);
	if (err != K_ENTER)
		return -1;
52 53 54

	if (target[0] == '0' && tolower(target[1]) == 'x') {
		u64 addr = strtoull(target, NULL, 16);
55
		sym = map__find_symbol(browser->map, addr, NULL);
56
	} else
57
		sym = map__find_symbol_by_name(browser->map, target, NULL);
58 59 60 61

	if (sym != NULL) {
		u32 *idx = symbol__browser_index(sym);

62 63
		browser->b.top = &sym->rb_node;
		browser->b.index = browser->b.top_idx = *idx;
64 65 66 67 68 69
	} else
		ui_helpline__fpush("%s not found!", target);

	return 0;
}

70
static int map_browser__run(struct map_browser *browser)
71
{
72 73
	int key;

74
	if (ui_browser__show(&browser->b, browser->map->dso->long_name,
75
			     "Press ESC to exit, %s / to search",
76
			     verbose ? "" : "restart with -v to use") < 0)
77 78 79
		return -1;

	while (1) {
80
		key = ui_browser__run(&browser->b, 0);
81

82 83 84
		switch (key) {
		case '/':
			if (verbose)
85
				map_browser__search(browser);
86
		default:
87
			break;
88 89 90 91 92 93
                case K_LEFT:
                case K_ESC:
                case 'q':
                case CTRL('c'):
                        goto out;
		}
94
	}
95
out:
96
	ui_browser__hide(&browser->b);
97
	return key;
98 99
}

100
int map__browse(struct map *map)
101 102 103
{
	struct map_browser mb = {
		.b = {
104
			.entries = &map->dso->symbols[map->type],
105 106 107 108
			.refresh = ui_browser__rb_tree_refresh,
			.seek	 = ui_browser__rb_tree_seek,
			.write	 = map_browser__write,
		},
109
		.map = map,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	};
	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;
	}

127
	mb.addrlen = snprintf(tmp, sizeof(tmp), "%" PRIx64, maxaddr);
128
	return map_browser__run(&mb);
129
}