1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2003-2004 Hewlett-Packard Co
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Copyright (c) 2003 Hewlett-Packard Co.
8
9Permission is hereby granted, free of charge, to any person obtaining
10a copy of this software and associated documentation files (the
11"Software"), to deal in the Software without restriction, including
12without limitation the rights to use, copy, modify, merge, publish,
13distribute, sublicense, and/or sell copies of the Software, and to
14permit persons to whom the Software is furnished to do so, subject to
15the following conditions:
16
17The above copyright notice and this permission notice shall be
18included in all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
27
28#include "compiler.h"
29
30#include <libunwind.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include <sys/resource.h>
37
38#define panic(args...)				\
39	{ fprintf (stderr, args); exit (-1); }
40
41int verbose;
42
43static void
44do_backtrace (void)
45{
46  unw_cursor_t cursor;
47  unw_word_t ip, sp;
48  unw_context_t uc;
49  int ret;
50
51  unw_getcontext (&uc);
52  if (unw_init_local (&cursor, &uc) < 0)
53    panic ("unw_init_local failed!\n");
54
55  do
56    {
57      unw_get_reg (&cursor, UNW_REG_IP, &ip);
58      unw_get_reg (&cursor, UNW_REG_SP, &sp);
59
60      if (verbose)
61	printf ("%016lx (sp=%016lx)\n", (long) ip, (long) sp);
62
63      ret = unw_step (&cursor);
64      if (ret < 0)
65	{
66	  unw_get_reg (&cursor, UNW_REG_IP, &ip);
67	  panic ("FAILURE: unw_step() returned %d for ip=%lx\n",
68		 ret, (long) ip);
69	}
70    }
71  while (ret > 0);
72}
73
74int
75consume_some_stack_space (void)
76{
77  unw_cursor_t cursor;
78  unw_context_t uc;
79  char string[1024];
80
81  memset (&cursor, 0, sizeof (cursor));
82  memset (&uc, 0, sizeof (uc));
83  return sprintf (string, "hello %p %p\n", &cursor, &uc);
84}
85
86int
87main (int argc, char **argv UNUSED)
88{
89  struct rlimit rlim;
90
91  verbose = argc > 1;
92
93  if (consume_some_stack_space () > 9999)
94    exit (-1);	/* can't happen, but don't let the compiler know... */
95
96  rlim.rlim_cur = 0;
97  rlim.rlim_max = RLIM_INFINITY;
98  setrlimit (RLIMIT_DATA, &rlim);
99  setrlimit (RLIMIT_AS, &rlim);
100
101  do_backtrace ();
102  return 0;
103}
104