int main(int argc, char** argv) { char * filename = util_alloc_dump_filename(); if (util_file_exists(filename)) { remove(filename); } test_assert_false(util_file_exists(filename)); pid_t child_pid = fork(); if (child_pid == 0) { util_abort("I was terminated with the util_abort function"); } else { waitpid(child_pid, NULL, 0); test_assert_true(util_file_exists(filename)); free(filename); } return (EXIT_SUCCESS); }
void util_abort__(const char * file , const char * function , int line , const char * fmt , ...) { util_abort_test_intercept( function ); pthread_mutex_lock( &__abort_mutex ); /* Abort before unlock() */ { char * filename = NULL; FILE * abort_dump = NULL; if (!getenv("ERT_SHOW_BACKTRACE")) filename = util_alloc_dump_filename(); if (filename) abort_dump = fopen(filename, "w"); if (abort_dump == NULL) abort_dump = stderr; va_list ap; va_start(ap , fmt); fprintf(abort_dump , "\n\n"); fprintf(abort_dump , "Abort called from: %s (%s:%d) \n\n",function , file , line); fprintf(abort_dump , "Error message: "); vfprintf(abort_dump , fmt , ap); fprintf(abort_dump , "\n\n"); va_end(ap); /* The backtrace is based on calling the external program addr2line; the call is based on util_spawn() which is currently only available on POSIX. */ const bool include_backtrace = true; if (include_backtrace) { if (__abort_program_message != NULL) { #if !defined(__GLIBC__) /* allocate a temporary buffer to hold the path */ char* program_invocation_name = alloca (PATH_MAX); # if defined(__APPLE__) uint32_t buflen = PATH_MAX; _NSGetExecutablePath (program_invocation_name, &buflen); # elif defined(__LINUX__) readlink ("/proc/self/exe", program_invocation_name, PATH_MAX); # endif #endif /* !defined(__GLIBC__) */ fprintf(abort_dump,"--------------------------------------------------------------------------------\n"); fprintf(abort_dump,"%s",__abort_program_message); fprintf(abort_dump, "Current executable ..: %s\n" , program_invocation_name); fprintf(abort_dump,"--------------------------------------------------------------------------------\n"); } fprintf(abort_dump,"\n"); util_fprintf_backtrace( abort_dump ); } if (abort_dump != stderr) { util_fclose(abort_dump); fprintf(stderr, "\nError message: "); { va_list args; va_start(args , fmt); vfprintf(stderr , fmt , args); va_end(args); } fprintf(stderr, "\nSee file: %s for more details of the crash.\nSetting the environment variable \"ERT_SHOW_BACKTRACE\" will show the backtrace on stderr.\n", filename); } chmod(filename, 00644); // -rw-r--r-- free(filename); } pthread_mutex_unlock(&__abort_mutex); signal(SIGABRT, SIG_DFL); abort(); }