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
;
48 #error thread_get_register_pointer_values not defined for this architecture
51 kern_return_t ret
= thread_get_state(thread
, flavor
, (thread_state_t
)&state
, &count
);
52 if (ret
!= KERN_SUCCESS
){
56 // If the provided pointer value is > PAGE_SIZE, add it to the output array
57 // if there's available space. (Values between 0 and PAGE_SIZE are the NULL page
58 // and not valid pointers.)
59 #define push_register_value(p) do { \
60 if ((uintptr_t)p > PAGE_SIZE) { \
61 if (out_length < in_length && values) \
62 values[out_length] = p; \
67 if (sp
) *sp
= state
.__esp
;
69 push_register_value(state
.__eip
);
71 push_register_value(state
.__eax
);
72 push_register_value(state
.__ebx
);
73 push_register_value(state
.__ecx
);
74 push_register_value(state
.__edx
);
75 push_register_value(state
.__edi
);
76 push_register_value(state
.__esi
);
77 push_register_value(state
.__ebp
);
78 #elif defined(__x86_64__)
79 if (sp
) *sp
= state
.__rsp
- 128 /* redzone */;
81 push_register_value(state
.__rip
);
83 push_register_value(state
.__rax
);
84 push_register_value(state
.__rbx
);
85 push_register_value(state
.__rcx
);
86 push_register_value(state
.__rdx
);
87 push_register_value(state
.__rdi
);
88 push_register_value(state
.__rbp
);
89 push_register_value(state
.__r8
);
90 push_register_value(state
.__r9
);
91 push_register_value(state
.__r10
);
92 push_register_value(state
.__r11
);
93 push_register_value(state
.__r12
);
94 push_register_value(state
.__r13
);
95 push_register_value(state
.__r14
);
96 push_register_value(state
.__r15
);
98 #error thread_get_register_pointer_values not defined for this architecture
101 *length
= out_length
;
103 if (in_length
== 0 || out_length
> in_length
){
104 return KERN_INSUFFICIENT_BUFFER_SIZE
;