@@ -213,8 +213,9 @@ static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
213
213
jsonw_end_object (json_wtr );
214
214
}
215
215
216
- static void print_entry_error (struct bpf_map_info * info , unsigned char * key ,
217
- const char * error_msg )
216
+ static void
217
+ print_entry_error_msg (struct bpf_map_info * info , unsigned char * key ,
218
+ const char * error_msg )
218
219
{
219
220
int msg_size = strlen (error_msg );
220
221
bool single_line , break_names ;
@@ -232,6 +233,40 @@ static void print_entry_error(struct bpf_map_info *info, unsigned char *key,
232
233
printf ("\n" );
233
234
}
234
235
236
+ static void
237
+ print_entry_error (struct bpf_map_info * map_info , void * key , int lookup_errno )
238
+ {
239
+ /* For prog_array maps or arrays of maps, failure to lookup the value
240
+ * means there is no entry for that key. Do not print an error message
241
+ * in that case.
242
+ */
243
+ if (map_is_map_of_maps (map_info -> type ) ||
244
+ map_is_map_of_progs (map_info -> type ))
245
+ return ;
246
+
247
+ if (json_output ) {
248
+ jsonw_start_object (json_wtr ); /* entry */
249
+ jsonw_name (json_wtr , "key" );
250
+ print_hex_data_json (key , map_info -> key_size );
251
+ jsonw_name (json_wtr , "value" );
252
+ jsonw_start_object (json_wtr ); /* error */
253
+ jsonw_string_field (json_wtr , "error" , strerror (lookup_errno ));
254
+ jsonw_end_object (json_wtr ); /* error */
255
+ jsonw_end_object (json_wtr ); /* entry */
256
+ } else {
257
+ const char * msg = NULL ;
258
+
259
+ if (lookup_errno == ENOENT )
260
+ msg = "<no entry>" ;
261
+ else if (lookup_errno == ENOSPC &&
262
+ map_info -> type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY )
263
+ msg = "<cannot read>" ;
264
+
265
+ print_entry_error_msg (map_info , key ,
266
+ msg ? : strerror (lookup_errno ));
267
+ }
268
+ }
269
+
235
270
static void print_entry_plain (struct bpf_map_info * info , unsigned char * key ,
236
271
unsigned char * value )
237
272
{
@@ -713,56 +748,23 @@ static int dump_map_elem(int fd, void *key, void *value,
713
748
struct bpf_map_info * map_info , struct btf * btf ,
714
749
json_writer_t * btf_wtr )
715
750
{
716
- int num_elems = 0 ;
717
- int lookup_errno ;
718
-
719
- if (!bpf_map_lookup_elem (fd , key , value )) {
720
- if (json_output ) {
721
- print_entry_json (map_info , key , value , btf );
722
- } else {
723
- if (btf ) {
724
- struct btf_dumper d = {
725
- .btf = btf ,
726
- .jw = btf_wtr ,
727
- .is_plain_text = true,
728
- };
729
-
730
- do_dump_btf (& d , map_info , key , value );
731
- } else {
732
- print_entry_plain (map_info , key , value );
733
- }
734
- num_elems ++ ;
735
- }
736
- return num_elems ;
751
+ if (bpf_map_lookup_elem (fd , key , value )) {
752
+ print_entry_error (map_info , key , errno );
753
+ return -1 ;
737
754
}
738
755
739
- /* lookup error handling */
740
- lookup_errno = errno ;
741
-
742
- if (map_is_map_of_maps (map_info -> type ) ||
743
- map_is_map_of_progs (map_info -> type ))
744
- return 0 ;
745
-
746
756
if (json_output ) {
747
- jsonw_start_object (json_wtr );
748
- jsonw_name (json_wtr , "key" );
749
- print_hex_data_json (key , map_info -> key_size );
750
- jsonw_name (json_wtr , "value" );
751
- jsonw_start_object (json_wtr );
752
- jsonw_string_field (json_wtr , "error" , strerror (lookup_errno ));
753
- jsonw_end_object (json_wtr );
754
- jsonw_end_object (json_wtr );
755
- } else {
756
- const char * msg = NULL ;
757
-
758
- if (lookup_errno == ENOENT )
759
- msg = "<no entry>" ;
760
- else if (lookup_errno == ENOSPC &&
761
- map_info -> type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY )
762
- msg = "<cannot read>" ;
757
+ print_entry_json (map_info , key , value , btf );
758
+ } else if (btf ) {
759
+ struct btf_dumper d = {
760
+ .btf = btf ,
761
+ .jw = btf_wtr ,
762
+ .is_plain_text = true,
763
+ };
763
764
764
- print_entry_error (map_info , key ,
765
- msg ? : strerror (lookup_errno ));
765
+ do_dump_btf (& d , map_info , key , value );
766
+ } else {
767
+ print_entry_plain (map_info , key , value );
766
768
}
767
769
768
770
return 0 ;
@@ -873,7 +875,8 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
873
875
err = 0 ;
874
876
break ;
875
877
}
876
- num_elems += dump_map_elem (fd , key , value , info , btf , wtr );
878
+ if (!dump_map_elem (fd , key , value , info , btf , wtr ))
879
+ num_elems ++ ;
877
880
prev_key = key ;
878
881
}
879
882
0 commit comments