2018-12-12 19:59:26 -08:00
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2017-10-23 09:24:05 -07:00
/*
* Simple streaming JSON writer
*
* This takes care of the annoying bits of JSON syntax like the commas
* after elements
*
* Authors : Stephen Hemminger < stephen @ networkplumber . org >
*/
# ifndef _JSON_WRITER_H_
# define _JSON_WRITER_H_
# include <stdbool.h>
# include <stdint.h>
tools: bpftool: add JSON output for `bpftool prog dump xlated *` command
Add a new printing function to dump translated eBPF instructions as
JSON. As for plain output, opcodes are printed only on request (when
`opcodes` is provided on the command line).
The disassembled output is generated by the same code that is used by
the kernel verifier.
Example output:
$ bpftool --json --pretty prog dump xlated id 1
[{
"disasm": "(bf) r6 = r1"
},{
"disasm": "(61) r7 = *(u32 *)(r6 +16)"
},{
"disasm": "(95) exit"
}
]
$ bpftool --json --pretty prog dump xlated id 1 opcodes
[{
"disasm": "(bf) r6 = r1",
"opcodes": {
"code": "0xbf",
"src_reg": "0x1",
"dst_reg": "0x6",
"off": ["0x00","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
},{
"disasm": "(61) r7 = *(u32 *)(r6 +16)",
"opcodes": {
"code": "0x61",
"src_reg": "0x6",
"dst_reg": "0x7",
"off": ["0x10","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
},{
"disasm": "(95) exit",
"opcodes": {
"code": "0x95",
"src_reg": "0x0",
"dst_reg": "0x0",
"off": ["0x00","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
}
]
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-10-23 09:24:10 -07:00
# include <stdarg.h>
tools: bpftool: json: Fix backslash escape typo in jsonw_puts
This is essentially a backport of iproute2's
commit ed54f76484b5 ("json: fix backslash escape typo in jsonw_puts")
Also added the stdio.h include in json_writer.h to be able to compile
and run the json_writer test as used below).
Before this fix:
$ gcc -D notused -D TEST -I../../include -o json_writer json_writer.c
json_writer.h
$ ./json_writer
{
"Vyatta": {
"url": "http://vyatta.com",
"downloads": 2000000,
"stock": 8.16,
"ARGV": [],
"empty": [],
"NIL": {},
"my_null": null,
"special chars": [
"slash": "/",
"newline": "\n",
"tab": "\t",
"ff": "\f",
"quote": "\"",
"tick": "'",
"backslash": "\n"
]
}
}
After:
$ gcc -D notused -D TEST -I../../include -o json_writer json_writer.c
json_writer.h
$ ./json_writer
{
"Vyatta": {
"url": "http://vyatta.com",
"downloads": 2000000,
"stock": 8.16,
"ARGV": [],
"empty": [],
"NIL": {},
"my_null": null,
"special chars": [
"slash": "/",
"newline": "\n",
"tab": "\t",
"ff": "\f",
"quote": "\"",
"tick": "'",
"backslash": "\\"
]
}
}
Signed-off-by: Manu Bretelle <chantr4@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20230329073002.2026563-1-chantr4@gmail.com
2023-03-29 00:30:02 -07:00
# include <stdio.h>
2019-08-15 15:32:20 +01:00
# include <linux/compiler.h>
2017-10-23 09:24:05 -07:00
/* Opaque class structure */
typedef struct json_writer json_writer_t ;
/* Create a new JSON stream */
json_writer_t * jsonw_new ( FILE * f ) ;
/* End output to JSON stream */
void jsonw_destroy ( json_writer_t * * self_p ) ;
/* Cause output to have pretty whitespace */
void jsonw_pretty ( json_writer_t * self , bool on ) ;
2020-09-15 16:45:42 -07:00
/* Reset separator to create new JSON */
void jsonw_reset ( json_writer_t * self ) ;
2017-10-23 09:24:05 -07:00
/* Add property name */
void jsonw_name ( json_writer_t * self , const char * name ) ;
/* Add value */
2019-08-15 15:32:20 +01:00
void __printf ( 2 , 0 ) jsonw_vprintf_enquote ( json_writer_t * self , const char * fmt ,
va_list ap ) ;
void __printf ( 2 , 3 ) jsonw_printf ( json_writer_t * self , const char * fmt , . . . ) ;
2017-10-23 09:24:05 -07:00
void jsonw_string ( json_writer_t * self , const char * value ) ;
void jsonw_bool ( json_writer_t * self , bool value ) ;
void jsonw_float ( json_writer_t * self , double number ) ;
void jsonw_float_fmt ( json_writer_t * self , const char * fmt , double num ) ;
void jsonw_uint ( json_writer_t * self , uint64_t number ) ;
void jsonw_hu ( json_writer_t * self , unsigned short number ) ;
void jsonw_int ( json_writer_t * self , int64_t number ) ;
void jsonw_null ( json_writer_t * self ) ;
void jsonw_lluint ( json_writer_t * self , unsigned long long int num ) ;
/* Useful Combinations of name and value */
void jsonw_string_field ( json_writer_t * self , const char * prop , const char * val ) ;
void jsonw_bool_field ( json_writer_t * self , const char * prop , bool value ) ;
void jsonw_float_field ( json_writer_t * self , const char * prop , double num ) ;
void jsonw_uint_field ( json_writer_t * self , const char * prop , uint64_t num ) ;
void jsonw_hu_field ( json_writer_t * self , const char * prop , unsigned short num ) ;
void jsonw_int_field ( json_writer_t * self , const char * prop , int64_t num ) ;
void jsonw_null_field ( json_writer_t * self , const char * prop ) ;
void jsonw_lluint_field ( json_writer_t * self , const char * prop ,
unsigned long long int num ) ;
void jsonw_float_field_fmt ( json_writer_t * self , const char * prop ,
const char * fmt , double val ) ;
/* Collections */
void jsonw_start_object ( json_writer_t * self ) ;
void jsonw_end_object ( json_writer_t * self ) ;
void jsonw_start_array ( json_writer_t * self ) ;
void jsonw_end_array ( json_writer_t * self ) ;
/* Override default exception handling */
typedef void ( jsonw_err_handler_fn ) ( const char * ) ;
# endif /* _JSON_WRITER_H_ */