Skip to content

Commit

Permalink
Add debug/release_assert overloads which output error message strings (
Browse files Browse the repository at this point in the history
…#3077)

* Add debug/release_assert overloads which accept error messages

* Keep release and debug related code together
  • Loading branch information
wezrule authored Feb 2, 2021
1 parent 8ce7612 commit af28d69
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 44 deletions.
9 changes: 7 additions & 2 deletions nano/lib/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ void nano::move_all_files_to_dir (boost::filesystem::path const & from, boost::f
/*
* Backing code for "release_assert" & "debug_assert", which are macros
*/
void assert_internal (const char * check_expr, const char * func, const char * file, unsigned int line, bool is_release_assert)
void assert_internal (const char * check_expr, const char * func, const char * file, unsigned int line, bool is_release_assert, std::string_view error_msg)
{
std::cerr << "Assertion (" << check_expr << ") failed\n"
<< func << "\n"
<< file << ":" << line << "\n\n";
<< file << ":" << line << "\n";
if (!error_msg.empty ())
{
std::cerr << "Error: " << error_msg << "\n";
}
std::cerr << "\n";

// Output stack trace to cerr
auto backtrace_str = nano::generate_stacktrace ();
Expand Down
25 changes: 22 additions & 3 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <nano/lib/locks.hpp>

#include <boost/current_function.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/preprocessor/facilities/overload.hpp>

#include <cassert>
#include <functional>
Expand All @@ -22,13 +24,30 @@ namespace system
}
}

void assert_internal (const char * check_expr, const char * func, const char * file, unsigned int line, bool is_release_assert);
#define release_assert(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true)
void assert_internal (const char * check_expr, const char * func, const char * file, unsigned int line, bool is_release_assert, std::string_view error = "");

#define release_assert_1(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true)
#define release_assert_2(check, error_msg) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true, error_msg)
#if !BOOST_PP_VARIADICS_MSVC
#define release_assert(...) \
BOOST_PP_OVERLOAD (release_assert_, __VA_ARGS__) \
(__VA_ARGS__)
#else
#define release_assert(...) BOOST_PP_CAT (BOOST_PP_OVERLOAD (release_assert_, __VA_ARGS__) (__VA_ARGS__), BOOST_PP_EMPTY ())
#endif

#ifdef NDEBUG
#define debug_assert(check) (void)0
#else
#define debug_assert(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, false)
#define debug_assert_1(check) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, false)
#define debug_assert_2(check, error_msg) check ? (void)0 : assert_internal (#check, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, false, error_msg)
#if !BOOST_PP_VARIADICS_MSVC
#define debug_assert(...) \
BOOST_PP_OVERLOAD (debug_assert_, __VA_ARGS__) \
(__VA_ARGS__)
#else
#define debug_assert(...) BOOST_PP_CAT (BOOST_PP_OVERLOAD (debug_assert_, __VA_ARGS__) (__VA_ARGS__), BOOST_PP_EMPTY ())
#endif
#endif

namespace nano
Expand Down
33 changes: 19 additions & 14 deletions nano/node/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void nano::mdb_store::upgrade_v14_to_v15 (nano::write_transaction & transaction_

nano::mdb_val value{ data.size (), (void *)data.data () };
auto s = mdb_put (env.tx (transaction_a), state_blocks_new, nano::mdb_val (hash), value, MDB_APPEND);
release_assert (success (s));
release_assert_success (s);

// Every so often output to the log to indicate progress
constexpr auto output_cutoff = 1000000;
Expand Down Expand Up @@ -483,7 +483,7 @@ void nano::mdb_store::upgrade_v16_to_v17 (nano::write_transaction const & transa

// Clear it then append
auto status (mdb_drop (env.tx (transaction_a), confirmation_height, 0));
release_assert (status == MDB_SUCCESS);
release_assert_success (status);

for (auto const & confirmation_height_info_pair : confirmation_height_infos)
{
Expand Down Expand Up @@ -539,7 +539,7 @@ void nano::mdb_store::upgrade_v17_to_v18 (nano::write_transaction const & transa
}
nano::mdb_val value{ data.size (), (void *)data.data () };
auto s = mdb_cursor_put (state_i.cursor, state_i->first, value, MDB_CURRENT);
release_assert (success (s));
release_assert_success (s);

// Every so often output to the log to indicate progress
constexpr auto output_cutoff = 1000000;
Expand Down Expand Up @@ -609,7 +609,7 @@ void nano::mdb_store::upgrade_v18_to_v19 (nano::write_transaction const & transa

nano::mdb_val value{ data.size (), (void *)data.data () };
auto s = mdb_put (env.tx (transaction_a), temp_legacy_open_receive_change_blocks, nano::mdb_val (legacy_block.first), value, MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}
}

Expand All @@ -633,7 +633,7 @@ void nano::mdb_store::upgrade_v18_to_v19 (nano::write_transaction const & transa

nano::mdb_val value{ data.size (), (void *)data.data () };
auto s = mdb_put (env.tx (transaction_a), temp_legacy_send_blocks, nano::mdb_val (i->first), value, MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}
}

Expand All @@ -651,7 +651,7 @@ void nano::mdb_store::upgrade_v18_to_v19 (nano::write_transaction const & transa
for (; i != n; ++i)
{
auto s = mdb_put (env.tx (transaction_a), temp_legacy_send_open_receive_change_blocks, nano::mdb_val (i->first), nano::mdb_val (i->second), MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}

// Delete tables
Expand Down Expand Up @@ -698,7 +698,7 @@ void nano::mdb_store::upgrade_v18_to_v19 (nano::write_transaction const & transa

nano::mdb_val value{ data.size (), (void *)data.data () };
auto s = mdb_put (env.tx (transaction_a), temp_state_blocks, nano::mdb_val (i->first), value, MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}
}

Expand All @@ -714,7 +714,7 @@ void nano::mdb_store::upgrade_v18_to_v19 (nano::write_transaction const & transa
for (; i != n; ++i)
{
auto s = mdb_put (env.tx (transaction_a), blocks, nano::mdb_val (i->first), nano::mdb_val (i->second), MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}

// Delete tables
Expand Down Expand Up @@ -787,7 +787,7 @@ void nano::mdb_store::version_put (nano::write_transaction const & transaction_a
nano::uint256_union version_key (1);
nano::uint256_union version_value (version_a);
auto status (mdb_put (env.tx (transaction_a), meta, nano::mdb_val (version_key), nano::mdb_val (version_value), 0));
release_assert (status == 0);
release_assert_success (status);
}

bool nano::mdb_store::exists (nano::transaction const & transaction_a, tables table_a, nano::mdb_val const & key_a) const
Expand Down Expand Up @@ -832,7 +832,7 @@ uint64_t nano::mdb_store::count (nano::transaction const & transaction_a, MDB_db
{
MDB_stat stats;
auto status (mdb_stat (env.tx (transaction_a), db_a, &stats));
release_assert (status == 0);
release_assert_success (status);
return (stats.ms_entries);
}

Expand Down Expand Up @@ -881,6 +881,11 @@ int nano::mdb_store::status_code_not_found () const
return MDB_NOTFOUND;
}

std::string nano::mdb_store::error_string (int status) const
{
return mdb_strerror (status);
}

bool nano::mdb_store::copy_db (boost::filesystem::path const & destination_file)
{
return !mdb_env_copy2 (env.environment, destination_file.string ().c_str (), MDB_CP_COMPACT);
Expand All @@ -898,7 +903,7 @@ void nano::mdb_store::rebuild_db (nano::write_transaction const & transaction_a)
for (auto i (nano::store_iterator<nano::uint256_union, nano::mdb_val> (std::make_unique<nano::mdb_iterator<nano::uint256_union, nano::mdb_val>> (transaction_a, table))), n (nano::store_iterator<nano::uint256_union, nano::mdb_val> (nullptr)); i != n; ++i)
{
auto s = mdb_put (env.tx (transaction_a), temp, nano::mdb_val (i->first), i->second, MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}
release_assert (count (transaction_a, table) == count (transaction_a, temp));
// Clear existing table
Expand All @@ -907,7 +912,7 @@ void nano::mdb_store::rebuild_db (nano::write_transaction const & transaction_a)
for (auto i (nano::store_iterator<nano::uint256_union, nano::mdb_val> (std::make_unique<nano::mdb_iterator<nano::uint256_union, nano::mdb_val>> (transaction_a, temp))), n (nano::store_iterator<nano::uint256_union, nano::mdb_val> (nullptr)); i != n; ++i)
{
auto s = mdb_put (env.tx (transaction_a), table, nano::mdb_val (i->first), i->second, MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}
release_assert (count (transaction_a, table) == count (transaction_a, temp));
// Remove temporary table
Expand All @@ -921,15 +926,15 @@ void nano::mdb_store::rebuild_db (nano::write_transaction const & transaction_a)
for (auto i (nano::store_iterator<nano::pending_key, nano::pending_info> (std::make_unique<nano::mdb_iterator<nano::pending_key, nano::pending_info>> (transaction_a, pending))), n (nano::store_iterator<nano::pending_key, nano::pending_info> (nullptr)); i != n; ++i)
{
auto s = mdb_put (env.tx (transaction_a), temp, nano::mdb_val (i->first), nano::mdb_val (i->second), MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}
release_assert (count (transaction_a, pending) == count (transaction_a, temp));
mdb_drop (env.tx (transaction_a), pending, 0);
// Put values from copy
for (auto i (nano::store_iterator<nano::pending_key, nano::pending_info> (std::make_unique<nano::mdb_iterator<nano::pending_key, nano::pending_info>> (transaction_a, temp))), n (nano::store_iterator<nano::pending_key, nano::pending_info> (nullptr)); i != n; ++i)
{
auto s = mdb_put (env.tx (transaction_a), pending, nano::mdb_val (i->first), nano::mdb_val (i->second), MDB_APPEND);
release_assert (success (s));
release_assert_success (s);
}
release_assert (count (transaction_a, pending) == count (transaction_a, temp));
mdb_drop (env.tx (transaction_a), temp, 1);
Expand Down
1 change: 1 addition & 0 deletions nano/node/lmdb/lmdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class mdb_store : public block_store_partial<MDB_val, mdb_store>
bool init_error () const override;

uint64_t count (nano::transaction const &, MDB_dbi) const;
std::string error_string (int status) const override;

// These are only use in the upgrade process.
std::shared_ptr<nano::block> block_get_v14 (nano::transaction const & transaction_a, nano::block_hash const & hash_a, nano::block_sideband_v14 * sideband_a = nullptr, bool * is_state_v1 = nullptr) const;
Expand Down
4 changes: 2 additions & 2 deletions nano/node/lmdb/lmdb_txn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void nano::write_mdb_txn::commit ()
if (active)
{
auto status (mdb_txn_commit (handle));
release_assert (status == MDB_SUCCESS);
release_assert (status == MDB_SUCCESS, mdb_strerror (status));
txn_callbacks.txn_end (this);
active = false;
}
Expand All @@ -109,7 +109,7 @@ void nano::write_mdb_txn::commit ()
void nano::write_mdb_txn::renew ()
{
auto status (mdb_txn_begin (env, nullptr, 0, &handle));
release_assert (status == MDB_SUCCESS);
release_assert (status == MDB_SUCCESS, mdb_strerror (status));
txn_callbacks.txn_start (this);
active = true;
}
Expand Down
5 changes: 5 additions & 0 deletions nano/node/rocksdb/rocksdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,11 @@ unsigned nano::rocksdb_store::max_block_write_batch_num () const
return max_block_write_batch_num_m;
}

std::string nano::rocksdb_store::error_string (int status) const
{
return std::to_string (status);
}

nano::rocksdb_store::tombstone_info::tombstone_info (uint64_t num_since_last_flush_a, uint64_t const max_a) :
num_since_last_flush (num_since_last_flush_a),
max (max_a)
Expand Down
2 changes: 2 additions & 0 deletions nano/node/rocksdb/rocksdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class rocksdb_store : public block_store_partial<rocksdb::Slice, rocksdb_store>

bool init_error () const override;

std::string error_string (int status) const override;

private:
bool error{ false };
nano::logger_mt & logger;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/rocksdb/rocksdb_txn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void nano::write_rocksdb_txn::commit ()
++attempt_num;
}

release_assert (status.ok ());
release_assert (status.ok (), status.ToString ());
active = false;
}
}
Expand Down
Loading

0 comments on commit af28d69

Please sign in to comment.