]>
Commit | Line | Data |
---|---|---|
4bd07ac2 A |
1 | /* |
2 | * Copyright (c) 2015 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
4bd07ac2 A |
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. | |
0a7de745 | 14 | * |
4bd07ac2 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
4bd07ac2 A |
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. | |
0a7de745 | 25 | * |
4bd07ac2 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | #include <mach/mach.h> | |
29 | ||
30 | kern_return_t | |
31 | thread_get_register_pointer_values(thread_t thread, uintptr_t *sp, size_t *length, uintptr_t *values) | |
32 | { | |
0a7de745 A |
33 | if (!length) { |
34 | return KERN_INVALID_ARGUMENT; | |
35 | } | |
36 | if (*length > 0 && values == NULL) { | |
37 | return KERN_INVALID_ARGUMENT; | |
38 | } | |
4bd07ac2 | 39 | |
0a7de745 A |
40 | size_t in_length = *length; |
41 | size_t out_length = 0; | |
4bd07ac2 A |
42 | |
43 | #if defined(__i386__) | |
0a7de745 A |
44 | i386_thread_state_t state = {}; |
45 | thread_state_flavor_t flavor = x86_THREAD_STATE32; | |
46 | mach_msg_type_number_t count = i386_THREAD_STATE_COUNT; | |
4bd07ac2 | 47 | #elif defined(__x86_64__) |
0a7de745 A |
48 | x86_thread_state64_t state = {}; |
49 | thread_state_flavor_t flavor = x86_THREAD_STATE64; | |
50 | mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT; | |
5ba3f43e | 51 | #elif defined(__arm__) |
0a7de745 A |
52 | arm_thread_state_t state = {}; |
53 | thread_state_flavor_t flavor = ARM_THREAD_STATE; | |
54 | mach_msg_type_number_t count = ARM_THREAD_STATE_COUNT; | |
5ba3f43e | 55 | #elif defined(__arm64__) |
0a7de745 A |
56 | arm_thread_state64_t state = {}; |
57 | thread_state_flavor_t flavor = ARM_THREAD_STATE64; | |
58 | mach_msg_type_number_t count = ARM_THREAD_STATE64_COUNT; | |
4bd07ac2 A |
59 | #else |
60 | #error thread_get_register_pointer_values not defined for this architecture | |
61 | #endif | |
62 | ||
0a7de745 A |
63 | kern_return_t ret = thread_get_state(thread, flavor, (thread_state_t)&state, &count); |
64 | if (ret != KERN_SUCCESS) { | |
65 | return ret; | |
66 | } | |
4bd07ac2 | 67 | |
0a7de745 A |
68 | // If the provided pointer value is > PAGE_SIZE, add it to the output array |
69 | // if there's available space. (Values between 0 and PAGE_SIZE are the NULL page | |
70 | // and not valid pointers.) | |
4bd07ac2 A |
71 | #define push_register_value(p) do { \ |
72 | if ((uintptr_t)p > PAGE_SIZE) { \ | |
0a7de745 A |
73 | if (out_length < in_length && values) \ |
74 | values[out_length] = p; \ | |
75 | out_length++; \ | |
4bd07ac2 A |
76 | } } while (0) |
77 | ||
78 | #if defined(__i386__) | |
0a7de745 A |
79 | if (sp) { |
80 | *sp = state.__esp; | |
81 | } | |
4bd07ac2 | 82 | |
0a7de745 A |
83 | push_register_value(state.__eax); |
84 | push_register_value(state.__ebx); | |
85 | push_register_value(state.__ecx); | |
86 | push_register_value(state.__edx); | |
87 | push_register_value(state.__edi); | |
88 | push_register_value(state.__esi); | |
89 | push_register_value(state.__ebp); | |
4bd07ac2 | 90 | #elif defined(__x86_64__) |
0a7de745 A |
91 | if (sp) { |
92 | if (state.__rsp > 128) { | |
93 | *sp = state.__rsp - 128 /* redzone */; | |
94 | } else { | |
95 | *sp = 0; | |
96 | } | |
97 | } | |
4bd07ac2 | 98 | |
0a7de745 A |
99 | push_register_value(state.__rax); |
100 | push_register_value(state.__rbx); | |
101 | push_register_value(state.__rcx); | |
102 | push_register_value(state.__rdx); | |
103 | push_register_value(state.__rdi); | |
104 | push_register_value(state.__rbp); | |
105 | push_register_value(state.__r8); | |
106 | push_register_value(state.__r9); | |
107 | push_register_value(state.__r10); | |
108 | push_register_value(state.__r11); | |
109 | push_register_value(state.__r12); | |
110 | push_register_value(state.__r13); | |
111 | push_register_value(state.__r14); | |
112 | push_register_value(state.__r15); | |
5ba3f43e | 113 | #elif defined(__arm__) |
0a7de745 A |
114 | if (sp) { |
115 | *sp = state.__sp; | |
116 | } | |
5ba3f43e | 117 | |
0a7de745 | 118 | push_register_value(state.__lr); |
5ba3f43e | 119 | |
0a7de745 A |
120 | for (int i = 0; i < 13; i++) { |
121 | push_register_value(state.__r[i]); | |
122 | } | |
5ba3f43e | 123 | #elif defined(__arm64__) |
0a7de745 A |
124 | if (sp) { |
125 | uintptr_t __sp = arm_thread_state64_get_sp(state); | |
126 | if (__sp > 128) { | |
127 | *sp = __sp - 128 /* redzone */; | |
128 | } else { | |
129 | *sp = 0; | |
130 | } | |
131 | } | |
5ba3f43e | 132 | |
0a7de745 | 133 | push_register_value(arm_thread_state64_get_lr(state)); |
5ba3f43e | 134 | |
0a7de745 A |
135 | for (int i = 0; i < 29; i++) { |
136 | push_register_value(state.__x[i]); | |
137 | } | |
4bd07ac2 A |
138 | #else |
139 | #error thread_get_register_pointer_values not defined for this architecture | |
140 | #endif | |
141 | ||
0a7de745 | 142 | *length = out_length; |
4bd07ac2 | 143 | |
0a7de745 A |
144 | if (in_length == 0 || out_length > in_length) { |
145 | return KERN_INSUFFICIENT_BUFFER_SIZE; | |
146 | } | |
4bd07ac2 A |
147 | |
148 | return KERN_SUCCESS; | |
149 | } |