1 /* Low level interface for debugging FreeBSD user threads for GDB, the GNU debugger.
2 Copyright 1996, 1999 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* This module implements a sort of half target that sits between the
21 machine-independent parts of GDB and the ptrace interface (infptrace.c) to
22 provide access to the FreeBSD user-mode thread implementation.
24 FreeBSD threads are true user-mode threads, which are invoked via
25 the pthread_* interfaces. These are mostly implemented in
26 user-space, with all thread context kept in various structures that
27 live in the user's heap. For the most part, the kernel has no
28 knowlege of these threads.
30 Based largely on hpux-thread.c
35 * $DragonFly: src/gnu/usr.bin/binutils215/gdb/Attic/freebsd-uthread.c,v 1.1 2004/12/20 13:14:44 asmodai Exp $
39 #include <sys/queue.h>
42 #include "gdbthread.h"
50 extern int child_suppress_run;
51 extern struct target_ops child_ops; /* target vector for inftarg.c */
53 extern void _initialize_freebsd_uthread PARAMS ((void));
55 static int main_pid = -1; /* Real process ID */
57 /* Set to true while we are part-way through attaching */
58 static int freebsd_uthread_attaching;
60 static int freebsd_uthread_active = 0;
61 static CORE_ADDR P_thread_list;
62 static CORE_ADDR P_thread_run;
64 static struct cleanup * save_inferior_pid PARAMS ((void));
66 static void restore_inferior_pid PARAMS ((int pid));
68 static void freebsd_uthread_resume PARAMS ((int pid, int step,
69 enum target_signal signo));
71 static void init_freebsd_uthread_ops PARAMS ((void));
73 static struct target_ops freebsd_uthread_ops;
74 static struct target_thread_vector freebsd_uthread_vec;
80 save_inferior_pid - Save inferior_pid on the cleanup list
81 restore_inferior_pid - Restore inferior_pid from the cleanup list
85 struct cleanup *save_inferior_pid ()
86 void restore_inferior_pid (int pid)
90 These two functions act in unison to restore inferior_pid in
95 inferior_pid is a global variable that needs to be changed by many of
96 these routines before calling functions in procfs.c. In order to
97 guarantee that inferior_pid gets restored (in case of errors), you
98 need to call save_inferior_pid before changing it. At the end of the
99 function, you should invoke do_cleanups to restore it.
103 static struct cleanup *
106 return make_cleanup ((make_cleanup_func) restore_inferior_pid,
107 (void *)(intptr_t) inferior_pid);
111 restore_inferior_pid (pid)
117 static int find_active_thread PARAMS ((void));
119 struct cached_pthread {
129 static int cached_thread;
130 static struct cached_pthread cached_pthread;
131 static CORE_ADDR cached_pthread_addr;
133 #define THREADID_TID(id) ((id) >> 17)
134 #define THREADID_PID(id) ((id) & ((1 << 17) - 1))
136 LIST_HEAD(idmaplist, idmap);
139 LIST_ENTRY(idmap) link;
144 #define MAPHASH_SIZE 257
146 #define TID_MAX 16383
148 static int tid_to_hash[TID_MAX + 1]; /* set to map_hash index */
149 static struct idmaplist map_hash[MAPHASH_SIZE];
150 static int next_free_tid = TID_MIN; /* first available tid */
151 static int last_free_tid = TID_MIN; /* first unavailable */
153 static CORE_ADDR P_thread_next_offset;
154 static CORE_ADDR P_thread_uniqueid_offset;
155 static CORE_ADDR P_thread_state_offset;
156 static CORE_ADDR P_thread_name_offset;
157 static CORE_ADDR P_thread_ctx_offset;
158 static CORE_ADDR P_thread_PS_RUNNING_value;
159 static CORE_ADDR P_thread_PS_DEAD_value;
161 static int next_offset;
162 static int uniqueid_offset;
163 static int state_offset;
164 static int name_offset;
165 static int ctx_offset;
166 static int PS_RUNNING_value;
167 static int PS_DEAD_value;
169 #define UNIQUEID_HASH(id) (id % MAPHASH_SIZE)
170 #define TID_ADD1(tid) (((tid) + 1) == TID_MAX + 1 \
171 ? TID_MIN : (tid) + 1)
172 #define IS_TID_FREE(tid) (tid_to_hash[tid] == -1)
178 int tid = next_free_tid;
180 tid_to_hash[tid] = h;
181 next_free_tid = TID_ADD1(next_free_tid);
182 if (next_free_tid == last_free_tid)
186 for (i = last_free_tid; TID_ADD1(i) != last_free_tid; i = TID_ADD1(i))
189 if (TID_ADD1(i) == last_free_tid)
191 error("too many threads");
195 for (i = TID_ADD1(i); IS_TID_FREE(i); i = TID_ADD1(i))
207 int h = UNIQUEID_HASH(uniqueid);
210 LIST_FOREACH(im, &map_hash[h], link)
211 if (im->uniqueid == uniqueid)
212 return (im->tid << 17) + main_pid;
214 im = xmalloc(sizeof(struct idmap));
215 im->uniqueid = uniqueid;
216 im->tid = get_new_tid(h);
217 LIST_INSERT_HEAD(&map_hash[h], im, link);
219 return (im->tid << 17) + main_pid;
226 int tid = THREADID_TID(pid);
227 int h = tid_to_hash[tid];
232 LIST_FOREACH(im, &map_hash[h], link)
238 LIST_REMOVE(im, link);
239 tid_to_hash[tid] = -1;
243 #define READ_OFFSET(field) read_memory(P_thread_##field##_offset, \
244 (char *) &field##_offset, \
245 sizeof(field##_offset))
247 #define READ_VALUE(name) read_memory(P_thread_##name##_value, \
248 (char *) &name##_value, \
249 sizeof(name##_value))
252 read_thread_offsets ()
255 READ_OFFSET(uniqueid);
260 READ_VALUE(PS_RUNNING);
264 #define READ_FIELD(ptr, T, field, result) \
265 read_memory ((ptr) + field##_offset, (char *) &(result), sizeof result)
268 read_pthread_uniqueid (ptr)
272 READ_FIELD(ptr, u_int64_t, uniqueid, uniqueid);
277 read_pthread_next (ptr)
281 READ_FIELD(ptr, CORE_ADDR, next, next);
286 read_cached_pthread (ptr, cache)
288 struct cached_pthread *cache;
290 READ_FIELD(ptr, u_int64_t, uniqueid, cache->uniqueid);
291 READ_FIELD(ptr, int, state, cache->state);
292 READ_FIELD(ptr, CORE_ADDR, name, cache->name);
293 READ_FIELD(ptr, ucontext_t, ctx, cache->ctx);
297 find_active_thread ()
304 read_memory ((CORE_ADDR)P_thread_run,
308 return find_pid(read_pthread_uniqueid(ptr));
311 static CORE_ADDR find_pthread_addr PARAMS ((int thread));
312 static struct cached_pthread * find_pthread PARAMS ((int thread));
315 find_pthread_addr (thread)
320 if (thread == cached_thread)
321 return cached_pthread_addr;
323 read_memory ((CORE_ADDR)P_thread_list,
329 if (find_pid(read_pthread_uniqueid(ptr)) == thread)
331 cached_thread = thread;
332 cached_pthread_addr = ptr;
333 read_cached_pthread(ptr, &cached_pthread);
336 ptr = read_pthread_next(ptr);
342 static struct cached_pthread *
343 find_pthread (thread)
348 if (thread == cached_thread)
349 return &cached_pthread;
351 read_memory ((CORE_ADDR)P_thread_list,
357 if (find_pid(read_pthread_uniqueid(ptr)) == thread)
359 cached_thread = thread;
360 cached_pthread_addr = ptr;
361 read_cached_pthread(ptr, &cached_pthread);
362 return &cached_pthread;
364 ptr = read_pthread_next(ptr);
368 error ("Can't find pthread %d,%d",
369 THREADID_TID(thread), THREADID_PID(thread));
375 /* Most target vector functions from here on actually just pass through to
376 inftarg.c, as they don't need to do anything specific for threads. */
380 freebsd_uthread_open (arg, from_tty)
384 child_ops.to_open (arg, from_tty);
387 /* Attach to process PID, then initialize for debugging it
388 and wait for the trace-trap that results from attaching. */
391 freebsd_uthread_attach (args, from_tty)
395 child_ops.to_attach (args, from_tty);
396 push_target (&freebsd_uthread_ops);
397 freebsd_uthread_attaching = 1;
400 /* After an attach, see if the target is threaded */
403 freebsd_uthread_post_attach (pid)
406 if (freebsd_uthread_active)
408 read_thread_offsets ();
412 bind_target_thread_vector (&freebsd_uthread_vec);
414 inferior_pid = find_active_thread ();
416 add_thread (inferior_pid);
420 unpush_target (&freebsd_uthread_ops);
421 push_target (&child_ops);
424 freebsd_uthread_attaching = 0;
427 /* Take a program previously attached to and detaches it.
428 The program resumes execution and will no longer stop
429 on signals, etc. We'd better not have left any breakpoints
430 in the program or it'll die when it hits one. For this
431 to work, it may be necessary for the process to have been
432 previously attached. It *might* work if the program was
433 started via the normal ptrace (PTRACE_TRACEME). */
436 freebsd_uthread_detach (args, from_tty)
440 child_ops.to_detach (args, from_tty);
443 /* Resume execution of process PID. If STEP is nozero, then
444 just single step it. If SIGNAL is nonzero, restart it with that
445 signal activated. We may have to convert pid from a thread-id to an LWP id
449 freebsd_uthread_resume (pid, step, signo)
452 enum target_signal signo;
454 struct cleanup *old_chain;
456 if (freebsd_uthread_attaching)
458 child_ops.to_resume (pid, step, signo);
462 old_chain = save_inferior_pid ();
464 pid = inferior_pid = main_pid;
466 child_ops.to_resume (pid, step, signo);
470 do_cleanups (old_chain);
473 /* Wait for any threads to stop. We may have to convert PID from a thread id
474 to a LWP id, and vice versa on the way out. */
477 freebsd_uthread_wait (pid, ourstatus)
479 struct target_waitstatus *ourstatus;
482 struct cleanup *old_chain;
484 if (freebsd_uthread_attaching)
486 return child_ops.to_wait (pid, ourstatus);
489 old_chain = save_inferior_pid ();
491 inferior_pid = main_pid;
496 rtnval = child_ops.to_wait (pid, ourstatus);
500 rtnval = find_active_thread ();
501 if (!in_thread_list (rtnval))
505 do_cleanups (old_chain);
512 static char jmpmap[NUM_REGS] = /* map reg to jmp_buf */
536 static char jmpmap[NUM_REGS] = {
537 4, 5, 6, 7, 8, 9, 10, 11, /* v0 - t6 */
538 12, 13, 14, 15, 16, 17, 18, 19, /* t7 - fp */
539 20, 21, 22, 23, 24, 25, 26, 27, /* a0 - t9 */
540 28, 29, 30, 31, 32, 33, 34, 35, /* t10 - zero */
541 37, 38, 39, 40, 41, 42, 43, 44, /* f0 - f7 */
542 45, 46, 47, 48, 49, 50, 51, 52, /* f8 - f15 */
543 53, 54, 55, 56, 57, 58, 59, 60, /* f16 - f23 */
544 61, 62, 63, 64, 65, 66, 67, 68, /* f24 - f31 */
551 freebsd_uthread_fetch_registers (regno)
554 struct cached_pthread *thread;
555 struct cleanup *old_chain;
557 int first_regno, last_regno;
561 if (freebsd_uthread_attaching)
563 child_ops.to_fetch_registers (regno);
567 thread = find_pthread (inferior_pid);
569 old_chain = save_inferior_pid ();
571 active = (inferior_pid == find_active_thread());
573 inferior_pid = main_pid;
577 child_ops.to_fetch_registers (regno);
579 do_cleanups (old_chain);
587 last_regno = NUM_REGS - 1;
595 regbase = (register_t*) &thread->ctx.jb[0];
598 for (regno = first_regno; regno <= last_regno; regno++)
600 if (regmap[regno] == -1)
601 child_ops.to_fetch_registers (regno);
603 supply_register (regno, (char*) ®base[regmap[regno]]);
606 do_cleanups (old_chain);
610 freebsd_uthread_store_registers (regno)
613 struct cached_pthread *thread;
615 struct cleanup *old_chain;
616 int first_regno, last_regno;
620 if (freebsd_uthread_attaching)
622 child_ops.to_store_registers (regno);
626 thread = find_pthread (inferior_pid);
628 old_chain = save_inferior_pid ();
630 inferior_pid = main_pid;
632 if (thread->state == PS_RUNNING_value)
634 child_ops.to_store_registers (regno);
636 do_cleanups (old_chain);
644 last_regno = NUM_REGS - 1;
652 regbase = (u_int32_t*) &thread->ctx.jb[0];
655 ptr = find_pthread_addr (inferior_pid);
656 for (regno = first_regno; regno <= last_regno; regno++)
658 if (regmap[regno] == -1)
659 child_ops.to_store_registers (regno);
662 u_int32_t *reg = ®base[regmap[regno]];
665 /* Hang onto cached value */
666 memcpy(reg, registers + REGISTER_BYTE (regno),
667 REGISTER_RAW_SIZE (regno));
669 /* And push out to inferior */
670 off = (char *) reg - (char *) thread;
671 write_memory (ptr + off,
672 registers + REGISTER_BYTE (regno),
673 REGISTER_RAW_SIZE (regno));
677 do_cleanups (old_chain);
680 /* Get ready to modify the registers array. On machines which store
681 individual registers, this doesn't need to do anything. On machines
682 which store all the registers in one fell swoop, this makes sure
683 that registers contains all the registers from the program being
687 freebsd_uthread_prepare_to_store ()
689 struct cleanup *old_chain;
691 if (freebsd_uthread_attaching)
693 child_ops.to_prepare_to_store ();
697 old_chain = save_inferior_pid ();
698 inferior_pid = main_pid;
700 child_ops.to_prepare_to_store ();
702 do_cleanups (old_chain);
706 freebsd_uthread_xfer_memory (memaddr, myaddr, len, dowrite, target)
711 struct target_ops *target; /* ignored */
714 struct cleanup *old_chain;
716 if (freebsd_uthread_attaching)
718 return child_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
721 old_chain = save_inferior_pid ();
723 inferior_pid = main_pid;
725 retval = child_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target);
727 do_cleanups (old_chain);
732 /* Print status information about what we're accessing. */
735 freebsd_uthread_files_info (ignore)
736 struct target_ops *ignore;
738 child_ops.to_files_info (ignore);
742 freebsd_uthread_kill_inferior ()
744 inferior_pid = main_pid;
745 child_ops.to_kill ();
749 freebsd_uthread_notice_signals (pid)
752 struct cleanup *old_chain;
753 old_chain = save_inferior_pid ();
754 inferior_pid = main_pid;
756 child_ops.to_notice_signals (pid);
758 do_cleanups (old_chain);
761 /* Fork an inferior process, and start debugging it with /proc. */
764 freebsd_uthread_create_inferior (exec_file, allargs, env)
769 child_ops.to_create_inferior (exec_file, allargs, env);
771 if (inferior_pid && freebsd_uthread_active)
773 read_thread_offsets ();
775 main_pid = inferior_pid;
777 push_target (&freebsd_uthread_ops);
778 bind_target_thread_vector (&freebsd_uthread_vec);
780 inferior_pid = find_active_thread ();
782 add_thread (inferior_pid);
786 /* This routine is called to find out if the inferior is using threads.
787 We check for the _thread_run and _thread_list globals. */
790 freebsd_uthread_new_objfile (objfile)
791 struct objfile *objfile;
793 struct minimal_symbol *ms;
797 freebsd_uthread_active = 0;
801 ms = lookup_minimal_symbol ("_thread_run", NULL, objfile);
806 P_thread_run = SYMBOL_VALUE_ADDRESS (ms);
808 ms = lookup_minimal_symbol ("_thread_list", NULL, objfile);
813 P_thread_list = SYMBOL_VALUE_ADDRESS (ms);
815 #define OFFSET_SYM(field) "_thread_" #field "_offset"
816 #define LOOKUP_OFFSET(field) \
818 ms = lookup_minimal_symbol (OFFSET_SYM(field), NULL, objfile); \
821 P_thread_##field##_offset = SYMBOL_VALUE_ADDRESS (ms); \
824 #define VALUE_SYM(name) "_thread_" #name "_value"
825 #define LOOKUP_VALUE(name) \
827 ms = lookup_minimal_symbol (VALUE_SYM(name), NULL, objfile); \
830 P_thread_##name##_value = SYMBOL_VALUE_ADDRESS (ms); \
834 LOOKUP_OFFSET(uniqueid);
835 LOOKUP_OFFSET(state);
839 LOOKUP_VALUE(PS_RUNNING);
840 LOOKUP_VALUE(PS_DEAD);
842 freebsd_uthread_active = 1;
846 freebsd_uthread_has_exited (pid, wait_status, exit_status)
851 int t = child_ops.to_has_exited (pid, wait_status, exit_status);
857 /* Clean up after the inferior dies. */
860 freebsd_uthread_mourn_inferior ()
862 inferior_pid = main_pid; /* don't bother to restore inferior_pid */
863 child_ops.to_mourn_inferior ();
864 unpush_target (&freebsd_uthread_ops);
867 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
870 freebsd_uthread_can_run ()
872 return child_suppress_run;
876 freebsd_uthread_thread_alive (pid)
879 struct cleanup *old_chain;
880 struct cached_pthread *thread;
883 if (freebsd_uthread_attaching)
887 * We can get called from child_ops.to_wait() which passes the underlying
888 * pid (without a thread number).
890 if (THREADID_TID(pid) == 0)
893 old_chain = save_inferior_pid ();
894 inferior_pid = main_pid;
896 if (find_pthread_addr (pid) != 0)
898 thread = find_pthread (pid);
899 ret = (thread->state != PS_DEAD_value);
902 do_cleanups (old_chain);
911 freebsd_uthread_stop ()
913 struct cleanup *old_chain;
914 old_chain = save_inferior_pid ();
915 inferior_pid = main_pid;
917 child_ops.to_stop ();
919 do_cleanups (old_chain);
923 freebsd_uthread_find_new_threads ()
928 struct cleanup *old_chain;
930 old_chain = save_inferior_pid ();
931 inferior_pid = main_pid;
933 read_memory ((CORE_ADDR)P_thread_list,
939 READ_FIELD(ptr, int, state, state);
940 READ_FIELD(ptr, u_int64_t, uniqueid, uniqueid);
941 if (state != PS_DEAD_value &&
942 !in_thread_list (find_pid(uniqueid)))
943 add_thread (find_pid(uniqueid));
944 ptr = read_pthread_next(ptr);
947 do_cleanups (old_chain);
952 /* MUST MATCH enum pthread_state */
953 static const char *statenames[] = {
977 freebsd_uthread_get_thread_info (ref, selection, info)
980 struct gdb_ext_thread_info *info;
983 struct cached_pthread *thread = find_pthread (pid);
984 struct cleanup *old_chain;
986 old_chain = save_inferior_pid ();
987 inferior_pid = main_pid;
989 memset(&info->threadid, 0, OPAQUETHREADBYTES);
991 memcpy(&info->threadid, ref, sizeof *ref);
992 info->active = thread->state == PS_RUNNING_value;
993 strcpy(info->display, statenames[thread->state]);
995 read_memory ((CORE_ADDR) thread->name, info->shortname, 32);
997 strcpy(info->shortname, "");
999 do_cleanups (old_chain);
1004 freebsd_uthread_pid_to_str (pid)
1007 static char buf[30];
1009 if (STREQ (current_target.to_shortname, "freebsd-uthreads"))
1010 sprintf (buf, "process %d, thread %d\0",
1011 THREADID_PID(pid), THREADID_TID(pid));
1013 sprintf (buf, "process %d\0", pid);
1020 init_freebsd_uthread_ops ()
1022 freebsd_uthread_ops.to_shortname = "freebsd-uthreads";
1023 freebsd_uthread_ops.to_longname = "FreeBSD uthreads";
1024 freebsd_uthread_ops.to_doc = "FreeBSD user threads support.";
1025 freebsd_uthread_ops.to_open = freebsd_uthread_open;
1026 freebsd_uthread_ops.to_attach = freebsd_uthread_attach;
1027 freebsd_uthread_ops.to_post_attach = freebsd_uthread_post_attach;
1028 freebsd_uthread_ops.to_detach = freebsd_uthread_detach;
1029 freebsd_uthread_ops.to_resume = freebsd_uthread_resume;
1030 freebsd_uthread_ops.to_wait = freebsd_uthread_wait;
1031 freebsd_uthread_ops.to_fetch_registers = freebsd_uthread_fetch_registers;
1032 freebsd_uthread_ops.to_store_registers = freebsd_uthread_store_registers;
1033 freebsd_uthread_ops.to_prepare_to_store = freebsd_uthread_prepare_to_store;
1034 freebsd_uthread_ops.to_xfer_memory = freebsd_uthread_xfer_memory;
1035 freebsd_uthread_ops.to_files_info = freebsd_uthread_files_info;
1036 freebsd_uthread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1037 freebsd_uthread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1038 freebsd_uthread_ops.to_terminal_init = terminal_init_inferior;
1039 freebsd_uthread_ops.to_terminal_inferior = terminal_inferior;
1040 freebsd_uthread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1041 freebsd_uthread_ops.to_terminal_ours = terminal_ours;
1042 freebsd_uthread_ops.to_terminal_info = child_terminal_info;
1043 freebsd_uthread_ops.to_kill = freebsd_uthread_kill_inferior;
1044 freebsd_uthread_ops.to_create_inferior = freebsd_uthread_create_inferior;
1045 freebsd_uthread_ops.to_has_exited = freebsd_uthread_has_exited;
1046 freebsd_uthread_ops.to_mourn_inferior = freebsd_uthread_mourn_inferior;
1047 freebsd_uthread_ops.to_can_run = freebsd_uthread_can_run;
1048 freebsd_uthread_ops.to_notice_signals = freebsd_uthread_notice_signals;
1049 freebsd_uthread_ops.to_thread_alive = freebsd_uthread_thread_alive;
1050 freebsd_uthread_ops.to_stop = freebsd_uthread_stop;
1051 freebsd_uthread_ops.to_stratum = process_stratum;
1052 freebsd_uthread_ops.to_has_all_memory = 1;
1053 freebsd_uthread_ops.to_has_memory = 1;
1054 freebsd_uthread_ops.to_has_stack = 1;
1055 freebsd_uthread_ops.to_has_registers = 1;
1056 freebsd_uthread_ops.to_has_execution = 1;
1057 freebsd_uthread_ops.to_has_thread_control = 0;
1058 freebsd_uthread_ops.to_magic = OPS_MAGIC;
1060 freebsd_uthread_vec.find_new_threads = freebsd_uthread_find_new_threads;
1061 freebsd_uthread_vec.get_thread_info = freebsd_uthread_get_thread_info;
1065 _initialize_freebsd_uthread ()
1067 init_freebsd_uthread_ops ();
1068 add_target (&freebsd_uthread_ops);
1070 child_suppress_run = 1;