nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #!/usr/bin/env python |
2 | # |
||
3 | # Looks for registration routines in the taps, |
||
4 | # and assembles C code to call all the routines. |
||
5 | # |
||
6 | # This is a Python version of the make-reg-dotc shell script. |
||
7 | # Running the shell script on Win32 is very very slow because of |
||
8 | # all the process-launching that goes on --- multiple greps and |
||
9 | # seds for each input file. I wrote this python version so that |
||
10 | # less processes would have to be started. |
||
11 | # |
||
12 | # Copyright 2010 Anders Broman <anders.broman@ericsson.com> |
||
13 | # |
||
14 | # Wireshark - Network traffic analyzer |
||
15 | # By Gerald Combs <gerald@wireshark.org> |
||
16 | # Copyright 1998 Gerald Combs |
||
17 | # |
||
18 | # This program is free software; you can redistribute it and/or |
||
19 | # modify it under the terms of the GNU General Public License |
||
20 | # as published by the Free Software Foundation; either version 2 |
||
21 | # of the License, or (at your option) any later version. |
||
22 | # |
||
23 | # This program is distributed in the hope that it will be useful, |
||
24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
26 | # GNU General Public License for more details. |
||
27 | # |
||
28 | # You should have received a copy of the GNU General Public License |
||
29 | # along with this program; if not, write to the Free Software |
||
30 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
31 | |||
32 | import os |
||
33 | import sys |
||
34 | import re |
||
35 | import pickle |
||
36 | from stat import * |
||
37 | |||
38 | # |
||
39 | # The first argument is the directory in which the source files live. |
||
40 | # |
||
41 | srcdir = sys.argv[1] |
||
42 | |||
43 | # |
||
44 | # The second argument is "taps". |
||
45 | # |
||
46 | registertype = sys.argv[2] |
||
47 | if registertype == "taps": |
||
48 | tmp_filename = "wireshark-tap-register.c-tmp" |
||
49 | final_filename = "wireshark-tap-register.c" |
||
50 | cache_filename = "wireshark-tap-register-cache.pkl" |
||
51 | elif registertype == "tshark-taps": |
||
52 | tmp_filename = "tshark-tap-register.c-tmp" |
||
53 | final_filename = "tshark-tap-register.c" |
||
54 | cache_filename = "tshark-tap-register-cache.pkl" |
||
55 | else: |
||
56 | print("Unknown output type '%s'" % registertype) |
||
57 | sys.exit(1) |
||
58 | |||
59 | |||
60 | # |
||
61 | # All subsequent arguments are the files to scan. |
||
62 | # |
||
63 | files = sys.argv[3:] |
||
64 | |||
65 | # Create the proper list of filenames |
||
66 | filenames = [] |
||
67 | for file in files: |
||
68 | if os.path.isfile(file): |
||
69 | filenames.append(file) |
||
70 | else: |
||
71 | filenames.append("%s/%s" % (srcdir, file)) |
||
72 | |||
73 | if len(filenames) < 1: |
||
74 | print("No files found") |
||
75 | sys.exit(1) |
||
76 | |||
77 | |||
78 | # Look through all files, applying the regex to each line. |
||
79 | # If the pattern matches, save the "symbol" section to the |
||
80 | # appropriate array. |
||
81 | regs = { |
||
82 | 'tap_reg': [], |
||
83 | } |
||
84 | |||
85 | # For those that don't know Python, r"" indicates a raw string, |
||
86 | # devoid of Python escapes. |
||
87 | tap_regex0 = r"^(?P<symbol>register_tap_listener_[_A-Za-z0-9]+)\s*\([^;]+$" |
||
88 | tap_regex1 = r"void\s+(?P<symbol>register_tap_listener_[_A-Za-z0-9]+)\s*\([^;]+$" |
||
89 | |||
90 | # This table drives the pattern-matching and symbol-harvesting |
||
91 | patterns = [ |
||
92 | ( 'tap_reg', re.compile(tap_regex0) ), |
||
93 | ( 'tap_reg', re.compile(tap_regex1) ), |
||
94 | ] |
||
95 | |||
96 | # Open our registration symbol cache |
||
97 | cache = None |
||
98 | if cache_filename: |
||
99 | try: |
||
100 | cache_file = open(cache_filename, 'rb') |
||
101 | cache = pickle.load(cache_file) |
||
102 | cache_file.close() |
||
103 | except: |
||
104 | cache = {} |
||
105 | |||
106 | # Grep |
||
107 | for filename in filenames: |
||
108 | file = open(filename) |
||
109 | cur_mtime = os.fstat(file.fileno())[ST_MTIME] |
||
110 | if cache and filename in cache: |
||
111 | cdict = cache[filename] |
||
112 | if cur_mtime == cdict['mtime']: |
||
113 | # print "Pulling %s from cache" % (filename) |
||
114 | regs['tap_reg'].extend(cdict['tap_reg']) |
||
115 | file.close() |
||
116 | continue |
||
117 | # We don't have a cache entry |
||
118 | if cache is not None: |
||
119 | cache[filename] = { |
||
120 | 'mtime': cur_mtime, |
||
121 | 'tap_reg': [], |
||
122 | } |
||
123 | # print "Searching %s" % (filename) |
||
124 | for line in file.readlines(): |
||
125 | for action in patterns: |
||
126 | regex = action[1] |
||
127 | match = regex.search(line) |
||
128 | if match: |
||
129 | symbol = match.group("symbol") |
||
130 | sym_type = action[0] |
||
131 | regs[sym_type].append(symbol) |
||
132 | if cache is not None: |
||
133 | # print "Caching %s for %s: %s" % (sym_type, filename, symbol) |
||
134 | cache[filename][sym_type].append(symbol) |
||
135 | file.close() |
||
136 | |||
137 | if cache is not None and cache_filename is not None: |
||
138 | cache_file = open(cache_filename, 'wb') |
||
139 | pickle.dump(cache, cache_file) |
||
140 | cache_file.close() |
||
141 | |||
142 | # Make sure we actually processed something |
||
143 | if len(regs['tap_reg']) < 1: |
||
144 | print("No protocol registrations found") |
||
145 | sys.exit(1) |
||
146 | |||
147 | # Sort the lists to make them pretty |
||
148 | regs['tap_reg'].sort() |
||
149 | |||
150 | reg_code = open(tmp_filename, "w") |
||
151 | |||
152 | reg_code.write("/* Do not modify this file. Changes will be overwritten. */\n") |
||
153 | reg_code.write("/* Generated automatically from %s */\n" % (sys.argv[0])) |
||
154 | |||
155 | # Make the routine to register all taps |
||
156 | reg_code.write(""" |
||
157 | #include "register.h" |
||
158 | void register_all_tap_listeners(void) { |
||
159 | """); |
||
160 | |||
161 | for symbol in regs['tap_reg']: |
||
162 | line = " {extern void %s (void); %s ();}\n" % (symbol, symbol) |
||
163 | reg_code.write(line) |
||
164 | |||
165 | reg_code.write("}\n") |
||
166 | |||
167 | |||
168 | # Close the file |
||
169 | reg_code.close() |
||
170 | |||
171 | # Remove the old final_file if it exists. |
||
172 | try: |
||
173 | os.stat(final_filename) |
||
174 | os.remove(final_filename) |
||
175 | except OSError: |
||
176 | pass |
||
177 | |||
178 | # Move from tmp file to final file |
||
179 | os.rename(tmp_filename, final_filename) |
||
180 | |||
181 | # |
||
182 | # Editor modelines - http://www.wireshark.org/tools/modelines.html |
||
183 | # |
||
184 | # Local variables: |
||
185 | # c-basic-offset: 4 |
||
186 | # indent-tabs-mode: nil |
||
187 | # End: |
||
188 | # |
||
189 | # vi: set shiftwidth=4 expandtab: |
||
190 | # :indentSize=4:noTabs=true: |
||
191 | # |