Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Dump arbitrary struct in C

#include <stdio.h>

struct nested {
  int n;
};
struct foo {
  int member_a;
  unsigned long member_b;
  char *str;
  void *ptr;
  struct nested nested;
};

int main(void) {
  struct foo f = {
    .member_a = 123,
    .member_b = 0x4141414141414141,
    .str = "foobar",
    .ptr = &f,
    .nested = {.n = 42}
  };
  __builtin_dump_struct(&f, printf);
}

Put it in test.c, compile it:

clang -o test test.c

Run it:

$ ./test
struct foo {
  int member_a = 123
  unsigned long member_b = 4702111234474983745
  char * str = "foobar"
  void * ptr = 0x7fff1df41b78
  struct nested nested = {
    int n = 42
  }
}

Original via: https://infosec.exchange/@jann/114241610482728821 Also: https://hachyderm.io/@jer/115479734230815229

Last change: , commit: 2c7187e