1 /* BSD user-level threads support.
3 Copyright 2005 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "gdbthread.h"
34 #include "gdb_assert.h"
35 #include "gdb_obstack.h"
37 #include "bsd-uthread.h"
39 /* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target. */
40 static struct target_ops
*bsd_uthread_ops_hack
;
43 /* Architecture-specific operations. */
45 /* Per-architecture data key. */
46 static struct gdbarch_data
*bsd_uthread_data
;
48 struct bsd_uthread_ops
50 /* Supply registers for an inactive thread to a register cache. */
51 void (*supply_uthread
)(struct regcache
*, int, CORE_ADDR
);
53 /* Collect registers for an inactive thread from a register cache. */
54 void (*collect_uthread
)(const struct regcache
*, int, CORE_ADDR
);
58 bsd_uthread_init (struct obstack
*obstack
)
60 struct bsd_uthread_ops
*ops
;
62 ops
= OBSTACK_ZALLOC (obstack
, struct bsd_uthread_ops
);
66 /* Set the function that supplies registers from an inactive thread
67 for architecture GDBARCH to SUPPLY_UTHREAD. */
70 bsd_uthread_set_supply_uthread (struct gdbarch
*gdbarch
,
71 void (*supply_uthread
) (struct regcache
*,
74 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
75 ops
->supply_uthread
= supply_uthread
;
78 /* Set the function that collects registers for an inactive thread for
79 architecture GDBARCH to SUPPLY_UTHREAD. */
82 bsd_uthread_set_collect_uthread (struct gdbarch
*gdbarch
,
83 void (*collect_uthread
) (const struct regcache
*,
86 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
87 ops
->collect_uthread
= collect_uthread
;
90 /* Magic number to help recognize a valid thread structure. */
91 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
93 /* Check whether the thread structure at ADDR is valid. */
96 bsd_uthread_check_magic (CORE_ADDR addr
)
98 ULONGEST magic
= read_memory_unsigned_integer (addr
, 4);
100 if (magic
!= BSD_UTHREAD_PTHREAD_MAGIC
)
101 error (_("Bad magic"));
105 #define BSD_UTHREAD_PS_RUNNING 0
106 #define BSD_UTHREAD_PS_DEAD 18
108 /* Address of the pointer to the the thread structure for the running
110 static CORE_ADDR bsd_uthread_thread_run_addr
;
112 /* Address of the list of all threads. */
113 static CORE_ADDR bsd_uthread_thread_list_addr
;
115 /* Offsets of various "interesting" bits in the thread structure. */
116 static int bsd_uthread_thread_state_offset
= -1;
117 static int bsd_uthread_thread_next_offset
= -1;
118 static int bsd_uthread_thread_ctx_offset
;
120 /* Name of shared threads library. */
121 static const char *bsd_uthread_solib_name
;
123 /* Non-zero if the thread startum implemented by this module is active. */
124 static int bsd_uthread_active
;
127 bsd_uthread_lookup_address (const char *name
, struct objfile
*objfile
)
129 struct minimal_symbol
*sym
;
131 sym
= lookup_minimal_symbol (name
, NULL
, objfile
);
133 return SYMBOL_VALUE_ADDRESS (sym
);
139 bsd_uthread_lookup_offset (const char *name
, struct objfile
*objfile
)
143 addr
= bsd_uthread_lookup_address (name
, objfile
);
147 return read_memory_unsigned_integer (addr
, 4);
150 /* If OBJFILE contains the symbols corresponding to one of the
151 supported user-level threads libraries, activate the thread stratum
152 implemented by this module. */
155 bsd_uthread_activate (struct objfile
*objfile
)
157 struct gdbarch
*gdbarch
= current_gdbarch
;
158 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
160 /* Skip if the thread stratum has already been activated. */
161 if (bsd_uthread_active
)
164 /* There's no point in enabling this module if no
165 architecture-specific operations are provided. */
166 if (!ops
->supply_uthread
)
169 bsd_uthread_thread_run_addr
=
170 bsd_uthread_lookup_address ("_thread_run", objfile
);
171 if (bsd_uthread_thread_run_addr
== 0)
174 bsd_uthread_thread_list_addr
=
175 bsd_uthread_lookup_address ("_thread_list", objfile
);
176 if (bsd_uthread_thread_list_addr
== 0)
179 bsd_uthread_thread_state_offset
=
180 bsd_uthread_lookup_offset ("_thread_state_offset", objfile
);
181 if (bsd_uthread_thread_state_offset
== 0)
184 bsd_uthread_thread_next_offset
=
185 bsd_uthread_lookup_offset ("_thread_next_offset", objfile
);
186 if (bsd_uthread_thread_next_offset
== 0)
189 bsd_uthread_thread_ctx_offset
=
190 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile
);
192 push_target (bsd_uthread_ops_hack
);
193 bsd_uthread_active
= 1;
197 /* Deactivate the thread stratum implemented by this module. */
200 bsd_uthread_deactivate (void)
202 /* Skip if the thread stratum has already been deactivated. */
203 if (!bsd_uthread_active
)
206 bsd_uthread_active
= 0;
207 unpush_target (bsd_uthread_ops_hack
);
209 bsd_uthread_thread_run_addr
= 0;
210 bsd_uthread_thread_list_addr
= 0;
211 bsd_uthread_thread_state_offset
= 0;
212 bsd_uthread_thread_next_offset
= 0;
213 bsd_uthread_thread_ctx_offset
= 0;
214 bsd_uthread_solib_name
= NULL
;
218 bsd_uthread_inferior_created (struct target_ops
*ops
, int from_tty
)
220 bsd_uthread_activate (NULL
);
223 /* Likely candidates for the threads library. */
224 static const char *bsd_uthread_solib_names
[] =
226 "/usr/lib/libc_r.so", /* FreeBSD */
227 "/usr/lib/libpthread.so", /* OpenBSD */
232 bsd_uthread_solib_loaded (struct so_list
*so
)
234 const char **names
= bsd_uthread_solib_names
;
236 for (names
= bsd_uthread_solib_names
; *names
; names
++)
238 if (strncmp (so
->so_original_name
, *names
, strlen (*names
)) == 0)
240 solib_read_symbols (so
, so
->from_tty
);
242 if (bsd_uthread_activate (so
->objfile
))
244 bsd_uthread_solib_name
== so
->so_original_name
;
252 bsd_uthread_solib_unloaded (struct so_list
*so
)
254 if (!bsd_uthread_solib_name
)
257 if (strcmp (so
->so_original_name
, bsd_uthread_solib_name
) == 0)
258 bsd_uthread_deactivate ();
262 bsd_uthread_mourn_inferior (void)
264 find_target_beneath (bsd_uthread_ops_hack
)->to_mourn_inferior ();
265 bsd_uthread_deactivate ();
269 bsd_uthread_fetch_registers (int regnum
)
271 struct gdbarch
*gdbarch
= current_gdbarch
;
272 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
273 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
274 CORE_ADDR active_addr
;
276 /* Always fetch the appropriate registers from the layer beneath. */
277 find_target_beneath (bsd_uthread_ops_hack
)->to_fetch_registers (regnum
);
279 /* FIXME: That might have gotten us more than we asked for. Make
280 sure we overwrite all relevant registers with values from the
281 thread structure. This can go once we fix the underlying target. */
284 active_addr
= read_memory_typed_address (bsd_uthread_thread_run_addr
,
285 builtin_type_void_data_ptr
);
286 if (addr
!= 0 && addr
!= active_addr
)
288 bsd_uthread_check_magic (addr
);
289 ops
->supply_uthread (current_regcache
, regnum
,
290 addr
+ bsd_uthread_thread_ctx_offset
);
295 bsd_uthread_store_registers (int regnum
)
297 struct gdbarch
*gdbarch
= current_gdbarch
;
298 struct bsd_uthread_ops
*ops
= gdbarch_data (gdbarch
, bsd_uthread_data
);
299 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
300 CORE_ADDR active_addr
;
302 active_addr
= read_memory_typed_address (bsd_uthread_thread_run_addr
,
303 builtin_type_void_data_ptr
);
304 if (addr
!= 0 && addr
!= active_addr
)
306 bsd_uthread_check_magic (addr
);
307 ops
->collect_uthread (current_regcache
, regnum
,
308 addr
+ bsd_uthread_thread_ctx_offset
);
312 /* Updating the thread that is currently running; pass the
313 request to the layer beneath. */
314 find_target_beneath (bsd_uthread_ops_hack
)->to_store_registers (regnum
);
318 /* FIXME: This function is only there because otherwise GDB tries to
319 invoke deprecate_xfer_memory. */
322 bsd_uthread_xfer_partial (struct target_ops
*ops
, enum target_object object
,
323 const char *annex
, gdb_byte
*readbuf
,
324 const gdb_byte
*writebuf
,
325 ULONGEST offset
, LONGEST len
)
327 gdb_assert (ops
->beneath
->to_xfer_partial
);
328 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
, readbuf
,
329 writebuf
, offset
, len
);
333 bsd_uthread_wait (ptid_t ptid
, struct target_waitstatus
*status
)
337 /* Pass the request to the layer beneath. */
338 ptid
= find_target_beneath (bsd_uthread_ops_hack
)->to_wait (ptid
, status
);
340 /* Fetch the corresponding thread ID, and augment the returned
341 process ID with it. */
342 addr
= read_memory_typed_address (bsd_uthread_thread_run_addr
,
343 builtin_type_void_data_ptr
);
348 /* FIXME: For executables linked statically with the threads
349 library, we end up here before the program has actually been
350 executed. In that case ADDR will be garbage since it has
351 been read from the wrong virtual memory image. */
352 if (target_read_memory (addr
, buf
, 4) == 0)
354 ULONGEST magic
= extract_unsigned_integer (buf
, 4);
355 if (magic
== BSD_UTHREAD_PTHREAD_MAGIC
)
356 ptid
= ptid_build (ptid_get_pid (ptid
), 0, addr
);
360 /* HACK: Twiddle INFERIOR_PTID such that the initial thread of a
361 process isn't recognized as a new thread. */
362 if (ptid_get_tid (ptid
) != 0 && !in_thread_list (ptid
)
363 && ptid_get_tid (inferior_ptid
) == 0)
366 inferior_ptid
= ptid
;
373 bsd_uthread_resume (ptid_t ptid
, int step
, enum target_signal sig
)
375 /* Pass the request to the layer beneath. */
376 find_target_beneath (bsd_uthread_ops_hack
)->to_resume (ptid
, step
, sig
);
380 bsd_uthread_thread_alive (ptid_t ptid
)
382 CORE_ADDR addr
= ptid_get_tid (inferior_ptid
);
386 int offset
= bsd_uthread_thread_state_offset
;
389 bsd_uthread_check_magic (addr
);
391 state
= read_memory_unsigned_integer (addr
+ offset
, 4);
392 if (state
== BSD_UTHREAD_PS_DEAD
)
396 return find_target_beneath (bsd_uthread_ops_hack
)->to_thread_alive (ptid
);
400 bsd_uthread_find_new_threads (void)
402 pid_t pid
= ptid_get_pid (inferior_ptid
);
403 int offset
= bsd_uthread_thread_next_offset
;
406 addr
= read_memory_typed_address (bsd_uthread_thread_list_addr
,
407 builtin_type_void_data_ptr
);
410 ptid_t ptid
= ptid_build (pid
, 0, addr
);
412 if (!in_thread_list (ptid
))
415 addr
= read_memory_typed_address (addr
+ offset
,
416 builtin_type_void_data_ptr
);
420 /* Possible states a thread can be in. */
421 static char *bsd_uthread_state
[] =
445 /* Return a string describing th state of the thread specified by
449 bsd_uthread_extra_thread_info (struct thread_info
*info
)
451 CORE_ADDR addr
= ptid_get_tid (info
->ptid
);
455 int offset
= bsd_uthread_thread_state_offset
;
458 state
= read_memory_unsigned_integer (addr
+ offset
, 4);
459 if (state
< ARRAY_SIZE (bsd_uthread_state
))
460 return bsd_uthread_state
[state
];
467 bsd_uthread_pid_to_str (ptid_t ptid
)
469 if (ptid_get_tid (ptid
) != 0)
474 size
= snprintf (buf
, sizeof buf
, "process %d, thread 0x%lx",
475 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
476 gdb_assert (size
< sizeof buf
);
480 return normal_pid_to_str (ptid
);
484 bsd_uthread_target (void)
486 struct target_ops
*t
= XZALLOC (struct target_ops
);
488 t
->to_shortname
= "bsd-uthreads";
489 t
->to_longname
= "BSD user-level threads";
490 t
->to_doc
= "BSD user-level threads";
491 t
->to_mourn_inferior
= bsd_uthread_mourn_inferior
;
492 t
->to_fetch_registers
= bsd_uthread_fetch_registers
;
493 t
->to_store_registers
= bsd_uthread_store_registers
;
494 t
->to_xfer_partial
= bsd_uthread_xfer_partial
;
495 t
->to_wait
= bsd_uthread_wait
;
496 t
->to_resume
= bsd_uthread_resume
;
497 t
->to_thread_alive
= bsd_uthread_thread_alive
;
498 t
->to_find_new_threads
= bsd_uthread_find_new_threads
;
499 t
->to_extra_thread_info
= bsd_uthread_extra_thread_info
;
500 t
->to_pid_to_str
= bsd_uthread_pid_to_str
;
501 t
->to_stratum
= thread_stratum
;
502 t
->to_magic
= OPS_MAGIC
;
503 bsd_uthread_ops_hack
= t
;
509 _initialize_bsd_uthread (void)
511 add_target (bsd_uthread_target ());
513 bsd_uthread_data
= gdbarch_data_register_pre_init (bsd_uthread_init
);
515 observer_attach_inferior_created (bsd_uthread_inferior_created
);
516 observer_attach_solib_loaded (bsd_uthread_solib_loaded
);
517 observer_attach_solib_unloaded (bsd_uthread_solib_unloaded
);