Debian Codegraph @

scripts/kprobes_test/gen_code.py

raw
1 #!/usr/bin/python
2
3 # Copyright (C) 2008, 2010 Red Hat Inc.
4 #
5 # This file is part of systemtap, and is free software. You can
6 # redistribute it and/or modify it under the terms of the GNU General
7 # Public License (GPL); either version 2, or (at your option) any
8 # later version.
9
10 import os
11 import sys
12 from config_opts import config_opts
13
14 def gen_files(dir, subset):
15 f = open('%s/kprobe_defs.h' % dir, 'w')
16
17 # Output the array of kp_data structs
18 print >>f, "static struct kp_data kp_data[] = {"
19 i = 0
20 while i < len(subset):
21 print >>f, ("\t{ .kp={ .symbol_name=\"%s\", .pre_handler=&handler_pre, }, .use_count=ATOMIC_INIT(0) },"
22 % (subset[i]))
23 i += 1
24 print >>f, "};"
25 print >>f
26 f.close()
27
28 # Generate the Makefile
29 f = open('Makefile', 'w')
30 print >>f, """
31 EXTRA_CFLAGS :=
32 EXTRA_CFLAGS += -freorder-blocks
33 EXTRA_CFLAGS += -Wno-unused -Werror
34 obj-m := kprobe_module.o"""
35 f.close()
36
37 def run_make_cmd(cmd):
38 # Before running make, fix up the environment a bit. Clean out a
39 # few variables that /lib/modules/${KVER}/build/Makefile uses.
40 os.unsetenv("ARCH")
41 os.unsetenv("KBUILD_EXTMOD")
42 os.unsetenv("CROSS_COMPILE")
43 os.unsetenv("KBUILD_IMAGE")
44 os.unsetenv("KCONFIG_CONFIG")
45 os.unsetenv("INSTALL_PATH");
46
47 print "Running", cmd
48 return os.system(cmd)
49
50 def gen_module():
51 f = open(config_opts['probes_current'])
52 probes = f.readlines()
53 f.close()
54 if len(probes) == 0:
55 print >>sys.stderr, ("Error: no probe points in %s"
56 % config_opts['probes_current'])
57 return -1
58
59 # Cleanup each probe by stripping whitespace
60 i = 0
61 while i < len(probes):
62 probes[i] = probes[i].rstrip()
63 i += 1
64
65 # Generate necessary files
66 gen_files(os.getcwd(), probes)
67
68 # Try to build the module - add "V=1" at the end for more verbosity
69 os.system('rm -f ./kprobe_module.ko')
70 (sysname, nodename, release, version, machine) = os.uname()
71 cmd = ("make -C \"/lib/modules/%s/build\" M=\"%s\" modules CONFIG_MODULE_SIG=n >build.log 2>&1"
72 % (release, os.getcwd()))
73 rc = run_make_cmd(cmd)
74 if os.WEXITSTATUS(rc) != 0:
75 print >>sys.stderr, "Error: Make failed, see build.log for details"
76 return -1
77 return 0
78
79 def main():
80 rc = gen_module()
81 sys.exit(rc)
82
83 if __name__ == "__main__":
84 main()