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>
5 This file is part of libunwind.
7 Copyright (c) 2003 Hewlett-Packard Co.
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:
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
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. */
30 #include <libunwind.h>
36 #include <sys/resource.h>
39 #define panic(args...) \
40 { fprintf (stderr, args); exit (-1); }
44 #define PAGE_SIZE 4096
46 void do_backtrace (void)
50 mprotect((void*)((uintptr_t)stack_start & ~(PAGE_SIZE - 1)),
51 PAGE_SIZE, PROT_NONE);
60 if (unw_init_local (&cursor, &uc) < 0)
61 panic ("unw_init_local failed!\n");
65 unw_get_reg (&cursor, UNW_REG_IP, &ip);
66 unw_get_reg (&cursor, UNW_REG_SP, &sp);
68 ret = unw_step (&cursor);
71 unw_get_reg (&cursor, UNW_REG_IP, &ip);
82 mprotect((void*)((uintptr_t)stack_start & ~(PAGE_SIZE - 1)),
83 PAGE_SIZE, PROT_READ|PROT_WRITE);
86 void consume_and_run (int depth)
92 sprintf (string, "hello %p %p\n", &cursor, &uc);
96 consume_and_run(depth - 1);
101 main (int argc, char **argv UNUSED)
107 stack_start = &start;
109 // Initialize pipe mem validate check, opens file descriptors
111 if (unw_init_local (&cursor, &uc) < 0)
112 panic ("unw_init_local failed!\n");
115 for (i = 3; i < 10; i++)
118 pid_t childpid = fork();
121 /* Close fds and make sure we still work */
129 if (WIFEXITED(status))
130 return WEXITSTATUS(status);
136 consume_and_run (10);