Skip to content

Commit c552e10

Browse files
Matheus MarchiniMylesBorins
Matheus Marchini
authored andcommitted
src, tools: add debug symbols for node internals
Before these changes, only V8 added debug symbols to Node's binary, limiting the possibilities for debugger's developers to add some features that rely on investigating Node's internal structures. These changes are a first steps towards empowering debug tools to navigate Node's internals strucutres. One example of what can be achieved with this is shown at nodejs/llnode#122 (a command which prints information about handles and requests on the queue for a core dump file). Node debug symbols are prefixed with node_dbg_. Ref: nodejs/llnode#122 PR-URL: #14901 Refs: nodejs/post-mortem#46 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 3f363d3 commit c552e10

File tree

2 files changed

+225
-1
lines changed

2 files changed

+225
-1
lines changed

node.gyp

+25-1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174

175175
'dependencies': [
176176
'node_js2c#host',
177+
'deps/nghttp2/nghttp2.gyp:nghttp2'
177178
],
178179

179180
'includes': [
@@ -294,6 +295,7 @@
294295
# node.gyp is added to the project by default.
295296
'common.gypi',
296297
'<(SHARED_INTERMEDIATE_DIR)/node_javascript.cc',
298+
'<(SHARED_INTERMEDIATE_DIR)/node-debug-support.cc',
297299
],
298300

299301
'variables': {
@@ -965,7 +967,29 @@
965967
}]]
966968
}],
967969
]
968-
}
970+
},
971+
{
972+
'target_name': 'node_postmortem_metadata',
973+
'type': 'none',
974+
'toolsets': ['host'],
975+
'actions': [
976+
{
977+
'action_name': 'gen-postmortem-metadata',
978+
'process_outputs_as_sources': 1,
979+
'inputs': [
980+
'./tools/gen-postmortem-metadata.py',
981+
],
982+
'outputs': [
983+
'<(SHARED_INTERMEDIATE_DIR)/node-debug-support.cc',
984+
],
985+
'action': [
986+
'python',
987+
'./tools/gen-postmortem-metadata.py',
988+
'<@(_outputs)',
989+
]
990+
}
991+
]
992+
},
969993
], # end targets
970994

971995
'conditions': [

tools/gen-postmortem-metadata.py

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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='offsetof(BaseObject, persistent_handle_)',
72+
headers=['base_object-inl.h'],
73+
type_='size_t',
74+
),
75+
DebugSymbol(
76+
name='class__Environment__handleWrapQueue',
77+
value='offsetof(Environment, handle_wrap_queue_)',
78+
headers=['env.h'],
79+
type_='size_t',
80+
),
81+
DebugSymbol(
82+
name='class__HandleWrap__node',
83+
value='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='offsetof(Environment, req_wrap_queue_)',
102+
headers=['env.h'],
103+
type_='size_t',
104+
),
105+
DebugSymbol(
106+
name='class__ReqWrap__node',
107+
value='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+
{includes}
140+
141+
{declare_symbols}
142+
143+
namespace node {{
144+
145+
int GenDebugSymbol() {{
146+
{fill_symbols}
147+
return 1;
148+
}}
149+
150+
int debug_symbols_generated = GenDebugSymbol();
151+
152+
}}
153+
'''
154+
155+
156+
def get_standard_includes():
157+
'''
158+
Try to find all standard C++ headers needed by node and its dependencies
159+
'''
160+
includes = set()
161+
regex = re.compile('#include *<([a-zA-Z0-9\-_]*)>')
162+
for src in ["src", "deps"]:
163+
for root, dirnames, filenames in os.walk(src):
164+
for filename in fnmatch.filter(filenames, '*.h'):
165+
f = open(os.path.join(root, filename), 'r')
166+
for line in f.readlines():
167+
match = regex.match(line)
168+
if match:
169+
includes.add(match.group(1))
170+
return sorted(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

Comments
 (0)