1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2004 Hewlett-Packard Co
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Copyright (c) 2002 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/* This test simply verifies that unw_init_remote() can be used in
29   lieu of unw_init_local().  This was broken for a while on ia64.  */
30
31#ifdef HAVE_CONFIG_H
32# include "config.h"
33#endif
34
35#include "compiler.h"
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <libunwind.h>
41
42#define panic(args...)				\
43	{ fprintf (stderr, args); exit (-1); }
44
45int verbose;
46
47static int
48do_backtrace (void)
49{
50  char buf[512], name[256];
51  unw_word_t ip, sp, off;
52  unw_cursor_t cursor;
53  unw_context_t uc;
54  int ret;
55
56  unw_getcontext (&uc);
57  if (unw_init_remote (&cursor, unw_local_addr_space, &uc) < 0)
58    panic ("unw_init_remote failed!\n");
59
60  do
61    {
62      unw_get_reg (&cursor, UNW_REG_IP, &ip);
63      unw_get_reg (&cursor, UNW_REG_SP, &sp);
64      buf[0] = '\0';
65      if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0)
66	{
67	  if (off)
68	    snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off);
69	  else
70	    snprintf (buf, sizeof (buf), "<%s>", name);
71	}
72      if (verbose)
73	printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
74
75      ret = unw_step (&cursor);
76      if (ret < 0)
77	{
78	  unw_get_reg (&cursor, UNW_REG_IP, &ip);
79	  printf ("FAILURE: unw_step() returned %d for ip=%lx\n",
80		  ret, (long) ip);
81	  return -1;
82	}
83    }
84  while (ret > 0);
85
86  return 0;
87}
88
89static int
90foo (void)
91{
92  return do_backtrace ();
93}
94
95int
96main (int argc, char **argv UNUSED)
97{
98  verbose = (argc > 1);
99
100  if (verbose)
101    printf ("Normal backtrace:\n");
102  return foo ();
103}
104