gdb cannot step into printf - c++

here is my sample program:
#include<stdio.h>
int main()
{
printf("hello good morning \n");
return 0;
}
gcc -Wall -g temp.c
/opt/langtools/bin/gdb a.out
HP gdb 3.3 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 3.3 (based on GDB) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.
..
(gdb) b 6
Breakpoint 1 at 0x2b14: file temp.c, line 6.
(gdb) run
Starting program: /oo_dgfqausr/test/dfqwrk4/temp/a.out
Breakpoint 1, main () at temp.c:6
6 printf("hello good morning \n");
(gdb) step
hello good morning
7 return 0;
(gdb)
as soon as i try to step into the printf function.its exiting and returning to main.
does this mean that the shred library in which the printf function is defined is not provided with the debug symbols?Or am i doing something wrong?

This means there's no available source/debug symbols for printf. You can use stepi to step into printf anyway, you'll only have disassembly available (use the disas command).

That's correct, you likely do not have debugging symbols available. Make sure libc-devel or similar is installed. Also, make sure to compile with -O0 to prevent optimization; optimizations make debugging more difficult to follow.

Also, -g3 is required for maximum symbols. With -g3, even symbolic constants will be available. -ggdb may be helpful too. Jan from GDB tells us there are no mainline GDB extensions, but Apple may have offered some and omitted backstrem patches.

Related

Why do I get an <incomplete type> message for a ifstream object in gdb?

If I run the following code in the gdb debugger and type in print inFile, I get the message "incomplete type".
#include <fstream>
#include <iostream>
int main() {
std::ifstream inFile;
inFile.open("testFile.txt");
std::cout<<"In the debugger if I print the inFile variable at this point I get the incomplete type message";
inFile.close();
}
This makes it rather difficult to debug. Anyone knows what the reason for this might be?
This is how I compile the file:
g++ -g -std=c++11 main.cpp -o main
Here is the complete GDB output:
>> gdb main
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...done.
(gdb) break main
Breakpoint 1 at 0x400ac2: file main.cpp, line 3.
(gdb) run
Starting program: ~/main
Breakpoint 1, main () at main.cpp:3
3 int main() {
(gdb) n
4 std::ifstream inFile;
(gdb)
5 inFile.open("testFile.txt");
(gdb) print inFile
$1 = <incomplete type>
(gdb)
I used the approach ks1322 described in his post and got the following message for
dpkg --list | grep libstdc++
ii libstdc++-5-dev:amd64 5.4.0-6ubuntu1~16.04.5 amd64 GNU Standard C++ Library v3 (development files)
ii libstdc++6:amd64 5.4.0-6ubuntu1~16.04.5 amd64 GNU Standard C++ Library v3
The
sudo apt-get install libstdc++
displays
libstdc++6 libstdc++6-5-dbg-m68k-cross
libstdc++6-4.7-dbg libstdc++6-5-dbg-mips64-cross
libstdc++6-4.7-dbg-armel-cross libstdc++6-5-dbg-mips64el-cross
libstdc++6-4.7-dbg-armhf-cross libstdc++6-5-dbg-mips-cross
libstdc++6-4.7-dev libstdc++6-5-dbg-mipsel-cross
libstdc++6-4.7-dev-armel-cross libstdc++6-5-dbg-powerpc-cross
libstdc++6-4.7-dev-armhf-cross libstdc++6-5-dbg-powerpcspe-cross
libstdc++6-4.7-doc libstdc++6-5-dbg-ppc64-cross
libstdc++6-4.7-pic libstdc++6-5-dbg-ppc64el-cross
libstdc++6-4.7-pic-armel-cross libstdc++6-5-dbg-s390x-cross
libstdc++6-4.7-pic-armhf-cross libstdc++6-5-dbg-sh4-cross
libstdc++6-4.8-dbg libstdc++6-5-dbg-sparc64-cross
libstdc++6-4.8-dbg-arm64-cross libstdc++6-alpha-cross
libstdc++6-4.8-dbg-armhf-cross libstdc++6-arm64-cross
libstdc++6-4.8-dbg-powerpc-cross libstdc++6-armel-cross
libstdc++6-4.8-dbg-ppc64el-cross libstdc++6-armhf-cross
libstdc++6-4.9-dbg libstdc++6-hppa-cross
libstdc++6-4.9-dbg-arm64-cross libstdc++6-m68k-cross
libstdc++6-4.9-dbg-armel-cross libstdc++6-mips64-cross
libstdc++6-4.9-dbg-armhf-cross libstdc++6-mips64el-cross
libstdc++6-4.9-dbg-powerpc-cross libstdc++6-mips-cross
libstdc++6-4.9-dbg-ppc64el-cross libstdc++6-mipsel-cross
libstdc++6-4.9-dbg-s390x-cross libstdc++6-powerpc-cross
libstdc++6-5-dbg libstdc++6-powerpcspe-cross
libstdc++6-5-dbg-alpha-cross libstdc++6-ppc64-cross
libstdc++6-5-dbg-arm64-cross libstdc++6-ppc64el-cross
libstdc++6-5-dbg-armel-cross libstdc++6-s390x-cross
libstdc++6-5-dbg-armhf-cross libstdc++6-sh4-cross
libstdc++6-5-dbg-hppa-cross libstdc++6-sparc64-cross
You need to install debug symbols for libstdc++ (this is where std::ifstream resides). See here the similar issue for std::stringstream:
That's caused by gdb not having debug information for your standard
library. It's not enough to compile your program with -g, because it's
the code in your standard library that defines those types. For
example, if you're using gcc with a Debian based system, there's a
-dbg version of libstdc++ that you can install. Example:
So you need to install this package libstdc++<your version>-dbg with apt-get:
sudo apt-get install libstdc++<your version>-dbg
Replace <your version> with your version of libstdc++.
A useful solution can be found here:
https://stackoverflow.com/a/58754863/11867786

Xcode and C++ [Beginner] - Why is the Hello World program printing all this extra stuff?

Whenever I build+run my program, it displays all this extra stuff:
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02
UTC 2011) Copyright 2004 Free Software Foundation, Inc. GDB is free
software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain
conditions. Type "show copying" to see the conditions. There is
absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
[Switching to process 3022 thread 0x0]
Hello, World!
Program endedwith exit code: 0
Here is the Hello World, default code:
#include <iostream>
int main (int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
Why is it printing all the extra stuff, and how can I stop it? I'm using Xcode and C++. Thanks.
It's printing all of that extra stuff because it's running your executable in the debugger. You can change the popup from All Output to just "Target Output" like shown in the images below:
Build the app and then run it from the command line (in Terminal) and you won't see any of that GDB stuff.
If you want to do debugging though, you'll see (and eventually come to ignore) those extra lines.
Checking really quickly, I see that Apple's newer debugger (LLDB) prints out significantly less "extra" stuff into XCode's debugging output/console.
gdb is the GNU debugger. Running in the IDE that way seems to default to running under the debugger. In short it has nothing to do with your code and everything to do with the environment you are using.

D2 and gdb issue

Short question: Is there any support of D2 in gdb (I have gdb 7.2) ?
Long story: I compiled next little application ...
// file main.d
int glVar = 0xAAAAAAAA;
void main()
{
glVar = 0xBBBBBBBB;
}
... using command "dmd -gc -debug main.d";
then I load it to gdb and trying to debug it:
vnm#vnm:~/proj/d_gdb_test$ gdb main
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/vnm/proj/d_gbb_test/main...done.
(gdb) b main
Breakpoint 1 at 0x804b667
(gdb) r
Starting program: /home/vnm/proj/d_gbb_test/main
[Thread debugging using libthread_db enabled]
Breakpoint 1, 0x0804b667 in main ()
(gdb) info line
No line number information available.
(gdb) info variables glVar
All variables matching regular expression "glVar":
File main.d:
int _D4main5glVari;
Why gdb can't show line information and why it shows symbols in mangled form ? Is this software issues or I'm doing something wrong ?
gdb has support for D starting with version 7.2. So, you can debug D programs with gdb 7.2.
Now, that doesn't mean that support is perfect - far from it in fact. For instance, I'm not sure that you can get it to print strings properly. And it's not at all surprising if it doesn't demangle D symbols. So, it works, but it's far from perfect.

Debugging MinGW program with gdb on Windows, not terminating at assert failure

How do I set up gdb on window so that it does not allow a program with assertion failure to terminate? I intend to check the stack trace and variables in the program.
For example, running this test.cpp program compiled with MinGW 'g++ -g test.cpp -o test' in gdb:
#include <cassert>
int main(int argc, char ** argv) { assert(1==2); return 0; }
Gives:
$ gdb test.exe
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-mingw32"...
(gdb) r
Starting program: f:\code/test.exe
[New thread 4616.0x1200]
Error: dll starting at 0x77030000 not found.
Error: dll starting at 0x75f80000 not found.
Error: dll starting at 0x77030000 not found.
Error: dll starting at 0x76f30000 not found.
Assertion failed: 1==2, file test.cpp, line 2
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Program exited with code 03.
(gdb)
I would like to be able to stop the program from terminating immediately, like how Visual Studio's debugger and gdb on Linux does it. I have done a search and found some stuff on trapping signals but I can't seem to find a good post on how to set up gdb to do this.
Found out that the breakpoint can be put in the .gdbinit file with the lines:
set breakpoint pending on
b exit
This removes the need to enter yes for windows.
Just set a breakpoint on exit:
(gdb) b exit
Using recent (March 2017) msys2 with gcc 6.3 and gdb 7.12.1 you should use:
break _exit
i.e. use _exit and not exit. I expect this also to work in other cases as I expect that exit will call _exit to actually exit.

Strange error that I've never encounter in c++ before, anyone know what it means?

I won't post any code, because there is too much that could be relevant. But When I run my program it prints
Internal Bad Op Name!
: Success
Anybody even know what that means? I'm using g++ to compile my code and nowhere in my code do I cout anything even remotely close to something like that. I don't know where it's coming from. Also, any suggestions as to figure out where in the code it's coming from, maybe using gdb somehow to do that?
Thanks!
It's not a message I've seen, and Googling for it doesn't show anything obviously related.
You can identify where it comes from by stepping through the program with gdb until the message appears. Alternatively, one can sprinkle some timing delays, "I am here" statements, or input prompts to discover suspect portions of the logic.
< < < (edit) > > >
To use gdb, first be sure to compile and link with debug symbols. With either gcc or g++, just add -g to the command line. It's also often helpful to eliminate any compiler optimizations since those can sometimes make stepping through the program non-intuitive.
[wally#lf ~]$ gdb program
GNU gdb Fedora (6.8-32.fc10)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) break main
Breakpoint 1 at 0x8048c3c: file rtpsim.cpp, line 30.
(gdb) run
Starting program: ~/program
Breakpoint 1, main () at rtpsim.cpp:30
30 rtp_io (&obj, INIT_CYCLE);
(gdb) next
31 printf ("- - - - - init complete - - - - -\n");
(gdb) <---- pressed "enter" to repeat last command
- - - - - init complete - - - - -
33 for (int j = 0; j < 10; ++j)
(gdb)
35 sleep (1);
(gdb)
36 rtp_io (&obj, SCAN_CYCLE);
(gdb)
37 printf ("- - - - - scan %d complete - - - - -\n", j+1);
...
What libraries and what platform are you using? No C++ compiler I know of (certainly not GCC) introduces output to your program except before aborting.
Edit: maybe easier than backtracking or finding references, use grep -a to find that string in all your sources and library binaries.
To debug the program with GDB, first make sure that it is compiled with the -g flag. Then type gdb your-program-name into the command line. GDB is a command based debugger. To get started, type help. Or, there are graphical debugging tools, like xxgdb (though for this, it is a good thing to understand basic gdb commands), ddd, kdbg (KDE based), Eclipse (it is not very straightforward to configure if you want to use your own makefile) etc.