views:

113

answers:

1

Hi,

I am learning to solve some runtime error with gdb. Here are my questions:

  1. when runtime error happens because some access operations of some memory is conflicted, can I find out in the dumped core the address of that memory?

  2. Given an address, is it possible to find out which variable is using it (the address may be at the begining, end or middle of the memory of the variable)?

  3. Given the memory used by a variable, is it possible to find out its nearby variables down below and right above it?

Thanks and regards!

+2  A: 
  1. Usually yes. Assuming your program crashed outside GDB due to SIGSEGV and left a core dump, you can:
    A: find out which instruction actually caused the access violation:

        (gdb) x/i $pc
    

    This will usually be a memory access instruction, e.g. "movl $1,8(%eax)". What's important is then what value does the register which is supposed to point to valid memory have.
    B. find out value of that register:

       (gdb) p/x $eax
    

    Often this would be 0 (you are writing through a NULL pointer), or some nonsense value, e.g. 0x32314043 (you've corrupted the pointer, or overwrote it with an ASCII string).

  2. The GDB "info symbol" command will tell you which symbol (if any) is near the given address.

  3. Use the same "info symbol" command for addresses slightly smaller and slightly larger the address of your "target" variable.

Update:
The info symbol doesn't work on local (automatic) variables, because such variables don't have a (symbol table) symbol associated with them.

To find info about local variables, do "info locals". You can then print their addresses with

(gdb) print &a_local_variable

I don't know of any way to do the inverse (i.e. map an address of a local variable back to its symbolic name). However, if you only have a small number of locals, it is usually trivial to map an address into one of them "by hand". And if you have too many locals, that's a bad "code smell" -- you should probably refactor your code so that you don't.

Employed Russian
Thanks a lot!Looks like "info symbol" only works for global or nonstatic variable, but does't work on the memory address of a local nonstatic variable even if I am in the same frame as that variable. So how do you find out the local nonstatic variable that is using a given address? If it can be useful, why "info symbol" doesn't support it?
Tim