Pol  Revision:cb584c9
passert.cpp
Go to the documentation of this file.
1 
10 #include "passert.h"
11 
12 #include <format/format.h>
13 
15 #include "esignal.h"
16 #include "logfacility.h"
17 #include "stlutil.h"
18 
19 #ifdef WINDOWS
20 #include "Header_Windows.h"
21 #include "mdumpimp.h"
22 #else
23 #include <cstdlib>
24 #endif
25 
26 namespace Pol
27 {
28 namespace Clib
29 {
30 bool passert_dump_stack = false;
31 bool passert_shutdown = false;
32 bool passert_abort = true;
33 bool passert_nosave = false;
35 
38 
39 #ifdef _WIN32
40 void force_backtrace( bool /*complete*/ )
41 {
42  __try
43  {
44  int* p = 0;
45  *p = 7; // dump the stack
46  }
47  __except ( ( HiddenMiniDumper::TopLevelFilter( GetExceptionInformation() ) ),
48  EXCEPTION_EXECUTE_HANDLER )
49  {
50  }
51 }
52 #else
53 void force_backtrace( bool complete )
54 {
55  std::string stack_trace = Clib::ExceptionParser::getTrace();
56  fmt::Writer tmp;
57  tmp << "=== Stack Backtrace ===\n" << stack_trace;
58 
59  POLLOG_ERROR << tmp.str() << "\n";
60  if ( complete )
62 }
63 #endif
64 
65 void passert_failed( const char* expr, const char* file, unsigned line )
66 {
67  passert_failed( expr, "", file, line );
68 }
69 
70 void passert_failed( const char* expr, const std::string& reason, const char* file, unsigned line )
71 {
72  if ( reason != "" )
73  POLLOG_ERROR << "Assertion Failed: " << expr << " (" << reason << "), " << file << ", line "
74  << line << "\n";
75  else
76  POLLOG_ERROR << "Assertion Failed: " << expr << ", " << file << ", line " << line << "\n";
77 
78  if ( passert_dump_stack )
79  {
80  POLLOG_ERROR << "Forcing stack backtrace.\n";
82  }
83  else
84  {
85 #ifdef _WIN32
87 #endif
88  }
89 
94  {
95  char reportedReason[512];
96  if ( sprintf( reportedReason, "ASSERT(%s, reason: \"%s\") failed in %s:%d", expr,
97  reason.c_str(), file, line ) > 0 )
99  std::string( reportedReason ) );
100  else
102  }
103 
104 
105  if ( passert_shutdown )
106  {
107  POLLOG_ERROR << "Shutting down due to assertion failure.\n";
108  exit_signalled = true;
109  passert_shutdown_due_to_assertion = true;
110  }
111  if ( passert_abort )
112  {
113  POLLOG_ERROR << "Aborting due to assertion failure.\n";
114  abort();
115  }
116 
117  if ( reason != "" )
118  {
119  throw std::runtime_error( "Assertion Failed: " + std::string( expr ) + " (" +
120  std::string( reason ) + "), " + std::string( file ) + ", line " +
121  tostring( line ) );
122  }
123  else
124  {
125  throw std::runtime_error( "Assertion Failed: " + std::string( expr ) + ", " +
126  std::string( file ) + ", line " + tostring( line ) );
127  }
128 }
129 }
130 }
std::string scripts_thread_script
Definition: passert.cpp:36
static LONG WINAPI TopLevelFilter(struct _EXCEPTION_POINTERS *pExceptionInfo)
Definition: mdump.cpp:136
bool passert_shutdown
Definition: passert.cpp:31
void force_backtrace(bool complete)
Definition: passert.cpp:53
static std::string getTrace()
Returns a string containing the current stack trace.
#define POLLOG_ERROR
Definition: logfacility.h:207
bool passert_dump_stack
Definition: passert.cpp:30
bool passert_nosave
Definition: passert.cpp:33
std::string tostring(const Bscript::BTokenType &v)
Definition: tokens.cpp:19
static bool programAbortReporting()
Returns true if the bug reporting is active.
static void reportProgramAbort(const std::string &stackTrace, const std::string &reason)
Reports a program abort to the program devs.
static void logAllStackTraces()
Logs stack traces of all threads to stdout and error output.
bool passert_shutdown_due_to_assertion
Definition: passert.cpp:34
void passert_failed(const char *expr, const char *file, unsigned line)
Definition: passert.cpp:65
Definition: berror.cpp:12
std::atomic< bool > exit_signalled
bool passert_abort
Definition: passert.cpp:32
unsigned scripts_thread_scriptPC
Definition: passert.cpp:37
static void print_backtrace()
Definition: mdump.cpp:263