[Lldb-commits] [PATCH] D115926: [lldb/lua] Support external breakpoint callback

2021-12-17 Thread Siger Young via Phabricator via lldb-commits
siger-young created this revision.
siger-young added reviewers: JDevlieghere, tammela.
siger-young requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This commit supports registerubg breakpoint callback when using
liblldb in Lua. (able to `SetScriptCallback(function(a, b, ...) end)`)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115926

Files:
  lldb/bindings/interface/SBBreakpoint.i
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/include/lldb/API/SBBreakpoint.h
  lldb/include/lldb/API/SBBreakpointOptionCommon.h
  lldb/include/lldb/API/SBDefines.h
  lldb/source/API/SBBreakpoint.cpp
  lldb/source/API/SBBreakpointName.cpp
  lldb/source/API/SBBreakpointOptionCommon.cpp
  lldb/source/API/SBBreakpointOptionCommon.h
  lldb/test/API/lua_api/TestBreakpointAPI.lua

Index: lldb/test/API/lua_api/TestBreakpointAPI.lua
===
--- lldb/test/API/lua_api/TestBreakpointAPI.lua
+++ lldb/test/API/lua_api/TestBreakpointAPI.lua
@@ -49,4 +49,20 @@
 assertEquals(var_argc_value, 3)
 end
 
+function _T:TestBreakpointRealCallback()
+local target = self:create_target()
+local breakpoint = target:BreakpointCreateByLocation('main.c', 31)
+assertTrue(breakpoint:IsValid() and breakpoint:GetNumLocations() == 1)
+local flag = false
+local i = 1
+breakpoint:SetScriptCallback(function(frame)
+flag = true
+assertEquals(frame:FindVariable('i'):GetValueAsSigned(), i)
+i = i + 1
+return false
+end)
+target:LaunchSimple(nil, nil, nil)
+assertTrue(flag)
+end
+
 os.exit(_T:run())
Index: lldb/source/API/SBBreakpointOptionCommon.h
===
--- lldb/source/API/SBBreakpointOptionCommon.h
+++ /dev/null
@@ -1,36 +0,0 @@
-//===-- SBBreakpointOptionCommon.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_SOURCE_API_SBBREAKPOINTOPTIONCOMMON_H
-#define LLDB_SOURCE_API_SBBREAKPOINTOPTIONCOMMON_H
-
-#include "lldb/API/SBDefines.h"
-#include "lldb/Utility/Baton.h"
-
-namespace lldb
-{
-struct CallbackData {
-  SBBreakpointHitCallback callback;
-  void *callback_baton;
-};
-
-class SBBreakpointCallbackBaton : public lldb_private::TypedBaton {
-public:
-  SBBreakpointCallbackBaton(SBBreakpointHitCallback callback,
-void *baton);
-
-  ~SBBreakpointCallbackBaton() override;
-
-  static bool PrivateBreakpointHitCallback(void *baton,
-   lldb_private::StoppointCallbackContext *ctx,
-   lldb::user_id_t break_id,
-   lldb::user_id_t break_loc_id);
-};
-
-} // namespace lldb
-#endif // LLDB_SOURCE_API_SBBREAKPOINTOPTIONCOMMON_H
Index: lldb/source/API/SBBreakpointOptionCommon.cpp
===
--- lldb/source/API/SBBreakpointOptionCommon.cpp
+++ lldb/source/API/SBBreakpointOptionCommon.cpp
@@ -6,8 +6,9 @@
 //
 //===--===//
 
-#include "lldb/API/SBBreakpointName.h"
+#include "lldb/API/SBBreakpointOptionCommon.h"
 #include "lldb/API/SBBreakpointLocation.h"
+#include "lldb/API/SBBreakpointName.h"
 #include "lldb/API/SBDebugger.h"
 #include "lldb/API/SBEvent.h"
 #include "lldb/API/SBProcess.h"
@@ -31,13 +32,15 @@
 
 #include "lldb/lldb-enumerations.h"
 
-#include "SBBreakpointOptionCommon.h"
-
 #include "llvm/ADT/STLExtras.h"
 
 using namespace lldb;
 using namespace lldb_private;
 
+SBBreakpointCallbackBaton::SBBreakpointCallbackBaton(
+std::unique_ptr Data)
+: TypedBaton(std::move(Data)) {}
+
 SBBreakpointCallbackBaton::SBBreakpointCallbackBaton(SBBreakpointHitCallback 
  callback,
  void *baton)
@@ -52,26 +55,18 @@
   lldb::user_id_t break_loc_id)
 {
   ExecutionContext exe_ctx(ctx->exe_ctx_ref);
+  SBFrame sb_frame(exe_ctx.GetFrameSP());
   BreakpointSP bp_sp(
   exe_ctx.GetTargetRef().GetBreakpointList().FindBreakpointByID(break_id));
   if (baton && bp_sp) {
 CallbackData *data = (CallbackData *)baton;
 lldb_private::Breakpoint *bp = bp_sp.get();
-if (bp && data->callback) {
-  Process *process = exe_ctx.GetProcessPtr();
-  if (process) {
-SBProcess sb_process(process->shared_from_this());
-SBThread sb_thread;
-SBBreakpointLocation sb_location;
-assert(bp_sp);
-sb_loca

[Lldb-commits] [PATCH] D115926: [lldb/lua] Support external breakpoint callback

2021-12-17 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 395076.
siger-young added a comment.

Remove "return" to prevent memory leakage.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115926/new/

https://reviews.llvm.org/D115926

Files:
  lldb/bindings/interface/SBBreakpoint.i
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/include/lldb/API/SBBreakpoint.h
  lldb/include/lldb/API/SBBreakpointOptionCommon.h
  lldb/include/lldb/API/SBDefines.h
  lldb/source/API/SBBreakpoint.cpp
  lldb/source/API/SBBreakpointName.cpp
  lldb/source/API/SBBreakpointOptionCommon.cpp
  lldb/source/API/SBBreakpointOptionCommon.h
  lldb/test/API/lua_api/TestBreakpointAPI.lua

Index: lldb/test/API/lua_api/TestBreakpointAPI.lua
===
--- lldb/test/API/lua_api/TestBreakpointAPI.lua
+++ lldb/test/API/lua_api/TestBreakpointAPI.lua
@@ -49,4 +49,20 @@
 assertEquals(var_argc_value, 3)
 end
 
+function _T:TestBreakpointRealCallback()
+local target = self:create_target()
+local breakpoint = target:BreakpointCreateByLocation('main.c', 31)
+assertTrue(breakpoint:IsValid() and breakpoint:GetNumLocations() == 1)
+local flag = false
+local i = 1
+breakpoint:SetScriptCallback(function(frame)
+flag = true
+assertEquals(frame:FindVariable('i'):GetValueAsSigned(), i)
+i = i + 1
+return false
+end)
+target:LaunchSimple(nil, nil, nil)
+assertTrue(flag)
+end
+
 os.exit(_T:run())
Index: lldb/source/API/SBBreakpointOptionCommon.h
===
--- lldb/source/API/SBBreakpointOptionCommon.h
+++ /dev/null
@@ -1,36 +0,0 @@
-//===-- SBBreakpointOptionCommon.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_SOURCE_API_SBBREAKPOINTOPTIONCOMMON_H
-#define LLDB_SOURCE_API_SBBREAKPOINTOPTIONCOMMON_H
-
-#include "lldb/API/SBDefines.h"
-#include "lldb/Utility/Baton.h"
-
-namespace lldb
-{
-struct CallbackData {
-  SBBreakpointHitCallback callback;
-  void *callback_baton;
-};
-
-class SBBreakpointCallbackBaton : public lldb_private::TypedBaton {
-public:
-  SBBreakpointCallbackBaton(SBBreakpointHitCallback callback,
-void *baton);
-
-  ~SBBreakpointCallbackBaton() override;
-
-  static bool PrivateBreakpointHitCallback(void *baton,
-   lldb_private::StoppointCallbackContext *ctx,
-   lldb::user_id_t break_id,
-   lldb::user_id_t break_loc_id);
-};
-
-} // namespace lldb
-#endif // LLDB_SOURCE_API_SBBREAKPOINTOPTIONCOMMON_H
Index: lldb/source/API/SBBreakpointOptionCommon.cpp
===
--- lldb/source/API/SBBreakpointOptionCommon.cpp
+++ lldb/source/API/SBBreakpointOptionCommon.cpp
@@ -6,8 +6,9 @@
 //
 //===--===//
 
-#include "lldb/API/SBBreakpointName.h"
+#include "lldb/API/SBBreakpointOptionCommon.h"
 #include "lldb/API/SBBreakpointLocation.h"
+#include "lldb/API/SBBreakpointName.h"
 #include "lldb/API/SBDebugger.h"
 #include "lldb/API/SBEvent.h"
 #include "lldb/API/SBProcess.h"
@@ -31,13 +32,15 @@
 
 #include "lldb/lldb-enumerations.h"
 
-#include "SBBreakpointOptionCommon.h"
-
 #include "llvm/ADT/STLExtras.h"
 
 using namespace lldb;
 using namespace lldb_private;
 
+SBBreakpointCallbackBaton::SBBreakpointCallbackBaton(
+std::unique_ptr Data)
+: TypedBaton(std::move(Data)) {}
+
 SBBreakpointCallbackBaton::SBBreakpointCallbackBaton(SBBreakpointHitCallback 
  callback,
  void *baton)
@@ -52,26 +55,18 @@
   lldb::user_id_t break_loc_id)
 {
   ExecutionContext exe_ctx(ctx->exe_ctx_ref);
+  SBFrame sb_frame(exe_ctx.GetFrameSP());
   BreakpointSP bp_sp(
   exe_ctx.GetTargetRef().GetBreakpointList().FindBreakpointByID(break_id));
   if (baton && bp_sp) {
 CallbackData *data = (CallbackData *)baton;
 lldb_private::Breakpoint *bp = bp_sp.get();
-if (bp && data->callback) {
-  Process *process = exe_ctx.GetProcessPtr();
-  if (process) {
-SBProcess sb_process(process->shared_from_this());
-SBThread sb_thread;
-SBBreakpointLocation sb_location;
-assert(bp_sp);
-sb_location.SetLocation(bp_sp->FindLocationByID(break_loc_id));
-Thread *thread = exe_ctx.GetThreadPtr();
-if (thread)
-  sb_thread.Se

[Lldb-commits] [PATCH] D115926: [lldb/lua] Support external breakpoint callback

2021-12-17 Thread Siger Young via Phabricator via lldb-commits
siger-young added a comment.

In D115926#3199496 , @labath wrote:

> I don't know if you've seen this but we have some description of it here 
> https://lldb.llvm.org/design/sbapi.html. The gist of it is:
>
> - be backward compatible
> - don't depend on other stuff

Thanks, I got it. I will stick to these rules.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115926/new/

https://reviews.llvm.org/D115926

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


[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-09-09 Thread Siger Young via Phabricator via lldb-commits
siger-young added inline comments.



Comment at: lldb/bindings/lua/lua-typemaps.swig:219-221
+%typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** {
+   $1 = (lua_istable(L, $input) || lua_isnil(L, $input));
+}

tammela wrote:
> This is not being generated by SWIG for some reason.
> 
> I think it's more readable to just have an else clause that raises an error 
> on line 212.
I tried commenting and uncommenting these lines then diff, found that the 
typecheck is actually generated on those overloaded functions. 
`SBTarget::Launch` no longer works after commenting in my cases:
```
lua5.3: test.lua:27: Wrong arguments for overloaded function 'SBTarget_Launch'
  Possible C/C++ prototypes are:
lldb::SBTarget::Launch(lldb::SBListener &,char const **,char const **,char 
const *,char const *,char const *,char const *,uint32_t,bool,lldb::SBError &)
lldb::SBTarget::Launch(lldb::SBLaunchInfo &,lldb::SBError &)
```

It seems that SWIG uses "SWIG_isptrtype" to decide arg matches "char**", so 
this typecheck is necessary.

For those functions with just one definition (i.e. not overloaded), this 
typecheck does nothing on them, so it will be `typemap(in)`'s responsibility to 
check arg types.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

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


[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-09-26 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 375081.
siger-young added a comment.

This update mainly fixed problematic typemaps and adding necessary comments.

Together, it forced Lua installation path as "PREFIX/lib/lua/5.3" and removed 
"lit.util" in tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

Files:
  lldb/CMakeLists.txt
  lldb/bindings/lua/CMakeLists.txt
  lldb/bindings/lua/lua-typemaps.swig
  lldb/test/API/lua_api/TestComprehensive.lua
  lldb/test/API/lua_api/TestLuaAPI.py

Index: lldb/test/API/lua_api/TestLuaAPI.py
===
--- lldb/test/API/lua_api/TestLuaAPI.py
+++ lldb/test/API/lua_api/TestLuaAPI.py
@@ -5,8 +5,129 @@
 from lldbsuite.test.decorators import *
 from lldbsuite.test.lldbtest import *
 from lldbsuite.test import lldbutil
-import lit.util
+import subprocess
 
+def to_string(b):
+"""Return the parameter as type 'str', possibly encoding it.
+
+In Python2, the 'str' type is the same as 'bytes'. In Python3, the
+'str' type is (essentially) Python2's 'unicode' type, and 'bytes' is
+distinct.
+
+"""
+if isinstance(b, str):
+# In Python2, this branch is taken for types 'str' and 'bytes'.
+# In Python3, this branch is taken only for 'str'.
+return b
+if isinstance(b, bytes):
+# In Python2, this branch is never taken ('bytes' is handled as 'str').
+# In Python3, this is true only for 'bytes'.
+try:
+return b.decode('utf-8')
+except UnicodeDecodeError:
+# If the value is not valid Unicode, return the default
+# repr-line encoding.
+return str(b)
+
+# By this point, here's what we *don't* have:
+#
+#  - In Python2:
+#- 'str' or 'bytes' (1st branch above)
+#  - In Python3:
+#- 'str' (1st branch above)
+#- 'bytes' (2nd branch above)
+#
+# The last type we might expect is the Python2 'unicode' type. There is no
+# 'unicode' type in Python3 (all the Python3 cases were already handled). In
+# order to get a 'str' object, we need to encode the 'unicode' object.
+try:
+return b.encode('utf-8')
+except AttributeError:
+raise TypeError('not sure how to convert %s to %s' % (type(b), str))
+
+class ExecuteCommandTimeoutException(Exception):
+def __init__(self, msg, out, err, exitCode):
+assert isinstance(msg, str)
+assert isinstance(out, str)
+assert isinstance(err, str)
+assert isinstance(exitCode, int)
+self.msg = msg
+self.out = out
+self.err = err
+self.exitCode = exitCode
+
+
+# Close extra file handles on UNIX (on Windows this cannot be done while
+# also redirecting input).
+kUseCloseFDs = not (platform.system() == 'Windows')
+
+
+def executeCommand(command, cwd=None, env=None, input=None, timeout=0):
+"""Execute command ``command`` (list of arguments or string) with.
+
+* working directory ``cwd`` (str), use None to use the current
+working directory
+* environment ``env`` (dict), use None for none
+* Input to the command ``input`` (str), use string to pass
+no input.
+* Max execution time ``timeout`` (int) seconds. Use 0 for no timeout.
+
+Returns a tuple (out, err, exitCode) where
+* ``out`` (str) is the standard output of running the command
+* ``err`` (str) is the standard error of running the command
+* ``exitCode`` (int) is the exitCode of running the command
+
+If the timeout is hit an ``ExecuteCommandTimeoutException``
+is raised.
+
+"""
+if input is not None:
+input = to_bytes(input)
+p = subprocess.Popen(command, cwd=cwd,
+stdin=subprocess.PIPE,
+stdout=subprocess.PIPE,
+stderr=subprocess.PIPE,
+env=env, close_fds=kUseCloseFDs)
+timerObject = None
+# FIXME: Because of the way nested function scopes work in Python 2.x we
+# need to use a reference to a mutable object rather than a plain
+# bool. In Python 3 we could use the "nonlocal" keyword but we need
+# to support Python 2 as well.
+hitTimeOut = [False]
+try:
+if timeout > 0:
+def killProcess():
+# We may be invoking a shell so we need to kill the
+# process and all its children.
+hitTimeOut[0] = True
+killProcessAndChildren(p.pid)
+
+timerObject = threading.Timer(timeout, killProcess)
+timerObject.start()
+
+out, err = p.communicate(input=input)
+exitCode = p.wait()
+finally:
+if timerObject != None:
+timerObject.cancel()
+
+# Ensure the resulting output is always of string type.
+out = to_string(out)
+err = to_string(err)
+
+if hitTimeOut[0]:
+  

[Lldb-commits] [PATCH] D108515: [lldb/lua] Force Lua version to be 5.3

2021-09-26 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 375086.
siger-young added a comment.

Remove "REQUIRED" flags when finding Lua at "FindLuaAndSwig.cmake".


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108515/new/

https://reviews.llvm.org/D108515

Files:
  lldb/cmake/modules/FindLuaAndSwig.cmake
  lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt


Index: lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
+++ lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
@@ -1,5 +1,3 @@
-find_package(Lua REQUIRED)
-
 add_lldb_library(lldbPluginScriptInterpreterLua PLUGIN
   Lua.cpp
   ScriptInterpreterLua.cpp
Index: lldb/cmake/modules/FindLuaAndSwig.cmake
===
--- lldb/cmake/modules/FindLuaAndSwig.cmake
+++ lldb/cmake/modules/FindLuaAndSwig.cmake
@@ -9,7 +9,7 @@
 else()
   find_package(SWIG 3.0)
   if (SWIG_FOUND)
-find_package(Lua 5.3)
+find_package(Lua 5.3 EXACT)
 if(LUA_FOUND AND SWIG_FOUND)
   mark_as_advanced(
 LUA_LIBRARIES


Index: lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
+++ lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
@@ -1,5 +1,3 @@
-find_package(Lua REQUIRED)
-
 add_lldb_library(lldbPluginScriptInterpreterLua PLUGIN
   Lua.cpp
   ScriptInterpreterLua.cpp
Index: lldb/cmake/modules/FindLuaAndSwig.cmake
===
--- lldb/cmake/modules/FindLuaAndSwig.cmake
+++ lldb/cmake/modules/FindLuaAndSwig.cmake
@@ -9,7 +9,7 @@
 else()
   find_package(SWIG 3.0)
   if (SWIG_FOUND)
-find_package(Lua 5.3)
+find_package(Lua 5.3 EXACT)
 if(LUA_FOUND AND SWIG_FOUND)
   mark_as_advanced(
 LUA_LIBRARIES
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-09-26 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 375100.
siger-young added a comment.

Fix typo in SBData test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

Files:
  lldb/test/API/lua_api/TestComprehensive.lua


Index: lldb/test/API/lua_api/TestComprehensive.lua
===
--- lldb/test/API/lua_api/TestComprehensive.lua
+++ lldb/test/API/lua_api/TestComprehensive.lua
@@ -71,7 +71,7 @@
 assertTrue(error:Success())
 local data_le = 
lldb.SBData.CreateDataFromUInt32Array(lldb.eByteOrderLittle, 1, {0xDEADBEEF})
 local data_be = lldb.SBData.CreateDataFromUInt32Array(lldb.eByteOrderBig, 
1, {0xDEADBEEF})
-assertTrue(data_le:GetUnsignedInt32(error, 0) == 0xDCADBEEF or 
data_be:GetUnsignedInt32(error, 0) == 0xDCADBEEF)
+assertTrue(data_le:GetUnsignedInt32(error, 0) == 0xDEADBEEF or 
data_be:GetUnsignedInt32(error, 0) == 0xDEADBEEF)
 assertTrue(raw_data == "\xEF\xBE\xAD\xDE" or raw_data == 
"\xDE\xAD\xBE\xEF")
 end
 


Index: lldb/test/API/lua_api/TestComprehensive.lua
===
--- lldb/test/API/lua_api/TestComprehensive.lua
+++ lldb/test/API/lua_api/TestComprehensive.lua
@@ -71,7 +71,7 @@
 assertTrue(error:Success())
 local data_le = lldb.SBData.CreateDataFromUInt32Array(lldb.eByteOrderLittle, 1, {0xDEADBEEF})
 local data_be = lldb.SBData.CreateDataFromUInt32Array(lldb.eByteOrderBig, 1, {0xDEADBEEF})
-assertTrue(data_le:GetUnsignedInt32(error, 0) == 0xDCADBEEF or data_be:GetUnsignedInt32(error, 0) == 0xDCADBEEF)
+assertTrue(data_le:GetUnsignedInt32(error, 0) == 0xDEADBEEF or data_be:GetUnsignedInt32(error, 0) == 0xDEADBEEF)
 assertTrue(raw_data == "\xEF\xBE\xAD\xDE" or raw_data == "\xDE\xAD\xBE\xEF")
 end
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-09-26 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 375103.
siger-young added a comment.

Rebase commits.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

Files:
  lldb/CMakeLists.txt
  lldb/bindings/lua/CMakeLists.txt
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/bindings/lua/lua.swig
  lldb/source/API/liblldb-private.exports
  lldb/source/API/liblldb.exports
  lldb/test/API/lit.site.cfg.py.in
  lldb/test/API/lldbtest.py
  lldb/test/API/lua_api/Makefile
  lldb/test/API/lua_api/TestBreakpointAPI.lua
  lldb/test/API/lua_api/TestComprehensive.lua
  lldb/test/API/lua_api/TestFileHandle.lua
  lldb/test/API/lua_api/TestLuaAPI.py
  lldb/test/API/lua_api/TestProcessAPI.lua
  lldb/test/API/lua_api/lua_lldb_test.lua
  lldb/test/API/lua_api/main.c

Index: lldb/test/API/lua_api/main.c
===
--- /dev/null
+++ lldb/test/API/lua_api/main.c
@@ -0,0 +1,35 @@
+#include 
+
+void BFunction()
+{
+}
+
+void AFunction()
+{
+printf("I am a function.\n");
+}
+
+int main(int argc, const char *argv[])
+{
+int inited = 0xDEADBEEF;
+int sum = 0;
+if(argc > 1)
+{
+for(int i = 0; i < argc; i++)
+{
+puts(argv[i]);
+}
+if(argc > 2)
+{
+return argc;
+}
+}
+AFunction();
+for(int i = 1; i <= 100; i++)
+{
+BFunction();
+sum += i;
+}
+printf("sum = %d\n", sum);
+return 0;
+}
Index: lldb/test/API/lua_api/lua_lldb_test.lua
===
--- /dev/null
+++ lldb/test/API/lua_api/lua_lldb_test.lua
@@ -0,0 +1,107 @@
+-- Import all functions of luaunit
+EXPORT_ASSERT_TO_GLOBALS = true
+require('luaunit')
+
+-- Make lldb available in global
+lldb = require('lldb')
+
+-- Global helper functions
+function read_file_non_empty_lines(f)
+local lines = {}
+while true do
+local line = f:read('*l')
+if not line then break end
+if line ~= '\n' then table.insert(lines, line) end
+end
+return lines
+end
+
+function split_lines(str)
+local lines = {}
+for line in str:gmatch("[^\r\n]+") do
+table.insert(lines, line)
+end
+return lines
+end
+
+function get_stopped_threads(process, reason)
+local threads = {}
+for i = 0, process:GetNumThreads() - 1 do
+local t = process:GetThreadAtIndex(i)
+if t:IsValid() and t:GetStopReason() == reason then
+table.insert(threads, t)
+end
+end
+return threads
+end
+
+function get_stopped_thread(process, reason)
+local threads = get_stopped_threads(process, reason)
+if #threads ~= 0 then return threads[1]
+else return nil end
+end
+
+-- Test helper
+
+local _M = {}
+local _m = {}
+
+local _mt = { __index = _m }
+
+function _M.create_test(name, exe, output, input)
+print('[lldb/lua] Doing test ' .. name)
+exe = exe or os.getenv('TEST_EXE')
+output = output or os.getenv('TEST_OUTPUT')
+input = input or os.getenv('TEST_INPUT')
+lldb.SBDebugger.Initialize()
+local debugger = lldb.SBDebugger.Create()
+-- Ensure that debugger is created
+assertNotNil(debugger)
+assertTrue(debugger:IsValid())
+
+debugger:SetAsync(false)
+
+local lua_language = debugger:GetScriptingLanguage('lua')
+assertNotNil(lua_language)
+debugger:SetScriptLanguage(lua_language)
+
+local test = setmetatable({
+output = output,
+input = input,
+name = name,
+exe = exe,
+debugger = debugger
+}, _mt)
+_G[name] = test
+return test
+end
+
+function _m:create_target(exe)
+local target
+if not exe then exe = self.exe end
+target = self.debugger:CreateTarget(exe)
+-- Ensure that target is created
+assertNotNil(target)
+assertTrue(target:IsValid())
+return target
+end
+
+function _m:handle_command(command, collect)
+if collect == nil then collect = true end
+if collect then
+local ret = lldb.SBCommandReturnObject()
+local interpreter = self.debugger:GetCommandInterpreter()
+assertTrue(interpreter:IsValid())
+interpreter:HandleCommand(command, ret)
+self.debugger:GetOutputFile():Flush()
+self.debugger:GetErrorFile():Flush()
+assertTrue(ret:Succeeded())
+return ret:GetOutput()
+else
+self.debugger:HandleCommand(command)
+self.debugger:GetOutputFile():Flush()
+self.debugger:GetErrorFile():Flush()
+end
+end
+
+return _M
Index: lldb/test/API/lua_api/TestProcessAPI.lua
===
--- /dev/null
+++ lldb/test/API/lua_api/TestProcessAPI.lua
@@ -0,0 +1,59 @@
+_T = require('lua_lldb_test').create_test('TestProcessAPI')
+
+function _T:TestProcessLaunchSimple()
+local target = self:create_target()
+local

[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-09-27 Thread Siger Young via Phabricator via lldb-commits
siger-young added inline comments.



Comment at: lldb/test/API/lua_api/lua_lldb_test.lua:3
+EXPORT_ASSERT_TO_GLOBALS = true
+require('luaunit')
+

tammela wrote:
> Could we not use an external dependency?
> For instance in my setup it fails because it couldn't find this library.
Does it make sense to directly copy "luaunit.lua" to the Lua test dir?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

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


[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-09-27 Thread Siger Young via Phabricator via lldb-commits
siger-young added inline comments.



Comment at: lldb/test/API/lua_api/lua_lldb_test.lua:3
+EXPORT_ASSERT_TO_GLOBALS = true
+require('luaunit')
+

tammela wrote:
> siger-young wrote:
> > tammela wrote:
> > > Could we not use an external dependency?
> > > For instance in my setup it fails because it couldn't find this library.
> > Does it make sense to directly copy "luaunit.lua" to the Lua test dir?
> You don't seem to have a hard dependency on it.
> Couldn't you just replicate what you are interested? Instead of bringing in a 
> full blown unit testing framework...
Oh sorry, I didn't notice the comments here.

I will remove the dependency of "luaunit" soon.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

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


[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-09-27 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 375240.
siger-young added a comment.

Add assertion functions and error status detection to remove "luaunit"


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

Files:
  lldb/CMakeLists.txt
  lldb/bindings/lua/CMakeLists.txt
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/bindings/lua/lua.swig
  lldb/source/API/liblldb-private.exports
  lldb/source/API/liblldb.exports
  lldb/test/API/lit.site.cfg.py.in
  lldb/test/API/lldbtest.py
  lldb/test/API/lua_api/Makefile
  lldb/test/API/lua_api/TestBreakpointAPI.lua
  lldb/test/API/lua_api/TestComprehensive.lua
  lldb/test/API/lua_api/TestFileHandle.lua
  lldb/test/API/lua_api/TestLuaAPI.py
  lldb/test/API/lua_api/TestProcessAPI.lua
  lldb/test/API/lua_api/lua_lldb_test.lua
  lldb/test/API/lua_api/main.c

Index: lldb/test/API/lua_api/main.c
===
--- /dev/null
+++ lldb/test/API/lua_api/main.c
@@ -0,0 +1,35 @@
+#include 
+
+void BFunction()
+{
+}
+
+void AFunction()
+{
+printf("I am a function.\n");
+}
+
+int main(int argc, const char *argv[])
+{
+int inited = 0xDEADBEEF;
+int sum = 0;
+if(argc > 1)
+{
+for(int i = 0; i < argc; i++)
+{
+puts(argv[i]);
+}
+if(argc > 2)
+{
+return argc;
+}
+}
+AFunction();
+for(int i = 1; i <= 100; i++)
+{
+BFunction();
+sum += i;
+}
+printf("sum = %d\n", sum);
+return 0;
+}
Index: lldb/test/API/lua_api/lua_lldb_test.lua
===
--- /dev/null
+++ lldb/test/API/lua_api/lua_lldb_test.lua
@@ -0,0 +1,155 @@
+-- Make lldb available in global
+lldb = require('lldb')
+
+-- Global assertion functions
+function assertTrue(x)
+if not x then error('assertTrue failure') end
+end
+
+function assertFalse(x)
+if x then error('assertNotNil failure') end
+end
+
+function assertNotNil(x)
+if x == nil then error('assertNotNil failure') end
+end
+
+function assertEquals(x, y)
+if type(x) == 'table' and type(y) == 'table' then
+for k, _ in pairs(x) do
+assertEquals(x[k], y[k])
+end
+elseif type(x) ~= type(y) then
+error('assertEquals failure')
+elseif x ~= y then
+error('assertEquals failure')
+end
+end
+
+function assertStrContains(x, y)
+if not string.find(x, y, 1, true) then
+error('assertStrContains failure')
+end
+end
+
+-- Global helper functions
+function read_file_non_empty_lines(f)
+local lines = {}
+while true do
+local line = f:read('*l')
+if not line then break end
+if line ~= '\n' then table.insert(lines, line) end
+end
+return lines
+end
+
+function split_lines(str)
+local lines = {}
+for line in str:gmatch("[^\r\n]+") do
+table.insert(lines, line)
+end
+return lines
+end
+
+function get_stopped_threads(process, reason)
+local threads = {}
+for i = 0, process:GetNumThreads() - 1 do
+local t = process:GetThreadAtIndex(i)
+if t:IsValid() and t:GetStopReason() == reason then
+table.insert(threads, t)
+end
+end
+return threads
+end
+
+function get_stopped_thread(process, reason)
+local threads = get_stopped_threads(process, reason)
+if #threads ~= 0 then return threads[1]
+else return nil end
+end
+
+-- Test helper
+
+local _M = {}
+local _m = {}
+
+local _mt = { __index = _m }
+
+function _M.create_test(name, exe, output, input)
+print('[lldb/lua] Create test ' .. name)
+exe = exe or os.getenv('TEST_EXE')
+output = output or os.getenv('TEST_OUTPUT')
+input = input or os.getenv('TEST_INPUT')
+lldb.SBDebugger.Initialize()
+local debugger = lldb.SBDebugger.Create()
+-- Ensure that debugger is created
+assertNotNil(debugger)
+assertTrue(debugger:IsValid())
+
+debugger:SetAsync(false)
+
+local lua_language = debugger:GetScriptingLanguage('lua')
+assertNotNil(lua_language)
+debugger:SetScriptLanguage(lua_language)
+
+local test = setmetatable({
+output = output,
+input = input,
+name = name,
+exe = exe,
+debugger = debugger
+}, _mt)
+_G[name] = test
+return test
+end
+
+function _m:create_target(exe)
+local target
+if not exe then exe = self.exe end
+target = self.debugger:CreateTarget(exe)
+-- Ensure that target is created
+assertNotNil(target)
+assertTrue(target:IsValid())
+return target
+end
+
+function _m:handle_command(command, collect)
+if collect == nil then collect = true end
+if collect then
+local ret = lldb.SBCommandReturnObject()
+local interpreter = self.debugger:GetCommandInterpreter()
+assertTrue(interpreter:IsValid())
+interpreter:Han

[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-10-12 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 378971.
siger-young added a comment.

Pull and merge conflicts, will soon be merged into main.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

Files:
  lldb/CMakeLists.txt
  lldb/bindings/lua/CMakeLists.txt
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/bindings/lua/lua.swig
  lldb/source/API/liblldb-private.exports
  lldb/source/API/liblldb.exports
  lldb/test/API/lit.site.cfg.py.in
  lldb/test/API/lldbtest.py
  lldb/test/API/lua_api/Makefile
  lldb/test/API/lua_api/TestBreakpointAPI.lua
  lldb/test/API/lua_api/TestComprehensive.lua
  lldb/test/API/lua_api/TestFileHandle.lua
  lldb/test/API/lua_api/TestLuaAPI.py
  lldb/test/API/lua_api/TestProcessAPI.lua
  lldb/test/API/lua_api/lua_lldb_test.lua
  lldb/test/API/lua_api/main.c

Index: lldb/test/API/lua_api/main.c
===
--- /dev/null
+++ lldb/test/API/lua_api/main.c
@@ -0,0 +1,35 @@
+#include 
+
+void BFunction()
+{
+}
+
+void AFunction()
+{
+printf("I am a function.\n");
+}
+
+int main(int argc, const char *argv[])
+{
+int inited = 0xDEADBEEF;
+int sum = 0;
+if(argc > 1)
+{
+for(int i = 0; i < argc; i++)
+{
+puts(argv[i]);
+}
+if(argc > 2)
+{
+return argc;
+}
+}
+AFunction();
+for(int i = 1; i <= 100; i++)
+{
+BFunction();
+sum += i;
+}
+printf("sum = %d\n", sum);
+return 0;
+}
Index: lldb/test/API/lua_api/lua_lldb_test.lua
===
--- /dev/null
+++ lldb/test/API/lua_api/lua_lldb_test.lua
@@ -0,0 +1,155 @@
+-- Make lldb available in global
+lldb = require('lldb')
+
+-- Global assertion functions
+function assertTrue(x)
+if not x then error('assertTrue failure') end
+end
+
+function assertFalse(x)
+if x then error('assertNotNil failure') end
+end
+
+function assertNotNil(x)
+if x == nil then error('assertNotNil failure') end
+end
+
+function assertEquals(x, y)
+if type(x) == 'table' and type(y) == 'table' then
+for k, _ in pairs(x) do
+assertEquals(x[k], y[k])
+end
+elseif type(x) ~= type(y) then
+error('assertEquals failure')
+elseif x ~= y then
+error('assertEquals failure')
+end
+end
+
+function assertStrContains(x, y)
+if not string.find(x, y, 1, true) then
+error('assertStrContains failure')
+end
+end
+
+-- Global helper functions
+function read_file_non_empty_lines(f)
+local lines = {}
+while true do
+local line = f:read('*l')
+if not line then break end
+if line ~= '\n' then table.insert(lines, line) end
+end
+return lines
+end
+
+function split_lines(str)
+local lines = {}
+for line in str:gmatch("[^\r\n]+") do
+table.insert(lines, line)
+end
+return lines
+end
+
+function get_stopped_threads(process, reason)
+local threads = {}
+for i = 0, process:GetNumThreads() - 1 do
+local t = process:GetThreadAtIndex(i)
+if t:IsValid() and t:GetStopReason() == reason then
+table.insert(threads, t)
+end
+end
+return threads
+end
+
+function get_stopped_thread(process, reason)
+local threads = get_stopped_threads(process, reason)
+if #threads ~= 0 then return threads[1]
+else return nil end
+end
+
+-- Test helper
+
+local _M = {}
+local _m = {}
+
+local _mt = { __index = _m }
+
+function _M.create_test(name, exe, output, input)
+print('[lldb/lua] Create test ' .. name)
+exe = exe or os.getenv('TEST_EXE')
+output = output or os.getenv('TEST_OUTPUT')
+input = input or os.getenv('TEST_INPUT')
+lldb.SBDebugger.Initialize()
+local debugger = lldb.SBDebugger.Create()
+-- Ensure that debugger is created
+assertNotNil(debugger)
+assertTrue(debugger:IsValid())
+
+debugger:SetAsync(false)
+
+local lua_language = debugger:GetScriptingLanguage('lua')
+assertNotNil(lua_language)
+debugger:SetScriptLanguage(lua_language)
+
+local test = setmetatable({
+output = output,
+input = input,
+name = name,
+exe = exe,
+debugger = debugger
+}, _mt)
+_G[name] = test
+return test
+end
+
+function _m:create_target(exe)
+local target
+if not exe then exe = self.exe end
+target = self.debugger:CreateTarget(exe)
+-- Ensure that target is created
+assertNotNil(target)
+assertTrue(target:IsValid())
+return target
+end
+
+function _m:handle_command(command, collect)
+if collect == nil then collect = true end
+if collect then
+local ret = lldb.SBCommandReturnObject()
+local interpreter = self.debugger:GetCommandInterpreter()
+assertTrue(interpreter:IsValid())
+interpreter:HandleCommand(com

[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-06-14 Thread Siger Young via Phabricator via lldb-commits
siger-young created this revision.
Herald added a subscriber: arphaman.
siger-young requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Add a basic reference page for Lua scripting, as a counterpart to section 
"Python Reference".


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104281

Files:
  lldb/docs/index.rst
  lldb/docs/use/lua-reference.rst

Index: lldb/docs/use/lua-reference.rst
===
--- /dev/null
+++ lldb/docs/use/lua-reference.rst
@@ -0,0 +1,190 @@
+Lua Reference
+
+
+.. default-role:: samp
+
+Lua is a lightweight but powerful language. At present, Lua is only serving as
+an embedded interpreter in LLDB.
+
+.. contents::
+   :local:
+
+Embedded Lua Interpreter
+---
+
+The embedded Lua interpreter can be accessed in a variety of ways from
+within LLDB. The easiest way is to use the lldb command script with no
+arguments at the lldb command prompt:
+
+::
+
+   (lldb) script
+   >>> t = { 1, 2, 3 }
+   >>> print(t[1])
+   1
+   >>> print(t[1] + t[2])
+   3
+   >>>
+
+This drops you into the embedded Lua interpreter. When running under the
+script command, lldb sets some convenience variables that give you quick access
+to the currently selected entities that characterize the program and debugger
+state. In each case, if there is no currently selected entity of the
+appropriate type, the variable's IsValid method will return false. These
+variables are:
+
++---+-+-+-+
+| Variable  | Type| Equivalent  | Description |
++---+-+-+-+
+| `lldb.debugger`   | `lldb.SBDebugger`   | `SBTarget:GetDebugger`  | Contains the debugger object whose `script` command was invoked.|
+|   | | | The `lldb.SBDebugger` object owns the command interpreter   |
+|   | | | and all the targets in your debug session.  There will always be a  |
+|   | | | Debugger in the embedded interpreter.   |
++---+-+-+-+
+| `lldb.target` | `lldb.SBTarget` | `SBDebugger:GetSelectedTarget`  | Contains the currently selected target - for instance the one made with the |
+|   | | | `file` or selected by the `target select ` command.   |
+|   | | `SBProcess:GetTarget`   | The `lldb.SBTarget` manages one running process, and all the executable |
+|   | | | and debug files for the process.|
++---+-+-+-+
+| `lldb.process`| `lldb.SBProcess`| `SBTarget:GetProcess`   | Contains the process of the currently selected target.  |
+|   | | | The `lldb.SBProcess` object manages the threads and allows access to|
+|   | | `SBThread:GetProcess`   | memory for the process. |
++---+-+-+-+
+| `lldb.thread` | `lldb.SBThread` | `SBProcess:GetSelectedThread`   | Contains the currently selected thread. |
+|   | | | The `lldb.SBThread` object manages the stack frames in that thread. |
+|   | | `SBFrame:GetThread` | A thread is always selected in the command interpreter when a target stops. |
+|   | |

[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-06-15 Thread Siger Young via Phabricator via lldb-commits
siger-young added a comment.

In D104281#2818620 , @teemperor wrote:

> Instead of copying the text from the Python page, I was actually thinking 
> whether we should make the 'scripting' page more generic and just add the 
> language-specific examples for Python and Lua there. I am not sure what's the 
> best way to do that visually though with RST. We could just go for raw HTML 
> and use a tab switcher like this:

In fact, the same idea also occurred to me before. I think it's also achievable 
by a plugin sphinx-code-tabs.

I will try to improve the whole scripting section in this kind of way.

> Also I don't think a lot of LLDB distributors include Lua at the moment, so I 
> think that should be pointed out at some point, otherwise this leads to 
> confusion.

Yes. I will put some tips at the beginning of the docs.

I will update the patch in few weeks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

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


[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-06-21 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 353434.
siger-young added a comment.

Reference page for Python and Lua is ready. A new sphinx extension "sphinx-tabs"
is added to enable users switching the language.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

Files:
  lldb/docs/conf.py
  lldb/docs/index.rst
  lldb/docs/use/python-reference.rst
  lldb/docs/use/scripting-reference.rst

Index: lldb/docs/use/scripting-reference.rst
===
--- lldb/docs/use/scripting-reference.rst
+++ lldb/docs/use/scripting-reference.rst
@@ -1,23 +1,30 @@
-Python Reference
-
+Scripting Reference
+===
 
-The entire LLDB API is available as Python functions through a script bridging
-interface. This means the LLDB API's can be used directly from python either
-interactively or to build python apps that provide debugger features.
-
-Additionally, Python can be used as a programmatic interface within the lldb
-command interpreter (we refer to this for brevity as the embedded interpreter).
+Python can be used as a programmatic interface within the LLDB command
+interpreter (we refer to this for brevity as the embedded interpreter).
 Of course, in this context it has full access to the LLDB API - with some
 additional conveniences we will call out in the FAQ.
 
+Additionally, the entire LLDB API is available as Python functions through
+a script bridging interface. This means the LLDB API's can be used directly
+from Python either interactively or to build Python apps that provide debugger
+features.
+
+At present, Lua is experimentally available as an embedded interpreter in LLDB.
+Since some scripting features are still unavailable in Lua, there is no language
+switch tab on code blocks of unsupported features. If you want to use Lua
+interpreter, you should ensure that your LLDB is compiled with Lua support.
+See :doc:`Building ` for instructions. 
+
 .. contents::
:local:
 
-Documentation
---
+Built-in Documentation
+--
 
-The LLDB API is contained in a python module named lldb. A useful resource when
-writing Python extensions is the lldb Python classes reference guide.
+The LLDB API is contained in a Python module named ``lldb``. A useful resource
+when writing Python extensions is the `lldb` Python classes reference guide.
 
 The documentation is also accessible in an interactive debugger session with
 the following command:
@@ -54,7 +61,8 @@
   |
...
 
-Or you can get help using any python object, here we use the lldb.process
+
+Or you can get help using any Python object, here we use the lldb.process
 object which is a global variable in the lldb module which represents the
 currently selected process:
 
@@ -74,25 +82,53 @@
   |
...
 
-Embedded Python Interpreter
+Embedded Script Interpreter
 ---
 
-The embedded python interpreter can be accessed in a variety of ways from
-within LLDB. The easiest way is to use the lldb command script with no
-arguments at the lldb command prompt:
+The embedded script interpreter can be accessed in a variety of ways from
+within LLDB. The easiest way is to use the LLDB command ``script`` with no
+arguments at the LLDB command prompt.
+
+By default, the embedded script interpreter is Python. To choose the language
+you prefer, you can specify default language via ``--script-language`` options
+when launching LLDB:
+
+::
+
+   $ lldb --script-language lua
+
+or pass a parameter to LLDB command ``script``.
 
 ::
 
-   (lldb) script
-   Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
-   >>> 2+3
-   5
-   >>> hex(12345)
-   '0x3039'
-   >>>
+   (lldb) script -l lua --
+
+Language features are all available in the embedded script interpreter:
+
+.. tabs::
+   .. code-tab:: python
+
+  (lldb) script
+  Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
+  >>> 2+3
+  5
+  >>> hex(12345)
+  '0x3039'
+  >>>
+
+   .. code-tab:: lua
+  :emphasize-lines: 1
+
+  (lldb) script -l lua --
+  >>> t = { 1, 2, 3 }
+  >>> print(t[1])
+  1
+  >>> print(t[1] + t[2])
+  3
+  >>>
 
-This drops you into the embedded python interpreter. When running under the
-script command, lldb sets some convenience variables that give you quick access
+This drops you into the embedded script interpreter. When running under the
+script command, LLDB sets some convenience variables that give you quick access
 to the currently selected entities that characterize the program and debugger
 state. In each case, if there is no currently selected entity of the
 appropriate type, the variable's IsValid method will return false. These
@@ -138,60 +174,84 @@
 
 Moreover, they are only defined and meaningful while in the interactive Python
 interpreter. There is no guarantee

[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-06-21 Thread Siger Young via Phabricator via lldb-commits
siger-young added inline comments.



Comment at: lldb/docs/conf.py:32-33
 # set by CMake.
-sys.path.insert(0, os.getenv("LLDB_SWIG_MODULE"))
+if os.getenv("LLDB_SWIG_MODULE") is not None:
+sys.path.insert(0, os.getenv("LLDB_SWIG_MODULE"))
 

JDevlieghere wrote:
> Why is this necessary? It sounds like CMake should be setting this variable.
At the beginning, I was using `ninja lldb-docs-html` to build the docs. But it 
seems that it will build all of the docs each time it runs. It wastes some 
time, so I turn to `sphinx-build` command to build it, thus the env might 
become `None`. If extension `sphinx_tabs.tabs` is added, `None` value in 
`sys.path` will affect its importing.



Comment at: lldb/docs/conf.py:64-77
 try:
-  import recommonmark
+import recommonmark
 except ImportError:
-  # manpages do not use any .md sources
-  if not building_man_page:
-raise
+# manpages do not use any .md sources
+if not building_man_page:
+raise
 else:

JDevlieghere wrote:
> Unrelated change? 
Yes, it's unrelated.



Comment at: lldb/docs/conf.py:180-182
+# Add any extra stylesheets and scripts here.
+html_css_files = ['lldb.css']
+html_js_files = []

JDevlieghere wrote:
> Why is this necessary?
When I built the docs using the `html_context`, the final pages only include 
`lldb.css`, missing files from theme e.g. `alabaster.css`.



Comment at: lldb/docs/conf.py:306-308
+('index', 'LLDB', u'LLDB Documentation',
+ u'The LLDB Team', 'LLDB', 'One line description of project.',
+ 'Miscellaneous'),

JDevlieghere wrote:
> Unrelated whitespace change?
Yes.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

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


[Lldb-commits] [PATCH] D105034: [lldb/lua] Add scripted watchpoints for Lua

2021-06-28 Thread Siger Young via Phabricator via lldb-commits
siger-young created this revision.
siger-young added reviewers: tammela, JDevlieghere.
siger-young added a project: LLDB.
siger-young requested review of this revision.
Herald added a subscriber: lldb-commits.

Add support for Lua scripted watchpoints, with basic tests.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105034

Files:
  lldb/bindings/lua/lua-swigsafecast.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
  lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
  lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
  lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
  lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp

Index: lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
===
--- lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
+++ lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
@@ -30,6 +30,11 @@
   return false;
 }
 
+extern "C" llvm::Expected LLDBSwigLuaWatchpointCallbackFunction(
+lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp) {
+  return false;
+}
+
 #if _MSC_VER
 #pragma warning (pop)
 #endif
Index: lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
===
--- lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
+++ lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
@@ -1,9 +1,15 @@
 # REQUIRES: lua
 # XFAIL: system-netbsd
-# RUN: echo "int main() { return 0; }" | %clang_host -x c - -o %t
+# RUN: echo "int main() { int val = 1; val++; return 0; }" | %clang_host -x c - -g -o %t
 # RUN: %lldb -s %s --script-language lua %t 2>&1 | FileCheck %s
 b main
 r
-watchpoint set expr 0x0
+watchpoint set variable val
 watchpoint command add -s lua
-# CHECK: error: This script interpreter does not support watchpoint callbacks
+print("val="..tostring(frame:FindVariable("val"):GetValue()))
+quit
+c
+# CHECK: val=1
+# CHECK: val=2
+# CHECK: val=nil
+# CHECK: Process {{[0-9]+}} exited
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
@@ -9,6 +9,7 @@
 #ifndef liblldb_ScriptInterpreterLua_h_
 #define liblldb_ScriptInterpreterLua_h_
 
+#include "lldb/Breakpoint/WatchpointOptions.h"
 #include "lldb/Core/StructuredDataImpl.h"
 #include "lldb/Interpreter/ScriptInterpreter.h"
 #include "lldb/Utility/Status.h"
@@ -61,6 +62,10 @@
  lldb::user_id_t break_id,
  lldb::user_id_t break_loc_id);
 
+  static bool WatchpointCallbackFunction(void *baton,
+ StoppointCallbackContext *context,
+ lldb::user_id_t watch_id);
+
   // PluginInterface protocol
   lldb_private::ConstString GetPluginName() override;
 
@@ -75,9 +80,16 @@
   std::vector &bp_options_vec,
   CommandReturnObject &result) override;
 
+  void
+  CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
+  CommandReturnObject &result) override;
+
   Status SetBreakpointCommandCallback(BreakpointOptions *bp_options,
   const char *command_body_text) override;
 
+  void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
+const char *command_body_text) override;
+
   Status SetBreakpointCommandCallbackFunction(
   BreakpointOptions *bp_options, const char *function_name,
   StructuredData::ObjectSP extra_args_sp) override;
@@ -89,6 +101,10 @@
   Status RegisterBreakpointCallback(BreakpointOptions *bp_options,
 const char *command_body_text,
 StructuredData::ObjectSP extra_args_sp);
+
+  Status RegisterWatchpointCallback(WatchpointOptions *wp_options,
+const char *command_body_text,
+StructuredData::ObjectSP extra_args_sp);
 };
 
 } // namespace lldb_private
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -58,7 +58,13 @@
 const char *instructions = nullptr;
 switch (m_active_io_handler) {
 case eIOHandlerNone:
+  break;
 case eIOHandlerWatchpoint:
+  instructions = "Enter your Lua command(s). Type 'quit' to end.\n"
+ "The commands are compiled as the body of the following "
+   

[Lldb-commits] [PATCH] D105034: [lldb/lua] Add scripted watchpoints for Lua

2021-06-29 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 355428.
siger-young added a comment.

This patch mainly adds two extra tests for Lua scripted watchpoints.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D105034/new/

https://reviews.llvm.org/D105034

Files:
  lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
  lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test


Index: lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
===
--- lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
+++ lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
@@ -6,10 +6,28 @@
 r
 watchpoint set variable val
 watchpoint command add -s lua
-print("val="..tostring(frame:FindVariable("val"):GetValue()))
+print("val=" .. tostring(frame:FindVariable("val"):GetValue()))
 quit
 c
 # CHECK: val=1
 # CHECK: val=2
-# CHECK: val=nil
+# CHECK: Process {{[0-9]+}} exited
+r
+watchpoint set variable val
+watchpoint modify 1 -c "(val == 1)"
+watchpoint command add -s lua
+print("conditional watchpoint")
+wp:SetEnabled(false)
+quit
+c
+# CHECK: conditional watchpoint
+# CHECK-NOT: conditional watchpoint
+# CHECK: Process {{[0-9]+}} exited
+r
+watchpoint set expr 0x00
+watchpoint command add -s lua
+print("never triggers")
+quit
+c
+# CHECK-NOT: never triggers
 # CHECK: Process {{[0-9]+}} exited
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -117,8 +117,8 @@
   io_handler.SetIsDone(true);
 } break;
 case eIOHandlerWatchpoint: {
-  auto *wp_options = static_cast(
-  io_handler.GetUserData());
+  auto *wp_options =
+  static_cast(io_handler.GetUserData());
   m_script_interpreter.SetWatchpointCommandCallback(wp_options,
 data.c_str());
   io_handler.SetIsDone(true);
@@ -304,9 +304,8 @@
   debugger.GetScriptInterpreter(true, eScriptLanguageLua));
   Lua &lua = lua_interpreter->GetLua();
 
-  llvm::Expected BoolOrErr = lua.CallWatchpointCallback(baton,
-  stop_frame_sp,
-  wp_sp);
+  llvm::Expected BoolOrErr =
+  lua.CallWatchpointCallback(baton, stop_frame_sp, wp_sp);
   if (llvm::Error E = BoolOrErr.takeError()) {
 debugger.GetErrorStream() << toString(std::move(E));
 return true;
@@ -325,8 +324,7 @@
 }
 
 void ScriptInterpreterLua::CollectDataForWatchpointCommandCallback(
-WatchpointOptions *wp_options,
-CommandReturnObject &result) {
+WatchpointOptions *wp_options, CommandReturnObject &result) {
   IOHandlerSP io_handler_sp(
   new IOHandlerLuaInterpreter(m_debugger, *this, eIOHandlerWatchpoint));
   io_handler_sp->SetUserData(wp_options);


Index: lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
===
--- lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
+++ lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
@@ -6,10 +6,28 @@
 r
 watchpoint set variable val
 watchpoint command add -s lua
-print("val="..tostring(frame:FindVariable("val"):GetValue()))
+print("val=" .. tostring(frame:FindVariable("val"):GetValue()))
 quit
 c
 # CHECK: val=1
 # CHECK: val=2
-# CHECK: val=nil
+# CHECK: Process {{[0-9]+}} exited
+r
+watchpoint set variable val
+watchpoint modify 1 -c "(val == 1)"
+watchpoint command add -s lua
+print("conditional watchpoint")
+wp:SetEnabled(false)
+quit
+c
+# CHECK: conditional watchpoint
+# CHECK-NOT: conditional watchpoint
+# CHECK: Process {{[0-9]+}} exited
+r
+watchpoint set expr 0x00
+watchpoint command add -s lua
+print("never triggers")
+quit
+c
+# CHECK-NOT: never triggers
 # CHECK: Process {{[0-9]+}} exited
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -117,8 +117,8 @@
   io_handler.SetIsDone(true);
 } break;
 case eIOHandlerWatchpoint: {
-  auto *wp_options = static_cast(
-  io_handler.GetUserData());
+  auto *wp_options =
+  static_cast(io_handler.GetUserData());
   m_script_interpreter.SetWatchpointCommandCallback(wp_options,
 data.c_str());
   io_handler.SetIsDone(true);
@@ -304,9 +304,8 @@
   debugger.GetScriptInterpreter(true, eScriptLanguageLua));
   Lua &lua = lua_interpreter->GetLua();
 
-  llvm::Expected BoolOrErr = lua.CallWatchpointCa

[Lldb-commits] [PATCH] D105034: [lldb/lua] Add scripted watchpoints for Lua

2021-06-29 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 355429.
siger-young added a comment.

Fix some linting and add extra tests for Lua scripted watchpoints.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D105034/new/

https://reviews.llvm.org/D105034

Files:
  lldb/bindings/lua/lua-swigsafecast.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
  lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
  lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
  lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
  lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp

Index: lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
===
--- lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
+++ lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
@@ -30,6 +30,11 @@
   return false;
 }
 
+extern "C" llvm::Expected LLDBSwigLuaWatchpointCallbackFunction(
+lua_State *L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp) {
+  return false;
+}
+
 #if _MSC_VER
 #pragma warning (pop)
 #endif
Index: lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
===
--- lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
+++ lldb/test/Shell/ScriptInterpreter/Lua/watchpoint_callback.test
@@ -1,9 +1,33 @@
 # REQUIRES: lua
 # XFAIL: system-netbsd
-# RUN: echo "int main() { return 0; }" | %clang_host -x c - -o %t
+# RUN: echo "int main() { int val = 1; val++; return 0; }" | %clang_host -x c - -g -o %t
 # RUN: %lldb -s %s --script-language lua %t 2>&1 | FileCheck %s
 b main
 r
-watchpoint set expr 0x0
+watchpoint set variable val
 watchpoint command add -s lua
-# CHECK: error: This script interpreter does not support watchpoint callbacks
+print("val=" .. tostring(frame:FindVariable("val"):GetValue()))
+quit
+c
+# CHECK: val=1
+# CHECK: val=2
+# CHECK: Process {{[0-9]+}} exited
+r
+watchpoint set variable val
+watchpoint modify 1 -c "(val == 1)"
+watchpoint command add -s lua
+print("conditional watchpoint")
+wp:SetEnabled(false)
+quit
+c
+# CHECK: conditional watchpoint
+# CHECK-NOT: conditional watchpoint
+# CHECK: Process {{[0-9]+}} exited
+r
+watchpoint set expr 0x00
+watchpoint command add -s lua
+print("never triggers")
+quit
+c
+# CHECK-NOT: never triggers
+# CHECK: Process {{[0-9]+}} exited
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
@@ -9,6 +9,7 @@
 #ifndef liblldb_ScriptInterpreterLua_h_
 #define liblldb_ScriptInterpreterLua_h_
 
+#include "lldb/Breakpoint/WatchpointOptions.h"
 #include "lldb/Core/StructuredDataImpl.h"
 #include "lldb/Interpreter/ScriptInterpreter.h"
 #include "lldb/Utility/Status.h"
@@ -61,6 +62,10 @@
  lldb::user_id_t break_id,
  lldb::user_id_t break_loc_id);
 
+  static bool WatchpointCallbackFunction(void *baton,
+ StoppointCallbackContext *context,
+ lldb::user_id_t watch_id);
+
   // PluginInterface protocol
   lldb_private::ConstString GetPluginName() override;
 
@@ -75,9 +80,16 @@
   std::vector &bp_options_vec,
   CommandReturnObject &result) override;
 
+  void
+  CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
+  CommandReturnObject &result) override;
+
   Status SetBreakpointCommandCallback(BreakpointOptions *bp_options,
   const char *command_body_text) override;
 
+  void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
+const char *command_body_text) override;
+
   Status SetBreakpointCommandCallbackFunction(
   BreakpointOptions *bp_options, const char *function_name,
   StructuredData::ObjectSP extra_args_sp) override;
@@ -89,6 +101,10 @@
   Status RegisterBreakpointCallback(BreakpointOptions *bp_options,
 const char *command_body_text,
 StructuredData::ObjectSP extra_args_sp);
+
+  Status RegisterWatchpointCallback(WatchpointOptions *wp_options,
+const char *command_body_text,
+StructuredData::ObjectSP extra_args_sp);
 };
 
 } // namespace lldb_private
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ lldb/source/Plugins/ScriptInterprete

[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-06-29 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 355439.
siger-young added a comment.

Rebase two commits.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

Files:
  lldb/docs/conf.py
  lldb/docs/index.rst
  lldb/docs/use/lua-reference.rst
  lldb/docs/use/python-reference.rst
  lldb/docs/use/scripting-reference.rst

Index: lldb/docs/use/scripting-reference.rst
===
--- lldb/docs/use/scripting-reference.rst
+++ lldb/docs/use/scripting-reference.rst
@@ -1,23 +1,30 @@
-Python Reference
-
+Scripting Reference
+===
 
-The entire LLDB API is available as Python functions through a script bridging
-interface. This means the LLDB API's can be used directly from python either
-interactively or to build python apps that provide debugger features.
-
-Additionally, Python can be used as a programmatic interface within the lldb
-command interpreter (we refer to this for brevity as the embedded interpreter).
+Python can be used as a programmatic interface within the LLDB command
+interpreter (we refer to this for brevity as the embedded interpreter).
 Of course, in this context it has full access to the LLDB API - with some
 additional conveniences we will call out in the FAQ.
 
+Additionally, the entire LLDB API is available as Python functions through
+a script bridging interface. This means the LLDB API's can be used directly
+from Python either interactively or to build Python apps that provide debugger
+features.
+
+At present, Lua is experimentally available as an embedded interpreter in LLDB.
+Since some scripting features are still unavailable in Lua, there is no language
+switch tab on code blocks of unsupported features. If you want to use Lua
+interpreter, you should ensure that your LLDB is compiled with Lua support.
+See :doc:`Building ` for instructions. 
+
 .. contents::
:local:
 
-Documentation
---
+Built-in Documentation
+--
 
-The LLDB API is contained in a python module named lldb. A useful resource when
-writing Python extensions is the lldb Python classes reference guide.
+The LLDB API is contained in a Python module named ``lldb``. A useful resource
+when writing Python extensions is the `lldb` Python classes reference guide.
 
 The documentation is also accessible in an interactive debugger session with
 the following command:
@@ -54,7 +61,8 @@
   |
...
 
-Or you can get help using any python object, here we use the lldb.process
+
+Or you can get help using any Python object, here we use the lldb.process
 object which is a global variable in the lldb module which represents the
 currently selected process:
 
@@ -74,25 +82,53 @@
   |
...
 
-Embedded Python Interpreter
+Embedded Script Interpreter
 ---
 
-The embedded python interpreter can be accessed in a variety of ways from
-within LLDB. The easiest way is to use the lldb command script with no
-arguments at the lldb command prompt:
+The embedded script interpreter can be accessed in a variety of ways from
+within LLDB. The easiest way is to use the LLDB command ``script`` with no
+arguments at the LLDB command prompt.
+
+By default, the embedded script interpreter is Python. To choose the language
+you prefer, you can specify default language via ``--script-language`` options
+when launching LLDB:
+
+::
+
+   $ lldb --script-language lua
+
+or pass a parameter to LLDB command ``script``.
 
 ::
 
-   (lldb) script
-   Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
-   >>> 2+3
-   5
-   >>> hex(12345)
-   '0x3039'
-   >>>
+   (lldb) script -l lua --
+
+Language features are all available in the embedded script interpreter:
+
+.. tabs::
+   .. code-tab:: python
+
+  (lldb) script
+  Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
+  >>> 2+3
+  5
+  >>> hex(12345)
+  '0x3039'
+  >>>
+
+   .. code-tab:: lua
+  :emphasize-lines: 1
+
+  (lldb) script -l lua --
+  >>> t = { 1, 2, 3 }
+  >>> print(t[1])
+  1
+  >>> print(t[1] + t[2])
+  3
+  >>>
 
-This drops you into the embedded python interpreter. When running under the
-script command, lldb sets some convenience variables that give you quick access
+This drops you into the embedded script interpreter. When running under the
+script command, LLDB sets some convenience variables that give you quick access
 to the currently selected entities that characterize the program and debugger
 state. In each case, if there is no currently selected entity of the
 appropriate type, the variable's IsValid method will return false. These
@@ -138,60 +174,84 @@
 
 Moreover, they are only defined and meaningful while in the interactive Python
 interpreter. There is no guarantee on their value in any other situation, hence
-you should not use them when 

[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-07-30 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 362990.
siger-young added a comment.

Add integrated pages (Lua + Python) for scripting docs.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

Files:
  lldb/docs/conf.py
  lldb/docs/index.rst
  lldb/docs/use/python-reference.rst
  lldb/docs/use/python.rst
  lldb/docs/use/scripting-example.rst
  lldb/docs/use/scripting-reference.rst

Index: lldb/docs/use/scripting-reference.rst
===
--- lldb/docs/use/scripting-reference.rst
+++ lldb/docs/use/scripting-reference.rst
@@ -1,23 +1,30 @@
-Python Reference
-
+Scripting Reference
+===
 
-The entire LLDB API is available as Python functions through a script bridging
-interface. This means the LLDB API's can be used directly from python either
-interactively or to build python apps that provide debugger features.
-
-Additionally, Python can be used as a programmatic interface within the lldb
-command interpreter (we refer to this for brevity as the embedded interpreter).
+Python can be used as a programmatic interface within the LLDB command
+interpreter (we refer to this for brevity as the embedded interpreter).
 Of course, in this context it has full access to the LLDB API - with some
 additional conveniences we will call out in the FAQ.
 
+Additionally, the entire LLDB API is available as Python functions through
+a script bridging interface. This means the LLDB API's can be used directly
+from Python either interactively or to build Python apps that provide debugger
+features.
+
+At present, Lua is experimentally available as an embedded interpreter in LLDB.
+Since some scripting features are still unavailable in Lua, there is no language
+switch tab on code blocks of unsupported features. If you want to use Lua
+interpreter, you should ensure that your LLDB is compiled with Lua support.
+See :doc:`Building ` for instructions. 
+
 .. contents::
:local:
 
-Documentation
---
+Built-in Documentation
+--
 
-The LLDB API is contained in a python module named lldb. A useful resource when
-writing Python extensions is the lldb Python classes reference guide.
+The LLDB API is contained in a Python module named ``lldb``. A useful resource
+when writing Python extensions is the `lldb` Python classes reference guide.
 
 The documentation is also accessible in an interactive debugger session with
 the following command:
@@ -54,7 +61,8 @@
   |
...
 
-Or you can get help using any python object, here we use the lldb.process
+
+Or you can get help using any Python object, here we use the lldb.process
 object which is a global variable in the lldb module which represents the
 currently selected process:
 
@@ -74,25 +82,53 @@
   |
...
 
-Embedded Python Interpreter
+Embedded Script Interpreter
 ---
 
-The embedded python interpreter can be accessed in a variety of ways from
-within LLDB. The easiest way is to use the lldb command script with no
-arguments at the lldb command prompt:
+The embedded script interpreter can be accessed in a variety of ways from
+within LLDB. The easiest way is to use the LLDB command ``script`` with no
+arguments at the LLDB command prompt.
+
+By default, the embedded script interpreter is Python. To choose the language
+you prefer, you can specify default language via ``--script-language`` options
+when launching LLDB:
+
+::
+
+   $ lldb --script-language lua
+
+or pass a parameter to LLDB command ``script``.
 
 ::
 
-   (lldb) script
-   Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
-   >>> 2+3
-   5
-   >>> hex(12345)
-   '0x3039'
-   >>>
+   (lldb) script -l lua --
+
+Language features are all available in the embedded script interpreter:
+
+.. tabs::
+   .. code-tab:: python
+
+  (lldb) script
+  Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
+  >>> 2+3
+  5
+  >>> hex(12345)
+  '0x3039'
+  >>>
+
+   .. code-tab:: lua
+  :emphasize-lines: 1
+
+  (lldb) script -l lua --
+  >>> t = { 1, 2, 3 }
+  >>> print(t[1])
+  1
+  >>> print(t[1] + t[2])
+  3
+  >>>
 
-This drops you into the embedded python interpreter. When running under the
-script command, lldb sets some convenience variables that give you quick access
+This drops you into the embedded script interpreter. When running under the
+script command, LLDB sets some convenience variables that give you quick access
 to the currently selected entities that characterize the program and debugger
 state. In each case, if there is no currently selected entity of the
 appropriate type, the variable's IsValid method will return false. These
@@ -138,60 +174,84 @@
 
 Moreover, they are only defined and meaningful while in the interactive Python
 interpreter. There is no guarantee on their

[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-08-15 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 366494.
siger-young added a comment.

This update marks features that are only supported by Python, and adds Lua
example code for using LLDB module.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

Files:
  lldb/docs/conf.py
  lldb/docs/index.rst
  lldb/docs/use/python-reference.rst
  lldb/docs/use/python.rst
  lldb/docs/use/scripting-example.rst
  lldb/docs/use/scripting-reference.rst

Index: lldb/docs/use/scripting-reference.rst
===
--- lldb/docs/use/scripting-reference.rst
+++ lldb/docs/use/scripting-reference.rst
@@ -1,23 +1,32 @@
-Python Reference
-
+Scripting Reference
+===
 
-The entire LLDB API is available as Python functions through a script bridging
-interface. This means the LLDB API's can be used directly from python either
-interactively or to build python apps that provide debugger features.
-
-Additionally, Python can be used as a programmatic interface within the lldb
-command interpreter (we refer to this for brevity as the embedded interpreter).
+Python can be used as a programmatic interface within the LLDB command
+interpreter (we refer to this for brevity as the embedded interpreter).
 Of course, in this context it has full access to the LLDB API - with some
 additional conveniences we will call out in the FAQ.
 
+Additionally, the entire LLDB API is available as Python functions through
+a script bridging interface. This means the LLDB API's can be used directly
+from Python either interactively or to build Python apps that provide debugger
+features.
+
+At present, Lua is experimentally available as an embedded interpreter in LLDB.
+Moreover, LLDB API is also visible to standalone Lua. Since some scripting
+features are still unavailable in Lua, there is no language switch tab on code
+blocks of unsupported features.
+
+If you want to use Lua interpreter, you should ensure that your LLDB is compiled
+with Lua support. See :doc:`Building ` for instructions. 
+
 .. contents::
:local:
 
-Documentation
---
+Built-in Documentation
+--
 
-The LLDB API is contained in a python module named lldb. A useful resource when
-writing Python extensions is the lldb Python classes reference guide.
+The LLDB API is contained in a Python module named ``lldb``. A useful resource
+when writing Python extensions is the `lldb` Python classes reference guide.
 
 The documentation is also accessible in an interactive debugger session with
 the following command:
@@ -54,7 +63,8 @@
   |
...
 
-Or you can get help using any python object, here we use the lldb.process
+
+Or you can get help using any Python object, here we use the lldb.process
 object which is a global variable in the lldb module which represents the
 currently selected process:
 
@@ -74,25 +84,53 @@
   |
...
 
-Embedded Python Interpreter
+Embedded Script Interpreter
 ---
 
-The embedded python interpreter can be accessed in a variety of ways from
-within LLDB. The easiest way is to use the lldb command script with no
-arguments at the lldb command prompt:
+The embedded script interpreter can be accessed in a variety of ways from
+within LLDB. The easiest way is to use the LLDB command ``script`` with no
+arguments at the LLDB command prompt.
+
+By default, the embedded script interpreter is Python. To choose the language
+you prefer, you can specify default language via ``--script-language`` options
+when launching LLDB:
+
+::
+
+   $ lldb --script-language lua
+
+or pass a parameter to LLDB command ``script``.
 
 ::
 
-   (lldb) script
-   Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
-   >>> 2+3
-   5
-   >>> hex(12345)
-   '0x3039'
-   >>>
+   (lldb) script -l lua --
+
+Language features are all available in the embedded script interpreter:
+
+.. tabs::
+   .. code-tab:: python
+
+  (lldb) script
+  Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
+  >>> 2+3
+  5
+  >>> hex(12345)
+  '0x3039'
+  >>>
+
+   .. code-tab:: lua
+  :emphasize-lines: 1
 
-This drops you into the embedded python interpreter. When running under the
-script command, lldb sets some convenience variables that give you quick access
+  (lldb) script -l lua --
+  >>> t = { 1, 2, 3 }
+  >>> print(t[1])
+  1
+  >>> print(t[1] + t[2])
+  3
+  >>>
+
+This drops you into the embedded script interpreter. When running under the
+script command, LLDB sets some convenience variables that give you quick access
 to the currently selected entities that characterize the program and debugger
 state. In each case, if there is no currently selected entity of the
 appropriate type, the variable's IsValid method will return false. These
@@ -138,60 +176,84 @@
 
 Moreover, t

[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement typemaps for Lua bindings

2021-08-15 Thread Siger Young via Phabricator via lldb-commits
siger-young created this revision.
Herald added a subscriber: mgorny.
siger-young requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Add necessary typemaps for Lua bindings.

Signed-off-by: Siger Yang 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108090

Files:
  lldb/CMakeLists.txt
  lldb/bindings/lua/CMakeLists.txt
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/bindings/lua/lua.swig
  lldb/source/API/liblldb-private.exports
  lldb/source/API/liblldb.exports

Index: lldb/source/API/liblldb.exports
===
--- lldb/source/API/liblldb.exports
+++ lldb/source/API/liblldb.exports
@@ -2,3 +2,4 @@
 _ZNK4lldb*
 init_lld*
 PyInit__lldb*
+luaopen_lldb*
Index: lldb/source/API/liblldb-private.exports
===
--- lldb/source/API/liblldb-private.exports
+++ lldb/source/API/liblldb-private.exports
@@ -4,3 +4,4 @@
 _ZNK12lldb_private*
 init_lld*
 PyInit__lldb*
+luaopen_lldb*
Index: lldb/bindings/lua/lua.swig
===
--- lldb/bindings/lua/lua.swig
+++ lldb/bindings/lua/lua.swig
@@ -17,6 +17,10 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "../bindings/lua/lua-swigsafecast.swig"
+
+// required headers for typemaps
+#include "lldb/Host/File.h"
+
 using namespace lldb_private;
 using namespace lldb;
 %}
Index: lldb/bindings/lua/lua-wrapper.swig
===
--- lldb/bindings/lua/lua-wrapper.swig
+++ lldb/bindings/lua/lua-wrapper.swig
@@ -6,6 +6,19 @@
 
 %}
 
+%runtime %{
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton);
+int LLDBSwigLuaCloseFileHandle(lua_State *L);
+
+#ifdef __cplusplus
+}
+#endif
+%}
+
 %wrapper %{
 
 // This function is called from Lua::CallBreakpointCallback
@@ -88,5 +101,20 @@
return stop;
 }
 
+SWIGEXPORT void
+LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) {
+   lua_State *L = (lua_State *)baton;
+
+   lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
+   lua_gettable(L, LUA_REGISTRYINDEX);
+
+   // FIXME: There's no way to report errors back to the user
+   lua_pushstring(L, str);
+   lua_pcall(L, 1, 0, 0);
+}
+
+int LLDBSwigLuaCloseFileHandle(lua_State *L) {
+   return luaL_error(L, "You cannot close a file handle used by lldb.");
+}
 
 %}
Index: lldb/bindings/lua/lua-typemaps.swig
===
--- lldb/bindings/lua/lua-typemaps.swig
+++ lldb/bindings/lua/lua-typemaps.swig
@@ -12,7 +12,7 @@
 
 // Primitive integer mapping
 %typemap(in,checkfn="lua_isinteger") TYPE
-%{ $1 = (TYPE)lua_tointeger(L, $input); %}
+%{ $1 = ($type)lua_tointeger(L, $input); %}
 %typemap(in,checkfn="lua_isinteger") const TYPE&($basetype temp)
 %{ temp=($basetype)lua_tointeger(L,$input); $1=&temp;%}
 %typemap(out) TYPE
@@ -54,6 +54,7 @@
 LLDB_NUMBER_TYPEMAP(long long);
 LLDB_NUMBER_TYPEMAP(unsigned long long);
 LLDB_NUMBER_TYPEMAP(signed long long);
+LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
 
 %apply unsigned long { size_t };
 %apply const unsigned long & { const size_t & };
@@ -77,7 +78,7 @@
 %typemap(in) (char *dst, size_t dst_len) {
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
-   return luaL_error(L, "Positive integer expected");
+  return luaL_error(L, "Positive integer expected");
}
$1 = (char *) malloc($2);
 }
@@ -86,6 +87,9 @@
 // as char data instead of byte data.
 %typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
 
+// Also SBProcess::ReadMemory.
+%typemap(in) (void *buf, size_t size) = (char *dst, size_t dst_len);
+
 // Return the char buffer.  Discarding any previous return result
 %typemap(argout) (char *dst, size_t dst_len) {
lua_pop(L, 1); // Blow away the previous result
@@ -102,4 +106,178 @@
 // as char data instead of byte data.
 %typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
 
+// Also SBProcess::ReadMemory.
+%typemap(argout) (void *buf, size_t size) = (char *dst, size_t dst_len);
+
 //===--===//
+
+// Typemap for handling a snprintf-like API like SBThread::GetStopDescription.
+
+%typemap(in) (char *dst_or_null, size_t dst_len) {
+   $2 = luaL_checkinteger(L, $input);
+   if ($2 <= 0) {
+  return luaL_error(L, "Positive integer expected");
+   }
+   $1 = (char *)malloc($2);
+}
+
+%typemap(argout) (char *dst_or_null, size_t dst_len) {
+   lua_pop(L, 1); // Blow away the previous result
+   lua_pushlstring(L, (const char *)$1, $result);
+   free($1);
+   // SWIG_arg was already incremented
+}
+
+//===--===//
+
+// Typemap for handling SBModule:

[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement typemaps for Lua bindings

2021-08-15 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 366520.
siger-young added a comment.

Using `lua_newuserdata` instead of `lua_newuserdatauv` to support Lua 5.3


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

Files:
  lldb/CMakeLists.txt
  lldb/bindings/lua/CMakeLists.txt
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/bindings/lua/lua.swig
  lldb/source/API/liblldb-private.exports
  lldb/source/API/liblldb.exports

Index: lldb/source/API/liblldb.exports
===
--- lldb/source/API/liblldb.exports
+++ lldb/source/API/liblldb.exports
@@ -2,3 +2,4 @@
 _ZNK4lldb*
 init_lld*
 PyInit__lldb*
+luaopen_lldb*
Index: lldb/source/API/liblldb-private.exports
===
--- lldb/source/API/liblldb-private.exports
+++ lldb/source/API/liblldb-private.exports
@@ -4,3 +4,4 @@
 _ZNK12lldb_private*
 init_lld*
 PyInit__lldb*
+luaopen_lldb*
Index: lldb/bindings/lua/lua.swig
===
--- lldb/bindings/lua/lua.swig
+++ lldb/bindings/lua/lua.swig
@@ -17,6 +17,10 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "../bindings/lua/lua-swigsafecast.swig"
+
+// required headers for typemaps
+#include "lldb/Host/File.h"
+
 using namespace lldb_private;
 using namespace lldb;
 %}
Index: lldb/bindings/lua/lua-wrapper.swig
===
--- lldb/bindings/lua/lua-wrapper.swig
+++ lldb/bindings/lua/lua-wrapper.swig
@@ -6,6 +6,19 @@
 
 %}
 
+%runtime %{
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton);
+int LLDBSwigLuaCloseFileHandle(lua_State *L);
+
+#ifdef __cplusplus
+}
+#endif
+%}
+
 %wrapper %{
 
 // This function is called from Lua::CallBreakpointCallback
@@ -88,5 +101,20 @@
return stop;
 }
 
+SWIGEXPORT void
+LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) {
+   lua_State *L = (lua_State *)baton;
+
+   lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
+   lua_gettable(L, LUA_REGISTRYINDEX);
+
+   // FIXME: There's no way to report errors back to the user
+   lua_pushstring(L, str);
+   lua_pcall(L, 1, 0, 0);
+}
+
+int LLDBSwigLuaCloseFileHandle(lua_State *L) {
+   return luaL_error(L, "You cannot close a file handle used by lldb.");
+}
 
 %}
Index: lldb/bindings/lua/lua-typemaps.swig
===
--- lldb/bindings/lua/lua-typemaps.swig
+++ lldb/bindings/lua/lua-typemaps.swig
@@ -12,7 +12,7 @@
 
 // Primitive integer mapping
 %typemap(in,checkfn="lua_isinteger") TYPE
-%{ $1 = (TYPE)lua_tointeger(L, $input); %}
+%{ $1 = ($type)lua_tointeger(L, $input); %}
 %typemap(in,checkfn="lua_isinteger") const TYPE&($basetype temp)
 %{ temp=($basetype)lua_tointeger(L,$input); $1=&temp;%}
 %typemap(out) TYPE
@@ -54,6 +54,7 @@
 LLDB_NUMBER_TYPEMAP(long long);
 LLDB_NUMBER_TYPEMAP(unsigned long long);
 LLDB_NUMBER_TYPEMAP(signed long long);
+LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
 
 %apply unsigned long { size_t };
 %apply const unsigned long & { const size_t & };
@@ -77,7 +78,7 @@
 %typemap(in) (char *dst, size_t dst_len) {
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
-   return luaL_error(L, "Positive integer expected");
+  return luaL_error(L, "Positive integer expected");
}
$1 = (char *) malloc($2);
 }
@@ -86,6 +87,9 @@
 // as char data instead of byte data.
 %typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
 
+// Also SBProcess::ReadMemory.
+%typemap(in) (void *buf, size_t size) = (char *dst, size_t dst_len);
+
 // Return the char buffer.  Discarding any previous return result
 %typemap(argout) (char *dst, size_t dst_len) {
lua_pop(L, 1); // Blow away the previous result
@@ -102,4 +106,178 @@
 // as char data instead of byte data.
 %typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
 
+// Also SBProcess::ReadMemory.
+%typemap(argout) (void *buf, size_t size) = (char *dst, size_t dst_len);
+
 //===--===//
+
+// Typemap for handling a snprintf-like API like SBThread::GetStopDescription.
+
+%typemap(in) (char *dst_or_null, size_t dst_len) {
+   $2 = luaL_checkinteger(L, $input);
+   if ($2 <= 0) {
+  return luaL_error(L, "Positive integer expected");
+   }
+   $1 = (char *)malloc($2);
+}
+
+%typemap(argout) (char *dst_or_null, size_t dst_len) {
+   lua_pop(L, 1); // Blow away the previous result
+   lua_pushlstring(L, (const char *)$1, $result);
+   free($1);
+   // SWIG_arg was already incremented
+}
+
+//===--===//
+
+// Typemap for handling SBModule::GetVersion
+
+%typemap(in) (uint32_t

[Lldb-commits] [PATCH] D108090: [lldb/lua] Supplement Lua bindings for lldb module

2021-08-21 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 367971.
siger-young added a comment.

This update adds some tests for Lua LLDB module.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108090/new/

https://reviews.llvm.org/D108090

Files:
  lldb/CMakeLists.txt
  lldb/bindings/lua/CMakeLists.txt
  lldb/bindings/lua/lua-typemaps.swig
  lldb/bindings/lua/lua-wrapper.swig
  lldb/bindings/lua/lua.swig
  lldb/source/API/liblldb-private.exports
  lldb/source/API/liblldb.exports
  lldb/test/API/lit.site.cfg.py.in
  lldb/test/API/lldbtest.py
  lldb/test/API/lua_api/Makefile
  lldb/test/API/lua_api/TestBreakpointAPI.lua
  lldb/test/API/lua_api/TestComprehensive.lua
  lldb/test/API/lua_api/TestFileHandle.lua
  lldb/test/API/lua_api/TestLuaAPI.py
  lldb/test/API/lua_api/TestProcessAPI.lua
  lldb/test/API/lua_api/lua_lldb_test.lua
  lldb/test/API/lua_api/main.c

Index: lldb/test/API/lua_api/main.c
===
--- /dev/null
+++ lldb/test/API/lua_api/main.c
@@ -0,0 +1,35 @@
+#include 
+
+void BFunction()
+{
+}
+
+void AFunction()
+{
+printf("I am a function.\n");
+}
+
+int main(int argc, const char *argv[])
+{
+int inited = 0xDEADBEEF;
+int sum = 0;
+if(argc > 1)
+{
+for(int i = 0; i < argc; i++)
+{
+puts(argv[i]);
+}
+if(argc > 2)
+{
+return argc;
+}
+}
+AFunction();
+for(int i = 1; i <= 100; i++)
+{
+BFunction();
+sum += i;
+}
+printf("sum = %d\n", sum);
+return 0;
+}
Index: lldb/test/API/lua_api/lua_lldb_test.lua
===
--- /dev/null
+++ lldb/test/API/lua_api/lua_lldb_test.lua
@@ -0,0 +1,107 @@
+-- Import all functions of luaunit
+EXPORT_ASSERT_TO_GLOBALS = true
+require('luaunit')
+
+-- Make lldb available in global
+lldb = require('lldb')
+
+-- Global helper functions
+function read_file_non_empty_lines(f)
+local lines = {}
+while true do
+local line = f:read('*l')
+if not line then break end
+if line ~= '\n' then table.insert(lines, line) end
+end
+return lines
+end
+
+function split_lines(str)
+local lines = {}
+for line in str:gmatch("[^\r\n]+") do
+table.insert(lines, line)
+end
+return lines
+end
+
+function get_stopped_threads(process, reason)
+local threads = {}
+for i = 0, process:GetNumThreads() - 1 do
+local t = process:GetThreadAtIndex(i)
+if t:IsValid() and t:GetStopReason() == reason then
+table.insert(threads, t)
+end
+end
+return threads
+end
+
+function get_stopped_thread(process, reason)
+local threads = get_stopped_threads(process, reason)
+if #threads ~= 0 then return threads[1]
+else return nil end
+end
+
+-- Test helper
+
+local _M = {}
+local _m = {}
+
+local _mt = { __index = _m }
+
+function _M.create_test(name, exe, output, input)
+print('[lldb/lua] Doing test ' .. name)
+exe = exe or os.getenv('TEST_EXE')
+output = output or os.getenv('TEST_OUTPUT')
+input = input or os.getenv('TEST_INPUT')
+lldb.SBDebugger.Initialize()
+local debugger = lldb.SBDebugger.Create()
+-- Ensure that debugger is created
+assertNotNil(debugger)
+assertTrue(debugger:IsValid())
+
+debugger:SetAsync(false)
+
+local lua_language = debugger:GetScriptingLanguage('lua')
+assertNotNil(lua_language)
+debugger:SetScriptLanguage(lua_language)
+
+local test = setmetatable({
+output = output,
+input = input,
+name = name,
+exe = exe,
+debugger = debugger
+}, _mt)
+_G[name] = test
+return test
+end
+
+function _m:create_target(exe)
+local target
+if not exe then exe = self.exe end
+target = self.debugger:CreateTarget(exe)
+-- Ensure that target is created
+assertNotNil(target)
+assertTrue(target:IsValid())
+return target
+end
+
+function _m:handle_command(command, collect)
+if collect == nil then collect = true end
+if collect then
+local ret = lldb.SBCommandReturnObject()
+local interpreter = self.debugger:GetCommandInterpreter()
+assertTrue(interpreter:IsValid())
+interpreter:HandleCommand(command, ret)
+self.debugger:GetOutputFile():Flush()
+self.debugger:GetErrorFile():Flush()
+assertTrue(ret:Succeeded())
+return ret:GetOutput()
+else
+self.debugger:HandleCommand(command)
+self.debugger:GetOutputFile():Flush()
+self.debugger:GetErrorFile():Flush()
+end
+end
+
+return _M
Index: lldb/test/API/lua_api/TestProcessAPI.lua
===
--- /dev/null
+++ lldb/test/API/lua_api/TestProcessAPI.lua
@@ -0,0 +1,59 @@
+_T = require('lua_lldb_test').create_test('TestProcessAPI')
+
+function _T:TestProcessLaunchSimple()
+local target 

[Lldb-commits] [PATCH] D108515: [lldb/lua] Force Lua version to be 5.3

2021-08-21 Thread Siger Young via Phabricator via lldb-commits
siger-young created this revision.
siger-young added reviewers: tammela, JDevlieghere.
Herald added a subscriber: mgorny.
siger-young requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Due to CMake cache, find_package in FindLuaAndSwig.cmake
will be ignored. This commit adds EXACT and REQUIRED flags
to it and removes find_package in Lua ScriptInterpreter.

Signed-off-by: Siger Yang 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108515

Files:
  lldb/cmake/modules/FindLuaAndSwig.cmake
  lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt


Index: lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
+++ lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
@@ -1,5 +1,3 @@
-find_package(Lua REQUIRED)
-
 add_lldb_library(lldbPluginScriptInterpreterLua PLUGIN
   Lua.cpp
   ScriptInterpreterLua.cpp
Index: lldb/cmake/modules/FindLuaAndSwig.cmake
===
--- lldb/cmake/modules/FindLuaAndSwig.cmake
+++ lldb/cmake/modules/FindLuaAndSwig.cmake
@@ -9,7 +9,7 @@
 else()
   find_package(SWIG 3.0)
   if (SWIG_FOUND)
-find_package(Lua 5.3)
+find_package(Lua 5.3 EXACT REQUIRED)
 if(LUA_FOUND AND SWIG_FOUND)
   mark_as_advanced(
 LUA_LIBRARIES


Index: lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
===
--- lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
+++ lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
@@ -1,5 +1,3 @@
-find_package(Lua REQUIRED)
-
 add_lldb_library(lldbPluginScriptInterpreterLua PLUGIN
   Lua.cpp
   ScriptInterpreterLua.cpp
Index: lldb/cmake/modules/FindLuaAndSwig.cmake
===
--- lldb/cmake/modules/FindLuaAndSwig.cmake
+++ lldb/cmake/modules/FindLuaAndSwig.cmake
@@ -9,7 +9,7 @@
 else()
   find_package(SWIG 3.0)
   if (SWIG_FOUND)
-find_package(Lua 5.3)
+find_package(Lua 5.3 EXACT REQUIRED)
 if(LUA_FOUND AND SWIG_FOUND)
   mark_as_advanced(
 LUA_LIBRARIES
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-08-22 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 368028.
siger-young added a comment.

This update makes scripting example more coherent even if Lua parts are added.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

Files:
  lldb/docs/conf.py
  lldb/docs/index.rst
  lldb/docs/use/python-reference.rst
  lldb/docs/use/python.rst
  lldb/docs/use/scripting-example.rst
  lldb/docs/use/scripting-reference.rst

Index: lldb/docs/use/scripting-reference.rst
===
--- lldb/docs/use/scripting-reference.rst
+++ lldb/docs/use/scripting-reference.rst
@@ -1,23 +1,32 @@
-Python Reference
-
+Scripting Reference
+===
 
-The entire LLDB API is available as Python functions through a script bridging
-interface. This means the LLDB API's can be used directly from python either
-interactively or to build python apps that provide debugger features.
-
-Additionally, Python can be used as a programmatic interface within the lldb
-command interpreter (we refer to this for brevity as the embedded interpreter).
+Python can be used as a programmatic interface within the LLDB command
+interpreter (we refer to this for brevity as the embedded interpreter).
 Of course, in this context it has full access to the LLDB API - with some
 additional conveniences we will call out in the FAQ.
 
+Additionally, the entire LLDB API is available as Python functions through
+a script bridging interface. This means the LLDB API's can be used directly
+from Python either interactively or to build Python apps that provide debugger
+features.
+
+At present, Lua is experimentally available as an embedded interpreter in LLDB.
+Moreover, LLDB API is also visible to standalone Lua. Since some scripting
+features are still unavailable in Lua, there is no language switch tab on code
+blocks of unsupported features.
+
+If you want to use Lua interpreter, you should ensure that your LLDB is compiled
+with Lua support. See :doc:`Building ` for instructions. 
+
 .. contents::
:local:
 
-Documentation
---
+Built-in Documentation
+--
 
-The LLDB API is contained in a python module named lldb. A useful resource when
-writing Python extensions is the lldb Python classes reference guide.
+The LLDB API is contained in a Python module named ``lldb``. A useful resource
+when writing Python extensions is the `lldb` Python classes reference guide.
 
 The documentation is also accessible in an interactive debugger session with
 the following command:
@@ -54,7 +63,8 @@
   |
...
 
-Or you can get help using any python object, here we use the lldb.process
+
+Or you can get help using any Python object, here we use the lldb.process
 object which is a global variable in the lldb module which represents the
 currently selected process:
 
@@ -74,25 +84,53 @@
   |
...
 
-Embedded Python Interpreter
+Embedded Script Interpreter
 ---
 
-The embedded python interpreter can be accessed in a variety of ways from
-within LLDB. The easiest way is to use the lldb command script with no
-arguments at the lldb command prompt:
+The embedded script interpreter can be accessed in a variety of ways from
+within LLDB. The easiest way is to use the LLDB command ``script`` with no
+arguments at the LLDB command prompt.
+
+By default, the embedded script interpreter is Python. To choose the language
+you prefer, you can specify default language via ``--script-language`` options
+when launching LLDB:
+
+::
+
+   $ lldb --script-language lua
+
+or pass a parameter to LLDB command ``script``.
 
 ::
 
-   (lldb) script
-   Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
-   >>> 2+3
-   5
-   >>> hex(12345)
-   '0x3039'
-   >>>
+   (lldb) script -l lua --
+
+Language features are all available in the embedded script interpreter:
+
+.. tabs::
+   .. code-tab:: python
+
+  (lldb) script
+  Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
+  >>> 2+3
+  5
+  >>> hex(12345)
+  '0x3039'
+  >>>
+
+   .. code-tab:: lua
+  :emphasize-lines: 1
 
-This drops you into the embedded python interpreter. When running under the
-script command, lldb sets some convenience variables that give you quick access
+  (lldb) script -l lua --
+  >>> t = { 1, 2, 3 }
+  >>> print(t[1])
+  1
+  >>> print(t[1] + t[2])
+  3
+  >>>
+
+This drops you into the embedded script interpreter. When running under the
+script command, LLDB sets some convenience variables that give you quick access
 to the currently selected entities that characterize the program and debugger
 state. In each case, if there is no currently selected entity of the
 appropriate type, the variable's IsValid method will return false. These
@@ -138,60 +176,84 @@
 
 Moreover, they are only defined and meaning

[Lldb-commits] [PATCH] D104281: [lldb][docs] Add reference docs for Lua scripting

2021-08-22 Thread Siger Young via Phabricator via lldb-commits
siger-young updated this revision to Diff 368029.
siger-young added a comment.

This update improves the scripting reference parts.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104281/new/

https://reviews.llvm.org/D104281

Files:
  lldb/docs/conf.py
  lldb/docs/index.rst
  lldb/docs/use/python-reference.rst
  lldb/docs/use/python.rst
  lldb/docs/use/scripting-example.rst
  lldb/docs/use/scripting-reference.rst

Index: lldb/docs/use/scripting-reference.rst
===
--- lldb/docs/use/scripting-reference.rst
+++ lldb/docs/use/scripting-reference.rst
@@ -1,23 +1,32 @@
-Python Reference
-
+Scripting Reference
+===
 
-The entire LLDB API is available as Python functions through a script bridging
-interface. This means the LLDB API's can be used directly from python either
-interactively or to build python apps that provide debugger features.
-
-Additionally, Python can be used as a programmatic interface within the lldb
-command interpreter (we refer to this for brevity as the embedded interpreter).
+Python can be used as a programmatic interface within the LLDB command
+interpreter (we refer to this for brevity as the embedded interpreter).
 Of course, in this context it has full access to the LLDB API - with some
 additional conveniences we will call out in the FAQ.
 
+Additionally, the entire LLDB API is available as Python functions through
+a script bridging interface. This means the LLDB API's can be used directly
+from Python either interactively or to build Python apps that provide debugger
+features.
+
+At present, Lua is experimentally available as an embedded interpreter in LLDB.
+Moreover, LLDB API is also visible to standalone Lua. Since some scripting
+features are still unavailable in Lua, there is no language switch tab on code
+blocks of unsupported features.
+
+If you want to use Lua interpreter, you should ensure that your LLDB is compiled
+with Lua support. See :doc:`Building ` for instructions. 
+
 .. contents::
:local:
 
-Documentation
---
+Built-in Documentation
+--
 
-The LLDB API is contained in a python module named lldb. A useful resource when
-writing Python extensions is the lldb Python classes reference guide.
+The LLDB API is contained in a Python module named ``lldb``. A useful resource
+when writing Python extensions is the `lldb` Python classes reference guide.
 
 The documentation is also accessible in an interactive debugger session with
 the following command:
@@ -54,7 +63,8 @@
   |
...
 
-Or you can get help using any python object, here we use the lldb.process
+
+Or you can get help using any Python object, here we use the lldb.process
 object which is a global variable in the lldb module which represents the
 currently selected process:
 
@@ -74,25 +84,53 @@
   |
...
 
-Embedded Python Interpreter
+Embedded Script Interpreter
 ---
 
-The embedded python interpreter can be accessed in a variety of ways from
-within LLDB. The easiest way is to use the lldb command script with no
-arguments at the lldb command prompt:
+The embedded script interpreter can be accessed in a variety of ways from
+within LLDB. The easiest way is to use the LLDB command ``script`` with no
+arguments at the LLDB command prompt.
+
+By default, the embedded script interpreter is Python. To choose the language
+you prefer (e.g. Lua), you can specify default language via
+``--script-language`` options when launching LLDB:
+
+::
+
+   $ lldb --script-language lua
+
+or pass a parameter to LLDB command ``script``.
 
 ::
 
-   (lldb) script
-   Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
-   >>> 2+3
-   5
-   >>> hex(12345)
-   '0x3039'
-   >>>
+   (lldb) script -l lua --
+
+Language features are all available in the embedded script interpreter:
+
+.. tabs::
+   .. code-tab:: python
+
+  (lldb) script
+  Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
+  >>> 2+3
+  5
+  >>> hex(12345)
+  '0x3039'
+  >>>
+
+   .. code-tab:: lua
+  :emphasize-lines: 1
 
-This drops you into the embedded python interpreter. When running under the
-script command, lldb sets some convenience variables that give you quick access
+  (lldb) script -l lua --
+  >>> t = { 1, 2, 3 }
+  >>> print(t[1])
+  1
+  >>> print(t[1] + t[2])
+  3
+  >>>
+
+This drops you into the embedded script interpreter. When running under the
+script command, LLDB sets some convenience variables that give you quick access
 to the currently selected entities that characterize the program and debugger
 state. In each case, if there is no currently selected entity of the
 appropriate type, the variable's IsValid method will return false. These
@@ -136,62 +174,86 @@
 on entry to the embedded interpreter. They do not update as y

[Lldb-commits] [PATCH] D108515: [lldb/lua] Force Lua version to be 5.3

2021-09-03 Thread Siger Young via Phabricator via lldb-commits
siger-young added inline comments.



Comment at: lldb/cmake/modules/FindLuaAndSwig.cmake:12
   if (SWIG_FOUND)
-find_package(Lua 5.3)
+find_package(Lua 5.3 EXACT REQUIRED)
 if(LUA_FOUND AND SWIG_FOUND)

mstorsjo wrote:
> This breaks building in setups where SWIG is available, but not Lua. 
> Previously this detected Lua and took it into use if both Lua and SWIG were 
> available, and if not , proceeded without them.
I think removing the "REQUIRED" flags might work. I will revert the broken 
commit first.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108515/new/

https://reviews.llvm.org/D108515

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