1 /* Copyright (C) 2008-2014 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <sys/procfs.h>
27 #include "gdbthread.h"
29 #include <pthread_debug.h>
31 /* Print debugging traces if set to non-zero. */
32 static int debug_dec_thread = 0;
34 /* Non-zero if the dec-thread layer is active. */
35 static int dec_thread_active = 0;
37 /* The pthread_debug context. */
38 pthreadDebugContext_t debug_context;
40 /* The dec-thread target_ops structure. */
41 static struct target_ops dec_thread_ops;
43 /* Print a debug trace if DEBUG_DEC_THREAD is set (its value is adjusted
44 by the user using "set debug dec-thread ..."). */
47 debug (char *format, ...)
53 va_start (args, format);
54 printf_unfiltered ("DEC Threads: ");
55 vprintf_unfiltered (format, args);
56 printf_unfiltered ("\n");
61 /* pthread debug callbacks. */
64 suspend_clbk (void *caller_context)
70 resume_clbk (void *caller_context)
76 hold_clbk (void *caller_context, pthreadDebugKId_t kernel_tid)
82 unhold_clbk (void *caller_context, pthreadDebugKId_t kernel_tid)
88 read_clbk (void *caller_context, void *address, void *buffer,
91 int status = target_read_memory ((CORE_ADDR) address, buffer, size);
100 write_clbk (void *caller_context, void *address, void *buffer,
103 int status = target_write_memory ((CORE_ADDR) address, buffer, size);
111 /* Get integer regs. */
114 get_reg_clbk(void *caller_context, pthreadDebugGetRegRtn_t regs,
115 pthreadDebugKId_t kernel_tid)
117 debug ("get_reg_clbk");
119 /* Not sure that we actually need to do anything in this callback. */
123 /* Set integer regs. */
126 set_reg_clbk(void *caller_context, const pthreadDebugRegs_t *regs,
127 pthreadDebugKId_t kernel_tid)
129 debug ("set_reg_clbk");
131 /* Not sure that we actually need to do anything in this callback. */
136 output_clbk (void *caller_context, char *line)
138 printf_filtered ("%s\n", line);
143 error_clbk (void *caller_context, char *line)
145 fprintf_filtered (gdb_stderr, "%s\n", line);
149 /* Get floating-point regs. */
152 get_fpreg_clbk (void *caller_context, pthreadDebugFregs_p fregs,
153 pthreadDebugKId_t kernel_tid)
155 debug ("get_fpreg_clbk");
157 /* Not sure that we actually need to do anything in this callback. */
161 /* Set floating-point regs. */
164 set_fpreg_clbk (void *caller_context, const pthreadDebugFregs_t *fregs,
165 pthreadDebugKId_t kernel_tid)
167 debug ("set_fpreg_clbk");
169 /* Not sure that we actually need to do anything in this callback. */
174 malloc_clbk (void *caller_context, size_t size)
176 return xmalloc (size);
180 free_clbk (void *caller_context, void *address)
186 kthdinfo_clbk (pthreadDebugClient_t caller_context,
187 pthreadDebugKId_t kernel_tid,
188 pthreadDebugKThreadInfo_p thread_info)
194 speckthd_clbk (pthreadDebugClient_t caller_context,
195 pthreadDebugSpecialType_t type,
196 pthreadDebugKId_t *kernel_tid)
201 static pthreadDebugCallbacks_t debug_callbacks =
203 PTHREAD_DEBUG_VERSION,
204 (pthreadDebugGetMemRtn_t) read_clbk,
205 (pthreadDebugSetMemRtn_t) write_clbk,
211 (pthreadDebugGetFregRtn_t) get_fpreg_clbk,
212 (pthreadDebugSetFregRtn_t) set_fpreg_clbk,
213 (pthreadDebugGetRegRtn_t) get_reg_clbk,
214 (pthreadDebugSetRegRtn_t) set_reg_clbk,
215 (pthreadDebugOutputRtn_t) output_clbk,
216 (pthreadDebugOutputRtn_t) error_clbk,
222 /* Activate thread support if appropriate. Do nothing if thread
223 support is already active. */
226 enable_dec_thread (void)
228 struct bound_minimal_symbol msym;
229 void* caller_context;
232 /* If already active, nothing more to do. */
233 if (dec_thread_active)
236 msym = lookup_minimal_symbol ("__pthread_dbg_symtable", NULL, NULL);
237 if (msym.minsym == NULL)
239 debug ("enable_dec_thread: No __pthread_dbg_symtable");
243 status = pthreadDebugContextInit (&caller_context, &debug_callbacks,
244 (void *) SYMBOL_VALUE_ADDRESS (msym.minsym),
246 if (status != ESUCCESS)
248 debug ("enable_dec_thread: pthreadDebugContextInit -> %d",
253 push_target (&dec_thread_ops);
254 dec_thread_active = 1;
256 debug ("enable_dec_thread: Thread support enabled.");
259 /* Deactivate thread support. Do nothing if thread support is
263 disable_dec_thread (void)
265 if (!dec_thread_active)
268 pthreadDebugContextDestroy (debug_context);
269 unpush_target (&dec_thread_ops);
270 dec_thread_active = 0;
273 /* A structure that contains a thread ID and is associated
274 pthreadDebugThreadInfo_t data. */
276 struct dec_thread_info
278 pthreadDebugId_t thread;
279 pthreadDebugThreadInfo_t info;
281 typedef struct dec_thread_info dec_thread_info_s;
283 /* The list of user threads. */
285 DEF_VEC_O (dec_thread_info_s);
286 VEC(dec_thread_info_s) *dec_thread_list;
288 /* Release the memory used by the given VECP thread list pointer.
289 Then set *VECP to NULL. */
292 free_dec_thread_info_vec (VEC(dec_thread_info_s) **vecp)
295 struct dec_thread_info *item;
296 VEC(dec_thread_info_s) *vec = *vecp;
298 for (i = 0; VEC_iterate (dec_thread_info_s, vec, i, item); i++)
300 VEC_free (dec_thread_info_s, vec);
304 /* Return a thread's ptid given its associated INFO. */
307 ptid_build_from_info (struct dec_thread_info info)
309 int pid = ptid_get_pid (inferior_ptid);
311 return ptid_build (pid, 0, (long) info.thread);
314 /* Return non-zero if PTID is still alive.
316 Assumes that DEC_THREAD_LIST is up to date. */
318 dec_thread_ptid_is_alive (ptid_t ptid)
320 pthreadDebugId_t tid = ptid_get_tid (ptid);
322 struct dec_thread_info *info;
325 /* This is the thread corresponding to the process. This ptid
326 is always alive until the program exits. */
329 /* Search whether an entry with the same tid exists in the dec-thread
330 list of threads. If it does, then the thread is still alive.
331 No match found means that the thread must be dead, now. */
332 for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info); i++)
333 if (info->thread == tid)
338 /* Recompute the list of user threads and store the result in
342 update_dec_thread_list (void)
344 pthreadDebugId_t thread;
345 pthreadDebugThreadInfo_t info;
348 free_dec_thread_info_vec (&dec_thread_list);
349 res = pthreadDebugThdSeqInit (debug_context, &thread);
350 while (res == ESUCCESS)
353 res = pthreadDebugThdGetInfo (debug_context, thread, &info);
355 warning (_("unable to get thread info, ignoring thread %ld"),
357 else if (info.kind == PTHREAD_DEBUG_THD_KIND_INITIAL
358 || info.kind == PTHREAD_DEBUG_THD_KIND_NORMAL)
360 struct dec_thread_info *item =
361 xmalloc (sizeof (struct dec_thread_info));
363 item->thread = thread;
365 VEC_safe_push (dec_thread_info_s, dec_thread_list, item);
367 res = pthreadDebugThdSeqNext (debug_context, &thread);
369 pthreadDebugThdSeqDestroy (debug_context);
372 /* A callback to count the number of threads known to GDB. */
375 dec_thread_count_gdb_threads (struct thread_info *ignored, void *context)
377 int *count = (int *) context;
383 /* A callback that saves the given thread INFO at the end of an
384 array. The end of the array is given in the CONTEXT and is
385 incremented once the info has been added. */
388 dec_thread_add_gdb_thread (struct thread_info *info, void *context)
390 struct thread_info ***listp = (struct thread_info ***) context;
397 /* Find new threads. */
400 dec_thread_find_new_threads (struct target_ops *ops)
403 struct dec_thread_info *info;
405 update_dec_thread_list ();
406 for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info); i++)
408 ptid_t ptid = ptid_build_from_info (*info);
410 if (!in_thread_list (ptid))
415 /* Implement the update_thread_list target_ops method. */
418 dec_thread_update_thread_list (struct target_ops *ops)
421 struct dec_thread_info *info;
423 /* Delete dead threads. */
426 /* Now find new threads. */
427 dec_thread_find_new_threads (ops);
430 /* Resynchronize the list of threads known by GDB with the actual
431 list of threads reported by libpthread_debug. */
434 resync_thread_list (struct target_ops *ops)
437 int num_gdb_threads = 0;
438 struct thread_info **gdb_thread_list;
439 struct thread_info **next_thread_info;
441 /* Add new threads. */
442 dec_thread_find_new_threads (ops);
444 /* Remove threads that no longer exist. To help with the search,
445 we build an array of GDB threads, and then iterate over this
448 iterate_over_threads (dec_thread_count_gdb_threads,
449 (void *) &num_gdb_threads);
450 gdb_thread_list = alloca (num_gdb_threads * sizeof (struct thread_info *));
451 next_thread_info = gdb_thread_list;
452 iterate_over_threads (dec_thread_add_gdb_thread, (void *) &next_thread_info);
454 for (i = 0; i < num_gdb_threads; i++)
455 if (!dec_thread_ptid_is_alive (gdb_thread_list[i]->ptid))
456 delete_thread (gdb_thread_list[i]->ptid);
459 /* The "to_detach" method of the dec_thread_ops. */
462 dec_thread_detach (struct target_ops *ops, const char *args, int from_tty)
464 struct target_ops *beneath = find_target_beneath (ops);
466 debug ("dec_thread_detach");
468 disable_dec_thread ();
469 beneath->to_detach (beneath, args, from_tty);
472 /* Return the ptid of the thread that is currently active. */
475 get_active_ptid (void)
478 struct dec_thread_info *info;
480 for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info);
482 if (info->info.state == PTHREAD_DEBUG_STATE_RUNNING)
483 return ptid_build_from_info (*info);
485 /* No active thread found. This can happen when the program
490 /* The "to_wait" method of the dec_thread_ops. */
493 dec_thread_wait (struct target_ops *ops,
494 ptid_t ptid, struct target_waitstatus *status, int options)
497 struct target_ops *beneath = find_target_beneath (ops);
499 debug ("dec_thread_wait");
501 ptid = beneath->to_wait (beneath, ptid, status, options);
503 /* The ptid returned by the target beneath us is the ptid of the process.
504 We need to find which thread is currently active and return its ptid. */
505 resync_thread_list (ops);
506 active_ptid = get_active_ptid ();
507 if (ptid_equal (active_ptid, null_ptid))
512 /* Fetch the general purpose and floating point registers for the given
513 thread TID, and store the result in GREGSET and FPREGSET. Return
514 zero if successful. */
517 dec_thread_get_regsets (pthreadDebugId_t tid, gdb_gregset_t *gregset,
518 gdb_fpregset_t *fpregset)
521 pthreadDebugRegs_t regs;
522 pthreadDebugFregs_t fregs;
524 res = pthreadDebugThdGetReg (debug_context, tid, ®s);
527 debug ("dec_thread_get_regsets: pthreadDebugThdGetReg -> %d", res);
530 memcpy (gregset->regs, ®s, sizeof (regs));
532 res = pthreadDebugThdGetFreg (debug_context, tid, &fregs);
535 debug ("dec_thread_get_regsets: pthreadDebugThdGetFreg -> %d", res);
538 memcpy (fpregset->regs, &fregs, sizeof (fregs));
543 /* The "to_fetch_registers" method of the dec_thread_ops.
545 Because the dec-thread debug API doesn't allow us to fetch
546 only one register, we simply ignore regno and fetch+supply all
550 dec_thread_fetch_registers (struct target_ops *ops,
551 struct regcache *regcache, int regno)
553 pthreadDebugId_t tid = ptid_get_tid (inferior_ptid);
558 debug ("dec_thread_fetch_registers (tid=%ld, regno=%d)", tid, regno);
561 if (tid == 0 || ptid_equal (inferior_ptid, get_active_ptid ()))
563 struct target_ops *beneath = find_target_beneath (ops);
565 beneath->to_fetch_registers (beneath, regcache, regno);
569 res = dec_thread_get_regsets (tid, &gregset, &fpregset);
573 supply_gregset (regcache, &gregset);
574 supply_fpregset (regcache, &fpregset);
577 /* Store the registers given in GREGSET and FPREGSET into the associated
578 general purpose and floating point registers of thread TID. Return
579 zero if successful. */
582 dec_thread_set_regsets (pthreadDebugId_t tid, gdb_gregset_t gregset,
583 gdb_fpregset_t fpregset)
586 pthreadDebugRegs_t regs;
587 pthreadDebugFregs_t fregs;
589 memcpy (®s, gregset.regs, sizeof (regs));
590 res = pthreadDebugThdSetReg (debug_context, tid, ®s);
593 debug ("dec_thread_set_regsets: pthreadDebugThdSetReg -> %d", res);
597 memcpy (&fregs, fpregset.regs, sizeof (fregs));
598 res = pthreadDebugThdSetFreg (debug_context, tid, &fregs);
601 debug ("dec_thread_set_regsets: pthreadDebugThdSetFreg -> %d", res);
608 /* The "to_store_registers" method of the dec_thread_ops.
610 Because the dec-thread debug API doesn't allow us to store
611 just one register, we store all the registers. */
614 dec_thread_store_registers (struct target_ops *ops,
615 struct regcache *regcache, int regno)
617 pthreadDebugId_t tid = ptid_get_tid (inferior_ptid);
622 debug ("dec_thread_store_registers (tid=%ld, regno=%d)", tid, regno);
624 if (tid == 0 || ptid_equal (inferior_ptid, get_active_ptid ()))
626 struct target_ops *beneath = find_target_beneath (ops);
628 beneath->to_store_registers (beneath, regcache, regno);
632 /* FIXME: brobecker/2008-05-28: I wonder if we could simply check
633 in which register set the register is and then only store the
634 registers for that register set, instead of storing both register
636 fill_gregset (regcache, &gregset, -1);
637 fill_fpregset (regcache, &fpregset, -1);
639 res = dec_thread_set_regsets (tid, gregset, fpregset);
641 warning (_("failed to store registers."));
644 /* The "to_mourn_inferior" method of the dec_thread_ops. */
647 dec_thread_mourn_inferior (struct target_ops *ops)
649 struct target_ops *beneath = find_target_beneath (ops);
651 debug ("dec_thread_mourn_inferior");
653 disable_dec_thread ();
654 beneath->to_mourn_inferior (beneath);
657 /* The "to_thread_alive" method of the dec_thread_ops. */
659 dec_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
661 debug ("dec_thread_thread_alive (tid=%ld)", ptid_get_tid (ptid));
663 /* The thread list maintained by GDB is up to date, since we update
664 it everytime we stop. So check this list. */
665 return in_thread_list (ptid);
668 /* The "to_pid_to_str" method of the dec_thread_ops. */
671 dec_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
673 static char *ret = NULL;
675 if (ptid_get_tid (ptid) == 0)
677 struct target_ops *beneath = find_target_beneath (ops);
679 return beneath->to_pid_to_str (beneath, ptid);
682 /* Free previous return value; a new one will be allocated by
686 ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
690 /* A "new-objfile" observer. Used to activate/deactivate dec-thread
694 dec_thread_new_objfile_observer (struct objfile *objfile)
697 enable_dec_thread ();
699 disable_dec_thread ();
702 /* The "to_get_ada_task_ptid" method of the dec_thread_ops. */
705 dec_thread_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
708 struct dec_thread_info *info;
710 debug ("dec_thread_get_ada_task_ptid (struct target_ops *self,"
711 " lwp=0x%lx, thread=0x%lx)",
714 for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info);
716 if (info->info.teb == (pthread_t) thread)
717 return ptid_build_from_info (*info);
719 warning (_("Could not find thread id from THREAD = 0x%lx"), thread);
720 return inferior_ptid;
724 init_dec_thread_ops (void)
726 dec_thread_ops.to_shortname = "dec-threads";
727 dec_thread_ops.to_longname = _("DEC threads support");
728 dec_thread_ops.to_doc = _("DEC threads support");
729 dec_thread_ops.to_detach = dec_thread_detach;
730 dec_thread_ops.to_wait = dec_thread_wait;
731 dec_thread_ops.to_fetch_registers = dec_thread_fetch_registers;
732 dec_thread_ops.to_store_registers = dec_thread_store_registers;
733 dec_thread_ops.to_mourn_inferior = dec_thread_mourn_inferior;
734 dec_thread_ops.to_thread_alive = dec_thread_thread_alive;
735 dec_thread_ops.to_update_thread_list = dec_thread_update_thread_list;
736 dec_thread_ops.to_pid_to_str = dec_thread_pid_to_str;
737 dec_thread_ops.to_stratum = thread_stratum;
738 dec_thread_ops.to_get_ada_task_ptid = dec_thread_get_ada_task_ptid;
739 dec_thread_ops.to_magic = OPS_MAGIC;
743 _initialize_dec_thread (void)
745 init_dec_thread_ops ();
746 complete_target_initialization (&dec_thread_ops);
748 observer_attach_new_objfile (dec_thread_new_objfile_observer);
750 add_setshow_boolean_cmd ("dec-thread", class_maintenance, &debug_dec_thread,
751 _("Set debugging of DEC threads module."),
752 _("Show debugging of DEC threads module."),
753 _("Enables debugging output (used to debug GDB)."),
755 &setdebuglist, &showdebuglist);