Re: [PATCH bpf-next 2/4] bpftool: Print as a string for char array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Mar 15, 2020 at 5:57 PM Martin KaFai Lau <kafai@xxxxxx> wrote:
>
> A char[] is currently printed as an integer array.
> This patch will print it as a string when
> 1) The array element type is an one byte int
> 2) The array element type has a BTF_INT_CHAR encoding or
>    the array element type's name is "char"
> 3) All characters is between (0x1f, 0x7f) and it is terminated
>    by a null character.
>
> Signed-off-by: Martin KaFai Lau <kafai@xxxxxx>
> ---
>  tools/bpf/bpftool/btf_dumper.c | 41 ++++++++++++++++++++++++++++++++++
>  1 file changed, 41 insertions(+)
>
> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
> index 57bd6c0fafc9..1d2d8d2cedea 100644
> --- a/tools/bpf/bpftool/btf_dumper.c
> +++ b/tools/bpf/bpftool/btf_dumper.c
> @@ -77,6 +77,42 @@ static void btf_dumper_enum(const struct btf_dumper *d,
>         jsonw_int(d->jw, value);
>  }
>
> +static bool is_str_array(const struct btf *btf, const struct btf_array *arr,
> +                        const char *s)
> +{
> +       const struct btf_type *elem_type;
> +       const char *end_s;
> +
> +       if (!arr->nelems)
> +               return false;
> +
> +       elem_type = btf__type_by_id(btf, arr->type);
> +       /* Not skipping typedef.  typedef to char does not count as
> +        * a string now.
> +        */
> +       while (elem_type && btf_is_mod(elem_type))
> +               elem_type = btf__type_by_id(btf, elem_type->type);
> +
> +       if (!elem_type || !btf_is_int(elem_type) || elem_type->size != 1)
> +               return false;
> +
> +       if (btf_int_encoding(elem_type) != BTF_INT_CHAR &&
> +           strcmp("char", btf__name_by_offset(btf, elem_type->name_off)))
> +               return false;
> +
> +       end_s = s + arr->nelems;
> +       while (s < end_s) {
> +               if (!*s)
> +                       return true;
> +               if (*s <= 0x1f || *s >= 0x7f)
> +                       return false;
> +               s++;
> +       }
> +
> +       /* '\0' is not found */
> +       return false;
> +}
> +
>  static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
>                             const void *data)
>  {
> @@ -86,6 +122,11 @@ static int btf_dumper_array(const struct btf_dumper *d, __u32 type_id,
>         int ret = 0;
>         __u32 i;
>
> +       if (is_str_array(d->btf, arr, data)) {
> +               jsonw_string(d->jw, data);
> +               return 0;
> +       }
> +

Looks good, but curious how the string that contains ' or " will be
output in json? Will it be escaped properly or will result in
malformed JSON?

Acked-by: Andrii Nakryiko <andriin@xxxxxx>

>         elem_size = btf__resolve_size(d->btf, arr->type);
>         if (elem_size < 0)
>                 return elem_size;
> --
> 2.17.1
>



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux