Sfoglia il codice sorgente

[Contribs] Add command-line utility for looking up gPXE error codes.

tags/v0.9.4
Stefan Hajnoczi 16 anni fa
parent
commit
43b04236ec
2 ha cambiato i file con 129 aggiunte e 0 eliminazioni
  1. 66
    0
      contrib/errcode/build_errcodedb.py
  2. 63
    0
      contrib/errcode/errcode.py

+ 66
- 0
contrib/errcode/build_errcodedb.py Vedi File

@@ -0,0 +1,66 @@
1
+#!/usr/bin/env python
2
+import sys
3
+import re
4
+
5
+pxenv_status_files = ('../../src/include/errno.h', )
6
+errfile_files = ('../../src/include/gpxe/errfile.h',
7
+            '../../src/arch/i386/include/bits/errfile.h')
8
+posix_errno_files = ('../../src/include/errno.h', )
9
+
10
+PXENV_STATUS_RE = re.compile(r'^#define\s+(PXENV_STATUS_[^\s]+)\s+(.+)$')
11
+ERRFILE_RE = re.compile(r'^#define\s+(ERRFILE_[^\s]+)\s+(.+)$')
12
+POSIX_ERRNO_RE = re.compile(r'^#define\s+(E[A-Z]+)\s+.*(0x[0-9a-f]+).*$')
13
+
14
+def err(msg):
15
+    sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))
16
+    sys.exit(1)
17
+
18
+def load_header_file(filename, regexp):
19
+    defines = {}
20
+    for line in open(filename, 'r'):
21
+        m = regexp.match(line)
22
+        if m:
23
+            key, val = m.groups()
24
+            defines[key] = val
25
+    return defines
26
+
27
+def evaluate(defines, expr):
28
+    pyexpr = ''
29
+    for token in expr.split():
30
+        if token in '()':
31
+            pass
32
+        elif token.startswith('/*') or token.startswith('//'):
33
+            break
34
+        elif token.startswith('0x') or token == '|':
35
+            pyexpr += token
36
+        else:
37
+            if token in defines:
38
+                pyexpr += '0x%x' % defines[token]
39
+            else:
40
+                return -1
41
+    if not re.match(r'^[0-9a-zA-Z_|]+$', pyexpr):
42
+        err('invalid expression')
43
+    return eval(pyexpr)
44
+
45
+def build(filenames, regexp):
46
+    unevaluated = {}
47
+    for filename in filenames:
48
+        unevaluated.update(load_header_file(filename, regexp))
49
+
50
+    evaluated = {}
51
+    changed = True
52
+    while changed:
53
+        changed = False
54
+        for key in list(unevaluated.keys()):
55
+            val = evaluate(evaluated, unevaluated[key])
56
+            if val != -1:
57
+                del unevaluated[key]
58
+                evaluated[key] = val
59
+                changed = True
60
+    if unevaluated:
61
+        err('unable to evaluate all #defines')
62
+    return evaluated
63
+
64
+print 'pxenv_status =', repr(build(pxenv_status_files, PXENV_STATUS_RE))
65
+print 'errfile =', repr(build(errfile_files, ERRFILE_RE))
66
+print 'posix_errno =', repr(build(posix_errno_files, POSIX_ERRNO_RE))

+ 63
- 0
contrib/errcode/errcode.py Vedi File

@@ -0,0 +1,63 @@
1
+#!/usr/bin/env python
2
+import sys
3
+
4
+try:
5
+    import errcodedb
6
+except ImportError:
7
+    sys.stderr.write('Please run this first: ./build_errcodedb.py >errcodedb.py\n')
8
+    sys.exit(1)
9
+
10
+def to_pxenv_status(errno):
11
+    return errno & 0xff
12
+
13
+def to_uniq(errno):
14
+    return (errno >> 8) & 0x1f
15
+
16
+def to_errfile(errno):
17
+    return (errno >> 13) & 0x7ff
18
+
19
+def to_posix_errno(errno):
20
+    return (errno >> 24) & 0x7f
21
+
22
+def lookup_errno_component(defines, selector, component):
23
+    for key, val in defines.iteritems():
24
+        if selector(val) == component:
25
+            return key
26
+    return '0x%x' % component
27
+
28
+class Errno(object):
29
+    def __init__(self, errno):
30
+        self.pxenv_status = to_pxenv_status(errno)
31
+        self.uniq = to_uniq(errno)
32
+        self.errfile = to_errfile(errno)
33
+        self.posix_errno = to_posix_errno(errno)
34
+
35
+    def rawstr(self):
36
+        return 'pxenv_status=0x%x uniq=%d errfile=0x%x posix_errno=0x%x' % (self.pxenv_status, self.uniq, self.errfile, self.posix_errno)
37
+
38
+    def prettystr(self):
39
+        return 'pxenv_status=%s uniq=%d errfile=%s posix_errno=%s' % (
40
+                lookup_errno_component(errcodedb.pxenv_status, to_pxenv_status, self.pxenv_status),
41
+                self.uniq,
42
+                lookup_errno_component(errcodedb.errfile, to_errfile, self.errfile),
43
+                lookup_errno_component(errcodedb.posix_errno, to_posix_errno, self.posix_errno)
44
+                )
45
+
46
+    def __str__(self):
47
+        return self.prettystr()
48
+
49
+def usage():
50
+    sys.stderr.write('usage: %s ERROR_NUMBER\n' % sys.argv[0])
51
+    sys.exit(1)
52
+
53
+if __name__ == '__main__':
54
+    if len(sys.argv) != 2:
55
+        usage()
56
+
57
+    try:
58
+        errno = int(sys.argv[1], 16)
59
+    except ValueError:
60
+        usage()
61
+
62
+    print Errno(errno)
63
+    sys.exit(0)

Loading…
Annulla
Salva