main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <QCoreApplication>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include "MultimediaService.h"
  8. #include "configUtils.h"
  9. #include "MusicPlayer/MusicPlayer.h"
  10. #include "VideoPlayer/VideoPlayer.h"
  11. #include "DeviceWatcher/DeviceWatcher.h"
  12. #include <iostream>
  13. #include <csignal>
  14. #include <execinfo.h>
  15. #include <dlfcn.h> // 用于dladdr
  16. static void printStackTrace() {
  17. void *callstack[128];
  18. int frames = backtrace(callstack, 128);
  19. char **strs = backtrace_symbols(callstack, frames);
  20. QString str_hour = QDateTime::currentDateTime().toString("hh");
  21. QString str_min = QDateTime::currentDateTime().toString("mm");
  22. QString str_sec = QDateTime::currentDateTime().toString("ss");
  23. QString str_curTime = str_hour + str_min + str_sec;
  24. QString fileName = QString("/data/crash_multimediaService") + str_curTime + QString(".log");
  25. FILE *log = fopen(fileName.toStdString().c_str(), "a");
  26. for (int i = 0; i < frames; ++i) {
  27. fprintf(log,"%s\n", strs[i]);
  28. }
  29. fclose(log);
  30. free(strs);
  31. }
  32. static void handler(int sig, siginfo_t *info, void *ucontext) {
  33. (void)ucontext;
  34. printf("MultimediaService捕获到信号: %d, 发送进程PID: %d processName:%s\n", sig, info->si_pid, strsignal(sig)) ;
  35. switch(sig) {
  36. case SIGINT:
  37. case SIGILL:
  38. case SIGTERM:
  39. case SIGSEGV:
  40. case SIGFPE:
  41. case SIGABRT:
  42. printStackTrace();
  43. if(MyClass::m_MusicPlayStatus == 1) {
  44. g_DeviceWatcher->onDiskDeviceChangeHandler(g_DeviceWatcher->getUsbDiskPath(), DiskDeviceWatcher::A_Remove);
  45. if(g_MusicPlayer->getMusicPthreadValue() != 0) {
  46. pthread_join(g_MusicPlayer->getMusicPthreadValue(), NULL);
  47. }
  48. }
  49. if(MyClass::m_VideoPlayStatus == 1) {
  50. g_DeviceWatcher->onDiskDeviceChangeHandler(g_DeviceWatcher->getUsbDiskPath(), DiskDeviceWatcher::A_Remove);
  51. if(g_VideoPlayer->getVideoPthreadValue() != 0) {
  52. pthread_join(g_VideoPlayer->getVideoPthreadValue(), NULL);
  53. }
  54. }
  55. if(MyClass::m_MusicPlayStatus != 1 && MyClass::m_VideoPlayStatus != 1) {
  56. g_DeviceWatcher->onDiskDeviceChangeHandler(g_DeviceWatcher->getUsbDiskPath(), DiskDeviceWatcher::A_Remove);
  57. }
  58. printf("*********pthread exit\n");
  59. CHECK_SYSTEM("echo host > /sys/devices/platform/soc/40000000.usb/usb_role/40000000.usb-role-switch/role");
  60. CHECK_SYSTEM("echo host > /sys/devices/platform/soc/40c00000.usb/usb_role/40c00000.usb-role-switch/role");
  61. break;
  62. default:
  63. break;
  64. }
  65. _exit(1);
  66. }
  67. static void setTextCodec();
  68. int main(int argc, char *argv[])
  69. {
  70. freopen("/dev/ttyS0", "w", stdout);
  71. freopen("/dev/ttyS0", "w", stderr);
  72. printf(">>>>>>>>>>>>pid:%d\n",getpid());
  73. struct sigaction sa;
  74. // 清空结构体
  75. memset(&sa, 0, sizeof(sa));
  76. // 指定信号处理函数(使用更强大的 sa_sigaction)
  77. sa.sa_sigaction = handler;
  78. // 设置 SA_SIGINFO 标志以使用 sa_sigaction
  79. sa.sa_flags = SA_SIGINFO;
  80. // 可选:在处理此信号时,阻塞其他信号(如SIGTERM)
  81. sigemptyset(&sa.sa_mask);
  82. if (sigaction(SIGINT, &sa, NULL) == -1) {
  83. perror("sigaction 设置失败");
  84. return 1;
  85. } else if (sigaction(SIGTERM, &sa, NULL) == -1) {
  86. perror("sigaction 设置失败");
  87. return 1;
  88. } else if (sigaction(SIGSEGV, &sa, NULL) == -1) {
  89. perror("sigaction 设置失败");
  90. return 1;
  91. } else if (sigaction(SIGABRT, &sa, NULL) == -1) {
  92. perror("sigaction 设置失败");
  93. return 1;
  94. } else if (sigaction(SIGILL, &sa, NULL) == -1) {
  95. perror("sigaction 设置失败");
  96. return 1;
  97. } else if (sigaction(SIGFPE, &sa, NULL) == -1) {
  98. perror("sigaction 设置失败");
  99. return 1;
  100. }
  101. QCoreApplication app(argc, argv);
  102. setTextCodec();
  103. MultimediaService multimediaService;
  104. (void)multimediaService;
  105. return app.exec();
  106. }
  107. void setTextCodec() {
  108. // 设置默认编码为UTF-8
  109. QTextCodec *codec = QTextCodec::codecForName("UTF-8");
  110. QTextCodec::setCodecForLocale(codec);
  111. }