Home > database >  im tring to debug a program with gdb but im not able to remove optimizations
im tring to debug a program with gdb but im not able to remove optimizations

Time:10-25

make CFLAGS ="-g -O0 -fsanitize=address"

after running that, I still have this on the backtrace -

 0x000000000040146e in main (argc=<optimized out>, argv=<optimized out>)

Make file code block

 # Add -Werror if defined
    CFLAGS = -g0 -O2 -Wno-system-headers 
    CFLAG_CARES_SYMBOL_HIDING = -fvisibility=hidden
    CPP = `enter code here`gcc -E
    CPPFLAGS = 
    CPPFLAG_CARES_STATICLIB = 
    CYGPATH_W = echo
    DEFS = -DHAVE_CONFIG_H

CodePudding user response:

Yes, as I said in my top comments, your Makefile is overriding your command line arguments.

Rather than have the make arguments do things, let's have the Makefile do the heavy lifting.

Here is a refactored Makefile:

# Add -Werror if defined
CFLAGS  = -Wno-system-headers

# compile for debug
ifdef DEBUG
  CFLAGS  = -g
  CFLAGS  = -fsanitize=address
# compile for production
else
  CFLAGS  = -O2
endif

CFLAG_CARES_SYMBOL_HIDING = -fvisibility=hidden
CPP = gcc -E
CPPFLAGS =
CPPFLAG_CARES_STATICLIB =
CYGPATH_W = echo
DEFS = -DHAVE_CONFIG_H

To build for release, just do:

make

To build for debug, do:

make DEBUG=1

We can be a bit more sophisticated. When we want debugging, we don't always want -fsanitize=address.

So, we can make separate options:

# Add -Werror if defined
CFLAGS  = -Wno-system-headers

# compile for debug
ifdef DEBUG
  CFLAGS  = -g
# compile for production
else
  CFLAGS  = -O2
endif

ifdef FSANITIZE
  CFLAGS  = -fsanitize=address -O0 -g
endif

CFLAG_CARES_SYMBOL_HIDING = -fvisibility=hidden
CPP = gcc -E
CPPFLAGS =
CPPFLAG_CARES_STATICLIB =
CYGPATH_W = echo
DEFS = -DHAVE_CONFIG_H

Then, we can do either:

make DEBUG=1

Or:

make DEBUG=1 FSANITIZE=1

Even if we forget to add DEBUG=1 when we do FSANITIZE=1, we'll get a CFLAGS result of:

-O2 -fsanitize=address -g -O0

Because -O0 appears last, it will override -O2

These are just some simple examples. Create the Makefile with as many options as you want.

CodePudding user response:

Your mistake is with the command line notation:

make CFLAGS ="-g -O0 -fsanitize=address"

does not do what you think it does. I don't actually know what this does.

What you should write is (note the missing ):

make CFLAGS="-g -O0 -fsanitize=address"

Passing VARIABLE=VALUE on the make command line will override any VARIABLE=VALUE within the makefile. The documentation for this can be found here.

Here's an example of it in action:

$ cat Makefile
CFLAGS = -g0 -O2 -Wno-system-headers 

all:
    @echo "CFLAGS = $(CFLAGS)"
$ make
CFLAGS = -g0 -O2 -Wno-system-headers 
$ make CFLAGS="-g0 -O0"
CFLAGS = -g0 -O0 

You probably spotted the problem here, the -Wno-system-headers flag was lost. You probably wanted that maintained I suspect.

To solve this you will need to tweak your Makefile, here's an improved example:

$ cat Makefile
INTERNAL_CFLAGS = -g0 -O2 -Wno-system-headers $(CFLAGS)

all:
    @echo "CFLAGS = $(INTERNAL_CFLAGS)"
$ make
CFLAGS = -g0 -O2 -Wno-system-headers 
$ make CFLAGS="-g3 -O0"
CFLAGS = -g0 -O2 -Wno-system-headers -g3 -O0

You might worry in the last line about having -g0 and -g3 or -O2 and -O0, but most modern compilers will correctly select the last flag in this case, so you'll get the behaviour you want, and this allows you to override only some of the flags from the command line, if that's what you want, e.g. make CFLAGS="-g3", in which case you'll still compile at -O2.

However, if the duplicate flags really bothers you, try this one, which I don't recommend:

$ cat Makefile
CFLAGS = -g0 -O2
INTERNAL_CFLAGS = -Wno-system-headers $(CFLAGS)

all:
    @echo "CFLAGS = $(INTERNAL_CFLAGS)"
$ make
CFLAGS = -Wno-system-headers -g0 -O2
$ make CFLAGS="-g3 -O0"
CFLAGS = -Wno-system-headers -g3 -O0

Here the -Wno-system-headers flag will always be retained, but any override of CFLAGS on the command line will replace all the CFLAGS.

  • Related