• Yonghong Song's avatar
    tools: bpftool: support pretty print with kind_flag set · 8772c8bc
    Yonghong Song authored
    The following example shows map pretty print with structures
    which include bitfield members.
    
      enum A { A1, A2, A3, A4, A5 };
      typedef enum A ___A;
      struct tmp_t {
           char a1:4;
           int  a2:4;
           int  :4;
           __u32 a3:4;
           int b;
           ___A b1:4;
           enum A b2:4;
      };
      struct bpf_map_def SEC("maps") tmpmap = {
           .type = BPF_MAP_TYPE_ARRAY,
           .key_size = sizeof(__u32),
           .value_size = sizeof(struct tmp_t),
           .max_entries = 1,
      };
      BPF_ANNOTATE_KV_PAIR(tmpmap, int, struct tmp_t);
    
    and the following map update in the bpf program:
    
      key = 0;
      struct tmp_t t = {};
      t.a1 = 2;
      t.a2 = 4;
      t.a3 = 6;
      t.b = 7;
      t.b1 = 8;
      t.b2 = 10;
      bpf_map_update_elem(&tmpmap, &key, &t, 0);
    
    With this patch, I am able to print out the map values
    correctly with this patch:
    bpftool map dump id 187
      [{
            "key": 0,
            "value": {
                "a1": 0x2,
                "a2": 0x4,
                "a3": 0x6,
                "b": 7,
                "b1": 0x8,
                "b2": 0xa
            }
        }
      ]
    
    Previously, if a function prototype argument has a typedef
    type, the prototype is not printed since
    function __btf_dumper_type_only() bailed out with error
    if the type is a typedef. This commit corrected this
    behavior by printing out typedef properly.
    
    The following example shows forward type and
    typedef type can be properly printed in function prototype
    with modified test_btf_haskv.c.
    
      struct t;
      union  u;
    
      __attribute__((noinline))
      static int test_long_fname_1(struct dummy_tracepoint_args *arg,
                                   struct t *p1, union u *p2,
                                   __u32 unused)
      ...
      int _dummy_tracepoint(struct dummy_tracepoint_args *arg) {
        return test_long_fname_1(arg, 0, 0, 0);
      }
    
      $ bpftool p d xlated id 24
      ...
      int test_long_fname_1(struct dummy_tracepoint_args * arg,
                            struct t * p1, union u * p2,
                            __u32 unused)
      ...
    Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
    Signed-off-by: default avatarYonghong Song <yhs@fb.com>
    Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
    8772c8bc
btf_dumper.c 11.7 KB