[Lldb-commits] [lldb] r249597 - [lldb-mi] Fix evaluation of strings containing characters from non-ascii range

Dawn Perchik via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 7 12:55:33 PDT 2015


Author: dperchik
Date: Wed Oct  7 14:55:33 2015
New Revision: 249597

URL: http://llvm.org/viewvc/llvm-project?rev=249597&view=rev
Log:
[lldb-mi] Fix evaluation of strings containing characters from non-ascii range

If a string contained characters outside the ASCII range, lldb-mi would
print them as hexadecimal codes.  This patch fixes this behaviour by
converting to UTF-8 instead, by having lldb-mi use registered type
summary providers, when they are available.  This patch also fixes
incorrect evaluation of some composite types, like std::string, by
having them use a type registered type summary provider.

Based on patch from evgeny.leviant at gmail.com
Reviewed by: ki.stfu, granata.enrico, clayborg
Subscribers: lldb-commits
Differential Revision: http://reviews.llvm.org/D13058

Modified:
    lldb/trunk/include/lldb/API/SBTypeSummary.h
    lldb/trunk/source/API/SBTypeSummary.cpp
    lldb/trunk/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py
    lldb/trunk/test/tools/lldb-mi/variable/TestMiVar.py
    lldb/trunk/test/tools/lldb-mi/variable/main.cpp
    lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
    lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.h

Modified: lldb/trunk/include/lldb/API/SBTypeSummary.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBTypeSummary.h?rev=249597&r1=249596&r2=249597&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBTypeSummary.h (original)
+++ lldb/trunk/include/lldb/API/SBTypeSummary.h Wed Oct  7 14:55:33 2015
@@ -126,6 +126,9 @@ namespace lldb {
         operator = (const lldb::SBTypeSummary &rhs);
         
         bool
+        DoesPrintValue (lldb::SBValue value);
+        
+        bool
         IsEqualTo (lldb::SBTypeSummary &rhs);
 
         bool

Modified: lldb/trunk/source/API/SBTypeSummary.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBTypeSummary.cpp?rev=249597&r1=249596&r2=249597&view=diff
==============================================================================
--- lldb/trunk/source/API/SBTypeSummary.cpp (original)
+++ lldb/trunk/source/API/SBTypeSummary.cpp Wed Oct  7 14:55:33 2015
@@ -8,9 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/API/SBTypeSummary.h"
-
 #include "lldb/API/SBStream.h"
-
+#include "lldb/API/SBValue.h"
 #include "lldb/DataFormatters/DataVisualization.h"
 
 using namespace lldb;
@@ -284,6 +283,15 @@ SBTypeSummary::GetDescription (lldb::SBS
     }
 }
 
+bool
+SBTypeSummary::DoesPrintValue (lldb::SBValue value)
+{
+    if (!IsValid())
+        return false;
+    lldb::ValueObjectSP value_sp = value.GetSP();
+    return m_opaque_sp->DoesPrintValue(value_sp.get());
+}
+
 lldb::SBTypeSummary &
 SBTypeSummary::operator = (const lldb::SBTypeSummary &rhs)
 {

Modified: lldb/trunk/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py?rev=249597&r1=249596&r2=249597&view=diff
==============================================================================
--- lldb/trunk/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py (original)
+++ lldb/trunk/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py Wed Oct  7 14:55:33 2015
@@ -1,3 +1,4 @@
+#coding=utf8
 """
 Test lldb-mi -gdb-set and -gdb-show commands for 'print option-name'.
 """
@@ -10,6 +11,17 @@ class MiGdbSetShowTestCase(lldbmi_testca
 
     mydir = TestBase.compute_mydir(__file__)
 
+    # evaluates array when char-array-as-string is off
+    def eval_and_check_array(self, var, typ, length):
+        self.runCmd("-var-create - * %s" % var)
+        self.expect('\^done,name="var\d+",numchild="%d",value="\[%d\]",type="%s \[%d\]",thread-id="1",has_more="0"' % (length, length, typ, length))
+
+    # evaluates any type which can be represented as string of characters
+    def eval_and_match_string(self, var, value, typ):
+        value=value.replace("\\", "\\\\").replace("\"", "\\\"")
+        self.runCmd("-var-create - * " + var)
+        self.expect('\^done,name="var\d+",numchild="[0-9]+",value="%s",type="%s",thread-id="1",has_more="0"' % (value, typ))
+
     @lldbmi_test
     @skipIfWindows #llvm.org/pr24452: Get lldb-mi working on Windows
     @skipIfFreeBSD # llvm.org/pr22411: Failure presumably due to known thread races
@@ -35,29 +47,23 @@ class MiGdbSetShowTestCase(lldbmi_testca
         self.runCmd("-gdb-show print char-array-as-string")
         self.expect("\^done,value=\"off\"")
 
-        # Test that an char* is expanded to string when print char-array-as-string is "off"
-        self.runCmd("-var-create - * cp")
-        self.expect("\^done,name=\"var\d+\",numchild=\"1\",value=\"0x[0-9a-f]+ \\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char \*\",thread-id=\"1\",has_more=\"0\"")
-
-        # Test that an char[] isn't expanded to string when print char-array-as-string is "off"
-        self.runCmd("-var-create - * ca")
-        self.expect("\^done,name=\"var\d+\",numchild=\"10\",value=\"\[10\]\",type=\"const char \[10\]\",thread-id=\"1\",has_more=\"0\"")
-
-        # Test that an char16_t* is expanded to string when print char-array-as-string is "off"
-        self.runCmd("-var-create - * u16p")
-        self.expect("\^done,name=\"var\d+\",numchild=\"1\",value=\"0x[0-9a-f]+ u\\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char16_t \*\",thread-id=\"1\",has_more=\"0\"")
-
-        # Test that an char16_t[] isn't expanded to string when print char-array-as-string is "off"
-        self.runCmd("-var-create - * u16a")
-        self.expect("\^done,name=\"var\d+\",numchild=\"10\",value=\"\[10\]\",type=\"const char16_t \[10\]\",thread-id=\"1\",has_more=\"0\"")
-
-        # Test that an char32_t* is expanded to string when print char-array-as-string is "off"
-        self.runCmd("-var-create - * u32p")
-        self.expect("\^done,name=\"var\d+\",numchild=\"1\",value=\"0x[0-9a-f]+ U\\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char32_t \*\",thread-id=\"1\",has_more=\"0\"")
-
-        # Test that an char32_t[] isn't expanded to string when print char-array-as-string is "off"
-        self.runCmd("-var-create - * u32a")
-        self.expect("\^done,name=\"var\d+\",numchild=\"10\",value=\"\[10\]\",type=\"const char32_t \[10\]\",thread-id=\"1\",has_more=\"0\"")
+        # Test that a char* is expanded to string when print char-array-as-string is "off"
+        self.eval_and_match_string("cp", r'0x[0-9a-f]+ \"\\t\\\"hello\\\"\\n\"', r'const char \*')
+
+        # Test that a char[] isn't expanded to string when print char-array-as-string is "off"
+        self.eval_and_check_array("ca", "const char", 10);
+
+        # Test that a char16_t* is expanded to string when print char-array-as-string is "off"
+        self.eval_and_match_string("u16p", r'0x[0-9a-f]+ u\"\\t\\\"hello\\\"\\n\"', r'const char16_t \*')
+
+        # Test that a char16_t[] isn't expanded to string when print char-array-as-string is "off"
+        self.eval_and_check_array("u16a", "const char16_t", 10);
+
+        # Test that a char32_t* is expanded to string when print char-array-as-string is "off"
+        self.eval_and_match_string("u32p", r'0x[0-9a-f]+ U\"\\t\\\"hello\\\"\\n\"', r'const char32_t \*')
+
+        # Test that a char32_t[] isn't expanded to string when print char-array-as-string is "off"
+        self.eval_and_check_array("u32a", "const char32_t", 10);
 
         # Test that -gdb-set can set print char-array-as-string flag
         self.runCmd("-gdb-set print char-array-as-string on")
@@ -67,29 +73,29 @@ class MiGdbSetShowTestCase(lldbmi_testca
         self.runCmd("-gdb-show print char-array-as-string")
         self.expect("\^done,value=\"on\"")
 
-        # Test that an char* with escape chars is expanded to string when print char-array-as-string is "on"
-        self.runCmd("-var-create - * cp")
-        self.expect("\^done,name=\"var\d+\",numchild=\"1\",value=\"0x[0-9a-f]+ \\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char \*\",thread-id=\"1\",has_more=\"0\"")
+        # Test that a char* with escape chars is expanded to string when print char-array-as-string is "on"
+        self.eval_and_match_string("cp", r'0x[0-9a-f]+ \"\\t\\\"hello\\\"\\n\"', r'const char \*')
         
-        # Test that an char[] with escape chars is expanded to string when print char-array-as-string is "on"
-        self.runCmd("-var-create - * ca")
-        self.expect("\^done,name=\"var\d+\",numchild=\"10\",value=\"\\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char \[10\]\",thread-id=\"1\",has_more=\"0\"")
+        # Test that a char[] with escape chars is expanded to string when print char-array-as-string is "on"
+        self.eval_and_match_string("ca", r'\"\\t\\\"hello\\\"\\n\"', r'const char \[10\]')
         
-        # Test that an char16_t* with esc1ape chars is expanded to string when print char-array-as-string is "on"
-        self.runCmd("-var-create - * u16p")
-        self.expect("\^done,name=\"var\d+\",numchild=\"1\",value=\"0x[0-9a-f]+ u\\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char16_t \*\",thread-id=\"1\",has_more=\"0\"")
+        # Test that a char16_t* with escape chars is expanded to string when print char-array-as-string is "on"
+        self.eval_and_match_string("u16p", r'0x[0-9a-f]+ u\"\\t\\\"hello\\\"\\n\"', r'const char16_t \*')
         
-        # Test that an char16_t[] with escape chars is expanded to string when print char-array-as-string is "on"
-        self.runCmd("-var-create - * u16a")
-        self.expect("\^done,name=\"var\d+\",numchild=\"10\",value=\"u\\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char16_t \[10\]\",thread-id=\"1\",has_more=\"0\"")
+        # Test that a char16_t[] with escape chars is expanded to string when print char-array-as-string is "on"
+        self.eval_and_match_string("u16a", r'u\"\\t\\\"hello\\\"\\n\"', r'const char16_t \[10\]')
         
-        # Test that an char32_t* with escape chars is expanded to string when print char-array-as-string is "on"
-        self.runCmd("-var-create - * u32p")
-        self.expect("\^done,name=\"var\d+\",numchild=\"1\",value=\"0x[0-9a-f]+ U\\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char32_t \*\",thread-id=\"1\",has_more=\"0\"")
+        # Test that a char32_t* with escape chars is expanded to string when print char-array-as-string is "on"
+        self.eval_and_match_string("u32p", r'0x[0-9a-f]+ U\"\\t\\\"hello\\\"\\n\"', r'const char32_t \*')
         
-        # Test that an char32_t[] with escape chars is expanded to string when print char-array-as-string is "on"
-        self.runCmd("-var-create - * u32a")
-        self.expect("\^done,name=\"var\d+\",numchild=\"10\",value=\"U\\\\\\\"\\\\\\\\t\\\\\\\\\\\\\"hello\\\\\\\\\\\\\"\\\\\\\\n\\\\\\\"\",type=\"const char32_t \[10\]\",thread-id=\"1\",has_more=\"0\"")
+        # Test that a char32_t[] with escape chars is expanded to string when print char-array-as-string is "on"
+        self.eval_and_match_string("u32a", r'U\"\\t\\\"hello\\\"\\n\"', r'const char32_t \[10\]')
+
+        # Test russian unicode strings
+        self.eval_and_match_string("u16p_rus", r'0x[0-9a-f]+ u\"\\\\Аламо-сквер\"', r'const char16_t \*')
+        self.eval_and_match_string("u16a_rus", r'u\"\\\\Бейвью\"', r'const char16_t \[8\]')
+        self.eval_and_match_string("u32p_rus", r'0x[0-9a-f]+ U\"\\\\Чайнатаун\"', r'const char32_t \*')
+        self.eval_and_match_string("u32a_rus", r'U\"\\\\Догпатч\"', r'const char32_t \[9\]')
 
         # Test that -gdb-set print char-array-as-string fails if "on"/"off" isn't specified
         self.runCmd("-gdb-set print char-array-as-string")
@@ -129,8 +135,7 @@ class MiGdbSetShowTestCase(lldbmi_testca
         self.expect("\^done,name=\"var1\",numchild=\"3\",value=\"{\.\.\.}\",type=\"complex_type\",thread-id=\"1\",has_more=\"0\"")
 
         # Test that composite type[] isn't expanded when print expand-aggregates is "off"
-        self.runCmd("-var-create var2 * complx_array")
-        self.expect("\^done,name=\"var2\",numchild=\"2\",value=\"\[2\]\",type=\"complex_type \[2\]\",thread-id=\"1\",has_more=\"0\"")
+        self.eval_and_check_array("complx_array", "complex_type", 2)
 
         # Test that a struct with a char first element is not formatted as a string
         self.runCmd("-var-create - * &nstr")

Modified: lldb/trunk/test/tools/lldb-mi/variable/TestMiVar.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/tools/lldb-mi/variable/TestMiVar.py?rev=249597&r1=249596&r2=249597&view=diff
==============================================================================
--- lldb/trunk/test/tools/lldb-mi/variable/TestMiVar.py (original)
+++ lldb/trunk/test/tools/lldb-mi/variable/TestMiVar.py Wed Oct  7 14:55:33 2015
@@ -329,5 +329,30 @@ class MiVarTestCase(lldbmi_testcase.MiTe
         self.runCmd("-var-list-children 0 var_complx 0")
         self.expect("\^error,msg=\"Command 'var-list-children'. Variable children range invalid\"")
 
+    @lldbmi_test
+    @skipIfWindows #llvm.org/pr24452: Get lldb-mi working on Windows
+    @skipIfFreeBSD # llvm.org/pr22411: Failure presumably due to known thread races
+    @skipIfLinux # llvm.org/pr22841: lldb-mi tests fail on all Linux buildbots
+    def test_lldbmi_var_create_for_stl_types(self):
+        """Test that 'lldb-mi --interpreter' print summary for STL types."""
+
+        self.spawnLldbMi(args = None)
+
+        # Load executable
+        self.runCmd("-file-exec-and-symbols %s" % self.myexe)
+        self.expect("\^done")
+
+        # Run to BP_gdb_set_show_print_char_array_as_string_test
+        line = line_number('main.cpp', '// BP_cpp_stl_types_test')
+        self.runCmd("-break-insert main.cpp:%d" % line)
+        self.expect("\^done,bkpt={number=\"1\"")
+        self.runCmd("-exec-run")
+        self.expect("\^running")
+        self.expect("\*stopped,reason=\"breakpoint-hit\"")
+
+        # Test for std::string
+        self.runCmd("-var-create - * std_string")
+        self.expect('\^done,name="var\d+",numchild="[0-9]+",value="\\\\"hello\\\\"",type="std::[\S]*?string",thread-id="1",has_more="0"')
+
 if __name__ == '__main__':
     unittest2.main()

Modified: lldb/trunk/test/tools/lldb-mi/variable/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/tools/lldb-mi/variable/main.cpp?rev=249597&r1=249596&r2=249597&view=diff
==============================================================================
--- lldb/trunk/test/tools/lldb-mi/variable/main.cpp (original)
+++ lldb/trunk/test/tools/lldb-mi/variable/main.cpp Wed Oct  7 14:55:33 2015
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include <cstdint>
+#include <string>
 
 struct complex_type
 {
@@ -64,9 +65,21 @@ gdb_set_show_print_char_array_as_string_
     const char32_t *u32p = U"\t\"hello\"\n";
     const char32_t u32a[] = U"\t\"hello\"\n";
 
+    const char16_t* u16p_rus = u"\\Аламо-сквер";
+    const char16_t  u16a_rus[] = u"\\Бейвью";
+    const char32_t* u32p_rus = U"\\Чайнатаун";
+    const char32_t  u32a_rus[] = U"\\Догпатч";
+
     // BP_gdb_set_show_print_char_array_as_string_test
 }
 
+void
+cpp_stl_types_test(void)
+{
+    std::string std_string = "hello";
+    // BP_cpp_stl_types_test
+}
+
 struct not_str
 {
     not_str(char _c, int _f)
@@ -105,6 +118,7 @@ main(int argc, char const *argv[])
     var_update_test();
     var_list_children_test();
     gdb_set_show_print_char_array_as_string_test();
+    cpp_stl_types_test();
     gdb_set_show_print_expand_aggregates();
     gdb_set_show_print_aggregate_field_names();
     return 0; // BP_return

Modified: lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp?rev=249597&r1=249596&r2=249597&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp (original)
+++ lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp Wed Oct  7 14:55:33 2015
@@ -9,6 +9,7 @@
 
 // Third party headers:
 #include <cinttypes>
+#include "lldb/API/SBTypeSummary.h"
 
 // In-house headers:
 #include "MICmnLLDBUtilSBValue.h"
@@ -165,6 +166,14 @@ CMICmnLLDBUtilSBValue::GetSimpleValue(co
             return MIstatus::success;
         }
     }
+    else
+    {
+        // Treat composite value which has registered summary
+        // (for example with AddCXXSummary) as simple value
+        vwrValue = GetValueSummary();
+        if (!vwrValue.empty())
+            return MIstatus::success;
+    }
 
     // Composite variable type i.e. struct
     return MIstatus::failure;
@@ -180,6 +189,10 @@ CMICmnLLDBUtilSBValue::GetSimpleValue(co
 CMIUtilString
 CMICmnLLDBUtilSBValue::GetSimpleValueChar() const
 {
+    const CMIUtilString& summary = GetValueSummary();
+    if (!summary.empty())
+        return summary;
+
     const uint64_t value = m_rValue.GetValueAsUnsigned();
     if (value == 0)
     {
@@ -223,6 +236,10 @@ CMICmnLLDBUtilSBValue::GetSimpleValueCha
 CMIUtilString
 CMICmnLLDBUtilSBValue::GetSimpleValueCStringPointer() const
 {
+    const CMIUtilString& summary = GetValueSummary();
+    if (!summary.empty())
+        return summary;
+
     const char *value = m_rValue.GetValue();
     if (value == nullptr)
         return m_pUnkwn;
@@ -266,6 +283,10 @@ CMICmnLLDBUtilSBValue::GetSimpleValueCSt
 CMIUtilString
 CMICmnLLDBUtilSBValue::GetSimpleValueCStringArray() const
 {
+    const CMIUtilString& summary = GetValueSummary();
+    if (!summary.empty())
+        return summary;
+
     const MIuint nChildren = m_rValue.GetNumChildren();
     lldb::SBValue child = m_rValue.GetChildAtIndex(0);
     const lldb::BasicType eType = child.GetType().GetBasicType();
@@ -347,6 +368,30 @@ CMICmnLLDBUtilSBValue::GetCompositeValue
     return MIstatus::success;
 }
 
+CMIUtilString
+CMICmnLLDBUtilSBValue::GetValueSummary() const
+{
+    CMIUtilString valSummary;
+    if (m_rValue.IsValid())
+    {
+        const char *c_str = m_rValue.GetSummary();
+        if (c_str && c_str[0])
+        {
+            valSummary = c_str;
+            lldb::SBTypeSummary summary = m_rValue.GetTypeSummary();
+            if (summary.IsValid() && summary.DoesPrintValue(m_rValue))
+            {
+                c_str = m_rValue.GetValue();
+                if (c_str && c_str[0])
+                {
+                    valSummary.insert(0, std::string(c_str) + " ");
+                }
+            }
+        }
+    }
+    return valSummary;
+}
+
 //++ ------------------------------------------------------------------------------------
 // Details: Check that basic type is a char type. Char type can be signed or unsigned.
 // Type:    Static.

Modified: lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.h?rev=249597&r1=249596&r2=249597&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.h (original)
+++ lldb/trunk/tools/lldb-mi/MICmnLLDBUtilSBValue.h Wed Oct  7 14:55:33 2015
@@ -55,6 +55,7 @@ class CMICmnLLDBUtilSBValue
     CMIUtilString GetSimpleValueCStringPointer() const;
     CMIUtilString GetSimpleValueCStringArray() const;
     bool GetCompositeValue(const bool vbPrintFieldNames, CMICmnMIValueTuple &vwrMiValueTuple, const MIuint vnDepth = 1) const;
+    CMIUtilString GetValueSummary() const;
 
     // Statics:
   private:




More information about the lldb-commits mailing list