|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# |
| 4 | +# gen-postmortem-metadata.py output_file.cc |
| 5 | +# |
| 6 | +# Creates debugging symbols to help naviage Node's internals using post-mortem |
| 7 | +# debugging tools. |
| 8 | +# |
| 9 | + |
| 10 | +import os |
| 11 | +import fnmatch |
| 12 | +import re |
| 13 | +from glob import glob |
| 14 | +import sys |
| 15 | + |
| 16 | + |
| 17 | +class DebugSymbol(object): |
| 18 | + type_ = 'int' |
| 19 | + _prefix = 'nodedbg_' |
| 20 | + |
| 21 | + def __init__(self, name, value, headers=[], type_=None): |
| 22 | + self.name = name |
| 23 | + self.value = value |
| 24 | + self.headers = headers |
| 25 | + self.type_ = type_ or DebugSymbol.type_ |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def get_headers(cls, debug_symbols): |
| 29 | + ''' |
| 30 | + Return a list of headers without duplicates, preserving the order they were |
| 31 | + declared |
| 32 | + ''' |
| 33 | + seen = set() |
| 34 | + headers = [debug_symbol.headers for debug_symbol in debug_symbols] |
| 35 | + headers = sum(headers, []) |
| 36 | + |
| 37 | + result = [] |
| 38 | + for h in headers: |
| 39 | + if not h in seen: |
| 40 | + seen.add(h) |
| 41 | + result.append(h) |
| 42 | + |
| 43 | + return result |
| 44 | + |
| 45 | + @property |
| 46 | + def declare(self): |
| 47 | + return '{type} {prefix}{name};'.format( |
| 48 | + type=self.type_, |
| 49 | + prefix=self._prefix, |
| 50 | + name=self.name, |
| 51 | + ) |
| 52 | + |
| 53 | + @property |
| 54 | + def fill(self): |
| 55 | + return '{prefix}{name} = {value};'.format( |
| 56 | + prefix=self._prefix, |
| 57 | + name=self.name, |
| 58 | + value=self.value, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +debug_symbols = [ |
| 63 | + DebugSymbol( |
| 64 | + name='environment_context_idx_embedder_data', |
| 65 | + value='Environment::kContextEmbedderDataIndex', |
| 66 | + headers=['env.h'], |
| 67 | + type_='int', |
| 68 | + ), |
| 69 | + DebugSymbol( |
| 70 | + name='class__BaseObject__persistent_handle', |
| 71 | + value='nonstandard_offsetof(BaseObject, persistent_handle_)', |
| 72 | + headers=['base_object-inl.h'], |
| 73 | + type_='size_t', |
| 74 | + ), |
| 75 | + DebugSymbol( |
| 76 | + name='class__Environment__handleWrapQueue', |
| 77 | + value='nonstandard_offsetof(Environment, handle_wrap_queue_)', |
| 78 | + headers=['env.h'], |
| 79 | + type_='size_t', |
| 80 | + ), |
| 81 | + DebugSymbol( |
| 82 | + name='class__HandleWrap__node', |
| 83 | + value='nonstandard_offsetof(HandleWrap, handle_wrap_queue_)', |
| 84 | + headers=['handle_wrap.h'], |
| 85 | + type_='size_t', |
| 86 | + ), |
| 87 | + DebugSymbol( |
| 88 | + name='class__HandleWrapQueue__headOffset', |
| 89 | + value='offsetof(Environment::HandleWrapQueue, head_)', |
| 90 | + headers=['env.h'], |
| 91 | + type_='size_t', |
| 92 | + ), |
| 93 | + DebugSymbol( |
| 94 | + name='class__HandleWrapQueue__nextOffset', |
| 95 | + value='offsetof(ListNode<HandleWrap>, next_)', |
| 96 | + headers=['handle_wrap.h', 'util.h'], |
| 97 | + type_='size_t', |
| 98 | + ), |
| 99 | + DebugSymbol( |
| 100 | + name='class__Environment__reqWrapQueue', |
| 101 | + value='nonstandard_offsetof(Environment, req_wrap_queue_)', |
| 102 | + headers=['env.h'], |
| 103 | + type_='size_t', |
| 104 | + ), |
| 105 | + DebugSymbol( |
| 106 | + name='class__ReqWrap__node', |
| 107 | + value='nonstandard_offsetof(ReqWrap<uv_req_t>, req_wrap_queue_)', |
| 108 | + headers=['req_wrap.h'], |
| 109 | + type_='size_t', |
| 110 | + ), |
| 111 | + DebugSymbol( |
| 112 | + name='class__ReqWrapQueue__headOffset', |
| 113 | + value='offsetof(Environment::ReqWrapQueue, head_)', |
| 114 | + headers=['env.h'], |
| 115 | + type_='size_t', |
| 116 | + ), |
| 117 | + DebugSymbol( |
| 118 | + name='class__ReqWrapQueue__nextOffset', |
| 119 | + value='offsetof(ListNode<ReqWrap<uv_req_t>>, next_)', |
| 120 | + headers=['req_wrap.h', 'util.h'], |
| 121 | + type_='size_t', |
| 122 | + ), |
| 123 | +] |
| 124 | + |
| 125 | + |
| 126 | +template = ''' |
| 127 | +/* |
| 128 | + * This file is generated by {filename}. Do not edit directly. |
| 129 | + */ |
| 130 | +
|
| 131 | +// Need to import standard headers before redefining private, otherwise it |
| 132 | +// won't compile |
| 133 | +{standard_includes} |
| 134 | +
|
| 135 | +int GenDebugSymbol(); |
| 136 | +
|
| 137 | +#define private friend int GenDebugSymbol(); private |
| 138 | +
|
| 139 | +#define nonstandard_offsetof(CLASS, FIELD) reinterpret_cast<uintptr_t>\ |
| 140 | + (&(static_cast<CLASS*>(0)->FIELD)) |
| 141 | +
|
| 142 | +{includes} |
| 143 | +
|
| 144 | +{declare_symbols} |
| 145 | +
|
| 146 | +namespace node {{ |
| 147 | +
|
| 148 | +int GenDebugSymbol() {{ |
| 149 | +{fill_symbols} |
| 150 | +return 1; |
| 151 | +}} |
| 152 | +
|
| 153 | +int debug_symbols_generated = GenDebugSymbol(); |
| 154 | +
|
| 155 | +}} |
| 156 | +''' |
| 157 | + |
| 158 | + |
| 159 | +def get_standard_includes(): |
| 160 | + ''' |
| 161 | + Try to find all standard C++ headers needed by node and its dependencies |
| 162 | + ''' |
| 163 | + includes = ["algorithm", "array", "atomic", "bitset", "cctype", "climits", |
| 164 | + "cmath", "cstdarg", "cstddef", "cstdint", "cstdio", "cstdlib", "cstring", |
| 165 | + "ctime", "deque", "exception", "forward_list", "fstream", "functional", |
| 166 | + "iomanip", "iosfwd", "iostream", "istream", "iterator", "limits", "list", |
| 167 | + "map", "memory", "new", "ostream", "queue", "set", "sstream", "stack", |
| 168 | + "streambuf", "string", "tuple", "type_traits", "typeinfo", "unordered_map", |
| 169 | + "unordered_set", "utility", "vector"] |
| 170 | + return includes |
| 171 | + |
| 172 | + |
| 173 | +def create_symbols_file(): |
| 174 | + out = file(sys.argv[1], 'w') |
| 175 | + headers = DebugSymbol.get_headers(debug_symbols) |
| 176 | + includes = ['#include "{0}"'.format(header) for header in headers] |
| 177 | + includes = '\n'.join(includes) |
| 178 | + |
| 179 | + standard_includes = get_standard_includes() |
| 180 | + standard_includes = ['#include <{0}>'.format(include) for include in standard_includes] |
| 181 | + standard_includes = '\n'.join(standard_includes) |
| 182 | + |
| 183 | + declare_symbols = '\n'.join([symbol.declare for symbol in debug_symbols]) |
| 184 | + fill_symbols = '\n'.join([symbol.fill for symbol in debug_symbols]) |
| 185 | + |
| 186 | + out.write(template.format( |
| 187 | + filename=sys.argv[0], |
| 188 | + includes=includes, |
| 189 | + standard_includes=standard_includes, |
| 190 | + declare_symbols=declare_symbols, |
| 191 | + fill_symbols=fill_symbols, |
| 192 | + )) |
| 193 | + |
| 194 | + |
| 195 | +if len(sys.argv) < 2: |
| 196 | + print('usage: {0} output.cc'.format(sys.argv[0])) |
| 197 | + sys.exit(2) |
| 198 | + |
| 199 | + |
| 200 | +create_symbols_file() |
0 commit comments