输入从日志和错误codeS除了提高code可调试 [英] Inputs for improving code debuggability apart from logs and error codes

查看:151
本文介绍了输入从日志和错误codeS除了提高code可调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了误差codeS,错误字符串和日志,还有可在code被纳入任何其他功能来提高获取调试/ code运行时跟踪信息,它可以帮助调试问题(还是让我们知道什么是在运行时)去?

Apart from error codes, error strings and logs, are there any other features which can be incorporated in the code to increase getting debug / trace information during code runtime which can help debug issues (or let us know what is going on) at runtime?

推荐答案

这里的code,它在分段错误发送堆栈跟踪到一个文件的例子

Here's an example of the code that sends a stacktrace to a file upon a segmentation fault


#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <stdarg.h>

static void signal_handler(int);
static void dumpstack(void);
static void cleanup(void);
void init_signals(void);
void panic(const char *, ...);

struct sigaction sigact;
char *progname;

int main(int argc, char **argv){
    char *s;
    progname = *(argv);
    atexit(cleanup);
    init_signals();
    printf("About to seg fault by assigning zero to *s\n");
    *s = 0;
    sigemptyset(&sigact.sa_mask);
    return 0;
}

void init_signals(void){
    sigact.sa_handler = signal_handler;
    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = 0;
    sigaction(SIGINT, &sigact, (struct sigaction *)NULL);

    sigaddset(&sigact.sa_mask, SIGSEGV);
    sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL);

    sigaddset(&sigact.sa_mask, SIGBUS);
    sigaction(SIGBUS, &sigact, (struct sigaction *)NULL);

    sigaddset(&sigact.sa_mask, SIGQUIT);
    sigaction(SIGQUIT, &sigact, (struct sigaction *)NULL);

    sigaddset(&sigact.sa_mask, SIGHUP);
    sigaction(SIGHUP, &sigact, (struct sigaction *)NULL);

    sigaddset(&sigact.sa_mask, SIGKILL);
    sigaction(SIGKILL, &sigact, (struct sigaction *)NULL);
}

static void signal_handler(int sig){
    if (sig == SIGHUP) panic("FATAL: Program hanged up\n");
    if (sig == SIGSEGV || sig == SIGBUS){
        dumpstack();
        panic("FATAL: %s Fault. Logged StackTrace\n", (sig == SIGSEGV) ? "Segmentation" : ((sig == SIGBUS) ? "Bus" : "Unknown"));
    }
    if (sig == SIGQUIT) panic("QUIT signal ended program\n");
    if (sig == SIGKILL) panic("KILL signal ended program\n");
    if (sig == SIGINT) ;
}

void panic(const char *fmt, ...){
    char buf[50];
    va_list argptr;
    va_start(argptr, fmt);
    vsprintf(buf, fmt, argptr);
    va_end(argptr);
    fprintf(stderr, buf);
    exit(-1);
}

static void dumpstack(void){
    /* Got this routine from http://www.whitefang.com/unix/faq_toc.html
    ** Section 6.5. Modified to redirect to file to prevent clutter
    */
    char dbx[160];
    sprintf(dbx, "echo 'where\ndetach' | dbx -a %d > %s.dump", getpid(), progname);
    system(dbx);
    return;
}

void cleanup(void){
    sigemptyset(&sigact.sa_mask);
    /* Do any cleaning up chores here */
}

在功能 dumpstack DBX 需要改变,以满足您的调试器,如 GDB 的GNU调试器,是当我在AIX上编程盒采用了几年前这个code。注意信号是如何设置的,如果出现故障SIGSEGV,处理程序转储堆栈与扩展使用.dump 的文件。在code演示了段故障,并转储堆栈跟踪。

In the function dumpstack, dbx needs to be changed to suit your debugger, such as gdb for the GNU Debugger, this code was used when I was programming on AIX box a few years ago. Notice how the signals are set up, and if a SIGSEGV fault occurs, the handler dumps the stack to a file with extension .dump. The code demonstrates the segmentation fault and dumps the stacktrace.

这是我最喜欢的code。

That is my favourite code.

希望这有助于
诚挚的问候,
汤姆。

Hope this helps, Best regards, Tom.

这篇关于输入从日志和错误codeS除了提高code可调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆