1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2013 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 "gdb_assert.h"
44 #include "gdbthread.h"
50 #include "gdb_string.h"
54 #include <sys/types.h>
55 #include <sys/ptrace.h>
58 #include <sys/pthdebug.h>
60 #if !HAVE_DECL_GETTHRDS
61 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
64 /* Whether to emit debugging output. */
65 static int debug_aix_thread;
67 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
68 #ifndef PTHDB_VERSION_3
69 #define pthdb_tid_t tid_t
72 /* Return whether to treat PID as a debuggable thread id. */
74 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
76 /* Build a thread ptid. */
77 #define BUILD_THREAD(TID, PID) ptid_build (PID, 0, TID)
79 /* Build and lwp ptid. */
80 #define BUILD_LWP(LWP, PID) MERGEPID (PID, LWP)
82 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
83 PTHDB_BAD_USER errors, so use 1. */
87 /* Success and failure values returned by pthdb callbacks. */
89 #define PDC_SUCCESS PTHDB_SUCCESS
90 #define PDC_FAILURE PTHDB_CALLBACK
92 /* Private data attached to each element in GDB's thread list. */
94 struct private_thread_info {
95 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
96 pthdb_tid_t tid; /* kernel thread id */
99 /* Information about a thread of which libpthdebug is aware. */
102 pthdb_pthread_t pdtid;
107 /* This module's target-specific operations, active while pd_able is true. */
109 static struct target_ops aix_thread_ops;
111 /* Address of the function that libpthread will call when libpthdebug
112 is ready to be initialized. */
114 static CORE_ADDR pd_brk_addr;
116 /* Whether the current application is debuggable by pthdb. */
118 static int pd_able = 0;
120 /* Whether a threaded application is being debugged. */
122 static int pd_active = 0;
124 /* Whether the current architecture is 64-bit.
125 Only valid when pd_able is true. */
129 /* Forward declarations for pthdb callbacks. */
131 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
132 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
133 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
134 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
135 unsigned long long flags,
136 pthdb_context_t *context);
137 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
138 unsigned long long flags,
139 pthdb_context_t *context);
140 static int pdc_alloc (pthdb_user_t, size_t, void **);
141 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
142 static int pdc_dealloc (pthdb_user_t, void *);
144 /* pthdb callbacks. */
146 static pthdb_callbacks_t pd_callbacks = {
158 /* Current pthdb session. */
160 static pthdb_session_t pd_session;
162 /* Return a printable representation of pthdebug function return
166 pd_status2str (int status)
170 case PTHDB_SUCCESS: return "SUCCESS";
171 case PTHDB_NOSYS: return "NOSYS";
172 case PTHDB_NOTSUP: return "NOTSUP";
173 case PTHDB_BAD_VERSION: return "BAD_VERSION";
174 case PTHDB_BAD_USER: return "BAD_USER";
175 case PTHDB_BAD_SESSION: return "BAD_SESSION";
176 case PTHDB_BAD_MODE: return "BAD_MODE";
177 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
178 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
179 case PTHDB_BAD_POINTER: return "BAD_POINTER";
180 case PTHDB_BAD_CMD: return "BAD_CMD";
181 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
182 case PTHDB_BAD_ATTR: return "BAD_ATTR";
183 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
184 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
185 case PTHDB_BAD_COND: return "BAD_COND";
186 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
187 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
188 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
189 case PTHDB_BAD_KEY: return "BAD_KEY";
190 case PTHDB_BAD_PTID: return "BAD_PTID";
191 case PTHDB_BAD_TID: return "BAD_TID";
192 case PTHDB_CALLBACK: return "CALLBACK";
193 case PTHDB_CONTEXT: return "CONTEXT";
194 case PTHDB_HELD: return "HELD";
195 case PTHDB_NOT_HELD: return "NOT_HELD";
196 case PTHDB_MEMORY: return "MEMORY";
197 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
198 case PTHDB_SYMBOL: return "SYMBOL";
199 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
200 case PTHDB_INTERNAL: return "INTERNAL";
201 default: return "UNKNOWN";
205 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
206 exceptional conditions and either return nonlocally or else return
207 1 for success and 0 for failure. */
210 ptrace_check (int req, int id, int ret)
212 if (ret == 0 && !errno)
215 /* According to ptrace(2), ptrace may fail with EPERM if "the
216 Identifier parameter corresponds to a kernel thread which is
217 stopped in kernel mode and whose computational state cannot be
218 read or written." This happens quite often with register reads. */
225 if (ret == -1 && errno == EPERM)
227 if (debug_aix_thread)
228 fprintf_unfiltered (gdb_stdlog,
229 "ptrace (%d, %d) = %d (errno = %d)\n",
230 req, id, ret, errno);
231 return ret == -1 ? 0 : 1;
235 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
236 req, id, ret, errno, safe_strerror (errno));
237 return 0; /* Not reached. */
240 /* Call ptracex (REQ, ID, ADDR, DATA, BUF). Return success. */
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). Return success. */
252 ptrace32 (int req, int id, int *addr, int data, int *buf)
255 return ptrace_check (req, id,
256 ptrace (req, id, (int *) addr, data, buf));
259 /* If *PIDP is a composite process/thread id, convert it to a
263 pid_to_prc (ptid_t *ptidp)
269 *ptidp = pid_to_ptid (PIDGET (ptid));
272 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
273 the address of SYMBOLS[<i>].name. */
276 pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
278 struct minimal_symbol *ms;
282 if (debug_aix_thread)
283 fprintf_unfiltered (gdb_stdlog,
284 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
285 user, (long) symbols, count);
287 for (i = 0; i < count; i++)
289 name = symbols[i].name;
290 if (debug_aix_thread)
291 fprintf_unfiltered (gdb_stdlog,
292 " symbols[%d].name = \"%s\"\n", i, name);
298 if (!(ms = lookup_minimal_symbol (name, NULL, NULL)))
300 if (debug_aix_thread)
301 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE\n");
304 symbols[i].addr = SYMBOL_VALUE_ADDRESS (ms);
306 if (debug_aix_thread)
307 fprintf_unfiltered (gdb_stdlog, " symbols[%d].addr = %s\n",
308 i, hex_string (symbols[i].addr));
310 if (debug_aix_thread)
311 fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS\n");
315 /* Read registers call back function should be able to read the
316 context information of a debuggee kernel thread from an active
317 process or from a core file. The information should be formatted
318 in context64 form for both 32-bit and 64-bit process.
319 If successful return 0, else non-zero is returned. */
322 pdc_read_regs (pthdb_user_t user,
324 unsigned long long flags,
325 pthdb_context_t *context)
327 /* This function doesn't appear to be used, so we could probably
328 just return 0 here. HOWEVER, if it is not defined, the OS will
329 complain and several thread debug functions will fail. In case
330 this is needed, I have implemented what I think it should do,
331 however this code is untested. */
333 uint64_t gprs64[ppc_num_gprs];
334 uint32_t gprs32[ppc_num_gprs];
335 double fprs[ppc_num_fprs];
336 struct ptxsprs sprs64;
337 struct ptsprs sprs32;
339 if (debug_aix_thread)
340 fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
341 (int) tid, hex_string (flags));
343 /* General-purpose registers. */
344 if (flags & PTHDB_FLAG_GPRS)
348 if (!ptrace64aix (PTT_READ_GPRS, tid,
349 (unsigned long) gprs64, 0, NULL))
350 memset (gprs64, 0, sizeof (gprs64));
351 memcpy (context->gpr, gprs64, sizeof(gprs64));
355 if (!ptrace32 (PTT_READ_GPRS, tid, gprs32, 0, NULL))
356 memset (gprs32, 0, sizeof (gprs32));
357 memcpy (context->gpr, gprs32, sizeof(gprs32));
361 /* Floating-point registers. */
362 if (flags & PTHDB_FLAG_FPRS)
364 if (!ptrace32 (PTT_READ_FPRS, tid, (void *) fprs, 0, NULL))
365 memset (fprs, 0, sizeof (fprs));
366 memcpy (context->fpr, fprs, sizeof(fprs));
369 /* Special-purpose registers. */
370 if (flags & PTHDB_FLAG_SPRS)
374 if (!ptrace64aix (PTT_READ_SPRS, tid,
375 (unsigned long) &sprs64, 0, NULL))
376 memset (&sprs64, 0, sizeof (sprs64));
377 memcpy (&context->msr, &sprs64, sizeof(sprs64));
381 if (!ptrace32 (PTT_READ_SPRS, tid, (int *) &sprs32, 0, NULL))
382 memset (&sprs32, 0, sizeof (sprs32));
383 memcpy (&context->msr, &sprs32, sizeof(sprs32));
389 /* Write register function should be able to write requested context
390 information to specified debuggee's kernel thread id.
391 If successful return 0, else non-zero is returned. */
394 pdc_write_regs (pthdb_user_t user,
396 unsigned long long flags,
397 pthdb_context_t *context)
399 /* This function doesn't appear to be used, so we could probably
400 just return 0 here. HOWEVER, if it is not defined, the OS will
401 complain and several thread debug functions will fail. In case
402 this is needed, I have implemented what I think it should do,
403 however this code is untested. */
405 if (debug_aix_thread)
406 fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
407 (int) tid, hex_string (flags));
409 /* General-purpose registers. */
410 if (flags & PTHDB_FLAG_GPRS)
413 ptrace64aix (PTT_WRITE_GPRS, tid,
414 (unsigned long) context->gpr, 0, NULL);
416 ptrace32 (PTT_WRITE_GPRS, tid, (int *) context->gpr, 0, NULL);
419 /* Floating-point registers. */
420 if (flags & PTHDB_FLAG_FPRS)
422 ptrace32 (PTT_WRITE_FPRS, tid, (int *) context->fpr, 0, NULL);
425 /* Special-purpose registers. */
426 if (flags & PTHDB_FLAG_SPRS)
430 ptrace64aix (PTT_WRITE_SPRS, tid,
431 (unsigned long) &context->msr, 0, NULL);
435 ptrace32 (PTT_WRITE_SPRS, tid, (void *) &context->msr, 0, NULL);
441 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
444 pdc_read_data (pthdb_user_t user, void *buf,
445 pthdb_addr_t addr, size_t len)
449 if (debug_aix_thread)
450 fprintf_unfiltered (gdb_stdlog,
451 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
452 user, (long) buf, hex_string (addr), len);
454 status = target_read_memory (addr, buf, len);
455 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
457 if (debug_aix_thread)
458 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n",
459 status, pd_status2str (ret));
463 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
466 pdc_write_data (pthdb_user_t user, void *buf,
467 pthdb_addr_t addr, size_t len)
471 if (debug_aix_thread)
472 fprintf_unfiltered (gdb_stdlog,
473 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
474 user, (long) buf, hex_string (addr), len);
476 status = target_write_memory (addr, buf, len);
477 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
479 if (debug_aix_thread)
480 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n", status,
481 pd_status2str (ret));
485 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
489 pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
491 if (debug_aix_thread)
492 fprintf_unfiltered (gdb_stdlog,
493 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
494 user, len, (long) bufp);
495 *bufp = xmalloc (len);
496 if (debug_aix_thread)
497 fprintf_unfiltered (gdb_stdlog,
498 " malloc returned 0x%lx\n", (long) *bufp);
500 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
503 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
506 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
507 realloc callback, so that it contains LEN bytes, and store a
508 pointer to the result in BUFP. */
511 pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
513 if (debug_aix_thread)
514 fprintf_unfiltered (gdb_stdlog,
515 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
516 user, (long) buf, len, (long) bufp);
517 *bufp = xrealloc (buf, len);
518 if (debug_aix_thread)
519 fprintf_unfiltered (gdb_stdlog,
520 " realloc returned 0x%lx\n", (long) *bufp);
521 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
524 /* pthdb callback: free BUF, which was allocated by the alloc or
528 pdc_dealloc (pthdb_user_t user, void *buf)
530 if (debug_aix_thread)
531 fprintf_unfiltered (gdb_stdlog,
532 "pdc_free (user = %ld, buf = 0x%lx)\n", user,
538 /* Return a printable representation of pthread STATE. */
541 state2str (pthdb_state_t state)
546 /* i18n: Like "Thread-Id %d, [state] idle" */
547 return _("idle"); /* being created */
549 /* i18n: Like "Thread-Id %d, [state] running" */
550 return _("running"); /* running */
552 /* i18n: Like "Thread-Id %d, [state] sleeping" */
553 return _("sleeping"); /* awaiting an event */
555 /* i18n: Like "Thread-Id %d, [state] ready" */
556 return _("ready"); /* runnable */
558 /* i18n: Like "Thread-Id %d, [state] finished" */
559 return _("finished"); /* awaiting a join/detach */
561 /* i18n: Like "Thread-Id %d, [state] unknown" */
566 /* qsort() comparison function for sorting pd_thread structs by pthid. */
569 pcmp (const void *p1v, const void *p2v)
571 struct pd_thread *p1 = (struct pd_thread *) p1v;
572 struct pd_thread *p2 = (struct pd_thread *) p2v;
573 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
576 /* iterate_over_threads() callback for counting GDB threads.
578 Do not count the main thread (whose tid is zero). This matches
579 the list of threads provided by the pthreaddebug library, which
580 does not include that main thread either, and thus allows us
581 to compare the two lists. */
584 giter_count (struct thread_info *thread, void *countp)
586 if (PD_TID (thread->ptid))
591 /* iterate_over_threads() callback for accumulating GDB thread pids.
593 Do not include the main thread (whose tid is zero). This matches
594 the list of threads provided by the pthreaddebug library, which
595 does not include that main thread either, and thus allows us
596 to compare the two lists. */
599 giter_accum (struct thread_info *thread, void *bufp)
601 if (PD_TID (thread->ptid))
603 **(struct thread_info ***) bufp = thread;
604 (*(struct thread_info ***) bufp)++;
609 /* ptid comparison function */
612 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
616 if (ptid_get_pid (ptid1) < ptid_get_pid (ptid2))
618 else if (ptid_get_pid (ptid1) > ptid_get_pid (ptid2))
620 else if (ptid_get_tid (ptid1) < ptid_get_tid (ptid2))
622 else if (ptid_get_tid (ptid1) > ptid_get_tid (ptid2))
624 else if (ptid_get_lwp (ptid1) < ptid_get_lwp (ptid2))
626 else if (ptid_get_lwp (ptid1) > ptid_get_lwp (ptid2))
632 /* qsort() comparison function for sorting thread_info structs by pid. */
635 gcmp (const void *t1v, const void *t2v)
637 struct thread_info *t1 = *(struct thread_info **) t1v;
638 struct thread_info *t2 = *(struct thread_info **) t2v;
639 return ptid_cmp (t1->ptid, t2->ptid);
642 /* Search through the list of all kernel threads for the thread
643 that has stopped on a SIGTRAP signal, and return its TID.
644 Return 0 if none found. */
647 get_signaled_thread (void)
649 struct thrdsinfo64 thrinf;
655 if (getthrds (PIDGET (inferior_ptid), &thrinf,
656 sizeof (thrinf), &ktid, 1) != 1)
659 if (thrinf.ti_cursig == SIGTRAP)
660 return thrinf.ti_tid;
663 /* Didn't find any thread stopped on a SIGTRAP signal. */
667 /* Synchronize GDB's thread list with libpthdebug's.
669 There are some benefits of doing this every time the inferior stops:
671 - allows users to run thread-specific commands without needing to
672 run "info threads" first
674 - helps pthdb_tid_pthread() work properly (see "libpthdebug
675 peculiarities" at the top of this module)
677 - simplifies the demands placed on libpthdebug, which seems to
678 have difficulty with certain call patterns */
681 sync_threadlists (void)
683 int cmd, status, infpid;
684 int pcount, psize, pi, gcount, gi;
685 struct pd_thread *pbuf;
686 struct thread_info **gbuf, **g, *thread;
687 pthdb_pthread_t pdtid;
691 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
695 pbuf = (struct pd_thread *) xmalloc (psize * sizeof *pbuf);
697 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
699 status = pthdb_pthread (pd_session, &pdtid, cmd);
700 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
703 status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
704 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
710 pbuf = (struct pd_thread *) xrealloc (pbuf,
711 psize * sizeof *pbuf);
713 pbuf[pcount].pdtid = pdtid;
714 pbuf[pcount].pthid = pthid;
718 for (pi = 0; pi < pcount; pi++)
720 status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
721 if (status != PTHDB_SUCCESS)
722 tid = PTHDB_INVALID_TID;
726 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
728 /* Accumulate an array of GDB threads sorted by pid. */
731 iterate_over_threads (giter_count, &gcount);
732 g = gbuf = (struct thread_info **) xmalloc (gcount * sizeof *gbuf);
733 iterate_over_threads (giter_accum, &g);
734 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
736 /* Apply differences between the two arrays to GDB's thread list. */
738 infpid = PIDGET (inferior_ptid);
739 for (pi = gi = 0; pi < pcount || gi < gcount;)
743 delete_thread (gbuf[gi]->ptid);
746 else if (gi == gcount)
748 thread = add_thread (BUILD_THREAD (pbuf[pi].pthid, infpid));
749 thread->private = xmalloc (sizeof (struct private_thread_info));
750 thread->private->pdtid = pbuf[pi].pdtid;
751 thread->private->tid = pbuf[pi].tid;
759 pptid = BUILD_THREAD (pbuf[pi].pthid, infpid);
760 gptid = gbuf[gi]->ptid;
761 pdtid = pbuf[pi].pdtid;
764 cmp_result = ptid_cmp (pptid, gptid);
768 gbuf[gi]->private->pdtid = pdtid;
769 gbuf[gi]->private->tid = tid;
773 else if (cmp_result > 0)
775 delete_thread (gptid);
780 thread = add_thread (pptid);
781 thread->private = xmalloc (sizeof (struct private_thread_info));
782 thread->private->pdtid = pdtid;
783 thread->private->tid = tid;
793 /* Iterate_over_threads() callback for locating a thread, using
794 the TID of its associated kernel thread. */
797 iter_tid (struct thread_info *thread, void *tidp)
799 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
801 return (thread->private->tid == tid);
804 /* Synchronize libpthdebug's state with the inferior and with GDB,
805 generate a composite process/thread <pid> for the current thread,
806 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
809 pd_update (int set_infpid)
814 struct thread_info *thread = NULL;
817 return inferior_ptid;
819 status = pthdb_session_update (pd_session);
820 if (status != PTHDB_SUCCESS)
821 return inferior_ptid;
825 /* Define "current thread" as one that just received a trap signal. */
827 tid = get_signaled_thread ();
829 thread = iterate_over_threads (iter_tid, &tid);
831 ptid = inferior_ptid;
836 inferior_ptid = ptid;
841 /* Try to start debugging threads in the current process.
842 If successful and SET_INFPID, set inferior_ptid to reflect the
846 pd_activate (int set_infpid)
850 status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
851 PTHDB_FLAG_REGS, &pd_callbacks,
853 if (status != PTHDB_SUCCESS)
855 return inferior_ptid;
858 return pd_update (set_infpid);
861 /* Undo the effects of pd_activate(). */
868 pthdb_session_destroy (pd_session);
870 pid_to_prc (&inferior_ptid);
874 /* An object file has just been loaded. Check whether the current
875 application is pthreaded, and if so, prepare for thread debugging. */
882 struct minimal_symbol *ms;
884 /* Don't initialize twice. */
888 /* Check application word size. */
889 arch64 = register_size (target_gdbarch (), 0) == 8;
891 /* Check whether the application is pthreaded. */
893 status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
894 &pd_callbacks, &stub_name);
895 if ((status != PTHDB_SUCCESS
896 && status != PTHDB_NOT_PTHREADED) || !stub_name)
899 /* Set a breakpoint on the returned stub function. */
900 if (!(ms = lookup_minimal_symbol (stub_name, NULL, NULL)))
902 pd_brk_addr = SYMBOL_VALUE_ADDRESS (ms);
903 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
906 /* Prepare for thread debugging. */
907 push_target (&aix_thread_ops);
910 /* If we're debugging a core file or an attached inferior, the
911 pthread library may already have been initialized, so try to
912 activate thread debugging. */
916 /* Undo the effects of pd_enable(). */
926 unpush_target (&aix_thread_ops);
929 /* new_objfile observer callback.
931 If OBJFILE is non-null, check whether a threaded application is
932 being debugged, and if so, prepare for thread debugging.
934 If OBJFILE is null, stop debugging threads. */
937 new_objfile (struct objfile *objfile)
945 /* Attach to process specified by ARGS. */
948 aix_thread_attach (struct target_ops *ops, char *args, int from_tty)
950 struct target_ops *beneath = find_target_beneath (ops);
952 beneath->to_attach (beneath, args, from_tty);
956 /* Detach from the process attached to by aix_thread_attach(). */
959 aix_thread_detach (struct target_ops *ops, char *args, int from_tty)
961 struct target_ops *beneath = find_target_beneath (ops);
964 beneath->to_detach (beneath, args, from_tty);
967 /* Tell the inferior process to continue running thread PID if != -1
968 and all threads otherwise. */
971 aix_thread_resume (struct target_ops *ops,
972 ptid_t ptid, int step, enum gdb_signal sig)
974 struct thread_info *thread;
979 struct cleanup *cleanup = save_inferior_ptid ();
980 struct target_ops *beneath = find_target_beneath (ops);
982 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
983 beneath->to_resume (beneath, ptid, step, sig);
984 do_cleanups (cleanup);
988 thread = find_thread_ptid (ptid);
990 error (_("aix-thread resume: unknown pthread %ld"),
993 tid[0] = thread->private->tid;
994 if (tid[0] == PTHDB_INVALID_TID)
995 error (_("aix-thread resume: no tid for pthread %ld"),
1000 ptrace64aix (PTT_CONTINUE, tid[0], 1,
1001 gdb_signal_to_host (sig), (void *) tid);
1003 ptrace32 (PTT_CONTINUE, tid[0], (int *) 1,
1004 gdb_signal_to_host (sig), (void *) tid);
1008 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1009 If an error occurs, return -1, else return the pid of the stopped
1013 aix_thread_wait (struct target_ops *ops,
1014 ptid_t ptid, struct target_waitstatus *status, int options)
1016 struct cleanup *cleanup = save_inferior_ptid ();
1017 struct target_ops *beneath = find_target_beneath (ops);
1021 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1022 ptid = beneath->to_wait (beneath, ptid, status, options);
1023 do_cleanups (cleanup);
1025 if (PIDGET (ptid) == -1)
1026 return pid_to_ptid (-1);
1028 /* Check whether libpthdebug might be ready to be initialized. */
1029 if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
1030 && status->value.sig == GDB_SIGNAL_TRAP)
1032 struct regcache *regcache = get_thread_regcache (ptid);
1033 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1035 if (regcache_read_pc (regcache)
1036 - gdbarch_decr_pc_after_break (gdbarch) == pd_brk_addr)
1037 return pd_activate (0);
1040 return pd_update (0);
1043 /* Record that the 64-bit general-purpose registers contain VALS. */
1046 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1048 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1051 for (regno = 0; regno < ppc_num_gprs; regno++)
1052 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + regno,
1053 (char *) (vals + regno));
1056 /* Record that 32-bit register REGNO contains VAL. */
1059 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1061 regcache_raw_supply (regcache, regno, (char *) &val);
1064 /* Record that the floating-point registers contain VALS. */
1067 supply_fprs (struct regcache *regcache, double *vals)
1069 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1070 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1073 /* This function should never be called on architectures without
1074 floating-point registers. */
1075 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1077 for (regno = tdep->ppc_fp0_regnum;
1078 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1080 regcache_raw_supply (regcache, regno,
1081 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1084 /* Predicate to test whether given register number is a "special" register. */
1086 special_register_p (struct gdbarch *gdbarch, int regno)
1088 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1090 return regno == gdbarch_pc_regnum (gdbarch)
1091 || regno == tdep->ppc_ps_regnum
1092 || regno == tdep->ppc_cr_regnum
1093 || regno == tdep->ppc_lr_regnum
1094 || regno == tdep->ppc_ctr_regnum
1095 || regno == tdep->ppc_xer_regnum
1096 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1097 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1101 /* Record that the special registers contain the specified 64-bit and
1105 supply_sprs64 (struct regcache *regcache,
1106 uint64_t iar, uint64_t msr, uint32_t cr,
1107 uint64_t lr, uint64_t ctr, uint32_t xer,
1110 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1111 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1113 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1115 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1116 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1117 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1118 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1119 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1120 if (tdep->ppc_fpscr_regnum >= 0)
1121 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1125 /* Record that the special registers contain the specified 32-bit
1129 supply_sprs32 (struct regcache *regcache,
1130 uint32_t iar, uint32_t msr, uint32_t cr,
1131 uint32_t lr, uint32_t ctr, uint32_t xer,
1134 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1135 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1137 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1139 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1140 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1141 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1142 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1143 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1144 if (tdep->ppc_fpscr_regnum >= 0)
1145 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1149 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1152 There's no way to query a single register from a non-kernel
1153 pthread, so there's no need for a single-register version of this
1157 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1159 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1160 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1162 pthdb_context_t ctx;
1164 if (debug_aix_thread)
1165 fprintf_unfiltered (gdb_stdlog,
1166 "fetch_regs_user_thread %lx\n", (long) pdtid);
1167 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1168 if (status != PTHDB_SUCCESS)
1169 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1170 pd_status2str (status));
1172 /* General-purpose registers. */
1175 supply_gprs64 (regcache, ctx.gpr);
1177 for (i = 0; i < ppc_num_gprs; i++)
1178 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1180 /* Floating-point registers. */
1182 if (ppc_floating_point_unit_p (gdbarch))
1183 supply_fprs (regcache, ctx.fpr);
1185 /* Special registers. */
1188 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1189 ctx.xer, ctx.fpscr);
1191 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1192 ctx.xer, ctx.fpscr);
1195 /* Fetch register REGNO if != -1 or all registers otherwise from
1198 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1199 SPRs, but there's no way to query individual registers within those
1200 groups. Therefore, if REGNO != -1, this function fetches an entire
1203 Unfortunately, kernel thread register queries often fail with
1204 EPERM, indicating that the thread is in kernel space. This breaks
1205 backtraces of threads other than the current one. To make that
1206 breakage obvious without throwing an error to top level (which is
1207 bad e.g. during "info threads" output), zero registers that can't
1211 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1214 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1215 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1216 uint64_t gprs64[ppc_num_gprs];
1217 uint32_t gprs32[ppc_num_gprs];
1218 double fprs[ppc_num_fprs];
1219 struct ptxsprs sprs64;
1220 struct ptsprs sprs32;
1223 if (debug_aix_thread)
1224 fprintf_unfiltered (gdb_stdlog,
1225 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1226 (long) tid, regno, arch64);
1228 /* General-purpose registers. */
1230 || (tdep->ppc_gp0_regnum <= regno
1231 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1235 if (!ptrace64aix (PTT_READ_GPRS, tid,
1236 (unsigned long) gprs64, 0, NULL))
1237 memset (gprs64, 0, sizeof (gprs64));
1238 supply_gprs64 (regcache, gprs64);
1242 if (!ptrace32 (PTT_READ_GPRS, tid, gprs32, 0, NULL))
1243 memset (gprs32, 0, sizeof (gprs32));
1244 for (i = 0; i < ppc_num_gprs; i++)
1245 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1249 /* Floating-point registers. */
1251 if (ppc_floating_point_unit_p (gdbarch)
1253 || (regno >= tdep->ppc_fp0_regnum
1254 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1256 if (!ptrace32 (PTT_READ_FPRS, tid, (void *) fprs, 0, NULL))
1257 memset (fprs, 0, sizeof (fprs));
1258 supply_fprs (regcache, fprs);
1261 /* Special-purpose registers. */
1263 if (regno == -1 || special_register_p (gdbarch, regno))
1267 if (!ptrace64aix (PTT_READ_SPRS, tid,
1268 (unsigned long) &sprs64, 0, NULL))
1269 memset (&sprs64, 0, sizeof (sprs64));
1270 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1271 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1272 sprs64.pt_xer, sprs64.pt_fpscr);
1276 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1278 if (!ptrace32 (PTT_READ_SPRS, tid, (int *) &sprs32, 0, NULL))
1279 memset (&sprs32, 0, sizeof (sprs32));
1280 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1281 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1284 if (tdep->ppc_mq_regnum >= 0)
1285 regcache_raw_supply (regcache, tdep->ppc_mq_regnum,
1286 (char *) &sprs32.pt_mq);
1291 /* Fetch register REGNO if != -1 or all registers otherwise in the
1292 thread/process specified by inferior_ptid. */
1295 aix_thread_fetch_registers (struct target_ops *ops,
1296 struct regcache *regcache, int regno)
1298 struct thread_info *thread;
1300 struct target_ops *beneath = find_target_beneath (ops);
1302 if (!PD_TID (inferior_ptid))
1303 beneath->to_fetch_registers (beneath, regcache, regno);
1306 thread = find_thread_ptid (inferior_ptid);
1307 tid = thread->private->tid;
1309 if (tid == PTHDB_INVALID_TID)
1310 fetch_regs_user_thread (regcache, thread->private->pdtid);
1312 fetch_regs_kernel_thread (regcache, regno, tid);
1316 /* Store the gp registers into an array of uint32_t or uint64_t. */
1319 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1321 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1324 for (regno = 0; regno < ppc_num_gprs; regno++)
1325 if (REG_VALID == regcache_register_status (regcache,
1326 tdep->ppc_gp0_regnum + regno))
1327 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1332 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1334 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1337 for (regno = 0; regno < ppc_num_gprs; regno++)
1338 if (REG_VALID == regcache_register_status (regcache,
1339 tdep->ppc_gp0_regnum + regno))
1340 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1344 /* Store the floating point registers into a double array. */
1346 fill_fprs (const struct regcache *regcache, double *vals)
1348 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1349 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1352 /* This function should never be called on architectures without
1353 floating-point registers. */
1354 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1356 for (regno = tdep->ppc_fp0_regnum;
1357 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1359 if (REG_VALID == regcache_register_status (regcache, regno))
1360 regcache_raw_collect (regcache, regno,
1361 vals + regno - tdep->ppc_fp0_regnum);
1364 /* Store the special registers into the specified 64-bit and 32-bit
1368 fill_sprs64 (const struct regcache *regcache,
1369 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1370 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1373 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1374 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1376 /* Verify that the size of the size of the IAR buffer is the
1377 same as the raw size of the PC (in the register cache). If
1378 they're not, then either GDB has been built incorrectly, or
1379 there's some other kind of internal error. To be really safe,
1380 we should check all of the sizes. */
1381 gdb_assert (sizeof (*iar) == register_size
1382 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1384 if (REG_VALID == regcache_register_status (regcache,
1385 gdbarch_pc_regnum (gdbarch)))
1386 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1387 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1388 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1389 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1390 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1391 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1392 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1393 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1394 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1395 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1396 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1397 if (tdep->ppc_fpscr_regnum >= 0
1398 && REG_VALID == regcache_register_status (regcache,
1399 tdep->ppc_fpscr_regnum))
1400 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1404 fill_sprs32 (const struct regcache *regcache,
1405 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1406 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1409 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1410 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1412 /* Verify that the size of the size of the IAR buffer is the
1413 same as the raw size of the PC (in the register cache). If
1414 they're not, then either GDB has been built incorrectly, or
1415 there's some other kind of internal error. To be really safe,
1416 we should check all of the sizes. */
1417 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1418 gdbarch_pc_regnum (gdbarch)));
1420 if (REG_VALID == regcache_register_status (regcache,
1421 gdbarch_pc_regnum (gdbarch)))
1422 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1423 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1424 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1425 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1426 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1427 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1428 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1429 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1430 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1431 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1432 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1433 if (tdep->ppc_fpscr_regnum >= 0
1434 && REG_VALID == regcache_register_status (regcache, tdep->ppc_fpscr_regnum))
1435 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1438 /* Store all registers into pthread PDTID, which doesn't have a kernel
1441 It's possible to store a single register into a non-kernel pthread,
1442 but I doubt it's worth the effort. */
1445 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1447 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1448 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1450 pthdb_context_t ctx;
1455 if (debug_aix_thread)
1456 fprintf_unfiltered (gdb_stdlog,
1457 "store_regs_user_thread %lx\n", (long) pdtid);
1459 /* Retrieve the thread's current context for its non-register
1461 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1462 if (status != PTHDB_SUCCESS)
1463 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1464 pd_status2str (status));
1466 /* Collect general-purpose register values from the regcache. */
1468 for (i = 0; i < ppc_num_gprs; i++)
1469 if (REG_VALID == regcache_register_status (regcache,
1470 tdep->ppc_gp0_regnum + i))
1474 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1480 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1486 /* Collect floating-point register values from the regcache. */
1487 if (ppc_floating_point_unit_p (gdbarch))
1488 fill_fprs (regcache, ctx.fpr);
1490 /* Special registers (always kept in ctx as 64 bits). */
1493 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1494 &ctx.xer, &ctx.fpscr);
1498 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1499 Solution: use 32-bit temp variables. */
1500 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1503 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1504 &tmp_xer, &tmp_fpscr);
1505 if (REG_VALID == regcache_register_status (regcache,
1506 gdbarch_pc_regnum (gdbarch)))
1508 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1510 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1512 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1514 if (REG_VALID == regcache_register_status (regcache,
1515 tdep->ppc_ctr_regnum))
1517 if (REG_VALID == regcache_register_status (regcache,
1518 tdep->ppc_xer_regnum))
1520 if (REG_VALID == regcache_register_status (regcache,
1521 tdep->ppc_xer_regnum))
1522 ctx.fpscr = tmp_fpscr;
1525 status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1526 if (status != PTHDB_SUCCESS)
1527 error (_("aix-thread: store_registers: "
1528 "pthdb_pthread_setcontext returned %s"),
1529 pd_status2str (status));
1532 /* Store register REGNO if != -1 or all registers otherwise into
1535 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1536 SPRs, but there's no way to set individual registers within those
1537 groups. Therefore, if REGNO != -1, this function stores an entire
1541 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1544 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1545 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1546 uint64_t gprs64[ppc_num_gprs];
1547 uint32_t gprs32[ppc_num_gprs];
1548 double fprs[ppc_num_fprs];
1549 struct ptxsprs sprs64;
1550 struct ptsprs sprs32;
1553 if (debug_aix_thread)
1554 fprintf_unfiltered (gdb_stdlog,
1555 "store_regs_kernel_thread tid=%lx regno=%d\n",
1558 /* General-purpose registers. */
1560 || (tdep->ppc_gp0_regnum <= regno
1561 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1565 /* Pre-fetch: some regs may not be in the cache. */
1566 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1567 fill_gprs64 (regcache, gprs64);
1568 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1572 /* Pre-fetch: some regs may not be in the cache. */
1573 ptrace32 (PTT_READ_GPRS, tid, gprs32, 0, NULL);
1574 fill_gprs32 (regcache, gprs32);
1575 ptrace32 (PTT_WRITE_GPRS, tid, gprs32, 0, NULL);
1579 /* Floating-point registers. */
1581 if (ppc_floating_point_unit_p (gdbarch)
1583 || (regno >= tdep->ppc_fp0_regnum
1584 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1586 /* Pre-fetch: some regs may not be in the cache. */
1587 ptrace32 (PTT_READ_FPRS, tid, (void *) fprs, 0, NULL);
1588 fill_fprs (regcache, fprs);
1589 ptrace32 (PTT_WRITE_FPRS, tid, (void *) fprs, 0, NULL);
1592 /* Special-purpose registers. */
1594 if (regno == -1 || special_register_p (gdbarch, regno))
1598 /* Pre-fetch: some registers won't be in the cache. */
1599 ptrace64aix (PTT_READ_SPRS, tid,
1600 (unsigned long) &sprs64, 0, NULL);
1601 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1602 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1603 &sprs64.pt_xer, &sprs64.pt_fpscr);
1604 ptrace64aix (PTT_WRITE_SPRS, tid,
1605 (unsigned long) &sprs64, 0, NULL);
1609 /* The contents of "struct ptspr" were declared as "unsigned
1610 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1611 Use temporaries to work around this problem. Also, add an
1612 assert here to make sure we fail if the system header files
1613 use "unsigned long", and the size of that type is not what
1614 the headers expect. */
1615 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1618 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1620 /* Pre-fetch: some registers won't be in the cache. */
1621 ptrace32 (PTT_READ_SPRS, tid, (int *) &sprs32, 0, NULL);
1623 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1624 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1626 sprs32.pt_iar = tmp_iar;
1627 sprs32.pt_msr = tmp_msr;
1628 sprs32.pt_cr = tmp_cr;
1629 sprs32.pt_lr = tmp_lr;
1630 sprs32.pt_ctr = tmp_ctr;
1631 sprs32.pt_xer = tmp_xer;
1632 sprs32.pt_fpscr = tmp_fpscr;
1634 if (tdep->ppc_mq_regnum >= 0)
1635 if (REG_VALID == regcache_register_status (regcache,
1636 tdep->ppc_mq_regnum))
1637 regcache_raw_collect (regcache, tdep->ppc_mq_regnum,
1640 ptrace32 (PTT_WRITE_SPRS, tid, (int *) &sprs32, 0, NULL);
1645 /* Store gdb's current view of the register set into the
1646 thread/process specified by inferior_ptid. */
1649 aix_thread_store_registers (struct target_ops *ops,
1650 struct regcache *regcache, int regno)
1652 struct thread_info *thread;
1654 struct target_ops *beneath = find_target_beneath (ops);
1656 if (!PD_TID (inferior_ptid))
1657 beneath->to_store_registers (beneath, regcache, regno);
1660 thread = find_thread_ptid (inferior_ptid);
1661 tid = thread->private->tid;
1663 if (tid == PTHDB_INVALID_TID)
1664 store_regs_user_thread (regcache, thread->private->pdtid);
1666 store_regs_kernel_thread (regcache, regno, tid);
1670 /* Attempt a transfer all LEN bytes starting at OFFSET between the
1671 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
1672 Return the number of bytes actually transferred. */
1675 aix_thread_xfer_partial (struct target_ops *ops, enum target_object object,
1676 const char *annex, gdb_byte *readbuf,
1677 const gdb_byte *writebuf,
1678 ULONGEST offset, LONGEST len)
1680 struct cleanup *old_chain = save_inferior_ptid ();
1682 struct target_ops *beneath = find_target_beneath (ops);
1684 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1685 xfer = beneath->to_xfer_partial (beneath, object, annex,
1686 readbuf, writebuf, offset, len);
1688 do_cleanups (old_chain);
1692 /* Clean up after the inferior exits. */
1695 aix_thread_mourn_inferior (struct target_ops *ops)
1697 struct target_ops *beneath = find_target_beneath (ops);
1700 beneath->to_mourn_inferior (beneath);
1703 /* Return whether thread PID is still valid. */
1706 aix_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
1708 struct target_ops *beneath = find_target_beneath (ops);
1711 return beneath->to_thread_alive (beneath, ptid);
1713 /* We update the thread list every time the child stops, so all
1714 valid threads should be in the thread list. */
1715 return in_thread_list (ptid);
1718 /* Return a printable representation of composite PID for use in
1719 "info threads" output. */
1722 aix_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
1724 static char *ret = NULL;
1725 struct target_ops *beneath = find_target_beneath (ops);
1728 return beneath->to_pid_to_str (beneath, ptid);
1730 /* Free previous return value; a new one will be allocated by
1734 ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
1738 /* Return a printable representation of extra information about
1739 THREAD, for use in "info threads" output. */
1742 aix_thread_extra_thread_info (struct thread_info *thread)
1744 struct ui_file *buf;
1746 pthdb_pthread_t pdtid;
1748 pthdb_state_t state;
1749 pthdb_suspendstate_t suspendstate;
1750 pthdb_detachstate_t detachstate;
1752 static char *ret = NULL;
1754 if (!PD_TID (thread->ptid))
1757 buf = mem_fileopen ();
1759 pdtid = thread->private->pdtid;
1760 tid = thread->private->tid;
1762 if (tid != PTHDB_INVALID_TID)
1763 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1764 fprintf_unfiltered (buf, _("tid %d"), (int)tid);
1766 status = pthdb_pthread_state (pd_session, pdtid, &state);
1767 if (status != PTHDB_SUCCESS)
1769 fprintf_unfiltered (buf, ", %s", state2str (state));
1771 status = pthdb_pthread_suspendstate (pd_session, pdtid,
1773 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
1774 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1775 fprintf_unfiltered (buf, _(", suspended"));
1777 status = pthdb_pthread_detachstate (pd_session, pdtid,
1779 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
1780 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1781 fprintf_unfiltered (buf, _(", detached"));
1783 pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1784 if (status == PTHDB_SUCCESS && cancelpend)
1785 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1786 fprintf_unfiltered (buf, _(", cancel pending"));
1788 ui_file_write (buf, "", 1);
1790 xfree (ret); /* Free old buffer. */
1792 ret = ui_file_xstrdup (buf, NULL);
1793 ui_file_delete (buf);
1799 aix_thread_get_ada_task_ptid (long lwp, long thread)
1801 return ptid_build (ptid_get_pid (inferior_ptid), 0, thread);
1804 /* Initialize target aix_thread_ops. */
1807 init_aix_thread_ops (void)
1809 aix_thread_ops.to_shortname = "aix-threads";
1810 aix_thread_ops.to_longname = _("AIX pthread support");
1811 aix_thread_ops.to_doc = _("AIX pthread support");
1813 aix_thread_ops.to_attach = aix_thread_attach;
1814 aix_thread_ops.to_detach = aix_thread_detach;
1815 aix_thread_ops.to_resume = aix_thread_resume;
1816 aix_thread_ops.to_wait = aix_thread_wait;
1817 aix_thread_ops.to_fetch_registers = aix_thread_fetch_registers;
1818 aix_thread_ops.to_store_registers = aix_thread_store_registers;
1819 aix_thread_ops.to_xfer_partial = aix_thread_xfer_partial;
1820 /* No need for aix_thread_ops.to_create_inferior, because we activate thread
1821 debugging when the inferior reaches pd_brk_addr. */
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
1834 void _initialize_aix_thread (void);
1837 _initialize_aix_thread (void)
1839 init_aix_thread_ops ();
1840 add_target (&aix_thread_ops);
1842 /* Notice when object files get loaded and unloaded. */
1843 observer_attach_new_objfile (new_objfile);
1845 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
1846 _("Set debugging of AIX thread module."),
1847 _("Show debugging of AIX thread module."),
1848 _("Enables debugging output (used to debug GDB)."),
1850 /* FIXME: i18n: Debugging of AIX thread
1851 module is \"%d\". */
1852 &setdebuglist, &showdebuglist);