1

I was playing around with a simple program (see below) and I came across behavior that did not seem correct. I ran the program with gdb as shown below, and it does not seem to read the passed arguments to func correctly, for instance it says b=21845, and even says . Is there an explanation for this?

I am running this on a MacBook Pro, 2.3 GHz 8-Core Intel Core i9, MACOS 13.1 (22C65). I compiled the program with: g++ -std=c++2a test.cpp -g -lpthread -lstdc++fs -o test I got the DBG debugger from https://marketplace.visualstudio.com/items?itemName=coolchyni.beyond-debug. It is GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1, configured as x86_64-linux-gnu. I am using visual studio code.

Code: test.cpp

#include <iostream>

void func(int b, int &c)
{
  std::cout << b <<" and " << c<< std::endl;
  
}

int main()
{
  
  int num2 = 4;
  int &num3 = num2;
  func(num2, num3);
  
  return 0;
}

GDB Run

Starting program: /home/es927/exper/test 

Breakpoint 1, main () at test.cpp:10

10        int num2 = 4;

(gdb) s

11        int &num3 = num2;

(gdb) s

12        func(num2, num3);

(gdb) s

func (b=21845, c=<error reading variable>) at test.cpp:4

4       {

(gdb) s

5         std::cout << b << " and " << c << std::endl;

(gdb) s

4 and 4

6       }

(gdb) s

main () at test.cpp:14

14        return 0;

(gdb) s

15      }

(gdb) c

Continuing.

[Inferior 1 (process 1235898) exited normally]

I was expecting the values of the passed arguments when stepping after code line 12.

DjuroPucar
  • 11
  • 2
  • You can see it correctly outputs "4 and 4" like it should. I don't use gdb, but debuggers usually have what seems like incorrect values when a function is first called, then loads them correctly right afterward. – ChrisMM Mar 04 '23 at 21:30
  • The market place probably doesn't have the most recent version of gdb. You compiled with -std=c++2a and that's probably confusing your version of gdb. I haven't worked in this space in a very long time, so not entirely certain that is the issue. I would advice updating whatever version of GDB they installed on your box to the latest version. – jwdonahue Mar 04 '23 at 23:52
  • Instead of stepping after line 12, try `info locals`. That should give you a little more information. – jwdonahue Mar 05 '23 at 00:04
  • Now I think about it, if that doesn't yield what you want, try another step and follow it with the same `info local` command. – jwdonahue Mar 05 '23 at 00:06

1 Answers1

4

This is fully expected behavior. This kind of parameter behaviour is described in the gdb manual - 10.3 Program Variables (emphasis mine):

Warning: Occasionally, a local variable may appear to have the wrong value at certain points in a function—just after entry to a new scope, and just before exit.

Thus, the easiest solution is to just step once more into the actual function block scope and use info local. The manual also goes on to talk about methods for trying to mitigate this wrong value behavior. Unfortunately, I was not able to reproduce the weird values you were having (in WSL with gcc-11.3.0 and gdb-13.1) so I was unable to verify these methods.

All things considered, I would not worry about this kind of debugger behavior too much. For example, these uninitialized parameter values are every day for the Visual Studio's debugger, even to the point of making it hard to debug single line functions.

Edit: Based on the comments and more findings, here are some other things you can try:

Rane
  • 321
  • 2
  • 6
  • 3
    The warning seems generic, but the text below it mentions stepping by instructions specifically which OP doesn't do. I don't think I ever noticed wrong values for parameters in gdb when stepping normally (not by instruction), but maybe I was never attentive to it. – user17732522 Mar 04 '23 at 23:37
  • @user17732522 Very true. I will remove the quote referring to instruction stepping as it is irrelevant to the question. Sadly, the other reasons that would cause this kind of behavior are also rather generic (optimizations, debug formats etc.). – Rane Mar 04 '23 at 23:49
  • 1
    @Rane, look at where the OP got their gdb and how they compiled the code. I suspect this is an issue with the debugger version not fully supporting that particular compiler output. Of course, it could also be an issue with how the plugin interacts with gdb. – jwdonahue Mar 04 '23 at 23:56
  • 1
    @jwdonahue Good point with the gdb version. OP's [GDB Debugger - Beyond](https://marketplace.visualstudio.com/items?itemName=coolchyni.beyond-debug) extension uses the system gdb, so if there is an issue with the version, it is with the system gdb. – Rane Mar 05 '23 at 00:05
  • 1
    @Rane, I think your answer is probably close (+1 for that). You should add that stepping one more time and dumping the local variables usually does the trick. I would be shocked if there were no way for the OP to view what's on that function stack before finally exiting that scope. – jwdonahue Mar 05 '23 at 00:16