dwjbosman
dwjbosman

Reputation: 966

Change struct field in GDB using a gdb.value

I am defining a convenience variable in gdb

>set $param = (T_packet*)malloc(sizeof(T_packet))

I can retrieve it via Python

>p = gdb_helper.parse_and_eval("$param")
<gdb.Value at 0x7f30b42f9170>

show the fields of the struct

>python print(p.dereference())

{ID_PACKET = 0 , L_PACKET = 0}

Try to change a field (C equivalent: p->ID_PACKET=1)

p.dereference()["ID_PACKET"] = 1
>"Setting of struct elements is not currently supported"

Is there way to update the value of the field ID_Packet inside p using GDB's Python API?

Upvotes: 2

Views: 1035

Answers (1)

Tom Tromey
Tom Tromey

Reputation: 22519

There's currently no way to set a value using the Value API. This is just a little hole in gdb (I looked but could not find a bug for this, so perhaps filing one would be a good thing to do).

Meanwhile you can work around it, with a bit of difficulty, using gdb.parse_and_eval. The idea is to get the address of the field in question, then form an expression like *(TYPE *) 0xADDR = VALUE. Alternatively you can write directly to memory using Inferior.write_memory.

Both of these approaches will fail in some situations, for example you cannot write to a register this way, preventing this from working on a structure that's been split apart due to SRA optimization.

Upvotes: 1

Related Questions