diff --git a/lib/include/srsran/adt/bounded_bitset.h b/lib/include/srsran/adt/bounded_bitset.h index 552d98319..82b2749b9 100644 --- a/lib/include/srsran/adt/bounded_bitset.h +++ b/lib/include/srsran/adt/bounded_bitset.h @@ -13,8 +13,8 @@ #ifndef SRSRAN_DYN_BITSET_H #define SRSRAN_DYN_BITSET_H -#include "srsran/common/srsran_assert.h" #include "srsran/srslog/bundled/fmt/format.h" +#include "support/srsran_assert.h" #include #include #include diff --git a/lib/include/srsran/adt/bounded_vector.h b/lib/include/srsran/adt/bounded_vector.h index 0d6b7fd04..7f3b00ec4 100644 --- a/lib/include/srsran/adt/bounded_vector.h +++ b/lib/include/srsran/adt/bounded_vector.h @@ -14,7 +14,7 @@ #define SRSRAN_BOUNDED_VECTOR_H #include "srsran/adt/detail/type_storage.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" #include #include #include diff --git a/lib/include/srsran/adt/circular_buffer.h b/lib/include/srsran/adt/circular_buffer.h index 058dda42c..e411831bb 100644 --- a/lib/include/srsran/adt/circular_buffer.h +++ b/lib/include/srsran/adt/circular_buffer.h @@ -16,7 +16,7 @@ #include "srsran/adt/detail/type_storage.h" #include "srsran/adt/expected.h" #include "srsran/adt/pool/pool_utils.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" #include #include diff --git a/lib/include/srsran/adt/circular_map.h b/lib/include/srsran/adt/circular_map.h index ce6500112..4d1c9b87f 100644 --- a/lib/include/srsran/adt/circular_map.h +++ b/lib/include/srsran/adt/circular_map.h @@ -15,7 +15,7 @@ #include "detail/type_storage.h" #include "expected.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" #include namespace srsran { diff --git a/lib/include/srsran/adt/expected.h b/lib/include/srsran/adt/expected.h index 51a68dda1..ff6e0760d 100644 --- a/lib/include/srsran/adt/expected.h +++ b/lib/include/srsran/adt/expected.h @@ -13,7 +13,7 @@ #ifndef SRSRAN_EXPECTED_H #define SRSRAN_EXPECTED_H -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" #include #include diff --git a/lib/include/srsran/adt/interval.h b/lib/include/srsran/adt/interval.h index c20ffe499..c2a9ff9a9 100644 --- a/lib/include/srsran/adt/interval.h +++ b/lib/include/srsran/adt/interval.h @@ -13,8 +13,8 @@ #ifndef SRSRAN_INTERVAL_H #define SRSRAN_INTERVAL_H -#include "srsran/common/srsran_assert.h" #include "srsran/srslog/bundled/fmt/format.h" +#include "support/srsran_assert.h" #include #include #include diff --git a/lib/include/srsran/adt/move_callback.h b/lib/include/srsran/adt/move_callback.h index a80d3aa80..7526b76a5 100644 --- a/lib/include/srsran/adt/move_callback.h +++ b/lib/include/srsran/adt/move_callback.h @@ -14,7 +14,7 @@ #define SRSRAN_MOVE_CALLBACK_H #include "detail/type_storage.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" #include #include #include @@ -60,7 +60,7 @@ public: constexpr empty_table_t() = default; R call(void* src, Args... args) const final { - srsran_terminate("ERROR: bad function call (cause: function ptr is empty)"); + srsran_assertion_failure("bad function call (cause: function ptr is empty)"); } void move(void* src, void* dest) const final {} void dtor(void* src) const final {} diff --git a/lib/include/srsran/adt/optional.h b/lib/include/srsran/adt/optional.h index 9f938d41c..bcdb9c574 100644 --- a/lib/include/srsran/adt/optional.h +++ b/lib/include/srsran/adt/optional.h @@ -14,7 +14,7 @@ #define SRSRAN_OPTIONAL_H #include "detail/type_storage.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" namespace srsran { diff --git a/lib/include/srsran/adt/pool/batch_mem_pool.h b/lib/include/srsran/adt/pool/batch_mem_pool.h index 1829fca26..dea333e2a 100644 --- a/lib/include/srsran/adt/pool/batch_mem_pool.h +++ b/lib/include/srsran/adt/pool/batch_mem_pool.h @@ -15,8 +15,8 @@ #include "memblock_cache.h" #include "pool_utils.h" -#include "srsran/common/srsran_assert.h" #include "srsran/common/thread_pool.h" +#include "support/srsran_assert.h" #include #include diff --git a/lib/include/srsran/adt/pool/linear_allocator.h b/lib/include/srsran/adt/pool/linear_allocator.h index bcfc05044..c79fd9f15 100644 --- a/lib/include/srsran/adt/pool/linear_allocator.h +++ b/lib/include/srsran/adt/pool/linear_allocator.h @@ -14,7 +14,7 @@ #define SRSRAN_LINEAR_ALLOCATOR_H #include "pool_utils.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" namespace srsran { diff --git a/lib/include/srsran/asn1/asn1_utils.h b/lib/include/srsran/asn1/asn1_utils.h index f86f7eee1..e130d271c 100644 --- a/lib/include/srsran/asn1/asn1_utils.h +++ b/lib/include/srsran/asn1/asn1_utils.h @@ -13,8 +13,8 @@ #ifndef SRSASN_COMMON_UTILS_H #define SRSASN_COMMON_UTILS_H -#include "srsran/common/srsran_assert.h" #include "srsran/srslog/srslog.h" +#include "support/srsran_assert.h" #include #include #include diff --git a/lib/include/srsran/common/slot_point.h b/lib/include/srsran/common/slot_point.h index 70477312e..97b0bbed1 100644 --- a/lib/include/srsran/common/slot_point.h +++ b/lib/include/srsran/common/slot_point.h @@ -14,7 +14,7 @@ #define SRSRAN_SLOT_POINT_H #include "srsran/adt/interval.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" namespace srsran { diff --git a/lib/include/srsran/common/test_common.h b/lib/include/srsran/common/test_common.h index 76180d184..7457eb951 100644 --- a/lib/include/srsran/common/test_common.h +++ b/lib/include/srsran/common/test_common.h @@ -19,9 +19,9 @@ #include "srsran/common/buffer_pool.h" #include "srsran/common/crash_handler.h" -#include "srsran/common/srsran_assert.h" #include "srsran/common/standard_streams.h" #include "srsran/srslog/srslog.h" +#include "support/srsran_assert.h" #include #include diff --git a/lib/include/srsran/common/tti_point.h b/lib/include/srsran/common/tti_point.h index 8ae0fd803..aac2e0b80 100644 --- a/lib/include/srsran/common/tti_point.h +++ b/lib/include/srsran/common/tti_point.h @@ -15,8 +15,8 @@ #include "srsran/adt/interval.h" #include "srsran/common/common.h" -#include "srsran/common/srsran_assert.h" #include "srsran/srslog/srslog.h" +#include "support/srsran_assert.h" #include #include diff --git a/lib/include/srsran/rlc/rlc_am_lte.h b/lib/include/srsran/rlc/rlc_am_lte.h index 9066f3236..19cd565c9 100644 --- a/lib/include/srsran/rlc/rlc_am_lte.h +++ b/lib/include/srsran/rlc/rlc_am_lte.h @@ -19,13 +19,13 @@ #include "srsran/adt/intrusive_list.h" #include "srsran/common/buffer_pool.h" #include "srsran/common/common.h" -#include "srsran/common/srsran_assert.h" #include "srsran/common/task_scheduler.h" #include "srsran/common/timeout.h" #include "srsran/interfaces/pdcp_interface_types.h" #include "srsran/rlc/rlc_am_base.h" #include "srsran/rlc/rlc_common.h" #include "srsran/upper/byte_buffer_queue.h" +#include "support/srsran_assert.h" #include #include #include diff --git a/lib/include/srsran/common/srsran_assert.h b/lib/include/support/srsran_assert.h similarity index 52% rename from lib/include/srsran/common/srsran_assert.h rename to lib/include/support/srsran_assert.h index 45eaa4a6d..91c6e8369 100644 --- a/lib/include/srsran/common/srsran_assert.h +++ b/lib/include/support/srsran_assert.h @@ -13,38 +13,54 @@ #ifndef SRSRAN_ASSERT_H #define SRSRAN_ASSERT_H +#ifdef __cplusplus #include "srsran/srslog/srslog.h" #include +#include #define srsran_unlikely(expr) __builtin_expect(!!(expr), 0) -#define srsran_terminate(fmt, ...) \ - srslog::flush(); \ - std::fprintf(stderr, "%s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \ - std::abort() - -#ifdef ASSERTS_ENABLED +/** + * Command to terminate srsRAN application with an error message, ensuring first that the log is flushed + */ +[[gnu::noinline, noreturn]] inline bool srsran_terminate(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + srslog::flush(); + vfprintf(stderr, fmt, args); + va_end(args); + std::abort(); +} + +#define srsran_assertion_failure(fmt, ...) \ + srsran_terminate("%s:%d: Assertion Failure: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) /** - * Macro that asserts condition is true. If false, it logs the remaining parameters, prints the backtrace and closes - * the application + * Macro that asserts condition is true. If false, it logs the remaining macro args, flushes the log, + * prints the backtrace (if it was activated) and closes the application. */ -#define srsran_assert(condition, fmt, ...) \ - do { \ - if (srsran_unlikely(not(condition))) { \ - srsran_terminate(fmt, ##__VA_ARGS__); \ - } \ - } while (0) +#define srsran_always_assert(condition, fmt, ...) (void)((condition) || srsran_assertion_failure(fmt, ##__VA_ARGS__)) -#ifdef STOP_ON_WARNING +#define SRSRAN_IS_DEFINED(x) SRSRAN_IS_DEFINED2(x) +#define SRSRAN_IS_DEFINED2(x) (#x[0] == 0 || (#x[0] >= '1' && #x[0] <= '9')) /** - * Macro that verifies if condition is true. If false, and STOP_ON_WARNING is true, it behaves like srsran_assert. - * If STOP_ON_WARNING is false, it logs a warning. + * Same as "srsran_always_assert" but it is only active when "enable_check" flag is defined */ +#define srsran_assert_ifdef(enable_check, condition, fmt, ...) \ + (void)((not SRSRAN_IS_DEFINED(enable_check)) || (srsran_always_assert(condition, fmt, ##__VA_ARGS__), 0)) + +/** + * Specialization of "srsran_assert_ifdef" for the ASSERTS_ENABLED flag + */ +#define srsran_assert(condition, fmt, ...) srsran_assert_ifdef(ASSERTS_ENABLED, condition, fmt, ##__VA_ARGS__) + +#ifdef STOP_ON_WARNING + #define srsran_expect(condition, fmt, ...) srsran_assert(condition, fmt, ##__VA_ARGS__) -#else // STOP_ON_WARNING +#else #define srsran_expect(condition, fmt, ...) \ do { \ @@ -53,16 +69,20 @@ } \ } while (0) -#endif // STOP_ON_WARNING +#endif + +#else // __ifcplusplus -#else // ASSERTS_ENABLED +#include +#ifdef ASSERTS_ENABLED +#define srsran_assert(condition, fmt, ...) (void)((condition) || (__assert(#condition, __FILE__, __FLAG__), 0)) +#else #define srsran_assert(condition, fmt, ...) \ do { \ } while (0) - -#define srsran_expect(condition, fmt, ...) srsran_assert(condition, fmt, ##__VA_ARGS__) - #endif +#endif // __ifcplusplus + #endif // SRSRAN_ASSERT_H diff --git a/lib/src/common/phy_cfg_nr_default.cc b/lib/src/common/phy_cfg_nr_default.cc index 1c1eca71e..e8b5d91ab 100644 --- a/lib/src/common/phy_cfg_nr_default.cc +++ b/lib/src/common/phy_cfg_nr_default.cc @@ -34,7 +34,7 @@ phy_cfg_nr_default_t::reference_cfg_t::reference_cfg_t(const std::string& args) // Skip if size is invalid if (param.size() != 2) { - srsran_terminate("Invalid reference argument '%s'", e.c_str()); + srsran_assertion_failure("Invalid reference argument '%s'", e.c_str()); } if (param.front() == "carrier") { @@ -59,7 +59,7 @@ phy_cfg_nr_default_t::reference_cfg_t::reference_cfg_t(const std::string& args) } srsran_assert(pdsch != R_PDSCH_COUNT, "Invalid PDSCH reference configuration '%s'", param.back().c_str()); } else { - srsran_terminate("Invalid %s reference component", param.front().c_str()); + srsran_assertion_failure("Invalid %s reference component", param.front().c_str()); } } } @@ -212,7 +212,7 @@ void make_nzp_csi_rs_ts38101_table_5_2_1(const srsran_carrier_nr_t& carrier, srs res3.periodicity.offset = 21; res4.periodicity.offset = 21; } else { - srsran_terminate("Invalid subcarrier spacing %d kHz", 15U << (uint32_t)carrier.scs); + srsran_assertion_failure("Invalid subcarrier spacing %d kHz", 15U << (uint32_t)carrier.scs); } res1.resource_mapping.freq_band = {0, carrier.nof_prb}; @@ -370,7 +370,7 @@ phy_cfg_nr_default_t::phy_cfg_nr_default_t(const reference_cfg_t& reference_cfg) make_carrier_custom_20MHz(carrier); break; case reference_cfg_t::R_CARRIER_COUNT: - srsran_terminate("Invalid carrier reference"); + srsran_assertion_failure("Invalid carrier reference"); } switch (reference_cfg.tdd) { @@ -381,7 +381,7 @@ phy_cfg_nr_default_t::phy_cfg_nr_default_t(const reference_cfg_t& reference_cfg) make_tdd_fr1_15_1(tdd); break; case reference_cfg_t::R_TDD_COUNT: - srsran_terminate("Invalid TDD reference"); + srsran_assertion_failure("Invalid TDD reference"); } switch (reference_cfg.pdcch) { @@ -398,7 +398,7 @@ phy_cfg_nr_default_t::phy_cfg_nr_default_t(const reference_cfg_t& reference_cfg) make_pdsch_2_1_1_tdd(carrier, pdsch); break; case reference_cfg_t::R_PDSCH_COUNT: - srsran_terminate("Invalid PDSCH reference configuration"); + srsran_assertion_failure("Invalid PDSCH reference configuration"); } switch (reference_cfg.pusch) { diff --git a/srsenb/hdr/stack/mac/sched_phy_ch/sched_phy_resource.h b/srsenb/hdr/stack/mac/sched_phy_ch/sched_phy_resource.h index 144948750..8e218b3da 100644 --- a/srsenb/hdr/stack/mac/sched_phy_ch/sched_phy_resource.h +++ b/srsenb/hdr/stack/mac/sched_phy_ch/sched_phy_resource.h @@ -15,7 +15,7 @@ #include "srsran/adt/bounded_bitset.h" #include "srsran/adt/interval.h" -#include "srsran/common/srsran_assert.h" +#include "support/srsran_assert.h" extern "C" { #include "srsran/phy/phch/ra.h" } @@ -46,7 +46,7 @@ inline uint32_t cell_nof_prb_to_rbg(uint32_t nof_prbs) case 100: return 25; default: - srsran_terminate("Provided nof PRBs not valid"); + srsran_assertion_failure("Provided nof PRBs not valid"); } return 0; } @@ -68,7 +68,7 @@ inline uint32_t cell_nof_rbg_to_prb(uint32_t nof_rbgs) case 25: return 100; default: - srsran_terminate("Provided nof PRBs not valid"); + srsran_assertion_failure("Provided nof PRBs not valid"); } return 0; } diff --git a/srsenb/src/stack/rrc/rrc_ue.cc b/srsenb/src/stack/rrc/rrc_ue.cc index 4083cdb85..6ac359cae 100644 --- a/srsenb/src/stack/rrc/rrc_ue.cc +++ b/srsenb/src/stack/rrc/rrc_ue.cc @@ -18,11 +18,11 @@ #include "srsenb/hdr/stack/rrc/ue_rr_cfg.h" #include "srsran/asn1/rrc_utils.h" #include "srsran/common/enb_events.h" -#include "srsran/common/srsran_assert.h" #include "srsran/common/standard_streams.h" #include "srsran/interfaces/enb_pdcp_interfaces.h" #include "srsran/interfaces/enb_rlc_interfaces.h" #include "srsran/interfaces/enb_s1ap_interfaces.h" +#include "support/srsran_assert.h" using namespace asn1::rrc; @@ -1420,7 +1420,7 @@ void rrc::ue::apply_rlc_rb_updates(const rr_cfg_ded_s& pending_rr_cfg) } else if (srb.srb_id == 2) { srb_cfg = &parent->cfg.srb2_cfg; } else { - srsran_terminate("Invalid LTE SRB id=%d", srb.srb_id); + srsran_assertion_failure("Invalid LTE SRB id=%d", srb.srb_id); } if (srb_cfg->rlc_cfg.type() == srb_to_add_mod_s::rlc_cfg_c_::types_opts::explicit_value) { diff --git a/srsenb/src/stack/upper/gtpu.cc b/srsenb/src/stack/upper/gtpu.cc index 4931e9dbc..0f105cf03 100644 --- a/srsenb/src/stack/upper/gtpu.cc +++ b/srsenb/src/stack/upper/gtpu.cc @@ -13,11 +13,11 @@ #include "srsran/upper/gtpu.h" #include "srsenb/hdr/stack/upper/gtpu.h" #include "srsran/common/network_utils.h" -#include "srsran/common/srsran_assert.h" #include "srsran/common/standard_streams.h" #include "srsran/common/string_helpers.h" #include "srsran/interfaces/enb_interfaces.h" #include "srsran/interfaces/enb_pdcp_interfaces.h" +#include "support/srsran_assert.h" #include #include