fbsd-threads.c revision 134149
1/* $FreeBSD: head/gnu/usr.bin/gdb/libgdb/fbsd-threads.c 134149 2004-08-22 08:48:56Z 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 vector. */ 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 /* Don't attempt to use thread_db on targets which can not run 328 (core files). */ 329 if (objfile == NULL) 330 { 331 /* All symbols have been discarded. If the thread_db target is 332 active, deactivate it now. */ 333 if (fbsd_thread_active) 334 { 335 gdb_assert (proc_handle.pid == 0); 336 fbsd_thread_active = 0; 337 } 338 339 goto quit; 340 } 341 342 if (!child_suppress_run) 343 goto quit; 344 345 if (fbsd_thread_active) 346 /* Nothing to do. The thread library was already detected and the 347 target vector was already activated. */ 348 goto quit; 349 350 /* Initialize the structure that identifies the child process. Note 351 that at this point there is no guarantee that we actually have a 352 child process. */ 353 proc_handle.pid = GET_PID (inferior_ptid); 354 355 /* Now attempt to open a connection to the thread library. */ 356 err = td_ta_new_p (&proc_handle, &thread_agent); 357 switch (err) 358 { 359 case TD_NOLIBTHREAD: 360 /* No thread library was detected. */ 361 break; 362 363 case TD_OK: 364 /* The thread library was detected. Activate the thread_db target. */ 365 fbsd_thread_present = 1; 366 367 /* We can only poke around if there actually is a child process. 368 If there is no child process alive, postpone the steps below 369 until one has been created. */ 370 if (fbsd_thread_core == 0 && proc_handle.pid != 0) 371 { 372 push_target(&fbsd_thread_ops); 373 fbsd_thread_activate(); 374 } 375 else 376 { 377 td_ta_delete_p(thread_agent); 378 thread_agent = NULL; 379 } 380 break; 381 382 default: 383 warning ("Cannot initialize thread debugging library: %s", 384 thread_db_err_str (err)); 385 break; 386 } 387 388 quit: 389 if (target_new_objfile_chain) 390 target_new_objfile_chain (objfile); 391} 392 393static void 394fbsd_thread_attach (char *args, int from_tty) 395{ 396 fbsd_thread_core = 0; 397 398 child_ops.to_attach (args, from_tty); 399 400 /* Must get symbols from solibs before libthread_db can run! */ 401 SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0, auto_solib_add); 402 403 if (fbsd_thread_present && !fbsd_thread_active) 404 push_target(&fbsd_thread_ops); 405} 406 407static void 408fbsd_thread_post_attach (int pid) 409{ 410 child_ops.to_post_attach (pid); 411 412 if (fbsd_thread_present && !fbsd_thread_active) 413 { 414 proc_handle.pid = GET_PID (inferior_ptid); 415 fbsd_thread_activate (); 416 } 417} 418 419static void 420fbsd_thread_detach (char *args, int from_tty) 421{ 422 fbsd_thread_deactivate (); 423 unpush_target (&fbsd_thread_ops); 424 425 /* Clear gdb solib information and symbol file 426 cache, so that after detach and re-attach, new_objfile 427 hook will be called */ 428 429 clear_solib(); 430 symbol_file_clear(0); 431 proc_handle.pid = 0; 432 child_ops.to_detach (args, from_tty); 433} 434 435static int 436suspend_thread_callback (const td_thrhandle_t *th_p, void *data) 437{ 438 int err = td_thr_dbsuspend_p (th_p); 439 if (err != 0) 440 fprintf_filtered(gdb_stderr, "%s %s\n", __func__, thread_db_err_str (err)); 441 return (err); 442} 443 444static int 445resume_thread_callback (const td_thrhandle_t *th_p, void *data) 446{ 447 int err = td_thr_dbresume_p (th_p); 448 if (err != 0) 449 fprintf_filtered(gdb_stderr, "%s %s\n", __func__, thread_db_err_str (err)); 450 return (err); 451} 452 453static void 454fbsd_thread_resume (ptid_t ptid, int step, enum target_signal signo) 455{ 456 td_thrhandle_t th; 457 td_thrinfo_t ti; 458 ptid_t work_ptid; 459 int resume_all, ret; 460 long lwp, thvalid = 0; 461 462 if (!fbsd_thread_active) 463 { 464 child_ops.to_resume (ptid, step, signo); 465 return; 466 } 467 468 if (GET_PID(ptid) != -1 && step != 0) 469 { 470 resume_all = 0; 471 work_ptid = ptid; 472 } 473 else 474 { 475 resume_all = 1; 476 work_ptid = inferior_ptid; 477 } 478 479 lwp = GET_LWP (work_ptid); 480 if (lwp == 0) 481 { 482 /* check user thread */ 483 ret = td_ta_map_id2thr_p (thread_agent, GET_THREAD(work_ptid), &th); 484 if (ret) 485 error (thread_db_err_str (ret)); 486 487 /* 488 * For M:N thread, we need to tell UTS to set/unset single step 489 * flag at context switch time, the flag will be written into 490 * thread mailbox. This becauses some architecture may not have 491 * machine single step flag in ucontext, so we put the flag in mailbox, 492 * when the thread switches back, kse_switchin restores the single step 493 * state. 494 */ 495 ret = td_thr_sstep_p (&th, step); 496 if (ret) 497 error (thread_db_err_str (ret)); 498 ret = td_thr_get_info_p (&th, &ti); 499 if (ret) 500 error (thread_db_err_str (ret)); 501 thvalid = 1; 502 lwp = ti.ti_lid; 503 } 504 505 if (lwp) 506 { 507 int req = step ? PT_SETSTEP : PT_CLEARSTEP; 508 if (ptrace (req, (pid_t) lwp, (caddr_t) 1, target_signal_to_host(signo))) 509 perror_with_name ("PT_SETSTEP/PT_CLEARSTEP"); 510 } 511 512 if (!ptid_equal (last_single_step_thread, null_ptid)) 513 { 514 ret = td_ta_thr_iter_p (thread_agent, resume_thread_callback, NULL, 515 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 516 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 517 if (ret != TD_OK) 518 error ("resume error: %s", thread_db_err_str (ret)); 519 } 520 521 if (!resume_all) 522 { 523 ret = td_ta_thr_iter_p (thread_agent, suspend_thread_callback, NULL, 524 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 525 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 526 if (ret != TD_OK) 527 error ("suspend error: %s", thread_db_err_str (ret)); 528 last_single_step_thread = work_ptid; 529 } 530 else 531 last_single_step_thread = null_ptid; 532 533 if (thvalid) 534 { 535 ret = td_thr_dbresume_p (&th); 536 if (ret != TD_OK) 537 error ("resume error: %s", thread_db_err_str (ret)); 538 } 539 else 540 { 541 /* it is not necessary, put it here for completness */ 542 ret = ptrace(PT_RESUME, lwp, 0, 0); 543 } 544 545 /* now continue the process, suspended thread wont run */ 546 if (ptrace (PT_CONTINUE, proc_handle.pid , (caddr_t)1, 547 target_signal_to_host(signo))) 548 perror_with_name ("PT_CONTINUE"); 549} 550 551static ptid_t 552fbsd_thread_wait (ptid_t ptid, struct target_waitstatus *ourstatus) 553{ 554 ptid_t ret; 555 long lwp; 556 CORE_ADDR stop_pc; 557 558 ret = child_ops.to_wait (ptid, ourstatus); 559 if (GET_PID(ret) >= 0 && ourstatus->kind == TARGET_WAITKIND_STOPPED) 560 { 561 lwp = get_current_lwp (GET_PID(ret)); 562 ret = thread_from_lwp (BUILD_LWP (lwp, GET_PID (ret))); 563 if (!in_thread_list (ret)) 564 add_thread (ret); 565 /* this is a hack, if an event won't cause gdb to stop, for example, 566 SIGARLM, gdb resumes the process immediatly without setting 567 inferior_ptid to the new thread returned here, this is a bug 568 because inferior_ptid may already not exist there, and passing 569 a none existing thread to fbsd_thread_resume causes error. */ 570 if (!fbsd_thread_alive (inferior_ptid)) 571 { 572 delete_thread (inferior_ptid); 573 inferior_ptid = ret; 574 } 575 } 576 577 return (ret); 578} 579 580static int 581fbsd_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, 582 struct mem_attrib *attrib, struct target_ops *target) 583{ 584 int err; 585 586 if (target_has_execution) 587 err = child_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib, 588 target); 589 else 590 err = orig_core_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib, 591 target); 592 593 return (err); 594} 595 596static void 597fbsd_lwp_fetch_registers (int regno) 598{ 599 gregset_t gregs; 600 fpregset_t fpregs; 601 lwpid_t lwp; 602 603 if (!target_has_execution) 604 { 605 orig_core_ops.to_fetch_registers (-1); 606 return; 607 } 608 609 lwp = GET_LWP (inferior_ptid); 610 611 if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1) 612 error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno)); 613 supply_gregset (&gregs); 614 615 if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1) 616 error ("Cannot get lwp %d registers: %s\n ", lwp, safe_strerror (errno)); 617 618 supply_fpregset (&fpregs); 619} 620 621static void 622fbsd_thread_fetch_registers (int regno) 623{ 624 prgregset_t gregset; 625 prfpregset_t fpregset; 626 td_thrhandle_t th; 627 td_err_e err; 628 629 if (!IS_THREAD (inferior_ptid)) 630 { 631 fbsd_lwp_fetch_registers (regno); 632 return; 633 } 634 635 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th); 636 if (err != TD_OK) 637 error ("Cannot find thread %d: Thread ID=%ld, %s", 638 pid_to_thread_id (inferior_ptid), 639 GET_THREAD (inferior_ptid), thread_db_err_str (err)); 640 641 err = td_thr_getgregs_p (&th, gregset); 642 if (err != TD_OK) 643 error ("Cannot fetch general-purpose registers for thread %d: Thread ID=%ld, %s", 644 pid_to_thread_id (inferior_ptid), 645 GET_THREAD (inferior_ptid), thread_db_err_str (err)); 646 647 err = td_thr_getfpregs_p (&th, &fpregset); 648 if (err != TD_OK) 649 error ("Cannot get floating-point registers for thread %d: Thread ID=%ld, %s", 650 pid_to_thread_id (inferior_ptid), 651 GET_THREAD (inferior_ptid), thread_db_err_str (err)); 652 653 supply_gregset (gregset); 654 supply_fpregset (&fpregset); 655} 656 657static void 658fbsd_lwp_store_registers (int regno) 659{ 660 gregset_t gregs; 661 fpregset_t fpregs; 662 lwpid_t lwp; 663 664 /* FIXME, is it possible ? */ 665 if (!IS_LWP (inferior_ptid)) 666 { 667 child_ops.to_store_registers (regno); 668 return ; 669 } 670 671 lwp = GET_LWP (inferior_ptid); 672 if (regno != -1) 673 if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1) 674 error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno)); 675 676 fill_gregset (&gregs, regno); 677 if (ptrace (PT_SETREGS, lwp, (caddr_t) &gregs, 0) == -1) 678 error ("Cannot set lwp %d registers: %s\n", lwp, safe_strerror (errno)); 679 680 if (regno != -1) 681 if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1) 682 error ("Cannot get lwp %d float registers: %s\n", lwp, 683 safe_strerror (errno)); 684 685 fill_fpregset (&fpregs, regno); 686 if (ptrace (PT_SETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1) 687 error ("Cannot set lwp %d float registers: %s\n", lwp, 688 safe_strerror (errno)); 689} 690 691static void 692fbsd_thread_store_registers (int regno) 693{ 694 prgregset_t gregset; 695 prfpregset_t fpregset; 696 td_thrhandle_t th; 697 td_err_e err; 698 699 if (!IS_THREAD (inferior_ptid)) 700 { 701 fbsd_lwp_store_registers (regno); 702 return; 703 } 704 705 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th); 706 if (err != TD_OK) 707 error ("Cannot find thread %d: Thread ID=%ld, %s", 708 pid_to_thread_id (inferior_ptid), 709 GET_THREAD (inferior_ptid), 710 thread_db_err_str (err)); 711 712 if (regno != -1) 713 { 714 char old_value[MAX_REGISTER_SIZE]; 715 716 regcache_collect (regno, old_value); 717 err = td_thr_getgregs_p (&th, gregset); 718 if (err != TD_OK) 719 error ("%s: td_thr_getgregs %s", __func__, thread_db_err_str (err)); 720 err = td_thr_getfpregs_p (&th, &fpregset); 721 if (err != TD_OK) 722 error ("%s: td_thr_getfpgregs %s", __func__, thread_db_err_str (err)); 723 supply_register (regno, old_value); 724 } 725 726 fill_gregset (gregset, regno); 727 fill_fpregset (&fpregset, regno); 728 729 err = td_thr_setgregs_p (&th, gregset); 730 if (err != TD_OK) 731 error ("Cannot store general-purpose registers for thread %d: Thread ID=%d, %s", 732 pid_to_thread_id (inferior_ptid), GET_THREAD (inferior_ptid), 733 thread_db_err_str (err)); 734 err = td_thr_setfpregs_p (&th, &fpregset); 735 if (err != TD_OK) 736 error ("Cannot store floating-point registers for thread %d: Thread ID=%d, %s", 737 pid_to_thread_id (inferior_ptid), GET_THREAD (inferior_ptid), 738 thread_db_err_str (err)); 739} 740 741static void 742fbsd_thread_kill (void) 743{ 744 child_ops.to_kill(); 745} 746 747static int 748fbsd_thread_can_run (void) 749{ 750 return child_suppress_run; 751} 752 753static void 754fbsd_thread_create_inferior (char *exec_file, char *allargs, char **env) 755{ 756 if (fbsd_thread_present && !fbsd_thread_active) 757 push_target(&fbsd_thread_ops); 758 759 child_ops.to_create_inferior (exec_file, allargs, env); 760} 761 762static void 763fbsd_thread_post_startup_inferior (ptid_t ptid) 764{ 765 if (fbsd_thread_present && !fbsd_thread_active) 766 { 767 /* 768 * The child process is now the actual multi-threaded 769 * program. Snatch its process ID... 770 */ 771 proc_handle.pid = GET_PID (ptid); 772 td_ta_new_p (&proc_handle, &thread_agent); 773 fbsd_thread_activate(); 774 } 775} 776 777static void 778fbsd_thread_mourn_inferior (void) 779{ 780 /* 781 * Forget about the child's process ID. We shouldn't need it 782 * anymore. 783 */ 784 if (fbsd_thread_active) 785 fbsd_thread_deactivate (); 786 787 unpush_target (&fbsd_thread_ops); 788 789 child_ops.to_mourn_inferior (); 790} 791 792static void 793fbsd_core_check_lwp (bfd *abfd, asection *asect, void *obj) 794{ 795 lwpid_t lwp; 796 797 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0) 798 return; 799 800 /* already found */ 801 if (*(lwpid_t *)obj == 0) 802 return; 803 804 lwp = atoi (bfd_section_name (abfd, asect) + 5); 805 if (*(lwpid_t *)obj == lwp) 806 *(lwpid_t *)obj = 0; 807} 808 809static int 810fbsd_thread_alive (ptid_t ptid) 811{ 812 td_thrhandle_t th; 813 td_thrinfo_t ti; 814 td_err_e err; 815 gregset_t gregs; 816 lwpid_t lwp; 817 818 if (IS_THREAD (ptid)) 819 { 820 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th); 821 if (err != TD_OK) 822 return 0; 823 824 err = td_thr_get_info_p (&th, &ti); 825 if (err != TD_OK) 826 return 0; 827 828 /* A zombie thread. */ 829 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) 830 return 0; 831 832 return 1; 833 } 834 else if (GET_LWP (ptid) == 0) 835 { 836 /* we sometimes are called with lwp == 0 */ 837 return 1; 838 } 839 840 if (fbsd_thread_active) 841 { 842 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); 843 844 /* 845 * if the lwp was already mapped to user thread, don't use it 846 * directly, please use user thread id instead. 847 */ 848 if (err == TD_OK) 849 return 0; 850 } 851 852 if (!target_has_execution) 853 { 854 lwp = GET_LWP (ptid); 855 bfd_map_over_sections (core_bfd, fbsd_core_check_lwp, &lwp); 856 return (lwp == 0); 857 } 858 859 /* check lwp in kernel */ 860 return ptrace (PT_GETREGS, GET_LWP (ptid), (caddr_t)&gregs, 0) == 0; 861} 862 863static void 864fbsd_thread_files_info (struct target_ops *ignore) 865{ 866 child_ops.to_files_info (ignore); 867} 868 869static int 870find_new_threads_callback (const td_thrhandle_t *th_p, void *data) 871{ 872 td_thrinfo_t ti; 873 td_err_e err; 874 ptid_t ptid; 875 876 err = td_thr_get_info_p (th_p, &ti); 877 if (err != TD_OK) 878 error ("Cannot get thread info: %s", thread_db_err_str (err)); 879 880 /* Ignore zombie */ 881 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) 882 return 0; 883 884 ptid = BUILD_THREAD (ti.ti_tid, proc_handle.pid); 885 886 if (!in_thread_list (ptid)) 887 add_thread (ptid); 888 return 0; 889} 890 891static void 892fbsd_thread_find_new_threads (void) 893{ 894 td_err_e err; 895 896 if (!fbsd_thread_active) 897 return; 898 899 /* Iterate over all user-space threads to discover new threads. */ 900 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL, 901 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 902 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 903 if (err != TD_OK) 904 error ("Cannot find new threads: %s", thread_db_err_str (err)); 905} 906 907static char * 908fbsd_thread_pid_to_str (ptid_t ptid) 909{ 910 static char buf[64]; 911 912 if (IS_THREAD (ptid)) 913 { 914 td_thrhandle_t th; 915 td_thrinfo_t ti; 916 td_err_e err; 917 918 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th); 919 if (err != TD_OK) 920 error ("Cannot find thread, Thread ID=%ld, %s", 921 GET_THREAD (ptid), thread_db_err_str (err)); 922 923 err = td_thr_get_info_p (&th, &ti); 924 if (err != TD_OK) 925 error ("Cannot get thread info, Thread ID=%ld, %s", 926 GET_THREAD (ptid), thread_db_err_str (err)); 927 928 if (ti.ti_lid != 0) 929 { 930 snprintf (buf, sizeof (buf), "Thread %ld (LWP %d)", 931 GET_THREAD (ptid), ti.ti_lid); 932 } 933 else 934 { 935 snprintf (buf, sizeof (buf), "Thread %ld (%s)", 936 GET_THREAD (ptid), thread_db_state_str (ti.ti_state)); 937 } 938 939 return buf; 940 } 941 else if (IS_LWP (ptid)) 942 { 943 snprintf (buf, sizeof (buf), "LWP %d", (int) GET_LWP (ptid)); 944 return buf; 945 } 946 return normal_pid_to_str (ptid); 947} 948 949CORE_ADDR 950fbsd_thread_get_local_address(ptid_t ptid, struct objfile *objfile, 951 CORE_ADDR offset) 952{ 953 td_thrhandle_t th; 954 void *address; 955 CORE_ADDR lm; 956 int ret, is_library = (objfile->flags & OBJF_SHARED); 957 958 if (IS_THREAD (ptid)) 959 { 960 if (!td_thr_tls_get_addr_p) 961 error ("Cannot find thread-local interface in thread_db library."); 962 963 /* Get the address of the link map for this objfile. */ 964 lm = svr4_fetch_objfile_link_map (objfile); 965 966 /* Couldn't find link map. Bail out. */ 967 if (!lm) 968 { 969 if (is_library) 970 error ("Cannot find shared library `%s' link_map in dynamic" 971 " linker's module list", objfile->name); 972 else 973 error ("Cannot find executable file `%s' link_map in dynamic" 974 " linker's module list", objfile->name); 975 } 976 977 ret = td_ta_map_id2thr_p (thread_agent, GET_THREAD(ptid), &th); 978 979 /* get the address of the variable. */ 980 ret = td_thr_tls_get_addr_p (&th, (void *) lm, offset, &address); 981 982 if (ret != TD_OK) 983 { 984 if (is_library) 985 error ("Cannot find thread-local storage for thread %ld, " 986 "shared library %s:\n%s", 987 (long) GET_THREAD (ptid), 988 objfile->name, thread_db_err_str (ret)); 989 else 990 error ("Cannot find thread-local storage for thread %ld, " 991 "executable file %s:\n%s", 992 (long) GET_THREAD (ptid), 993 objfile->name, thread_db_err_str (ret)); 994 } 995 996 /* Cast assuming host == target. */ 997 return (CORE_ADDR) address; 998 } 999 return (0); 1000} 1001 1002static int 1003tsd_cb (thread_key_t key, void (*destructor)(void *), void *ignore) 1004{ 1005 struct minimal_symbol *ms; 1006 char *name; 1007 1008 ms = lookup_minimal_symbol_by_pc ((CORE_ADDR)destructor); 1009 if (!ms) 1010 name = "???"; 1011 else 1012 name = DEPRECATED_SYMBOL_NAME (ms); 1013 1014 printf_filtered ("Destructor %p <%s>\n", destructor, name); 1015 return 0; 1016} 1017 1018static void 1019fbsd_thread_tsd_cmd (char *exp, int from_tty) 1020{ 1021 if (fbsd_thread_active) 1022 td_ta_tsd_iter_p (thread_agent, tsd_cb, NULL); 1023} 1024 1025static int 1026ignore (CORE_ADDR addr, char *contents) 1027{ 1028 return 0; 1029} 1030 1031static void 1032fbsd_core_open (char *filename, int from_tty) 1033{ 1034 int err; 1035 1036 fbsd_thread_core = 1; 1037 1038 orig_core_ops.to_open (filename, from_tty); 1039 1040 if (fbsd_thread_present) 1041 { 1042 err = td_ta_new_p (&proc_handle, &thread_agent); 1043 if (err == TD_OK) 1044 { 1045 proc_handle.pid = elf_tdata (core_bfd)->core_pid; 1046 fbsd_thread_activate (); 1047 } 1048 else 1049 error ("fbsd_core_open: td_ta_new: %s", thread_db_err_str (err)); 1050 } 1051} 1052 1053static void 1054fbsd_core_close (int quitting) 1055{ 1056 orig_core_ops.to_close (quitting); 1057} 1058 1059static void 1060fbsd_core_detach (char *args, int from_tty) 1061{ 1062 if (fbsd_thread_active) 1063 fbsd_thread_deactivate (); 1064 unpush_target (&fbsd_thread_ops); 1065 orig_core_ops.to_detach (args, from_tty); 1066 1067 /* Clear gdb solib information and symbol file 1068 cache, so that after detach and re-attach, new_objfile 1069 hook will be called */ 1070 clear_solib(); 1071 symbol_file_clear(0); 1072} 1073 1074static void 1075fbsd_core_files_info (struct target_ops *ignore) 1076{ 1077 orig_core_ops.to_files_info (ignore); 1078} 1079 1080static void 1081init_fbsd_core_ops (void) 1082{ 1083 fbsd_core_ops.to_shortname = "FreeBSD-core"; 1084 fbsd_core_ops.to_longname = "FreeBSD core thread."; 1085 fbsd_core_ops.to_doc = "FreeBSD threads support for core files."; 1086 fbsd_core_ops.to_open = fbsd_core_open; 1087 fbsd_core_ops.to_close = fbsd_core_close; 1088 fbsd_core_ops.to_attach = 0; 1089 fbsd_core_ops.to_post_attach = 0; 1090 fbsd_core_ops.to_detach = fbsd_core_detach; 1091 /* fbsd_core_ops.to_resume = 0; */ 1092 /* fbsd_core_ops.to_wait = 0; */ 1093 fbsd_core_ops.to_fetch_registers = fbsd_thread_fetch_registers; 1094 /* fbsd_core_ops.to_store_registers = 0; */ 1095 /* fbsd_core_ops.to_prepare_to_store = 0; */ 1096 fbsd_core_ops.to_xfer_memory = fbsd_thread_xfer_memory; 1097 fbsd_core_ops.to_files_info = fbsd_core_files_info; 1098 fbsd_core_ops.to_insert_breakpoint = ignore; 1099 fbsd_core_ops.to_remove_breakpoint = ignore; 1100 /* fbsd_core_ops.to_lookup_symbol = 0; */ 1101 fbsd_core_ops.to_create_inferior = fbsd_thread_create_inferior; 1102 fbsd_core_ops.to_stratum = core_stratum; 1103 fbsd_core_ops.to_has_all_memory = 0; 1104 fbsd_core_ops.to_has_memory = 1; 1105 fbsd_core_ops.to_has_stack = 1; 1106 fbsd_core_ops.to_has_registers = 1; 1107 fbsd_core_ops.to_has_execution = 0; 1108 fbsd_core_ops.to_has_thread_control = tc_none; 1109 fbsd_core_ops.to_thread_alive = fbsd_thread_alive; 1110 fbsd_core_ops.to_pid_to_str = fbsd_thread_pid_to_str; 1111 fbsd_core_ops.to_find_new_threads = fbsd_thread_find_new_threads; 1112 fbsd_core_ops.to_sections = 0; 1113 fbsd_core_ops.to_sections_end = 0; 1114 fbsd_core_ops.to_magic = OPS_MAGIC; 1115} 1116 1117static void 1118init_fbsd_thread_ops (void) 1119{ 1120 fbsd_thread_ops.to_shortname = "freebsd-threads"; 1121 fbsd_thread_ops.to_longname = "FreeBSD multithreaded child process."; 1122 fbsd_thread_ops.to_doc = "FreeBSD threads support."; 1123 fbsd_thread_ops.to_attach = fbsd_thread_attach; 1124 fbsd_thread_ops.to_detach = fbsd_thread_detach; 1125 fbsd_thread_ops.to_post_attach = fbsd_thread_post_attach; 1126 fbsd_thread_ops.to_resume = fbsd_thread_resume; 1127 fbsd_thread_ops.to_wait = fbsd_thread_wait; 1128 fbsd_thread_ops.to_fetch_registers = fbsd_thread_fetch_registers; 1129 fbsd_thread_ops.to_store_registers = fbsd_thread_store_registers; 1130 fbsd_thread_ops.to_xfer_memory = fbsd_thread_xfer_memory; 1131 fbsd_thread_ops.to_files_info = fbsd_thread_files_info; 1132 fbsd_thread_ops.to_kill = fbsd_thread_kill; 1133 fbsd_thread_ops.to_create_inferior = fbsd_thread_create_inferior; 1134 fbsd_thread_ops.to_post_startup_inferior = fbsd_thread_post_startup_inferior; 1135 fbsd_thread_ops.to_mourn_inferior = fbsd_thread_mourn_inferior; 1136 fbsd_thread_ops.to_can_run = fbsd_thread_can_run; 1137 fbsd_thread_ops.to_thread_alive = fbsd_thread_alive; 1138 fbsd_thread_ops.to_find_new_threads = fbsd_thread_find_new_threads; 1139 fbsd_thread_ops.to_pid_to_str = fbsd_thread_pid_to_str; 1140 fbsd_thread_ops.to_stratum = thread_stratum; 1141 fbsd_thread_ops.to_has_thread_control = tc_none; 1142 fbsd_thread_ops.to_has_all_memory = 1; 1143 fbsd_thread_ops.to_has_memory = 1; 1144 fbsd_thread_ops.to_has_stack = 1; 1145 fbsd_thread_ops.to_has_registers = 1; 1146 fbsd_thread_ops.to_has_execution = 1; 1147 fbsd_thread_ops.to_insert_breakpoint = memory_insert_breakpoint; 1148 fbsd_thread_ops.to_remove_breakpoint = memory_remove_breakpoint; 1149 fbsd_thread_ops.to_get_thread_local_address = fbsd_thread_get_local_address; 1150 fbsd_thread_ops.to_magic = OPS_MAGIC; 1151} 1152 1153static int 1154thread_db_load (void) 1155{ 1156 void *handle; 1157 td_err_e err; 1158 1159 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW); 1160 if (handle == NULL) 1161 return 0; 1162 1163#define resolve(X) \ 1164 if (!(X##_p = dlsym (handle, #X))) \ 1165 return 0; 1166 1167 resolve(td_init); 1168 resolve(td_ta_new); 1169 resolve(td_ta_delete); 1170 resolve(td_ta_map_id2thr); 1171 resolve(td_ta_map_lwp2thr); 1172 resolve(td_ta_thr_iter); 1173 resolve(td_thr_get_info); 1174 resolve(td_thr_getfpregs); 1175 resolve(td_thr_getgregs); 1176 resolve(td_thr_setfpregs); 1177 resolve(td_thr_setgregs); 1178 resolve(td_thr_sstep); 1179 resolve(td_ta_tsd_iter); 1180 resolve(td_thr_dbsuspend); 1181 resolve(td_thr_dbresume); 1182 resolve(td_thr_tls_get_addr); 1183 1184 /* Initialize the library. */ 1185 err = td_init_p (); 1186 if (err != TD_OK) 1187 { 1188 warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err)); 1189 return 0; 1190 } 1191 1192 return 1; 1193} 1194 1195/* we suppress the call to add_target of core_ops in corelow because 1196 if there are two targets in the stratum core_stratum, find_core_target 1197 won't know which one to return. see corelow.c for an additonal 1198 comment on coreops_suppress_target. */ 1199 1200int coreops_suppress_target = 1; 1201 1202void 1203_initialize_thread_db (void) 1204{ 1205 init_fbsd_thread_ops (); 1206 init_fbsd_core_ops (); 1207 1208 if (thread_db_load ()) 1209 { 1210 add_target (&fbsd_thread_ops); 1211 1212 /* "thread tsd" command */ 1213 add_cmd ("tsd", class_run, fbsd_thread_tsd_cmd, 1214 "Show the thread-specific data keys and destructors " 1215 "for the process.\n", 1216 &thread_cmd_list); 1217 1218 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops)); 1219 memcpy (&core_ops, &fbsd_core_ops, sizeof (struct target_ops)); 1220 add_target (&core_ops); 1221 1222 /* Add ourselves to objfile event chain. */ 1223 target_new_objfile_chain = target_new_objfile_hook; 1224 target_new_objfile_hook = fbsd_thread_new_objfile; 1225 1226 child_suppress_run = 1; 1227 } 1228 else 1229 { 1230 fprintf_unfiltered (gdb_stderr, 1231 "[GDB will not be able to debug user-mode threads: %s]\n", dlerror()); 1232 1233 /* allow the user to debug non-threaded core files */ 1234 add_target (&core_ops); 1235 } 1236} 1237 1238/* proc service functions */ 1239void 1240ps_plog (const char *fmt, ...) 1241{ 1242 va_list args; 1243 1244 va_start (args, fmt); 1245 vfprintf_filtered (gdb_stderr, fmt, args); 1246 va_end (args); 1247} 1248 1249ps_err_e 1250ps_pglobal_lookup (struct ps_prochandle *ph, const char *obj, 1251 const char *name, psaddr_t *sym_addr) 1252{ 1253 struct minimal_symbol *ms; 1254 1255 ms = lookup_minimal_symbol (name, NULL, NULL); 1256 if (ms == NULL) 1257 return PS_NOSYM; 1258 1259 *sym_addr = (psaddr_t) SYMBOL_VALUE_ADDRESS (ms); 1260 return PS_OK; 1261} 1262 1263ps_err_e 1264ps_pread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len) 1265{ 1266 int err = target_read_memory ((CORE_ADDR) addr, buf, len); 1267 return (err == 0 ? PS_OK : PS_ERR); 1268} 1269 1270ps_err_e 1271ps_pwrite (struct ps_prochandle *ph, psaddr_t addr, const void *buf, 1272 size_t len) 1273{ 1274 int err = target_write_memory ((CORE_ADDR) addr, (void *)buf, len); 1275 return (err == 0 ? PS_OK : PS_ERR); 1276} 1277 1278ps_err_e 1279ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset) 1280{ 1281 struct cleanup *old_chain; 1282 1283 old_chain = save_inferior_ptid (); 1284 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1285 target_fetch_registers (-1); 1286 fill_gregset (gregset, -1); 1287 do_cleanups (old_chain); 1288 return PS_OK; 1289} 1290 1291ps_err_e 1292ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset) 1293{ 1294 struct cleanup *old_chain; 1295 1296 old_chain = save_inferior_ptid (); 1297 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1298 supply_gregset (gregset); 1299 target_store_registers (-1); 1300 do_cleanups (old_chain); 1301 return PS_OK; 1302} 1303 1304ps_err_e 1305ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, prfpregset_t *fpregset) 1306{ 1307 struct cleanup *old_chain; 1308 1309 old_chain = save_inferior_ptid (); 1310 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1311 target_fetch_registers (-1); 1312 fill_fpregset (fpregset, -1); 1313 do_cleanups (old_chain); 1314 return PS_OK; 1315} 1316 1317ps_err_e 1318ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, 1319 const prfpregset_t *fpregset) 1320{ 1321 struct cleanup *old_chain; 1322 1323 old_chain = save_inferior_ptid (); 1324 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid)); 1325 supply_fpregset (fpregset); 1326 target_store_registers (-1); 1327 do_cleanups (old_chain); 1328 return PS_OK; 1329} 1330 1331ps_err_e 1332ps_lstop(struct ps_prochandle *ph, lwpid_t lwpid) 1333{ 1334 if (ptrace (PT_SUSPEND, lwpid, 0, 0) == -1) 1335 return PS_ERR; 1336 return PS_OK; 1337} 1338 1339ps_err_e 1340ps_lcontinue(struct ps_prochandle *ph, lwpid_t lwpid) 1341{ 1342 if (ptrace (PT_RESUME, lwpid, 0, 0) == -1) 1343 return PS_ERR; 1344 return PS_OK; 1345} 1346