Removed redundant and incorrect character cleaning check from --input-file
[signduterre.git] / proc_modules.py
blobbbff184d5ff128223b621609b41ff9a2baa0ad46
1 # proc_modules
2 #
3 # Signature-du-Terroir module to handle /proc/modules
5 # Help text
6 help_text = """
7 Values:
8 modulelist: List of module names
9 moduledict: Dictionary of [module-name] = {'size', 'loadnum', 'dependencies', 'state', 'offset'}
10 moduledict['video']['offset'] -> offset adress in kernel of the 'video' module
11 min_address: Lowest offset address
12 max_load: Highest offset address
13 max_address: max_load + size-of-last-module
14 sum_sizes: sum of all module sizes
15 memory_range: max_address - min_address
16 kallsyms_modulelist: List of module names constructed from the exported kernel symbols in /proc/kallsyms
18 Functions:
19 sorted_sizes_table(): Alphabetically sorted table of modules+sizes as a string
20 sorted_offset_table(): Alphabetically sorted table of modules+offset as a string
21 sorted_loadnum_table(): Alphabetically sorted table of modules+loadnum as a string
22 sorted_dependencies_table(): Alphabetically sorted table of modules+dependencies as a string
23 sorted_state_table(): Alphabetically sorted table of modules+state as a string
25 ===================
26 proc_modules
27 Signature-du-Terroir module to access /proc/modules pseudo file.
29 copyright 2009, R.J.J.H. van Son
31 This program is free software: you can redistribute it and/or modify
32 it under the terms of the GNU General Public License as published by
33 the Free Software Foundation, either version 3 of the License, or
34 (at your option) any later version.
36 This program is distributed in the hope that it will be useful,
37 but WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 GNU General Public License for more details.
41 You should have received a copy of the GNU General Public License
42 along with this program. If not, see <http://www.gnu.org/licenses/>.
43 """
45 import re;
46 modulelist = [];
47 moduledict = {};
48 min_address = 10**100;
49 max_address = -1;
50 max_load = -1;
51 sum_sizes = 0;
52 with open('/proc/modules') as m:
53 for l in m:
54 module_value = re.split(r'\s+', l.rstrip('\s\n'));
55 (module, size, loadnum, dependencies, state, offset) = module_value[0:6];
56 modulelist.append(module);
57 size = int(size);
58 loadnum = int(loadnum);
59 offset = int(offset, 16);
60 module_entry = {'size':size, 'loadnum':loadnum, 'dependencies':dependencies, 'state':state, 'offset':offset};
61 moduledict[module] = module_entry;
62 if min_address > offset: min_address = offset;
63 if max_address < offset+size: max_address = offset + size;
64 if max_load < offset: max_load = offset;
65 sum_sizes += size;
67 kallsyms_modulelist = [];
68 with open('/proc/kallsyms') as m:
69 for l in m:
70 module_value = re.split(r'\s+', l.rstrip('\s\n'));
71 module = module_value[-1];
72 if module.startswith('['):
73 modulename = module.lstrip('[').rstrip(']');
74 if not modulename in kallsyms_modulelist:
75 kallsyms_modulelist.append(modulename);
76 kallsyms_modulelist.sort();
78 memory_range = max_address - min_address
80 def sorted_sizes_table():
81 modulelist.sort();
82 s="";
83 for m in modulelist:
84 s += m+"\t"+str(moduledict[m]['size'])+"\n";
85 return s;
87 def sorted_offset_table():
88 modulelist.sort();
89 s="";
90 for m in modulelist:
91 s += m+"\t"+str(moduledict[m]['offset'])+"\n";
92 return s;
94 def sorted_loadnum_table():
95 modulelist.sort();
96 s="";
97 for m in modulelist:
98 s += m+"\t"+str(moduledict[m]['loadnum'])+"\n";
99 return s;
101 def sorted_dependencies_table():
102 modulelist.sort();
103 s="";
104 for m in modulelist:
105 s += m+"\t"+str(moduledict[m]['dependencies'])+"\n";
106 return s;
108 def sorted_state_table ():
109 modulelist.sort();
110 s="";
111 for m in modulelist:
112 s += m+"\t"+str(moduledict[m]['state'])+"\n";
113 return s;
115 def dump_module_binary (name):
116 module_binary = open('/proc/kcore');
118 if __name__ == "__main__":
119 print(help_text);
120 print(kallsyms_modulelist);