当前位置: 技术问答>linux和unix
怎么知道程序当中引起异常的地方?
来源: 互联网 发布时间:2016-03-06
本文导语: 在Linux 中,如果我们的程序出现了异常,LINUX 内核会给我们程序发一些特定的信号来控制我们的程序,要么让我们程序终止,要么进行其他的操作。现在我想问大家的是,我自己在程序中定义了信号处理函数来处理...
在Linux 中,如果我们的程序出现了异常,LINUX 内核会给我们程序发一些特定的信号来控制我们的程序,要么让我们程序终止,要么进行其他的操作。现在我想问大家的是,我自己在程序中定义了信号处理函数来处理,怎么能知道出现异常的位置呢?(精确到一个文件就可以了,不用其他的工具)
举个例子,下面的程序怎么能让 系统打印出出错的文件及行号呢?
#include
#include
#include
#include
void write_log_with_time(char *error)
{//Write the log string to the log file.
long timezone = 8;
time_t timep;
char *time_str = NULL;
time(&timep);
FILE *fp=fopen("sys_log","a+");
time_str = asctime(localtime(&timep));
fprintf(fp,"%s%sn",time_str,error);
fclose(fp);
exit(0);
}
void abrt_handler(int sig)
{
write_log_with_time("aborted with some exception");
}
void fpe_handler(int sig)
{
write_log_with_time("arithmetic exception");
}
void io_handler(int sig)
{
write_log_with_time("io exception");
}
void poll_handler(int sig)
{
write_log_with_time("poll exception");
}
void segv_handler(int sig)
{
write_log_with_time("access the invalidation's district");
}
void sys_handler(int sig)
{
write_log_with_time("invalid system called");
}
void xcpu_handler(int sig)
{
write_log_with_time("exceed the cpu's limit");
}
void xfsz_handler(int sig)
{
write_log_with_time("exceed the file's limit length");
}
int main()
{char hanframe_buf[10]="456789";
int hanframe[3]={1,2,3},i;
char *p=NULL;
signal(SIGABRT,abrt_handler);
signal(SIGFPE,fpe_handler);
signal(SIGIO,io_handler);
signal(SIGPOLL,poll_handler);
signal(SIGSEGV,segv_handler);
signal(SIGSYS,sys_handler);
signal(SIGXCPU,xcpu_handler);
signal(SIGXFSZ,xfsz_handler);
//strcpy(p,"23456");
i = 100/0;
puts("test 1n");
// hanframe[10]=100;
puts("test 2n");
// hanframe_buf[100]='';
puts("test 3n");
return 0;
}
举个例子,下面的程序怎么能让 系统打印出出错的文件及行号呢?
#include
#include
#include
#include
void write_log_with_time(char *error)
{//Write the log string to the log file.
long timezone = 8;
time_t timep;
char *time_str = NULL;
time(&timep);
FILE *fp=fopen("sys_log","a+");
time_str = asctime(localtime(&timep));
fprintf(fp,"%s%sn",time_str,error);
fclose(fp);
exit(0);
}
void abrt_handler(int sig)
{
write_log_with_time("aborted with some exception");
}
void fpe_handler(int sig)
{
write_log_with_time("arithmetic exception");
}
void io_handler(int sig)
{
write_log_with_time("io exception");
}
void poll_handler(int sig)
{
write_log_with_time("poll exception");
}
void segv_handler(int sig)
{
write_log_with_time("access the invalidation's district");
}
void sys_handler(int sig)
{
write_log_with_time("invalid system called");
}
void xcpu_handler(int sig)
{
write_log_with_time("exceed the cpu's limit");
}
void xfsz_handler(int sig)
{
write_log_with_time("exceed the file's limit length");
}
int main()
{char hanframe_buf[10]="456789";
int hanframe[3]={1,2,3},i;
char *p=NULL;
signal(SIGABRT,abrt_handler);
signal(SIGFPE,fpe_handler);
signal(SIGIO,io_handler);
signal(SIGPOLL,poll_handler);
signal(SIGSEGV,segv_handler);
signal(SIGSYS,sys_handler);
signal(SIGXCPU,xcpu_handler);
signal(SIGXFSZ,xfsz_handler);
//strcpy(p,"23456");
i = 100/0;
puts("test 1n");
// hanframe[10]=100;
puts("test 2n");
// hanframe_buf[100]='';
puts("test 3n");
return 0;
}
|
那里有gdb那么复杂? gdb用的是ptrace.
#include
#include
#include
#include
#include
#include "bfd.h"
static bfd_vma pc;
static const char *filename;
static const char *functionname;
static unsigned int line;
static bfd_boolean found;
static char *file_name=NULL;
static asymbol **syms; /* Symbol table. */
static void find_address_in_section (bfd *abfd, asection *section, void *data)
{
bfd_vma vma;
bfd_size_type size;
if (found)
return;
if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
return;
vma = bfd_get_section_vma (abfd, section);
if (pc = vma + size)
return;
found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
&filename, &functionname, &line);
}
void write_log_with_time(char *error)
{//Write the log string to the log file.
long timezone = 8;
time_t timep;
char *time_str = NULL;
char **matching=NULL;
void *addr[60];
bfd *abfd;
int i;
time(&timep);
FILE *fp=fopen("sys_log","a+");
time_str = asctime(localtime(&timep));
fprintf(fp,"%s%sn",time_str,error);
memset(addr, 0, sizeof(addr));
backtrace(addr, 60);
for (i=0; i
#include
#include
#include
#include
#include
#include "bfd.h"
static bfd_vma pc;
static const char *filename;
static const char *functionname;
static unsigned int line;
static bfd_boolean found;
static char *file_name=NULL;
static asymbol **syms; /* Symbol table. */
static void find_address_in_section (bfd *abfd, asection *section, void *data)
{
bfd_vma vma;
bfd_size_type size;
if (found)
return;
if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
return;
vma = bfd_get_section_vma (abfd, section);
if (pc = vma + size)
return;
found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
&filename, &functionname, &line);
}
void write_log_with_time(char *error)
{//Write the log string to the log file.
long timezone = 8;
time_t timep;
char *time_str = NULL;
char **matching=NULL;
void *addr[60];
bfd *abfd;
int i;
time(&timep);
FILE *fp=fopen("sys_log","a+");
time_str = asctime(localtime(&timep));
fprintf(fp,"%s%sn",time_str,error);
memset(addr, 0, sizeof(addr));
backtrace(addr, 60);
for (i=0; i