xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/aix-thread.c (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* Low level interface for debugging AIX 4.3+ pthreads.
2 
3    Copyright (C) 1999-2023 Free Software Foundation, Inc.
4    Written by Nick Duffek <nsd@redhat.com>.
5 
6    This file is part of GDB.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 
22 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
23    debugging pthread applications.
24 
25    Some name prefix conventions:
26      pthdb_	provided by libpthdebug.a
27      pdc_	callbacks that this module provides to libpthdebug.a
28      pd_	variables or functions interfacing with libpthdebug.a
29 
30    libpthdebug peculiarities:
31 
32      - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but
33        it's not documented, and after several calls it stops working
34        and causes other libpthdebug functions to fail.
35 
36      - pthdb_tid_pthread() doesn't always work after
37        pthdb_session_update(), but it does work after cycling through
38        all threads using pthdb_pthread().
39 
40      */
41 
42 #include "defs.h"
43 #include "gdbthread.h"
44 #include "target.h"
45 #include "inferior.h"
46 #include "regcache.h"
47 #include "gdbcmd.h"
48 #include "ppc-tdep.h"
49 #include "observable.h"
50 #include "objfiles.h"
51 
52 #include <procinfo.h>
53 #include <sys/types.h>
54 #include <sys/ptrace.h>
55 #include <sys/reg.h>
56 #include <sched.h>
57 #include <sys/pthdebug.h>
58 
59 #if !HAVE_DECL_GETTHRDS
60 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
61 #endif
62 
63 /* Whether to emit debugging output.  */
64 static bool debug_aix_thread;
65 
66 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t.  */
67 #ifndef PTHDB_VERSION_3
68 #define pthdb_tid_t	tid_t
69 #endif
70 
71 /* Return whether to treat PID as a debuggable thread id.  */
72 
73 #define PD_TID(ptid)	(pd_active && ptid.tid () != 0)
74 
75 /* Success and failure values returned by pthdb callbacks.  */
76 
77 #define PDC_SUCCESS	PTHDB_SUCCESS
78 #define PDC_FAILURE	PTHDB_CALLBACK
79 
80 /* Private data attached to each element in GDB's thread list.  */
81 
82 struct aix_thread_info : public private_thread_info
83 {
84   pthdb_pthread_t pdtid;	 /* thread's libpthdebug id */
85   pthdb_tid_t tid;			/* kernel thread id */
86 };
87 
88 /* Return the aix_thread_info attached to THREAD.  */
89 
90 static aix_thread_info *
91 get_aix_thread_info (thread_info *thread)
92 {
93   return gdb::checked_static_cast<aix_thread_info *> (thread->priv.get ());
94 }
95 
96 /* Information about a thread of which libpthdebug is aware.  */
97 
98 struct pd_thread {
99   pthdb_pthread_t pdtid;
100   pthread_t pthid;
101   pthdb_tid_t tid;
102 };
103 
104 /* This module's target-specific operations, active while pd_able is true.  */
105 
106 static const target_info aix_thread_target_info = {
107   "aix-threads",
108   N_("AIX pthread support"),
109   N_("AIX pthread support")
110 };
111 
112 class aix_thread_target final : public target_ops
113 {
114 public:
115   const target_info &info () const override
116   { return aix_thread_target_info; }
117 
118   strata stratum () const override { return thread_stratum; }
119 
120   void detach (inferior *, int) override;
121   void resume (ptid_t, int, enum gdb_signal) override;
122   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
123 
124   void fetch_registers (struct regcache *, int) override;
125   void store_registers (struct regcache *, int) override;
126 
127   enum target_xfer_status xfer_partial (enum target_object object,
128 					const char *annex,
129 					gdb_byte *readbuf,
130 					const gdb_byte *writebuf,
131 					ULONGEST offset, ULONGEST len,
132 					ULONGEST *xfered_len) override;
133 
134   void mourn_inferior () override;
135 
136   bool thread_alive (ptid_t ptid) override;
137 
138   std::string pid_to_str (ptid_t) override;
139 
140   const char *extra_thread_info (struct thread_info *) override;
141 
142   ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
143 };
144 
145 static aix_thread_target aix_thread_ops;
146 
147 /* Address of the function that libpthread will call when libpthdebug
148    is ready to be initialized.  */
149 
150 static CORE_ADDR pd_brk_addr;
151 
152 /* Whether the current application is debuggable by pthdb.  */
153 
154 static int pd_able = 0;
155 
156 /* Whether a threaded application is being debugged.  */
157 
158 static int pd_active = 0;
159 
160 /* Whether the current architecture is 64-bit.
161    Only valid when pd_able is true.  */
162 
163 static int arch64;
164 
165 /* Forward declarations for pthdb callbacks.  */
166 
167 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
168 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
169 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
170 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
171 			  unsigned long long flags,
172 			  pthdb_context_t *context);
173 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
174 			   unsigned long long flags,
175 			   pthdb_context_t *context);
176 static int pdc_alloc (pthdb_user_t, size_t, void **);
177 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
178 static int pdc_dealloc (pthdb_user_t, void *);
179 
180 /* pthdb callbacks.  */
181 
182 static pthdb_callbacks_t pd_callbacks = {
183   pdc_symbol_addrs,
184   pdc_read_data,
185   pdc_write_data,
186   pdc_read_regs,
187   pdc_write_regs,
188   pdc_alloc,
189   pdc_realloc,
190   pdc_dealloc,
191   NULL
192 };
193 
194 /* Current pthdb session.  */
195 
196 static pthdb_session_t pd_session;
197 
198 /* Return a printable representation of pthdebug function return
199    STATUS.  */
200 
201 static const char *
202 pd_status2str (int status)
203 {
204   switch (status)
205     {
206     case PTHDB_SUCCESS:		return "SUCCESS";
207     case PTHDB_NOSYS:		return "NOSYS";
208     case PTHDB_NOTSUP:		return "NOTSUP";
209     case PTHDB_BAD_VERSION:	return "BAD_VERSION";
210     case PTHDB_BAD_USER:	return "BAD_USER";
211     case PTHDB_BAD_SESSION:	return "BAD_SESSION";
212     case PTHDB_BAD_MODE:	return "BAD_MODE";
213     case PTHDB_BAD_FLAGS:	return "BAD_FLAGS";
214     case PTHDB_BAD_CALLBACK:	return "BAD_CALLBACK";
215     case PTHDB_BAD_POINTER:	return "BAD_POINTER";
216     case PTHDB_BAD_CMD:		return "BAD_CMD";
217     case PTHDB_BAD_PTHREAD:	return "BAD_PTHREAD";
218     case PTHDB_BAD_ATTR:	return "BAD_ATTR";
219     case PTHDB_BAD_MUTEX:	return "BAD_MUTEX";
220     case PTHDB_BAD_MUTEXATTR:	return "BAD_MUTEXATTR";
221     case PTHDB_BAD_COND:	return "BAD_COND";
222     case PTHDB_BAD_CONDATTR:	return "BAD_CONDATTR";
223     case PTHDB_BAD_RWLOCK:	return "BAD_RWLOCK";
224     case PTHDB_BAD_RWLOCKATTR:	return "BAD_RWLOCKATTR";
225     case PTHDB_BAD_KEY:		return "BAD_KEY";
226     case PTHDB_BAD_PTID:	return "BAD_PTID";
227     case PTHDB_BAD_TID:		return "BAD_TID";
228     case PTHDB_CALLBACK:	return "CALLBACK";
229     case PTHDB_CONTEXT:		return "CONTEXT";
230     case PTHDB_HELD:		return "HELD";
231     case PTHDB_NOT_HELD:	return "NOT_HELD";
232     case PTHDB_MEMORY:		return "MEMORY";
233     case PTHDB_NOT_PTHREADED:	return "NOT_PTHREADED";
234     case PTHDB_SYMBOL:		return "SYMBOL";
235     case PTHDB_NOT_AVAIL:	return "NOT_AVAIL";
236     case PTHDB_INTERNAL:	return "INTERNAL";
237     default:			return "UNKNOWN";
238     }
239 }
240 
241 /* A call to ptrace(REQ, ID, ...) just returned RET.  Check for
242    exceptional conditions and either return nonlocally or else return
243    1 for success and 0 for failure.  */
244 
245 static int
246 ptrace_check (int req, int id, int ret)
247 {
248   if (ret == 0 && !errno)
249     return 1;
250 
251   /* According to ptrace(2), ptrace may fail with EPERM if "the
252      Identifier parameter corresponds to a kernel thread which is
253      stopped in kernel mode and whose computational state cannot be
254      read or written."  This happens quite often with register reads.  */
255 
256   switch (req)
257     {
258     case PTT_READ_GPRS:
259     case PTT_READ_FPRS:
260     case PTT_READ_SPRS:
261       if (ret == -1 && errno == EPERM)
262 	{
263 	  if (debug_aix_thread)
264 	    gdb_printf (gdb_stdlog,
265 			"ptrace (%d, %d) = %d (errno = %d)\n",
266 			req, id, ret, errno);
267 	  return ret == -1 ? 0 : 1;
268 	}
269       break;
270     }
271   error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
272 	 req, id, ret, errno, safe_strerror (errno));
273   return 0;  /* Not reached.  */
274 }
275 
276 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
277    ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
278    Return success.  */
279 
280 #ifdef HAVE_PTRACE64
281 # define ptracex(request, pid, addr, data, buf) \
282 	 ptrace64 (request, pid, addr, data, buf)
283 #endif
284 
285 static int
286 ptrace64aix (int req, int id, long long addr, int data, int *buf)
287 {
288   errno = 0;
289   return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
290 }
291 
292 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
293    ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
294    Return success.  */
295 
296 #ifdef HAVE_PTRACE64
297 # define ptrace(request, pid, addr, data, buf) \
298 	 ptrace64 (request, pid, addr, data, buf)
299 # define addr_ptr long long
300 #else
301 # define addr_ptr int *
302 #endif
303 
304 static int
305 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
306 {
307   errno = 0;
308   return ptrace_check (req, id,
309 		       ptrace (req, id, addr, data, buf));
310 }
311 
312 /* If *PIDP is a composite process/thread id, convert it to a
313    process id.  */
314 
315 static void
316 pid_to_prc (ptid_t *ptidp)
317 {
318   ptid_t ptid;
319 
320   ptid = *ptidp;
321   if (PD_TID (ptid))
322     *ptidp = ptid_t (ptid.pid ());
323 }
324 
325 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
326    the address of SYMBOLS[<i>].name.  */
327 
328 static int
329 pdc_symbol_addrs (pthdb_user_t user_current_pid, pthdb_symbol_t *symbols, int count)
330 {
331   struct bound_minimal_symbol ms;
332   int i;
333   char *name;
334 
335   if (debug_aix_thread)
336     gdb_printf (gdb_stdlog,
337 		"pdc_symbol_addrs (user_current_pid = %ld, symbols = 0x%lx, count = %d)\n",
338 		user_current_pid, (long) symbols, count);
339 
340   for (i = 0; i < count; i++)
341     {
342       name = symbols[i].name;
343       if (debug_aix_thread)
344 	gdb_printf (gdb_stdlog,
345 		    "  symbols[%d].name = \"%s\"\n", i, name);
346 
347       if (!*name)
348 	symbols[i].addr = 0;
349       else
350 	{
351 	  ms = lookup_minimal_symbol (name, NULL, NULL);
352 	  if (ms.minsym == NULL)
353 	    {
354 	      if (debug_aix_thread)
355 		gdb_printf (gdb_stdlog, " returning PDC_FAILURE\n");
356 	      return PDC_FAILURE;
357 	    }
358 	  symbols[i].addr = ms.value_address ();
359 	}
360       if (debug_aix_thread)
361 	gdb_printf (gdb_stdlog, "  symbols[%d].addr = %s\n",
362 		    i, hex_string (symbols[i].addr));
363     }
364   if (debug_aix_thread)
365     gdb_printf (gdb_stdlog, " returning PDC_SUCCESS\n");
366   return PDC_SUCCESS;
367 }
368 
369 /* Read registers call back function should be able to read the
370    context information of a debuggee kernel thread from an active
371    process or from a core file.  The information should be formatted
372    in context64 form for both 32-bit and 64-bit process.
373    If successful return 0, else non-zero is returned.  */
374 
375 static int
376 pdc_read_regs (pthdb_user_t user_current_pid,
377 	       pthdb_tid_t tid,
378 	       unsigned long long flags,
379 	       pthdb_context_t *context)
380 {
381   /* This function doesn't appear to be used, so we could probably
382    just return 0 here.  HOWEVER, if it is not defined, the OS will
383    complain and several thread debug functions will fail.  In case
384    this is needed, I have implemented what I think it should do,
385    however this code is untested.  */
386 
387   uint64_t gprs64[ppc_num_gprs];
388   uint32_t gprs32[ppc_num_gprs];
389   double fprs[ppc_num_fprs];
390   struct ptxsprs sprs64;
391   struct ptsprs sprs32;
392 
393   if (debug_aix_thread)
394     gdb_printf (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
395 		(int) tid, hex_string (flags));
396 
397   /* General-purpose registers.  */
398   if (flags & PTHDB_FLAG_GPRS)
399     {
400       if (arch64)
401 	{
402 	  if (!ptrace64aix (PTT_READ_GPRS, tid,
403 			    (unsigned long) gprs64, 0, NULL))
404 	    memset (gprs64, 0, sizeof (gprs64));
405 	  memcpy (context->gpr, gprs64, sizeof(gprs64));
406 	}
407       else
408 	{
409 	  if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
410 	    memset (gprs32, 0, sizeof (gprs32));
411 	  memcpy (context->gpr, gprs32, sizeof(gprs32));
412 	}
413     }
414 
415   /* Floating-point registers.  */
416   if (flags & PTHDB_FLAG_FPRS)
417     {
418       if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
419 	memset (fprs, 0, sizeof (fprs));
420       memcpy (context->fpr, fprs, sizeof(fprs));
421     }
422 
423   /* Special-purpose registers.  */
424   if (flags & PTHDB_FLAG_SPRS)
425     {
426       if (arch64)
427 	{
428 	  if (!ptrace64aix (PTT_READ_SPRS, tid,
429 			    (unsigned long) &sprs64, 0, NULL))
430 	    memset (&sprs64, 0, sizeof (sprs64));
431 	  memcpy (&context->msr, &sprs64, sizeof(sprs64));
432 	}
433       else
434 	{
435 	  if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
436 	    memset (&sprs32, 0, sizeof (sprs32));
437 	  memcpy (&context->msr, &sprs32, sizeof(sprs32));
438 	}
439     }
440   return 0;
441 }
442 
443 /* Write register function should be able to write requested context
444    information to specified debuggee's kernel thread id.
445    If successful return 0, else non-zero is returned.  */
446 
447 static int
448 pdc_write_regs (pthdb_user_t user_current_pid,
449 		pthdb_tid_t tid,
450 		unsigned long long flags,
451 		pthdb_context_t *context)
452 {
453   /* This function doesn't appear to be used, so we could probably
454      just return 0 here.  HOWEVER, if it is not defined, the OS will
455      complain and several thread debug functions will fail.  In case
456      this is needed, I have implemented what I think it should do,
457      however this code is untested.  */
458 
459   if (debug_aix_thread)
460     gdb_printf (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
461 		(int) tid, hex_string (flags));
462 
463   /* General-purpose registers.  */
464   if (flags & PTHDB_FLAG_GPRS)
465     {
466       if (arch64)
467 	ptrace64aix (PTT_WRITE_GPRS, tid,
468 		     (unsigned long) context->gpr, 0, NULL);
469       else
470 	ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
471     }
472 
473  /* Floating-point registers.  */
474   if (flags & PTHDB_FLAG_FPRS)
475     {
476       ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
477     }
478 
479   /* Special-purpose registers.  */
480   if (flags & PTHDB_FLAG_SPRS)
481     {
482       if (arch64)
483 	{
484 	  ptrace64aix (PTT_WRITE_SPRS, tid,
485 		       (unsigned long) &context->msr, 0, NULL);
486 	}
487       else
488 	{
489 	  ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
490 	}
491     }
492   return 0;
493 }
494 
495 /* pthdb callback: read LEN bytes from process ADDR into BUF.  */
496 
497 static int
498 pdc_read_data (pthdb_user_t user_current_pid, void *buf,
499 	       pthdb_addr_t addr, size_t len)
500 {
501   int status, ret;
502 
503   if (debug_aix_thread)
504     gdb_printf (gdb_stdlog,
505 		"pdc_read_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
506 		user_current_pid, (long) buf, hex_string (addr), len);
507 
508   /* This is needed to eliminate the dependency of current thread
509      which is null so that thread reads the correct target memory.  */
510   {
511     scoped_restore_current_thread restore_current_thread;
512     /* Before the first inferior is added, we pass inferior_ptid.pid ()
513        from pd_enable () which is 0.  There is no need to switch threads
514        during first initialisation.  In the rest of the callbacks the
515        current thread needs to be correct.  */
516     if (user_current_pid != 0)
517       switch_to_thread (current_inferior ()->process_target (),
518 			ptid_t (user_current_pid));
519     status = target_read_memory (addr, (gdb_byte *) buf, len);
520   }
521   ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
522 
523   if (debug_aix_thread)
524     gdb_printf (gdb_stdlog, "  status=%d, returning %s\n",
525 		status, pd_status2str (ret));
526   return ret;
527 }
528 
529 /* pthdb callback: write LEN bytes from BUF to process ADDR.  */
530 
531 static int
532 pdc_write_data (pthdb_user_t user_current_pid, void *buf,
533 		pthdb_addr_t addr, size_t len)
534 {
535   int status, ret;
536 
537   if (debug_aix_thread)
538     gdb_printf (gdb_stdlog,
539 		"pdc_write_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
540 		user_current_pid, (long) buf, hex_string (addr), len);
541 
542   status = target_write_memory (addr, (gdb_byte *) buf, len);
543   ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
544 
545   if (debug_aix_thread)
546     gdb_printf (gdb_stdlog, "  status=%d, returning %s\n", status,
547 		pd_status2str (ret));
548   return ret;
549 }
550 
551 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
552    in BUFP.  */
553 
554 static int
555 pdc_alloc (pthdb_user_t user_current_pid, size_t len, void **bufp)
556 {
557   if (debug_aix_thread)
558     gdb_printf (gdb_stdlog,
559 		"pdc_alloc (user_current_pid = %ld, len = %ld, bufp = 0x%lx)\n",
560 		user_current_pid, len, (long) bufp);
561   *bufp = xmalloc (len);
562   if (debug_aix_thread)
563     gdb_printf (gdb_stdlog,
564 		"  malloc returned 0x%lx\n", (long) *bufp);
565 
566   /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
567      be returned.  */
568 
569   return *bufp ? PDC_SUCCESS : PDC_FAILURE;
570 }
571 
572 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
573    realloc callback, so that it contains LEN bytes, and store a
574    pointer to the result in BUFP.  */
575 
576 static int
577 pdc_realloc (pthdb_user_t user_current_pid, void *buf, size_t len, void **bufp)
578 {
579   if (debug_aix_thread)
580     gdb_printf (gdb_stdlog,
581 		"pdc_realloc (user_current_pid = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
582 		user_current_pid, (long) buf, len, (long) bufp);
583   *bufp = xrealloc (buf, len);
584   if (debug_aix_thread)
585     gdb_printf (gdb_stdlog,
586 		"  realloc returned 0x%lx\n", (long) *bufp);
587   return *bufp ? PDC_SUCCESS : PDC_FAILURE;
588 }
589 
590 /* pthdb callback: free BUF, which was allocated by the alloc or
591    realloc callback.  */
592 
593 static int
594 pdc_dealloc (pthdb_user_t user_current_pid, void *buf)
595 {
596   if (debug_aix_thread)
597     gdb_printf (gdb_stdlog,
598 		"pdc_free (user_current_pid = %ld, buf = 0x%lx)\n", user_current_pid,
599 		(long) buf);
600   xfree (buf);
601   return PDC_SUCCESS;
602 }
603 
604 /* Return a printable representation of pthread STATE.  */
605 
606 static char *
607 state2str (pthdb_state_t state)
608 {
609   switch (state)
610     {
611     case PST_IDLE:
612       /* i18n: Like "Thread-Id %d, [state] idle" */
613       return _("idle");      /* being created */
614     case PST_RUN:
615       /* i18n: Like "Thread-Id %d, [state] running" */
616       return _("running");   /* running */
617     case PST_SLEEP:
618       /* i18n: Like "Thread-Id %d, [state] sleeping" */
619       return _("sleeping");  /* awaiting an event */
620     case PST_READY:
621       /* i18n: Like "Thread-Id %d, [state] ready" */
622       return _("ready");     /* runnable */
623     case PST_TERM:
624       /* i18n: Like "Thread-Id %d, [state] finished" */
625       return _("finished");  /* awaiting a join/detach */
626     default:
627       /* i18n: Like "Thread-Id %d, [state] unknown" */
628       return _("unknown");
629     }
630 }
631 
632 /* qsort() comparison function for sorting pd_thread structs by pthid.  */
633 
634 static int
635 pcmp (const void *p1v, const void *p2v)
636 {
637   struct pd_thread *p1 = (struct pd_thread *) p1v;
638   struct pd_thread *p2 = (struct pd_thread *) p2v;
639   return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
640 }
641 
642 /* iterate_over_threads() callback for counting GDB threads.
643 
644    Do not count the main thread (whose tid is zero).  This matches
645    the list of threads provided by the pthreaddebug library, which
646    does not include that main thread either, and thus allows us
647    to compare the two lists.  */
648 
649 static int
650 giter_count (struct thread_info *thread, void *countp)
651 {
652   if (PD_TID (thread->ptid))
653     (*(int *) countp)++;
654   return 0;
655 }
656 
657 /* iterate_over_threads() callback for accumulating GDB thread pids.
658 
659    Do not include the main thread (whose tid is zero).  This matches
660    the list of threads provided by the pthreaddebug library, which
661    does not include that main thread either, and thus allows us
662    to compare the two lists.  */
663 
664 static int
665 giter_accum (struct thread_info *thread, void *bufp)
666 {
667   if (PD_TID (thread->ptid))
668     {
669       **(struct thread_info ***) bufp = thread;
670       (*(struct thread_info ***) bufp)++;
671     }
672   return 0;
673 }
674 
675 /* ptid comparison function */
676 
677 static int
678 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
679 {
680   if (ptid1.pid () < ptid2.pid ())
681     return -1;
682   else if (ptid1.pid () > ptid2.pid ())
683     return 1;
684   else if (ptid1.tid () < ptid2.tid ())
685     return -1;
686   else if (ptid1.tid () > ptid2.tid ())
687     return 1;
688   else if (ptid1.lwp () < ptid2.lwp ())
689     return -1;
690   else if (ptid1.lwp () > ptid2.lwp ())
691     return 1;
692   else
693     return 0;
694 }
695 
696 /* qsort() comparison function for sorting thread_info structs by pid.  */
697 
698 static int
699 gcmp (const void *t1v, const void *t2v)
700 {
701   struct thread_info *t1 = *(struct thread_info **) t1v;
702   struct thread_info *t2 = *(struct thread_info **) t2v;
703   return ptid_cmp (t1->ptid, t2->ptid);
704 }
705 
706 /* Search through the list of all kernel threads for the thread
707    that has stopped on a SIGTRAP signal, and return its TID.
708    Return 0 if none found.  */
709 
710 static pthdb_tid_t
711 get_signaled_thread (int pid)
712 {
713   struct thrdsinfo64 thrinf;
714   tid_t ktid = 0;
715 
716   while (1)
717     {
718       if (getthrds (pid, &thrinf,
719 		    sizeof (thrinf), &ktid, 1) != 1)
720 	break;
721 
722       if (thrinf.ti_cursig == SIGTRAP)
723 	return thrinf.ti_tid;
724     }
725 
726   /* Didn't find any thread stopped on a SIGTRAP signal.  */
727   return 0;
728 }
729 
730 /* Synchronize GDB's thread list with libpthdebug's.
731 
732    There are some benefits of doing this every time the inferior stops:
733 
734      - allows users to run thread-specific commands without needing to
735        run "info threads" first
736 
737      - helps pthdb_tid_pthread() work properly (see "libpthdebug
738        peculiarities" at the top of this module)
739 
740      - simplifies the demands placed on libpthdebug, which seems to
741        have difficulty with certain call patterns */
742 
743 static void
744 sync_threadlists (int pid)
745 {
746   int cmd, status;
747   int pcount, psize, pi, gcount, gi;
748   struct pd_thread *pbuf;
749   struct thread_info **gbuf, **g, *thread;
750   pthdb_pthread_t pdtid;
751   pthread_t pthid;
752   pthdb_tid_t tid;
753 
754   /* Accumulate an array of libpthdebug threads sorted by pthread id.  */
755 
756   pcount = 0;
757   psize = 1;
758   pbuf = XNEWVEC (struct pd_thread, psize);
759 
760   for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
761     {
762       status = pthdb_pthread (pd_session, &pdtid, cmd);
763       if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
764 	break;
765 
766       status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
767       if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
768 	continue;
769 
770       if (pcount == psize)
771 	{
772 	  psize *= 2;
773 	  pbuf = (struct pd_thread *) xrealloc (pbuf,
774 						psize * sizeof *pbuf);
775 	}
776       pbuf[pcount].pdtid = pdtid;
777       pbuf[pcount].pthid = pthid;
778       pcount++;
779     }
780 
781   for (pi = 0; pi < pcount; pi++)
782     {
783       status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
784       if (status != PTHDB_SUCCESS)
785 	tid = PTHDB_INVALID_TID;
786       pbuf[pi].tid = tid;
787     }
788 
789   qsort (pbuf, pcount, sizeof *pbuf, pcmp);
790 
791   /* Accumulate an array of GDB threads sorted by pid.  */
792 
793   gcount = 0;
794   iterate_over_threads (giter_count, &gcount);
795   g = gbuf = XNEWVEC (struct thread_info *, gcount);
796   iterate_over_threads (giter_accum, &g);
797   qsort (gbuf, gcount, sizeof *gbuf, gcmp);
798 
799   /* Apply differences between the two arrays to GDB's thread list.  */
800   for (pi = gi = 0; pi < pcount || gi < gcount;)
801     {
802       if (pi == pcount)
803 	{
804 	  delete_thread (gbuf[gi]);
805 	  gi++;
806 	}
807       else if (gi == gcount)
808 	{
809 	  aix_thread_info *priv = new aix_thread_info;
810 	  priv->pdtid = pbuf[pi].pdtid;
811 	  priv->tid = pbuf[pi].tid;
812 
813 	  process_stratum_target *proc_target
814 	    = current_inferior ()->process_target ();
815 	  thread = add_thread_with_info (proc_target,
816 					 ptid_t (pid, 0, pbuf[pi].pthid),
817 					 priv);
818 
819 	  pi++;
820 	}
821       else
822 	{
823 	  ptid_t pptid, gptid;
824 	  int cmp_result;
825 
826 	  pptid = ptid_t (pid, 0, pbuf[pi].pthid);
827 	  gptid = gbuf[gi]->ptid;
828 	  pdtid = pbuf[pi].pdtid;
829 	  tid = pbuf[pi].tid;
830 
831 	  cmp_result = ptid_cmp (pptid, gptid);
832 
833 	  if (cmp_result == 0)
834 	    {
835 	      aix_thread_info *priv = get_aix_thread_info (gbuf[gi]);
836 
837 	      priv->pdtid = pdtid;
838 	      priv->tid = tid;
839 	      pi++;
840 	      gi++;
841 	    }
842 	  else if (cmp_result > 0)
843 	    {
844 	      delete_thread (gbuf[gi]);
845 	      gi++;
846 	    }
847 	  else
848 	    {
849 	      process_stratum_target *proc_target
850 		= current_inferior ()->process_target ();
851 	      thread = add_thread (proc_target, pptid);
852 
853 	      aix_thread_info *priv = new aix_thread_info;
854 	      thread->priv.reset (priv);
855 	      priv->pdtid = pdtid;
856 	      priv->tid = tid;
857 	      pi++;
858 	    }
859 	}
860     }
861 
862   xfree (pbuf);
863   xfree (gbuf);
864 }
865 
866 /* Iterate_over_threads() callback for locating a thread, using
867    the TID of its associated kernel thread.  */
868 
869 static int
870 iter_tid (struct thread_info *thread, void *tidp)
871 {
872   const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
873   aix_thread_info *priv = get_aix_thread_info (thread);
874 
875   return priv->tid == tid;
876 }
877 
878 /* Synchronize libpthdebug's state with the inferior and with GDB,
879    generate a composite process/thread <pid> for the current thread,
880    Return the ptid of the event thread if one can be found, else
881    return a pid-only ptid with PID.  */
882 
883 static ptid_t
884 pd_update (int pid)
885 {
886   int status;
887   ptid_t ptid;
888   pthdb_tid_t tid;
889   struct thread_info *thread = NULL;
890 
891   if (!pd_active)
892     return ptid_t (pid);
893 
894   status = pthdb_session_update (pd_session);
895   if (status != PTHDB_SUCCESS)
896     return ptid_t (pid);
897 
898   sync_threadlists (pid);
899 
900   /* Define "current thread" as one that just received a trap signal.  */
901 
902   tid = get_signaled_thread (pid);
903   if (tid != 0)
904     thread = iterate_over_threads (iter_tid, &tid);
905   if (!thread)
906     ptid = ptid_t (pid);
907   else
908     ptid = thread->ptid;
909 
910   return ptid;
911 }
912 
913 /* Try to start debugging threads in the current process.
914    If successful and there exists and we can find an event thread, return a ptid
915    for that thread.  Otherwise, return a ptid-only ptid using PID.  */
916 
917 static ptid_t
918 pd_activate (int pid)
919 {
920   int status;
921 
922   status = pthdb_session_init (pid, arch64 ? PEM_64BIT : PEM_32BIT,
923 			       PTHDB_FLAG_REGS, &pd_callbacks,
924 			       &pd_session);
925   if (status != PTHDB_SUCCESS)
926     {
927       return ptid_t (pid);
928     }
929   pd_active = 1;
930   return pd_update (pid);
931 }
932 
933 /* Undo the effects of pd_activate().  */
934 
935 static void
936 pd_deactivate (void)
937 {
938   if (!pd_active)
939     return;
940   pthdb_session_destroy (pd_session);
941 
942   pid_to_prc (&inferior_ptid);
943   pd_active = 0;
944 }
945 
946 /* An object file has just been loaded.  Check whether the current
947    application is pthreaded, and if so, prepare for thread debugging.  */
948 
949 static void
950 pd_enable (void)
951 {
952   int status;
953   char *stub_name;
954   struct bound_minimal_symbol ms;
955 
956   /* Don't initialize twice.  */
957   if (pd_able)
958     return;
959 
960   /* Check application word size.  */
961   arch64 = register_size (target_gdbarch (), 0) == 8;
962 
963   /* Check whether the application is pthreaded.  */
964   stub_name = NULL;
965   status = pthdb_session_pthreaded (inferior_ptid.pid (), PTHDB_FLAG_REGS,
966 				    &pd_callbacks, &stub_name);
967   if ((status != PTHDB_SUCCESS
968        && status != PTHDB_NOT_PTHREADED) || !stub_name)
969     return;
970 
971   /* Set a breakpoint on the returned stub function.  */
972   ms = lookup_minimal_symbol (stub_name, NULL, NULL);
973   if (ms.minsym == NULL)
974     return;
975   pd_brk_addr = ms.value_address ();
976   if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
977     return;
978 
979   /* Prepare for thread debugging.  */
980   current_inferior ()->push_target (&aix_thread_ops);
981   pd_able = 1;
982 
983   /* If we're debugging a core file or an attached inferior, the
984      pthread library may already have been initialized, so try to
985      activate thread debugging.  */
986   pd_activate (inferior_ptid.pid ());
987 }
988 
989 /* Undo the effects of pd_enable().  */
990 
991 static void
992 pd_disable (void)
993 {
994   if (!pd_able)
995     return;
996   if (pd_active)
997     pd_deactivate ();
998   pd_able = 0;
999   current_inferior ()->unpush_target (&aix_thread_ops);
1000 }
1001 
1002 /* new_objfile observer callback.
1003 
1004    If OBJFILE is non-null, check whether a threaded application is
1005    being debugged, and if so, prepare for thread debugging.
1006 
1007    If OBJFILE is null, stop debugging threads.  */
1008 
1009 static void
1010 new_objfile (struct objfile *objfile)
1011 {
1012   if (objfile)
1013     pd_enable ();
1014   else
1015     pd_disable ();
1016 }
1017 
1018 /* Attach to process specified by ARGS.  */
1019 
1020 static void
1021 aix_thread_inferior_created (inferior *inf)
1022 {
1023   pd_enable ();
1024 }
1025 
1026 /* Detach from the process attached to by aix_thread_attach().  */
1027 
1028 void
1029 aix_thread_target::detach (inferior *inf, int from_tty)
1030 {
1031   target_ops *beneath = this->beneath ();
1032 
1033   pd_disable ();
1034   beneath->detach (inf, from_tty);
1035 }
1036 
1037 /* Tell the inferior process to continue running thread PID if != -1
1038    and all threads otherwise.  */
1039 
1040 void
1041 aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1042 {
1043   struct thread_info *thread;
1044   pthdb_tid_t tid[2];
1045 
1046   if (!PD_TID (ptid))
1047     {
1048       scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1049 
1050       inferior_ptid = ptid_t (inferior_ptid.pid ());
1051       beneath ()->resume (ptid, step, sig);
1052     }
1053   else
1054     {
1055       thread = find_thread_ptid (current_inferior (), ptid);
1056       if (!thread)
1057 	error (_("aix-thread resume: unknown pthread %ld"),
1058 	       ptid.lwp ());
1059 
1060       aix_thread_info *priv = get_aix_thread_info (thread);
1061 
1062       tid[0] = priv->tid;
1063       if (tid[0] == PTHDB_INVALID_TID)
1064 	error (_("aix-thread resume: no tid for pthread %ld"),
1065 	       ptid.lwp ());
1066       tid[1] = 0;
1067 
1068       if (arch64)
1069 	ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1070 		     gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1071       else
1072 	ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1073 		  gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1074     }
1075 }
1076 
1077 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1078    If an error occurs, return -1, else return the pid of the stopped
1079    thread.  */
1080 
1081 ptid_t
1082 aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1083 			 target_wait_flags options)
1084 {
1085   {
1086     pid_to_prc (&ptid);
1087 
1088     ptid = beneath ()->wait (ptid, status, options);
1089   }
1090 
1091   if (ptid.pid () == -1)
1092     return ptid_t (-1);
1093 
1094   /* The target beneath does not deal with threads, so it should only return
1095      pid-only ptids.  */
1096   gdb_assert (ptid.is_pid ());
1097 
1098   /* Check whether libpthdebug might be ready to be initialized.  */
1099   if (!pd_active && status->kind () == TARGET_WAITKIND_STOPPED
1100       && status->sig () == GDB_SIGNAL_TRAP)
1101     {
1102       process_stratum_target *proc_target
1103 	= current_inferior ()->process_target ();
1104       struct regcache *regcache = get_thread_regcache (proc_target, ptid);
1105       struct gdbarch *gdbarch = regcache->arch ();
1106 
1107       if (regcache_read_pc (regcache)
1108 	  - gdbarch_decr_pc_after_break (gdbarch) == pd_brk_addr)
1109 	return pd_activate (ptid.pid ());
1110     }
1111 
1112   return pd_update (ptid.pid ());
1113 }
1114 
1115 /* Record that the 64-bit general-purpose registers contain VALS.  */
1116 
1117 static void
1118 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1119 {
1120   ppc_gdbarch_tdep *tdep
1121     = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1122   int regno;
1123 
1124   for (regno = 0; regno < ppc_num_gprs; regno++)
1125     regcache->raw_supply (tdep->ppc_gp0_regnum + regno,
1126 			  (char *) (vals + regno));
1127 }
1128 
1129 /* Record that 32-bit register REGNO contains VAL.  */
1130 
1131 static void
1132 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1133 {
1134   regcache->raw_supply (regno, (char *) &val);
1135 }
1136 
1137 /* Record that the floating-point registers contain VALS.  */
1138 
1139 static void
1140 supply_fprs (struct regcache *regcache, double *vals)
1141 {
1142   struct gdbarch *gdbarch = regcache->arch ();
1143   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1144   int regno;
1145 
1146   /* This function should never be called on architectures without
1147      floating-point registers.  */
1148   gdb_assert (ppc_floating_point_unit_p (gdbarch));
1149 
1150   for (regno = tdep->ppc_fp0_regnum;
1151        regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1152        regno++)
1153     regcache->raw_supply (regno,
1154 			  (char *) (vals + regno - tdep->ppc_fp0_regnum));
1155 }
1156 
1157 /* Predicate to test whether given register number is a "special" register.  */
1158 static int
1159 special_register_p (struct gdbarch *gdbarch, int regno)
1160 {
1161   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1162 
1163   return regno == gdbarch_pc_regnum (gdbarch)
1164       || regno == tdep->ppc_ps_regnum
1165       || regno == tdep->ppc_cr_regnum
1166       || regno == tdep->ppc_lr_regnum
1167       || regno == tdep->ppc_ctr_regnum
1168       || regno == tdep->ppc_xer_regnum
1169       || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1170       || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1171 }
1172 
1173 
1174 /* Record that the special registers contain the specified 64-bit and
1175    32-bit values.  */
1176 
1177 static void
1178 supply_sprs64 (struct regcache *regcache,
1179 	       uint64_t iar, uint64_t msr, uint32_t cr,
1180 	       uint64_t lr, uint64_t ctr, uint32_t xer,
1181 	       uint32_t fpscr)
1182 {
1183   struct gdbarch *gdbarch = regcache->arch ();
1184   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1185 
1186   regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1187   regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1188   regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1189   regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1190   regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1191   regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1192   if (tdep->ppc_fpscr_regnum >= 0)
1193     regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1194 }
1195 
1196 /* Record that the special registers contain the specified 32-bit
1197    values.  */
1198 
1199 static void
1200 supply_sprs32 (struct regcache *regcache,
1201 	       uint32_t iar, uint32_t msr, uint32_t cr,
1202 	       uint32_t lr, uint32_t ctr, uint32_t xer,
1203 	       uint32_t fpscr)
1204 {
1205   struct gdbarch *gdbarch = regcache->arch ();
1206   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1207 
1208   regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1209   regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1210   regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1211   regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1212   regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1213   regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1214   if (tdep->ppc_fpscr_regnum >= 0)
1215     regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1216 }
1217 
1218 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1219    thread.
1220 
1221    There's no way to query a single register from a non-kernel
1222    pthread, so there's no need for a single-register version of this
1223    function.  */
1224 
1225 static void
1226 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1227 {
1228   struct gdbarch *gdbarch = regcache->arch ();
1229   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1230   int status, i;
1231   pthdb_context_t ctx;
1232 
1233   if (debug_aix_thread)
1234     gdb_printf (gdb_stdlog,
1235 		"fetch_regs_user_thread %lx\n", (long) pdtid);
1236   status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1237   if (status != PTHDB_SUCCESS)
1238     error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1239 	   pd_status2str (status));
1240 
1241   /* General-purpose registers.  */
1242 
1243   if (arch64)
1244     supply_gprs64 (regcache, ctx.gpr);
1245   else
1246     for (i = 0; i < ppc_num_gprs; i++)
1247       supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1248 
1249   /* Floating-point registers.  */
1250 
1251   if (ppc_floating_point_unit_p (gdbarch))
1252     supply_fprs (regcache, ctx.fpr);
1253 
1254   /* Special registers.  */
1255 
1256   if (arch64)
1257     supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1258 			     ctx.xer, ctx.fpscr);
1259   else
1260     supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1261 			     ctx.xer, ctx.fpscr);
1262 }
1263 
1264 /* Fetch register REGNO if != -1 or all registers otherwise from
1265    kernel thread TID.
1266 
1267    AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1268    SPRs, but there's no way to query individual registers within those
1269    groups.  Therefore, if REGNO != -1, this function fetches an entire
1270    group.
1271 
1272    Unfortunately, kernel thread register queries often fail with
1273    EPERM, indicating that the thread is in kernel space.  This breaks
1274    backtraces of threads other than the current one.  To make that
1275    breakage obvious without throwing an error to top level (which is
1276    bad e.g. during "info threads" output), zero registers that can't
1277    be retrieved.  */
1278 
1279 static void
1280 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1281 			  pthdb_tid_t tid)
1282 {
1283   struct gdbarch *gdbarch = regcache->arch ();
1284   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1285   uint64_t gprs64[ppc_num_gprs];
1286   uint32_t gprs32[ppc_num_gprs];
1287   double fprs[ppc_num_fprs];
1288   struct ptxsprs sprs64;
1289   struct ptsprs sprs32;
1290   int i;
1291 
1292   if (debug_aix_thread)
1293     gdb_printf (gdb_stdlog,
1294 		"fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1295 		(long) tid, regno, arch64);
1296 
1297   /* General-purpose registers.  */
1298   if (regno == -1
1299       || (tdep->ppc_gp0_regnum <= regno
1300 	  && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1301     {
1302       if (arch64)
1303 	{
1304 	  if (!ptrace64aix (PTT_READ_GPRS, tid,
1305 			    (unsigned long) gprs64, 0, NULL))
1306 	    memset (gprs64, 0, sizeof (gprs64));
1307 	  supply_gprs64 (regcache, gprs64);
1308 	}
1309       else
1310 	{
1311 	  if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1312 	    memset (gprs32, 0, sizeof (gprs32));
1313 	  for (i = 0; i < ppc_num_gprs; i++)
1314 	    supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1315 	}
1316     }
1317 
1318   /* Floating-point registers.  */
1319 
1320   if (ppc_floating_point_unit_p (gdbarch)
1321       && (regno == -1
1322 	  || (regno >= tdep->ppc_fp0_regnum
1323 	      && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1324     {
1325       if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1326 	memset (fprs, 0, sizeof (fprs));
1327       supply_fprs (regcache, fprs);
1328     }
1329 
1330   /* Special-purpose registers.  */
1331 
1332   if (regno == -1 || special_register_p (gdbarch, regno))
1333     {
1334       if (arch64)
1335 	{
1336 	  if (!ptrace64aix (PTT_READ_SPRS, tid,
1337 			    (unsigned long) &sprs64, 0, NULL))
1338 	    memset (&sprs64, 0, sizeof (sprs64));
1339 	  supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1340 			 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1341 			 sprs64.pt_xer, sprs64.pt_fpscr);
1342 	}
1343       else
1344 	{
1345 	  if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1346 	    memset (&sprs32, 0, sizeof (sprs32));
1347 	  supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1348 			 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1349 			 sprs32.pt_fpscr);
1350 
1351 	  if (tdep->ppc_mq_regnum >= 0)
1352 	    regcache->raw_supply (tdep->ppc_mq_regnum, (char *) &sprs32.pt_mq);
1353 	}
1354     }
1355 }
1356 
1357 /* Fetch register REGNO if != -1 or all registers otherwise from the
1358    thread/process connected to REGCACHE.  */
1359 
1360 void
1361 aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
1362 {
1363   struct thread_info *thread;
1364   pthdb_tid_t tid;
1365 
1366   if (!PD_TID (regcache->ptid ()))
1367     beneath ()->fetch_registers (regcache, regno);
1368   else
1369     {
1370       thread = find_thread_ptid (current_inferior (), regcache->ptid ());
1371       aix_thread_info *priv = get_aix_thread_info (thread);
1372       tid = priv->tid;
1373 
1374       if (tid == PTHDB_INVALID_TID)
1375 	fetch_regs_user_thread (regcache, priv->pdtid);
1376       else
1377 	fetch_regs_kernel_thread (regcache, regno, tid);
1378     }
1379 }
1380 
1381 /* Store the gp registers into an array of uint32_t or uint64_t.  */
1382 
1383 static void
1384 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1385 {
1386   ppc_gdbarch_tdep *tdep
1387     = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1388   int regno;
1389 
1390   for (regno = 0; regno < ppc_num_gprs; regno++)
1391     if (REG_VALID == regcache->get_register_status
1392 		       (tdep->ppc_gp0_regnum + regno))
1393       regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1394 }
1395 
1396 static void
1397 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1398 {
1399   ppc_gdbarch_tdep *tdep
1400     = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1401   int regno;
1402 
1403   for (regno = 0; regno < ppc_num_gprs; regno++)
1404     if (REG_VALID == regcache->get_register_status
1405 		       (tdep->ppc_gp0_regnum + regno))
1406       regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1407 }
1408 
1409 /* Store the floating point registers into a double array.  */
1410 static void
1411 fill_fprs (const struct regcache *regcache, double *vals)
1412 {
1413   struct gdbarch *gdbarch = regcache->arch ();
1414   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1415   int regno;
1416 
1417   /* This function should never be called on architectures without
1418      floating-point registers.  */
1419   gdb_assert (ppc_floating_point_unit_p (gdbarch));
1420 
1421   for (regno = tdep->ppc_fp0_regnum;
1422        regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1423        regno++)
1424     if (REG_VALID == regcache->get_register_status (regno))
1425       regcache->raw_collect (regno, vals + regno - tdep->ppc_fp0_regnum);
1426 }
1427 
1428 /* Store the special registers into the specified 64-bit and 32-bit
1429    locations.  */
1430 
1431 static void
1432 fill_sprs64 (const struct regcache *regcache,
1433 	     uint64_t *iar, uint64_t *msr, uint32_t *cr,
1434 	     uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1435 	     uint32_t *fpscr)
1436 {
1437   struct gdbarch *gdbarch = regcache->arch ();
1438   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1439 
1440   /* Verify that the size of the size of the IAR buffer is the
1441      same as the raw size of the PC (in the register cache).  If
1442      they're not, then either GDB has been built incorrectly, or
1443      there's some other kind of internal error.  To be really safe,
1444      we should check all of the sizes.   */
1445   gdb_assert (sizeof (*iar) == register_size
1446 				 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1447 
1448   if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1449     regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1450   if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1451     regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1452   if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1453     regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1454   if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1455     regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1456   if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1457     regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1458   if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1459     regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1460   if (tdep->ppc_fpscr_regnum >= 0
1461       && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1462     regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1463 }
1464 
1465 static void
1466 fill_sprs32 (const struct regcache *regcache,
1467 	     uint32_t *iar, uint32_t *msr, uint32_t *cr,
1468 	     uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1469 	     uint32_t *fpscr)
1470 {
1471   struct gdbarch *gdbarch = regcache->arch ();
1472   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1473 
1474   /* Verify that the size of the size of the IAR buffer is the
1475      same as the raw size of the PC (in the register cache).  If
1476      they're not, then either GDB has been built incorrectly, or
1477      there's some other kind of internal error.  To be really safe,
1478      we should check all of the sizes.  */
1479   gdb_assert (sizeof (*iar) == register_size (gdbarch,
1480 					      gdbarch_pc_regnum (gdbarch)));
1481 
1482   if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1483     regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1484   if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1485     regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1486   if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1487     regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1488   if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1489     regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1490   if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1491     regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1492   if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1493     regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1494   if (tdep->ppc_fpscr_regnum >= 0
1495       && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1496     regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1497 }
1498 
1499 /* Store all registers into pthread PDTID, which doesn't have a kernel
1500    thread.
1501 
1502    It's possible to store a single register into a non-kernel pthread,
1503    but I doubt it's worth the effort.  */
1504 
1505 static void
1506 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1507 {
1508   struct gdbarch *gdbarch = regcache->arch ();
1509   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1510   int status, i;
1511   pthdb_context_t ctx;
1512   uint32_t int32;
1513   uint64_t int64;
1514 
1515   if (debug_aix_thread)
1516     gdb_printf (gdb_stdlog,
1517 		"store_regs_user_thread %lx\n", (long) pdtid);
1518 
1519   /* Retrieve the thread's current context for its non-register
1520      values.  */
1521   status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1522   if (status != PTHDB_SUCCESS)
1523     error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1524 	   pd_status2str (status));
1525 
1526   /* Collect general-purpose register values from the regcache.  */
1527 
1528   for (i = 0; i < ppc_num_gprs; i++)
1529     if (REG_VALID == regcache->get_register_status (tdep->ppc_gp0_regnum + i))
1530       {
1531 	if (arch64)
1532 	  {
1533 	    regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int64);
1534 	    ctx.gpr[i] = int64;
1535 	  }
1536 	else
1537 	  {
1538 	    regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int32);
1539 	    ctx.gpr[i] = int32;
1540 	  }
1541       }
1542 
1543   /* Collect floating-point register values from the regcache.  */
1544   if (ppc_floating_point_unit_p (gdbarch))
1545     fill_fprs (regcache, ctx.fpr);
1546 
1547   /* Special registers (always kept in ctx as 64 bits).  */
1548   if (arch64)
1549     {
1550       fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1551 			     &ctx.xer, &ctx.fpscr);
1552     }
1553   else
1554     {
1555       /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1556 	 Solution: use 32-bit temp variables.  */
1557       uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1558 	       tmp_fpscr;
1559 
1560       fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1561 			     &tmp_xer, &tmp_fpscr);
1562       if (REG_VALID == regcache->get_register_status
1563 			 (gdbarch_pc_regnum (gdbarch)))
1564 	ctx.iar = tmp_iar;
1565       if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1566 	ctx.msr = tmp_msr;
1567       if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1568 	ctx.cr  = tmp_cr;
1569       if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1570 	ctx.lr  = tmp_lr;
1571       if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1572 	ctx.ctr = tmp_ctr;
1573       if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1574 	ctx.xer = tmp_xer;
1575       if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1576 	ctx.fpscr = tmp_fpscr;
1577     }
1578 
1579   status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1580   if (status != PTHDB_SUCCESS)
1581     error (_("aix-thread: store_registers: "
1582 	     "pthdb_pthread_setcontext returned %s"),
1583 	   pd_status2str (status));
1584 }
1585 
1586 /* Store register REGNO if != -1 or all registers otherwise into
1587    kernel thread TID.
1588 
1589    AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1590    SPRs, but there's no way to set individual registers within those
1591    groups.  Therefore, if REGNO != -1, this function stores an entire
1592    group.  */
1593 
1594 static void
1595 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1596 			  pthdb_tid_t tid)
1597 {
1598   struct gdbarch *gdbarch = regcache->arch ();
1599   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1600   uint64_t gprs64[ppc_num_gprs];
1601   uint32_t gprs32[ppc_num_gprs];
1602   double fprs[ppc_num_fprs];
1603   struct ptxsprs sprs64;
1604   struct ptsprs  sprs32;
1605 
1606   if (debug_aix_thread)
1607     gdb_printf (gdb_stdlog,
1608 		"store_regs_kernel_thread tid=%lx regno=%d\n",
1609 		(long) tid, regno);
1610 
1611   /* General-purpose registers.  */
1612   if (regno == -1
1613       || (tdep->ppc_gp0_regnum <= regno
1614 	  && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1615     {
1616       if (arch64)
1617 	{
1618 	  /* Pre-fetch: some regs may not be in the cache.  */
1619 	  ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1620 	  fill_gprs64 (regcache, gprs64);
1621 	  ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1622 	}
1623       else
1624 	{
1625 	  /* Pre-fetch: some regs may not be in the cache.  */
1626 	  ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1627 	  fill_gprs32 (regcache, gprs32);
1628 	  ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1629 	}
1630     }
1631 
1632   /* Floating-point registers.  */
1633 
1634   if (ppc_floating_point_unit_p (gdbarch)
1635       && (regno == -1
1636 	  || (regno >= tdep->ppc_fp0_regnum
1637 	      && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1638     {
1639       /* Pre-fetch: some regs may not be in the cache.  */
1640       ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1641       fill_fprs (regcache, fprs);
1642       ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1643     }
1644 
1645   /* Special-purpose registers.  */
1646 
1647   if (regno == -1 || special_register_p (gdbarch, regno))
1648     {
1649       if (arch64)
1650 	{
1651 	  /* Pre-fetch: some registers won't be in the cache.  */
1652 	  ptrace64aix (PTT_READ_SPRS, tid,
1653 		       (unsigned long) &sprs64, 0, NULL);
1654 	  fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1655 		       &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1656 		       &sprs64.pt_xer, &sprs64.pt_fpscr);
1657 	  ptrace64aix (PTT_WRITE_SPRS, tid,
1658 		       (unsigned long) &sprs64, 0, NULL);
1659 	}
1660       else
1661 	{
1662 	  /* The contents of "struct ptspr" were declared as "unsigned
1663 	     long" up to AIX 5.2, but are "unsigned int" since 5.3.
1664 	     Use temporaries to work around this problem.  Also, add an
1665 	     assert here to make sure we fail if the system header files
1666 	     use "unsigned long", and the size of that type is not what
1667 	     the headers expect.  */
1668 	  uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1669 		   tmp_fpscr;
1670 
1671 	  gdb_assert (sizeof (sprs32.pt_iar) == 4);
1672 
1673 	  /* Pre-fetch: some registers won't be in the cache.  */
1674 	  ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1675 
1676 	  fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1677 		       &tmp_ctr, &tmp_xer, &tmp_fpscr);
1678 
1679 	  sprs32.pt_iar = tmp_iar;
1680 	  sprs32.pt_msr = tmp_msr;
1681 	  sprs32.pt_cr = tmp_cr;
1682 	  sprs32.pt_lr = tmp_lr;
1683 	  sprs32.pt_ctr = tmp_ctr;
1684 	  sprs32.pt_xer = tmp_xer;
1685 	  sprs32.pt_fpscr = tmp_fpscr;
1686 
1687 	  if (tdep->ppc_mq_regnum >= 0)
1688 	    if (REG_VALID == regcache->get_register_status
1689 			       (tdep->ppc_mq_regnum))
1690 	      regcache->raw_collect (tdep->ppc_mq_regnum, &sprs32.pt_mq);
1691 
1692 	  ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1693 	}
1694     }
1695 }
1696 
1697 /* Store gdb's current view of the register set into the
1698    thread/process connected to REGCACHE.  */
1699 
1700 void
1701 aix_thread_target::store_registers (struct regcache *regcache, int regno)
1702 {
1703   struct thread_info *thread;
1704   pthdb_tid_t tid;
1705 
1706   if (!PD_TID (regcache->ptid ()))
1707     beneath ()->store_registers (regcache, regno);
1708   else
1709     {
1710       thread = find_thread_ptid (current_inferior (), regcache->ptid ());
1711       aix_thread_info *priv = get_aix_thread_info (thread);
1712       tid = priv->tid;
1713 
1714       if (tid == PTHDB_INVALID_TID)
1715 	store_regs_user_thread (regcache, priv->pdtid);
1716       else
1717 	store_regs_kernel_thread (regcache, regno, tid);
1718     }
1719 }
1720 
1721 /* Implement the to_xfer_partial target_ops method.  */
1722 
1723 enum target_xfer_status
1724 aix_thread_target::xfer_partial (enum target_object object,
1725 				 const char *annex, gdb_byte *readbuf,
1726 				 const gdb_byte *writebuf,
1727 				 ULONGEST offset, ULONGEST len,
1728 				 ULONGEST *xfered_len)
1729 {
1730   scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1731 
1732   inferior_ptid = ptid_t (inferior_ptid.pid ());
1733   return beneath ()->xfer_partial (object, annex, readbuf,
1734 				   writebuf, offset, len, xfered_len);
1735 }
1736 
1737 /* Clean up after the inferior exits.  */
1738 
1739 void
1740 aix_thread_target::mourn_inferior ()
1741 {
1742   target_ops *beneath = this->beneath ();
1743 
1744   pd_deactivate ();
1745   beneath->mourn_inferior ();
1746 }
1747 
1748 /* Return whether thread PID is still valid.  */
1749 
1750 bool
1751 aix_thread_target::thread_alive (ptid_t ptid)
1752 {
1753   if (!PD_TID (ptid))
1754     return beneath ()->thread_alive (ptid);
1755 
1756   /* We update the thread list every time the child stops, so all
1757      valid threads should be in the thread list.  */
1758   process_stratum_target *proc_target
1759     = current_inferior ()->process_target ();
1760   return in_thread_list (proc_target, ptid);
1761 }
1762 
1763 /* Return a printable representation of composite PID for use in
1764    "info threads" output.  */
1765 
1766 std::string
1767 aix_thread_target::pid_to_str (ptid_t ptid)
1768 {
1769   if (!PD_TID (ptid))
1770     return beneath ()->pid_to_str (ptid);
1771 
1772   return string_printf (_("Thread %s"), pulongest (ptid.tid ()));
1773 }
1774 
1775 /* Return a printable representation of extra information about
1776    THREAD, for use in "info threads" output.  */
1777 
1778 const char *
1779 aix_thread_target::extra_thread_info (struct thread_info *thread)
1780 {
1781   int status;
1782   pthdb_pthread_t pdtid;
1783   pthdb_tid_t tid;
1784   pthdb_state_t state;
1785   pthdb_suspendstate_t suspendstate;
1786   pthdb_detachstate_t detachstate;
1787   int cancelpend;
1788   static char *ret = NULL;
1789 
1790   if (!PD_TID (thread->ptid))
1791     return NULL;
1792 
1793   string_file buf;
1794   aix_thread_info *priv = get_aix_thread_info (thread);
1795 
1796   pdtid = priv->pdtid;
1797   tid = priv->tid;
1798 
1799   if (tid != PTHDB_INVALID_TID)
1800     /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1801     buf.printf (_("tid %d"), (int)tid);
1802 
1803   status = pthdb_pthread_state (pd_session, pdtid, &state);
1804   if (status != PTHDB_SUCCESS)
1805     state = PST_NOTSUP;
1806   buf.printf (", %s", state2str (state));
1807 
1808   status = pthdb_pthread_suspendstate (pd_session, pdtid,
1809 				       &suspendstate);
1810   if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
1811     /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1812     buf.printf (_(", suspended"));
1813 
1814   status = pthdb_pthread_detachstate (pd_session, pdtid,
1815 				      &detachstate);
1816   if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
1817     /* i18n: Like "Thread-Id %d, [state] running, detached" */
1818     buf.printf (_(", detached"));
1819 
1820   pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1821   if (status == PTHDB_SUCCESS && cancelpend)
1822     /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1823     buf.printf (_(", cancel pending"));
1824 
1825   buf.write ("", 1);
1826 
1827   xfree (ret);			/* Free old buffer.  */
1828 
1829   ret = xstrdup (buf.c_str ());
1830 
1831   return ret;
1832 }
1833 
1834 ptid_t
1835 aix_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
1836 {
1837   return ptid_t (inferior_ptid.pid (), 0, thread);
1838 }
1839 
1840 
1841 /* Module startup initialization function, automagically called by
1842    init.c.  */
1843 
1844 void _initialize_aix_thread ();
1845 void
1846 _initialize_aix_thread ()
1847 {
1848   /* Notice when object files get loaded and unloaded.  */
1849   gdb::observers::new_objfile.attach (new_objfile, "aix-thread");
1850 
1851   /* Add ourselves to inferior_created event chain.
1852      This is needed to enable the thread target on "attach".  */
1853   gdb::observers::inferior_created.attach (aix_thread_inferior_created,
1854 					   "aix-thread");
1855 
1856   add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
1857 			   _("Set debugging of AIX thread module."),
1858 			   _("Show debugging of AIX thread module."),
1859 			   _("Enables debugging output (used to debug GDB)."),
1860 			   NULL, NULL,
1861 			   /* FIXME: i18n: Debugging of AIX thread
1862 			      module is \"%d\".  */
1863 			   &setdebuglist, &showdebuglist);
1864 }
1865