Gdb - get variable register name

GDB info registers or info all-registers will display all the names of the register names and their values.

Question:

How to get the name of a variable (i.e. from source code) that is stored in this register? (or line number in source code or something else)

For instance:

 int my_reg = /* something */; float another_reg = /* something else */; ... 

Then perhaps info all-registers will return:

 R0 0x0 0 R1 0xfffbf0 16776176 R2 0x0 0 R3 0x0 0 R4 0x6 6 

How to determine which register (R0? R2? R4?) Is “associated” with my_reg ?

+4
source share
2 answers

At any given time, there may be one register, several registers, or even no registers associated with any given variable C. You will need to check the disassembly to see what happens.

Why not just print my_reg to see the value?

l *$pc display the source code around the current executable command.

+2
source

If you have access to debug symbols (and understand how to read them, that is, you have code that analyzes debug symbols), you can precisely determine which register corresponds to that register. However, this, quite possibly, changes from one line to another, because the compiler decides to move things for one reason or another (for example, some calculations start with R1 and end with the result in R2, because it's better than trying to store the value in R1 [or we need the original value in R1 too - think array[x++] - now we have a new value x , I hope, in the register, and the value of the old x , which we need to use for indexing, should also be in the register to add to base address of array .

Not all variables fall into registers (depending on the processor and "which registers are available").

The WILL debugger knows where each variable is at any given time, but sometimes it can be a big embarrassment, for example:

 int array[10000]; ... for(int i = 0; i < 10000; i++) { array[i] = rand(); } 

might translate something like this during optimization:

 int array[10000]; int *ptr = array; int *ptr2 = &array[10000]; while(ptr < ptr2) { *ptr++ = rand(); } 

Now try to print i ...;)

+2
source

Source: https://habr.com/ru/post/1487172/


All Articles