9

System

Fresh install of codeblocks 12.11 + mingw pack.

  • win7 64
  • gcc 4.7.1
  • gdb 7.5

Example code

Compiled with -g and no optimization.

#include <stdio.h>

void foo(int a, int b);

int main() {

    foo(400, 42);

    return 0;
}

void foo(int a, int b) {

    a = a - 10;
    b = a + 1;

    printf("y2 %d\n", b);

}

Problem

I put a breakpoint on "void foo(int a, int b)" and I look value of b as I step through the 3 lines. Either with the codeblocks debugging features or with the gdb command line, the value of b is 42 instead of being 391. The console output is correct, 391.

GDB commands

C:\DebugTest>"C:\Program Files (x86)\CodeBlocks\MinGW\bin\gdb.exe"
GNU gdb (GDB) 7.5
Copyright (C) 2012 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".
For bug reporting instructions, please see:  
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) file bin/Debug/DebugTest.exe
Reading symbols from C:\DebugTest\bin\Debug\DebugTest.exe...done.
(gdb) break foo
Breakpoint 1 at 0x401363: file C:\DebugTest\main.c, line 14.
(gdb) run
Starting program: C:\DebugTest\bin\Debug\DebugTest.exe
[New Thread 3596.0x658]

Breakpoint 1, foo (a=400, b=42) at C:\DebugTest\main.c:14
14          a = a - 10;
(gdb) print b
$1 = 42
(gdb) step
15          b = a + 1;
(gdb) print b
$2 = 42
(gdb) step
17          printf("y2 %d\n", b);
(gdb) print b
$3 = 42
(gdb)

Remarks

  • When the same operations are done without a function, with a and b as local variables inside main, the debug output is correct.
  • When compiled with gcc 4.4.1, the debug output is correct.

Any idea what could be wrong ? =)

5
  • I'm using Code Blocks 10.05 (GNU gdb 6.8), shows b as 391. Commented Feb 6, 2013 at 16:46
  • 1
    Are you printing the value of b in gdb after assignment b = a + 1;? Commented Feb 6, 2013 at 16:49
  • what do you mean with step inside foo? break foo, run then print b? Commented Feb 6, 2013 at 16:53
  • Yeah I print b both before and after b = a + 1 line. @DavideBerra I put a breakpoint on the void foo(int a, int b) { using codeblocks, I don't use gdb command except print. Updated the post. Commented Feb 6, 2013 at 19:40
  • A complete set of gdb commands showing the problem could confirm or disprove some of those suspicions that it could just be a misuse/misunderstanding... Commented Feb 6, 2013 at 19:47

3 Answers 3

9

I searched on gcc bugzilla and found this bug report :

Althoug the report is about gcc 4.8 and I'm using 4.7, I tried the proposed workaround and it works !

Compiling with -fvar-tracking allows GDB to print the correct value for b after assignment.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using gcc 4.8.2 and had this same issue (and -fvar-tracking fixed it). No problems on another machine with gcc 5.3.1.
1

gcc does not generate debugging info correctly for values that are in registers -- either values that have been put in registers or values that start there due to the calling conventions. This is a long-standing problem with gcc since at least 4.0, and makes it tricky to debug things.

1 Comment

I've just tried compiling the code with gcc 4.4.1. With the same gdb (7.5) and the exact same commands, b has the correct value after assignement (391). Do you have any links for more info about the problem you're describing ?
0

Sometimes the optimizer is smarter than the debugger. Try debugging unoptimized code, or step through disassembly and watch the HW registers directly rather than stepping through C source lines and watching the debugger's idea of what the variables are.

2 Comments

The code isn't optimized and the register shows the correct value. So I could use only the register view but maybe you will agree that it's not the more convenient way ^^'
Agreed. Unfortunately, sometimes that's the only game in town.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.