Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / tests / gensymmap.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <errno.h>
39 #include <sys/utsname.h>
40 #include <string.h>
41
42 struct file_addr {
43         char path[256];
44         char modname[32];
45         unsigned long base; 
46 };
47
48 int print_symbol_address(struct file_addr * fa)
49 {
50         char buffer[4096];
51         char cmd[256];
52         char func_name[256];
53         unsigned long addr;
54         char mode[256];
55         FILE *file;
56
57         sprintf(cmd, "modprobe -l %s", fa->modname);
58         file = popen(cmd, "r");
59         if (!file) {
60                 printf("failed to execute %s:%s\n."
61                        "Have you installed modules?\n", 
62                         cmd, strerror(errno));
63                 pclose(file);
64                 return -1;
65         }
66         if (fgets(buffer, 4095, file) == NULL) {
67                 printf("failed to get modprobe ouput for %s:%s\n", 
68                         fa->modname, strerror(errno));
69                 pclose(file);
70                 return -1;
71         }
72         pclose(file);
73
74         sprintf(cmd, "nm -n %s", buffer);
75         file = popen(cmd, "r");
76         if (!file) {
77                 printf("failed to execute %s:%s\n."
78                        "Have you installed modules?\n", 
79                         cmd, strerror(errno));
80                 return -1;
81         }
82
83         while (fgets(buffer, 4095, file)) {
84                 if (fscanf(file, "%x %s %s\n", &addr, mode, func_name) != 3)
85                         continue;
86
87                 /* only list symbol in text section. */
88                 if (strcasecmp(mode, "t") == 0) {
89                         /* skip __init functoin. How to filter others? */
90                         if (strcmp(func_name, "init_module") != 0)
91                                 printf("%x %s %s\n", fa->base + addr, 
92                                         mode, func_name);
93                 }
94         }
95         pclose(file);
96         return 0;
97 }
98
99
100 int generate_symbol_file()
101 {
102         static char* cmd = "lctl modules";
103         char         other[4096];
104         FILE         *file;
105         struct file_addr gfa;
106
107         memset(&gfa, 0, sizeof(gfa));
108         file = popen(cmd, "r");
109         if (!file) {
110                 printf("failed to execute %s: %s\n", cmd, strerror(errno));
111                 return -1;
112         }
113
114         while ( fscanf(file, "%s %s %lx\n", other, gfa.path, &gfa.base) == 3) {
115                 strncpy(gfa.modname, strrchr(gfa.path, '/') + 1, 
116                         strrchr(gfa.path, '.') - strrchr(gfa.path, '/') - 1);
117
118                  //fprintf(stderr, "%s %s %#x\n", gfa.path, gfa.modname, gfa.base);
119
120                 /* continue going without checking result */
121                 print_symbol_address(&gfa);
122                 memset(&gfa, 0, sizeof(gfa));
123         }
124         pclose(file);
125         return 0;
126 }
127
128
129 int main() 
130 {
131         return  generate_symbol_file();
132 }