Git Product home page Git Product logo

clayout's Introduction

clayout, translate C++/Rust type into C type with the same memory layout. Generally, clayout is used together with bpftrace.

clayout is developed on ddbug. THANKS FOR ddbug!

Usage

Imagine a scenario where you want to use bpftrace to track the value of S::x during the running of the following program.

#include <stdio.h>
#include <unistd.h>

struct X {
  virtual ~X() {}

  int x1;
};

struct S : public X {
  S() : x(0) {}

  S(const S &other) : x(other.x) {}

  S f(int y, int z) {
    printf("output from a.out: this.x=%d y=%d z=%d\n", x, y, z);
    x += (y + z);
    return *this;
  }

  int x;
};

int main(int argc, char **argv) {
  S s;
  int i = 0;
  while (1) {
    s.f(i, i);
    ++i;
    sleep(1);
    // break;
  }
  return 0;
}

clayout can translate S into a C structure with the same memory layout:

# clayout will generate struct.h, struct.c
$ clayout -i ${binary path} -o struct S
// struct.h
// Generated by hidva/clayout! 大吉大利!
#pragma once
#include <linux/types.h>
struct HidvaStruct2 {
  void** __mem1;
  int x1;
} __attribute__((__packed__));


struct S {
  struct HidvaStruct2 __parent0;
  int x;
} __attribute__((__packed__));

So you can easily write the following bpftrace script:

#include "struct.h"

u:/apsara/zhanyi.ww/tmp/bphtrace/x/trace:_ZN1S1fEii {
  printf("output from bpftrace: ret=%p this.x=%d y=%d z=%d\n", (int32*)arg0, ((struct S*)arg1)->x, arg2, arg3)
}
$ bpftrace  -c ./trace t.bt
Attaching 1 probe...
output from a.out: this.x=0 y=0 z=0
output from bpftrace: ret=0x7ffff3044610 this.x=0 y=0 z=0
output from a.out: this.x=0 y=1 z=1
output from bpftrace: ret=0x7ffff3044610 this.x=0 y=1 z=1

Please note that you may intuitively think that the layout of S is as follows:

struct X {
  void** __mem1;
  int x1;
}

struct S {
  struct X __parent0;
  int x;
}

But actually it is wrong! S::x will reuse the padding part of X in C++!

multi input

clayout supports multiple input files, and type references across files.

// x.h
struct X {
  virtual ~X();

  int x1;
};

struct S : public X {
  S();

  S(const S &other);

  S f(int y, int z);

  int x;
};

// X.cc
#include <stdio.h>
#include "x.h"

X::~X() {}

S::S(): x(0) {}

S::S(const S &other) : x(other.x) {}

S S::f(int y, int z) {
  printf("output from a.out: this.x=%d y=%d z=%d\n", x, y, z);
  x += (y + z);
  return *this;
}

// trace.cc
#include <unistd.h>
#include "x.h"

int main(int argc, char **argv) {
  S s;
  int i = 0;
  while (1) {
    s.f(i, i);
    ++i;
    sleep(1);
  }
  return 0;
}
$ clang++ -fPIC -shared -g -O0 X.cc -o libzh_x.so
$ clang++ -g -O0 trace.cc -o trace -L. -lzh_x

Because of -fstandalone-debug, the trace binary file does not contain any debugging information of X:

$ readelf --debug-dump=info trace
 <1><fc>: Abbrev Number: 13 (DW_TAG_structure_type)
    <fd>   DW_AT_name        : X
    <ff>   DW_AT_declaration : 1

Because there is no debugging information of X in the trace binary file, a placeholder __u8 __unknown_type1[12] is used.

$ clayout -i trace -o output S
// output.h
// Generated by hidva/clayout! 大吉大利!
#pragma once
#include <linux/types.h>

struct S {
  __u8 __unknown_type1[12];
  int x;
} __attribute__((__packed__));

We can use multi input file to get the detail of X:

$ clayout -i trace -i libzh_x.so -o output S
// output.h
// Generated by hidva/clayout! 大吉大利!
#pragma once
#include <linux/types.h>
struct HidvaStruct2 {
  void** __mem1;
  int x1;
} __attribute__((__packed__));


struct S {
  struct HidvaStruct2 __parent0;
  int x;
} __attribute__((__packed__));

clayout's People

Contributors

hidva avatar philipc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ioperations neeky

clayout's Issues

duplicate type debuginfo

// tyname=::std::_Rb_tree_node_base tyidx=TypeIndex { input_id: 0, typoff: TypeOffset(60984357) }
// tyname=::std::_Rb_tree_node_base tyidx=TypeIndex { input_id: 0, typoff: TypeOffset(28125307) }
// tyname=::std::_Rb_tree_node_base tyidx=TypeIndex { input_id: 0, typoff: TypeOffset(69763994) }
// tyname=::std::_Rb_tree_node_base tyidx=TypeIndex { input_id: 0, typoff: TypeOffset(22768814) }
// tyname=::std::_Rb_tree_node_base tyidx=TypeIndex { input_id: 0, typoff: TypeOffset(56430823) }
// tyname=::std::_Rb_tree_node_base tyidx=TypeIndex { input_id: 0, typoff: TypeOffset(31413305) }
$ grep -F 'char_Array16' 12.h  |grep -F typedef
typedef char char_Array16[16];
typedef char char_Array16_1[16];
typedef char char_Array16_2[16];
typedef char char_Array16_3[16];
typedef char char_Array16_4[16];
typedef char char_Array16_5[16];
typedef char char_Array16_6[16];
typedef char char_Array16_7[16];
typedef char char_Array16_8[16];
typedef char char_Array16_9[16];
typedef char char_Array16_10[16];
typedef char char_Array16_11[16];
typedef char char_Array16_12[16];

pretty type name

union u {
	long u_l;
	char u_c;
	long u_b: 2;
	S1 s1;
};

will be translated to

struct HidvaStruct1 {  // #2
  long int l;
  char ch;
} __attribute__((__packed__));


// tyidx=TypeIndex { input_id: 0, typoff: TypeOffset(92) } tyname=::u
union HidvaUnion3 {  // #1
  long int u_l;
  char u_c;
  __u8 __bitfield0[1];  
  struct HidvaStruct1 s1;  // #2
  __u8 __HIDVA_dont_use2[16];
} __attribute__((__packed__));

bitfield support

union u {
	long u_l;
	char u_c;
	long u_b: 2;
	S1 s1;
};

will be translated to:

union HidvaUnion3 {
  long int u_l;
  char u_c;
  __u8 __bitfield0[1];   // #1
  struct HidvaStruct1 s1;
  __u8 __HIDVA_dont_use2[16];
} __attribute__((__packed__));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.