| 1 | /* BSD user-level threads support. |
| 2 | |
| 3 | Copyright (C) 2005, 2007, 2008, 2009 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 3 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, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "gdbcore.h" |
| 22 | #include "gdbthread.h" |
| 23 | #include "inferior.h" |
| 24 | #include "objfiles.h" |
| 25 | #include "observer.h" |
| 26 | #include "regcache.h" |
| 27 | #include "solib.h" |
| 28 | #include "solist.h" |
| 29 | #include "symfile.h" |
| 30 | #include "target.h" |
| 31 | |
| 32 | #include "gdb_assert.h" |
| 33 | #include "gdb_obstack.h" |
| 34 | |
| 35 | #include "bsd-uthread.h" |
| 36 | |
| 37 | /* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target. */ |
| 38 | static struct target_ops *bsd_uthread_ops_hack; |
| 39 | \f |
| 40 | |
| 41 | /* Architecture-specific operations. */ |
| 42 | |
| 43 | /* Per-architecture data key. */ |
| 44 | static struct gdbarch_data *bsd_uthread_data; |
| 45 | |
| 46 | struct bsd_uthread_ops |
| 47 | { |
| 48 | /* Supply registers for an inactive thread to a register cache. */ |
| 49 | void (*supply_uthread)(struct regcache *, int, CORE_ADDR); |
| 50 | |
| 51 | /* Collect registers for an inactive thread from a register cache. */ |
| 52 | void (*collect_uthread)(const struct regcache *, int, CORE_ADDR); |
| 53 | }; |
| 54 | |
| 55 | static void * |
| 56 | bsd_uthread_init (struct obstack *obstack) |
| 57 | { |
| 58 | struct bsd_uthread_ops *ops; |
| 59 | |
| 60 | ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops); |
| 61 | return ops; |
| 62 | } |
| 63 | |
| 64 | /* Set the function that supplies registers from an inactive thread |
| 65 | for architecture GDBARCH to SUPPLY_UTHREAD. */ |
| 66 | |
| 67 | void |
| 68 | bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch, |
| 69 | void (*supply_uthread) (struct regcache *, |
| 70 | int, CORE_ADDR)) |
| 71 | { |
| 72 | struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data); |
| 73 | ops->supply_uthread = supply_uthread; |
| 74 | } |
| 75 | |
| 76 | /* Set the function that collects registers for an inactive thread for |
| 77 | architecture GDBARCH to SUPPLY_UTHREAD. */ |
| 78 | |
| 79 | void |
| 80 | bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch, |
| 81 | void (*collect_uthread) (const struct regcache *, |
| 82 | int, CORE_ADDR)) |
| 83 | { |
| 84 | struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data); |
| 85 | ops->collect_uthread = collect_uthread; |
| 86 | } |
| 87 | |
| 88 | /* Magic number to help recognize a valid thread structure. */ |
| 89 | #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115 |
| 90 | |
| 91 | /* Check whether the thread structure at ADDR is valid. */ |
| 92 | |
| 93 | static void |
| 94 | bsd_uthread_check_magic (CORE_ADDR addr) |
| 95 | { |
| 96 | ULONGEST magic = read_memory_unsigned_integer (addr, 4); |
| 97 | |
| 98 | if (magic != BSD_UTHREAD_PTHREAD_MAGIC) |
| 99 | error (_("Bad magic")); |
| 100 | } |
| 101 | |
| 102 | /* Thread states. */ |
| 103 | #define BSD_UTHREAD_PS_RUNNING 0 |
| 104 | #define BSD_UTHREAD_PS_DEAD 18 |
| 105 | |
| 106 | /* Address of the pointer to the the thread structure for the running |
| 107 | thread. */ |
| 108 | static CORE_ADDR bsd_uthread_thread_run_addr; |
| 109 | |
| 110 | /* Address of the list of all threads. */ |
| 111 | static CORE_ADDR bsd_uthread_thread_list_addr; |
| 112 | |
| 113 | /* Offsets of various "interesting" bits in the thread structure. */ |
| 114 | static int bsd_uthread_thread_state_offset = -1; |
| 115 | static int bsd_uthread_thread_next_offset = -1; |
| 116 | static int bsd_uthread_thread_ctx_offset; |
| 117 | |
| 118 | /* Name of shared threads library. */ |
| 119 | static const char *bsd_uthread_solib_name; |
| 120 | |
| 121 | /* Non-zero if the thread startum implemented by this module is active. */ |
| 122 | static int bsd_uthread_active; |
| 123 | |
| 124 | static CORE_ADDR |
| 125 | bsd_uthread_lookup_address (const char *name, struct objfile *objfile) |
| 126 | { |
| 127 | struct minimal_symbol *sym; |
| 128 | |
| 129 | sym = lookup_minimal_symbol (name, NULL, objfile); |
| 130 | if (sym) |
| 131 | return SYMBOL_VALUE_ADDRESS (sym); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int |
| 137 | bsd_uthread_lookup_offset (const char *name, struct objfile *objfile) |
| 138 | { |
| 139 | CORE_ADDR addr; |
| 140 | |
| 141 | addr = bsd_uthread_lookup_address (name, objfile); |
| 142 | if (addr == 0) |
| 143 | return 0; |
| 144 | |
| 145 | return read_memory_unsigned_integer (addr, 4); |
| 146 | } |
| 147 | |
| 148 | static CORE_ADDR |
| 149 | bsd_uthread_read_memory_address (CORE_ADDR addr) |
| 150 | { |
| 151 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
| 152 | return read_memory_typed_address (addr, ptr_type); |
| 153 | } |
| 154 | |
| 155 | /* If OBJFILE contains the symbols corresponding to one of the |
| 156 | supported user-level threads libraries, activate the thread stratum |
| 157 | implemented by this module. */ |
| 158 | |
| 159 | static int |
| 160 | bsd_uthread_activate (struct objfile *objfile) |
| 161 | { |
| 162 | struct gdbarch *gdbarch = current_gdbarch; |
| 163 | struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data); |
| 164 | |
| 165 | /* Skip if the thread stratum has already been activated. */ |
| 166 | if (bsd_uthread_active) |
| 167 | return 0; |
| 168 | |
| 169 | /* There's no point in enabling this module if no |
| 170 | architecture-specific operations are provided. */ |
| 171 | if (!ops->supply_uthread) |
| 172 | return 0; |
| 173 | |
| 174 | bsd_uthread_thread_run_addr = |
| 175 | bsd_uthread_lookup_address ("_thread_run", objfile); |
| 176 | if (bsd_uthread_thread_run_addr == 0) |
| 177 | return 0; |
| 178 | |
| 179 | bsd_uthread_thread_list_addr = |
| 180 | bsd_uthread_lookup_address ("_thread_list", objfile); |
| 181 | if (bsd_uthread_thread_list_addr == 0) |
| 182 | return 0; |
| 183 | |
| 184 | bsd_uthread_thread_state_offset = |
| 185 | bsd_uthread_lookup_offset ("_thread_state_offset", objfile); |
| 186 | if (bsd_uthread_thread_state_offset == 0) |
| 187 | return 0; |
| 188 | |
| 189 | bsd_uthread_thread_next_offset = |
| 190 | bsd_uthread_lookup_offset ("_thread_next_offset", objfile); |
| 191 | if (bsd_uthread_thread_next_offset == 0) |
| 192 | return 0; |
| 193 | |
| 194 | bsd_uthread_thread_ctx_offset = |
| 195 | bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile); |
| 196 | |
| 197 | push_target (bsd_uthread_ops_hack); |
| 198 | bsd_uthread_active = 1; |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | /* Cleanup due to deactivation. */ |
| 203 | |
| 204 | static void |
| 205 | bsd_uthread_close (int quitting) |
| 206 | { |
| 207 | bsd_uthread_active = 0; |
| 208 | bsd_uthread_thread_run_addr = 0; |
| 209 | bsd_uthread_thread_list_addr = 0; |
| 210 | bsd_uthread_thread_state_offset = 0; |
| 211 | bsd_uthread_thread_next_offset = 0; |
| 212 | bsd_uthread_thread_ctx_offset = 0; |
| 213 | bsd_uthread_solib_name = NULL; |
| 214 | } |
| 215 | |
| 216 | /* Deactivate the thread stratum implemented by this module. */ |
| 217 | |
| 218 | static void |
| 219 | bsd_uthread_deactivate (void) |
| 220 | { |
| 221 | /* Skip if the thread stratum has already been deactivated. */ |
| 222 | if (!bsd_uthread_active) |
| 223 | return; |
| 224 | |
| 225 | unpush_target (bsd_uthread_ops_hack); |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | bsd_uthread_inferior_created (struct target_ops *ops, int from_tty) |
| 230 | { |
| 231 | bsd_uthread_activate (NULL); |
| 232 | } |
| 233 | |
| 234 | /* Likely candidates for the threads library. */ |
| 235 | static const char *bsd_uthread_solib_names[] = |
| 236 | { |
| 237 | "/usr/lib/libc_r.so", /* FreeBSD */ |
| 238 | "/usr/lib/libpthread.so", /* OpenBSD */ |
| 239 | NULL |
| 240 | }; |
| 241 | |
| 242 | void |
| 243 | bsd_uthread_solib_loaded (struct so_list *so) |
| 244 | { |
| 245 | const char **names = bsd_uthread_solib_names; |
| 246 | |
| 247 | for (names = bsd_uthread_solib_names; *names; names++) |
| 248 | { |
| 249 | if (strncmp (so->so_original_name, *names, strlen (*names)) == 0) |
| 250 | { |
| 251 | solib_read_symbols (so, so->from_tty); |
| 252 | |
| 253 | if (bsd_uthread_activate (so->objfile)) |
| 254 | { |
| 255 | bsd_uthread_solib_name = so->so_original_name; |
| 256 | return; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void |
| 263 | bsd_uthread_solib_unloaded (struct so_list *so) |
| 264 | { |
| 265 | if (!bsd_uthread_solib_name) |
| 266 | return; |
| 267 | |
| 268 | if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0) |
| 269 | bsd_uthread_deactivate (); |
| 270 | } |
| 271 | |
| 272 | static void |
| 273 | bsd_uthread_mourn_inferior (struct target_ops *ops) |
| 274 | { |
| 275 | struct target_ops *beneath = find_target_beneath (bsd_uthread_ops_hack); |
| 276 | beneath->to_mourn_inferior (beneath); |
| 277 | bsd_uthread_deactivate (); |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | bsd_uthread_fetch_registers (struct regcache *regcache, int regnum) |
| 282 | { |
| 283 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 284 | struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data); |
| 285 | CORE_ADDR addr = ptid_get_tid (inferior_ptid); |
| 286 | CORE_ADDR active_addr; |
| 287 | |
| 288 | /* Always fetch the appropriate registers from the layer beneath. */ |
| 289 | find_target_beneath (bsd_uthread_ops_hack)->to_fetch_registers (regcache, regnum); |
| 290 | |
| 291 | /* FIXME: That might have gotten us more than we asked for. Make |
| 292 | sure we overwrite all relevant registers with values from the |
| 293 | thread structure. This can go once we fix the underlying target. */ |
| 294 | regnum = -1; |
| 295 | |
| 296 | active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr); |
| 297 | if (addr != 0 && addr != active_addr) |
| 298 | { |
| 299 | bsd_uthread_check_magic (addr); |
| 300 | ops->supply_uthread (regcache, regnum, |
| 301 | addr + bsd_uthread_thread_ctx_offset); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | bsd_uthread_store_registers (struct regcache *regcache, int regnum) |
| 307 | { |
| 308 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 309 | struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data); |
| 310 | CORE_ADDR addr = ptid_get_tid (inferior_ptid); |
| 311 | CORE_ADDR active_addr; |
| 312 | |
| 313 | active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr); |
| 314 | if (addr != 0 && addr != active_addr) |
| 315 | { |
| 316 | bsd_uthread_check_magic (addr); |
| 317 | ops->collect_uthread (regcache, regnum, |
| 318 | addr + bsd_uthread_thread_ctx_offset); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | /* Updating the thread that is currently running; pass the |
| 323 | request to the layer beneath. */ |
| 324 | find_target_beneath (bsd_uthread_ops_hack)->to_store_registers (regcache, regnum); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* FIXME: This function is only there because otherwise GDB tries to |
| 329 | invoke deprecate_xfer_memory. */ |
| 330 | |
| 331 | static LONGEST |
| 332 | bsd_uthread_xfer_partial (struct target_ops *ops, enum target_object object, |
| 333 | const char *annex, gdb_byte *readbuf, |
| 334 | const gdb_byte *writebuf, |
| 335 | ULONGEST offset, LONGEST len) |
| 336 | { |
| 337 | gdb_assert (ops->beneath->to_xfer_partial); |
| 338 | return ops->beneath->to_xfer_partial (ops->beneath, object, annex, readbuf, |
| 339 | writebuf, offset, len); |
| 340 | } |
| 341 | |
| 342 | static ptid_t |
| 343 | bsd_uthread_wait (struct target_ops *ops, |
| 344 | ptid_t ptid, struct target_waitstatus *status) |
| 345 | { |
| 346 | CORE_ADDR addr; |
| 347 | struct target_ops *beneath = find_target_beneath (bsd_uthread_ops_hack); |
| 348 | |
| 349 | /* Pass the request to the layer beneath. */ |
| 350 | ptid = beneath->to_wait (beneath, ptid, status); |
| 351 | |
| 352 | /* If the process is no longer alive, there's no point in figuring |
| 353 | out the thread ID. It will fail anyway. */ |
| 354 | if (status->kind == TARGET_WAITKIND_SIGNALLED |
| 355 | || status->kind == TARGET_WAITKIND_EXITED) |
| 356 | return ptid; |
| 357 | |
| 358 | /* Fetch the corresponding thread ID, and augment the returned |
| 359 | process ID with it. */ |
| 360 | addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr); |
| 361 | if (addr != 0) |
| 362 | { |
| 363 | gdb_byte buf[4]; |
| 364 | |
| 365 | /* FIXME: For executables linked statically with the threads |
| 366 | library, we end up here before the program has actually been |
| 367 | executed. In that case ADDR will be garbage since it has |
| 368 | been read from the wrong virtual memory image. */ |
| 369 | if (target_read_memory (addr, buf, 4) == 0) |
| 370 | { |
| 371 | ULONGEST magic = extract_unsigned_integer (buf, 4); |
| 372 | if (magic == BSD_UTHREAD_PTHREAD_MAGIC) |
| 373 | ptid = ptid_build (ptid_get_pid (ptid), 0, addr); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a |
| 378 | ptid with tid set, then ptid is still the initial thread of |
| 379 | the process. Notify GDB core about it. */ |
| 380 | if (ptid_get_tid (inferior_ptid) == 0 |
| 381 | && ptid_get_tid (ptid) != 0 && !in_thread_list (ptid)) |
| 382 | thread_change_ptid (inferior_ptid, ptid); |
| 383 | |
| 384 | /* Don't let the core see a ptid without a corresponding thread. */ |
| 385 | if (!in_thread_list (ptid) || is_exited (ptid)) |
| 386 | add_thread (ptid); |
| 387 | |
| 388 | return ptid; |
| 389 | } |
| 390 | |
| 391 | static void |
| 392 | bsd_uthread_resume (ptid_t ptid, int step, enum target_signal sig) |
| 393 | { |
| 394 | /* Pass the request to the layer beneath. */ |
| 395 | find_target_beneath (bsd_uthread_ops_hack)->to_resume (ptid, step, sig); |
| 396 | } |
| 397 | |
| 398 | static int |
| 399 | bsd_uthread_thread_alive (ptid_t ptid) |
| 400 | { |
| 401 | CORE_ADDR addr = ptid_get_tid (inferior_ptid); |
| 402 | |
| 403 | if (addr != 0) |
| 404 | { |
| 405 | int offset = bsd_uthread_thread_state_offset; |
| 406 | ULONGEST state; |
| 407 | |
| 408 | bsd_uthread_check_magic (addr); |
| 409 | |
| 410 | state = read_memory_unsigned_integer (addr + offset, 4); |
| 411 | if (state == BSD_UTHREAD_PS_DEAD) |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | return find_target_beneath (bsd_uthread_ops_hack)->to_thread_alive (ptid); |
| 416 | } |
| 417 | |
| 418 | static void |
| 419 | bsd_uthread_find_new_threads (void) |
| 420 | { |
| 421 | pid_t pid = ptid_get_pid (inferior_ptid); |
| 422 | int offset = bsd_uthread_thread_next_offset; |
| 423 | CORE_ADDR addr; |
| 424 | |
| 425 | addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr); |
| 426 | while (addr != 0) |
| 427 | { |
| 428 | ptid_t ptid = ptid_build (pid, 0, addr); |
| 429 | |
| 430 | if (!in_thread_list (ptid) || is_exited (ptid)) |
| 431 | { |
| 432 | /* If INFERIOR_PTID doesn't have a tid member yet, then ptid |
| 433 | is still the initial thread of the process. Notify GDB |
| 434 | core about it. */ |
| 435 | if (ptid_get_tid (inferior_ptid) == 0) |
| 436 | thread_change_ptid (inferior_ptid, ptid); |
| 437 | else |
| 438 | add_thread (ptid); |
| 439 | } |
| 440 | |
| 441 | addr = bsd_uthread_read_memory_address (addr + offset); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /* Possible states a thread can be in. */ |
| 446 | static char *bsd_uthread_state[] = |
| 447 | { |
| 448 | "RUNNING", |
| 449 | "SIGTHREAD", |
| 450 | "MUTEX_WAIT", |
| 451 | "COND_WAIT", |
| 452 | "FDLR_WAIT", |
| 453 | "FDLW_WAIT", |
| 454 | "FDR_WAIT", |
| 455 | "FDW_WAIT", |
| 456 | "FILE_WAIT", |
| 457 | "POLL_WAIT", |
| 458 | "SELECT_WAIT", |
| 459 | "SLEEP_WAIT", |
| 460 | "WAIT_WAIT", |
| 461 | "SIGSUSPEND", |
| 462 | "SIGWAIT", |
| 463 | "SPINBLOCK", |
| 464 | "JOIN", |
| 465 | "SUSPENDED", |
| 466 | "DEAD", |
| 467 | "DEADLOCK" |
| 468 | }; |
| 469 | |
| 470 | /* Return a string describing th state of the thread specified by |
| 471 | INFO. */ |
| 472 | |
| 473 | static char * |
| 474 | bsd_uthread_extra_thread_info (struct thread_info *info) |
| 475 | { |
| 476 | CORE_ADDR addr = ptid_get_tid (info->ptid); |
| 477 | |
| 478 | if (addr != 0) |
| 479 | { |
| 480 | int offset = bsd_uthread_thread_state_offset; |
| 481 | ULONGEST state; |
| 482 | |
| 483 | state = read_memory_unsigned_integer (addr + offset, 4); |
| 484 | if (state < ARRAY_SIZE (bsd_uthread_state)) |
| 485 | return bsd_uthread_state[state]; |
| 486 | } |
| 487 | |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | static char * |
| 492 | bsd_uthread_pid_to_str (struct target_ops *ops, ptid_t ptid) |
| 493 | { |
| 494 | if (ptid_get_tid (ptid) != 0) |
| 495 | { |
| 496 | static char buf[64]; |
| 497 | |
| 498 | xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx", |
| 499 | ptid_get_pid (ptid), ptid_get_tid (ptid)); |
| 500 | return buf; |
| 501 | } |
| 502 | |
| 503 | return normal_pid_to_str (ptid); |
| 504 | } |
| 505 | |
| 506 | struct target_ops * |
| 507 | bsd_uthread_target (void) |
| 508 | { |
| 509 | struct target_ops *t = XZALLOC (struct target_ops); |
| 510 | |
| 511 | t->to_shortname = "bsd-uthreads"; |
| 512 | t->to_longname = "BSD user-level threads"; |
| 513 | t->to_doc = "BSD user-level threads"; |
| 514 | t->to_close = bsd_uthread_close; |
| 515 | t->to_mourn_inferior = bsd_uthread_mourn_inferior; |
| 516 | t->to_fetch_registers = bsd_uthread_fetch_registers; |
| 517 | t->to_store_registers = bsd_uthread_store_registers; |
| 518 | t->to_xfer_partial = bsd_uthread_xfer_partial; |
| 519 | t->to_wait = bsd_uthread_wait; |
| 520 | t->to_resume = bsd_uthread_resume; |
| 521 | t->to_thread_alive = bsd_uthread_thread_alive; |
| 522 | t->to_find_new_threads = bsd_uthread_find_new_threads; |
| 523 | t->to_extra_thread_info = bsd_uthread_extra_thread_info; |
| 524 | t->to_pid_to_str = bsd_uthread_pid_to_str; |
| 525 | t->to_stratum = thread_stratum; |
| 526 | t->to_magic = OPS_MAGIC; |
| 527 | bsd_uthread_ops_hack = t; |
| 528 | |
| 529 | return t; |
| 530 | } |
| 531 | |
| 532 | void |
| 533 | _initialize_bsd_uthread (void) |
| 534 | { |
| 535 | add_target (bsd_uthread_target ()); |
| 536 | |
| 537 | bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init); |
| 538 | |
| 539 | observer_attach_inferior_created (bsd_uthread_inferior_created); |
| 540 | observer_attach_solib_loaded (bsd_uthread_solib_loaded); |
| 541 | observer_attach_solib_unloaded (bsd_uthread_solib_unloaded); |
| 542 | } |