gdb/aix-thread.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Low level interface for debugging AIX 4.3+ pthreads.

  2.    Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3.    Written by Nick Duffek <nsd@redhat.com>.

  4.    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.

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

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


  15. /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
  16.    debugging pthread applications.

  17.    Some name prefix conventions:
  18.      pthdb_        provided by libpthdebug.a
  19.      pdc_        callbacks that this module provides to libpthdebug.a
  20.      pd_        variables or functions interfacing with libpthdebug.a

  21.    libpthdebug peculiarities:

  22.      - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but
  23.        it's not documented, and after several calls it stops working
  24.        and causes other libpthdebug functions to fail.

  25.      - pthdb_tid_pthread() doesn't always work after
  26.        pthdb_session_update(), but it does work after cycling through
  27.        all threads using pthdb_pthread().

  28.      */

  29. #include "defs.h"
  30. #include "gdbthread.h"
  31. #include "target.h"
  32. #include "inferior.h"
  33. #include "regcache.h"
  34. #include "gdbcmd.h"
  35. #include "ppc-tdep.h"
  36. #include "observer.h"
  37. #include "objfiles.h"

  38. #include <procinfo.h>
  39. #include <sys/types.h>
  40. #include <sys/ptrace.h>
  41. #include <sys/reg.h>
  42. #include <sched.h>
  43. #include <sys/pthdebug.h>

  44. #if !HAVE_DECL_GETTHRDS
  45. extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
  46. #endif

  47. /* Whether to emit debugging output.  */
  48. static int debug_aix_thread;

  49. /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t.  */
  50. #ifndef PTHDB_VERSION_3
  51. #define pthdb_tid_t        tid_t
  52. #endif

  53. /* Return whether to treat PID as a debuggable thread id.  */

  54. #define PD_TID(ptid)        (pd_active && ptid_get_tid (ptid) != 0)

  55. /* pthdb_user_t value that we pass to pthdb functions.  0 causes
  56.    PTHDB_BAD_USER errors, so use 1.  */

  57. #define PD_USER        1

  58. /* Success and failure values returned by pthdb callbacks.  */

  59. #define PDC_SUCCESS        PTHDB_SUCCESS
  60. #define PDC_FAILURE        PTHDB_CALLBACK

  61. /* Private data attached to each element in GDB's thread list.  */

  62. struct private_thread_info {
  63.   pthdb_pthread_t pdtid;         /* thread's libpthdebug id */
  64.   pthdb_tid_t tid;                        /* kernel thread id */
  65. };

  66. /* Information about a thread of which libpthdebug is aware.  */

  67. struct pd_thread {
  68.   pthdb_pthread_t pdtid;
  69.   pthread_t pthid;
  70.   pthdb_tid_t tid;
  71. };

  72. /* This module's target-specific operations, active while pd_able is true.  */

  73. static struct target_ops aix_thread_ops;

  74. /* Address of the function that libpthread will call when libpthdebug
  75.    is ready to be initialized.  */

  76. static CORE_ADDR pd_brk_addr;

  77. /* Whether the current application is debuggable by pthdb.  */

  78. static int pd_able = 0;

  79. /* Whether a threaded application is being debugged.  */

  80. static int pd_active = 0;

  81. /* Whether the current architecture is 64-bit.
  82.    Only valid when pd_able is true.  */

  83. static int arch64;

  84. /* Forward declarations for pthdb callbacks.  */

  85. static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
  86. static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
  87. static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
  88. static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
  89.                           unsigned long long flags,
  90.                           pthdb_context_t *context);
  91. static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
  92.                            unsigned long long flags,
  93.                            pthdb_context_t *context);
  94. static int pdc_alloc (pthdb_user_t, size_t, void **);
  95. static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
  96. static int pdc_dealloc (pthdb_user_t, void *);

  97. /* pthdb callbacks.  */

  98. static pthdb_callbacks_t pd_callbacks = {
  99.   pdc_symbol_addrs,
  100.   pdc_read_data,
  101.   pdc_write_data,
  102.   pdc_read_regs,
  103.   pdc_write_regs,
  104.   pdc_alloc,
  105.   pdc_realloc,
  106.   pdc_dealloc,
  107.   NULL
  108. };

  109. /* Current pthdb session.  */

  110. static pthdb_session_t pd_session;

  111. /* Return a printable representation of pthdebug function return
  112.    STATUS.  */

  113. static char *
  114. pd_status2str (int status)
  115. {
  116.   switch (status)
  117.     {
  118.     case PTHDB_SUCCESS:                return "SUCCESS";
  119.     case PTHDB_NOSYS:                return "NOSYS";
  120.     case PTHDB_NOTSUP:                return "NOTSUP";
  121.     case PTHDB_BAD_VERSION:        return "BAD_VERSION";
  122.     case PTHDB_BAD_USER:        return "BAD_USER";
  123.     case PTHDB_BAD_SESSION:        return "BAD_SESSION";
  124.     case PTHDB_BAD_MODE:        return "BAD_MODE";
  125.     case PTHDB_BAD_FLAGS:        return "BAD_FLAGS";
  126.     case PTHDB_BAD_CALLBACK:        return "BAD_CALLBACK";
  127.     case PTHDB_BAD_POINTER:        return "BAD_POINTER";
  128.     case PTHDB_BAD_CMD:                return "BAD_CMD";
  129.     case PTHDB_BAD_PTHREAD:        return "BAD_PTHREAD";
  130.     case PTHDB_BAD_ATTR:        return "BAD_ATTR";
  131.     case PTHDB_BAD_MUTEX:        return "BAD_MUTEX";
  132.     case PTHDB_BAD_MUTEXATTR:        return "BAD_MUTEXATTR";
  133.     case PTHDB_BAD_COND:        return "BAD_COND";
  134.     case PTHDB_BAD_CONDATTR:        return "BAD_CONDATTR";
  135.     case PTHDB_BAD_RWLOCK:        return "BAD_RWLOCK";
  136.     case PTHDB_BAD_RWLOCKATTR:        return "BAD_RWLOCKATTR";
  137.     case PTHDB_BAD_KEY:                return "BAD_KEY";
  138.     case PTHDB_BAD_PTID:        return "BAD_PTID";
  139.     case PTHDB_BAD_TID:                return "BAD_TID";
  140.     case PTHDB_CALLBACK:        return "CALLBACK";
  141.     case PTHDB_CONTEXT:                return "CONTEXT";
  142.     case PTHDB_HELD:                return "HELD";
  143.     case PTHDB_NOT_HELD:        return "NOT_HELD";
  144.     case PTHDB_MEMORY:                return "MEMORY";
  145.     case PTHDB_NOT_PTHREADED:        return "NOT_PTHREADED";
  146.     case PTHDB_SYMBOL:                return "SYMBOL";
  147.     case PTHDB_NOT_AVAIL:        return "NOT_AVAIL";
  148.     case PTHDB_INTERNAL:        return "INTERNAL";
  149.     default:                        return "UNKNOWN";
  150.     }
  151. }

  152. /* A call to ptrace(REQ, ID, ...) just returned RET.  Check for
  153.    exceptional conditions and either return nonlocally or else return
  154.    1 for success and 0 for failure.  */

  155. static int
  156. ptrace_check (int req, int id, int ret)
  157. {
  158.   if (ret == 0 && !errno)
  159.     return 1;

  160.   /* According to ptrace(2), ptrace may fail with EPERM if "the
  161.      Identifier parameter corresponds to a kernel thread which is
  162.      stopped in kernel mode and whose computational state cannot be
  163.      read or written."  This happens quite often with register reads.  */

  164.   switch (req)
  165.     {
  166.     case PTT_READ_GPRS:
  167.     case PTT_READ_FPRS:
  168.     case PTT_READ_SPRS:
  169.       if (ret == -1 && errno == EPERM)
  170.         {
  171.           if (debug_aix_thread)
  172.             fprintf_unfiltered (gdb_stdlog,
  173.                                 "ptrace (%d, %d) = %d (errno = %d)\n",
  174.                                 req, id, ret, errno);
  175.           return ret == -1 ? 0 : 1;
  176.         }
  177.       break;
  178.     }
  179.   error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
  180.          req, id, ret, errno, safe_strerror (errno));
  181.   return 0/* Not reached.  */
  182. }

  183. /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
  184.    ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
  185.    Return success.  */

  186. #ifdef HAVE_PTRACE64
  187. # define ptracex(request, pid, addr, data, buf) \
  188.          ptrace64 (request, pid, addr, data, buf)
  189. #endif

  190. static int
  191. ptrace64aix (int req, int id, long long addr, int data, int *buf)
  192. {
  193.   errno = 0;
  194.   return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
  195. }

  196. /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
  197.    ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
  198.    Return success.  */

  199. #ifdef HAVE_PTRACE64
  200. # define ptrace(request, pid, addr, data, buf) \
  201.          ptrace64 (request, pid, addr, data, buf)
  202. # define addr_ptr long long
  203. #else
  204. # define addr_ptr int *
  205. #endif

  206. static int
  207. ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
  208. {
  209.   errno = 0;
  210.   return ptrace_check (req, id,
  211.                        ptrace (req, id, addr, data, buf));
  212. }

  213. /* If *PIDP is a composite process/thread id, convert it to a
  214.    process id.  */

  215. static void
  216. pid_to_prc (ptid_t *ptidp)
  217. {
  218.   ptid_t ptid;

  219.   ptid = *ptidp;
  220.   if (PD_TID (ptid))
  221.     *ptidp = pid_to_ptid (ptid_get_pid (ptid));
  222. }

  223. /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
  224.    the address of SYMBOLS[<i>].name.  */

  225. static int
  226. pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
  227. {
  228.   struct bound_minimal_symbol ms;
  229.   int i;
  230.   char *name;

  231.   if (debug_aix_thread)
  232.     fprintf_unfiltered (gdb_stdlog,
  233.       "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
  234.       user, (long) symbols, count);

  235.   for (i = 0; i < count; i++)
  236.     {
  237.       name = symbols[i].name;
  238.       if (debug_aix_thread)
  239.         fprintf_unfiltered (gdb_stdlog,
  240.                             "  symbols[%d].name = \"%s\"\n", i, name);

  241.       if (!*name)
  242.         symbols[i].addr = 0;
  243.       else
  244.         {
  245.           ms = lookup_minimal_symbol (name, NULL, NULL);
  246.           if (ms.minsym == NULL)
  247.             {
  248.               if (debug_aix_thread)
  249.                 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE\n");
  250.               return PDC_FAILURE;
  251.             }
  252.           symbols[i].addr = BMSYMBOL_VALUE_ADDRESS (ms);
  253.         }
  254.       if (debug_aix_thread)
  255.         fprintf_unfiltered (gdb_stdlog, "  symbols[%d].addr = %s\n",
  256.                             i, hex_string (symbols[i].addr));
  257.     }
  258.   if (debug_aix_thread)
  259.     fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS\n");
  260.   return PDC_SUCCESS;
  261. }

  262. /* Read registers call back function should be able to read the
  263.    context information of a debuggee kernel thread from an active
  264.    process or from a core file.  The information should be formatted
  265.    in context64 form for both 32-bit and 64-bit process.
  266.    If successful return 0, else non-zero is returned.  */

  267. static int
  268. pdc_read_regs (pthdb_user_t user,
  269.                pthdb_tid_t tid,
  270.                unsigned long long flags,
  271.                pthdb_context_t *context)
  272. {
  273.   /* This function doesn't appear to be used, so we could probably
  274.    just return 0 here.  HOWEVER, if it is not defined, the OS will
  275.    complain and several thread debug functions will fail.  In case
  276.    this is needed, I have implemented what I think it should do,
  277.    however this code is untested.  */

  278.   uint64_t gprs64[ppc_num_gprs];
  279.   uint32_t gprs32[ppc_num_gprs];
  280.   double fprs[ppc_num_fprs];
  281.   struct ptxsprs sprs64;
  282.   struct ptsprs sprs32;

  283.   if (debug_aix_thread)
  284.     fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
  285.                         (int) tid, hex_string (flags));

  286.   /* General-purpose registers.  */
  287.   if (flags & PTHDB_FLAG_GPRS)
  288.     {
  289.       if (arch64)
  290.         {
  291.           if (!ptrace64aix (PTT_READ_GPRS, tid,
  292.                             (unsigned long) gprs64, 0, NULL))
  293.             memset (gprs64, 0, sizeof (gprs64));
  294.           memcpy (context->gpr, gprs64, sizeof(gprs64));
  295.         }
  296.       else
  297.         {
  298.           if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
  299.             memset (gprs32, 0, sizeof (gprs32));
  300.           memcpy (context->gpr, gprs32, sizeof(gprs32));
  301.         }
  302.     }

  303.   /* Floating-point registers.  */
  304.   if (flags & PTHDB_FLAG_FPRS)
  305.     {
  306.       if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
  307.         memset (fprs, 0, sizeof (fprs));
  308.       memcpy (context->fpr, fprs, sizeof(fprs));
  309.     }

  310.   /* Special-purpose registers.  */
  311.   if (flags & PTHDB_FLAG_SPRS)
  312.     {
  313.       if (arch64)
  314.         {
  315.           if (!ptrace64aix (PTT_READ_SPRS, tid,
  316.                             (unsigned long) &sprs64, 0, NULL))
  317.             memset (&sprs64, 0, sizeof (sprs64));
  318.                 memcpy (&context->msr, &sprs64, sizeof(sprs64));
  319.         }
  320.       else
  321.         {
  322.           if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
  323.             memset (&sprs32, 0, sizeof (sprs32));
  324.                 memcpy (&context->msr, &sprs32, sizeof(sprs32));
  325.         }
  326.     }
  327.   return 0;
  328. }

  329. /* Write register function should be able to write requested context
  330.    information to specified debuggee's kernel thread id.
  331.    If successful return 0, else non-zero is returned.  */

  332. static int
  333. pdc_write_regs (pthdb_user_t user,
  334.                 pthdb_tid_t tid,
  335.                 unsigned long long flags,
  336.                 pthdb_context_t *context)
  337. {
  338.   /* This function doesn't appear to be used, so we could probably
  339.      just return 0 here.  HOWEVER, if it is not defined, the OS will
  340.      complain and several thread debug functions will fail.  In case
  341.      this is needed, I have implemented what I think it should do,
  342.      however this code is untested.  */

  343.   if (debug_aix_thread)
  344.     fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
  345.                         (int) tid, hex_string (flags));

  346.   /* General-purpose registers.  */
  347.   if (flags & PTHDB_FLAG_GPRS)
  348.     {
  349.       if (arch64)
  350.         ptrace64aix (PTT_WRITE_GPRS, tid,
  351.                      (unsigned long) context->gpr, 0, NULL);
  352.       else
  353.         ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
  354.     }

  355. /* Floating-point registers.  */
  356.   if (flags & PTHDB_FLAG_FPRS)
  357.     {
  358.       ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
  359.     }

  360.   /* Special-purpose registers.  */
  361.   if (flags & PTHDB_FLAG_SPRS)
  362.     {
  363.       if (arch64)
  364.         {
  365.           ptrace64aix (PTT_WRITE_SPRS, tid,
  366.                        (unsigned long) &context->msr, 0, NULL);
  367.         }
  368.       else
  369.         {
  370.           ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
  371.         }
  372.     }
  373.   return 0;
  374. }

  375. /* pthdb callback: read LEN bytes from process ADDR into BUF.  */

  376. static int
  377. pdc_read_data (pthdb_user_t user, void *buf,
  378.                pthdb_addr_t addr, size_t len)
  379. {
  380.   int status, ret;

  381.   if (debug_aix_thread)
  382.     fprintf_unfiltered (gdb_stdlog,
  383.       "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
  384.       user, (long) buf, hex_string (addr), len);

  385.   status = target_read_memory (addr, buf, len);
  386.   ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;

  387.   if (debug_aix_thread)
  388.     fprintf_unfiltered (gdb_stdlog, "  status=%d, returning %s\n",
  389.                         status, pd_status2str (ret));
  390.   return ret;
  391. }

  392. /* pthdb callback: write LEN bytes from BUF to process ADDR.  */

  393. static int
  394. pdc_write_data (pthdb_user_t user, void *buf,
  395.                 pthdb_addr_t addr, size_t len)
  396. {
  397.   int status, ret;

  398.   if (debug_aix_thread)
  399.     fprintf_unfiltered (gdb_stdlog,
  400.       "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
  401.       user, (long) buf, hex_string (addr), len);

  402.   status = target_write_memory (addr, buf, len);
  403.   ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;

  404.   if (debug_aix_thread)
  405.     fprintf_unfiltered (gdb_stdlog, "  status=%d, returning %s\n", status,
  406.                         pd_status2str (ret));
  407.   return ret;
  408. }

  409. /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
  410.    in BUFP.  */

  411. static int
  412. pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
  413. {
  414.   if (debug_aix_thread)
  415.     fprintf_unfiltered (gdb_stdlog,
  416.                         "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
  417.                         user, len, (long) bufp);
  418.   *bufp = xmalloc (len);
  419.   if (debug_aix_thread)
  420.     fprintf_unfiltered (gdb_stdlog,
  421.                         malloc returned 0x%lx\n", (long) *bufp);

  422.   /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
  423.      be returned.  */

  424.   return *bufp ? PDC_SUCCESS : PDC_FAILURE;
  425. }

  426. /* pthdb callback: reallocate BUF, which was allocated by the alloc or
  427.    realloc callback, so that it contains LEN bytes, and store a
  428.    pointer to the result in BUFP.  */

  429. static int
  430. pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
  431. {
  432.   if (debug_aix_thread)
  433.     fprintf_unfiltered (gdb_stdlog,
  434.       "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
  435.       user, (long) buf, len, (long) bufp);
  436.   *bufp = xrealloc (buf, len);
  437.   if (debug_aix_thread)
  438.     fprintf_unfiltered (gdb_stdlog,
  439.                         "  realloc returned 0x%lx\n", (long) *bufp);
  440.   return *bufp ? PDC_SUCCESS : PDC_FAILURE;
  441. }

  442. /* pthdb callback: free BUF, which was allocated by the alloc or
  443.    realloc callback.  */

  444. static int
  445. pdc_dealloc (pthdb_user_t user, void *buf)
  446. {
  447.   if (debug_aix_thread)
  448.     fprintf_unfiltered (gdb_stdlog,
  449.                         "pdc_free (user = %ld, buf = 0x%lx)\n", user,
  450.                         (long) buf);
  451.   xfree (buf);
  452.   return PDC_SUCCESS;
  453. }

  454. /* Return a printable representation of pthread STATE.  */

  455. static char *
  456. state2str (pthdb_state_t state)
  457. {
  458.   switch (state)
  459.     {
  460.     case PST_IDLE:
  461.       /* i18n: Like "Thread-Id %d, [state] idle" */
  462.       return _("idle");      /* being created */
  463.     case PST_RUN:
  464.       /* i18n: Like "Thread-Id %d, [state] running" */
  465.       return _("running");   /* running */
  466.     case PST_SLEEP:
  467.       /* i18n: Like "Thread-Id %d, [state] sleeping" */
  468.       return _("sleeping");  /* awaiting an event */
  469.     case PST_READY:
  470.       /* i18n: Like "Thread-Id %d, [state] ready" */
  471.       return _("ready");     /* runnable */
  472.     case PST_TERM:
  473.       /* i18n: Like "Thread-Id %d, [state] finished" */
  474.       return _("finished");  /* awaiting a join/detach */
  475.     default:
  476.       /* i18n: Like "Thread-Id %d, [state] unknown" */
  477.       return _("unknown");
  478.     }
  479. }

  480. /* qsort() comparison function for sorting pd_thread structs by pthid.  */

  481. static int
  482. pcmp (const void *p1v, const void *p2v)
  483. {
  484.   struct pd_thread *p1 = (struct pd_thread *) p1v;
  485.   struct pd_thread *p2 = (struct pd_thread *) p2v;
  486.   return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
  487. }

  488. /* iterate_over_threads() callback for counting GDB threads.

  489.    Do not count the main thread (whose tid is zero).  This matches
  490.    the list of threads provided by the pthreaddebug library, which
  491.    does not include that main thread either, and thus allows us
  492.    to compare the two lists.  */

  493. static int
  494. giter_count (struct thread_info *thread, void *countp)
  495. {
  496.   if (PD_TID (thread->ptid))
  497.     (*(int *) countp)++;
  498.   return 0;
  499. }

  500. /* iterate_over_threads() callback for accumulating GDB thread pids.

  501.    Do not include the main thread (whose tid is zero).  This matches
  502.    the list of threads provided by the pthreaddebug library, which
  503.    does not include that main thread either, and thus allows us
  504.    to compare the two lists.  */

  505. static int
  506. giter_accum (struct thread_info *thread, void *bufp)
  507. {
  508.   if (PD_TID (thread->ptid))
  509.     {
  510.       **(struct thread_info ***) bufp = thread;
  511.       (*(struct thread_info ***) bufp)++;
  512.     }
  513.   return 0;
  514. }

  515. /* ptid comparison function */

  516. static int
  517. ptid_cmp (ptid_t ptid1, ptid_t ptid2)
  518. {
  519.   int pid1, pid2;

  520.   if (ptid_get_pid (ptid1) < ptid_get_pid (ptid2))
  521.     return -1;
  522.   else if (ptid_get_pid (ptid1) > ptid_get_pid (ptid2))
  523.     return 1;
  524.   else if (ptid_get_tid (ptid1) < ptid_get_tid (ptid2))
  525.     return -1;
  526.   else if (ptid_get_tid (ptid1) > ptid_get_tid (ptid2))
  527.     return 1;
  528.   else if (ptid_get_lwp (ptid1) < ptid_get_lwp (ptid2))
  529.     return -1;
  530.   else if (ptid_get_lwp (ptid1) > ptid_get_lwp (ptid2))
  531.     return 1;
  532.   else
  533.     return 0;
  534. }

  535. /* qsort() comparison function for sorting thread_info structs by pid.  */

  536. static int
  537. gcmp (const void *t1v, const void *t2v)
  538. {
  539.   struct thread_info *t1 = *(struct thread_info **) t1v;
  540.   struct thread_info *t2 = *(struct thread_info **) t2v;
  541.   return ptid_cmp (t1->ptid, t2->ptid);
  542. }

  543. /* Search through the list of all kernel threads for the thread
  544.    that has stopped on a SIGTRAP signal, and return its TID.
  545.    Return 0 if none found.  */

  546. static pthdb_tid_t
  547. get_signaled_thread (void)
  548. {
  549.   struct thrdsinfo64 thrinf;
  550.   tid_t ktid = 0;
  551.   int result = 0;

  552.   while (1)
  553.   {
  554.     if (getthrds (ptid_get_pid (inferior_ptid), &thrinf,
  555.                     sizeof (thrinf), &ktid, 1) != 1)
  556.       break;

  557.     if (thrinf.ti_cursig == SIGTRAP)
  558.       return thrinf.ti_tid;
  559.   }

  560.   /* Didn't find any thread stopped on a SIGTRAP signal.  */
  561.   return 0;
  562. }

  563. /* Synchronize GDB's thread list with libpthdebug's.

  564.    There are some benefits of doing this every time the inferior stops:

  565.      - allows users to run thread-specific commands without needing to
  566.        run "info threads" first

  567.      - helps pthdb_tid_pthread() work properly (see "libpthdebug
  568.        peculiarities" at the top of this module)

  569.      - simplifies the demands placed on libpthdebug, which seems to
  570.        have difficulty with certain call patterns */

  571. static void
  572. sync_threadlists (void)
  573. {
  574.   int cmd, status, infpid;
  575.   int pcount, psize, pi, gcount, gi;
  576.   struct pd_thread *pbuf;
  577.   struct thread_info **gbuf, **g, *thread;
  578.   pthdb_pthread_t pdtid;
  579.   pthread_t pthid;
  580.   pthdb_tid_t tid;

  581.   /* Accumulate an array of libpthdebug threads sorted by pthread id.  */

  582.   pcount = 0;
  583.   psize = 1;
  584.   pbuf = (struct pd_thread *) xmalloc (psize * sizeof *pbuf);

  585.   for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
  586.     {
  587.       status = pthdb_pthread (pd_session, &pdtid, cmd);
  588.       if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
  589.         break;

  590.       status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
  591.       if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
  592.         continue;

  593.       if (pcount == psize)
  594.         {
  595.           psize *= 2;
  596.           pbuf = (struct pd_thread *) xrealloc (pbuf,
  597.                                                 psize * sizeof *pbuf);
  598.         }
  599.       pbuf[pcount].pdtid = pdtid;
  600.       pbuf[pcount].pthid = pthid;
  601.       pcount++;
  602.     }

  603.   for (pi = 0; pi < pcount; pi++)
  604.     {
  605.       status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
  606.       if (status != PTHDB_SUCCESS)
  607.         tid = PTHDB_INVALID_TID;
  608.       pbuf[pi].tid = tid;
  609.     }

  610.   qsort (pbuf, pcount, sizeof *pbuf, pcmp);

  611.   /* Accumulate an array of GDB threads sorted by pid.  */

  612.   gcount = 0;
  613.   iterate_over_threads (giter_count, &gcount);
  614.   g = gbuf = (struct thread_info **) xmalloc (gcount * sizeof *gbuf);
  615.   iterate_over_threads (giter_accum, &g);
  616.   qsort (gbuf, gcount, sizeof *gbuf, gcmp);

  617.   /* Apply differences between the two arrays to GDB's thread list.  */

  618.   infpid = ptid_get_pid (inferior_ptid);
  619.   for (pi = gi = 0; pi < pcount || gi < gcount;)
  620.     {
  621.       if (pi == pcount)
  622.         {
  623.           delete_thread (gbuf[gi]->ptid);
  624.           gi++;
  625.         }
  626.       else if (gi == gcount)
  627.         {
  628.           thread = add_thread (ptid_build (infpid, 0, pbuf[pi].pthid));
  629.           thread->private = xmalloc (sizeof (struct private_thread_info));
  630.           thread->private->pdtid = pbuf[pi].pdtid;
  631.           thread->private->tid = pbuf[pi].tid;
  632.           pi++;
  633.         }
  634.       else
  635.         {
  636.           ptid_t pptid, gptid;
  637.           int cmp_result;

  638.           pptid = ptid_build (infpid, 0, pbuf[pi].pthid);
  639.           gptid = gbuf[gi]->ptid;
  640.           pdtid = pbuf[pi].pdtid;
  641.           tid = pbuf[pi].tid;

  642.           cmp_result = ptid_cmp (pptid, gptid);

  643.           if (cmp_result == 0)
  644.             {
  645.               gbuf[gi]->private->pdtid = pdtid;
  646.               gbuf[gi]->private->tid = tid;
  647.               pi++;
  648.               gi++;
  649.             }
  650.           else if (cmp_result > 0)
  651.             {
  652.               delete_thread (gptid);
  653.               gi++;
  654.             }
  655.           else
  656.             {
  657.               thread = add_thread (pptid);
  658.               thread->private = xmalloc (sizeof (struct private_thread_info));
  659.               thread->private->pdtid = pdtid;
  660.               thread->private->tid = tid;
  661.               pi++;
  662.             }
  663.         }
  664.     }

  665.   xfree (pbuf);
  666.   xfree (gbuf);
  667. }

  668. /* Iterate_over_threads() callback for locating a thread, using
  669.    the TID of its associated kernel thread.  */

  670. static int
  671. iter_tid (struct thread_info *thread, void *tidp)
  672. {
  673.   const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;

  674.   return (thread->private->tid == tid);
  675. }

  676. /* Synchronize libpthdebug's state with the inferior and with GDB,
  677.    generate a composite process/thread <pid> for the current thread,
  678.    set inferior_ptid to <pid> if SET_INFPID, and return <pid>.  */

  679. static ptid_t
  680. pd_update (int set_infpid)
  681. {
  682.   int status;
  683.   ptid_t ptid;
  684.   pthdb_tid_t tid;
  685.   struct thread_info *thread = NULL;

  686.   if (!pd_active)
  687.     return inferior_ptid;

  688.   status = pthdb_session_update (pd_session);
  689.   if (status != PTHDB_SUCCESS)
  690.     return inferior_ptid;

  691.   sync_threadlists ();

  692.   /* Define "current thread" as one that just received a trap signal.  */

  693.   tid = get_signaled_thread ();
  694.   if (tid != 0)
  695.     thread = iterate_over_threads (iter_tid, &tid);
  696.   if (!thread)
  697.     ptid = inferior_ptid;
  698.   else
  699.     {
  700.       ptid = thread->ptid;
  701.       if (set_infpid)
  702.         inferior_ptid = ptid;
  703.     }
  704.   return ptid;
  705. }

  706. /* Try to start debugging threads in the current process.
  707.    If successful and SET_INFPID, set inferior_ptid to reflect the
  708.    current thread.  */

  709. static ptid_t
  710. pd_activate (int set_infpid)
  711. {
  712.   int status;

  713.   status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
  714.                                PTHDB_FLAG_REGS, &pd_callbacks,
  715.                                &pd_session);
  716.   if (status != PTHDB_SUCCESS)
  717.     {
  718.       return inferior_ptid;
  719.     }
  720.   pd_active = 1;
  721.   return pd_update (set_infpid);
  722. }

  723. /* Undo the effects of pd_activate().  */

  724. static void
  725. pd_deactivate (void)
  726. {
  727.   if (!pd_active)
  728.     return;
  729.   pthdb_session_destroy (pd_session);

  730.   pid_to_prc (&inferior_ptid);
  731.   pd_active = 0;
  732. }

  733. /* An object file has just been loaded.  Check whether the current
  734.    application is pthreaded, and if so, prepare for thread debugging.  */

  735. static void
  736. pd_enable (void)
  737. {
  738.   int status;
  739.   char *stub_name;
  740.   struct bound_minimal_symbol ms;

  741.   /* Don't initialize twice.  */
  742.   if (pd_able)
  743.     return;

  744.   /* Check application word size.  */
  745.   arch64 = register_size (target_gdbarch (), 0) == 8;

  746.   /* Check whether the application is pthreaded.  */
  747.   stub_name = NULL;
  748.   status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
  749.                                     &pd_callbacks, &stub_name);
  750.   if ((status != PTHDB_SUCCESS
  751.        && status != PTHDB_NOT_PTHREADED) || !stub_name)
  752.     return;

  753.   /* Set a breakpoint on the returned stub function.  */
  754.   ms = lookup_minimal_symbol (stub_name, NULL, NULL);
  755.   if (ms.minsym == NULL)
  756.     return;
  757.   pd_brk_addr = BMSYMBOL_VALUE_ADDRESS (ms);
  758.   if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
  759.     return;

  760.   /* Prepare for thread debugging.  */
  761.   push_target (&aix_thread_ops);
  762.   pd_able = 1;

  763.   /* If we're debugging a core file or an attached inferior, the
  764.      pthread library may already have been initialized, so try to
  765.      activate thread debugging.  */
  766.   pd_activate (1);
  767. }

  768. /* Undo the effects of pd_enable().  */

  769. static void
  770. pd_disable (void)
  771. {
  772.   if (!pd_able)
  773.     return;
  774.   if (pd_active)
  775.     pd_deactivate ();
  776.   pd_able = 0;
  777.   unpush_target (&aix_thread_ops);
  778. }

  779. /* new_objfile observer callback.

  780.    If OBJFILE is non-null, check whether a threaded application is
  781.    being debugged, and if so, prepare for thread debugging.

  782.    If OBJFILE is null, stop debugging threads.  */

  783. static void
  784. new_objfile (struct objfile *objfile)
  785. {
  786.   if (objfile)
  787.     pd_enable ();
  788.   else
  789.     pd_disable ();
  790. }

  791. /* Attach to process specified by ARGS.  */

  792. static void
  793. aix_thread_inferior_created (struct target_ops *ops, int from_tty)
  794. {
  795.   pd_enable ();
  796. }

  797. /* Detach from the process attached to by aix_thread_attach().  */

  798. static void
  799. aix_thread_detach (struct target_ops *ops, const char *args, int from_tty)
  800. {
  801.   struct target_ops *beneath = find_target_beneath (ops);

  802.   pd_disable ();
  803.   beneath->to_detach (beneath, args, from_tty);
  804. }

  805. /* Tell the inferior process to continue running thread PID if != -1
  806.    and all threads otherwise.  */

  807. static void
  808. aix_thread_resume (struct target_ops *ops,
  809.                    ptid_t ptid, int step, enum gdb_signal sig)
  810. {
  811.   struct thread_info *thread;
  812.   pthdb_tid_t tid[2];

  813.   if (!PD_TID (ptid))
  814.     {
  815.       struct cleanup *cleanup = save_inferior_ptid ();
  816.       struct target_ops *beneath = find_target_beneath (ops);

  817.       inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
  818.       beneath->to_resume (beneath, ptid, step, sig);
  819.       do_cleanups (cleanup);
  820.     }
  821.   else
  822.     {
  823.       thread = find_thread_ptid (ptid);
  824.       if (!thread)
  825.         error (_("aix-thread resume: unknown pthread %ld"),
  826.                ptid_get_lwp (ptid));

  827.       tid[0] = thread->private->tid;
  828.       if (tid[0] == PTHDB_INVALID_TID)
  829.         error (_("aix-thread resume: no tid for pthread %ld"),
  830.                ptid_get_lwp (ptid));
  831.       tid[1] = 0;

  832.       if (arch64)
  833.         ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
  834.                      gdb_signal_to_host (sig), (void *) tid);
  835.       else
  836.         ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
  837.                   gdb_signal_to_host (sig), (void *) tid);
  838.     }
  839. }

  840. /* Wait for thread/process ID if != -1 or for any thread otherwise.
  841.    If an error occurs, return -1, else return the pid of the stopped
  842.    thread.  */

  843. static ptid_t
  844. aix_thread_wait (struct target_ops *ops,
  845.                  ptid_t ptid, struct target_waitstatus *status, int options)
  846. {
  847.   struct cleanup *cleanup = save_inferior_ptid ();
  848.   struct target_ops *beneath = find_target_beneath (ops);

  849.   pid_to_prc (&ptid);

  850.   inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
  851.   ptid = beneath->to_wait (beneath, ptid, status, options);
  852.   do_cleanups (cleanup);

  853.   if (ptid_get_pid (ptid) == -1)
  854.     return pid_to_ptid (-1);

  855.   /* Check whether libpthdebug might be ready to be initialized.  */
  856.   if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
  857.       && status->value.sig == GDB_SIGNAL_TRAP)
  858.     {
  859.       struct regcache *regcache = get_thread_regcache (ptid);
  860.       struct gdbarch *gdbarch = get_regcache_arch (regcache);

  861.       if (regcache_read_pc (regcache)
  862.           - target_decr_pc_after_break (gdbarch) == pd_brk_addr)
  863.         return pd_activate (0);
  864.     }

  865.   return pd_update (0);
  866. }

  867. /* Record that the 64-bit general-purpose registers contain VALS.  */

  868. static void
  869. supply_gprs64 (struct regcache *regcache, uint64_t *vals)
  870. {
  871.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
  872.   int regno;

  873.   for (regno = 0; regno < ppc_num_gprs; regno++)
  874.     regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + regno,
  875.                          (char *) (vals + regno));
  876. }

  877. /* Record that 32-bit register REGNO contains VAL.  */

  878. static void
  879. supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
  880. {
  881.   regcache_raw_supply (regcache, regno, (char *) &val);
  882. }

  883. /* Record that the floating-point registers contain VALS.  */

  884. static void
  885. supply_fprs (struct regcache *regcache, double *vals)
  886. {
  887.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  888.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  889.   int regno;

  890.   /* This function should never be called on architectures without
  891.      floating-point registers.  */
  892.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  893.   for (regno = tdep->ppc_fp0_regnum;
  894.        regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
  895.        regno++)
  896.     regcache_raw_supply (regcache, regno,
  897.                          (char *) (vals + regno - tdep->ppc_fp0_regnum));
  898. }

  899. /* Predicate to test whether given register number is a "special" register.  */
  900. static int
  901. special_register_p (struct gdbarch *gdbarch, int regno)
  902. {
  903.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  904.   return regno == gdbarch_pc_regnum (gdbarch)
  905.       || regno == tdep->ppc_ps_regnum
  906.       || regno == tdep->ppc_cr_regnum
  907.       || regno == tdep->ppc_lr_regnum
  908.       || regno == tdep->ppc_ctr_regnum
  909.       || regno == tdep->ppc_xer_regnum
  910.       || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
  911.       || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
  912. }


  913. /* Record that the special registers contain the specified 64-bit and
  914.    32-bit values.  */

  915. static void
  916. supply_sprs64 (struct regcache *regcache,
  917.                uint64_t iar, uint64_t msr, uint32_t cr,
  918.                uint64_t lr, uint64_t ctr, uint32_t xer,
  919.                uint32_t fpscr)
  920. {
  921.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  922.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  923.   regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
  924.                        (char *) &iar);
  925.   regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
  926.   regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
  927.   regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
  928.   regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
  929.   regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
  930.   if (tdep->ppc_fpscr_regnum >= 0)
  931.     regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
  932.                          (char *) &fpscr);
  933. }

  934. /* Record that the special registers contain the specified 32-bit
  935.    values.  */

  936. static void
  937. supply_sprs32 (struct regcache *regcache,
  938.                uint32_t iar, uint32_t msr, uint32_t cr,
  939.                uint32_t lr, uint32_t ctr, uint32_t xer,
  940.                uint32_t fpscr)
  941. {
  942.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  943.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  944.   regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
  945.                        (char *) &iar);
  946.   regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
  947.   regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
  948.   regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
  949.   regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
  950.   regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
  951.   if (tdep->ppc_fpscr_regnum >= 0)
  952.     regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
  953.                          (char *) &fpscr);
  954. }

  955. /* Fetch all registers from pthread PDTID, which doesn't have a kernel
  956.    thread.

  957.    There's no way to query a single register from a non-kernel
  958.    pthread, so there's no need for a single-register version of this
  959.    function.  */

  960. static void
  961. fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
  962. {
  963.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  964.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  965.   int status, i;
  966.   pthdb_context_t ctx;

  967.   if (debug_aix_thread)
  968.     fprintf_unfiltered (gdb_stdlog,
  969.                         "fetch_regs_user_thread %lx\n", (long) pdtid);
  970.   status = pthdb_pthread_context (pd_session, pdtid, &ctx);
  971.   if (status != PTHDB_SUCCESS)
  972.     error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
  973.            pd_status2str (status));

  974.   /* General-purpose registers.  */

  975.   if (arch64)
  976.     supply_gprs64 (regcache, ctx.gpr);
  977.   else
  978.     for (i = 0; i < ppc_num_gprs; i++)
  979.       supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);

  980.   /* Floating-point registers.  */

  981.   if (ppc_floating_point_unit_p (gdbarch))
  982.     supply_fprs (regcache, ctx.fpr);

  983.   /* Special registers.  */

  984.   if (arch64)
  985.     supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
  986.                              ctx.xer, ctx.fpscr);
  987.   else
  988.     supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
  989.                              ctx.xer, ctx.fpscr);
  990. }

  991. /* Fetch register REGNO if != -1 or all registers otherwise from
  992.    kernel thread TID.

  993.    AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
  994.    SPRs, but there's no way to query individual registers within those
  995.    groups.  Therefore, if REGNO != -1, this function fetches an entire
  996.    group.

  997.    Unfortunately, kernel thread register queries often fail with
  998.    EPERM, indicating that the thread is in kernel space.  This breaks
  999.    backtraces of threads other than the current one.  To make that
  1000.    breakage obvious without throwing an error to top level (which is
  1001.    bad e.g. during "info threads" output), zero registers that can't
  1002.    be retrieved.  */

  1003. static void
  1004. fetch_regs_kernel_thread (struct regcache *regcache, int regno,
  1005.                           pthdb_tid_t tid)
  1006. {
  1007.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1008.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1009.   uint64_t gprs64[ppc_num_gprs];
  1010.   uint32_t gprs32[ppc_num_gprs];
  1011.   double fprs[ppc_num_fprs];
  1012.   struct ptxsprs sprs64;
  1013.   struct ptsprs sprs32;
  1014.   int i;

  1015.   if (debug_aix_thread)
  1016.     fprintf_unfiltered (gdb_stdlog,
  1017.         "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
  1018.         (long) tid, regno, arch64);

  1019.   /* General-purpose registers.  */
  1020.   if (regno == -1
  1021.       || (tdep->ppc_gp0_regnum <= regno
  1022.           && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
  1023.     {
  1024.       if (arch64)
  1025.         {
  1026.           if (!ptrace64aix (PTT_READ_GPRS, tid,
  1027.                             (unsigned long) gprs64, 0, NULL))
  1028.             memset (gprs64, 0, sizeof (gprs64));
  1029.           supply_gprs64 (regcache, gprs64);
  1030.         }
  1031.       else
  1032.         {
  1033.           if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
  1034.             memset (gprs32, 0, sizeof (gprs32));
  1035.           for (i = 0; i < ppc_num_gprs; i++)
  1036.             supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
  1037.         }
  1038.     }

  1039.   /* Floating-point registers.  */

  1040.   if (ppc_floating_point_unit_p (gdbarch)
  1041.       && (regno == -1
  1042.           || (regno >= tdep->ppc_fp0_regnum
  1043.               && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
  1044.     {
  1045.       if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
  1046.         memset (fprs, 0, sizeof (fprs));
  1047.       supply_fprs (regcache, fprs);
  1048.     }

  1049.   /* Special-purpose registers.  */

  1050.   if (regno == -1 || special_register_p (gdbarch, regno))
  1051.     {
  1052.       if (arch64)
  1053.         {
  1054.           if (!ptrace64aix (PTT_READ_SPRS, tid,
  1055.                             (unsigned long) &sprs64, 0, NULL))
  1056.             memset (&sprs64, 0, sizeof (sprs64));
  1057.           supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
  1058.                          sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
  1059.                          sprs64.pt_xer, sprs64.pt_fpscr);
  1060.         }
  1061.       else
  1062.         {
  1063.           struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1064.           if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
  1065.             memset (&sprs32, 0, sizeof (sprs32));
  1066.           supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
  1067.                          sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
  1068.                          sprs32.pt_fpscr);

  1069.           if (tdep->ppc_mq_regnum >= 0)
  1070.             regcache_raw_supply (regcache, tdep->ppc_mq_regnum,
  1071.                                  (char *) &sprs32.pt_mq);
  1072.         }
  1073.     }
  1074. }

  1075. /* Fetch register REGNO if != -1 or all registers otherwise in the
  1076.    thread/process specified by inferior_ptid.  */

  1077. static void
  1078. aix_thread_fetch_registers (struct target_ops *ops,
  1079.                             struct regcache *regcache, int regno)
  1080. {
  1081.   struct thread_info *thread;
  1082.   pthdb_tid_t tid;
  1083.   struct target_ops *beneath = find_target_beneath (ops);

  1084.   if (!PD_TID (inferior_ptid))
  1085.     beneath->to_fetch_registers (beneath, regcache, regno);
  1086.   else
  1087.     {
  1088.       thread = find_thread_ptid (inferior_ptid);
  1089.       tid = thread->private->tid;

  1090.       if (tid == PTHDB_INVALID_TID)
  1091.         fetch_regs_user_thread (regcache, thread->private->pdtid);
  1092.       else
  1093.         fetch_regs_kernel_thread (regcache, regno, tid);
  1094.     }
  1095. }

  1096. /* Store the gp registers into an array of uint32_t or uint64_t.  */

  1097. static void
  1098. fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
  1099. {
  1100.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
  1101.   int regno;

  1102.   for (regno = 0; regno < ppc_num_gprs; regno++)
  1103.     if (REG_VALID == regcache_register_status (regcache,
  1104.                                                tdep->ppc_gp0_regnum + regno))
  1105.       regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
  1106.                             vals + regno);
  1107. }

  1108. static void
  1109. fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
  1110. {
  1111.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
  1112.   int regno;

  1113.   for (regno = 0; regno < ppc_num_gprs; regno++)
  1114.     if (REG_VALID == regcache_register_status (regcache,
  1115.                                                tdep->ppc_gp0_regnum + regno))
  1116.       regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
  1117.                             vals + regno);
  1118. }

  1119. /* Store the floating point registers into a double array.  */
  1120. static void
  1121. fill_fprs (const struct regcache *regcache, double *vals)
  1122. {
  1123.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1124.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1125.   int regno;

  1126.   /* This function should never be called on architectures without
  1127.      floating-point registers.  */
  1128.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  1129.   for (regno = tdep->ppc_fp0_regnum;
  1130.        regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
  1131.        regno++)
  1132.     if (REG_VALID == regcache_register_status (regcache, regno))
  1133.       regcache_raw_collect (regcache, regno,
  1134.                             vals + regno - tdep->ppc_fp0_regnum);
  1135. }

  1136. /* Store the special registers into the specified 64-bit and 32-bit
  1137.    locations.  */

  1138. static void
  1139. fill_sprs64 (const struct regcache *regcache,
  1140.              uint64_t *iar, uint64_t *msr, uint32_t *cr,
  1141.              uint64_t *lr, uint64_t *ctr, uint32_t *xer,
  1142.              uint32_t *fpscr)
  1143. {
  1144.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1145.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1146.   /* Verify that the size of the size of the IAR buffer is the
  1147.      same as the raw size of the PC (in the register cache).  If
  1148.      they're not, then either GDB has been built incorrectly, or
  1149.      there's some other kind of internal error.  To be really safe,
  1150.      we should check all of the sizes.   */
  1151.   gdb_assert (sizeof (*iar) == register_size
  1152.                                  (gdbarch, gdbarch_pc_regnum (gdbarch)));

  1153.   if (REG_VALID == regcache_register_status (regcache,
  1154.                                              gdbarch_pc_regnum (gdbarch)))
  1155.     regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
  1156.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
  1157.     regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
  1158.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
  1159.     regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
  1160.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
  1161.     regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
  1162.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
  1163.     regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
  1164.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
  1165.     regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
  1166.   if (tdep->ppc_fpscr_regnum >= 0
  1167.       && REG_VALID == regcache_register_status (regcache,
  1168.                                                 tdep->ppc_fpscr_regnum))
  1169.     regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
  1170. }

  1171. static void
  1172. fill_sprs32 (const struct regcache *regcache,
  1173.              uint32_t *iar, uint32_t *msr, uint32_t *cr,
  1174.              uint32_t *lr, uint32_t *ctr, uint32_t *xer,
  1175.              uint32_t *fpscr)
  1176. {
  1177.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1178.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1179.   /* Verify that the size of the size of the IAR buffer is the
  1180.      same as the raw size of the PC (in the register cache).  If
  1181.      they're not, then either GDB has been built incorrectly, or
  1182.      there's some other kind of internal error.  To be really safe,
  1183.      we should check all of the sizes.  */
  1184.   gdb_assert (sizeof (*iar) == register_size (gdbarch,
  1185.                                               gdbarch_pc_regnum (gdbarch)));

  1186.   if (REG_VALID == regcache_register_status (regcache,
  1187.                                              gdbarch_pc_regnum (gdbarch)))
  1188.     regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
  1189.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
  1190.     regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
  1191.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
  1192.     regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
  1193.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
  1194.     regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
  1195.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
  1196.     regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
  1197.   if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
  1198.     regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
  1199.   if (tdep->ppc_fpscr_regnum >= 0
  1200.       && REG_VALID == regcache_register_status (regcache, tdep->ppc_fpscr_regnum))
  1201.     regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
  1202. }

  1203. /* Store all registers into pthread PDTID, which doesn't have a kernel
  1204.    thread.

  1205.    It's possible to store a single register into a non-kernel pthread,
  1206.    but I doubt it's worth the effort.  */

  1207. static void
  1208. store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
  1209. {
  1210.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1211.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1212.   int status, i;
  1213.   pthdb_context_t ctx;
  1214.   uint32_t int32;
  1215.   uint64_t int64;
  1216.   double   dbl;

  1217.   if (debug_aix_thread)
  1218.     fprintf_unfiltered (gdb_stdlog,
  1219.                         "store_regs_user_thread %lx\n", (long) pdtid);

  1220.   /* Retrieve the thread's current context for its non-register
  1221.      values.  */
  1222.   status = pthdb_pthread_context (pd_session, pdtid, &ctx);
  1223.   if (status != PTHDB_SUCCESS)
  1224.     error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
  1225.            pd_status2str (status));

  1226.   /* Collect general-purpose register values from the regcache.  */

  1227.   for (i = 0; i < ppc_num_gprs; i++)
  1228.     if (REG_VALID == regcache_register_status (regcache,
  1229.                                                tdep->ppc_gp0_regnum + i))
  1230.       {
  1231.         if (arch64)
  1232.           {
  1233.             regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
  1234.                                   (void *) &int64);
  1235.             ctx.gpr[i] = int64;
  1236.           }
  1237.         else
  1238.           {
  1239.             regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
  1240.                                   (void *) &int32);
  1241.             ctx.gpr[i] = int32;
  1242.           }
  1243.       }

  1244.   /* Collect floating-point register values from the regcache.  */
  1245.   if (ppc_floating_point_unit_p (gdbarch))
  1246.     fill_fprs (regcache, ctx.fpr);

  1247.   /* Special registers (always kept in ctx as 64 bits).  */
  1248.   if (arch64)
  1249.     {
  1250.       fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
  1251.                              &ctx.xer, &ctx.fpscr);
  1252.     }
  1253.   else
  1254.     {
  1255.       /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
  1256.          Solution: use 32-bit temp variables.  */
  1257.       uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
  1258.                tmp_fpscr;

  1259.       fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
  1260.                              &tmp_xer, &tmp_fpscr);
  1261.       if (REG_VALID == regcache_register_status (regcache,
  1262.                                                  gdbarch_pc_regnum (gdbarch)))
  1263.         ctx.iar = tmp_iar;
  1264.       if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
  1265.         ctx.msr = tmp_msr;
  1266.       if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
  1267.         ctx.cr  = tmp_cr;
  1268.       if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
  1269.         ctx.lr  = tmp_lr;
  1270.       if (REG_VALID == regcache_register_status (regcache,
  1271.                                                  tdep->ppc_ctr_regnum))
  1272.         ctx.ctr = tmp_ctr;
  1273.       if (REG_VALID == regcache_register_status (regcache,
  1274.                                                  tdep->ppc_xer_regnum))
  1275.         ctx.xer = tmp_xer;
  1276.       if (REG_VALID == regcache_register_status (regcache,
  1277.                                                  tdep->ppc_xer_regnum))
  1278.         ctx.fpscr = tmp_fpscr;
  1279.     }

  1280.   status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
  1281.   if (status != PTHDB_SUCCESS)
  1282.     error (_("aix-thread: store_registers: "
  1283.              "pthdb_pthread_setcontext returned %s"),
  1284.            pd_status2str (status));
  1285. }

  1286. /* Store register REGNO if != -1 or all registers otherwise into
  1287.    kernel thread TID.

  1288.    AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
  1289.    SPRs, but there's no way to set individual registers within those
  1290.    groups.  Therefore, if REGNO != -1, this function stores an entire
  1291.    group.  */

  1292. static void
  1293. store_regs_kernel_thread (const struct regcache *regcache, int regno,
  1294.                           pthdb_tid_t tid)
  1295. {
  1296.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1297.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1298.   uint64_t gprs64[ppc_num_gprs];
  1299.   uint32_t gprs32[ppc_num_gprs];
  1300.   double fprs[ppc_num_fprs];
  1301.   struct ptxsprs sprs64;
  1302.   struct ptsprs  sprs32;
  1303.   int i;

  1304.   if (debug_aix_thread)
  1305.     fprintf_unfiltered (gdb_stdlog,
  1306.                         "store_regs_kernel_thread tid=%lx regno=%d\n",
  1307.                         (long) tid, regno);

  1308.   /* General-purpose registers.  */
  1309.   if (regno == -1
  1310.       || (tdep->ppc_gp0_regnum <= regno
  1311.           && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
  1312.     {
  1313.       if (arch64)
  1314.         {
  1315.           /* Pre-fetch: some regs may not be in the cache.  */
  1316.           ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
  1317.           fill_gprs64 (regcache, gprs64);
  1318.           ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
  1319.         }
  1320.       else
  1321.         {
  1322.           /* Pre-fetch: some regs may not be in the cache.  */
  1323.           ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
  1324.           fill_gprs32 (regcache, gprs32);
  1325.           ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
  1326.         }
  1327.     }

  1328.   /* Floating-point registers.  */

  1329.   if (ppc_floating_point_unit_p (gdbarch)
  1330.       && (regno == -1
  1331.           || (regno >= tdep->ppc_fp0_regnum
  1332.               && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
  1333.     {
  1334.       /* Pre-fetch: some regs may not be in the cache.  */
  1335.       ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
  1336.       fill_fprs (regcache, fprs);
  1337.       ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
  1338.     }

  1339.   /* Special-purpose registers.  */

  1340.   if (regno == -1 || special_register_p (gdbarch, regno))
  1341.     {
  1342.       if (arch64)
  1343.         {
  1344.           /* Pre-fetch: some registers won't be in the cache.  */
  1345.           ptrace64aix (PTT_READ_SPRS, tid,
  1346.                        (unsigned long) &sprs64, 0, NULL);
  1347.           fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
  1348.                        &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
  1349.                        &sprs64.pt_xer, &sprs64.pt_fpscr);
  1350.           ptrace64aix (PTT_WRITE_SPRS, tid,
  1351.                        (unsigned long) &sprs64, 0, NULL);
  1352.         }
  1353.       else
  1354.         {
  1355.           /* The contents of "struct ptspr" were declared as "unsigned
  1356.              long" up to AIX 5.2, but are "unsigned int" since 5.3.
  1357.              Use temporaries to work around this problem.  Also, add an
  1358.              assert here to make sure we fail if the system header files
  1359.              use "unsigned long", and the size of that type is not what
  1360.              the headers expect.  */
  1361.           uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
  1362.                    tmp_fpscr;

  1363.           gdb_assert (sizeof (sprs32.pt_iar) == 4);

  1364.           /* Pre-fetch: some registers won't be in the cache.  */
  1365.           ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);

  1366.           fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
  1367.                        &tmp_ctr, &tmp_xer, &tmp_fpscr);

  1368.           sprs32.pt_iar = tmp_iar;
  1369.           sprs32.pt_msr = tmp_msr;
  1370.           sprs32.pt_cr = tmp_cr;
  1371.           sprs32.pt_lr = tmp_lr;
  1372.           sprs32.pt_ctr = tmp_ctr;
  1373.           sprs32.pt_xer = tmp_xer;
  1374.           sprs32.pt_fpscr = tmp_fpscr;

  1375.           if (tdep->ppc_mq_regnum >= 0)
  1376.             if (REG_VALID == regcache_register_status (regcache,
  1377.                                                        tdep->ppc_mq_regnum))
  1378.               regcache_raw_collect (regcache, tdep->ppc_mq_regnum,
  1379.                                     &sprs32.pt_mq);

  1380.           ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
  1381.         }
  1382.     }
  1383. }

  1384. /* Store gdb's current view of the register set into the
  1385.    thread/process specified by inferior_ptid.  */

  1386. static void
  1387. aix_thread_store_registers (struct target_ops *ops,
  1388.                             struct regcache *regcache, int regno)
  1389. {
  1390.   struct thread_info *thread;
  1391.   pthdb_tid_t tid;
  1392.   struct target_ops *beneath = find_target_beneath (ops);

  1393.   if (!PD_TID (inferior_ptid))
  1394.     beneath->to_store_registers (beneath, regcache, regno);
  1395.   else
  1396.     {
  1397.       thread = find_thread_ptid (inferior_ptid);
  1398.       tid = thread->private->tid;

  1399.       if (tid == PTHDB_INVALID_TID)
  1400.         store_regs_user_thread (regcache, thread->private->pdtid);
  1401.       else
  1402.         store_regs_kernel_thread (regcache, regno, tid);
  1403.     }
  1404. }

  1405. /* Implement the to_xfer_partial target_ops method.  */

  1406. static enum target_xfer_status
  1407. aix_thread_xfer_partial (struct target_ops *ops, enum target_object object,
  1408.                          const char *annex, gdb_byte *readbuf,
  1409.                          const gdb_byte *writebuf,
  1410.                          ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
  1411. {
  1412.   struct cleanup *old_chain = save_inferior_ptid ();
  1413.   enum target_xfer_status xfer;
  1414.   struct target_ops *beneath = find_target_beneath (ops);

  1415.   inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
  1416.   xfer = beneath->to_xfer_partial (beneath, object, annex, readbuf,
  1417.                                    writebuf, offset, len, xfered_len);

  1418.   do_cleanups (old_chain);
  1419.   return xfer;
  1420. }

  1421. /* Clean up after the inferior exits.  */

  1422. static void
  1423. aix_thread_mourn_inferior (struct target_ops *ops)
  1424. {
  1425.   struct target_ops *beneath = find_target_beneath (ops);

  1426.   pd_deactivate ();
  1427.   beneath->to_mourn_inferior (beneath);
  1428. }

  1429. /* Return whether thread PID is still valid.  */

  1430. static int
  1431. aix_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
  1432. {
  1433.   struct target_ops *beneath = find_target_beneath (ops);

  1434.   if (!PD_TID (ptid))
  1435.     return beneath->to_thread_alive (beneath, ptid);

  1436.   /* We update the thread list every time the child stops, so all
  1437.      valid threads should be in the thread list.  */
  1438.   return in_thread_list (ptid);
  1439. }

  1440. /* Return a printable representation of composite PID for use in
  1441.    "info threads" output.  */

  1442. static char *
  1443. aix_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
  1444. {
  1445.   static char *ret = NULL;
  1446.   struct target_ops *beneath = find_target_beneath (ops);

  1447.   if (!PD_TID (ptid))
  1448.     return beneath->to_pid_to_str (beneath, ptid);

  1449.   /* Free previous return value; a new one will be allocated by
  1450.      xstrprintf().  */
  1451.   xfree (ret);

  1452.   ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
  1453.   return ret;
  1454. }

  1455. /* Return a printable representation of extra information about
  1456.    THREAD, for use in "info threads" output.  */

  1457. static char *
  1458. aix_thread_extra_thread_info (struct target_ops *self,
  1459.                               struct thread_info *thread)
  1460. {
  1461.   struct ui_file *buf;
  1462.   int status;
  1463.   pthdb_pthread_t pdtid;
  1464.   pthdb_tid_t tid;
  1465.   pthdb_state_t state;
  1466.   pthdb_suspendstate_t suspendstate;
  1467.   pthdb_detachstate_t detachstate;
  1468.   int cancelpend;
  1469.   static char *ret = NULL;

  1470.   if (!PD_TID (thread->ptid))
  1471.     return NULL;

  1472.   buf = mem_fileopen ();

  1473.   pdtid = thread->private->pdtid;
  1474.   tid = thread->private->tid;

  1475.   if (tid != PTHDB_INVALID_TID)
  1476.     /* i18n: Like "thread-identifier %d, [state] running, suspended" */
  1477.     fprintf_unfiltered (buf, _("tid %d"), (int)tid);

  1478.   status = pthdb_pthread_state (pd_session, pdtid, &state);
  1479.   if (status != PTHDB_SUCCESS)
  1480.     state = PST_NOTSUP;
  1481.   fprintf_unfiltered (buf, ", %s", state2str (state));

  1482.   status = pthdb_pthread_suspendstate (pd_session, pdtid,
  1483.                                        &suspendstate);
  1484.   if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
  1485.     /* i18n: Like "Thread-Id %d, [state] running, suspended" */
  1486.     fprintf_unfiltered (buf, _(", suspended"));

  1487.   status = pthdb_pthread_detachstate (pd_session, pdtid,
  1488.                                       &detachstate);
  1489.   if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
  1490.     /* i18n: Like "Thread-Id %d, [state] running, detached" */
  1491.     fprintf_unfiltered (buf, _(", detached"));

  1492.   pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
  1493.   if (status == PTHDB_SUCCESS && cancelpend)
  1494.     /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
  1495.     fprintf_unfiltered (buf, _(", cancel pending"));

  1496.   ui_file_write (buf, "", 1);

  1497.   xfree (ret);                        /* Free old buffer.  */

  1498.   ret = ui_file_xstrdup (buf, NULL);
  1499.   ui_file_delete (buf);

  1500.   return ret;
  1501. }

  1502. static ptid_t
  1503. aix_thread_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
  1504. {
  1505.   return ptid_build (ptid_get_pid (inferior_ptid), 0, thread);
  1506. }

  1507. /* Initialize target aix_thread_ops.  */

  1508. static void
  1509. init_aix_thread_ops (void)
  1510. {
  1511.   aix_thread_ops.to_shortname = "aix-threads";
  1512.   aix_thread_ops.to_longname = _("AIX pthread support");
  1513.   aix_thread_ops.to_doc = _("AIX pthread support");

  1514.   aix_thread_ops.to_detach = aix_thread_detach;
  1515.   aix_thread_ops.to_resume = aix_thread_resume;
  1516.   aix_thread_ops.to_wait = aix_thread_wait;
  1517.   aix_thread_ops.to_fetch_registers = aix_thread_fetch_registers;
  1518.   aix_thread_ops.to_store_registers = aix_thread_store_registers;
  1519.   aix_thread_ops.to_xfer_partial = aix_thread_xfer_partial;
  1520.   aix_thread_ops.to_mourn_inferior = aix_thread_mourn_inferior;
  1521.   aix_thread_ops.to_thread_alive = aix_thread_thread_alive;
  1522.   aix_thread_ops.to_pid_to_str = aix_thread_pid_to_str;
  1523.   aix_thread_ops.to_extra_thread_info = aix_thread_extra_thread_info;
  1524.   aix_thread_ops.to_get_ada_task_ptid = aix_thread_get_ada_task_ptid;
  1525.   aix_thread_ops.to_stratum = thread_stratum;
  1526.   aix_thread_ops.to_magic = OPS_MAGIC;
  1527. }

  1528. /* Module startup initialization function, automagically called by
  1529.    init.c.  */

  1530. void _initialize_aix_thread (void);

  1531. void
  1532. _initialize_aix_thread (void)
  1533. {
  1534.   init_aix_thread_ops ();
  1535.   complete_target_initialization (&aix_thread_ops);

  1536.   /* Notice when object files get loaded and unloaded.  */
  1537.   observer_attach_new_objfile (new_objfile);

  1538.   /* Add ourselves to inferior_created event chain.
  1539.      This is needed to enable the thread target on "attach".  */
  1540.   observer_attach_inferior_created (aix_thread_inferior_created);

  1541.   add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
  1542.                            _("Set debugging of AIX thread module."),
  1543.                            _("Show debugging of AIX thread module."),
  1544.                            _("Enables debugging output (used to debug GDB)."),
  1545.                            NULL, NULL,
  1546.                            /* FIXME: i18n: Debugging of AIX thread
  1547.                               module is \"%d\".  */
  1548.                            &setdebuglist, &showdebuglist);
  1549. }