1. 问题
运行第 StateMachineTest_OnEvent012 用例时报错
测试用例代码如下
HWTEST_F(CooperatePluginTest, StateMachineTest_OnEvent012, TestSize.Level0)
{
CALL_TEST_DEBUG;
std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
CooperateEvent bothLocalEvent(
CooperateEventType::DSOFTBUS_START_COOPERATE,
DSoftbusStartCooperate {
.networkId = localNetworkId
});
auto env = ContextService::GetInstance();
ASSERT_NE(env, nullptr);
Context cooperateContext(env);
g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
Cooperate::CooperateIn stateIn(*g_stateMachine, env);
ASSERT_NE(stateIn.initial_, nullptr);
auto relay = std::make_shared<Cooperate::CooperateIn::RelayConfirmation>(stateIn, stateIn.initial_);
ASSERT_NE(relay, nullptr);
cooperateContext.remoteNetworkId_ = localNetworkId;
stateIn.initial_->OnRemoteStart(cooperateContext, bothLocalEvent);
relay->OnRemoteStart(cooperateContext, bothLocalEvent);
bool ret = g_context->mouseLocation_.HasLocalListener();
EXPECT_FALSE(ret);
}
2. 原因分析
打开 cppcrash-CooperatePluginTest-0-20170805230851 日志,如下片段
Generated by HiviewDFX@OpenHarmony
================================================================
Device info:OpenHarmony 3.2
Build info:OpenHarmony 5.0.0.24
Fingerprint:3c44cc1faf2a52afc8ddaa5cf406f01990018aa091a909dedc27b9a2c2507a13
Module name:CooperatePluginTest
Timestamp:2017-08-05 23:08:51.000
Pid:5038
Uid:0
Process name:./data/CooperatePluginTest
Process life time:2s
Reason:Signal:SIGSEGV(SEGV_MAPERR)@0000000000 probably caused by NULL pointer dereference
Fault thread info:
Tid:5038, Name:CooperatePlugin
#00 pc 00051e80 /system/lib/libintention_cooperate.z.so(OHOS::Msdp::DeviceStatus::Cooperate::StateMachine::GetCooperateState(OHOS::Msdp::DeviceStatus::Cooperate::Context&, OHOS::Msdp::DeviceStatus::Cooperate::CooperateEvent const&)+176)(3aa2fd1f262954e85537b789af0106df)
#01 pc 00025fb7 /data/CooperatePluginTest(OHOS::Msdp::DeviceStatus::CooperatePluginTest_StateMachineTest_OnEvent012_Test::TestBody()+366)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#02 pc 0005484f /data/CooperatePluginTest(testing::Test::Run()+206)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#03 pc 0005505b /data/CooperatePluginTest(testing::TestInfo::Run()+310)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#04 pc 0005586b /data/CooperatePluginTest(testing::TestSuite::Run()+338)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#05 pc 0005f19b /data/CooperatePluginTest(testing::internal::UnitTestImpl::RunAllTests()+1450)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#06 pc 0005ead9 /data/CooperatePluginTest(testing::UnitTest::Run()+84)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#07 pc 0004f25b /data/CooperatePluginTest(main+58)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#08 pc 00071dd8 /system/lib/ld-musl-arm.so.1(libc_start_main_stage2+56)(0ad219058118057922ee9ce3bf9b229b)
#09 pc 0001d078 /data/CooperatePluginTest(_start_c+84)(8bb6dbc0523b18bdc1d5b079d53
用 addr2line 反编译指令输入如下指令
./prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-addr2line -Cfpie ./out/rk3568/lib.unstripped/msdp/device_status/libintention_cooperate.z.so 00051e80
得到如下结果
(inlined by) OHOS::Msdp::DeviceStatus::Cooperate::CooperateFree::Initial::OnRemoteStart(OHOS::Msdp::DeviceStatus::Cooperate::Context&, OHOS::Msdp::DeviceStatus::Cooperate::CooperateEvent const&) at /mnt/data/wangyeyu/code_t67_0515/out/rk3568/../../base/msdp/device_status/intention/cooperate/plugin/src/cooperate_free.cpp:172
DSoftbusStartCooperate notice = std::get(event.event) 报错
查看源代码,发现构造 CooperateEvent 事件时缺少 DSoftbusStartCooperate 赋值
3. 解决方案
增加 DSoftbusStartCooperate 赋值,问题解决
HWTEST_F(CooperatePluginTest, StateMachineTest_OnEvent012, TestSize.Level0)
{
CALL_TEST_DEBUG;
std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
CooperateEvent bothLocalEvent(
CooperateEventType::DSOFTBUS_START_COOPERATE,
DSoftbusStartCooperate {
.networkId = localNetworkId
});
auto env = ContextService::GetInstance();
ASSERT_NE(env, nullptr);
Context cooperateContext(env);
g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
Cooperate::CooperateIn stateIn(*g_stateMachine, env);
ASSERT_NE(stateIn.initial_, nullptr);
auto relay = std::make_shared<Cooperate::CooperateIn::RelayConfirmation>(stateIn, stateIn.initial_);
ASSERT_NE(relay, nullptr);
cooperateContext.remoteNetworkId_ = localNetworkId;
stateIn.initial_->OnRemoteStart(cooperateContext, bothLocalEvent);
relay->OnRemoteStart(cooperateContext, bothLocalEvent);
bool ret = g_context->mouseLocation_.HasLocalListener();
EXPECT_FALSE(ret);
}
4. 小结
运行代码过程中出现 crash,用 addr2ling 反编译指令,对 lib.unstripped 未压缩的 so 进行反编译,出错地址和 so 来源于 crash 日志文件。
-
请使用 prebuilts 目录下的 llvm-addr2line 指令,否则解析不出来!
-
此类问题如果打印了进出日志,此时只会打印进入日志,缺少离开日志,如下:
总是有很多小伙伴反馈说:OpenHarmony开发不知道学习哪些技术?不知道需要重点掌握哪些OpenHarmony开发知识点? 为了解决大家这些学习烦恼。在这准备了一份很实用的鸿蒙全栈开发学习路线与学习文档给大家用来跟着学习。
针对一些列因素,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线,包含了鸿蒙开发必掌握的核心知识要点,内容有(OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开发、系统定制移植……等)技术知识点。
OpenHarmony 开发环境搭建:https://gitcode.com/HarmonyOS_MN/733GH/overview
《OpenHarmony源码解析》
搭建开发环境
系统架构分析
- 构建子系统
- 启动流程
- 子系统
- 分布式任务调度子系统
- 分布式通信子系统
- 驱动子系统
- ……