You need to know how to debug with gdb first. This is a good book to start with as it explains how the program ypu are debugging is structured, something else that’s good to know. Plenty of examples. It’s quite old, 2008, but I have it and still use it when I have to.
Things don’t work that way. Debugging an ESP32 involves having a debug probe, e.g. in the form of an esp-prog with a chip that can speak JTAG while having a USB connection, connected to the ESP32 via its respective JTAG lines. See article at Low-cost ESP32 In-circuit Debugging | by Manuel Bl. | Medium as an example. I agree with @normandunbar here, a book on that might clear up these fundamental misconceptions and give you a strong background in that area.
For debugging AVRs, PlatformIO has documentation here. This debugging method is quite special as it uses something called a GDB stub that communicates over serial – hence you program is also not allowed to use Serial when debugging this way. Debugging via ISP / debugWire, which would be the equivalent of JTAG debugging for other chips, is not implemented in PlatformIO natively.
In case you can’t order the book, or wait for its arrival, there’s an online tutorial covering just abpout everything you might need to knwo about GDB for debugging. It’s obvioulsy not Arduino/ESP32 based, but the commands are the same – looks useful.
You can also download the tutorial as a OPDF file for a nominal fee, $9.99 in your part of the world.