g++4.8 hides variables from gdb - c++

After recently upgrading to g++ 4.8.1 I've found that debugging is completely impossible in gdb. g++ seems to hide all variables from gdb, regardless of optimization options. In the following session, runner.cpp is as follows:
#include <vector>
using namespace std;
int main(void) {
vector<int> arr;
int a = 3;
int b = 2;
b = a + 3;
arr.push_back(1);
arr.push_back(2);
arr.push_back(3);
arr.push_back(4);
return 0;
}
This is the result:
Script started on Tue 14 Jul 2015 01:11:14 PM PDT
me#ministation:~/Development/clib$ g++ -g -O0 runner.cpp
me#ministation:~/Development/clib$ gdb -q ./a.out
Reading symbols from /home/me/Development/clib/a.out...done.
(gdb) break 11
Breakpoint 1 at 0x40095c: file runner.cpp, line 11.
(gdb) run
Starting program: /home/me/Development/clib/a.out
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, main () at runner.cpp:11
11 arr.push_back(1);
(gdb) print a
$1 = {i = {0, 1045149306}, d = 1.2904777690891933e-08} ## I have no idea what this means
(gdb) print b
$2 = {i = {0, 1068498944}, d = 0.0625}
(gdb) print arr
No symbol "arr" in current context.
(gdb) info locals
No locals.
(gdb) next
12 arr.push_back(2);
(gdb)
13 arr.push_back(3);
(gdb) print arr
No symbol "arr" in current context.
(gdb) next
14 arr.push_back(4);
(gdb)
16 return 0;
(gdb) print arr
No symbol "arr" in current context.
(gdb) q
A debugging session is active.
Inferior 1 [process 6392] will be killed.
Quit anyway? (y or n) y
me#ministation:~/Development/clib$
Script done on Tue 14 Jul 2015 01:12:05 PM PDT
I've seen somewhat similar posts where the -O0 flag was recommended, but it doesn't seem to work here. The exact same session after compiling with g++4.6 produces the expected results. Any ideas on what's causing this with g++4.8?

The particular problem here is that the meaning of the DW_AT_high_pc tag in the debug info has been extended to also mean an offset.
Originally, a function ranging from 0x804dd8e to 0x804ddae was encoded as
DW_AT_low_pc : 0x804dd8e
DW_AT_high_pc : 0x804ddae
Now it also can be encoded as
DW_AT_low_pc : 0x804dd8e
DW_AT_high_pc : 0x20
which saves a bit of space in the debug info.
Older versions of GDB only recognize the first version, and interpret the second in a way that there can't be any variables in this range.
Possible solutions are to compile with -gdwarf-2 or to upgrade GDB.

gdb and gcc need to be compatible versions when it comes to debug format (and they are generally quite well compatible over many versions, but SOMETIMES compatibility is broken from one version of a compiler to another).
I have had exactly the same problem. The solution is either to throw some flag at gcc to compile with "older style debug format", or to upgrade the version of gdb to one that "works" with the debug format given by the newer version of gcc.
It would be NICE if gdb actually said something like "Your debug format is X, you need debug format Y or Z for gdb to work correctly", but I'm not 100% sure that can be reliably determined (for example when a bug is found in the generation that is fixed, and need the corresponding fix in the debugger, because the same bug appears in both the "produce" and "consume" parts of the debug-symbol handler - so an older, buggy version can't read the newer unbuggy format and vice versa)

Related

GDB is going backwards [duplicate]

I am simply using gdb to step through a code line by line to learn how it is working and what it is doing. It worked fine the first time I did this, but now the next command is not working right. Sometimes it goes forward, sometimes it goes backwards. It doesn't make sense. Every time I do this it seems to be the same pattern. Below is an example. Anyone know what is wrong?
Note: This is an old Mac with old program versions (That I do not have the power to update)
Please be explicit. I am fairly new to linux and programming in general.
Thanks!
Reading symbols for shared libraries ... done
(gdb) b main
Breakpoint 1 at 0x2730: file ../../../../gcc-3.4.6/libf2c/libF77/pow_zi.c, line 14.
(gdb) r
Starting program: /Users/kevin/project/ConstU/main
nReading symbols for shared libraries +. done
Error in re-setting breakpoint 1:
Function "main" not defined.
Re-enabling shared library breakpoints: 1
Breakpoint 1, 0x00002730 in main () at main.c:34
34 {
(gdb) n
main () at main.c:42
42 for (i=0;i<DpDIM;i++) {
(gdb) n
34 {
(gdb) n
35 runstart=clock();
(gdb) n
39 Init=ReadInit(&CaseDim);/*reads in initial valies from initfile*/
(gdb) n
35 runstart=clock();
(gdb) n
39 Init=ReadInit(&CaseDim);/*reads in initial valies from initfile*/
(gdb)
Anyone know what is wrong?
The symptoms you've described are very common when debugging programs built with optimization: the compiler re-arranges code in such a way that instructions from different lines become intermixed.
Make sure you compile without any -O flags, or add -O0 at the end of your compilation lines.
here is part of the make file: CC = gcc CFLAGS = -g -O2
That would do it: remove -O2 and your debugging would go much easier.

How should I go about debugging a SIGFPE in a large, unfamiliar software project?

I'm trying to get to the bottom of a bug in KDE 5.6. The locker screen breaks no matter how I lock it. Here's the relevant code: https://github.com/KDE/kscreenlocker/blob/master/abstractlocker.cpp#L51
When I run /usr/lib/kscreenlocker_greet --testing, I get an output of:
KCrash: Application 'kscreenlocker_greet' crashing...
Floating point exception (core dumped)
I'm trying to run it with gdb to try and pin the exact location of the bug, but I'm not sure where to set the breakpoints in order to isolate the bug. Should I be looking for calls to KCrash? Or perhaps a raise() call? Can I get gdb to print off the relevant line of code that causes SIGFPE?
Thanks for any advice you can offer.
but I'm not sure where to set the breakpoints in order to isolate the bug
You shouldn't need to set any breakpoints at all: when a process running under GDB encounters a fatal signal (such as SIGFPE), the OS notices that the process is being traced by the debugger, and notifies the debugger (instead of terminating the process). That in turn causes GDB to stop, and prompt you for additional commands. It is at that time that you can look around and understand what caused the crash.
Example:
cat -n t.c
1 #include <fenv.h>
2
3 int foo(double d) {
4 return 1/d;
5 }
6
7 int main()
8 {
9 feenableexcept(FE_DIVBYZERO);
10 return foo(0);
11 }
gcc -g t.c -lm
./a.out
Floating point exception
gdb -q ./a.out
(gdb) run
Starting program: /tmp/a.out
Program received signal SIGFPE, Arithmetic exception.
0x000000000040060e in foo (d=0) at t.c:4
4 return 1/d;
(gdb) bt
#0 0x000000000040060e in foo (d=0) at t.c:4
#1 0x0000000000400635 in main () at t.c:10
(gdb) q
Here, as you can see, GDB stops when SIGFPE is delivered, and allows you to look around and understand the crash.
In your case, you would want to first install debuginfo symbols for KDE, and then run
gdb --args /usr/lib/kscreenlocker_greet --testing
(gdb) run

lldb: Couldn't materialize: couldn't get the value of variable

I have compiled a cpp file with this command line: g++ -g test.cpp
It throws an exception at line 28. I want to investigate the cause by inspecting the variables in lldb. I set a break point at line 28 and run the a.out in lldb.
(lldb) n
Process 84233 stopped
* thread #1: tid = 0xa44b86, 0x00000001000017fb a.out`say(s=<unavailable>) + 987 at so.cpp:28, queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x00000001000017fb a.out`say(s=<unavailable>) + 987 at so.cpp:28
25 }
26 else{
27 s.insert(0, to_string(sz));
-> 28 s.erase(2, sz-1);
29 }
30 return s;
31 }
(lldb) po s
error: Couldn't materialize: couldn't get the value of variable s: variable not available
Errored out in Execute, couldn't PrepareToExecuteJITExpression
Why the error message? How can I inspect the variable s?
lldb version: lldb-320.4.115.3
g++ version: Configured with: --prefix=/Applications/Xcode6-Beta5.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.45.3) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
That error means the debug information does mention the variable, but says it has no storage location at the current PC.
That can be because the variable got optimized out (unlikely given you are just calling a function on the variable) or because the compiler flubbed the debug info for the variable and lost track of where it went.
Make sure you are compiling the code you are trying to debug at -O0 as there aren't many compilers that emit good debug information at higher optimization levels. If you are compiling at -O0, this is a compiler bug. You should probably report it to the gcc folks. You could see if you have better luck with clang. Otherwise, you have to read the assembly of the function to figure out where the variable actually lives, then tell the debugger to print the appropriately cast address.
I had this problem when I enabled the "Address Sanitizer" from my app scheme. Disable it fixed the issue.
I see this when I run a RELEASE (vs a DEBUG) build (Product->Scheme...->Edit Scheme...->Info, then set Build Configuration to "Debug".
I had this issue when compiling with the flag -Og. For some reason I was thinking that that meant 'optimize for debugging'. I don't think thats the case in reality. Removing that flag fixed the issue for me.

next command in gdb is not working correctly

I am simply using gdb to step through a code line by line to learn how it is working and what it is doing. It worked fine the first time I did this, but now the next command is not working right. Sometimes it goes forward, sometimes it goes backwards. It doesn't make sense. Every time I do this it seems to be the same pattern. Below is an example. Anyone know what is wrong?
Note: This is an old Mac with old program versions (That I do not have the power to update)
Please be explicit. I am fairly new to linux and programming in general.
Thanks!
Reading symbols for shared libraries ... done
(gdb) b main
Breakpoint 1 at 0x2730: file ../../../../gcc-3.4.6/libf2c/libF77/pow_zi.c, line 14.
(gdb) r
Starting program: /Users/kevin/project/ConstU/main
nReading symbols for shared libraries +. done
Error in re-setting breakpoint 1:
Function "main" not defined.
Re-enabling shared library breakpoints: 1
Breakpoint 1, 0x00002730 in main () at main.c:34
34 {
(gdb) n
main () at main.c:42
42 for (i=0;i<DpDIM;i++) {
(gdb) n
34 {
(gdb) n
35 runstart=clock();
(gdb) n
39 Init=ReadInit(&CaseDim);/*reads in initial valies from initfile*/
(gdb) n
35 runstart=clock();
(gdb) n
39 Init=ReadInit(&CaseDim);/*reads in initial valies from initfile*/
(gdb)
Anyone know what is wrong?
The symptoms you've described are very common when debugging programs built with optimization: the compiler re-arranges code in such a way that instructions from different lines become intermixed.
Make sure you compile without any -O flags, or add -O0 at the end of your compilation lines.
here is part of the make file: CC = gcc CFLAGS = -g -O2
That would do it: remove -O2 and your debugging would go much easier.

Snow Leopard crashes after gdb attempts to access an address using watchpoints

I am debugging a binary (assembly only) using GDB 7.1 compiled via MacPorts for Snow Leopard. I am interested in a specific address that I found using find gdb command. So that, it is a valid address indeed:
(gdb) printf "%s\n", 0x00196f34
bruno
(gdb)
The problem is whenever I set a watchpoint for it (watch *0x00196f34) AND it gets accessed, the system crashes and reboots right away.
I noticed hardware watchpoint is enabled then, expecting a less severe result, I switched to software watchpoint using set can-use-hw-watchpoints 0 but this didn't fix the problem.
I tried to use gdb 6.3 version that comes with Xcode and watch works fine. There is a difference I noticed when setting such watch for both 6.3 and 7.1:
for gdb 6.3
(gdb) watch *0x00196f34
Watchpoint 1: *0x00196f34
(gdb) info breakpoints
Num Type Disp Enb Address What
1 watchpoint keep y *0x00196f34
for gdb 7.1
(gdb) watch *0x00196f34
Hardware watchpoint 1: *1666868
(gdb) info breakpoints
Num Type Disp Enb Address What
1 hw watchpoint keep y *1666868
Why does the older gdb, which works, print in hex while the new one translates the address into decimal number? I still want to work with gdb 7, so any clue what is it going on here?
Bruno Velasco