| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
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 |
| @@ -26,49 +26,51 @@ | | | @@ -26,49 +26,51 @@ |
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; |
| @@ -206,132 +208,125 @@ td_thr_iter(td_proc_t *proc, int (*call) | | | @@ -206,132 +208,125 @@ td_thr_iter(td_proc_t *proc, int (*call) |
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: |
| @@ -368,28 +363,27 @@ td_map_id2thr(td_proc_t *proc, int threa | | | @@ -368,28 +363,27 @@ td_map_id2thr(td_proc_t *proc, int threa |
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) { |
| @@ -443,53 +437,49 @@ td_tsd_iter(td_proc_t *proc, | | | @@ -443,53 +437,49 @@ td_tsd_iter(td_proc_t *proc, |
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 | |
| @@ -512,19 +502,18 @@ td__getthread(td_proc_t *proc, caddr_t a | | | @@ -512,19 +502,18 @@ td__getthread(td_proc_t *proc, caddr_t a |
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 | } |