1 /* $FreeBSD: src/gnu/usr.bin/binutils/gdb/freebsd-uthread.c,v 1.3.2.4 2003/01/06 11:28:54 fjoe Exp $ */
2 /* Low level interface for debugging FreeBSD user threads for GDB, the GNU debugger.
3 Copyright 1996, 1999 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, Boston, MA 02111-1307, USA. */
21 /* This module implements a sort of half target that sits between the
22 machine-independent parts of GDB and the ptrace interface (infptrace.c) to
23 provide access to the FreeBSD user-mode thread implementation.
25 FreeBSD threads are true user-mode threads, which are invoked via
26 the pthread_* interfaces. These are mostly implemented in
27 user-space, with all thread context kept in various structures that
28 live in the user's heap. For the most part, the kernel has no
29 knowlege of these threads.
31 Based largely on hpux-thread.c
37 #include <sys/queue.h>
40 #include "gdbthread.h"
48 extern int child_suppress_run;
49 extern struct target_ops child_ops; /* target vector for inftarg.c */
51 extern void _initialize_freebsd_uthread PARAMS ((void));
53 static int main_pid = -1; /* Real process ID */
55 /* Set to true while we are part-way through attaching */
56 static int freebsd_uthread_attaching;
58 static int freebsd_uthread_active = 0;
59 static CORE_ADDR P_thread_list;
60 static CORE_ADDR P_thread_run;
62 static struct cleanup * save_inferior_pid PARAMS ((void));
64 static void restore_inferior_pid PARAMS ((int pid));
66 static void freebsd_uthread_resume PARAMS ((int pid, int step,
67 enum target_signal signo));
69 static void init_freebsd_uthread_ops PARAMS ((void));
71 static struct target_ops freebsd_uthread_ops;
72 static struct target_thread_vector freebsd_uthread_vec;
78 save_inferior_pid - Save inferior_pid on the cleanup list
79 restore_inferior_pid - Restore inferior_pid from the cleanup list
83 struct cleanup *save_inferior_pid ()
84 void restore_inferior_pid (int pid)
88 These two functions act in unison to restore inferior_pid in
93 inferior_pid is a global variable that needs to be changed by many of
94 these routines before calling functions in procfs.c. In order to
95 guarantee that inferior_pid gets restored (in case of errors), you
96 need to call save_inferior_pid before changing it. At the end of the
97 function, you should invoke do_cleanups to restore it.
101 static struct cleanup *
104 return make_cleanup ((make_cleanup_func) restore_inferior_pid,
105 (void *)(intptr_t) inferior_pid);
109 restore_inferior_pid (pid)
115 static int find_active_thread PARAMS ((void));
117 struct cached_pthread {
127 static int cached_thread;
128 static struct cached_pthread cached_pthread;
129 static CORE_ADDR cached_pthread_addr;
131 #define THREADID_TID(id) ((id) >> 17)
132 #define THREADID_PID(id) ((id) & ((1 << 17) - 1))
134 LIST_HEAD(idmaplist, idmap);
137 LIST_ENTRY(idmap) link;
142 #define MAPHASH_SIZE 257
144 #define TID_MAX 16383
146 static int tid_to_hash[TID_MAX + 1]; /* set to map_hash index */
147 static struct idmaplist map_hash[MAPHASH_SIZE];
148 static int next_free_tid = TID_MIN; /* first available tid */
149 static int last_free_tid = TID_MIN; /* first unavailable */
151 static CORE_ADDR P_thread_next_offset;
152 static CORE_ADDR P_thread_uniqueid_offset;
153 static CORE_ADDR P_thread_state_offset;
154 static CORE_ADDR P_thread_name_offset;
155 static CORE_ADDR P_thread_ctx_offset;
156 static CORE_ADDR P_thread_PS_RUNNING_value;
157 static CORE_ADDR P_thread_PS_DEAD_value;
159 static int next_offset;
160 static int uniqueid_offset;
161 static int state_offset;
162 static int name_offset;
163 static int ctx_offset;
164 static int PS_RUNNING_value;
165 static int PS_DEAD_value;
167 #define UNIQUEID_HASH(id) (id % MAPHASH_SIZE)
168 #define TID_ADD1(tid) (((tid) + 1) == TID_MAX + 1 \
169 ? TID_MIN : (tid) + 1)
170 #define IS_TID_FREE(tid) (tid_to_hash[tid] == -1)
176 int tid = next_free_tid;
178 tid_to_hash[tid] = h;
179 next_free_tid = TID_ADD1(next_free_tid);
180 if (next_free_tid == last_free_tid)
184 for (i = last_free_tid; TID_ADD1(i) != last_free_tid; i = TID_ADD1(i))
187 if (TID_ADD1(i) == last_free_tid)
189 error("too many threads");
193 for (i = TID_ADD1(i); IS_TID_FREE(i); i = TID_ADD1(i))
205 int h = UNIQUEID_HASH(uniqueid);
208 LIST_FOREACH(im, &map_hash[h], link)
209 if (im->uniqueid == uniqueid)
210 return (im->tid << 17) + main_pid;
212 im = xmalloc(sizeof(struct idmap));
213 im->uniqueid = uniqueid;
214 im->tid = get_new_tid(h);
215 LIST_INSERT_HEAD(&map_hash[h], im, link);
217 return (im->tid << 17) + main_pid;
224 int tid = THREADID_TID(pid);
225 int h = tid_to_hash[tid];
230 LIST_FOREACH(im, &map_hash[h], link)
236 LIST_REMOVE(im, link);
237 tid_to_hash[tid] = -1;
241 #define READ_OFFSET(field) read_memory(P_thread_##field##_offset, \
242 (char *) &field##_offset, \
243 sizeof(field##_offset))
245 #define READ_VALUE(name) read_memory(P_thread_##name##_value, \
246 (char *) &name##_value, \
247 sizeof(name##_value))
250 read_thread_offsets ()
253 READ_OFFSET(uniqueid);
258 READ_VALUE(PS_RUNNING);
262 #define READ_FIELD(ptr, T, field, result) \
263 read_memory ((ptr) + field##_offset, (char *) &(result), sizeof result)
266 read_pthread_uniqueid (ptr)
270 READ_FIELD(ptr, u_int64_t, uniqueid, uniqueid);
275 read_pthread_next (ptr)
279 READ_FIELD(ptr, CORE_ADDR, next, next);
284 read_cached_pthread (ptr, cache)
286 struct cached_pthread *cache;
288 READ_FIELD(ptr, u_int64_t, uniqueid, cache->uniqueid);
289 READ_FIELD(ptr, int, state, cache->state);
290 READ_FIELD(ptr, CORE_ADDR, name, cache->name);
291 READ_FIELD(ptr, ucontext_t, ctx, cache->ctx);
295 find_active_thread ()
302 read_memory ((CORE_ADDR)P_thread_run,
306 return find_pid(read_pthread_uniqueid(ptr));
309 static CORE_ADDR find_pthread_addr PARAMS ((int thread));
310 static struct cached_pthread * find_pthread PARAMS ((int thread));
313 find_pthread_addr (thread)
318 if (thread == cached_thread)
319 return cached_pthread_addr;
321 read_memory ((CORE_ADDR)P_thread_list,
327 if (find_pid(read_pthread_uniqueid(ptr)) == thread)
329 cached_thread = thread;
330 cached_pthread_addr = ptr;
331 read_cached_pthread(ptr, &cached_pthread);
334 ptr = read_pthread_next(ptr);
340 static struct cached_pthread *
341 find_pthread (thread)
346 if (thread == cached_thread)
347 return &cached_pthread;
349 read_memory ((CORE_ADDR)P_thread_list,
355 if (find_pid(read_pthread_uniqueid(ptr)) == thread)
357 cached_thread = thread;
358 cached_pthread_addr = ptr;
359 read_cached_pthread(ptr, &cached_pthread);
360 return &cached_pthread;
362 ptr = read_pthread_next(ptr);
366 error ("Can't find pthread %d,%d",
367 THREADID_TID(thread), THREADID_PID(thread));
373 /* Most target vector functions from here on actually just pass through to
374 inftarg.c, as they don't need to do anything specific for threads. */
378 freebsd_uthread_open (arg, from_tty)
382 child_ops.to_open (arg, from_tty);
385 /* Attach to process PID, then initialize for debugging it
386 and wait for the trace-trap that results from attaching. */
389 freebsd_uthread_attach (args, from_tty)
393 child_ops.to_attach (args, from_tty);
394 push_target (&freebsd_uthread_ops);
395 freebsd_uthread_attaching = 1;
398 /* After an attach, see if the target is threaded */
401 freebsd_uthread_post_attach (pid)
404 if (freebsd_uthread_active)
406 read_thread_offsets ();
410 bind_target_thread_vector (&freebsd_uthread_vec);
412 inferior_pid = find_active_thread ();
414 add_thread (inferior_pid);
418 unpush_target (&freebsd_uthread_ops);
419 push_target (&child_ops);
422 freebsd_uthread_attaching = 0;
425 /* Take a program previously attached to and detaches it.
426 The program resumes execution and will no longer stop
427 on signals, etc. We'd better not have left any breakpoints
428 in the program or it'll die when it hits one. For this
429 to work, it may be necessary for the process to have been
430 previously attached. It *might* work if the program was
431 started via the normal ptrace (PTRACE_TRACEME). */
434 freebsd_uthread_detach (args, from_tty)
438 child_ops.to_detach (args, from_tty);
441 /* Resume execution of process PID. If STEP is nozero, then
442 just single step it. If SIGNAL is nonzero, restart it with that
443 signal activated. We may have to convert pid from a thread-id to an LWP id
447 freebsd_uthread_resume (pid, step, signo)
450 enum target_signal signo;
452 struct cleanup *old_chain;
454 if (freebsd_uthread_attaching)
456 child_ops.to_resume (pid, step, signo);
460 old_chain = save_inferior_pid ();
462 pid = inferior_pid = main_pid;
464 child_ops.to_resume (pid, step, signo);
468 do_cleanups (old_chain);
471 /* Wait for any threads to stop. We may have to convert PID from a thread id
472 to a LWP id, and vice versa on the way out. */
475 freebsd_uthread_wait (pid, ourstatus)
477 struct target_waitstatus *ourstatus;
480 struct cleanup *old_chain;
482 if (freebsd_uthread_attaching)
484 return child_ops.to_wait (pid, ourstatus);
487 old_chain = save_inferior_pid ();
489 inferior_pid = main_pid;
494 rtnval = child_ops.to_wait (pid, ourstatus);
498 rtnval = find_active_thread ();
499 if (!in_thread_list (rtnval))
503 do_cleanups (old_chain);
510 static char jmpmap[NUM_REGS] = /* map reg to jmp_buf */
534 static char jmpmap[NUM_REGS] = {
535 4, 5, 6, 7, 8, 9, 10, 11, /* v0 - t6 */
536 12, 13, 14, 15, 16, 17, 18, 19, /* t7 - fp */
537 20, 21, 22, 23, 24, 25, 26, 27, /* a0 - t9 */
538 28, 29, 30, 31, 32, 33, 34, 35, /* t10 - zero */
539 37, 38, 39, 40, 41, 42, 43, 44, /* f0 - f7 */
540 45, 46, 47, 48, 49, 50, 51, 52, /* f8 - f15 */
541 53, 54, 55, 56, 57, 58, 59, 60, /* f16 - f23 */
542 61, 62, 63, 64, 65, 66, 67, 68, /* f24 - f31 */
549 freebsd_uthread_fetch_registers (regno)
552 struct cached_pthread *thread;
553 struct cleanup *old_chain;
555 int first_regno, last_regno;
559 if (freebsd_uthread_attaching)
561 child_ops.to_fetch_registers (regno);
565 thread = find_pthread (inferior_pid);
567 old_chain = save_inferior_pid ();
569 active = (inferior_pid == find_active_thread());
571 inferior_pid = main_pid;
575 child_ops.to_fetch_registers (regno);
577 do_cleanups (old_chain);
585 last_regno = NUM_REGS - 1;
593 regbase = (register_t*) &thread->ctx.jb[0];
596 for (regno = first_regno; regno <= last_regno; regno++)
598 if (regmap[regno] == -1)
599 child_ops.to_fetch_registers (regno);
601 supply_register (regno, (char*) ®base[regmap[regno]]);
604 do_cleanups (old_chain);
608 freebsd_uthread_store_registers (regno)
611 struct cached_pthread *thread;
613 struct cleanup *old_chain;
614 int first_regno, last_regno;
618 if (freebsd_uthread_attaching)
620 child_ops.to_store_registers (regno);
624 thread = find_pthread (inferior_pid);
626 old_chain = save_inferior_pid ();
628 inferior_pid = main_pid;
630 if (thread->state == PS_RUNNING_value)
632 child_ops.to_store_registers (regno);
634 do_cleanups (old_chain);
642 last_regno = NUM_REGS - 1;
650 regbase = (u_int32_t*) &thread->ctx.jb[0];
653 ptr = find_pthread_addr (inferior_pid);
654 for (regno = first_regno; regno <= last_regno; regno++)
656 if (regmap[regno] == -1)
657 child_ops.to_store_registers (regno);
660 u_int32_t *reg = ®base[regmap[regno]];
663 /* Hang onto cached value */
664 memcpy(reg, registers + REGISTER_BYTE (regno),
665 REGISTER_RAW_SIZE (regno));
667 /* And push out to inferior */
668 off = (char *) reg - (char *) thread;
669 write_memory (ptr + off,
670 registers + REGISTER_BYTE (regno),
671 REGISTER_RAW_SIZE (regno));
675 do_cleanups (old_chain);
678 /* Get ready to modify the registers array. On machines which store
679 individual registers, this doesn't need to do anything. On machines
680 which store all the registers in one fell swoop, this makes sure
681 that registers contains all the registers from the program being
685 freebsd_uthread_prepare_to_store ()
687 struct cleanup *old_chain;
689 if (freebsd_uthread_attaching)
691 child_ops.to_prepare_to_store ();
695 old_chain = save_inferior_pid ();
696 inferior_pid = main_pid;
698 child_ops.to_prepare_to_store ();
700 do_cleanups (old_chain);
704 freebsd_uthread_xfer_memory (memaddr, myaddr, len, dowrite, target)
709 struct target_ops *target; /* ignored */
712 struct cleanup *old_chain;
714 if (freebsd_uthread_attaching)
716 return child_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
719 old_chain = save_inferior_pid ();
721 inferior_pid = main_pid;
723 retval = child_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
725 do_cleanups (old_chain);
730 /* Print status information about what we're accessing. */
733 freebsd_uthread_files_info (ignore)
734 struct target_ops *ignore;
736 child_ops.to_files_info (ignore);
740 freebsd_uthread_kill_inferior ()
742 inferior_pid = main_pid;
743 child_ops.to_kill ();
747 freebsd_uthread_notice_signals (pid)
750 struct cleanup *old_chain;
751 old_chain = save_inferior_pid ();
752 inferior_pid = main_pid;
754 child_ops.to_notice_signals (pid);
756 do_cleanups (old_chain);
759 /* Fork an inferior process, and start debugging it with /proc. */
762 freebsd_uthread_create_inferior (exec_file, allargs, env)
767 child_ops.to_create_inferior (exec_file, allargs, env);
769 if (inferior_pid && freebsd_uthread_active)
771 read_thread_offsets ();
773 main_pid = inferior_pid;
775 push_target (&freebsd_uthread_ops);
776 bind_target_thread_vector (&freebsd_uthread_vec);
778 inferior_pid = find_active_thread ();
780 add_thread (inferior_pid);
784 /* This routine is called to find out if the inferior is using threads.
785 We check for the _thread_run and _thread_list globals. */
788 freebsd_uthread_new_objfile (objfile)
789 struct objfile *objfile;
791 struct minimal_symbol *ms;
795 freebsd_uthread_active = 0;
799 ms = lookup_minimal_symbol ("_thread_run", NULL, objfile);
804 P_thread_run = SYMBOL_VALUE_ADDRESS (ms);
806 ms = lookup_minimal_symbol ("_thread_list", NULL, objfile);
811 P_thread_list = SYMBOL_VALUE_ADDRESS (ms);
813 #define OFFSET_SYM(field) "_thread_" #field "_offset"
814 #define LOOKUP_OFFSET(field) \
816 ms = lookup_minimal_symbol (OFFSET_SYM(field), NULL, objfile); \
819 P_thread_##field##_offset = SYMBOL_VALUE_ADDRESS (ms); \
822 #define VALUE_SYM(name) "_thread_" #name "_value"
823 #define LOOKUP_VALUE(name) \
825 ms = lookup_minimal_symbol (VALUE_SYM(name), NULL, objfile); \
828 P_thread_##name##_value = SYMBOL_VALUE_ADDRESS (ms); \
832 LOOKUP_OFFSET(uniqueid);
833 LOOKUP_OFFSET(state);
837 LOOKUP_VALUE(PS_RUNNING);
838 LOOKUP_VALUE(PS_DEAD);
840 freebsd_uthread_active = 1;
844 freebsd_uthread_has_exited (pid, wait_status, exit_status)
849 int t = child_ops.to_has_exited (pid, wait_status, exit_status);
855 /* Clean up after the inferior dies. */
858 freebsd_uthread_mourn_inferior ()
860 inferior_pid = main_pid; /* don't bother to restore inferior_pid */
861 child_ops.to_mourn_inferior ();
862 unpush_target (&freebsd_uthread_ops);
865 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
868 freebsd_uthread_can_run ()
870 return child_suppress_run;
874 freebsd_uthread_thread_alive (pid)
877 struct cleanup *old_chain;
878 struct cached_pthread *thread;
881 if (freebsd_uthread_attaching)
885 * We can get called from child_ops.to_wait() which passes the underlying
886 * pid (without a thread number).
888 if (THREADID_TID(pid) == 0)
891 old_chain = save_inferior_pid ();
892 inferior_pid = main_pid;
894 if (find_pthread_addr (pid) != 0)
896 thread = find_pthread (pid);
897 ret = (thread->state != PS_DEAD_value);
900 do_cleanups (old_chain);
909 freebsd_uthread_stop ()
911 struct cleanup *old_chain;
912 old_chain = save_inferior_pid ();
913 inferior_pid = main_pid;
915 child_ops.to_stop ();
917 do_cleanups (old_chain);
921 freebsd_uthread_find_new_threads ()
926 struct cleanup *old_chain;
928 old_chain = save_inferior_pid ();
929 inferior_pid = main_pid;
931 read_memory ((CORE_ADDR)P_thread_list,
937 READ_FIELD(ptr, int, state, state);
938 READ_FIELD(ptr, u_int64_t, uniqueid, uniqueid);
939 if (state != PS_DEAD_value &&
940 !in_thread_list (find_pid(uniqueid)))
941 add_thread (find_pid(uniqueid));
942 ptr = read_pthread_next(ptr);
945 do_cleanups (old_chain);
950 /* MUST MATCH enum pthread_state */
951 static const char *statenames[] = {
975 freebsd_uthread_get_thread_info (ref, selection, info)
978 struct gdb_ext_thread_info *info;
981 struct cached_pthread *thread = find_pthread (pid);
982 struct cleanup *old_chain;
984 old_chain = save_inferior_pid ();
985 inferior_pid = main_pid;
987 memset(&info->threadid, 0, OPAQUETHREADBYTES);
989 memcpy(&info->threadid, ref, sizeof *ref);
990 info->active = thread->state == PS_RUNNING_value;
991 strcpy(info->display, statenames[thread->state]);
993 read_memory ((CORE_ADDR) thread->name, info->shortname, 32);
995 strcpy(info->shortname, "");
997 do_cleanups (old_chain);
1002 freebsd_uthread_pid_to_str (pid)
1005 static char buf[30];
1007 if (STREQ (current_target.to_shortname, "freebsd-uthreads"))
1008 sprintf (buf, "process %d, thread %d\0",
1009 THREADID_PID(pid), THREADID_TID(pid));
1011 sprintf (buf, "process %d\0", pid);
1018 init_freebsd_uthread_ops ()
1020 freebsd_uthread_ops.to_shortname = "freebsd-uthreads";
1021 freebsd_uthread_ops.to_longname = "FreeBSD uthreads";
1022 freebsd_uthread_ops.to_doc = "FreeBSD user threads support.";
1023 freebsd_uthread_ops.to_open = freebsd_uthread_open;
1024 freebsd_uthread_ops.to_attach = freebsd_uthread_attach;
1025 freebsd_uthread_ops.to_post_attach = freebsd_uthread_post_attach;
1026 freebsd_uthread_ops.to_detach = freebsd_uthread_detach;
1027 freebsd_uthread_ops.to_resume = freebsd_uthread_resume;
1028 freebsd_uthread_ops.to_wait = freebsd_uthread_wait;
1029 freebsd_uthread_ops.to_fetch_registers = freebsd_uthread_fetch_registers;
1030 freebsd_uthread_ops.to_store_registers = freebsd_uthread_store_registers;
1031 freebsd_uthread_ops.to_prepare_to_store = freebsd_uthread_prepare_to_store;
1032 freebsd_uthread_ops.to_xfer_memory = freebsd_uthread_xfer_memory;
1033 freebsd_uthread_ops.to_files_info = freebsd_uthread_files_info;
1034 freebsd_uthread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1035 freebsd_uthread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1036 freebsd_uthread_ops.to_terminal_init = terminal_init_inferior;
1037 freebsd_uthread_ops.to_terminal_inferior = terminal_inferior;
1038 freebsd_uthread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1039 freebsd_uthread_ops.to_terminal_ours = terminal_ours;
1040 freebsd_uthread_ops.to_terminal_info = child_terminal_info;
1041 freebsd_uthread_ops.to_kill = freebsd_uthread_kill_inferior;
1042 freebsd_uthread_ops.to_create_inferior = freebsd_uthread_create_inferior;
1043 freebsd_uthread_ops.to_has_exited = freebsd_uthread_has_exited;
1044 freebsd_uthread_ops.to_mourn_inferior = freebsd_uthread_mourn_inferior;
1045 freebsd_uthread_ops.to_can_run = freebsd_uthread_can_run;
1046 freebsd_uthread_ops.to_notice_signals = freebsd_uthread_notice_signals;
1047 freebsd_uthread_ops.to_thread_alive = freebsd_uthread_thread_alive;
1048 freebsd_uthread_ops.to_stop = freebsd_uthread_stop;
1049 freebsd_uthread_ops.to_stratum = process_stratum;
1050 freebsd_uthread_ops.to_has_all_memory = 1;
1051 freebsd_uthread_ops.to_has_memory = 1;
1052 freebsd_uthread_ops.to_has_stack = 1;
1053 freebsd_uthread_ops.to_has_registers = 1;
1054 freebsd_uthread_ops.to_has_execution = 1;
1055 freebsd_uthread_ops.to_has_thread_control = 0;
1056 freebsd_uthread_ops.to_magic = OPS_MAGIC;
1058 freebsd_uthread_vec.find_new_threads = freebsd_uthread_find_new_threads;
1059 freebsd_uthread_vec.get_thread_info = freebsd_uthread_get_thread_info;
1063 _initialize_freebsd_uthread ()
1065 init_freebsd_uthread_ops ();
1066 add_target (&freebsd_uthread_ops);
1068 child_suppress_run = 1;