1 /* BSD user-level threads support.
3 Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "gdbthread.h"
32 #include "gdb_assert.h"
33 #include "gdb_obstack.h"
35 #include "bsd-uthread.h"
37 /* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target. */
38 static struct target_ops
*bsd_uthread_ops_hack
;
41 /* Architecture-specific operations. */
43 /* Per-architecture data key. */
44 static struct gdbarch_data
*bsd_uthread_data
;
46 struct bsd_uthread_ops
48 /* Supply registers for an inactive thread to a register cache. */
49 void (*supply_uthread
)(struct regcache
*, int, CORE_ADDR
);
51 /* Collect registers for an inactive thread from a register cache. */
52 void (*collect_uthread
)(const struct regcache
*, int, CORE_ADDR
);
56 bsd_uthread_init (struct obstack
*obstack
)
58 struct bsd_uthread_ops
*ops
;
60 ops
= OBSTACK_ZALLOC (obstack
, struct bsd_uthread_ops
);
64 /* Set the function that supplies registers from an inactive thread
65 for architecture GDBARCH to SUPPLY_UTHREAD. */
68 bsd_uthread_set_supply_uthread (struct gdbarch
*gdbarch
,
69 void (*supply_uthread
) (struct regcache
*,
72 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
73 ops
->supply_uthread
= supply_uthread
;
76 /* Set the function that collects registers for an inactive thread for
77 architecture GDBARCH to SUPPLY_UTHREAD. */
80 bsd_uthread_set_collect_uthread (struct gdbarch
*gdbarch
,
81 void (*collect_uthread
) (const struct regcache
*,
84 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
85 ops
->collect_uthread
= collect_uthread
;
88 /* Magic number to help recognize a valid thread structure. */
89 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
91 /* Check whether the thread structure at ADDR is valid. */
94 bsd_uthread_check_magic (CORE_ADDR addr
)
96 ULONGEST magic
= read_memory_unsigned_integer (addr
, 4);
98 if (magic
!= BSD_UTHREAD_PTHREAD_MAGIC
)
99 error (_("Bad magic"));
103 #define BSD_UTHREAD_PS_RUNNING 0
104 #define BSD_UTHREAD_PS_DEAD 18
106 /* Address of the pointer to the the thread structure for the running
108 static CORE_ADDR bsd_uthread_thread_run_addr
;
110 /* Address of the list of all threads. */
111 static CORE_ADDR bsd_uthread_thread_list_addr
;
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
;
118 /* Name of shared threads library. */
119 static const char *bsd_uthread_solib_name
;
121 /* Non-zero if the thread startum implemented by this module is active. */
122 static int bsd_uthread_active
;
125 bsd_uthread_lookup_address (const char *name
, struct objfile
*objfile
)
127 struct minimal_symbol
*sym
;
129 sym
= lookup_minimal_symbol (name
, NULL
, objfile
);
131 return SYMBOL_VALUE_ADDRESS (sym
);
137 bsd_uthread_lookup_offset (const char *name
, struct objfile
*objfile
)
141 addr
= bsd_uthread_lookup_address (name
, objfile
);
145 return read_memory_unsigned_integer (addr
, 4);
149 bsd_uthread_read_memory_address (CORE_ADDR addr
)
151 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
152 return read_memory_typed_address (addr
, ptr_type
);
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. */
160 bsd_uthread_activate (struct objfile
*objfile
)
162 struct gdbarch
*gdbarch
= current_gdbarch
;
163 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
165 /* Skip if the thread stratum has already been activated. */
166 if (bsd_uthread_active
)
169 /* There's no point in enabling this module if no
170 architecture-specific operations are provided. */
171 if (!ops
->supply_uthread
)
174 bsd_uthread_thread_run_addr
=
175 bsd_uthread_lookup_address ("_thread_run", objfile
);
176 if (bsd_uthread_thread_run_addr
== 0)
179 bsd_uthread_thread_list_addr
=
180 bsd_uthread_lookup_address ("_thread_list", objfile
);
181 if (bsd_uthread_thread_list_addr
== 0)
184 bsd_uthread_thread_state_offset
=
185 bsd_uthread_lookup_offset ("_thread_state_offset", objfile
);
186 if (bsd_uthread_thread_state_offset
== 0)
189 bsd_uthread_thread_next_offset
=
190 bsd_uthread_lookup_offset ("_thread_next_offset", objfile
);
191 if (bsd_uthread_thread_next_offset
== 0)
194 bsd_uthread_thread_ctx_offset
=
195 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile
);
197 push_target (bsd_uthread_ops_hack
);
198 bsd_uthread_active
= 1;
202 /* Cleanup due to deactivation. */
205 bsd_uthread_close (int quitting
)
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
;
216 /* Deactivate the thread stratum implemented by this module. */
219 bsd_uthread_deactivate (void)
221 /* Skip if the thread stratum has already been deactivated. */
222 if (!bsd_uthread_active
)
225 unpush_target (bsd_uthread_ops_hack
);
229 bsd_uthread_inferior_created (struct target_ops
*ops
, int from_tty
)
231 bsd_uthread_activate (NULL
);
234 /* Likely candidates for the threads library. */
235 static const char *bsd_uthread_solib_names
[] =
237 "/usr/lib/libc_r.so", /* FreeBSD */
238 "/usr/lib/libpthread.so", /* OpenBSD */
243 bsd_uthread_solib_loaded (struct so_list
*so
)
245 const char **names
= bsd_uthread_solib_names
;
247 for (names
= bsd_uthread_solib_names
; *names
; names
++)
249 if (strncmp (so
->so_original_name
, *names
, strlen (*names
)) == 0)
251 solib_read_symbols (so
, so
->from_tty
);
253 if (bsd_uthread_activate (so
->objfile
))
255 bsd_uthread_solib_name
= so
->so_original_name
;
263 bsd_uthread_solib_unloaded (struct so_list
*so
)
265 if (!bsd_uthread_solib_name
)
268 if (strcmp (so
->so_original_name
, bsd_uthread_solib_name
) == 0)
269 bsd_uthread_deactivate ();
273 bsd_uthread_mourn_inferior (void)
275 find_target_beneath (bsd_uthread_ops_hack
)->to_mourn_inferior ();
276 bsd_uthread_deactivate ();
280 bsd_uthread_fetch_registers (struct regcache
*regcache
, int regnum
)
282 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
283 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
284 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
285 CORE_ADDR active_addr
;
287 /* Always fetch the appropriate registers from the layer beneath. */
288 find_target_beneath (bsd_uthread_ops_hack
)->to_fetch_registers (regcache
, regnum
);
290 /* FIXME: That might have gotten us more than we asked for. Make
291 sure we overwrite all relevant registers with values from the
292 thread structure. This can go once we fix the underlying target. */
295 active_addr
= bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr
);
296 if (addr
!= 0 && addr
!= active_addr
)
298 bsd_uthread_check_magic (addr
);
299 ops
->supply_uthread (regcache
, regnum
,
300 addr
+ bsd_uthread_thread_ctx_offset
);
305 bsd_uthread_store_registers (struct regcache
*regcache
, int regnum
)
307 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
308 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
309 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
310 CORE_ADDR active_addr
;
312 active_addr
= bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr
);
313 if (addr
!= 0 && addr
!= active_addr
)
315 bsd_uthread_check_magic (addr
);
316 ops
->collect_uthread (regcache
, regnum
,
317 addr
+ bsd_uthread_thread_ctx_offset
);
321 /* Updating the thread that is currently running; pass the
322 request to the layer beneath. */
323 find_target_beneath (bsd_uthread_ops_hack
)->to_store_registers (regcache
, regnum
);
327 /* FIXME: This function is only there because otherwise GDB tries to
328 invoke deprecate_xfer_memory. */
331 bsd_uthread_xfer_partial (struct target_ops
*ops
, enum target_object object
,
332 const char *annex
, gdb_byte
*readbuf
,
333 const gdb_byte
*writebuf
,
334 ULONGEST offset
, LONGEST len
)
336 gdb_assert (ops
->beneath
->to_xfer_partial
);
337 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
, readbuf
,
338 writebuf
, offset
, len
);
342 bsd_uthread_wait (ptid_t ptid
, struct target_waitstatus
*status
)
346 /* Pass the request to the layer beneath. */
347 ptid
= find_target_beneath (bsd_uthread_ops_hack
)->to_wait (ptid
, status
);
349 /* If the process is no longer alive, there's no point in figuring
350 out the thread ID. It will fail anyway. */
351 if (status
->kind
== TARGET_WAITKIND_SIGNALLED
352 || status
->kind
== TARGET_WAITKIND_EXITED
)
355 /* Fetch the corresponding thread ID, and augment the returned
356 process ID with it. */
357 addr
= bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr
);
362 /* FIXME: For executables linked statically with the threads
363 library, we end up here before the program has actually been
364 executed. In that case ADDR will be garbage since it has
365 been read from the wrong virtual memory image. */
366 if (target_read_memory (addr
, buf
, 4) == 0)
368 ULONGEST magic
= extract_unsigned_integer (buf
, 4);
369 if (magic
== BSD_UTHREAD_PTHREAD_MAGIC
)
370 ptid
= ptid_build (ptid_get_pid (ptid
), 0, addr
);
374 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
375 ptid with tid set, then ptid is still the initial thread of
376 the process. Notify GDB core about it. */
377 if (ptid_get_tid (inferior_ptid
) == 0
378 && ptid_get_tid (ptid
) != 0 && !in_thread_list (ptid
))
379 thread_change_ptid (inferior_ptid
, ptid
);
381 /* Don't let the core see a ptid without a corresponding thread. */
382 if (!in_thread_list (ptid
) || is_exited (ptid
))
389 bsd_uthread_resume (ptid_t ptid
, int step
, enum target_signal sig
)
391 /* Pass the request to the layer beneath. */
392 find_target_beneath (bsd_uthread_ops_hack
)->to_resume (ptid
, step
, sig
);
396 bsd_uthread_thread_alive (ptid_t ptid
)
398 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
402 int offset
= bsd_uthread_thread_state_offset
;
405 bsd_uthread_check_magic (addr
);
407 state
= read_memory_unsigned_integer (addr
+ offset
, 4);
408 if (state
== BSD_UTHREAD_PS_DEAD
)
412 return find_target_beneath (bsd_uthread_ops_hack
)->to_thread_alive (ptid
);
416 bsd_uthread_find_new_threads (void)
418 pid_t pid
= ptid_get_pid (inferior_ptid
);
419 int offset
= bsd_uthread_thread_next_offset
;
422 addr
= bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr
);
425 ptid_t ptid
= ptid_build (pid
, 0, addr
);
427 if (!in_thread_list (ptid
) || is_exited (ptid
))
429 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
430 is still the initial thread of the process. Notify GDB
432 if (ptid_get_tid (inferior_ptid
) == 0)
433 thread_change_ptid (inferior_ptid
, ptid
);
438 addr
= bsd_uthread_read_memory_address (addr
+ offset
);
442 /* Possible states a thread can be in. */
443 static char *bsd_uthread_state
[] =
467 /* Return a string describing th state of the thread specified by
471 bsd_uthread_extra_thread_info (struct thread_info
*info
)
473 CORE_ADDR addr
= ptid_get_tid (info
->ptid
);
477 int offset
= bsd_uthread_thread_state_offset
;
480 state
= read_memory_unsigned_integer (addr
+ offset
, 4);
481 if (state
< ARRAY_SIZE (bsd_uthread_state
))
482 return bsd_uthread_state
[state
];
489 bsd_uthread_pid_to_str (ptid_t ptid
)
491 if (ptid_get_tid (ptid
) != 0)
495 xsnprintf (buf
, sizeof buf
, "process %d, thread 0x%lx",
496 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
500 return normal_pid_to_str (ptid
);
504 bsd_uthread_target (void)
506 struct target_ops
*t
= XZALLOC (struct target_ops
);
508 t
->to_shortname
= "bsd-uthreads";
509 t
->to_longname
= "BSD user-level threads";
510 t
->to_doc
= "BSD user-level threads";
511 t
->to_close
= bsd_uthread_close
;
512 t
->to_mourn_inferior
= bsd_uthread_mourn_inferior
;
513 t
->to_fetch_registers
= bsd_uthread_fetch_registers
;
514 t
->to_store_registers
= bsd_uthread_store_registers
;
515 t
->to_xfer_partial
= bsd_uthread_xfer_partial
;
516 t
->to_wait
= bsd_uthread_wait
;
517 t
->to_resume
= bsd_uthread_resume
;
518 t
->to_thread_alive
= bsd_uthread_thread_alive
;
519 t
->to_find_new_threads
= bsd_uthread_find_new_threads
;
520 t
->to_extra_thread_info
= bsd_uthread_extra_thread_info
;
521 t
->to_pid_to_str
= bsd_uthread_pid_to_str
;
522 t
->to_stratum
= thread_stratum
;
523 t
->to_magic
= OPS_MAGIC
;
524 bsd_uthread_ops_hack
= t
;
530 _initialize_bsd_uthread (void)
532 add_target (bsd_uthread_target ());
534 bsd_uthread_data
= gdbarch_data_register_pre_init (bsd_uthread_init
);
536 observer_attach_inferior_created (bsd_uthread_inferior_created
);
537 observer_attach_solib_loaded (bsd_uthread_solib_loaded
);
538 observer_attach_solib_unloaded (bsd_uthread_solib_unloaded
);