1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2017 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"
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_get_tid (ptid) != 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 private_thread_info {
88 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
89 pthdb_tid_t tid; /* kernel thread id */
92 /* Information about a thread of which libpthdebug is aware. */
95 pthdb_pthread_t pdtid;
100 /* This module's target-specific operations, active while pd_able is true. */
102 static struct target_ops aix_thread_ops;
104 /* Address of the function that libpthread will call when libpthdebug
105 is ready to be initialized. */
107 static CORE_ADDR pd_brk_addr;
109 /* Whether the current application is debuggable by pthdb. */
111 static int pd_able = 0;
113 /* Whether a threaded application is being debugged. */
115 static int pd_active = 0;
117 /* Whether the current architecture is 64-bit.
118 Only valid when pd_able is true. */
122 /* Forward declarations for pthdb callbacks. */
124 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
125 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
126 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
127 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
128 unsigned long long flags,
129 pthdb_context_t *context);
130 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
131 unsigned long long flags,
132 pthdb_context_t *context);
133 static int pdc_alloc (pthdb_user_t, size_t, void **);
134 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
135 static int pdc_dealloc (pthdb_user_t, void *);
137 /* pthdb callbacks. */
139 static pthdb_callbacks_t pd_callbacks = {
151 /* Current pthdb session. */
153 static pthdb_session_t pd_session;
155 /* Return a printable representation of pthdebug function return
159 pd_status2str (int status)
163 case PTHDB_SUCCESS: return "SUCCESS";
164 case PTHDB_NOSYS: return "NOSYS";
165 case PTHDB_NOTSUP: return "NOTSUP";
166 case PTHDB_BAD_VERSION: return "BAD_VERSION";
167 case PTHDB_BAD_USER: return "BAD_USER";
168 case PTHDB_BAD_SESSION: return "BAD_SESSION";
169 case PTHDB_BAD_MODE: return "BAD_MODE";
170 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
171 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
172 case PTHDB_BAD_POINTER: return "BAD_POINTER";
173 case PTHDB_BAD_CMD: return "BAD_CMD";
174 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
175 case PTHDB_BAD_ATTR: return "BAD_ATTR";
176 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
177 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
178 case PTHDB_BAD_COND: return "BAD_COND";
179 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
180 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
181 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
182 case PTHDB_BAD_KEY: return "BAD_KEY";
183 case PTHDB_BAD_PTID: return "BAD_PTID";
184 case PTHDB_BAD_TID: return "BAD_TID";
185 case PTHDB_CALLBACK: return "CALLBACK";
186 case PTHDB_CONTEXT: return "CONTEXT";
187 case PTHDB_HELD: return "HELD";
188 case PTHDB_NOT_HELD: return "NOT_HELD";
189 case PTHDB_MEMORY: return "MEMORY";
190 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
191 case PTHDB_SYMBOL: return "SYMBOL";
192 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
193 case PTHDB_INTERNAL: return "INTERNAL";
194 default: return "UNKNOWN";
198 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
199 exceptional conditions and either return nonlocally or else return
200 1 for success and 0 for failure. */
203 ptrace_check (int req, int id, int ret)
205 if (ret == 0 && !errno)
208 /* According to ptrace(2), ptrace may fail with EPERM if "the
209 Identifier parameter corresponds to a kernel thread which is
210 stopped in kernel mode and whose computational state cannot be
211 read or written." This happens quite often with register reads. */
218 if (ret == -1 && errno == EPERM)
220 if (debug_aix_thread)
221 fprintf_unfiltered (gdb_stdlog,
222 "ptrace (%d, %d) = %d (errno = %d)\n",
223 req, id, ret, errno);
224 return ret == -1 ? 0 : 1;
228 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
229 req, id, ret, errno, safe_strerror (errno));
230 return 0; /* Not reached. */
233 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
234 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
238 # define ptracex(request, pid, addr, data, buf) \
239 ptrace64 (request, pid, addr, data, buf)
243 ptrace64aix (int req, int id, long long addr, int data, int *buf)
246 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
249 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
250 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
254 # define ptrace(request, pid, addr, data, buf) \
255 ptrace64 (request, pid, addr, data, buf)
256 # define addr_ptr long long
258 # define addr_ptr int *
262 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
265 return ptrace_check (req, id,
266 ptrace (req, id, addr, data, buf));
269 /* If *PIDP is a composite process/thread id, convert it to a
273 pid_to_prc (ptid_t *ptidp)
279 *ptidp = pid_to_ptid (ptid_get_pid (ptid));
282 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
283 the address of SYMBOLS[<i>].name. */
286 pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
288 struct bound_minimal_symbol ms;
292 if (debug_aix_thread)
293 fprintf_unfiltered (gdb_stdlog,
294 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
295 user, (long) symbols, count);
297 for (i = 0; i < count; i++)
299 name = symbols[i].name;
300 if (debug_aix_thread)
301 fprintf_unfiltered (gdb_stdlog,
302 " symbols[%d].name = \"%s\"\n", i, name);
308 ms = lookup_minimal_symbol (name, NULL, NULL);
309 if (ms.minsym == NULL)
311 if (debug_aix_thread)
312 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE\n");
315 symbols[i].addr = BMSYMBOL_VALUE_ADDRESS (ms);
317 if (debug_aix_thread)
318 fprintf_unfiltered (gdb_stdlog, " symbols[%d].addr = %s\n",
319 i, hex_string (symbols[i].addr));
321 if (debug_aix_thread)
322 fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS\n");
326 /* Read registers call back function should be able to read the
327 context information of a debuggee kernel thread from an active
328 process or from a core file. The information should be formatted
329 in context64 form for both 32-bit and 64-bit process.
330 If successful return 0, else non-zero is returned. */
333 pdc_read_regs (pthdb_user_t user,
335 unsigned long long flags,
336 pthdb_context_t *context)
338 /* This function doesn't appear to be used, so we could probably
339 just return 0 here. HOWEVER, if it is not defined, the OS will
340 complain and several thread debug functions will fail. In case
341 this is needed, I have implemented what I think it should do,
342 however this code is untested. */
344 uint64_t gprs64[ppc_num_gprs];
345 uint32_t gprs32[ppc_num_gprs];
346 double fprs[ppc_num_fprs];
347 struct ptxsprs sprs64;
348 struct ptsprs sprs32;
350 if (debug_aix_thread)
351 fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
352 (int) tid, hex_string (flags));
354 /* General-purpose registers. */
355 if (flags & PTHDB_FLAG_GPRS)
359 if (!ptrace64aix (PTT_READ_GPRS, tid,
360 (unsigned long) gprs64, 0, NULL))
361 memset (gprs64, 0, sizeof (gprs64));
362 memcpy (context->gpr, gprs64, sizeof(gprs64));
366 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
367 memset (gprs32, 0, sizeof (gprs32));
368 memcpy (context->gpr, gprs32, sizeof(gprs32));
372 /* Floating-point registers. */
373 if (flags & PTHDB_FLAG_FPRS)
375 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
376 memset (fprs, 0, sizeof (fprs));
377 memcpy (context->fpr, fprs, sizeof(fprs));
380 /* Special-purpose registers. */
381 if (flags & PTHDB_FLAG_SPRS)
385 if (!ptrace64aix (PTT_READ_SPRS, tid,
386 (unsigned long) &sprs64, 0, NULL))
387 memset (&sprs64, 0, sizeof (sprs64));
388 memcpy (&context->msr, &sprs64, sizeof(sprs64));
392 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
393 memset (&sprs32, 0, sizeof (sprs32));
394 memcpy (&context->msr, &sprs32, sizeof(sprs32));
400 /* Write register function should be able to write requested context
401 information to specified debuggee's kernel thread id.
402 If successful return 0, else non-zero is returned. */
405 pdc_write_regs (pthdb_user_t user,
407 unsigned long long flags,
408 pthdb_context_t *context)
410 /* This function doesn't appear to be used, so we could probably
411 just return 0 here. HOWEVER, if it is not defined, the OS will
412 complain and several thread debug functions will fail. In case
413 this is needed, I have implemented what I think it should do,
414 however this code is untested. */
416 if (debug_aix_thread)
417 fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
418 (int) tid, hex_string (flags));
420 /* General-purpose registers. */
421 if (flags & PTHDB_FLAG_GPRS)
424 ptrace64aix (PTT_WRITE_GPRS, tid,
425 (unsigned long) context->gpr, 0, NULL);
427 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
430 /* Floating-point registers. */
431 if (flags & PTHDB_FLAG_FPRS)
433 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
436 /* Special-purpose registers. */
437 if (flags & PTHDB_FLAG_SPRS)
441 ptrace64aix (PTT_WRITE_SPRS, tid,
442 (unsigned long) &context->msr, 0, NULL);
446 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
452 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
455 pdc_read_data (pthdb_user_t user, void *buf,
456 pthdb_addr_t addr, size_t len)
460 if (debug_aix_thread)
461 fprintf_unfiltered (gdb_stdlog,
462 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
463 user, (long) buf, hex_string (addr), len);
465 status = target_read_memory (addr, (gdb_byte *) buf, len);
466 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
468 if (debug_aix_thread)
469 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n",
470 status, pd_status2str (ret));
474 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
477 pdc_write_data (pthdb_user_t user, void *buf,
478 pthdb_addr_t addr, size_t len)
482 if (debug_aix_thread)
483 fprintf_unfiltered (gdb_stdlog,
484 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
485 user, (long) buf, hex_string (addr), len);
487 status = target_write_memory (addr, (gdb_byte *) buf, len);
488 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
490 if (debug_aix_thread)
491 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n", status,
492 pd_status2str (ret));
496 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
500 pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
502 if (debug_aix_thread)
503 fprintf_unfiltered (gdb_stdlog,
504 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
505 user, len, (long) bufp);
506 *bufp = xmalloc (len);
507 if (debug_aix_thread)
508 fprintf_unfiltered (gdb_stdlog,
509 " malloc returned 0x%lx\n", (long) *bufp);
511 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
514 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
517 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
518 realloc callback, so that it contains LEN bytes, and store a
519 pointer to the result in BUFP. */
522 pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
524 if (debug_aix_thread)
525 fprintf_unfiltered (gdb_stdlog,
526 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
527 user, (long) buf, len, (long) bufp);
528 *bufp = xrealloc (buf, len);
529 if (debug_aix_thread)
530 fprintf_unfiltered (gdb_stdlog,
531 " realloc returned 0x%lx\n", (long) *bufp);
532 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
535 /* pthdb callback: free BUF, which was allocated by the alloc or
539 pdc_dealloc (pthdb_user_t user, void *buf)
541 if (debug_aix_thread)
542 fprintf_unfiltered (gdb_stdlog,
543 "pdc_free (user = %ld, buf = 0x%lx)\n", user,
549 /* Return a printable representation of pthread STATE. */
552 state2str (pthdb_state_t state)
557 /* i18n: Like "Thread-Id %d, [state] idle" */
558 return _("idle"); /* being created */
560 /* i18n: Like "Thread-Id %d, [state] running" */
561 return _("running"); /* running */
563 /* i18n: Like "Thread-Id %d, [state] sleeping" */
564 return _("sleeping"); /* awaiting an event */
566 /* i18n: Like "Thread-Id %d, [state] ready" */
567 return _("ready"); /* runnable */
569 /* i18n: Like "Thread-Id %d, [state] finished" */
570 return _("finished"); /* awaiting a join/detach */
572 /* i18n: Like "Thread-Id %d, [state] unknown" */
577 /* qsort() comparison function for sorting pd_thread structs by pthid. */
580 pcmp (const void *p1v, const void *p2v)
582 struct pd_thread *p1 = (struct pd_thread *) p1v;
583 struct pd_thread *p2 = (struct pd_thread *) p2v;
584 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
587 /* iterate_over_threads() callback for counting GDB threads.
589 Do not count the main thread (whose tid is zero). This matches
590 the list of threads provided by the pthreaddebug library, which
591 does not include that main thread either, and thus allows us
592 to compare the two lists. */
595 giter_count (struct thread_info *thread, void *countp)
597 if (PD_TID (thread->ptid))
602 /* iterate_over_threads() callback for accumulating GDB thread pids.
604 Do not include the main thread (whose tid is zero). This matches
605 the list of threads provided by the pthreaddebug library, which
606 does not include that main thread either, and thus allows us
607 to compare the two lists. */
610 giter_accum (struct thread_info *thread, void *bufp)
612 if (PD_TID (thread->ptid))
614 **(struct thread_info ***) bufp = thread;
615 (*(struct thread_info ***) bufp)++;
620 /* ptid comparison function */
623 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
627 if (ptid_get_pid (ptid1) < ptid_get_pid (ptid2))
629 else if (ptid_get_pid (ptid1) > ptid_get_pid (ptid2))
631 else if (ptid_get_tid (ptid1) < ptid_get_tid (ptid2))
633 else if (ptid_get_tid (ptid1) > ptid_get_tid (ptid2))
635 else if (ptid_get_lwp (ptid1) < ptid_get_lwp (ptid2))
637 else if (ptid_get_lwp (ptid1) > ptid_get_lwp (ptid2))
643 /* qsort() comparison function for sorting thread_info structs by pid. */
646 gcmp (const void *t1v, const void *t2v)
648 struct thread_info *t1 = *(struct thread_info **) t1v;
649 struct thread_info *t2 = *(struct thread_info **) t2v;
650 return ptid_cmp (t1->ptid, t2->ptid);
653 /* Search through the list of all kernel threads for the thread
654 that has stopped on a SIGTRAP signal, and return its TID.
655 Return 0 if none found. */
658 get_signaled_thread (void)
660 struct thrdsinfo64 thrinf;
666 if (getthrds (ptid_get_pid (inferior_ptid), &thrinf,
667 sizeof (thrinf), &ktid, 1) != 1)
670 if (thrinf.ti_cursig == SIGTRAP)
671 return thrinf.ti_tid;
674 /* Didn't find any thread stopped on a SIGTRAP signal. */
678 /* Synchronize GDB's thread list with libpthdebug's.
680 There are some benefits of doing this every time the inferior stops:
682 - allows users to run thread-specific commands without needing to
683 run "info threads" first
685 - helps pthdb_tid_pthread() work properly (see "libpthdebug
686 peculiarities" at the top of this module)
688 - simplifies the demands placed on libpthdebug, which seems to
689 have difficulty with certain call patterns */
692 sync_threadlists (void)
694 int cmd, status, infpid;
695 int pcount, psize, pi, gcount, gi;
696 struct pd_thread *pbuf;
697 struct thread_info **gbuf, **g, *thread;
698 pthdb_pthread_t pdtid;
702 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
706 pbuf = XNEWVEC (struct pd_thread, psize);
708 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
710 status = pthdb_pthread (pd_session, &pdtid, cmd);
711 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
714 status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
715 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
721 pbuf = (struct pd_thread *) xrealloc (pbuf,
722 psize * sizeof *pbuf);
724 pbuf[pcount].pdtid = pdtid;
725 pbuf[pcount].pthid = pthid;
729 for (pi = 0; pi < pcount; pi++)
731 status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
732 if (status != PTHDB_SUCCESS)
733 tid = PTHDB_INVALID_TID;
737 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
739 /* Accumulate an array of GDB threads sorted by pid. */
742 iterate_over_threads (giter_count, &gcount);
743 g = gbuf = XNEWVEC (struct thread_info *, gcount);
744 iterate_over_threads (giter_accum, &g);
745 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
747 /* Apply differences between the two arrays to GDB's thread list. */
749 infpid = ptid_get_pid (inferior_ptid);
750 for (pi = gi = 0; pi < pcount || gi < gcount;)
754 delete_thread (gbuf[gi]->ptid);
757 else if (gi == gcount)
759 thread = add_thread (ptid_build (infpid, 0, pbuf[pi].pthid));
760 thread->priv = XNEW (struct private_thread_info);
761 thread->priv->pdtid = pbuf[pi].pdtid;
762 thread->priv->tid = pbuf[pi].tid;
770 pptid = ptid_build (infpid, 0, pbuf[pi].pthid);
771 gptid = gbuf[gi]->ptid;
772 pdtid = pbuf[pi].pdtid;
775 cmp_result = ptid_cmp (pptid, gptid);
779 gbuf[gi]->priv->pdtid = pdtid;
780 gbuf[gi]->priv->tid = tid;
784 else if (cmp_result > 0)
786 delete_thread (gptid);
791 thread = add_thread (pptid);
792 thread->priv = XNEW (struct private_thread_info);
793 thread->priv->pdtid = pdtid;
794 thread->priv->tid = tid;
804 /* Iterate_over_threads() callback for locating a thread, using
805 the TID of its associated kernel thread. */
808 iter_tid (struct thread_info *thread, void *tidp)
810 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
812 return (thread->priv->tid == tid);
815 /* Synchronize libpthdebug's state with the inferior and with GDB,
816 generate a composite process/thread <pid> for the current thread,
817 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
820 pd_update (int set_infpid)
825 struct thread_info *thread = NULL;
828 return inferior_ptid;
830 status = pthdb_session_update (pd_session);
831 if (status != PTHDB_SUCCESS)
832 return inferior_ptid;
836 /* Define "current thread" as one that just received a trap signal. */
838 tid = get_signaled_thread ();
840 thread = iterate_over_threads (iter_tid, &tid);
842 ptid = inferior_ptid;
847 inferior_ptid = ptid;
852 /* Try to start debugging threads in the current process.
853 If successful and SET_INFPID, set inferior_ptid to reflect the
857 pd_activate (int set_infpid)
861 status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
862 PTHDB_FLAG_REGS, &pd_callbacks,
864 if (status != PTHDB_SUCCESS)
866 return inferior_ptid;
869 return pd_update (set_infpid);
872 /* Undo the effects of pd_activate(). */
879 pthdb_session_destroy (pd_session);
881 pid_to_prc (&inferior_ptid);
885 /* An object file has just been loaded. Check whether the current
886 application is pthreaded, and if so, prepare for thread debugging. */
893 struct bound_minimal_symbol ms;
895 /* Don't initialize twice. */
899 /* Check application word size. */
900 arch64 = register_size (target_gdbarch (), 0) == 8;
902 /* Check whether the application is pthreaded. */
904 status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
905 &pd_callbacks, &stub_name);
906 if ((status != PTHDB_SUCCESS
907 && status != PTHDB_NOT_PTHREADED) || !stub_name)
910 /* Set a breakpoint on the returned stub function. */
911 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
912 if (ms.minsym == NULL)
914 pd_brk_addr = BMSYMBOL_VALUE_ADDRESS (ms);
915 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
918 /* Prepare for thread debugging. */
919 push_target (&aix_thread_ops);
922 /* If we're debugging a core file or an attached inferior, the
923 pthread library may already have been initialized, so try to
924 activate thread debugging. */
928 /* Undo the effects of pd_enable(). */
938 unpush_target (&aix_thread_ops);
941 /* new_objfile observer callback.
943 If OBJFILE is non-null, check whether a threaded application is
944 being debugged, and if so, prepare for thread debugging.
946 If OBJFILE is null, stop debugging threads. */
949 new_objfile (struct objfile *objfile)
957 /* Attach to process specified by ARGS. */
960 aix_thread_inferior_created (struct target_ops *ops, int from_tty)
965 /* Detach from the process attached to by aix_thread_attach(). */
968 aix_thread_detach (struct target_ops *ops, const char *args, int from_tty)
970 struct target_ops *beneath = find_target_beneath (ops);
973 beneath->to_detach (beneath, args, from_tty);
976 /* Tell the inferior process to continue running thread PID if != -1
977 and all threads otherwise. */
980 aix_thread_resume (struct target_ops *ops,
981 ptid_t ptid, int step, enum gdb_signal sig)
983 struct thread_info *thread;
988 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
989 struct target_ops *beneath = find_target_beneath (ops);
991 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
992 beneath->to_resume (beneath, ptid, step, sig);
996 thread = find_thread_ptid (ptid);
998 error (_("aix-thread resume: unknown pthread %ld"),
999 ptid_get_lwp (ptid));
1001 tid[0] = thread->priv->tid;
1002 if (tid[0] == PTHDB_INVALID_TID)
1003 error (_("aix-thread resume: no tid for pthread %ld"),
1004 ptid_get_lwp (ptid));
1008 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1009 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1011 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1012 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1016 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1017 If an error occurs, return -1, else return the pid of the stopped
1021 aix_thread_wait (struct target_ops *ops,
1022 ptid_t ptid, struct target_waitstatus *status, int options)
1024 struct target_ops *beneath = find_target_beneath (ops);
1027 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1031 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1032 ptid = beneath->to_wait (beneath, ptid, status, options);
1035 if (ptid_get_pid (ptid) == -1)
1036 return pid_to_ptid (-1);
1038 /* Check whether libpthdebug might be ready to be initialized. */
1039 if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
1040 && status->value.sig == GDB_SIGNAL_TRAP)
1042 struct regcache *regcache = get_thread_regcache (ptid);
1043 struct gdbarch *gdbarch = regcache->arch ();
1045 if (regcache_read_pc (regcache)
1046 - gdbarch_decr_pc_after_break (gdbarch) == pd_brk_addr)
1047 return pd_activate (0);
1050 return pd_update (0);
1053 /* Record that the 64-bit general-purpose registers contain VALS. */
1056 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1058 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
1061 for (regno = 0; regno < ppc_num_gprs; regno++)
1062 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + regno,
1063 (char *) (vals + regno));
1066 /* Record that 32-bit register REGNO contains VAL. */
1069 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1071 regcache_raw_supply (regcache, regno, (char *) &val);
1074 /* Record that the floating-point registers contain VALS. */
1077 supply_fprs (struct regcache *regcache, double *vals)
1079 struct gdbarch *gdbarch = regcache->arch ();
1080 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1083 /* This function should never be called on architectures without
1084 floating-point registers. */
1085 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1087 for (regno = tdep->ppc_fp0_regnum;
1088 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1090 regcache_raw_supply (regcache, regno,
1091 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1094 /* Predicate to test whether given register number is a "special" register. */
1096 special_register_p (struct gdbarch *gdbarch, int regno)
1098 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1100 return regno == gdbarch_pc_regnum (gdbarch)
1101 || regno == tdep->ppc_ps_regnum
1102 || regno == tdep->ppc_cr_regnum
1103 || regno == tdep->ppc_lr_regnum
1104 || regno == tdep->ppc_ctr_regnum
1105 || regno == tdep->ppc_xer_regnum
1106 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1107 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1111 /* Record that the special registers contain the specified 64-bit and
1115 supply_sprs64 (struct regcache *regcache,
1116 uint64_t iar, uint64_t msr, uint32_t cr,
1117 uint64_t lr, uint64_t ctr, uint32_t xer,
1120 struct gdbarch *gdbarch = regcache->arch ();
1121 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1123 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1125 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1126 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1127 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1128 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1129 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1130 if (tdep->ppc_fpscr_regnum >= 0)
1131 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1135 /* Record that the special registers contain the specified 32-bit
1139 supply_sprs32 (struct regcache *regcache,
1140 uint32_t iar, uint32_t msr, uint32_t cr,
1141 uint32_t lr, uint32_t ctr, uint32_t xer,
1144 struct gdbarch *gdbarch = regcache->arch ();
1145 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1147 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1149 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1150 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1151 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1152 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1153 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1154 if (tdep->ppc_fpscr_regnum >= 0)
1155 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1159 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1162 There's no way to query a single register from a non-kernel
1163 pthread, so there's no need for a single-register version of this
1167 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1169 struct gdbarch *gdbarch = regcache->arch ();
1170 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1172 pthdb_context_t ctx;
1174 if (debug_aix_thread)
1175 fprintf_unfiltered (gdb_stdlog,
1176 "fetch_regs_user_thread %lx\n", (long) pdtid);
1177 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1178 if (status != PTHDB_SUCCESS)
1179 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1180 pd_status2str (status));
1182 /* General-purpose registers. */
1185 supply_gprs64 (regcache, ctx.gpr);
1187 for (i = 0; i < ppc_num_gprs; i++)
1188 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1190 /* Floating-point registers. */
1192 if (ppc_floating_point_unit_p (gdbarch))
1193 supply_fprs (regcache, ctx.fpr);
1195 /* Special registers. */
1198 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1199 ctx.xer, ctx.fpscr);
1201 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1202 ctx.xer, ctx.fpscr);
1205 /* Fetch register REGNO if != -1 or all registers otherwise from
1208 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1209 SPRs, but there's no way to query individual registers within those
1210 groups. Therefore, if REGNO != -1, this function fetches an entire
1213 Unfortunately, kernel thread register queries often fail with
1214 EPERM, indicating that the thread is in kernel space. This breaks
1215 backtraces of threads other than the current one. To make that
1216 breakage obvious without throwing an error to top level (which is
1217 bad e.g. during "info threads" output), zero registers that can't
1221 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1224 struct gdbarch *gdbarch = regcache->arch ();
1225 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1226 uint64_t gprs64[ppc_num_gprs];
1227 uint32_t gprs32[ppc_num_gprs];
1228 double fprs[ppc_num_fprs];
1229 struct ptxsprs sprs64;
1230 struct ptsprs sprs32;
1233 if (debug_aix_thread)
1234 fprintf_unfiltered (gdb_stdlog,
1235 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1236 (long) tid, regno, arch64);
1238 /* General-purpose registers. */
1240 || (tdep->ppc_gp0_regnum <= regno
1241 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1245 if (!ptrace64aix (PTT_READ_GPRS, tid,
1246 (unsigned long) gprs64, 0, NULL))
1247 memset (gprs64, 0, sizeof (gprs64));
1248 supply_gprs64 (regcache, gprs64);
1252 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1253 memset (gprs32, 0, sizeof (gprs32));
1254 for (i = 0; i < ppc_num_gprs; i++)
1255 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1259 /* Floating-point registers. */
1261 if (ppc_floating_point_unit_p (gdbarch)
1263 || (regno >= tdep->ppc_fp0_regnum
1264 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1266 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1267 memset (fprs, 0, sizeof (fprs));
1268 supply_fprs (regcache, fprs);
1271 /* Special-purpose registers. */
1273 if (regno == -1 || special_register_p (gdbarch, regno))
1277 if (!ptrace64aix (PTT_READ_SPRS, tid,
1278 (unsigned long) &sprs64, 0, NULL))
1279 memset (&sprs64, 0, sizeof (sprs64));
1280 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1281 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1282 sprs64.pt_xer, sprs64.pt_fpscr);
1286 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1288 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1289 memset (&sprs32, 0, sizeof (sprs32));
1290 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1291 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1294 if (tdep->ppc_mq_regnum >= 0)
1295 regcache_raw_supply (regcache, tdep->ppc_mq_regnum,
1296 (char *) &sprs32.pt_mq);
1301 /* Fetch register REGNO if != -1 or all registers otherwise from the
1302 thread/process connected to REGCACHE. */
1305 aix_thread_fetch_registers (struct target_ops *ops,
1306 struct regcache *regcache, int regno)
1308 struct thread_info *thread;
1310 struct target_ops *beneath = find_target_beneath (ops);
1312 if (!PD_TID (regcache_get_ptid (regcache)))
1313 beneath->to_fetch_registers (beneath, regcache, regno);
1316 thread = find_thread_ptid (regcache_get_ptid (regcache));
1317 tid = thread->priv->tid;
1319 if (tid == PTHDB_INVALID_TID)
1320 fetch_regs_user_thread (regcache, thread->priv->pdtid);
1322 fetch_regs_kernel_thread (regcache, regno, tid);
1326 /* Store the gp registers into an array of uint32_t or uint64_t. */
1329 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1331 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
1334 for (regno = 0; regno < ppc_num_gprs; regno++)
1335 if (REG_VALID == regcache_register_status (regcache,
1336 tdep->ppc_gp0_regnum + regno))
1337 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1342 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1344 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
1347 for (regno = 0; regno < ppc_num_gprs; regno++)
1348 if (REG_VALID == regcache_register_status (regcache,
1349 tdep->ppc_gp0_regnum + regno))
1350 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1354 /* Store the floating point registers into a double array. */
1356 fill_fprs (const struct regcache *regcache, double *vals)
1358 struct gdbarch *gdbarch = regcache->arch ();
1359 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1362 /* This function should never be called on architectures without
1363 floating-point registers. */
1364 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1366 for (regno = tdep->ppc_fp0_regnum;
1367 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1369 if (REG_VALID == regcache_register_status (regcache, regno))
1370 regcache_raw_collect (regcache, regno,
1371 vals + regno - tdep->ppc_fp0_regnum);
1374 /* Store the special registers into the specified 64-bit and 32-bit
1378 fill_sprs64 (const struct regcache *regcache,
1379 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1380 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1383 struct gdbarch *gdbarch = regcache->arch ();
1384 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1386 /* Verify that the size of the size of the IAR buffer is the
1387 same as the raw size of the PC (in the register cache). If
1388 they're not, then either GDB has been built incorrectly, or
1389 there's some other kind of internal error. To be really safe,
1390 we should check all of the sizes. */
1391 gdb_assert (sizeof (*iar) == register_size
1392 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1394 if (REG_VALID == regcache_register_status (regcache,
1395 gdbarch_pc_regnum (gdbarch)))
1396 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1397 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1398 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1399 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1400 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1401 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1402 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1403 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1404 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1405 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1406 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1407 if (tdep->ppc_fpscr_regnum >= 0
1408 && REG_VALID == regcache_register_status (regcache,
1409 tdep->ppc_fpscr_regnum))
1410 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1414 fill_sprs32 (const struct regcache *regcache,
1415 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1416 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1419 struct gdbarch *gdbarch = regcache->arch ();
1420 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1422 /* Verify that the size of the size of the IAR buffer is the
1423 same as the raw size of the PC (in the register cache). If
1424 they're not, then either GDB has been built incorrectly, or
1425 there's some other kind of internal error. To be really safe,
1426 we should check all of the sizes. */
1427 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1428 gdbarch_pc_regnum (gdbarch)));
1430 if (REG_VALID == regcache_register_status (regcache,
1431 gdbarch_pc_regnum (gdbarch)))
1432 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1433 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1434 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1435 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1436 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1437 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1438 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1439 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1440 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1441 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1442 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1443 if (tdep->ppc_fpscr_regnum >= 0
1444 && REG_VALID == regcache_register_status (regcache, tdep->ppc_fpscr_regnum))
1445 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1448 /* Store all registers into pthread PDTID, which doesn't have a kernel
1451 It's possible to store a single register into a non-kernel pthread,
1452 but I doubt it's worth the effort. */
1455 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1457 struct gdbarch *gdbarch = regcache->arch ();
1458 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1460 pthdb_context_t ctx;
1465 if (debug_aix_thread)
1466 fprintf_unfiltered (gdb_stdlog,
1467 "store_regs_user_thread %lx\n", (long) pdtid);
1469 /* Retrieve the thread's current context for its non-register
1471 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1472 if (status != PTHDB_SUCCESS)
1473 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1474 pd_status2str (status));
1476 /* Collect general-purpose register values from the regcache. */
1478 for (i = 0; i < ppc_num_gprs; i++)
1479 if (REG_VALID == regcache_register_status (regcache,
1480 tdep->ppc_gp0_regnum + i))
1484 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1490 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1496 /* Collect floating-point register values from the regcache. */
1497 if (ppc_floating_point_unit_p (gdbarch))
1498 fill_fprs (regcache, ctx.fpr);
1500 /* Special registers (always kept in ctx as 64 bits). */
1503 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1504 &ctx.xer, &ctx.fpscr);
1508 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1509 Solution: use 32-bit temp variables. */
1510 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1513 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1514 &tmp_xer, &tmp_fpscr);
1515 if (REG_VALID == regcache_register_status (regcache,
1516 gdbarch_pc_regnum (gdbarch)))
1518 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1520 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1522 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1524 if (REG_VALID == regcache_register_status (regcache,
1525 tdep->ppc_ctr_regnum))
1527 if (REG_VALID == regcache_register_status (regcache,
1528 tdep->ppc_xer_regnum))
1530 if (REG_VALID == regcache_register_status (regcache,
1531 tdep->ppc_xer_regnum))
1532 ctx.fpscr = tmp_fpscr;
1535 status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1536 if (status != PTHDB_SUCCESS)
1537 error (_("aix-thread: store_registers: "
1538 "pthdb_pthread_setcontext returned %s"),
1539 pd_status2str (status));
1542 /* Store register REGNO if != -1 or all registers otherwise into
1545 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1546 SPRs, but there's no way to set individual registers within those
1547 groups. Therefore, if REGNO != -1, this function stores an entire
1551 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1554 struct gdbarch *gdbarch = regcache->arch ();
1555 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1556 uint64_t gprs64[ppc_num_gprs];
1557 uint32_t gprs32[ppc_num_gprs];
1558 double fprs[ppc_num_fprs];
1559 struct ptxsprs sprs64;
1560 struct ptsprs sprs32;
1563 if (debug_aix_thread)
1564 fprintf_unfiltered (gdb_stdlog,
1565 "store_regs_kernel_thread tid=%lx regno=%d\n",
1568 /* General-purpose registers. */
1570 || (tdep->ppc_gp0_regnum <= regno
1571 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1575 /* Pre-fetch: some regs may not be in the cache. */
1576 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1577 fill_gprs64 (regcache, gprs64);
1578 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1582 /* Pre-fetch: some regs may not be in the cache. */
1583 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1584 fill_gprs32 (regcache, gprs32);
1585 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1589 /* Floating-point registers. */
1591 if (ppc_floating_point_unit_p (gdbarch)
1593 || (regno >= tdep->ppc_fp0_regnum
1594 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1596 /* Pre-fetch: some regs may not be in the cache. */
1597 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1598 fill_fprs (regcache, fprs);
1599 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1602 /* Special-purpose registers. */
1604 if (regno == -1 || special_register_p (gdbarch, regno))
1608 /* Pre-fetch: some registers won't be in the cache. */
1609 ptrace64aix (PTT_READ_SPRS, tid,
1610 (unsigned long) &sprs64, 0, NULL);
1611 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1612 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1613 &sprs64.pt_xer, &sprs64.pt_fpscr);
1614 ptrace64aix (PTT_WRITE_SPRS, tid,
1615 (unsigned long) &sprs64, 0, NULL);
1619 /* The contents of "struct ptspr" were declared as "unsigned
1620 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1621 Use temporaries to work around this problem. Also, add an
1622 assert here to make sure we fail if the system header files
1623 use "unsigned long", and the size of that type is not what
1624 the headers expect. */
1625 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1628 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1630 /* Pre-fetch: some registers won't be in the cache. */
1631 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1633 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1634 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1636 sprs32.pt_iar = tmp_iar;
1637 sprs32.pt_msr = tmp_msr;
1638 sprs32.pt_cr = tmp_cr;
1639 sprs32.pt_lr = tmp_lr;
1640 sprs32.pt_ctr = tmp_ctr;
1641 sprs32.pt_xer = tmp_xer;
1642 sprs32.pt_fpscr = tmp_fpscr;
1644 if (tdep->ppc_mq_regnum >= 0)
1645 if (REG_VALID == regcache_register_status (regcache,
1646 tdep->ppc_mq_regnum))
1647 regcache_raw_collect (regcache, tdep->ppc_mq_regnum,
1650 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1655 /* Store gdb's current view of the register set into the
1656 thread/process connected to REGCACHE. */
1659 aix_thread_store_registers (struct target_ops *ops,
1660 struct regcache *regcache, int regno)
1662 struct thread_info *thread;
1664 struct target_ops *beneath = find_target_beneath (ops);
1666 if (!PD_TID (regcache_get_ptid (regcache)))
1667 beneath->to_store_registers (beneath, regcache, regno);
1670 thread = find_thread_ptid (regcache_get_ptid (regcache));
1671 tid = thread->priv->tid;
1673 if (tid == PTHDB_INVALID_TID)
1674 store_regs_user_thread (regcache, thread->priv->pdtid);
1676 store_regs_kernel_thread (regcache, regno, tid);
1680 /* Implement the to_xfer_partial target_ops method. */
1682 static enum target_xfer_status
1683 aix_thread_xfer_partial (struct target_ops *ops, enum target_object object,
1684 const char *annex, gdb_byte *readbuf,
1685 const gdb_byte *writebuf,
1686 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1688 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1689 struct target_ops *beneath = find_target_beneath (ops);
1691 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1692 return beneath->to_xfer_partial (beneath, object, annex, readbuf,
1693 writebuf, offset, len, xfered_len);
1696 /* Clean up after the inferior exits. */
1699 aix_thread_mourn_inferior (struct target_ops *ops)
1701 struct target_ops *beneath = find_target_beneath (ops);
1704 beneath->to_mourn_inferior (beneath);
1707 /* Return whether thread PID is still valid. */
1710 aix_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
1712 struct target_ops *beneath = find_target_beneath (ops);
1715 return beneath->to_thread_alive (beneath, ptid);
1717 /* We update the thread list every time the child stops, so all
1718 valid threads should be in the thread list. */
1719 return in_thread_list (ptid);
1722 /* Return a printable representation of composite PID for use in
1723 "info threads" output. */
1726 aix_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
1728 static char *ret = NULL;
1729 struct target_ops *beneath = find_target_beneath (ops);
1732 return beneath->to_pid_to_str (beneath, ptid);
1734 /* Free previous return value; a new one will be allocated by
1738 ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
1742 /* Return a printable representation of extra information about
1743 THREAD, for use in "info threads" output. */
1746 aix_thread_extra_thread_info (struct target_ops *self,
1747 struct thread_info *thread)
1750 pthdb_pthread_t pdtid;
1752 pthdb_state_t state;
1753 pthdb_suspendstate_t suspendstate;
1754 pthdb_detachstate_t detachstate;
1756 static char *ret = NULL;
1758 if (!PD_TID (thread->ptid))
1763 pdtid = thread->priv->pdtid;
1764 tid = thread->priv->tid;
1766 if (tid != PTHDB_INVALID_TID)
1767 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1768 buf.printf (_("tid %d"), (int)tid);
1770 status = pthdb_pthread_state (pd_session, pdtid, &state);
1771 if (status != PTHDB_SUCCESS)
1773 buf.printf (", %s", state2str (state));
1775 status = pthdb_pthread_suspendstate (pd_session, pdtid,
1777 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
1778 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1779 buf.printf (_(", suspended"));
1781 status = pthdb_pthread_detachstate (pd_session, pdtid,
1783 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
1784 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1785 buf.printf (_(", detached"));
1787 pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1788 if (status == PTHDB_SUCCESS && cancelpend)
1789 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1790 buf.printf (_(", cancel pending"));
1794 xfree (ret); /* Free old buffer. */
1796 ret = xstrdup (buf.c_str ());
1802 aix_thread_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
1804 return ptid_build (ptid_get_pid (inferior_ptid), 0, thread);
1807 /* Initialize target aix_thread_ops. */
1810 init_aix_thread_ops (void)
1812 aix_thread_ops.to_shortname = "aix-threads";
1813 aix_thread_ops.to_longname = _("AIX pthread support");
1814 aix_thread_ops.to_doc = _("AIX pthread support");
1816 aix_thread_ops.to_detach = aix_thread_detach;
1817 aix_thread_ops.to_resume = aix_thread_resume;
1818 aix_thread_ops.to_wait = aix_thread_wait;
1819 aix_thread_ops.to_fetch_registers = aix_thread_fetch_registers;
1820 aix_thread_ops.to_store_registers = aix_thread_store_registers;
1821 aix_thread_ops.to_xfer_partial = aix_thread_xfer_partial;
1822 aix_thread_ops.to_mourn_inferior = aix_thread_mourn_inferior;
1823 aix_thread_ops.to_thread_alive = aix_thread_thread_alive;
1824 aix_thread_ops.to_pid_to_str = aix_thread_pid_to_str;
1825 aix_thread_ops.to_extra_thread_info = aix_thread_extra_thread_info;
1826 aix_thread_ops.to_get_ada_task_ptid = aix_thread_get_ada_task_ptid;
1827 aix_thread_ops.to_stratum = thread_stratum;
1828 aix_thread_ops.to_magic = OPS_MAGIC;
1831 /* Module startup initialization function, automagically called by
1835 _initialize_aix_thread (void)
1837 init_aix_thread_ops ();
1838 complete_target_initialization (&aix_thread_ops);
1840 /* Notice when object files get loaded and unloaded. */
1841 observer_attach_new_objfile (new_objfile);
1843 /* Add ourselves to inferior_created event chain.
1844 This is needed to enable the thread target on "attach". */
1845 observer_attach_inferior_created (aix_thread_inferior_created);
1847 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
1848 _("Set debugging of AIX thread module."),
1849 _("Show debugging of AIX thread module."),
1850 _("Enables debugging output (used to debug GDB)."),
1852 /* FIXME: i18n: Debugging of AIX thread
1853 module is \"%d\". */
1854 &setdebuglist, &showdebuglist);