debuggingterminalgdbneovimcgdb

How can I debug a terminal program (like vim) with gdb/cgdb?


Background

I am modifying the source code of vim and I need to use a debugger.

Problem

Since vim is a terminal program, it takes over the terminal when it starts. So as soon as I start inside gdb, I'm just inside vim and I can't use gdb commands. When I quit vim, then I can get back to gdb but, well... then the target program ends so I can't debug it.

If I enter gdb, set breakpoints, then run vim and interact with it, my breakpoints don't actually break.

What I Tried

  1. Make sure vim is built with debug symbols (make CMAKE_BUILD_TYPE=Debug)
  2. Try with normal cgdb (VIMRUNTIME=runtime cgdb --args ./build/bin/nvim my-file)
  3. Try attaching to the process separately (sudo cgdb -p 62556)
  4. Try with some flags (VIMRUNTIME=runtime gdb ./build/bin/nvim -q --tui)

Question

How can I run gdb (or cgdb) in such a way where I can interact with gdb and also interact with the terminal program that I am debugging?


Solution

  • General Answer

    The approach with cgdb -p <pid> was close, and the right idea. However, some programs (and Neovim in particular) will spawn an additional process. Sometimes that is the process to which you have to connect in order for breakpoints to work.

    To properly view the list of processes, use ps aux. In this case, it shows one process for nvim and a separate process for nvim --embed. In order to debug the process, I had to connect to the --embed one.

    Situation-specific Details

    Neovim provides some details about how to debug the program. This is also a viable method in this case, though not generically applicable.