How to Implement a Debugging Tool in My Custom C++ Editor?

I have a custom C++ editor I built from scratch, and I want to integrate a debugging tool similar to GDB. I want to be able to debug compiled .cpp files and retrieve the exact values of printed variables. Is there a third-party library (available on GitHub or elsewhere) that can help me achieve this? I need a solution that can be integrated into my custom C++ editor, as using the GDB command line directly is not feasible for my use case.

This is why it’s impossible to use GDB command line: It’s impossible to link a function in my custom C++ editor with the GDB command line, I mean it’s impossible to set/get function pointers between GDB command line and my C++ code editor. Or maybe I’m wrong because how did the VSCode developers do?

Here’s the code snippet from my custom C++ editor where I want to implement this debugging functionality:

#include <stdio.h>
#include <string>

int main() {
    int a = 2;
    int b = 7;
    int c = a + b;
    printf("%d\n", c);
    return 0;
}

GDB (command line)

(gdb) break 7
Breakpoint 1 at 0x1400016eb: file test.cpp, line 7.
(gdb) run
Starting program: test.exe
[New Thread 10892.0x38dc]

Thread 1 hit Breakpoint 1, main () at test.cpp:7
7           int c = a + b;
(gdb) next
8           printf("%d\n", c);
(gdb) print a
$1 = 2
(gdb) print b
$2 = 7
(gdb) print c
$3 = 9

Question: Is there a third-party library (GitHub) version of GDB so I can debug a compiled .cpp and getting the exact value of printed variable for example?

void _Project::run_debug() {
    if (isRunning()) {
        throw string("It's already running."); return;
    }
    _isRunning_debug = true;

    // What should be the code here?
}

It’s no simple task but this thread has plenty of good leads. If you haven’t already, consider taking some course in compilers as I feel like it will give you a nice perspective.

@toastedpitabread, I read your link. It seems that one approach is to analyze the gdb-remote protocol packets. An individual recommends using LLDB, which is a more recent alternative to GDB. Therefore, I’m considering investing my time in a more advanced tool like LLDB for complex tasks. In the link you shared, some people also propose studying the source code of an existing debugger. However, this would require the ability to compile the source code, which is not easy.

Hence, if I decide to dedicate my time, I would prefer to utilize the newer technology, LLDB, which, as per the information in your link, offers an API.

There’s probably a trade-off there. GDB is older but more mature in its offerings. Depending on what you have in mind for it there are a lot of other considerations. But yes, for quicker ease of use and development LLDB sounds like a good fit.

In the link you gave, I found the following command line which makes me think that there maybe a solution to set/get function pointers or something like that, but how to do it? You can answer for GDB.

gdb -p <pid of process>

// How to get the function pointer of this function from the C++ code editor and call it?
extern "C" void myFunction() {
    printf("Hello, World!\n");
}

Clang command line:
clang++ -Wall -Wdouble-promotion -c -fPIC “source.cpp” -o “source.o” -O3 -s -DNDEBUG
clang++ -shared “source.o” -lstdc++ -o “main-lib.so”

Error:
clang++: error: unsupported option ‘-fPIC’ for target ‘x86_64-pc-windows-msvc’

I post this if someone can tell me how to fix it?

Why I like GCC more than Clang?
GCC compilation time can be 2 times slower than Clang compilation time, but in my C++ editor, I just use it to compile small codes alternative to scripts for the moment, so compilation time is not really a problem for the moment. But GCC compiled executable files [Debug] have much more performance than Clang compiled executable files [Debug].

Well ok, you’re also in windows (which is why you get the target error), so you’ll have to work with the Windows debugging libraries and their documentation. This is a very involved task. The best I can do is point you to resources. Again, to put it in perspective, this is not a simple task. But definitely go for it as it is pretty fun to learn about.

The links you gave me are for Windows, but if possible, I need cross platform APIs/libraries, so I will have the same codes for Linux/Android/Windows/… if possible.

The reason I ask on forum is I couldn’t find answers of my main question everywhere, even from ChatGPT/Google Bard, in the following link, someone tries to make a debugging: debugging - How to debug and step into custom language sources transpiled to C++? - Stack Overflow

Again, to put it in perspective, this is not a simple task.

Since it’s not a simple task, is it much easier to build a debugger from scratch as I already know how to transpile the code into .cpp , I mean is it simpler than using GDB with cross platform codes, if you look at my experience?

This is the code editor with custom programming language I built from scratch I was talking about, the transpiled code image is edited which is a concept, and the breakpoint is an edited image too. I just want to tell what is my goal.