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 / +149 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
/**
80
 * \ingroup debugging
81
 *
82
 * \brief Sets the terminate handler to the normal C++ handler.
83
 *
84
 * This macro sets the default handler for std::terminate() back
85
 * to the standard C++ handler.
86
 *
87
 * The standard C++ handler (post-gcc3.4) will identify any
88
 * uncaught exceptions and print the output of what(), before
89
 * raising SIGIOT and terminate without clean up.
90
 */
91
#define NS_FATAL_SET_CXX_VERBOSE()                                  \
92
  do                                                                \
93
    {                                                               \
94
      std::set_terminate (__gnu_cxx::__verbose_terminate_handler);  \
95
    }                                                               \
96
  while (false)
97
98
/**
99
 * \ingroup debugging
100
 *
101
 * \brief Sets the terminate handler to silent abort.
102
 *
103
 * This macro replaces the default handler for std::terminate()
104
 * with the std::abort().
105
 *
106
 * std::abort will raise SIGIOT and terminates without clean up
107
 * or any output.
108
 */
109
#define NS_FATAL_SET_CXX_SILENT()                   \
110
  do                                                \
111
    {                                               \
112
      std::set_terminate (std::abort);              \
113
    }                                               \
114
  while (false)
115
116
/**
117
 * \ingroup debugging
118
 *
119
 * \brief Sets the terminate handler to the NS3 default.
120
 *
121
 * This macro replaces the default handler for std::terminate()
122
 * with the ns3::FatalError::HandleTerminate().
123
 *
124
 * ns3::FatalError::HandleTerminate will flush all registered
125
 * ostreams and all FILE* before aborting.
126
 */
127
#define NS_FATAL_SET_DEFAULT()                                \
128
  do                                                          \
129
    {                                                         \
130
      std::set_terminate (ns3::FatalError::HandleTerminate);  \
131
    }                                                         \
132
  while (false)
133
134
/**
135
 * \ingroup debugging
136
 * \param handler The handler to ben triggered on std::terminate()
137
 *
138
 * \brief Replaces the default terminate handler.
139
 *
140
 * This macro replaces the default handler for std::terminate()
141
 * with the specified handler.
142
 *
143
 * The handler should be a function that takes no arguments and
144
 * returns void. The function should not return. If the function
145
 * returns, std::terminate will raise SIGIOT and abort execution.
146
 */
147
#define NS_FATAL_SET_HANDLER(handler)                         \
148
  do                                                          \
149
    {                                                         \
150
      std::set_terminate (static_cast<void (*)()>(handler));  \
151
    }                                                         \
152
  while (false)
153
154
/**
155
 * \ingroup debugging
156
 * \param stream pointer to stream to be flushed.
157
 *
158
 * \brief Register a stream to be flushed on abnormal exit.
159
 *
160
 * This is an alias to ns3::FatalError::RegisterStream()
161
 */
162
#define NS_FATAL_REGISTER_STREAM(stream)            \
163
  do                                                \
164
    {                                               \
165
      ns3::FatalError::RegisterStream (stream);     \
166
    }                                               \
167
  while (false)
168
169
/**
170
 * \ingroup debugging
171
 * \param stream A pointer to the stream to be unregistered.
172
 *
173
 * \brief Unregister a stream for flushing on abnormal exit.
174
 *
175
 * This is an alias to ns3::FatalError::UnregisterStream()
176
 */
177
#define NS_FATAL_UNREGISTER_STREAM(stream)          \
178
  do                                                \
179
    {                                               \
180
      ns3::FatalError::UnregisterStream (stream);   \
181
    }                                               \
182
  while (false)
46
183
47
#endif /* FATAL_ERROR_H */
184
#endif /* FATAL_ERROR_H */
(-)069840de4fac (+116 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
namespace FatalError {
35
36
/* File-scope */
37
namespace {
38
  std::list<std::ostream*>& GetStreamList (void)
39
    {
40
      static std::list<ostream*> streams;
41
      return streams;
42
    }
43
}
44
45
void
46
RegisterStream (ostream* stream)
47
{
48
  GetStreamList ().push_back (stream);
49
}
50
51
void
52
UnregisterStream (ostream* stream)
53
{
54
  GetStreamList ().remove (stream);
55
}
56
57
void
58
HandleTerminate (void)
59
{
60
  FlushStreams ();
61
  std::abort ();
62
}
63
64
65
namespace {
66
  /* Overrides normal SIGSEGV handler once the
67
   * HandleTerminate function is run. */
68
  void sigHandler(int sig) {HandleTerminate ();}
69
}
70
71
inline void
72
FlushStreams (void)
73
{
74
  struct sigaction hdl;
75
76
  /* Override default SIGSEGV handler - will flush subsequent
77
   * streams even if one of the stream pointers is bad.
78
   * The SIGSEGV override should only be active for the
79
   * duration of this function. */
80
  hdl.sa_handler=sigHandler;
81
  sigaction (SIGSEGV, &hdl, 0);
82
83
  std::list<ostream*>& l = GetStreamList ();
84
85
  /* Need to do it this way in case any of the ostream* causes SIGSEGV */
86
  while (!l.empty ())
87
    {
88
      ostream* s (l.front ());
89
      l.pop_front ();
90
      s->flush ();
91
    }
92
93
  /* Restore default SIGSEGV handler (Not that it matters anyway) */
94
  hdl.sa_handler=SIG_DFL;
95
  sigaction (SIGSEGV, &hdl, 0);
96
97
  /* Flush all opened FILE* */
98
  std::fflush (0);
99
100
  /* Flush stdandard streams - shouldn't be required (except for clog) */
101
  std::cout.flush ();
102
  std::cerr.flush ();
103
  std::clog.flush ();
104
}
105
106
namespace {
107
  struct _StartUp {
108
    // Run following code on startup
109
    _StartUp (void) {
110
      std::set_terminate (HandleTerminate);
111
    }
112
  } _start;
113
}
114
115
} //FatalError
116
} //ns3
(-)069840de4fac (+137 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
/**
27
 * \ingroup debugging
28
 * \defgroup fatalHandler Fatal Error Handler 
29
 *
30
 * \brief Functions to help clean up when fatal error
31
 * is encountered.
32
 *
33
 * The functions in this group are used to perform
34
 * limited clean up, like flushing active streams, when
35
 * fatal error are encountered (through assertion fail,
36
 * calls to NS_ABORT_* and calls to NS_FATAL_ERROR.
37
 *
38
 * Currently, other than flushing active ostreams, these
39
 * functions does not interfere with outside memory. There
40
 * is still a residual risk that may be invalid ostream
41
 * pointers may be present, and may corrupt the memory
42
 * on the attempt to execute the flush() function.
43
 *
44
 * The function to handle abnormal termination should not
45
 * be called directly. It is automatically triggered when
46
 * a std::terminate() is called. Do not call FlushStreams()
47
 * or HandleTerminate - they will cause the program to
48
 * perform abnormal termination.
49
 *
50
 * Terminating by dereferencing null pointers and by calls
51
 * to std::abort will not trigger the clean up code.
52
 *
53
 * NS3 developers may use NS_FATAL_SET_HANDLER to change
54
 * the behaviour on abnormal termination caused by calls
55
 * to std::terminate(). Developers should call the
56
 * FlushStreams() function at the end of their handler
57
 * when they are implementing their own handler, otherwise
58
 * the ostreams will be not flushed, or their handling
59
 * code may not be executed (FlushStreams() may call
60
 * std::abort() if there are bad ostream* registered.
61
 *
62
 */
63
64
namespace ns3 {
65
namespace FatalError {
66
67
/**
68
 * \ingroup fatalHandler
69
 * \param stream The stream to be flushed on abnormal exit.
70
 *
71
 * \brief Register a stream to be flushed on abnormal exit.
72
 *
73
 * If a std::terminate() call is encountered after the
74
 * stream had been registered and before it had been
75
 * unregistered, stream->flush() will be called. Users of
76
 * this function is to ensure stream remains valid until
77
 * it had been unregistered.
78
 */
79
void RegisterStream (std::ostream* stream);
80
81
/**
82
 * \ingroup fatalHandler
83
 * \param stream The stream to be unregistered.
84
 *
85
 * \brief Unregister a stream for flushing on abnormal exit.
86
 *
87
 * After a stream had been unregistered, stream->flush()
88
 * will no longer be called should abnormal termination is
89
 * encountered.
90
 *
91
 * If stream is not registered, nothing will happen.
92
 */
93
void UnregisterStream (std::ostream* stream);
94
95
/**
96
 * \ingroup fatalHandler
97
 *
98
 * \brief Flush all currently registered streams.
99
 *
100
 * This function iterates through each registered stream and
101
 * unregister them. The default SIGSEGV handler is overridden
102
 * when this function is being executed, and will be restored
103
 * when this function returns.
104
 *
105
 * If a SIGSEGV is encountered (most likely due to bad ostream*
106
 * being registered, or a registered osteam* pointing to an
107
 * ostream that had already been destroyed), this function will
108
 * skip the bad ostream* and continue to flush the next stram.
109
 * The function will then terminate raising SIGIOT (aka SIGABRT)
110
 *
111
 * DO NOT call this function until the program is ready to crash.
112
 */
113
void FlushStreams (void);
114
115
/**
116
 * \ingroup fatalHandler
117
 *
118
 * \brief Handles clean up when std::terminate() is called.
119
 *
120
 * Once this handler had been registered using
121
 * std::set_terminate() (via the macro NS_FATAL_SET_DEFAULT()),
122
 * The system will trigger this function when std::terminate() is
123
 * called. std::terminate() expects this function not to return.
124
 * If this function returns, std::terminate will raise a SIGIOT
125
 * and terminate the program.
126
 *
127
 * This function calls FlushStreams() and then aborts execution
128
 * using std::abort().
129
 *
130
 * This function does not return.
131
 */
132
void HandleTerminate (void);
133
134
} //FatalError
135
} //ns3
136
137
#endif
(-)a/src/core/wscript (+2 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')
 Lines 128-133    Link Here 
128
        'names.h',
129
        'names.h',
129
        'vector.h',
130
        'vector.h',
130
        'default-deleter.h',
131
        'default-deleter.h',
132
        'fatal-impl.h',
131
        ]
133
        ]
132
134
133
    if sys.platform == 'win32':
135
    if sys.platform == 'win32':

Return to bug 933