2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <mach/mach.h>
31 thread_get_register_pointer_values(thread_t thread
, uintptr_t *sp
, size_t *length
, uintptr_t *values
)
33 if (!length
) return KERN_INVALID_ARGUMENT
;
34 if (*length
> 0 && values
== NULL
) return KERN_INVALID_ARGUMENT
;
36 size_t in_length
= *length
;
37 size_t out_length
= 0;
40 i386_thread_state_t state
= {};
41 thread_state_flavor_t flavor
= x86_THREAD_STATE32
;
42 mach_msg_type_number_t count
= i386_THREAD_STATE_COUNT
;
43 #elif defined(__x86_64__)
44 x86_thread_state64_t state
= {};
45 thread_state_flavor_t flavor
= x86_THREAD_STATE64
;
46 mach_msg_type_number_t count
= x86_THREAD_STATE64_COUNT
;
47 #elif defined(__arm__)
48 arm_thread_state_t state
= {};
49 thread_state_flavor_t flavor
= ARM_THREAD_STATE
;
50 mach_msg_type_number_t count
= ARM_THREAD_STATE_COUNT
;
51 #elif defined(__arm64__)
52 arm_thread_state64_t state
= {};
53 thread_state_flavor_t flavor
= ARM_THREAD_STATE64
;
54 mach_msg_type_number_t count
= ARM_THREAD_STATE64_COUNT
;
56 #error thread_get_register_pointer_values not defined for this architecture
59 kern_return_t ret
= thread_get_state(thread
, flavor
, (thread_state_t
)&state
, &count
);
60 if (ret
!= KERN_SUCCESS
){
64 // If the provided pointer value is > PAGE_SIZE, add it to the output array
65 // if there's available space. (Values between 0 and PAGE_SIZE are the NULL page
66 // and not valid pointers.)
67 #define push_register_value(p) do { \
68 if ((uintptr_t)p > PAGE_SIZE) { \
69 if (out_length < in_length && values) \
70 values[out_length] = p; \
75 if (sp
) *sp
= state
.__esp
;
77 push_register_value(state
.__eax
);
78 push_register_value(state
.__ebx
);
79 push_register_value(state
.__ecx
);
80 push_register_value(state
.__edx
);
81 push_register_value(state
.__edi
);
82 push_register_value(state
.__esi
);
83 push_register_value(state
.__ebp
);
84 #elif defined(__x86_64__)
86 if (state
.__rsp
> 128)
87 *sp
= state
.__rsp
- 128 /* redzone */;
92 push_register_value(state
.__rax
);
93 push_register_value(state
.__rbx
);
94 push_register_value(state
.__rcx
);
95 push_register_value(state
.__rdx
);
96 push_register_value(state
.__rdi
);
97 push_register_value(state
.__rbp
);
98 push_register_value(state
.__r8
);
99 push_register_value(state
.__r9
);
100 push_register_value(state
.__r10
);
101 push_register_value(state
.__r11
);
102 push_register_value(state
.__r12
);
103 push_register_value(state
.__r13
);
104 push_register_value(state
.__r14
);
105 push_register_value(state
.__r15
);
106 #elif defined(__arm__)
107 if (sp
) *sp
= state
.__sp
;
109 push_register_value(state
.__lr
);
111 for (int i
= 0; i
< 13; i
++){
112 push_register_value(state
.__r
[i
]);
114 #elif defined(__arm64__)
116 uintptr_t __sp
= arm_thread_state64_get_sp(state
);
118 *sp
= __sp
- 128 /* redzone */;
123 push_register_value(arm_thread_state64_get_lr(state
));
125 for (int i
= 0; i
< 29; i
++){
126 push_register_value(state
.__x
[i
]);
129 #error thread_get_register_pointer_values not defined for this architecture
132 *length
= out_length
;
134 if (in_length
== 0 || out_length
> in_length
){
135 return KERN_INSUFFICIENT_BUFFER_SIZE
;