Examine a variable with GDB

This is a discussion on Examine a variable with GDB within the Linux Programming forums, part of the Platform Specific Boards category; I need to examine the value of a pointer at a certain point in my program, and I'm not quite ...

  1. #1
    FOX
    Join Date
    May 2005
    Posts
    188

    Examine a variable with GDB

    I need to examine the value of a pointer at a certain point in my program, and I'm not quite sure how to do it. I tried 'print ptr', but GDB just throws back this message:
    Code:
    No symbol "ptr" in current context.
    It doesn't work for any other variable either. I've compiled the program with the -g switch.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    Did you get other messages when you started gdb <filename> ?

    What that error means is that the variable name you entered is not in scope.
    Try running the code and breaking on the line where you think the variable has a problem.
    Code:
    gdb <filename>
    br  22
    r
    print *ptr

  3. #3
    FOX
    Join Date
    May 2005
    Posts
    188
    So if I put the break inside a function call, I can't examine variables in main()?

  4. #4
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    hmm, you were actually using print ptr?
    or were you using *ptr?

    ptr would be a variable in itself.
    *ptr is the pointer. and you would need to know which variable is being accessed.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  5. #5
    FOX
    Join Date
    May 2005
    Posts
    188
    I wanted to find out what address and possible also what memory label ptr was pointing at.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    you can "br main" or br on a line in main just after your function call.

    If you step through the function, you will eventually come back to main.

    When you are in another function, the calling function's variables are not in scope.

  7. #7
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    You just need to go "up" in the stack to bring ptr back in scope.
    Code:
    jdeckard@bender ~/test $ cat gdbfun.c
    void myfunc( void )
    {
       int x;
    
       x = 1 + 1;
    }
    
    int main( void )
    {
       char *ptr = "Greetings from the text segment!";
    
       myfunc();
    
       return 0;
    }
    
    jdeckard@bender ~/test $ gcc -g -o gdbfun gdbfun.c -std=c99
    jdeckard@bender ~/test $ gdb gdbfun
    GNU gdb 6.3
    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 "x86_64-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
    
    (gdb) b myfunc
    Breakpoint 1 at 0x40046c: file gdbfun.c, line 5.
    (gdb) r
    Starting program: /home/jdeckard/test/gdbfun
    
    Breakpoint 1, myfunc () at gdbfun.c:5
    5          x = 1 + 1;
    (gdb) up
    #1  0x000000000040048a in main () at gdbfun.c:12
    12         myfunc();
    (gdb) p ptr
    $1 = 0x400590 "Greetings from the text segment!"
    (gdb)
    Jason Deckard

  8. #8
    FOX
    Join Date
    May 2005
    Posts
    188
    Ah right, so 'up' moves up one stack frame? That's good to know.

  9. #9
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Correct, and it doesn't change the flow of execution. I'm sure you can guess what command takes you back down the stack.
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using gdb
    By amitapm in forum C Programming
    Replies: 10
    Last Post: 04-17-2009, 08:14 AM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. does gdb lie?
    By dinjas in forum C Programming
    Replies: 8
    Last Post: 03-10-2005, 04:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21