From d4329ae594deab04ab7afde7219ec8a575161a43 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sat, 14 Jan 2023 12:19:25 +0100 Subject: [PATCH] Show file:line where panic() was called from --- src/p2p_server.cpp | 12 ++++++------ src/p2pool.cpp | 12 ++++++------ src/p2pool_api.cpp | 8 ++++---- src/pool_block.cpp | 4 ++-- src/pow_hash.cpp | 10 +++++----- src/side_chain.cpp | 8 ++++---- src/stratum_server.cpp | 2 +- src/tcp_server.inl | 28 ++++++++++++++-------------- src/util.cpp | 18 +++++++----------- src/util.h | 7 ++++++- 10 files changed, 55 insertions(+), 54 deletions(-) diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index ae82eba..0ec7735 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -75,7 +75,7 @@ P2PServer::P2PServer(p2pool* pool) [this](bool is_v6, const std::string& /*address*/, const std::string& ip, int port) { if (!str_to_ip(is_v6, ip.c_str(), m_socks5ProxyIP)) { - panic(); + PANIC_STOP(); } m_socks5ProxyV6 = is_v6; m_socks5ProxyPort = port; @@ -95,7 +95,7 @@ P2PServer::P2PServer(p2pool* pool) int err = uv_async_init(&m_loop, &m_broadcastAsync, on_broadcast); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } m_broadcastAsync.data = this; m_broadcastQueue.reserve(2); @@ -103,21 +103,21 @@ P2PServer::P2PServer(p2pool* pool) err = uv_async_init(&m_loop, &m_connectToPeersAsync, on_connect_to_peers); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } m_connectToPeersAsync.data = this; err = uv_async_init(&m_loop, &m_showPeersAsync, on_show_peers); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } m_showPeersAsync.data = this; err = uv_timer_init(&m_loop, &m_timer); if (err) { LOGERR(1, "failed to create timer, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } if (m_cache) { @@ -130,7 +130,7 @@ P2PServer::P2PServer(p2pool* pool) err = uv_timer_start(&m_timer, on_timer, 1000, m_timerInterval * 1000); if (err) { LOGERR(1, "failed to start timer, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } load_peer_list(); diff --git a/src/p2pool.cpp b/src/p2pool.cpp index d1dbb7c..0d2bf00 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -701,14 +701,14 @@ void p2pool::download_block_headers(uint64_t current_height) } else { LOGERR(1, "fatal error: couldn't download block header for seed height " << height); - panic(); + PANIC_STOP(); } }, [height](const char* data, size_t size) { if (size > 0) { LOGERR(1, "fatal error: couldn't download block header for seed height " << height << ", error " << log::const_buf(data, size)); - panic(); + PANIC_STOP(); } }); } @@ -729,7 +729,7 @@ void p2pool::download_block_headers(uint64_t current_height) } catch (const std::exception& e) { LOGERR(1, "Couldn't start ZMQ reader: exception " << e.what()); - panic(); + PANIC_STOP(); } m_stratumServer = new StratumServer(this); @@ -932,7 +932,7 @@ void p2pool::parse_get_info_rpc(const char* data, size_t size) if (monero_network != sidechain_network) { LOGERR(1, "monerod is on " << monero_network << ", but you're mining to a " << sidechain_network << " sidechain"); - panic(); + PANIC_STOP(); } get_version(); @@ -1002,7 +1002,7 @@ void p2pool::parse_get_version_rpc(const char* data, size_t size) const uint64_t required_version_hi = required >> 16; const uint64_t required_version_lo = required & 65535; LOGERR(1, "monerod RPC v" << version_hi << '.' << version_lo << " is incompatible, update to RPC >= v" << required_version_hi << '.' << required_version_lo << " (Monero v0.18.0.0 or newer)"); - panic(); + PANIC_STOP(); } get_miner_data(); @@ -1577,7 +1577,7 @@ int p2pool::run() catch (const std::exception& e) { const char* s = e.what(); LOGERR(1, "exception " << s); - panic(); + PANIC_STOP(); } m_stopped = true; diff --git a/src/p2pool_api.cpp b/src/p2pool_api.cpp index 6cf56c6..b26704d 100644 --- a/src/p2pool_api.cpp +++ b/src/p2pool_api.cpp @@ -34,7 +34,7 @@ p2pool_api::p2pool_api(const std::string& api_path, const bool local_stats) { if (m_apiPath.empty()) { LOGERR(1, "api path is empty"); - panic(); + PANIC_STOP(); } if ((m_apiPath.back() != '/') @@ -48,13 +48,13 @@ p2pool_api::p2pool_api(const std::string& api_path, const bool local_stats) struct stat buf; if (stat(m_apiPath.c_str(), &buf) != 0) { LOGERR(1, "path " << m_apiPath << " doesn't exist"); - panic(); + PANIC_STOP(); } int result = uv_async_init(uv_default_loop_checked(), &m_dumpToFileAsync, on_dump_to_file); if (result) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(result)); - panic(); + PANIC_STOP(); } m_dumpToFileAsync.data = this; @@ -93,7 +93,7 @@ void p2pool_api::create_dir(const std::string& path) result = errno; if (result != EEXIST) { LOGERR(1, "mkdir(" << path << ") failed, error " << result); - panic(); + PANIC_STOP(); } } } diff --git a/src/pool_block.cpp b/src/pool_block.cpp index c481e23..57e5102 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -226,7 +226,7 @@ std::vector PoolBlock::serialize_mainchain_data_nolock(size_t* header_s #if POOL_BLOCK_DEBUG if (!nonce && !extra_nonce && !m_mainChainDataDebug.empty() && (data != m_mainChainDataDebug)) { LOGERR(1, "serialize_mainchain_data() has a bug, fix it!"); - panic(); + PANIC_STOP(); } #endif @@ -280,7 +280,7 @@ std::vector PoolBlock::serialize_sidechain_data() const #if POOL_BLOCK_DEBUG if (!m_sideChainDataDebug.empty() && (data != m_sideChainDataDebug)) { LOGERR(1, "serialize_sidechain_data() has a bug, fix it!"); - panic(); + PANIC_STOP(); } #endif diff --git a/src/pow_hash.cpp b/src/pow_hash.cpp index 9b20c1f..4713577 100644 --- a/src/pow_hash.cpp +++ b/src/pow_hash.cpp @@ -68,7 +68,7 @@ RandomX_Hasher::RandomX_Hasher(p2pool* pool) m_cache[i] = randomx_alloc_cache(flags); if (!m_cache[i]) { LOGERR(1, "couldn't allocate RandomX cache, aborting"); - panic(); + PANIC_STOP(); } } memory_allocated += RANDOMX_ARGON_MEMORY * 1024; @@ -206,7 +206,7 @@ void RandomX_Hasher::set_seed(const hash& seed) m_vm[m_index].vm = randomx_create_vm(flags, m_cache[m_index], nullptr); if (!m_vm[m_index].vm) { LOGERR(1, "couldn't allocate RandomX light VM, aborting"); - panic(); + PANIC_STOP(); } } } @@ -308,7 +308,7 @@ void RandomX_Hasher::set_old_seed(const hash& seed) m_vm[old_index].vm = randomx_create_vm(flags, m_cache[old_index], nullptr); if (!m_vm[old_index].vm) { LOGERR(1, "couldn't allocate RandomX light VM, aborting"); - panic(); + PANIC_STOP(); } } } @@ -376,7 +376,7 @@ RandomX_Hasher_RPC::RandomX_Hasher_RPC(p2pool* pool) int err = uv_loop_init(&m_loop); if (err) { LOGERR(1, "failed to create event loop, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } // Init loop user data before running it @@ -394,7 +394,7 @@ RandomX_Hasher_RPC::RandomX_Hasher_RPC(p2pool* pool) err = uv_thread_create(&m_loopThread, loop, this); if (err) { LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } } diff --git a/src/side_chain.cpp b/src/side_chain.cpp index cf2be14..6f957ae 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -70,11 +70,11 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name) LOGINFO(1, log::LightCyan() << "network type = " << m_networkType); if (m_pool && !load_config(m_pool->params().m_config)) { - panic(); + PANIC_STOP(); } if (!check_config()) { - panic(); + PANIC_STOP(); } uv_rwlock_init_checked(&m_sidechainLock); @@ -117,7 +117,7 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name) cache = randomx_alloc_cache(flags); if (!cache) { LOGERR(1, "couldn't allocate RandomX cache, aborting"); - panic(); + PANIC_STOP(); } } @@ -146,7 +146,7 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name) m_consensusId.assign(id.h, id.h + HASH_SIZE); #else LOGERR(1, "Can't calculate consensus ID without RandomX library"); - panic(); + PANIC_STOP(); #endif } diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index 366bf11..6ff2913 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -76,7 +76,7 @@ StratumServer::StratumServer(p2pool* pool) const int err = uv_async_init(&m_loop, &m_blobsAsync, on_blobs_ready); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } m_blobsAsync.data = this; m_blobsQueue.reserve(2); diff --git a/src/tcp_server.inl b/src/tcp_server.inl index 8731585..e82d4d2 100644 --- a/src/tcp_server.inl +++ b/src/tcp_server.inl @@ -41,7 +41,7 @@ TCPServer::TCPServer(allocate_client_callback all int err = uv_loop_init(&m_loop); if (err) { LOGERR(1, "failed to create event loop, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } // Init loop user data before running it @@ -50,14 +50,14 @@ TCPServer::TCPServer(allocate_client_callback all err = uv_async_init(&m_loop, &m_dropConnectionsAsync, on_drop_connections); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } m_dropConnectionsAsync.data = this; err = uv_async_init(&m_loop, &m_shutdownAsync, on_shutdown); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } m_shutdownAsync.data = this; @@ -133,7 +133,7 @@ void TCPServer::start_listening(const std::string { if (listen_addresses.empty()) { LOGERR(1, "listen address not set"); - panic(); + PANIC_STOP(); } parse_address_list(listen_addresses, @@ -144,7 +144,7 @@ void TCPServer::start_listening(const std::string } else if (m_listenPort != port) { LOGERR(1, "all sockets must be listening on the same port number, fix the command line"); - panic(); + PANIC_STOP(); } uv_tcp_t* socket = new uv_tcp_t(); @@ -159,14 +159,14 @@ void TCPServer::start_listening(const std::string int err = uv_tcp_init(&m_loop, socket); if (err) { LOGERR(1, "failed to create tcp server handle, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } socket->data = this; err = uv_tcp_nodelay(socket, 1); if (err) { LOGERR(1, "failed to set tcp_nodelay on tcp server handle, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } if (is_v6) { @@ -174,13 +174,13 @@ void TCPServer::start_listening(const std::string err = uv_ip6_addr(ip.c_str(), port, &addr6); if (err) { LOGERR(1, "failed to parse IPv6 address " << ip << ", error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } err = uv_tcp_bind(socket, reinterpret_cast(&addr6), UV_TCP_IPV6ONLY); if (err) { LOGERR(1, "failed to bind tcp server IPv6 socket " << address << ", error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } } else { @@ -188,20 +188,20 @@ void TCPServer::start_listening(const std::string err = uv_ip4_addr(ip.c_str(), port, &addr); if (err) { LOGERR(1, "failed to parse IPv4 address " << ip << ", error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } err = uv_tcp_bind(socket, reinterpret_cast(&addr), 0); if (err) { LOGERR(1, "failed to bind tcp server IPv4 socket " << address << ", error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } } err = uv_listen(reinterpret_cast(socket), DEFAULT_BACKLOG, on_new_connection); if (err) { LOGERR(1, "failed to listen on tcp server socket " << address << ", error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } LOGINFO(1, "listening on " << log::Gray() << address); @@ -210,7 +210,7 @@ void TCPServer::start_listening(const std::string const int err = uv_thread_create(&m_loopThread, loop, this); if (err) { LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } } @@ -509,7 +509,7 @@ bool TCPServer::send_internal(Client* client, Sen if (bytes_written > WRITE_BUF_SIZE) { LOGERR(0, "send callback wrote " << bytes_written << " bytes, expected no more than " << WRITE_BUF_SIZE << " bytes"); - panic(); + PANIC_STOP(); } if (bytes_written == 0) { diff --git a/src/util.cpp b/src/util.cpp index f0eb874..a8f0a00 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -29,9 +29,6 @@ static constexpr char log_category_prefix[] = "Util "; namespace p2pool { -#define STR2(X) STR(X) -#define STR(X) #X - const char* VERSION = "v" STR2(P2POOL_VERSION_MAJOR) "." STR2(P2POOL_VERSION_MINOR) " (built" #if defined(__clang__) " with clang/" __clang_version__ @@ -42,13 +39,12 @@ const char* VERSION = "v" STR2(P2POOL_VERSION_MAJOR) "." STR2(P2POOL_VERSION_MIN #endif " on " __DATE__ ")"; -#undef STR2 -#undef STR - MinerCallbackHandler::~MinerCallbackHandler() {} -void panic() +void panic_stop(const char* message) { + fprintf(stderr, "P2Pool can't continue execution: panic at %s\n", message); + p2pool::log::stop(); do { #ifdef _WIN32 @@ -254,7 +250,7 @@ void uv_cond_init_checked(uv_cond_t* cond) const int result = uv_cond_init(cond); if (result) { LOGERR(1, "failed to create conditional variable, error " << uv_err_name(result)); - panic(); + PANIC_STOP(); } } @@ -263,7 +259,7 @@ void uv_mutex_init_checked(uv_mutex_t* mutex) const int result = uv_mutex_init(mutex); if (result) { LOGERR(1, "failed to create mutex, error " << uv_err_name(result)); - panic(); + PANIC_STOP(); } } @@ -272,7 +268,7 @@ void uv_rwlock_init_checked(uv_rwlock_t* lock) const int result = uv_rwlock_init(lock); if (result) { LOGERR(1, "failed to create rwlock, error " << uv_err_name(result)); - panic(); + PANIC_STOP(); } } @@ -281,7 +277,7 @@ void uv_async_init_checked(uv_loop_t* loop, uv_async_t* async, uv_async_cb async const int err = uv_async_init(loop, async, async_cb); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + PANIC_STOP(); } } diff --git a/src/util.h b/src/util.h index 1e39d61..c33e079 100644 --- a/src/util.h +++ b/src/util.h @@ -149,7 +149,12 @@ FORCEINLINE T read_unaligned(const T* p) template FORCEINLINE constexpr size_t array_size(T(&)[N]) { return N; } template FORCEINLINE constexpr size_t array_size(T(U::*)[N]) { return N; } -[[noreturn]] void panic(); +[[noreturn]] void panic_stop(const char* message); + +#define STR(X) #X +#define STR2(X) STR(X) + +#define PANIC_STOP(...) panic_stop(__FILE__ ":" STR2(__LINE__)) void make_thread_background();