Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all; I'm using ubuntu 9.04. I've started using "gdb" to debug my project line by line and have not worked with command line debugger yet. So, I'm not familiar ...
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    27

    Problem in debugging with "GDB"?

    Hi all;

    I'm using ubuntu 9.04.

    I've started using "gdb" to debug my project line by line and have not worked with command
    line debugger yet. So, I'm not familiar with this type of debugging!

    When I write "gdb ./myprogram" in terminal, I see:
    Code:
    GNU gdb 6.8-debian
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...
    And when I write "b 20" to set a breakpoint at 20th line of code, I see:
    Code:
    Breakpoint 1 at 0x8048150: file ../sysdeps/i386/elf/start.S, line 20.
    Afterwards, when I run "r" command, I expect that the program runs till 20th line and then I can write "n" command line by line to see the parameters' values and memory contents, but I face with the program's complete running without any stop on line 20!!

    What is the solution please?!

    TIA.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Did you compile the program with debugging options?
    gcc -g -O2 myprogram.c -o myprogram
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    As a general rule, when debugging, you should compile the program without optimizations:
    Code:
    gcc -g -O0 myprogram.c -o myprogram
    As Lakshmipathi says, "-g" enables debugging symbols, which allows gdb to better connect your code and the program.

    Also be aware that there's a lot more code than what you wrote in your executable (the C library, your code, and any external libraries you use). Therefore, when you set a breakpoint, specify the file:
    Code:
    b myprogram.c:20
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Sep 2009
    Posts
    27
    Hi and thanks of your help!

    When I write "b myprogram.cpp:20" in gdb, I see the result:
    (while, I'm in myprogram.cpp directory, of course!)

    Code:
    No source file named myprogram.cpp.
    Make breakpoint pending on future shared library load? (y or [n]) y
    Breakpoint 1 (myprogram.cpp:20) pending.
    And then, I type the "r" command, but the program runs completely without any stop on line 20!

    What is the reason please?!

    THX.

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    That is bizarre. gdb should know about your application's source code if you compiled your program with the "-g" flag.

    If you open gdb, you can run the "info sources" command to see what source code files gdb knows about. That may give you an idea to the problem.
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You are probably running the debugger/program in a different directory from the source. Use the full path if you can, or set it to stop in the function that the line is in. You can set break points at the entry poitns to a function call, lines in source files, etc. In any case, do read the man pages for gdb with some care.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...