@@ -879,6 +879,110 @@ static void test_btf_dump_var_data(struct btf *btf, struct btf_dump *d,
879
879
"static int bpf_cgrp_storage_busy = (int)2" , 2 );
880
880
}
881
881
882
+ struct btf_dump_string_ctx {
883
+ struct btf * btf ;
884
+ struct btf_dump * d ;
885
+ char * str ;
886
+ struct btf_dump_type_data_opts * opts ;
887
+ int array_id ;
888
+ };
889
+
890
+ static int btf_dump_one_string (struct btf_dump_string_ctx * ctx ,
891
+ char * ptr , size_t ptr_sz ,
892
+ const char * expected_val )
893
+ {
894
+ size_t type_sz ;
895
+ int ret ;
896
+
897
+ ctx -> str [0 ] = '\0' ;
898
+ type_sz = btf__resolve_size (ctx -> btf , ctx -> array_id );
899
+ ret = btf_dump__dump_type_data (ctx -> d , ctx -> array_id , ptr , ptr_sz , ctx -> opts );
900
+ if (type_sz <= ptr_sz ) {
901
+ if (!ASSERT_EQ (ret , type_sz , "failed/unexpected type_sz" ))
902
+ return - EINVAL ;
903
+ } else {
904
+ if (!ASSERT_EQ (ret , - E2BIG , "failed to return -E2BIG" ))
905
+ return - EINVAL ;
906
+ }
907
+ if (!ASSERT_STREQ (ctx -> str , expected_val , "ensure expected/actual match" ))
908
+ return - EFAULT ;
909
+ return 0 ;
910
+ }
911
+
912
+ static void btf_dump_strings (struct btf_dump_string_ctx * ctx )
913
+ {
914
+ struct btf_dump_type_data_opts * opts = ctx -> opts ;
915
+
916
+ opts -> emit_strings = true;
917
+
918
+ opts -> compact = true;
919
+ opts -> emit_zeroes = false;
920
+
921
+ opts -> skip_names = false;
922
+ btf_dump_one_string (ctx , "foo" , 4 , "(char[4])\"foo\"" );
923
+
924
+ opts -> skip_names = true;
925
+ btf_dump_one_string (ctx , "foo" , 4 , "\"foo\"" );
926
+
927
+ /* This should have no effect. */
928
+ opts -> emit_zeroes = false;
929
+ btf_dump_one_string (ctx , "foo" , 4 , "\"foo\"" );
930
+
931
+ /* This should have no effect. */
932
+ opts -> compact = false;
933
+ btf_dump_one_string (ctx , "foo" , 4 , "\"foo\"" );
934
+
935
+ /* Non-printable characters come out as hex. */
936
+ btf_dump_one_string (ctx , "fo\xff" , 4 , "\"fo\\xff\"" );
937
+ btf_dump_one_string (ctx , "fo\x7" , 4 , "\"fo\\x07\"" );
938
+
939
+ /* Should get printed properly even though there's no NUL. */
940
+ char food [4 ] = { 'f' , 'o' , 'o' , 'd' };
941
+
942
+ btf_dump_one_string (ctx , food , 4 , "\"food\"" );
943
+
944
+ /* The embedded NUL should terminate the string. */
945
+ char embed [4 ] = { 'f' , 'o' , '\0' , 'd' };
946
+
947
+ btf_dump_one_string (ctx , embed , 4 , "\"fo\"" );
948
+ }
949
+
950
+ static void test_btf_dump_string_data (void )
951
+ {
952
+ struct test_ctx t = {};
953
+ char str [STRSIZE ];
954
+ struct btf_dump * d ;
955
+ DECLARE_LIBBPF_OPTS (btf_dump_type_data_opts , opts );
956
+ struct btf_dump_string_ctx ctx ;
957
+ int char_id , int_id , array_id ;
958
+
959
+ if (test_ctx__init (& t ))
960
+ return ;
961
+
962
+ d = btf_dump__new (t .btf , btf_dump_snprintf , str , NULL );
963
+ if (!ASSERT_OK_PTR (d , "could not create BTF dump" ))
964
+ return ;
965
+
966
+ /* Generate BTF for a four-element char array. */
967
+ char_id = btf__add_int (t .btf , "char" , 1 , BTF_INT_CHAR );
968
+ ASSERT_EQ (char_id , 1 , "char_id" );
969
+ int_id = btf__add_int (t .btf , "int" , 4 , BTF_INT_SIGNED );
970
+ ASSERT_EQ (int_id , 2 , "int_id" );
971
+ array_id = btf__add_array (t .btf , int_id , char_id , 4 );
972
+ ASSERT_EQ (array_id , 3 , "array_id" );
973
+
974
+ ctx .btf = t .btf ;
975
+ ctx .d = d ;
976
+ ctx .str = str ;
977
+ ctx .opts = & opts ;
978
+ ctx .array_id = array_id ;
979
+
980
+ btf_dump_strings (& ctx );
981
+
982
+ btf_dump__free (d );
983
+ test_ctx__free (& t );
984
+ }
985
+
882
986
static void test_btf_datasec (struct btf * btf , struct btf_dump * d , char * str ,
883
987
const char * name , const char * expected_val ,
884
988
void * data , size_t data_sz )
@@ -970,6 +1074,8 @@ void test_btf_dump() {
970
1074
test_btf_dump_struct_data (btf , d , str );
971
1075
if (test__start_subtest ("btf_dump: var_data" ))
972
1076
test_btf_dump_var_data (btf , d , str );
1077
+ if (test__start_subtest ("btf_dump: string_data" ))
1078
+ test_btf_dump_string_data ();
973
1079
btf_dump__free (d );
974
1080
btf__free (btf );
975
1081
0 commit comments