Jorge Zapata
Jorge Zapata

Reputation: 2336

Debugging C project with GDB

First of all, I want to point that I have no C programming background so a more detailed answer will help me a lot. I downloaded a C mp3 decoder project from here and I need to debug it in order to understand some steps from the mp3 compression algorithm.

I've learned the basics of GDB from different tutorials so I started by compiling the project using the Makefile included, then I ran GDB for the compiled output like this:

gdb --args mp3decode test.mp3

But when trying to list the files to set a breakpoint I get this:

(gdb) list
init.c:1: No such file or directory.

Googling a little bit about above output, the more common mistake when compiling is not using the -g option however I made sure that option is in the Makefile.

CFLAGS = -g -O4 -funroll-loops -Wall -ansi -DOUTPUT_SOUND
OBJS = MP3_Bitstream.o MP3_Decoder.o MP3_Huffman.o MP3_Huffman_Table.o \
    MP3_Main.o MP3_Synth_Table.o main.o remote_control.o audio.o debug.o

Then later:

mp3decode: $(OBJS)
    $(CC) $(CFLAGS) -o mp3decode  $(OBJS) -lm

What I want to achieve is to set a breakpoint on line 46 of main.c file and then step into the MPG_Get_Filepos() method on the same line, but the main.c is not listed in the GDB console.

I think the problem is in the compile command since creates a C object for all files (don't know if I'm wrong) and that's why I cannot debug the code.

What can I do to debug step by step in a project like this? Your help will be appreciated, thanks.

UPDATE:

This is the command I'm using to compile the program:

gcc -g -funroll-loops -Wall -ansi -DOUTPUT_SOUND -o mp3decode  MP3_Bitstream.o MP3_Decoder.o MP3_Huffman.o MP3_Huffman_Table.o MP3_Main.o MP3_Synth_Table.o main.o remote_control.o audio.o debug.o -lm

Then I use GDB with tui to debug it:

$ gdb -tui --args mp3decode test2.mp3
(gdb) br main
Breakpoint 1 at 0x804ea2e
(gdb) run
Starting program: /home/jorge/Documents/mp3decoder/mp3decode test2.mp3

Breakpoint 1, 0x0804ea2e in main ()
(gdb) list
init.c:1: No such file or directory.

Upvotes: 0

Views: 1832

Answers (2)

Jorge Zapata
Jorge Zapata

Reputation: 2336

The problem was I didn't delete all binary files specifically C shared objects. After deleting all binary files I recompiled with the -g option and then GDB started working as expected.

Upvotes: 2

Sagar Sakre
Sagar Sakre

Reputation: 2426

Check by setting dir to source path.

Example: Suppose source code is in /home/exp/mp3decoder

then after

gdb --args mp3decode test.mp3

gdb dir /home/exp/mp3decoder

Upvotes: 1

Related Questions