Debugging gdb: exit code 139 in VSCode - c++

I am working on a large codebase and trying to debug the same using gdb(C/C++) extension on VSCode.
On running a particular configuration, I get an error with exit code 139. I know this is a segmentation fault, but am unable to exactly pinpoint where the seg fault is occurring. Is there any way I can monitor that?
Probably some file that I am missing where the details are being captured?
I can debug segfault using gdb from the terminal. But since the code requires certain configurations and is spread over hundreds of files, I am not sure how to do that.

Related

Debug huge C++ project on linux

I developed a project on Mac using C++. It works perfectly. However when I try to launch it from a Linux server, I get a bad alloc error:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
I don't know how to debug the whole project because I have absolutely no idea of where the issue come from. Why is it working on my Mac and not on Linux? All articles and questions about that only ask for single file program but not 40+ files project.
Is there a way to get the file or line that causes the bad alloc?
Build your program with the -g compiler option to get meaningful stack traces.
Then run the program in a debugger, e.g. gdb:
gdb --args [executable] [arguments...]
When gdb has loaded, enter the command run and you program will be run. As soon as the exception is thrown and not caught by your program, gdb will show you a stack trace showing you from where the exception was thrown.
std::bad_alloc means that your program wasn't able to allocate any more memory, probably because the OS either ran out of memory or you hit an explicit memory limit imposed by the system.

The gdb process terminated unexpectedly. Error codes

I'm having this message: The gdb process terminated unexpectedly (code 3) while debugging a plain C++ application at Qt Creator.
The problem is that sometimes gdb crashes and sometimes it works. The application consists of one file: main.cpp.
If I use Code::Blocks, the application works.
The problem is connected with my code, because I've never had this error before.
I have 2 questions:
Where can I find information about the meaning of the error codes? What does code 3 mean?
How can we cause this gdb error by "bad code"? We can easily cause run-time or compiler error, but how can we make gdb to crash?
Thanks so much for answers

Debugging segmentation faults (core dumped) for big projects

I tried to run this code [the-git-link] (https://github.com/DropIn/SequentialTextReading), but I experience a "Segmentation fault (core dumped)". Do you have any idea about this error? I am running it on Ubuntu 14.4 and
the code is too big to use traditional debug methods on it like using gdb.
Any idea how to solve this problem or what tools should i use to solve it knowing that there are many files and main in the project?
You can also load your coredump to gdb and find the root cause, after your program crashed.
You may benefit from reading How to generate core dump file in Ubuntu, and How to analyze a program's core dump file with gdb?

Project build failed in release mode, runs fine in debug mode

I have a big project solution that I am trying to execute, it builds perfectly fine in both release as well as debug mode. The problem is when I try to execute it the debug one is fine but release mode creates segmentation fault and creates a core dump.
Now can anyone help me about how to find out the line number in the code that might have caused the segmentation fault. I tried using gdb but couldn't do much.
Now can anyone help me about how to find out the line number in the code that might have caused the segmentation fault.
Not with the information you've provided (which is insufficient).
You should read GDB documentation. Here is a good place to start.
You should load the executable and core into GDB, and use where command to fund out which function you are crashing in.
Since the crash only happens in "release mode", you'll need to figure out how to add -g flag to your release mode build. Once you've done that, GDB will tell you the file and line number where the crash happens.

c++ what is a good debugger for segmentation errors?

Does anyone know a good debugger for C++ segmentation errors on the Linux environment? It would be good enough if the debugger could trace which function is causing the error.
Also consider some techniques that do require from you code changes:
Run your app via valgrind memcheck tool. It's possible to catch error when you access wrong address (e.g. freed pointer, not initialized) - see here.
If you use extensievly stl/boost, consider compiling with -D_GLIBCXX_DEBUG and -D_GLIBCXX_DEBUG_PEDANTIC (see here). This can catch such errors as using invalidated iterator, accessing incorrect index in vector etc.
tcmalloc (from google per tool). When linking with it's debug enabled version, it may find memory related problems
Even more ...
GDB! what else is available on Linux?
Check this out for starting up with GDB, its a nice, concise and easy to understand tutorial.
GDB is indeed about the only choice. There are some GUI's but they are allmost all wrappers for gdb. Finding a segfault is easy. Make sure you compile with -g -O0 then start gdb with your program as argument.
In gdb type run
To start your program running, gdb will stop it is soon as it hits a segfault and report on which line that was. If you need a full backtrace then just type bt. To get out of gdb enter quit.
BTW gdb has a build in help, just type help.