更加详细的debug信息

This commit is contained in:
Pan Qiancheng 2025-04-07 19:20:44 +08:00
parent e389cde2d9
commit da26edc508
2 changed files with 24 additions and 9 deletions

View File

@ -4,6 +4,7 @@
#include <errno.h> // 添加 errno 相关定义 #include <errno.h> // 添加 errno 相关定义
#include <fcntl.h> #include <fcntl.h>
#include <json-c/json.h> #include <json-c/json.h>
#include <pty.h>
#include <signal.h> // 添加 SIGCHLD 相关定义 #include <signal.h> // 添加 SIGCHLD 相关定义
#include <stdbool.h> // 引入 bool 类型 #include <stdbool.h> // 引入 bool 类型
#include <stdio.h> #include <stdio.h>
@ -15,13 +16,22 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <pty.h>
#ifdef DEBUG #ifdef DEBUG
#define DEBUG_LOG(fmt, ...) \ // getpid要用到
fprintf(stderr, "[DEBUG] %s:%d:%s(): " fmt "\n", __FILE__, __LINE__, \ #include <execinfo.h>
__func__, ##__VA_ARGS__) #include <unistd.h>
void print_stacktrace() {
void *buffer[100];
int size = backtrace(buffer, 100); // 注意这里是 void ** 和 int 参数
backtrace_symbols_fd(buffer, size, STDERR_FILENO);
}
#define DEBUG_LOG(fmt, ...) \
fprintf(stderr, "[DEBUG][PID %d] %s:%d:%s(): " fmt "\n", getpid(), \
__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
#define DEBUG_LOG(fmt, ...) ((void)0) #define DEBUG_LOG(fmt, ...) ((void)0)
@ -290,7 +300,7 @@ void load_config_if_needed() {
// 复制 stdout/stderr 到日志文件,同时保留终端颜色 // 复制 stdout/stderr 到日志文件,同时保留终端颜色
void duplicate_output_to_log() { void duplicate_output_to_log() {
DEBUG_LOG("Duplicating stdout/stderr to log file: %s", LOG_OUT_FILE); DEBUG_LOG("Duplicating stdout/stderr to log file: %s", LOG_OUT_FILE);
int log_fd = open(LOG_OUT_FILE, O_WRONLY | O_CREAT | O_APPEND, 0644); int log_fd = open(LOG_OUT_FILE, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (log_fd == -1) { if (log_fd == -1) {
perror("Failed to open log file"); perror("Failed to open log file");
@ -338,8 +348,7 @@ void duplicate_output_to_log() {
while ((n = read(master_fd, buffer, sizeof(buffer))) > 0) { while ((n = read(master_fd, buffer, sizeof(buffer))) > 0) {
// 检查错误 // 检查错误
if (memmem(buffer, n, "error", 5) || if (memmem(buffer, n, "error", 5) || memmem(buffer, n, "Error", 5) ||
memmem(buffer, n, "Error", 5) ||
memmem(buffer, n, "ERROR", 5)) { memmem(buffer, n, "ERROR", 5)) {
has_error = 1; has_error = 1;
} }
@ -427,13 +436,15 @@ int execve(const char *filename, char *const argv[], char *const envp[]) {
char input = getchar(); char input = getchar();
if (input != 'Y' && input != 'y') { if (input != 'Y' && input != 'y') {
printf("\nExecution cancelled.\n"); printf("\nExecution cancelled.\n");
return -1; exit(EXIT_FAILURE);
// return -1;
} }
printf("\nContinuing execution...\n"); printf("\nContinuing execution...\n");
} else if (strcmp(shared_config->rules[i].type, "error") == 0) { } else if (strcmp(shared_config->rules[i].type, "error") == 0) {
printf(ANSI_COLOR_RED "[Error] %s" ANSI_COLOR_RESET "\n", printf(ANSI_COLOR_RED "[Error] %s" ANSI_COLOR_RESET "\n",
shared_config->rules[i].msg); shared_config->rules[i].msg);
return -1; exit(EXIT_FAILURE);
// return -1;
} }
break; break;
} }
@ -490,6 +501,10 @@ __attribute__((destructor)) static void cleanup_shared_memory() {
} }
shared_config = NULL; shared_config = NULL;
} }
#ifdef DEBUG
print_stacktrace();
#endif
// if (log_fd != -1) { // if (log_fd != -1) {
// DEBUG_LOG("Closing log file descriptor."); // DEBUG_LOG("Closing log file descriptor.");
// close(log_fd); // close(log_fd);

Binary file not shown.