views:

376

answers:

3

I would like to run a GTK+/C program line by line with some debugger. I have never debugged a Linux program so where can I find instructions to a very beginner on how to debug code? I have just an idea that I have to download the sources from net, compile the project with debug symbols and run sources through DDD or GDB. So can anyone give suggestions how to start studying these subjects and what could be a good debugger to start? I have at least time to learn things.

+1  A: 

Look into using GDB and compiling with gcc -g, or using another debugger.

I'd go into more detail, but your 0% accept rate is concerning.

http://en.wikipedia.org/wiki/Gdb

Mike
Thank you very much! I accepted the answer but rating shows still 0%. Trust me: I accept your and anyone else answers I have got so far no matter the rate shows.I don't trust Wikipedia as anyone can edit it. Is there more reliable tutorial?
Jaska
@Mike: He's only asked 7 questions. Give it time.
Brian
@Jaska: The article links to http://www.dirac.org/linux/gdb/ as a suggested tutorial.
Brian
Wikipedia shouldn't be treated as reliable information. I use it as an easy way of finding reliable sources of information.
Mike
+1  A: 

Also look into nemiver besides DDD, they are good at different things but nemiver is coming along very nicely.

PS. If you're on Ubuntu and you want to step through an application that's installed from the package repository, let's called it some_package, then do this:

  1. install the packages "build-essential" and "devscripts"
  2. run "sudo apt-get build-dep some_package" to install all things needed to compile that package
  3. run "mkdir -p ~/src/some_package ; cd ~/src/some_package" to create a directory for the source code
  4. Go into System::Administration::Software Sources and activate the "Source Code" repository
  5. run "apt-get source some_package" to download the source code for some_package and put it in the current directory
  6. use "cd" to move into the specific app directory, usually something like "some-app-1.2.3"

7A. run "debuild -us -uc -b" to compile the source into a fresh installable .DEB file compiled in release mode WITHOUT debug information

or (and this is the central part):

7B. run "MAKEFLAGS=-j6 DEB_BUILD_OPTIONS="nostrip noopt parallel=5" CFLAGS="-g3 -O0" debuild -us -uc" to build a deb in debug mode WITH debug information

  1. use "cd .." to move up one step and then do "ls" and you should see the generated DEB files (multiple binary packages, i.e. DEBs, can be generated from a single source package)
  2. sudo "sudo dpkg -i some_package_123.deb" to install the version you just built

Of course, you could optionally modify the code between steps 6 and 7. The nice thing about this method is that it works for pretty much any application. You can use it for Firefox, the Linux kernel, the mesa stack or whatever.

martin
Thanks! I tried Nemiver to debug gedit and this works. But it calls some GTK+'s libraries so how can I use Nemiver to see what is going to happen inside GTK library? And can I combine valgrind and Nemiver so that I can see if there is a memory leak between lines, say 1 to 350.
Jaska
By the way, Ubuntu repositories does not hold the latest code developers uses. Is there some method to compile the latest code they are working on?
Jaska
Different upstream projects use different VCS and build systems so there is no universal method for building them. You just have to learn whatever tools that particular project uses, which can be a big hurdle for large projects like Mozilla or Xorg.
martin
A: 

http://www.sourceware.org/current/onlinedocs/gdb%5Ftoc.html

Full gdb user manual online.

Michael Snyder