• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: GDB & static const variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GDB & static const variables


  • Subject: Re: GDB & static const variables
  • From: Jim Ingham <email@hidden>
  • Date: Thu, 16 Jul 2009 12:04:34 -0700

As other folks have said, it is most likely the case that gcc optimized this away. But you can tell for sure:

 > cat test.m
#include <Foundation/Foundation.h>
Boolean TestFunction( void )
{
  static const int doubleByteDegreeMark = 42;
  static int anotherVar = 12;

  return ( doubleByteDegreeMark == 42 && anotherVar == 12 );
}
 > gcc -g -c -O0 test.m
 > dwarfdump -n TestFunction -c test.o
----------------------------------------------------------------------
 File: test.o (x86_64)
----------------------------------------------------------------------
Searching .debug_info for 'TestFunction'... 1 match:

0x00000575: TAG_subprogram [17] *
             AT_external( 0x01 )
             AT_name( "TestFunction" )
             AT_decl_file( "/private/tmp/test.m" )
             AT_decl_line( 3 )
             AT_prototyped( 0x01 )
             AT_type( {0x00000497} ( Boolean ) )
             AT_low_pc( 0x0000000000000000 )
             AT_high_pc( 0x0000000000000033 )
             AT_frame_base( 0x00000000
                0x0000000000000000 - 0x0000000000000001: rsp+8
                0x0000000000000001 - 0x0000000000000004: rsp+16
                0x0000000000000004 - 0x0000000000000033: rbp+16 )
             AT_sibling( {0x000005dd} )

0x0000059a:     TAG_variable [18]
                 AT_name( "doubleByteDegreeMark" )
                 AT_decl_file( "/private/tmp/test.m" )
                 AT_decl_line( 4 )
                 AT_type( {0x000005dd} ( const int ) )
                 AT_location( [0x0000000000000a1c] )

0x000005c0:     TAG_variable [18]
                 AT_name( "anotherVar" )
                 AT_decl_file( "/private/tmp/test.m" )
                 AT_decl_line( 5 )
                 AT_type( {0x0000009f} ( int ) )
                 AT_location( [0x0000000000000a18] )

0x000005dc:     NULL


So in the case where this is compiled with the C compiler, there is a variable declaration in the debug info for "doubleByteDegreeMark" and you should be able to print it. But as Quincy guessed, in the C++ case, this gets optimized to a constant, and there is no debug info for it:


 > gcc -g -x objective-c++ -c -O0 test.m
 > dwarfdump -n TestFunction -c test.o
----------------------------------------------------------------------
 File: test.o (x86_64)
----------------------------------------------------------------------
Searching .debug_info for 'TestFunction'... 1 match:

0x0000057f: TAG_subprogram [16] *
             AT_external( 0x01 )
             AT_name( "TestFunction" )
             AT_decl_file( "/private/tmp/test.m" )
             AT_decl_line( 2 )
             AT_MIPS_linkage_name( "_Z12TestFunctionv" )
             AT_type( {0x000004a8} ( Boolean ) )
             AT_low_pc( 0x0000000000000000 )
             AT_high_pc( 0x0000000000000015 )
             AT_frame_base( 0x00000000
                0x0000000000000000 - 0x0000000000000001: rsp+8
                0x0000000000000001 - 0x0000000000000004: rsp+16
                0x0000000000000004 - 0x0000000000000015: rbp+16 )
             AT_sibling( {0x000005c4} )

0x000005a7:     TAG_variable [17]
                 AT_name( "anotherVar" )
                 AT_decl_file( "/private/tmp/test.m" )
                 AT_decl_line( 5 )
                 AT_type( {0x000000a1} ( int ) )
                 AT_location( [0x00000000000009e0] )

0x000005c3:     NULL

That's arguably a bug in the compiler. Even if the compiler chooses to optimize the name to a constant, it can still record that fact in the debug information. At -O0 particularly, the compiler is supposed to preserve as much information as it can to make the debugging experience faithful to your source code.

Of course, it may be that the compiler handles const's the way it handles #defines, and the compiler doesn't by default emit debug information for #defines. So the compiler folks may say they aren't going to fix this bug...

But there's nothing the debugger can do about this, the info is not present.

Hope this helps,

Jim

On Jul 16, 2009, at 9:39 AM, Eric Gorr wrote:


On Jul 16, 2009, at 12:29 PM, Fritz Anderson wrote:

On 16 Jul 2009, at 10:25 AM, Eric Gorr wrote:

Boolean TestFunction( void )
{
	static const int doubleByteDegreeMark = 42;
	static int anotherVar = 12;

	return ( doubleByteDegreeMark == 42 && anotherVar == 12 );
}

from GDB, if I do 'info locals' it prints out:

(gdb) info locals
anotherVar = 12

if I try to print out the value of doubleByteDegreeMark, it gives me the error message:

(gdb) print doubleByteDegreeMark
No symbol "doubleByteDegreeMark" in current context.

So, my (probably really simple) question is how can I print out the value of doubleByteDegreeMark from GDB?

Given that doubleByteDegreeMark is a constant, and referred to only within the function, gcc optimized by doing away with the storage, and substituted the literal value 42; and then it saw "42 == 42," and optimized that to true; and then saw "true && anotherVar == 12," and optimized that to "anotherVar == 12." I don't know why it didn't optimize that to simply "true." Maybe at the optimization level you chose, it didn't do the analysis to get that far.


There being no storage for doubleByteDegreeMark, gdb didn't have any stored value to display. I don't know the details of why it didn't know about it just as a constant; I've seen it display enum constants.

Thank you for your comments.

I still see the same problem if change the test function to:

Boolean TestFunction( int val )
{
	static const int doubleByteDegreeMark = 42;
	static int anotherVar = 12;


	return ( val == doubleByteDegreeMark || val == anotherVar );
}

Now, I suppose it is still possible that the compiler will have simply optimized this to:

Boolean TestFunction( int val )
{
	static int anotherVar = 12;

	return ( val == 42 || val == anotherVar );
}

or something.

This is the debug build with all optimizations turned off.

Under other circumstances, should 'info locals' display doubleByteDegreeMark? Should I be able to print doubleByteDegreeMark?


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: GDB & static const variables
      • From: Jonas Maebe <email@hidden>
References: 
 >GDB & static const variables (From: Eric Gorr <email@hidden>)
 >Re: GDB & static const variables (From: Fritz Anderson <email@hidden>)
 >Re: GDB & static const variables (From: Eric Gorr <email@hidden>)

  • Prev by Date: Re: GDB & static const variables
  • Next by Date: Re: GDB & static const variables
  • Previous by thread: Re: GDB & static const variables
  • Next by thread: Re: GDB & static const variables
  • Index(es):
    • Date
    • Thread