Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you debug a Linux core dump using VSCode?

I am purposely generating a core dump from a C++ application I'm writing using VSCode. I cannot figure out how to debug the core dump. Has anyone had any experience with this they'd be willing to share?

***** UPDATE ***** I believe I have it working now. I created a second debug configuration for core files. I needed to add the "coreDumpPath" option that pointed to the dump file generated. I also needed to remove preLaunchTask option that would always build a new executable.


like image 666
David Massat Avatar asked Apr 17 '19 21:04

David Massat


3 Answers

From the VScode docs

Memory dump debugging

The C/C++ extension for VS Code also has the ability to debug memory dumps. To debug a memory dump, open your launch.json file and add the coreDumpPath (for GDB or LLDB) or dumpPath (for the Visual Studio Windows Debugger) property to the C++ Launch configuration, set its value to be a string containing the path to the memory dump. This will even work for x86 programs being debugged on an x64 machine.

P.S. The asker has already updated the question with the solution. But adding this answer to help those people who jump directly into the answer section ;)


Update: Sample launch.json for C/C++ extension without hardcoding core file name

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Open a core dump(c/c++)",
            "program": "<Path to the program here>",
            "coreDumpPath": "${input:coreFileName}",
            "cwd": "${workspaceFolder}",
            "MIMode": "lldb" // or gdb, if you are using gdb
        }
    ],
    "inputs": [
      {
        "id": "coreFileName",
        "type": "promptString",
        "description": "Enter core file path"
      }
    ]
}

Sample launch.json for CodeLLDB extension without hardcoding core file name

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "custom",
            "name": "Open a core dump",
            "initCommands": [
                "target create -c ${input:coreFileName}"
            ]
        }
    ],
    "inputs": [
      {
        "id": "coreFileName",
        "type": "promptString",
        "description": "Enter core file path"
      }
    ]    
}
like image 164
Insaf K Avatar answered Nov 10 '22 15:11

Insaf K


To give a concrete example of @insaf's answer. This can be used as a template for launch.json (click the settings button on top of the debug side-bar to edit it):

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "<PATH-TO-BINARY>",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "coreDumpPath": "<PATH-TO-CORE-DUMP>"
        }
    ]
}

Edit the paths of the binary and core dump accordingly.

Then press the green play button (start debugging) -- it doesn't really start anything but load the core dump.

like image 26
bluenote10 Avatar answered Nov 10 '22 15:11

bluenote10


I wrote a little helper script that duplicates the functionality of coredumpctl in a machine-readable way.

It

  • searches the systemd journal for logged coredumps
  • uses fzf to allow the user to interactively select one of them
  • decompresses the selected coredump to /tmp/coredump
  • links the associated executable to /tmp/coredump_program

Then I created the following tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "select coredump",
            "type": "shell",
            "command": "coredumpselect.py",
            "args": [],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        }
    ]
}

and added the following debug configuration to launch.json:

        {
            "name": "View coredump",
            "type": "cppdbg",
            "request": "launch",
            "program": "/tmp/coredump-program",
            "args": [],
            "cwd": "${workspaceRoot}",
            "coreDumpPath": "/tmp/coredump",
            "preLaunchTask": "select coredump",
            "MIMode": "gdb",
        }

Now when you launch the "View coredump" debugging action, you'll be given an interactive choice of coredumps in the terminal. After selecting one of them, the vscode debugger will open it.

To run the script on your Linux system, you need to install the systemd Python package. On Ubuntu, use sudo apt install python3-systemd. On Arch Linux, use sudo pacman -S python-systemd. With pip, the packages is available as systemd-python (not to be confused with systemd, which is a different incompatible package). You'll also need fzf (sudo apt install fzf, sudo pacman -S fzf), and the proper decompressor for the coredump (e.g. lz4 or zstd).

like image 1
mic_e Avatar answered Nov 10 '22 14:11

mic_e