Thread: how to list all stack valies from GDB debugger

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    how to list all stack valies from GDB debugger

    Hi,

    I am trying to see all values in data segment ,stack segment from GDB debugger

    in this process,

    i am able to see the stack values from GDB command "backtrace full" .

    I want to see all values the values in data segment( both initialized and uninitialized vlues) also. I mean mainly i want to see all the global variables list. Because any way i can see constant and static values if i define in main function from "backtrace full"

    But i did have any idea to see total variables in data segment(mainly global variable) from GDB commands

    any body suggest me to see all those values (global values)?

    i know the see the golbal values like "print variablename" in gdb.

    but i want to see all the variable stored in data segment from gdb.

    please help any one.


    please see the below program to see stack values from GDB

    Code:
    
    #include<stdio.h>
    int g;
    int main()
    {
            int i;
            const int c;
            static int s;
            fun1();
            fun2();
            fun3();
            fun4();
            return 0;
    }
    
    int fun1()
    {
            int i=1;
            printf ("enter the value of i in function one\n");
            scanf ("%d",&i);
            printf ("%d\n",i);
            return 0;
    }
    int fun2()
    {
            int i=2;
            return 0;
    }
    int fun3()
    {
            int i=3;
            return 0;
    }
    int fun4()
    {
            int i=4;
            return 0;
    }
    GDB logs

    Code:
    cc -g 25.c
    [root@examples]# gdb a.out
    GNU gdb Red Hat Linux (6.3.0.0-1.96rh)
    Copyright 2004 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 "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
    
    (gdb) break 0
    No line 0 in file "25.c".
    (gdb) break 1
    Breakpoint 1 at 0x804839c: file 25.c, line 1.
    (gdb) run
    Starting program: /home/koti/examples/c_examples/a.out
    
    Breakpoint 1, main () at 25.c:4
    4       {
    (gdb) n
    8               fun1();
    (gdb) backtrace full
    #0  main () at 25.c:8
            i = 9190560
            c = 0
            s = 0
    (gdb) n
    enter the value of i in function one
    1
    1
    9               fun2();
    (gdb)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    info variables will give you the names of all the global variables, which you can then print.

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi,

    i read the info help. in that for global variables we need to use the command "info variables"

    but how will i trace the global variables from below log?

    because i getting so many variables

    and one more doubt how will i trace static variables? because "info variables" is for only global and static?




    Result logs for "info variables"

    Code:
    All defined variables:
    
    Non-debugging symbols:
    0x08048490  _fp_hw
    0x08048494  _IO_stdin_used
    0x080484a0  __FRAME_END__
    0x080494a4  __CTOR_LIST__
    0x080494a8  __CTOR_END__
    0x080494ac  __DTOR_LIST__
    0x080494b0  __DTOR_END__
    0x080494b4  __JCR_END__
    0x080494b4  __JCR_LIST__
    0x080494b8  _DYNAMIC
    0x08049584  _GLOBAL_OFFSET_TABLE_
    0x08049598  __data_start
    0x08049598  data_start
    0x0804959c  __dso_handle
    0x080495a0  p.0
    0x080495a4  i.0
    0x080495a8  completed.1
    0x00000000  __libc_tsd_LOCALE
    0x00000004  __libc_resp
    0x00000004  __resp
    0x00000008  __libc_errno
    0x00000008  errno
    0x0000000c  __libc_tsd_CTYPE_B
    0x00000010  __libc_tsd_CTYPE_TOUPPER
    0x00000014  __libc_tsd_CTYPE_TOLOWER
    0x00000018  __libc_tsd_MALLOC
    0x0000001c  __libc_h_errno
    0x0000001c  h_errno
    0x00000020  __libc_tsd_RPC_VARS
    0x00000024  data.0
    0x009cdac0  __libc_version
    0x009cdac6  __libc_release
    0x009cdae0  banner
    0x009cde54  gconv_conf_filename
    0x009cde62  gconv_module_ext
    0x009cde66  default_gconv_path
    0x009cde75  inmask.0
    0x009cde80  _nl_category_name_sizes
    0x009cdec0  _nl_default_locale_path
    0x009cdee0  codeset_idx.0
    0x009cdf20  _nl_category_num_items
    0x009cdf60  _nl_value_type_LC_COLLATE
    0x009cdfc0  _nl_value_type_LC_CTYPE
    0x009ce0e0  _nl_value_type_LC_MONETARY
    0x009ce198  _nl_value_type_LC_NUMERIC
    0x009ce1c0  _nl_value_type_LC_TIME
    0x009ce37c  _nl_value_type_LC_MESSAGES
    0x009ce390  _nl_value_type_LC_PAPER
    0x009ce39c  _nl_value_type_LC_NAME
    0x009ce3c0  _nl_value_type_LC_ADDRESS
    0x009ce3f4  _nl_value_type_LC_TELEPHONE
    0x009ce408  _nl_value_type_LC_MEASUREMENT
    0x009ce420  _nl_value_type_LC_IDENTIFICATION
    0
    Last edited by nkrao123@gmail.; 09-06-2011 at 06:52 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    because i getting so many variables
    The moon isn't made of green cheese after all?

    and one more doubt how will i trace static variables? because "info variables" if for only global and static?
    If you are just looking to trace certain variables, use "print"

    (gdb) print MyGlobal

    will give you the value of the variable labelled "MyGlobal", if it is currently in scope.

    You might also find ddd, the graphical gdb, interesting:

    http://cboard.cprogramming.com/progr...linux-ddd.html

    It makes keeping track of a few things at once somewhat easier, since you can stick things you want to watch in one of the windows and they will be updated at each breakpoint.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Code:
    info var g
    Gives the location of "g" in the data segment and for tracing you'd need to set a break(watch) point in the function that uses "g".


    [Edit] First set a breakpoint on the statement that uses "g", run the program, and when it comes to that statement issue a "display g" command.
    Last edited by itCbitC; 09-06-2011 at 06:57 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your globals appear before the non-debugging symbols. I just ran a quick test and got
    Code:
    All defined variables:
    
    File temp.c:
    struct Leg global;
    
    Non-debugging symbols:
    <lots of things>

  7. #7
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi,

    I understand your reply.

    but my ultimate aim is to see all the variables stored in data segment.

    it there any command to check this? is it possible?

    because if i have 200 global variables, its difficult to see each value by giving "printf $globalvariablename" command.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is it possible for you to care about 200 different variables at the same time? Why not print just the one (or few) that you care about?

    (EDIT: And I suppose if you did somehow care about 200 different variables, you could check each address to make sure they are in fact next to each other, and if so, then use GDB's @ operator to examine the whole chunk of memory at once.)
    Last edited by tabstop; 09-06-2011 at 06:58 AM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nkrao123@gmail. View Post
    because if i have 200 global variables...
    Trust me on this... If you're program has 200 global variables, you need to start re-writing!

  10. #10
    kotin
    Join Date
    Oct 2009
    Posts
    132
    hi if i give the command "info variables" , is there any way to send all logs to a fine like

    info variables > logs.txt

    ?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    On Unix and Linux systems it'd be something like:
    Code:
    gdb binfilename | tee outfilename
    but it'll give you globals created by the linker too.

  12. #12
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI all,

    Thanks for your reply's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack using linked list problem
    By effa in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2009, 12:10 PM
  2. STACK/FIFO as a linked list
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-24-2009, 02:04 PM
  3. Stack in linked list
    By mag_chan in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 05:31 AM
  4. Linked list Stack question
    By lyrick in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2005, 06:23 AM
  5. linked list stack question
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2003, 05:05 AM