summaryrefslogtreecommitdiffstats
path: root/lldb/packages/Python/lldbsuite/test/commands/command
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/packages/Python/lldbsuite/test/commands/command')
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/history/.categories1
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/history/TestCommandHistory.py107
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/Makefile5
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/TestNestedAlias.py90
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/main.cpp21
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/.categories1
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/Makefile5
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py153
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/bug11569.py6
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/decorated.py35
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/Makefile6
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/TestImport.py76
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/bar.py15
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/barutil.py2
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/dummymodule.py2
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/bar/foobar.py6
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo.py6
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo2.py11
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/main.c15
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/Makefile3
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/TestRdar12586188.py36
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail12586188.py4
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail212586188.py4
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitA.py7
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitB.py7
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/__init__.py11
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/main.cpp69
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/mysto.py22
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/py_import13
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script/welcome.py53
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script_alias/.categories1
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script_alias/TestCommandScriptAlias.py38
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/script_alias/tcsacmd.py11
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/source/.categories1
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/source/.lldb2
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/source/TestCommandSource.py35
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/source/commands.txt2
-rw-r--r--lldb/packages/Python/lldbsuite/test/commands/command/source/my.py7
38 files changed, 889 insertions, 0 deletions
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/history/.categories b/lldb/packages/Python/lldbsuite/test/commands/command/history/.categories
new file mode 100644
index 00000000000..3a3f4df6416
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/history/.categories
@@ -0,0 +1 @@
+cmdline
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/history/TestCommandHistory.py b/lldb/packages/Python/lldbsuite/test/commands/command/history/TestCommandHistory.py
new file mode 100644
index 00000000000..2763ab50c88
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/history/TestCommandHistory.py
@@ -0,0 +1,107 @@
+"""
+Test the command history mechanism
+"""
+
+from __future__ import print_function
+
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class CommandHistoryTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @no_debug_info_test
+ def test_history(self):
+ self.runCmd('command history --clear', inHistory=False)
+ self.runCmd('breakpoint list', check=False, inHistory=True) # 0
+ self.runCmd('register read', check=False, inHistory=True) # 1
+ self.runCmd('apropos hello', check=False, inHistory=True) # 2
+ self.runCmd('memory write', check=False, inHistory=True) # 3
+ self.runCmd('log list', check=False, inHistory=True) # 4
+ self.runCmd('disassemble', check=False, inHistory=True) # 5
+ self.runCmd('expression 1', check=False, inHistory=True) # 6
+ self.runCmd(
+ 'type summary list -w default',
+ check=False,
+ inHistory=True) # 7
+ self.runCmd('version', check=False, inHistory=True) # 8
+ self.runCmd('frame select 1', check=False, inHistory=True) # 9
+
+ self.expect(
+ "command history -s 3 -c 3",
+ inHistory=True,
+ substrs=[
+ '3: memory write',
+ '4: log list',
+ '5: disassemble'])
+
+ self.expect("command history -s 3 -e 3", inHistory=True,
+ substrs=['3: memory write'])
+
+ self.expect(
+ "command history -s 6 -e 7",
+ inHistory=True,
+ substrs=[
+ '6: expression 1',
+ '7: type summary list -w default'])
+
+ self.expect("command history -c 2", inHistory=True,
+ substrs=['0: breakpoint list', '1: register read'])
+
+ self.expect("command history -e 3 -c 1", inHistory=True,
+ substrs=['3: memory write'])
+
+ self.expect(
+ "command history -e 2",
+ inHistory=True,
+ substrs=[
+ '0: breakpoint list',
+ '1: register read',
+ '2: apropos hello'])
+
+ self.expect(
+ "command history -s 12",
+ inHistory=True,
+ substrs=[
+ '12: command history -s 6 -e 7',
+ '13: command history -c 2',
+ '14: command history -e 3 -c 1',
+ '15: command history -e 2',
+ '16: command history -s 12'])
+
+ self.expect(
+ "command history -s end -c 3",
+ inHistory=True,
+ substrs=[
+ '15: command history -e 2',
+ '16: command history -s 12',
+ '17: command history -s end -c 3'])
+
+ self.expect(
+ "command history -s end -e 15",
+ inHistory=True,
+ substrs=[
+ '15: command history -e 2',
+ '16: command history -s 12',
+ '17: command history -s end -c 3',
+ 'command history -s end -e 15'])
+
+ self.expect("command history -s 5 -c 1", inHistory=True,
+ substrs=['5: disassemble'])
+
+ self.expect("command history -c 1 -s 5", inHistory=True,
+ substrs=['5: disassemble'])
+
+ self.expect("command history -c 1 -e 3", inHistory=True,
+ substrs=['3: memory write'])
+
+ self.expect(
+ "command history -c 1 -e 3 -s 5",
+ error=True,
+ inHistory=True,
+ substrs=['error: --count, --start-index and --end-index cannot be all specified in the same invocation'])
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/Makefile b/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/Makefile
new file mode 100644
index 00000000000..8a7102e347a
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/Makefile
@@ -0,0 +1,5 @@
+LEVEL = ../../make
+
+CXX_SOURCES := main.cpp
+
+include $(LEVEL)/Makefile.rules
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/TestNestedAlias.py b/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/TestNestedAlias.py
new file mode 100644
index 00000000000..37c56f6d216
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/TestNestedAlias.py
@@ -0,0 +1,90 @@
+"""
+Test that an alias can reference other aliases without crashing.
+"""
+
+from __future__ import print_function
+
+
+import lldb
+from lldbsuite.test.lldbtest import *
+import lldbsuite.test.lldbutil as lldbutil
+
+
+class NestedAliasTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ def setUp(self):
+ # Call super's setUp().
+ TestBase.setUp(self)
+ # Find the line number to break inside main().
+ self.line = line_number('main.cpp', '// break here')
+
+ def test_nested_alias(self):
+ """Test that an alias can reference other aliases without crashing."""
+ self.build()
+ exe = self.getBuildArtifact("a.out")
+ self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
+
+ # Break in main() after the variables are assigned values.
+ lldbutil.run_break_set_by_file_and_line(
+ self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
+
+ self.runCmd("run", RUN_SUCCEEDED)
+
+ # The stop reason of the thread should be breakpoint.
+ self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
+ substrs=['stopped', 'stop reason = breakpoint'])
+
+ # The breakpoint should have a hit count of 1.
+ self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
+ substrs=[' resolved, hit count = 1'])
+
+ # This is the function to remove the custom aliases in order to have a
+ # clean slate for the next test case.
+ def cleanup():
+ self.runCmd('command unalias read', check=False)
+ self.runCmd('command unalias rd', check=False)
+ self.runCmd('command unalias fo', check=False)
+ self.runCmd('command unalias foself', check=False)
+
+ # Execute the cleanup function during test case tear down.
+ self.addTearDownHook(cleanup)
+
+ self.runCmd('command alias read memory read -f A')
+ self.runCmd('command alias rd read -c 3')
+
+ self.expect(
+ 'memory read -f A -c 3 `&my_ptr[0]`',
+ substrs=[
+ 'deadbeef',
+ 'main.cpp:',
+ 'feedbeef'])
+ self.expect(
+ 'rd `&my_ptr[0]`',
+ substrs=[
+ 'deadbeef',
+ 'main.cpp:',
+ 'feedbeef'])
+
+ self.expect(
+ 'memory read -f A -c 3 `&my_ptr[0]`',
+ substrs=['deadfeed'],
+ matching=False)
+ self.expect('rd `&my_ptr[0]`', substrs=['deadfeed'], matching=False)
+
+ self.runCmd('command alias fo frame variable -O --')
+ self.runCmd('command alias foself fo self')
+
+ self.expect(
+ 'help foself',
+ substrs=[
+ '--show-all-children',
+ '--raw-output'],
+ matching=False)
+ self.expect(
+ 'help foself',
+ substrs=[
+ 'Show variables for the current',
+ 'stack frame.'],
+ matching=True)
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/main.cpp b/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/main.cpp
new file mode 100644
index 00000000000..4df7eed40a5
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/nested_alias/main.cpp
@@ -0,0 +1,21 @@
+//===-- main.cpp ------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include <stdio.h>
+
+int main (int argc, char const *argv[])
+{
+ void* my_ptr[] = {
+ reinterpret_cast<void*>(0xDEADBEEF),
+ reinterpret_cast<void*>(main),
+ reinterpret_cast<void*>(0xFEEDBEEF),
+ reinterpret_cast<void*>(0xFEEDDEAD),
+ reinterpret_cast<void*>(0xDEADFEED)
+ };
+ return 0; // break here
+}
+
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/.categories b/lldb/packages/Python/lldbsuite/test/commands/command/script/.categories
new file mode 100644
index 00000000000..3a3f4df6416
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/.categories
@@ -0,0 +1 @@
+cmdline
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/Makefile b/lldb/packages/Python/lldbsuite/test/commands/command/script/Makefile
new file mode 100644
index 00000000000..8a7102e347a
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/Makefile
@@ -0,0 +1,5 @@
+LEVEL = ../../make
+
+CXX_SOURCES := main.cpp
+
+include $(LEVEL)/Makefile.rules
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py
new file mode 100644
index 00000000000..6531cd67279
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py
@@ -0,0 +1,153 @@
+"""
+Test lldb Python commands.
+"""
+
+from __future__ import print_function
+
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class CmdPythonTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+ NO_DEBUG_INFO_TESTCASE = True
+
+ def test(self):
+ self.build()
+ self.pycmd_tests()
+
+ def pycmd_tests(self):
+ self.runCmd("command source py_import")
+
+ # Verify command that specifies eCommandRequiresTarget returns failure
+ # without a target.
+ self.expect('targetname',
+ substrs=['a.out'], matching=False, error=True)
+
+ exe = self.getBuildArtifact("a.out")
+ self.expect("file " + exe,
+ patterns=["Current executable set to .*a.out"])
+
+ self.expect('targetname',
+ substrs=['a.out'], matching=True, error=False)
+
+ # This is the function to remove the custom commands in order to have a
+ # clean slate for the next test case.
+ def cleanup():
+ self.runCmd('command script delete welcome', check=False)
+ self.runCmd('command script delete targetname', check=False)
+ self.runCmd('command script delete longwait', check=False)
+ self.runCmd('command script delete mysto', check=False)
+ self.runCmd('command script delete tell_sync', check=False)
+ self.runCmd('command script delete tell_async', check=False)
+ self.runCmd('command script delete tell_curr', check=False)
+ self.runCmd('command script delete bug11569', check=False)
+ self.runCmd('command script delete takes_exe_ctx', check=False)
+ self.runCmd('command script delete decorated', check=False)
+
+ # Execute the cleanup function during test case tear down.
+ self.addTearDownHook(cleanup)
+
+ # Interact with debugger in synchronous mode
+ self.setAsync(False)
+
+ # We don't want to display the stdout if not in TraceOn() mode.
+ if not self.TraceOn():
+ self.HideStdout()
+
+ self.expect('welcome Enrico',
+ substrs=['Hello Enrico, welcome to LLDB'])
+
+ self.expect("help welcome",
+ substrs=['Just a docstring for welcome_impl',
+ 'A command that says hello to LLDB users'])
+
+ decorated_commands = ["decorated" + str(n) for n in range(1, 5)]
+ for name in decorated_commands:
+ self.expect(name, substrs=["hello from " + name])
+ self.expect("help " + name,
+ substrs=["Python command defined by @lldb.command"])
+
+ self.expect("help",
+ substrs=['For more information run',
+ 'welcome'] + decorated_commands)
+
+ self.expect("help -a",
+ substrs=['For more information run',
+ 'welcome'] + decorated_commands)
+
+ self.expect("help -u", matching=False,
+ substrs=['For more information run'])
+
+ self.runCmd("command script delete welcome")
+
+ self.expect('welcome Enrico', matching=False, error=True,
+ substrs=['Hello Enrico, welcome to LLDB'])
+
+ self.expect('targetname fail', error=True,
+ substrs=['a test for error in command'])
+
+ self.expect('command script list',
+ substrs=['targetname',
+ 'For more information run'])
+
+ self.expect("help targetname",
+ substrs=['Expects', '\'raw\'', 'input',
+ 'help', 'raw-input'])
+
+ self.expect("longwait",
+ substrs=['Done; if you saw the delays I am doing OK'])
+
+ self.runCmd("b main")
+ self.runCmd("run")
+ self.runCmd("mysto 3")
+ self.expect("frame variable array",
+ substrs=['[0] = 79630', '[1] = 388785018', '[2] = 0'])
+ self.runCmd("mysto 3")
+ self.expect("frame variable array",
+ substrs=['[0] = 79630', '[4] = 388785018', '[5] = 0'])
+
+# we cannot use the stepover command to check for async execution mode since LLDB
+# seems to get confused when events start to queue up
+ self.expect("tell_sync",
+ substrs=['running sync'])
+ self.expect("tell_async",
+ substrs=['running async'])
+ self.expect("tell_curr",
+ substrs=['I am running sync'])
+
+# check that the execution context is passed in to commands that ask for it
+ self.expect("takes_exe_ctx", substrs=["a.out"])
+
+ # Test that a python command can redefine itself
+ self.expect('command script add -f foobar welcome -h "just some help"')
+
+ self.runCmd("command script clear")
+
+ # Test that re-defining an existing command works
+ self.runCmd(
+ 'command script add my_command --class welcome.WelcomeCommand')
+ self.expect('my_command Blah', substrs=['Hello Blah, welcome to LLDB'])
+
+ self.runCmd(
+ 'command script add my_command --class welcome.TargetnameCommand')
+ self.expect('my_command', substrs=['a.out'])
+
+ self.runCmd("command script clear")
+
+ self.expect('command script list', matching=False,
+ substrs=['targetname',
+ 'longwait'])
+
+ self.expect('command script add -f foobar frame', error=True,
+ substrs=['cannot add command'])
+
+ # http://llvm.org/bugs/show_bug.cgi?id=11569
+ # LLDBSwigPythonCallCommand crashes when a command script returns an
+ # object
+ self.runCmd('command script add -f bug11569 bug11569')
+ # This should not crash.
+ self.runCmd('bug11569', check=False)
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/bug11569.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/bug11569.py
new file mode 100644
index 00000000000..3c124de79bf
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/bug11569.py
@@ -0,0 +1,6 @@
+def bug11569(debugger, args, result, dict):
+ """
+ http://llvm.org/bugs/show_bug.cgi?id=11569
+ LLDBSwigPythonCallCommand crashes when a command script returns an object.
+ """
+ return ["return", "a", "non-string", "should", "not", "crash", "LLDB"]
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/decorated.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/decorated.py
new file mode 100644
index 00000000000..f9707a5706a
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/decorated.py
@@ -0,0 +1,35 @@
+from __future__ import print_function
+
+import lldb
+
+
+@lldb.command()
+def decorated1(debugger, args, exe_ctx, result, dict):
+ """
+ Python command defined by @lldb.command
+ """
+ print("hello from decorated1", file=result)
+
+
+@lldb.command(doc="Python command defined by @lldb.command")
+def decorated2(debugger, args, exe_ctx, result, dict):
+ """
+ This docstring is overridden.
+ """
+ print("hello from decorated2", file=result)
+
+
+@lldb.command()
+def decorated3(debugger, args, result, dict):
+ """
+ Python command defined by @lldb.command
+ """
+ print("hello from decorated3", file=result)
+
+
+@lldb.command("decorated4")
+def _decorated4(debugger, args, exe_ctx, result, dict):
+ """
+ Python command defined by @lldb.command
+ """
+ print("hello from decorated4", file=result)
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/Makefile b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/Makefile
new file mode 100644
index 00000000000..9374aef487f
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/Makefile
@@ -0,0 +1,6 @@
+LEVEL = ../../../make
+
+C_SOURCES := main.c
+EXE := hello_world
+
+include $(LEVEL)/Makefile.rules
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/TestImport.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/TestImport.py
new file mode 100644
index 00000000000..bbeee1a5e32
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/TestImport.py
@@ -0,0 +1,76 @@
+"""Test custom import command to import files by path."""
+
+from __future__ import print_function
+
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class ImportTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_import_command(self):
+ """Import some Python scripts by path and test them"""
+ self.run_test()
+
+ def setUp(self):
+ # Call super's setUp().
+ TestBase.setUp(self)
+
+ def run_test(self):
+ """Import some Python scripts by path and test them."""
+
+ # This is the function to remove the custom commands in order to have a
+ # clean slate for the next test case.
+ def cleanup():
+ self.runCmd('command script delete foo2cmd', check=False)
+ self.runCmd('command script delete foocmd', check=False)
+ self.runCmd('command script delete foobarcmd', check=False)
+ self.runCmd('command script delete barcmd', check=False)
+ self.runCmd('command script delete barothercmd', check=False)
+ self.runCmd('command script delete TPcommandA', check=False)
+ self.runCmd('command script delete TPcommandB', check=False)
+
+ # Execute the cleanup function during test case tear down.
+ self.addTearDownHook(cleanup)
+
+ self.runCmd("command script import ./foo/foo.py --allow-reload")
+ self.runCmd("command script import ./foo/foo2.py --allow-reload")
+ self.runCmd("command script import ./foo/bar/foobar.py --allow-reload")
+ self.runCmd("command script import ./bar/bar.py --allow-reload")
+
+ self.expect("command script import ./nosuchfile.py",
+ error=True, startstr='error: module importing failed')
+ self.expect("command script import ./nosuchfolder/",
+ error=True, startstr='error: module importing failed')
+ self.expect("command script import ./foo/foo.py", error=False)
+
+ self.runCmd("command script import --allow-reload ./thepackage")
+ self.expect("TPcommandA", substrs=["hello world A"])
+ self.expect("TPcommandB", substrs=["hello world B"])
+
+ self.runCmd("script import dummymodule")
+ self.expect("command script import ./dummymodule.py", error=False)
+ self.expect(
+ "command script import --allow-reload ./dummymodule.py",
+ error=False)
+
+ self.runCmd("command script add -f foo.foo_function foocmd")
+ self.runCmd("command script add -f foobar.foo_function foobarcmd")
+ self.runCmd("command script add -f bar.bar_function barcmd")
+ self.expect("foocmd hello",
+ substrs=['foo says', 'hello'])
+ self.expect("foo2cmd hello",
+ substrs=['foo2 says', 'hello'])
+ self.expect("barcmd hello",
+ substrs=['barutil says', 'bar told me', 'hello'])
+ self.expect("barothercmd hello",
+ substrs=['barutil says', 'bar told me', 'hello'])
+ self.expect("foobarcmd hello",
+ substrs=['foobar says', 'hello'])
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/bar.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/bar.py
new file mode 100644
index 00000000000..444e00976ad
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/bar.py
@@ -0,0 +1,15 @@
+from __future__ import print_function
+
+
+def bar_function(debugger, args, result, dict):
+ global UtilityModule
+ print(UtilityModule.barutil_function("bar told me " + args), file=result)
+ return None
+
+
+def __lldb_init_module(debugger, session_dict):
+ global UtilityModule
+ UtilityModule = __import__("barutil")
+ debugger.HandleCommand(
+ "command script add -f bar.bar_function barothercmd")
+ return None
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/barutil.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/barutil.py
new file mode 100644
index 00000000000..70ecea30057
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/bar/barutil.py
@@ -0,0 +1,2 @@
+def barutil_function(x):
+ return "barutil says: " + x
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/dummymodule.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/dummymodule.py
new file mode 100644
index 00000000000..668a5b90ea4
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/dummymodule.py
@@ -0,0 +1,2 @@
+def no_useful_code(foo):
+ return foo
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/bar/foobar.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/bar/foobar.py
new file mode 100644
index 00000000000..6ef71064c9a
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/bar/foobar.py
@@ -0,0 +1,6 @@
+from __future__ import print_function
+
+
+def foo_function(debugger, args, result, dict):
+ print("foobar says " + args, file=result)
+ return None
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo.py
new file mode 100644
index 00000000000..1ccc3892939
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo.py
@@ -0,0 +1,6 @@
+from __future__ import print_function
+
+
+def foo_function(debugger, args, result, dict):
+ print("foo says " + args, file=result)
+ return None
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo2.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo2.py
new file mode 100644
index 00000000000..71657c299c2
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/foo/foo2.py
@@ -0,0 +1,11 @@
+from __future__ import print_function
+
+
+def foo2_function(debugger, args, result, dict):
+ print("foo2 says " + args, file=result)
+ return None
+
+
+def __lldb_init_module(debugger, session_dict):
+ debugger.HandleCommand("command script add -f foo2.foo2_function foo2cmd")
+ return None
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/main.c b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/main.c
new file mode 100644
index 00000000000..dffc8c77b04
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/main.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int main(int argc, char const *argv[]) {
+ printf("Hello world.\n"); // Set break point at this line.
+ if (argc == 1)
+ return 0;
+
+ // Waiting to be attached by the debugger, otherwise.
+ char line[100];
+ while (fgets(line, sizeof(line), stdin)) { // Waiting to be attached...
+ printf("input line=>%s\n", line);
+ }
+
+ printf("Exiting now\n");
+}
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/Makefile b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/Makefile
new file mode 100644
index 00000000000..7913aaa4b74
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/Makefile
@@ -0,0 +1,3 @@
+LEVEL = ../../../../make
+
+include $(LEVEL)/Makefile.rules
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/TestRdar12586188.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/TestRdar12586188.py
new file mode 100644
index 00000000000..fdcf8764e67
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/TestRdar12586188.py
@@ -0,0 +1,36 @@
+"""Check that we handle an ImportError in a special way when command script importing files."""
+
+from __future__ import print_function
+
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class Rdar12586188TestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_rdar12586188_command(self):
+ """Check that we handle an ImportError in a special way when command script importing files."""
+ self.run_test()
+
+ def setUp(self):
+ # Call super's setUp().
+ TestBase.setUp(self)
+
+ def run_test(self):
+ """Check that we handle an ImportError in a special way when command script importing files."""
+
+ self.expect(
+ "command script import ./fail12586188.py --allow-reload",
+ error=True,
+ substrs=['raise ImportError("I do not want to be imported")'])
+ self.expect(
+ "command script import ./fail212586188.py --allow-reload",
+ error=True,
+ substrs=['raise ValueError("I do not want to be imported")'])
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail12586188.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail12586188.py
new file mode 100644
index 00000000000..ea385e03e04
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail12586188.py
@@ -0,0 +1,4 @@
+def f(x):
+ return x + 1
+
+raise ImportError("I do not want to be imported")
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail212586188.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail212586188.py
new file mode 100644
index 00000000000..8dbc0e67ba1
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/rdar-12586188/fail212586188.py
@@ -0,0 +1,4 @@
+def f(x):
+ return x + 1
+
+raise ValueError("I do not want to be imported")
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitA.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitA.py
new file mode 100644
index 00000000000..9694b084295
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitA.py
@@ -0,0 +1,7 @@
+
+import six
+
+
+def command(debugger, command, result, internal_dict):
+ result.PutCString(six.u("hello world A"))
+ return None
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitB.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitB.py
new file mode 100644
index 00000000000..94a333bc696
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/TPunitB.py
@@ -0,0 +1,7 @@
+
+import six
+
+
+def command(debugger, command, result, internal_dict):
+ result.PutCString(six.u("hello world B"))
+ return None
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/__init__.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/__init__.py
new file mode 100644
index 00000000000..24cdea60f2c
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/import/thepackage/__init__.py
@@ -0,0 +1,11 @@
+from __future__ import absolute_import
+
+from . import TPunitA
+from . import TPunitB
+
+
+def __lldb_init_module(debugger, *args):
+ debugger.HandleCommand(
+ "command script add -f thepackage.TPunitA.command TPcommandA")
+ debugger.HandleCommand(
+ "command script add -f thepackage.TPunitB.command TPcommandB")
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/main.cpp b/lldb/packages/Python/lldbsuite/test/commands/command/script/main.cpp
new file mode 100644
index 00000000000..a0e9efd6ee0
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/main.cpp
@@ -0,0 +1,69 @@
+//===-- main.cpp ------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <fstream>
+#include <iostream>
+
+int
+product (int x, int y)
+{
+ int result = x * y;
+ return result;
+}
+
+int
+sum (int a, int b)
+{
+ int result = a + b;
+ return result;
+}
+
+int
+strange_max (int m, int n)
+{
+ if (m > n)
+ return m;
+ else if (n > m)
+ return n;
+ else
+ return 0;
+}
+
+int
+foo (int i, int j)
+{
+ if (strange_max (i, j) == i)
+ return product (i, j);
+ else if (strange_max (i, j) == j)
+ return sum (i, j);
+ else
+ return product (sum (i, i), sum (j, j));
+}
+
+int
+main(int argc, char const *argv[])
+{
+
+ int array[9];
+ memset(array,0,9*sizeof(int));
+
+ array[0] = foo (1238, 78392);
+ array[1] = foo (379265, 23674);
+ array[2] = foo (872934, 234);
+ array[3] = foo (1238, 78392);
+ array[4] = foo (379265, 23674);
+ array[5] = foo (872934, 234);
+ array[6] = foo (1238, 78392);
+ array[7] = foo (379265, 23674);
+ array[8] = foo (872934, 234);
+
+ return 0;
+}
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/mysto.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/mysto.py
new file mode 100644
index 00000000000..04eceb7eb93
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/mysto.py
@@ -0,0 +1,22 @@
+from __future__ import print_function
+
+import lldb
+
+
+def StepOver(debugger, args, result, dict):
+ """
+ Step over a given number of times instead of only just once
+ """
+ arg_split = args.split(" ")
+ print(type(arg_split))
+ count = int(arg_split[0])
+ for i in range(0, count):
+ debugger.GetSelectedTarget().GetProcess(
+ ).GetSelectedThread().StepOver(lldb.eOnlyThisThread)
+ print("step<%d>" % i)
+
+
+def __lldb_init_module(debugger, session_dict):
+ # by default, --synchronicity is set to synchronous
+ debugger.HandleCommand("command script add -f mysto.StepOver mysto")
+ return None
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/py_import b/lldb/packages/Python/lldbsuite/test/commands/command/script/py_import
new file mode 100644
index 00000000000..6c1f7b8185f
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/py_import
@@ -0,0 +1,13 @@
+script import sys, os
+script sys.path.append(os.path.join(os.getcwd(), os.pardir))
+script import welcome
+script import bug11569
+command script add welcome --class welcome.WelcomeCommand
+command script add targetname --class welcome.TargetnameCommand
+command script add longwait --function welcome.print_wait_impl
+command script import mysto.py --allow-reload
+command script add tell_sync --function welcome.check_for_synchro --synchronicity sync
+command script add tell_async --function welcome.check_for_synchro --synchronicity async
+command script add tell_curr --function welcome.check_for_synchro --synchronicity curr
+command script add takes_exe_ctx --function welcome.takes_exe_ctx
+command script import decorated.py
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script/welcome.py b/lldb/packages/Python/lldbsuite/test/commands/command/script/welcome.py
new file mode 100644
index 00000000000..0539d7c1721
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/welcome.py
@@ -0,0 +1,53 @@
+from __future__ import print_function
+import lldb
+import sys
+
+
+class WelcomeCommand(object):
+
+ def __init__(self, debugger, session_dict):
+ pass
+
+ def get_short_help(self):
+ return "Just a docstring for welcome_impl\nA command that says hello to LLDB users"
+
+ def __call__(self, debugger, args, exe_ctx, result):
+ print('Hello ' + args + ', welcome to LLDB', file=result)
+ return None
+
+
+class TargetnameCommand(object):
+
+ def __init__(self, debugger, session_dict):
+ pass
+
+ def __call__(self, debugger, args, exe_ctx, result):
+ target = debugger.GetSelectedTarget()
+ file = target.GetExecutable()
+ print('Current target ' + file.GetFilename(), file=result)
+ if args == 'fail':
+ result.SetError('a test for error in command')
+
+ def get_flags(self):
+ return lldb.eCommandRequiresTarget
+
+
+def print_wait_impl(debugger, args, result, dict):
+ result.SetImmediateOutputFile(sys.stdout)
+ print('Trying to do long task..', file=result)
+ import time
+ time.sleep(1)
+ print('Still doing long task..', file=result)
+ time.sleep(1)
+ print('Done; if you saw the delays I am doing OK', file=result)
+
+
+def check_for_synchro(debugger, args, result, dict):
+ if debugger.GetAsync():
+ print('I am running async', file=result)
+ if debugger.GetAsync() == False:
+ print('I am running sync', file=result)
+
+
+def takes_exe_ctx(debugger, args, exe_ctx, result, dict):
+ print(str(exe_ctx.GetTarget()), file=result)
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/.categories b/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/.categories
new file mode 100644
index 00000000000..3a3f4df6416
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/.categories
@@ -0,0 +1 @@
+cmdline
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/TestCommandScriptAlias.py b/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/TestCommandScriptAlias.py
new file mode 100644
index 00000000000..e454365edeb
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/TestCommandScriptAlias.py
@@ -0,0 +1,38 @@
+"""
+Test lldb Python commands.
+"""
+
+from __future__ import print_function
+
+
+import lldb
+from lldbsuite.test.lldbtest import *
+
+
+class CommandScriptAliasTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ def test(self):
+ self.pycmd_tests()
+
+ def pycmd_tests(self):
+ self.runCmd("command script import tcsacmd.py")
+ self.runCmd("command script add -f tcsacmd.some_command_here attach")
+
+ # This is the function to remove the custom commands in order to have a
+ # clean slate for the next test case.
+ def cleanup():
+ self.runCmd('command script delete attach', check=False)
+
+ # Execute the cleanup function during test case tear down.
+ self.addTearDownHook(cleanup)
+
+ # We don't want to display the stdout if not in TraceOn() mode.
+ if not self.TraceOn():
+ self.HideStdout()
+
+ self.expect('attach a', substrs=['Victory is mine'])
+ self.runCmd("command script delete attach")
+ # this can't crash but we don't care whether the actual attach works
+ self.runCmd('attach noprocessexistswiththisname', check=False)
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/tcsacmd.py b/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/tcsacmd.py
new file mode 100644
index 00000000000..8d3248c2723
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script_alias/tcsacmd.py
@@ -0,0 +1,11 @@
+from __future__ import print_function
+import lldb
+
+
+def some_command_here(debugger, command, result, d):
+ if command == "a":
+ print("Victory is mine", file=result)
+ return True
+ else:
+ print("Sadness for all", file=result)
+ return False
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/source/.categories b/lldb/packages/Python/lldbsuite/test/commands/command/source/.categories
new file mode 100644
index 00000000000..3a3f4df6416
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/source/.categories
@@ -0,0 +1 @@
+cmdline
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/source/.lldb b/lldb/packages/Python/lldbsuite/test/commands/command/source/.lldb
new file mode 100644
index 00000000000..ecbdcff4462
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/source/.lldb
@@ -0,0 +1,2 @@
+# one more level of indirection to stress the command interpreter reentrancy
+command source commands.txt
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/source/TestCommandSource.py b/lldb/packages/Python/lldbsuite/test/commands/command/source/TestCommandSource.py
new file mode 100644
index 00000000000..a30f0d497be
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/source/TestCommandSource.py
@@ -0,0 +1,35 @@
+"""
+Test that lldb command "command source" works correctly.
+"""
+
+from __future__ import print_function
+
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class CommandSourceTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @no_debug_info_test
+ def test_command_source(self):
+ """Test that lldb command "command source" works correctly."""
+
+ # Sourcing .lldb in the current working directory, which in turn imports
+ # the "my" package that defines the date() function.
+ self.runCmd("command source .lldb")
+
+ # Python should evaluate "my.date()" successfully.
+ command_interpreter = self.dbg.GetCommandInterpreter()
+ self.assertTrue(command_interpreter, VALID_COMMAND_INTERPRETER)
+ result = lldb.SBCommandReturnObject()
+ command_interpreter.HandleCommand("script my.date()", result)
+
+ import datetime
+ self.expect(result.GetOutput(), "script my.date() runs successfully",
+ exe=False,
+ substrs=[str(datetime.date.today())])
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/source/commands.txt b/lldb/packages/Python/lldbsuite/test/commands/command/source/commands.txt
new file mode 100644
index 00000000000..8e4de66d469
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/source/commands.txt
@@ -0,0 +1,2 @@
+script import my
+p 1 + 1
diff --git a/lldb/packages/Python/lldbsuite/test/commands/command/source/my.py b/lldb/packages/Python/lldbsuite/test/commands/command/source/my.py
new file mode 100644
index 00000000000..bd97fda3cbb
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/source/my.py
@@ -0,0 +1,7 @@
+from __future__ import print_function
+
+
+def date():
+ import datetime
+ today = datetime.date.today()
+ print(today)
OpenPOWER on IntegriCloud