[Lldb-commits] [lldb] 27ca945 - [lldb] add fallback for LLDB_PYTHON_RELATIVE_PATH

2021-12-02 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-12-02T21:13:35-08:00
New Revision: 27ca9458012caf8b556ce31fc7aaac571af878d2

URL: 
https://github.com/llvm/llvm-project/commit/27ca9458012caf8b556ce31fc7aaac571af878d2
DIFF: 
https://github.com/llvm/llvm-project/commit/27ca9458012caf8b556ce31fc7aaac571af878d2.diff

LOG: [lldb] add fallback for LLDB_PYTHON_RELATIVE_PATH

Some pythons are configured to set platlib somewhere outside of their
sys.prefix.   It's important that we at least use some reasonable
default for LLDB_PYTHON_RELATIVE_PATH even in that case, because
even if the user overrides it on the cmake invocation, cmake will
still be called without the override in order to build tablegen.

Reviewed By: JDevlieghere, clayborg

Differential Revision: https://reviews.llvm.org/D114973

Added: 


Modified: 
lldb/bindings/python/get-python-config.py

Removed: 




diff  --git a/lldb/bindings/python/get-python-config.py 
b/lldb/bindings/python/get-python-config.py
index 5b670d77451a3..32d82a54c160f 100755
--- a/lldb/bindings/python/get-python-config.py
+++ b/lldb/bindings/python/get-python-config.py
@@ -30,7 +30,17 @@ def main():
 # lldb's python lib will be put in the correct place for python to 
find it.
 # If not, you'll have to use lldb -P or lldb 
-print-script-interpreter-info
 # to figure out where it is.
-print(relpath_nodots(sysconfig.get_path("platlib"), sys.prefix))
+try:
+print(relpath_nodots(sysconfig.get_path("platlib"), sys.prefix))
+except ValueError:
+# Try to fall back to something reasonable if sysconfig's platlib
+# is outside of sys.prefix
+if os.name == 'posix':
+print('lib/python%d.%d/site-packages' % sys.version_info[:2])
+elif os.name == 'nt':
+print('Lib\\site-packages')
+else:
+raise
 elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH":
 tried = list()
 exe = sys.executable
@@ -57,4 +67,4 @@ def main():
 parser.error(f"unknown variable {args.variable_name}")
 
 if __name__ == '__main__':
-main()
\ No newline at end of file
+main()



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 6327071 - [lldb] remove usage of distutils, fix python path on debian/ubuntu

2021-11-17 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-17T13:14:30-08:00
New Revision: 63270710f13af10808aac980795128db127153f5

URL: 
https://github.com/llvm/llvm-project/commit/63270710f13af10808aac980795128db127153f5
DIFF: 
https://github.com/llvm/llvm-project/commit/63270710f13af10808aac980795128db127153f5.diff

LOG: [lldb] remove usage of distutils, fix python path on debian/ubuntu

distutils is deprecated and will be removed, so we shouldn't be
using it.

We were using it to compute LLDB_PYTHON_RELATIVE_PATH.

Discussing a similar issue
[at python.org](https://bugs.python.org/issue41282), Filipe LaĆ­ns said:

If you are relying on the value of distutils.sysconfig.get_python_lib()
as you shown in your system, you probably don't want to. That
directory (dist-packages) should be for Debian provided packages
only, so moving to sysconfig.get_path() would be a good thing,
as it has the correct value for user installed packages on your
system.

So I propose using a relative path from `sys.prefix` to
`sysconfig.get_path("platlib")` instead.

On Mac and windows, this results in the same paths as we had before,
which are `lib/python3.9/site-packages` and `Lib\site-packages`,
respectively.

On ubuntu however, this will change the path from
`lib/python3/dist-packages` to `lib/python3.9/site-packages`.

This change seems to be correct, as Filipe said above, `dist-packages`
belongs to the distribution, not us.

Reviewed By: labath

Differential Revision: https://reviews.llvm.org/D114106

Added: 


Modified: 
lldb/CMakeLists.txt
lldb/bindings/python/get-python-config.py

Removed: 




diff  --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index c45064d9bef23..5c2dac3f51f6d 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -32,9 +32,9 @@ endif()
 
 if (LLDB_ENABLE_PYTHON)
   set(cachestring_LLDB_PYTHON_RELATIVE_PATH
-"Path where Python modules are installed, relative to install prefix")
+"Path where Python modules are installed, relative to LLDB's install 
prefix")
   set(cachestring_LLDB_PYTHON_EXE_RELATIVE_PATH
-"Path to python interpreter exectuable, relative to install prefix")
+"Path to python interpreter exectuable, relative to python's install 
prefix")
   set(cachestring_LLDB_PYTHON_EXT_SUFFIX
 "Filename extension for native code python modules")
 

diff  --git a/lldb/bindings/python/get-python-config.py 
b/lldb/bindings/python/get-python-config.py
index 978dc07b3e4c0..5b670d77451a3 100755
--- a/lldb/bindings/python/get-python-config.py
+++ b/lldb/bindings/python/get-python-config.py
@@ -4,7 +4,6 @@
 import sys
 import argparse
 import sysconfig
-import distutils.sysconfig
 
 
 def relpath_nodots(path, base):
@@ -20,7 +19,18 @@ def main():
 parser.add_argument("variable_name")
 args = parser.parse_args()
 if args.variable_name == "LLDB_PYTHON_RELATIVE_PATH":
-print(distutils.sysconfig.get_python_lib(True, False, ''))
+# LLDB_PYTHON_RELATIVE_PATH is the relative path from lldb's prefix
+# to where lldb's python libraries will be installed.
+#
+# The way we're going to compute this is to take the relative path from
+# PYTHON'S prefix to where python libraries are supposed to be
+# installed.
+#
+# The result is if LLDB and python are give the same prefix, then
+# lldb's python lib will be put in the correct place for python to 
find it.
+# If not, you'll have to use lldb -P or lldb 
-print-script-interpreter-info
+# to figure out where it is.
+print(relpath_nodots(sysconfig.get_path("platlib"), sys.prefix))
 elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH":
 tried = list()
 exe = sys.executable



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] f07ddbc - [lldb] build failure for LLDB_PYTHON_EXE_RELATIVE_PATH on greendragon

2021-11-17 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-17T09:26:24-08:00
New Revision: f07ddbc620a0466ebbb41ccbc78f20a8591bed5a

URL: 
https://github.com/llvm/llvm-project/commit/f07ddbc620a0466ebbb41ccbc78f20a8591bed5a
DIFF: 
https://github.com/llvm/llvm-project/commit/f07ddbc620a0466ebbb41ccbc78f20a8591bed5a.diff

LOG: [lldb] build failure for LLDB_PYTHON_EXE_RELATIVE_PATH on greendragon

see: https://green.lab.llvm.org/green/view/LLDB/job/lldb-cmake/38387/console

```
Could not find a relative path to sys.executable under sys.prefix
tried: /usr/local/opt/python/bin/python3.7
tried: 
/usr/local/opt/python/bin/../Frameworks/Python.framework/Versions/3.7/bin/python3.7
sys.prefix: 
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7
```

It was unable to find LLDB_PYTHON_EXE_RELATIVE_PATH because it was not resolving
the real path of sys.prefix.

caused by: https://reviews.llvm.org/D113650

Added: 


Modified: 
lldb/bindings/python/get-python-config.py

Removed: 




diff  --git a/lldb/bindings/python/get-python-config.py 
b/lldb/bindings/python/get-python-config.py
index 507a8aa072076..978dc07b3e4c0 100755
--- a/lldb/bindings/python/get-python-config.py
+++ b/lldb/bindings/python/get-python-config.py
@@ -24,19 +24,21 @@ def main():
 elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH":
 tried = list()
 exe = sys.executable
+prefix = os.path.realpath(sys.prefix)
 while True:
 try:
-print(relpath_nodots(exe, sys.prefix))
+print(relpath_nodots(exe, prefix))
 break
 except ValueError:
 tried.append(exe)
 if os.path.islink(exe):
-exe = os.path.join(os.path.dirname(exe), os.readlink(exe))
+exe = os.path.join(os.path.realpath(os.path.dirname(exe)), 
os.readlink(exe))
 continue
 else:
 print("Could not find a relative path to sys.executable 
under sys.prefix", file=sys.stderr)
 for e in tried:
 print("tried:", e, file=sys.stderr)
+print("realpath(sys.prefix):", prefix, file=sys.stderr)
 print("sys.prefix:", sys.prefix, file=sys.stderr)
 sys.exit(1)
 elif args.variable_name == "LLDB_PYTHON_EXT_SUFFIX":



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] ae389b2 - [lldb] use EXT_SUFFIX for python extension

2021-11-16 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-16T14:32:26-08:00
New Revision: ae389b2450bd604a3f3bbe5b09b333b2d99801dd

URL: 
https://github.com/llvm/llvm-project/commit/ae389b2450bd604a3f3bbe5b09b333b2d99801dd
DIFF: 
https://github.com/llvm/llvm-project/commit/ae389b2450bd604a3f3bbe5b09b333b2d99801dd.diff

LOG: [lldb] use EXT_SUFFIX for python extension

LLDB doesn't use only the python stable ABI, which means loading
it into an incompatible python can cause the process to crash.
_lldb.so should be named with the full EXT_SUFFIX from sysconfig
-- such as _lldb.cpython-39-darwin.so -- so this doesn't happen.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D112972

Added: 


Modified: 
lldb/CMakeLists.txt
lldb/bindings/python/CMakeLists.txt
lldb/bindings/python/get-python-config.py

Removed: 




diff  --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 1a54addf3d557..c45064d9bef23 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -35,8 +35,10 @@ if (LLDB_ENABLE_PYTHON)
 "Path where Python modules are installed, relative to install prefix")
   set(cachestring_LLDB_PYTHON_EXE_RELATIVE_PATH
 "Path to python interpreter exectuable, relative to install prefix")
+  set(cachestring_LLDB_PYTHON_EXT_SUFFIX
+"Filename extension for native code python modules")
 
-  foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH)
+  foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH 
LLDB_PYTHON_EXT_SUFFIX)
 if(NOT DEFINED ${var} AND NOT CMAKE_CROSSCOMPILING)
   execute_process(
 COMMAND ${Python3_EXECUTABLE}

diff  --git a/lldb/bindings/python/CMakeLists.txt 
b/lldb/bindings/python/CMakeLists.txt
index 5aabaf574636c..1f7ed18a2a0cb 100644
--- a/lldb/bindings/python/CMakeLists.txt
+++ b/lldb/bindings/python/CMakeLists.txt
@@ -149,15 +149,7 @@ function(finish_swig_python swig_target 
lldb_python_bindings_dir lldb_python_tar
   else()
 set(LIBLLDB_SYMLINK_DEST 
"${LLVM_SHLIB_OUTPUT_INTDIR}/liblldb${CMAKE_SHARED_LIBRARY_SUFFIX}")
   endif()
-  if(WIN32)
-if(CMAKE_BUILD_TYPE STREQUAL Debug)
-  set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb_d.pyd")
-else()
-  set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb.pyd")
-endif()
-  else()
-set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb.so")
-  endif()
+  set(LIBLLDB_SYMLINK_OUTPUT_FILE "_lldb${LLDB_PYTHON_EXT_SUFFIX}")
   create_relative_symlink(${swig_target} ${LIBLLDB_SYMLINK_DEST}
   ${lldb_python_target_dir} 
${LIBLLDB_SYMLINK_OUTPUT_FILE})
 

diff  --git a/lldb/bindings/python/get-python-config.py 
b/lldb/bindings/python/get-python-config.py
index 7acad6d2d4c9a..507a8aa072076 100755
--- a/lldb/bindings/python/get-python-config.py
+++ b/lldb/bindings/python/get-python-config.py
@@ -39,7 +39,8 @@ def main():
 print("tried:", e, file=sys.stderr)
 print("sys.prefix:", sys.prefix, file=sys.stderr)
 sys.exit(1)
-
+elif args.variable_name == "LLDB_PYTHON_EXT_SUFFIX":
+print(sysconfig.get_config_var('EXT_SUFFIX'))
 else:
 parser.error(f"unknown variable {args.variable_name}")
 



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 4c2cf3a - [lldb] fix -print-script-interpreter-info on windows

2021-11-16 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-16T13:50:20-08:00
New Revision: 4c2cf3a314d9131b1b288e7c8ab0c75ac1b2be1d

URL: 
https://github.com/llvm/llvm-project/commit/4c2cf3a314d9131b1b288e7c8ab0c75ac1b2be1d
DIFF: 
https://github.com/llvm/llvm-project/commit/4c2cf3a314d9131b1b288e7c8ab0c75ac1b2be1d.diff

LOG: [lldb] fix -print-script-interpreter-info on windows

Apparently "{sys.prefix}/bin/python3" isn't where you find the
python interpreter on windows, so the test I wrote for
-print-script-interpreter-info is failing.

We can't rely on sys.executable at runtime, because that will point
to lldb.exe not python.exe.

We can't just record sys.executable from build time, because python
could have been moved to a different location.

But it should be OK to apply relative path from sys.prefix to sys.executable
from build-time to the sys.prefix at runtime.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D113650

Added: 
lldb/bindings/python/get-python-config.py

Modified: 
lldb/CMakeLists.txt
lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/test/API/functionalities/paths/TestPaths.py

Removed: 




diff  --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 028dadbb8c73c..1a54addf3d557 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -31,24 +31,28 @@ if (WIN32)
 endif()
 
 if (LLDB_ENABLE_PYTHON)
-  if (NOT CMAKE_CROSSCOMPILING)
-execute_process(
-  COMMAND ${Python3_EXECUTABLE}
-  -c "import distutils.sysconfig; 
print(distutils.sysconfig.get_python_lib(True, False, ''))"
-  OUTPUT_VARIABLE LLDB_PYTHON_DEFAULT_RELATIVE_PATH
-  OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-file(TO_CMAKE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH} 
LLDB_PYTHON_DEFAULT_RELATIVE_PATH)
-  else ()
-if ("${LLDB_PYTHON_RELATIVE_PATH}" STREQUAL "")
-  message(FATAL_ERROR
-"Crosscompiling LLDB with Python requires manually setting
-LLDB_PYTHON_RELATIVE_PATH.")
-endif ()
-  endif ()
-
-  set(LLDB_PYTHON_RELATIVE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH}
-CACHE STRING "Path where Python modules are installed, relative to install 
prefix")
+  set(cachestring_LLDB_PYTHON_RELATIVE_PATH
+"Path where Python modules are installed, relative to install prefix")
+  set(cachestring_LLDB_PYTHON_EXE_RELATIVE_PATH
+"Path to python interpreter exectuable, relative to install prefix")
+
+  foreach(var LLDB_PYTHON_RELATIVE_PATH LLDB_PYTHON_EXE_RELATIVE_PATH)
+if(NOT DEFINED ${var} AND NOT CMAKE_CROSSCOMPILING)
+  execute_process(
+COMMAND ${Python3_EXECUTABLE}
+  ${CMAKE_CURRENT_SOURCE_DIR}/bindings/python/get-python-config.py
+  ${var}
+OUTPUT_VARIABLE value
+OUTPUT_STRIP_TRAILING_WHITESPACE)
+  file(TO_CMAKE_PATH "${value}" value)
+  set(${var} ${value} CACHE STRING ${cachestring_${var}})
+else()
+  if ("${${var}}" STREQUAL "")
+message(FATAL_ERROR
+  "Crosscompiling LLDB with Python requires manually setting ${var}.")
+  endif()
+endif()
+  endforeach()
 endif ()
 
 if (LLDB_ENABLE_LUA)

diff  --git a/lldb/bindings/python/get-python-config.py 
b/lldb/bindings/python/get-python-config.py
new file mode 100755
index 0..7acad6d2d4c9a
--- /dev/null
+++ b/lldb/bindings/python/get-python-config.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+import os
+import sys
+import argparse
+import sysconfig
+import distutils.sysconfig
+
+
+def relpath_nodots(path, base):
+rel = os.path.normpath(os.path.relpath(path, base))
+assert not os.path.isabs(rel)
+parts = rel.split(os.path.sep)
+if parts and parts[0] == '..':
+raise ValueError(f"{path} is not under {base}")
+return rel
+
+def main():
+parser = argparse.ArgumentParser(description="extract cmake variables from 
python")
+parser.add_argument("variable_name")
+args = parser.parse_args()
+if args.variable_name == "LLDB_PYTHON_RELATIVE_PATH":
+print(distutils.sysconfig.get_python_lib(True, False, ''))
+elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH":
+tried = list()
+exe = sys.executable
+while True:
+try:
+print(relpath_nodots(exe, sys.prefix))
+break
+except ValueError:
+tried.append(exe)
+if os.path.islink(exe):
+exe = os.path.join(os.path.dirname(exe), os.readlink(exe))
+continue
+else:
+print("Could not find a relative path to sys.executable 
under sys.prefix", file=sys.stderr)
+for e in tried:
+print("tried:", e, file=sys.stderr)
+print("sys.prefix:", sys.prefix, file=sys.stderr)
+sys.exit(1)
+
+   

[Lldb-commits] [lldb] 19cd6f3 - [lldb] temporarily disable TestPaths.test_interpreter_info on windows

2021-11-12 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-12T15:41:39-08:00
New Revision: 19cd6f31d83ec9cf4712e76668b2076a4c9741e4

URL: 
https://github.com/llvm/llvm-project/commit/19cd6f31d83ec9cf4712e76668b2076a4c9741e4
DIFF: 
https://github.com/llvm/llvm-project/commit/19cd6f31d83ec9cf4712e76668b2076a4c9741e4.diff

LOG: [lldb] temporarily disable TestPaths.test_interpreter_info on windows

I'm disabling this test until the fix is reviewed
(here https://reviews.llvm.org/D113650/)

Added: 


Modified: 
lldb/test/API/functionalities/paths/TestPaths.py

Removed: 




diff  --git a/lldb/test/API/functionalities/paths/TestPaths.py 
b/lldb/test/API/functionalities/paths/TestPaths.py
index da26da28a562..7b00f2126ec5 100644
--- a/lldb/test/API/functionalities/paths/TestPaths.py
+++ b/lldb/test/API/functionalities/paths/TestPaths.py
@@ -51,6 +51,8 @@ def test_interpreter_info(self):
 stream = lldb.SBStream()
 self.assertTrue(info_sd.GetAsJSON(stream).Success())
 info = json.loads(stream.GetData())
+if os.name == 'nt': #FIXME
+return
 prefix = info['prefix']
 self.assertEqual(os.path.realpath(sys.prefix), 
os.path.realpath(prefix))
 self.assertEqual(



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] bbef51e - [lldb] make it easier to find LLDB's python

2021-11-10 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-10T10:33:34-08:00
New Revision: bbef51eb43c2e8f8e36fbbc0d0b4cca75b6f0863

URL: 
https://github.com/llvm/llvm-project/commit/bbef51eb43c2e8f8e36fbbc0d0b4cca75b6f0863
DIFF: 
https://github.com/llvm/llvm-project/commit/bbef51eb43c2e8f8e36fbbc0d0b4cca75b6f0863.diff

LOG: [lldb] make it easier to find LLDB's python

It is surprisingly difficult to write a simple python script that
can reliably `import lldb` without failing, or crashing.   I'm
currently resorting to convolutions like this:

def find_lldb(may_reexec=False):
if prefix := os.environ.get('LLDB_PYTHON_PREFIX'):
if os.path.realpath(prefix) != 
os.path.realpath(sys.prefix):
raise Exception("cannot import lldb.\n"
f"  sys.prefix should be: {prefix}\n"
f"  but it is: {sys.prefix}")
else:
line1, line2 = subprocess.run(
['lldb', '-x', '-b', '-o', 'script 
print(sys.prefix)'],
encoding='utf8', stdout=subprocess.PIPE,
check=True).stdout.strip().splitlines()
assert line1.strip() == '(lldb) script 
print(sys.prefix)'
prefix = line2.strip()
os.environ['LLDB_PYTHON_PREFIX'] = prefix

if sys.prefix != prefix:
if not may_reexec:
raise Exception(
"cannot import lldb.\n" +
f"  This python, at {sys.prefix}\n"
f"  does not math LLDB's python at 
{prefix}")
os.environ['LLDB_PYTHON_PREFIX'] = prefix
python_exe = os.path.join(prefix, 'bin', 'python3')
os.execl(python_exe, python_exe, *sys.argv)

lldb_path = subprocess.run(['lldb', '-P'],
check=True, stdout=subprocess.PIPE,
encoding='utf8').stdout.strip()

sys.path = [lldb_path] + sys.path

This patch aims to replace all that with:

  #!/usr/bin/env lldb-python
  import lldb
  ...

... by adding the following features:

* new command line option: --print-script-interpreter-info.  This
   prints language-specific information about the script interpreter
   in JSON format.

* new tool (unix only): lldb-python which finds python and exec's it.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D112973

Added: 
lldb/bindings/python/lldb-python

Modified: 
lldb/bindings/interface/SBDebugger.i
lldb/bindings/python/CMakeLists.txt
lldb/docs/man/lldb.rst
lldb/include/lldb/API/SBDebugger.h
lldb/include/lldb/Interpreter/ScriptInterpreter.h
lldb/source/API/SBDebugger.cpp
lldb/source/Interpreter/ScriptInterpreter.cpp
lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
lldb/test/API/functionalities/paths/TestPaths.py
lldb/test/Shell/Driver/TestHelp.test
lldb/tools/driver/Driver.cpp
lldb/tools/driver/Driver.h
lldb/tools/driver/Options.td

Removed: 




diff  --git a/lldb/bindings/interface/SBDebugger.i 
b/lldb/bindings/interface/SBDebugger.i
index cf4411980cc30..aae72dd513940 100644
--- a/lldb/bindings/interface/SBDebugger.i
+++ b/lldb/bindings/interface/SBDebugger.i
@@ -479,6 +479,8 @@ public:
 lldb::SBTypeSynthetic
 GetSyntheticForType (lldb::SBTypeNameSpecifier);
 
+SBStructuredData GetScriptInterpreterInfo(ScriptLanguage);
+
 STRING_EXTENSION(SBDebugger)
 
 %feature("docstring",

diff  --git a/lldb/bindings/python/CMakeLists.txt 
b/lldb/bindings/python/CMakeLists.txt
index 9422ee00cb5fc..5aabaf574636c 100644
--- a/lldb/bindings/python/CMakeLists.txt
+++ b/lldb/bindings/python/CMakeLists.txt
@@ -23,6 +23,18 @@ add_custom_target(swig_wrapper_python ALL DEPENDS
   ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
 )
 
+if (NOT WIN32)
+add_custom_command(
+  OUTPUT  ${LLVM_RUNTIME_OUTPUT_INTDIR}/lldb-python
+  VERBATIM
+  COMMAND ${CMAKE_COMMAND} -E copy lldb-python 
${LLVM_RUNTIME_OUTPUT_INTDIR}/lldb-python
+  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+add_custom_target(lldb-python-wrapper ALL DEPENDS
+  ${LLVM_RUNTIME_OUTPUT_INTDIR}/lldb-python
+)
+endif()
+
 function(create_python_package swig_target working_dir pkg_dir)
   cmake_parse_arguments(ARG 

[Lldb-commits] [lldb] 531d877 - [lldb] Fix TestEchoCommands.test again

2021-11-04 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-04T01:24:25-07:00
New Revision: 531d877ee6410a94f5b4cb888d3c785d6ef0552c

URL: 
https://github.com/llvm/llvm-project/commit/531d877ee6410a94f5b4cb888d3c785d6ef0552c
DIFF: 
https://github.com/llvm/llvm-project/commit/531d877ee6410a94f5b4cb888d3c785d6ef0552c.diff

LOG: [lldb] Fix TestEchoCommands.test again

In 7f01f78593d6 [lldb] update TestEchoCommands -- I fixed this test,
but not on windows, becuase I used  some unix shell syntax that
doesn't work with cmd.exe.   Fixed it so it will work in both.
Test logic is the same.

This is a trivial fix, so bypassing review to get the build clean again
ASAP.

Added: 


Modified: 
lldb/test/Shell/Settings/TestEchoCommands.test

Removed: 




diff  --git a/lldb/test/Shell/Settings/TestEchoCommands.test 
b/lldb/test/Shell/Settings/TestEchoCommands.test
index a667456303573..234b9742bfa2a 100644
--- a/lldb/test/Shell/Settings/TestEchoCommands.test
+++ b/lldb/test/Shell/Settings/TestEchoCommands.test
@@ -1,4 +1,8 @@
 # RUN: %lldb -x -b -o 'settings set interpreter.echo-comment-commands true'  
-s %S/Inputs/EchoCommandsTest.in | FileCheck %S/Inputs/EchoCommandsAll.out
 # RUN: %lldb -x -b -o 'settings set interpreter.echo-comment-commands false' 
-s %S/Inputs/EchoCommandsTest.in | FileCheck 
%S/Inputs/EchoCommandsNoComments.out
 # RUN: %lldb -x -b -o 'settings set interpreter.echo-commands false' 
-s %S/Inputs/EchoCommandsTest.in | FileCheck %S/Inputs/EchoCommandsNone.out
-# RUN: ( echo start ; %lldb -x -b --source-quietly -s 
%S/Inputs/EchoCommandsTest.in ; echo done ) | FileCheck  
%S/Inputs/EchoCommandsQuiet.out
+
+RUN: echo start >%t.file
+RUN: %lldb -x -b --source-quietly -s %S/Inputs/EchoCommandsTest.in >>%t.file
+RUN: echo done >>%t.file
+RUN: FileCheck  %S/Inputs/EchoCommandsQuiet.out <%t.file



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 7f01f78 - [lldb] update TestEchoCommands

2021-11-02 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-02T14:30:08-07:00
New Revision: 7f01f78593d68741f1a26911e8cecca9805b3fa4

URL: 
https://github.com/llvm/llvm-project/commit/7f01f78593d68741f1a26911e8cecca9805b3fa4
DIFF: 
https://github.com/llvm/llvm-project/commit/7f01f78593d68741f1a26911e8cecca9805b3fa4.diff

LOG: [lldb] update TestEchoCommands

Followup to https://reviews.llvm.org/D112988

Sorry, I broke this test.   The test was verifying the bad behavior
of --source-quietly that the previous change fixed -- namely that
it still echos the initial list of startup commands while
sourcing them.

Updated the test to verify that --source-quietly is quiet, rather than
loud.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D113047

Added: 


Modified: 
lldb/test/Shell/Settings/Inputs/EchoCommandsQuiet.out
lldb/test/Shell/Settings/TestEchoCommands.test

Removed: 




diff  --git a/lldb/test/Shell/Settings/Inputs/EchoCommandsQuiet.out 
b/lldb/test/Shell/Settings/Inputs/EchoCommandsQuiet.out
index 12ad094292dea..11eee55f1f768 100644
--- a/lldb/test/Shell/Settings/Inputs/EchoCommandsQuiet.out
+++ b/lldb/test/Shell/Settings/Inputs/EchoCommandsQuiet.out
@@ -1,2 +1,4 @@
-# CHECK: (lldb) command source -s 1 {{.*\n}}
-# CHECK-NEXT: (lldb) command source -s 1 {{.*\n}}
+CHECK: start
+CHECK-NOT: source
+CHECK-NOT: lldb
+CHECK-NEXT: done

diff  --git a/lldb/test/Shell/Settings/TestEchoCommands.test 
b/lldb/test/Shell/Settings/TestEchoCommands.test
index 67547eaabf89c..a667456303573 100644
--- a/lldb/test/Shell/Settings/TestEchoCommands.test
+++ b/lldb/test/Shell/Settings/TestEchoCommands.test
@@ -1,4 +1,4 @@
 # RUN: %lldb -x -b -o 'settings set interpreter.echo-comment-commands true'  
-s %S/Inputs/EchoCommandsTest.in | FileCheck %S/Inputs/EchoCommandsAll.out
 # RUN: %lldb -x -b -o 'settings set interpreter.echo-comment-commands false' 
-s %S/Inputs/EchoCommandsTest.in | FileCheck 
%S/Inputs/EchoCommandsNoComments.out
 # RUN: %lldb -x -b -o 'settings set interpreter.echo-commands false' 
-s %S/Inputs/EchoCommandsTest.in | FileCheck %S/Inputs/EchoCommandsNone.out
-# RUN: %lldb -x -b --source-quietly  
-s %S/Inputs/EchoCommandsTest.in | FileCheck %S/Inputs/EchoCommandsQuiet.out
+# RUN: ( echo start ; %lldb -x -b --source-quietly -s 
%S/Inputs/EchoCommandsTest.in ; echo done ) | FileCheck  
%S/Inputs/EchoCommandsQuiet.out



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] e2a6c08 - [lldb] fix --source-quietly

2021-11-02 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-11-02T11:01:55-07:00
New Revision: e2a6c08bbc385b38e02f4e5d31d1cf6d4403f066

URL: 
https://github.com/llvm/llvm-project/commit/e2a6c08bbc385b38e02f4e5d31d1cf6d4403f066
DIFF: 
https://github.com/llvm/llvm-project/commit/e2a6c08bbc385b38e02f4e5d31d1cf6d4403f066.diff

LOG: [lldb] fix --source-quietly

Jim says:

lldb has a -Q or --source-quietly option, which supposedly does:

--source-quietly Tells the debugger to execute this one-line lldb 
command before any file has been loaded.

That seems like a weird description, since we don't generally use source for 
one line entries, but anyway, let's try it:

> $LLDB_LLVM/clean-mono/build/Debug/bin/lldb -Q "script print('I should be 
quiet')" a.out -O "script print('I should be before')" -o "script print('I 
should be after')"
(lldb) script print('I should be before')
I should be before
(lldb) target create "script print('I should be quiet')"
error: unable to find executable for 'script print('I should be quiet')'

That was weird.  The first real -O gets sourced but not quietly, then the 
argument to the -Q gets treated as the target.

> $LLDB_LLVM/clean-mono/build/Debug/bin/lldb -Q a.out -O "script print('I 
should be before')" -o "script print('I should be after')"
(lldb) script print('I should be before')
I should be before
(lldb) target create "a.out"
Current executable set to '/tmp/a.out' (x86_64).
(lldb) script print('I should be after')
I should be after

Well, that's a little better, but the -Q option seems to have done nothing.

---

This fixes the description of --source-quietly, as well as causing it
to actually suppress echoing while executing the initialization
commands.

Reviewed By: jingham

Differential Revision: https://reviews.llvm.org/D112988

Added: 
lldb/test/Shell/Driver/TestQuiet.test

Modified: 
lldb/docs/man/lldb.rst
lldb/tools/driver/Driver.cpp
lldb/tools/driver/Options.td

Removed: 




diff  --git a/lldb/docs/man/lldb.rst b/lldb/docs/man/lldb.rst
index b75288db380de..35db1dc68c129 100644
--- a/lldb/docs/man/lldb.rst
+++ b/lldb/docs/man/lldb.rst
@@ -111,7 +111,7 @@ COMMANDS
 
 .. option:: --source-quietly
 
- Tells the debugger to execute this one-line lldb command before any file has 
been loaded.
+ Tells the debugger not to echo commands while sourcing files or one-line 
commands provided on the command line.
 
 .. option:: --source 
 

diff  --git a/lldb/test/Shell/Driver/TestQuiet.test 
b/lldb/test/Shell/Driver/TestQuiet.test
new file mode 100644
index 0..8598792aeba07
--- /dev/null
+++ b/lldb/test/Shell/Driver/TestQuiet.test
@@ -0,0 +1,7 @@
+RUN: %lldb -b -Q -o "expr 40 + 2" | FileCheck %s
+RUN: %lldb -b -Q -O "expr 40 + 2" | FileCheck %s
+
+CHECK-NOT: expr
+CHECK-NOT: lldb
+CHECK-NOT: source
+CHECK: 42
\ No newline at end of file

diff  --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index a6a4a2a1b80b8..df070ebe4db8d 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -609,6 +609,7 @@ int Driver::MainLoop() {
 options.SetSpawnThread(false);
 options.SetStopOnError(true);
 options.SetStopOnCrash(m_option_data.m_batch);
+options.SetEchoCommands(!m_option_data.m_source_quietly);
 
 SBCommandInterpreterRunResult results =
 m_debugger.RunCommandInterpreter(options);

diff  --git a/lldb/tools/driver/Options.td b/lldb/tools/driver/Options.td
index 8bcb0e7bc52e9..be2b4bfa30044 100644
--- a/lldb/tools/driver/Options.td
+++ b/lldb/tools/driver/Options.td
@@ -110,7 +110,7 @@ def: Flag<["-"], "b">,
   Group;
 
 def source_quietly: F<"source-quietly">,
-  HelpText<"Tells the debugger to execute this one-line lldb command before 
any file has been loaded.">,
+  HelpText<"Tells the debugger not to echo commands while sourcing files or 
one-line commands provided on the command line.">,
   Group;
 def: Flag<["-"], "Q">,
   Alias,



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 8ac5a66 - [lldb] improve the help strings for gdb-remote and kdp-remote

2021-10-19 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2021-10-19T13:08:21-07:00
New Revision: 8ac5a6641fa4d742fb4599b485c40700e773f01f

URL: 
https://github.com/llvm/llvm-project/commit/8ac5a6641fa4d742fb4599b485c40700e773f01f
DIFF: 
https://github.com/llvm/llvm-project/commit/8ac5a6641fa4d742fb4599b485c40700e773f01f.diff

LOG: [lldb] improve the help strings for gdb-remote and kdp-remote

The help string can be more helpful by explaining these are
aliases for 'process connect'

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D111965

Added: 


Modified: 
lldb/source/Interpreter/CommandInterpreter.cpp

Removed: 




diff  --git a/lldb/source/Interpreter/CommandInterpreter.cpp 
b/lldb/source/Interpreter/CommandInterpreter.cpp
index d5426ba1b6db..301bf949feef 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -744,8 +744,10 @@ void CommandInterpreter::LoadCommandDictionary() {
   std::unique_ptr connect_gdb_remote_cmd_up(
   new CommandObjectRegexCommand(
   *this, "gdb-remote",
-  "Connect to a process via remote GDB server.  "
-  "If no host is specifed, localhost is assumed.",
+  "Connect to a process via remote GDB server.\n"
+  "If no host is specifed, localhost is assumed.\n"
+  "gdb-remote is an abbreviation for 'process connect --plugin "
+  "gdb-remote connect://:'\n",
   "gdb-remote [:]", 2, 0, false));
   if (connect_gdb_remote_cmd_up) {
 if (connect_gdb_remote_cmd_up->AddRegexCommand(
@@ -762,9 +764,10 @@ void CommandInterpreter::LoadCommandDictionary() {
   std::unique_ptr connect_kdp_remote_cmd_up(
   new CommandObjectRegexCommand(
   *this, "kdp-remote",
-  "Connect to a process via remote KDP server.  "
-  "If no UDP port is specified, port 41139 is "
-  "assumed.",
+  "Connect to a process via remote KDP server.\n"
+  "If no UDP port is specified, port 41139 is assumed.\n"
+  "kdp-remote is an abbreviation for 'process connect --plugin "
+  "kdp-remote udp://:'\n",
   "kdp-remote [:]", 2, 0, false));
   if (connect_kdp_remote_cmd_up) {
 if (connect_kdp_remote_cmd_up->AddRegexCommand(



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 4594f81 - Fix Xcode project for debugserver

2021-10-15 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'\''Anna
Date: 2021-10-15T15:08:06-07:00
New Revision: 4594f81165433c681cc4f09ed50b662b1a237137

URL: 
https://github.com/llvm/llvm-project/commit/4594f81165433c681cc4f09ed50b662b1a237137
DIFF: 
https://github.com/llvm/llvm-project/commit/4594f81165433c681cc4f09ed50b662b1a237137.diff

LOG: Fix Xcode project for debugserver

It seems StringConvert.cpp was moved, and the Xcode project file
wasn't updated.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D111910

Added: 


Modified: 
lldb/tools/debugserver/debugserver.xcodeproj/project.pbxproj

Removed: 




diff  --git a/lldb/tools/debugserver/debugserver.xcodeproj/project.pbxproj 
b/lldb/tools/debugserver/debugserver.xcodeproj/project.pbxproj
index df466d0c98d7b..1ff26818a7eb8 100644
--- a/lldb/tools/debugserver/debugserver.xcodeproj/project.pbxproj
+++ b/lldb/tools/debugserver/debugserver.xcodeproj/project.pbxproj
@@ -91,7 +91,7 @@
23071D4A5DAE0016ABC0 /* CMakeLists.txt */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = 
CMakeLists.txt; sourceTree = ""; };
233B4EA51D2DB54300E98261 /* JSON.cpp */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; 
path = JSON.cpp; sourceTree = ""; };
233B4EA61D2DB54300E98261 /* JSON.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
JSON.h; sourceTree = ""; };
-   233B4EA81D2DB96A00E98261 /* StringConvert.cpp */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; 
name = StringConvert.cpp; path = ../../../source/Host/common/StringConvert.cpp; 
sourceTree = ""; };
+   233B4EA81D2DB96A00E98261 /* StringConvert.cpp */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; 
path = StringConvert.cpp; sourceTree = ""; };
23D1B0271D497E8B00FF831B /* OsLogger.cpp */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; 
path = OsLogger.cpp; sourceTree = ""; };
23D1B0281D497E8B00FF831B /* OsLogger.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
OsLogger.h; sourceTree = ""; };
260828DE0CBAF7F400F95054 /* DNBRuntimeAction.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
DNBRuntimeAction.h; sourceTree = ""; };



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 52712d3 - Re-land "get rid of PythonInteger::GetInteger()"

2020-05-08 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2020-05-08T10:57:10-07:00
New Revision: 52712d3ff7a2f7bcf737996d6ab59ef2cc29c20d

URL: 
https://github.com/llvm/llvm-project/commit/52712d3ff7a2f7bcf737996d6ab59ef2cc29c20d
DIFF: 
https://github.com/llvm/llvm-project/commit/52712d3ff7a2f7bcf737996d6ab59ef2cc29c20d.diff

LOG: Re-land "get rid of PythonInteger::GetInteger()"

This was reverted due to a python2-specific bug.  Re-landing with a fix
for python2.

Summary:
One small step in my long running quest to improve python exception handling in
LLDB.  Replace GetInteger() which just returns an int with As and
friends, which return Expected types that can track python exceptions

Reviewers: labath, jasonmolenda, JDevlieghere, vadimcn, omjavaid

Reviewed By: labath, omjavaid

Subscribers: omjavaid, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D78462

Added: 


Modified: 
lldb/bindings/python/python-typemaps.swig
lldb/bindings/python/python-wrapper.swig
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Removed: 




diff  --git a/lldb/bindings/python/python-typemaps.swig 
b/lldb/bindings/python/python-typemaps.swig
index 46dcaf611a4f..c08aeab71f78 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -59,37 +59,25 @@
   $result = list.release();
 }
 
-
 %typemap(in) lldb::tid_t {
-  if (PythonInteger::Check($input))
-  {
-PythonInteger py_int(PyRefType::Borrowed, $input);
-$1 = static_cast(py_int.GetInteger());
-  }
-  else
-  {
-PyErr_SetString(PyExc_ValueError, "Expecting an integer");
+  PythonObject obj = Retain($input);
+  lldb::tid_t value = unwrapOrSetPythonException(As(obj)); 
+  if (PyErr_Occurred())
 return nullptr;
-  }
+  $1 = value;
 }
 
 %typemap(in) lldb::StateType {
-  if (PythonInteger::Check($input))
-  {
-PythonInteger py_int(PyRefType::Borrowed, $input);
-int64_t state_type_value = py_int.GetInteger() ;
-
-if (state_type_value > lldb::StateType::kLastStateType) {
-  PyErr_SetString(PyExc_ValueError, "Not a valid StateType value");
-  return nullptr;
-}
-$1 = static_cast(state_type_value);
-  }
-  else
-  {
-PyErr_SetString(PyExc_ValueError, "Expecting an integer");
+  PythonObject obj = Retain($input);
+  unsigned long long state_type_value =
+unwrapOrSetPythonException(As(obj));
+  if (PyErr_Occurred())
+return nullptr;
+  if (state_type_value > lldb::StateType::kLastStateType) {
+PyErr_SetString(PyExc_ValueError, "Not a valid StateType value");
 return nullptr;
   }
+  $1 = static_cast(state_type_value);
 }
 
 /* Typemap definitions to allow SWIG to properly handle char buffer. */

diff  --git a/lldb/bindings/python/python-wrapper.swig 
b/lldb/bindings/python/python-wrapper.swig
index 3a63165cf58d..f9e89373fe25 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -444,6 +444,7 @@ LLDBSwigPythonCallBreakpointResolver
 if (PyErr_Occurred())
 {
 PyErr_Print();
+PyErr_Clear();
 return 0;
 }
 
@@ -457,11 +458,13 @@ LLDBSwigPythonCallBreakpointResolver
   return 1;
 }
 
-PythonInteger int_result = result.AsType();
-if (!int_result.IsAllocated())
-return 0;
+long long ret_val = unwrapOrSetPythonException(As(result));
 
-unsigned int ret_val = int_result.GetInteger();
+if (PyErr_Occurred()) {
+PyErr_Print();
+PyErr_Clear();
+return 0;
+}
 
 return ret_val;
 }
@@ -515,26 +518,17 @@ LLDBSwigPython_CalculateNumChildren
 return 0;
 }
 
-PythonObject result;
-
+size_t ret_val;
 if (arg_info.get().max_positional_args < 1)
-result = pfunc();
+ret_val = unwrapOrSetPythonException(As(pfunc.Call()));
 else
-result = pfunc(PythonInteger(max));
-
-if (!result.IsAllocated())
-return 0;
-
-PythonInteger int_result = result.AsType();
-if (!int_result.IsAllocated())
-return 0;
-
-size_t ret_val = int_result.GetInteger();
+ret_val = unwrapOrSetPythonException(As(pfunc.Call(PythonInteger(max;
 
-if (PyErr_Occurred()) //FIXME use Expected to catch python exceptions
+if (PyErr_Occurred())
 {
 PyErr_Print();
 PyErr_Clear();
+return 0;
 }
 
 if (arg_info.get().max_positional_args < 1)
@@ -588,16 +582,15 @@ LLDBSwigPython_GetIndexOfChildWithName
 if (!pfunc.IsAllocated())
 return UINT32_MAX;
 
-PythonObject result = pfunc(PythonString(child_name));
+llvm::Expected result = pfunc.Call(PythonString(child_name));
 
-if (!result.IsAllocated())
-   

[Lldb-commits] [lldb] 7375212 - get rid of PythonInteger::GetInteger()

2020-04-21 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2020-04-21T16:55:51-07:00
New Revision: 7375212172951d2fc283c81d03c1a8588c3280c6

URL: 
https://github.com/llvm/llvm-project/commit/7375212172951d2fc283c81d03c1a8588c3280c6
DIFF: 
https://github.com/llvm/llvm-project/commit/7375212172951d2fc283c81d03c1a8588c3280c6.diff

LOG: get rid of PythonInteger::GetInteger()

Summary:
One small step in my long running quest to improve python exception handling in
LLDB.  Replace GetInteger() which just returns an int with As and
friends, which return Expected types that can track python exceptions

Reviewers: labath, jasonmolenda, JDevlieghere, vadimcn

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D78462

Added: 


Modified: 
lldb/bindings/python/python-typemaps.swig
lldb/bindings/python/python-wrapper.swig
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Removed: 




diff  --git a/lldb/bindings/python/python-typemaps.swig 
b/lldb/bindings/python/python-typemaps.swig
index 46dcaf611a4f..c08aeab71f78 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -59,37 +59,25 @@
   $result = list.release();
 }
 
-
 %typemap(in) lldb::tid_t {
-  if (PythonInteger::Check($input))
-  {
-PythonInteger py_int(PyRefType::Borrowed, $input);
-$1 = static_cast(py_int.GetInteger());
-  }
-  else
-  {
-PyErr_SetString(PyExc_ValueError, "Expecting an integer");
+  PythonObject obj = Retain($input);
+  lldb::tid_t value = unwrapOrSetPythonException(As(obj)); 
+  if (PyErr_Occurred())
 return nullptr;
-  }
+  $1 = value;
 }
 
 %typemap(in) lldb::StateType {
-  if (PythonInteger::Check($input))
-  {
-PythonInteger py_int(PyRefType::Borrowed, $input);
-int64_t state_type_value = py_int.GetInteger() ;
-
-if (state_type_value > lldb::StateType::kLastStateType) {
-  PyErr_SetString(PyExc_ValueError, "Not a valid StateType value");
-  return nullptr;
-}
-$1 = static_cast(state_type_value);
-  }
-  else
-  {
-PyErr_SetString(PyExc_ValueError, "Expecting an integer");
+  PythonObject obj = Retain($input);
+  unsigned long long state_type_value =
+unwrapOrSetPythonException(As(obj));
+  if (PyErr_Occurred())
+return nullptr;
+  if (state_type_value > lldb::StateType::kLastStateType) {
+PyErr_SetString(PyExc_ValueError, "Not a valid StateType value");
 return nullptr;
   }
+  $1 = static_cast(state_type_value);
 }
 
 /* Typemap definitions to allow SWIG to properly handle char buffer. */

diff  --git a/lldb/bindings/python/python-wrapper.swig 
b/lldb/bindings/python/python-wrapper.swig
index 3a63165cf58d..f9e89373fe25 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -444,6 +444,7 @@ LLDBSwigPythonCallBreakpointResolver
 if (PyErr_Occurred())
 {
 PyErr_Print();
+PyErr_Clear();
 return 0;
 }
 
@@ -457,11 +458,13 @@ LLDBSwigPythonCallBreakpointResolver
   return 1;
 }
 
-PythonInteger int_result = result.AsType();
-if (!int_result.IsAllocated())
-return 0;
+long long ret_val = unwrapOrSetPythonException(As(result));
 
-unsigned int ret_val = int_result.GetInteger();
+if (PyErr_Occurred()) {
+PyErr_Print();
+PyErr_Clear();
+return 0;
+}
 
 return ret_val;
 }
@@ -515,26 +518,17 @@ LLDBSwigPython_CalculateNumChildren
 return 0;
 }
 
-PythonObject result;
-
+size_t ret_val;
 if (arg_info.get().max_positional_args < 1)
-result = pfunc();
+ret_val = unwrapOrSetPythonException(As(pfunc.Call()));
 else
-result = pfunc(PythonInteger(max));
-
-if (!result.IsAllocated())
-return 0;
-
-PythonInteger int_result = result.AsType();
-if (!int_result.IsAllocated())
-return 0;
-
-size_t ret_val = int_result.GetInteger();
+ret_val = unwrapOrSetPythonException(As(pfunc.Call(PythonInteger(max;
 
-if (PyErr_Occurred()) //FIXME use Expected to catch python exceptions
+if (PyErr_Occurred())
 {
 PyErr_Print();
 PyErr_Clear();
+return 0;
 }
 
 if (arg_info.get().max_positional_args < 1)
@@ -588,16 +582,15 @@ LLDBSwigPython_GetIndexOfChildWithName
 if (!pfunc.IsAllocated())
 return UINT32_MAX;
 
-PythonObject result = pfunc(PythonString(child_name));
+llvm::Expected result = pfunc.Call(PythonString(child_name));
 
-if (!result.IsAllocated())
-return UINT32_MAX;
+long long retval = unwrapOrSetPythonException(As(std::move(result)));
 
-PythonInteger 

[Lldb-commits] [lldb] c8de17b - Fix illegal early call to PyBuffer_Release in swig typemaps

2020-04-07 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2020-04-07T13:31:29-07:00
New Revision: c8de17bca658e62bbf8c33eae839e457332e885e

URL: 
https://github.com/llvm/llvm-project/commit/c8de17bca658e62bbf8c33eae839e457332e885e
DIFF: 
https://github.com/llvm/llvm-project/commit/c8de17bca658e62bbf8c33eae839e457332e885e.diff

LOG: Fix illegal early call to PyBuffer_Release in swig typemaps

Summary:
The buffer protocol does not allow us to just call PyBuffer_Release
and assume the buffer will still be there.   Most things that implement the
buffer protocol will let us get away with that, but not all.   We need
to release it at the end of the SWIG wrapper.

Reviewers: labath, jasonmolenda, JDevlieghere, vadimcn

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D77480

Added: 


Modified: 
lldb/bindings/python/python-typemaps.swig

Removed: 




diff  --git a/lldb/bindings/python/python-typemaps.swig 
b/lldb/bindings/python/python-typemaps.swig
index bfd7ef9007d1..46dcaf611a4f 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -488,39 +488,53 @@ bool SetNumberFromPyObject(double , 
PyObject *obj) {
 }
 }
 
+%inline %{
+
+struct Py_buffer_RAII {
+  Py_buffer buffer = {};
+  Py_buffer_RAII() {};
+  Py_buffer =(const Py_buffer_RAII &) = delete;
+  Py_buffer_RAII(const Py_buffer_RAII &) = delete;
+  ~Py_buffer_RAII() {
+if (buffer.obj)
+  PyBuffer_Release();
+  }
+};
+
+%}
+
 // These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i,
 // and fixed so they will not crash if PyObject_GetBuffer fails.
 // https://github.com/swig/swig/issues/1640
+//
+// I've also moved the call to PyBuffer_Release to the end of the SWIG wrapper,
+// doing it right away is not legal according to the python buffer protocol.
 
 %define %pybuffer_mutable_binary(TYPEMAP, SIZE)
-%typemap(in) (TYPEMAP, SIZE) {
+%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
   int res; Py_ssize_t size = 0; void *buf = 0;
-  Py_buffer view;
-  res = PyObject_GetBuffer($input, , PyBUF_WRITABLE);
+  res = PyObject_GetBuffer($input, , PyBUF_WRITABLE);
   if (res < 0) {
 PyErr_Clear();
 %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
   }
-  size = view.len;
-  buf = view.buf;
-  PyBuffer_Release();
+  size = view.buffer.len;
+  buf = view.buffer.buf;
   $1 = ($1_ltype) buf;
   $2 = ($2_ltype) (size/sizeof($*1_type));
 }
 %enddef
 
 %define %pybuffer_binary(TYPEMAP, SIZE)
-%typemap(in) (TYPEMAP, SIZE) {
+%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
   int res; Py_ssize_t size = 0; const void *buf = 0;
-  Py_buffer view;
-  res = PyObject_GetBuffer($input, , PyBUF_CONTIG_RO);
+  res = PyObject_GetBuffer($input, , PyBUF_CONTIG_RO);
   if (res < 0) {
 PyErr_Clear();
 %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
   }
-  size = view.len;
-  buf = view.buf;
-  PyBuffer_Release();
+  size = view.buffer.len;
+  buf = view.buffer.buf;
   $1 = ($1_ltype) buf;
   $2 = ($2_ltype) (size / sizeof($*1_type));
 }



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] adbf64c - [LLDB][Python] remove ArgInfo::count

2019-11-04 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-11-04T12:48:49-08:00
New Revision: adbf64ccc9e18278600ebaeadd8f0117eb8e64b1

URL: 
https://github.com/llvm/llvm-project/commit/adbf64ccc9e18278600ebaeadd8f0117eb8e64b1
DIFF: 
https://github.com/llvm/llvm-project/commit/adbf64ccc9e18278600ebaeadd8f0117eb8e64b1.diff

LOG: [LLDB][Python] remove ArgInfo::count

Summary:
This patch updates the last user of ArgInfo::count and deletes
it.   I also delete `GetNumInitArguments()` and `GetInitArgInfo()`.
Classess are callables and `GetArgInfo()` should work on them.

On python 3 it already works, of course. `inspect` is good.

On python 2 we have to add yet another special case.   But hey if
python 2 wasn't crufty we wouln't need python 3.

I also delete `is_bound_method` becuase it is unused.

This path is tested in `TestStepScripted.py`

Reviewers: labath, mgorny, JDevlieghere

Reviewed By: labath, JDevlieghere

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69742

Added: 


Modified: 
lldb/scripts/Python/python-wrapper.swig
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Removed: 




diff  --git a/lldb/scripts/Python/python-wrapper.swig 
b/lldb/scripts/Python/python-wrapper.swig
index 71a958acb72c..3a63165cf58d 100644
--- a/lldb/scripts/Python/python-wrapper.swig
+++ b/lldb/scripts/Python/python-wrapper.swig
@@ -291,20 +291,32 @@ LLDBSwigPythonCreateScriptedThreadPlan
 if (!tp_arg.IsAllocated())
 Py_RETURN_NONE;
 
+llvm::Expected arg_info = pfunc.GetArgInfo();
+if (!arg_info) {
+llvm::handleAllErrors(
+arg_info.takeError(),
+[&](PythonException ) {
+error_string.append(E.ReadBacktrace());
+},
+[&](const llvm::ErrorInfoBase ) {
+error_string.append(E.message());
+});
+Py_RETURN_NONE;
+}
+
 PythonObject result = {};
-size_t init_num_args = pfunc.GetNumInitArguments().count;
-if (init_num_args == 3) {
+if (arg_info.get().max_positional_args == 2) {
 if (args_impl != nullptr) {
error_string.assign("args passed, but __init__ does not take an 
args dictionary");
Py_RETURN_NONE;
 }
 result = pfunc(tp_arg, dict);
-} else if (init_num_args == 4) {
+} else if (arg_info.get().max_positional_args >= 3) {
 lldb::SBStructuredData *args_value = new 
lldb::SBStructuredData(args_impl);
 PythonObject args_arg(PyRefType::Owned, 
SBTypeToSWIGWrapper(args_value));
 result = pfunc(tp_arg, args_arg, dict);
 } else {
-error_string.assign("wrong number of arguments in __init__, should be 
1 or 2 (not including self & dict)");
+error_string.assign("wrong number of arguments in __init__, should be 
2 or 3 (not including self)");
 Py_RETURN_NONE;
 }
 

diff  --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index ef5eb7a57d9c..9dee25c300ab 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -802,29 +802,11 @@ bool PythonCallable::Check(PyObject *py_obj) {
   return PyCallable_Check(py_obj);
 }
 
-PythonCallable::ArgInfo PythonCallable::GetNumInitArguments() const {
-  auto arginfo = GetInitArgInfo();
-  if (!arginfo) {
-llvm::consumeError(arginfo.takeError());
-return ArgInfo{};
-  }
-  return arginfo.get();
-}
-
-Expected PythonCallable::GetInitArgInfo() const {
-  if (!IsValid())
-return nullDeref();
-  auto init = As(GetAttribute("__init__"));
-  if (!init)
-return init.takeError();
-  return init.get().GetArgInfo();
-}
-
 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
 static const char get_arg_info_script[] = R"(
 from inspect import signature, Parameter, ismethod
 from collections import namedtuple
-ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs', 'is_bound_method'])
+ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs'])
 def main(f):
 count = 0
 varargs = False
@@ -840,7 +822,7 @@ def main(f):
 pass
 else:
 raise Exception(f'unknown parameter kind: {kind}')
-return ArgInfo(count, varargs, ismethod(f))
+return ArgInfo(count, varargs)
 )";
 #endif
 
@@ -856,21 +838,27 @@ Expected 
PythonCallable::GetArgInfo() const {
   Expected pyarginfo = get_arg_info(*this);
   if (!pyarginfo)
 return pyarginfo.takeError();
-  result.count = cantFail(As(pyarginfo.get().GetAttribute("count")));
-  result.has_varargs =
+  long long count =
+  

[Lldb-commits] [lldb] fb01c01 - [LLDB][Python] warning fix for LLDBSwigPythonBreakpointCallbackFunction

2019-10-30 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-30T09:47:27-07:00
New Revision: fb01c01bf3f60d1d229126ea4088519adae5c015

URL: 
https://github.com/llvm/llvm-project/commit/fb01c01bf3f60d1d229126ea4088519adae5c015
DIFF: 
https://github.com/llvm/llvm-project/commit/fb01c01bf3f60d1d229126ea4088519adae5c015.diff

LOG: [LLDB][Python] warning fix for LLDBSwigPythonBreakpointCallbackFunction

This is a quick followup to this commit:

https://reviews.llvm.org/rGa69bbe02a2352271e8b14542073f177e24c499c1

In that, I #pragma-squelch this warning in `ScriptInterpreterPython.cpp`
but we get the same warning in `PythonTestSuite.cpp`.

This patch squelches the same warning in the same way as the
reviweed commit.   I'm submitting it without review under the
"obviously correct" rule.

At least if this is incorrect the main commit was also incorrect.

By the way, as far as I can tell, these functions are extern "C" because
SWIG does that to everything, not because they particularly need to be.

Added: 


Modified: 
lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Removed: 




diff  --git a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp 
b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
index 2ac631eba424..12ffdfe79ec3 100644
--- a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
+++ b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
@@ -59,6 +59,9 @@ extern "C" void init_lldb(void) {}
 #define LLDBSwigPyInit init_lldb
 #endif
 
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
+
 extern "C" llvm::Expected LLDBSwigPythonBreakpointCallbackFunction(
 const char *python_function_name, const char *session_dictionary_name,
 const lldb::StackFrameSP _frame,
@@ -67,6 +70,8 @@ extern "C" llvm::Expected 
LLDBSwigPythonBreakpointCallbackFunction(
   return false;
 }
 
+#pragma clang diagnostic pop
+
 extern "C" bool LLDBSwigPythonWatchpointCallbackFunction(
 const char *python_function_name, const char *session_dictionary_name,
 const lldb::StackFrameSP _frame, const lldb::WatchpointSP _wp) {



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 3071ebf - [LLDB][PythonFile] fix dangerous borrow semantics on python2

2019-10-30 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-30T09:46:51-07:00
New Revision: 3071ebf7b38341e89be04aa64c257c4643e0648c

URL: 
https://github.com/llvm/llvm-project/commit/3071ebf7b38341e89be04aa64c257c4643e0648c
DIFF: 
https://github.com/llvm/llvm-project/commit/3071ebf7b38341e89be04aa64c257c4643e0648c.diff

LOG: [LLDB][PythonFile] fix dangerous borrow semantics on python2

Summary:
It is inherently unsafe to allow a python program to manipulate borrowed
memory from a python object's destructor. It would be nice to
flush a borrowed file when python is finished with it, but it's not safe
to do on python 2.

Python 3 does not suffer from this issue.

Reviewers: labath, mgorny

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69532

Added: 


Modified: 
lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp

Removed: 




diff  --git 
a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py 
b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
index f7f1ad08500a..5d025a46dced 100644
--- 
a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
+++ 
b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
@@ -851,10 +851,6 @@ def i(sbf):
 yield sbf
 sbf.Write(str(i).encode('ascii') + b"\n")
 files = list(i(sbf))
-# delete them in reverse order, again because each is a borrow
-# of the previous.
-while files:
-files.pop()
 with open(self.out_filename, 'r') as f:
 self.assertEqual(list(range(10)), list(map(int, 
f.read().strip().split(
 

diff  --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index df8bac951fc4..ef5eb7a57d9c 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -1500,21 +1500,23 @@ Expected PythonFile::FromFile(File , 
const char *mode) {
   PyObject *file_obj;
 #if PY_MAJOR_VERSION >= 3
   file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr,
-   "ignore", nullptr, 0);
+   "ignore", nullptr, /*closefd=*/0);
 #else
-  // We pass ::flush instead of ::fclose here so we borrow the FILE* --
-  // the lldb_private::File still owns it.   NetBSD does not allow
-  // input files to be flushed, so we have to check for that case too.
-  int (*closer)(FILE *);
-  auto opts = file.GetOptions();
-  if (!opts)
-return opts.takeError();
-  if (opts.get() & File::eOpenOptionWrite)
-closer = ::fflush;
-  else
-closer = [](FILE *) { return 0; };
+  // I'd like to pass ::fflush here if the file is writable,  so that
+  // when the python side destructs the file object it will be flushed.
+  // However, this would be dangerous.It can cause fflush to be called
+  // after fclose if the python program keeps a reference to the file after
+  // the original lldb_private::File has been destructed.
+  //
+  // It's all well and good to ask a python program not to use a closed file
+  // but asking a python program to make sure objects get released in a
+  // particular order is not safe.
+  //
+  // The tradeoff here is that if a python 2 program wants to make sure this
+  // file gets flushed, they'll have to do it explicitly or wait untill the
+  // original lldb File itself gets flushed.
   file_obj = PyFile_FromFile(file.GetStream(), py2_const_cast(""),
- py2_const_cast(mode), closer);
+ py2_const_cast(mode), [](FILE *) { return 0; });
 #endif
 
   if (!file_obj)



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] a69bbe0 - [LLDB][breakpoints] ArgInfo::count -> ArgInfo::max_positional_args

2019-10-29 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-29T15:03:02-07:00
New Revision: a69bbe02a2352271e8b14542073f177e24c499c1

URL: 
https://github.com/llvm/llvm-project/commit/a69bbe02a2352271e8b14542073f177e24c499c1
DIFF: 
https://github.com/llvm/llvm-project/commit/a69bbe02a2352271e8b14542073f177e24c499c1.diff

LOG: [LLDB][breakpoints] ArgInfo::count -> ArgInfo::max_positional_args

Summary:
Move breakpoints from the old, bad ArgInfo::count to the new, better
ArgInfo::max_positional_args.   Soon ArgInfo::count will be no more.

It looks like this functionality is already well tested by
`TestBreakpointCommandsFromPython.py`, so there's no need to write
additional tests for it.

Reviewers: labath, jingham, JDevlieghere

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69468

Added: 


Modified: 
lldb/include/lldb/Interpreter/ScriptInterpreter.h

lldb/packages/Python/lldbsuite/test/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
lldb/scripts/Python/python-wrapper.swig
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Removed: 




diff  --git a/lldb/include/lldb/Interpreter/ScriptInterpreter.h 
b/lldb/include/lldb/Interpreter/ScriptInterpreter.h
index 69af88091a40..b32962b80355 100644
--- a/lldb/include/lldb/Interpreter/ScriptInterpreter.h
+++ b/lldb/include/lldb/Interpreter/ScriptInterpreter.h
@@ -315,8 +315,7 @@ class ScriptInterpreter : public PluginInterface {
 
   Status SetBreakpointCommandCallbackFunction(
   std::vector _options_vec,
-  const char *function_name, 
-  StructuredData::ObjectSP extra_args_sp);
+  const char *function_name, StructuredData::ObjectSP extra_args_sp);
 
   /// Set a script function as the callback for the breakpoint.
   virtual Status
@@ -472,9 +471,9 @@ class ScriptInterpreter : public PluginInterface {
   const char *GetScriptInterpreterPtyName();
 
   int GetMasterFileDescriptor();
-  
-  virtual llvm::Expected 
-  GetNumFixedArgumentsForCallable(const llvm::StringRef _name) { 
+
+  virtual llvm::Expected
+  GetMaxPositionalArgumentsForCallable(const llvm::StringRef _name) {
 return llvm::createStringError(
 llvm::inconvertibleErrorCode(), "Unimplemented function");
   }

diff  --git 
a/lldb/packages/Python/lldbsuite/test/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
 
b/lldb/packages/Python/lldbsuite/test/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
index ccb61d79c403..15a31201c565 100644
--- 
a/lldb/packages/Python/lldbsuite/test/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
+++ 
b/lldb/packages/Python/lldbsuite/test/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
@@ -132,7 +132,7 @@ def do_set_python_command_from_python(self):
 # Now finish, and make sure the return value is correct.
 threads = lldbutil.get_threads_stopped_at_breakpoint(
 self.process, body_bkpt)
-self.assertTrue(len(threads) == 1, "Stopped at inner breakpoint.")
+self.assertEquals(len(threads), 1, "Stopped at inner breakpoint.")
 self.thread = threads[0]
 
 self.assertEquals("callback was here", side_effect.callback)

diff  --git a/lldb/scripts/Python/python-wrapper.swig 
b/lldb/scripts/Python/python-wrapper.swig
index 5e9a2ba1367c..71a958acb72c 100644
--- a/lldb/scripts/Python/python-wrapper.swig
+++ b/lldb/scripts/Python/python-wrapper.swig
@@ -39,7 +39,7 @@ private:
 // This function is called by 
lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
 // and is used when a script command is attached to a breakpoint for execution.
 
-SWIGEXPORT bool
+SWIGEXPORT llvm::Expected
 LLDBSwigPythonBreakpointCallbackFunction
 (
 const char *python_function_name,
@@ -49,38 +49,40 @@ LLDBSwigPythonBreakpointCallbackFunction
 lldb_private::StructuredDataImpl *args_impl
 )
 {
+using namespace llvm;
+
 lldb::SBFrame sb_frame (frame_sp);
 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
 
-bool stop_at_breakpoint = true;
-
 PyErr_Cleaner py_err_cleaner(true);
 auto dict = 
PythonModule::MainModule().ResolveName(session_dictionary_name);
 auto pfunc = 
PythonObject::ResolveNameWithDictionary(python_function_name, 
dict);
 
-if (!pfunc.IsAllocated())
-return stop_at_breakpoint;
+unsigned max_positional_args;
+if (auto arg_info = pfunc.GetArgInfo())
+max_positional_args = arg_info.get().max_positional_args;
+else
+return arg_info.takeError();
 
 PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
 PythonObject 

[Lldb-commits] [lldb] 6a93a12 - [LLDB][Python] fix another fflush issue on NetBSD

2019-10-29 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-29T09:41:22-07:00
New Revision: 6a93a12a8dd98291225a282b5b8f3c97e68ebe49

URL: 
https://github.com/llvm/llvm-project/commit/6a93a12a8dd98291225a282b5b8f3c97e68ebe49
DIFF: 
https://github.com/llvm/llvm-project/commit/6a93a12a8dd98291225a282b5b8f3c97e68ebe49.diff

LOG: [LLDB][Python] fix another fflush issue on NetBSD

Summary:
Here's another instance where we were calling fflush on an input
stream, which is illegal on NetBSD.

Reviewers: labath, mgorny

Reviewed By: mgorny

Subscribers: krytarowski, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69488

Added: 


Modified: 
lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/source/Host/common/File.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Removed: 




diff  --git 
a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py 
b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
index 5a75187262ab..f7f1ad08500a 100644
--- 
a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
+++ 
b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
@@ -845,11 +845,16 @@ def test_back_and_forth(self):
 def i(sbf):
 for i in range(10):
 f = sbf.GetFile()
+self.assertEqual(f.mode, "w")
 yield f
 sbf = lldb.SBFile.Create(f, borrow=True)
 yield sbf
 sbf.Write(str(i).encode('ascii') + b"\n")
 files = list(i(sbf))
+# delete them in reverse order, again because each is a borrow
+# of the previous.
+while files:
+files.pop()
 with open(self.out_filename, 'r') as f:
 self.assertEqual(list(range(10)), list(map(int, 
f.read().strip().split(
 

diff  --git a/lldb/source/Host/common/File.cpp 
b/lldb/source/Host/common/File.cpp
index 9dae24d766f6..7850222376f7 100644
--- a/lldb/source/Host/common/File.cpp
+++ b/lldb/source/Host/common/File.cpp
@@ -310,7 +310,7 @@ Status NativeFile::Close() {
 if (m_own_stream) {
   if (::fclose(m_stream) == EOF)
 error.SetErrorToErrno();
-} else {
+} else if (m_options & eOpenOptionWrite) {
   if (::fflush(m_stream) == EOF)
 error.SetErrorToErrno();
 }

diff  --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 2b85ebf18c6a..df8bac951fc4 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -1502,12 +1502,19 @@ Expected PythonFile::FromFile(File , 
const char *mode) {
   file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr,
"ignore", nullptr, 0);
 #else
-  // Read through the Python source, doesn't seem to modify these strings
-  char *cmode = const_cast(mode);
   // We pass ::flush instead of ::fclose here so we borrow the FILE* --
-  // the lldb_private::File still owns it.
-  file_obj =
-  PyFile_FromFile(file.GetStream(), const_cast(""), cmode, 
::fflush);
+  // the lldb_private::File still owns it.   NetBSD does not allow
+  // input files to be flushed, so we have to check for that case too.
+  int (*closer)(FILE *);
+  auto opts = file.GetOptions();
+  if (!opts)
+return opts.takeError();
+  if (opts.get() & File::eOpenOptionWrite)
+closer = ::fflush;
+  else
+closer = [](FILE *) { return 0; };
+  file_obj = PyFile_FromFile(file.GetStream(), py2_const_cast(""),
+ py2_const_cast(mode), closer);
 #endif
 
   if (!file_obj)

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h 
b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
index 373d3212697d..302901664ec0 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
@@ -189,6 +189,14 @@ inline llvm::Error keyError() {
  "key not in dict");
 }
 
+#if PY_MAJOR_VERSION < 3
+// The python 2 API declares some arguments as char* that should
+// be const char *, but it doesn't actually modify them.
+inline char *py2_const_cast(const char *s) { return const_cast(s); }
+#else
+inline const char *py2_const_cast(const char *s) { return s; }
+#endif
+
 enum class PyInitialValue { Invalid, Empty };
 
 template  struct PythonFormat;
@@ -309,16 +317,6 @@ class PythonObject {
 
   StructuredData::ObjectSP CreateStructuredObject() const;
 
-protected:
-
-#if PY_MAJOR_VERSION < 3
-  // The python 2 API declares 

[Lldb-commits] [lldb] 40b0fa7 - [LLDB][formatters] ArgInfo::count -> ArgInfo::max_positional_args

2019-10-27 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-27T16:01:46-07:00
New Revision: 40b0fa7ef2123866b2252ef6990040c2707cabe4

URL: 
https://github.com/llvm/llvm-project/commit/40b0fa7ef2123866b2252ef6990040c2707cabe4
DIFF: 
https://github.com/llvm/llvm-project/commit/40b0fa7ef2123866b2252ef6990040c2707cabe4.diff

LOG: [LLDB][formatters] ArgInfo::count -> ArgInfo::max_positional_args

Summary:
Move breakpoints from the old, bad ArgInfo::count to the new, better
ArgInfo::max_positional_args.   Soon ArgInfo::count will be no more.

This functionality is tested in `TestFormatters.py`, 
`TestDataFormatterSynthVal.py`,
`TestDataFormatterSynthType.py`.

You may notice that the old code was passing 0 arguments when count was 1, and 
passing
1 argument when count is 2.

This is no longer necessary because max_positional_args counts the self pointer
correctly.

Reviewers: labath, jingham, JDevlieghere

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69469

Added: 


Modified: 
lldb/scripts/Python/python-wrapper.swig

Removed: 




diff  --git a/lldb/scripts/Python/python-wrapper.swig 
b/lldb/scripts/Python/python-wrapper.swig
index b7af34221934..5e9a2ba1367c 100644
--- a/lldb/scripts/Python/python-wrapper.swig
+++ b/lldb/scripts/Python/python-wrapper.swig
@@ -495,11 +495,17 @@ LLDBSwigPython_CalculateNumChildren
 if (!pfunc.IsAllocated())
 return 0;
 
+auto arg_info = pfunc.GetArgInfo();
+if (!arg_info) {
+llvm::consumeError(arg_info.takeError());
+return 0;
+}
+
 PythonObject result;
-auto argc = pfunc.GetNumArguments();
-if (argc.count == 1)
+
+if (arg_info.get().max_positional_args < 1)
 result = pfunc();
-else if (argc.count == 2)
+else
 result = pfunc(PythonInteger(max));
 
 if (!result.IsAllocated())
@@ -511,13 +517,13 @@ LLDBSwigPython_CalculateNumChildren
 
 size_t ret_val = int_result.GetInteger();
 
-if (PyErr_Occurred())
+if (PyErr_Occurred()) //FIXME use Expected to catch python exceptions
 {
 PyErr_Print();
 PyErr_Clear();
 }
 
-if (argc.count == 1)
+if (arg_info.get().max_positional_args < 1)
 ret_val = std::min(ret_val, static_cast(max));
 
 return ret_val;



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] d602e0d - fix PythonDataObjectsTest.TestExceptions on windows

2019-10-21 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-22T04:00:37Z
New Revision: d602e0d0cab270761553c79d2e42b8ac6b756157

URL: 
https://github.com/llvm/llvm-project/commit/d602e0d0cab270761553c79d2e42b8ac6b756157
DIFF: 
https://github.com/llvm/llvm-project/commit/d602e0d0cab270761553c79d2e42b8ac6b756157.diff

LOG: fix PythonDataObjectsTest.TestExceptions on windows

Looks like on windows googlemock regexes treat newlines differently
from on darwin.This patch fixes the regex in this test so it
will work on both.

Fixes: https://reviews.llvm.org/D69214
llvm-svn: 375477

Added: 


Modified: 
lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Removed: 




diff  --git 
a/lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp 
b/lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
index b676b42da666..7481482fd8fc 100644
--- a/lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
+++ b/lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
@@ -805,13 +805,13 @@ main = foo
 
   PythonScript foo(script);
 
-  EXPECT_THAT_EXPECTED(foo(),
-   llvm::Failed(testing::Property(
-   ::ReadBacktrace,
-   testing::ContainsRegex("line 3, in foo..*"
-  "line 5, in bar.*"
-  "line 7, in baz.*"
-  "ZeroDivisionError";
+  EXPECT_THAT_EXPECTED(
+  foo(), llvm::Failed(testing::Property(
+ ::ReadBacktrace,
+ testing::AllOf(testing::ContainsRegex("line 3, in foo"),
+testing::ContainsRegex("line 5, in bar"),
+testing::ContainsRegex("line 7, in baz"),
+
testing::ContainsRegex("ZeroDivisionError");
 
   static const char script2[] = R"(
 class MyError(Exception):



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 04edd18 - remove multi-argument form of PythonObject::Reset()

2019-10-21 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-22T02:32:37Z
New Revision: 04edd1893c2d0f35880fd5f81e78dc23979df0b9

URL: 
https://github.com/llvm/llvm-project/commit/04edd1893c2d0f35880fd5f81e78dc23979df0b9
DIFF: 
https://github.com/llvm/llvm-project/commit/04edd1893c2d0f35880fd5f81e78dc23979df0b9.diff

LOG: remove multi-argument form of PythonObject::Reset()

Summary:
With this patch, only the no-argument form of `Reset()` remains in
PythonDataObjects.   It also deletes PythonExceptionState in favor of
PythonException, because the only call-site of PythonExceptionState was
also using Reset, so I cleaned up both while I was there.

Reviewers: JDevlieghere, clayborg, labath, jingham

Reviewed By: labath

Subscribers: mgorny, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69214

llvm-svn: 375475

Added: 


Modified: 
lldb/include/lldb/Interpreter/ScriptInterpreter.h
lldb/scripts/Python/python-extensions.swig
lldb/scripts/Python/python-typemaps.swig
lldb/scripts/Python/python-wrapper.swig
lldb/scripts/lldb.swig
lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
lldb/unittests/ScriptInterpreter/Python/CMakeLists.txt
lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Removed: 
lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.h
lldb/unittests/ScriptInterpreter/Python/PythonExceptionStateTests.cpp



diff  --git a/lldb/include/lldb/Interpreter/ScriptInterpreter.h 
b/lldb/include/lldb/Interpreter/ScriptInterpreter.h
index 3dd75b558508..23fadf02e591 100644
--- a/lldb/include/lldb/Interpreter/ScriptInterpreter.h
+++ b/lldb/include/lldb/Interpreter/ScriptInterpreter.h
@@ -65,6 +65,9 @@ class ScriptInterpreter : public PluginInterface {
 
 bool GetSetLLDBGlobals() const { return m_set_lldb_globals; }
 
+// If this is true then any exceptions raised by the script will be
+// cleared with PyErr_Clear().   If false then they will be left for
+// the caller to clean up
 bool GetMaskoutErrors() const { return m_maskout_errors; }
 
 ExecuteScriptOptions (bool enable) {

diff  --git a/lldb/scripts/Python/python-extensions.swig 
b/lldb/scripts/Python/python-extensions.swig
index 7823dc4ad1a0..c10c32b44877 100644
--- a/lldb/scripts/Python/python-extensions.swig
+++ b/lldb/scripts/Python/python-extensions.swig
@@ -8,10 +8,7 @@
 size_t desc_len = description.GetSize();
 if (desc_len > 0 && (desc[desc_len-1] == '\n' || 
desc[desc_len-1] == '\r'))
 --desc_len;
-if (desc_len > 0)
-return lldb_private::PythonString(llvm::StringRef(desc, 
desc_len)).release();
-else
-return lldb_private::PythonString("").release();
+return PythonString(llvm::StringRef(desc, desc_len)).release();
 }
 %clearnothreadallow;
 }
@@ -24,10 +21,7 @@
 size_t desc_len = description.GetSize();
 if (desc_len > 0 && (desc[desc_len-1] == '\n' || 
desc[desc_len-1] == '\r'))
 --desc_len;
-if (desc_len > 0)
-return lldb_private::PythonString(llvm::StringRef(desc, 
desc_len)).release();
-else
-return lldb_private::PythonString("").release();
+return PythonString(llvm::StringRef(desc, desc_len)).release();
 }
 %clearnothreadallow;
 }
@@ -40,10 +34,7 @@
 size_t desc_len = description.GetSize();
 if (desc_len > 0 && (desc[desc_len-1] == '\n' || 
desc[desc_len-1] == '\r'))
 --desc_len;
-if (desc_len > 0)
-return lldb_private::PythonString(llvm::StringRef(desc, 
desc_len)).release();
-else
-return lldb_private::PythonString("").release();
+return PythonString(llvm::StringRef(desc, desc_len)).release();
 }
 %clearnothreadallow;
 
@@ -71,10 +62,7 @@
 size_t desc_len = description.GetSize();
 if (desc_len > 0 && (desc[desc_len-1] == '\n' || 
desc[desc_len-1] == '\r'))
 --desc_len;
-if (desc_len > 0)
-return lldb_private::PythonString(llvm::StringRef(desc, 
desc_len)).release();
-else
-return lldb_private::PythonString("").release();
+return PythonString(llvm::StringRef(desc, desc_len)).release();
 }
 

[Lldb-commits] [lldb] 722b618 - eliminate nontrivial Reset(...) from TypedPythonObject

2019-10-19 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-19T18:43:49Z
New Revision: 722b61892454b3217d73ec486e52156c5a92b5b3

URL: 
https://github.com/llvm/llvm-project/commit/722b61892454b3217d73ec486e52156c5a92b5b3
DIFF: 
https://github.com/llvm/llvm-project/commit/722b61892454b3217d73ec486e52156c5a92b5b3.diff

LOG: eliminate nontrivial Reset(...) from TypedPythonObject

Summary:
This deletes `Reset(...)`, except for the no-argument form `Reset()`
from `TypedPythonObject`, and therefore from `PythonString`, `PythonList`,
etc.

It updates the various callers to use assignment, `As<>`, `Take<>`,
and `Retain<>`, as appropriate.

followon to https://reviews.llvm.org/D69080

Reviewers: JDevlieghere, clayborg, labath, jingham

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69133

llvm-svn: 375350

Added: 


Modified: 
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Removed: 




diff  --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 2b70762e368f..d0d593656efd 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -34,6 +34,7 @@ using namespace lldb_private::python;
 using llvm::cantFail;
 using llvm::Error;
 using llvm::Expected;
+using llvm::Twine;
 
 template <> Expected python::As(Expected &) {
   if (!obj)
@@ -278,7 +279,7 @@ PythonByteArray::PythonByteArray(llvm::ArrayRef 
bytes)
 
 PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) {
   const char *str = reinterpret_cast(bytes);
-  Reset(PyRefType::Owned, PyByteArray_FromStringAndSize(str, length));
+  *this = Take(PyByteArray_FromStringAndSize(str, length));
 }
 
 bool PythonByteArray::Check(PyObject *py_obj) {
@@ -522,11 +523,11 @@ StructuredData::BooleanSP 
PythonBoolean::CreateStructuredBoolean() const {
 
 PythonList::PythonList(PyInitialValue value) {
   if (value == PyInitialValue::Empty)
-Reset(PyRefType::Owned, PyList_New(0));
+*this = Take(PyList_New(0));
 }
 
 PythonList::PythonList(int list_size) {
-  Reset(PyRefType::Owned, PyList_New(list_size));
+  *this = Take(PyList_New(list_size));
 }
 
 bool PythonList::Check(PyObject *py_obj) {
@@ -578,11 +579,11 @@ StructuredData::ArraySP 
PythonList::CreateStructuredArray() const {
 
 PythonTuple::PythonTuple(PyInitialValue value) {
   if (value == PyInitialValue::Empty)
-Reset(PyRefType::Owned, PyTuple_New(0));
+*this = Take(PyTuple_New(0));
 }
 
 PythonTuple::PythonTuple(int tuple_size) {
-  Reset(PyRefType::Owned, PyTuple_New(tuple_size));
+  *this = Take(PyTuple_New(tuple_size));
 }
 
 PythonTuple::PythonTuple(std::initializer_list objects) {
@@ -649,7 +650,7 @@ StructuredData::ArraySP 
PythonTuple::CreateStructuredArray() const {
 
 PythonDictionary::PythonDictionary(PyInitialValue value) {
   if (value == PyInitialValue::Empty)
-Reset(PyRefType::Owned, PyDict_New());
+*this = Take(PyDict_New());
 }
 
 bool PythonDictionary::Check(PyObject *py_obj) {
@@ -696,10 +697,10 @@ PythonDictionary::GetItem(const PythonObject ) const {
   return Retain(o);
 }
 
-Expected PythonDictionary::GetItem(const char *key) const {
+Expected PythonDictionary::GetItem(const Twine ) const {
   if (!IsValid())
 return nullDeref();
-  PyObject *o = PyDict_GetItemString(m_py_obj, key);
+  PyObject *o = PyDict_GetItemString(m_py_obj, NullTerminated(key));
   if (PyErr_Occurred())
 return exception();
   if (!o)
@@ -717,11 +718,11 @@ Error PythonDictionary::SetItem(const PythonObject ,
   return Error::success();
 }
 
-Error PythonDictionary::SetItem(const char *key,
+Error PythonDictionary::SetItem(const Twine ,
 const PythonObject ) const {
   if (!IsValid() || !value.IsValid())
 return nullDeref();
-  int r = PyDict_SetItemString(m_py_obj, key, value.get());
+  int r = PyDict_SetItemString(m_py_obj, NullTerminated(key), value.get());
   if (r < 0)
 return exception();
   return Error::success();
@@ -763,20 +764,20 @@ PythonModule PythonModule::AddModule(llvm::StringRef 
module) {
   return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str()));
 }
 
-Expected PythonModule::Import(const char *name) {
-  PyObject *mod = PyImport_ImportModule(name);
+Expected PythonModule::Import(const Twine ) {
+  PyObject *mod = PyImport_ImportModule(NullTerminated(name));
   if (!mod)
 return exception();
   return Take(mod);
 }
 
-Expected PythonModule::Get(const char *name) {
+Expected PythonModule::Get(const Twine ) {
   if (!IsValid())
 return 

[Lldb-commits] [lldb] bdcad0a - convert LLDBSwigPythonCallTypeScript to ArgInfo::max_positional_args

2019-10-19 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-19T07:05:39Z
New Revision: bdcad0aca0a05145364ee153a8f54af4aea2c445

URL: 
https://github.com/llvm/llvm-project/commit/bdcad0aca0a05145364ee153a8f54af4aea2c445
DIFF: 
https://github.com/llvm/llvm-project/commit/bdcad0aca0a05145364ee153a8f54af4aea2c445.diff

LOG: convert LLDBSwigPythonCallTypeScript to ArgInfo::max_positional_args

Summary:
This patch converts another user of ArgInfo::count over to
use ArgInfo::max_positional_args instead.   I also add a test
to make sure both documented signatures for python type formatters
work.

Reviewers: JDevlieghere, clayborg, labath, jingham

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69153

llvm-svn: 375334

Added: 


Modified: 

lldb/packages/Python/lldbsuite/test/commands/expression/formatters/TestFormatters.py

lldb/packages/Python/lldbsuite/test/commands/expression/formatters/formatters.py
lldb/scripts/Python/python-wrapper.swig

Removed: 




diff  --git 
a/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/TestFormatters.py
 
b/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/TestFormatters.py
index b13d6555f33b..011dabce6e9d 100644
--- 
a/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/TestFormatters.py
+++ 
b/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/TestFormatters.py
@@ -74,6 +74,21 @@ def cleanup():
 # EXPR-TYPES-NEW-FOO-NEXT:   }
 # EXPR-TYPES-NEW-FOO-NEXT: }
 
+
+self.runCmd("type summary add -F formatters.foo_SummaryProvider3 foo")
+self.filecheck("expression foo1", __file__, 
'-check-prefix=EXPR-FOO1opts')
+# EXPR-FOO1opts: (foo) $
+# EXPR-FOO1opts-SAME: a = 12
+# EXPR-FOO1opts-SAME: a_ptr = {{[0-9]+}} -> 13
+# EXPR-FOO1opts-SAME: i = 24
+# EXPR-FOO1opts-SAME: i_ptr = {{[0-9]+}} -> 25
+# EXPR-FOO1opts-SAME: b_ref = {{[0-9]+}}
+# EXPR-FOO1opts-SAME: h = 27
+# EXPR-FOO1opts-SAME: k = 29
+# EXPR-FOO1opts-SAME: WITH_OPTS
+
+self.runCmd("type summary delete foo")
+
 self.runCmd("type summary add -F formatters.foo_SummaryProvider foo")
 
 self.expect("expression new int(12)",

diff  --git 
a/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/formatters.py
 
b/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/formatters.py
index dae84988af9e..ac2888bd203f 100644
--- 
a/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/formatters.py
+++ 
b/lldb/packages/Python/lldbsuite/test/commands/expression/formatters/formatters.py
@@ -1,3 +1,5 @@
+import lldb
+
 def foo_SummaryProvider(valobj, dict):
 a = valobj.GetChildMemberWithName('a')
 a_ptr = valobj.GetChildMemberWithName('a_ptr')
@@ -15,3 +17,8 @@ def foo_SummaryProvider(valobj, dict):
 ', i_ptr = ' + str(i_ptr.GetValueAsUnsigned(0)) + ' -> ' + 
str(i_ptr.Dereference().GetValueAsUnsigned(0)) + \
 ', b_ref = ' + str(b_ref.GetValueAsUnsigned(0)) + \
 ', h = ' + str(h.GetValueAsUnsigned(0)) + ' , k = ' + 
str(k.GetValueAsUnsigned(0))
+
+def foo_SummaryProvider3(valobj, dict, options):
+if not isinstance(options, lldb.SBTypeSummaryOptions):
+raise Exception()
+return foo_SummaryProvider(valobj, dict) + ", WITH_OPTS"
\ No newline at end of file

diff  --git a/lldb/scripts/Python/python-wrapper.swig 
b/lldb/scripts/Python/python-wrapper.swig
index c50f9d1fab92..277b8657d344 100644
--- a/lldb/scripts/Python/python-wrapper.swig
+++ b/lldb/scripts/Python/python-wrapper.swig
@@ -163,14 +163,19 @@ LLDBSwigPythonCallTypeScript
 }
 
 PythonObject result;
-auto argc = pfunc.GetNumArguments();
-// if the third argument is supported, or varargs are allowed
+auto argc = pfunc.GetArgInfo();
+if (!argc) {
+llvm::consumeError(argc.takeError());
+return false;
+}
+
 PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value));
 PythonObject options_arg(PyRefType::Owned, 
SBTypeToSWIGWrapper(sb_options));
-if (argc.count == 3 || argc.has_varargs)
-result = pfunc(value_arg,dict,options_arg);
-else
+
+if (argc.get().max_positional_args < 3)
 result = pfunc(value_arg,dict);
+else
+result = pfunc(value_arg,dict,options_arg);
 
 retval = result.Str().GetString().str();
 



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 2386537 - [LLDB] bugfix: command script add -f doesn't work for some callables

2019-10-19 Thread Lawrence D'Anna via lldb-commits

Author: Lawrence D'Anna
Date: 2019-10-19T07:05:33Z
New Revision: 2386537c2469a97501a305c6b3138231b907a67f

URL: 
https://github.com/llvm/llvm-project/commit/2386537c2469a97501a305c6b3138231b907a67f
DIFF: 
https://github.com/llvm/llvm-project/commit/2386537c2469a97501a305c6b3138231b907a67f.diff

LOG: [LLDB] bugfix: command script add -f doesn't work for some callables

Summary:
When users define a debugger command from python, they provide a callable
object.   Because the signature of the function has been extended, LLDB
needs to inspect the number of parameters the callable can take.

The rule it was using to decide was weird, apparently not tested, and
giving wrong results for some kinds of python callables.

This patch replaces the weird rule with a simple one: if the callable can
take 5 arguments, it gets the 5 argument version of the signature.
Otherwise it gets the old 4 argument version.

It also adds tests with a bunch of different kinds of python callables
with both 4 and 5 arguments.

Reviewers: JDevlieghere, clayborg, labath, jingham

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69014

llvm-svn: 375333

Added: 
lldb/packages/Python/lldbsuite/test/commands/command/script/callables.py

Modified: 

lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py
lldb/packages/Python/lldbsuite/test/commands/command/script/py_import
lldb/scripts/Python/python-wrapper.swig
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Removed: 




diff  --git 
a/lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py
 
b/lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py
index 6531cd672792..9542d0264a6b 100644
--- 
a/lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py
+++ 
b/lldb/packages/Python/lldbsuite/test/commands/command/script/TestCommandScript.py
@@ -4,7 +4,7 @@
 
 from __future__ import print_function
 
-
+import sys
 import lldb
 from lldbsuite.test.decorators import *
 from lldbsuite.test.lldbtest import *
@@ -22,6 +22,21 @@ def test(self):
 def pycmd_tests(self):
 self.runCmd("command source py_import")
 
+# Test a bunch of 
diff erent kinds of python callables with
+# both 4 and 5 positional arguments.
+self.expect("foobar", substrs=["All good"])
+self.expect("foobar4", substrs=["All good"])
+self.expect("vfoobar", substrs=["All good"])
+self.expect("v5foobar", substrs=["All good"])
+self.expect("sfoobar", substrs=["All good"])
+self.expect("cfoobar", substrs=["All good"])
+self.expect("ifoobar", substrs=["All good"])
+self.expect("sfoobar4", substrs=["All good"])
+self.expect("cfoobar4", substrs=["All good"])
+self.expect("ifoobar4", substrs=["All good"])
+self.expect("ofoobar", substrs=["All good"])
+self.expect("ofoobar4", substrs=["All good"])
+
 # Verify command that specifies eCommandRequiresTarget returns failure
 # without a target.
 self.expect('targetname',

diff  --git 
a/lldb/packages/Python/lldbsuite/test/commands/command/script/callables.py 
b/lldb/packages/Python/lldbsuite/test/commands/command/script/callables.py
new file mode 100644
index ..21e599b82e5b
--- /dev/null
+++ b/lldb/packages/Python/lldbsuite/test/commands/command/script/callables.py
@@ -0,0 +1,63 @@
+
+from __future__ import print_function
+
+import lldb
+
+# bunch of 
diff erent kinds of python callables that should
+# all work as commands.
+
+def check(debugger, command, context, result, internal_dict):
+if (not isinstance(debugger, lldb.SBDebugger) or
+not isinstance(command, str) or
+not isinstance(result, lldb.SBCommandReturnObject) or
+not isinstance(internal_dict, dict) or
+(not context is None and
+not isinstance(context, lldb.SBExecutionContext))):
+  raise Exception()
+result.AppendMessage("All good.")
+
+def vfoobar(*args):
+check(*args)
+
+def v5foobar(debugger, command, context, result, internal_dict, *args):
+check(debugger, command, context, result, internal_dict)
+
+def foobar(debugger, command, context, result, internal_dict):
+check(debugger, command, context, result, internal_dict)
+
+def foobar4(debugger, command, result, internal_dict):
+check(debugger, command, None, result, internal_dict)
+
+class FooBar:
+@staticmethod
+def sfoobar(debugger, command, context, result, internal_dict):
+  check(debugger, command, context, result, internal_dict)
+
+@classmethod
+def cfoobar(cls, debugger, command, context, 

[Lldb-commits] [lldb] r375182 - eliminate one form of PythonObject::Reset()

2019-10-17 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Thu Oct 17 15:22:09 2019
New Revision: 375182

URL: http://llvm.org/viewvc/llvm-project?rev=375182=rev
Log:
eliminate one form of PythonObject::Reset()

Summary:
I'd like to eliminate all forms of Reset() and all public constructors
on these objects, so the only way to make them is with Take<> and Retain<>
and the only way to copy or move them is with actual c++ copy, move, or
assignment.

This is a simple place to start.

Reviewers: JDevlieghere, clayborg, labath, jingham

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D69080

Modified:
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Modified: 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=375182=375181=375182=diff
==
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
(original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
Thu Oct 17 15:22:09 2019
@@ -410,7 +410,7 @@ void PythonString::SetString(llvm::Strin
 llvm::consumeError(s.takeError());
 Reset();
   } else {
-PythonObject::Reset(std::move(s.get()));
+*this = std::move(s.get());
   }
 }
 

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h?rev=375182=375181=375182=diff
==
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h 
(original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h Thu 
Oct 17 15:22:09 2019
@@ -182,7 +182,8 @@ public:
 Reset(type, py_obj);
   }
 
-  PythonObject(const PythonObject ) : m_py_obj(nullptr) { Reset(rhs); }
+  PythonObject(const PythonObject )
+  : PythonObject(PyRefType::Borrowed, rhs.m_py_obj) {}
 
   PythonObject(PythonObject &) {
 m_py_obj = rhs.m_py_obj;
@@ -197,19 +198,6 @@ public:
 m_py_obj = nullptr;
   }
 
-  void Reset(const PythonObject ) {
-if (!rhs.IsValid())
-  Reset();
-else
-  Reset(PyRefType::Borrowed, rhs.m_py_obj);
-  }
-
-  // PythonObject is implicitly convertible to PyObject *, which will call the
-  // wrong overload.  We want to explicitly disallow this, since a PyObject
-  // *always* owns its reference.  Therefore the overload which takes a
-  // PyRefType doesn't make sense, and the copy constructor should be used.
-  void Reset(PyRefType type, const PythonObject ) = delete;
-
   void Reset(PyRefType type, PyObject *py_obj) {
 if (py_obj == m_py_obj)
   return;
@@ -244,19 +232,9 @@ public:
 return result;
   }
 
-  PythonObject =(const PythonObject ) {
-Reset(PyRefType::Borrowed, other.get());
-return *this;
-  }
-
-  void Reset(PythonObject &) {
+  PythonObject =(PythonObject other) {
 Reset();
-m_py_obj = other.m_py_obj;
-other.m_py_obj = nullptr;
-  }
-
-  PythonObject =(PythonObject &) {
-Reset(std::move(other));
+m_py_obj = std::exchange(other.m_py_obj, nullptr);
 return *this;
   }
 

Modified: 
lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp?rev=375182=375181=375182=diff
==
--- lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp 
(original)
+++ lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp 
Thu Oct 17 15:22:09 2019
@@ -323,8 +323,8 @@ TEST_F(PythonDataObjectsTest, TestPython
   PythonList list(PyRefType::Owned, py_list);
 
   PythonObject list_items[list_size];
-  list_items[0].Reset(PythonInteger(long_value0));
-  list_items[1].Reset(PythonString(string_value1));
+  list_items[0] = PythonInteger(long_value0);
+  list_items[1] = PythonString(string_value1);
 
   for (unsigned i = 0; i < list_size; ++i)
 list.SetItemAtIndex(i, list_items[i]);
@@ -469,10 +469,10 @@ TEST_F(PythonDataObjectsTest, TestPython
   PythonObject py_keys[dict_entries];
   PythonObject py_values[dict_entries];
 
-  py_keys[0].Reset(PythonString(key_0));
-  py_keys[1].Reset(PythonInteger(key_1));
-  py_values[0].Reset(PythonInteger(value_0));
-  py_values[1].Reset(PythonString(value_1));
+  py_keys[0] = PythonString(key_0);
+  py_keys[1] = PythonInteger(key_1);
+  py_values[0] = PythonInteger(value_0);
+  py_values[1] = PythonString(value_1);
 
   PyObject *py_dict = PyDict_New();
   

[Lldb-commits] [lldb] r375181 - clean up the implementation of PythonCallable::GetNumArguments

2019-10-17 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Thu Oct 17 15:22:06 2019
New Revision: 375181

URL: http://llvm.org/viewvc/llvm-project?rev=375181=rev
Log:
clean up the implementation of PythonCallable::GetNumArguments

Summary:
The current implementation of PythonCallable::GetNumArguments
is not exception safe, has weird semantics, and is just plain
incorrect for some kinds of functions.

Python 3.3 introduces inspect.signature, which lets us easily
query for function signatures in a sane and documented way.

This patch leaves the old implementation in place for < 3.3,
but uses inspect.signature for modern pythons.   It also leaves
the old weird semantics in place, but with FIXMEs grousing about
it.   We should update the callers and fix the semantics in a
subsequent patch.It also adds some tests.

Reviewers: JDevlieghere, clayborg, labath, jingham

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68995

Modified:
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Modified: 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=375181=375180=375181=diff
==
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
(original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
Thu Oct 17 15:22:06 2019
@@ -31,6 +31,7 @@
 using namespace lldb_private;
 using namespace lldb;
 using namespace lldb_private::python;
+using llvm::cantFail;
 using llvm::Error;
 using llvm::Expected;
 
@@ -47,6 +48,20 @@ Expected python::As
+Expected python::As(Expected &) {
+  if (!obj)
+return obj.takeError();
+  PyObject *str_obj = PyObject_Str(obj.get().get());
+  if (!obj)
+return llvm::make_error();
+  auto str = Take(str_obj);
+  auto utf8 = str.AsUTF8();
+  if (!utf8)
+return utf8.takeError();
+  return utf8.get();
+}
+
 void StructuredPythonObject::Serialize(llvm::json::OStream ) const {
   s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str());
 }
@@ -657,16 +672,66 @@ PythonList PythonDictionary::GetKeys() c
 }
 
 PythonObject PythonDictionary::GetItemForKey(const PythonObject ) const {
-  if (IsAllocated() && key.IsValid())
-return PythonObject(PyRefType::Borrowed,
-PyDict_GetItem(m_py_obj, key.get()));
-  return PythonObject();
+  auto item = GetItem(key);
+  if (!item) {
+llvm::consumeError(item.takeError());
+return PythonObject();
+  }
+  return std::move(item.get());
+}
+
+Expected
+PythonDictionary::GetItem(const PythonObject ) const {
+  if (!IsValid())
+return nullDeref();
+#if PY_MAJOR_VERSION >= 3
+  PyObject *o = PyDict_GetItemWithError(m_py_obj, key.get());
+  if (PyErr_Occurred())
+return exception();
+#else
+  PyObject *o = PyDict_GetItem(m_py_obj, key.get());
+#endif
+  if (!o)
+return keyError();
+  return Retain(o);
+}
+
+Expected PythonDictionary::GetItem(const char *key) const {
+  if (!IsValid())
+return nullDeref();
+  PyObject *o = PyDict_GetItemString(m_py_obj, key);
+  if (PyErr_Occurred())
+return exception();
+  if (!o)
+return keyError();
+  return Retain(o);
+}
+
+Error PythonDictionary::SetItem(const PythonObject ,
+const PythonObject ) const {
+  if (!IsValid() || !value.IsValid())
+return nullDeref();
+  int r = PyDict_SetItem(m_py_obj, key.get(), value.get());
+  if (r < 0)
+return exception();
+  return Error::success();
+}
+
+Error PythonDictionary::SetItem(const char *key,
+const PythonObject ) const {
+  if (!IsValid() || !value.IsValid())
+return nullDeref();
+  int r = PyDict_SetItemString(m_py_obj, key, value.get());
+  if (r < 0)
+return exception();
+  return Error::success();
 }
 
 void PythonDictionary::SetItemForKey(const PythonObject ,
  const PythonObject ) {
-  if (IsAllocated() && key.IsValid() && value.IsValid())
-PyDict_SetItem(m_py_obj, key.get(), value.get());
+  Error error = SetItem(key, value);
+  if (error)
+llvm::consumeError(std::move(error));
 }
 
 StructuredData::DictionarySP
@@ -736,23 +801,89 @@ bool PythonCallable::Check(PyObject *py_
 }
 
 PythonCallable::ArgInfo PythonCallable::GetNumInitArguments() const {
-  ArgInfo result = {0, false, false, false};
-  if (!IsValid())
-return result;
-
-  PythonObject __init__ = GetAttributeValue("__init__");
-  if (__init__.IsValid() ) {
-auto __init_callable__ = __init__.AsType();
-if (__init_callable__.IsValid())
-  return __init_callable__.GetNumArguments();
+  auto arginfo = GetInitArgInfo();
+  if (!arginfo) {
+

[Lldb-commits] [lldb] r375073 - delete SWIG typemaps for FILE*

2019-10-16 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct 16 18:35:22 2019
New Revision: 375073

URL: http://llvm.org/viewvc/llvm-project?rev=375073=rev
Log:
delete SWIG typemaps for FILE*

Summary:
The SWIG typemaps for FILE* are no longer used, so
this patch deletes them.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68963

Modified:
lldb/trunk/include/lldb/Host/File.h
lldb/trunk/scripts/Python/python-typemaps.swig
lldb/trunk/source/Host/common/File.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Modified: lldb/trunk/include/lldb/Host/File.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=375073=375072=375073=diff
==
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Wed Oct 16 18:35:22 2019
@@ -136,20 +136,6 @@ public:
   /// ENOTSUP, success, or another error.
   virtual Status GetFileSpec(FileSpec _spec) const;
 
-  /// DEPRECATED! Extract the underlying FILE* and reset this File without 
closing it.
-  ///
-  /// This is only here to support legacy SB interfaces that need to convert 
scripting
-  /// language objects into FILE* streams.   That conversion is inherently 
sketchy and
-  /// doing so may cause the stream to be leaked.
-  ///
-  /// After calling this the File will be reset to its original state.  It 
will be
-  /// invalid and it will not hold on to any resources.
-  ///
-  /// \return
-  /// The underlying FILE* stream from this File, if one exists and can be 
extracted,
-  /// nullptr otherwise.
-  virtual FILE *TakeStreamAndClear();
-
   /// Get underlying OS file descriptor for this file, or kInvalidDescriptor.
   /// If the descriptor is valid, then it may be used directly for I/O
   /// However, the File may also perform it's own buffering, so avoid using
@@ -413,7 +399,6 @@ public:
   Status Close() override;
   WaitableHandle GetWaitableHandle() override;
   Status GetFileSpec(FileSpec _spec) const override;
-  FILE *TakeStreamAndClear() override;
   int GetDescriptor() const override;
   FILE *GetStream() override;
   off_t SeekFromStart(off_t offset, Status *error_ptr = nullptr) override;

Modified: lldb/trunk/scripts/Python/python-typemaps.swig
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-typemaps.swig?rev=375073=375072=375073=diff
==
--- lldb/trunk/scripts/Python/python-typemaps.swig (original)
+++ lldb/trunk/scripts/Python/python-typemaps.swig Wed Oct 16 18:35:22 2019
@@ -451,74 +451,6 @@ bool SetNumberFromPyObject(doubl
   }
 }
 
-// FIXME both of these paths wind up calling fdopen() with no provision for 
ever calling
-// fclose() on the result.  SB interfaces that use FILE* should be deprecated 
for scripting
-// use and this typemap should eventually be removed.
-%typemap(in) FILE * {
-   using namespace lldb_private;
-   if ($input == Py_None)
-  $1 = nullptr;
-   else if (!lldb_private::PythonFile::Check($input)) {
-  int fd = PyObject_AsFileDescriptor($input);
-  if (fd < 0 || PyErr_Occurred())
-return nullptr;
-  PythonObject py_input(PyRefType::Borrowed, $input);
-  PythonString py_mode = 
py_input.GetAttributeValue("mode").AsType();
-  if (!py_mode.IsValid() || PyErr_Occurred())
-return nullptr;
-FILE *f;
-if ((f = fdopen(fd, py_mode.GetString().str().c_str(
-  $1 = f;
-else {
-  PyErr_SetString(PyExc_TypeError, strerror(errno));
-  return nullptr;
-}
-   }
-   else
-   {
-  PythonFile py_file(PyRefType::Borrowed, $input);
-  lldb::FileUP file = py_file.GetUnderlyingFile();
-  if (!file)
- return nullptr;
-  $1 = file->TakeStreamAndClear();
-}
-}
-
-%typemap(out) FILE * {
-   // TODO: This is gross. We should find a way to fetch the mode flags from 
the
-   // lldb_private::File object.
-   char mode[4] = {0};
-%#ifdef __APPLE__
-   int i = 0;
-   if ($1)
-   {
-   short flags = $1->_flags;
-
-   if (flags & __SRD)
-  mode[i++] = 'r';
-   else if (flags & __SWR)
-  mode[i++] = 'w';
-   else // if (flags & __SRW)
-  mode[i++] = 'a';
-}
-%#else
-   // There's no portable way to get the mode here. We just return a mode which
-   // permits both reads and writes and count on the operating system to return
-   // an error when an invalid operation is attempted.
-   mode[0] = 'r';
-   mode[1] = '+';
-%#endif
-   using namespace lldb_private;
-   NativeFile file($1, false);

[Lldb-commits] [lldb] r374964 - update ScriptInterpreterPython to use File, not FILE*

2019-10-15 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Tue Oct 15 18:58:15 2019
New Revision: 374964

URL: http://llvm.org/viewvc/llvm-project?rev=374964=rev
Log:
update ScriptInterpreterPython to use File, not FILE*

Summary:
ScriptInterpreterPython needs to save and restore sys.stdout and
friends when LLDB runs a python script.

It currently does this using FILE*, which is not optimal.  If
whatever was in sys.stdout can not be represented as a FILE*, then
it will not be restored correctly when the script is finished.

It also means that if the debugger's own output stream is not
representable as a file, ScriptInterpreterPython will not be able
to redirect python's  output correctly.

This patch updates ScriptInterpreterPython to represent files with
lldb_private::File, and to represent whatever the user had in
sys.stdout as simply a PythonObject.

This will make lldb interoperate better with other scripts or programs
that need to manipulate sys.stdout.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68962

Modified:

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/source/Core/Debugger.cpp

lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374964=374963=374964=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Tue Oct 15 18:58:15 2019
@@ -445,9 +445,8 @@ class FileHandleTestCase(lldbtest.TestBa
 self.assertTrue(re.search(r'error:.*lolwut', errors))
 self.assertTrue(re.search(r'zork', errors))
 
-#FIXME This shouldn't fail for python2 either.
+
 @add_test_categories(['pyapi'])
-@skipIf(py_version=['<', (3,)])
 def test_replace_stdout(self):
 f = io.StringIO()
 with replace_stdout(f):
@@ -458,7 +457,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() #FIXME bug in ScriptInterpreterPython
 def test_replace_stdout_with_nonfile(self):
 debugger = self.debugger
 f = io.StringIO()

Modified: lldb/trunk/source/Core/Debugger.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Debugger.cpp?rev=374964=374963=374964=diff
==
--- lldb/trunk/source/Core/Debugger.cpp (original)
+++ lldb/trunk/source/Core/Debugger.cpp Tue Oct 15 18:58:15 2019
@@ -973,34 +973,31 @@ void Debugger::AdoptTopIOHandlerFilesIfI
   std::lock_guard guard(m_input_reader_stack.GetMutex());
   IOHandlerSP top_reader_sp(m_input_reader_stack.Top());
   // If no STDIN has been set, then set it appropriately
-  if (!in) {
+  if (!in || !in->IsValid()) {
 if (top_reader_sp)
   in = top_reader_sp->GetInputFileSP();
 else
   in = GetInputFileSP();
-
 // If there is nothing, use stdin
 if (!in)
   in = std::make_shared(stdin, false);
   }
   // If no STDOUT has been set, then set it appropriately
-  if (!out) {
+  if (!out || !out->GetFile().IsValid()) {
 if (top_reader_sp)
   out = top_reader_sp->GetOutputStreamFileSP();
 else
   out = GetOutputStreamSP();
-
 // If there is nothing, use stdout
 if (!out)
   out = std::make_shared(stdout, false);
   }
   // If no STDERR has been set, then set it appropriately
-  if (!err) {
+  if (!err || !err->GetFile().IsValid()) {
 if (top_reader_sp)
   err = top_reader_sp->GetErrorStreamFileSP();
 else
   err = GetErrorStreamSP();
-
 // If there is nothing, use stderr
 if (!err)
   err = std::make_shared(stderr, false);

Modified: 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=374964=374963=374964=diff
==
--- 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp 
(original)
+++ 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp 
Tue Oct 15 18:58:15 2019
@@ -370,7 +370,7 @@ void ScriptInterpreterPython::Terminate(
 
 ScriptInterpreterPythonImpl::Locker::Locker(
 ScriptInterpreterPythonImpl *py_interpreter, uint16_t on_entry,
-uint16_t on_leave, FILE *in, FILE *out, FILE *err)
+uint16_t on_leave, FileSP in, FileSP out, FileSP 

[Lldb-commits] [lldb] r374924 - remove FILE* usage from SBStream.i

2019-10-15 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Tue Oct 15 10:41:40 2019
New Revision: 374924

URL: http://llvm.org/viewvc/llvm-project?rev=374924=rev
Log:
remove FILE* usage from SBStream.i

Summary:
This patch removes FILE* and replaces it with SBFile and FileSP the
SWIG interface for `SBStream.i`.   And this is the last one.   With
this change, nothing in the python API will can access a FILE* method
on the C++ side.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68960

Modified:
lldb/trunk/include/lldb/API/SBStream.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/interface/SBStream.i
lldb/trunk/source/API/SBStream.cpp

Modified: lldb/trunk/include/lldb/API/SBStream.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBStream.h?rev=374924=374923=374924=diff
==
--- lldb/trunk/include/lldb/API/SBStream.h (original)
+++ lldb/trunk/include/lldb/API/SBStream.h Tue Oct 15 10:41:40 2019
@@ -39,6 +39,10 @@ public:
 
   void RedirectToFile(const char *path, bool append);
 
+  void RedirectToFile(lldb::SBFile file);
+
+  void RedirectToFile(lldb::FileSP file);
+
   void RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership);
 
   void RedirectToFileDescriptor(int fd, bool transfer_fh_ownership);

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374924=374923=374924=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Tue Oct 15 10:41:40 2019
@@ -892,3 +892,30 @@ class FileHandleTestCase(lldbtest.TestBa
 sbf = self.debugger.GetInputFile()
 if sys.version_info.major >= 3:
 self.assertEqual(sbf.GetFile().fileno(), 0)
+
+
+@add_test_categories(['pyapi'])
+def test_sbstream(self):
+
+with open(self.out_filename, 'w') as f:
+stream = lldb.SBStream()
+stream.RedirectToFile(f)
+stream.Print("zork")
+with open(self.out_filename, 'r') as f:
+self.assertEqual(f.read().strip(), "zork")
+
+with open(self.out_filename, 'w') as f:
+stream = lldb.SBStream()
+stream.RedirectToFileHandle(f, True)
+stream.Print("Yendor")
+with open(self.out_filename, 'r') as f:
+self.assertEqual(f.read().strip(), "Yendor")
+
+stream = lldb.SBStream()
+f = open(self.out_filename,  'w')
+stream.RedirectToFile(lldb.SBFile.Create(f, borrow=False))
+stream.Print("Frobozz")
+stream = None
+self.assertTrue(f.closed)
+with open(self.out_filename, 'r') as f:
+self.assertEqual(f.read().strip(), "Frobozz")

Modified: lldb/trunk/scripts/interface/SBStream.i
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBStream.i?rev=374924=374923=374924=diff
==
--- lldb/trunk/scripts/interface/SBStream.i (original)
+++ lldb/trunk/scripts/interface/SBStream.i Tue Oct 15 10:41:40 2019
@@ -75,7 +75,18 @@ public:
 RedirectToFile (const char *path, bool append);
 
 void
-RedirectToFileHandle (FILE *fh, bool transfer_fh_ownership);
+RedirectToFile (lldb::SBFile file);
+
+void
+RedirectToFile (lldb::FileSP file);
+
+%extend {
+%feature("autodoc", "DEPRECATED, use RedirectToFile");
+void
+RedirectToFileHandle (lldb::FileSP file, bool transfer_fh_ownership) {
+self->RedirectToFile(file);
+}
+}
 
 void
 RedirectToFileDescriptor (int fd, bool transfer_fh_ownership);

Modified: lldb/trunk/source/API/SBStream.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBStream.cpp?rev=374924=374923=374924=diff
==
--- lldb/trunk/source/API/SBStream.cpp (original)
+++ lldb/trunk/source/API/SBStream.cpp Tue Oct 15 10:41:40 2019
@@ -9,6 +9,7 @@
 #include "lldb/API/SBStream.h"
 
 #include "SBReproducerPrivate.h"
+#include "lldb/API/SBFile.h"
 #include "lldb/Core/StreamFile.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Utility/Status.h"
@@ -108,8 +109,19 @@ void SBStream::RedirectToFile(const char
 void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
   LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
  transfer_fh_ownership);
+  FileSP file = std::make_unique(fh, 

[Lldb-commits] [lldb] r374916 - eliminate virtual methods from PythonDataObjects

2019-10-15 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Tue Oct 15 10:12:49 2019
New Revision: 374916

URL: http://llvm.org/viewvc/llvm-project?rev=374916=rev
Log:
eliminate virtual methods from PythonDataObjects

Summary:
This patch eliminates a bunch of boilerplate from
PythonDataObjects, as well as the use of virtual methods.
In my opinion it also makes the Reset logic a lot more
clear and easy to follow.   The price is yet another
template.   I think it's worth it.

Reviewers: JDevlieghere, jasonmolenda, labath, zturner

Reviewed By: JDevlieghere, labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68918

Modified:
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Modified: 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=374916=374915=374916=diff
==
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
(original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
Tue Oct 15 10:12:49 2019
@@ -213,43 +213,19 @@ StructuredData::ObjectSP PythonObject::C
 }
 
 // PythonString
-PythonBytes::PythonBytes() : PythonObject() {}
 
-PythonBytes::PythonBytes(llvm::ArrayRef bytes) : PythonObject() {
-  SetBytes(bytes);
-}
+PythonBytes::PythonBytes(llvm::ArrayRef bytes) { SetBytes(bytes); }
 
-PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) : PythonObject() 
{
+PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) {
   SetBytes(llvm::ArrayRef(bytes, length));
 }
 
-PythonBytes::PythonBytes(PyRefType type, PyObject *py_obj) : PythonObject() {
-  Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
-}
-
-PythonBytes::~PythonBytes() {}
-
 bool PythonBytes::Check(PyObject *py_obj) {
   if (!py_obj)
 return false;
   return PyBytes_Check(py_obj);
 }
 
-void PythonBytes::Reset(PyRefType type, PyObject *py_obj) {
-  // Grab the desired reference type so that if we end up rejecting `py_obj` it
-  // still gets decremented if necessary.
-  PythonObject result(type, py_obj);
-
-  if (!PythonBytes::Check(py_obj)) {
-PythonObject::Reset();
-return;
-  }
-
-  // Calling PythonObject::Reset(const PythonObject&) will lead to stack
-  // overflow since it calls back into the virtual implementation.
-  PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
 llvm::ArrayRef PythonBytes::GetBytes() const {
   if (!IsValid())
 return llvm::ArrayRef();
@@ -290,36 +266,12 @@ PythonByteArray::PythonByteArray(const u
   Reset(PyRefType::Owned, PyByteArray_FromStringAndSize(str, length));
 }
 
-PythonByteArray::PythonByteArray(PyRefType type, PyObject *o) {
-  Reset(type, o);
-}
-
-PythonByteArray::PythonByteArray(const PythonBytes )
-: PythonObject(object) {}
-
-PythonByteArray::~PythonByteArray() {}
-
 bool PythonByteArray::Check(PyObject *py_obj) {
   if (!py_obj)
 return false;
   return PyByteArray_Check(py_obj);
 }
 
-void PythonByteArray::Reset(PyRefType type, PyObject *py_obj) {
-  // Grab the desired reference type so that if we end up rejecting `py_obj` it
-  // still gets decremented if necessary.
-  PythonObject result(type, py_obj);
-
-  if (!PythonByteArray::Check(py_obj)) {
-PythonObject::Reset();
-return;
-  }
-
-  // Calling PythonObject::Reset(const PythonObject&) will lead to stack
-  // overflow since it calls back into the virtual implementation.
-  PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
 llvm::ArrayRef PythonByteArray::GetBytes() const {
   if (!IsValid())
 return llvm::ArrayRef();
@@ -357,17 +309,7 @@ Expected PythonString::Fro
   return Take(str);
 }
 
-PythonString::PythonString(PyRefType type, PyObject *py_obj) : PythonObject() {
-  Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
-}
-
-PythonString::PythonString(llvm::StringRef string) : PythonObject() {
-  SetString(string);
-}
-
-PythonString::PythonString() : PythonObject() {}
-
-PythonString::~PythonString() {}
+PythonString::PythonString(llvm::StringRef string) { SetString(string); }
 
 bool PythonString::Check(PyObject *py_obj) {
   if (!py_obj)
@@ -382,29 +324,26 @@ bool PythonString::Check(PyObject *py_ob
   return false;
 }
 
-void PythonString::Reset(PyRefType type, PyObject *py_obj) {
-  // Grab the desired reference type so that if we end up rejecting `py_obj` it
-  // still gets decremented if necessary.
-  PythonObject result(type, py_obj);
-
-  if (!PythonString::Check(py_obj)) {
-PythonObject::Reset();
-return;
-  }
+void PythonString::Convert(PyRefType , PyObject *_obj) {
 #if PY_MAJOR_VERSION < 3
   // In Python 2, Don't store PyUnicode objects directly, because we need
   // access to their underlying character buffers which 

[Lldb-commits] [lldb] r374912 - convert SBDebugger::***FileHandle() wrappers to native files.

2019-10-15 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Tue Oct 15 09:59:20 2019
New Revision: 374912

URL: http://llvm.org/viewvc/llvm-project?rev=374912=rev
Log:
convert SBDebugger::***FileHandle() wrappers to native files.

Summary:
This patch converts the swig wrappers for SetInputFileHandle() and friends
to emulate the old behavior using SetInputFile().

This will clear the way for deleting the FILE* typemaps altogether.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: mehdi_amini, dexonsmith, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68856

Modified:

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/interface/SBDebugger.i

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374912=374911=374912=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Tue Oct 15 09:59:20 2019
@@ -129,8 +129,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@skipIfWindows # FIXME pre-existing bug, should be fixed
-   # when we delete the FILE* typemaps.
 def test_legacy_file_out_script(self):
 with open(self.out_filename, 'w') as f:
 self.debugger.SetOutputFileHandle(f, False)
@@ -155,8 +153,6 @@ class FileHandleTestCase(lldbtest.TestBa
 self.assertIn('deadbeef', f.read())
 
 @add_test_categories(['pyapi'])
-@skipIfWindows # FIXME pre-existing bug, should be fixed
-   # when we delete the FILE* typemaps.
 def test_legacy_file_err_with_get(self):
 with open(self.out_filename, 'w') as f:
 self.debugger.SetErrorFileHandle(f, False)
@@ -194,11 +190,11 @@ class FileHandleTestCase(lldbtest.TestBa
 @add_test_categories(['pyapi'])
 def test_sbfile_type_errors(self):
 sbf = lldb.SBFile()
-self.assertRaises(TypeError, sbf.Write, None)
-self.assertRaises(TypeError, sbf.Read, None)
-self.assertRaises(TypeError, sbf.Read, b'this bytes is not mutable')
-self.assertRaises(TypeError, sbf.Write, u"ham sandwich")
-self.assertRaises(TypeError, sbf.Read, u"ham sandwich")
+self.assertRaises(Exception, sbf.Write, None)
+self.assertRaises(Exception, sbf.Read, None)
+self.assertRaises(Exception, sbf.Read, b'this bytes is not mutable')
+self.assertRaises(Exception, sbf.Write, u"ham sandwich")
+self.assertRaises(Exception, sbf.Read, u"ham sandwich")
 
 
 @add_test_categories(['pyapi'])
@@ -859,3 +855,40 @@ class FileHandleTestCase(lldbtest.TestBa
 with open(self.out_filename, 'r') as f:
 self.assertEqual(list(range(10)), list(map(int, 
f.read().strip().split(
 
+
+@add_test_categories(['pyapi'])
+def test_set_filehandle_none(self):
+self.assertRaises(Exception, self.debugger.SetOutputFile, None)
+self.assertRaises(Exception, self.debugger.SetOutputFile, "ham 
sandwich")
+self.assertRaises(Exception, self.debugger.SetOutputFileHandle, "ham 
sandwich")
+self.assertRaises(Exception, self.debugger.SetInputFile, None)
+self.assertRaises(Exception, self.debugger.SetInputFile, "ham 
sandwich")
+self.assertRaises(Exception, self.debugger.SetInputFileHandle, "ham 
sandwich")
+self.assertRaises(Exception, self.debugger.SetErrorFile, None)
+self.assertRaises(Exception, self.debugger.SetErrorFile, "ham 
sandwich")
+self.assertRaises(Exception, self.debugger.SetErrorFileHandle, "ham 
sandwich")
+
+with open(self.out_filename, 'w') as f:
+status = self.debugger.SetOutputFile(f)
+self.assertTrue(status.Success())
+status = self.debugger.SetErrorFile(f)
+self.assertTrue(status.Success())
+self.debugger.SetOutputFileHandle(None, False)
+self.debugger.SetErrorFileHandle(None, False)
+sbf = self.debugger.GetOutputFile()
+if sys.version_info.major >= 3:
+# python 2 lacks PyFile_FromFd, so GetFile() will
+# have to duplicate the file descriptor and make a FILE*
+# in order to convert a NativeFile it back to a python
+# file.
+self.assertEqual(sbf.GetFile().fileno(), 1)
+sbf = self.debugger.GetErrorFile()
+if sys.version_info.major >= 3:
+self.assertEqual(sbf.GetFile().fileno(), 2)
+with open(self.out_filename, 'r') as f:
+status = self.debugger.SetInputFile(f)
+self.assertTrue(status.Success())

[Lldb-commits] [lldb] r374911 - SBFile::GetFile: convert SBFile back into python native files.

2019-10-15 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Tue Oct 15 09:46:27 2019
New Revision: 374911

URL: http://llvm.org/viewvc/llvm-project?rev=374911=rev
Log:
SBFile::GetFile: convert SBFile back into python native files.

Summary:
This makes SBFile::GetFile public and adds a SWIG typemap to convert
the result back into a python native file.

If the underlying File itself came from a python file, it is returned
identically.   Otherwise a new python file object is created using
the file descriptor.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68737

Modified:
lldb/trunk/include/lldb/API/SBFile.h
lldb/trunk/include/lldb/Host/File.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/Python/python-typemaps.swig
lldb/trunk/scripts/interface/SBFile.i
lldb/trunk/source/API/SBFile.cpp
lldb/trunk/source/Host/common/File.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Modified: lldb/trunk/include/lldb/API/SBFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFile.h?rev=374911=374910=374911=diff
==
--- lldb/trunk/include/lldb/API/SBFile.h (original)
+++ lldb/trunk/include/lldb/API/SBFile.h Tue Oct 15 09:46:27 2019
@@ -36,6 +36,8 @@ public:
   operator bool() const;
   bool operator!() const;
 
+  FileSP GetFile() const;
+
 private:
   FileSP m_opaque_sp;
 };

Modified: lldb/trunk/include/lldb/Host/File.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=374911=374910=374911=diff
==
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Tue Oct 15 09:46:27 2019
@@ -62,6 +62,8 @@ public:
   static mode_t ConvertOpenOptionsForPOSIXOpen(OpenOptions open_options);
   static llvm::Expected GetOptionsFromMode(llvm::StringRef mode);
   static bool DescriptorIsValid(int descriptor) { return descriptor >= 0; };
+  static llvm::Expected
+  GetStreamOpenModeFromOptions(OpenOptions options);
 
   File()
   : IOObject(eFDTypeFile), m_is_interactive(eLazyBoolCalculate),
@@ -317,6 +319,25 @@ public:
   /// format string \a format.
   virtual size_t PrintfVarArg(const char *format, va_list args);
 
+  /// Return the OpenOptions for this file.
+  ///
+  /// Some options like eOpenOptionDontFollowSymlinks only make
+  /// sense when a file is being opened (or not at all)
+  /// and may not be preserved for this method.  But any valid
+  /// File should return either or both of eOpenOptionRead and
+  /// eOpenOptionWrite here.
+  ///
+  /// \return
+  ///OpenOptions flags for this file, or an error.
+  virtual llvm::Expected GetOptions() const;
+
+  llvm::Expected GetOpenMode() const {
+auto opts = GetOptions();
+if (!opts)
+  return opts.takeError();
+return GetStreamOpenModeFromOptions(opts.get());
+  }
+
   /// Get the permissions for a this file.
   ///
   /// \return
@@ -352,6 +373,10 @@ public:
 
   bool operator!() const { return !IsValid(); };
 
+  static char ID;
+  virtual bool isA(const void *classID) const { return classID ==  }
+  static bool classof(const File *file) { return file->isA(); }
+
 protected:
   LazyBool m_is_interactive;
   LazyBool m_is_real_terminal;
@@ -399,6 +424,13 @@ public:
   Status Flush() override;
   Status Sync() override;
   size_t PrintfVarArg(const char *format, va_list args) override;
+  llvm::Expected GetOptions() const override;
+
+  static char ID;
+  virtual bool isA(const void *classID) const override {
+return classID ==  || File::isA(classID);
+  }
+  static bool classof(const File *file) { return file->isA(); }
 
 protected:
   bool DescriptorIsValid() const {

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374911=374910=374911=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Tue Oct 15 09:46:27 2019
@@ -772,7 +772,21 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() # FIXME implement SBFile::GetFile
+def test_stdout_file(self):
+with open(self.out_filename, 'w') as f:
+status = self.debugger.SetOutputFile(f)
+self.assertTrue(status.Success())
+self.handleCmd(r"script sys.stdout.write('foobar\n')")
+with open(self.out_filename, 

[Lldb-commits] [lldb] r374825 - build fix for SBInstruction.

2019-10-14 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Oct 14 14:51:02 2019
New Revision: 374825

URL: http://llvm.org/viewvc/llvm-project?rev=374825=rev
Log:
build fix for SBInstruction.

oops!  I cherry-picked  rL374820 thinking it was completely
independent of D68737, but it wasn't.  It makes an incidental
use of SBFile::GetFile, which is introduced there, so I broke the
build.

The docs say you can commit without review for "obvious".   I think
this qualifies.   If this kind of fix isn't considered obvious, let
me know and I'll revert instead.

Fixes: rL374820

Modified:
lldb/trunk/include/lldb/API/SBFile.h
lldb/trunk/source/API/SBInstruction.cpp
lldb/trunk/source/API/SBInstructionList.cpp

Modified: lldb/trunk/include/lldb/API/SBFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFile.h?rev=374825=374824=374825=diff
==
--- lldb/trunk/include/lldb/API/SBFile.h (original)
+++ lldb/trunk/include/lldb/API/SBFile.h Mon Oct 14 14:51:02 2019
@@ -14,6 +14,8 @@
 namespace lldb {
 
 class LLDB_API SBFile {
+  friend class SBInstruction;
+  friend class SBInstructionList;
   friend class SBDebugger;
   friend class SBCommandReturnObject;
   friend class SBProcess;

Modified: lldb/trunk/source/API/SBInstruction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBInstruction.cpp?rev=374825=374824=374825=diff
==
--- lldb/trunk/source/API/SBInstruction.cpp (original)
+++ lldb/trunk/source/API/SBInstruction.cpp Mon Oct 14 14:51:02 2019
@@ -264,7 +264,7 @@ void SBInstruction::Print(FILE *outp) {
 
 void SBInstruction::Print(SBFile out) {
   LLDB_RECORD_METHOD(void, SBInstruction, Print, (SBFile), out);
-  Print(out.GetFile());
+  Print(out.m_opaque_sp);
 }
 
 void SBInstruction::Print(FileSP out_sp) {

Modified: lldb/trunk/source/API/SBInstructionList.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBInstructionList.cpp?rev=374825=374824=374825=diff
==
--- lldb/trunk/source/API/SBInstructionList.cpp (original)
+++ lldb/trunk/source/API/SBInstructionList.cpp Mon Oct 14 14:51:02 2019
@@ -130,7 +130,7 @@ void SBInstructionList::Print(SBFile out
   LLDB_RECORD_METHOD(void, SBInstructionList, Print, (SBFile), out);
   if (!out.IsValid())
 return;
-  StreamFile stream(out.GetFile());
+  StreamFile stream(out.m_opaque_sp);
   GetDescription(stream);
 }
 


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r374820 - remove FILE* bindings from SBInstruction.

2019-10-14 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Oct 14 13:59:57 2019
New Revision: 374820

URL: http://llvm.org/viewvc/llvm-project?rev=374820=rev
Log:
remove FILE* bindings from SBInstruction.

Summary:
This patch replaces the FILE* python bindings for SBInstruction and
SBInstructionList and replaces them with the new, safe SBFile and FileSP
bindings.

I also re-enable `Test_Disassemble_VST1_64`, because now we can use
the file bindings as an additional test of the disassembler, and we
can use the disassembler test as a test of the file bindings.

The bugs referred to in the comments appear to have been fixed.   The
radar is closed now and the bugzilla bug does not reproduce with the
instructions given.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68890

Modified:
lldb/trunk/include/lldb/API/SBInstruction.h
lldb/trunk/include/lldb/API/SBInstructionList.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instruction.py

lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instructionlist.py

lldb/trunk/packages/Python/lldbsuite/test/python_api/disassemble-raw-data/TestDisassemble_VST1_64.py
lldb/trunk/scripts/interface/SBInstruction.i
lldb/trunk/scripts/interface/SBInstructionList.i
lldb/trunk/source/API/SBInstruction.cpp
lldb/trunk/source/API/SBInstructionList.cpp

Modified: lldb/trunk/include/lldb/API/SBInstruction.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBInstruction.h?rev=374820=374819=374820=diff
==
--- lldb/trunk/include/lldb/API/SBInstruction.h (original)
+++ lldb/trunk/include/lldb/API/SBInstruction.h Mon Oct 14 13:59:57 2019
@@ -55,6 +55,10 @@ public:
 
   void Print(FILE *out);
 
+  void Print(SBFile out);
+
+  void Print(FileSP out);
+
   bool GetDescription(lldb::SBStream );
 
   bool EmulateWithFrame(lldb::SBFrame , uint32_t evaluate_options);

Modified: lldb/trunk/include/lldb/API/SBInstructionList.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBInstructionList.h?rev=374820=374819=374820=diff
==
--- lldb/trunk/include/lldb/API/SBInstructionList.h (original)
+++ lldb/trunk/include/lldb/API/SBInstructionList.h Mon Oct 14 13:59:57 2019
@@ -46,6 +46,10 @@ public:
 
   void Print(FILE *out);
 
+  void Print(SBFile out);
+
+  void Print(FileSP out);
+
   bool GetDescription(lldb::SBStream );
 
   bool DumpEmulationForAllInstructions(const char *triple);
@@ -56,6 +60,8 @@ protected:
   friend class SBTarget;
 
   void SetDisassembler(const lldb::DisassemblerSP _sp);
+  bool GetDescription(lldb_private::Stream );
+
 
 private:
   lldb::DisassemblerSP m_opaque_sp;

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instruction.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instruction.py?rev=374820=374819=374820=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instruction.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instruction.py
 Mon Oct 14 13:59:57 2019
@@ -9,7 +9,10 @@ def fuzz_obj(obj):
 obj.GetAddress()
 obj.GetByteSize()
 obj.DoesBranch()
-obj.Print(None)
+try:
+obj.Print(None)
+except Exception:
+pass
 obj.GetDescription(lldb.SBStream())
 obj.EmulateWithFrame(lldb.SBFrame(), 0)
 obj.DumpEmulation("armv7")

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instructionlist.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instructionlist.py?rev=374820=374819=374820=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instructionlist.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instructionlist.py
 Mon Oct 14 13:59:57 2019
@@ -9,7 +9,10 @@ def fuzz_obj(obj):
 obj.GetSize()
 obj.GetInstructionAtIndex(0x)
 obj.AppendInstruction(lldb.SBInstruction())
-obj.Print(None)
+try:
+obj.Print(None)
+except Exception:
+pass
 obj.GetDescription(lldb.SBStream())
 obj.DumpEmulationForAllInstructions("armv7")
 obj.Clear()

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/disassemble-raw-data/TestDisassemble_VST1_64.py
URL: 

[Lldb-commits] [lldb] r374817 - uint32_t options -> File::OpenOptions options

2019-10-14 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Oct 14 13:15:34 2019
New Revision: 374817

URL: http://llvm.org/viewvc/llvm-project?rev=374817=rev
Log:
uint32_t options -> File::OpenOptions options

Summary:
This patch re-types everywhere that passes a File::OpenOptions
as a uint32_t so it actually uses File::OpenOptions.

It also converts some OpenOptions related functions that fail
by returning 0 or NULL into llvm::Expected

split off from https://reviews.llvm.org/D68737

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68853

Modified:
lldb/trunk/include/lldb/Core/StreamFile.h
lldb/trunk/include/lldb/Host/File.h
lldb/trunk/include/lldb/Host/FileCache.h
lldb/trunk/include/lldb/Host/FileSystem.h
lldb/trunk/include/lldb/Target/Platform.h
lldb/trunk/include/lldb/Target/RemoteAwarePlatform.h
lldb/trunk/source/API/SBFile.cpp
lldb/trunk/source/API/SBStream.cpp
lldb/trunk/source/Commands/CommandObjectMemory.cpp
lldb/trunk/source/Commands/CommandObjectSettings.cpp
lldb/trunk/source/Core/StreamFile.cpp
lldb/trunk/source/Host/common/File.cpp
lldb/trunk/source/Host/common/FileCache.cpp
lldb/trunk/source/Host/common/FileSystem.cpp

lldb/trunk/source/Plugins/Platform/MacOSX/objcxx/PlatformiOSSimulatorCoreSimulatorSupport.mm
lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h

lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h

lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Target/Platform.cpp
lldb/trunk/source/Target/RemoteAwarePlatform.cpp
lldb/trunk/source/Target/Target.cpp

Modified: lldb/trunk/include/lldb/Core/StreamFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/StreamFile.h?rev=374817=374816=374817=diff
==
--- lldb/trunk/include/lldb/Core/StreamFile.h (original)
+++ lldb/trunk/include/lldb/Core/StreamFile.h Mon Oct 14 13:15:34 2019
@@ -30,7 +30,7 @@ public:
 
   StreamFile(const char *path);
 
-  StreamFile(const char *path, uint32_t options,
+  StreamFile(const char *path, File::OpenOptions options,
  uint32_t permissions = lldb::eFilePermissionsFileDefault);
 
   StreamFile(FILE *fh, bool transfer_ownership);

Modified: lldb/trunk/include/lldb/Host/File.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=374817=374816=374817=diff
==
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Mon Oct 14 13:15:34 2019
@@ -13,6 +13,7 @@
 #include "lldb/Utility/IOObject.h"
 #include "lldb/Utility/Status.h"
 #include "lldb/lldb-private.h"
+#include "llvm/ADT/BitmaskEnum.h"
 
 #include 
 #include 
@@ -21,6 +22,8 @@
 
 namespace lldb_private {
 
+LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
+
 /// \class File File.h "lldb/Host/File.h"
 /// An abstract base class for files.
 ///
@@ -35,7 +38,12 @@ public:
 
   // NB this enum is used in the lldb platform gdb-remote packet
   // vFile:open: and existing values cannot be modified.
-  enum OpenOptions {
+  //
+  // FIXME
+  // These values do not match the values used by GDB
+  // * https://sourceware.org/gdb/onlinedocs/gdb/Open-Flags.html#Open-Flags
+  // * rdar://problem/46788934
+  enum OpenOptions : uint32_t {
 eOpenOptionRead = (1u << 0),  // Open file for reading
 eOpenOptionWrite = (1u << 1), // Open file for writing
 eOpenOptionAppend =
@@ -47,11 +55,12 @@ public:
 (1u << 6), // Can create file only if it doesn't already exist
 eOpenOptionDontFollowSymlinks = (1u << 7),
 eOpenOptionCloseOnExec =
-(1u << 8) // Close the file when executing a new process
+(1u << 8), // Close the file when executing a new process
+LLVM_MARK_AS_BITMASK_ENUM(/* largest_value= */ eOpenOptionCloseOnExec)
   };
 
-  static mode_t ConvertOpenOptionsForPOSIXOpen(uint32_t open_options);
-  static uint32_t GetOptionsFromMode(llvm::StringRef mode);
+  static mode_t ConvertOpenOptionsForPOSIXOpen(OpenOptions open_options);
+  static llvm::Expected GetOptionsFromMode(llvm::StringRef mode);
   static bool DescriptorIsValid(int descriptor) { return descriptor >= 0; };
 
   File()
@@ -358,13 +367,13 @@ class NativeFile : public File {
 public:
   NativeFile()
   : m_descriptor(kInvalidDescriptor), m_own_descriptor(false),
-m_stream(kInvalidStream), m_options(0), m_own_stream(false) {}
+m_stream(kInvalidStream), m_options(), m_own_stream(false) {}
 
   NativeFile(FILE *fh, 

[Lldb-commits] [lldb] r374816 - remove FILE* usage from ReportEventState() and HandleProcessEvent()

2019-10-14 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Oct 14 13:15:28 2019
New Revision: 374816

URL: http://llvm.org/viewvc/llvm-project?rev=374816=rev
Log:
remove FILE* usage from ReportEventState() and HandleProcessEvent()

Summary:
This patch adds FileSP and SBFile versions of the API methods
ReportEventState and  HandleProcessEvent.   It points the SWIG
wrappers at these instead of the ones that use FILE* streams.

Reviewers: JDevlieghere, jasonmolenda, labath, jingham

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68546

Modified:
lldb/trunk/include/lldb/API/SBDebugger.h
lldb/trunk/include/lldb/API/SBFile.h
lldb/trunk/include/lldb/API/SBProcess.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_debugger.py

lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_process.py
lldb/trunk/scripts/interface/SBDebugger.i
lldb/trunk/scripts/interface/SBProcess.i
lldb/trunk/source/API/SBDebugger.cpp
lldb/trunk/source/API/SBProcess.cpp

Modified: lldb/trunk/include/lldb/API/SBDebugger.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBDebugger.h?rev=374816=374815=374816=diff
==
--- lldb/trunk/include/lldb/API/SBDebugger.h (original)
+++ lldb/trunk/include/lldb/API/SBDebugger.h Mon Oct 14 13:15:28 2019
@@ -117,7 +117,14 @@ public:
   lldb::SBListener GetListener();
 
   void HandleProcessEvent(const lldb::SBProcess ,
-  const lldb::SBEvent , FILE *out, FILE *err);
+  const lldb::SBEvent , FILE *out,
+  FILE *err); // DEPRECATED
+
+  void HandleProcessEvent(const lldb::SBProcess ,
+  const lldb::SBEvent , SBFile out, SBFile err);
+
+  void HandleProcessEvent(const lldb::SBProcess ,
+  const lldb::SBEvent , FileSP out, FileSP err);
 
   lldb::SBTarget CreateTarget(const char *filename, const char *target_triple,
   const char *platform_name,

Modified: lldb/trunk/include/lldb/API/SBFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFile.h?rev=374816=374815=374816=diff
==
--- lldb/trunk/include/lldb/API/SBFile.h (original)
+++ lldb/trunk/include/lldb/API/SBFile.h Mon Oct 14 13:15:28 2019
@@ -16,6 +16,7 @@ namespace lldb {
 class LLDB_API SBFile {
   friend class SBDebugger;
   friend class SBCommandReturnObject;
+  friend class SBProcess;
 
 public:
   SBFile();

Modified: lldb/trunk/include/lldb/API/SBProcess.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBProcess.h?rev=374816=374815=374816=diff
==
--- lldb/trunk/include/lldb/API/SBProcess.h (original)
+++ lldb/trunk/include/lldb/API/SBProcess.h Mon Oct 14 13:15:28 2019
@@ -67,6 +67,10 @@ public:
 
   void ReportEventState(const lldb::SBEvent , FILE *out) const;
 
+  void ReportEventState(const lldb::SBEvent , SBFile file) const;
+
+  void ReportEventState(const lldb::SBEvent , FileSP file) const;
+
   void AppendEventStateReport(const lldb::SBEvent ,
   lldb::SBCommandReturnObject );
 

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_debugger.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_debugger.py?rev=374816=374815=374816=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_debugger.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_debugger.py
 Mon Oct 14 13:15:28 2019
@@ -19,7 +19,10 @@ def fuzz_obj(obj):
 obj.GetCommandInterpreter()
 obj.HandleCommand("nothing here")
 listener = obj.GetListener()
-obj.HandleProcessEvent(lldb.SBProcess(), lldb.SBEvent(), None, None)
+try:
+obj.HandleProcessEvent(lldb.SBProcess(), lldb.SBEvent(), None, None)
+except Exception:
+pass
 obj.CreateTargetWithFileAndTargetTriple("a.out", "A-B-C")
 obj.CreateTargetWithFileAndArch("b.out", "arm")
 obj.CreateTarget("c.out")

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_process.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_process.py?rev=374816=374815=374816=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_process.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/sb_process.py
 Mon Oct 14 13:15:28 

[Lldb-commits] [lldb] r374803 - Fix test breakage caused by r374424

2019-10-14 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Oct 14 11:53:27 2019
New Revision: 374803

URL: http://llvm.org/viewvc/llvm-project?rev=374803=rev
Log:
Fix test breakage caused by r374424

Summary:
The build directory name is based on the test method name, so having
two test methods with the same name in the same test file is a
problem, even if they're in different test classes.

On linux and darwin this conflict can go unnoticed, but windows
has different filesystem semantics and it will fail when one
process tries to delete files still held open by another.

The problem is fixed just by changing the name of one of the test
methods.

Reviewers: JDevlieghere, jasonmolenda, labath, stella.stamenova

Reviewed By: stella.stamenova

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68951

Modified:

lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py?rev=374803=374802=374803=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py
 Mon Oct 14 11:53:27 2019
@@ -30,7 +30,7 @@ class CommandRunInterpreterLegacyAPICase
 self.dbg.SetErrorFileHandle (self.devnull, False)
 
 @add_test_categories(['pyapi'])
-def test_run_session_with_error_and_quit(self):
+def test_run_session_with_error_and_quit_legacy(self):
 """Run non-existing and quit command returns appropriate values"""
 
 n_errors, quit_requested, has_crashed = self.dbg.RunCommandInterpreter(


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r374576 - IOHandler: fall back on File::Read if a FILE* isn't available.

2019-10-11 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Fri Oct 11 10:43:32 2019
New Revision: 374576

URL: http://llvm.org/viewvc/llvm-project?rev=374576=rev
Log:
IOHandler: fall back on File::Read if a FILE* isn't available.

Summary:
IOHandler needs to read lines of input from a lldb::File.
The way it currently does this using, FILE*, which is something
we want to avoid now.   I'd prefer to just replace the FILE* code
with calls to File::Read, but it contains an awkward and
delicate workaround specific to ctrl-C handling on windows, and
it's not clear if or how that workaround would translate to
lldb::File.

So in this patch, we use use the FILE* if it's available, and only
fall back on File::Read if that's the only option.

I think this is a reasonable approach here for two reasons.  First
is that interactive terminal support is the one area where FILE*
can't be avoided.   We need them for libedit and curses anyway,
and using them here as well is consistent with that pattern.

The second reason is that the comments express a hope that the
underlying windows bug that's being worked around will be fixed one
day, so hopefully when that happens, that whole path can be deleted.

Reviewers: JDevlieghere, jasonmolenda, labath, lanza

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68622

Modified:
lldb/trunk/include/lldb/Core/IOHandler.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/source/Core/IOHandler.cpp

Modified: lldb/trunk/include/lldb/Core/IOHandler.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/IOHandler.h?rev=374576=374575=374576=diff
==
--- lldb/trunk/include/lldb/Core/IOHandler.h (original)
+++ lldb/trunk/include/lldb/Core/IOHandler.h Fri Oct 11 10:43:32 2019
@@ -431,6 +431,7 @@ protected:
   bool m_interrupt_exits;
   bool m_editing; // Set to true when fetching a line manually (not using
   // libedit)
+  std::string m_line_buffer;
 };
 
 // The order of base classes is important. Look at the constructor of

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374576=374575=374576=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Fri Oct 11 10:43:32 2019
@@ -399,9 +399,9 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() # FIXME IOHandler still using FILE*
+@skipIf(py_version=['<', (3,)])
 def test_string_inout(self):
-inf = io.StringIO("help help\n")
+inf = io.StringIO("help help\np/x ~0\n")
 outf = io.StringIO()
 status = self.debugger.SetOutputFile(lldb.SBFile(outf))
 self.assertTrue(status.Success())
@@ -412,10 +412,11 @@ class FileHandleTestCase(lldbtest.TestBa
 self.debugger.GetOutputFile().Flush()
 output = outf.getvalue()
 self.assertIn('Show a list of all debugger commands', output)
+self.assertIn('0xfff', output)
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() # FIXME IOHandler still using FILE*
+@skipIf(py_version=['<', (3,)])
 def test_bytes_inout(self):
 inf = io.BytesIO(b"help help\nhelp b\n")
 outf = io.BytesIO()

Modified: lldb/trunk/source/Core/IOHandler.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/IOHandler.cpp?rev=374576=374575=374576=diff
==
--- lldb/trunk/source/Core/IOHandler.cpp (original)
+++ lldb/trunk/source/Core/IOHandler.cpp Fri Oct 11 10:43:32 2019
@@ -70,6 +70,10 @@
 
 using namespace lldb;
 using namespace lldb_private;
+using llvm::None;
+using llvm::Optional;
+using llvm::StringRef;
+
 
 IOHandler::IOHandler(Debugger , IOHandler::Type type)
 : IOHandler(debugger, type,
@@ -306,94 +310,119 @@ void IOHandlerEditline::Deactivate() {
   m_delegate.IOHandlerDeactivated(*this);
 }
 
+// Split out a line from the buffer, if there is a full one to get.
+static Optional SplitLine(std::string _buffer) {
+  size_t pos = line_buffer.find('\n');
+  if (pos == std::string::npos)
+return None;
+  std::string line = StringRef(line_buffer.c_str(), pos).rtrim("\n\r");
+  line_buffer = line_buffer.substr(pos + 1);
+  return line;
+}
+
+// If the final line of the file ends without a end-of-line, return
+// it as a line anyway.
+static Optional SplitLineEOF(std::string _buffer) {
+  if (llvm::all_of(line_buffer, isspace))
+return None;
+  std::string line = std::move(line_buffer);
+  

[Lldb-commits] [lldb] r374424 - update TestRunCommandInterpreterAPI to use SBFile

2019-10-10 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Thu Oct 10 12:24:07 2019
New Revision: 374424

URL: http://llvm.org/viewvc/llvm-project?rev=374424=rev
Log:
update TestRunCommandInterpreterAPI to use SBFile

Summary:
If you look at what this test is doing, it's actually quite
mysterious why it works at all.   It sets the input file
inside a "with open".   As soon as the with block ends,
that file will be closed.   And yet somehow LLDB reads
commands from it anyway.What's actually happening is that
the file descriptor gets dup'd when something inside LLDB
calls File::GetStream().   I think it's fair to say that
what this test is doing is illegal and it has no right
to expect it to work.

This patch updates the test with two cases.  One uses
the SBFile api, and actually transfers ownership of
the original file descriptor to the debugger.   The other
just uses the old FILE* API, but in a sane way.

I also set NO_DEBUG_INFO_TESTCASE, because this test doesn't
use any debug info and doesn't need to run three times.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: aprantl, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68738

Modified:

lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py?rev=374424=374423=374424=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/interpreter/TestRunCommandInterpreterAPI.py
 Thu Oct 10 12:24:07 2019
@@ -5,8 +5,46 @@ import lldb
 from lldbsuite.test.decorators import *
 from lldbsuite.test.lldbtest import *
 
+class CommandRunInterpreterLegacyAPICase(TestBase):
+
+NO_DEBUG_INFO_TESTCASE = True
+mydir = TestBase.compute_mydir(__file__)
+
+def setUp(self):
+TestBase.setUp(self)
+
+self.stdin_path = self.getBuildArtifact("stdin.txt")
+
+with open(self.stdin_path, 'w') as input_handle:
+input_handle.write("nonexistingcommand\nquit")
+
+# Python will close the file descriptor if all references
+# to the filehandle object lapse, so we need to keep one
+# around.
+self.filehandle = open(self.stdin_path, 'r')
+self.dbg.SetInputFileHandle(self.filehandle, False)
+
+# No need to track the output
+self.devnull = open(os.devnull, 'w')
+self.dbg.SetOutputFileHandle(self.devnull, False)
+self.dbg.SetErrorFileHandle (self.devnull, False)
+
+@add_test_categories(['pyapi'])
+def test_run_session_with_error_and_quit(self):
+"""Run non-existing and quit command returns appropriate values"""
+
+n_errors, quit_requested, has_crashed = self.dbg.RunCommandInterpreter(
+True, False, lldb.SBCommandInterpreterRunOptions(), 0, False,
+False)
+
+self.assertGreater(n_errors, 0)
+self.assertTrue(quit_requested)
+self.assertFalse(has_crashed)
+
+
 class CommandRunInterpreterAPICase(TestBase):
 
+NO_DEBUG_INFO_TESTCASE = True
 mydir = TestBase.compute_mydir(__file__)
 
 def setUp(self):
@@ -17,13 +55,12 @@ class CommandRunInterpreterAPICase(TestB
 with open(self.stdin_path, 'w') as input_handle:
 input_handle.write("nonexistingcommand\nquit")
 
-with open(self.stdin_path, 'r') as input_handle:
-self.dbg.SetInputFileHandle(input_handle, False)
+self.dbg.SetInputFile(open(self.stdin_path, 'r'))
 
 # No need to track the output
 devnull = open(os.devnull, 'w')
-self.dbg.SetOutputFileHandle(devnull, False)
-self.dbg.SetErrorFileHandle(devnull, False)
+self.dbg.SetOutputFile(devnull)
+self.dbg.SetErrorFile(devnull)
 
 @add_test_categories(['pyapi'])
 def test_run_session_with_error_and_quit(self):


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r374422 - update SBDebugger::SetInputFile() etc to work on native Files

2019-10-10 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Thu Oct 10 12:10:59 2019
New Revision: 374422

URL: http://llvm.org/viewvc/llvm-project?rev=374422=rev
Log:
update SBDebugger::SetInputFile() etc to work on native Files

Summary:
This patch adds FileSP versions of SetInputFile(),
SetOutputFile, and SetErrorFile().   SWIG will convert native
python file objects into FileSP.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: clayborg, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68734

Modified:
lldb/trunk/include/lldb/API/SBDebugger.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/interface/SBDebugger.i
lldb/trunk/source/API/SBDebugger.cpp

Modified: lldb/trunk/include/lldb/API/SBDebugger.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBDebugger.h?rev=374422=374421=374422=diff
==
--- lldb/trunk/include/lldb/API/SBDebugger.h (original)
+++ lldb/trunk/include/lldb/API/SBDebugger.h Thu Oct 10 12:10:59 2019
@@ -94,6 +94,12 @@ public:
 
   SBError SetErrorFile(SBFile file);
 
+  SBError SetInputFile(FileSP file);
+
+  SBError SetOutputFile(FileSP file);
+
+  SBError SetErrorFile(FileSP file);
+
   SBFile GetInputFile();
 
   SBFile GetOutputFile();

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374422=374421=374422=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Thu Oct 10 12:10:59 2019
@@ -549,7 +549,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 @add_test_categories(['pyapi'])
 @skipIf(py_version=['<', (3,)])
-@expectedFailureAll() # fixme multiple problems with this
 def test_string_out(self):
 f = io.StringIO()
 status = self.debugger.SetOutputFile(f)
@@ -559,7 +558,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() # FIXME need FileSP version of 
SBDebugger::SetErrorFile
 @skipIf(py_version=['<', (3,)])
 def test_string_error(self):
 f = io.StringIO()
@@ -630,7 +628,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() # FIXME need FileSP version of 
SBDebugger::SetErrorFile
 @skipIf(py_version=['<', (3,)])
 def test_file_out(self):
 with open(self.out_filename, 'w') as f:
@@ -654,7 +651,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() # FIXME need FileSP version of 
SBDebugger::SetErrorFile
 def test_file_error(self):
 with open(self.out_filename, 'w') as f:
 status = self.debugger.SetErrorFile(f)
@@ -746,7 +742,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@expectedFailureAll() # FIXME need FileSP version of 
SBDebugger::SetOutputFile
 def test_close(self):
 debugger = self.debugger
 with open(self.out_filename, 'w') as f:
@@ -767,7 +762,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 @add_test_categories(['pyapi'])
 @skipIf(py_version=['<', (3,)])
-@expectedFailureAll() # FIXME need FileSP version of 
SBDebugger::SetOutputFile
 def test_stdout(self):
 f = io.StringIO()
 status = self.debugger.SetOutputFile(f)

Modified: lldb/trunk/scripts/interface/SBDebugger.i
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBDebugger.i?rev=374422=374421=374422=diff
==
--- lldb/trunk/scripts/interface/SBDebugger.i (original)
+++ lldb/trunk/scripts/interface/SBDebugger.i Thu Oct 10 12:10:59 2019
@@ -165,21 +165,27 @@ public:
 void
 SkipLLDBInitFiles (bool b);
 
+%feature("autodoc", "DEPRECATED, use SetInputFile");
 void
 SetInputFileHandle (FILE *f, bool transfer_ownership);
 
+%feature("autodoc", "DEPRECATED, use SetOutputFile");
 void
 SetOutputFileHandle (FILE *f, bool transfer_ownership);
 
+%feature("autodoc", "DEPRECATED, use SetErrorFile");
 void
 SetErrorFileHandle (FILE *f, bool transfer_ownership);
 
+%feature("autodoc", "DEPRECATED, use GetInputFile");
 FILE *
 GetInputFileHandle ();
 
+%feature("autodoc", "DEPRECATED, use GetOutputFile");
 FILE *
 GetOutputFileHandle ();
 
+%feature("autodoc", "DEPRECATED, use GetErrorFile");
 FILE *
 GetErrorFileHandle ();
 
@@ -192,6 +198,15 @@ public:
 SBError
 SetErrorFile (SBFile file);
 
+SBError
+

[Lldb-commits] [lldb] r374417 - TestFileHandle.py: fix for Python 3.6

2019-10-10 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Thu Oct 10 11:38:23 2019
New Revision: 374417

URL: http://llvm.org/viewvc/llvm-project?rev=374417=rev
Log:
TestFileHandle.py: fix for Python 3.6

Summary:
Python 3.6 stringifies exceptions as `ExceptionClass("foo",)` instead
of `ExceptionClass("foo")`.   This patch makes the test assertions a
little more flexible so the test passes anyway.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68745

Modified:

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374417=374416=374417=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Thu Oct 10 11:38:23 2019
@@ -676,11 +676,11 @@ class FileHandleTestCase(lldbtest.TestBa
 error, n = lldb.SBFile(BadIO()).Write(b"FOO")
 self.assertEqual(n, 0)
 self.assertTrue(error.Fail())
-self.assertEqual(error.GetCString(), "OhNoe('OH NOE')")
+self.assertIn('OH NOE', error.GetCString())
 error, n = lldb.SBFile(BadIO()).Read(bytearray(100))
 self.assertEqual(n, 0)
 self.assertTrue(error.Fail())
-self.assertEqual(error.GetCString(), "OhNoe('OH NOE')")
+self.assertIn('OH NOE', error.GetCString())
 
 
 @add_test_categories(['pyapi'])


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r374237 - SBFile: add a bunch of tests that should eventually work.

2019-10-09 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  9 14:50:46 2019
New Revision: 374237

URL: http://llvm.org/viewvc/llvm-project?rev=374237=rev
Log:
SBFile: add a bunch of tests that should eventually work.

Summary:
It's really annoying and confusing to have to keep referring
back to earlier versions of this SBFile work to find the
tests that need to be added for each patch, and not duplicate
them with new tests.

This patch just imports all my tests.   A bunch of them don't
work yet, so they are marked to be skipped.   They'll be
unmarked as I fix them.

One of these tests will actually trip an assert in the SWIG
code now instead of just failing, so I'm fixing that here too.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: JDevlieghere, labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68433

Modified:

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/Python/python-typemaps.swig

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374237=374236=374237=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Wed Oct  9 14:50:46 2019
@@ -13,7 +13,7 @@ from contextlib import contextmanager
 import lldb
 from lldbsuite.test import  lldbtest
 from lldbsuite.test.decorators import (
-add_test_categories, skipIf, skipIfWindows)
+add_test_categories, skipIf, skipIfWindows, expectedFailure)
 
 class OhNoe(Exception):
 pass
@@ -162,7 +162,9 @@ class FileHandleTestCase(lldbtest.TestBa
 with open(self.out_filename, 'w') as f:
 self.debugger.SetErrorFileHandle(f, False)
 self.handleCmd('lolwut', check=False, collect_result=False)
-self.debugger.GetErrorFileHandle().write('FOOBAR\n')
+f2 = self.debugger.GetErrorFileHandle()
+f2.write('FOOBAR\n')
+f2.flush()
 lldb.SBDebugger.Destroy(self.debugger)
 with open(self.out_filename, 'r') as f:
 errors = f.read()
@@ -181,6 +183,16 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
+def test_legacy_file_error(self):
+debugger = self.debugger
+with open(self.out_filename, 'w') as f:
+debugger.SetErrorFileHandle(f, False)
+self.handleCmd('lolwut', check=False, collect_result=False)
+with open(self.out_filename, 'r') as f:
+errors = f.read()
+self.assertTrue(re.search(r'error:.*lolwut', errors))
+
+@add_test_categories(['pyapi'])
 def test_sbfile_type_errors(self):
 sbf = lldb.SBFile()
 self.assertRaises(TypeError, sbf.Write, None)
@@ -270,6 +282,17 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
+def test_help(self):
+debugger = self.debugger
+with open(self.out_filename, 'w') as f:
+status = debugger.SetOutputFile(lldb.SBFile(f))
+self.assertTrue(status.Success())
+self.handleCmd("help help", check=False, collect_result=False)
+with open(self.out_filename, 'r') as f:
+self.assertIn('Show a list of all debugger commands', f.read())
+
+
+@add_test_categories(['pyapi'])
 def test_immediate(self):
 with open(self.out_filename, 'w') as f:
 ret = lldb.SBCommandReturnObject()
@@ -278,9 +301,7 @@ class FileHandleTestCase(lldbtest.TestBa
 interpreter.HandleCommand("help help", ret)
 # make sure the file wasn't closed early.
 f.write("\nQUUX\n")
-
 ret = None # call destructor and flush streams
-
 with open(self.out_filename, 'r') as f:
 output = f.read()
 self.assertTrue(re.search(r'Show a list of all debugger commands', 
output))
@@ -288,6 +309,37 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
+@expectedFailure # FIXME need SBFile interfaces on SBCommandReturnObject
+def test_immediate_string(self):
+f = io.StringIO()
+ret = lldb.SBCommandReturnObject()
+ret.SetImmediateOutputFile(f)
+interpreter = self.debugger.GetCommandInterpreter()
+interpreter.HandleCommand("help help", ret)
+# make sure the file wasn't closed early.
+f.write("\nQUUX\n")
+ret = None # call destructor and flush streams
+output = f.getvalue()
+self.assertTrue(re.search(r'Show a list of all debugger commands', 
output))
+self.assertTrue(re.search(r'QUUX', output))
+
+
+@add_test_categories(['pyapi'])
+@expectedFailure # FIXME 

[Lldb-commits] [lldb] r374239 - remove a smattering of isolated, unnecessary uses of FILE*

2019-10-09 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  9 14:50:52 2019
New Revision: 374239

URL: http://llvm.org/viewvc/llvm-project?rev=374239=rev
Log:
remove a smattering of isolated, unnecessary uses of FILE*

Summary:
There a a few call sites that use FILE* which are easy to
fix without disrupting anything else.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: JDevlieghere, labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68444

Modified:
lldb/trunk/source/API/SBDebugger.cpp
lldb/trunk/source/Core/IOHandler.cpp
lldb/trunk/source/Expression/REPL.cpp

Modified: lldb/trunk/source/API/SBDebugger.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBDebugger.cpp?rev=374239=374238=374239=diff
==
--- lldb/trunk/source/API/SBDebugger.cpp (original)
+++ lldb/trunk/source/API/SBDebugger.cpp Wed Oct  9 14:50:52 2019
@@ -467,10 +467,8 @@ void SBDebugger::HandleCommand(const cha
 
 sb_interpreter.HandleCommand(command, result, false);
 
-if (GetErrorFileHandle() != nullptr)
-  result.PutError(GetErrorFile());
-if (GetOutputFileHandle() != nullptr)
-  result.PutOutput(GetOutputFile());
+result.PutError(m_opaque_sp->GetErrorStream().GetFileSP());
+result.PutOutput(m_opaque_sp->GetOutputStream().GetFileSP());
 
 if (!m_opaque_sp->GetAsyncExecution()) {
   SBProcess process(GetCommandInterpreter().GetProcess());

Modified: lldb/trunk/source/Core/IOHandler.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/IOHandler.cpp?rev=374239=374238=374239=diff
==
--- lldb/trunk/source/Core/IOHandler.cpp (original)
+++ lldb/trunk/source/Core/IOHandler.cpp Wed Oct  9 14:50:52 2019
@@ -329,10 +329,9 @@ bool IOHandlerEditline::GetLine(std::str
   prompt = GetPrompt();
 
 if (prompt && prompt[0]) {
-  FILE *out = GetOutputFILE();
-  if (out) {
-::fprintf(out, "%s", prompt);
-::fflush(out);
+  if (m_output_sp) {
+m_output_sp->Printf("%s", prompt);
+m_output_sp->Flush();
   }
 }
   }
@@ -491,10 +490,11 @@ bool IOHandlerEditline::GetLines(StringL
   // Show line numbers if we are asked to
   std::string line;
   if (m_base_line_number > 0 && GetIsInteractive()) {
-FILE *out = GetOutputFILE();
-if (out)
-  ::fprintf(out, "%u%s", m_base_line_number + 
(uint32_t)lines.GetSize(),
-GetPrompt() == nullptr ? " " : "");
+if (m_output_sp) {
+  m_output_sp->Printf("%u%s",
+  m_base_line_number + (uint32_t)lines.GetSize(),
+  GetPrompt() == nullptr ? " " : "");
+}
   }
 
   m_curr_line_idx = lines.GetSize();

Modified: lldb/trunk/source/Expression/REPL.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/REPL.cpp?rev=374239=374238=374239=diff
==
--- lldb/trunk/source/Expression/REPL.cpp (original)
+++ lldb/trunk/source/Expression/REPL.cpp Wed Oct  9 14:50:52 2019
@@ -423,7 +423,7 @@ void REPL::IOHandlerInputComplete(IOHand
   .SetBaseLineNumber(m_code.GetSize() + 1);
 }
 if (extra_line) {
-  fprintf(output_sp->GetFile().GetStream(), "\n");
+  output_sp->Printf("\n");
 }
   }
 }


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r374238 - SBFile support in SBCommandReturnObject

2019-10-09 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  9 14:50:49 2019
New Revision: 374238

URL: http://llvm.org/viewvc/llvm-project?rev=374238=rev
Log:
SBFile support in SBCommandReturnObject

Summary:
This patch add SBFile interfaces to SBCommandReturnObject, and
removes the internal callers of its FILE* interfaces.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: JDevlieghere

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68434

Modified:
lldb/trunk/include/lldb/API/SBCommandReturnObject.h
lldb/trunk/include/lldb/API/SBFile.h
lldb/trunk/include/lldb/Interpreter/CommandReturnObject.h
lldb/trunk/include/lldb/Utility/ReproducerInstrumentation.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/interface/SBCommandReturnObject.i
lldb/trunk/source/API/SBCommandReturnObject.cpp
lldb/trunk/source/API/SBDebugger.cpp
lldb/trunk/source/API/SBFile.cpp

lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/trunk/tools/driver/Driver.cpp

Modified: lldb/trunk/include/lldb/API/SBCommandReturnObject.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBCommandReturnObject.h?rev=374238=374237=374238=diff
==
--- lldb/trunk/include/lldb/API/SBCommandReturnObject.h (original)
+++ lldb/trunk/include/lldb/API/SBCommandReturnObject.h Wed Oct  9 14:50:49 2019
@@ -44,13 +44,21 @@ public:
 
   const char *GetError();
 
-  size_t PutOutput(FILE *fh);
+  size_t PutOutput(FILE *fh); // DEPRECATED
+
+  size_t PutOutput(SBFile file);
+
+  size_t PutOutput(FileSP file);
 
   size_t GetOutputSize();
 
   size_t GetErrorSize();
 
-  size_t PutError(FILE *fh);
+  size_t PutError(FILE *fh); // DEPRECATED
+
+  size_t PutError(SBFile file);
+
+  size_t PutError(FileSP file);
 
   void Clear();
 
@@ -68,14 +76,21 @@ public:
 
   bool GetDescription(lldb::SBStream );
 
-  // deprecated, these two functions do not take ownership of file handle
-  void SetImmediateOutputFile(FILE *fh);
+  void SetImmediateOutputFile(FILE *fh); // DEPRECATED
+
+  void SetImmediateErrorFile(FILE *fh); // DEPRECATED
+
+  void SetImmediateOutputFile(FILE *fh, bool transfer_ownership); // DEPRECATED
+
+  void SetImmediateErrorFile(FILE *fh, bool transfer_ownership); // DEPRECATED
+
+  void SetImmediateOutputFile(SBFile file);
 
-  void SetImmediateErrorFile(FILE *fh);
+  void SetImmediateErrorFile(SBFile file);
 
-  void SetImmediateOutputFile(FILE *fh, bool transfer_ownership);
+  void SetImmediateOutputFile(FileSP file);
 
-  void SetImmediateErrorFile(FILE *fh, bool transfer_ownership);
+  void SetImmediateErrorFile(FileSP file);
 
   void PutCString(const char *string, int len = -1);
 

Modified: lldb/trunk/include/lldb/API/SBFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFile.h?rev=374238=374237=374238=diff
==
--- lldb/trunk/include/lldb/API/SBFile.h (original)
+++ lldb/trunk/include/lldb/API/SBFile.h Wed Oct  9 14:50:49 2019
@@ -15,6 +15,7 @@ namespace lldb {
 
 class LLDB_API SBFile {
   friend class SBDebugger;
+  friend class SBCommandReturnObject;
 
 public:
   SBFile();

Modified: lldb/trunk/include/lldb/Interpreter/CommandReturnObject.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandReturnObject.h?rev=374238=374237=374238=diff
==
--- lldb/trunk/include/lldb/Interpreter/CommandReturnObject.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandReturnObject.h Wed Oct  9 
14:50:49 2019
@@ -62,13 +62,13 @@ public:
 return m_err_stream;
   }
 
-  void SetImmediateOutputFile(FILE *fh, bool transfer_fh_ownership = false) {
-lldb::StreamSP stream_sp(new StreamFile(fh, transfer_fh_ownership));
+  void SetImmediateOutputFile(lldb::FileSP file_sp) {
+lldb::StreamSP stream_sp(new StreamFile(file_sp));
 m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
   }
 
-  void SetImmediateErrorFile(FILE *fh, bool transfer_fh_ownership = false) {
-lldb::StreamSP stream_sp(new StreamFile(fh, transfer_fh_ownership));
+  void SetImmediateErrorFile(lldb::FileSP file_sp) {
+lldb::StreamSP stream_sp(new StreamFile(file_sp));
 m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
   }
 

Modified: lldb/trunk/include/lldb/Utility/ReproducerInstrumentation.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/ReproducerInstrumentation.h?rev=374238=374237=374238=diff
==
--- lldb/trunk/include/lldb/Utility/ReproducerInstrumentation.h (original)
+++ lldb/trunk/include/lldb/Utility/ReproducerInstrumentation.h Wed Oct  9 
14:50:49 2019
@@ -238,9 +238,12 @@ struct 

[Lldb-commits] [lldb] r374225 - allow arbitrary python streams to be converted to SBFile

2019-10-09 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  9 13:56:17 2019
New Revision: 374225

URL: http://llvm.org/viewvc/llvm-project?rev=374225=rev
Log:
allow arbitrary python streams to be converted to SBFile

Summary:
This patch adds SWIG typemaps that can convert arbitrary python
file objects into lldb_private::File.

A SBFile may be initialized from a python file using the
constructor.   There are also alternate, tagged constructors
that allow python files to be borrowed, and for the caller
to control whether or not the python I/O methods will be
called even when a file descriptor is available.I

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: zturner, amccarth, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68188

Modified:
lldb/trunk/include/lldb/API/SBFile.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/Python/python-typemaps.swig
lldb/trunk/scripts/interface/SBFile.i
lldb/trunk/source/API/SBFile.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp

Modified: lldb/trunk/include/lldb/API/SBFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFile.h?rev=374225=374224=374225=diff
==
--- lldb/trunk/include/lldb/API/SBFile.h (original)
+++ lldb/trunk/include/lldb/API/SBFile.h Wed Oct  9 13:56:17 2019
@@ -18,6 +18,7 @@ class LLDB_API SBFile {
 
 public:
   SBFile();
+  SBFile(FileSP file_sp);
   SBFile(FILE *file, bool transfer_ownership);
   SBFile(int fd, const char *mode, bool transfer_ownership);
   ~SBFile();
@@ -33,7 +34,6 @@ public:
 
 private:
   FileSP m_opaque_sp;
-  SBFile(FileSP file_sp);
 };
 
 } // namespace lldb

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=374225=374224=374225=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Wed Oct  9 13:56:17 2019
@@ -13,8 +13,53 @@ from contextlib import contextmanager
 import lldb
 from lldbsuite.test import  lldbtest
 from lldbsuite.test.decorators import (
-add_test_categories, no_debug_info_test, skipIf)
+add_test_categories, skipIf, skipIfWindows)
 
+class OhNoe(Exception):
+pass
+
+class BadIO(io.TextIOBase):
+@property
+def closed(self):
+return False
+def writable(self):
+return True
+def readable(self):
+return True
+def write(self, s):
+raise OhNoe('OH NOE')
+def read(self, n):
+raise OhNoe("OH NOE")
+def flush(self):
+raise OhNoe('OH NOE')
+
+# This class will raise an exception while it's being
+# converted into a C++ object by swig
+class ReallyBadIO(io.TextIOBase):
+def fileno(self):
+return 999
+def writable(self):
+raise OhNoe("OH NOE!!!")
+
+class MutableBool():
+def __init__(self, value):
+self.value = value
+def set(self, value):
+self.value = bool(value)
+def __bool__(self):
+return self.value
+
+class FlushTestIO(io.StringIO):
+def __init__(self, mutable_flushed, mutable_closed):
+super(FlushTestIO, self).__init__()
+self.mut_flushed = mutable_flushed
+self.mut_closed = mutable_closed
+def close(self):
+self.mut_closed.set(True)
+return super(FlushTestIO, self).close()
+def flush(self):
+self.mut_flushed.set(True)
+return super(FlushTestIO, self).flush()
 
 @contextmanager
 def replace_stdout(new):
@@ -36,6 +81,7 @@ def readStrippedLines(f):
 
 class FileHandleTestCase(lldbtest.TestBase):
 
+NO_DEBUG_INFO_TESTCASE = True
 mydir = lldbtest.Base.compute_mydir(__file__)
 
 # The way this class interacts with the debugger is different
@@ -84,7 +130,8 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@no_debug_info_test
+@skipIfWindows # FIXME pre-existing bug, should be fixed
+   # when we delete the FILE* typemaps.
 def test_legacy_file_out_script(self):
 with open(self.out_filename, 'w') as f:
 self.debugger.SetOutputFileHandle(f, False)
@@ -100,7 +147,6 @@ class FileHandleTestCase(lldbtest.TestBa
 
 
 @add_test_categories(['pyapi'])
-@no_debug_info_test
 def test_legacy_file_out(self):
 with open(self.out_filename, 'w') as f:
 self.debugger.SetOutputFileHandle(f, False)
@@ -110,7 +156,8 @@ class FileHandleTestCase(lldbtest.TestBa
 self.assertIn('deadbeef', f.read())
 
 @add_test_categories(['pyapi'])
-@no_debug_info_test
+

[Lldb-commits] [lldb] r374197 - protect libedit and LLDB gui from receiving null FILE* streams

2019-10-09 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  9 11:43:03 2019
New Revision: 374197

URL: http://llvm.org/viewvc/llvm-project?rev=374197=rev
Log:
protect libedit and LLDB gui from receiving null FILE* streams

Summary:
We now have valid files that will return NULL from GetStream().
libedit and the LLDB gui are the only places left that need FILE*
streams.  Both are doing curses-like user interaction that only
make sense with a real terminal anyway, so there is no need to convert
them off of their use of FILE*.   But we should check for null streams
before enabling these features.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: JDevlieghere, labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68677

Modified:
lldb/trunk/source/Commands/CommandObjectGUI.cpp
lldb/trunk/source/Core/IOHandler.cpp

Modified: lldb/trunk/source/Commands/CommandObjectGUI.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectGUI.cpp?rev=374197=374196=374197=diff
==
--- lldb/trunk/source/Commands/CommandObjectGUI.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectGUI.cpp Wed Oct  9 11:43:03 2019
@@ -29,7 +29,9 @@ bool CommandObjectGUI::DoExecute(Args 
 Debugger  = GetDebugger();
 
 File  = debugger.GetInputFile();
-if (input.GetIsRealTerminal() && input.GetIsInteractive()) {
+File  = debugger.GetOutputFile();
+if (input.GetStream() && output.GetStream() && input.GetIsRealTerminal() &&
+input.GetIsInteractive()) {
   IOHandlerSP io_handler_sp(new IOHandlerCursesGUI(debugger));
   if (io_handler_sp)
 debugger.PushIOHandler(io_handler_sp);

Modified: lldb/trunk/source/Core/IOHandler.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/IOHandler.cpp?rev=374197=374196=374197=diff
==
--- lldb/trunk/source/Core/IOHandler.cpp (original)
+++ lldb/trunk/source/Core/IOHandler.cpp Wed Oct  9 11:43:03 2019
@@ -266,7 +266,8 @@ IOHandlerEditline::IOHandlerEditline(
 #ifndef LLDB_DISABLE_LIBEDIT
   bool use_editline = false;
 
-  use_editline = m_input_sp && m_input_sp->GetIsRealTerminal();
+  use_editline = GetInputFILE() && GetOutputFILE() && GetErrorFILE() &&
+ m_input_sp && m_input_sp->GetIsRealTerminal();
 
   if (use_editline) {
 m_editline_up.reset(new Editline(editline_name, GetInputFILE(),


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r374094 - exception handling in PythonDataObjects.

2019-10-08 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Tue Oct  8 10:56:18 2019
New Revision: 374094

URL: http://llvm.org/viewvc/llvm-project?rev=374094=rev
Log:
exception handling in PythonDataObjects.

Summary:
Python APIs nearly all can return an exception.   They do this
by returning NULL, or -1, or some such value and setting
the exception state with PyErr_Set*().   Exceptions must be
handled before further python API functions are called.   Failure
to do so will result in asserts on debug builds of python.
It will also sometimes, but not usually result in crashes of
release builds.

Nearly everything in PythonDataObjects.h needs to be updated
to account for this.   This patch doesn't fix everything,
but it does introduce some new methods using Expected<>
return types that are safe to use.

split off from https://reviews.llvm.org/D68188

Reviewers: JDevlieghere, jasonmolenda, labath, zturner

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68547

Modified:
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Modified: 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=374094=374093=374094=diff
==
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
(original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
Tue Oct  8 10:56:18 2019
@@ -18,6 +18,7 @@
 #include "lldb/Host/File.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Interpreter/ScriptInterpreter.h"
+#include "lldb/Utility/Log.h"
 #include "lldb/Utility/Stream.h"
 
 #include "llvm/ADT/StringSwitch.h"
@@ -28,6 +29,22 @@
 
 using namespace lldb_private;
 using namespace lldb;
+using namespace lldb_private::python;
+using llvm::Error;
+using llvm::Expected;
+
+template <> Expected python::As(Expected &) {
+  if (!obj)
+return obj.takeError();
+  return obj.get().IsTrue();
+}
+
+template <>
+Expected python::As(Expected &) {
+  if (!obj)
+return obj.takeError();
+  return obj.get().AsLongLong();
+}
 
 void StructuredPythonObject::Serialize(llvm::json::OStream ) const {
   s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str());
@@ -167,12 +184,6 @@ PythonObject PythonObject::GetAttributeV
   PyObject_GetAttr(m_py_obj, py_attr.get()));
 }
 
-bool PythonObject::IsNone() const { return m_py_obj == Py_None; }
-
-bool PythonObject::IsValid() const { return m_py_obj != nullptr; }
-
-bool PythonObject::IsAllocated() const { return IsValid() && !IsNone(); }
-
 StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
   switch (GetObjectType()) {
   case PyObjectType::Dictionary:
@@ -334,6 +345,17 @@ StructuredData::StringSP PythonByteArray
 
 // PythonString
 
+Expected PythonString::FromUTF8(llvm::StringRef string) {
+#if PY_MAJOR_VERSION >= 3
+  PyObject *str = PyUnicode_FromStringAndSize(string.data(), string.size());
+#else
+  PyObject *str = PyString_FromStringAndSize(string.data(), string.size());
+#endif
+  if (!str)
+return llvm::make_error();
+  return Take(str);
+}
+
 PythonString::PythonString(PyRefType type, PyObject *py_obj) : PythonObject() {
   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
 }
@@ -342,10 +364,6 @@ PythonString::PythonString(llvm::StringR
   SetString(string);
 }
 
-PythonString::PythonString(const char *string) : PythonObject() {
-  SetString(llvm::StringRef(string));
-}
-
 PythonString::PythonString() : PythonObject() {}
 
 PythonString::~PythonString() {}
@@ -376,8 +394,12 @@ void PythonString::Reset(PyRefType type,
   // In Python 2, Don't store PyUnicode objects directly, because we need
   // access to their underlying character buffers which Python 2 doesn't
   // provide.
-  if (PyUnicode_Check(py_obj))
-result.Reset(PyRefType::Owned, PyUnicode_AsUTF8String(result.get()));
+  if (PyUnicode_Check(py_obj)) {
+PyObject *s = PyUnicode_AsUTF8String(result.get());
+if (s == NULL)
+  PyErr_Clear();
+result.Reset(PyRefType::Owned, s);
+  }
 #endif
   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
   // overflow since it calls back into the virtual implementation.
@@ -385,8 +407,17 @@ void PythonString::Reset(PyRefType type,
 }
 
 llvm::StringRef PythonString::GetString() const {
+  auto s = AsUTF8();
+  if (!s) {
+llvm::consumeError(s.takeError());
+return llvm::StringRef("");
+  }
+  return s.get();
+}
+
+Expected PythonString::AsUTF8() const {
   if (!IsValid())
-return llvm::StringRef();
+return nullDeref();
 
   Py_ssize_t size;
   const char *data;
@@ -394,10 +425,16 @@ llvm::StringRef PythonString::GetString(
 #if PY_MAJOR_VERSION >= 3
   data = PyUnicode_AsUTF8AndSize(m_py_obj, 

[Lldb-commits] [lldb] r374007 - test fix: TestLoadUsingPaths should use realpath

2019-10-07 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Oct  7 18:16:29 2019
New Revision: 374007

URL: http://llvm.org/viewvc/llvm-project?rev=374007=rev
Log:
test fix: TestLoadUsingPaths should use realpath

Summary:
TestLoadUsingPaths will fail if the build directory has
symlinks in its path, because the real paths reported by
the debugger won't match the symlink-laden paths it's expecting.

This can be solved just by using os.path.realpath on the base
path for the test.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: JDevlieghere

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68618

Modified:

lldb/trunk/packages/Python/lldbsuite/test/functionalities/load_using_paths/TestLoadUsingPaths.py

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/functionalities/load_using_paths/TestLoadUsingPaths.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/load_using_paths/TestLoadUsingPaths.py?rev=374007=374006=374007=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/functionalities/load_using_paths/TestLoadUsingPaths.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/functionalities/load_using_paths/TestLoadUsingPaths.py
 Mon Oct  7 18:16:29 2019
@@ -33,7 +33,7 @@ class LoadUsingPathsTestCase(TestBase):
 ext = 'dylib'
 self.lib_name = 'libloadunload.' + ext
 
-self.wd = self.getBuildDir()
+self.wd = os.path.realpath(self.getBuildDir())
 self.hidden_dir = os.path.join(self.wd, 'hidden')
 self.hidden_lib = os.path.join(self.hidden_dir, self.lib_name)
 


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r373997 - DWIMy filterspecs for dotest.py

2019-10-07 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Oct  7 17:26:53 2019
New Revision: 373997

URL: http://llvm.org/viewvc/llvm-project?rev=373997=rev
Log:
DWIMy filterspecs for dotest.py

Summary:
dotest.py currently requires a filterspec to be of the
form `TestCase.test_method`.   This patch makes it more
flexible, so you can pass `TestModule.TestCase.test_method`
or `TestModule.TestCase` or `TestCase.test_method` or just
`test_method`.

This makes it more convenient to just copy a test name
out of the terminal after running a bunch of tests and use
it as a filterspec.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: JDevlieghere

Subscribers: jingham, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68545

Modified:
lldb/trunk/packages/Python/lldbsuite/test/dotest.py
lldb/trunk/packages/Python/lldbsuite/test/dotest_args.py

Modified: lldb/trunk/packages/Python/lldbsuite/test/dotest.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/dotest.py?rev=373997=373996=373997=diff
==
--- lldb/trunk/packages/Python/lldbsuite/test/dotest.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/dotest.py Mon Oct  7 17:26:53 2019
@@ -667,34 +667,42 @@ def visit_file(dir, name):
 
 # Thoroughly check the filterspec against the base module and admit
 # the (base, filterspec) combination only when it makes sense.
-filterspec = None
-for filterspec in configuration.filters:
-# Optimistically set the flag to True.
-filtered = True
-module = __import__(base)
-parts = filterspec.split('.')
-obj = module
+
+def check(obj, parts):
 for part in parts:
 try:
 parent, obj = obj, getattr(obj, part)
 except AttributeError:
 # The filterspec has failed.
-filtered = False
-break
+return False
+return True
+
+module = __import__(base)
 
-# If filtered, we have a good filterspec.  Add it.
-if filtered:
-# print("adding filter spec %s to module %s" % (filterspec, 
module))
-configuration.suite.addTests(
-unittest2.defaultTestLoader.loadTestsFromName(
-filterspec, module))
-continue
+def iter_filters():
+for filterspec in configuration.filters:
+parts = filterspec.split('.')
+if check(module, parts):
+yield filterspec
+elif parts[0] == base and len(parts) > 1 and check(module, 
parts[1:]):
+yield '.'.join(parts[1:])
+else:
+for key,value in module.__dict__.items():
+if check(value, parts):
+yield key + '.' + filterspec
+
+filtered = False
+for filterspec in iter_filters():
+filtered = True
+print("adding filter spec %s to module %s" % (filterspec, 
repr(module)))
+tests = unittest2.defaultTestLoader.loadTestsFromName(filterspec, 
module)
+configuration.suite.addTests(tests)
 
 # Forgo this module if the (base, filterspec) combo is invalid
 if configuration.filters and not filtered:
 return
 
-if not filterspec or not filtered:
+if not filtered:
 # Add the entire file's worth of tests since we're not filtered.
 # Also the fail-over case when the filterspec branch
 # (base, filterspec) combo doesn't make sense.

Modified: lldb/trunk/packages/Python/lldbsuite/test/dotest_args.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/dotest_args.py?rev=373997=373996=373997=diff
==
--- lldb/trunk/packages/Python/lldbsuite/test/dotest_args.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/dotest_args.py Mon Oct  7 
17:26:53 2019
@@ -61,7 +61,9 @@ def create_parser():
 '-f',
 metavar='filterspec',
 action='append',
-help='Specify a filter, which consists of the test class name, a dot, 
followed by the test method, to only admit such test into the test suite')  # 
FIXME: Example?
+help=('Specify a filter, which looks like 
"TestModule.TestClass.test_name".  '+
+'You may also use shortened filters, such as '+
+'"TestModule.TestClass", "TestClass.test_name", or just 
"test_name".'))
 group.add_argument(
 '-p',
 metavar='pattern',


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r373564 - factor out an abstract base class for File

2019-10-02 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  2 21:31:46 2019
New Revision: 373564

URL: http://llvm.org/viewvc/llvm-project?rev=373564=rev
Log:
factor out an abstract base class for File

Summary:
This patch factors out File as an abstract base
class and moves most of its actual functionality into
a subclass called NativeFile.   In the next patch,
I'm going to be adding subclasses of File that
don't necessarily have any connection to actual OS files,
so they will not inherit from NativeFile.

This patch was split out as a prerequisite for
https://reviews.llvm.org/D68188

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68317

Modified:
lldb/trunk/include/lldb/Host/File.h
lldb/trunk/scripts/Python/python-typemaps.swig
lldb/trunk/source/API/SBDebugger.cpp
lldb/trunk/source/API/SBFile.cpp
lldb/trunk/source/Core/Debugger.cpp
lldb/trunk/source/Core/StreamFile.cpp
lldb/trunk/source/Host/common/File.cpp
lldb/trunk/source/Host/common/FileSystem.cpp
lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

lldb/trunk/source/Plugins/Platform/MacOSX/objcxx/PlatformiOSSimulatorCoreSimulatorSupport.mm
lldb/trunk/source/Plugins/Process/Darwin/NativeProcessDarwin.cpp

lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp

lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
lldb/trunk/source/Target/Process.cpp
lldb/trunk/unittests/Host/FileTest.cpp

Modified: lldb/trunk/include/lldb/Host/File.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=373564=373563=373564=diff
==
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Wed Oct  2 21:31:46 2019
@@ -22,10 +22,12 @@
 namespace lldb_private {
 
 /// \class File File.h "lldb/Host/File.h"
-/// A file class.
+/// An abstract base class for files.
 ///
-/// A file class that divides abstracts the LLDB core from host file
-/// functionality.
+/// Files will often be NativeFiles, which provides a wrapper
+/// around host OS file functionality.   But it
+/// is also possible to subclass file to provide objects that have file
+/// or stream functionality but are not backed by any host OS file.
 class File : public IOObject {
 public:
   static int kInvalidDescriptor;
@@ -49,77 +51,80 @@ public:
   };
 
   static mode_t ConvertOpenOptionsForPOSIXOpen(uint32_t open_options);
+  static uint32_t GetOptionsFromMode(llvm::StringRef mode);
+  static bool DescriptorIsValid(int descriptor) { return descriptor >= 0; };
 
   File()
-  : IOObject(eFDTypeFile), m_descriptor(kInvalidDescriptor),
-m_own_descriptor(false), m_stream(kInvalidStream), m_options(0),
-m_own_stream(false), m_is_interactive(eLazyBoolCalculate),
-m_is_real_terminal(eLazyBoolCalculate),
-m_supports_colors(eLazyBoolCalculate) {}
-
-  File(FILE *fh, bool transfer_ownership)
-  : IOObject(eFDTypeFile), m_descriptor(kInvalidDescriptor),
-m_own_descriptor(false), m_stream(fh), m_options(0),
-m_own_stream(transfer_ownership), m_is_interactive(eLazyBoolCalculate),
+  : IOObject(eFDTypeFile), m_is_interactive(eLazyBoolCalculate),
 m_is_real_terminal(eLazyBoolCalculate),
-m_supports_colors(eLazyBoolCalculate) {}
+m_supports_colors(eLazyBoolCalculate){};
 
-  File(int fd, uint32_t options, bool transfer_ownership)
-  : IOObject(eFDTypeFile), m_descriptor(fd),
-m_own_descriptor(transfer_ownership), m_stream(kInvalidStream),
-m_options(options), m_own_stream(false),
-m_is_interactive(eLazyBoolCalculate),
-m_is_real_terminal(eLazyBoolCalculate) {}
-
-  /// Destructor.
+  /// Read bytes from a file from the current file position into buf.
   ///
-  /// The destructor is virtual in case this class is subclassed.
-  ~File() override;
-
-  bool IsValid() const override {
-return DescriptorIsValid() || StreamIsValid();
-  }
-
-  /// Convert to pointer operator.
+  /// NOTE: This function is NOT thread safe. Use the read function
+  /// that takes an "off_t " to ensure correct operation in multi-
+  /// threaded environments.
   ///
-  /// This allows code to check a File object to see if it contains anything
-  /// valid using code such as:
+  /// \param[out] buf
   ///
-  /// \code
-  /// File file(...);
-  /// if (file)
-  /// { ...
-  /// \endcode
+  /// \param[in,out] num_bytes.
+  ///Pass in the size of buf.  Read will pass out the number
+  ///of bytes read.   Zero bytes read with no error indicates
+  ///EOF.
   ///
   /// \return
-  /// A pointer to this object if either the 

[Lldb-commits] [lldb] r373563 - SBDebugger::SetInputFile, SetOutputFile, etc.

2019-10-02 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  2 21:04:48 2019
New Revision: 373563

URL: http://llvm.org/viewvc/llvm-project?rev=373563=rev
Log:
SBDebugger::SetInputFile, SetOutputFile, etc.

Summary:
Add new methods to SBDebugger to set IO files as SBFiles instead of
as FILE* streams.

In future commits, the FILE* methods will be deprecated and these
will become the primary way to set the debugger I/O streams.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68181

Modified:
lldb/trunk/include/lldb/API/SBDebugger.h
lldb/trunk/include/lldb/API/SBFile.h
lldb/trunk/include/lldb/Core/Debugger.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/interface/SBDebugger.i
lldb/trunk/source/API/SBDebugger.cpp
lldb/trunk/source/API/SBFile.cpp
lldb/trunk/source/Core/Debugger.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp

Modified: lldb/trunk/include/lldb/API/SBDebugger.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBDebugger.h?rev=373563=373562=373563=diff
==
--- lldb/trunk/include/lldb/API/SBDebugger.h (original)
+++ lldb/trunk/include/lldb/API/SBDebugger.h Wed Oct  2 21:04:48 2019
@@ -88,6 +88,18 @@ public:
 
   FILE *GetErrorFileHandle();
 
+  SBError SetInputFile(SBFile file);
+
+  SBError SetOutputFile(SBFile file);
+
+  SBError SetErrorFile(SBFile file);
+
+  SBFile GetInputFile();
+
+  SBFile GetOutputFile();
+
+  SBFile GetErrorFile();
+
   void SaveInputTerminalState();
 
   void RestoreInputTerminalState();

Modified: lldb/trunk/include/lldb/API/SBFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFile.h?rev=373563=373562=373563=diff
==
--- lldb/trunk/include/lldb/API/SBFile.h (original)
+++ lldb/trunk/include/lldb/API/SBFile.h Wed Oct  2 21:04:48 2019
@@ -14,6 +14,8 @@
 namespace lldb {
 
 class LLDB_API SBFile {
+  friend class SBDebugger;
+
 public:
   SBFile();
   SBFile(FILE *file, bool transfer_ownership);
@@ -31,6 +33,7 @@ public:
 
 private:
   FileSP m_opaque_sp;
+  SBFile(FileSP file_sp);
 };
 
 } // namespace lldb

Modified: lldb/trunk/include/lldb/Core/Debugger.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Debugger.h?rev=373563=373562=373563=diff
==
--- lldb/trunk/include/lldb/Core/Debugger.h (original)
+++ lldb/trunk/include/lldb/Core/Debugger.h Wed Oct  2 21:04:48 2019
@@ -132,12 +132,11 @@ public:
 
   repro::DataRecorder *GetInputRecorder();
 
-  void SetInputFileHandle(FILE *fh, bool tranfer_ownership,
-  repro::DataRecorder *recorder = nullptr);
+  void SetInputFile(lldb::FileSP file, repro::DataRecorder *recorder = 
nullptr);
 
-  void SetOutputFileHandle(FILE *fh, bool tranfer_ownership);
+  void SetOutputFile(lldb::FileSP file);
 
-  void SetErrorFileHandle(FILE *fh, bool tranfer_ownership);
+  void SetErrorFile(lldb::FileSP file);
 
   void SaveInputTerminalState();
 

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py?rev=373563=373562=373563=diff
==
--- 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 (original)
+++ 
lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
 Wed Oct  2 21:04:48 2019
@@ -12,9 +12,19 @@ from contextlib import contextmanager
 
 import lldb
 from lldbsuite.test import  lldbtest
-from lldbsuite.test.decorators import add_test_categories, no_debug_info_test
+from lldbsuite.test.decorators import (
+add_test_categories, no_debug_info_test, skipIf)
 
 
+@contextmanager
+def replace_stdout(new):
+old = sys.stdout
+sys.stdout = new
+try:
+yield
+finally:
+sys.stdout = old
+
 def readStrippedLines(f):
 def i():
 for line in f:
@@ -66,6 +76,8 @@ class FileHandleTestCase(lldbtest.TestBa
 interpreter.HandleCommand(cmd, ret)
 else:
 self.debugger.HandleCommand(cmd)
+self.debugger.GetOutputFile().Flush()
+self.debugger.GetErrorFile().Flush()
 if collect_result and check:
 self.assertTrue(ret.Succeeded())
 return ret.GetOutput()
@@ -97,6 +109,19 @@ class FileHandleTestCase(lldbtest.TestBa
 with open(self.out_filename, 'r') as f:
 self.assertIn('deadbeef', f.read())
 
+@add_test_categories(['pyapi'])
+@no_debug_info_test
+def test_legacy_file_err_with_get(self):
+

[Lldb-commits] [lldb] r373562 - new api class: SBFile

2019-10-02 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Wed Oct  2 21:01:07 2019
New Revision: 373562

URL: http://llvm.org/viewvc/llvm-project?rev=373562=rev
Log:
new api class: SBFile

Summary:
SBFile is a scripting API wrapper for lldb_private::File

This is the first step in a project to enable arbitrary python
io.IOBase file objects -- including those that override the read()
and write() methods -- to be used as the main debugger IOStreams.

Currently this is impossible because python file objects must first
be converted into FILE* streams by SWIG in order to be passed into
the debugger.

full prototype: https://github.com/smoofra/llvm-project/tree/files

Reviewers: JDevlieghere, jasonmolenda, zturner, jingham, labath

Reviewed By: labath

Subscribers: labath, mgorny, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D67793

Added:
lldb/trunk/include/lldb/API/SBFile.h
lldb/trunk/scripts/interface/SBFile.i
lldb/trunk/source/API/SBFile.cpp
Modified:
lldb/trunk/include/lldb/API/LLDB.h
lldb/trunk/include/lldb/API/SBDefines.h
lldb/trunk/include/lldb/API/SBError.h
lldb/trunk/include/lldb/Host/File.h

lldb/trunk/packages/Python/lldbsuite/test/python_api/default-constructor/TestDefaultConstructorForAPIObjects.py

lldb/trunk/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
lldb/trunk/scripts/Python/python-typemaps.swig
lldb/trunk/scripts/lldb.swig
lldb/trunk/source/API/CMakeLists.txt
lldb/trunk/source/API/SBReproducer.cpp
lldb/trunk/source/Host/common/File.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Modified: lldb/trunk/include/lldb/API/LLDB.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/LLDB.h?rev=373562=373561=373562=diff
==
--- lldb/trunk/include/lldb/API/LLDB.h (original)
+++ lldb/trunk/include/lldb/API/LLDB.h Wed Oct  2 21:01:07 2019
@@ -13,8 +13,8 @@
 #include "lldb/API/SBAttachInfo.h"
 #include "lldb/API/SBBlock.h"
 #include "lldb/API/SBBreakpoint.h"
-#include "lldb/API/SBBreakpointName.h"
 #include "lldb/API/SBBreakpointLocation.h"
+#include "lldb/API/SBBreakpointName.h"
 #include "lldb/API/SBBroadcaster.h"
 #include "lldb/API/SBCommandInterpreter.h"
 #include "lldb/API/SBCommandReturnObject.h"
@@ -28,6 +28,7 @@
 #include "lldb/API/SBEvent.h"
 #include "lldb/API/SBExecutionContext.h"
 #include "lldb/API/SBExpressionOptions.h"
+#include "lldb/API/SBFile.h"
 #include "lldb/API/SBFileSpec.h"
 #include "lldb/API/SBFileSpecList.h"
 #include "lldb/API/SBFrame.h"

Modified: lldb/trunk/include/lldb/API/SBDefines.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBDefines.h?rev=373562=373561=373562=diff
==
--- lldb/trunk/include/lldb/API/SBDefines.h (original)
+++ lldb/trunk/include/lldb/API/SBDefines.h Wed Oct  2 21:01:07 2019
@@ -41,6 +41,7 @@ class LLDB_API SBEvent;
 class LLDB_API SBEventList;
 class LLDB_API SBExecutionContext;
 class LLDB_API SBExpressionOptions;
+class LLDB_API SBFile;
 class LLDB_API SBFileSpec;
 class LLDB_API SBFileSpecList;
 class LLDB_API SBFrame;

Modified: lldb/trunk/include/lldb/API/SBError.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBError.h?rev=373562=373561=373562=diff
==
--- lldb/trunk/include/lldb/API/SBError.h (original)
+++ lldb/trunk/include/lldb/API/SBError.h Wed Oct  2 21:01:07 2019
@@ -70,6 +70,7 @@ protected:
   friend class SBTrace;
   friend class SBValue;
   friend class SBWatchpoint;
+  friend class SBFile;
 
   lldb_private::Status *get();
 

Added: lldb/trunk/include/lldb/API/SBFile.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFile.h?rev=373562=auto
==
--- lldb/trunk/include/lldb/API/SBFile.h (added)
+++ lldb/trunk/include/lldb/API/SBFile.h Wed Oct  2 21:01:07 2019
@@ -0,0 +1,38 @@
+//===-- SBFile.h *- 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
+//
+//===--===//
+
+#ifndef LLDB_SBFile_h_
+#define LLDB_SBFile_h_
+
+#include "lldb/API/SBDefines.h"
+
+namespace lldb {
+
+class LLDB_API SBFile {
+public:
+  SBFile();
+  SBFile(FILE *file, bool transfer_ownership);
+  SBFile(int fd, const char *mode, bool transfer_ownership);
+  ~SBFile();
+
+  SBError Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read);
+  SBError Write(const uint8_t *buf, size_t num_bytes, size_t *bytes_written);
+  

[Lldb-commits] [lldb] r373285 - File::Clear() -> File::TakeStreamAndClear()

2019-09-30 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Mon Sep 30 18:05:02 2019
New Revision: 373285

URL: http://llvm.org/viewvc/llvm-project?rev=373285=rev
Log:
File::Clear() -> File::TakeStreamAndClear()

Summary:
File::Clear() is an ugly function.  It's only used in one place,
which is the swig typemaps for FILE*.   This patch refactors and
renames that function to make it clear what it's really for and
why nobody else should use it.

Both File::TakeStreamAndClear() and the FILE* typemaps will be
removed in later patches after a suitable replacement is in place.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68160

Modified:
lldb/trunk/include/lldb/Host/File.h
lldb/trunk/scripts/Python/python-typemaps.swig
lldb/trunk/source/Host/common/File.cpp

Modified: lldb/trunk/include/lldb/Host/File.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=373285=373284=373285=diff
==
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Mon Sep 30 18:05:02 2019
@@ -120,7 +120,19 @@ public:
 
   Status Close() override;
 
-  void Clear();
+  /// DEPRECATED! Extract the underlying FILE* and reset this File without 
closing it.
+  ///
+  /// This is only here to support legacy SB interfaces that need to convert 
scripting
+  /// language objects into FILE* streams.   That conversion is inherently 
sketchy and
+  /// doing so may cause the stream to be leaked.
+  ///
+  /// After calling this the File will be reset to its original state.  It 
will be
+  /// invalid and it will not hold on to any resources.
+  ///
+  /// \return
+  /// The underlying FILE* stream from this File, if one exists and can be 
extracted,
+  /// nullptr otherwise.
+  FILE *TakeStreamAndClear();
 
   int GetDescriptor() const;
 

Modified: lldb/trunk/scripts/Python/python-typemaps.swig
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-typemaps.swig?rev=373285=373284=373285=diff
==
--- lldb/trunk/scripts/Python/python-typemaps.swig (original)
+++ lldb/trunk/scripts/Python/python-typemaps.swig Mon Sep 30 18:05:02 2019
@@ -372,6 +372,9 @@ bool SetNumberFromPyObject(doubl
   $1 = $1 || PyCallable_Check(reinterpret_cast($input));
 }
 
+// FIXME both of these paths wind up calling fdopen() with no provision for 
ever calling
+// fclose() on the result.  SB interfaces that use FILE* should be deprecated 
for scripting
+// use and this typemap should eventually be removed.
 %typemap(in) FILE * {
using namespace lldb_private;
if ($input == Py_None)
@@ -398,9 +401,7 @@ bool SetNumberFromPyObject(doubl
   lldb::FileUP file = py_file.GetUnderlyingFile();
   if (!file)
  return nullptr;
-  $1 = file->GetStream();
-  if ($1)
- file->Clear();
+  $1 = file->TakeStreamAndClear();
 }
 }
 

Modified: lldb/trunk/source/Host/common/File.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/File.cpp?rev=373285=373284=373285=diff
==
--- lldb/trunk/source/Host/common/File.cpp (original)
+++ lldb/trunk/source/Host/common/File.cpp Mon Sep 30 18:05:02 2019
@@ -162,13 +162,16 @@ Status File::Close() {
   return error;
 }
 
-void File::Clear() {
-  m_stream = nullptr;
+FILE *File::TakeStreamAndClear() {
+  FILE *stream = GetStream();
+  m_stream = NULL;
   m_descriptor = kInvalidDescriptor;
   m_options = 0;
   m_own_stream = false;
+  m_own_descriptor = false;
   m_is_interactive = m_supports_colors = m_is_real_terminal =
   eLazyBoolCalculate;
+  return stream;
 }
 
 Status File::GetFileSpec(FileSpec _spec) const {


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r373126 - refactor: move IOObject::m_should_close_fd into subclasses

2019-09-27 Thread Lawrence D'Anna via lldb-commits
Author: lawrence_danna
Date: Fri Sep 27 13:43:50 2019
New Revision: 373126

URL: http://llvm.org/viewvc/llvm-project?rev=373126=rev
Log:
refactor: move IOObject::m_should_close_fd into subclasses

Summary:
m_should_close_fd doesn't need to be in IOObject.   It will be useful
for my next change to move it down into File and Socket.

Reviewers: labath, JDevlieghere, jasonmolenda

Reviewed By: JDevlieghere

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68152

Modified:
lldb/trunk/include/lldb/Host/File.h
lldb/trunk/include/lldb/Host/Socket.h
lldb/trunk/include/lldb/Utility/IOObject.h
lldb/trunk/source/Host/common/File.cpp
lldb/trunk/source/Host/common/Socket.cpp

Modified: lldb/trunk/include/lldb/Host/File.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=373126=373125=373126=diff
==
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Fri Sep 27 13:43:50 2019
@@ -51,22 +51,23 @@ public:
   static mode_t ConvertOpenOptionsForPOSIXOpen(uint32_t open_options);
 
   File()
-  : IOObject(eFDTypeFile, false), m_descriptor(kInvalidDescriptor),
-m_stream(kInvalidStream), m_options(0), m_own_stream(false),
-m_is_interactive(eLazyBoolCalculate),
+  : IOObject(eFDTypeFile), m_descriptor(kInvalidDescriptor),
+m_own_descriptor(false), m_stream(kInvalidStream), m_options(0),
+m_own_stream(false), m_is_interactive(eLazyBoolCalculate),
 m_is_real_terminal(eLazyBoolCalculate),
 m_supports_colors(eLazyBoolCalculate) {}
 
   File(FILE *fh, bool transfer_ownership)
-  : IOObject(eFDTypeFile, false), m_descriptor(kInvalidDescriptor),
-m_stream(fh), m_options(0), m_own_stream(transfer_ownership),
-m_is_interactive(eLazyBoolCalculate),
+  : IOObject(eFDTypeFile), m_descriptor(kInvalidDescriptor),
+m_own_descriptor(false), m_stream(fh), m_options(0),
+m_own_stream(transfer_ownership), m_is_interactive(eLazyBoolCalculate),
 m_is_real_terminal(eLazyBoolCalculate),
 m_supports_colors(eLazyBoolCalculate) {}
 
   File(int fd, uint32_t options, bool transfer_ownership)
-  : IOObject(eFDTypeFile, transfer_ownership), m_descriptor(fd),
-m_stream(kInvalidStream), m_options(options), m_own_stream(false),
+  : IOObject(eFDTypeFile), m_descriptor(fd),
+m_own_descriptor(transfer_ownership), m_stream(kInvalidStream),
+m_options(options), m_own_stream(false),
 m_is_interactive(eLazyBoolCalculate),
 m_is_real_terminal(eLazyBoolCalculate) {}
 
@@ -339,6 +340,7 @@ protected:
 
   // Member variables
   int m_descriptor;
+  bool m_own_descriptor;
   FILE *m_stream;
   uint32_t m_options;
   bool m_own_stream;

Modified: lldb/trunk/include/lldb/Host/Socket.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Socket.h?rev=373126=373125=373126=diff
==
--- lldb/trunk/include/lldb/Host/Socket.h (original)
+++ lldb/trunk/include/lldb/Host/Socket.h Fri Sep 27 13:43:50 2019
@@ -122,6 +122,7 @@ protected:
   SocketProtocol m_protocol;
   NativeSocket m_socket;
   bool m_child_processes_inherit;
+  bool m_should_close_fd;
 };
 
 } // namespace lldb_private

Modified: lldb/trunk/include/lldb/Utility/IOObject.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/IOObject.h?rev=373126=373125=373126=diff
==
--- lldb/trunk/include/lldb/Utility/IOObject.h (original)
+++ lldb/trunk/include/lldb/Utility/IOObject.h Fri Sep 27 13:43:50 2019
@@ -29,8 +29,7 @@ public:
   typedef int WaitableHandle;
   static const WaitableHandle kInvalidHandleValue;
 
-  IOObject(FDType type, bool should_close)
-  : m_fd_type(type), m_should_close_fd(should_close) {}
+  IOObject(FDType type) : m_fd_type(type) {}
   virtual ~IOObject();
 
   virtual Status Read(void *buf, size_t _bytes) = 0;
@@ -44,8 +43,6 @@ public:
 
 protected:
   FDType m_fd_type;
-  bool m_should_close_fd; // True if this class should close the file 
descriptor
-  // when it goes away.
 
 private:
   DISALLOW_COPY_AND_ASSIGN(IOObject);

Modified: lldb/trunk/source/Host/common/File.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/File.cpp?rev=373126=373125=373126=diff
==
--- lldb/trunk/source/Host/common/File.cpp (original)
+++ lldb/trunk/source/Host/common/File.cpp Fri Sep 27 13:43:50 2019
@@ -98,7 +98,7 @@ FILE *File::GetStream() {
 if (DescriptorIsValid()) {
   const char *mode = GetStreamOpenModeFromOptions(m_options);
   if (mode) {
-if (!m_should_close_fd) {
+if (!m_own_descriptor) {
 // We must