Skip to content

Instantly share code, notes, and snippets.

@radosny
Last active May 20, 2019 02:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radosny/781fad70dc980fc68f6bb3c287e20b86 to your computer and use it in GitHub Desktop.
Save radosny/781fad70dc980fc68f6bb3c287e20b86 to your computer and use it in GitHub Desktop.
Node postmortem debugging (OSX)

Node postmortem debugging (OSX)

I assume that you have an access to ssh and node server process is up and running.

1. Taking core dump

To get core dump of running node process:

  • Take PID of that process:

    pgrep -lf node
  • Run lldb debugger with attached process:

    lldb -p <PID>
  • Take a dump of process data:

    (lldb) process save-core <FILE_NAME>

It won't kill your service, so you can dump it while is running

Alternative: If you have signed (on OSX only) gdb with gcore you can run simply gcore <PID>

2. Extend lldb with v8 debugger support

  • Fetch the repository with lldb extension. (It will be used later).

    git clone git@github.com:nodejs/llnode.git
  • Install llnode

    brew install llnode
  • Install python with pip if you don't have it already.

    brew install python
  • Install python six module

    pip install six
  • Link llnode with lldb

    mkdir -p ~/Library/Application\ Support/LLDB/PlugIns
    
    ln -sf /usr/local/opt/llnode/llnode.dylib ~/Library/Application\ Support/LLDB/PlugIns/

3. Debug

In order to have full functionallity we need to have generated memory ranges file for each core dump that we want to investigate.

  • To do so we need to run from previously fetched llnode repo:

    ./scripts/otool2segments.py <GENERATED_CORE_DUMP> > <RANGES_FILE>
  • Then just export that file in environment variable.

    export LLNODE_RANGESFILE=<RANGES_FILE>
  • Run lldb with our node dump. (Please notice that node binary should be the same as your generated coredump)

    lldb node -c <GENERATED_CORE_DUMP>

Alternative: You can also lunch it with single command: env LLNODE_RANGESFILE=<RANGES_FILE> lldb node -c <GENERATED_CORE_DUMP>

  • Check if everything works:
    (lldb) help v8

Some docs for v8 commands are available here: https://developer.ibm.com/node/2016/09/27/advances-in-core-dump-debugging-for-node-js/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment