diff --git a/execve_intercept.c b/execve_intercept.c index f4dbf88..04ba7ef 100644 --- a/execve_intercept.c +++ b/execve_intercept.c @@ -4,6 +4,7 @@ #include // 添加 errno 相关定义 #include #include +#include #include // 添加 SIGCHLD 相关定义 #include // 引入 bool 类型 #include @@ -15,13 +16,22 @@ #include #include #include -#include #ifdef DEBUG -#define DEBUG_LOG(fmt, ...) \ - fprintf(stderr, "[DEBUG] %s:%d:%s(): " fmt "\n", __FILE__, __LINE__, \ - __func__, ##__VA_ARGS__) +// getpid要用到 +#include +#include + +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 #define DEBUG_LOG(fmt, ...) ((void)0) @@ -290,7 +300,7 @@ void load_config_if_needed() { // 复制 stdout/stderr 到日志文件,同时保留终端颜色 void duplicate_output_to_log() { 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); if (log_fd == -1) { perror("Failed to open log file"); @@ -338,8 +348,7 @@ void duplicate_output_to_log() { while ((n = read(master_fd, buffer, sizeof(buffer))) > 0) { // 检查错误 - if (memmem(buffer, n, "error", 5) || - memmem(buffer, n, "Error", 5) || + if (memmem(buffer, n, "error", 5) || memmem(buffer, n, "Error", 5) || memmem(buffer, n, "ERROR", 5)) { has_error = 1; } @@ -427,13 +436,15 @@ int execve(const char *filename, char *const argv[], char *const envp[]) { char input = getchar(); if (input != 'Y' && input != 'y') { printf("\nExecution cancelled.\n"); - return -1; + exit(EXIT_FAILURE); + // return -1; } printf("\nContinuing execution...\n"); } else if (strcmp(shared_config->rules[i].type, "error") == 0) { printf(ANSI_COLOR_RED "[Error] %s" ANSI_COLOR_RESET "\n", shared_config->rules[i].msg); - return -1; + exit(EXIT_FAILURE); + // return -1; } break; } @@ -490,6 +501,10 @@ __attribute__((destructor)) static void cleanup_shared_memory() { } shared_config = NULL; } +#ifdef DEBUG + print_stacktrace(); +#endif + // if (log_fd != -1) { // DEBUG_LOG("Closing log file descriptor."); // close(log_fd); diff --git a/intercept.so b/intercept.so index 0dad944..7dc80e5 100755 Binary files a/intercept.so and b/intercept.so differ