From 7f4d16dbcc5adef06ba7cbc5066664998149f3a4 Mon Sep 17 00:00:00 2001 From: liujia178 Date: Thu, 7 Dec 2023 10:11:00 +0800 Subject: [PATCH] [LLDB] Add the interpreter module during the the launch process Signed-off-by: liujia178 --- .../POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp | 5 ++ .../TestModuleLoadedNotifys.py | 6 +- lldb/test/API/test_linker/Makefile | 18 ++++ lldb/test/API/test_linker/TestLinkerLaunch.py | 88 +++++++++++++++++++ lldb/test/API/test_linker/main.c | 22 +++++ 5 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 lldb/test/API/test_linker/Makefile create mode 100644 lldb/test/API/test_linker/TestLinkerLaunch.py create mode 100644 lldb/test/API/test_linker/main.c diff --git a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp index 8fc7488071d4..9491d55d14f3 100644 --- a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp +++ b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp @@ -193,6 +193,11 @@ void DynamicLoaderPOSIXDYLD::DidLaunch() { } LoadVDSO(); + // OHOS_LOCAL begin + ModuleSP interpreter = m_interpreter_module.lock(); + if (interpreter) + module_list.Append(interpreter); + // OHOS_LOCAL end m_process->GetTarget().ModulesDidLoad(module_list); } } diff --git a/lldb/test/API/functionalities/target-new-solib-notifications/TestModuleLoadedNotifys.py b/lldb/test/API/functionalities/target-new-solib-notifications/TestModuleLoadedNotifys.py index 06169b041d37..4cf335b4744f 100644 --- a/lldb/test/API/functionalities/target-new-solib-notifications/TestModuleLoadedNotifys.py +++ b/lldb/test/API/functionalities/target-new-solib-notifications/TestModuleLoadedNotifys.py @@ -83,7 +83,11 @@ class ModuleLoadedNotifysTestCase(TestBase): # On macOS Ventura and later, dyld and the main binary # will be loaded again when dyld moves itself into the # shared cache. - if module.file.fullpath not in ['/usr/lib/dyld', exe]: + # OHOS requires lldb to load the interpreter module during + # the launch process to support debugging musl/linker. So + # ld.so will be loaded repeatedly. + if (module.file.fullpath not in ['/usr/lib/dyld', exe] and + not module.file.basename.startswith('ld-')): # OHOS_LOCAL self.assertTrue(module not in already_loaded_modules, '{} is already loaded'.format(module)) already_loaded_modules.append(module) if self.TraceOn(): diff --git a/lldb/test/API/test_linker/Makefile b/lldb/test/API/test_linker/Makefile new file mode 100644 index 000000000000..c4e75503d3e2 --- /dev/null +++ b/lldb/test/API/test_linker/Makefile @@ -0,0 +1,18 @@ +# Copyright (c) 2023 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 +# See TestLinkerLaunch.py, which specifies the executable name with a dictionary. +EXE := test_linker + +include Makefile.rules diff --git a/lldb/test/API/test_linker/TestLinkerLaunch.py b/lldb/test/API/test_linker/TestLinkerLaunch.py new file mode 100644 index 000000000000..e968bd951d87 --- /dev/null +++ b/lldb/test/API/test_linker/TestLinkerLaunch.py @@ -0,0 +1,88 @@ +# Copyright (c) 2023 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. + +""" +Test lldb hit the breakpoint in the linker or musl. This test case only runs +when the local ubuntu is connected to the phone or other device and there is +a remote-ohos connection, will skip on CI or remote = false. +""" + +import subprocess +import sys + +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + +class LaunchLinker(TestBase): + NO_DEBUG_INFO_TESTCASE = True + + def setUp(self): + # Call super's setUp(). + TestBase.setUp(self) + # Get the full path to our executable to be debugged. + self.exe = "%s_%d" % (self.getBuildArtifact(self.testMethodName), os.getpid()) + d = {"EXE": self.exe} + self.build(dictionary=d) + self.setTearDownCleanup(dictionary=d) + + def tearDown(self): + # Call super's tearDown(). + TestBase.tearDown(self) + + @skipIf(remote=False) + @skipUnlessPlatform(["ohos"]) + def test_linker_with_breakpoint(self): + """Create target, breakpoint, launch a process, and then kill it.""" + target = self.dbg.CreateTarget(self.exe) + + breakpoint = target.BreakpointCreateByName("__dls2b") + # The default state after breakpoint creation should be enabled. + self.assertTrue( + breakpoint.IsEnabled(), "Breakpoint should be enabled after creation" + ) + process = target.LaunchSimple(None, None, self.get_process_working_directory()) + self.assertTrue(process, PROCESS_IS_VALID) + + thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) + self.assertIsNotNone(thread) + + # The breakpoint should have a hit count of 1. + self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE) + + # Destroy process before TestBase.tearDown() + self.dbg.GetSelectedTarget().GetProcess().Destroy() + + @skipIf(remote=False) + @skipUnlessPlatform(["ohos"]) + def test_musl_with_breakpoint(self): + """Create target, breakpoint, launch a process, and then kill it.""" + target = self.dbg.CreateTarget(self.exe) + + breakpoint = target.BreakpointCreateByName("printf") + # The default state after breakpoint creation should be enabled. + self.assertTrue( + breakpoint.IsEnabled(), "Breakpoint should be enabled after creation" + ) + process = target.LaunchSimple(None, None, self.get_process_working_directory()) + self.assertTrue(process, PROCESS_IS_VALID) + + thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) + self.assertIsNotNone(thread) + + # The breakpoint should have a hit count of 1. + self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE) + + # Destroy process before TestBase.tearDown() + self.dbg.GetSelectedTarget().GetProcess().Destroy() diff --git a/lldb/test/API/test_linker/main.c b/lldb/test/API/test_linker/main.c new file mode 100644 index 000000000000..f404bb69b769 --- /dev/null +++ b/lldb/test/API/test_linker/main.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 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 + +int main () +{ + printf("test musl/linker!\n"); + return 0; +} -- Gitee