Imported Upstream version 1.3.1
[platform/upstream/libunwind.git] / tests / test-reg-state.c
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
5 This file is part of libunwind.
6
7 Copyright (c) 2003 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 #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/mman.h>
37 #include <sys/resource.h>
38
39 #define panic(args...)                          \
40         { fprintf (stderr, args); exit (-1); }
41
42 int verbose;
43
44 struct cb_data
45 {
46   unw_word_t ip;
47   void* reg_state;
48   size_t len;
49 };
50
51 static int
52 dwarf_reg_states_callback(void *token,
53                           void *rs,
54                           size_t size,
55                           unw_word_t start_ip, unw_word_t end_ip)
56 {
57   struct cb_data *data = token;
58   if (start_ip <= data->ip && data->ip < end_ip)
59     {
60       data->reg_state = mmap(NULL, size, PROT_READ | PROT_WRITE,
61                              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
62       memcpy(data->reg_state, rs, size);
63       data->len = size;
64     }
65   return 0;
66 }
67
68 static void
69 do_backtrace (void)
70 {
71   unw_cursor_t cursor;
72   unw_word_t ip, sp;
73   unw_context_t uc;
74   int ret;
75
76   unw_getcontext (&uc);
77   if (unw_init_local (&cursor, &uc) < 0)
78     panic ("unw_init_local failed!\n");
79
80   do
81     {
82       unw_get_reg (&cursor, UNW_REG_IP, &ip);
83       unw_get_reg (&cursor, UNW_REG_SP, &sp);
84
85       if (verbose)
86         printf ("%016lx (sp=%016lx)\n", (long) ip, (long) sp);
87
88       struct cb_data data = {.ip = ip, .reg_state = NULL};
89       ret = unw_reg_states_iterate(&cursor, dwarf_reg_states_callback, &data);
90       if (ret > 0)
91         {
92           ret = unw_apply_reg_state (&cursor, data.reg_state);
93           munmap(data.reg_state, data.len);
94         }
95       if (ret < 0)
96         {
97           unw_get_reg (&cursor, UNW_REG_IP, &ip);
98           panic ("FAILURE: unw_step() returned %d for ip=%lx\n",
99                  ret, (long) ip);
100         }
101     }
102   while (ret > 0);
103 }
104
105 int
106 consume_some_stack_space (void)
107 {
108   unw_cursor_t cursor;
109   unw_context_t uc;
110   char string[1024];
111
112   memset (&cursor, 0, sizeof (cursor));
113   memset (&uc, 0, sizeof (uc));
114   return sprintf (string, "hello %p %p\n", &cursor, &uc);
115 }
116
117 int
118 main (int argc, char **argv UNUSED)
119 {
120   struct rlimit rlim;
121
122   verbose = argc > 1;
123
124   if (consume_some_stack_space () > 9999)
125     exit (-1);  /* can't happen, but don't let the compiler know... */
126
127   rlim.rlim_cur = 0;
128   rlim.rlim_max = RLIM_INFINITY;
129   setrlimit (RLIMIT_DATA, &rlim);
130
131   do_backtrace ();
132   return 0;
133 }