Home | History | Annotate | Download | only in tests
      1 /* libunwind - a platform-independent unwind library
      2    Copyright (C) 2004 Hewlett-Packard Co
      3 	Contributed by David Mosberger-Tang <davidm (at) hpl.hp.com>
      4 
      5 This file is part of libunwind.
      6 
      7 Copyright (c) 2002 Hewlett-Packard Co.
      8 
      9 Permission is hereby granted, free of charge, to any person obtaining
     10 a copy of this software and associated documentation files (the
     11 "Software"), to deal in the Software without restriction, including
     12 without limitation the rights to use, copy, modify, merge, publish,
     13 distribute, sublicense, and/or sell copies of the Software, and to
     14 permit persons to whom the Software is furnished to do so, subject to
     15 the following conditions:
     16 
     17 The above copyright notice and this permission notice shall be
     18 included in all copies or substantial portions of the Software.
     19 
     20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     26 WITH 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 
     45 int verbose;
     46 
     47 static int
     48 do_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 
     89 static int
     90 foo (void)
     91 {
     92   return do_backtrace ();
     93 }
     94 
     95 int
     96 main (int argc, char **argv UNUSED)
     97 {
     98   verbose = (argc > 1);
     99 
    100   if (verbose)
    101     printf ("Normal backtrace:\n");
    102   return foo ();
    103 }
    104