Skip to content

Commit

Permalink
src, tools: add debug symbols for node internals
Browse files Browse the repository at this point in the history
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
Ref: nodejs/post-mortem#46
  • Loading branch information
Matheus Marchini authored and Matheus Marchini committed Nov 7, 2017
1 parent 83fcb9f commit 94504f8
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 1 deletion.
26 changes: 25 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@

'dependencies': [
'node_js2c#host',
'node_postmortem_metadata#host',
'deps/nghttp2/nghttp2.gyp:nghttp2'
],

Expand Down Expand Up @@ -295,6 +296,7 @@
# node.gyp is added to the project by default.
'common.gypi',
'<(SHARED_INTERMEDIATE_DIR)/node_javascript.cc',
'<(SHARED_INTERMEDIATE_DIR)/node-debug-support.cc',
],

'defines': [
Expand Down Expand Up @@ -743,7 +745,29 @@
'ldflags': [ '-I<(SHARED_INTERMEDIATE_DIR)' ]
}],
]
}
},
{
'target_name': 'node_postmortem_metadata',
'type': 'none',
'toolsets': ['host'],
'actions': [
{
'action_name': 'gen-postmortem-metadata',
'process_outputs_as_sources': 1,
'inputs': [
'./tools/gen-postmortem-metadata.py',
],
'outputs': [
'<(SHARED_INTERMEDIATE_DIR)/node-debug-support.cc',
],
'action': [
'python',
'./tools/gen-postmortem-metadata.py',
'<@(_outputs)',
]
}
]
},
], # end targets

'conditions': [
Expand Down
2 changes: 2 additions & 0 deletions src/base-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "v8.h"
#include "util.h"

namespace node {

Expand Down Expand Up @@ -59,6 +60,7 @@ class BaseObject {
inline void ClearWeak();

private:
ALLOW_DEBUG_SYMBOLS
BaseObject();

template <typename Type>
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ class Environment {
bool EmitNapiWarning();

private:
ALLOW_DEBUG_SYMBOLS
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
const char* errmsg);

Expand Down
1 change: 1 addition & 0 deletions src/handle_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class HandleWrap : public AsyncWrap {
~HandleWrap() override;

private:
ALLOW_DEBUG_SYMBOLS
friend class Environment;
friend void GetActiveHandles(const v8::FunctionCallbackInfo<v8::Value>&);
static void OnClose(uv_handle_t* handle);
Expand Down
1 change: 1 addition & 0 deletions src/req-wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ReqWrap : public AsyncWrap {
T* req() { return &req_; }

private:
ALLOW_DEBUG_SYMBOLS
friend class Environment;
ListNode<ReqWrap> req_wrap_queue_;

Expand Down
8 changes: 8 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ inline char* Calloc(size_t n);
inline char* UncheckedMalloc(size_t n);
inline char* UncheckedCalloc(size_t n);


// Used to make all members of a class visible when creating debug symbol (see
// tools/gen-postmortem-metadata.py)
#define ALLOW_DEBUG_SYMBOLS friend int genDebugSymbols();

// Used by the allocation functions when allocation fails.
// Thin wrapper around v8::Isolate::LowMemoryNotification() that checks
// whether V8 is initialized.
Expand Down Expand Up @@ -158,6 +163,7 @@ class ListNode {
inline bool IsEmpty() const;

private:
ALLOW_DEBUG_SYMBOLS
template <typename U, ListNode<U> (U::*M)> friend class ListHead;
ListNode* prev_;
ListNode* next_;
Expand All @@ -174,6 +180,7 @@ class ListHead {
inline bool operator!=(const Iterator& that) const;

private:
ALLOW_DEBUG_SYMBOLS
friend class ListHead;
inline explicit Iterator(ListNode<T>* node);
ListNode<T>* node_;
Expand All @@ -190,6 +197,7 @@ class ListHead {
inline Iterator end() const;

private:
ALLOW_DEBUG_SYMBOLS
ListNode<T> head_;
DISALLOW_COPY_AND_ASSIGN(ListHead);
};
Expand Down
169 changes: 169 additions & 0 deletions tools/gen-postmortem-metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#!/usr/bin/env python

#
# gen-postmortem-metadata.py output_file.cc
#
# Creates debugging symbols to help naviage Node's internals using post-mortem
# debugging tools.
#

import sys


class DebugSymbol(object):
type_ = 'int'
_prefix = 'nodedbg_'

def __init__(self, name, value, headers=[], type_=None):
self.name = name
self.value = value
self.headers = headers
self.type_ = type_ or DebugSymbol.type_

@classmethod
def get_headers(cls, debug_symbols):
'''
Return a list of headers without duplicates, preserving the order they were
declared
'''
seen = set()
headers = [debug_symbol.headers for debug_symbol in debug_symbols]
headers = sum(headers, [])

result = []
for h in headers:
if not h in seen:
seen.add(h)
result.append(h)

return result

@property
def declare(self):
return '{type} {prefix}{name};'.format(
type=self.type_,
prefix=self._prefix,
name=self.name,
)

@property
def fill(self):
return '{prefix}{name} = {value};'.format(
prefix=self._prefix,
name=self.name,
value=self.value,
)


debug_symbols = [
DebugSymbol(
name='environment_context_idx_embedder_data',
value='Environment::kContextEmbedderDataIndex',
headers=['env.h'],
type_='int',
),
DebugSymbol(
name='class__BaseObject__persistent_handle',
value='offsetof(BaseObject, persistent_handle_)',
headers=['base-object.h', 'base-object-inl.h'],
type_='size_t',
),
DebugSymbol(
name='class__Environment__handleWrapQueue',
value='offsetof(Environment, handle_wrap_queue_)',
headers=['env.h'],
type_='size_t',
),
DebugSymbol(
name='class__HandleWrap__node',
value='offsetof(HandleWrap, handle_wrap_queue_)',
headers=['handle_wrap.h'],
type_='size_t',
),
DebugSymbol(
name='class__HandleWrapQueue__headOffset',
value='offsetof(Environment::HandleWrapQueue, head_)',
headers=['env.h'],
type_='size_t',
),
DebugSymbol(
name='class__HandleWrapQueue__nextOffset',
value='offsetof(ListNode<HandleWrap>, next_)',
headers=['handle_wrap.h', 'util.h'],
type_='size_t',
),
DebugSymbol(
name='class__Environment__reqWrapQueue',
value='offsetof(Environment, req_wrap_queue_)',
headers=['env.h'],
type_='size_t',
),
DebugSymbol(
name='class__ReqWrap__node',
value='offsetof(ReqWrap<uv_req_t>, req_wrap_queue_)',
headers=['req-wrap.h'],
type_='size_t',
),
DebugSymbol(
name='class__ReqWrapQueue__headOffset',
value='offsetof(Environment::ReqWrapQueue, head_)',
headers=['env.h'],
type_='size_t',
),
DebugSymbol(
name='class__ReqWrapQueue__nextOffset',
value='offsetof(ListNode<ReqWrap<uv_req_t>>, next_)',
headers=['req-wrap.h', 'util.h'],
type_='size_t',
),
]


template = '''
/*
* This file is generated by {filename}. Do not edit directly.
*/
// #define private public
// #define protected public
{includes}
{declare_symbols}
namespace node {{
int genDebugSymbols() {{
{fill_symbols}
return 1;
}}
int debugSymbolsGenerated = genDebugSymbols();
}}
'''


def create_symbols_file():
out = file(sys.argv[1], 'w')
headers = DebugSymbol.get_headers(debug_symbols)
includes = ['#include "{0}"'.format(header) for header in headers]
includes = '\n'.join(includes)

declare_symbols = '\n'.join([symbol.declare for symbol in debug_symbols])
fill_symbols = '\n'.join([symbol.fill for symbol in debug_symbols])

out.write(template.format(
filename=sys.argv[0],
includes=includes,
declare_symbols=declare_symbols,
fill_symbols=fill_symbols,
))


if len(sys.argv) < 2:
print('usage: {0} output.cc'.format(sys.argv[0]))
sys.exit(2)


create_symbols_file()

0 comments on commit 94504f8

Please sign in to comment.