bpf: btf: add btf print functionality
This consumes functionality exported in the previous patch. It does the main job of printing with BTF data. This is used in the following patch to provide a more readable output of a map's dump. It relies on json_writer to do json printing. Below is sample output where map keys are ints and values are of type struct A: typedef int int_type; enum E { E0, E1, }; struct B { int x; int y; }; struct A { int m; unsigned long long n; char o; int p[8]; int q[4][8]; enum E r; void *s; struct B t; const int u; int_type v; unsigned int w1: 3; unsigned int w2: 3; }; $ sudo bpftool map dump id 14 [{ "key": 0, "value": { "m": 1, "n": 2, "o": "c", "p": [15,16,17,18,15,16,17,18 ], "q": [[25,26,27,28,25,26,27,28 ],[35,36,37,38,35,36,37,38 ],[45,46,47,48,45,46,47,48 ],[55,56,57,58,55,56,57,58 ] ], "r": 1, "s": 0x7ffd80531cf8, "t": { "x": 5, "y": 10 }, "u": 100, "v": 20, "w1": 0x7, "w2": 0x3 } } ] This patch uses json's {} and [] to imply struct/union and array. More explicit information can be added later. For example, a command line option can be introduced to print whether a key or value is struct or union, name of a struct etc. This will however come at the expense of duplicating info when, for example, printing an array of structs. enums are printed as ints without their names. Signed-off-by: Okash Khawaja <osk@fb.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:

committed by
Daniel Borkmann

parent
92b57121ca
commit
b12d6ec097
@@ -150,4 +150,19 @@ unsigned int get_page_size(void);
|
||||
unsigned int get_possible_cpus(void);
|
||||
const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino);
|
||||
|
||||
struct btf_dumper {
|
||||
const struct btf *btf;
|
||||
json_writer_t *jw;
|
||||
bool is_plain_text;
|
||||
};
|
||||
|
||||
/* btf_dumper_type - print data along with type information
|
||||
* @d: an instance containing context for dumping types
|
||||
* @type_id: index in btf->types array. this points to the type to be dumped
|
||||
* @data: pointer the actual data, i.e. the values to be printed
|
||||
*
|
||||
* Returns zero on success and negative error code otherwise
|
||||
*/
|
||||
int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
|
||||
const void *data);
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user