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);
148 /* If OBJFILE contains the symbols corresponding to one of the
149 supported user-level threads libraries, activate the thread stratum
150 implemented by this module. */
153 bsd_uthread_activate (struct objfile
*objfile
)
155 struct gdbarch
*gdbarch
= current_gdbarch
;
156 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
158 /* Skip if the thread stratum has already been activated. */
159 if (bsd_uthread_active
)
162 /* There's no point in enabling this module if no
163 architecture-specific operations are provided. */
164 if (!ops
->supply_uthread
)
167 bsd_uthread_thread_run_addr
=
168 bsd_uthread_lookup_address ("_thread_run", objfile
);
169 if (bsd_uthread_thread_run_addr
== 0)
172 bsd_uthread_thread_list_addr
=
173 bsd_uthread_lookup_address ("_thread_list", objfile
);
174 if (bsd_uthread_thread_list_addr
== 0)
177 bsd_uthread_thread_state_offset
=
178 bsd_uthread_lookup_offset ("_thread_state_offset", objfile
);
179 if (bsd_uthread_thread_state_offset
== 0)
182 bsd_uthread_thread_next_offset
=
183 bsd_uthread_lookup_offset ("_thread_next_offset", objfile
);
184 if (bsd_uthread_thread_next_offset
== 0)
187 bsd_uthread_thread_ctx_offset
=
188 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile
);
190 push_target (bsd_uthread_ops_hack
);
191 bsd_uthread_active
= 1;
195 /* Cleanup due to deactivation. */
198 bsd_uthread_close (int quitting
)
200 bsd_uthread_active
= 0;
201 bsd_uthread_thread_run_addr
= 0;
202 bsd_uthread_thread_list_addr
= 0;
203 bsd_uthread_thread_state_offset
= 0;
204 bsd_uthread_thread_next_offset
= 0;
205 bsd_uthread_thread_ctx_offset
= 0;
206 bsd_uthread_solib_name
= NULL
;
209 /* Deactivate the thread stratum implemented by this module. */
212 bsd_uthread_deactivate (void)
214 /* Skip if the thread stratum has already been deactivated. */
215 if (!bsd_uthread_active
)
218 unpush_target (bsd_uthread_ops_hack
);
222 bsd_uthread_inferior_created (struct target_ops
*ops
, int from_tty
)
224 bsd_uthread_activate (NULL
);
227 /* Likely candidates for the threads library. */
228 static const char *bsd_uthread_solib_names
[] =
230 "/usr/lib/libc_r.so", /* FreeBSD */
231 "/usr/lib/libpthread.so", /* OpenBSD */
236 bsd_uthread_solib_loaded (struct so_list
*so
)
238 const char **names
= bsd_uthread_solib_names
;
240 for (names
= bsd_uthread_solib_names
; *names
; names
++)
242 if (strncmp (so
->so_original_name
, *names
, strlen (*names
)) == 0)
244 solib_read_symbols (so
, so
->from_tty
);
246 if (bsd_uthread_activate (so
->objfile
))
248 bsd_uthread_solib_name
= so
->so_original_name
;
256 bsd_uthread_solib_unloaded (struct so_list
*so
)
258 if (!bsd_uthread_solib_name
)
261 if (strcmp (so
->so_original_name
, bsd_uthread_solib_name
) == 0)
262 bsd_uthread_deactivate ();
266 bsd_uthread_mourn_inferior (void)
268 find_target_beneath (bsd_uthread_ops_hack
)->to_mourn_inferior ();
269 bsd_uthread_deactivate ();
273 bsd_uthread_fetch_registers (struct regcache
*regcache
, int regnum
)
275 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
276 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
277 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
278 CORE_ADDR active_addr
;
280 /* Always fetch the appropriate registers from the layer beneath. */
281 find_target_beneath (bsd_uthread_ops_hack
)->to_fetch_registers (regcache
, regnum
);
283 /* FIXME: That might have gotten us more than we asked for. Make
284 sure we overwrite all relevant registers with values from the
285 thread structure. This can go once we fix the underlying target. */
288 active_addr
= read_memory_typed_address (bsd_uthread_thread_run_addr
,
289 builtin_type_void_data_ptr
);
290 if (addr
!= 0 && addr
!= active_addr
)
292 bsd_uthread_check_magic (addr
);
293 ops
->supply_uthread (regcache
, regnum
,
294 addr
+ bsd_uthread_thread_ctx_offset
);
299 bsd_uthread_store_registers (struct regcache
*regcache
, int regnum
)
301 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
302 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
303 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
304 CORE_ADDR active_addr
;
306 active_addr
= read_memory_typed_address (bsd_uthread_thread_run_addr
,
307 builtin_type_void_data_ptr
);
308 if (addr
!= 0 && addr
!= active_addr
)
310 bsd_uthread_check_magic (addr
);
311 ops
->collect_uthread (regcache
, regnum
,
312 addr
+ bsd_uthread_thread_ctx_offset
);
316 /* Updating the thread that is currently running; pass the
317 request to the layer beneath. */
318 find_target_beneath (bsd_uthread_ops_hack
)->to_store_registers (regcache
, regnum
);
322 /* FIXME: This function is only there because otherwise GDB tries to
323 invoke deprecate_xfer_memory. */
326 bsd_uthread_xfer_partial (struct target_ops
*ops
, enum target_object object
,
327 const char *annex
, gdb_byte
*readbuf
,
328 const gdb_byte
*writebuf
,
329 ULONGEST offset
, LONGEST len
)
331 gdb_assert (ops
->beneath
->to_xfer_partial
);
332 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
, readbuf
,
333 writebuf
, offset
, len
);
337 bsd_uthread_wait (ptid_t ptid
, struct target_waitstatus
*status
)
341 /* Pass the request to the layer beneath. */
342 ptid
= find_target_beneath (bsd_uthread_ops_hack
)->to_wait (ptid
, status
);
344 /* If the process is no longer alive, there's no point in figuring
345 out the thread ID. It will fail anyway. */
346 if (status
->kind
== TARGET_WAITKIND_SIGNALLED
347 || status
->kind
== TARGET_WAITKIND_EXITED
)
350 /* Fetch the corresponding thread ID, and augment the returned
351 process ID with it. */
352 addr
= read_memory_typed_address (bsd_uthread_thread_run_addr
,
353 builtin_type_void_data_ptr
);
358 /* FIXME: For executables linked statically with the threads
359 library, we end up here before the program has actually been
360 executed. In that case ADDR will be garbage since it has
361 been read from the wrong virtual memory image. */
362 if (target_read_memory (addr
, buf
, 4) == 0)
364 ULONGEST magic
= extract_unsigned_integer (buf
, 4);
365 if (magic
== BSD_UTHREAD_PTHREAD_MAGIC
)
366 ptid
= ptid_build (ptid_get_pid (ptid
), 0, addr
);
370 /* HACK: Twiddle INFERIOR_PTID such that the initial thread of a
371 process isn't recognized as a new thread. */
372 if (ptid_get_tid (ptid
) != 0 && !in_thread_list (ptid
)
373 && ptid_get_tid (inferior_ptid
) == 0)
375 add_thread_silent (ptid
);
376 inferior_ptid
= ptid
;
383 bsd_uthread_resume (ptid_t ptid
, int step
, enum target_signal sig
)
385 /* Pass the request to the layer beneath. */
386 find_target_beneath (bsd_uthread_ops_hack
)->to_resume (ptid
, step
, sig
);
390 bsd_uthread_thread_alive (ptid_t ptid
)
392 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
396 int offset
= bsd_uthread_thread_state_offset
;
399 bsd_uthread_check_magic (addr
);
401 state
= read_memory_unsigned_integer (addr
+ offset
, 4);
402 if (state
== BSD_UTHREAD_PS_DEAD
)
406 return find_target_beneath (bsd_uthread_ops_hack
)->to_thread_alive (ptid
);
410 bsd_uthread_find_new_threads (void)
412 pid_t pid
= ptid_get_pid (inferior_ptid
);
413 int offset
= bsd_uthread_thread_next_offset
;
416 addr
= read_memory_typed_address (bsd_uthread_thread_list_addr
,
417 builtin_type_void_data_ptr
);
420 ptid_t ptid
= ptid_build (pid
, 0, addr
);
422 if (!in_thread_list (ptid
))
425 addr
= read_memory_typed_address (addr
+ offset
,
426 builtin_type_void_data_ptr
);
430 /* Possible states a thread can be in. */
431 static char *bsd_uthread_state
[] =
455 /* Return a string describing th state of the thread specified by
459 bsd_uthread_extra_thread_info (struct thread_info
*info
)
461 CORE_ADDR addr
= ptid_get_tid (info
->ptid
);
465 int offset
= bsd_uthread_thread_state_offset
;
468 state
= read_memory_unsigned_integer (addr
+ offset
, 4);
469 if (state
< ARRAY_SIZE (bsd_uthread_state
))
470 return bsd_uthread_state
[state
];
477 bsd_uthread_pid_to_str (ptid_t ptid
)
479 if (ptid_get_tid (ptid
) != 0)
483 xsnprintf (buf
, sizeof buf
, "process %d, thread 0x%lx",
484 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
488 return normal_pid_to_str (ptid
);
492 bsd_uthread_target (void)
494 struct target_ops
*t
= XZALLOC (struct target_ops
);
496 t
->to_shortname
= "bsd-uthreads";
497 t
->to_longname
= "BSD user-level threads";
498 t
->to_doc
= "BSD user-level threads";
499 t
->to_close
= bsd_uthread_close
;
500 t
->to_mourn_inferior
= bsd_uthread_mourn_inferior
;
501 t
->to_fetch_registers
= bsd_uthread_fetch_registers
;
502 t
->to_store_registers
= bsd_uthread_store_registers
;
503 t
->to_xfer_partial
= bsd_uthread_xfer_partial
;
504 t
->to_wait
= bsd_uthread_wait
;
505 t
->to_resume
= bsd_uthread_resume
;
506 t
->to_thread_alive
= bsd_uthread_thread_alive
;
507 t
->to_find_new_threads
= bsd_uthread_find_new_threads
;
508 t
->to_extra_thread_info
= bsd_uthread_extra_thread_info
;
509 t
->to_pid_to_str
= bsd_uthread_pid_to_str
;
510 t
->to_stratum
= thread_stratum
;
511 t
->to_magic
= OPS_MAGIC
;
512 bsd_uthread_ops_hack
= t
;
518 _initialize_bsd_uthread (void)
520 add_target (bsd_uthread_target ());
522 bsd_uthread_data
= gdbarch_data_register_pre_init (bsd_uthread_init
);
524 observer_attach_inferior_created (bsd_uthread_inferior_created
);
525 observer_attach_solib_loaded (bsd_uthread_solib_loaded
);
526 observer_attach_solib_unloaded (bsd_uthread_solib_unloaded
);