Whamcloud - gitweb
- make HEAD from b_post_cmd3
[fs/lustre-release.git] / lustre / tests / gensymmap.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <sys/utsname.h>
4 #include <string.h>
5
6 struct file_addr {
7         char path[256];
8         char modname[32];
9         unsigned long base; 
10 };
11
12 int print_symbol_address(struct file_addr * fa)
13 {
14         char buffer[4096];
15         char cmd[256];
16         char func_name[256];
17         unsigned long addr;
18         char mode[256];
19         FILE *file;
20
21         sprintf(cmd, "modprobe -l %s", fa->modname);
22         file = popen(cmd, "r");
23         if (!file) {
24                 printf("failed to execute %s:%s\n."
25                        "Have you installed modules?\n", 
26                         cmd, strerror(errno));
27                 pclose(file);
28                 return -1;
29         }
30         if (fgets(buffer, 4095, file) == NULL) {
31                 printf("failed to get modprobe ouput for %s:%s\n", 
32                         fa->modname, strerror(errno));
33                 pclose(file);
34                 return -1;
35         }
36         pclose(file);
37
38         sprintf(cmd, "nm -n %s", buffer);
39         file = popen(cmd, "r");
40         if (!file) {
41                 printf("failed to execute %s:%s\n."
42                        "Have you installed modules?\n", 
43                         cmd, strerror(errno));
44                 return -1;
45         }
46
47         while (fgets(buffer, 4095, file)) {
48                 if (fscanf(file, "%x %s %s\n", &addr, mode, func_name) != 3)
49                         continue;
50
51                 /* only list symbol in text section. */
52                 if (strcasecmp(mode, "t") == 0) {
53                         /* skip __init functoin. How to filter others? */
54                         if (strcmp(func_name, "init_module") != 0)
55                                 printf("%x %s %s\n", fa->base + addr, 
56                                         mode, func_name);
57                 }
58         }
59         pclose(file);
60         return 0;
61 }
62
63
64 int generate_symbol_file()
65 {
66         static char* cmd = "lctl modules";
67         char         other[4096];
68         FILE         *file;
69         struct file_addr gfa;
70
71         memset(&gfa, 0, sizeof(gfa));
72         file = popen(cmd, "r");
73         if (!file) {
74                 printf("failed to execute %s: %s\n", cmd, strerror(errno));
75                 return -1;
76         }
77
78         while ( fscanf(file, "%s %s %lx\n", other, gfa.path, &gfa.base) == 3) {
79                 strncpy(gfa.modname, strrchr(gfa.path, '/') + 1, 
80                         strrchr(gfa.path, '.') - strrchr(gfa.path, '/') - 1);
81
82                  //fprintf(stderr, "%s %s %#x\n", gfa.path, gfa.modname, gfa.base);
83
84                 /* continue going without checking result */
85                 print_symbol_address(&gfa);
86                 memset(&gfa, 0, sizeof(gfa));
87         }
88         pclose(file);
89         return 0;
90 }
91
92
93 int main() 
94 {
95         return  generate_symbol_file();
96 }