18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <elf.h>
38c2ecf20Sopenharmony_ci#include <inttypes.h>
48c2ecf20Sopenharmony_ci#include <sys/ttydefaults.h>
58c2ecf20Sopenharmony_ci#include <stdlib.h>
68c2ecf20Sopenharmony_ci#include <string.h>
78c2ecf20Sopenharmony_ci#include <linux/bitops.h>
88c2ecf20Sopenharmony_ci#include "../../util/debug.h"
98c2ecf20Sopenharmony_ci#include "../../util/map.h"
108c2ecf20Sopenharmony_ci#include "../../util/dso.h"
118c2ecf20Sopenharmony_ci#include "../../util/symbol.h"
128c2ecf20Sopenharmony_ci#include "../browser.h"
138c2ecf20Sopenharmony_ci#include "../helpline.h"
148c2ecf20Sopenharmony_ci#include "../keysyms.h"
158c2ecf20Sopenharmony_ci#include "map.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/ctype.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct map_browser {
208c2ecf20Sopenharmony_ci	struct ui_browser b;
218c2ecf20Sopenharmony_ci	struct map	  *map;
228c2ecf20Sopenharmony_ci	u8		  addrlen;
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic void map_browser__write(struct ui_browser *browser, void *nd, int row)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
288c2ecf20Sopenharmony_ci	struct map_browser *mb = container_of(browser, struct map_browser, b);
298c2ecf20Sopenharmony_ci	bool current_entry = ui_browser__is_current_entry(browser, row);
308c2ecf20Sopenharmony_ci	int width;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	ui_browser__set_percent_color(browser, 0, current_entry);
338c2ecf20Sopenharmony_ci	ui_browser__printf(browser, "%*" PRIx64 " %*" PRIx64 " %c ",
348c2ecf20Sopenharmony_ci			   mb->addrlen, sym->start, mb->addrlen, sym->end,
358c2ecf20Sopenharmony_ci			   sym->binding == STB_GLOBAL ? 'g' :
368c2ecf20Sopenharmony_ci				sym->binding == STB_LOCAL  ? 'l' : 'w');
378c2ecf20Sopenharmony_ci	width = browser->width - ((mb->addrlen * 2) + 4);
388c2ecf20Sopenharmony_ci	if (width > 0)
398c2ecf20Sopenharmony_ci		ui_browser__write_nstring(browser, sym->name, width);
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* FIXME uber-kludgy, see comment on cmd_report... */
438c2ecf20Sopenharmony_cistatic u32 *symbol__browser_index(struct symbol *browser)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic int map_browser__search(struct map_browser *browser)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	char target[512];
518c2ecf20Sopenharmony_ci	struct symbol *sym;
528c2ecf20Sopenharmony_ci	int err = ui_browser__input_window("Search by name/addr",
538c2ecf20Sopenharmony_ci					   "Prefix with 0x to search by address",
548c2ecf20Sopenharmony_ci					   target, "ENTER: OK, ESC: Cancel", 0);
558c2ecf20Sopenharmony_ci	if (err != K_ENTER)
568c2ecf20Sopenharmony_ci		return -1;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (target[0] == '0' && tolower(target[1]) == 'x') {
598c2ecf20Sopenharmony_ci		u64 addr = strtoull(target, NULL, 16);
608c2ecf20Sopenharmony_ci		sym = map__find_symbol(browser->map, addr);
618c2ecf20Sopenharmony_ci	} else
628c2ecf20Sopenharmony_ci		sym = map__find_symbol_by_name(browser->map, target);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	if (sym != NULL) {
658c2ecf20Sopenharmony_ci		u32 *idx = symbol__browser_index(sym);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci		browser->b.top = &sym->rb_node;
688c2ecf20Sopenharmony_ci		browser->b.index = browser->b.top_idx = *idx;
698c2ecf20Sopenharmony_ci	} else
708c2ecf20Sopenharmony_ci		ui_helpline__fpush("%s not found!", target);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return 0;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int map_browser__run(struct map_browser *browser)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	int key;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (ui_browser__show(&browser->b, browser->map->dso->long_name,
808c2ecf20Sopenharmony_ci			     "Press ESC to exit, %s / to search",
818c2ecf20Sopenharmony_ci			     verbose > 0 ? "" : "restart with -v to use") < 0)
828c2ecf20Sopenharmony_ci		return -1;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	while (1) {
858c2ecf20Sopenharmony_ci		key = ui_browser__run(&browser->b, 0);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci		switch (key) {
888c2ecf20Sopenharmony_ci		case '/':
898c2ecf20Sopenharmony_ci			if (verbose > 0)
908c2ecf20Sopenharmony_ci				map_browser__search(browser);
918c2ecf20Sopenharmony_ci		default:
928c2ecf20Sopenharmony_ci			break;
938c2ecf20Sopenharmony_ci                case K_LEFT:
948c2ecf20Sopenharmony_ci                case K_ESC:
958c2ecf20Sopenharmony_ci                case 'q':
968c2ecf20Sopenharmony_ci                case CTRL('c'):
978c2ecf20Sopenharmony_ci                        goto out;
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ciout:
1018c2ecf20Sopenharmony_ci	ui_browser__hide(&browser->b);
1028c2ecf20Sopenharmony_ci	return key;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciint map__browse(struct map *map)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct map_browser mb = {
1088c2ecf20Sopenharmony_ci		.b = {
1098c2ecf20Sopenharmony_ci			.entries = &map->dso->symbols,
1108c2ecf20Sopenharmony_ci			.refresh = ui_browser__rb_tree_refresh,
1118c2ecf20Sopenharmony_ci			.seek	 = ui_browser__rb_tree_seek,
1128c2ecf20Sopenharmony_ci			.write	 = map_browser__write,
1138c2ecf20Sopenharmony_ci		},
1148c2ecf20Sopenharmony_ci		.map = map,
1158c2ecf20Sopenharmony_ci	};
1168c2ecf20Sopenharmony_ci	struct rb_node *nd;
1178c2ecf20Sopenharmony_ci	char tmp[BITS_PER_LONG / 4];
1188c2ecf20Sopenharmony_ci	u64 maxaddr = 0;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
1218c2ecf20Sopenharmony_ci		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		if (maxaddr < pos->end)
1248c2ecf20Sopenharmony_ci			maxaddr = pos->end;
1258c2ecf20Sopenharmony_ci		if (verbose > 0) {
1268c2ecf20Sopenharmony_ci			u32 *idx = symbol__browser_index(pos);
1278c2ecf20Sopenharmony_ci			*idx = mb.b.nr_entries;
1288c2ecf20Sopenharmony_ci		}
1298c2ecf20Sopenharmony_ci		++mb.b.nr_entries;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	mb.addrlen = snprintf(tmp, sizeof(tmp), "%" PRIx64, maxaddr);
1338c2ecf20Sopenharmony_ci	return map_browser__run(&mb);
1348c2ecf20Sopenharmony_ci}
135