簡體   English   中英

如何找到在 C++ 中引發異常的位置?

[英]How do I find where an exception was thrown in C++?

我有一個程序會在某處引發未捕獲的異常。 我得到的只是一個異常被拋出的報告,並且沒有關於它被拋出的位置的信息。 編譯為包含調試符號的程序不通知我在我的代碼中生成異常的位置似乎不合邏輯。

如果沒有在 gdb 中設置“catch throw”並為每個拋出的異常調用回溯,有什么方法可以告訴我的異常來自哪里?

以下是一些可能對調試您的問題有用的信息

如果未捕獲異常,則自動調用特殊庫函數std::terminate() Terminate 實際上是一個指向函數的指針,默認值是標准 C 庫函數std::abort() 如果未對未捕獲的異常進行清理 ,則實際上可能有助於調試此問題,因為沒有調用析構函數。
†在調用std::terminate()之前堆棧是否展開是實現定義的。


abort()的調用通常可用於生成可分析以確定異常原因的核心轉儲。 確保通過ulimit -c unlimited (Linux) 啟用核心轉儲。


您可以使用std::set_terminate()安裝自己的terminate()函數。 您應該能夠在 gdb 中的終止函數上設置斷點。 可能能夠從您的terminate()函數生成堆棧回溯,並且此回溯可能有助於識別異常的位置。

Bruce Eckel 的 Thinking in C++, 2nd Ed中對未捕獲的異常進行了簡短的討論,這也可能會有所幫助。


由於terminate()默認調用abort() (默認情況下會導致SIGABRT信號),您可以設置SIGABRT處理程序,然后從信號處理程序中打印堆棧回溯 此回溯可能有助於識別異常的位置。


注意:我說可能是因為 C++ 通過使用語言結構將錯誤處理和報告代碼與普通代碼分開來支持非本地錯誤處理。 catch 塊可以並且通常位於與拋出點不同的函數/方法中。 在評論中還向我指出(感謝Dan ),在調用terminate()之前堆棧是否展開是由實現定義的。

更新:我將一個名為的 Linux 測試程序放在一起,該程序在通過set_terminate()設置的terminate()函數中生成回溯,並在SIGABRT的信號處理程序中生成另一個。 兩個回溯都正確顯示了未處理異常的位置。

更新 2:感謝有關在 terminate 中捕獲未捕獲異常的博客文章,我學到了一些新技巧; 包括在終止處理程序中重新拋出未捕獲的異常。 需要注意的是,自定義終止處理程序中的空throw語句適用於 GCC,而不是可移植的解決方案。

代碼:

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif

#include <execinfo.h>
#include <signal.h>
#include <string.h>

#include <iostream>
#include <cstdlib>
#include <stdexcept>

void my_terminate(void);

namespace {
    // invoke set_terminate as part of global constant initialization
    static const bool SET_TERMINATE = std::set_terminate(my_terminate);
}

// This structure mirrors the one found in /usr/include/asm/ucontext.h
typedef struct _sig_ucontext {
   unsigned long     uc_flags;
   struct ucontext   *uc_link;
   stack_t           uc_stack;
   struct sigcontext uc_mcontext;
   sigset_t          uc_sigmask;
} sig_ucontext_t;

void crit_err_hdlr(int sig_num, siginfo_t * info, void * ucontext) {
    sig_ucontext_t * uc = (sig_ucontext_t *)ucontext;

    // Get the address at the time the signal was raised from the EIP (x86)
    void * caller_address = (void *) uc->uc_mcontext.eip;
    
    std::cerr << "signal " << sig_num 
              << " (" << strsignal(sig_num) << "), address is " 
              << info->si_addr << " from " 
              << caller_address << std::endl;

    void * array[50];
    int size = backtrace(array, 50);

    std::cerr << __FUNCTION__ << " backtrace returned " 
              << size << " frames\n\n";

    // overwrite sigaction with caller's address
    array[1] = caller_address;

    char ** messages = backtrace_symbols(array, size);

    // skip first stack frame (points here)
    for (int i = 1; i < size && messages != NULL; ++i) {
        std::cerr << "[bt]: (" << i << ") " << messages[i] << std::endl;
    }
    std::cerr << std::endl;

    free(messages);

    exit(EXIT_FAILURE);
}

void my_terminate() {
    static bool tried_throw = false;

    try {
        // try once to re-throw currently active exception
        if (!tried_throw++) throw;
    }
    catch (const std::exception &e) {
        std::cerr << __FUNCTION__ << " caught unhandled exception. what(): "
                  << e.what() << std::endl;
    }
    catch (...) {
        std::cerr << __FUNCTION__ << " caught unknown/unhandled exception." 
                  << std::endl;
    }

    void * array[50];
    int size = backtrace(array, 50);    

    std::cerr << __FUNCTION__ << " backtrace returned " 
              << size << " frames\n\n";

    char ** messages = backtrace_symbols(array, size);

    for (int i = 0; i < size && messages != NULL; ++i) {
        std::cerr << "[bt]: (" << i << ") " << messages[i] << std::endl;
    }
    std::cerr << std::endl;

    free(messages);

    abort();
}

int throw_exception() {
    // throw an unhandled runtime error
    throw std::runtime_error("RUNTIME ERROR!");
    return 0;
}

int foo2() {
    throw_exception();
    return 0;
}

int foo1() {
    foo2();
    return 0;
}

int main(int argc, char ** argv) {
    struct sigaction sigact;

    sigact.sa_sigaction = crit_err_hdlr;
    sigact.sa_flags = SA_RESTART | SA_SIGINFO;

    if (sigaction(SIGABRT, &sigact, (struct sigaction *)NULL) != 0) {
        std::cerr << "error setting handler for signal " << SIGABRT 
                  << " (" << strsignal(SIGABRT) << ")\n";
        exit(EXIT_FAILURE);
    }

    foo1();

    exit(EXIT_SUCCESS);
}

輸出:

my_terminate caught unhanded exception. what(): RUNTIME ERROR!
my_terminate backtrace returned 10 frames

[bt]: (0) ./test(my_terminate__Fv+0x1a) [0x8048e52]
[bt]: (1) /usr/lib/libstdc++-libc6.2-2.so.3 [0x40045baa]
[bt]: (2) /usr/lib/libstdc++-libc6.2-2.so.3 [0x400468e5]
[bt]: (3) /usr/lib/libstdc++-libc6.2-2.so.3(__rethrow+0xaf) [0x40046bdf]
[bt]: (4) ./test(throw_exception__Fv+0x68) [0x8049008]
[bt]: (5) ./test(foo2__Fv+0xb) [0x8049043]
[bt]: (6) ./test(foo1__Fv+0xb) [0x8049057]
[bt]: (7) ./test(main+0xc1) [0x8049121]
[bt]: (8) ./test(__libc_start_main+0x95) [0x42017589]
[bt]: (9) ./test(__eh_alloc+0x3d) [0x8048b21]

signal 6 (Aborted), address is 0x1239 from 0x42029331
crit_err_hdlr backtrace returned 13 frames

[bt]: (1) ./test(kill+0x11) [0x42029331]
[bt]: (2) ./test(abort+0x16e) [0x4202a8c2]
[bt]: (3) ./test [0x8048f9f]
[bt]: (4) /usr/lib/libstdc++-libc6.2-2.so.3 [0x40045baa]
[bt]: (5) /usr/lib/libstdc++-libc6.2-2.so.3 [0x400468e5]
[bt]: (6) /usr/lib/libstdc++-libc6.2-2.so.3(__rethrow+0xaf) [0x40046bdf]
[bt]: (7) ./test(throw_exception__Fv+0x68) [0x8049008]
[bt]: (8) ./test(foo2__Fv+0xb) [0x8049043]
[bt]: (9) ./test(foo1__Fv+0xb) [0x8049057]
[bt]: (10) ./test(main+0xc1) [0x8049121]
[bt]: (11) ./test(__libc_start_main+0x95) [0x42017589]
[bt]: (12) ./test(__eh_alloc+0x3d) [0x8048b21]

正如你所說,我們可以在 gdb 中使用“catch throw”並為每個拋出的異常調用“backtrace”。 雖然這通常太繁瑣而無法手動完成,但 gdb 允許該過程自動化。 這允許查看所有拋出的異常的回溯,包括最后一個未捕獲的異常:

數據庫>

set pagination off
catch throw
commands
backtrace
continue
end
run

如果沒有進一步的人工干預,這會產生大量回溯,包括最后一個未捕獲的異常:

Catchpoint 1 (exception thrown), 0x00a30 in __cxa_throw () from libstdc++.so.6
#0  0x0da30 in __cxa_throw () from /usr/.../libstdc++.so.6
#1  0x021f2 in std::__throw_bad_weak_ptr () at .../shared_ptr_base.h:76
[...]
terminate called after throwing an instance of 'std::bad_weak_ptr'
  what():  bad_weak_ptr
Program received signal SIGABRT, Aborted.

這是一篇很棒的博客文章,總結了這一點:http: //741mhz.com/throw-stacktrace [onarchive.org]

您可以創建一個宏,如:

#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, __LINE__, (message) )

...它會給你拋出異常的位置(當然不是堆棧跟蹤)。 您有必要從采用上述構造函數的某些基類派生異常。

您可以將代碼中的主要緊湊位置標記為noexcept以定位異常,然后使用libunwind (只需將-lunwind添加到鏈接器參數)(使用clang++ 3.6測試):

demagle.hpp:

#pragma once

char const *
get_demangled_name(char const * const symbol) noexcept;

demangle.cpp:

#include "demangle.hpp"

#include <memory>

#include <cstdlib>

#include <cxxabi.h>

namespace
{

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
std::unique_ptr< char, decltype(std::free) & > demangled_name{nullptr, std::free};
#pragma clang diagnostic pop

}

char const *
get_demangled_name(char const * const symbol) noexcept
{
    if (!symbol) {
        return "<null>";
    }
    int status = -4;
    demangled_name.reset(abi::__cxa_demangle(symbol, demangled_name.release(), nullptr, &status));
    return ((status == 0) ? demangled_name.get() : symbol);
}

回溯.hpp:

#pragma once
#include <ostream>

void
backtrace(std::ostream & _out) noexcept;

回溯.cpp:

#include "backtrace.hpp"

#include <iostream>
#include <iomanip>
#include <limits>
#include <ostream>

#include <cstdint>

#define UNW_LOCAL_ONLY
#include <libunwind.h>

namespace
{

void
print_reg(std::ostream & _out, unw_word_t reg) noexcept
{
    constexpr std::size_t address_width = std::numeric_limits< std::uintptr_t >::digits / 4;
    _out << "0x" << std::setfill('0') << std::setw(address_width) << reg;
}

char symbol[1024];

}

void
backtrace(std::ostream & _out) noexcept
{
    unw_cursor_t cursor;
    unw_context_t context;
    unw_getcontext(&context);
    unw_init_local(&cursor, &context);
    _out << std::hex << std::uppercase;
    while (0 < unw_step(&cursor)) {
        unw_word_t ip = 0;
        unw_get_reg(&cursor, UNW_REG_IP, &ip);
        if (ip == 0) {
            break;
        }
        unw_word_t sp = 0;
        unw_get_reg(&cursor, UNW_REG_SP, &sp);
        print_reg(_out, ip);
        _out << ": (SP:";
        print_reg(_out, sp);
        _out << ") ";
        unw_word_t offset = 0;
        if (unw_get_proc_name(&cursor, symbol, sizeof(symbol), &offset) == 0) {
            _out << "(" << get_demangled_name(symbol) << " + 0x" << offset << ")\n\n";
        } else {
            _out << "-- error: unable to obtain symbol name for this frame\n\n";
        }
    }
    _out << std::flush;
}

backtrace_on_terminate.hpp:

#include "demangle.hpp"
#include "backtrace.hpp"

#include <iostream>
#include <type_traits>
#include <exception>
#include <memory>
#include <typeinfo>

#include <cstdlib>

#include <cxxabi.h>

namespace
{

[[noreturn]]
void
backtrace_on_terminate() noexcept;

static_assert(std::is_same< std::terminate_handler, decltype(&backtrace_on_terminate) >{});

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
std::unique_ptr< std::remove_pointer_t< std::terminate_handler >, decltype(std::set_terminate) & > terminate_handler{std::set_terminate(backtrace_on_terminate), std::set_terminate};
#pragma clang diagnostic pop

[[noreturn]]
void
backtrace_on_terminate() noexcept
{
    std::set_terminate(terminate_handler.release()); // to avoid infinite looping if any
    backtrace(std::clog);
    if (std::exception_ptr ep = std::current_exception()) {
        try {
            std::rethrow_exception(ep);
        } catch (std::exception const & e) {
            std::clog << "backtrace: unhandled exception std::exception:what(): " << e.what() << std::endl;
        } catch (...) {
            if (std::type_info * et = abi::__cxa_current_exception_type()) {
                std::clog << "backtrace: unhandled exception type: " << get_demangled_name(et->name()) << std::endl;
            } else {
                std::clog << "backtrace: unhandled unknown exception" << std::endl;
            }
        }
    }
    std::_Exit(EXIT_FAILURE); // change to desired return code
}

}

關於這個問題有一篇很好的文章

您沒有傳遞有關您使用的操作系統/編譯器的信息。

在 Visual Studio C++ 中可以檢測異常。

請參閱 ddj.com 上的“Visual C++ 異常處理工具”

我在 ddj.com 上的文章“事后調試”包括使用 Win32 結構化異常處理(由儀器使用)進行日志記錄等的代碼。

我在 Windows/Visual Studio 中有執行此操作的代碼,如果您需要大綱,請告訴我。 雖然不知道如何為 dwarf2 代碼執行此操作,但快速谷歌建議 libgcc 中有一個函數 _Unwind_Backtrace 可能是您需要的一部分。

檢查這個線程,也許它有幫助:

捕獲所有未處理的 C++ 異常?

我使用該軟件獲得了很好的體驗:

http://www.codeproject.com/KB/applications/blackbox.aspx

對於任何未處理的異常,它可以將堆棧跟蹤打印到文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM