diff --git a/lldb/bindings/headers.swig b/lldb/bindings/headers.swig index 3c2cd85f504da91d4373a47d71e7c1505308b738..fe1bcd3e4cc663dbdba29d14bfba25e154b0fddc 100644 --- a/lldb/bindings/headers.swig +++ b/lldb/bindings/headers.swig @@ -41,6 +41,7 @@ #include "lldb/API/SBListener.h" #include "lldb/API/SBMemoryRegionInfo.h" #include "lldb/API/SBMemoryRegionInfoList.h" +#include "lldb/API/SBMixedArkTSDebugger.h" /* OHOS_LOCAL */ #include "lldb/API/SBModule.h" #include "lldb/API/SBModuleSpec.h" #include "lldb/API/SBPlatform.h" diff --git a/lldb/bindings/interface/SBMixedArkTSDebugger.i b/lldb/bindings/interface/SBMixedArkTSDebugger.i new file mode 100644 index 0000000000000000000000000000000000000000..11bb629f7eaf18ca5ae4174a7a6951680cbed465 --- /dev/null +++ b/lldb/bindings/interface/SBMixedArkTSDebugger.i @@ -0,0 +1,40 @@ +//===-- SWIG Interface for SBMixedArkTSDebugger -----------------*- C++ -*-===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +namespace lldb { + +%feature("docstring", +"Represents py:class:`SBMixedArkTSDebugger`. + +SBMixedArkTSDebugger supports OHOS Applications to get ArkTS debug information after +calling napi and during debugging C/C++. + +This class should not be used when ArkTS runtime running. If used, unexpected issue +may occur." +) SBMixedArkTSDebugger; +class SBMixedArkTSDebugger { +public: + SBMixedArkTSDebugger(); + + SBMixedArkTSDebugger(const lldb::SBTarget &rhs); + + SBMixedArkTSDebugger(const lldb::TargetSP &taraget_sp); + + lldb::SBData GetBackTrace(SBError &er); +}; + +} // namespace lldb diff --git a/lldb/bindings/interfaces.swig b/lldb/bindings/interfaces.swig index c9a6d0f06056864281b42ac9511de00bf7a43c29..6fa25dd8dcaa67ba59100fe4a26382ff70145fba 100644 --- a/lldb/bindings/interfaces.swig +++ b/lldb/bindings/interfaces.swig @@ -48,6 +48,7 @@ %include "./interface/SBListener.i" %include "./interface/SBMemoryRegionInfo.i" %include "./interface/SBMemoryRegionInfoList.i" +%include "./interface/SBMixedArkTSDebugger.i" /* OHOS_LOCAL */ %include "./interface/SBModule.i" %include "./interface/SBModuleSpec.i" %include "./interface/SBPlatform.i" diff --git a/lldb/include/lldb/API/LLDB.h b/lldb/include/lldb/API/LLDB.h index eacbbeafcf1cd8658efba33c0dd536379f2fd0d0..38ab8b94f7ce6b6e661c25e5f59273a00e5a5ac0 100644 --- a/lldb/include/lldb/API/LLDB.h +++ b/lldb/include/lldb/API/LLDB.h @@ -44,6 +44,7 @@ #include "lldb/API/SBListener.h" #include "lldb/API/SBMemoryRegionInfo.h" #include "lldb/API/SBMemoryRegionInfoList.h" +#include "lldb/API/SBMixedArkTSDebugger.h" // OHOS_LOCAL #include "lldb/API/SBModule.h" #include "lldb/API/SBModuleSpec.h" #include "lldb/API/SBPlatform.h" diff --git a/lldb/include/lldb/API/SBData.h b/lldb/include/lldb/API/SBData.h index 89a699f2f713a0f8c02d0d2e3dbae107595502ea..fa1704a0531a139de945200ae97c5983e75e7c65 100644 --- a/lldb/include/lldb/API/SBData.h +++ b/lldb/include/lldb/API/SBData.h @@ -153,6 +153,7 @@ private: friend class SBSection; friend class SBTarget; friend class SBValue; + friend class SBMixedArkTSDebugger; // OHOS_LOCAL friend class lldb_private::ScriptInterpreter; diff --git a/lldb/include/lldb/API/SBDefines.h b/lldb/include/lldb/API/SBDefines.h index ecf1dc34d8c5854c0f1ca71b5e073089e5d79470..244ea78046b8e1bdcadf34238017477c0157ec5a 100644 --- a/lldb/include/lldb/API/SBDefines.h +++ b/lldb/include/lldb/API/SBDefines.h @@ -68,6 +68,7 @@ class LLDB_API SBLineEntry; class LLDB_API SBListener; class LLDB_API SBMemoryRegionInfo; class LLDB_API SBMemoryRegionInfoList; +class LLDB_API SBMixedArkTSDebugger; // OHOS_LOCAL class LLDB_API SBModule; class LLDB_API SBModuleSpec; class LLDB_API SBModuleSpecList; diff --git a/lldb/include/lldb/API/SBError.h b/lldb/include/lldb/API/SBError.h index f8289e2fcbb3a48916422326c4ccee01b8f99c0b..9ca763404875e2f1cf30e9131c5c2c759f6ec9e1 100644 --- a/lldb/include/lldb/API/SBError.h +++ b/lldb/include/lldb/API/SBError.h @@ -75,6 +75,7 @@ protected: friend class SBValue; friend class SBWatchpoint; friend class SBFile; + friend class SBMixedArkTSDebugger; // OHOS_LOCAL friend class lldb_private::ScriptInterpreter; diff --git a/lldb/include/lldb/API/SBMixedArkTSDebugger.h b/lldb/include/lldb/API/SBMixedArkTSDebugger.h new file mode 100644 index 0000000000000000000000000000000000000000..595fcbfd4a21096449e900622d959f67d5edad6a --- /dev/null +++ b/lldb/include/lldb/API/SBMixedArkTSDebugger.h @@ -0,0 +1,53 @@ +//===-- SBMixedArkTSDebugger.h ----------------------------------*- C++ -*-===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_API_SBMIXEDARKTSDEBUGGER_H +#define LLDB_API_SBMIXEDARKTSDEBUGGER_H + +#include "lldb/API/SBDefines.h" + +namespace lldb { + +class LLDB_API SBMixedArkTSDebugger { +public: + SBMixedArkTSDebugger(); + + SBMixedArkTSDebugger(const lldb::SBTarget &rhs); + + SBMixedArkTSDebugger(const lldb::TargetSP &target_sp); + + ~SBMixedArkTSDebugger(); + + /// Get the backtrace of ArkTS for current thread. A cstring which contains + /// the information of backtrace is saved in return. + /// + /// If the current thread does not have an ArkTS runtime, "\0" will be returned. + /// + /// \param [out] er + /// The variable to get error reason, when some error occurred. + /// + /// \return + /// An lldb::SBData object which contain the raw cstring of ArkTS backtrace. + lldb::SBData GetBackTrace(SBError &er); + +private: + lldb_private::MixedArkTSDebugger* m_opaque_ptr; +}; + +} // namespace lldb + +#endif // LLDB_API_SBMIXEDARKTSDEBUGGER_H \ No newline at end of file diff --git a/lldb/include/lldb/API/SBTarget.h b/lldb/include/lldb/API/SBTarget.h index 018acfd595adef1db6297fc7c037df0d5673c80d..086ca63c518c23616eb54f1b7bbe278b05e9f84d 100644 --- a/lldb/include/lldb/API/SBTarget.h +++ b/lldb/include/lldb/API/SBTarget.h @@ -879,6 +879,7 @@ protected: friend class SBSymbol; friend class SBValue; friend class SBVariablesOptions; + friend class SBMixedArkTSDebugger; // OHOS_LOCAL // Constructors are private, use static Target::Create function to create an // instance of this class. diff --git a/lldb/include/lldb/Target/MixedArkTSDebugger.h b/lldb/include/lldb/Target/MixedArkTSDebugger.h new file mode 100644 index 0000000000000000000000000000000000000000..464e62b19306e3aa307e8f53886fdb17a806e622 --- /dev/null +++ b/lldb/include/lldb/Target/MixedArkTSDebugger.h @@ -0,0 +1,37 @@ +//===-- MixedArkTSDebugger.h ------------------------------------*- C++ -*-===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_SOURCE_PLUGINS_MIXEDDEBUGGER_ARKTS_MIXEDARKTSDEBUGGER_H +#define LLDB_SOURCE_PLUGINS_MIXEDDEBUGGER_ARKTS_MIXEDARKTSDEBUGGER_H + +#include "lldb/Target/MixedDebugger.h" +using namespace lldb; + +namespace lldb_private { + +class MixedArkTSDebugger : public MixedDebugger { +public: + MixedArkTSDebugger(const lldb::TargetSP &target_sp); + + ~MixedArkTSDebugger() {}; + + DataExtractorSP GetCurrentThreadBackTrace(Status &error) override; +}; + +} // namespace lldb_private + +#endif // LLDB_SOURCE_PLUGINS_MIXEDDEBUGGER_MIXEDDEBUGGER_H diff --git a/lldb/include/lldb/Target/MixedDebugger.h b/lldb/include/lldb/Target/MixedDebugger.h new file mode 100644 index 0000000000000000000000000000000000000000..01f43454a8e6a6300bc30b73bcfc1cd2f474be1c --- /dev/null +++ b/lldb/include/lldb/Target/MixedDebugger.h @@ -0,0 +1,59 @@ +//===-- MixedDebugger.h -----------------------------------------*- C++ -*-===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_SOURCE_PLUGINS_MIXEDDEBUGGER_MIXEDDEBUGGER_H +#define LLDB_SOURCE_PLUGINS_MIXEDDEBUGGER_MIXEDDEBUGGER_H + +#include "lldb/Utility/DataExtractor.h" +#include "lldb/Target/Target.h" +#include "lldb/Utility/Status.h" +#include "lldb/lldb-forward.h" + +using namespace lldb; + +namespace lldb_private { + +class MixedDebugger{ +public: + MixedDebugger(const TargetSP &target_sp); + + virtual ~MixedDebugger() {}; + + /// Execute the C API provided by runtime to get debug information. + /// + /// \param [in] expr + /// A cstring of expression which start with "(const char*)". + /// + /// \param [out] error + /// The variable to record error reason. + /// + /// \return + /// A DataExtractorSP contain a cstring which length is less than + /// UINT32_MAX. + DataExtractorSP ExecuteAction(const char* expr, Status &error); + + virtual DataExtractorSP GetCurrentThreadBackTrace(Status &error) = 0; + +protected: + TargetSP m_target_sp; + + MixedDebugger() = delete; +}; + +} // namespace lldb_private + +#endif // LLDB_SOURCE_PLUGINS_MIXEDDEBUGGER_MIXEDDEBUGGER_H diff --git a/lldb/include/lldb/Utility/LLDBLog.h b/lldb/include/lldb/Utility/LLDBLog.h index 1252dcc6bc2dd5317bbd153f4b430dcb9ff68e39..024e1b1788b05080651269b4da07c50fe8325c5b 100644 --- a/lldb/include/lldb/Utility/LLDBLog.h +++ b/lldb/include/lldb/Utility/LLDBLog.h @@ -50,7 +50,8 @@ enum class LLDBLog : Log::MaskType { // OHOS_LOCAL begin Performance = Log::ChannelFlag<31>, OnDemand = Log::ChannelFlag<32>, - LLVM_MARK_AS_BITMASK_ENUM(OnDemand), + MixedDebugger = Log::ChannelFlag<33>, + LLVM_MARK_AS_BITMASK_ENUM(MixedDebugger), // OHOS_LOCAL end }; diff --git a/lldb/include/lldb/lldb-forward.h b/lldb/include/lldb/lldb-forward.h index c51e1850338fff0616b719e58501a5adaf88861c..4988efbf8bbce3495fb242bd367d4044a229e440 100644 --- a/lldb/include/lldb/lldb-forward.h +++ b/lldb/include/lldb/lldb-forward.h @@ -124,6 +124,10 @@ class Materializer; class MemoryHistory; class MemoryRegionInfo; class MemoryRegionInfos; +// OHOS_LOCAL begin +class MixedArkTSDebugger; +class MixedDebugger; +// OHOS_LOCAL end class Module; class ModuleList; class ModuleSpec; diff --git a/lldb/source/API/CMakeLists.txt b/lldb/source/API/CMakeLists.txt index 894ef6335ada903703655a60edd17958cc678afc..e2a6f1d1c5cdb571ecaf39ccae8899d613f94e5b 100644 --- a/lldb/source/API/CMakeLists.txt +++ b/lldb/source/API/CMakeLists.txt @@ -65,6 +65,7 @@ add_lldb_library(liblldb ${LIBLLDB_LIB_TYPE} ${option_framework} # OHOS_LOCAL SBListener.cpp SBMemoryRegionInfo.cpp SBMemoryRegionInfoList.cpp + SBMixedArkTSDebugger.cpp # OHOS_LOCAL SBModule.cpp SBModuleSpec.cpp SBPlatform.cpp diff --git a/lldb/source/API/SBMixedArkTSDebugger.cpp b/lldb/source/API/SBMixedArkTSDebugger.cpp new file mode 100644 index 0000000000000000000000000000000000000000..148f9e473f9b0893caae49a81de81404de244e2b --- /dev/null +++ b/lldb/source/API/SBMixedArkTSDebugger.cpp @@ -0,0 +1,54 @@ +//===-- SBMixedArkTSDebugger.cpp ------------------------------------------===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBMixedArkTSDebugger.h" +#include "lldb/API/SBData.h" +#include "lldb/API/SBTarget.h" +#include "lldb/API/SBError.h" +#include "lldb/Utility/Instrumentation.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/MixedArkTSDebugger.h" + +using namespace lldb; +using namespace lldb_private; + +SBMixedArkTSDebugger::SBMixedArkTSDebugger() { + LLDB_INSTRUMENT_VA(this); +} + +SBMixedArkTSDebugger::SBMixedArkTSDebugger(const lldb::SBTarget &rhs) + : m_opaque_ptr(new MixedArkTSDebugger(rhs.GetSP())) { + LLDB_INSTRUMENT_VA(this, rhs); +} + +SBMixedArkTSDebugger::SBMixedArkTSDebugger(const lldb::TargetSP &target_sp) + : m_opaque_ptr(new MixedArkTSDebugger(target_sp)) { + LLDB_INSTRUMENT_VA(this, target_sp); +} + +SBMixedArkTSDebugger::~SBMixedArkTSDebugger() { + if (m_opaque_ptr) { + delete m_opaque_ptr; + m_opaque_ptr = nullptr; + } +} + +lldb::SBData SBMixedArkTSDebugger::GetBackTrace(SBError &er) { + LLDB_INSTRUMENT_VA(this, er); + + return SBData(m_opaque_ptr->GetCurrentThreadBackTrace(er.ref())); +} diff --git a/lldb/source/Target/CMakeLists.txt b/lldb/source/Target/CMakeLists.txt index c75a10cf61c146da2e51176c8145c1d91b2cdd05..215f385fc8a9377b889f351ad21296b4b048ddb8 100644 --- a/lldb/source/Target/CMakeLists.txt +++ b/lldb/source/Target/CMakeLists.txt @@ -21,6 +21,10 @@ add_lldb_library(lldbTarget MemoryHistory.cpp MemoryRegionInfo.cpp MemoryTagMap.cpp + # OHOS_LOCAL begin + MixedArkTSDebugger.cpp + MixedDebugger.cpp + # OHOS_LOCAL end ModuleCache.cpp OperatingSystem.cpp PathMappingList.cpp diff --git a/lldb/source/Target/MixedArkTSDebugger.cpp b/lldb/source/Target/MixedArkTSDebugger.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfe46ec7e41d0347b2eeffdccd393a97fdab1808 --- /dev/null +++ b/lldb/source/Target/MixedArkTSDebugger.cpp @@ -0,0 +1,37 @@ +//===-- MixedArkTSDebugger.cpp --------------------------------------------===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Target/MixedArkTSDebugger.h" +#include "lldb/Utility/LLDBLog.h" + +using namespace lldb; +using namespace lldb_private; + +static const char* BackTrace = "(const char*)GetJsBacktrace()"; + +MixedArkTSDebugger::MixedArkTSDebugger(const TargetSP &target_sp) + : MixedDebugger(target_sp) {} + +DataExtractorSP MixedArkTSDebugger::GetCurrentThreadBackTrace(Status &error) { + DataExtractorSP result = ExecuteAction(BackTrace, error); + if (!error.Success()) { + Log *log = GetLog(LLDBLog::MixedDebugger); + LLDB_LOGF(log, "[MixedArkTSDebugger::GetBackTrace] failed for %s", + error.AsCString()); + } + return result; +} diff --git a/lldb/source/Target/MixedDebugger.cpp b/lldb/source/Target/MixedDebugger.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c21ac062592b02985949e65e6e1740c6d619cbc --- /dev/null +++ b/lldb/source/Target/MixedDebugger.cpp @@ -0,0 +1,94 @@ +//===-- MixedDebugger.cpp -------------------------------------------------===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Target/MixedDebugger.h" +#include "lldb/Core/ValueObject.h" +#include "lldb/Utility/LLDBLog.h" +#include "lldb/Utility/DataExtractor.h" + +using namespace lldb; +using namespace lldb_private; + +MixedDebugger::MixedDebugger(const TargetSP &target_sp) : m_target_sp(target_sp) {} + +DataExtractorSP MixedDebugger::ExecuteAction(const char* expr, Status &error) { + Log *log = GetLog(LLDBLog::MixedDebugger); + error.Clear(); + ValueObjectSP expr_value_sp; + TargetSP target_sp(m_target_sp); + DataExtractorSP result(new DataExtractor()); + + if (m_target_sp) { + if (expr == nullptr || expr[0] == 0) { + error.SetErrorString("Expression is empty"); + return result; + } + + std::lock_guard guard(target_sp->GetAPIMutex()); + ExecutionContext exe_ctx(target_sp.get()); + + StackFrame *frame = exe_ctx.GetFramePtr(); + Target *target = exe_ctx.GetTargetPtr(); + + if (target) { + lldb::ExpressionResults expr_re = + target->EvaluateExpression(expr, frame, expr_value_sp); + if (expr_re) { + error.SetErrorStringWithFormat( + "[MixedDebugger::ExecuteAction] exe %s failed: %d", expr, expr_re); + return result; + } + + const size_t k_max_buf_size = 64; + size_t cstr_len = UINT32_MAX; + size_t offset = 0; + size_t bytes_read = 0; + DataExtractor data; + while ((bytes_read = expr_value_sp->GetPointeeData(data, offset, k_max_buf_size)) > 0) { + const char *cstr = data.PeekCStr(0); + size_t len = strnlen(cstr, k_max_buf_size); + if (len >= cstr_len) { + error.SetErrorString("[MixedDebugger::ExecuteAction] result over size"); + result->Clear(); + return result; + } + if (!result->Append(const_cast(cstr), len)) { + error.SetErrorString("[MixedDebugger::ExecuteAction] result append data failed"); + result->Clear(); + return result; + } + if (len < k_max_buf_size) { + break; + } + cstr_len -= len; + offset += len; + } + + // Add terminator to result data + char te = '\0'; + if (!result->Append(&te, 1)) { + error.SetErrorString("[MixedDebugger::ExecuteAction] result append terminator failed"); + result->Clear(); + return result; + } + } + } + LLDB_LOGF(log, + "[MixedDebugger::ExecuteAction] result is " + "%s", result->PeekCStr(0)); + return result; +} diff --git a/lldb/test/API/functionalities/mixed_debugger/arkts/Makefile b/lldb/test/API/functionalities/mixed_debugger/arkts/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..651ab44223196307e56e783c9bdab1d3f20eee0a --- /dev/null +++ b/lldb/test/API/functionalities/mixed_debugger/arkts/Makefile @@ -0,0 +1,16 @@ +# Copyright (C) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +C_SOURCES = main.c + +include Makefile.rules diff --git a/lldb/test/API/functionalities/mixed_debugger/arkts/TestMixedArktsDebugger.py b/lldb/test/API/functionalities/mixed_debugger/arkts/TestMixedArktsDebugger.py new file mode 100644 index 0000000000000000000000000000000000000000..6560d9261f0549c565f0c304481904672202a901 --- /dev/null +++ b/lldb/test/API/functionalities/mixed_debugger/arkts/TestMixedArktsDebugger.py @@ -0,0 +1,84 @@ +""" +Copyright (C) 2024 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import unittest2 +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +class MixedArkTSDebuggerTest(TestBase): + + def setUp(self): + # Call super's setUp(). + TestBase.setUp(self) + # Find the line number for our breakpoint. + self.breakpoint = line_number('main.c', '// break on this line') + + def test(self): + """Test getting js backtrace.""" + self.build() + + exe = self.getBuildArtifact("a.out") + self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) + + # This should create a breakpoint in the main thread. + lldbutil.run_break_set_by_file_and_line( + self, "main.c", self.breakpoint, num_expected_locations=1) + + # The breakpoint list should show 1 location. + self.expect( + "breakpoint list -f", + "Breakpoint location shown correctly", + substrs=[ + "1: file = 'main.c', line = %d, exact_match = 0, locations = 1" % + self.breakpoint]) + + # Run the program. + self.runCmd("run", RUN_SUCCEEDED) + + # The stop reason of the thread should be breakpoint. + self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, + substrs=['stopped', + 'stop reason = breakpoint']) + + # Get the target and process + target = self.dbg.GetSelectedTarget() + process = target.GetProcess() + + # Get arkts backtrace + arkdb = lldb.SBMixedArkTSDebugger(target) + er = lldb.SBError() + bt = arkdb.GetBackTrace(er) + + # Check the result + self.assertTrue(er.Success(), "ArkTS debugger get backtrace failed.") + er.Clear() + self.assertTrue(bt.GetString(er, 0) == 'This is a ArkTS backtrace', + 'ArkTS debugger get wrong backtrace.') + + # Run to completion + self.runCmd("continue") + + # If the process hasn't exited, collect some information + if process.GetState() != lldb.eStateExited: + self.runCmd("thread list") + self.runCmd("process status") + + # At this point, the inferior process should have exited. + self.assertEqual( + process.GetState(), lldb.eStateExited, + PROCESS_EXITED) diff --git a/lldb/test/API/functionalities/mixed_debugger/arkts/main.c b/lldb/test/API/functionalities/mixed_debugger/arkts/main.c new file mode 100644 index 0000000000000000000000000000000000000000..ae2d25530c6e099b46975a7c4150740718c6fcdf --- /dev/null +++ b/lldb/test/API/functionalities/mixed_debugger/arkts/main.c @@ -0,0 +1,27 @@ +// Copyright (C) 2024 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +static int var = 5; +const char *bt = "This is a ArkTS backtrace"; + +const char *GetJsBacktrace() { + return bt; +} + +int main () +{ + printf ("%p is %d\n", &var, var); // break on this line + return ++var; +}