Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;获取文件名和行号;分段故障“;Linux x64_C++_Linux_64 Bit - Fatal编程技术网

C++ C++;获取文件名和行号;分段故障“;Linux x64

C++ C++;获取文件名和行号;分段故障“;Linux x64,c++,linux,64-bit,C++,Linux,64 Bit,不运行GDB,但只运行程序本身 编辑:我已经看过这样的节目,但我自己也不知道该怎么做:(为什么您不想运行gdb?如果您使用它,您可以很容易地检测到SEGFULTS的位置。为什么您不想运行gdb?如果您使用它,您可以很容易地检测到SEGFULTS的位置。嗯……您可以尝试这样做 struct sigaction g_sigact; void panic(const char *fmt, ...){ char buf[PANICBUF_LEN]; va_list argptr;

不运行GDB,但只运行程序本身


编辑:我已经看过这样的节目,但我自己也不知道该怎么做:(

为什么您不想运行gdb?如果您使用它,您可以很容易地检测到SEGFULTS的位置。

为什么您不想运行gdb?如果您使用它,您可以很容易地检测到SEGFULTS的位置。

嗯……您可以尝试这样做

struct sigaction g_sigact;

void panic(const char *fmt, ...){
    char buf[PANICBUF_LEN];
    va_list argptr;
    va_start(argptr, fmt);
    vsprintf(buf, fmt, argptr);
    va_end(argptr);
    fprintf(stderr, "%s\n", buf);
    exit(-1);
}
void init_signals(void){
    g_sigact.sa_handler = signal_handler;
    sigemptyset(&g_sigact.sa_mask);
    g_sigact.sa_flags = 0;
    sigaction(SIGINT, &g_sigact, (struct sigaction *)NULL);

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

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

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

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

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

static void signal_handler(int sig){
    if (sig == SIGHUP) g_keepRunning = 0;
    if (sig == SIGSEGV || sig == SIGBUS){
        dumpstack();
        panic("FATAL: %s Fault\n", (sig == SIGSEGV) ? "Segmentation" : ((sig == SIGBUS) ? "Bus" : "Unknown"));
    }
    if ((sig == SIGQUIT || (sig == SIGKILL) || (sig == SIGINT)) ;
}

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 -ne 'detach\n' | gdb --eval-command=where --pid=%d > %d.dump", getpid(), getpid());
    system(dbx);
    return;
}

当一个分段错误被捕获时,
dumpstack
被调用并打印最新的堆栈跟踪,直到分段错误并被重定向到一个带有进程数字pid的文件….

Hmmm…您可以尝试这样做

struct sigaction g_sigact;

void panic(const char *fmt, ...){
    char buf[PANICBUF_LEN];
    va_list argptr;
    va_start(argptr, fmt);
    vsprintf(buf, fmt, argptr);
    va_end(argptr);
    fprintf(stderr, "%s\n", buf);
    exit(-1);
}
void init_signals(void){
    g_sigact.sa_handler = signal_handler;
    sigemptyset(&g_sigact.sa_mask);
    g_sigact.sa_flags = 0;
    sigaction(SIGINT, &g_sigact, (struct sigaction *)NULL);

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

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

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

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

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

static void signal_handler(int sig){
    if (sig == SIGHUP) g_keepRunning = 0;
    if (sig == SIGSEGV || sig == SIGBUS){
        dumpstack();
        panic("FATAL: %s Fault\n", (sig == SIGSEGV) ? "Segmentation" : ((sig == SIGBUS) ? "Bus" : "Unknown"));
    }
    if ((sig == SIGQUIT || (sig == SIGKILL) || (sig == SIGINT)) ;
}

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 -ne 'detach\n' | gdb --eval-command=where --pid=%d > %d.dump", getpid(), getpid());
    system(dbx);
    return;
}

当一个分段错误被捕获时,
dumpstack
会被调用并打印最新的堆栈跟踪,直到它分段错误并被重定向到一个带有进程数字pid的文件……

Tommie解决了这个问题,值得称赞

通过使用
中的标准
backtrace
实用程序,我将他的答案与GDB分离

您可以尝试打开一个文件并进行写入,而不是
STDERR\u FILENO
,但我会避免在崩溃的过程中执行如此繁重的操作

在我的系统上,输出如下所示:

Shadow:code dkrauss$ ./dumpy 
0   dumpy                               0x0000000100000d81 dumpstack + 25
1   dumpy                               0x0000000100000d18 signal_handler + 47
2   libSystem.B.dylib                   0x00007fff86b1766a _sigtramp + 26
3   ???                                 0x0000000000000000 0x0 + 0
4   dumpy                               0x0000000100000a18 start + 52
FATAL: Segmentation Fault

因此,它没有给出文件名+行号,但给出了函数名+代码偏移量,您可以很容易地进行翻译。

Tommie解决了这个问题,值得称赞

通过使用
中的标准
backtrace
实用程序,我将他的答案与GDB分离

您可以尝试打开一个文件并进行写入,而不是
STDERR\u FILENO
,但我会避免在崩溃的过程中执行如此繁重的操作

在我的系统上,输出如下所示:

Shadow:code dkrauss$ ./dumpy 
0   dumpy                               0x0000000100000d81 dumpstack + 25
1   dumpy                               0x0000000100000d18 signal_handler + 47
2   libSystem.B.dylib                   0x00007fff86b1766a _sigtramp + 26
3   ???                                 0x0000000000000000 0x0 + 0
4   dumpy                               0x0000000100000a18 start + 52
FATAL: Segmentation Fault

因此,它没有给出文件名+行号,但给出了函数名+代码偏移量,您可以很容易地进行翻译。

这个问题在当前形式下不可回答。您的代码在哪里?相关:这个问题在当前形式下不可回答。您的代码在哪里?相关:因为我不想让任何使用该程序的人un GDB..?在某些情况下,您无法运行GDB:1)当您构建的项目的一部分由Makefile运行时。2)当嵌入低资源时,因为我不希望任何人使用该程序运行GDB..?在某些情况下,您无法运行GDB:1)当您构建的项目的一部分由Makefile运行时。2) 当嵌入low-Resources时,它在Darwin/OS X上工作。在
(sig==SIGQUIT
)之后缺少一个关闭参数,我不得不将GDB命令调整为
sprintf(dbx,“echo”attach%d\nwhere\ndetach'| GDB>%d.dump“,getpid(),getpid())
-这似乎更具可移植性。@Tenev:尝试这些修复。我已经对它进行了一些修改,不得不使用一些gcc选项来启用函数名:)已经在Darwin/OS X上运行。在
(sig==SIGQUIT
)之后,您缺少了一个关闭参数,我不得不将GDB命令调整为
sprintf(dbx),“echo'attach%d\nwhere\ndetach'| gdb>%d.dump',getpid(),getpid());
-这似乎更便于移植。@Tenev:尝试这些修复。我已经对其进行了一些修改,不得不使用一些gcc选项来启用函数名:)啊,为荣誉欢呼…有趣的回溯-不知道这个…+1来自我!:啊,为荣誉欢呼…有趣的回溯-不知道这个…+1来自我!:D