<div dir="ltr"><div dir="ltr">On Wed, Nov 3, 2021 at 3:02 PM Fāng-ruì Sòng <<a href="mailto:maskray@google.com" target="_blank">maskray@google.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, Nov 3, 2021 at 2:23 PM David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<br>
><br>
> Hey Ray - do you recall what the motivation for this patch was/is? I ask because this breaks gdb's ability to truncate long strings (which means printing out data structures with StringRefs that refer to, for instance, the entire contents of an object/executable, or of a section in such a file - is unfortunate/locks up the debugger for ages printing things out) and it looks like gdb escapes unprintable characters itself, without the need to do this manually so far as I can tell?<br>
<br>
It's been 3 years so I cannot remember. If I made the change, likely<br>
because gdb at that time did not display strings correctly and this<br>
commit fixed the problem.<br>
It's possible that gdb's behavior changed or somehow the commit's<br>
behavior becomes harmful now.<br>
<br>
I think I do notice some string stuff does not look pretty, but<br>
haven't spent time investigating the problem.<br></blockquote><div><br>Sure sure - yeah, eventually did find the sort of problems this was addressing. Not sure why I didn't reproduce it earlier.<br><br>In any case the libstdc++ pretty printer works around/addresses this by using lazy_string/gdb.LazyString - which mostly addresses our uses too, though Twine still presents a problem (std::string's pretty printer can produce a lazy string and I don't know how to concatenate that with a normal string (or another lazy string, for that matter) - I sent mail to the gdb list about that, maybe they'll have some suggestions).<br><br>Committed <a href="https://github.com/llvm/llvm-project/commit/def232915f81ce391ef54b8a7af85c6729a6d359">https://github.com/llvm/llvm-project/commit/def232915f81ce391ef54b8a7af85c6729a6d359</a> and <a href="https://github.com/llvm/llvm-project/commit/baa820c510d027911b464ee018e15217d09568da">https://github.com/llvm/llvm-project/commit/baa820c510d027911b464ee018e15217d09568da</a> which should hopefully help things a bit.<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> On Mon, Jun 25, 2018 at 5:46 PM Fangrui Song via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br>
>><br>
>> Author: maskray<br>
>> Date: Mon Jun 25 17:41:49 2018<br>
>> New Revision: 335561<br>
>><br>
>> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=335561&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=335561&view=rev</a><br>
>> Log:<br>
>> [gdb] Escape unprintable bytes in SmallString and StringRef<br>
>><br>
>> Modified:<br>
>> llvm/trunk/utils/gdb-scripts/prettyprinters.py<br>
>><br>
>> Modified: llvm/trunk/utils/gdb-scripts/prettyprinters.py<br>
>> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/gdb-scripts/prettyprinters.py?rev=335561&r1=335560&r2=335561&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/gdb-scripts/prettyprinters.py?rev=335561&r1=335560&r2=335561&view=diff</a><br>
>> ==============================================================================<br>
>> --- llvm/trunk/utils/gdb-scripts/prettyprinters.py (original)<br>
>> +++ llvm/trunk/utils/gdb-scripts/prettyprinters.py Mon Jun 25 17:41:49 2018<br>
>> @@ -11,6 +11,8 @@ class Iterator:<br>
>> def children(self):<br>
>> return self<br>
>><br>
>> +def escape_bytes(val, l):<br>
>> + return '"' + val.string(encoding='Latin-1', length=l).encode('unicode_escape').decode() + '"'<br>
>><br>
>> class SmallStringPrinter:<br>
>> """Print an llvm::SmallString object."""<br>
>> @@ -21,10 +23,7 @@ class SmallStringPrinter:<br>
>> def to_string(self):<br>
>> begin = self.val['BeginX']<br>
>> end = self.val['EndX']<br>
>> - return begin.cast(gdb.lookup_type("char").pointer()).string(length = end - begin)<br>
>> -<br>
>> - def display_hint (self):<br>
>> - return 'string'<br>
>> + return escape_bytes(begin.cast(gdb.lookup_type('char').pointer()), end - begin)<br>
>><br>
>> class StringRefPrinter:<br>
>> """Print an llvm::StringRef object."""<br>
>> @@ -33,10 +32,7 @@ class StringRefPrinter:<br>
>> self.val = val<br>
>><br>
>> def to_string(self):<br>
>> - return self.val['Data'].string(encoding='Latin-1', length=self.val['Length'])<br>
>> -<br>
>> - def display_hint (self):<br>
>> - return 'string'<br>
>> + return escape_bytes(self.val['Data'], self.val['Length'])<br>
>><br>
>> class SmallVectorPrinter:<br>
>> """Print an llvm::SmallVector object."""<br>
>><br>
>><br>
>> _______________________________________________<br>
>> llvm-commits mailing list<br>
>> <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
>> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
<br>
<br>
<br>
-- <br>
宋方睿<br>
</blockquote></div></div>