#include #include #include #include #include #include #include "AudioService.h" #include #include #include #include // 用于dladdr static void printStackTrace() { void *callstack[128]; int frames = backtrace(callstack, 128); char **strs = backtrace_symbols(callstack, frames); QString str_hour = QDateTime::currentDateTime().toString("hh"); QString str_min = QDateTime::currentDateTime().toString("mm"); QString str_sec = QDateTime::currentDateTime().toString("ss"); QString str_curTime = str_hour + str_min + str_sec; QString fileName = QString("/data/crash_audioService") + str_curTime + QString(".log"); FILE *log = fopen(fileName.toStdString().c_str(), "a"); for (int i = 0; i < frames; ++i) { fprintf(log,"%s\n", strs[i]); } fclose(log); free(strs); } static AudioService* _audioService = NULL; static void handler(int sig, siginfo_t *info, void *ucontext) { (void)ucontext; printf("AudioService 捕获到信号: %d, 发送进程PID: %d processName:%s\n", sig, info->si_pid, strsignal(sig)) ; switch(sig) { case SIGINT: case SIGILL: case SIGTERM: case SIGSEGV: case SIGFPE: case SIGABRT: printStackTrace(); if(_audioService != NULL) { emit _audioService->onAudioServiceRestart(); } break; default: break; } usleep(100 * 1000); _exit(1); } static void setTextCodec(); int main(int argc, char *argv[]) { freopen("/dev/ttyS0", "w", stdout); freopen("/dev/ttyS0", "w", stderr); printf(">>>>>>>>>>>>pid:%d\n",getpid()); struct sigaction sa; // 清空结构体 memset(&sa, 0, sizeof(sa)); // 指定信号处理函数(使用更强大的 sa_sigaction) sa.sa_sigaction = handler; // 设置 SA_SIGINFO 标志以使用 sa_sigaction sa.sa_flags = SA_SIGINFO; // 可选:在处理此信号时,阻塞其他信号(如SIGTERM) sigemptyset(&sa.sa_mask); if (sigaction(SIGINT, &sa, NULL) == -1) { perror("sigaction 设置失败"); return 1; } else if (sigaction(SIGTERM, &sa, NULL) == -1) { perror("sigaction 设置失败"); return 1; } else if (sigaction(SIGSEGV, &sa, NULL) == -1) { perror("sigaction 设置失败"); return 1; } else if (sigaction(SIGABRT, &sa, NULL) == -1) { perror("sigaction 设置失败"); return 1; } else if (sigaction(SIGILL, &sa, NULL) == -1) { perror("sigaction 设置失败"); return 1; } else if (sigaction(SIGFPE, &sa, NULL) == -1) { perror("sigaction 设置失败"); return 1; } QCoreApplication app(argc, argv); setTextCodec(); _audioService = new AudioService(&app); return app.exec(); } void setTextCodec() { // 设置默认编码为UTF-8 QTextCodec *codec = QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForLocale(codec); }