From 84bdfa4ac58ca6b017d5ea20f8c43cba3f458eac Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 6 Feb 2007 20:40:38 +0100 Subject: [PATCH 01/15] add debugging support --- SConstruct | 16 ++++++ src/core/debug.cc | 133 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/debug.h | 64 ++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 src/core/debug.cc create mode 100644 src/core/debug.h diff --git a/SConstruct b/SConstruct index af7467119..65bc9736e 100644 --- a/SConstruct +++ b/SConstruct @@ -18,6 +18,7 @@ ns3.add(core) core.add_sources([ 'reference-list-test.cc', 'callback-test.cc', + 'debug.cc', 'ptr.cc', 'test.cc' ]) @@ -39,9 +40,24 @@ core.add_inst_headers([ 'reference-list.h', 'callback.h', 'ptr.h', + 'debug.h', 'test.h' ]) +def config_core (env, config): + retval = [] + # XXX This check is primitive but it should be + # good enough for now. + if config.CheckCHeader ('stdlib.h') == 1: + retval.append ('#define HAVE_STDLIB_H 1') + retval.append ('#define HAVE_GETENV 1') + else: + retval.append ('#undef HAVE_STDLIB_H') + retval.append ('#undef HAVE_GETENV') + return retval +core.add_config (config_core) + + # # The Simu module diff --git a/src/core/debug.cc b/src/core/debug.cc new file mode 100644 index 000000000..11892bc4b --- /dev/null +++ b/src/core/debug.cc @@ -0,0 +1,133 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#include +#include +#include +#include +#include "debug.h" +#include "ns3/core-config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif + +namespace ns3 { + +typedef std::list > ComponentList; +typedef std::list >::iterator ComponentListI; + +static ComponentList g_components; + +DebugComponent::DebugComponent (std::string name) + : m_isEnabled (false) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + assert (i->first.compare (name) != 0); + } + g_components.push_back (std::make_pair (name, this)); +} +bool +DebugComponent::IsEnabled (void) +{ + return m_isEnabled; +} +void +DebugComponent::Enable (void) +{ + m_isEnabled = true; +} +void +DebugComponent::Disable (void) +{ + m_isEnabled = false; +} + +void +DebugComponentEnable (char const *name) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (name) == 0) + { + i->second->Enable (); + break; + } + } +} +void +DebugComponentDisable (char const *name) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (name) == 0) + { + i->second->Disable (); + break; + } + } +} + +void +DebugComponentEnableEnvVar (void) +{ +#ifdef HAVE_GETENV + char *envVar = getenv("NS3_DEBUG"); + if (envVar == 0) + { + return; + } + std::string env = envVar; + std::string::size_type cur = 0; + std::string::size_type next = 0; + while (true) + { + next = env.find_first_of (";", cur); + if (next == std::string::npos) + { + std::string tmp = env.substr (cur, next); + DebugComponentEnable (tmp.c_str ()); + } + cur = next; + } +#endif +} + +void +DebugComponentPrintList (void) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + std::cout << i->first << "=" << (i->second->IsEnabled ()?"enabled":"disabled") << std::endl; + } +} + + + +}; // namespace ns3 diff --git a/src/core/debug.h b/src/core/debug.h new file mode 100644 index 000000000..e1b241824 --- /dev/null +++ b/src/core/debug.h @@ -0,0 +1,64 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef DEBUG_H +#define DEBUG_H + +#include +#include + +namespace ns3 { + +void DebugComponentEnable (char const *name); +void DebugComponentDisable (char const *name); +void DebugComponentEnableEnvVar (void); +void DebugComponentPrintList (void); + +class DebugComponent { +public: + DebugComponent (std::string name); + bool IsEnabled (void); + void Enable (void); + void Disable (void); +private: + bool m_isEnabled; +}; + +}; // namespace ns3 + + +#ifdef NS3_DEBUG_ENABLE + +#define DEBUG_COMPONENT_DEFINE(name) \ + static DebugComponent g_debug = DebugComponent (name); + +#define DEBUG(x) \ + if (g_debug.IsEnabled ()) \ + { \ + std::cout << x << std::endl; \ + } +#else /* NS3_DEBUG_ENABLE */ + +#define DEBUG_COMPONENT_DEFINE(name) +#define DEBUG(x) + +#endif /* NS3_DEBUG_ENABLE */ + +#endif /* DEBUG_H */ From 8cfa657d892f1c5a1b7b2f8437c45d0a6bfb1f0e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 6 Feb 2007 21:42:31 +0100 Subject: [PATCH 02/15] remove EnableEnvVar function --- src/core/debug.cc | 70 +++++++++++++++++++++++++++++++---------------- src/core/debug.h | 1 - 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/core/debug.cc b/src/core/debug.cc index 11892bc4b..f2764eb89 100644 --- a/src/core/debug.cc +++ b/src/core/debug.cc @@ -35,6 +35,48 @@ typedef std::list > ComponentList; typedef std::list >::iterator ComponentListI; static ComponentList g_components; +static bool g_firstDebug = true; + +void +DebugComponentEnableEnvVar (void) +{ +#ifdef HAVE_GETENV + char *envVar = getenv("NS3_DEBUG"); + if (envVar == 0) + { + return; + } + std::string env = envVar; + std::string::size_type cur = 0; + std::string::size_type next = 0; + while (true) + { + next = env.find_first_of (";", cur); + if (next == std::string::npos) + { + std::string tmp = env.substr (cur, next); + bool found = false; + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (tmp) == 0) + { + found = true; + i->second->Enable (); + break; + } + } + if (!found) + { + std::cout << "No debug component named=\"" << tmp << "\"" << std::endl; + } + } + cur = next; + } +#endif +} + DebugComponent::DebugComponent (std::string name) : m_isEnabled (false) @@ -55,6 +97,10 @@ DebugComponent::IsEnabled (void) void DebugComponent::Enable (void) { + if (g_firstDebug) { + DebugComponentEnableEnvVar (); + g_firstDebug = false; + } m_isEnabled = true; } void @@ -92,30 +138,6 @@ DebugComponentDisable (char const *name) } } -void -DebugComponentEnableEnvVar (void) -{ -#ifdef HAVE_GETENV - char *envVar = getenv("NS3_DEBUG"); - if (envVar == 0) - { - return; - } - std::string env = envVar; - std::string::size_type cur = 0; - std::string::size_type next = 0; - while (true) - { - next = env.find_first_of (";", cur); - if (next == std::string::npos) - { - std::string tmp = env.substr (cur, next); - DebugComponentEnable (tmp.c_str ()); - } - cur = next; - } -#endif -} void DebugComponentPrintList (void) diff --git a/src/core/debug.h b/src/core/debug.h index e1b241824..7dbddda5e 100644 --- a/src/core/debug.h +++ b/src/core/debug.h @@ -28,7 +28,6 @@ namespace ns3 { void DebugComponentEnable (char const *name); void DebugComponentDisable (char const *name); -void DebugComponentEnableEnvVar (void); void DebugComponentPrintList (void); class DebugComponent { From 95778d9453bda7dede108af9ca086ec68105f11f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 7 Feb 2007 08:29:01 +0100 Subject: [PATCH 03/15] define NS3_DEBBUG_ENABLE when needed --- build.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/build.py b/build.py index 02868a0b0..6c048a89a 100644 --- a/build.py +++ b/build.py @@ -458,8 +458,9 @@ class Ns3: dbg_env = env.Copy() - env.Append(CFLAGS = debug_flags, - CXXFLAGS = debug_flags, ) + dbg_env.Append(CFLAGS = debug_flags, + CXXFLAGS = debug_flags, + CPPDEFINES = ['NS3_DEBUG_ENABLE']) # debug static support variant.static = True variant.env = dbg_env @@ -469,8 +470,9 @@ class Ns3: dbg_env.Alias('dbg-static', builder) dbg_env = env.Copy() - env.Append(CFLAGS=debug_flags, - CXXFLAGS=debug_flags, ) + dbg_env.Append(CFLAGS=debug_flags, + CXXFLAGS=debug_flags, + CPPDEFINES = ['NS3_DEBUG_ENABLE']) # debug shared support variant.static = False variant.env = dbg_env From 7abd85418369f1a55726bbfdaa8195790e7b853f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 7 Feb 2007 08:49:54 +0100 Subject: [PATCH 04/15] test and debug the debug support --- SConstruct | 7 ++++ samples/main-debug-other.cc | 13 ++++++++ samples/main-debug.cc | 18 ++++++++++ src/core/debug.cc | 66 ++++++++++++++++++++++++------------- src/core/debug.h | 10 +++--- 5 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 samples/main-debug-other.cc create mode 100644 samples/main-debug.cc diff --git a/SConstruct b/SConstruct index 65bc9736e..db6f8c9b5 100644 --- a/SConstruct +++ b/SConstruct @@ -183,6 +183,13 @@ replay_simu.add_source('replay-simulation.cc') # samples +sample_debug = build.Ns3Module('sample-debug', 'samples') +sample_debug.set_executable() +ns3.add(sample_debug) +sample_debug.add_dep('core') +sample_debug.add_source('main-debug.cc') +sample_debug.add_source('main-debug-other.cc') + sample_callback = build.Ns3Module('sample-callback', 'samples') sample_callback.set_executable() ns3.add(sample_callback) diff --git a/samples/main-debug-other.cc b/samples/main-debug-other.cc new file mode 100644 index 000000000..2ce182f8c --- /dev/null +++ b/samples/main-debug-other.cc @@ -0,0 +1,13 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +#include "ns3/debug.h" + +NS3_DEBUG_COMPONENT_DEFINE ("MyComponentB"); + +namespace foo { + +void OneFunction (void) +{ + NS3_DEBUG ("OneFunction debug"); +} + +}; // namespace foo diff --git a/samples/main-debug.cc b/samples/main-debug.cc new file mode 100644 index 000000000..332e693f2 --- /dev/null +++ b/samples/main-debug.cc @@ -0,0 +1,18 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +#include "ns3/debug.h" + +NS3_DEBUG_COMPONENT_DEFINE ("MyComponentA"); + +// declare other function +namespace foo { +void OneFunction (void); +} + +int main (int argc, int argv) +{ + NS3_DEBUG ("nargc="<first.compare (tmp) == 0) { - if (i->first.compare (tmp) == 0) - { - found = true; - i->second->Enable (); - break; - } + found = true; + i->second->Enable (); + break; } - if (!found) - { - std::cout << "No debug component named=\"" << tmp << "\"" << std::endl; - } - } - cur = next; + } + if (!found) + { + std::cout << "No debug component named=\"" << tmp << "\"" << std::endl; + } + if (next == std::string::npos) + { + break; + } + cur = next + 1; + if (cur >= env.size ()) + { + break; + } } #endif } @@ -92,15 +110,15 @@ DebugComponent::DebugComponent (std::string name) bool DebugComponent::IsEnabled (void) { + if (g_firstDebug) { + DebugComponentEnableEnvVar (); + g_firstDebug = false; + } return m_isEnabled; } void DebugComponent::Enable (void) { - if (g_firstDebug) { - DebugComponentEnableEnvVar (); - g_firstDebug = false; - } m_isEnabled = true; } void @@ -153,3 +171,5 @@ DebugComponentPrintList (void) }; // namespace ns3 + + diff --git a/src/core/debug.h b/src/core/debug.h index 7dbddda5e..c64f88e45 100644 --- a/src/core/debug.h +++ b/src/core/debug.h @@ -45,18 +45,18 @@ private: #ifdef NS3_DEBUG_ENABLE -#define DEBUG_COMPONENT_DEFINE(name) \ - static DebugComponent g_debug = DebugComponent (name); +#define NS3_DEBUG_COMPONENT_DEFINE(name) \ + static ns3::DebugComponent g_debug = ns3::DebugComponent (name); -#define DEBUG(x) \ +#define NS3_DEBUG(x) \ if (g_debug.IsEnabled ()) \ { \ std::cout << x << std::endl; \ } #else /* NS3_DEBUG_ENABLE */ -#define DEBUG_COMPONENT_DEFINE(name) -#define DEBUG(x) +#define NS3_DEBUG_COMPONENT_DEFINE(name) +#define NS3_DEBUG(x) #endif /* NS3_DEBUG_ENABLE */ From bd19595461a80ad40b678b573d1322f77fcdec47 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 13 Feb 2007 09:45:32 +0100 Subject: [PATCH 05/15] add NS3_ASSERT and NS3_ASSERT_MSG --- samples/main-debug.cc | 7 +++++++ src/core/debug.cc | 6 ++++++ src/core/debug.h | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/samples/main-debug.cc b/samples/main-debug.cc index 332e693f2..d10f5169d 100644 --- a/samples/main-debug.cc +++ b/samples/main-debug.cc @@ -15,4 +15,11 @@ int main (int argc, int argv) foo::OneFunction (); NS3_DEBUG ("other debug output"); + + int a = 0; + + NS3_ASSERT (a == 0); + NS3_ASSERT_MSG (a == 0, "my msg"); + NS3_ASSERT (a != 0) + NS3_ASSERT_MSG (a != 0, "my 2 msg"); } diff --git a/src/core/debug.cc b/src/core/debug.cc index addfa6b1c..a0da17a5e 100644 --- a/src/core/debug.cc +++ b/src/core/debug.cc @@ -168,6 +168,12 @@ DebugComponentPrintList (void) } } +void +AssertBreakpoint (void) +{ + int *a = 0; + a = 0; +} }; // namespace ns3 diff --git a/src/core/debug.h b/src/core/debug.h index c64f88e45..458d1f243 100644 --- a/src/core/debug.h +++ b/src/core/debug.h @@ -40,6 +40,8 @@ private: bool m_isEnabled; }; +void AssertBreakpoint (void); + }; // namespace ns3 @@ -53,10 +55,29 @@ private: { \ std::cout << x << std::endl; \ } + +#define NS3_ASSERT(condition) \ + if (!(condition)) \ + { \ + std::cout << "assert failed. file=" << __FILE__ << \ + ", line=" << __LINE__ << ", cond=\""#condition << \ + "\"" << std::endl; \ + ns3::AssertBreakpoint (); \ + } + +#define NS3_ASSERT_MSG(condition, message) \ + if (!(condition)) \ + { \ + std::cout << message << std::endl; \ + ns3::AssertBreakpoint (); \ + } + #else /* NS3_DEBUG_ENABLE */ #define NS3_DEBUG_COMPONENT_DEFINE(name) #define NS3_DEBUG(x) +#define NS3_ASSERT(cond) +#define NS3_ASSERT_MSG(cond) #endif /* NS3_DEBUG_ENABLE */ From f5fee4c7af0eb6d5f1934ca6d44fc5c44723ee91 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 13 Feb 2007 09:52:42 +0100 Subject: [PATCH 06/15] really trigger segfault when needed --- src/core/debug.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/debug.cc b/src/core/debug.cc index a0da17a5e..178f01735 100644 --- a/src/core/debug.cc +++ b/src/core/debug.cc @@ -172,7 +172,7 @@ void AssertBreakpoint (void) { int *a = 0; - a = 0; + *a = 0; } From cb102a5d0b52311a7f63c223247d89047b4c1fbe Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 13 Feb 2007 10:22:49 +0100 Subject: [PATCH 07/15] improve assert brekapoint function --- src/core/debug.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/core/debug.cc b/src/core/debug.cc index 178f01735..162e23578 100644 --- a/src/core/debug.cc +++ b/src/core/debug.cc @@ -172,7 +172,15 @@ void AssertBreakpoint (void) { int *a = 0; - *a = 0; + /** + * we test here to allow a debugger to change the value of + * the variable 'a' to allow the debugger to avoid the + * subsequent segfault. + */ + if (a == 0) + { + *a = 0; + } } From 310f67b83550e0a76fe50f28480af73ee015fe97 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 13 Feb 2007 10:23:10 +0100 Subject: [PATCH 08/15] avoid warning in optimized builds --- samples/main-debug.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/main-debug.cc b/samples/main-debug.cc index d10f5169d..dda796bd9 100644 --- a/samples/main-debug.cc +++ b/samples/main-debug.cc @@ -16,7 +16,8 @@ int main (int argc, int argv) NS3_DEBUG ("other debug output"); - int a = 0; + int a; + a = 0; NS3_ASSERT (a == 0); NS3_ASSERT_MSG (a == 0, "my msg"); From b2c8866cbff478c352f194bb162036d3d8fc0feb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 13 Feb 2007 10:23:31 +0100 Subject: [PATCH 09/15] make sure tha NS3_DEBUG_ENABLE is defined by doxygen preprocessor --- doc/doxygen.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/doxygen.conf b/doc/doxygen.conf index 61806a900..07fbd04c4 100644 --- a/doc/doxygen.conf +++ b/doc/doxygen.conf @@ -996,7 +996,7 @@ INCLUDE_FILE_PATTERNS = # undefined via #undef or recursively expanded use the := operator # instead of the = operator. -PREDEFINED = RUN_SELF_TESTS +PREDEFINED = RUN_SELF_TESTS NS3_DEBUG_ENABLE # If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then # this tag can be used to specify a list of macro names that should be expanded. From 42ea8aabe07ff2d54f96f6ce09c555baae5285f7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 13 Feb 2007 10:23:47 +0100 Subject: [PATCH 10/15] add API doc --- src/core/debug.h | 98 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/src/core/debug.h b/src/core/debug.h index 458d1f243..d1aecbb18 100644 --- a/src/core/debug.h +++ b/src/core/debug.h @@ -24,10 +24,53 @@ #include #include +/** + * \defgroup debugging + * \brief Debugging functions and macros + * + * The ns3 debugging support provides a few simple macros + * for debugging and sending out messages to the user. There + * two classes of functionality: + * - ASSERT functionality: macros which are used to verify + * at runtime that a certain condition is true. If it is + * not true, the program halts. These checks are built + * into the program only in debugging builds. They are + * removed in optimized builds. + * + * - DEBUG functionality: macros which allow developers to + * send information out on screen only in debugging builds. + * All debug messages are disabled by default. To enable + * selected debug messages, use the ns3::DebugComponentEnable + * function. Alternatively, you can use the NS3_DEBUG + * environment variable to define a ';'-separated list of + * messages to enable. For example, NS3_DEBUG=a;b;c;DAFD;GH + * would enable the components 'a', 'b', 'c', 'DAFD', and, 'GH'. + */ + namespace ns3 { +/** + * \param name a debug component name + * \ingroup debugging + * + * Enable the debugging output associated with that debug component. + * The debugging output can be later disabled with a call + * to ns3::DebugComponentDisable. + */ void DebugComponentEnable (char const *name); +/** + * \param name a debug component name + * \ingroup debugging + * + * Disable the debugging output associated with that debug component. + * The debugging output can be later re-enabled with a call + * to ns3::DebugComponentEnable. + */ void DebugComponentDisable (char const *name); +/** + * \ingroup debugging + * Print the list of debugging messages available. + */ void DebugComponentPrintList (void); class DebugComponent { @@ -40,6 +83,15 @@ private: bool m_isEnabled; }; +/** + * \ingroup debugging + * + * When an NS3_ASSERT cannot verify its condition, + * this function is called. This is where you should + * be able to put a breakpoint with a debugger if + * you want to catch assertions before the program + * halts. + */ void AssertBreakpoint (void); }; // namespace ns3 @@ -47,15 +99,46 @@ void AssertBreakpoint (void); #ifdef NS3_DEBUG_ENABLE +/** + * \ingroup debugging + * \param name a string + * + * Define a Debug component with a specific name. This macro + * should be used at the top of every file in which you want + * to use the NS3_DEBUG macro. This macro defines a new + * "debug component" which can be later selectively enabled + * or disabled with the ns3::DebugComponentEnable and + * ns3::DebugComponentDisable functions or with the NS3_DEBUG + * environment variable. + */ #define NS3_DEBUG_COMPONENT_DEFINE(name) \ static ns3::DebugComponent g_debug = ns3::DebugComponent (name); -#define NS3_DEBUG(x) \ +/** + * \ingroup debugging + * \param msg message to output + * + * Generate debugging output in the "debug component" of the + * current file. i.e., every call to NS3_DEBUG from within + * a file implicitely generates out within the component + * defined with the NS3_DEBUG_COMPONENT_DEFINE macro in the + * same file. + */ +#define NS3_DEBUG(msg) \ if (g_debug.IsEnabled ()) \ { \ - std::cout << x << std::endl; \ + std::cout << msg << std::endl; \ } +/** + * \ingroup debugging + * \param condition condition to verifiy. + * + * At runtime, in debugging builds, if this condition is not + * true, the program prints the source file, line number and + * unverified condition and halts in the ns3::AssertBreakpoint + * function. + */ #define NS3_ASSERT(condition) \ if (!(condition)) \ { \ @@ -65,6 +148,15 @@ void AssertBreakpoint (void); ns3::AssertBreakpoint (); \ } +/** + * \ingroup debugging + * \param condition condition to verifiy. + * \param message message to output + * + * At runtime, in debugging builds, if this condition is not + * true, the program prints the message to output and + * halts in the ns3::AssertBreakpoint function. + */ #define NS3_ASSERT_MSG(condition, message) \ if (!(condition)) \ { \ @@ -77,7 +169,7 @@ void AssertBreakpoint (void); #define NS3_DEBUG_COMPONENT_DEFINE(name) #define NS3_DEBUG(x) #define NS3_ASSERT(cond) -#define NS3_ASSERT_MSG(cond) +#define NS3_ASSERT_MSG(cond,msg) #endif /* NS3_DEBUG_ENABLE */ From 06788bac2715c6517d90330c92fe62d031edeeaf Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 13 Feb 2007 18:20:24 +0100 Subject: [PATCH 11/15] regex: s/NS3_/NS_/ --- samples/main-debug-other.cc | 4 ++-- samples/main-debug.cc | 14 +++++++------- src/core/debug.h | 30 +++++++++++++++--------------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/samples/main-debug-other.cc b/samples/main-debug-other.cc index 2ce182f8c..da7587506 100644 --- a/samples/main-debug-other.cc +++ b/samples/main-debug-other.cc @@ -1,13 +1,13 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ #include "ns3/debug.h" -NS3_DEBUG_COMPONENT_DEFINE ("MyComponentB"); +NS_DEBUG_COMPONENT_DEFINE ("MyComponentB"); namespace foo { void OneFunction (void) { - NS3_DEBUG ("OneFunction debug"); + NS_DEBUG ("OneFunction debug"); } }; // namespace foo diff --git a/samples/main-debug.cc b/samples/main-debug.cc index dda796bd9..303775582 100644 --- a/samples/main-debug.cc +++ b/samples/main-debug.cc @@ -1,7 +1,7 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ #include "ns3/debug.h" -NS3_DEBUG_COMPONENT_DEFINE ("MyComponentA"); +NS_DEBUG_COMPONENT_DEFINE ("MyComponentA"); // declare other function namespace foo { @@ -10,17 +10,17 @@ void OneFunction (void); int main (int argc, int argv) { - NS3_DEBUG ("nargc="< + */ + +#include "assert.h" + +namespace ns3 { + +void +AssertBreakpoint (void) +{ + int *a = 0; + /** + * we test here to allow a debugger to change the value of + * the variable 'a' to allow the debugger to avoid the + * subsequent segfault. + */ + if (a == 0) + { + *a = 0; + } +} + +}//namespace ns3 diff --git a/src/core/assert.h b/src/core/assert.h new file mode 100644 index 000000000..bf3fed648 --- /dev/null +++ b/src/core/assert.h @@ -0,0 +1,95 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef ASSERT_H +#define ASSERT_H + +/** + * \defgroup assert + * \brief assert functions and macros + * + * The assert macros are used to verify + * at runtime that a certain condition is true. If it is + * not true, the program halts. These checks are built + * into the program only in debugging builds. They are + * removed in optimized builds. + */ + +namespace ns3 { + +/** + * \ingroup debugging + * + * When an NS_ASSERT cannot verify its condition, + * this function is called. This is where you should + * be able to put a breakpoint with a debugger if + * you want to catch assertions before the program + * halts. + */ +void AssertBreakpoint (void); + +}//namespace ns3 + +#ifdef NS3_ASSERT_ENABLE + +#include + +/** + * \ingroup assert + * \param condition condition to verifiy. + * + * At runtime, in debugging builds, if this condition is not + * true, the program prints the source file, line number and + * unverified condition and halts in the ns3::AssertBreakpoint + * function. + */ +#define NS_ASSERT(condition) \ + if (!(condition)) \ + { \ + std::cout << "assert failed. file=" << __FILE__ << \ + ", line=" << __LINE__ << ", cond=\""#condition << \ + "\"" << std::endl; \ + ns3::AssertBreakpoint (); \ + } + +/** + * \ingroup assert + * \param condition condition to verifiy. + * \param message message to output + * + * At runtime, in debugging builds, if this condition is not + * true, the program prints the message to output and + * halts in the ns3::AssertBreakpoint function. + */ +#define NS_ASSERT_MSG(condition, message) \ + if (!(condition)) \ + { \ + std::cout << message << std::endl; \ + ns3::AssertBreakpoint (); \ + } + +#else /* NS3_ASSERT_ENABLE */ + +#define NS_ASSERT(cond) +#define NS_ASSERT_MSG(cond,msg) + +#endif /* NS3_ASSERT_ENABLE */ + +#endif /* ASSERT_H */ diff --git a/src/core/debug.cc b/src/core/debug.cc index 162e23578..a250be103 100644 --- a/src/core/debug.cc +++ b/src/core/debug.cc @@ -96,7 +96,7 @@ DebugComponentEnableEnvVar (void) } -DebugComponent::DebugComponent (std::string name) +DebugComponent::DebugComponent (char const * name) : m_isEnabled (false) { for (ComponentListI i = g_components.begin (); @@ -168,22 +168,6 @@ DebugComponentPrintList (void) } } -void -AssertBreakpoint (void) -{ - int *a = 0; - /** - * we test here to allow a debugger to change the value of - * the variable 'a' to allow the debugger to avoid the - * subsequent segfault. - */ - if (a == 0) - { - *a = 0; - } -} - - }; // namespace ns3 diff --git a/src/core/debug.h b/src/core/debug.h index 51705241b..31253d4a8 100644 --- a/src/core/debug.h +++ b/src/core/debug.h @@ -21,22 +21,10 @@ #ifndef DEBUG_H #define DEBUG_H -#include -#include - /** * \defgroup debugging * \brief Debugging functions and macros * - * The ns3 debugging support provides a few simple macros - * for debugging and sending out messages to the user. There - * two classes of functionality: - * - ASSERT functionality: macros which are used to verify - * at runtime that a certain condition is true. If it is - * not true, the program halts. These checks are built - * into the program only in debugging builds. They are - * removed in optimized builds. - * * - DEBUG functionality: macros which allow developers to * send information out on screen only in debugging builds. * All debug messages are disabled by default. To enable @@ -75,7 +63,7 @@ void DebugComponentPrintList (void); class DebugComponent { public: - DebugComponent (std::string name); + DebugComponent (char const *name); bool IsEnabled (void); void Enable (void); void Disable (void); @@ -83,22 +71,15 @@ private: bool m_isEnabled; }; -/** - * \ingroup debugging - * - * When an NS_ASSERT cannot verify its condition, - * this function is called. This is where you should - * be able to put a breakpoint with a debugger if - * you want to catch assertions before the program - * halts. - */ -void AssertBreakpoint (void); - }; // namespace ns3 #ifdef NS3_DEBUG_ENABLE +#include +#include + + /** * \ingroup debugging * \param name a string @@ -130,46 +111,10 @@ void AssertBreakpoint (void); std::cout << msg << std::endl; \ } -/** - * \ingroup debugging - * \param condition condition to verifiy. - * - * At runtime, in debugging builds, if this condition is not - * true, the program prints the source file, line number and - * unverified condition and halts in the ns3::AssertBreakpoint - * function. - */ -#define NS_ASSERT(condition) \ - if (!(condition)) \ - { \ - std::cout << "assert failed. file=" << __FILE__ << \ - ", line=" << __LINE__ << ", cond=\""#condition << \ - "\"" << std::endl; \ - ns3::AssertBreakpoint (); \ - } - -/** - * \ingroup debugging - * \param condition condition to verifiy. - * \param message message to output - * - * At runtime, in debugging builds, if this condition is not - * true, the program prints the message to output and - * halts in the ns3::AssertBreakpoint function. - */ -#define NS_ASSERT_MSG(condition, message) \ - if (!(condition)) \ - { \ - std::cout << message << std::endl; \ - ns3::AssertBreakpoint (); \ - } - #else /* NS3_DEBUG_ENABLE */ #define NS_DEBUG_COMPONENT_DEFINE(name) #define NS_DEBUG(x) -#define NS_ASSERT(cond) -#define NS_ASSERT_MSG(cond,msg) #endif /* NS3_DEBUG_ENABLE */ From 8cf0f1f4b4be479f8267f43bc8ac855effc62c82 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 16 Feb 2007 09:23:13 +0100 Subject: [PATCH 13/15] make sure that NS3_ASSER_ENABLE is defined in debug builds --- build.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build.py b/build.py index 6c048a89a..89e130083 100644 --- a/build.py +++ b/build.py @@ -460,7 +460,8 @@ class Ns3: dbg_env = env.Copy() dbg_env.Append(CFLAGS = debug_flags, CXXFLAGS = debug_flags, - CPPDEFINES = ['NS3_DEBUG_ENABLE']) + CPPDEFINES = ['NS3_DEBUG_ENABLE', + 'NS3_ASSERT_ENABLE']) # debug static support variant.static = True variant.env = dbg_env @@ -472,7 +473,8 @@ class Ns3: dbg_env = env.Copy() dbg_env.Append(CFLAGS=debug_flags, CXXFLAGS=debug_flags, - CPPDEFINES = ['NS3_DEBUG_ENABLE']) + CPPDEFINES = ['NS3_DEBUG_ENABLE', + 'NS3_ASSERT_ENABLE']) # debug shared support variant.static = False variant.env = dbg_env From 07b9fc12aad06af56627e5db4ea36e0ce57023dd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 16 Feb 2007 09:23:22 +0100 Subject: [PATCH 14/15] add fatal-error.h header --- SConstruct | 1 + src/core/fatal-error.h | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/core/fatal-error.h diff --git a/SConstruct b/SConstruct index dea42ab6f..ef87afdd1 100644 --- a/SConstruct +++ b/SConstruct @@ -43,6 +43,7 @@ core.add_inst_headers([ 'ptr.h', 'debug.h', 'assert.h', + 'fatal-error.h', 'test.h' ]) diff --git a/src/core/fatal-error.h b/src/core/fatal-error.h new file mode 100644 index 000000000..f2bba0f84 --- /dev/null +++ b/src/core/fatal-error.h @@ -0,0 +1,43 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef FATAL_ERROR_H +#define FATAL_ERROR_H + +#include "assert.h" + +/** + * \defgroup error + * \brief fatal error handling + * + * \param msg message to output when this macro is hit. + * + * When this macro is hit at runtime, the user-specified + * error message is output and the program is halted by calling + * the ns3::AssertBreakpoint function. This macro is enabled + * unconditionally in all builds, including debug and optimized + * builds. + */ +#define NS_FATAL_ERROR(msg) \ + std::cout << msg << std::endl; \ + ns3::AssertBreakpoint (); + + +#endif /* FATAL_ERROR_H */ From ded5b872b9d17b4fbec4ffd144012c61171a902f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 16 Feb 2007 09:26:56 +0100 Subject: [PATCH 15/15] add NS_DEBUG_UNCOND --- src/core/debug.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/core/debug.h b/src/core/debug.h index 31253d4a8..c7025a8b3 100644 --- a/src/core/debug.h +++ b/src/core/debug.h @@ -111,10 +111,21 @@ private: std::cout << msg << std::endl; \ } +/** + * \ingroup debugging + * \param msg message to output + * + * Generate debugging output unconditionally in all + * debug builds. + */ +#define NS_DEBUG_UNCOND (msg) \ + std::cout << msg << std::endl; + #else /* NS3_DEBUG_ENABLE */ #define NS_DEBUG_COMPONENT_DEFINE(name) #define NS_DEBUG(x) +#define NS_DEBUG_UNCOND(msg) #endif /* NS3_DEBUG_ENABLE */