blob: d83ddbf254c364760708f05b80e5719cefcd56ed [file] [log] [blame]
mostang.com!davidma4ab1092003-03-28 07:43:22 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2002-2003 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26/* This file tests unwinding from a constructor from within an
27 atexit() handler. */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32
33#include <libunwind.h>
34
hp.com!davidm587bba52003-09-25 05:29:14 +000035int verbose, errors;
mostang.com!davidma4ab1092003-03-28 07:43:22 +000036
37#define panic(args...) \
38 { ++errors; fprintf (stderr, args); return; }
39
40class Test_Class {
41 public:
42 Test_Class (void);
43};
44
45static Test_Class t;
46
47static void
48backtrace (void)
49{
50 char name[128], off[32];
51 unw_word_t ip, offset;
52 unw_cursor_t cursor;
53 unw_context_t uc;
54 int ret, count = 0;
55
56 unw_getcontext (&uc);
57 unw_init_local (&cursor, &uc);
58
59 do
60 {
61 unw_get_reg (&cursor, UNW_REG_IP, &ip);
62 name[0] = '\0';
63 off[0] = '\0';
64 if (unw_get_proc_name (&cursor, name, sizeof (name), &offset) == 0
65 && off > 0)
66 snprintf (off, sizeof (off), "+0x%lx", (long) offset);
hp.com!davidm587bba52003-09-25 05:29:14 +000067 if (verbose)
68 printf (" [%lx] <%s%s>\n", (long) ip, name, off);
mostang.com!davidma4ab1092003-03-28 07:43:22 +000069 if (++count > 32)
70 panic ("FAILURE: didn't reach beginning of unwind-chain\n");
71 }
72 while ((ret = unw_step (&cursor)) > 0);
73
74 if (ret < 0)
75 panic ("FAILURE: unw_step() returned %d\n", ret);
76}
77
78static void
79b (void)
80{
81 backtrace();
82}
83
84static void
85a (void)
86{
hp.com!davidm587bba52003-09-25 05:29:14 +000087 if (verbose)
88 printf ("backtrace() from atexit()-handler:\n");
mostang.com!davidma4ab1092003-03-28 07:43:22 +000089 b();
90 if (errors)
91 abort (); /* cannot portably call exit() from an atexit() handler */
92}
93
94Test_Class::Test_Class (void)
95{
hp.com!davidm587bba52003-09-25 05:29:14 +000096 if (verbose)
97 printf ("backtrace() from constructor:\n");
mostang.com!davidma4ab1092003-03-28 07:43:22 +000098 b();
99}
100
101int
102main (int argc, char **argv)
103{
hp.com!davidm587bba52003-09-25 05:29:14 +0000104 verbose = argc > 1;
mostang.com!davidma4ab1092003-03-28 07:43:22 +0000105 return atexit (a);
106}