| @@ -1,530 +1,519 @@ | | | @@ -1,530 +1,519 @@ |
1 | /* $NetBSD: pthread_dbg.c,v 1.43 2015/05/29 07:37:32 manu Exp $ */ | | 1 | /* $NetBSD: pthread_dbg.c,v 1.44 2016/01/23 14:02:21 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2002 Wasabi Systems, Inc. | | 4 | * Copyright (c) 2002 Wasabi Systems, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Written by Nathan J. Williams for Wasabi Systems, Inc. | | 7 | * Written by Nathan J. Williams for Wasabi Systems, Inc. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the | | 15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. | | 16 | * documentation and/or other materials provided with the distribution. |
17 | * 3. All advertising materials mentioning features or use of this software | | 17 | * 3. All advertising materials mentioning features or use of this software |
18 | * must display the following acknowledgement: | | 18 | * must display the following acknowledgement: |
19 | * This product includes software developed for the NetBSD Project by | | 19 | * This product includes software developed for the NetBSD Project by |
20 | * Wasabi Systems, Inc. | | 20 | * Wasabi Systems, Inc. |
21 | * 4. The name of Wasabi Systems, Inc. may not be used to endorse | | 21 | * 4. The name of Wasabi Systems, Inc. may not be used to endorse |
22 | * or promote products derived from this software without specific | | 22 | * or promote products derived from this software without specific |
23 | * prior written permission. | | 23 | * prior written permission. |
24 | * | | 24 | * |
25 | * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND | | 25 | * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND |
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
27 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 27 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC | | 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC |
29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
35 | * POSSIBILITY OF SUCH DAMAGE. | | 35 | * POSSIBILITY OF SUCH DAMAGE. |
36 | */ | | 36 | */ |
37 | | | 37 | |
38 | #include <sys/cdefs.h> | | 38 | #include <sys/cdefs.h> |
39 | __RCSID("$NetBSD: pthread_dbg.c,v 1.43 2015/05/29 07:37:32 manu Exp $"); | | 39 | __RCSID("$NetBSD: pthread_dbg.c,v 1.44 2016/01/23 14:02:21 christos Exp $"); |
40 | | | 40 | |
41 | #define __EXPOSE_STACK 1 | | 41 | #define __EXPOSE_STACK 1 |
42 | | | 42 | |
43 | #include <sys/param.h> | | 43 | #include <sys/param.h> |
44 | #include <sys/types.h> | | 44 | #include <sys/types.h> |
45 | #include <sys/lock.h> | | 45 | #include <sys/lock.h> |
46 | | | 46 | |
47 | #include <stddef.h> | | 47 | #include <stddef.h> |
48 | #include <stdlib.h> | | 48 | #include <stdlib.h> |
49 | #include <string.h> | | 49 | #include <string.h> |
50 | #include <errno.h> | | 50 | #include <errno.h> |
51 | #include <unistd.h> | | 51 | #include <unistd.h> |
52 | #include <lwp.h> | | 52 | #include <lwp.h> |
53 | | | 53 | |
54 | #include <machine/reg.h> | | 54 | #include <machine/reg.h> |
55 | | | 55 | |
56 | #include <pthread.h> | | 56 | #include <pthread.h> |
57 | #include <pthread_int.h> | | 57 | #include <pthread_int.h> |
58 | #include <pthread_dbg.h> | | 58 | #include <pthread_dbg.h> |
59 | #include <pthread_dbg_int.h> | | 59 | #include <pthread_dbg_int.h> |
60 | | | 60 | |
61 | #define PT_STACKMASK (proc->stackmask) | | 61 | #define PT_STACKMASK (proc->stackmask) |
| | | 62 | #define OFFSET(thread, field) \ |
| | | 63 | (((char *)(thread)->addr) + offsetof(struct __pthread_st, field)) |
62 | | | 64 | |
63 | /* Compensate for debuggers that want a zero ID to be a sentinel */ | | 65 | /* Compensate for debuggers that want a zero ID to be a sentinel */ |
64 | #define TN_OFFSET 1 | | 66 | #define TN_OFFSET 1 |
65 | | | 67 | |
66 | static int td__getthread(td_proc_t *proc, caddr_t addr, td_thread_t **threadp); | | 68 | static int td__getthread(td_proc_t *proc, caddr_t addr, td_thread_t **threadp); |
67 | | | 69 | |
68 | int | | 70 | int |
69 | td_open(struct td_proc_callbacks_t *cb, void *arg, td_proc_t **procp) | | 71 | td_open(struct td_proc_callbacks_t *cb, void *arg, td_proc_t **procp) |
70 | { | | 72 | { |
71 | td_proc_t *proc; | | 73 | td_proc_t *proc; |
72 | caddr_t addr; | | 74 | caddr_t addr; |
73 | int dbg; | | 75 | int dbg; |
74 | int val; | | 76 | int val; |
75 | | | 77 | |
76 | proc = malloc(sizeof(*proc)); | | 78 | proc = malloc(sizeof(*proc)); |
77 | if (proc == NULL) | | 79 | if (proc == NULL) |
78 | return TD_ERR_NOMEM; | | 80 | return TD_ERR_NOMEM; |
79 | proc->cb = cb; | | 81 | proc->cb = cb; |
80 | proc->arg = arg; | | 82 | proc->arg = arg; |
81 | | | 83 | |
82 | val = LOOKUP(proc, "pthread__dbg", &addr); | | 84 | val = LOOKUP(proc, "pthread__dbg", &addr); |
83 | if (val != 0) { | | 85 | if (val != 0) { |
84 | if (val == TD_ERR_NOSYM) | | 86 | if (val == TD_ERR_NOSYM) |
85 | val = TD_ERR_NOLIB; | | 87 | val = TD_ERR_NOLIB; |
86 | goto error; | | 88 | goto error; |
87 | } | | 89 | } |
88 | proc->dbgaddr = addr; | | 90 | proc->dbgaddr = addr; |
89 | | | 91 | |
90 | val = LOOKUP(proc, "pthread__allqueue", &addr); | | 92 | val = LOOKUP(proc, "pthread__allqueue", &addr); |
91 | if (val != 0) | | 93 | if (val != 0) |
92 | goto error; | | 94 | goto error; |
93 | proc->allqaddr = addr; | | 95 | proc->allqaddr = addr; |
94 | | | 96 | |
95 | val = LOOKUP(proc, "pthread__tsd_list", &addr); | | 97 | val = LOOKUP(proc, "pthread__tsd_list", &addr); |
96 | if (val != 0) | | 98 | if (val != 0) |
97 | goto error; | | 99 | goto error; |
98 | proc->tsdlistaddr = addr; | | 100 | proc->tsdlistaddr = addr; |
99 | | | 101 | |
100 | val = LOOKUP(proc, "pthread__tsd_destructors", &addr); | | 102 | val = LOOKUP(proc, "pthread__tsd_destructors", &addr); |
101 | if (val != 0) | | 103 | if (val != 0) |
102 | goto error; | | 104 | goto error; |
103 | proc->tsddestaddr = addr; | | 105 | proc->tsddestaddr = addr; |
104 | | | 106 | |
105 | val = READ(proc, proc->dbgaddr, &dbg, sizeof(int)); | | 107 | val = READ(proc, proc->dbgaddr, &dbg, sizeof(int)); |
106 | if (val != 0) | | 108 | if (val != 0) |
107 | goto error; | | 109 | goto error; |
108 | | | 110 | |
109 | if (dbg != 0) { | | 111 | if (dbg != 0) { |
110 | /* Another instance of libpthread_dbg is already attached. */ | | 112 | /* Another instance of libpthread_dbg is already attached. */ |
111 | val = TD_ERR_INUSE; | | 113 | val = TD_ERR_INUSE; |
112 | goto error; | | 114 | goto error; |
113 | } | | 115 | } |
114 | | | 116 | |
115 | val = LOOKUP(proc, "pthread__stacksize_lg", &addr); | | 117 | val = LOOKUP(proc, "pthread__stacksize_lg", &addr); |
116 | if (val == 0) | | 118 | if (val == 0) |
117 | proc->stacksizeaddr = addr; | | 119 | proc->stacksizeaddr = addr; |
118 | else | | 120 | else |
119 | proc->stacksizeaddr = NULL; | | 121 | proc->stacksizeaddr = NULL; |
120 | proc->stacksizelg = -1; | | 122 | proc->stacksizelg = -1; |
121 | proc->stacksize = 0; | | 123 | proc->stacksize = 0; |
122 | proc->stackmask = 0; | | 124 | proc->stackmask = 0; |
123 | | | 125 | |
124 | proc->regbuf = NULL; | | 126 | proc->regbuf = NULL; |
125 | proc->fpregbuf = NULL; | | 127 | proc->fpregbuf = NULL; |
126 | | | 128 | |
127 | dbg = getpid(); | | 129 | dbg = getpid(); |
128 | /* | | 130 | /* |
129 | * If this fails it probably means we're debugging a core file and | | 131 | * If this fails it probably means we're debugging a core file and |
130 | * can't write to it. | | 132 | * can't write to it. |
131 | * If it's something else we'll lose the next time we hit WRITE, | | 133 | * If it's something else we'll lose the next time we hit WRITE, |
132 | * but not before, and that's OK. | | 134 | * but not before, and that's OK. |
133 | */ | | 135 | */ |
134 | WRITE(proc, proc->dbgaddr, &dbg, sizeof(int)); | | 136 | WRITE(proc, proc->dbgaddr, &dbg, sizeof(int)); |
135 | | | 137 | |
136 | PTQ_INIT(&proc->threads); | | 138 | PTQ_INIT(&proc->threads); |
137 | | | 139 | |
138 | *procp = proc; | | 140 | *procp = proc; |
139 | | | 141 | |
140 | return 0; | | 142 | return 0; |
141 | | | 143 | |
142 | error: | | 144 | error: |
143 | free(proc); | | 145 | free(proc); |
144 | return val; | | 146 | return val; |
145 | } | | 147 | } |
146 | | | 148 | |
147 | int | | 149 | int |
148 | td_close(td_proc_t *proc) | | 150 | td_close(td_proc_t *proc) |
149 | { | | 151 | { |
150 | int dbg; | | 152 | int dbg; |
151 | td_thread_t *t, *next; | | 153 | td_thread_t *t, *next; |
152 | | | 154 | |
153 | dbg = 0; | | 155 | dbg = 0; |
154 | /* | | 156 | /* |
155 | * Error returns from this write are mot really a problem; | | 157 | * Error returns from this write are mot really a problem; |
156 | * the process doesn't exist any more. | | 158 | * the process doesn't exist any more. |
157 | */ | | 159 | */ |
158 | WRITE(proc, proc->dbgaddr, &dbg, sizeof(int)); | | 160 | WRITE(proc, proc->dbgaddr, &dbg, sizeof(int)); |
159 | | | 161 | |
160 | /* Deallocate the list of thread structures */ | | 162 | /* Deallocate the list of thread structures */ |
161 | for (t = PTQ_FIRST(&proc->threads); t; t = next) { | | 163 | for (t = PTQ_FIRST(&proc->threads); t; t = next) { |
162 | next = PTQ_NEXT(t, list); | | 164 | next = PTQ_NEXT(t, list); |
163 | PTQ_REMOVE(&proc->threads, t, list); | | 165 | PTQ_REMOVE(&proc->threads, t, list); |
164 | free(t); | | 166 | free(t); |
165 | } | | 167 | } |
166 | if (proc->regbuf != NULL) { | | 168 | if (proc->regbuf != NULL) { |
167 | free(proc->regbuf); | | 169 | free(proc->regbuf); |
168 | free(proc->fpregbuf); | | 170 | free(proc->fpregbuf); |
169 | } | | 171 | } |
170 | | | 172 | |
171 | free(proc); | | 173 | free(proc); |
172 | return 0; | | 174 | return 0; |
173 | } | | 175 | } |
174 | | | 176 | |
175 | | | 177 | |
176 | int | | 178 | int |
177 | td_thr_iter(td_proc_t *proc, int (*call)(td_thread_t *, void *), void *callarg) | | 179 | td_thr_iter(td_proc_t *proc, int (*call)(td_thread_t *, void *), void *callarg) |
178 | { | | 180 | { |
179 | int val; | | 181 | int val; |
180 | caddr_t next; | | 182 | caddr_t next; |
181 | pthread_queue_t allq; | | 183 | pthread_queue_t allq; |
182 | td_thread_t *thread; | | 184 | td_thread_t *thread; |
183 | | | 185 | |
184 | val = READ(proc, proc->allqaddr, &allq, sizeof(allq)); | | 186 | val = READ(proc, proc->allqaddr, &allq, sizeof(allq)); |
185 | if (val != 0) | | 187 | if (val != 0) |
186 | return val; | | 188 | return val; |
187 | | | 189 | |
188 | next = (void *)allq.ptqh_first; | | 190 | next = (void *)allq.ptqh_first; |
189 | while (next != NULL) { | | 191 | while (next != NULL) { |
190 | val = td__getthread(proc, next, &thread); | | 192 | val = td__getthread(proc, next, &thread); |
191 | if (val != 0) | | 193 | if (val != 0) |
192 | return val; | | 194 | return val; |
193 | val = (*call)(thread, callarg); | | 195 | val = (*call)(thread, callarg); |
194 | if (val != 0) | | 196 | if (val != 0) |
195 | return 0; | | 197 | return 0; |
196 | | | 198 | |
197 | val = READ(proc, | | 199 | val = READ(proc, |
198 | next + offsetof(struct __pthread_st, pt_allq.ptqe_next), | | 200 | next + offsetof(struct __pthread_st, pt_allq.ptqe_next), |
199 | &next, sizeof(next)); | | 201 | &next, sizeof(next)); |
200 | if (val != 0) | | 202 | if (val != 0) |
201 | return val; | | 203 | return val; |
202 | } | | 204 | } |
203 | return 0; | | 205 | return 0; |
204 | } | | 206 | } |
205 | | | 207 | |
206 | int | | 208 | int |
207 | td_thr_info(td_thread_t *thread, td_thread_info_t *info) | | 209 | td_thr_info(td_thread_t *thread, td_thread_info_t *info) |
208 | { | | 210 | { |
209 | int tmp, val; | | 211 | int tmp, val; |
210 | | | 212 | |
211 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); | | 213 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); |
212 | if (val != 0) | | 214 | if (val != 0) |
213 | return val; | | 215 | return val; |
214 | | | 216 | |
215 | if (tmp != PT_MAGIC) | | 217 | if (tmp != PT_MAGIC) |
216 | return TD_ERR_BADTHREAD; | | 218 | return TD_ERR_BADTHREAD; |
217 | | | 219 | |
218 | info->thread_addr = thread->addr; | | 220 | info->thread_addr = thread->addr; |
219 | if ((val = READ(thread->proc, | | 221 | if ((val = READ(thread->proc, |
220 | thread->addr + offsetof(struct __pthread_st, pt_state), | | 222 | OFFSET(thread, pt_state), &tmp, sizeof(tmp))) != 0) |
221 | &tmp, sizeof(tmp))) != 0) | | | |
222 | return val; | | 223 | return val; |
223 | switch (tmp) { | | 224 | switch (tmp) { |
224 | case PT_STATE_RUNNING: | | 225 | case PT_STATE_RUNNING: |
225 | info->thread_state = TD_STATE_RUNNING; | | 226 | info->thread_state = TD_STATE_RUNNING; |
226 | break; | | 227 | break; |
227 | #ifdef XXXLWP | | 228 | #ifdef XXXLWP |
228 | case PT_STATE_SUSPENDED: | | 229 | case PT_STATE_SUSPENDED: |
229 | info->thread_state = TD_STATE_SUSPENDED; | | 230 | info->thread_state = TD_STATE_SUSPENDED; |
230 | break; | | 231 | break; |
231 | #endif | | 232 | #endif |
232 | case PT_STATE_ZOMBIE: | | 233 | case PT_STATE_ZOMBIE: |
233 | info->thread_state = TD_STATE_ZOMBIE; | | 234 | info->thread_state = TD_STATE_ZOMBIE; |
234 | break; | | 235 | break; |
235 | default: | | 236 | default: |
236 | info->thread_state = TD_STATE_UNKNOWN; | | 237 | info->thread_state = TD_STATE_UNKNOWN; |
237 | } | | 238 | } |
238 | | | 239 | |
239 | info->thread_type = TD_TYPE_USER; | | 240 | info->thread_type = TD_TYPE_USER; |
240 | | | 241 | |
241 | if ((val = READ(thread->proc, | | 242 | if ((val = READ(thread->proc, OFFSET(thread, pt_stack), |
242 | thread->addr + offsetof(struct __pthread_st, pt_stack), | | | |
243 | &info->thread_stack, sizeof(stack_t))) != 0) | | 243 | &info->thread_stack, sizeof(stack_t))) != 0) |
244 | return val; | | 244 | return val; |
245 | | | 245 | |
246 | if ((val = READ(thread->proc, | | 246 | if ((val = READ(thread->proc, OFFSET(thread, pt_errno), |
247 | thread->addr + offsetof(struct __pthread_st, pt_errno), | | | |
248 | &info->thread_errno, sizeof(info->thread_errno))) != 0) | | 247 | &info->thread_errno, sizeof(info->thread_errno))) != 0) |
249 | return val; | | 248 | return val; |
250 | | | 249 | |
251 | if ((val = READ(thread->proc, | | 250 | if ((val = READ(thread->proc, OFFSET(thread, pt_lid), |
252 | thread->addr + offsetof(struct __pthread_st, pt_lid), | | | |
253 | &info->thread_id, sizeof(info->thread_id))) != 0) | | 251 | &info->thread_id, sizeof(info->thread_id))) != 0) |
254 | return val; | | 252 | return val; |
255 | | | 253 | |
256 | info->thread_id += TN_OFFSET; | | 254 | info->thread_id += TN_OFFSET; |
257 | | | 255 | |
258 | return 0; | | 256 | return 0; |
259 | } | | 257 | } |
260 | | | 258 | |
261 | int | | 259 | int |
262 | td_thr_getname(td_thread_t *thread, char *name, int len) | | 260 | td_thr_getname(td_thread_t *thread, char *name, int len) |
263 | { | | 261 | { |
264 | int val, tmp; | | 262 | int val, tmp; |
265 | caddr_t nameaddr; | | 263 | caddr_t nameaddr; |
266 | | | 264 | |
267 | | | 265 | |
268 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); | | 266 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); |
269 | if (val != 0) | | 267 | if (val != 0) |
270 | return val; | | 268 | return val; |
271 | | | 269 | |
272 | if (tmp != PT_MAGIC) | | 270 | if (tmp != PT_MAGIC) |
273 | return TD_ERR_BADTHREAD; | | 271 | return TD_ERR_BADTHREAD; |
274 | | | 272 | |
275 | if ((val = READ(thread->proc, | | 273 | if ((val = READ(thread->proc, OFFSET(thread, pt_name), |
276 | thread->addr + offsetof(struct __pthread_st, pt_name), | | | |
277 | &nameaddr, sizeof(nameaddr))) != 0) | | 274 | &nameaddr, sizeof(nameaddr))) != 0) |
278 | return val; | | 275 | return val; |
279 | | | 276 | |
280 | if (nameaddr == 0) | | 277 | if (nameaddr == 0) |
281 | name[0] = '\0'; | | 278 | name[0] = '\0'; |
282 | else if ((val = READ(thread->proc, nameaddr, | | 279 | else if ((val = READ(thread->proc, nameaddr, |
283 | name, (size_t)MIN(PTHREAD_MAX_NAMELEN_NP, len))) != 0) | | 280 | name, (size_t)MIN(PTHREAD_MAX_NAMELEN_NP, len))) != 0) |
284 | return val; | | 281 | return val; |
285 | | | 282 | |
286 | return 0; | | 283 | return 0; |
287 | } | | 284 | } |
288 | | | 285 | |
289 | int | | 286 | int |
290 | td_thr_getregs(td_thread_t *thread, int regset, void *buf) | | 287 | td_thr_getregs(td_thread_t *thread, int regset, void *buf) |
291 | { | | 288 | { |
292 | int tmp, val; | | 289 | int tmp, val; |
293 | | | 290 | |
294 | if ((val = READ(thread->proc, | | 291 | if ((val = READ(thread->proc, OFFSET(thread, pt_state), |
295 | thread->addr + offsetof(struct __pthread_st, pt_state), | | 292 | &tmp, sizeof(tmp))) != 0) |
296 | &tmp, sizeof(tmp))) != 0) | | | |
297 | return val; | | 293 | return val; |
298 | | | 294 | |
299 | switch (tmp) { | | 295 | switch (tmp) { |
300 | case PT_STATE_RUNNING: | | 296 | case PT_STATE_RUNNING: |
301 | /* | | 297 | /* |
302 | * The register state of the thread is live in the | | 298 | * The register state of the thread is live in the |
303 | * inferior process's register state. | | 299 | * inferior process's register state. |
304 | */ | | 300 | */ |
305 | val = GETREGS(thread->proc, regset, thread->lwp, buf); | | 301 | val = GETREGS(thread->proc, regset, thread->lwp, buf); |
306 | if (val != 0) | | 302 | if (val != 0) |
307 | return val; | | 303 | return val; |
308 | break; | | 304 | break; |
309 | case PT_STATE_ZOMBIE: | | 305 | case PT_STATE_ZOMBIE: |
310 | default: | | 306 | default: |
311 | return TD_ERR_BADTHREAD; | | 307 | return TD_ERR_BADTHREAD; |
312 | } | | 308 | } |
313 | | | 309 | |
314 | return 0; | | 310 | return 0; |
315 | } | | 311 | } |
316 | | | 312 | |
317 | int | | 313 | int |
318 | td_thr_setregs(td_thread_t *thread, int regset, void *buf) | | 314 | td_thr_setregs(td_thread_t *thread, int regset, void *buf) |
319 | { | | 315 | { |
320 | int val, tmp; | | 316 | int val, tmp; |
321 | | | 317 | |
322 | if ((val = READ(thread->proc, | | 318 | if ((val = READ(thread->proc, OFFSET(thread, pt_state), |
323 | thread->addr + offsetof(struct __pthread_st, pt_state), | | 319 | &tmp, sizeof(tmp))) != 0) |
324 | &tmp, sizeof(tmp))) != 0) | | | |
325 | return val; | | 320 | return val; |
326 | | | 321 | |
327 | switch (tmp) { | | 322 | switch (tmp) { |
328 | case PT_STATE_RUNNING: | | 323 | case PT_STATE_RUNNING: |
329 | /* | | 324 | /* |
330 | * The register state of the thread is live in the | | 325 | * The register state of the thread is live in the |
331 | * inferior process's register state. | | 326 | * inferior process's register state. |
332 | */ | | 327 | */ |
333 | val = SETREGS(thread->proc, regset, thread->lwp, buf); | | 328 | val = SETREGS(thread->proc, regset, thread->lwp, buf); |
334 | if (val != 0) | | 329 | if (val != 0) |
335 | return val; | | 330 | return val; |
336 | break; | | 331 | break; |
337 | case PT_STATE_ZOMBIE: | | 332 | case PT_STATE_ZOMBIE: |
338 | default: | | 333 | default: |
339 | return TD_ERR_BADTHREAD; | | 334 | return TD_ERR_BADTHREAD; |
340 | } | | 335 | } |
341 | | | 336 | |
342 | return 0; | | 337 | return 0; |
343 | } | | 338 | } |
344 | | | 339 | |
345 | int | | 340 | int |
346 | td_map_pth2thr(td_proc_t *proc, pthread_t thread, td_thread_t **threadp) | | 341 | td_map_pth2thr(td_proc_t *proc, pthread_t thread, td_thread_t **threadp) |
347 | { | | 342 | { |
348 | int magic, val; | | 343 | int magic, val; |
349 | | | 344 | |
350 | val = READ(proc, (void *)thread, &magic, sizeof(magic)); | | 345 | val = READ(proc, (void *)thread, &magic, sizeof(magic)); |
351 | if (val != 0) | | 346 | if (val != 0) |
352 | return val; | | 347 | return val; |
353 | | | 348 | |
354 | if (magic != PT_MAGIC) | | 349 | if (magic != PT_MAGIC) |
355 | return TD_ERR_NOOBJ; | | 350 | return TD_ERR_NOOBJ; |
356 | | | 351 | |
357 | val = td__getthread(proc, (void *)thread, threadp); | | 352 | val = td__getthread(proc, (void *)thread, threadp); |
358 | if (val != 0) | | 353 | if (val != 0) |
359 | return val; | | 354 | return val; |
360 | | | 355 | |
361 | return 0; | | 356 | return 0; |
362 | } | | 357 | } |
363 | | | 358 | |
364 | int | | 359 | int |
365 | td_map_id2thr(td_proc_t *proc, int threadid, td_thread_t **threadp) | | 360 | td_map_id2thr(td_proc_t *proc, int threadid, td_thread_t **threadp) |
366 | { | | 361 | { |
367 | int val, num; | | 362 | int val, num; |
368 | caddr_t next; | | 363 | caddr_t next; |
369 | pthread_queue_t allq; | | 364 | pthread_queue_t allq; |
370 | td_thread_t *thread; | | 365 | td_thread_t *thread; |
371 | | | 366 | |
372 | | | 367 | |
373 | val = READ(proc, proc->allqaddr, &allq, sizeof(allq)); | | 368 | val = READ(proc, proc->allqaddr, &allq, sizeof(allq)); |
374 | if (val != 0) | | 369 | if (val != 0) |
375 | return val; | | 370 | return val; |
376 | | | 371 | |
377 | /* Correct for offset */ | | 372 | /* Correct for offset */ |
378 | threadid -= TN_OFFSET; | | 373 | threadid -= TN_OFFSET; |
379 | next = (void *)allq.ptqh_first; | | 374 | next = (void *)allq.ptqh_first; |
380 | while (next != NULL) { | | 375 | while (next != NULL) { |
381 | val = READ(proc, | | 376 | val = READ(proc, next + offsetof(struct __pthread_st, pt_lid), |
382 | next + offsetof(struct __pthread_st, pt_lid), | | | |
383 | &num, sizeof(num)); | | 377 | &num, sizeof(num)); |
384 | | | 378 | |
385 | if (num == threadid) | | 379 | if (num == threadid) |
386 | break; | | 380 | break; |
387 | | | 381 | |
388 | val = READ(proc, | | 382 | val = READ(proc, |
389 | next + offsetof(struct __pthread_st, pt_allq.ptqe_next), | | 383 | next + offsetof(struct __pthread_st, pt_allq.ptqe_next), |
390 | &next, sizeof(next)); | | 384 | &next, sizeof(next)); |
391 | if (val != 0) | | 385 | if (val != 0) |
392 | return val; | | 386 | return val; |
393 | } | | 387 | } |
394 | | | 388 | |
395 | if (next == 0) { | | 389 | if (next == 0) { |
396 | /* A matching thread was not found. */ | | 390 | /* A matching thread was not found. */ |
397 | return TD_ERR_NOOBJ; | | 391 | return TD_ERR_NOOBJ; |
398 | } | | 392 | } |
399 | | | 393 | |
400 | val = td__getthread(proc, next, &thread); | | 394 | val = td__getthread(proc, next, &thread); |
401 | if (val != 0) | | 395 | if (val != 0) |
402 | return val; | | 396 | return val; |
403 | *threadp = thread; | | 397 | *threadp = thread; |
404 | | | 398 | |
405 | return 0; | | 399 | return 0; |
406 | } | | 400 | } |
407 | | | 401 | |
408 | int | | 402 | int |
409 | td_tsd_iter(td_proc_t *proc, | | 403 | td_tsd_iter(td_proc_t *proc, |
410 | int (*call)(pthread_key_t, void (*)(void *), void *), void *arg) | | 404 | int (*call)(pthread_key_t, void (*)(void *), void *), void *arg) |
411 | { | | 405 | { |
412 | #ifdef notyet | | 406 | #ifdef notyet |
413 | int val; | | 407 | int val; |
414 | int i; | | 408 | int i; |
415 | void *allocated; | | 409 | void *allocated; |
416 | void (*destructor)(void *); | | 410 | void (*destructor)(void *); |
417 | | | 411 | |
418 | for (i = 0; i < pthread_keys_max; i++) { | | 412 | for (i = 0; i < pthread_keys_max; i++) { |
419 | val = READ(proc, proc->tsdlistaddr + i * sizeof(allocated), | | 413 | val = READ(proc, proc->tsdlistaddr + i * sizeof(allocated), |
420 | &allocated, sizeof(allocated)); | | 414 | &allocated, sizeof(allocated)); |
421 | if (val != 0) | | 415 | if (val != 0) |
422 | return val; | | 416 | return val; |
423 | | | 417 | |
424 | if ((uintptr_t)allocated) { | | 418 | if ((uintptr_t)allocated) { |
425 | val = READ(proc, proc->tsddestaddr + | | 419 | val = READ(proc, proc->tsddestaddr + |
426 | i * sizeof(destructor), | | 420 | i * sizeof(destructor), |
427 | &destructor, sizeof(destructor)); | | 421 | &destructor, sizeof(destructor)); |
428 | if (val != 0) | | 422 | if (val != 0) |
429 | return val; | | 423 | return val; |
430 | | | 424 | |
431 | val = (call)(i, destructor, arg); | | 425 | val = (call)(i, destructor, arg); |
432 | if (val != 0) | | 426 | if (val != 0) |
433 | return val; | | 427 | return val; |
434 | } | | 428 | } |
435 | } | | 429 | } |
436 | #else | | 430 | #else |
437 | abort(); | | 431 | abort(); |
438 | #endif | | 432 | #endif |
439 | | | 433 | |
440 | return 0; | | 434 | return 0; |
441 | } | | 435 | } |
442 | | | 436 | |
443 | /* Suspend a thread from running */ | | 437 | /* Suspend a thread from running */ |
444 | int | | 438 | int |
445 | td_thr_suspend(td_thread_t *thread) | | 439 | td_thr_suspend(td_thread_t *thread) |
446 | { | | 440 | { |
447 | int tmp, val; | | 441 | int tmp, val; |
448 | | | 442 | |
449 | /* validate the thread */ | | 443 | /* validate the thread */ |
450 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); | | 444 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); |
451 | if (val != 0) | | 445 | if (val != 0) |
452 | return val; | | 446 | return val; |
453 | if (tmp != PT_MAGIC) | | 447 | if (tmp != PT_MAGIC) |
454 | return TD_ERR_BADTHREAD; | | 448 | return TD_ERR_BADTHREAD; |
455 | | | 449 | |
456 | val = READ(thread->proc, thread->addr + | | 450 | val = READ(thread->proc, OFFSET(thread, pt_lid), &tmp, sizeof(tmp)); |
457 | offsetof(struct __pthread_st, pt_lid), | | | |
458 | &tmp, sizeof(tmp)); | | | |
459 | if (val != 0) | | 451 | if (val != 0) |
460 | return val; | | 452 | return val; |
461 | | | 453 | |
462 | /* XXXLWP continue the sucker */; | | 454 | /* XXXLWP continue the sucker */; |
463 | | | 455 | |
464 | return 0; | | 456 | return 0; |
465 | } | | 457 | } |
466 | | | 458 | |
467 | /* Restore a suspended thread to its previous state */ | | 459 | /* Restore a suspended thread to its previous state */ |
468 | int | | 460 | int |
469 | td_thr_resume(td_thread_t *thread) | | 461 | td_thr_resume(td_thread_t *thread) |
470 | { | | 462 | { |
471 | int tmp, val; | | 463 | int tmp, val; |
472 | | | 464 | |
473 | /* validate the thread */ | | 465 | /* validate the thread */ |
474 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); | | 466 | val = READ(thread->proc, thread->addr, &tmp, sizeof(tmp)); |
475 | if (val != 0) | | 467 | if (val != 0) |
476 | return val; | | 468 | return val; |
477 | if (tmp != PT_MAGIC) | | 469 | if (tmp != PT_MAGIC) |
478 | return TD_ERR_BADTHREAD; | | 470 | return TD_ERR_BADTHREAD; |
479 | | | 471 | |
480 | val = READ(thread->proc, thread->addr + | | 472 | val = READ(thread->proc, OFFSET(thread, pt_lid), &tmp, sizeof(tmp)); |
481 | offsetof(struct __pthread_st, pt_lid), | | | |
482 | &tmp, sizeof(tmp)); | | | |
483 | if (val != 0) | | 473 | if (val != 0) |
484 | return val; | | 474 | return val; |
485 | | | 475 | |
486 | /* XXXLWP continue the sucker */; | | 476 | /* XXXLWP continue the sucker */; |
487 | | | 477 | |
488 | return 0; | | 478 | return 0; |
489 | } | | 479 | } |
490 | | | 480 | |
491 | static int | | 481 | static int |
492 | td__getthread(td_proc_t *proc, caddr_t addr, td_thread_t **threadp) | | 482 | td__getthread(td_proc_t *proc, caddr_t addr, td_thread_t **threadp) |
493 | { | | 483 | { |
494 | td_thread_t *thread; | | 484 | td_thread_t *thread; |
495 | | | 485 | |
496 | /* | | 486 | /* |
497 | * Check if we've allocated a descriptor for this thread. | | 487 | * Check if we've allocated a descriptor for this thread. |
498 | * Sadly, this makes iterating over a set of threads O(N^2) | | 488 | * Sadly, this makes iterating over a set of threads O(N^2) |
499 | * in the number of threads. More sophisticated data structures | | 489 | * in the number of threads. More sophisticated data structures |
500 | * can wait. | | 490 | * can wait. |
501 | */ | | 491 | */ |
502 | PTQ_FOREACH(thread, &proc->threads, list) { | | 492 | PTQ_FOREACH(thread, &proc->threads, list) { |
503 | if (thread->addr == addr) | | 493 | if (thread->addr == addr) |
504 | break; | | 494 | break; |
505 | } | | 495 | } |
506 | if (thread == NULL) { | | 496 | if (thread == NULL) { |
507 | thread = malloc(sizeof(*thread)); | | 497 | thread = malloc(sizeof(*thread)); |
508 | if (thread == NULL) | | 498 | if (thread == NULL) |
509 | return TD_ERR_NOMEM; | | 499 | return TD_ERR_NOMEM; |
510 | thread->proc = proc; | | 500 | thread->proc = proc; |
511 | thread->addr = addr; | | 501 | thread->addr = addr; |
512 | thread->lwp = 0; | | 502 | thread->lwp = 0; |
513 | PTQ_INSERT_HEAD(&proc->threads, thread, list); | | 503 | PTQ_INSERT_HEAD(&proc->threads, thread, list); |
514 | } | | 504 | } |
515 | | | 505 | |
516 | *threadp = thread; | | 506 | *threadp = thread; |
517 | return 0; | | 507 | return 0; |
518 | } | | 508 | } |
519 | | | 509 | |
520 | int | | 510 | int |
521 | td_thr_tsd(td_thread_t *thread, pthread_key_t key, void **value) | | 511 | td_thr_tsd(td_thread_t *thread, pthread_key_t key, void **value) |
522 | { | | 512 | { |
523 | int val; | | 513 | int val; |
524 | | | 514 | |
525 | val = READ(thread->proc, thread->addr + | | 515 | val = READ(thread->proc, OFFSET(thread, pt_specific) + |
526 | offsetof(struct __pthread_st, pt_specific) + | | | |
527 | key * sizeof(void *), value, sizeof(*value)); | | 516 | key * sizeof(void *), value, sizeof(*value)); |
528 | | | 517 | |
529 | return val; | | 518 | return val; |
530 | } | | 519 | } |