由 Sytemd 服务启动时,程序因分段错误 (SEGV) 退出

由 Sytemd 服务启动时,程序因分段错误 (SEGV) 退出

我编写了一个程序来控制连接到 NVidia Xavier NX 的 USB 摄像头。该程序运行良好,并且当我使用以下命令手动运行它时,它会执行它应该执行的操作:

./<name-of-the-executable> -cfg <path-to-config-file.json>

无论我以用户身份还是 root 身份启动该程序,该程序都可以运行。然后我编写一个脚本来自动执行应用程序的运行过程。它导出一个指向 USB 传输层文件(.ct 和 .xml)的环境变量,终止已运行的应用程序以干净地重新启动,转到可执行文件的目录,并执行运行该程序的命令。

当以用户或 root 身份启动时,此 .sh 脚本也可以正常工作。现在问题出现在这里:

当我为 systemd 创建一个单元文件以在启动时将程序作为服务运行时,我收到 SEGV 信号并且程序中止。服务文件如下所示:

[Unit]
Description=Alvium Camera

[Service]
Type=forking
User=ekin
ExecStartPre=/bin/bash -c '. /opt/ekinCameraController/alvium/SetGenTLPath.sh'
ExecStart=/opt/ekinCameraController/startAlviumCamera.sh

[Install]
WantedBy=multi-user.target

该程序在运行时创建 LWP(轻量级进程),这就是为什么我将类型设置为“分叉”,但我不知道这是否被视为守护进程。然而,当我将类型更改为“简单”时,没有什么不同发生;程序仍然以 SEGV 停止。

我通过输出到 gdb 来检查 coredumpctl,结果是这样的:

coredumpctl gdb 8968

           PID: 8968 (AlviumCameraCon)
           UID: 1000 (ekin)
           GID: 1000 (ekin)
        Signal: 11 (SEGV)
     Timestamp: Tue 2022-08-09 10:40:45 +03 (21s ago)
  Command Line: ./AlviumCameraController -cfg alviumCamera.json -log 0
    Executable: /opt/ekinCameraController/AlviumCameraController
 Control Group: /system.slice/alviumCamera.service
          Unit: alviumCamera.service
         Slice: system.slice
       Boot ID: 053bf5a1b2864c22ae5b157ec06d20a1
    Machine ID: a3d9197b765643568af09eb2bd3e5ce7
      Hostname: 4-6-rev2-NVMe
.
.
.
[New LWP 8976]
[New LWP 8977]
[New LWP 8974]
[New LWP 8975]
[New LWP 8978]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
Core was generated by `./AlviumCameraController -cfg alviumCamera.json -log 0'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000007fa9b428a4 in AVT::VmbAPI::Feature::GetValue(long long&) const () from /media/sd/workspace/ekincameracontroller/lib/vimba/6.0/x64/gcc-arm/libVimbaCPP.so
[Current thread is 1 (Thread 0x7f8dff9e10 (LWP 8989))]

该过程的回溯显示:

#0  0x0000007fa9b428a4 in ?? ()
#1  0x000000555bafb3b4 in AVT::VmbAPI::Examples::AlviumCam::PrepareCamera (this=0x5576cd80b0) at /media/sd/workspace/ekincameracontroller/src/Camera/Alvium/AlviumCam.cpp:891
#2  0x000000555bafc8a4 in std::operator<< <std::char_traits<char> > (__s=0x555bb28e58 "response: ", __out=...) at /usr/include/c++/7/ostream:561
#3  AVT::VmbAPI::Examples::AlviumCam::StartContinuousImageAcquisition (this=0x5576cd80b0, Config=...) at /media/sd/workspace/ekincameracontroller/src/Camera/Alvium/AlviumCam.cpp:531
#4  0x000000555bb01170 in std::operator<< <std::char_traits<char> > (__s=0x555bb292c8 " Format:", __out=...) at /usr/include/c++/7/ostream:561
#5  AVT::VmbAPI::Examples::PrintFrameInfo (pFrame=...) at /media/sd/workspace/ekincameracontroller/src/Camera/Alvium/FrameObserver.cpp:151
#6  0x0000007fa921be94 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

我应该从这些中得到什么?现在我知道 libVimbaCPP.so 中的 GetValue 函数返回一条消息,表明调用无效。

SP_ACCESS(pCamera)->GetFeatureByName(Name, feature);

SP_ACCESS 定义为:

#define SP_ACCESS( sp ) (sp).get()

但我不知道如何继续。

要将 coredumpctl 消息与手动执行程序时的消息进行比较,您可以看到以下 gdb 输出:

           PID: 8549 (AlviumCameraCon)
           UID: 1000 (ekin)
           GID: 1000 (ekin)
        Signal: 6 (ABRT)
     Timestamp: Tue 2022-08-09 10:37:35 +03 (58s ago)
  Command Line: ./AlviumCameraController -cfg alviumCamera.json -log 0
    Executable: /opt/ekinCameraController/AlviumCameraController
 Control Group: /user.slice/user-1000.slice/session-6.scope
          Unit: session-6.scope
         Slice: user-1000.slice
       Session: 6
     Owner UID: 1000 (ekin)
       Boot ID: 053bf5a1b2864c22ae5b157ec06d20a1
    Machine ID: a3d9197b765643568af09eb2bd3e5ce7
      Hostname: 4-6-rev2-NVMe
       Storage: /var/lib/systemd/coredump/core.AlviumCameraCon.1000.053bf5a1b2864c22ae5b157ec06d20a1.8549.1660030655000000.lz4
       Message: Process 8549 (AlviumCameraCon) of user 1000 dumped core.
.
.
.
[New LWP 8552]
[New LWP 8553]
[New LWP 8574]
[New LWP 8554]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
Core was generated by `./AlviumCameraController -cfg alviumCamera.json -log 0'.
Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
[Current thread is 1 (Thread 0x7f7cfe0010 (LWP 8549))]
(gdb) quit

你能帮我看看为什么程序受系统控制时会出现分段错误吗? coredumpctl 显示了错误的根源,但我不知道如何解决它。如果有帮助的话,以下是来自journalctl的更多详细信息:

...
Ağu 09 10:40:45 4-6-rev2-NVMe startAlviumCamera.sh[8967]: Parameter GainAutoMinis invalid!
Ağu 09 10:40:45 4-6-rev2-NVMe startAlviumCamera.sh[8967]: Parameter GainAutoMaxis invalid!
Ağu 09 10:40:45 4-6-rev2-NVMe startAlviumCamera.sh[8967]: Parameter Gammais invalid!
Ağu 09 10:40:45 4-6-rev2-NVMe startAlviumCamera.sh[8967]: 3 - Preparing camera 5.
Ağu 09 10:40:45 4-6-rev2-NVMe startAlviumCamera.sh[8967]: 3 - Preparing camera 6.
Ağu 09 10:40:46 4-6-rev2-NVMe startAlviumCamera.sh[8967]: /opt/ekinCameraController/startAlviumCamera.sh: line 22:  8968 Segmentation fault      (core dumped) ./AlviumCameraController -cfg
Ağu 09 10:40:46 4-6-rev2-NVMe systemd[1]: alviumCamera.service: Control process exited, code=exited status=139
Ağu 09 10:40:46 4-6-rev2-NVMe systemd[1]: alviumCamera.service: Failed with result 'exit-code'.
Ağu 09 10:40:46 4-6-rev2-NVMe systemd[1]: Failed to start Alvium Camera.

“参数 X 无效!”行表明我无法到达相机指针。我不知道为什么会发生这种情况以及如何解决。我很感激任何帮助!

相关内容