<div dir="ltr">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?</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 25, 2018 at 5:46 PM Fangrui Song via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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>
</blockquote></div>