Search code examples
apigdbinternals

GDB Internals : Getting variable contents


I want to add a new command to gdb source code, and I can do that using add_cmd.

In the command backend, I want to read contents of a few variable and print data in a more relevant format to me.

I can get the variable address using the following code:

struct symbol *sym;

sym = lookup_symbol ("my_variable_name", get_selected_block (&context_pc), VAR_DOMAIN, NULL);

printf_filtered ("Symbol %s is ", sym->ginfo.name);
printf_filtered (" at address 0x%x\n", SYMBOL_VALUE(sym));

This gives me the address of the variable:

(gdb) my_command
Symbol my_variable_name is  at address 0x8049684

However, I am not able to find what API will give me the contents of this variable. Need help with the same.


Solution

  • In the command backend, I want to read contents of a few variable and print data in a more relevant format to me.

    It is exceedingly likely that you can achieve the same result using a Python pretty-printer. That requires no rebuilding of GDB, is easy to play with to get correctly, and provides a safe interface to GDB's internals.

    I am not able to find what API will give me the contents of this variable.

    target_read_memory() is the lowest API that will give you the contents of memory at the given address.