samples/bpf: move ksym_search() into library
authorAlexei Starovoitov <ast@fb.com>
Tue, 8 Mar 2016 05:57:19 +0000 (21:57 -0800)
committerDavid S. Miller <davem@davemloft.net>
Tue, 8 Mar 2016 20:28:32 +0000 (15:28 -0500)
move ksym search from offwaketime into library to be reused
in other tests

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
samples/bpf/bpf_load.c
samples/bpf/bpf_load.h
samples/bpf/offwaketime_user.c

index 816bca5760a09862d1cc58516fd8bf85c17a61e5..d16864293c00721f33ea6b945f4dbaaa9e0cce25 100644 (file)
@@ -346,3 +346,65 @@ void read_trace_pipe(void)
                }
        }
 }
+
+#define MAX_SYMS 300000
+static struct ksym syms[MAX_SYMS];
+static int sym_cnt;
+
+static int ksym_cmp(const void *p1, const void *p2)
+{
+       return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
+}
+
+int load_kallsyms(void)
+{
+       FILE *f = fopen("/proc/kallsyms", "r");
+       char func[256], buf[256];
+       char symbol;
+       void *addr;
+       int i = 0;
+
+       if (!f)
+               return -ENOENT;
+
+       while (!feof(f)) {
+               if (!fgets(buf, sizeof(buf), f))
+                       break;
+               if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
+                       break;
+               if (!addr)
+                       continue;
+               syms[i].addr = (long) addr;
+               syms[i].name = strdup(func);
+               i++;
+       }
+       sym_cnt = i;
+       qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
+       return 0;
+}
+
+struct ksym *ksym_search(long key)
+{
+       int start = 0, end = sym_cnt;
+       int result;
+
+       while (start < end) {
+               size_t mid = start + (end - start) / 2;
+
+               result = key - syms[mid].addr;
+               if (result < 0)
+                       end = mid;
+               else if (result > 0)
+                       start = mid + 1;
+               else
+                       return &syms[mid];
+       }
+
+       if (start >= 1 && syms[start - 1].addr < key &&
+           key < syms[start].addr)
+               /* valid ksym */
+               return &syms[start - 1];
+
+       /* out of range. return _stext */
+       return &syms[0];
+}
index cbd7c2b532b9a1a8e842c863f91b80d9bc79f42e..dfa57fe65c8e0f2eb67ae251705b631b1403fc55 100644 (file)
@@ -23,5 +23,11 @@ extern int event_fd[MAX_PROGS];
 int load_bpf_file(char *path);
 
 void read_trace_pipe(void);
+struct ksym {
+       long addr;
+       char *name;
+};
 
+int load_kallsyms(void);
+struct ksym *ksym_search(long key);
 #endif
index 17cf3024e22cc843ea614a731ec1551b93021da2..6f002a9c24faa38a81ba9b233519b6370478a2b0 100644 (file)
 #include "libbpf.h"
 #include "bpf_load.h"
 
-#define MAX_SYMS 300000
 #define PRINT_RAW_ADDR 0
 
-static struct ksym {
-       long addr;
-       char *name;
-} syms[MAX_SYMS];
-static int sym_cnt;
-
-static int ksym_cmp(const void *p1, const void *p2)
-{
-       return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
-}
-
-static int load_kallsyms(void)
-{
-       FILE *f = fopen("/proc/kallsyms", "r");
-       char func[256], buf[256];
-       char symbol;
-       void *addr;
-       int i = 0;
-
-       if (!f)
-               return -ENOENT;
-
-       while (!feof(f)) {
-               if (!fgets(buf, sizeof(buf), f))
-                       break;
-               if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
-                       break;
-               if (!addr)
-                       continue;
-               syms[i].addr = (long) addr;
-               syms[i].name = strdup(func);
-               i++;
-       }
-       sym_cnt = i;
-       qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
-       return 0;
-}
-
-static void *search(long key)
-{
-       int start = 0, end = sym_cnt;
-       int result;
-
-       while (start < end) {
-               size_t mid = start + (end - start) / 2;
-
-               result = key - syms[mid].addr;
-               if (result < 0)
-                       end = mid;
-               else if (result > 0)
-                       start = mid + 1;
-               else
-                       return &syms[mid];
-       }
-
-       if (start >= 1 && syms[start - 1].addr < key &&
-           key < syms[start].addr)
-               /* valid ksym */
-               return &syms[start - 1];
-
-       /* out of range. return _stext */
-       return &syms[0];
-}
-
 static void print_ksym(__u64 addr)
 {
        struct ksym *sym;
 
        if (!addr)
                return;
-       sym = search(addr);
+       sym = ksym_search(addr);
        if (PRINT_RAW_ADDR)
                printf("%s/%llx;", sym->name, addr);
        else