aboutsummaryrefslogtreecommitdiff
path: root/tests/backtrace/driver.cxx
blob: ecfa58e6c5af4bee1acf7cf4a46e09fd7b57a91e (plain)
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
// file      : tests/backtrace/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef _WIN32
#  include <sys/resource.h> // setrlimit()
#endif

#include <string>
#include <iostream>
#include <exception>    // set_terminate(), terminate_handler
#include <system_error>

#include <libbutl/process.hxx>
#include <libbutl/fdstream.hxx>
#include <libbutl/backtrace.hxx>

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace butl;

namespace test
{
  // Note: is not static to make sure the stack frame is not optimized out.
  //
  int
  func ()
  {
    throw system_error (EINVAL, generic_category ());
  }
}

static terminate_handler def_term_handler;

// Usages:
//
// argv[0] [-q]
// argv[0] -c [-b]
//
// In the first form run the child processes throwing unhandled exception,
// first of which sets up the backtrace-printing handler prior to throwing,
// and make sure that they terminate in the same way (abnormally or with the
// same exit status). Exit with the zero code if that's the case and the
// children terminated abnormally or with non-zero code and exit with the one
// code otherwise. Redirect stderr to /dev/null for the first child if
// requested (-q) and always for the second one.
//
// In the second form run as a child process that optionally sets up the
// backtrace-printing terminate handler (-b) and throws unhandled exception.
//
int
main (int argc, const char* argv[])
{
  bool child (false);
  bool backtrace (false);
  bool quiet (false);

  for (int i (1); i != argc; ++i)
  {
    string o (argv[i]);

    if (o == "-c")
    {
      child = true;
    }
    else if (o == "-b")
    {
      assert (child);
      backtrace = true;
    }
    else if (o == "-q")
    {
      assert (!child);
      quiet = true;
    }
    else
    {
      assert (false);
    }
  }

  // Run as a child.
  //
  if (child)
  {
    // Disable dumping core file on POSIX.
    //
#ifndef _WIN32
    struct rlimit rlim {0, 0};
    assert (setrlimit (RLIMIT_CORE, &rlim) == 0);
#endif

    if (backtrace)
    {
      def_term_handler = set_terminate ([]()
                                        {
                                          cerr << butl::backtrace ();

                                          if (def_term_handler != nullptr)
                                            def_term_handler ();
                                        });
    }

    return test::func ();
  }

  // Run as a parent.
  //
  auto_fd null (fdopen_null ());
  process_exit pe1 (process_run (0                       /* stdin */,
                                 1                       /* stdout */,
                                 quiet ? null.get () : 2 /* stderr */,
                                 argv[0], "-c", "-b"));

  if (pe1)
  {
    cerr << "error: child exited with zero code" << endl;
    return 1;
  }

  // Always run quiet.
  //
  process_exit pe2 (process_run (0    /* stdin */,
                                 1    /* stdout */,
                                 null /* stderr */,
                                 argv[0], "-c"));

  if (!(pe1.normal () == pe2.normal () &&
        (!pe1.normal () || pe1.code () == pe2.code ())))
  {
    cerr << "error: child processes terminated differently:" << endl <<
            "  info: with backtrace: "    << pe1 << endl <<
            "  info: without backtrace: " << pe2 << endl;

    return 1;
  }

  return 0;
}