error.c

来自「一个很有名的浏览器」· C语言 代码 · 共 254 行

C
254
字号
/* Error handling and debugging stuff *//* $Id: error.c,v 1.86.6.2 2005/02/28 01:20:29 jonas Exp $ */#ifndef _GNU_SOURCE#define _GNU_SOURCE /* Needed for vasprintf() */#endif#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <signal.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#include "elinks.h"#include "util/error.h"#include "util/lists.h"#include "util/memlist.h"#include "util/memory.h"#include "util/snprintf.h"#include "util/string.h"unsigned char full_static_version[1024] = "ELinks " VERSION_STRING;static voider(int bell, int shall_sleep, unsigned char *fmt, va_list params){	if (bell)#ifdef CONFIG_WIN32		MessageBeep(MB_ICONEXCLAMATION);#else		fputc(7, stderr); /* load and annoying on Windows */#endif	vfprintf(stderr, fmt, params);	fputc('\n', stderr);	fflush(stderr);	if (shall_sleep) sleep(1);}int errline;unsigned char *errfile;voidelinks_debug(unsigned char *fmt, ...){	unsigned char errbuf[4096];	va_list params;	va_start(params, fmt);	snprintf(errbuf, sizeof(errbuf), "DEBUG MESSAGE at %s:%d: %s",		 errfile, errline, fmt);	er(0, 0, errbuf, params);	va_end(params);}voidelinks_wdebug(unsigned char *fmt, ...){	unsigned char errbuf[4096];	va_list params;	va_start(params, fmt);	snprintf(errbuf, sizeof(errbuf), "DEBUG MESSAGE at %s:%d: %s",		 errfile, errline, fmt);	er(0, 1, errbuf, params);	va_end(params);}voidelinks_error(unsigned char *fmt, ...){	unsigned char errbuf[4096];	va_list params;	va_start(params, fmt);	snprintf(errbuf, sizeof(errbuf), "ERROR at %s:%d: %s",		 errfile, errline, fmt);	er(1, 1, errbuf, params);	va_end(params);}voidelinks_internal(unsigned char *fmt, ...){	unsigned char errbuf[4096];	va_list params;	va_start(params, fmt);	snprintf(errbuf, sizeof(errbuf),		 "\033[1mINTERNAL ERROR\033[0m at %s:%d: %s",		 errfile, errline, fmt);	er(1, 1, errbuf, params);	va_end(params);#ifdef CONFIG_DEBUG	force_dump();#endif}voidusrerror(unsigned char *fmt, ...){	va_list params;	va_start(params, fmt);	fputs("ELinks: ", stderr);	vfprintf(stderr, fmt, params);	fputc('\n', stderr);	fflush(stderr);	va_end(params);}int assert_failed = 0;voidelinks_assertm(int x, unsigned char *fmt, ...){	unsigned char *buf = NULL;	va_list params;	if (assert_failed) return;	if (!(assert_failed = !x)) return;	va_start(params, fmt);	vasprintf((char **) &buf, fmt, params);	va_end(params);	elinks_internal("assertion failed: %s", buf);	if (buf) free(buf);}#ifdef CONFIG_DEBUGvoidforce_dump(void){	fprintf(stderr,		"\n\033[1m%s\033[0m %s\n", "Forcing core dump!",		"Man the Lifeboats! Women and children first!\n");	fputs("But please DO NOT report this as a segfault!!! It is an internal error, not a\n"	      "normal segfault, there is a huge difference in these for us the developers.\n"	      "Also, noting the EXACT error you got above is crucial for hunting the problem\n"	      "down. Thanks, and please get in touch with us.\n",	      stderr);#ifndef CONFIG_BACKTRACE	fputs(full_static_version, stderr);	fputc('\n', stderr);#endif	fflush(stderr);	raise(SIGSEGV);}#endifvoiddo_not_optimize_here(void *p){	/* stop GCC optimization - avoid bugs in it */}#ifdef CONFIG_BACKTRACE/* The backtrace corner. */#include <stdio.h>#include <stdlib.h>#ifdef HAVE_EXECINFO_H#include <execinfo.h>#endifvoiddump_backtrace(FILE *f, int trouble){	/* If trouble is set, when we get here, we can be in various	 * interesting situations like inside of the SIGSEGV handler etc. So be	 * especially careful here.  Dynamic memory allocation may not work	 * (corrupted stack). A lot of other things may not work too. So better	 * don't do anything not 100% necessary. */#ifdef HAVE_EXECINFO_H	/* glibc way of doing this */	void *stack[20];	size_t size;	char **strings;	size_t i;	size = backtrace(stack, 20);	if (trouble) {		/* Let's hope fileno() is safe. */		backtrace_symbols_fd(stack, size, fileno(f));		/* Now out! */		return;	}	strings = backtrace_symbols(stack, size);	fprintf(f, "Obtained %d stack frames:\n", (int) size);	for (i = 0; i < size; i++)		fprintf(f, "[%p] %s\n", stack[i], strings[i]);	free(strings);#else	/* User torturation. */	/* You are worried about what you see here? You don't see anything in	 * the first place. Also, be assured that we know what are we doing. */	/* (We are killing the user, obviously.) */	/* TODO: Gettextify? Er, better not. More people (translators) could	 * find out what are we doing... ;-) --pasky */	/* TODO: Be more cruel when in trouble? ;-) --pasky */	fputs(	"Wheeeeeeeeeee! You played with the config.h by hand, didn't you?\n"		"Of _COURSE_ you did! Is that how a nice .. creature behaves like?\n"		"Of _COURSE_ it isn't! I feel offended and thus I will revenge now!\n"		"You will _suffer_ >:).\n"		"\n"		"CPU burning sequence initiated...\n", f);	/* TODO: Include cpuburn.c here. --pasky */	while (1);#endif	fflush(f);}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?