[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.
This commit is contained in:
parent
545cda649e
commit
933d8723b0
@ -8,7 +8,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
import lldb
|
import lldb
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
def __lldb_init_module(debugger, internal_dict):
|
def __lldb_init_module(debugger, internal_dict):
|
||||||
@ -192,28 +191,19 @@ def SmallStringSummaryProvider(valobj, internal_dict):
|
|||||||
|
|
||||||
|
|
||||||
def StringRefSummaryProvider(valobj, internal_dict):
|
def StringRefSummaryProvider(valobj, internal_dict):
|
||||||
if valobj.GetNumChildren() == 2:
|
data_pointer = valobj.GetChildMemberWithName("Data")
|
||||||
# StringRef's are also used to point at binary blobs in memory,
|
length = valobj.GetChildMemberWithName("Length").unsigned
|
||||||
# so filter out suspiciously long strings.
|
if data_pointer.unsigned == 0 or length == 0:
|
||||||
max_length = 1024
|
|
||||||
actual_length = valobj.GetChildAtIndex(1).GetValueAsUnsigned()
|
|
||||||
truncate = actual_length > max_length
|
|
||||||
length = min(max_length, actual_length)
|
|
||||||
if length == 0:
|
|
||||||
return '""'
|
return '""'
|
||||||
|
|
||||||
data = valobj.GetChildAtIndex(0).GetPointeeData(item_count=length)
|
data = data_pointer.deref
|
||||||
error = lldb.SBError()
|
# Get a char[N] type, from the underlying char type.
|
||||||
string = data.ReadRawData(error, 0, data.GetByteSize()).decode()
|
array_type = data.type.GetArrayType(length)
|
||||||
if error.Fail():
|
# Cast the char* string data to a char[N] array.
|
||||||
return "<error: %s>" % error.description
|
char_array = data.Cast(array_type)
|
||||||
|
# Use the builtin summary for its support of max-string-summary-length and
|
||||||
# json.dumps conveniently escapes the string for us.
|
# display of non-printable bytes.
|
||||||
string = json.dumps(string)
|
return char_array.summary
|
||||||
if truncate:
|
|
||||||
string += "..."
|
|
||||||
return string
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def ConstStringSummaryProvider(valobj, internal_dict):
|
def ConstStringSummaryProvider(valobj, internal_dict):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user