Thread: info symbol in gdb

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    info symbol in gdb

    Hi,
    I am learning to use "info symbol <address>" in gdb. here is my code:
    Code:
      #include <iostream>
      #include "functions.h"
      using namespace std;
    
      int main(){
    
        int tt = 3;
    B =>cout << tt << endl;
    
          print_hello();
          cout << endl;
          cout << "The factorial of 5 is " << factorial(5) << endl;
          return 0;
      }
    I stopped the running at B and then tried to test the using of "info symbol" with the variable tt:
    (gdb) info symbol &tt
    No symbol matches &tt.
    (gdb) p &tt
    $9 = (int *) 0xbfbcbf9c
    (gdb) info symbol 0xbfbcbf9c
    No symbol matches 0xbfbcbf9c.
    Does someone know why it doesn't work?

    Thanks and regards!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you type "help info symbol" into gdb?
    Quote Originally Posted by help info symbol
    (gdb) help info symbol
    Describe what symbol is at location ADDR.
    Only for symbols with fixed locations (global or static scope).

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    info symbol|address requests or returns a stack-frame offset address, not a memory address.

    info symbol tt is what you are looking for. But will not print 0xbfbcbf9c.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks! I was reading the gdb document and didn't see the global or static requirement the online help offers.

    So for non-global or non-static variables, such as tt in my case:

    1. is it possible to get the symbol given address?

    2. as to the reverse,
    Code:
    (gdb) info address tt
    Symbol "tt" is a variable with complex or multiple locations (DWARF2).
    (gdb) help info address
    Describe where symbol SYM is stored.
    Is it also require global static variable? is it possible for nonstatic local ones?

    Thanks and regards!

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks Mario F.!

    How to get stack-frame offset address from a memory address?

    Assuming my program has segment fault and stops in gdb, isn't the address shown by backtrace the memory address? How to do if I want to know what variable (usually local non-static) is using the memory address?

    Thanks and regards!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's info locals, but that won't give you addresses. You can print arbitrary expressions:
    (gdb) info locals
    tt = 3
    (gdb) p &tt
    $1 = (int *) 0xbffff95c
    (gdb) p *(0xbffff95c)
    $2 = 3
    I don't remember whether you get type information -- so if tt was a float, would you get 3.0 or whatever 3 looks like as a float. That should be a start, I guess.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lehe View Post
    Thanks Mario F.!

    How to get stack-frame offset address from a memory address?

    Assuming my program has segment fault and stops in gdb, isn't the address shown by backtrace the memory address? How to do if I want to know what variable (usually local non-static) is using the memory address?

    Thanks and regards!
    A memory address, but a memory address of code, not a variable (unless I am grossly misunderstanding what you mean). And generally gdb will show you that line if it's available.

    EDIT: For example, you might get something like this:
    Code:
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
    0x00001e29 in main () at symbols.c:9
    9	    cout << *foo << endl;
    The 0x00000000 is the value of foo in the sample line, not where foo is stored. The 0x00001e29 is where my code is, and that specific line is given to me.
    Last edited by tabstop; 08-29-2009 at 12:42 PM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thank you, tabstop!
    1. does the symbol generated with gdb -g only for global or static variables, not for local nonstatic variables?
    2. Given an address, is it possible to know the local variable that is using it?
    3. when analyzing the place where segment fault happens, Is it possible to find out the memory address whose access causes the runtime error?
    Thanks!

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lehe View Post
    Thank you, tabstop!
    1. does the symbol generated with gdb -g only for global or static variables, not for local nonstatic variables?
    2. Given an address, is it possible to know the local variable that is using it?
    3. when analyzing the place where segment fault happens, Is it possible to find out the memory address whose access causes the runtime error?
    Thanks!
    1. info frame and info locals gives you some information, and you can probably piece things together from there if you have to.
    2. I can't think of a way off the top of my head. I can also not dream up a scenario in which such thing is even a little bit useful, so I admit I didn't work very hard at it. (Apart from going down the list in info locals and finding the address of each.)
    3. Given that gdb tells you straight up when such a thing happens, I would say "yes". Did you look at the edit I put in last time?

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks again!

    for 3, I meant the memory address for a variable not for the code line.

    But now I think if I could know the line of code that directly causes the problem, the variable whose memory access causes the problem must be used by the line of code. And then look around nearby variables stored around that variable, I could locate the actual reason that causes the runtime error. Is this the right way to solve the memory runtime error?

    Thanks!

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lehe View Post
    Thanks again!

    for 3, I meant the memory address for a variable not for the code line.

    But now I think if I could know the line of code that directly causes the problem, the variable whose memory access causes the problem must be used by the line of code. And then look around nearby variables stored around that variable, I could locate the actual reason that causes the runtime error. Is this the right way to solve the memory runtime error?

    Thanks!
    That's right. Except there's no "looking around" involved, really -- it's all about the variable that's actually used. Did you read my post?
    Code:
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
    0x00001e29 in main () at symbols.c:9
    9	    cout << *foo << endl;
    I wonder which line and which variable could be involved....

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Yes I see.
    As to looking around that variable, I meant in some cases when I write out of the boundary of an array, runtime error occurs not at that place but several lines later where a nearby variable is affected and triggers the error. So l think it might help to look around.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lehe View Post
    Yes I see.
    As to looking around that variable, I meant in some cases when I write out of the boundary of an array, runtime error occurs not at that place but several lines later where a nearby variable is affected and triggers the error. So l think it might help to look around.
    Perhaps. If you do that commonly, you should look at doing bounds-checking rather than waiting for a run-time error.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    A final question:
    are there some cases "info symbol <address>" will help to debug an error?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM