Find info from address in debugging
debugging, gdb, memory
Solution
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).
The `GDB` `"info symbol"` command will tell you which symbol (if any) is near the given address.
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.
Problem
I am learning to solve some runtime error with gdb. Here are my questions: 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? 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)? 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!