Why gdb switched to home directory? - gdb

Suppose the current directory is /home/xxx/test, there is a text file named "test.txt" which contains a single word "hello", and a file named "test.cpp" is as following,
#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;
int main ()
{
char cwd[1024];
getcwd(cwd, 1024);
cout << cwd << endl;
string s;
ifstream i("test.txt");
if (!i.good())
cout << "Can't open test.txt" << endl;
i >> s;
i.close();
cout << s << endl;
return 0;
}
test> g++ test.cpp
test> ./a.out
/home/xxx/test
hello
test> gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/xxx/test/a.out...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/xxx/test/a.out
/home/xxx
Can't open test.txt
Program exited normally.
(gdb) pwd
Working directory /home/xxx/test.
(gdb) shell pwd
/home/xxx
My question is why gdb switched to home directory which yields 'test.txt' can't be found?
Why 'pwd' and 'shell pwd' give different result?
Thanks.

There is a 'cd ~' in my .cshrc, this is the root cause for the difference between 'shell pwd' and 'pwd'.
After remove it, all the problems are resolved.
Thanks.

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

"No such file or directory" messages when using GDB with C++ programs

I'm pretty new to using GDB, so sorry if this is a noob question.
When using GDB on any C++ program, I get a large number of "No such file or directory" messages. This does not happen with C programs.
Here's what I'm doing:
$ g++ -g hello-world.cpp -o hello-world.o
$ gdb hello-world.o
GNU gdb (GDB) 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-apple-darwin15.5.0".
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 hello-world.o...Reading symbols from /Users/noah/c++/hello-world.o.dSYM/Contents/Resources/DWARF/hello-world.o...done.
done.
(gdb) r
Starting program: /Users/noah/c++/hello-world.o
warning: `/private/tmp/gcc6-20160515-25034-1nu0tpk/gcc-6.1.0/build/x86_64-apple-darwin15.2.0/libstdc++-v3/src/.libs/compatibility-atomic-c++0x.o': can't open to read symbols: No such file or directory.
warning: `/private/tmp/gcc6-20160515-25034-1nu0tpk/gcc-6.1.0/build/x86_64-apple-darwin15.2.0/libstdc++-v3/src/.libs/compatibility-c++0x.o': can't open to read symbols: No such file or directory.
warning: `/private/tmp/gcc6-20160515-25034-1nu0tpk/gcc-6.1.0/build/x86_64-apple-darwin15.2.0/libstdc++-v3/src/.libs/compatibility-chrono.o': can't open to read symbols: No such file or directory.
(many more messages omitted)
warning: `/private/tmp/gcc6-20160515-25034-1nu0tpk/gcc-6.1.0/build/x86_64-apple-darwin15.2.0/libgcc/unordtf2_s.o': can't open to read symbols: No such file or directory.
warning: `/private/tmp/gcc6-20160515-25034-1nu0tpk/gcc-6.1.0/build/x86_64-apple-darwin15.2.0/libgcc/unwind-c_s.o': can't open to read symbols: No such file or directory.
warning: `/private/tmp/gcc6-20160515-25034-1nu0tpk/gcc-6.1.0/build/x86_64-apple-darwin15.2.0/libgcc/unwind-dw2_s.o': can't open to read symbols: No such file or directory.
Hello World
[Inferior 1 (process 2319) exited normally]
(gdb)
I'm using OS X El Capitan, with G++ 6.1.0 and GDB 7.11.1, both installed through Homebrew. GDB is codesigned using these instructions.

XCode Compiling

When I compile my XCode Code I get this at the bottom...Im not sure whats wrong?...I am coding in C++
GNU gdb 6.3.50-20050815 (Apple version
gdb-1518) (Sat Feb 12 02:52:12 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
sharedlibrary apply-load-rules all
[Switching to process 2627 thread 0x0]
(gdb)
here is my code
#include <iostream>
#include <cmath>
int main ()
{
using namespace std;
double num1;
num1 = pow (2.0,4.0);
cout << num1 << endl;
system("pause");
return 0;
}
At a guess:
system("pause");
causes the problem, as OS X may not have a "pause" command.
That is some logging generated by the debugger, nothing to worry about.
The debugger is a process you can use to monitor your application at run time, if you haven't done any code optimization during compilation you can even change your code while your app is running.
If you don't want the debugger you can turn it off by changing the scheme in xcode.
Also the "pause" command does not ship with os X by default, is it something you made?

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.

how to setup breakpoint in gdb with shared library function

I have problem with gdb, it wasn't stop in one function. Can you explain why and how to stop program after calling strcpy?
grzes#trampek:~/poligon$ gdb ./char_array2
GNU gdb (GDB) 7.2
Copyright (C) 2010 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-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/grzes/poligon/char_array2...done.
(gdb) list
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char str_a[20];
6 printf("ssss");
7 strcpy(str_a, "Hello, world!\n");
8 printf(str_a);
9 }
10
(gdb) break main
Breakpoint 1 at 0x8048465: file char_array2.c, line 4.
(gdb) run
Starting program: /home/grzes/poligon/char_array2
Breakpoint 1, main () at char_array2.c:4
4 int main() {
(gdb) break strcpy
Breakpoint 2 at 0x1a1205
(gdb) cont
Continuing.
ssssHello, world!
Program exited with code 016.
(gdb) q
grzes#trampek:~/poligon$
You may be using gcc's __builtin_strcpy. Compile with gcc -fno-builtin and see if that helps.
For more info see: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/C-Dialect-Options.html#C-Dialect-Options