1/* -*- mode: C; c-basic-offset: 4; -*-
2 *
3 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 *
24 *
25 *            C interface to libuwind
26 *
27 * Source compatible with libuwind API documented at:
28 *    http://www.nongnu.org/libunwind/man/libunwind(3).html
29 *
30 */
31
32
33#ifndef __LIBUNWIND__
34#define __LIBUNWIND__
35
36#include <stdint.h>
37#include <stddef.h>
38#include <mach/mach_types.h>
39#include <Availability.h>
40
41/* error codes */
42enum {
43    UNW_ESUCCESS           = 0,            /* no error */
44    UNW_EUNSPEC            = -6540,        /* unspecified (general) error */
45    UNW_ENOMEM             = -6541,        /* out of memory */
46    UNW_EBADREG            = -6542,        /* bad register number */
47    UNW_EREADONLYREG       = -6543,        /* attempt to write read-only register */
48    UNW_ESTOPUNWIND        = -6544,        /* stop unwinding */
49    UNW_EINVALIDIP         = -6545,        /* invalid IP */
50    UNW_EBADFRAME          = -6546,        /* bad frame */
51    UNW_EINVAL             = -6547,        /* unsupported operation or bad value */
52    UNW_EBADVERSION        = -6548,        /* unwind info has unsupported version */
53    UNW_ENOINFO            = -6549         /* no unwind info found */
54};
55
56
57struct unw_context_t { uint64_t data[128]; };
58typedef struct unw_context_t     unw_context_t;
59
60struct unw_cursor_t { uint64_t data[140]; };
61typedef struct unw_cursor_t      unw_cursor_t;
62
63typedef struct unw_addr_space*   unw_addr_space_t;
64
65typedef int                      unw_regnum_t;
66typedef uint64_t                 unw_word_t;
67typedef double                   unw_fpreg_t;
68
69struct unw_proc_info_t
70{
71    unw_word_t    start_ip;         /* start address of function */
72    unw_word_t    end_ip;           /* address after end of function */
73    unw_word_t    lsda;             /* address of language specific data area, or zero if not used */
74    unw_word_t    handler;          /* personality routine, or zero if not used */
75    unw_word_t    gp;               /* not used */
76    unw_word_t    flags;            /* not used */
77    uint32_t      format;           /* compact unwind encoding, or zero if none */
78    uint32_t      unwind_info_size; /* size of dwarf unwind info, or zero if none */
79    unw_word_t    unwind_info;      /* address of dwarf unwind info, or zero if none */
80    unw_word_t    extra;            /* mach_header of mach-o image containing function */
81};
82typedef struct unw_proc_info_t unw_proc_info_t;
83
84
85#ifdef __cplusplus
86extern "C" {
87#endif
88
89#if !__arm__
90extern int         unw_getcontext(unw_context_t*)                               __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
91extern int         unw_init_local(unw_cursor_t*, unw_context_t*)                __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
92extern int         unw_step(unw_cursor_t*)                                      __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
93extern int         unw_get_reg(unw_cursor_t*, unw_regnum_t, unw_word_t*)        __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
94extern int         unw_get_fpreg(unw_cursor_t*, unw_regnum_t, unw_fpreg_t*)     __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
95extern int         unw_set_reg(unw_cursor_t*, unw_regnum_t, unw_word_t)         __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
96extern int         unw_set_fpreg(unw_cursor_t*, unw_regnum_t, unw_fpreg_t)      __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
97extern int         unw_resume(unw_cursor_t*)                                    __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
98
99extern const char* unw_regname(unw_cursor_t*, unw_regnum_t)                     __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
100extern int         unw_get_proc_info(unw_cursor_t*, unw_proc_info_t*)           __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
101extern int         unw_is_fpreg(unw_cursor_t*, unw_regnum_t)                    __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
102extern int         unw_is_signal_frame(unw_cursor_t*)                           __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
103extern int         unw_get_proc_name(unw_cursor_t*, char*, size_t, unw_word_t*) __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_5_0);
104//extern int       unw_get_save_loc(unw_cursor_t*, int, unw_save_loc_t*);
105#endif
106
107
108#if UNW_REMOTE
109/*
110 * Mac OS X "remote" API for unwinding other processes on same machine
111 *
112 */
113extern unw_addr_space_t    unw_local_addr_space;
114extern unw_addr_space_t    unw_create_addr_space_for_task(task_t);
115extern void                unw_destroy_addr_space(unw_addr_space_t);
116extern int                 unw_init_remote_thread(unw_cursor_t*, unw_addr_space_t, thread_t*);
117#endif
118
119
120/*
121 * traditional libuwind "remote" API
122 *   NOT IMPLEMENTED on Mac OS X
123 *
124 * extern int               unw_init_remote(unw_cursor_t*, unw_addr_space_t, thread_t*);
125 * extern unw_accessors_t   unw_get_accessors(unw_addr_space_t);
126 * extern unw_addr_space_t  unw_create_addr_space(unw_accessors_t, int);
127 * extern void              unw_flush_cache(unw_addr_space_t, unw_word_t, unw_word_t);
128 * extern int               unw_set_caching_policy(unw_addr_space_t, unw_caching_policy_t);
129 * extern void              _U_dyn_register(unw_dyn_info_t*);
130 * extern void              _U_dyn_cancel(unw_dyn_info_t*);
131 */
132
133
134#ifdef __cplusplus
135}
136#endif
137
138
139// architecture independent register numbers
140enum {
141    UNW_REG_IP = -1,        // instruction pointer
142    UNW_REG_SP = -2,        // stack pointer
143};
144
145
146// 32-bit x86 registers
147enum {
148    UNW_X86_EAX = 0,
149    UNW_X86_ECX = 1,
150    UNW_X86_EDX = 2,
151    UNW_X86_EBX = 3,
152    UNW_X86_EBP = 4,
153    UNW_X86_ESP = 5,
154    UNW_X86_ESI = 6,
155    UNW_X86_EDI = 7
156};
157
158
159// 64-bit x86_64 registers
160enum {
161    UNW_X86_64_RAX =  0,
162    UNW_X86_64_RDX =  1,
163    UNW_X86_64_RCX =  2,
164    UNW_X86_64_RBX =  3,
165    UNW_X86_64_RSI =  4,
166    UNW_X86_64_RDI =  5,
167    UNW_X86_64_RBP =  6,
168    UNW_X86_64_RSP =  7,
169    UNW_X86_64_R8  =  8,
170    UNW_X86_64_R9  =  9,
171    UNW_X86_64_R10 = 10,
172    UNW_X86_64_R11 = 11,
173    UNW_X86_64_R12 = 12,
174    UNW_X86_64_R13 = 13,
175    UNW_X86_64_R14 = 14,
176    UNW_X86_64_R15 = 15
177};
178
179
180// 32-bit ppc register numbers
181enum {
182    UNW_PPC_R0  =  0,
183    UNW_PPC_R1  =  1,
184    UNW_PPC_R2  =  2,
185    UNW_PPC_R3  =  3,
186    UNW_PPC_R4  =  4,
187    UNW_PPC_R5  =  5,
188    UNW_PPC_R6  =  6,
189    UNW_PPC_R7  =  7,
190    UNW_PPC_R8  =  8,
191    UNW_PPC_R9  =  9,
192    UNW_PPC_R10 = 10,
193    UNW_PPC_R11 = 11,
194    UNW_PPC_R12 = 12,
195    UNW_PPC_R13 = 13,
196    UNW_PPC_R14 = 14,
197    UNW_PPC_R15 = 15,
198    UNW_PPC_R16 = 16,
199    UNW_PPC_R17 = 17,
200    UNW_PPC_R18 = 18,
201    UNW_PPC_R19 = 19,
202    UNW_PPC_R20 = 20,
203    UNW_PPC_R21 = 21,
204    UNW_PPC_R22 = 22,
205    UNW_PPC_R23 = 23,
206    UNW_PPC_R24 = 24,
207    UNW_PPC_R25 = 25,
208    UNW_PPC_R26 = 26,
209    UNW_PPC_R27 = 27,
210    UNW_PPC_R28 = 28,
211    UNW_PPC_R29 = 29,
212    UNW_PPC_R30 = 30,
213    UNW_PPC_R31 = 31,
214    UNW_PPC_F0  = 32,
215    UNW_PPC_F1  = 33,
216    UNW_PPC_F2  = 34,
217    UNW_PPC_F3  = 35,
218    UNW_PPC_F4  = 36,
219    UNW_PPC_F5  = 37,
220    UNW_PPC_F6  = 38,
221    UNW_PPC_F7  = 39,
222    UNW_PPC_F8  = 40,
223    UNW_PPC_F9  = 41,
224    UNW_PPC_F10 = 42,
225    UNW_PPC_F11 = 43,
226    UNW_PPC_F12 = 44,
227    UNW_PPC_F13 = 45,
228    UNW_PPC_F14 = 46,
229    UNW_PPC_F15 = 47,
230    UNW_PPC_F16 = 48,
231    UNW_PPC_F17 = 49,
232    UNW_PPC_F18 = 50,
233    UNW_PPC_F19 = 51,
234    UNW_PPC_F20 = 52,
235    UNW_PPC_F21 = 53,
236    UNW_PPC_F22 = 54,
237    UNW_PPC_F23 = 55,
238    UNW_PPC_F24 = 56,
239    UNW_PPC_F25 = 57,
240    UNW_PPC_F26 = 58,
241    UNW_PPC_F27 = 59,
242    UNW_PPC_F28 = 60,
243    UNW_PPC_F29 = 61,
244    UNW_PPC_F30 = 62,
245    UNW_PPC_F31 = 63,
246    UNW_PPC_MQ  = 64,
247    UNW_PPC_LR  = 65,
248    UNW_PPC_CTR = 66,
249    UNW_PPC_AP  = 67,
250	UNW_PPC_CR0 = 68,
251    UNW_PPC_CR1 = 69,
252	UNW_PPC_CR2 = 70,
253	UNW_PPC_CR3 = 71,
254	UNW_PPC_CR4 = 72,
255	UNW_PPC_CR5 = 73,
256	UNW_PPC_CR6 = 74,
257	UNW_PPC_CR7 = 75,
258	UNW_PPC_XER = 76,
259	UNW_PPC_V0  = 77,
260    UNW_PPC_V1  = 78,
261    UNW_PPC_V2  = 79,
262    UNW_PPC_V3  = 80,
263    UNW_PPC_V4  = 81,
264    UNW_PPC_V5  = 82,
265    UNW_PPC_V6  = 83,
266    UNW_PPC_V7  = 84,
267    UNW_PPC_V8  = 85,
268    UNW_PPC_V9  = 86,
269    UNW_PPC_V10 = 87,
270    UNW_PPC_V11 = 88,
271    UNW_PPC_V12 = 89,
272    UNW_PPC_V13 = 90,
273    UNW_PPC_V14 = 91,
274    UNW_PPC_V15 = 92,
275    UNW_PPC_V16 = 93,
276    UNW_PPC_V17 = 94,
277    UNW_PPC_V18 = 95,
278    UNW_PPC_V19 = 96,
279    UNW_PPC_V20 = 97,
280    UNW_PPC_V21 = 98,
281    UNW_PPC_V22 = 99,
282    UNW_PPC_V23 = 100,
283    UNW_PPC_V24 = 101,
284    UNW_PPC_V25 = 102,
285    UNW_PPC_V26 = 103,
286    UNW_PPC_V27 = 104,
287    UNW_PPC_V28 = 105,
288    UNW_PPC_V29 = 106,
289    UNW_PPC_V30 = 107,
290    UNW_PPC_V31 = 108,
291    UNW_PPC_VRSAVE  = 109,
292    UNW_PPC_VSCR    = 110,
293    UNW_PPC_SPE_ACC = 111,
294    UNW_PPC_SPEFSCR = 112
295
296};
297
298
299
300
301#endif
302
303