File: main.cc

package info (click to toggle)
monotone 0.18-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 16,440 kB
  • ctags: 13,394
  • sloc: sh: 130,618; ansic: 70,657; cpp: 51,980; perl: 421; makefile: 359; python: 184; lisp: 132; sql: 83
file content (468 lines) | stat: -rw-r--r-- 11,566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

// This file is derived from execution_monitor.cpp, a part of boost.
// 
// the error reporting mechanisms in that file were irritating to our
// users, so we've just copied and modified the code, cleaning up 
// parts of it in the process. 
//
// it is somewhat likely that you actually want to look in monotone.cc for
// cpp_main(), which is the function which does more interesting stuff. all
// this file does is interface with the operating system error reporting
// mechanisms to ensure that we back out of SEGV and friends using
// exceptions, and translate system exceptions to something helpful for a
// user doing debugging or error reporting.

#include <boost/cstdlib.hpp>
#include <boost/config.hpp>
#include <string>
#include <new>
#include <typeinfo>
#include <exception>
#include <stdexcept>
#include <cstring>
#include <iostream>
#include <ui.hh>

// Microsoft + other compatible compilers such as Intel
#if defined(_MSC_VER) || (defined(__MWERKS__) && __MWERKS__ >= 0x3000)
#define MS_STRUCTURED_EXCEPTION_HANDLING
#include <wtypes.h>
#include <winbase.h>
#include <excpt.h>
#include <eh.h> 
#if !defined(__MWERKS__)
#define MS_CRT_DEBUG_HOOK
#include <crtdbg.h>
#endif

#elif (defined(__BORLANDC__) && defined(_Windows))
#define MS_STRUCTURED_EXCEPTION_HANDLING
#include <windows.h>  // Borland 5.5.1 has its own way of doing things. 

#elif (defined(__GNUC__) && defined(__MINGW32__)) 
#define MS_STRUCTURED_EXCEPTION_HANDLING
#include <windows.h> 

#elif defined(__unix) || defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__)
#define UNIX_STYLE_SIGNAL_HANDLING
#include <unistd.h>
#include <csignal>
#include <csetjmp>

#else
#error "no known OS signal handling interface"
#endif


// A rough outline of what this file does:
// 
// runs main()
//   - sets up a try block to catch the_one_true_exception
//   - calls main_with_many_flavours_of_exception
//     + sets up a try block with a zillion little exception handlers
//       all of which translate into the_one_true_exception
//     + installs structured exception handler and assertion handler
//       for microsoft systems
//     + calls main_with_optional_signal_handling
//       * sets up a unix sigjump_buf if appropriate
//       * calls cpp_main

extern int 
cpp_main(int argc, char ** argv);

static const size_t 
REPORT_ERROR_BUFFER_SIZE = 512;

#ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::strlen; using ::strncat; }
#endif

struct
the_one_true_exception
{
  static char buf[REPORT_ERROR_BUFFER_SIZE];
  explicit the_one_true_exception(char const *msg1, char const *msg2)
  {
    buf[0] = '\0';
    std::strncat(buf, msg1, sizeof(buf)-1);
    std::strncat(buf, msg2, sizeof(buf)-1-std::strlen(buf));
  }
};
char the_one_true_exception::buf[REPORT_ERROR_BUFFER_SIZE];

static void 
report_error(char const *msg1, char const *msg2 = "")
{
  throw the_one_true_exception(msg1, msg2);
}


////////////////////////////////////////////////
// windows style structured exception handling 
// (and assertions, which get their own support)
////////////////////////////////////////////////

#if defined(MS_CRT_DEBUG_HOOK)
static int
assert_reporting_function(int reportType, char* userMessage, int* retVal)
{
  switch (reportType)
    {
    case _CRT_ASSERT:
      report_error(userMessage);
      return 1;
      
    case _CRT_ERROR:
      report_error(userMessage);
      return 1;
      
    default:
      return 0;
    }
}
#endif

#ifdef MS_STRUCTURED_EXCEPTION_HANDLING
struct
ms_se_exception 
{
  unsigned int exception_id;
  explicit ms_se_exception(unsigned int n) 
    : exception_id(n) 
  {}
};

static void
ms_se_trans_func(unsigned int id, _EXCEPTION_POINTERS*)
{
  throw ms_se_exception(id);
}

static void
report_ms_se_error(unsigned int id)
{
  switch (id) 
    {
    case EXCEPTION_ACCESS_VIOLATION:
      report_error("memory access violation");
      break;
      
    case EXCEPTION_ILLEGAL_INSTRUCTION:
      report_error("illegal instruction");
      break;
      
    case EXCEPTION_PRIV_INSTRUCTION:
      report_error("privilaged instruction");
      break;

    case EXCEPTION_IN_PAGE_ERROR:
      report_error("memory page error");
      break;

    case EXCEPTION_STACK_OVERFLOW:
      report_error("stack overflow");
      break;

    case EXCEPTION_DATATYPE_MISALIGNMENT:
      report_error("data misalignment");
      break;
      
    case EXCEPTION_INT_DIVIDE_BY_ZERO:
      report_error("integer divide by zero");
      break;

    case EXCEPTION_INT_OVERFLOW:
      report_error("integer overflow");
      break;

    case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
      report_error("array bounds exceeded");
      break;

    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
      report_error("floating point divide by zero");
      break;

    case EXCEPTION_FLT_STACK_CHECK:
      report_error("floating point stack check");
      break;
      
    case EXCEPTION_FLT_DENORMAL_OPERAND:
    case EXCEPTION_FLT_INEXACT_RESULT:
    case EXCEPTION_FLT_INVALID_OPERATION:
    case EXCEPTION_FLT_OVERFLOW:
    case EXCEPTION_FLT_UNDERFLOW:
      report_error("floating point error");
      break;
      
    default:
      report_error("unrecognized exception or signal");
    }
}

#if (defined(__BORLANDC__) && defined(_Windows))
// this works for Borland but not other Win32 compilers (which trap too many cases)
static int 
main_with_signal_handlers(int argc, char **argv)
{
  int result;
  __try 
    { 
      result = cpp_main(argc, argv); 
    }    
  __except (1)
    {
      throw ms_se_exception(GetExceptionCode());
    }
  return result;
}

#else
static int 
main_with_signal_handlers(int argc, char **argv)
{
  return cpp_main(argc, argv);
}
#endif


/////////////////////////////
// unix style signal handling 
/////////////////////////////

#elif defined(UNIX_STYLE_SIGNAL_HANDLING)
struct
unix_signal_exception 
{
  char const * error_message;
  explicit unix_signal_exception(char const * em)
    : error_message(em)
  {}
};

static sigjmp_buf jump_buf;

extern "C" 
{
  static void 
  unix_style_signal_handler(int sig)
  {
    siglongjmp(jump_buf, sig);
  }  
}

static int 
main_with_signal_handlers(int argc, char **argv)
{
    typedef struct sigaction* sigaction_ptr;
    static struct sigaction all_signals_action;
    struct sigaction old_SIGFPE_action;
    struct sigaction old_SIGTRAP_action;
    struct sigaction old_SIGSEGV_action;
    struct sigaction old_SIGBUS_action;
    struct sigaction old_SIGABRT_action;

    all_signals_action.sa_flags   = 0;
    all_signals_action.sa_handler = &unix_style_signal_handler;
    sigemptyset(&all_signals_action.sa_mask);
    
    sigaction(SIGFPE , &all_signals_action, &old_SIGFPE_action);
    sigaction(SIGTRAP, &all_signals_action, &old_SIGTRAP_action);
    sigaction(SIGSEGV, &all_signals_action, &old_SIGSEGV_action);
    sigaction(SIGBUS , &all_signals_action, &old_SIGBUS_action);
    sigaction(SIGABRT, &all_signals_action, &old_SIGABRT_action);

    int result = 0;
    bool trapped_signal = false;
    char const *em = NULL;

    volatile int sigtype = sigsetjmp(jump_buf, 1);

    if(sigtype == 0) 
      {
        result = cpp_main(argc, argv);
      }
    
    else 
      {
        trapped_signal = true;
        switch(sigtype) 
          {
          case SIGTRAP:
            em = "signal: SIGTRAP (perhaps integer divide by zero)";
            break;
          case SIGFPE:
            em = "signal: SIGFPE (arithmetic exception)";
            break;
          case SIGABRT:
            em = "signal: SIGABRT (application abort requested)";
            break;
          case SIGSEGV:
          case SIGBUS:
            em = "signal: memory access violation";
            break;
          default:
            em = "signal: unrecognized signal";
          }
      }
    
    sigaction(SIGFPE , &old_SIGFPE_action , sigaction_ptr());
    sigaction(SIGTRAP, &old_SIGTRAP_action, sigaction_ptr());
    sigaction(SIGSEGV, &old_SIGSEGV_action, sigaction_ptr());
    sigaction(SIGBUS , &old_SIGBUS_action , sigaction_ptr());
    sigaction(SIGABRT, &old_SIGABRT_action, sigaction_ptr());
    
    if(trapped_signal) 
      throw unix_signal_exception(em);

    return result;
}
#endif


static int 
main_with_many_flavours_of_exception(int argc, char **argv)
{
  
#if defined(MS_STRUCTURED_EXCEPTION_HANDLING) && !defined(__BORLANDC__) && !defined(__MINGW32__)
  _set_se_translator(ms_se_trans_func);
#endif
  
#if defined(MS_CRT_DEBUG_HOOK)
  _CrtSetReportHook(&assert_reporting_function);
#endif

    try 
      {
        return main_with_signal_handlers(argc, argv);
      }

    catch (char const * ex)
      { 
        report_error("C string: ", ex); 
      }

    catch (std::string const & ex)
      { 
        report_error("std::string: ", ex.c_str()); 
      }
    
    catch( std::bad_alloc const & ex )
      { 
        report_error("std::bad_alloc: ", ex.what()); 
      }
    
#if !defined(__BORLANDC__) || __BORLANDC__ > 0x0551
    catch (std::bad_cast const & ex)
      { 
        report_error("std::bad_cast: ", ex.what()); 
      }

    catch (std::bad_typeid const & ex)
      { 
        report_error("std::bad_typeid: ", ex.what()); 
      }
#else
    catch(std::bad_cast const & ex)
      { 
        report_error("std::bad_cast"); 
      }

    catch( std::bad_typeid const & ex)
      { 
        report_error("std::bad_typeid"); 
      }
#endif
    
    catch(std::bad_exception const & ex)
      { 
        report_error("std::bad_exception: ", ex.what()); 
      }

    catch( std::domain_error const& ex )
      { 
        report_error("std::domain_error: ", ex.what()); 
      }

    catch( std::invalid_argument const& ex )
      { 
        report_error("std::invalid_argument: ", ex.what()); 
      }

    catch( std::length_error const& ex )
      { 
        report_error("std::length_error: ", ex.what()); 
      }

    catch( std::out_of_range const& ex )
      { 
        report_error("std::out_of_range: ", ex.what()); 
      }

    catch( std::range_error const& ex )
      { 
        report_error("std::range_error: ", ex.what()); 
      }

    catch( std::overflow_error const& ex )
      { 
        report_error("std::overflow_error: ", ex.what()); 
      }

    catch( std::underflow_error const& ex )
      { 
        report_error("std::underflow_error: ", ex.what()); 
      }

    catch( std::logic_error const& ex )
      { 
        report_error("std::logic_error: ", ex.what()); 
      }

    catch( std::runtime_error const& ex )
      { 
        report_error("std::runtime_error: ", ex.what()); 
      }

    catch( std::exception const& ex )
      { 
        report_error("std::exception: ", ex.what()); 
      }

#if defined(MS_STRUCTURED_EXCEPTION_HANDLING)
    catch(ms_se_exception const & ex)
      { 
        report_ms_se_error(ex.exception_id); 
      }

#elif defined(UNIX_STYLE_SIGNAL_HANDLING)
    catch(unix_signal_exception const & ex)
      { 
        report_error(ex.error_message); 
      }
#endif

    catch( ... )
      { 
        report_error("unknown type" ); 
      }
    return 0;
}

int 
main(int argc, char **argv)
{
  try
    {
      return main_with_many_flavours_of_exception(argc, argv);
    }
  catch (the_one_true_exception const & e)
    {
      ui.fatal(std::string(e.buf) + "\n");
      // If we got here, it's because something went _really_ wrong, like an
      // invariant failure or a segfault.  So use a distinctive error code, in
      // particular so the testsuite can tell whether we detected an error
      // properly or waited until an invariant caught it...
      return 3;
    }
}