From aa82b4af84fb361077d3a984c1808fe7908d5c1f Mon Sep 17 00:00:00 2001 From: Ilia K Date: Sat, 25 Apr 2015 19:44:56 +0000 Subject: [PATCH] Add -gdb-set/-gdb-show print char-array-as-string option (MI) llvm-svn: 235804 --- .../lldb-mi/variable/TestMiGdbSetShowPrint.py | 69 ++++++++++++++++++++++ lldb/test/tools/lldb-mi/variable/main.cpp | 10 ++++ lldb/tools/lldb-mi/MICmdCmdGdbSet.cpp | 45 ++++++++++++++ lldb/tools/lldb-mi/MICmdCmdGdbSet.h | 1 + lldb/tools/lldb-mi/MICmdCmdGdbShow.cpp | 41 +++++++++++++ lldb/tools/lldb-mi/MICmdCmdGdbShow.h | 1 + lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.cpp | 1 + lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.h | 1 + lldb/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp | 21 ++++++- lldb/tools/lldb-mi/MICmnResources.cpp | 6 +- lldb/tools/lldb-mi/MICmnResources.h | 6 +- 11 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 lldb/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py diff --git a/lldb/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py b/lldb/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py new file mode 100644 index 0000000..1928446 --- /dev/null +++ b/lldb/test/tools/lldb-mi/variable/TestMiGdbSetShowPrint.py @@ -0,0 +1,69 @@ +""" +Test lldb-mi -gdb-set and -gdb-show commands for 'print option-name'. +""" + +import lldbmi_testcase +from lldbtest import * +import unittest2 + +class MiGdbSetShowTestCase(lldbmi_testcase.MiTestCaseBase): + + mydir = TestBase.compute_mydir(__file__) + + @lldbmi_test + @expectedFailureWindows("llvm.org/pr22274: need a pexpect replacement for windows") + def test_lldbmi_gdb_set_show_print_char_array_as_string(self): + """Test that 'lldb-mi --interpreter' can print array of chars as string.""" + + 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_gdb_set_show_print_char_array_as_string_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 that default print char-array-as-string value is "off" + 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 var1 * string_ptr") + self.expect("\^done,name=\"var1\",numchild=\"1\",value=\"0x[0-9a-f]+ \\\\\\\"string - const char \*\\\\\\\"\",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 var2 * string_arr") + self.expect("\^done,name=\"var2\",numchild=\"17\",value=\"\[17\]\",type=\"const char \[17\]\",thread-id=\"1\",has_more=\"0\"") + + # Test that -gdb-set can set print char-array-as-string flag + self.runCmd("-gdb-set print char-array-as-string on") + self.expect("\^done") + self.runCmd("-gdb-set print char-array-as-string 1") + self.expect("\^done") + self.runCmd("-gdb-show print char-array-as-string") + self.expect("\^done,value=\"on\"") + + # Test that an char* is expanded to string when print char-array-as-string is "on" + self.runCmd("-var-create var1 * string_ptr") + self.expect("\^done,name=\"var1\",numchild=\"1\",value=\"0x[0-9a-f]+ \\\\\\\"string - const char \*\\\\\\\"\",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 "on" + self.runCmd("-var-create var2 * string_arr") + self.expect("\^done,name=\"var2\",numchild=\"17\",value=\"\\\\\\\"string - char \[\]\\\\\\\"\",type=\"const char \[17\]\",thread-id=\"1\",has_more=\"0\"") + + # 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") + self.expect("\^error,msg=\"The request ''print' expects option-name and \"on\" or \"off\"' failed.\"") + + # Test that -gdb-set print char-array-as-string fails when option is unknown + self.runCmd("-gdb-set print char-array-as-string unknown") + self.expect("\^error,msg=\"The request ''print' expects option-name and \"on\" or \"off\"' failed.\"") + +if __name__ == '__main__': + unittest2.main() diff --git a/lldb/test/tools/lldb-mi/variable/main.cpp b/lldb/test/tools/lldb-mi/variable/main.cpp index 5a80817..a61baa5 100644 --- a/lldb/test/tools/lldb-mi/variable/main.cpp +++ b/lldb/test/tools/lldb-mi/variable/main.cpp @@ -34,6 +34,15 @@ var_update_test(void) // BP_var_update_test_complx_array } +void +gdb_set_show_print_char_array_as_string_test(void) +{ + const char *string_ptr = "string - const char *"; + const char string_arr[] = "string - char []"; + + // BP_gdb_set_show_print_char_array_as_string_test +} + int g_MyVar = 3; static int s_MyVar = 4; @@ -43,5 +52,6 @@ main(int argc, char const *argv[]) int a = 10, b = 20; s_MyVar = a + b; var_update_test(); + gdb_set_show_print_char_array_as_string_test(); return 0; // BP_return } diff --git a/lldb/tools/lldb-mi/MICmdCmdGdbSet.cpp b/lldb/tools/lldb-mi/MICmdCmdGdbSet.cpp index ace1475..b5202cf 100644 --- a/lldb/tools/lldb-mi/MICmdCmdGdbSet.cpp +++ b/lldb/tools/lldb-mi/MICmdCmdGdbSet.cpp @@ -21,6 +21,7 @@ // Instantiations: const CMICmdCmdGdbSet::MapGdbOptionNameToFnGdbOptionPtr_t CMICmdCmdGdbSet::ms_mapGdbOptionNameToFnGdbOptionPtr = { {"target-async", &CMICmdCmdGdbSet::OptionFnTargetAsync}, + {"print", &CMICmdCmdGdbSet::OptionFnPrint}, // { "auto-solib-add", &CMICmdCmdGdbSet::OptionFnAutoSolibAdd }, // Example code if need to implement GDB set other options {"output-radix", &CMICmdCmdGdbSet::OptionFnOutputRadix}, {"solib-search-path", &CMICmdCmdGdbSet::OptionFnSolibSearchPath}, @@ -256,6 +257,50 @@ CMICmdCmdGdbSet::OptionFnTargetAsync(const CMIUtilString::VecString_t &vrWords) } //++ ------------------------------------------------------------------------------------ +// Details: Carry out work to complete the GDB set option 'print-char-array-as-string' to +// prepare and send back information asked for. +// Type: Method. +// Args: vrWords - (R) List of additional parameters used by this option. +// Return: MIstatus::success - Function succeeded. +// MIstatus::failure - Function failed. +// Throws: None. +//-- +bool +CMICmdCmdGdbSet::OptionFnPrint(const CMIUtilString::VecString_t &vrWords) +{ + const bool bAllArgs(vrWords.size() == 2); + const bool bArgOn(bAllArgs && (CMIUtilString::Compare(vrWords[1], "on") || CMIUtilString::Compare(vrWords[1], "1"))); + const bool bArgOff(bAllArgs && (CMIUtilString::Compare(vrWords[1], "off") || CMIUtilString::Compare(vrWords[1], "0"))); + if (!bAllArgs || (!bArgOn && !bArgOff)) + { + m_bGbbOptionFnHasError = true; + m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSET_OPT_PRINT_BAD_ARGS); + return MIstatus::failure; + } + + const CMIUtilString strOption(vrWords[0]); + CMIUtilString strOptionKey; + if (CMIUtilString::Compare(strOption, "char-array-as-string")) + strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintCharArrayAsString; + else + { + m_bGbbOptionFnHasError = true; + m_strGdbOptionFnError = CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_GDBSET_OPT_PRINT_UNKNOWN_OPTION), strOption.c_str()); + return MIstatus::failure; + } + + const bool bOptionValue(bArgOn); + if (!m_rLLDBDebugSessionInfo.SharedDataAdd(strOptionKey, bOptionValue)) + { + m_bGbbOptionFnHasError = false; + SetError(CMIUtilString::Format(MIRSRC(IDS_DBGSESSION_ERR_SHARED_DATA_ADD), m_cmdData.strMiCmd.c_str(), strOptionKey.c_str())); + return MIstatus::failure; + } + + return MIstatus::success; +} + +//++ ------------------------------------------------------------------------------------ // Details: Carry out work to complete the GDB set option 'solib-search-path' to prepare // and send back information asked for. // Type: Method. diff --git a/lldb/tools/lldb-mi/MICmdCmdGdbSet.h b/lldb/tools/lldb-mi/MICmdCmdGdbSet.h index 3aa3e39..0095f81 100644 --- a/lldb/tools/lldb-mi/MICmdCmdGdbSet.h +++ b/lldb/tools/lldb-mi/MICmdCmdGdbSet.h @@ -70,6 +70,7 @@ class CMICmdCmdGdbSet : public CMICmdBase private: bool GetOptionFn(const CMIUtilString &vrGdbOptionName, FnGdbOptionPtr &vrwpFn) const; bool OptionFnTargetAsync(const CMIUtilString::VecString_t &vrWords); + bool OptionFnPrint(const CMIUtilString::VecString_t &vrWords); bool OptionFnSolibSearchPath(const CMIUtilString::VecString_t &vrWords); bool OptionFnOutputRadix(const CMIUtilString::VecString_t &vrWords); bool OptionFnFallback(const CMIUtilString::VecString_t &vrWords); diff --git a/lldb/tools/lldb-mi/MICmdCmdGdbShow.cpp b/lldb/tools/lldb-mi/MICmdCmdGdbShow.cpp index 73f5e8e..41661c2 100644 --- a/lldb/tools/lldb-mi/MICmdCmdGdbShow.cpp +++ b/lldb/tools/lldb-mi/MICmdCmdGdbShow.cpp @@ -21,6 +21,7 @@ // Instantiations: const CMICmdCmdGdbShow::MapGdbOptionNameToFnGdbOptionPtr_t CMICmdCmdGdbShow::ms_mapGdbOptionNameToFnGdbOptionPtr = { {"target-async", &CMICmdCmdGdbShow::OptionFnTargetAsync}, + {"print", &CMICmdCmdGdbShow::OptionFnPrint}, {"fallback", &CMICmdCmdGdbShow::OptionFnFallback}}; //++ ------------------------------------------------------------------------------------ @@ -239,6 +240,46 @@ CMICmdCmdGdbShow::OptionFnTargetAsync(const CMIUtilString::VecString_t &vrWords) } //++ ------------------------------------------------------------------------------------ +// Details: Carry out work to complete the GDB show option 'print' to prepare and send +// back the requested information. +// Type: Method. +// Args: vrWords - (R) List of additional parameters used by this option. +// Return: MIstatus::success - Function succeeded. +// MIstatus::failure - Function failed. +// Throws: None. +//-- +bool +CMICmdCmdGdbShow::OptionFnPrint(const CMIUtilString::VecString_t &vrWords) +{ + const bool bAllArgs(vrWords.size() == 1); + if (!bAllArgs) + { + m_bGbbOptionFnHasError = true; + m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS); + return MIstatus::failure; + } + + const CMIUtilString strOption(vrWords[0]); + CMIUtilString strOptionKey; + bool bOptionValueDefault = false; + if (CMIUtilString::Compare(strOption, "char-array-as-string")) + strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintCharArrayAsString; + else + { + m_bGbbOptionFnHasError = true; + m_strGdbOptionFnError = CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_UNKNOWN_OPTION), strOption.c_str()); + return MIstatus::failure; + } + + bool bOptionValue = false; + bOptionValue = bOptionValueDefault ? !m_rLLDBDebugSessionInfo.SharedDataRetrieve(strOptionKey, bOptionValue) || bOptionValue + : m_rLLDBDebugSessionInfo.SharedDataRetrieve(strOptionKey, bOptionValue) && bOptionValue; + + m_strValue = bOptionValue ? "on" : "off"; + return MIstatus::success; +} + +//++ ------------------------------------------------------------------------------------ // Details: Carry out work to complete the GDB show option to prepare and send back the // requested information. // Type: Method. diff --git a/lldb/tools/lldb-mi/MICmdCmdGdbShow.h b/lldb/tools/lldb-mi/MICmdCmdGdbShow.h index 40d1b2f..3935e24 100644 --- a/lldb/tools/lldb-mi/MICmdCmdGdbShow.h +++ b/lldb/tools/lldb-mi/MICmdCmdGdbShow.h @@ -67,6 +67,7 @@ class CMICmdCmdGdbShow : public CMICmdBase private: bool GetOptionFn(const CMIUtilString &vrGdbOptionName, FnGdbOptionPtr &vrwpFn) const; bool OptionFnTargetAsync(const CMIUtilString::VecString_t &vrWords); + bool OptionFnPrint(const CMIUtilString::VecString_t &vrWords); bool OptionFnFallback(const CMIUtilString::VecString_t &vrWords); // Attributes: diff --git a/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.cpp b/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.cpp index 6ec4935..63c9a18 100644 --- a/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.cpp +++ b/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.cpp @@ -40,6 +40,7 @@ CMICmnLLDBDebugSessionInfo::CMICmnLLDBDebugSessionInfo(void) , m_currentSelectedThread(LLDB_INVALID_THREAD_ID) , m_constStrSharedDataKeyWkDir("Working Directory") , m_constStrSharedDataSolibPath("Solib Path") + , m_constStrPrintCharArrayAsString("Print CharArrayAsString") { } diff --git a/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.h b/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.h index 48669bd..03ebc39 100644 --- a/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.h +++ b/lldb/tools/lldb-mi/MICmnLLDBDebugSessionInfo.h @@ -183,6 +183,7 @@ class CMICmnLLDBDebugSessionInfo : public CMICmnBase, public MI::ISingleton(rSessionInfo.m_constStrPrintCharArrayAsString, + bPrintCharArrayAsString) && bPrintCharArrayAsString; + if (bPrintCharArrayAsString && m_bHandleCharType && IsFirstChildCharType()) + { + // TODO: to match char* it should be the following + // vwrValue = CMIUtilString::Format("[%u] \"%s\"", nChildren, prefix.c_str()); + const CMIUtilString prefix(GetValueCString().Escape().AddSlashes()); + vwrValue = CMIUtilString::Format("\"%s\"", prefix.c_str()); + return MIstatus::success; + } + else if (vbHandleArrayType) + { + vwrValue = CMIUtilString::Format("[%u]", nChildren); + return MIstatus::success; + } } // Composite variable type i.e. struct diff --git a/lldb/tools/lldb-mi/MICmnResources.cpp b/lldb/tools/lldb-mi/MICmnResources.cpp index 798036c..65543fe 100644 --- a/lldb/tools/lldb-mi/MICmnResources.cpp +++ b/lldb/tools/lldb-mi/MICmnResources.cpp @@ -244,7 +244,11 @@ const CMICmnResources::SRsrcTextData CMICmnResources::ms_pResourceId2TextData[] {IDS_CMD_ERR_INFO_PRINTFN_NOT_FOUND, "The request '%s' was not recogised, not implemented"}, {IDS_CMD_ERR_INFO_PRINTFN_FAILED, "The request '%s' failed."}, {IDS_CMD_ERR_GDBSET_OPT_TARGETASYNC, "'target-async' expects \"on\" or \"off\""}, - {IDS_CMD_ERR_GDBSET_OPT_SOLIBSEARCHPATH, "'solib-search-path' requires at least one argument"}}; + {IDS_CMD_ERR_GDBSET_OPT_SOLIBSEARCHPATH, "'solib-search-path' requires at least one argument"}, + {IDS_CMD_ERR_GDBSET_OPT_PRINT_BAD_ARGS, "'print' expects option-name and \"on\" or \"off\""}, + {IDS_CMD_ERR_GDBSET_OPT_PRINT_UNKNOWN_OPTION, "'print' error. The option '%s' not found"}, + {IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS, "'print' expects option-name and \"on\" or \"off\""}, + {IDS_CMD_ERR_GDBSHOW_OPT_PRINT_UNKNOWN_OPTION, "'print' error. The option '%s' not found"}}; //++ ------------------------------------------------------------------------------------ // Details: CMICmnResources constructor. diff --git a/lldb/tools/lldb-mi/MICmnResources.h b/lldb/tools/lldb-mi/MICmnResources.h index 6064da6..ebcc2d1 100644 --- a/lldb/tools/lldb-mi/MICmnResources.h +++ b/lldb/tools/lldb-mi/MICmnResources.h @@ -262,7 +262,11 @@ enum IDS_CMD_ERR_INFO_PRINTFN_NOT_FOUND, IDS_CMD_ERR_INFO_PRINTFN_FAILED, IDS_CMD_ERR_GDBSET_OPT_TARGETASYNC, - IDS_CMD_ERR_GDBSET_OPT_SOLIBSEARCHPATH + IDS_CMD_ERR_GDBSET_OPT_SOLIBSEARCHPATH, + IDS_CMD_ERR_GDBSET_OPT_PRINT_BAD_ARGS, + IDS_CMD_ERR_GDBSET_OPT_PRINT_UNKNOWN_OPTION, + IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS, + IDS_CMD_ERR_GDBSHOW_OPT_PRINT_UNKNOWN_OPTION }; //++ ============================================================================ -- 2.7.4