mostang.com!davidm | 7fbfe0a | 2002-02-15 23:22:05 +0000 | [diff] [blame] | 1 | /* 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 | |
| 5 | This file is part of libunwind. |
| 6 | |
mostang.com!davidm | aca3843 | 2002-11-16 03:25:36 +0000 | [diff] [blame] | 7 | Permission is hereby granted, free of charge, to any person obtaining |
| 8 | a copy of this software and associated documentation files (the |
| 9 | "Software"), to deal in the Software without restriction, including |
| 10 | without limitation the rights to use, copy, modify, merge, publish, |
| 11 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | permit persons to whom the Software is furnished to do so, subject to |
| 13 | the following conditions: |
mostang.com!davidm | 7fbfe0a | 2002-02-15 23:22:05 +0000 | [diff] [blame] | 14 | |
mostang.com!davidm | aca3843 | 2002-11-16 03:25:36 +0000 | [diff] [blame] | 15 | The above copyright notice and this permission notice shall be |
| 16 | included in all copies or substantial portions of the Software. |
mostang.com!davidm | 7fbfe0a | 2002-02-15 23:22:05 +0000 | [diff] [blame] | 17 | |
mostang.com!davidm | aca3843 | 2002-11-16 03:25:36 +0000 | [diff] [blame] | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
mostang.com!davidm | 7fbfe0a | 2002-02-15 23:22:05 +0000 | [diff] [blame] | 25 | |
mostang.com!davidm | 97ae3ba | 2002-02-28 16:24:48 +0000 | [diff] [blame] | 26 | #define UNW_LOCAL_ONLY |
mostang.com!davidm | 7fbfe0a | 2002-02-15 23:22:05 +0000 | [diff] [blame] | 27 | #include <libunwind.h> |
| 28 | |
| 29 | /* See glibc manual for a description of this function. */ |
| 30 | |
| 31 | int |
| 32 | backtrace (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 | } |