Skip to content

Commit

Permalink
src, tools: add debug symbols for node internals
Browse files Browse the repository at this point in the history
Those changes are the first steps towards allowing debug tools to
navigate some of Node's internals strucutres, giving more possibilities
to developers doing post-mortem debugging. One example of what can be
achieved with the symbols added is a new command being developed for
llnode, which prints information about handles and requests on the
queue for a core dump file.

Ref: nodejs/post-mortem#46
  • Loading branch information
Matheus Marchini committed Aug 17, 2017
1 parent 60f2fa9 commit acf9da6
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 1 deletion.
1 change: 1 addition & 0 deletions common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
}, {
'os_posix': 1,
'v8_postmortem_support%': 'true',
'node_postmortem_support%': 'true',
}],
['OS== "mac"', {
'OBJ_DIR%': '<(PRODUCT_DIR)/obj.target',
Expand Down
23 changes: 22 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,28 @@
'ldflags': [ '-I<(SHARED_INTERMEDIATE_DIR)' ]
}],
]
}
},
{
'target_name': 'node-postmortem-metadata',
'type': 'none',
'variables': {},
'actions': [
{
'action_name': 'gen-postmortem-metadata',
'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
7 changes: 7 additions & 0 deletions node.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@
[ 'node_no_browser_globals=="true"', {
'defines': [ 'NODE_NO_BROWSER_GLOBALS' ],
} ],
[ 'node_postmortem_support=="true"', {
'dependencies': [ 'node-postmortem-metadata' ],
'defines': [ 'NODE_POSTMORTEM_SUPPORT' ],
'sources': [
'<(SHARED_INTERMEDIATE_DIR)/node-debug-support.cc'
]
}],
[ 'node_use_bundled_v8=="true" and v8_postmortem_support=="true"', {
'dependencies': [ 'deps/v8/src/v8.gyp:postmortem-metadata' ],
'conditions': [
Expand Down
3 changes: 3 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "env.h"
#include "env-inl.h"
#include "async-wrap.h"
#include "base-object.h"
#include "v8.h"
#include "v8-profiler.h"

Expand All @@ -23,6 +24,8 @@ using v8::Message;
using v8::StackFrame;
using v8::StackTrace;

Environment *Environment::currentEnvironment;

void Environment::Start(int argc,
const char* const* argv,
int exec_argc,
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ class IsolateData {

class Environment {
public:
static Environment *currentEnvironment;
class AsyncHooks {
public:
// Reason for both UidFields and Fields are that one is stored as a double*
Expand Down
7 changes: 7 additions & 0 deletions src/handle_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class HandleWrap : public AsyncWrap {

inline uv_handle_t* GetHandle() const { return handle_; }

#ifdef NODE_POSTMORTEM_SUPPORT
inline ListNode<HandleWrap> *handle_wrap_queue() {
return &handle_wrap_queue_;
}
#endif


protected:
HandleWrap(Environment* env,
v8::Local<v8::Object> object,
Expand Down
1 change: 1 addition & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4528,6 +4528,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
Environment env(isolate_data, context);
Environment::currentEnvironment = &env;
CHECK_EQ(0, uv_key_create(&thread_local_env));
uv_key_set(&thread_local_env, &env);
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
Expand Down
4 changes: 4 additions & 0 deletions src/req-wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ReqWrap : public AsyncWrap {
inline ~ReqWrap() override;
inline void Dispatched(); // Call this after the req has been dispatched.
T* req() { return &req_; }
#ifdef NODE_POSTMORTEM_SUPPORT
inline ListNode<ReqWrap> *req_wrap_queue() { return &req_wrap_queue_; }
#endif


private:
friend class Environment;
Expand Down
8 changes: 8 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class ListNode {
inline ~ListNode();
inline void Remove();
inline bool IsEmpty() const;
#ifdef NODE_POSTMORTEM_SUPPORT
inline ListNode** next() { return &next_; }
#endif

private:
template <typename U, ListNode<U> (U::*M)> friend class ListHead;
Expand Down Expand Up @@ -189,6 +192,11 @@ class ListHead {
inline Iterator begin() const;
inline Iterator end() const;

#ifdef NODE_POSTMORTEM_SUPPORT
inline ListNode<T> *head() { return &head_; }
#endif


private:
ListNode<T> head_;
DISALLOW_COPY_AND_ASSIGN(ListHead);
Expand Down
140 changes: 140 additions & 0 deletions tools/gen-postmortem-metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/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):
seen = set()
seen_add = seen.add # faster than seen.add on each iteration
headers = [debug_symbol.headers for debug_symbol in debug_symbols]
headers = sum(headers, [])

return [h for h in headers if not (h in seen or seen_add(h))]

def __str__(self):
return "{type} {prefix}{name} = {value};".format(
type=self.type_,
prefix=self._PREFIX,
name=self.name,
value=self.value,
)


debug_symbols = [
DebugSymbol(
name="currentEnvironment",
value="(uint64_t) &Environment::currentEnvironment",
headers=["env.h"],
type_="uint64_t",
),
DebugSymbol(
name="class__BaseObject__persistant_handle",
value="(size_t) &(((BaseObject*)0)->persistent())",
headers=["base-object.h", "base-object-inl.h"],
type_="size_t",
),
DebugSymbol(
name="class__Environment__handleWrapQueue",
value="(size_t) (((Environment*)0)->handle_wrap_queue())",
headers=["env.h"],
type_="size_t",
),
DebugSymbol(
name="class__HandleWrap__node",
value="(size_t) (((HandleWrap*)0)->handle_wrap_queue())",
headers=["handle_wrap.h"],
type_="size_t",
),
DebugSymbol(
name="class__HandleWrapQueue__headOffset",
value="(size_t) (((Environment::HandleWrapQueue*)0)->head())",
headers=["env.h"],
type_="size_t",
),
DebugSymbol(
name="class__HandleWrapQueue__nextOffset",
value="(size_t) (((ListNode<HandleWrap>*)0)->next())",
headers=["handle_wrap.h", "util.h"],
type_="size_t",
),
DebugSymbol(
name="class__Environment__reqWrapQueue",
value="(size_t) (((Environment*)0)->req_wrap_queue())",
headers=["env.h"],
type_="size_t",
),
DebugSymbol(
name="class__ReqWrap__node",
value="(size_t) (((ReqWrap<uv_req_t>*)0)->req_wrap_queue())",
headers=["req-wrap.h"],
type_="size_t",
),
DebugSymbol(
name="class__ReqWrapQueue__headOffset",
value="(size_t) (((Environment::ReqWrapQueue*)0)->head())",
headers=["env.h"],
type_="size_t",
),
DebugSymbol(
name="class__ReqWrapQueue__nextOffset",
value="(size_t) (((ListNode<ReqWrap<uv_req_t>>*)0)->next())",
headers=["req-wrap.h", "util.h"],
type_="size_t",
),
]


template = '''
/*
* This file is generated by {filename}. Do not edit directly.
*/
{includes}
using namespace node;
extern "C" {{
{symbols}
}}
'''


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

symbols = "\n".join([str(symbol) for symbol in debug_symbols])

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


if (len(sys.argv) < 2):
print('usage: %s output.cc' % sys.argv[0])
sys.exit(2)


create_symbols_file()

0 comments on commit acf9da6

Please sign in to comment.