summaryrefslogtreecommitdiff
path: root/scripts/gensystemtap.py
blob: ad808e30331f2847c941f6450999e9c9a08b81f2 (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
#!/usr/bin/env python3
#
# Copyright (C) 2011-2019 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see
# <http://www.gnu.org/licenses/>.
#
# Generate a set of systemtap functions for translating various
# RPC enum values into strings
#
#   python gensystemtap.py */*.x > libvirt_functions.stp
#

import re
import sys

funcs = {}

types = {}
status = {}
auth = {}


def load_file(fh):
    instatus = False
    intype = False
    inauth = False

    for line in fh:
        if re.search(r'''enum\s+virNetMessageType''', line):
            intype = True
        elif re.search(r'''enum\s+virNetMessageStatus''', line):
            instatus = True
        elif re.search(r'''enum remote_auth_type''', line):
            inauth = True
        elif "}" in line:
            intype = False
            instatus = False
            inauth = False
        elif instatus:
            m = re.search(r'''^\s+VIR_NET_(\w+)\s*=\s*(\d+),?$''', line)
            if m is not None:
                status[m.group(2)] = m.group(1).lower()
        elif intype:
            m = re.search(r'''^\s+VIR_NET_(\w+)\s*=\s*(\d+),?$''', line)
            if m is not None:
                types[m.group(2)] = m.group(1).lower()
        elif inauth:
            m = re.search(r'''^\s+REMOTE_AUTH_(\w+)\s*=\s*(\d+),?$''', line)
            if m is not None:
                auth[m.group(2)] = m.group(1).lower()
        else:
            m = re.search(r'''(?:VIR_)?(\w+?)(?:_PROTOCOL)?''' +
                          r'''_PROGRAM\s*=\s*0x([a-fA-F0-9]+)\s*;''', line)
            if m is not None:
                funcs[m.group(1).lower()] = {
                    "id": int(m.group(2), 16),
                    "version": None,
                    "progs": []
                }
                continue

            m = re.search(r'''(?:VIR_)?(\w+?)(?:_PROTOCOL)?_''' +
                          r'''(?:PROGRAM|PROTOCOL)_VERSION\s*=\s*(\d+)\s*;''',
                          line)
            if m is not None:
                funcs[m.group(1).lower()]["version"] = m.group(2)
                continue

            m = re.search(r'''(?:VIR_)?(\w+?)(?:_PROTOCOL)?''' +
                          r'''_PROC_(.*?)\s+=\s+(\d+)''', line)
            if m is not None:
                funcs[m.group(1).lower()]["progs"].insert(
                    int(m.group(3)), m.group(2).lower())


for file in sys.argv[1:]:
    with open(file, "r") as fh:
        load_file(fh)


def genfunc(name, varname, types):
    print("function %s(%s, verbose)" % (name, varname))
    print("{")

    first = True
    for typename in sorted(types.keys()):
        cond = "} else if"
        if first:
            cond = "if"
            first = False

        print("  %s (%s == %s) {" % (cond, varname, typename))
        print("      %sstr = \"%s\"" % (varname, types[typename]))

    print("  } else {")
    print("      %sstr = \"unknown\";" % varname)
    print("      verbose = 1;")
    print("  }")
    print("  if (verbose) {")
    print("      %sstr = %sstr . sprintf(\":%%d\", %s)" %
          (varname, varname, varname))
    print("  }")
    print("  return %sstr;" % varname)
    print("}")


genfunc("libvirt_rpc_auth_name", "type", auth)
genfunc("libvirt_rpc_type_name", "type", types)
genfunc("libvirt_rpc_status_name", "status", status)

print("function libvirt_rpc_program_name(program, verbose)")
print("{")

first = True
for funcname in sorted(funcs.keys()):
    cond = "} else if"
    if first:
        cond = "if"
        first = False

    print("  %s (program == %s) {" % (cond, funcs[funcname]["id"]))
    print("      programstr = \"%s\"" % funcname)

print("  } else {")
print("      programstr = \"unknown\";")
print("      verbose = 1;")
print("  }")
print("  if (verbose) {")
print("      programstr = programstr . sprintf(\":%d\", program)")
print("  }")
print("  return programstr;")
print("}")

print("function libvirt_rpc_procedure_name(program, version, proc, verbose)")
print("{")

first = True
for prog in sorted(funcs.keys()):
    cond = "} else if"
    if first:
        cond = "if"
        first = False

    print("  %s (program == %s && version == %s) {" %
          (cond, funcs[prog]["id"], funcs[prog]["version"]))

    pfirst = True
    for id in range(len(funcs[prog]["progs"])):
        pcond = "} else if"
        if pfirst:
            pcond = "if"
            pfirst = False

        print("      %s (proc == %s) {" % (pcond, id + 1))
        print("          procstr = \"%s\";" % funcs[prog]["progs"][id])

    print("      } else {")
    print("          procstr = \"unknown\";")
    print("          verbose = 1;")
    print("      }")

print("  } else {")
print("      procstr = \"unknown\";")
print("      verbose = 1;")
print("  }")
print("  if (verbose) {")
print("      procstr = procstr . sprintf(\":%d\", proc)")
print("  }")
print("  return procstr;")
print("}")