1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2010 stefan.demharter@gmx.net
3   Copyright (C) 2010 arun.sharma@google.com
4
5Permission is hereby granted, free of charge, to any person obtaining
6a copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sublicense, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
12
13The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
23
24#include <unistd.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <libunwind.h>
28#include "compiler.h"
29
30#define panic(args...)				\
31	{ fprintf (stderr, args); exit (-1); }
32
33static int verbose;
34
35struct Test
36{
37  public: // --- ctor/dtor ---
38    Test() { ++counter_; }
39    ~Test() { -- counter_; }
40    Test(const Test&) { ++counter_; }
41
42  public: // --- static members ---
43    static int counter_;
44};
45
46int Test::counter_ = 0;
47
48// Called by foo
49extern "C" void bar()
50{
51  Test t;
52  try {
53    Test t;
54    throw 5;
55  } catch (...) {
56    Test t;
57    if (verbose)
58      printf("Throwing an int\n");
59    throw 6;
60  }
61}
62
63int main(int argc, char **argv UNUSED)
64{
65  if (argc > 1)
66    verbose = 1;
67  try {
68    Test t;
69    bar();
70  } catch (int) {
71    // Dtor of all Test-object has to be called.
72    if (Test::counter_ != 0)
73      panic("Counter non-zero\n");
74    return Test::counter_;
75  } catch (...) {
76    // An int was thrown - we should not get here.
77    panic("Int was thrown why are we here?\n");
78  }
79  exit(0);
80}
81