blob: 97addea78871365c356759b5830a192e68623c6f [file] [log] [blame]
mostang.com!davidm7fbfe0a2002-02-15 23:22:05 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2002 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
mostang.com!davidmaca38432002-11-16 03:25:36 +00007Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
mostang.com!davidm7fbfe0a2002-02-15 23:22:05 +000014
mostang.com!davidmaca38432002-11-16 03:25:36 +000015The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
mostang.com!davidm7fbfe0a2002-02-15 23:22:05 +000017
mostang.com!davidmaca38432002-11-16 03:25:36 +000018THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
mostang.com!davidm7fbfe0a2002-02-15 23:22:05 +000025
mostang.com!davidm97ae3ba2002-02-28 16:24:48 +000026#define UNW_LOCAL_ONLY
mostang.com!davidm7fbfe0a2002-02-15 23:22:05 +000027#include <libunwind.h>
28
29/* See glibc manual for a description of this function. */
30
31int
32backtrace (void **buffer, int size)
33{
34 unw_cursor_t cursor;
35 unw_context_t uc;
36 unw_word_t ip;
37 int n = 0;
38
39 unw_getcontext (&uc);
40 if (unw_init_local (&cursor, &uc) < 0)
41 return 0;
42
43 while (unw_step (&cursor) > 0)
44 {
45 if (n >= size)
46 return n;
47
48 if (unw_get_reg (&cursor, UNW_REG_IP, &ip) < 0)
49 return n;
50 buffer[n++] = (void *) ip;
51 }
52 return n;
53}