View | Details | Raw Unified | Return to bug 933
Collapse All | Expand All

(-)a/src/core/abort.h (-29 / +99 lines)
 Lines 1-6    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
2
/*
3
 * Copyright (c) 2008 INRIA
3
 * Copyright (c) 2008 INRIA, 2010 NICTA
4
 *
4
 *
5
 * This program is free software; you can redistribute it and/or modify
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
6
 * it under the terms of the GNU General Public License version 2 as
 Lines 15-64    Link Here 
15
 * along with this program; if not, write to the Free Software
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
17
 *
18
 * Author:  Original author unknown
19
 *          Quincy Tse <quincy.tse@nicta.com.au>
18
 */
20
 */
19
#ifndef NS3_ABORT_H
21
#ifndef NS3_ABORT_H
20
#define NS3_ABORT_H
22
#define NS3_ABORT_H
21
23
22
#include "fatal-error.h"
24
#include "fatal-error.h"
23
25
26
/**
27
 * \ingroup debugging
28
 * \brief Abnormal program termination
29
 *
30
 * \param msg message to output when this macro is hit.
31
 *
32
 * This macro is essentially equivalent to NS_FATAL_ERROR,
33
 * excepts it prepends the error message with the string
34
 * "aborted. ". When this macro is hit a runtime, the
35
 * program will be halted using std::terminate, which
36
 * triggers clean up code regestered by std::set_terminate.
37
 *
38
 * This macro is enable unconditionally in all builds,
39
 * including debug and optimized builds.
40
 *
41
 * \see NS_FATAL_ERROR
42
 */
24
#define NS_ABORT_MSG(msg)                                       \
43
#define NS_ABORT_MSG(msg)                                       \
25
  do {								\
44
  do {								                                          \
26
    std::cerr << "file=" << __FILE__ <<                         \
45
    std::cerr << "aborted. ";                                   \
27
      ", line=" << __LINE__ << ", abort, msg=\"" <<             \
46
    NS_FATAL_ERROR (msg);                                       \
28
      msg << "\"" << std::endl;                                 \
29
    int *a = 0;                                                 \
30
    *a = 0;							\
31
  } while (false)
47
  } while (false)
32
48
33
49
34
#define NS_ABORT_IF(cond)					\
50
/**
35
  do {								\
51
 * \ingroup debugging
36
    if (cond)							\
52
 * \brief Abnormal program termination if cond is true.
37
      {								\
53
 *
38
	std::cerr << "file=" << __FILE__ <<			\
54
 * \param cond condition to be evaluated.
39
	  ", line=" << __LINE__ << ", abort on=\""#cond <<	\
55
 *
40
            "\"" << std::endl;                                  \
56
 * This is similar to NS_ASSERT(!(cond)), except this check
41
	int *a = 0;						\
57
 * is enabled in all builds. If cond is evaluated to true,
42
	*a = 0;							\
58
 * the espression evaluating to true is printed to stderr,
43
      }								\
59
 * followed by a call to the NS_FATAL_ERROR_NO_MSG() macro
60
 * which prints the details of filename and line number to
61
 * stderr. The program will be halted by calling
62
 * std::terminate(), triggering any clean up code registered
63
 * by std::set_terminate (NS3 default is a stream-flushing
64
 * code, but may be overridden).
65
 *
66
 * This macro is enable unconditionally in all builds,
67
 * including debug and optimized builds.
68
 */
69
#define NS_ABORT_IF(cond)					                              \
70
  do {								                                          \
71
    if (cond)							                                      \
72
      {								                                          \
73
	      std::cerr << "aborted. cond=\"" << #cond << ", ";       \
74
	      NS_FATAL_ERROR_NO_MSG ();                               \
75
      }								                                          \
44
  } while (false)
76
  } while (false)
45
77
46
#define NS_ABORT_MSG_IF(cond, msg)				\
78
/**
47
  do {								\
79
 * \ingroup debugging
48
    if (cond)							\
80
 * \brief Abnormal program termination if cond is true.
49
      {								\
81
 *
50
	std::cerr << "file=" << __FILE__ <<			\
82
 * \param cond condition to be evaluated.
51
	  ", line=" << __LINE__ << ", abort on=\""#cond <<	\
83
 * \param msg message to output when cond is true.
52
	  "\", msg=\"" << msg << "\"" << std::endl;		\
84
 *
53
	int *a = 0;						\
85
 * This is similar to NS_ASSERT_MSG(!(cond)), except this
54
	*a = 0;							\
86
 * check is enabled in all builds. If cond is evaluated to
55
      }								\
87
 * true, the espression evaluating to true is printed to
88
 * stderr, followed by a call to the NS_FATAL_ERROR() macro
89
 * which prints the user-specified error message, and details
90
 * of filename and line number to stderr. The program will
91
 * be halted by calling std::terminate(), triggering any
92
 * clean up code registered by std::set_terminate (NS3
93
 * default is a stream-flushing code, but may be overridden).
94
 *
95
 * This macro is enable unconditionally in all builds,
96
 * including debug and optimized builds.
97
 */
98
#define NS_ABORT_MSG_IF(cond, msg)				                      \
99
  do {								                                          \
100
    if (cond)							                                      \
101
      {								                                          \
102
	      std::cerr << "aborted. cond=\"" << #cond << "\", ";       \
103
	      NS_FATAL_ERROR (msg);                                   \
104
      }								                                          \
56
  } while (false)
105
  } while (false)
57
106
58
#define NS_ABORT_UNLESS(cond)				\
107
/**
108
 * \ingroup debugging
109
 * \brief Abnormal program termination if cond is false.
110
 *
111
 * \param cond condition to be evaluated.
112
 *
113
 * This is an alias for NS_ABORT_IF(!(cond))
114
 *
115
 * \see NS_ABORT_IF
116
 */
117
#define NS_ABORT_UNLESS(cond)				                            \
59
  NS_ABORT_IF(!(cond))
118
  NS_ABORT_IF(!(cond))
60
119
61
#define NS_ABORT_MSG_UNLESS(cond, msg)			\
120
/**
121
 * \ingroup debugging
122
 * \brief Abnormal program termination if cond is false.
123
 *
124
 * \param cond condition to be evaluated.
125
 * \param msg message to output if cond is false.
126
 *
127
 * This is an alias for NS_ABORT_MSG_IF(!(cond))
128
 *
129
 * \see NS_ABORT_MSG_IF
130
 */
131
#define NS_ABORT_MSG_UNLESS(cond, msg)			                    \
62
  NS_ABORT_MSG_IF(!(cond),msg)
132
  NS_ABORT_MSG_IF(!(cond),msg)
63
133
64
#endif /* NS3_ABORT_H */
134
#endif /* NS3_ABORT_H */
(-)a/src/core/assert.h (-20 / +30 lines)
 Lines 1-6    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
2
/*
3
 * Copyright (c) 2006 INRIA
3
 * Copyright (c) 2006 INRIA, 2010 NICTA
4
 *
4
 *
5
 * This program is free software; you can redistribute it and/or modify
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
6
 * it under the terms of the GNU General Public License version 2 as
 Lines 16-29    Link Here 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
17
 *
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
 *         Quincy Tse <quincy.tse@nicta.com.au>
19
 */
20
 */
20
#ifndef ASSERT_H
21
#ifndef NS_ASSERT_H
21
#define ASSERT_H
22
#define NS_ASSERT_H
22
23
23
#ifdef NS3_ASSERT_ENABLE
24
#ifdef NS3_ASSERT_ENABLE
24
25
25
#include <iostream>
26
#include <iostream>
26
27
28
#include "fatal-error.h"
29
27
/**
30
/**
28
 * \ingroup core
31
 * \ingroup core
29
 * \defgroup debugging Debugging
32
 * \defgroup debugging Debugging
 Lines 39-44    Link Here 
39
 * not true, the program halts. These checks are built
42
 * not true, the program halts. These checks are built
40
 * into the program only in debugging builds. They are
43
 * into the program only in debugging builds. They are
41
 * removed in optimized builds.
44
 * removed in optimized builds.
45
 *
46
 * These macro are intended to check certain conditions
47
 * to be true. Do not put code that also have side effects
48
 * that your program relies on (eg. code that advances
49
 * an iterator and return false at end of file) because
50
 * the code will not be executed on release builds!!
51
 *
52
 * If assertion-style checks are required for release
53
 * builds, use NS_ABORT_UNLESS and NS_ABORT_MSG_UNLESS.
42
 */
54
 */
43
55
44
/**
56
/**
 Lines 47-64    Link Here 
47
 *
59
 *
48
 * At runtime, in debugging builds, if this condition is not
60
 * At runtime, in debugging builds, if this condition is not
49
 * true, the program prints the source file, line number and 
61
 * true, the program prints the source file, line number and 
50
 * unverified condition and halts by dereferencing a null pointer.
62
 * unverified condition and halts by calling std::terminate
51
 */
63
 */
52
#define NS_ASSERT(condition)                                    \
64
#define NS_ASSERT(condition)                                    \
53
  do                                                            \
65
  do                                                            \
54
    {                                                           \
66
    {                                                           \
55
      if (!(condition))                                         \
67
      if (!(condition))                                         \
56
        {                                                       \
68
        {                                                       \
57
          std::cerr << "assert failed. file=" << __FILE__ <<    \
69
          std::cerr << "assert failed. cond=\"" <<              \
58
            ", line=" << __LINE__ << ", cond=\""#condition <<   \
70
            #condition << "\", ";                               \
59
            "\"" << std::endl;                                  \
71
          NS_FATAL_ERROR_NO_MSG();                              \
60
          int *a = 0;                                           \
61
          *a = 0;                                               \
62
        }                                                       \
72
        }                                                       \
63
    }                                                           \
73
    }                                                           \
64
  while (false)
74
  while (false)
 Lines 71-88    Link Here 
71
 *
81
 *
72
 * At runtime, in debugging builds, if this condition is not
82
 * At runtime, in debugging builds, if this condition is not
73
 * true, the program prints the message to output and
83
 * true, the program prints the message to output and
74
 * halts by dereferencing a null pointer.
84
 * halts by calling std::terminate.
75
 */
85
 */
76
#define NS_ASSERT_MSG(condition, message)       \
86
#define NS_ASSERT_MSG(condition, message)             \
77
  do                                            \
87
  do                                                  \
78
    {                                           \
88
    {                                                 \
79
      if (!(condition))                         \
89
      if (!(condition))                               \
80
        {                                       \
90
        {                                             \
81
          std::cerr << message << std::endl;    \
91
          std::cerr << "assert failed. cond=\"" <<    \
82
          int *a = 0;                           \
92
            #condition << "\", ";                     \
83
          *a = 0;                               \
93
          NS_FATAL_ERROR (message);                   \
84
        }                                       \
94
        }                                             \
85
    }                                           \
95
    }                                                 \
86
  while (false)
96
  while (false)
87
97
88
#else /* NS3_ASSERT_ENABLE */
98
#else /* NS3_ASSERT_ENABLE */
(-)a/src/core/fatal-error.h (-12 / +86 lines)
 Lines 1-6    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
2
/*
3
 * Copyright (c) 2006 INRIA
3
 * Copyright (c) 2006 INRIA, 2010 NICTA
4
 *
4
 *
5
 * This program is free software; you can redistribute it and/or modify
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
6
 * it under the terms of the GNU General Public License version 2 as
 Lines 16-26    Link Here 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
17
 *
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
 *         Quincy Tse <quincy.tse@nicta.com.au>
19
 */
20
 */
20
#ifndef FATAL_ERROR_H
21
#ifndef NS3_FATAL_ERROR_H
21
#define FATAL_ERROR_H
22
#define NS3_FATAL_ERROR_H
22
23
23
#include <iostream>
24
#include <iostream>
25
#include <exception>
26
#include <cstdlib>
27
28
#include "fatal-impl.h"
29
30
/**
31
 * \ingroup debugging
32
 * \brief fatal error handling
33
 *
34
 * When this macro is hit at runtime, details of filename
35
 * and line number is printed to stderr, and the program
36
 * is halted by calling std::terminate(). This will
37
 * trigger any clean up code registered by
38
 * std::set_terminate (NS3 default is a stream-flushing
39
 * code), but may be overridden.
40
 *
41
 * This macro is enabled unconditionally in all builds,
42
 * including debug and optimized builds.
43
 */
44
#define NS_FATAL_ERROR_NO_MSG()                           \
45
  do                                                      \
46
    {                                                     \
47
      std::cerr << "file=" << __FILE__ << ", line=" <<    \
48
        __LINE__ << std::endl;                            \
49
      std::terminate ();                                  \
50
    }                                                     \
51
  while (false)
24
52
25
/**
53
/**
26
 * \ingroup debugging
54
 * \ingroup debugging
 Lines 28-47    Link Here 
28
 *
56
 *
29
 * \param msg message to output when this macro is hit.
57
 * \param msg message to output when this macro is hit.
30
 *
58
 *
31
 * When this macro is hit at runtime, the user-specified 
59
 * When this macro is hit at runtime, the user-specified
32
 * error message is output and the program is halted by
60
 * error message is printed to stderr, followed by a call
33
 * dereferencing a null pointer. This macro is enabled 
61
 * to the NS_FATAL_ERROR_NO_MSG() macro which prints the
34
 * unconditionally in all builds, including debug and 
62
 * details of filename and line number to stderr. The
35
 * optimized builds.
63
 * program will be halted by calling std::terminate(),
64
 * triggering any clean up code registered by
65
 * std::set_terminate (NS3 default is a stream-flushing
66
 * code, but may be overridden).
67
 *
68
 * This macro is enabled unconditionally in all builds,
69
 * including debug and optimized builds.
36
 */
70
 */
37
#define NS_FATAL_ERROR(msg)				\
71
#define NS_FATAL_ERROR(msg)				                      \
38
  do                                                    \
72
  do                                                    \
39
    {                                                   \
73
    {                                                   \
40
      std::cerr << msg << std::endl;			\
74
      std::cerr << "msg=\"" << msg << "\", ";           \
41
      int *a = 0;                                       \
75
      NS_FATAL_ERROR_NO_MSG();                          \
42
      *a = 0;                                           \
43
    }                                                   \
76
    }                                                   \
44
  while (false)
77
  while (false)
45
78
79
#define NS_FATAL_SET_CXX_VERBOSE()                                  \
80
  do                                                                \
81
    {                                                               \
82
      std::set_terminate (__gnu_cxx::__verbose_terminate_handler);  \
83
    }                                                               \
84
  while (false)
85
86
#define NS_FATAL_SET_CXX_SILENT()                   \
87
  do                                                \
88
    {                                               \
89
      std::set_terminate (std::abort);              \
90
    }                                               \
91
  while (false)
92
93
#define NS_FATAL_SET_DEFAULT()                      \
94
  do                                                \
95
    {                                               \
96
      std::set_terminate (ns3::_handleTerminate);   \
97
    }                                               \
98
  while (false)
99
100
#define NS_FATAL_SET_HANDLER(handler)                         \
101
  do                                                          \
102
    {                                                         \
103
      std::set_terminate (static_cast<void (*)()>(handler));  \
104
    }                                                         \
105
  while (false)
106
107
#define NS_FATAL_REGISTER_STREAM(stream)            \
108
  do                                                \
109
    {                                               \
110
      ns3::_registerStream (stream);                \
111
    }                                               \
112
  while (false)
113
114
#define NS_FATAL_UNREGISTER_STREAM(stream)          \
115
  do                                                \
116
    {                                               \
117
      ns3::_unregisterStream (stream);              \
118
    }                                               \
119
  while (false)
46
120
47
#endif /* FATAL_ERROR_H */
121
#endif /* FATAL_ERROR_H */
(-)b08fe828b686 (+84 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2010 NICTA
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Quincy Tse <quincy.tse@nicta.com.au>
19
 */
20
#include "fatal-impl.h"
21
22
#include <iostream>
23
#include <list>
24
25
#include <cstdlib>
26
#include <cstdio>
27
28
#include <csignal>
29
30
using std::list;
31
using std::ostream;
32
33
namespace ns3 {
34
35
/* File-scope */
36
namespace {
37
  list<ostream*> s_streams;
38
39
  /* Overrides normal SIGSEGV handler once the
40
   * _handleTerminate function is run. */
41
  void sigHandler(int sig) {_handleTerminate ();}
42
  struct sigaction hdl;
43
}
44
45
void
46
_registerStream (ostream& stream)
47
{
48
  s_streams.push_back (&stream);
49
}
50
51
void
52
_unregisterStream (ostream& stream)
53
{
54
  s_streams.remove (&stream);
55
}
56
57
void
58
_handleTerminate (void)
59
{
60
  /* Override default SIGSEGV handler - will flush subsequent
61
   * streams even if one of the stream pointers is bad. */
62
  hdl.sa_handler=sigHandler;
63
  sigaction (SIGSEGV, &hdl, 0);
64
65
  while (!s_streams.empty ())
66
    {
67
      ostream* s (s_streams.front ());
68
      s_streams.pop_front ();
69
      s->flush ();
70
    }
71
72
  std::fflush (0);
73
74
  std::cout.flush ();
75
  std::cerr.flush ();
76
77
  /* Restore default SIGSEGV handler (Not that it matters anyway) */
78
  hdl.sa_handler=SIG_DFL;
79
  sigaction (SIGSEGV, &hdl, 0);
80
81
  std::abort ();
82
}
83
84
}
(-)b08fe828b686 (+36 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2010 NICTA
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Quincy Tse <quincy.tse@nicta.com.au>
19
 */
20
21
#ifndef FATAL_IMPL_H
22
#define FATAL_IMPL_H
23
24
#include <ostream>
25
26
namespace ns3 {
27
28
void _registerStream (std::ostream& stream);
29
30
void _unregisterStream (std::ostream& stream);
31
32
void _handleTerminate (void);
33
34
}
35
36
#endif
(-)a/src/core/wscript (+1 lines)
 Lines 80-85    Link Here 
80
        'type-traits-test-suite.cc',
80
        'type-traits-test-suite.cc',
81
        'traced-callback-test-suite.cc',
81
        'traced-callback-test-suite.cc',
82
        'ptr-test-suite.cc',
82
        'ptr-test-suite.cc',
83
        'fatal-impl.cc',
83
        ]
84
        ]
84
85
85
    headers = bld.new_task_gen('ns3header')
86
    headers = bld.new_task_gen('ns3header')

Return to bug 933