1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2019 Free Software Foundation, Inc.
4 Written by Nick Duffek <nsd@redhat.com>.
6 This file is part of GDB.
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.
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.
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/>. */
22 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
23 debugging pthread applications.
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
30 libpthdebug peculiarities:
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.
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().
43 #include "gdbthread.h"
49 #include "observable.h"
53 #include <sys/types.h>
54 #include <sys/ptrace.h>
57 #include <sys/pthdebug.h>
59 #if !HAVE_DECL_GETTHRDS
60 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
63 /* Whether to emit debugging output. */
64 static int debug_aix_thread;
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
71 /* Return whether to treat PID as a debuggable thread id. */
73 #define PD_TID(ptid) (pd_active && ptid.tid () != 0)
75 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
76 PTHDB_BAD_USER errors, so use 1. */
80 /* Success and failure values returned by pthdb callbacks. */
82 #define PDC_SUCCESS PTHDB_SUCCESS
83 #define PDC_FAILURE PTHDB_CALLBACK
85 /* Private data attached to each element in GDB's thread list. */
87 struct aix_thread_info : public private_thread_info
89 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
90 pthdb_tid_t tid; /* kernel thread id */
93 /* Return the aix_thread_info attached to THREAD. */
95 static aix_thread_info *
96 get_aix_thread_info (thread_info *thread)
98 return static_cast<aix_thread_info *> (thread->priv.get ());
101 /* Information about a thread of which libpthdebug is aware. */
104 pthdb_pthread_t pdtid;
109 /* This module's target-specific operations, active while pd_able is true. */
111 static const target_info aix_thread_target_info = {
113 N_("AIX pthread support"),
114 N_("AIX pthread support")
117 class aix_thread_target final : public target_ops
120 const target_info &info () const override
121 { return aix_thread_target_info; }
123 strata stratum () const override { return thread_stratum; }
125 void detach (inferior *, int) override;
126 void resume (ptid_t, int, enum gdb_signal) override;
127 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
129 void fetch_registers (struct regcache *, int) override;
130 void store_registers (struct regcache *, int) override;
132 enum target_xfer_status xfer_partial (enum target_object object,
135 const gdb_byte *writebuf,
136 ULONGEST offset, ULONGEST len,
137 ULONGEST *xfered_len) override;
139 void mourn_inferior () override;
141 bool thread_alive (ptid_t ptid) override;
143 const char *pid_to_str (ptid_t) override;
145 const char *extra_thread_info (struct thread_info *) override;
147 ptid_t get_ada_task_ptid (long lwp, long thread) override;
150 static aix_thread_target aix_thread_ops;
152 /* Address of the function that libpthread will call when libpthdebug
153 is ready to be initialized. */
155 static CORE_ADDR pd_brk_addr;
157 /* Whether the current application is debuggable by pthdb. */
159 static int pd_able = 0;
161 /* Whether a threaded application is being debugged. */
163 static int pd_active = 0;
165 /* Whether the current architecture is 64-bit.
166 Only valid when pd_able is true. */
170 /* Forward declarations for pthdb callbacks. */
172 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
173 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
174 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
175 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
176 unsigned long long flags,
177 pthdb_context_t *context);
178 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
179 unsigned long long flags,
180 pthdb_context_t *context);
181 static int pdc_alloc (pthdb_user_t, size_t, void **);
182 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
183 static int pdc_dealloc (pthdb_user_t, void *);
185 /* pthdb callbacks. */
187 static pthdb_callbacks_t pd_callbacks = {
199 /* Current pthdb session. */
201 static pthdb_session_t pd_session;
203 /* Return a printable representation of pthdebug function return
207 pd_status2str (int status)
211 case PTHDB_SUCCESS: return "SUCCESS";
212 case PTHDB_NOSYS: return "NOSYS";
213 case PTHDB_NOTSUP: return "NOTSUP";
214 case PTHDB_BAD_VERSION: return "BAD_VERSION";
215 case PTHDB_BAD_USER: return "BAD_USER";
216 case PTHDB_BAD_SESSION: return "BAD_SESSION";
217 case PTHDB_BAD_MODE: return "BAD_MODE";
218 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
219 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
220 case PTHDB_BAD_POINTER: return "BAD_POINTER";
221 case PTHDB_BAD_CMD: return "BAD_CMD";
222 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
223 case PTHDB_BAD_ATTR: return "BAD_ATTR";
224 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
225 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
226 case PTHDB_BAD_COND: return "BAD_COND";
227 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
228 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
229 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
230 case PTHDB_BAD_KEY: return "BAD_KEY";
231 case PTHDB_BAD_PTID: return "BAD_PTID";
232 case PTHDB_BAD_TID: return "BAD_TID";
233 case PTHDB_CALLBACK: return "CALLBACK";
234 case PTHDB_CONTEXT: return "CONTEXT";
235 case PTHDB_HELD: return "HELD";
236 case PTHDB_NOT_HELD: return "NOT_HELD";
237 case PTHDB_MEMORY: return "MEMORY";
238 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
239 case PTHDB_SYMBOL: return "SYMBOL";
240 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
241 case PTHDB_INTERNAL: return "INTERNAL";
242 default: return "UNKNOWN";
246 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
247 exceptional conditions and either return nonlocally or else return
248 1 for success and 0 for failure. */
251 ptrace_check (int req, int id, int ret)
253 if (ret == 0 && !errno)
256 /* According to ptrace(2), ptrace may fail with EPERM if "the
257 Identifier parameter corresponds to a kernel thread which is
258 stopped in kernel mode and whose computational state cannot be
259 read or written." This happens quite often with register reads. */
266 if (ret == -1 && errno == EPERM)
268 if (debug_aix_thread)
269 fprintf_unfiltered (gdb_stdlog,
270 "ptrace (%d, %d) = %d (errno = %d)\n",
271 req, id, ret, errno);
272 return ret == -1 ? 0 : 1;
276 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
277 req, id, ret, errno, safe_strerror (errno));
278 return 0; /* Not reached. */
281 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
282 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
286 # define ptracex(request, pid, addr, data, buf) \
287 ptrace64 (request, pid, addr, data, buf)
291 ptrace64aix (int req, int id, long long addr, int data, int *buf)
294 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
297 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
298 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
302 # define ptrace(request, pid, addr, data, buf) \
303 ptrace64 (request, pid, addr, data, buf)
304 # define addr_ptr long long
306 # define addr_ptr int *
310 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
313 return ptrace_check (req, id,
314 ptrace (req, id, addr, data, buf));
317 /* If *PIDP is a composite process/thread id, convert it to a
321 pid_to_prc (ptid_t *ptidp)
327 *ptidp = ptid_t (ptid.pid ());
330 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
331 the address of SYMBOLS[<i>].name. */
334 pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
336 struct bound_minimal_symbol ms;
340 if (debug_aix_thread)
341 fprintf_unfiltered (gdb_stdlog,
342 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
343 user, (long) symbols, count);
345 for (i = 0; i < count; i++)
347 name = symbols[i].name;
348 if (debug_aix_thread)
349 fprintf_unfiltered (gdb_stdlog,
350 " symbols[%d].name = \"%s\"\n", i, name);
356 ms = lookup_minimal_symbol (name, NULL, NULL);
357 if (ms.minsym == NULL)
359 if (debug_aix_thread)
360 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE\n");
363 symbols[i].addr = BMSYMBOL_VALUE_ADDRESS (ms);
365 if (debug_aix_thread)
366 fprintf_unfiltered (gdb_stdlog, " symbols[%d].addr = %s\n",
367 i, hex_string (symbols[i].addr));
369 if (debug_aix_thread)
370 fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS\n");
374 /* Read registers call back function should be able to read the
375 context information of a debuggee kernel thread from an active
376 process or from a core file. The information should be formatted
377 in context64 form for both 32-bit and 64-bit process.
378 If successful return 0, else non-zero is returned. */
381 pdc_read_regs (pthdb_user_t user,
383 unsigned long long flags,
384 pthdb_context_t *context)
386 /* This function doesn't appear to be used, so we could probably
387 just return 0 here. HOWEVER, if it is not defined, the OS will
388 complain and several thread debug functions will fail. In case
389 this is needed, I have implemented what I think it should do,
390 however this code is untested. */
392 uint64_t gprs64[ppc_num_gprs];
393 uint32_t gprs32[ppc_num_gprs];
394 double fprs[ppc_num_fprs];
395 struct ptxsprs sprs64;
396 struct ptsprs sprs32;
398 if (debug_aix_thread)
399 fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
400 (int) tid, hex_string (flags));
402 /* General-purpose registers. */
403 if (flags & PTHDB_FLAG_GPRS)
407 if (!ptrace64aix (PTT_READ_GPRS, tid,
408 (unsigned long) gprs64, 0, NULL))
409 memset (gprs64, 0, sizeof (gprs64));
410 memcpy (context->gpr, gprs64, sizeof(gprs64));
414 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
415 memset (gprs32, 0, sizeof (gprs32));
416 memcpy (context->gpr, gprs32, sizeof(gprs32));
420 /* Floating-point registers. */
421 if (flags & PTHDB_FLAG_FPRS)
423 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
424 memset (fprs, 0, sizeof (fprs));
425 memcpy (context->fpr, fprs, sizeof(fprs));
428 /* Special-purpose registers. */
429 if (flags & PTHDB_FLAG_SPRS)
433 if (!ptrace64aix (PTT_READ_SPRS, tid,
434 (unsigned long) &sprs64, 0, NULL))
435 memset (&sprs64, 0, sizeof (sprs64));
436 memcpy (&context->msr, &sprs64, sizeof(sprs64));
440 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
441 memset (&sprs32, 0, sizeof (sprs32));
442 memcpy (&context->msr, &sprs32, sizeof(sprs32));
448 /* Write register function should be able to write requested context
449 information to specified debuggee's kernel thread id.
450 If successful return 0, else non-zero is returned. */
453 pdc_write_regs (pthdb_user_t user,
455 unsigned long long flags,
456 pthdb_context_t *context)
458 /* This function doesn't appear to be used, so we could probably
459 just return 0 here. HOWEVER, if it is not defined, the OS will
460 complain and several thread debug functions will fail. In case
461 this is needed, I have implemented what I think it should do,
462 however this code is untested. */
464 if (debug_aix_thread)
465 fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
466 (int) tid, hex_string (flags));
468 /* General-purpose registers. */
469 if (flags & PTHDB_FLAG_GPRS)
472 ptrace64aix (PTT_WRITE_GPRS, tid,
473 (unsigned long) context->gpr, 0, NULL);
475 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
478 /* Floating-point registers. */
479 if (flags & PTHDB_FLAG_FPRS)
481 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
484 /* Special-purpose registers. */
485 if (flags & PTHDB_FLAG_SPRS)
489 ptrace64aix (PTT_WRITE_SPRS, tid,
490 (unsigned long) &context->msr, 0, NULL);
494 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
500 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
503 pdc_read_data (pthdb_user_t user, void *buf,
504 pthdb_addr_t addr, size_t len)
508 if (debug_aix_thread)
509 fprintf_unfiltered (gdb_stdlog,
510 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
511 user, (long) buf, hex_string (addr), len);
513 status = target_read_memory (addr, (gdb_byte *) buf, len);
514 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
516 if (debug_aix_thread)
517 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n",
518 status, pd_status2str (ret));
522 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
525 pdc_write_data (pthdb_user_t user, void *buf,
526 pthdb_addr_t addr, size_t len)
530 if (debug_aix_thread)
531 fprintf_unfiltered (gdb_stdlog,
532 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
533 user, (long) buf, hex_string (addr), len);
535 status = target_write_memory (addr, (gdb_byte *) buf, len);
536 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
538 if (debug_aix_thread)
539 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n", status,
540 pd_status2str (ret));
544 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
548 pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
550 if (debug_aix_thread)
551 fprintf_unfiltered (gdb_stdlog,
552 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
553 user, len, (long) bufp);
554 *bufp = xmalloc (len);
555 if (debug_aix_thread)
556 fprintf_unfiltered (gdb_stdlog,
557 " malloc returned 0x%lx\n", (long) *bufp);
559 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
562 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
565 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
566 realloc callback, so that it contains LEN bytes, and store a
567 pointer to the result in BUFP. */
570 pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
572 if (debug_aix_thread)
573 fprintf_unfiltered (gdb_stdlog,
574 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
575 user, (long) buf, len, (long) bufp);
576 *bufp = xrealloc (buf, len);
577 if (debug_aix_thread)
578 fprintf_unfiltered (gdb_stdlog,
579 " realloc returned 0x%lx\n", (long) *bufp);
580 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
583 /* pthdb callback: free BUF, which was allocated by the alloc or
587 pdc_dealloc (pthdb_user_t user, void *buf)
589 if (debug_aix_thread)
590 fprintf_unfiltered (gdb_stdlog,
591 "pdc_free (user = %ld, buf = 0x%lx)\n", user,
597 /* Return a printable representation of pthread STATE. */
600 state2str (pthdb_state_t state)
605 /* i18n: Like "Thread-Id %d, [state] idle" */
606 return _("idle"); /* being created */
608 /* i18n: Like "Thread-Id %d, [state] running" */
609 return _("running"); /* running */
611 /* i18n: Like "Thread-Id %d, [state] sleeping" */
612 return _("sleeping"); /* awaiting an event */
614 /* i18n: Like "Thread-Id %d, [state] ready" */
615 return _("ready"); /* runnable */
617 /* i18n: Like "Thread-Id %d, [state] finished" */
618 return _("finished"); /* awaiting a join/detach */
620 /* i18n: Like "Thread-Id %d, [state] unknown" */
625 /* qsort() comparison function for sorting pd_thread structs by pthid. */
628 pcmp (const void *p1v, const void *p2v)
630 struct pd_thread *p1 = (struct pd_thread *) p1v;
631 struct pd_thread *p2 = (struct pd_thread *) p2v;
632 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
635 /* iterate_over_threads() callback for counting GDB threads.
637 Do not count the main thread (whose tid is zero). This matches
638 the list of threads provided by the pthreaddebug library, which
639 does not include that main thread either, and thus allows us
640 to compare the two lists. */
643 giter_count (struct thread_info *thread, void *countp)
645 if (PD_TID (thread->ptid))
650 /* iterate_over_threads() callback for accumulating GDB thread pids.
652 Do not include the main thread (whose tid is zero). This matches
653 the list of threads provided by the pthreaddebug library, which
654 does not include that main thread either, and thus allows us
655 to compare the two lists. */
658 giter_accum (struct thread_info *thread, void *bufp)
660 if (PD_TID (thread->ptid))
662 **(struct thread_info ***) bufp = thread;
663 (*(struct thread_info ***) bufp)++;
668 /* ptid comparison function */
671 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
673 if (ptid1.pid () < ptid2.pid ())
675 else if (ptid1.pid () > ptid2.pid ())
677 else if (ptid1.tid () < ptid2.tid ())
679 else if (ptid1.tid () > ptid2.tid ())
681 else if (ptid1.lwp () < ptid2.lwp ())
683 else if (ptid1.lwp () > ptid2.lwp ())
689 /* qsort() comparison function for sorting thread_info structs by pid. */
692 gcmp (const void *t1v, const void *t2v)
694 struct thread_info *t1 = *(struct thread_info **) t1v;
695 struct thread_info *t2 = *(struct thread_info **) t2v;
696 return ptid_cmp (t1->ptid, t2->ptid);
699 /* Search through the list of all kernel threads for the thread
700 that has stopped on a SIGTRAP signal, and return its TID.
701 Return 0 if none found. */
704 get_signaled_thread (void)
706 struct thrdsinfo64 thrinf;
711 if (getthrds (inferior_ptid.pid (), &thrinf,
712 sizeof (thrinf), &ktid, 1) != 1)
715 if (thrinf.ti_cursig == SIGTRAP)
716 return thrinf.ti_tid;
719 /* Didn't find any thread stopped on a SIGTRAP signal. */
723 /* Synchronize GDB's thread list with libpthdebug's.
725 There are some benefits of doing this every time the inferior stops:
727 - allows users to run thread-specific commands without needing to
728 run "info threads" first
730 - helps pthdb_tid_pthread() work properly (see "libpthdebug
731 peculiarities" at the top of this module)
733 - simplifies the demands placed on libpthdebug, which seems to
734 have difficulty with certain call patterns */
737 sync_threadlists (void)
739 int cmd, status, infpid;
740 int pcount, psize, pi, gcount, gi;
741 struct pd_thread *pbuf;
742 struct thread_info **gbuf, **g, *thread;
743 pthdb_pthread_t pdtid;
747 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
751 pbuf = XNEWVEC (struct pd_thread, psize);
753 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
755 status = pthdb_pthread (pd_session, &pdtid, cmd);
756 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
759 status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
760 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
766 pbuf = (struct pd_thread *) xrealloc (pbuf,
767 psize * sizeof *pbuf);
769 pbuf[pcount].pdtid = pdtid;
770 pbuf[pcount].pthid = pthid;
774 for (pi = 0; pi < pcount; pi++)
776 status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
777 if (status != PTHDB_SUCCESS)
778 tid = PTHDB_INVALID_TID;
782 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
784 /* Accumulate an array of GDB threads sorted by pid. */
787 iterate_over_threads (giter_count, &gcount);
788 g = gbuf = XNEWVEC (struct thread_info *, gcount);
789 iterate_over_threads (giter_accum, &g);
790 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
792 /* Apply differences between the two arrays to GDB's thread list. */
794 infpid = inferior_ptid.pid ();
795 for (pi = gi = 0; pi < pcount || gi < gcount;)
799 delete_thread (gbuf[gi]);
802 else if (gi == gcount)
804 aix_thread_info *priv = new aix_thread_info;
805 priv->pdtid = pbuf[pi].pdtid;
806 priv->tid = pbuf[pi].tid;
808 thread = add_thread_with_info (ptid_t (infpid, 0, pbuf[pi].pthid), priv);
817 pptid = ptid_t (infpid, 0, pbuf[pi].pthid);
818 gptid = gbuf[gi]->ptid;
819 pdtid = pbuf[pi].pdtid;
822 cmp_result = ptid_cmp (pptid, gptid);
826 aix_thread_info *priv = get_aix_thread_info (gbuf[gi]);
833 else if (cmp_result > 0)
835 delete_thread (gbuf[gi]);
840 thread = add_thread (pptid);
842 aix_thread_info *priv = new aix_thread_info;
843 thread->priv.reset (priv);
855 /* Iterate_over_threads() callback for locating a thread, using
856 the TID of its associated kernel thread. */
859 iter_tid (struct thread_info *thread, void *tidp)
861 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
862 aix_thread_info *priv = get_aix_thread_info (thread);
864 return priv->tid == tid;
867 /* Synchronize libpthdebug's state with the inferior and with GDB,
868 generate a composite process/thread <pid> for the current thread,
869 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
872 pd_update (int set_infpid)
877 struct thread_info *thread = NULL;
880 return inferior_ptid;
882 status = pthdb_session_update (pd_session);
883 if (status != PTHDB_SUCCESS)
884 return inferior_ptid;
888 /* Define "current thread" as one that just received a trap signal. */
890 tid = get_signaled_thread ();
892 thread = iterate_over_threads (iter_tid, &tid);
894 ptid = inferior_ptid;
899 inferior_ptid = ptid;
904 /* Try to start debugging threads in the current process.
905 If successful and SET_INFPID, set inferior_ptid to reflect the
909 pd_activate (int set_infpid)
913 status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
914 PTHDB_FLAG_REGS, &pd_callbacks,
916 if (status != PTHDB_SUCCESS)
918 return inferior_ptid;
921 return pd_update (set_infpid);
924 /* Undo the effects of pd_activate(). */
931 pthdb_session_destroy (pd_session);
933 pid_to_prc (&inferior_ptid);
937 /* An object file has just been loaded. Check whether the current
938 application is pthreaded, and if so, prepare for thread debugging. */
945 struct bound_minimal_symbol ms;
947 /* Don't initialize twice. */
951 /* Check application word size. */
952 arch64 = register_size (target_gdbarch (), 0) == 8;
954 /* Check whether the application is pthreaded. */
956 status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
957 &pd_callbacks, &stub_name);
958 if ((status != PTHDB_SUCCESS
959 && status != PTHDB_NOT_PTHREADED) || !stub_name)
962 /* Set a breakpoint on the returned stub function. */
963 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
964 if (ms.minsym == NULL)
966 pd_brk_addr = BMSYMBOL_VALUE_ADDRESS (ms);
967 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
970 /* Prepare for thread debugging. */
971 push_target (&aix_thread_ops);
974 /* If we're debugging a core file or an attached inferior, the
975 pthread library may already have been initialized, so try to
976 activate thread debugging. */
980 /* Undo the effects of pd_enable(). */
990 unpush_target (&aix_thread_ops);
993 /* new_objfile observer callback.
995 If OBJFILE is non-null, check whether a threaded application is
996 being debugged, and if so, prepare for thread debugging.
998 If OBJFILE is null, stop debugging threads. */
1001 new_objfile (struct objfile *objfile)
1009 /* Attach to process specified by ARGS. */
1012 aix_thread_inferior_created (struct target_ops *ops, int from_tty)
1017 /* Detach from the process attached to by aix_thread_attach(). */
1020 aix_thread_target::detach (inferior *inf, int from_tty)
1022 target_ops *beneath = this->beneath ();
1025 beneath->detach (inf, from_tty);
1028 /* Tell the inferior process to continue running thread PID if != -1
1029 and all threads otherwise. */
1032 aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1034 struct thread_info *thread;
1039 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1041 inferior_ptid = ptid_t (inferior_ptid.pid ());
1042 beneath ()->resume (ptid, step, sig);
1046 thread = find_thread_ptid (ptid);
1048 error (_("aix-thread resume: unknown pthread %ld"),
1051 aix_thread_info *priv = get_aix_thread_info (thread);
1054 if (tid[0] == PTHDB_INVALID_TID)
1055 error (_("aix-thread resume: no tid for pthread %ld"),
1060 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1061 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1063 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1064 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1068 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1069 If an error occurs, return -1, else return the pid of the stopped
1073 aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1077 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1081 inferior_ptid = ptid_t (inferior_ptid.pid ());
1082 ptid = beneath ()->wait (ptid, status, options);
1085 if (ptid.pid () == -1)
1088 /* Check whether libpthdebug might be ready to be initialized. */
1089 if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
1090 && status->value.sig == GDB_SIGNAL_TRAP)
1092 struct regcache *regcache = get_thread_regcache (ptid);
1093 struct gdbarch *gdbarch = regcache->arch ();
1095 if (regcache_read_pc (regcache)
1096 - gdbarch_decr_pc_after_break (gdbarch) == pd_brk_addr)
1097 return pd_activate (0);
1100 return pd_update (0);
1103 /* Record that the 64-bit general-purpose registers contain VALS. */
1106 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1108 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
1111 for (regno = 0; regno < ppc_num_gprs; regno++)
1112 regcache->raw_supply (tdep->ppc_gp0_regnum + regno,
1113 (char *) (vals + regno));
1116 /* Record that 32-bit register REGNO contains VAL. */
1119 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1121 regcache->raw_supply (regno, (char *) &val);
1124 /* Record that the floating-point registers contain VALS. */
1127 supply_fprs (struct regcache *regcache, double *vals)
1129 struct gdbarch *gdbarch = regcache->arch ();
1130 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1133 /* This function should never be called on architectures without
1134 floating-point registers. */
1135 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1137 for (regno = tdep->ppc_fp0_regnum;
1138 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1140 regcache->raw_supply (regno,
1141 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1144 /* Predicate to test whether given register number is a "special" register. */
1146 special_register_p (struct gdbarch *gdbarch, int regno)
1148 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1150 return regno == gdbarch_pc_regnum (gdbarch)
1151 || regno == tdep->ppc_ps_regnum
1152 || regno == tdep->ppc_cr_regnum
1153 || regno == tdep->ppc_lr_regnum
1154 || regno == tdep->ppc_ctr_regnum
1155 || regno == tdep->ppc_xer_regnum
1156 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1157 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1161 /* Record that the special registers contain the specified 64-bit and
1165 supply_sprs64 (struct regcache *regcache,
1166 uint64_t iar, uint64_t msr, uint32_t cr,
1167 uint64_t lr, uint64_t ctr, uint32_t xer,
1170 struct gdbarch *gdbarch = regcache->arch ();
1171 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1173 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1174 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1175 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1176 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1177 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1178 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1179 if (tdep->ppc_fpscr_regnum >= 0)
1180 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1183 /* Record that the special registers contain the specified 32-bit
1187 supply_sprs32 (struct regcache *regcache,
1188 uint32_t iar, uint32_t msr, uint32_t cr,
1189 uint32_t lr, uint32_t ctr, uint32_t xer,
1192 struct gdbarch *gdbarch = regcache->arch ();
1193 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1195 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1196 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1197 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1198 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1199 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1200 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1201 if (tdep->ppc_fpscr_regnum >= 0)
1202 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1205 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1208 There's no way to query a single register from a non-kernel
1209 pthread, so there's no need for a single-register version of this
1213 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1215 struct gdbarch *gdbarch = regcache->arch ();
1216 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1218 pthdb_context_t ctx;
1220 if (debug_aix_thread)
1221 fprintf_unfiltered (gdb_stdlog,
1222 "fetch_regs_user_thread %lx\n", (long) pdtid);
1223 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1224 if (status != PTHDB_SUCCESS)
1225 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1226 pd_status2str (status));
1228 /* General-purpose registers. */
1231 supply_gprs64 (regcache, ctx.gpr);
1233 for (i = 0; i < ppc_num_gprs; i++)
1234 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1236 /* Floating-point registers. */
1238 if (ppc_floating_point_unit_p (gdbarch))
1239 supply_fprs (regcache, ctx.fpr);
1241 /* Special registers. */
1244 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1245 ctx.xer, ctx.fpscr);
1247 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1248 ctx.xer, ctx.fpscr);
1251 /* Fetch register REGNO if != -1 or all registers otherwise from
1254 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1255 SPRs, but there's no way to query individual registers within those
1256 groups. Therefore, if REGNO != -1, this function fetches an entire
1259 Unfortunately, kernel thread register queries often fail with
1260 EPERM, indicating that the thread is in kernel space. This breaks
1261 backtraces of threads other than the current one. To make that
1262 breakage obvious without throwing an error to top level (which is
1263 bad e.g. during "info threads" output), zero registers that can't
1267 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1270 struct gdbarch *gdbarch = regcache->arch ();
1271 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1272 uint64_t gprs64[ppc_num_gprs];
1273 uint32_t gprs32[ppc_num_gprs];
1274 double fprs[ppc_num_fprs];
1275 struct ptxsprs sprs64;
1276 struct ptsprs sprs32;
1279 if (debug_aix_thread)
1280 fprintf_unfiltered (gdb_stdlog,
1281 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1282 (long) tid, regno, arch64);
1284 /* General-purpose registers. */
1286 || (tdep->ppc_gp0_regnum <= regno
1287 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1291 if (!ptrace64aix (PTT_READ_GPRS, tid,
1292 (unsigned long) gprs64, 0, NULL))
1293 memset (gprs64, 0, sizeof (gprs64));
1294 supply_gprs64 (regcache, gprs64);
1298 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1299 memset (gprs32, 0, sizeof (gprs32));
1300 for (i = 0; i < ppc_num_gprs; i++)
1301 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1305 /* Floating-point registers. */
1307 if (ppc_floating_point_unit_p (gdbarch)
1309 || (regno >= tdep->ppc_fp0_regnum
1310 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1312 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1313 memset (fprs, 0, sizeof (fprs));
1314 supply_fprs (regcache, fprs);
1317 /* Special-purpose registers. */
1319 if (regno == -1 || special_register_p (gdbarch, regno))
1323 if (!ptrace64aix (PTT_READ_SPRS, tid,
1324 (unsigned long) &sprs64, 0, NULL))
1325 memset (&sprs64, 0, sizeof (sprs64));
1326 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1327 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1328 sprs64.pt_xer, sprs64.pt_fpscr);
1332 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1333 memset (&sprs32, 0, sizeof (sprs32));
1334 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1335 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1338 if (tdep->ppc_mq_regnum >= 0)
1339 regcache->raw_supply (tdep->ppc_mq_regnum, (char *) &sprs32.pt_mq);
1344 /* Fetch register REGNO if != -1 or all registers otherwise from the
1345 thread/process connected to REGCACHE. */
1348 aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
1350 struct thread_info *thread;
1353 if (!PD_TID (regcache->ptid ()))
1354 beneath ()->fetch_registers (regcache, regno);
1357 thread = find_thread_ptid (regcache->ptid ());
1358 aix_thread_info *priv = get_aix_thread_info (thread);
1361 if (tid == PTHDB_INVALID_TID)
1362 fetch_regs_user_thread (regcache, priv->pdtid);
1364 fetch_regs_kernel_thread (regcache, regno, tid);
1368 /* Store the gp registers into an array of uint32_t or uint64_t. */
1371 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1373 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
1376 for (regno = 0; regno < ppc_num_gprs; regno++)
1377 if (REG_VALID == regcache->get_register_status
1378 (tdep->ppc_gp0_regnum + regno))
1379 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1383 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1385 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
1388 for (regno = 0; regno < ppc_num_gprs; regno++)
1389 if (REG_VALID == regcache->get_register_status
1390 (tdep->ppc_gp0_regnum + regno))
1391 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1394 /* Store the floating point registers into a double array. */
1396 fill_fprs (const struct regcache *regcache, double *vals)
1398 struct gdbarch *gdbarch = regcache->arch ();
1399 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1402 /* This function should never be called on architectures without
1403 floating-point registers. */
1404 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1406 for (regno = tdep->ppc_fp0_regnum;
1407 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1409 if (REG_VALID == regcache->get_register_status (regno))
1410 regcache->raw_collect (regno, vals + regno - tdep->ppc_fp0_regnum);
1413 /* Store the special registers into the specified 64-bit and 32-bit
1417 fill_sprs64 (const struct regcache *regcache,
1418 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1419 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1422 struct gdbarch *gdbarch = regcache->arch ();
1423 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1425 /* Verify that the size of the size of the IAR buffer is the
1426 same as the raw size of the PC (in the register cache). If
1427 they're not, then either GDB has been built incorrectly, or
1428 there's some other kind of internal error. To be really safe,
1429 we should check all of the sizes. */
1430 gdb_assert (sizeof (*iar) == register_size
1431 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1433 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1434 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1435 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1436 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1437 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1438 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1439 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1440 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1441 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1442 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1443 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1444 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1445 if (tdep->ppc_fpscr_regnum >= 0
1446 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1447 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1451 fill_sprs32 (const struct regcache *regcache,
1452 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1453 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1456 struct gdbarch *gdbarch = regcache->arch ();
1457 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1459 /* Verify that the size of the size of the IAR buffer is the
1460 same as the raw size of the PC (in the register cache). If
1461 they're not, then either GDB has been built incorrectly, or
1462 there's some other kind of internal error. To be really safe,
1463 we should check all of the sizes. */
1464 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1465 gdbarch_pc_regnum (gdbarch)));
1467 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1468 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1469 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1470 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1471 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1472 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1473 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1474 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1475 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1476 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1477 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1478 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1479 if (tdep->ppc_fpscr_regnum >= 0
1480 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1481 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1484 /* Store all registers into pthread PDTID, which doesn't have a kernel
1487 It's possible to store a single register into a non-kernel pthread,
1488 but I doubt it's worth the effort. */
1491 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1493 struct gdbarch *gdbarch = regcache->arch ();
1494 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1496 pthdb_context_t ctx;
1500 if (debug_aix_thread)
1501 fprintf_unfiltered (gdb_stdlog,
1502 "store_regs_user_thread %lx\n", (long) pdtid);
1504 /* Retrieve the thread's current context for its non-register
1506 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1507 if (status != PTHDB_SUCCESS)
1508 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1509 pd_status2str (status));
1511 /* Collect general-purpose register values from the regcache. */
1513 for (i = 0; i < ppc_num_gprs; i++)
1514 if (REG_VALID == regcache->get_register_status (tdep->ppc_gp0_regnum + i))
1518 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int64);
1523 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int32);
1528 /* Collect floating-point register values from the regcache. */
1529 if (ppc_floating_point_unit_p (gdbarch))
1530 fill_fprs (regcache, ctx.fpr);
1532 /* Special registers (always kept in ctx as 64 bits). */
1535 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1536 &ctx.xer, &ctx.fpscr);
1540 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1541 Solution: use 32-bit temp variables. */
1542 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1545 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1546 &tmp_xer, &tmp_fpscr);
1547 if (REG_VALID == regcache->get_register_status
1548 (gdbarch_pc_regnum (gdbarch)))
1550 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1552 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1554 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1556 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1558 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1560 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1561 ctx.fpscr = tmp_fpscr;
1564 status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1565 if (status != PTHDB_SUCCESS)
1566 error (_("aix-thread: store_registers: "
1567 "pthdb_pthread_setcontext returned %s"),
1568 pd_status2str (status));
1571 /* Store register REGNO if != -1 or all registers otherwise into
1574 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1575 SPRs, but there's no way to set individual registers within those
1576 groups. Therefore, if REGNO != -1, this function stores an entire
1580 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1583 struct gdbarch *gdbarch = regcache->arch ();
1584 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1585 uint64_t gprs64[ppc_num_gprs];
1586 uint32_t gprs32[ppc_num_gprs];
1587 double fprs[ppc_num_fprs];
1588 struct ptxsprs sprs64;
1589 struct ptsprs sprs32;
1591 if (debug_aix_thread)
1592 fprintf_unfiltered (gdb_stdlog,
1593 "store_regs_kernel_thread tid=%lx regno=%d\n",
1596 /* General-purpose registers. */
1598 || (tdep->ppc_gp0_regnum <= regno
1599 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1603 /* Pre-fetch: some regs may not be in the cache. */
1604 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1605 fill_gprs64 (regcache, gprs64);
1606 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1610 /* Pre-fetch: some regs may not be in the cache. */
1611 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1612 fill_gprs32 (regcache, gprs32);
1613 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1617 /* Floating-point registers. */
1619 if (ppc_floating_point_unit_p (gdbarch)
1621 || (regno >= tdep->ppc_fp0_regnum
1622 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1624 /* Pre-fetch: some regs may not be in the cache. */
1625 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1626 fill_fprs (regcache, fprs);
1627 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1630 /* Special-purpose registers. */
1632 if (regno == -1 || special_register_p (gdbarch, regno))
1636 /* Pre-fetch: some registers won't be in the cache. */
1637 ptrace64aix (PTT_READ_SPRS, tid,
1638 (unsigned long) &sprs64, 0, NULL);
1639 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1640 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1641 &sprs64.pt_xer, &sprs64.pt_fpscr);
1642 ptrace64aix (PTT_WRITE_SPRS, tid,
1643 (unsigned long) &sprs64, 0, NULL);
1647 /* The contents of "struct ptspr" were declared as "unsigned
1648 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1649 Use temporaries to work around this problem. Also, add an
1650 assert here to make sure we fail if the system header files
1651 use "unsigned long", and the size of that type is not what
1652 the headers expect. */
1653 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1656 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1658 /* Pre-fetch: some registers won't be in the cache. */
1659 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1661 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1662 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1664 sprs32.pt_iar = tmp_iar;
1665 sprs32.pt_msr = tmp_msr;
1666 sprs32.pt_cr = tmp_cr;
1667 sprs32.pt_lr = tmp_lr;
1668 sprs32.pt_ctr = tmp_ctr;
1669 sprs32.pt_xer = tmp_xer;
1670 sprs32.pt_fpscr = tmp_fpscr;
1672 if (tdep->ppc_mq_regnum >= 0)
1673 if (REG_VALID == regcache->get_register_status
1674 (tdep->ppc_mq_regnum))
1675 regcache->raw_collect (tdep->ppc_mq_regnum, &sprs32.pt_mq);
1677 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1682 /* Store gdb's current view of the register set into the
1683 thread/process connected to REGCACHE. */
1686 aix_thread_target::store_registers (struct regcache *regcache, int regno)
1688 struct thread_info *thread;
1691 if (!PD_TID (regcache->ptid ()))
1692 beneath ()->store_registers (regcache, regno);
1695 thread = find_thread_ptid (regcache->ptid ());
1696 aix_thread_info *priv = get_aix_thread_info (thread);
1699 if (tid == PTHDB_INVALID_TID)
1700 store_regs_user_thread (regcache, priv->pdtid);
1702 store_regs_kernel_thread (regcache, regno, tid);
1706 /* Implement the to_xfer_partial target_ops method. */
1708 enum target_xfer_status
1709 aix_thread_target::xfer_partial (enum target_object object,
1710 const char *annex, gdb_byte *readbuf,
1711 const gdb_byte *writebuf,
1712 ULONGEST offset, ULONGEST len,
1713 ULONGEST *xfered_len)
1715 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1717 inferior_ptid = ptid_t (inferior_ptid.pid ());
1718 return beneath ()->xfer_partial (object, annex, readbuf,
1719 writebuf, offset, len, xfered_len);
1722 /* Clean up after the inferior exits. */
1725 aix_thread_target::mourn_inferior ()
1727 target_ops *beneath = this->beneath ();
1730 beneath->mourn_inferior ();
1733 /* Return whether thread PID is still valid. */
1736 aix_thread_target::thread_alive (ptid_t ptid)
1739 return beneath ()->thread_alive (ptid);
1741 /* We update the thread list every time the child stops, so all
1742 valid threads should be in the thread list. */
1743 return in_thread_list (ptid);
1746 /* Return a printable representation of composite PID for use in
1747 "info threads" output. */
1750 aix_thread_target::pid_to_str (ptid_t ptid)
1752 static char *ret = NULL;
1755 return beneath ()->pid_to_str (ptid);
1757 /* Free previous return value; a new one will be allocated by
1761 ret = xstrprintf (_("Thread %ld"), ptid.tid ());
1765 /* Return a printable representation of extra information about
1766 THREAD, for use in "info threads" output. */
1769 aix_thread_target::extra_thread_info (struct thread_info *thread)
1772 pthdb_pthread_t pdtid;
1774 pthdb_state_t state;
1775 pthdb_suspendstate_t suspendstate;
1776 pthdb_detachstate_t detachstate;
1778 static char *ret = NULL;
1780 if (!PD_TID (thread->ptid))
1784 aix_thread_info *priv = get_aix_thread_info (thread);
1786 pdtid = priv->pdtid;
1789 if (tid != PTHDB_INVALID_TID)
1790 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1791 buf.printf (_("tid %d"), (int)tid);
1793 status = pthdb_pthread_state (pd_session, pdtid, &state);
1794 if (status != PTHDB_SUCCESS)
1796 buf.printf (", %s", state2str (state));
1798 status = pthdb_pthread_suspendstate (pd_session, pdtid,
1800 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
1801 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1802 buf.printf (_(", suspended"));
1804 status = pthdb_pthread_detachstate (pd_session, pdtid,
1806 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
1807 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1808 buf.printf (_(", detached"));
1810 pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1811 if (status == PTHDB_SUCCESS && cancelpend)
1812 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1813 buf.printf (_(", cancel pending"));
1817 xfree (ret); /* Free old buffer. */
1819 ret = xstrdup (buf.c_str ());
1825 aix_thread_target::get_ada_task_ptid (long lwp, long thread)
1827 return ptid_t (inferior_ptid.pid (), 0, thread);
1831 /* Module startup initialization function, automagically called by
1835 _initialize_aix_thread (void)
1837 /* Notice when object files get loaded and unloaded. */
1838 gdb::observers::new_objfile.attach (new_objfile);
1840 /* Add ourselves to inferior_created event chain.
1841 This is needed to enable the thread target on "attach". */
1842 gdb::observers::inferior_created.attach (aix_thread_inferior_created);
1844 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
1845 _("Set debugging of AIX thread module."),
1846 _("Show debugging of AIX thread module."),
1847 _("Enables debugging output (used to debug GDB)."),
1849 /* FIXME: i18n: Debugging of AIX thread
1850 module is \"%d\". */
1851 &setdebuglist, &showdebuglist);