perf symbols: Simplify symbol machinery setup

And also express its configuration toggles via a struct.

Now all one has to do is to call symbol__init(NULL) if the
defaults are OK, or pass a struct symbol_conf pointer with the
desired configuration.

If a tool uses kernel_maps__find_symbol() to look at the kernel
and modules mappings for a symbol but didn't call symbol__init()
first, that will generate a one time warning too, alerting the
subcommand developer that symbol__init() must be called.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
LKML-Reference: <1259071517-3242-2-git-send-email-acme@infradead.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
diff --git a/tools/perf/util/data_map.c b/tools/perf/util/data_map.c
index f318d19..b238462 100644
--- a/tools/perf/util/data_map.c
+++ b/tools/perf/util/data_map.c
@@ -101,8 +101,6 @@
 
 int mmap_dispatch_perf_file(struct perf_header **pheader,
 			    const char *input_name,
-			    const char *vmlinux_name,
-			    bool try_vmlinux_path,
 			    int force,
 			    int full_paths,
 			    int *cwdlen,
@@ -172,12 +170,6 @@
 	    curr_handler->sample_type_check(sample_type) < 0)
 		goto out_delete;
 
-	err = -ENOMEM;
-	if (kernel_maps__init(vmlinux_name, try_vmlinux_path, true) < 0) {
-		pr_err("failed to setup the kernel maps to resolve symbols\n");
-		goto out_delete;
-	}
-
 	if (!full_paths) {
 		if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
 			pr_err("failed to get the current directory\n");
diff --git a/tools/perf/util/data_map.h b/tools/perf/util/data_map.h
index 3f0d21b..ae036ec 100644
--- a/tools/perf/util/data_map.h
+++ b/tools/perf/util/data_map.h
@@ -23,8 +23,6 @@
 void register_perf_file_handler(struct perf_file_handler *handler);
 int mmap_dispatch_perf_file(struct perf_header **pheader,
 			    const char *input_name,
-			    const char *vmlinux_name,
-			    bool try_vmlinux_path,
 			    int force,
 			    int full_paths,
 			    int *cwdlen,
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 1332f8e..271a160 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -253,12 +253,6 @@
 
 		buildid_sec = &feat_sec[idx++];
 
-		/*
-		 * Read the kernel buildid nad the list of loaded modules with
-		 * its build_ids:
-		 */
-		kernel_maps__init(NULL, false, true);
-
 		/* Write build-ids */
 		buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
 		err = dsos__write_buildid_table(fd);
diff --git a/tools/perf/util/include/asm/bug.h b/tools/perf/util/include/asm/bug.h
new file mode 100644
index 0000000..7fcc681
--- /dev/null
+++ b/tools/perf/util/include/asm/bug.h
@@ -0,0 +1,22 @@
+#ifndef _PERF_ASM_GENERIC_BUG_H
+#define _PERF_ASM_GENERIC_BUG_H
+
+#define __WARN_printf(arg...)	do { fprintf(stderr, arg); } while (0)
+
+#define WARN(condition, format...) ({		\
+	int __ret_warn_on = !!(condition);	\
+	if (unlikely(__ret_warn_on))		\
+		__WARN_printf(format);		\
+	unlikely(__ret_warn_on);		\
+})
+
+#define WARN_ONCE(condition, format...)	({	\
+	static int __warned;			\
+	int __ret_warn_once = !!(condition);	\
+						\
+	if (unlikely(__ret_warn_once))		\
+		if (WARN(!__warned, format)) 	\
+			__warned = 1;		\
+	unlikely(__ret_warn_once);		\
+})
+#endif
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 44d81d5..c4ca974 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -6,6 +6,7 @@
 
 #include "debug.h"
 
+#include <asm/bug.h>
 #include <libelf.h>
 #include <gelf.h>
 #include <elf.h>
@@ -37,6 +38,11 @@
 static int vmlinux_path__nr_entries;
 static char **vmlinux_path;
 
+static struct symbol_conf symbol_conf__defaults = {
+	.use_modules	  = true,
+	.try_vmlinux_path = true,
+};
+
 static struct rb_root kernel_maps;
 
 static void dso__fixup_sym_end(struct dso *self)
@@ -1166,7 +1172,9 @@
 	if (map) {
 		ip = map->map_ip(map, ip);
 		return map__find_symbol(map, ip, filter);
-	}
+	} else
+		WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps),
+			  "Empty kernel_maps, was symbol__init() called?\n");
 
 	return NULL;
 }
@@ -1485,9 +1493,9 @@
 	return ret;
 }
 
-static int kernel_maps__create_kernel_map(const char *vmlinux_name)
+static int kernel_maps__create_kernel_map(const struct symbol_conf *conf)
 {
-	struct dso *kernel = dso__new(vmlinux_name ?: "[kernel.kallsyms]");
+	struct dso *kernel = dso__new(conf->vmlinux_name ?: "[kernel.kallsyms]");
 
 	if (kernel == NULL)
 		return -1;
@@ -1577,18 +1585,21 @@
 	return -1;
 }
 
-int kernel_maps__init(const char *vmlinux_name, bool try_vmlinux_path,
-		      bool use_modules)
+static int kernel_maps__init(const struct symbol_conf *conf)
 {
-	if (try_vmlinux_path && vmlinux_path__init() < 0)
+	const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
+
+	symbol__priv_size = pconf->priv_size;
+
+	if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
 		return -1;
 
-	if (kernel_maps__create_kernel_map(vmlinux_name) < 0) {
+	if (kernel_maps__create_kernel_map(pconf) < 0) {
 		vmlinux_path__exit();
 		return -1;
 	}
 
-	if (use_modules && kernel_maps__create_module_maps() < 0)
+	if (pconf->use_modules && kernel_maps__create_module_maps() < 0)
 		pr_debug("Failed to load list of modules in use, "
 			 "continuing...\n");
 	/*
@@ -1598,8 +1609,8 @@
 	return 0;
 }
 
-void symbol__init(unsigned int priv_size)
+int symbol__init(struct symbol_conf *conf)
 {
 	elf_version(EV_CURRENT);
-	symbol__priv_size = priv_size;
+	return kernel_maps__init(conf);
 }
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 8c4d026..5538691 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -49,6 +49,13 @@
 	char		name[0];
 };
 
+struct symbol_conf {
+	unsigned short	priv_size;
+	bool		try_vmlinux_path,
+			use_modules;
+	const char	*vmlinux_name;
+};
+
 extern unsigned int symbol__priv_size;
 
 static inline void *symbol__priv(struct symbol *self)
@@ -93,11 +100,9 @@
 bool dsos__read_build_ids(void);
 int build_id__sprintf(u8 *self, int len, char *bf);
 
-int kernel_maps__init(const char *vmlinux_name, bool try_vmlinux_path,
-		      bool use_modules);
 size_t kernel_maps__fprintf(FILE *fp);
 
-void symbol__init(unsigned int priv_size);
+int symbol__init(struct symbol_conf *conf);
 
 extern struct list_head dsos;
 extern struct map *kernel_map;