libpthread_db.c revision 133802
1/* 2 * Copyright (c) 2004 David Xu <davidxu@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/lib/libthread_db/libpthread_db.c 133802 2004-08-16 03:30:16Z davidxu $"); 29 30#include <stddef.h> 31#include <stdlib.h> 32#include <string.h> 33#include <unistd.h> 34#include <pthread.h> 35#include <sys/types.h> 36#include <sys/kse.h> 37#include <sys/ptrace.h> 38#include <proc_service.h> 39#include <thread_db.h> 40 41#include "libpthread_db.h" 42 43#define P2T(c) ps2td(c) 44 45static void pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp); 46static int pt_validate(const td_thrhandle_t *th); 47 48static int 49ps2td(int c) 50{ 51 switch (c) { 52 case PS_OK: 53 return TD_OK; 54 case PS_ERR: 55 return TD_ERR; 56 case PS_BADPID: 57 return TD_BADPH; 58 case PS_BADLID: 59 return TD_NOLWP; 60 case PS_BADADDR: 61 return TD_ERR; 62 case PS_NOSYM: 63 return TD_NOLIBTHREAD; 64 case PS_NOFREGS: 65 return TD_NOFPREGS; 66 default: 67 return TD_ERR; 68 } 69} 70 71static long 72pt_map_thread(const td_thragent_t *const_ta, psaddr_t pt, int type) 73{ 74 td_thragent_t *ta = __DECONST(td_thragent_t *, const_ta); 75 struct pt_map *new; 76 int i, first = -1; 77 78 /* leave zero out */ 79 for (i = 1; i < ta->map_len; ++i) { 80 if (ta->map[i].type == PT_NONE) { 81 if (first == -1) 82 first = i; 83 } else if (ta->map[i].type == type && ta->map[i].thr == pt) { 84 return (i); 85 } 86 } 87 88 if (first == -1) { 89 if (ta->map_len == 0) { 90 ta->map = calloc(20, sizeof(struct pt_map)); 91 if (ta->map == NULL) 92 return (-1); 93 ta->map_len = 20; 94 first = 1; 95 } else { 96 new = realloc(ta->map, 97 sizeof(struct pt_map) * ta->map_len * 2); 98 if (new == NULL) 99 return (-1); 100 memset(new + ta->map_len, '\0', sizeof(struct pt_map) * 101 ta->map_len); 102 first = ta->map_len; 103 ta->map = new; 104 ta->map_len *= 2; 105 } 106 } 107 108 ta->map[first].type = type; 109 ta->map[first].thr = pt; 110 return (first); 111} 112 113static td_err_e 114pt_init(void) 115{ 116 pt_md_init(); 117 return (0); 118} 119 120static td_err_e 121pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta) 122{ 123#define LOOKUP_SYM(proc, sym, addr) \ 124 ret = ps_pglobal_lookup(proc, NULL, sym, addr); \ 125 if (ret != 0) { \ 126 TDBG("can not find symbol: %s\n", sym); \ 127 ret = TD_NOLIBTHREAD; \ 128 goto error; \ 129 } 130 131#define LOOKUP_VAL(proc, sym, val) \ 132 ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\ 133 if (ret != 0) { \ 134 TDBG("can not find symbol: %s\n", sym); \ 135 ret = TD_NOLIBTHREAD; \ 136 goto error; \ 137 } \ 138 ret = ps_pread(proc, vaddr, val, sizeof(int)); \ 139 if (ret != 0) { \ 140 TDBG("can not read value of %s\n", sym);\ 141 ret = TD_NOLIBTHREAD; \ 142 goto error; \ 143 } 144 145 td_thragent_t *ta; 146 psaddr_t vaddr; 147 int dbg; 148 int ret; 149 150 TDBG_FUNC(); 151 152 ta = malloc(sizeof(td_thragent_t)); 153 if (ta == NULL) 154 return (TD_MALLOC); 155 156 ta->ph = ph; 157 ta->thread_activated = 0; 158 ta->map = NULL; 159 ta->map_len = 0; 160 161 LOOKUP_SYM(ph, "_libkse_debug", &ta->libkse_debug_addr); 162 LOOKUP_SYM(ph, "_thread_list", &ta->thread_list_addr); 163 LOOKUP_SYM(ph, "_thread_activated", &ta->thread_activated_addr); 164 LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr); 165 LOOKUP_SYM(ph, "_thread_keytable", &ta->thread_keytable_addr); 166 LOOKUP_VAL(ph, "_thread_off_dtv", &ta->thread_off_dtv); 167 LOOKUP_VAL(ph, "_thread_off_kse_locklevel", &ta->thread_off_kse_locklevel); 168 LOOKUP_VAL(ph, "_thread_off_kse", &ta->thread_off_kse); 169 LOOKUP_VAL(ph, "_thread_off_tlsindex", &ta->thread_off_tlsindex); 170 LOOKUP_VAL(ph, "_thread_off_attr_flags", &ta->thread_off_attr_flags); 171 LOOKUP_VAL(ph, "_thread_size_key", &ta->thread_size_key); 172 LOOKUP_VAL(ph, "_thread_off_tcb", &ta->thread_off_tcb); 173 LOOKUP_VAL(ph, "_thread_off_linkmap", &ta->thread_off_linkmap); 174 LOOKUP_VAL(ph, "_thread_off_tmbx", &ta->thread_off_tmbx); 175 LOOKUP_VAL(ph, "_thread_off_thr_locklevel", &ta->thread_off_thr_locklevel); 176 LOOKUP_VAL(ph, "_thread_off_next", &ta->thread_off_next); 177 LOOKUP_VAL(ph, "_thread_off_state", &ta->thread_off_state); 178 LOOKUP_VAL(ph, "_thread_max_keys", &ta->thread_max_keys); 179 LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated); 180 LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor); 181 LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running); 182 LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie); 183 dbg = getpid(); 184 /* 185 * If this fails it probably means we're debugging a core file and 186 * can't write to it. 187 */ 188 ps_pwrite(ph, ta->libkse_debug_addr, &dbg, sizeof(int)); 189 *pta = ta; 190 return (0); 191 192error: 193 free(ta); 194 return (ret); 195} 196 197static td_err_e 198pt_ta_delete(td_thragent_t *ta) 199{ 200 int dbg; 201 202 TDBG_FUNC(); 203 204 dbg = 0; 205 /* 206 * Error returns from this write are not really a problem; 207 * the process doesn't exist any more. 208 */ 209 ps_pwrite(ta->ph, ta->libkse_debug_addr, &dbg, sizeof(int)); 210 if (ta->map) 211 free(ta->map); 212 free(ta); 213 return (TD_OK); 214} 215 216static td_err_e 217pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th) 218{ 219 prgregset_t gregs; 220 TAILQ_HEAD(, pthread) thread_list; 221 psaddr_t pt, tcb_addr; 222 lwpid_t lwp; 223 int ret; 224 225 TDBG_FUNC(); 226 227 if (id < 0 || id >= ta->map_len || ta->map[id].type == PT_NONE) 228 return (TD_NOTHR); 229 ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list, 230 sizeof(thread_list)); 231 if (ret != 0) 232 return (P2T(ret)); 233 pt = (psaddr_t)thread_list.tqh_first; 234 if (ta->map[id].type == PT_LWP) { 235 /* 236 * if we are referencing a lwp, make sure it was not already 237 * mapped to user thread. 238 */ 239 while (pt != 0) { 240 ret = ps_pread(ta->ph, 241 pt + ta->thread_off_tcb, 242 &tcb_addr, sizeof(tcb_addr)); 243 if (ret != 0) 244 return (P2T(ret)); 245 ret = ps_pread(ta->ph, 246 tcb_addr + ta->thread_off_tmbx + 247 offsetof(struct kse_thr_mailbox, tm_lwp), 248 &lwp, sizeof(lwp)); 249 if (ret != 0) 250 return (P2T(ret)); 251 /* 252 * If the lwp was already mapped to userland thread, 253 * we shouldn't reference it directly in future. 254 */ 255 if (lwp == ta->map[id].lwp) { 256 ta->map[id].type = PT_NONE; 257 return (TD_NOTHR); 258 } 259 /* get next thread */ 260 ret = ps_pread(ta->ph, 261 pt + ta->thread_off_next, 262 &pt, sizeof(pt)); 263 if (ret != 0) 264 return (P2T(ret)); 265 } 266 /* check lwp */ 267 ret = ptrace(PT_GETREGS, ta->map[id].lwp, (caddr_t)&gregs, 0); 268 if (ret != 0) { 269 /* no longer exists */ 270 ta->map[id].type = PT_NONE; 271 return (TD_NOTHR); 272 } 273 } else { 274 while (pt != 0 && ta->map[id].thr != pt) { 275 ret = ps_pread(ta->ph, 276 pt + ta->thread_off_tcb, 277 &tcb_addr, sizeof(tcb_addr)); 278 if (ret != 0) 279 return (P2T(ret)); 280 /* get next thread */ 281 ret = ps_pread(ta->ph, 282 pt + ta->thread_off_next, 283 &pt, sizeof(pt)); 284 if (ret != 0) 285 return (P2T(ret)); 286 } 287 288 if (pt == 0) { 289 /* no longer exists */ 290 ta->map[id].type = PT_NONE; 291 return (TD_NOTHR); 292 } 293 } 294 th->th_ta = ta; 295 th->th_tid = id; 296 return (TD_OK); 297} 298 299static td_err_e 300pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th) 301{ 302 TAILQ_HEAD(, pthread) thread_list; 303 psaddr_t pt, ptr; 304 lwpid_t tmp_lwp; 305 int ret; 306 307 TDBG_FUNC(); 308 309 ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list, 310 sizeof(thread_list)); 311 if (ret != 0) 312 return (P2T(ret)); 313 pt = (psaddr_t)thread_list.tqh_first; 314 while (pt != 0) { 315 ret = ps_pread(ta->ph, pt + ta->thread_off_tcb, 316 &ptr, sizeof(ptr)); 317 if (ret != 0) 318 return (P2T(ret)); 319 ptr += ta->thread_off_tmbx + 320 offsetof(struct kse_thr_mailbox, tm_lwp); 321 ret = ps_pread(ta->ph, ptr, &tmp_lwp, sizeof(lwpid_t)); 322 if (ret != 0) 323 return (P2T(ret)); 324 if (tmp_lwp == lwp) { 325 th->th_ta = ta; 326 th->th_tid = pt_map_thread(ta, pt, PT_USER); 327 if (th->th_tid == -1) 328 return (TD_MALLOC); 329 pt_unmap_lwp(ta, lwp); 330 return (TD_OK); 331 } 332 333 /* get next thread */ 334 ret = ps_pread(ta->ph, 335 pt + ta->thread_off_next, 336 &pt, sizeof(pt)); 337 if (ret != 0) 338 return (P2T(ret)); 339 } 340 341 return (TD_NOTHR); 342} 343 344static td_err_e 345pt_ta_thr_iter(const td_thragent_t *ta, 346 td_thr_iter_f *callback, void *cbdata_p, 347 td_thr_state_e state, int ti_pri, 348 sigset_t *ti_sigmask_p, 349 unsigned int ti_user_flags) 350{ 351 TAILQ_HEAD(, pthread) thread_list; 352 td_thrhandle_t th; 353 psaddr_t pt; 354 ps_err_e pserr; 355 int activated; 356 357 TDBG_FUNC(); 358 359 pserr = ps_pread(ta->ph, ta->thread_activated_addr, &activated, 360 sizeof(int)); 361 if (pserr != PS_OK) 362 return (P2T(pserr)); 363 if (!activated) 364 return (TD_OK); 365 366 pserr = ps_pread(ta->ph, ta->thread_list_addr, &thread_list, 367 sizeof(thread_list)); 368 if (pserr != 0) 369 return (P2T(pserr)); 370 pt = (psaddr_t)thread_list.tqh_first; 371 while (pt != 0) { 372 th.th_ta = ta; 373 th.th_tid = pt_map_thread(ta, pt, PT_USER); 374 /* should we unmap lwp here ? */ 375 if (th.th_tid == -1) 376 return (TD_MALLOC); 377 if ((*callback)(&th, cbdata_p)) 378 return (TD_DBERR); 379 /* get next thread */ 380 pserr = ps_pread(ta->ph, 381 pt + ta->thread_off_next, &pt, 382 sizeof(pt)); 383 if (pserr != PS_OK) 384 return (P2T(pserr)); 385 } 386 return (TD_OK); 387} 388 389static td_err_e 390pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg) 391{ 392 char *keytable; 393 void *destructor; 394 int i, ret, allocated; 395 396 TDBG_FUNC(); 397 398 keytable = malloc(ta->thread_max_keys * ta->thread_size_key); 399 if (keytable == NULL) 400 return (TD_MALLOC); 401 ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable, 402 ta->thread_max_keys * ta->thread_size_key); 403 if (ret != 0) 404 return (P2T(ret)); 405 for (i = 0; i < ta->thread_max_keys; i++) { 406 allocated = *(int *)(keytable + i * ta->thread_size_key + 407 ta->thread_off_key_allocated); 408 destructor = *(void **)(keytable + i * ta->thread_size_key + 409 ta->thread_off_key_destructor); 410 if (allocated) { 411 ret = (ki)(i, destructor, arg); 412 if (ret != 0) { 413 free(keytable); 414 return (TD_DBERR); 415 } 416 } 417 } 418 free(keytable); 419 return (TD_OK); 420} 421 422static td_err_e 423pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr) 424{ 425 TDBG_FUNC(); 426 return (TD_NOEVENT); 427} 428 429static td_err_e 430pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events) 431{ 432 TDBG_FUNC(); 433 return (TD_ERR); 434} 435 436static td_err_e 437pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events) 438{ 439 TDBG_FUNC(); 440 return (TD_ERR); 441} 442 443static td_err_e 444pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg) 445{ 446 TDBG_FUNC(); 447 return (TD_NOMSG); 448} 449 450static td_err_e 451pt_dbsuspend(const td_thrhandle_t *th, int suspend) 452{ 453 td_thragent_t *ta = (td_thragent_t *)th->th_ta; 454 psaddr_t tcb_addr, tmbx_addr, ptr; 455 lwpid_t lwp; 456 uint32_t dflags; 457 int attrflags, locklevel, ret; 458 459 TDBG_FUNC(); 460 461 ret = pt_validate(th); 462 if (ret) 463 return (ret); 464 465 if (ta->map[th->th_tid].type == PT_LWP) { 466 if (suspend) 467 ret = ps_lstop(ta->ph, ta->map[th->th_tid].lwp); 468 else 469 ret = ps_lcontinue(ta->ph, ta->map[th->th_tid].lwp); 470 return (P2T(ret)); 471 } 472 473 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + 474 ta->thread_off_attr_flags, 475 &attrflags, sizeof(attrflags)); 476 if (ret != 0) 477 return (P2T(ret)); 478 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + 479 ta->thread_off_tcb, 480 &tcb_addr, sizeof(tcb_addr)); 481 if (ret != 0) 482 return (P2T(ret)); 483 tmbx_addr = tcb_addr + ta->thread_off_tmbx; 484 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp); 485 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t)); 486 if (ret != 0) 487 return (P2T(ret)); 488 489 if (lwp != 0) { 490 /* don't suspend signal thread */ 491 if (attrflags & 0x200) 492 return (0); 493 if (attrflags & PTHREAD_SCOPE_SYSTEM) { 494 /* 495 * don't suspend system scope thread if it is holding 496 * some low level locks 497 */ 498 ptr = ta->map[th->th_tid].thr + ta->thread_off_kse; 499 ret = ps_pread(ta->ph, ptr, &ptr, sizeof(ptr)); 500 if (ret != 0) 501 return (P2T(ret)); 502 ret = ps_pread(ta->ph, ptr + ta->thread_off_kse_locklevel, 503 &locklevel, sizeof(int)); 504 if (ret != 0) 505 return (P2T(ret)); 506 if (locklevel <= 0) { 507 ptr = ta->map[th->th_tid].thr + 508 ta->thread_off_thr_locklevel; 509 ret = ps_pread(ta->ph, ptr, &locklevel, 510 sizeof(int)); 511 if (ret != 0) 512 return (P2T(ret)); 513 } 514 if (suspend) { 515 if (locklevel <= 0) 516 ret = ps_lstop(ta->ph, lwp); 517 } else { 518 ret = ps_lcontinue(ta->ph, lwp); 519 } 520 if (ret != 0) 521 return (P2T(ret)); 522 /* FALLTHROUGH */ 523 } else { 524 struct ptrace_lwpinfo pl; 525 526 if (ptrace(PT_LWPINFO, lwp, (caddr_t) &pl, sizeof(pl))) 527 return (TD_ERR); 528 if (suspend) { 529 if (!(pl.pl_flags & PL_FLAG_BOUND)) 530 ret = ps_lstop(ta->ph, lwp); 531 } else { 532 ret = ps_lcontinue(ta->ph, lwp); 533 } 534 if (ret != 0) 535 return (P2T(ret)); 536 /* FALLTHROUGH */ 537 } 538 } 539 /* read tm_dflags */ 540 ret = ps_pread(ta->ph, 541 tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags), 542 &dflags, sizeof(dflags)); 543 if (ret != 0) 544 return (P2T(ret)); 545 if (suspend) 546 dflags |= TMDF_SUSPEND; 547 else 548 dflags &= ~TMDF_SUSPEND; 549 ret = ps_pwrite(ta->ph, 550 tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags), 551 &dflags, sizeof(dflags)); 552 return (P2T(ret)); 553} 554 555static td_err_e 556pt_thr_dbresume(const td_thrhandle_t *th) 557{ 558 TDBG_FUNC(); 559 560 return pt_dbsuspend(th, 0); 561} 562 563static td_err_e 564pt_thr_dbsuspend(const td_thrhandle_t *th) 565{ 566 TDBG_FUNC(); 567 568 return pt_dbsuspend(th, 1); 569} 570 571static td_err_e 572pt_thr_validate(const td_thrhandle_t *th) 573{ 574 td_thrhandle_t temp; 575 int ret; 576 577 TDBG_FUNC(); 578 579 ret = pt_ta_map_id2thr(th->th_ta, th->th_tid, 580 &temp); 581 return (ret); 582} 583 584static td_err_e 585pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info) 586{ 587 const td_thragent_t *ta = th->th_ta; 588 psaddr_t tcb_addr; 589 uint32_t dflags; 590 int state; 591 int ret; 592 593 TDBG_FUNC(); 594 595 ret = pt_validate(th); 596 if (ret) 597 return (ret); 598 599 memset(info, 0, sizeof(*info)); 600 if (ta->map[th->th_tid].type == PT_LWP) { 601 info->ti_type = TD_THR_SYSTEM; 602 info->ti_lid = ta->map[th->th_tid].lwp; 603 info->ti_tid = th->th_tid; 604 info->ti_state = TD_THR_RUN; 605 info->ti_type = TD_THR_SYSTEM; 606 return (TD_OK); 607 } 608 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb, 609 &tcb_addr, sizeof(tcb_addr)); 610 if (ret != 0) 611 return (P2T(ret)); 612 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_state, 613 &state, sizeof(state)); 614 ret = ps_pread(ta->ph, 615 tcb_addr + ta->thread_off_tmbx + 616 offsetof(struct kse_thr_mailbox, tm_lwp), 617 &info->ti_lid, sizeof(lwpid_t)); 618 if (ret != 0) 619 return (P2T(ret)); 620 ret = ps_pread(ta->ph, 621 tcb_addr + ta->thread_off_tmbx + 622 offsetof(struct kse_thr_mailbox, tm_dflags), 623 &dflags, sizeof(dflags)); 624 if (ret != 0) 625 return (P2T(ret)); 626 info->ti_ta_p = th->th_ta; 627 info->ti_tid = th->th_tid; 628 if (state == ta->thread_state_running) 629 info->ti_state = TD_THR_RUN; 630 else if (state == ta->thread_state_zoombie) 631 info->ti_state = TD_THR_ZOMBIE; 632 else 633 info->ti_state = TD_THR_SLEEP; 634 info->ti_db_suspended = ((dflags & TMDF_SUSPEND) != 0); 635 info->ti_type = TD_THR_USER; 636 return (0); 637} 638 639static td_err_e 640pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs) 641{ 642 const td_thragent_t *ta = th->th_ta; 643 struct kse_thr_mailbox tmbx; 644 psaddr_t tcb_addr, tmbx_addr, ptr; 645 lwpid_t lwp; 646 int ret; 647 648 TDBG_FUNC(); 649 650 ret = pt_validate(th); 651 if (ret) 652 return (ret); 653 654 if (ta->map[th->th_tid].type == PT_LWP) { 655 ret = ps_lgetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs); 656 return (P2T(ret)); 657 } 658 659 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb, 660 &tcb_addr, sizeof(tcb_addr)); 661 if (ret != 0) 662 return (P2T(ret)); 663 tmbx_addr = tcb_addr + ta->thread_off_tmbx; 664 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp); 665 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t)); 666 if (ret != 0) 667 return (P2T(ret)); 668 if (lwp != 0) { 669 ret = ps_lgetfpregs(ta->ph, lwp, fpregs); 670 return (P2T(ret)); 671 } 672 673 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx)); 674 if (ret != 0) 675 return (P2T(ret)); 676 pt_ucontext_to_fpreg(&tmbx.tm_context, fpregs); 677 return (0); 678} 679 680static td_err_e 681pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs) 682{ 683 const td_thragent_t *ta = th->th_ta; 684 struct kse_thr_mailbox tmbx; 685 psaddr_t tcb_addr, tmbx_addr, ptr; 686 lwpid_t lwp; 687 int ret; 688 689 TDBG_FUNC(); 690 691 ret = pt_validate(th); 692 if (ret) 693 return (ret); 694 695 if (ta->map[th->th_tid].type == PT_LWP) { 696 ret = ps_lgetregs(ta->ph, 697 ta->map[th->th_tid].lwp, gregs); 698 return (P2T(ret)); 699 } 700 701 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb, 702 &tcb_addr, sizeof(tcb_addr)); 703 if (ret != 0) 704 return (P2T(ret)); 705 tmbx_addr = tcb_addr + ta->thread_off_tmbx; 706 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp); 707 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t)); 708 if (ret != 0) 709 return (P2T(ret)); 710 if (lwp != 0) { 711 ret = ps_lgetregs(ta->ph, lwp, gregs); 712 return (P2T(ret)); 713 } 714 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx)); 715 if (ret != 0) 716 return (P2T(ret)); 717 pt_ucontext_to_reg(&tmbx.tm_context, gregs); 718 return (0); 719} 720 721static td_err_e 722pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs) 723{ 724 const td_thragent_t *ta = th->th_ta; 725 struct kse_thr_mailbox tmbx; 726 psaddr_t tcb_addr, tmbx_addr, ptr; 727 lwpid_t lwp; 728 int ret; 729 730 TDBG_FUNC(); 731 732 ret = pt_validate(th); 733 if (ret) 734 return (ret); 735 736 if (ta->map[th->th_tid].type == PT_LWP) { 737 ret = ps_lsetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs); 738 return (P2T(ret)); 739 } 740 741 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + 742 ta->thread_off_tcb, 743 &tcb_addr, sizeof(tcb_addr)); 744 if (ret != 0) 745 return (P2T(ret)); 746 tmbx_addr = tcb_addr + ta->thread_off_tmbx; 747 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp); 748 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t)); 749 if (ret != 0) 750 return (P2T(ret)); 751 if (lwp != 0) { 752 ret = ps_lsetfpregs(ta->ph, lwp, fpregs); 753 return (P2T(ret)); 754 } 755 /* 756 * Read a copy of context, this makes sure that registers 757 * not covered by structure reg won't be clobbered 758 */ 759 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx)); 760 if (ret != 0) 761 return (P2T(ret)); 762 763 pt_fpreg_to_ucontext(fpregs, &tmbx.tm_context); 764 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx)); 765 return (P2T(ret)); 766} 767 768static td_err_e 769pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs) 770{ 771 const td_thragent_t *ta = th->th_ta; 772 struct kse_thr_mailbox tmbx; 773 psaddr_t tcb_addr, tmbx_addr, ptr; 774 lwpid_t lwp; 775 int ret; 776 777 TDBG_FUNC(); 778 779 ret = pt_validate(th); 780 if (ret) 781 return (ret); 782 783 if (ta->map[th->th_tid].type == PT_LWP) { 784 ret = ps_lsetregs(ta->ph, ta->map[th->th_tid].lwp, gregs); 785 return (P2T(ret)); 786 } 787 788 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + 789 ta->thread_off_tcb, 790 &tcb_addr, sizeof(tcb_addr)); 791 if (ret != 0) 792 return (P2T(ret)); 793 tmbx_addr = tcb_addr + ta->thread_off_tmbx; 794 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp); 795 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t)); 796 if (ret != 0) 797 return (P2T(ret)); 798 if (lwp != 0) { 799 ret = ps_lsetregs(ta->ph, lwp, gregs); 800 return (P2T(ret)); 801 } 802 803 /* 804 * Read a copy of context, make sure that registers 805 * not covered by structure reg won't be clobbered 806 */ 807 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx)); 808 if (ret != 0) 809 return (P2T(ret)); 810 pt_reg_to_ucontext(gregs, &tmbx.tm_context); 811 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx)); 812 return (P2T(ret)); 813} 814 815static td_err_e 816pt_thr_event_enable(const td_thrhandle_t *th, int en) 817{ 818 TDBG_FUNC(); 819 return (TD_ERR); 820} 821 822static td_err_e 823pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp) 824{ 825 TDBG_FUNC(); 826 return (TD_ERR); 827} 828 829static td_err_e 830pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp) 831{ 832 TDBG_FUNC(); 833 return (TD_ERR); 834} 835 836static td_err_e 837pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg) 838{ 839 TDBG_FUNC(); 840 return (TD_NOMSG); 841} 842 843static td_err_e 844pt_thr_sstep(const td_thrhandle_t *th, int step) 845{ 846 const td_thragent_t *ta = th->th_ta; 847 struct kse_thr_mailbox tmbx; 848 struct reg regs; 849 psaddr_t tcb_addr, tmbx_addr; 850 uint32_t dflags; 851 lwpid_t lwp; 852 int ret; 853 854 TDBG_FUNC(); 855 856 ret = pt_validate(th); 857 if (ret) 858 return (ret); 859 860 if (ta->map[th->th_tid].type == PT_LWP) 861 return (TD_BADTH); 862 863 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + 864 ta->thread_off_tcb, 865 &tcb_addr, sizeof(tcb_addr)); 866 if (ret != 0) 867 return (P2T(ret)); 868 869 /* Clear or set single step flag in thread mailbox */ 870 ret = ps_pread(ta->ph, 871 tcb_addr + ta->thread_off_tmbx + 872 offsetof(struct kse_thr_mailbox, tm_dflags), 873 &dflags, sizeof(uint32_t)); 874 if (ret != 0) 875 return (P2T(ret)); 876 if (step != 0) 877 dflags |= TMDF_SSTEP; 878 else 879 dflags &= ~TMDF_SSTEP; 880 ret = ps_pwrite(ta->ph, 881 tcb_addr + ta->thread_off_tmbx + 882 offsetof(struct kse_thr_mailbox, tm_dflags), 883 &dflags, sizeof(uint32_t)); 884 if (ret != 0) 885 return (P2T(ret)); 886 /* Get lwp */ 887 ret = ps_pread(ta->ph, 888 tcb_addr + ta->thread_off_tmbx + 889 offsetof(struct kse_thr_mailbox, tm_lwp), 890 &lwp, sizeof(lwpid_t)); 891 if (ret != 0) 892 return (P2T(ret)); 893 if (lwp != 0) 894 return (0); 895 896 tmbx_addr = tcb_addr + ta->thread_off_tmbx; 897 /* 898 * context is in userland, some architectures store 899 * single step status in registers, we should change 900 * these registers. 901 */ 902 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx)); 903 if (ret == 0) { 904 pt_ucontext_to_reg(&tmbx.tm_context, ®s); 905 /* only write out if it is really changed. */ 906 if (pt_reg_sstep(®s, step) != 0) { 907 pt_reg_to_ucontext(®s, &tmbx.tm_context); 908 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, 909 sizeof(tmbx)); 910 } 911 } 912 return (P2T(ret)); 913} 914 915static void 916pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp) 917{ 918 int i; 919 920 for (i = 0; i < ta->map_len; ++i) { 921 if (ta->map[i].type == PT_LWP && ta->map[i].lwp == lwp) { 922 ta->map[i].type = PT_NONE; 923 return; 924 } 925 } 926} 927 928static int 929pt_validate(const td_thrhandle_t *th) 930{ 931 932 if (th->th_tid < 0 || th->th_tid >= th->th_ta->map_len || 933 th->th_ta->map[th->th_tid].type == PT_NONE) 934 return (TD_NOTHR); 935 return (TD_OK); 936} 937 938td_err_e 939pt_thr_tls_get_addr(const td_thrhandle_t *th, void *_linkmap, size_t offset, 940 void **address) 941{ 942 char *obj_entry; 943 const td_thragent_t *ta = th->th_ta; 944 psaddr_t tcb_addr, *dtv_addr, tcb_tp; 945 int tls_index, ret; 946 947 /* linkmap is a member of Obj_Entry */ 948 obj_entry = (char *)_linkmap - ta->thread_off_linkmap; 949 950 /* get tlsindex of the object file */ 951 ret = ps_pread(ta->ph, 952 obj_entry + ta->thread_off_tlsindex, 953 &tls_index, sizeof(tls_index)); 954 if (ret != 0) 955 return (P2T(ret)); 956 957 /* get thread tcb */ 958 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + 959 ta->thread_off_tcb, 960 &tcb_addr, sizeof(tcb_addr)); 961 if (ret != 0) 962 return (P2T(ret)); 963 964 /* get dtv array address */ 965 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv, 966 &dtv_addr, sizeof(dtv_addr)); 967 if (ret != 0) 968 return (P2T(ret)); 969 /* now get the object's tls block base address */ 970 ret = ps_pread(ta->ph, &dtv_addr[tls_index+1], address, 971 sizeof(*address)); 972 if (ret != 0) 973 return (P2T(ret)); 974 975 *address += offset; 976 return (TD_OK); 977} 978 979struct ta_ops libpthread_db_ops = { 980 .to_init = pt_init, 981 .to_ta_clear_event = pt_ta_clear_event, 982 .to_ta_delete = pt_ta_delete, 983 .to_ta_event_addr = pt_ta_event_addr, 984 .to_ta_event_getmsg = pt_ta_event_getmsg, 985 .to_ta_map_id2thr = pt_ta_map_id2thr, 986 .to_ta_map_lwp2thr = pt_ta_map_lwp2thr, 987 .to_ta_new = pt_ta_new, 988 .to_ta_set_event = pt_ta_set_event, 989 .to_ta_thr_iter = pt_ta_thr_iter, 990 .to_ta_tsd_iter = pt_ta_tsd_iter, 991 .to_thr_clear_event = pt_thr_clear_event, 992 .to_thr_dbresume = pt_thr_dbresume, 993 .to_thr_dbsuspend = pt_thr_dbsuspend, 994 .to_thr_event_enable = pt_thr_event_enable, 995 .to_thr_event_getmsg = pt_thr_event_getmsg, 996 .to_thr_get_info = pt_thr_get_info, 997 .to_thr_getfpregs = pt_thr_getfpregs, 998 .to_thr_getgregs = pt_thr_getgregs, 999 .to_thr_set_event = pt_thr_set_event, 1000 .to_thr_setfpregs = pt_thr_setfpregs, 1001 .to_thr_setgregs = pt_thr_setgregs, 1002 .to_thr_validate = pt_thr_validate, 1003 .to_thr_tls_get_addr = pt_thr_tls_get_addr, 1004 1005 /* FreeBSD specific extensions. */ 1006 .to_thr_sstep = pt_thr_sstep, 1007}; 1008