Thread: gdb breakpoints

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    29

    gdb breakpoints

    In order to understand a program for data compression, I'm trying to step through a program using GDB, and am having a problem setting a breakpoint.The code is all in a single directory, and the Makefile is as follows:
    Code:
    ############################################################################
    #
    # Makefile for huffman encode/decode programs
    # 
    ############################################################################
    
    
    CC = gcc
    LD = gcc
    CFLAGS = -ggdb -O3 -Wall -Wextra -ansi -pedantic -c
    LDFLAGS = -O3 -o
    
    
    # libraries
    LIBS = -L. -lhuffman -loptlist
    
    
    # Treat NT and non-NT windows the same
    ifeq ($(OS),Windows_NT)
    	OS = Windows
    endif
    
    
    ifeq ($(OS),Windows)
    	EXE = .exe
    	DEL = del
    else	#assume Linux/Unix
    	EXE =
    	DEL = rm
    endif
    
    
    all:		sample$(EXE)
    
    
    sample$(EXE):	sample.o libhuffman.a liboptlist.a
    		$(LD) $^ $(LIBS) $(LDFLAGS) $@
    
    
    sample.o:	sample.c huffman.h optlist.h
    		$(CC) $(CFLAGS) $<
    
    
    libhuffman.a:	huffman.o canonical.o huflocal.o bitarray.o bitfile.o
    		ar crv libhuffman.a huffman.o canonical.o huflocal.o\
    		bitarray.o bitfile.o
    		ranlib libhuffman.a
    
    
    huffman.o:	huffman.c huflocal.h bitarray.h bitfile.h
    		$(CC) $(CFLAGS) $<
    
    
    canonical.o:	canonical.c huflocal.h bitarray.h bitfile.h
    		$(CC) $(CFLAGS) $<
    
    
    huflocal.o:	huflocal.c huflocal.h
    		$(CC) $(CFLAGS) $<
    
    
    bitarray.o:	bitarray.c bitarray.h
    		$(CC) $(CFLAGS) $<
    
    
    bitfile.o:	bitfile.c bitfile.h
    		$(CC) $(CFLAGS) $<
    
    
    liboptlist.a:	optlist.o
    		ar crv liboptlist.a optlist.o
    		ranlib liboptlist.a
    
    
    optlist.o:	optlist.c optlist.h
    		$(CC) $(CFLAGS) $<
    
    
    clean:
    		$(DEL) *.o
    		$(DEL) *.a
    		$(DEL) sample$(EXE)
    I'd like to set a breakpoint to step through HuffmanEncodeFile(), which is found in huffman.c:90 and called at sample.c:210
    When I try running gdb with such a breakpoint, I get the following terminal output:
    Code:
    Breakpoint 3 at 0x8048ca0<function, no debug info> HuffmanEncodeFile;
    I've checked the manual (which I'm not real clear on), and given a try to hbreak, but that didn't help.
    Do I have to make any changes to Makefile? Am I writing my breakpoints incorrectly?

    [I'm using a virtual machine running linux, with a clang compiler, if that matters]

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Try adding the -g3 option at the compilation command-line.
    E.g.

    Code:
     gcc -g3 ...
    Actually, you can copy your makefile to a debugging version, with all -O3's replaced by -g3's
    Last edited by migf1; 06-05-2015 at 04:14 PM.
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    29
    Quote Originally Posted by migf1 View Post
    Try adding the -g3 option at the compilation command-line.
    E.g.

    Code:
     gcc -g3 ...
    Actually, you can copy your makefile to a debugging version, with all -O3's replaced by -g3's
    No luck. With your first suggestion, I get terminal output of:
    Code:
    jharvard@appliance (~/Dropbox/huffman): makegcc sample.o libhuffman.a liboptlist.a -L. -lhuffman -loptlist -03 -o sample
    gcc: error: unrecognized command line option '-03'
    and with the second:
    Code:
    Breakpoint 1 at 0x8048789: file sample.c, line 83.
    (gdb) b huffman.c:90
    No source file named huffman.c.
    Perhaps I should mention that other files that I have upon which gdb functions properly are compiled with clang, rather than gcc. Would this make any difference?

  4. #4
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    I'm not really familiar with clang, but according to the docs it is supposed to be a drop-in replacement for gcc.

    Nevertheless, the makefile you have presented specifies gcc to be the compiler (in its first 2 lines).

    For the rest, -g3 means level-3 debugging, which produces extended debugging info (such as macro definitions). It is similar to -ggdb3 (depending on the platform and/or gcc port, they may even be identical).

    -O3 (note, that is an O, not a zero) means level-3 optimization, the most aggressive optimizing level available in gcc.

    Although gcc docs state that you can mix debug & optimize options in the command line, if you do so you may end up debugging assembly instead of C (due to heavy optimization I mean).

    Anyway, your makefile specifies both a debugging option (namely: -ggdb) and an optimization option (namely: -O3).

    What I suggested (but I was not clear in my previous post, sorry) was to have 2 makefiles: one for debugging, using only the debugging option (either -g3 or -ggdb, etc) and one for release, using only the optimization option (either -O3, or -O2, etc).

    For your convenience, I'm listing below links for gcc's both debug & optimize command-line options:

    1. gcc debug options
    2. gcc optimize options

    PS. -Og is a later addition, which is supposed to be the best compromise between debugging and optimization, you may also try that.
    Last edited by migf1; 06-05-2015 at 11:38 PM.
    "Talk is cheap, show me the code" - Linus Torvalds

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    29
    I'll take a look & see if anything works. So far I've tried redoing Makefile in all sorts of combos, and remade file, but still not the right combo. Maybe the links will help.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    29

    Smile gdb breakpoints

    finally got the right combination of switches in Makefile to allow gdb to function:
    Code:
    #############################################################################
    # Makefile for huffman encode/decode programs
    #
    ############################################################################
    
    
    CC = gcc -g3
    LD = gcc
    CFLAGS = -ggdb -O3 -Wall -Wextra -ansi -pedantic -c
    LDFLAGS = -O3 -o
    Thanks to all for your persistence & help

  7. #7
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    I'm glad you succeeded. However, those options are redundant.

    I just downloaded the library you are referring to, from here (I got the latest 0.9 version).

    The included makefile does NOT match the one you have showed in the thread.

    More precisely, the included makefile does NOT mix debug & optimize options (it shouldn't anyway). It only uses the highest optimize option: -O3 (so it is a makefile for the final release of the project).

    Code:
    ...
    CC = gcc
    LD = gcc
    CFLAGS = -O3 -Wall -Wextra -ansi -pedantic -c
    LDFLAGS = -O3 -o
    ...
    For a debugging version of that makefile, all we have to do is to replace the -O3 option with a debug option (I mostly use -g3, but you can use any other: -ggdb, -ggdb2, -ggdb3, etc).

    Code:
    ...
    CC = gcc
    LD = gcc
    CFLAGS = -g3 -Wall -Wextra -ansi -pedantic -c
    LDFLAGS = -g3 -o
    ...
    Using the modified makefile I successfully compiled a debugging version of the library with mingw 4.8.1 on Windows (actually I had to #define ENOTSUP 252 manually, since mingw's errno.h does not define it).

    I then successfully stepped through the code of simple.c via gdb.
    "Talk is cheap, show me the code" - Linus Torvalds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GDB help with setting breakpoints
    By shredderr in forum C Programming
    Replies: 5
    Last Post: 04-19-2011, 08:47 AM
  2. C::B breakpoints problem
    By Kayl669 in forum C Programming
    Replies: 4
    Last Post: 02-18-2010, 11:31 AM
  3. Breakpoints in VC7.1
    By nidhimathew in forum C++ Programming
    Replies: 1
    Last Post: 08-10-2004, 05:10 PM
  4. annoying breakpoints
    By Benzakhar in forum Windows Programming
    Replies: 2
    Last Post: 01-17-2004, 01:22 PM
  5. how do i get it to show the breakpoints
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2002, 09:13 PM