Skip to content

Commit

Permalink
[lldb-mi] Fix evaluation of strings containing characters from non-as…
Browse files Browse the repository at this point in the history
…cii 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@gmail.com
Reviewed by: ki.stfu, granata.enrico, clayborg
Subscribers: lldb-commits
Differential Revision: http://reviews.llvm.org/D13058

llvm-svn: 249597
  • Loading branch information
Dawn Perchik committed Oct 7, 2015
1 parent af6980c commit 74b8f4c
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 40 deletions.
3 changes: 3 additions & 0 deletions lldb/include/lldb/API/SBTypeSummary.h
Expand Up @@ -125,6 +125,9 @@ namespace lldb {
lldb::SBTypeSummary &
operator = (const lldb::SBTypeSummary &rhs);

bool
DoesPrintValue (lldb::SBValue value);

bool
IsEqualTo (lldb::SBTypeSummary &rhs);

Expand Down
12 changes: 10 additions & 2 deletions lldb/source/API/SBTypeSummary.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -284,6 +283,15 @@ SBTypeSummary::GetDescription (lldb::SBStream &description,
}
}

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)
{
Expand Down
81 changes: 43 additions & 38 deletions lldb/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py
@@ -1,3 +1,4 @@
#coding=utf8
"""
Test lldb-mi -gdb-set and -gdb-show commands for 'print option-name'.
"""
Expand All @@ -10,6 +11,17 @@ class MiGdbSetShowTestCase(lldbmi_testcase.MiTestCaseBase):

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
Expand All @@ -35,29 +47,23 @@ def test_lldbmi_gdb_set_show_print_char_array_as_string(self):
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 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 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 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 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 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 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 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 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 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 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 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")
Expand All @@ -67,29 +73,29 @@ def test_lldbmi_gdb_set_show_print_char_array_as_string(self):
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")
Expand Down Expand Up @@ -129,8 +135,7 @@ def test_lldbmi_gdb_set_show_print_expand_aggregates(self):
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")
Expand Down
25 changes: 25 additions & 0 deletions lldb/test/tools/lldb-mi/variable/TestMiVar.py
Expand Up @@ -329,5 +329,30 @@ def test_lldbmi_var_list_children(self):
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()
14 changes: 14 additions & 0 deletions lldb/test/tools/lldb-mi/variable/main.cpp
Expand Up @@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//

#include <cstdint>
#include <string>

struct complex_type
{
Expand Down Expand Up @@ -64,9 +65,21 @@ gdb_set_show_print_char_array_as_string_test(void)
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)
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lldb/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
Expand Up @@ -9,6 +9,7 @@

// Third party headers:
#include <cinttypes>
#include "lldb/API/SBTypeSummary.h"

// In-house headers:
#include "MICmnLLDBUtilSBValue.h"
Expand Down Expand Up @@ -165,6 +166,14 @@ CMICmnLLDBUtilSBValue::GetSimpleValue(const bool vbHandleArrayType, CMIUtilStrin
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;
Expand All @@ -180,6 +189,10 @@ CMICmnLLDBUtilSBValue::GetSimpleValue(const bool vbHandleArrayType, CMIUtilStrin
CMIUtilString
CMICmnLLDBUtilSBValue::GetSimpleValueChar() const
{
const CMIUtilString& summary = GetValueSummary();
if (!summary.empty())
return summary;

const uint64_t value = m_rValue.GetValueAsUnsigned();
if (value == 0)
{
Expand Down Expand Up @@ -223,6 +236,10 @@ CMICmnLLDBUtilSBValue::GetSimpleValueChar() const
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;
Expand Down Expand Up @@ -266,6 +283,10 @@ CMICmnLLDBUtilSBValue::GetSimpleValueCStringPointer() const
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();
Expand Down Expand Up @@ -347,6 +368,30 @@ CMICmnLLDBUtilSBValue::GetCompositeValue(const bool vbPrintFieldNames, CMICmnMIV
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.
Expand Down
1 change: 1 addition & 0 deletions lldb/tools/lldb-mi/MICmnLLDBUtilSBValue.h
Expand Up @@ -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:
Expand Down

0 comments on commit 74b8f4c

Please sign in to comment.