gdb/bsd-uthread.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* BSD user-level threads support.

  2.    Copyright (C) 2005-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "gdbthread.h"
  17. #include "inferior.h"
  18. #include "objfiles.h"
  19. #include "observer.h"
  20. #include "regcache.h"
  21. #include "solib.h"
  22. #include "solist.h"
  23. #include "symfile.h"
  24. #include "target.h"

  25. #include "gdb_obstack.h"

  26. #include "bsd-uthread.h"

  27. /* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target.  */
  28. static struct target_ops *bsd_uthread_ops_hack;


  29. /* Architecture-specific operations.  */

  30. /* Per-architecture data key.  */
  31. static struct gdbarch_data *bsd_uthread_data;

  32. struct bsd_uthread_ops
  33. {
  34.   /* Supply registers for an inactive thread to a register cache.  */
  35.   void (*supply_uthread)(struct regcache *, int, CORE_ADDR);

  36.   /* Collect registers for an inactive thread from a register cache.  */
  37.   void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
  38. };

  39. static void *
  40. bsd_uthread_init (struct obstack *obstack)
  41. {
  42.   struct bsd_uthread_ops *ops;

  43.   ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
  44.   return ops;
  45. }

  46. /* Set the function that supplies registers from an inactive thread
  47.    for architecture GDBARCH to SUPPLY_UTHREAD.  */

  48. void
  49. bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
  50.                                 void (*supply_uthread) (struct regcache *,
  51.                                                         int, CORE_ADDR))
  52. {
  53.   struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
  54.   ops->supply_uthread = supply_uthread;
  55. }

  56. /* Set the function that collects registers for an inactive thread for
  57.    architecture GDBARCH to SUPPLY_UTHREAD.  */

  58. void
  59. bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
  60.                          void (*collect_uthread) (const struct regcache *,
  61.                                                   int, CORE_ADDR))
  62. {
  63.   struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
  64.   ops->collect_uthread = collect_uthread;
  65. }

  66. /* Magic number to help recognize a valid thread structure.  */
  67. #define BSD_UTHREAD_PTHREAD_MAGIC        0xd09ba115

  68. /* Check whether the thread structure at ADDR is valid.  */

  69. static void
  70. bsd_uthread_check_magic (CORE_ADDR addr)
  71. {
  72.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  73.   ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);

  74.   if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
  75.     error (_("Bad magic"));
  76. }

  77. /* Thread states.  */
  78. #define BSD_UTHREAD_PS_RUNNING        0
  79. #define BSD_UTHREAD_PS_DEAD        18

  80. /* Address of the pointer to the thread structure for the running
  81.    thread.  */
  82. static CORE_ADDR bsd_uthread_thread_run_addr;

  83. /* Address of the list of all threads.  */
  84. static CORE_ADDR bsd_uthread_thread_list_addr;

  85. /* Offsets of various "interesting" bits in the thread structure.  */
  86. static int bsd_uthread_thread_state_offset = -1;
  87. static int bsd_uthread_thread_next_offset = -1;
  88. static int bsd_uthread_thread_ctx_offset;

  89. /* Name of shared threads library.  */
  90. static const char *bsd_uthread_solib_name;

  91. /* Non-zero if the thread startum implemented by this module is active.  */
  92. static int bsd_uthread_active;

  93. static CORE_ADDR
  94. bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
  95. {
  96.   struct bound_minimal_symbol sym;

  97.   sym = lookup_minimal_symbol (name, NULL, objfile);
  98.   if (sym.minsym)
  99.     return BMSYMBOL_VALUE_ADDRESS (sym);

  100.   return 0;
  101. }

  102. static int
  103. bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
  104. {
  105.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  106.   CORE_ADDR addr;

  107.   addr = bsd_uthread_lookup_address (name, objfile);
  108.   if (addr == 0)
  109.     return 0;

  110.   return read_memory_unsigned_integer (addr, 4, byte_order);
  111. }

  112. static CORE_ADDR
  113. bsd_uthread_read_memory_address (CORE_ADDR addr)
  114. {
  115.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  116.   return read_memory_typed_address (addr, ptr_type);
  117. }

  118. /* If OBJFILE contains the symbols corresponding to one of the
  119.    supported user-level threads libraries, activate the thread stratum
  120.    implemented by this module.  */

  121. static int
  122. bsd_uthread_activate (struct objfile *objfile)
  123. {
  124.   struct gdbarch *gdbarch = target_gdbarch ();
  125.   struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);

  126.   /* Skip if the thread stratum has already been activated.  */
  127.   if (bsd_uthread_active)
  128.     return 0;

  129.   /* There's no point in enabling this module if no
  130.      architecture-specific operations are provided.  */
  131.   if (!ops->supply_uthread)
  132.     return 0;

  133.   bsd_uthread_thread_run_addr =
  134.     bsd_uthread_lookup_address ("_thread_run", objfile);
  135.   if (bsd_uthread_thread_run_addr == 0)
  136.     return 0;

  137.   bsd_uthread_thread_list_addr =
  138.     bsd_uthread_lookup_address ("_thread_list", objfile);
  139.   if (bsd_uthread_thread_list_addr == 0)
  140.     return 0;

  141.   bsd_uthread_thread_state_offset =
  142.     bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
  143.   if (bsd_uthread_thread_state_offset == 0)
  144.     return 0;

  145.   bsd_uthread_thread_next_offset =
  146.     bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
  147.   if (bsd_uthread_thread_next_offset == 0)
  148.     return 0;

  149.   bsd_uthread_thread_ctx_offset =
  150.     bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);

  151.   push_target (bsd_uthread_ops_hack);
  152.   bsd_uthread_active = 1;
  153.   return 1;
  154. }

  155. /* Cleanup due to deactivation.  */

  156. static void
  157. bsd_uthread_close (struct target_ops *self)
  158. {
  159.   bsd_uthread_active = 0;
  160.   bsd_uthread_thread_run_addr = 0;
  161.   bsd_uthread_thread_list_addr = 0;
  162.   bsd_uthread_thread_state_offset = 0;
  163.   bsd_uthread_thread_next_offset = 0;
  164.   bsd_uthread_thread_ctx_offset = 0;
  165.   bsd_uthread_solib_name = NULL;
  166. }

  167. /* Deactivate the thread stratum implemented by this module.  */

  168. static void
  169. bsd_uthread_deactivate (void)
  170. {
  171.   /* Skip if the thread stratum has already been deactivated.  */
  172.   if (!bsd_uthread_active)
  173.     return;

  174.   unpush_target (bsd_uthread_ops_hack);
  175. }

  176. static void
  177. bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
  178. {
  179.   bsd_uthread_activate (NULL);
  180. }

  181. /* Likely candidates for the threads library.  */
  182. static const char *bsd_uthread_solib_names[] =
  183. {
  184.   "/usr/lib/libc_r.so",                /* FreeBSD */
  185.   "/usr/lib/libpthread.so",        /* OpenBSD */
  186.   NULL
  187. };

  188. static void
  189. bsd_uthread_solib_loaded (struct so_list *so)
  190. {
  191.   const char **names = bsd_uthread_solib_names;

  192.   for (names = bsd_uthread_solib_names; *names; names++)
  193.     {
  194.       if (strncmp (so->so_original_name, *names, strlen (*names)) == 0)
  195.         {
  196.           solib_read_symbols (so, 0);

  197.           if (bsd_uthread_activate (so->objfile))
  198.             {
  199.               bsd_uthread_solib_name = so->so_original_name;
  200.               return;
  201.             }
  202.         }
  203.     }
  204. }

  205. static void
  206. bsd_uthread_solib_unloaded (struct so_list *so)
  207. {
  208.   if (!bsd_uthread_solib_name)
  209.     return;

  210.   if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
  211.     bsd_uthread_deactivate ();
  212. }

  213. static void
  214. bsd_uthread_mourn_inferior (struct target_ops *ops)
  215. {
  216.   struct target_ops *beneath = find_target_beneath (ops);
  217.   beneath->to_mourn_inferior (beneath);
  218.   bsd_uthread_deactivate ();
  219. }

  220. static void
  221. bsd_uthread_fetch_registers (struct target_ops *ops,
  222.                              struct regcache *regcache, int regnum)
  223. {
  224.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  225.   struct bsd_uthread_ops *uthread_ops = gdbarch_data (gdbarch, bsd_uthread_data);
  226.   CORE_ADDR addr = ptid_get_tid (inferior_ptid);
  227.   struct target_ops *beneath = find_target_beneath (ops);
  228.   CORE_ADDR active_addr;

  229.   /* Always fetch the appropriate registers from the layer beneath.  */
  230.   beneath->to_fetch_registers (beneath, regcache, regnum);

  231.   /* FIXME: That might have gotten us more than we asked for.  Make
  232.      sure we overwrite all relevant registers with values from the
  233.      thread structure.  This can go once we fix the underlying target.  */
  234.   regnum = -1;

  235.   active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
  236.   if (addr != 0 && addr != active_addr)
  237.     {
  238.       bsd_uthread_check_magic (addr);
  239.       uthread_ops->supply_uthread (regcache, regnum,
  240.                                    addr + bsd_uthread_thread_ctx_offset);
  241.     }
  242. }

  243. static void
  244. bsd_uthread_store_registers (struct target_ops *ops,
  245.                              struct regcache *regcache, int regnum)
  246. {
  247.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  248.   struct bsd_uthread_ops *uthread_ops = gdbarch_data (gdbarch, bsd_uthread_data);
  249.   struct target_ops *beneath = find_target_beneath (ops);
  250.   CORE_ADDR addr = ptid_get_tid (inferior_ptid);
  251.   CORE_ADDR active_addr;

  252.   active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
  253.   if (addr != 0 && addr != active_addr)
  254.     {
  255.       bsd_uthread_check_magic (addr);
  256.       uthread_ops->collect_uthread (regcache, regnum,
  257.                                     addr + bsd_uthread_thread_ctx_offset);
  258.     }
  259.   else
  260.     {
  261.       /* Updating the thread that is currently running; pass the
  262.          request to the layer beneath.  */
  263.       beneath->to_store_registers (beneath, regcache, regnum);
  264.     }
  265. }

  266. static ptid_t
  267. bsd_uthread_wait (struct target_ops *ops,
  268.                   ptid_t ptid, struct target_waitstatus *status, int options)
  269. {
  270.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  271.   CORE_ADDR addr;
  272.   struct target_ops *beneath = find_target_beneath (ops);

  273.   /* Pass the request to the layer beneath.  */
  274.   ptid = beneath->to_wait (beneath, ptid, status, options);

  275.   /* If the process is no longer alive, there's no point in figuring
  276.      out the thread ID.  It will fail anyway.  */
  277.   if (status->kind == TARGET_WAITKIND_SIGNALLED
  278.       || status->kind == TARGET_WAITKIND_EXITED)
  279.     return ptid;

  280.   /* Fetch the corresponding thread ID, and augment the returned
  281.      process ID with it.  */
  282.   addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
  283.   if (addr != 0)
  284.     {
  285.       gdb_byte buf[4];

  286.       /* FIXME: For executables linked statically with the threads
  287.          library, we end up here before the program has actually been
  288.          executed.  In that case ADDR will be garbage since it has
  289.          been read from the wrong virtual memory image.  */
  290.       if (target_read_memory (addr, buf, 4) == 0)
  291.         {
  292.           ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
  293.           if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
  294.             ptid = ptid_build (ptid_get_pid (ptid), 0, addr);
  295.         }
  296.     }

  297.   /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
  298.      ptid with tid set, then ptid is still the initial thread of
  299.      the process.  Notify GDB core about it.  */
  300.   if (ptid_get_tid (inferior_ptid) == 0
  301.       && ptid_get_tid (ptid) != 0 && !in_thread_list (ptid))
  302.     thread_change_ptid (inferior_ptid, ptid);

  303.   /* Don't let the core see a ptid without a corresponding thread.  */
  304.   if (!in_thread_list (ptid) || is_exited (ptid))
  305.     add_thread (ptid);

  306.   return ptid;
  307. }

  308. static void
  309. bsd_uthread_resume (struct target_ops *ops,
  310.                     ptid_t ptid, int step, enum gdb_signal sig)
  311. {
  312.   /* Pass the request to the layer beneath.  */
  313.   struct target_ops *beneath = find_target_beneath (ops);
  314.   beneath->to_resume (beneath, ptid, step, sig);
  315. }

  316. static int
  317. bsd_uthread_thread_alive (struct target_ops *ops, ptid_t ptid)
  318. {
  319.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  320.   struct target_ops *beneath = find_target_beneath (ops);
  321.   CORE_ADDR addr = ptid_get_tid (inferior_ptid);

  322.   if (addr != 0)
  323.     {
  324.       int offset = bsd_uthread_thread_state_offset;
  325.       ULONGEST state;

  326.       bsd_uthread_check_magic (addr);

  327.       state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
  328.       if (state == BSD_UTHREAD_PS_DEAD)
  329.         return 0;
  330.     }

  331.   return beneath->to_thread_alive (beneath, ptid);
  332. }

  333. static void
  334. bsd_uthread_update_thread_list (struct target_ops *ops)
  335. {
  336.   pid_t pid = ptid_get_pid (inferior_ptid);
  337.   int offset = bsd_uthread_thread_next_offset;
  338.   CORE_ADDR addr;

  339.   prune_threads ();

  340.   addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
  341.   while (addr != 0)
  342.     {
  343.       ptid_t ptid = ptid_build (pid, 0, addr);

  344.       if (!in_thread_list (ptid) || is_exited (ptid))
  345.         {
  346.           /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
  347.              is still the initial thread of the process.  Notify GDB
  348.              core about it.  */
  349.           if (ptid_get_tid (inferior_ptid) == 0)
  350.             thread_change_ptid (inferior_ptid, ptid);
  351.           else
  352.             add_thread (ptid);
  353.         }

  354.       addr = bsd_uthread_read_memory_address (addr + offset);
  355.     }
  356. }

  357. /* Possible states a thread can be in.  */
  358. static char *bsd_uthread_state[] =
  359. {
  360.   "RUNNING",
  361.   "SIGTHREAD",
  362.   "MUTEX_WAIT",
  363.   "COND_WAIT",
  364.   "FDLR_WAIT",
  365.   "FDLW_WAIT",
  366.   "FDR_WAIT",
  367.   "FDW_WAIT",
  368.   "FILE_WAIT",
  369.   "POLL_WAIT",
  370.   "SELECT_WAIT",
  371.   "SLEEP_WAIT",
  372.   "WAIT_WAIT",
  373.   "SIGSUSPEND",
  374.   "SIGWAIT",
  375.   "SPINBLOCK",
  376.   "JOIN",
  377.   "SUSPENDED",
  378.   "DEAD",
  379.   "DEADLOCK"
  380. };

  381. /* Return a string describing th state of the thread specified by
  382.    INFO.  */

  383. static char *
  384. bsd_uthread_extra_thread_info (struct target_ops *self,
  385.                                struct thread_info *info)
  386. {
  387.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  388.   CORE_ADDR addr = ptid_get_tid (info->ptid);

  389.   if (addr != 0)
  390.     {
  391.       int offset = bsd_uthread_thread_state_offset;
  392.       ULONGEST state;

  393.       state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
  394.       if (state < ARRAY_SIZE (bsd_uthread_state))
  395.         return bsd_uthread_state[state];
  396.     }

  397.   return NULL;
  398. }

  399. static char *
  400. bsd_uthread_pid_to_str (struct target_ops *ops, ptid_t ptid)
  401. {
  402.   if (ptid_get_tid (ptid) != 0)
  403.     {
  404.       static char buf[64];

  405.       xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx",
  406.                  ptid_get_pid (ptid), ptid_get_tid (ptid));
  407.       return buf;
  408.     }

  409.   return normal_pid_to_str (ptid);
  410. }

  411. static struct target_ops *
  412. bsd_uthread_target (void)
  413. {
  414.   struct target_ops *t = XCNEW (struct target_ops);

  415.   t->to_shortname = "bsd-uthreads";
  416.   t->to_longname = "BSD user-level threads";
  417.   t->to_doc = "BSD user-level threads";
  418.   t->to_close = bsd_uthread_close;
  419.   t->to_mourn_inferior = bsd_uthread_mourn_inferior;
  420.   t->to_fetch_registers = bsd_uthread_fetch_registers;
  421.   t->to_store_registers = bsd_uthread_store_registers;
  422.   t->to_wait = bsd_uthread_wait;
  423.   t->to_resume = bsd_uthread_resume;
  424.   t->to_thread_alive = bsd_uthread_thread_alive;
  425.   t->to_update_thread_list = bsd_uthread_update_thread_list;
  426.   t->to_extra_thread_info = bsd_uthread_extra_thread_info;
  427.   t->to_pid_to_str = bsd_uthread_pid_to_str;
  428.   t->to_stratum = thread_stratum;
  429.   t->to_magic = OPS_MAGIC;
  430.   bsd_uthread_ops_hack = t;

  431.   return t;
  432. }

  433. /* Provide a prototype to silence -Wmissing-prototypes.  */
  434. extern initialize_file_ftype _initialize_bsd_uthread;

  435. void
  436. _initialize_bsd_uthread (void)
  437. {
  438.   complete_target_initialization (bsd_uthread_target ());

  439.   bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);

  440.   observer_attach_inferior_created (bsd_uthread_inferior_created);
  441.   observer_attach_solib_loaded (bsd_uthread_solib_loaded);
  442.   observer_attach_solib_unloaded (bsd_uthread_solib_unloaded);
  443. }