1

Does any common debugger allow to turn off this automatic initialization of uninitialized vars and members, so that it would be possible to actually see those with debugger?

I'm using gdb right now, the language in my case is C with GCC if it matters.

Edit: this is about local stack variables.

Edit 2: i wrote this question after i found a local structure with garbage in its members which caused crashes. Those crashes were not happening under debugger, which means, garbage or not (maybe special values for uninitialized data), the debugger treats those in a special way and prevents crash. I'd like to find out if this is a feature that can be disabled in any common debugger, gdb most preferably.

rebus_x
  • 11
  • 2
  • 4
    What kind of variables are you talking about? Local variables? Static variables? Global variables? Dynamically allocated memory? [Edit] and make that clear, and possibly add some example code. – Jabberwocky Feb 14 '23 at 13:17
  • 1
    It is not the debugger that initializes the variables. It is the compiler that emits code so that the variables get initialized. Depending on the compiler settings, the compiler may initialize the values of local uninitialized variables to special values, so that if these values are read, the program will likely fail in an obvious manner. – Andreas Wenzel Feb 14 '23 at 13:18
  • 2
    Besides, the value of an uninitialized variable is totally and completely meaningless. It's hard to imagine why anyone would care what it actually is. – Mark Benningfield Feb 14 '23 at 13:20
  • @MarkBenningfield it depends what kind of variables we're talking about. In C global and static variables are automatically initialized to 0. – Jabberwocky Feb 14 '23 at 13:21
  • @Jabberwocky, Yes, which means that they are *not* uninitialized variables. – Mark Benningfield Feb 14 '23 at 13:22
  • If you want the best possible debug simulation of what it means for a variable to be uninitialized, I believe you want valgrind. – Steve Summit Feb 14 '23 at 13:24
  • @MarkBenningfield: If it were true that “the value of an uninitialized variable is totally and completely meaningless,” then malicious hackers would never be able to exploit them. However, sometimes exploits of uninitialized variables do reveal meaningful information, often in conjunction with other errors, because they may leak other information in the program. The value of an uninitialized variable is not reliable for good programming purposes, and it is not specified by the C standard, but that does not make it meaningless. – Eric Postpischil Feb 14 '23 at 13:24
  • What exactly makes you think that "automatic initialization of uninitialized vars" is responsible for you not being able to see the values of variables in your debugger? How are you compiling the program with gcc? Are you using the [`-Og`](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) command-line option? What other command-line options are you using? – Andreas Wenzel Feb 14 '23 at 19:44
  • @AndreasWenzel context: i wrote this question after i found a local structure with garbage in its members which caused crashes. Those crashes were not happening under debugger. No, i'm not using -Og, all switches are handled by qmake in Qt Creator. For debug version they are -g -Wall -W -Wextra and a whole bunch of QT and mingw related; for release version it's -O2 -Wall -W -Wextra – rebus_x Feb 15 '23 at 06:27
  • @AndreasWenzel forget the release version, i should not have mentioned it. I ran only the debug version, with and without debugger – rebus_x Feb 15 '23 at 06:39
  • 1
    @rebus_x so the crashes were not happening with the debugger. That simply means that your structure was not initialized with the same garbage as without debugger. – Jabberwocky Feb 15 '23 at 07:08
  • 1
    Yeah it"s UB. One possibility of UB is 'works with debugger, doesn't work without'. – Martin James Feb 15 '23 at 11:14
  • I suggest that you try running your code with [MemorySanitizer](https://clang.llvm.org/docs/MemorySanitizer.html), which will detect reads of uninitialized data. As far as I know, MemorySanitizer is only available with clang, not gcc, though. – Andreas Wenzel Feb 15 '23 at 17:00
  • Other sanitizers that may be of interest are AddressSanitizer and UndefinedBehaviorSanitizer. In constrast to MemorySanitizer, those two sanitizers are also supported in gcc. You can simply compile with the `-fsanitize=address,undefined` command-line option. Note that this should only be done for debugging purposes, as the additional checks will slow down your program significantly. – Andreas Wenzel Feb 15 '23 at 17:40

0 Answers0