GDB hangs on GNU Fortran stack frame - c++

I have a large, mixed C++ and Fortran code base that I'm trying to debug.
The program is built with GCC and GNU Fortran version 4.8. The Fortran code is built with -c -g -cpp -ffixed-line-length-none -ffree-line-length-none -fcoarray=single -fno-underscoring -fPIC and the C++ code is built with -g -Wall -Wno-reorder -Wno-parentheses -std=c++11 -Wno-unknown-pragmas -fPIC. The program is linked with g++ -g ... -lgfortran.
When I run the program in GDB, I can set a break point in the Fortran code using break my_func.f90:35. However, when the program hits that breakpoint, the debugger appears to go into an infinite loop trying to print out the stack trace. If I press Ctrl+C at this point, I get a truncated stack trace, like this:
^CBreakpoint 2, my_func (accinfile=Quit
(gdb)
So the program has hit the breakpoint, it seems something is going wrong with the stack trace. If it breaks in C++ code but with a Fortran function in the stack trace, I get the stack trace up to the Fortran part, but again a hang at that point.
I've confirmed that GDB works with a very simple C++/Fortran program.
Has anyone come across this behaviour? Is there a known solution?

Related

My code segfaults when compiled as shared object, works fine as standalone binary

I have a vexing C++ problem. I've got a huge, complex wedge of code which is compiled into a Linux shared object. When it goes through one specific code path, it segfaults. (Basically one of the STL iterators seems to point a few bytes to the side of where it should.) But, if I #include all the code into one giant source file and compile it into a stand-alone program, everything works perfectly.
How do I even begin to debug this? The problem appears to not be a source-level problem, but (I guess?) some kind of weird linker issue. I'm completely lost. (I thought STL was all compile-time stuff...)
In case it matters, the SO is compiled like so:
g++ -m64 -c -D_CONSOLE -D__UNICODE__ -fPIC -O0 -g -D_DEBUG -D_GLIBCXX_DEBUG <name> -o <name>.o
g++ -m64 -shared -o MangoLib.so <objects...>
The stand-alone consists of a small driver program with a bunch of #include directives, built like so:
g++ -m64 -fPIC -O0 -g -D_DEBUG -D_GLIBCXX_DEBUG Test.cpp -o Test
I don't think I did anything wrong here... but the SO segfaults every single time, so I guess I did? I don't know what to do now.
I would start to check the usage of global variables and static initializers. The order of these things happening is not well defined AFAIK. Every global variable or static initializer that more complicated than "just fill this memory with 0 bits" should be suspect. Several static initializers referencing each other is basically a no go.

Expose debugging symbols for a program compiled with multiple languages

As per official instructions, to compile a program with debugging support you can run
g++ -std=c++11 -O0 -g -c -o program1.o program1.cpp
Now to do the same with a C program, it's just:
gcc -O0 -g -c -o program2.o program2.c
In order to link both types together, I could use:
g++ --std=c++11 -O0 -g -o program program.o program2.o
Then, to debug:
gdb program
gdb > run <PARAMS>
It worked completely after several attempts at tinkering with the compiler options (the above options are for a working version). In some cases the C symbols would load, but the C++ symbols would not.
Can someone shed some light as to what the recommended options are to enable debugging for non-trivial examples that mix several compiled languages? All of the documentation only refers to trivial examples.
Note that if you use just the -g option then the compiler will use operating system's native format, which can vary. You can explicitly specify the format instead, using other -g... varieties (for example -gdwarf-3 or -g-stabs). This allows you to guarantee that your object files will all have a consistent debug format, irrespective of where they were built.
You can also disable gdb-only extensions by using this approach, should you wish to use other debuggers. See this for details.
In each compilation and linking step, add the -g option to the compiler flags. -O0 is recommended too for debug builds so you don't get the compiler optimizing functions away. Being inconsistent may result in no debug symbols or partial debug symbols.

Debugger error: not in executable format: File format not recognized

I've written a program, saved it on the desktop under the name 'Swap.cpp' and when I run gdb (the first time), I get the error:
"/Users/myname/Desktop/Swap": not in executable format: File format
not recognized.
I have no idea what I'm doing wrong. Any help will be appreciated.
Sorry I should've given more information:
I am using Mac OS.
I've already compiled the program and have the Swap.o file that I can see on my desktop.And here are the commands that I enter while trying to run the debugger from bash:
$ clang++ -g Swap.cpp -o Swap
$ ./Swap
this runs Swap and then I try to access the debugger using:
$ gdb Swap
that then gives me the aforesaid message. I tried doing what Rakholiya Jenish suggested but to no avail.
To run gdb on windows:
path_to_gdb.exe program_to_debug
If the compilation was not proper, either compile it with your IDE (if you are using) or with g++ using cmd.exe as:
g++ -g Swap.cpp -o output -lm
I figured out how to use lldb instead of gdb. lldb works just fine. Here is what I did:
$ clang++ -g -o Swap Swap.cpp
$ lldb Swap
Thank you all for your help.

gdb not debugging mingw32-g++ compiled code well, but works with g++

I have a simple c++ program I'd like to debug with gdb. When I compile it with
g++ -g main.cpp -o main.exe
I can use gdb just fine with the resulting executable. But when I use
mingw32-g++ -g main.cpp -o main.exe
gdb says things such as
in ?? ()
and functionality is limited; I seem to be able to set break points and run the program, but stepping and such yields the message
Cannot find bounds of current function
As I said, this only happens when compiling with the mingw32-g++ executable, and not with the plain g++ one.
Why is this, and how can I debug executables created with migw32-g++? Both executables run without issue.
Extra info:
Am on Windows 7.
g++ version: 4.8.2
mingw32-g++ version: 4.8.1
gdb version: 7.6.50.[lots of numbers]-cvs
If there's any other info I can provide to help find the issue, just let me know.

how to add debug flags on compilation script execution:

I have a simple ./compile.make script that produces a bunch of object .o files. The contents are like this (first 5 lines printed):
compile.make:
gfortran -c -O3 active.f
gfortran -c -O3 alchemy.f
gfortran -c -O3 analysis.f
I run the script by doing ./compile.make. I'd like to compile everything with the -g flag so I can debug using (gdb) but I was wondering if there is a better way to add the "-g" flag without having to manually edit every line of my compile.make file.
*EDIT: I know that find/replace option is available and not much of a hassle at all. I was just curious as to whether the flags can be added upon execution of the script.
http://www.brunolinux.com/02-The_Terminal/Find_and%20Replace_with_Sed.html