1 /* 2 * Copyright (c) 2004 Marcel Moolenaar 3 * Copyright (c) 2005 David Xu 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <proc_service.h> 32 #include <stddef.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <sys/types.h> 36 #include <sys/linker_set.h> 37 #include <sys/ptrace.h> 38 #include <thread_db.h> 39 #include <unistd.h> 40 41 #include "thread_db_int.h" 42 43 #define TERMINATED 1 44 45 struct td_thragent { 46 TD_THRAGENT_FIELDS; 47 psaddr_t libthr_debug_addr; 48 psaddr_t thread_list_addr; 49 psaddr_t thread_active_threads_addr; 50 psaddr_t thread_keytable_addr; 51 psaddr_t thread_last_event_addr; 52 psaddr_t thread_event_mask_addr; 53 psaddr_t thread_bp_create_addr; 54 psaddr_t thread_bp_death_addr; 55 int thread_off_dtv; 56 int thread_off_tlsindex; 57 int thread_off_attr_flags; 58 int thread_size_key; 59 int thread_off_tcb; 60 int thread_off_linkmap; 61 int thread_off_next; 62 int thread_off_state; 63 int thread_off_tid; 64 int thread_max_keys; 65 int thread_off_key_allocated; 66 int thread_off_key_destructor; 67 int thread_off_report_events; 68 int thread_off_event_mask; 69 int thread_off_event_buf; 70 int thread_state_zoombie; 71 int thread_state_running; 72 }; 73 74 #define P2T(c) ps2td(c) 75 76 static int pt_validate(const td_thrhandle_t *th); 77 78 static int 79 ps2td(int c) 80 { 81 switch (c) { 82 case PS_OK: 83 return TD_OK; 84 case PS_ERR: 85 return TD_ERR; 86 case PS_BADPID: 87 return TD_BADPH; 88 case PS_BADLID: 89 return TD_NOLWP; 90 case PS_BADADDR: 91 return TD_ERR; 92 case PS_NOSYM: 93 return TD_NOLIBTHREAD; 94 case PS_NOFREGS: 95 return TD_NOFPREGS; 96 default: 97 return TD_ERR; 98 } 99 } 100 101 static td_err_e 102 pt_init(void) 103 { 104 return (0); 105 } 106 107 static td_err_e 108 pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta) 109 { 110 #define LOOKUP_SYM(proc, sym, addr) \ 111 ret = ps_pglobal_lookup(proc, NULL, sym, addr); \ 112 if (ret != 0) { \ 113 TDBG("can not find symbol: %s\n", sym); \ 114 ret = TD_NOLIBTHREAD; \ 115 goto error; \ 116 } 117 118 #define LOOKUP_VAL(proc, sym, val) \ 119 ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\ 120 if (ret != 0) { \ 121 TDBG("can not find symbol: %s\n", sym); \ 122 ret = TD_NOLIBTHREAD; \ 123 goto error; \ 124 } \ 125 ret = ps_pread(proc, vaddr, val, sizeof(int)); \ 126 if (ret != 0) { \ 127 TDBG("can not read value of %s\n", sym);\ 128 ret = TD_NOLIBTHREAD; \ 129 goto error; \ 130 } 131 132 td_thragent_t *ta; 133 psaddr_t vaddr; 134 int dbg; 135 int ret; 136 137 TDBG_FUNC(); 138 139 ta = malloc(sizeof(td_thragent_t)); 140 if (ta == NULL) 141 return (TD_MALLOC); 142 143 ta->ph = ph; 144 145 LOOKUP_SYM(ph, "_libthr_debug", &ta->libthr_debug_addr); 146 LOOKUP_SYM(ph, "_thread_list", &ta->thread_list_addr); 147 LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr); 148 LOOKUP_SYM(ph, "_thread_keytable", &ta->thread_keytable_addr); 149 LOOKUP_SYM(ph, "_thread_last_event", &ta->thread_last_event_addr); 150 LOOKUP_SYM(ph, "_thread_event_mask", &ta->thread_event_mask_addr); 151 LOOKUP_SYM(ph, "_thread_bp_create", &ta->thread_bp_create_addr); 152 LOOKUP_SYM(ph, "_thread_bp_death", &ta->thread_bp_death_addr); 153 LOOKUP_VAL(ph, "_thread_off_dtv", &ta->thread_off_dtv); 154 LOOKUP_VAL(ph, "_thread_off_tlsindex", &ta->thread_off_tlsindex); 155 LOOKUP_VAL(ph, "_thread_off_attr_flags", &ta->thread_off_attr_flags); 156 LOOKUP_VAL(ph, "_thread_size_key", &ta->thread_size_key); 157 LOOKUP_VAL(ph, "_thread_off_tcb", &ta->thread_off_tcb); 158 LOOKUP_VAL(ph, "_thread_off_tid", &ta->thread_off_tid); 159 LOOKUP_VAL(ph, "_thread_off_linkmap", &ta->thread_off_linkmap); 160 LOOKUP_VAL(ph, "_thread_off_next", &ta->thread_off_next); 161 LOOKUP_VAL(ph, "_thread_off_state", &ta->thread_off_state); 162 LOOKUP_VAL(ph, "_thread_max_keys", &ta->thread_max_keys); 163 LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated); 164 LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor); 165 LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running); 166 LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie); 167 LOOKUP_VAL(ph, "_thread_off_report_events", &ta->thread_off_report_events); 168 LOOKUP_VAL(ph, "_thread_off_event_mask", &ta->thread_off_event_mask); 169 LOOKUP_VAL(ph, "_thread_off_event_buf", &ta->thread_off_event_buf); 170 dbg = getpid(); 171 /* 172 * If this fails it probably means we're debugging a core file and 173 * can't write to it. 174 */ 175 ps_pwrite(ph, ta->libthr_debug_addr, &dbg, sizeof(int)); 176 *pta = ta; 177 return (0); 178 179 error: 180 free(ta); 181 return (ret); 182 } 183 184 static td_err_e 185 pt_ta_delete(td_thragent_t *ta) 186 { 187 int dbg; 188 189 TDBG_FUNC(); 190 191 dbg = 0; 192 /* 193 * Error returns from this write are not really a problem; 194 * the process doesn't exist any more. 195 */ 196 ps_pwrite(ta->ph, ta->libthr_debug_addr, &dbg, sizeof(int)); 197 free(ta); 198 return (TD_OK); 199 } 200 201 static td_err_e 202 pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th) 203 { 204 psaddr_t pt; 205 int32_t lwp; 206 int ret; 207 208 TDBG_FUNC(); 209 210 if (id == 0) 211 return (TD_NOTHR); 212 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt); 213 if (ret != 0) 214 return (TD_ERR); 215 /* Iterate through thread list to find pthread */ 216 while (pt != 0) { 217 ret = thr_pread_int(ta, pt + ta->thread_off_tid, &lwp); 218 if (ret != 0) 219 return (TD_ERR); 220 if (lwp == id) 221 break; 222 /* get next thread */ 223 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt); 224 if (ret != 0) 225 return (TD_ERR); 226 } 227 if (pt == 0) 228 return (TD_NOTHR); 229 th->th_ta = ta; 230 th->th_tid = id; 231 th->th_thread = pt; 232 return (TD_OK); 233 } 234 235 static td_err_e 236 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th) 237 { 238 return (pt_ta_map_id2thr(ta, lwp, th)); 239 } 240 241 static td_err_e 242 pt_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *callback, 243 void *cbdata_p, td_thr_state_e state __unused, int ti_pri __unused, 244 sigset_t *ti_sigmask_p __unused, unsigned int ti_user_flags __unused) 245 { 246 td_thrhandle_t th; 247 psaddr_t pt; 248 int32_t lwp; 249 int ret; 250 251 TDBG_FUNC(); 252 253 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt); 254 if (ret != 0) 255 return (TD_ERR); 256 while (pt != 0) { 257 ret = thr_pread_int(ta, pt + ta->thread_off_tid, &lwp); 258 if (ret != 0) 259 return (TD_ERR); 260 if (lwp != 0 && lwp != TERMINATED) { 261 th.th_ta = ta; 262 th.th_tid = (thread_t)lwp; 263 th.th_thread = pt; 264 if ((*callback)(&th, cbdata_p)) 265 return (TD_DBERR); 266 } 267 /* get next thread */ 268 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt); 269 if (ret != 0) 270 return (TD_ERR); 271 } 272 return (TD_OK); 273 } 274 275 static td_err_e 276 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg) 277 { 278 void *keytable; 279 void *destructor; 280 int i, ret, allocated; 281 282 TDBG_FUNC(); 283 284 keytable = malloc(ta->thread_max_keys * ta->thread_size_key); 285 if (keytable == NULL) 286 return (TD_MALLOC); 287 ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable, 288 ta->thread_max_keys * ta->thread_size_key); 289 if (ret != 0) { 290 free(keytable); 291 return (P2T(ret)); 292 } 293 for (i = 0; i < ta->thread_max_keys; i++) { 294 allocated = *(int *)(void *)((uintptr_t)keytable + 295 i * ta->thread_size_key + ta->thread_off_key_allocated); 296 destructor = *(void **)(void *)((uintptr_t)keytable + 297 i * ta->thread_size_key + ta->thread_off_key_destructor); 298 if (allocated) { 299 ret = (ki)(i, destructor, arg); 300 if (ret != 0) { 301 free(keytable); 302 return (TD_DBERR); 303 } 304 } 305 } 306 free(keytable); 307 return (TD_OK); 308 } 309 310 static td_err_e 311 pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr) 312 { 313 314 TDBG_FUNC(); 315 316 switch (event) { 317 case TD_CREATE: 318 ptr->type = NOTIFY_BPT; 319 ptr->u.bptaddr = ta->thread_bp_create_addr; 320 return (0); 321 case TD_DEATH: 322 ptr->type = NOTIFY_BPT; 323 ptr->u.bptaddr = ta->thread_bp_death_addr; 324 return (0); 325 default: 326 return (TD_ERR); 327 } 328 } 329 330 static td_err_e 331 pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events) 332 { 333 td_thr_events_t mask; 334 int ret; 335 336 TDBG_FUNC(); 337 ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask, 338 sizeof(mask)); 339 if (ret != 0) 340 return (P2T(ret)); 341 mask |= *events; 342 ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask, 343 sizeof(mask)); 344 return (P2T(ret)); 345 } 346 347 static td_err_e 348 pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events) 349 { 350 td_thr_events_t mask; 351 int ret; 352 353 TDBG_FUNC(); 354 ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask, 355 sizeof(mask)); 356 if (ret != 0) 357 return (P2T(ret)); 358 mask &= ~*events; 359 ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask, 360 sizeof(mask)); 361 return (P2T(ret)); 362 } 363 364 static td_err_e 365 pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg) 366 { 367 static td_thrhandle_t handle; 368 369 psaddr_t pt; 370 td_thr_events_e tmp; 371 int32_t lwp; 372 int ret; 373 374 TDBG_FUNC(); 375 376 ret = thr_pread_ptr(ta, ta->thread_last_event_addr, &pt); 377 if (ret != 0) 378 return (TD_ERR); 379 if (pt == 0) 380 return (TD_NOMSG); 381 /* 382 * Take the event pointer, at the time, libthr only reports event 383 * once a time, so it is not a link list. 384 */ 385 thr_pwrite_ptr(ta, ta->thread_last_event_addr, 0); 386 387 /* Read event info */ 388 ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg)); 389 if (ret != 0) 390 return (P2T(ret)); 391 if (msg->event == 0) 392 return (TD_NOMSG); 393 /* Clear event */ 394 tmp = 0; 395 ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp)); 396 /* Convert event */ 397 pt = msg->th_p; 398 ret = thr_pread_int(ta, pt + ta->thread_off_tid, &lwp); 399 if (ret != 0) 400 return (TD_ERR); 401 handle.th_ta = ta; 402 handle.th_tid = lwp; 403 handle.th_thread = pt; 404 msg->th_p = (uintptr_t)&handle; 405 return (0); 406 } 407 408 static td_err_e 409 pt_dbsuspend(const td_thrhandle_t *th, int suspend) 410 { 411 const td_thragent_t *ta = th->th_ta; 412 int ret; 413 414 TDBG_FUNC(); 415 416 ret = pt_validate(th); 417 if (ret) 418 return (ret); 419 420 if (suspend) 421 ret = ps_lstop(ta->ph, th->th_tid); 422 else 423 ret = ps_lcontinue(ta->ph, th->th_tid); 424 return (P2T(ret)); 425 } 426 427 static td_err_e 428 pt_thr_dbresume(const td_thrhandle_t *th) 429 { 430 TDBG_FUNC(); 431 432 return pt_dbsuspend(th, 0); 433 } 434 435 static td_err_e 436 pt_thr_dbsuspend(const td_thrhandle_t *th) 437 { 438 TDBG_FUNC(); 439 440 return pt_dbsuspend(th, 1); 441 } 442 443 static td_err_e 444 pt_thr_validate(const td_thrhandle_t *th) 445 { 446 td_thrhandle_t temp; 447 int ret; 448 449 TDBG_FUNC(); 450 451 ret = pt_ta_map_id2thr(th->th_ta, th->th_tid, &temp); 452 return (ret); 453 } 454 455 static td_err_e 456 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info) 457 { 458 const td_thragent_t *ta = th->th_ta; 459 struct ptrace_lwpinfo linfo; 460 int state; 461 int ret; 462 463 TDBG_FUNC(); 464 465 bzero(info, sizeof(*info)); 466 ret = pt_validate(th); 467 if (ret) 468 return (ret); 469 ret = thr_pread_int(ta, th->th_thread + ta->thread_off_state, &state); 470 if (ret != 0) 471 return (TD_ERR); 472 ret = thr_pread_int(ta, th->th_thread + ta->thread_off_report_events, 473 &info->ti_traceme); 474 if (ret != 0) 475 return (TD_ERR); 476 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask, 477 &info->ti_events, sizeof(td_thr_events_t)); 478 if (ret != 0) 479 return (P2T(ret)); 480 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb, 481 &info->ti_tls, sizeof(void *)); 482 info->ti_lid = th->th_tid; 483 info->ti_tid = th->th_tid; 484 info->ti_thread = th->th_thread; 485 info->ti_ta_p = th->th_ta; 486 ret = ps_linfo(ta->ph, th->th_tid, &linfo); 487 if (ret == PS_OK) { 488 info->ti_sigmask = linfo.pl_sigmask; 489 info->ti_pending = linfo.pl_siglist; 490 } else 491 return (ret); 492 if (state == ta->thread_state_running) 493 info->ti_state = TD_THR_RUN; 494 else if (state == ta->thread_state_zoombie) 495 info->ti_state = TD_THR_ZOMBIE; 496 else 497 info->ti_state = TD_THR_SLEEP; 498 info->ti_type = TD_THR_USER; 499 return (0); 500 } 501 502 #ifdef __i386__ 503 static td_err_e 504 pt_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave) 505 { 506 const td_thragent_t *ta = th->th_ta; 507 int ret; 508 509 TDBG_FUNC(); 510 511 ret = pt_validate(th); 512 if (ret) 513 return (ret); 514 515 ret = ps_lgetxmmregs(ta->ph, th->th_tid, fxsave); 516 return (P2T(ret)); 517 } 518 #endif 519 520 static td_err_e 521 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs) 522 { 523 const td_thragent_t *ta = th->th_ta; 524 int ret; 525 526 TDBG_FUNC(); 527 528 ret = pt_validate(th); 529 if (ret) 530 return (ret); 531 532 ret = ps_lgetfpregs(ta->ph, th->th_tid, fpregs); 533 return (P2T(ret)); 534 } 535 536 static td_err_e 537 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs) 538 { 539 const td_thragent_t *ta = th->th_ta; 540 int ret; 541 542 TDBG_FUNC(); 543 544 ret = pt_validate(th); 545 if (ret) 546 return (ret); 547 548 ret = ps_lgetregs(ta->ph, th->th_tid, gregs); 549 return (P2T(ret)); 550 } 551 552 #ifdef __i386__ 553 static td_err_e 554 pt_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave) 555 { 556 const td_thragent_t *ta = th->th_ta; 557 int ret; 558 559 TDBG_FUNC(); 560 561 ret = pt_validate(th); 562 if (ret) 563 return (ret); 564 565 ret = ps_lsetxmmregs(ta->ph, th->th_tid, fxsave); 566 return (P2T(ret)); 567 } 568 #endif 569 570 static td_err_e 571 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs) 572 { 573 const td_thragent_t *ta = th->th_ta; 574 int ret; 575 576 TDBG_FUNC(); 577 578 ret = pt_validate(th); 579 if (ret) 580 return (ret); 581 582 ret = ps_lsetfpregs(ta->ph, th->th_tid, fpregs); 583 return (P2T(ret)); 584 } 585 586 static td_err_e 587 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs) 588 { 589 const td_thragent_t *ta = th->th_ta; 590 int ret; 591 592 TDBG_FUNC(); 593 594 ret = pt_validate(th); 595 if (ret) 596 return (ret); 597 598 ret = ps_lsetregs(ta->ph, th->th_tid, gregs); 599 return (P2T(ret)); 600 } 601 602 static td_err_e 603 pt_thr_event_enable(const td_thrhandle_t *th, int en) 604 { 605 const td_thragent_t *ta = th->th_ta; 606 int ret; 607 608 TDBG_FUNC(); 609 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_report_events, 610 &en, sizeof(int)); 611 return (P2T(ret)); 612 } 613 614 static td_err_e 615 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp) 616 { 617 const td_thragent_t *ta = th->th_ta; 618 td_thr_events_t mask; 619 int ret; 620 621 TDBG_FUNC(); 622 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask, 623 &mask, sizeof(mask)); 624 mask |= *setp; 625 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask, 626 &mask, sizeof(mask)); 627 return (P2T(ret)); 628 } 629 630 static td_err_e 631 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp) 632 { 633 const td_thragent_t *ta = th->th_ta; 634 td_thr_events_t mask; 635 int ret; 636 637 TDBG_FUNC(); 638 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask, 639 &mask, sizeof(mask)); 640 mask &= ~*setp; 641 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask, 642 &mask, sizeof(mask)); 643 return (P2T(ret)); 644 } 645 646 static td_err_e 647 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg) 648 { 649 static td_thrhandle_t handle; 650 const td_thragent_t *ta = th->th_ta; 651 psaddr_t pt, pt_temp; 652 int32_t lwp; 653 int ret; 654 td_thr_events_e tmp; 655 656 TDBG_FUNC(); 657 pt = th->th_thread; 658 ret = thr_pread_ptr(ta, ta->thread_last_event_addr, &pt_temp); 659 if (ret != 0) 660 return (TD_ERR); 661 /* Get event */ 662 ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg)); 663 if (ret != 0) 664 return (P2T(ret)); 665 if (msg->event == 0) 666 return (TD_NOMSG); 667 /* 668 * Take the event pointer, at the time, libthr only reports event 669 * once a time, so it is not a link list. 670 */ 671 if (pt == pt_temp) 672 thr_pwrite_ptr(ta, ta->thread_last_event_addr, 0); 673 674 /* Clear event */ 675 tmp = 0; 676 ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp)); 677 /* Convert event */ 678 pt = msg->th_p; 679 ret = thr_pread_int(ta, pt + ta->thread_off_tid, &lwp); 680 if (ret != 0) 681 return (TD_ERR); 682 handle.th_ta = ta; 683 handle.th_tid = lwp; 684 handle.th_thread = pt; 685 msg->th_p = (uintptr_t)&handle; 686 return (0); 687 } 688 689 static td_err_e 690 pt_thr_sstep(const td_thrhandle_t *th, int step __unused) 691 { 692 TDBG_FUNC(); 693 694 return pt_validate(th); 695 } 696 697 static int 698 pt_validate(const td_thrhandle_t *th) 699 { 700 701 if (th->th_tid == 0 || th->th_thread == 0) 702 return (TD_ERR); 703 return (TD_OK); 704 } 705 706 static td_err_e 707 pt_thr_tls_get_addr(const td_thrhandle_t *th, psaddr_t _linkmap, size_t offset, 708 psaddr_t *address) 709 { 710 const td_thragent_t *ta = th->th_ta; 711 psaddr_t dtv_addr, obj_entry, tcb_addr; 712 int tls_index, ret; 713 714 /* linkmap is a member of Obj_Entry */ 715 obj_entry = _linkmap - ta->thread_off_linkmap; 716 717 /* get tlsindex of the object file */ 718 ret = ps_pread(ta->ph, 719 obj_entry + ta->thread_off_tlsindex, 720 &tls_index, sizeof(tls_index)); 721 if (ret != 0) 722 return (P2T(ret)); 723 724 /* get thread tcb */ 725 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb, 726 &tcb_addr, sizeof(tcb_addr)); 727 if (ret != 0) 728 return (P2T(ret)); 729 730 /* get dtv array address */ 731 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv, 732 &dtv_addr, sizeof(dtv_addr)); 733 if (ret != 0) 734 return (P2T(ret)); 735 /* now get the object's tls block base address */ 736 ret = ps_pread(ta->ph, dtv_addr + sizeof(void *) * (tls_index+1), 737 address, sizeof(*address)); 738 if (ret != 0) 739 return (P2T(ret)); 740 741 *address += offset; 742 return (TD_OK); 743 } 744 745 struct ta_ops libthr_db_ops = { 746 .to_init = pt_init, 747 .to_ta_clear_event = pt_ta_clear_event, 748 .to_ta_delete = pt_ta_delete, 749 .to_ta_event_addr = pt_ta_event_addr, 750 .to_ta_event_getmsg = pt_ta_event_getmsg, 751 .to_ta_map_id2thr = pt_ta_map_id2thr, 752 .to_ta_map_lwp2thr = pt_ta_map_lwp2thr, 753 .to_ta_new = pt_ta_new, 754 .to_ta_set_event = pt_ta_set_event, 755 .to_ta_thr_iter = pt_ta_thr_iter, 756 .to_ta_tsd_iter = pt_ta_tsd_iter, 757 .to_thr_clear_event = pt_thr_clear_event, 758 .to_thr_dbresume = pt_thr_dbresume, 759 .to_thr_dbsuspend = pt_thr_dbsuspend, 760 .to_thr_event_enable = pt_thr_event_enable, 761 .to_thr_event_getmsg = pt_thr_event_getmsg, 762 .to_thr_get_info = pt_thr_get_info, 763 .to_thr_getfpregs = pt_thr_getfpregs, 764 .to_thr_getgregs = pt_thr_getgregs, 765 .to_thr_set_event = pt_thr_set_event, 766 .to_thr_setfpregs = pt_thr_setfpregs, 767 .to_thr_setgregs = pt_thr_setgregs, 768 .to_thr_validate = pt_thr_validate, 769 .to_thr_tls_get_addr = pt_thr_tls_get_addr, 770 771 /* FreeBSD specific extensions. */ 772 .to_thr_sstep = pt_thr_sstep, 773 #ifdef __i386__ 774 .to_thr_getxmmregs = pt_thr_getxmmregs, 775 .to_thr_setxmmregs = pt_thr_setxmmregs, 776 #endif 777 }; 778 779 DATA_SET(__ta_ops, libthr_db_ops); 780