Skip to content

Commit 933d872

Browse files
authored
[llvm][utils] Improve the StringRef summary formatter (#151594)
Improve the `StringRef` summary formatter in the following ways: * inherit adherence to the `target.max-string-summary-length` setting * support non-printable bytes (such as null bytes, and any other binary data) With the previous implementation, some non-printable bytes would raise a Python exception.
1 parent 545cda6 commit 933d872

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

llvm/utils/lldbDataFormatters.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import collections
1010
import lldb
11-
import json
1211

1312

1413
def __lldb_init_module(debugger, internal_dict):
@@ -192,28 +191,19 @@ def SmallStringSummaryProvider(valobj, internal_dict):
192191

193192

194193
def StringRefSummaryProvider(valobj, internal_dict):
195-
if valobj.GetNumChildren() == 2:
196-
# StringRef's are also used to point at binary blobs in memory,
197-
# so filter out suspiciously long strings.
198-
max_length = 1024
199-
actual_length = valobj.GetChildAtIndex(1).GetValueAsUnsigned()
200-
truncate = actual_length > max_length
201-
length = min(max_length, actual_length)
202-
if length == 0:
203-
return '""'
204-
205-
data = valobj.GetChildAtIndex(0).GetPointeeData(item_count=length)
206-
error = lldb.SBError()
207-
string = data.ReadRawData(error, 0, data.GetByteSize()).decode()
208-
if error.Fail():
209-
return "<error: %s>" % error.description
210-
211-
# json.dumps conveniently escapes the string for us.
212-
string = json.dumps(string)
213-
if truncate:
214-
string += "..."
215-
return string
216-
return None
194+
data_pointer = valobj.GetChildMemberWithName("Data")
195+
length = valobj.GetChildMemberWithName("Length").unsigned
196+
if data_pointer.unsigned == 0 or length == 0:
197+
return '""'
198+
199+
data = data_pointer.deref
200+
# Get a char[N] type, from the underlying char type.
201+
array_type = data.type.GetArrayType(length)
202+
# Cast the char* string data to a char[N] array.
203+
char_array = data.Cast(array_type)
204+
# Use the builtin summary for its support of max-string-summary-length and
205+
# display of non-printable bytes.
206+
return char_array.summary
217207

218208

219209
def ConstStringSummaryProvider(valobj, internal_dict):

0 commit comments

Comments
 (0)