Thread: A simple question about GDB debugging

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A simple question about GDB debugging

    I use gdb to debug code. Each time I use "next" command to execute code line by line, GDB shows the line number of the current executing line, but it doesn't show the content of the code at the line. So I have to find it in my source code with the line number. It's very boring.

    How can I see the code content of a line this is being executing in GDB? Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    are you using the "-g" switch when compiling? (assuming gcc)

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    yes, of course. Otherwise I even can't use GDB.
    The problem is GDB does not show the code line content but the line number.
    I remembered it showed content before I changed some configure. Now I just want to change it back. :-(

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    does the 'list' command work as expected?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    I am curious that when GDB is working, does it refer to my source code to fetch the code content?
    If I, say, delete the source code and use GDB to debug the binary, is GDB still able to show the code content in any way? (I don't have my linux machine at hand today, so I can't verify it now)
    Anybody knows?
    Last edited by meili100; 12-08-2007 at 11:57 AM.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks, but what is "list" ?
    I tried to google it but can't find any clue.

    Quote Originally Posted by pheres View Post
    does the 'list' command work as expected?

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    cyberfish@cyberfish-laptop:~$ gdb
    GNU gdb 6.6-debian
    Copyright (C) 2006 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i486-linux-gnu".
    (gdb) help list
    List specified function or line.
    With no argument, lists ten more lines after or around previous listing.
    "list -" lists the ten lines before a previous ten-line listing.
    One argument specifies a line, and ten lines are listed around that line.
    Two arguments with comma between specify starting and ending lines to list.
    Lines can be specified in these ways:
      LINENUM, to list around that line in current file,
      FILE:LINENUM, to list around that line in that file,
      FUNCTION, to list around beginning of that function,
      FILE:FUNCTION, to distinguish among like-named static functions.
      *ADDRESS, to list around the line containing that address.
    With two args if one is empty it stands for ten lines away from the other arg.
    (gdb)

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    does it refer to my source code to fetch the code content?
    If I, say, delete the source code and use GDB to debug the binary, is GDB still able to show the code content in any way? (I don't have my linux machine at hand today, so I can't verify it now)
    Anybody knows?
    I just tried it on my Linux machine. Yes, that is the case. gdb no longer shows line content after I delete the source file.

    Code:
    cyberfish@cyberfish-laptop:/tmp$ echo "int main() { int a[10]; a[10000] = 5; }" > a.cpp
    cyberfish@cyberfish-laptop:/tmp$ g++ -g a.cpp
    cyberfish@cyberfish-laptop:/tmp$ gdb ./a.out
    GNU gdb 6.6-debian
    Copyright (C) 2006 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...
    Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
    (gdb) r
    Starting program: /tmp/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    main () at a.cpp:1
    1       int main() { int a[10]; a[10000] = 5; }
    (gdb) q
    The program is running.  Exit anyway? (y or n) y
    cyberfish@cyberfish-laptop:/tmp$ rm a.cpp
    cyberfish@cyberfish-laptop:/tmp$ gdb ./a.out
    GNU gdb 6.6-debian
    Copyright (C) 2006 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...
    Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
    (gdb) r
    Starting program: /tmp/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    main () at a.cpp:1
    1       a.cpp: No such file or directory.
            in a.cpp
    (gdb) q
    The program is running.  Exit anyway? (y or n) y

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM