Tuesday, 15 May 2012

How to reconstruct a C struct given memory content? -



How to reconstruct a C struct given memory content? -

given memory content (e.g. gdb) reconstruct content of c struct. struct defined follows (see man semop):

unsigned short sem_num; /* semaphore number */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */

and memory content (as indicated address of struct using debugger) follows:

00000000 00 00 ff ff 00 10 78 bd 21 0a 8c c8 24 0a c4 95 |......x.!...$...| 00000010 5e 09 d0 69 22 08 18 78 c9 bf ed f4 28 08 00 00 |^..i"..x....(...| 00000020 00 00 01 00 00 00 01 00 00 00 00 00 00 00 01 00 |................|

what values of sem_num, sem_op , sem_flag? safe assume first variable uses 1 byte, while other 2 utilize 2 bytes each? can next mapping?

sem_num = 00 sem_op = 00 ff sem_flg = ff 00

following suggestion of dark falcon, next code seems job:

#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int main (void) { struct sembuf my_sembuf[1]= { {0,-1,16*256} }; unsigned char data[sizeof my_sembuf]; size_t i; memcpy(data, &my_sembuf, sizeof my_sembuf); (i=0; < sizeof my_sembuf; ++i) printf("%02x\n", data[i]); homecoming 0; }

the include ensure semop construction defined, need filled , compared memory dump. turns out, content of semop construction must following:

unsigned short sem_num = 0; short sem_op = -1; short sem_flg = 16*256; /* corresponding flag sem_undo */

c struct gdb

No comments:

Post a Comment