aboutsummaryrefslogtreecommitdiffstats
path: root/tools/make-dissector-reg.py
blob: dadb27b17bc95ab57d8fa5ba9a9c0ad0ef2fce6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python
#
# Looks for registration routines in the protocol dissectors,
# and assembles C code to call all the routines.
#
# This is a Python version of the make-reg-dotc shell script.
# Running the shell script on Win32 is very very slow because of
# all the process-launching that goes on --- multiple greps and
# seds for each input file.  I wrote this python version so that
# less processes would have to be started.

import os
import sys
import re
import pickle
import hashlib
from stat import *

VERSION_KEY = '_VERSION'
CUR_VERSION = 'this_is_useless_with_git'

#
# The first argument is the directory in which the source files live.
#
srcdir = sys.argv[1]

#
# The second argument is either "plugin" or "dissectors"; if it's
# "plugin", we build a plugin.c for a plugin, and if it's
# "dissectors", we build a register.c for libwireshark.
#
registertype = sys.argv[2]
if registertype in ("plugin", "plugin_wtap"):
    final_filename = "plugin.c"
    cache_filename = None
    preamble = """\
/*
 * Do not modify this file. Changes will be overwritten.
 *
 * Generated automatically from %s.
 */
""" % (sys.argv[0])
elif registertype in ("dissectors", "dissectorsinfile"):
    final_filename = "register.c"
    cache_filename = "register-cache.pkl"
    preamble = """\
/*
 * Do not modify this file. Changes will be overwritten.
 *
 * Generated automatically by the "register.c" target in
 * epan/dissectors/Makefile using %s
 * and information in epan/dissectors/register-cache.pkl.
 *
 * You can force this file to be regenerated completely by deleting
 * it along with epan/dissectors/register-cache.pkl.
 */
""" % (sys.argv[0])
else:
    print(("Unknown output type '%s'" % registertype))
    sys.exit(1)


#
# All subsequent arguments are the files to scan
# or the name of a file containing the files to scan
#
if registertype == "dissectorsinfile":
    try:
        dissector_f = open(sys.argv[3])
    except IOError:
        print(("Unable to open input file '%s'" % sys.argv[3]))
        sys.exit(1)

    files = [line.rstrip() for line in dissector_f]
else:
    files = sys.argv[3:]

# Create the proper list of filenames
filenames = []
for file in files:
    if os.path.isfile(file):
        filenames.append(file)
    else:
        filenames.append(os.path.join(srcdir, file))

if len(filenames) < 1:
    print("No files found")
    sys.exit(1)


# Look through all files, applying the regex to each line.
# If the pattern matches, save the "symbol" section to the
# appropriate set.
regs = {
        'proto_reg': set(),
        'handoff_reg': set(),
        'wtap_register': set(),
        }

# For those that don't know Python, r"" indicates a raw string,
# devoid of Python escapes.
proto_regex = r"(?P<symbol>\bproto_register_[_A-Za-z0-9]+)\s*\(\s*void\s*\)[^;]*$"

handoff_regex = r"(?P<symbol>\bproto_reg_handoff_[_A-Za-z0-9]+)\s*\(\s*void\s*\)[^;]*$"

wtap_reg_regex = r"(?P<symbol>\bwtap_register_[_A-Za-z0-9]+)\s*\([^;]+$"

# This table drives the pattern-matching and symbol-harvesting
patterns = [
        ( 'proto_reg', re.compile(proto_regex, re.MULTILINE) ),
        ( 'handoff_reg', re.compile(handoff_regex, re.MULTILINE) ),
        ( 'wtap_register', re.compile(wtap_reg_regex, re.MULTILINE) ),
        ]

# Open our registration symbol cache
cache = None
if cache_filename:
    try:
        cache_file = open(cache_filename, 'rb')
        cache = pickle.load(cache_file)
        cache_file.close()
        if VERSION_KEY not in cache or cache[VERSION_KEY] != CUR_VERSION:
            cache = {VERSION_KEY: CUR_VERSION}
    except:
        cache = {VERSION_KEY: CUR_VERSION}

    print(("Registering %d files, %d cached" % (len(filenames), len(list(cache.keys()))-1)))

# Grep
cache_hits = 0
cache_misses = 0
for filename in filenames:
    file = open(filename)
    cur_mtime = os.fstat(file.fileno())[ST_MTIME]
    if cache and filename in cache:
        cdict = cache[filename]
        if cur_mtime == cdict['mtime']:
            cache_hits += 1
#                       print "Pulling %s from cache" % (filename)
            regs['proto_reg'] |= set(cdict['proto_reg'])
            regs['handoff_reg'] |= set(cdict['handoff_reg'])
            regs['wtap_register'] |= set(cdict['wtap_register'])
            file.close()
            continue
    # We don't have a cache entry
    if cache is not None:
        cache_misses += 1
        cache[filename] = {
                'mtime': cur_mtime,
                'proto_reg': [],
                'handoff_reg': [],
                'wtap_register': [],
                }
#       print "Searching %s" % (filename)
    # Read the whole file into memory
    contents = file.read()
    for action in patterns:
        regex = action[1]
        for match in regex.finditer(contents):
            symbol = match.group("symbol")
            sym_type = action[0]
            regs[sym_type].add(symbol)
            if cache is not None:
#                               print "Caching %s for %s: %s" % (sym_type, filename, symbol)
                cache[filename][sym_type].append(symbol)
    # We're done with the file contents
    del contents
    file.close()


if cache is not None and cache_filename is not None:
    cache_file = open(cache_filename, 'wb')
    pickle.dump(cache, cache_file)
    cache_file.close()
    print(("Cache hits: %d, misses: %d" % (cache_hits, cache_misses)))

# Make sure we actually processed something
if len(regs['proto_reg']) < 1:
    print("No protocol registrations found")
    sys.exit(1)

# Convert the sets into sorted lists to make the output pretty
regs['proto_reg'] = sorted(regs['proto_reg'])
regs['handoff_reg'] = sorted(regs['handoff_reg'])
regs['wtap_register'] = sorted(regs['wtap_register'])

reg_code = ""

reg_code += preamble

# Make the routine to register all protocols
if registertype == "plugin" or registertype == "plugin_wtap":
    reg_code += """
#include "config.h"

#include <gmodule.h>

#include "moduleinfo.h"

/* plugins are DLLs */
#define WS_BUILD_DLL
#include "ws_symbol_export.h"

#ifndef ENABLE_STATIC
WS_DLL_PUBLIC_DEF void plugin_register (void);

WS_DLL_PUBLIC_DEF const gchar plugin_version[] = VERSION;
WS_DLL_PUBLIC_DEF const gchar plugin_release[] = VERSION_RELEASE;

"""
else:
    reg_code += """
#include "register.h"
#include "ws_attributes.h"

#include <glib.h>
"""

for symbol in regs['proto_reg']:
    reg_code += "extern void %s(void);\n" % (symbol)

if registertype == "plugin" or registertype == "plugin_wtap":
    reg_code += """
/* Start the functions we need for the plugin stuff */

WS_DLL_PUBLIC_DEF void
plugin_register (void)
{
"""
else:
    reg_code += """
static const char *cur_cb_name = NULL;
//static GMutex register_cb_mtx;
static GAsyncQueue *register_cb_done_q;

#define CB_WAIT_TIME (150 * 1000) // microseconds

static void set_cb_name(const char *proto) {
    // g_mutex_lock(register_cb_mtx);
    cur_cb_name = proto;
    // g_mutex_unlock(register_cb_mtx);
}

static void *register_all_protocols_worker(void *arg _U_);

void
register_all_protocols(register_cb cb, gpointer cb_data)
{
    const char *cb_name;
    register_cb_done_q = g_async_queue_new();
    gboolean called_back = FALSE;

#if GLIB_CHECK_VERSION(2,31,0)
    g_thread_new("register_all_protocols_worker", &register_all_protocols_worker, NULL);
#else
    g_thread_create(&register_all_protocols_worker, TRUE, FALSE, NULL);
#endif
    while (!g_async_queue_timeout_pop(register_cb_done_q, CB_WAIT_TIME)) {
        // g_mutex_lock(register_cb_mtx);
        cb_name = cur_cb_name;
        // g_mutex_unlock(register_cb_mtx);
        if (cb && cb_name) {
            cb(RA_REGISTER, cb_name, cb_data);
            called_back = TRUE;
        }
    }
    if (cb && !called_back) {
            cb(RA_REGISTER, "Registration finished", cb_data);
    }
}

void
*register_all_protocols_worker(void *arg _U_)
{
"""

for symbol in regs['proto_reg']:
    if registertype != "plugin" and registertype != "plugin_wtap":
        reg_code += "    set_cb_name(\"%s\");\n" % (symbol)
    reg_code += "    %s();\n" % (symbol)

if registertype != "plugin" and registertype != "plugin_wtap":
    reg_code += """
    g_async_queue_push(register_cb_done_q, GINT_TO_POINTER(TRUE));
    return NULL;
"""
reg_code += "}\n\n"


# Make the routine to register all protocol handoffs

for symbol in regs['handoff_reg']:
    reg_code += "extern void %s(void);\n" % (symbol)

if registertype == "plugin" or registertype == "plugin_wtap":
    reg_code += """
WS_DLL_PUBLIC_DEF void plugin_reg_handoff(void);

WS_DLL_PUBLIC_DEF void
plugin_reg_handoff(void)
{
"""
else:
    reg_code += """
static void *register_all_protocol_handoffs_worker(void *arg _U_);

void
register_all_protocol_handoffs(register_cb cb, gpointer cb_data)
{
    cur_cb_name = NULL;
    const char *cb_name;
    gboolean called_back = FALSE;

#if GLIB_CHECK_VERSION(2,31,0)
    g_thread_new("register_all_protocol_hadoffss_worker", &register_all_protocol_handoffs_worker, NULL);
#else
    g_thread_create(&register_all_protocol_handoffs_worker, TRUE, FALSE, NULL);
#endif
    while (!g_async_queue_timeout_pop(register_cb_done_q, CB_WAIT_TIME)) {
        // g_mutex_lock(register_cb_mtx);
        cb_name = cur_cb_name;
        // g_mutex_unlock(register_cb_mtx);
        if (cb && cb_name) {
            cb(RA_HANDOFF, cb_name, cb_data);
            called_back = TRUE;
        }
    }
    if (cb && !called_back) {
            cb(RA_HANDOFF, "Registration finished", cb_data);
    }

    g_async_queue_unref(register_cb_done_q);
}

void
*register_all_protocol_handoffs_worker(void *arg _U_)
{
"""

for symbol in regs['handoff_reg']:
    if registertype != "plugin" and registertype != "plugin_wtap":
        reg_code += "    set_cb_name(\"%s\");\n" % (symbol)
    reg_code += "    %s();\n" % (symbol)

if registertype != "plugin" and registertype != "plugin_wtap":
    reg_code += """
    g_async_queue_push(register_cb_done_q, GINT_TO_POINTER(TRUE));
    return NULL;
"""
reg_code += "}\n"

if registertype == "plugin":
    reg_code += "#endif\n"
elif registertype == "plugin_wtap":
    reg_code += """
WS_DLL_PUBLIC_DEF void
register_wtap_module(void)
{
"""

    for symbol in regs['wtap_register']:
        line = "    {extern void %s (void); %s ();}\n" % (symbol, symbol)
        reg_code += line

    reg_code += """
}
#endif
"""

else:
    reg_code += """
static gulong proto_reg_count(void)
{
    return %(proto_reg_len)d;
}

static gulong handoff_reg_count(void)
{
    return %(handoff_reg_len)d;
}

gulong register_count(void)
{
    return proto_reg_count() + handoff_reg_count();
}
""" % {
    'proto_reg_len': len(regs['proto_reg']),
    'handoff_reg_len': len(regs['handoff_reg'])
  }


# Compare current and new content and update the file if anything has changed.

try:    # Python >= 2.6, >= 3.0
    reg_code_bytes = bytes(reg_code.encode('utf-8'))
except:
    reg_code_bytes = reg_code

new_hash = hashlib.sha1(reg_code_bytes).hexdigest()

try:
    fh = open(final_filename, 'rb')
    cur_hash = hashlib.sha1(fh.read()).hexdigest()
    fh.close()
except:
    cur_hash = ''

try:
    if new_hash != cur_hash:
        print(('Updating ' + final_filename))
        fh = open(final_filename, 'w')
        fh.write(reg_code)
        fh.close()
    else:
        print((final_filename + ' unchanged.'))
except OSError:
    sys.exit('Unable to write ' + final_filename + '.\n')

#
# Editor modelines  -  http://www.wireshark.org/tools/modelines.html
#
# Local variables:
# c-basic-offset: 4
# indent-tabs-mode: nil
# End:
#
# vi: set shiftwidth=4 expandtab:
# :indentSize=4:noTabs=true:
#