@@ -204,6 +204,44 @@ static unsigned long long adjust_signedness(unsigned long long value_int, int si
204
204
return (value_int & value_mask ) | ~value_mask ;
205
205
}
206
206
207
+ static int string_set_value (struct bt_ctf_field * field , const char * string )
208
+ {
209
+ char * buffer = NULL ;
210
+ size_t len = strlen (string ), i , p ;
211
+ int err ;
212
+
213
+ for (i = p = 0 ; i < len ; i ++ , p ++ ) {
214
+ if (isprint (string [i ])) {
215
+ if (!buffer )
216
+ continue ;
217
+ buffer [p ] = string [i ];
218
+ } else {
219
+ char numstr [5 ];
220
+
221
+ snprintf (numstr , sizeof (numstr ), "\\x%02x" ,
222
+ (unsigned int )(string [i ]) & 0xff );
223
+
224
+ if (!buffer ) {
225
+ buffer = zalloc (i + (len - i ) * 4 + 2 );
226
+ if (!buffer ) {
227
+ pr_err ("failed to set unprintable string '%s'\n" , string );
228
+ return bt_ctf_field_string_set_value (field , "UNPRINTABLE-STRING" );
229
+ }
230
+ if (i > 0 )
231
+ strncpy (buffer , string , i );
232
+ }
233
+ strncat (buffer + p , numstr , 4 );
234
+ p += 3 ;
235
+ }
236
+ }
237
+
238
+ if (!buffer )
239
+ return bt_ctf_field_string_set_value (field , string );
240
+ err = bt_ctf_field_string_set_value (field , buffer );
241
+ free (buffer );
242
+ return err ;
243
+ }
244
+
207
245
static int add_tracepoint_field_value (struct ctf_writer * cw ,
208
246
struct bt_ctf_event_class * event_class ,
209
247
struct bt_ctf_event * event ,
@@ -270,8 +308,7 @@ static int add_tracepoint_field_value(struct ctf_writer *cw,
270
308
}
271
309
272
310
if (flags & FIELD_IS_STRING )
273
- ret = bt_ctf_field_string_set_value (field ,
274
- data + offset + i * len );
311
+ ret = string_set_value (field , data + offset + i * len );
275
312
else {
276
313
unsigned long long value_int ;
277
314
0 commit comments