fbsd-threads.c revision 134152
1/* $FreeBSD: head/gnu/usr.bin/gdb/libgdb/fbsd-threads.c 134152 2004-08-22 09:05:57Z davidxu $ */ 2/* FreeBSD libthread_db assisted debugging support. 3 Copyright 1999, 2000, 2001 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 59 Temple Place - Suite 330, 20 Boston, MA 02111-1307, USA. */ 21 22#include <dlfcn.h> 23#include <sys/types.h> 24#include <sys/ptrace.h> 25 26#include "proc_service.h" 27#include "thread_db.h" 28 29#include "defs.h" 30#include "bfd.h" 31#include "elf-bfd.h" 32#include "gdb_assert.h" 33#include "gdbcore.h" 34#include "gdbthread.h" 35#include "inferior.h" 36#include "objfiles.h" 37#include "regcache.h" 38#include "symfile.h" 39#include "symtab.h" 40#include "target.h" 41#include "gdbcmd.h" 42#include "solib-svr4.h" 43 44 45#define LIBTHREAD_DB_SO "libthread_db.so" 46 47struct ps_prochandle 48{ 49 pid_t pid; 50}; 51 52extern int child_suppress_run; 53 54extern struct target_ops child_ops; 55 56/* This module's target vectors. */ 57static struct target_ops fbsd_thread_ops; 58static struct target_ops fbsd_core_ops; 59 60/* Saved copy of orignal core_ops. */ 61static struct target_ops orig_core_ops; 62extern struct target_ops core_ops; 63 64/* Pointer to the next function on the objfile event chain. */ 65static void (*target_new_objfile_chain) (struct objfile *objfile); 66 67/* Non-zero if there is a thread module */ 68static int fbsd_thread_present; 69 70/* Non-zero if we're using this module's target vector. */ 71static int fbsd_thread_active; 72 73/* Non-zero if core_open is called */ 74static int fbsd_thread_core = 0; 75 76/* Non-zero if we have to keep this module's target vector active 77 across re-runs. */ 78static int keep_thread_db; 79 80/* Structure that identifies the child process for the 81 <proc_service.h> interface. */ 82static struct ps_prochandle proc_handle; 83 84/* Connection to the libthread_db library. */ 85static td_thragent_t *thread_agent; 86 87/* The last thread we are single stepping */ 88static ptid_t last_single_step_thread; 89 90/* Pointers to the libthread_db functions. */ 91 92static td_err_e (*td_init_p) (void); 93 94static td_err_e (*td_ta_new_p) (struct ps_prochandle *ps, td_thragent_t **ta); 95static td_err_e (*td_ta_delete_p) (td_thragent_t *); 96static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt, 97 td_thrhandle_t *__th); 98static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid, 99 td_thrhandle_t *th); 100static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta, 101 td_thr_iter_f *callback, 102 void *cbdata_p, td_thr_state_e state, 103 int ti_pri, sigset_t *ti_sigmask_p, 104 unsigned int ti_user_flags); 105static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta, 106 td_event_e event, td_notify_t *ptr); 107static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta, 108 td_thr_events_t *event); 109static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta, 110 td_event_msg_t *msg); 111static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th, 112 td_thrinfo_t *infop); 113static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th, 114 prfpregset_t *regset); 115static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th, 116 prgregset_t gregs); 117static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th, 118 const prfpregset_t *fpregs); 119static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th, 120 prgregset_t gregs); 121static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event); 122 123static td_err_e (*td_thr_sstep_p) (td_thrhandle_t *th, int step); 124 125static td_err_e (*td_ta_tsd_iter_p) (const td_thragent_t *ta, 126 td_key_iter_f *func, void *data); 127static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, 128 void *map_address, 129 size_t offset, void **address); 130static td_err_e (*td_thr_dbsuspend_p) (const td_thrhandle_t *); 131static td_err_e (*td_thr_dbresume_p) (const td_thrhandle_t *); 132 133/* Prototypes for local functions. */ 134static void fbsd_thread_find_new_threads (void); 135static int fbsd_thread_alive (ptid_t ptid); 136 137/* Building process ids. */ 138 139#define GET_PID(ptid) ptid_get_pid (ptid) 140#define GET_LWP(ptid) ptid_get_lwp (ptid) 141#define GET_THREAD(ptid) ptid_get_tid (ptid) 142 143#define IS_LWP(ptid) (GET_LWP (ptid) != 0) 144#define IS_THREAD(ptid) (GET_THREAD (ptid) != 0) 145 146#define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0) 147#define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid) 148 149static char * 150thread_db_err_str (td_err_e err) 151{ 152 static char buf[64]; 153 154 switch (err) 155 { 156 case TD_OK: 157 return "generic 'call succeeded'"; 158 case TD_ERR: 159 return "generic error"; 160 case TD_NOTHR: 161 return "no thread to satisfy query"; 162 case TD_NOSV: 163 return "no sync handle to satisfy query"; 164 case TD_NOLWP: 165 return "no LWP to satisfy query"; 166 case TD_BADPH: 167 return "invalid process handle"; 168 case TD_BADTH: 169 return "invalid thread handle"; 170 case TD_BADSH: 171 return "invalid synchronization handle"; 172 case TD_BADTA: 173 return "invalid thread agent"; 174 case TD_BADKEY: 175 return "invalid key"; 176 case TD_NOMSG: 177 return "no event message for getmsg"; 178 case TD_NOFPREGS: 179 return "FPU register set not available"; 180 case TD_NOLIBTHREAD: 181 return "application not linked with libthread"; 182 case TD_NOEVENT: 183 return "requested event is not supported"; 184 case TD_NOCAPAB: 185 return "capability not available"; 186 case TD_DBERR: 187 return "debugger service failed"; 188 case TD_NOAPLIC: 189 return "operation not applicable to"; 190 case TD_NOTSD: 191 return "no thread-specific data for this thread"; 192 case TD_MALLOC: 193 return "malloc failed"; 194 case TD_PARTIALREG: 195 return "only part of register set was written/read"; 196 case TD_NOXREGS: 197 return "X register set not available for this thread"; 198 default: 199 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); 200 return buf; 201 } 202} 203 204static char * 205thread_db_state_str (td_thr_state_e state) 206{ 207 static char buf[64]; 208 209 switch (state) 210 { 211 case TD_THR_STOPPED: 212 return "stopped by debugger"; 213 case TD_THR_RUN: 214 return "runnable"; 215 case TD_THR_ACTIVE: 216 return "active"; 217 case TD_THR_ZOMBIE: 218 return "zombie"; 219 case TD_THR_SLEEP: 220 return "sleeping"; 221 case TD_THR_STOPPED_ASLEEP: 222 return "stopped by debugger AND blocked"; 223 default: 224 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state); 225 return buf; 226 } 227} 228 229/* Convert LWP to user-level thread id. */ 230static ptid_t 231thread_from_lwp (ptid_t ptid) 232{ 233 td_thrinfo_t ti; 234 td_thrhandle_t th; 235 td_err_e err; 236 237 gdb_assert (IS_LWP (ptid)); 238 239 if (fbsd_thread_active) 240 { 241 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); 242 if (err == TD_OK) 243 { 244 err = td_thr_get_info_p (&th, &ti); 245 if (err != TD_OK) 246 error ("Cannot get thread info: %s", thread_db_err_str (err)); 247 return BUILD_THREAD (ti.ti_tid, GET_PID (ptid)); 248 } 249 } 250 251 /* the LWP is not mapped to user thread */ 252 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid)); 253} 254 255static void 256fbsd_core_get_first_lwp (bfd *abfd, asection *asect, void *obj) 257{ 258 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) 259 return; 260 261 if (*(lwpid_t *)obj != 0) 262 return; 263 264 *(lwpid_t *)obj = atoi (bfd_section_name (abfd, asect) + 5); 265} 266 267static long 268get_current_lwp (int pid) 269{ 270 struct ptrace_lwpinfo pl; 271 lwpid_t lwpid; 272 273 if (!target_has_execution) 274 { 275 lwpid = 0; 276 bfd_map_over_sections (core_bfd, fbsd_core_get_first_lwp, &lwpid); 277 return lwpid; 278 } 279 if (ptrace (PT_LWPINFO, pid, (caddr_t)&pl, sizeof(pl))) 280 perror_with_name("PT_LWPINFO"); 281 282 return (long)pl.pl_lwpid; 283} 284 285static void 286get_current_thread () 287{ 288 long lwp; 289 ptid_t tmp, ptid; 290 291 lwp = get_current_lwp (proc_handle.pid); 292 tmp = BUILD_LWP (lwp, proc_handle.pid); 293 ptid = thread_from_lwp (tmp); 294 if (!in_thread_list (ptid)) 295 { 296 add_thread (ptid); 297 inferior_ptid = ptid; 298 } 299} 300 301static void 302fbsd_thread_activate (void) 303{ 304 fbsd_thread_active = 1; 305 init_thread_list(); 306 fbsd_thread_find_new_threads (); 307 get_current_thread (); 308} 309 310static void 311fbsd_thread_deactivate (void) 312{ 313 td_ta_delete_p (thread_agent); 314 315 inferior_ptid = pid_to_ptid (proc_handle.pid); 316 proc_handle.pid = 0; 317 fbsd_thread_active = 0; 318 fbsd_thread_present = 0; 319 init_thread_list (); 320} 321 322static void 323fbsd_thread_new_objfile (struct objfile *objfile) 324{ 325 td_err_e err; 326 327 if (objfile == NULL) 328 { 329 /* All symbols have been discarded. If the thread_db target is 330 active, deactivate it now. */ 331 if (fbsd_thread_active) 332 { 333 gdb_assert (proc_handle.pid == 0); 334 fbsd_thread_active = 0; 335 } 336 337 goto quit; 338 } 339 340 if (!child_suppress_run) 341 goto quit; 342 343 /* Nothing to do. The thread library was already detected and the 344 target vector was already activated. */ 345 if (fbsd_thread_active) 346 goto quit; 347 348 /* Initialize the structure that identifies the child process. Note 349 that at this point there is no guarantee that we actually have a 350 child process. */ 351 proc_handle.pid = GET_PID (inferior_ptid); 352 353 /* Now attempt to open a connection to the thread library. */ 354 err = td_ta_new_p (&proc_handle, &thread_agent); 355 switch (err) 356 { 357 case TD_NOLIBTHREAD: 358 /* No thread library was detected. */ 359 break; 360 361 case TD_OK: 362 /* The thread library was detected. Activate the thread_db target. */ 363 fbsd_thread_present = 1; 364 365 /* We can only poke around if there actually is a child process. 366 If there is no child process alive, postpone the steps below 367 until one has been created. */ 368 if (fbsd_thread_core == 0 && proc_handle.pid != 0) 369 { 370 push_target(&fbsd_thread_ops); 371 fbsd_thread_activate(); 372 } 373 else 374 { 375 td_ta_delete_p(thread_agent); 376 thread_agent = NULL; 377 } 378 break; 379 380 default: 381 warning ("Cannot initialize thread debugging library: %s", 382 thread_db_err_str (err)); 383 break; 384 } 385 386 quit: 387 if (target_new_objfile_chain) 388 target_new_objfile_chain (objfile); 389} 390 391static void 392fbsd_thread_attach (char *args, int from_tty) 393{ 394 fbsd_thread_core = 0; 395 396 child_ops.to_attach (args, from_tty); 397 398 /* Must get symbols from solibs before libthread_db can run! */ 399 SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0, auto_solib_add); 400 401 if (fbsd_thread_present && !fbsd_thread_active) 402 push_target(&fbsd_thread_ops); 403} 404 405static void 406fbsd_thread_post_attach (int pid) 407{ 408 child_ops.to_post_attach (pid); 409 410 if (fbsd_thread_present && !fbsd_thread_active) 411 { 412 proc_handle.pid = GET_PID (inferior_ptid); 413 fbsd_thread_activate (); 414 } 415} 416 417static void 418fbsd_thread_detach (char *args, int from_tty) 419{ 420 fbsd_thread_deactivate (); 421 unpush_target (&fbsd_thread_ops); 422 423 /* Clear gdb solib information and symbol file 424 cache, so that after detach and re-attach, new_objfile 425 hook will be called */ 426 427 clear_solib(); 428 symbol_file_clear(0); 429 proc_handle.pid = 0; 430 child_ops.to_detach (args, from_tty); 431} 432 433static int 434suspend_thread_callback (const td_thrhandle_t *th_p, void *data) 435{ 436 int err = td_thr_dbsuspend_p (th_p); 437 if (err != 0) 438 fprintf_filtered(gdb_stderr, "%s %s\n", __func__, thread_db_err_str (err)); 439 return (err); 440} 441 442static int 443resume_thread_callback (const td_thrhandle_t *th_p, void *data) 444{ 445 int err = td_thr_dbresume_p (th_p); 446 if (err != 0) 447 fprintf_filtered(gdb_stderr, "%s %s\n", __func__, thread_db_err_str (err)); 448 return (err); 449} 450 451static void 452fbsd_thread_resume (ptid_t ptid, int step, enum target_signal signo) 453{ 454 td_thrhandle_t th; 455 td_thrinfo_t ti; 456 ptid_t work_ptid; 457 int resume_all, ret; 458 long lwp, thvalid = 0; 459 460 if (!fbsd_thread_active) 461 { 462 child_ops.to_resume (ptid, step, signo); 463 return; 464 } 465 466 if (GET_PID(ptid) != -1 && step != 0) 467 { 468 resume_all = 0; 469 work_ptid = ptid; 470 } 471 else 472 { 473 resume_all = 1; 474 work_ptid = inferior_ptid; 475 } 476 477 lwp = GET_LWP (work_ptid); 478 if (lwp == 0) 479 { 480 /* check user thread */ 481 ret = td_ta_map_id2thr_p (thread_agent, GET_THREAD(work_ptid), &th); 482 if (ret) 483 error (thread_db_err_str (ret)); 484 485 /* For M:N thread, we need to tell UTS to set/unset single step 486 flag at context switch time, the flag will be written into 487 thread mailbox. This becauses some architecture may not have 488 machine single step flag in ucontext, so we put the flag in mailbox, 489 when the thread switches back, kse_switchin restores the single step 490 state. */ 491 ret = td_thr_sstep_p (&th, step); 492 if (ret) 493 error (thread_db_err_str (ret)); 494 ret = td_thr_get_info_p (&th, &ti); 495 if (ret) 496 error (thread_db_err_str (ret)); 497 thvalid = 1; 498 lwp = ti.ti_lid; 499 } 500 501 if (lwp) 502 { 503 int req = step ? PT_SETSTEP : PT_CLEARSTEP; 504 if (ptrace (req, (pid_t) lwp, (caddr_t) 1, target_signal_to_host(signo))) 505 perror_with_name ("PT_SETSTEP/PT_CLEARSTEP"); 506 } 507 508 if (!ptid_equal (last_single_step_thread, null_ptid)) 509 { 510 ret = td_ta_thr_iter_p (thread_agent, resume_thread_callback, NULL, 511 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 512 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 513 if (ret != TD_OK) 514 error ("resume error: %s", thread_db_err_str (ret)); 515 } 516 517 if (!resume_all) 518 { 519 ret = td_ta_thr_iter_p (thread_agent, suspend_thread_callback, NULL, 520 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 521 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 522 if (ret != TD_OK) 523 error ("suspend error: %s", thread_db_err_str (ret)); 524 last_single_step_thread = work_ptid; 525 } 526 else 527 last_single_step_thread = null_ptid; 528 529 if (thvalid) 530 { 531 ret = td_thr_dbresume_p (&th); 532 if (ret != TD_OK) 533 error ("resume error: %s", thread_db_err_str (ret)); 534 } 535 else 536 { 537 /* it is not necessary, put it here for completness */ 538 ret = ptrace(PT_RESUME, lwp, 0, 0); 539 } 540 541 /* now continue the process, suspended thread wont run */ 542 if (ptrace (PT_CONTINUE, proc_handle.pid , (caddr_t)1, 543 target_signal_to_host(signo))) 544 perror_with_name ("PT_CONTINUE"); 545} 546 547static ptid_t 548fbsd_thread_wait (ptid_t ptid, struct target_waitstatus *ourstatus) 549{ 550 ptid_t ret; 551 long lwp; 552 CORE_ADDR stop_pc; 553 554 ret = child_ops.to_wait (ptid, ourstatus); 555 if (GET_PID(ret) >= 0 && ourstatus->kind == TARGET_WAITKIND_STOPPED) 556 { 557 lwp = get_current_lwp (GET_PID(ret)); 558 ret = thread_from_lwp (BUILD_LWP (lwp, GET_PID (ret))); 559 if (!in_thread_list (ret)) 560 add_thread (ret); 561 /* this is a hack, if an event won't cause gdb to stop, for example, 562 SIGARLM, gdb resumes the process immediatly without setting 563 inferior_ptid to the new thread returned here, this is a bug 564 because inferior_ptid may already not exist there, and passing 565 a none existing thread to fbsd_thread_resume causes error. */ 566 if (!fbsd_thread_alive (inferior_ptid)) 567 { 568 delete_thread (inferior_ptid); 569 inferior_ptid = ret; 570 } 571 } 572 573 return (ret); 574} 575 576static int 577fbsd_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, 578 struct mem_attrib *attrib, struct target_ops *target) 579{ 580 int err; 581 582 if (target_has_execution) 583 err = child_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib, 584 target); 585 else 586 err = orig_core_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib, 587 target); 588 589 return (err); 590} 591 592static void 593fbsd_lwp_fetch_registers (int regno) 594{ 595 gregset_t gregs; 596 fpregset_t fpregs; 597 lwpid_t lwp; 598 599 if (!target_has_execution) 600 { 601 orig_core_ops.to_fetch_registers (-1); 602 return; 603 } 604 605 lwp = GET_LWP (inferior_ptid); 606 607 if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1) 608 error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno)); 609 supply_gregset (&gregs); 610 611 if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1) 612 error ("Cannot get lwp %d registers: %s\n ", lwp, safe_strerror (errno)); 613 614 supply_fpregset (&fpregs); 615} 616 617static void 618fbsd_thread_fetch_registers (int regno) 619{ 620 prgregset_t gregset; 621 prfpregset_t fpregset; 622 td_thrhandle_t th; 623 td_err_e err; 624 625 if (!IS_THREAD (inferior_ptid)) 626 { 627 fbsd_lwp_fetch_registers (regno); 628 return; 629 } 630 631 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th); 632 if (err != TD_OK) 633 error ("Cannot find thread %d: Thread ID=%ld, %s", 634 pid_to_thread_id (inferior_ptid), 635 GET_THREAD (inferior_ptid), thread_db_err_str (err)); 636 637 err = td_thr_getgregs_p (&th, gregset); 638 if (err != TD_OK) 639 error ("Cannot fetch general-purpose registers for thread %d: Thread ID=%ld, %s", 640 pid_to_thread_id (inferior_ptid), 641 GET_THREAD (inferior_ptid), thread_db_err_str (err)); 642 643 err = td_thr_getfpregs_p (&th, &fpregset); 644 if (err != TD_OK) 645 error ("Cannot get floating-point registers for thread %d: Thread ID=%ld, %s", 646 pid_to_thread_id (inferior_ptid), 647 GET_THREAD (inferior_ptid), thread_db_err_str (err)); 648 649 supply_gregset (gregset); 650 supply_fpregset (&fpregset); 651} 652 653static void 654fbsd_lwp_store_registers (int regno) 655{ 656 gregset_t gregs; 657 fpregset_t fpregs; 658 lwpid_t lwp; 659 660 /* FIXME, is it possible ? */ 661 if (!IS_LWP (inferior_ptid)) 662 { 663 child_ops.to_store_registers (regno); 664 return ; 665 } 666 667 lwp = GET_LWP (inferior_ptid); 668 if (regno != -1) 669 if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1) 670 error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno)); 671 672 fill_gregset (&gregs, regno); 673 if (ptrace (PT_SETREGS, lwp, (caddr_t) &gregs, 0) == -1) 674 error ("Cannot set lwp %d registers: %s\n", lwp, safe_strerror (errno)); 675 676 if (regno != -1) 677 if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1) 678 error ("Cannot get lwp %d float registers: %s\n", lwp, 679 safe_strerror (errno)); 680 681 fill_fpregset (&fpregs, regno); 682 if (ptrace (PT_SETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1) 683 error ("Cannot set lwp %d float registers: %s\n", lwp, 684 safe_strerror (errno)); 685} 686 687static void 688fbsd_thread_store_registers (int regno) 689{ 690 prgregset_t gregset; 691 prfpregset_t fpregset; 692 td_thrhandle_t th; 693 td_err_e err; 694 695 if (!IS_THREAD (inferior_ptid)) 696 { 697 fbsd_lwp_store_registers (regno); 698 return; 699 } 700 701 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th); 702 if (err != TD_OK) 703 error ("Cannot find thread %d: Thread ID=%ld, %s", 704 pid_to_thread_id (inferior_ptid), 705 GET_THREAD (inferior_ptid), 706 thread_db_err_str (err)); 707 708 if (regno != -1) 709 { 710 char old_value[MAX_REGISTER_SIZE]; 711 712 regcache_collect (regno, old_value); 713 err = td_thr_getgregs_p (&th, gregset); 714 if (err != TD_OK) 715 error ("%s: td_thr_getgregs %s", __func__, thread_db_err_str (err)); 716 err = td_thr_getfpregs_p (&th, &fpregset); 717 if (err != TD_OK) 718 error ("%s: td_thr_getfpgregs %s", __func__, thread_db_err_str (err)); 719 supply_register (regno, old_value); 720 } 721 722 fill_gregset (gregset, regno); 723 fill_fpregset (&fpregset, regno); 724 725 err = td_thr_setgregs_p (&th, gregset); 726 if (err != TD_OK) 727 error ("Cannot store general-purpose registers for thread %d: Thread ID=%d, %s", 728 pid_to_thread_id (inferior_ptid), GET_THREAD (inferior_ptid), 729 thread_db_err_str (err)); 730 err = td_thr_setfpregs_p (&th, &fpregset); 731 if (err != TD_OK) 732 error ("Cannot store floating-point registers for thread %d: Thread ID=%d, %s", 733 pid_to_thread_id (inferior_ptid), GET_THREAD (inferior_ptid), 734 thread_db_err_str (err)); 735} 736 737static void 738fbsd_thread_kill (void) 739{ 740 child_ops.to_kill(); 741} 742 743static int 744fbsd_thread_can_run (void) 745{ 746 return child_suppress_run; 747} 748 749static void 750fbsd_thread_create_inferior (char *exec_file, char *allargs, char **env) 751{ 752 if (fbsd_thread_present && !fbsd_thread_active) 753 push_target(&fbsd_thread_ops); 754 755 child_ops.to_create_inferior (exec_file, allargs, env); 756} 757 758static void 759fbsd_thread_post_startup_inferior (ptid_t ptid) 760{ 761 if (fbsd_thread_present && !fbsd_thread_active) 762 { 763 /* The child process is now the actual multi-threaded 764 program. Snatch its process ID... */ 765 proc_handle.pid = GET_PID (ptid); 766 td_ta_new_p (&proc_handle, &thread_agent); 767 fbsd_thread_activate(); 768 } 769} 770 771static void 772fbsd_thread_mourn_inferior (void) 773{ 774 if (fbsd_thread_active) 775 fbsd_thread_deactivate (); 776 777 unpush_target (&fbsd_thread_ops); 778 779 child_ops.to_mourn_inferior (); 780} 781 782static void 783fbsd_core_check_lwp (bfd *abfd, asection *asect, void *obj) 784{ 785 lwpid_t lwp; 786 787 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) 788 return; 789 790 /* already found */ 791 if (*(lwpid_t *)obj == 0) 792 return; 793 794 lwp = atoi (bfd_section_name (abfd, asect) + 5); 795 if (*(lwpid_t *)obj == lwp) 796 *(lwpid_t *)obj = 0; 797} 798 799static int 800fbsd_thread_alive (ptid_t ptid) 801{ 802 td_thrhandle_t th; 803 td_thrinfo_t ti; 804 td_err_e err; 805 gregset_t gregs; 806 lwpid_t lwp; 807 808 if (IS_THREAD (ptid)) 809 { 810 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th); 811 if (err != TD_OK) 812 return 0; 813 814 err = td_thr_get_info_p (&th, &ti); 815 if (err != TD_OK) 816 return 0; 817 818 /* A zombie thread. */ 819 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) 820 return 0; 821 822 return 1; 823 } 824 else if (GET_LWP (ptid) == 0) 825 { 826 /* we sometimes are called with lwp == 0 */ 827 return 1; 828 } 829 830 if (fbsd_thread_active) 831 { 832 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); 833 834 /* 835 * if the lwp was already mapped to user thread, don't use it 836 * directly, please use user thread id instead. 837 */ 838 if (err == TD_OK) 839 return 0; 840 } 841 842 if (!target_has_execution) 843 { 844 lwp = GET_LWP (ptid); 845 bfd_map_over_sections (core_bfd, fbsd_core_check_lwp, &lwp); 846 return (lwp == 0); 847 } 848 849 /* check lwp in kernel */ 850 return ptrace (PT_GETREGS, GET_LWP (ptid), (caddr_t)&gregs, 0) == 0; 851} 852 853static void 854fbsd_thread_files_info (struct target_ops *ignore) 855{ 856 child_ops.to_files_info (ignore); 857} 858 859static int 860find_new_threads_callback (const td_thrhandle_t *th_p, void *data) 861{ 862 td_thrinfo_t ti; 863 td_err_e err; 864 ptid_t ptid; 865 866 err = td_thr_get_info_p (th_p, &ti); 867 if (err != TD_OK) 868 error ("Cannot get thread info: %s", thread_db_err_str (err)); 869 870 /* Ignore zombie */ 871 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) 872 return 0; 873 874 ptid = BUILD_THREAD (ti.ti_tid, proc_handle.pid); 875 876 if (!in_thread_list (ptid)) 877 add_thread (ptid); 878 return 0; 879} 880 881static void 882fbsd_thread_find_new_threads (void) 883{ 884 td_err_e err; 885 886 if (!fbsd_thread_active) 887 return; 888 889 /* Iterate over all user-space threads to discover new threads. */ 890 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL, 891 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 892 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 893 if (err != TD_OK) 894 error ("Cannot find new threads: %s", thread_db_err_str (err)); 895} 896 897static char * 898fbsd_thread_pid_to_str (ptid_t ptid) 899{ 900 static char buf[64]; 901 902 if (IS_THREAD (ptid)) 903 { 904 td_thrhandle_t th; 905 td_thrinfo_t ti; 906 td_err_e err; 907 908 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th); 909 if (err != TD_OK) 910 error ("Cannot find thread, Thread ID=%ld, %s", 911 GET_THREAD (ptid), thread_db_err_str (err)); 912 913 err = td_thr_get_info_p (&th, &ti); 914 if (err != TD_OK) 915 error ("Cannot get thread info, Thread ID=%ld, %s", 916 GET_THREAD (ptid), thread_db_err_str (err)); 917 918 if (ti.ti_lid != 0) 919 { 920 snprintf (buf, sizeof (buf), "Thread %ld (LWP %d)", 921 GET_THREAD (ptid), ti.ti_lid); 922 } 923 else 924 { 925 snprintf (buf, sizeof (buf), "Thread %ld (%s)", 926 GET_THREAD (ptid), thread_db_state_str (ti.ti_state)); 927 } 928 929 return buf; 930 } 931 else if (IS_LWP (ptid)) 932 { 933 snprintf (buf, sizeof (buf), "LWP %d", (int) GET_LWP (ptid)); 934 return buf; 935 } 936 return normal_pid_to_str (ptid); 937} 938 939CORE_ADDR 940fbsd_thread_get_local_address(ptid_t ptid, struct objfile *objfile, 941 CORE_ADDR offset) 942{ 943 td_thrhandle_t th; 944 void *address; 945 CORE_ADDR lm; 946 int ret, is_library = (objfile->flags & OBJF_SHARED); 947 948 if (IS_THREAD (ptid)) 949 { 950 if (!td_thr_tls_get_addr_p) 951 error ("Cannot find thread-local interface in thread_db library."); 952 953 /* Get the address of the link map for this objfile. */ 954 lm = svr4_fetch_objfile_link_map (objfile); 955 956 /* Couldn't find link map. Bail out. */ 957 if (!lm) 958 { 959 if (is_library) 960 error ("Cannot find shared library `%s' link_map in dynamic" 961 " linker's module list", objfile->name); 962 else 963 error ("Cannot find executable file `%s' link_map in dynamic" 964 " linker's module list", objfile->name); 965 } 966 967 ret = td_ta_map_id2thr_p (thread_agent, GET_THREAD(ptid), &th); 968 969 /* get the address of the variable. */ 970 ret = td_thr_tls_get_addr_p (&th, (void *) lm, offset, &address); 971 972 if (ret != TD_OK) 973 { 974 if (is_library) 975 error ("Cannot find thread-local storage for thread %ld, " 976 "shared library %s:\n%s", 977 (long) GET_THREAD (ptid), 978 objfile->name, thread_db_err_str (ret)); 979 else 980 error ("Cannot find thread-local storage for thread %ld, " 981 "executable file %s:\n%s", 982 (long) GET_THREAD (ptid), 983 objfile->name, thread_db_err_str (ret)); 984 } 985 986 /* Cast assuming host == target. */ 987 return (CORE_ADDR) address; 988 } 989 return (0); 990} 991 992static int 993tsd_cb (thread_key_t key, void (*destructor)(void *), void *ignore) 994{ 995 struct minimal_symbol *ms; 996 char *name; 997 998 ms = lookup_minimal_symbol_by_pc ((CORE_ADDR)destructor); 999 if (!ms) 1000 name = "???"; 1001 else 1002 name = DEPRECATED_SYMBOL_NAME (ms); 1003 1004 printf_filtered ("Destructor %p <%s>\n", destructor, name); 1005 return 0; 1006} 1007 1008static void 1009fbsd_thread_tsd_cmd (char *exp, int from_tty) 1010{ 1011 if (fbsd_thread_active) 1012 td_ta_tsd_iter_p (thread_agent, tsd_cb, NULL); 1013} 1014 1015static int 1016ignore (CORE_ADDR addr, char *contents) 1017{ 1018 return 0; 1019} 1020 1021static void 1022fbsd_core_open (char *filename, int from_tty) 1023{ 1024 int err; 1025 1026 fbsd_thread_core = 1; 1027 1028 orig_core_ops.to_open (filename, from_tty); 1029 1030 if (fbsd_thread_present) 1031 { 1032 err = td_ta_new_p (&proc_handle, &thread_agent); 1033 if (err == TD_OK) 1034 { 1035 proc_handle.pid = elf_tdata (core_bfd)->core_pid; 1036 fbsd_thread_activate (); 1037 } 1038 else 1039 error ("fbsd_core_open: td_ta_new: %s", thread_db_err_str (err)); 1040 } 1041} 1042 1043static void 1044fbsd_core_close (int quitting) 1045{ 1046 orig_core_ops.to_close (quitting); 1047} 1048 1049static void 1050fbsd_core_detach (char *args, int from_tty) 1051{ 1052 if (fbsd_thread_active) 1053 fbsd_thread_deactivate (); 1054 unpush_target (&fbsd_thread_ops); 1055 orig_core_ops.to_detach (args, from_tty); 1056 1057 /* Clear gdb solib information and symbol file 1058 cache, so that after detach and re-attach, new_objfile 1059 hook will be called */ 1060 clear_solib(); 1061 symbol_file_clear(0); 1062} 1063 1064static void 1065fbsd_core_files_info (struct target_ops *ignore) 1066{ 1067 orig_core_ops.to_files_info (ignore); 1068} 1069 1070static void 1071init_fbsd_core_ops (void) 1072{ 1073 fbsd_core_ops.to_shortname = "FreeBSD-core"; 1074 fbsd_core_ops.to_longname = "FreeBSD core thread."; 1075 fbsd_core_ops.to_doc = "FreeBSD threads support for core files."; 1076 fbsd_core_ops.to_open = fbsd_core_open; 1077 fbsd_core_ops.to_close = fbsd_core_close; 1078 fbsd_core_ops.to_attach = 0; 1079 fbsd_core_ops.to_post_attach = 0; 1080 fbsd_core_ops.to_detach = fbsd_core_detach; 1081 /* fbsd_core_ops.to_resume = 0; */ 1082 /* fbsd_core_ops.to_wait = 0; */ 1083 fbsd_core_ops.to_fetch_registers = fbsd_thread_fetch_registers; 1084 /* fbsd_core_ops.to_store_registers = 0; */ 1085 /* fbsd_core_ops.to_prepare_to_store = 0; */ 1086 fbsd_core_ops.to_xfer_memory = fbsd_thread_xfer_memory; 1087 fbsd_core_ops.to_files_info = fbsd_core_files_info; 1088 fbsd_core_ops.to_insert_breakpoint = ignore; 1089 fbsd_core_ops.to_remove_breakpoint = ignore; 1090 /* fbsd_core_ops.to_lookup_symbol = 0; */ 1091 fbsd_core_ops.to_create_inferior = fbsd_thread_create_inferior; 1092 fbsd_core_ops.to_stratum = core_stratum; 1093 fbsd_core_ops.to_has_all_memory = 0; 1094 fbsd_core_ops.to_has_memory = 1; 1095 fbsd_core_ops.to_has_stack = 1; 1096 fbsd_core_ops.to_has_registers = 1; 1097 fbsd_core_ops.to_has_execution = 0; 1098 fbsd_core_ops.to_has_thread_control = tc_none; 1099 fbsd_core_ops.to_thread_alive = fbsd_thread_alive; 1100 fbsd_core_ops.to_pid_to_str = fbsd_thread_pid_to_str; 1101 fbsd_core_ops.to_find_new_threads = fbsd_thread_find_new_threads; 1102 fbsd_core_ops.to_sections = 0; 1103 fbsd_core_ops.to_sections_end = 0; 1104 fbsd_core_ops.to_magic = OPS_MAGIC; 1105} 1106 1107static void 1108init_fbsd_thread_ops (void) 1109{ 1110 fbsd_thread_ops.to_shortname = "freebsd-threads"; 1111 fbsd_thread_ops.to_longname = "FreeBSD multithreaded child process."; 1112 fbsd_thread_ops.to_doc = "FreeBSD threads support."; 1113 fbsd_thread_ops.to_attach = fbsd_thread_attach; 1114 fbsd_thread_ops.to_detach = fbsd_thread_detach; 1115 fbsd_thread_ops.to_post_attach = fbsd_thread_post_attach; 1116 fbsd_thread_ops.to_resume = fbsd_thread_resume; 1117 fbsd_thread_ops.to_wait = fbsd_thread_wait; 1118 fbsd_thread_ops.to_fetch_registers = fbsd_thread_fetch_registers; 1119 fbsd_thread_ops.to_store_registers = fbsd_thread_store_registers; 1120 fbsd_thread_ops.to_xfer_memory = fbsd_thread_xfer_memory; 1121 fbsd_thread_ops.to_files_info = fbsd_thread_files_info; 1122 fbsd_thread_ops.to_kill = fbsd_thread_kill; 1123 fbsd_thread_ops.to_create_inferior = fbsd_thread_create_inferior; 1124 fbsd_thread_ops.to_post_startup_inferior = fbsd_thread_post_startup_inferior; 1125 fbsd_thread_ops.to_mourn_inferior = fbsd_thread_mourn_inferior; 1126 fbsd_thread_ops.to_can_run = fbsd_thread_can_run; 1127 fbsd_thread_ops.to_thread_alive = fbsd_thread_alive; 1128 fbsd_thread_ops.to_find_new_threads = fbsd_thread_find_new_threads; 1129 fbsd_thread_ops.to_pid_to_str = fbsd_thread_pid_to_str; 1130 fbsd_thread_ops.to_stratum = thread_stratum; 1131 fbsd_thread_ops.to_has_thread_control = tc_none; 1132 fbsd_thread_ops.to_has_all_memory = 1; 1133 fbsd_thread_ops.to_has_memory = 1; 1134 fbsd_thread_ops.to_has_stack = 1; 1135 fbsd_thread_ops.to_has_registers = 1; 1136 fbsd_thread_ops.to_has_execution = 1; 1137 fbsd_thread_ops.to_insert_breakpoint = memory_insert_breakpoint; 1138 fbsd_thread_ops.to_remove_breakpoint = memory_remove_breakpoint; 1139 fbsd_thread_ops.to_get_thread_local_address = fbsd_thread_get_local_address; 1140 fbsd_thread_ops.to_magic = OPS_MAGIC; 1141} 1142 1143static int 1144thread_db_load (void) 1145{ 1146 void *handle; 1147 td_err_e err; 1148 1149 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW); 1150 if (handle == NULL) 1151 return 0; 1152 1153#define resolve(X) \ 1154 if (!(X##_p = dlsym (handle, #X))) \ 1155 return 0; 1156 1157 resolve(td_init); 1158 resolve(td_ta_new); 1159 resolve(td_ta_delete); 1160 resolve(td_ta_map_id2thr); 1161 resolve(td_ta_map_lwp2thr); 1162 resolve(td_ta_thr_iter); 1163 resolve(td_thr_get_info); 1164 resolve(td_thr_getfpregs); 1165 resolve(td_thr_getgregs); 1166 resolve(td_thr_setfpregs); 1167 resolve(td_thr_setgregs); 1168 resolve(td_thr_sstep); 1169 resolve(td_ta_tsd_iter); 1170 resolve(td_thr_dbsuspend); 1171 resolve(td_thr_dbresume); 1172 resolve(td_thr_tls_get_addr); 1173 1174 /* Initialize the library. */ 1175 err = td_init_p (); 1176 if (err != TD_OK) 1177 { 1178 warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err)); 1179 return 0; 1180 } 1181 1182 return 1; 1183} 1184 1185/* we suppress the call to add_target of core_ops in corelow because 1186 if there are two targets in the stratum core_stratum, find_core_target 1187 won't know which one to return. see corelow.c for an additonal 1188 comment on coreops_suppress_target. */ 1189 1190int coreops_suppress_target = 1; 1191 1192void 1193_initialize_thread_db (void) 1194{ 1195 init_fbsd_thread_ops (); 1196 init_fbsd_core_ops (); 1197 1198 if (thread_db_load ()) 1199 { 1200 add_target (&fbsd_thread_ops); 1201 1202 /* "thread tsd" command */ 1203 add_cmd ("tsd", class_run, fbsd_thread_tsd_cmd, 1204 "Show the thread-specific data keys and destructors " 1205 "for the process.\n", 1206 &thread_cmd_list); 1207 1208 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops)); 1209 memcpy (&core_ops, &fbsd_core_ops, sizeof (struct target_ops)); 1210 add_target (&core_ops); 1211 1212 /* Add ourselves to objfile event chain. */ 1213 target_new_objfile_chain = target_new_objfile_hook; 1214 target_new_objfile_hook = fbsd_thread_new_objfile; 1215 1216 child_suppress_run = 1; 1217 } 1218 else 1219 { 1220 fprintf_unfiltered (gdb_stderr, 1221 "[GDB will not be able to debug user-mode threads: %s]\n", dlerror()); 1222 1223 /* allow the user to debug non-threaded core files */ 1224 add_target (&core_ops); 1225 } 1226} 1227 1228/* proc service functions */ 1229void 1230ps_plog (const char *fmt, ...) 1231{ 1232 va_list args; 1233 1234 va_start (args, fmt); 1235 vfprintf_filtered (gdb_stderr, fmt, args); 1236 va_end (args); 1237} 1238 1239ps_err_e 1240ps_pglobal_lookup (struct ps_prochandle *ph, const char *obj, 1241 const char *name, psaddr_t *sym_addr) 1242{ 1243 struct minimal_symbol *ms; 1244 1245 ms = lookup_minimal_symbol (name, NULL, NULL); 1246 if (ms == NULL) 1247 return PS_NOSYM; 1248 1249 *sym_addr = (psaddr_t) SYMBOL_VALUE_ADDRESS (ms); 1250 return PS_OK; 1251} 1252 1253ps_err_e 1254ps_pread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len) 1255{ 1256 int err = target_read_memory ((CORE_ADDR) addr, buf, len); 1257 return (err == 0 ? PS_OK : PS_ERR); 1258} 1259 1260ps_err_e 1261ps_pwrite (struct ps_prochandle *ph, psaddr_t addr, const void *buf, 1262 size_t len) 1263{ 1264 int err = target_write_memory ((CORE_ADDR) addr, (void *)buf, len); 1265 return (err == 0 ? PS_OK : PS_ERR); 1266} 1267 1268ps_err_e 1269ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset) 1270{ 1271 struct cleanup *old_chain; 1272 1273 old_chain = save_inferior_ptid (); 1274 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1275 target_fetch_registers (-1); 1276 fill_gregset (gregset, -1); 1277 do_cleanups (old_chain); 1278 return PS_OK; 1279} 1280 1281ps_err_e 1282ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset) 1283{ 1284 struct cleanup *old_chain; 1285 1286 old_chain = save_inferior_ptid (); 1287 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1288 supply_gregset (gregset); 1289 target_store_registers (-1); 1290 do_cleanups (old_chain); 1291 return PS_OK; 1292} 1293 1294ps_err_e 1295ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, prfpregset_t *fpregset) 1296{ 1297 struct cleanup *old_chain; 1298 1299 old_chain = save_inferior_ptid (); 1300 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1301 target_fetch_registers (-1); 1302 fill_fpregset (fpregset, -1); 1303 do_cleanups (old_chain); 1304 return PS_OK; 1305} 1306 1307ps_err_e 1308ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, 1309 const prfpregset_t *fpregset) 1310{ 1311 struct cleanup *old_chain; 1312 1313 old_chain = save_inferior_ptid (); 1314 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1315 supply_fpregset (fpregset); 1316 target_store_registers (-1); 1317 do_cleanups (old_chain); 1318 return PS_OK; 1319} 1320 1321ps_err_e 1322ps_lstop(struct ps_prochandle *ph, lwpid_t lwpid) 1323{ 1324 if (ptrace (PT_SUSPEND, lwpid, 0, 0) == -1) 1325 return PS_ERR; 1326 return PS_OK; 1327} 1328 1329ps_err_e 1330ps_lcontinue(struct ps_prochandle *ph, lwpid_t lwpid) 1331{ 1332 if (ptrace (PT_RESUME, lwpid, 0, 0) == -1) 1333 return PS_ERR; 1334 return PS_OK; 1335} 1336