1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright 1999, 2000, 2002 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
25 debugging pthread applications.
27 Some name prefix conventions:
28 pthdb_ provided by libpthdebug.a
29 pdc_ callbacks that this module provides to libpthdebug.a
30 pd_ variables or functions interfacing with libpthdebug.a
32 libpthdebug peculiarities:
34 - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but it's not
35 documented, and after several calls it stops working and causes other
36 libpthdebug functions to fail.
38 - pthdb_tid_pthread() doesn't always work after pthdb_session_update(),
39 but it does work after cycling through all threads using
45 #include "gdbthread.h"
52 #include "coff/internal.h" /* for libcoff.h */
53 #include "bfd/libcoff.h" /* for xcoff_data */
57 #include <sys/types.h>
58 #include <sys/ptrace.h>
64 #include <sys/pthdebug.h>
66 /* Whether to emit debugging output. */
67 static int debug_aix_thread
;
69 /* in AIX 5.1, functions use pthdb_tid_t instead of tid_t */
70 #ifndef PTHDB_VERSION_3
71 #define pthdb_tid_t tid_t
74 /* Return whether to treat PID as a debuggable thread id. */
76 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
78 /* Build a thread ptid. */
79 #define BUILD_THREAD(TID, PID) ptid_build (PID, 0, TID)
81 /* Build and lwp ptid. */
82 #define BUILD_LWP(LWP, PID) MERGEPID (PID, LWP)
84 /* Call error() with a message indicating that libpthdebug FUNC failed with
87 #define PD_ERROR(func, status) \
88 error ("aix-thread: %s returned %s", func, pd_status2str (status))
90 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
91 PTHDB_BAD_USER errors, so use 1. */
95 /* Success and failure values returned by pthdb callbacks. */
97 #define PDC_SUCCESS PTHDB_SUCCESS
98 #define PDC_FAILURE PTHDB_CALLBACK
100 /* Convert composite process/thread inferior_ptid to a process id, evaluate
101 base_ops function CALL, and then restore inferior_ptid. */
103 #define CALL_BASE(call) \
105 struct cleanup *cleanup = save_inferior_ptid (); \
106 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid)); \
108 do_cleanups (cleanup); \
111 /* Private data attached to each element in GDB's thread list. */
113 struct private_thread_info
{
114 pthdb_pthread_t pdtid
; /* thread's libpthdebug id */
115 pthdb_tid_t tid
; /* kernel thread id */
118 /* Information about a thread of which libpthdebug is aware. */
121 pthdb_pthread_t pdtid
;
126 /* This module's target-specific operations, active while pd_able is true. */
128 static struct target_ops ops
;
130 /* Copy of the target over which ops is pushed. This is
131 more convenient than a pointer to child_ops or core_ops, because
132 they lack current_target's default callbacks. */
134 static struct target_ops base_ops
;
136 /* Address of the function that libpthread will call when libpthdebug is
137 ready to be initialized. */
139 static CORE_ADDR pd_brk_addr
;
141 /* Whether the current application is debuggable by pthdb. */
143 static int pd_able
= 0;
145 /* Whether a threaded application is being debugged. */
147 static int pd_active
= 0;
149 /* Whether the current architecture is 64-bit. Only valid when pd_able is
154 /* Saved pointer to previous owner of target_new_objfile_hook. */
156 static void (*target_new_objfile_chain
)(struct objfile
*);
158 /* Forward declarations for pthdb callbacks. */
160 static int pdc_symbol_addrs (pthdb_user_t
, pthdb_symbol_t
*, int);
161 static int pdc_read_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
162 static int pdc_write_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
163 static int pdc_read_regs (pthdb_user_t user
, pthdb_tid_t tid
,
164 unsigned long long flags
, pthdb_context_t
*context
);
165 static int pdc_write_regs (pthdb_user_t user
, pthdb_tid_t tid
,
166 unsigned long long flags
, pthdb_context_t
*context
);
167 static int pdc_alloc (pthdb_user_t
, size_t, void **);
168 static int pdc_realloc (pthdb_user_t
, void *, size_t, void **);
169 static int pdc_dealloc (pthdb_user_t
, void *);
171 /* pthdb callbacks. */
173 static pthdb_callbacks_t pd_callbacks
= {
185 /* Current pthdb session. */
187 static pthdb_session_t pd_session
;
189 /* Return a printable representation of pthdebug function return STATUS. */
192 pd_status2str (int status
)
196 case PTHDB_SUCCESS
: return "SUCCESS";
197 case PTHDB_NOSYS
: return "NOSYS";
198 case PTHDB_NOTSUP
: return "NOTSUP";
199 case PTHDB_BAD_VERSION
: return "BAD_VERSION";
200 case PTHDB_BAD_USER
: return "BAD_USER";
201 case PTHDB_BAD_SESSION
: return "BAD_SESSION";
202 case PTHDB_BAD_MODE
: return "BAD_MODE";
203 case PTHDB_BAD_FLAGS
: return "BAD_FLAGS";
204 case PTHDB_BAD_CALLBACK
: return "BAD_CALLBACK";
205 case PTHDB_BAD_POINTER
: return "BAD_POINTER";
206 case PTHDB_BAD_CMD
: return "BAD_CMD";
207 case PTHDB_BAD_PTHREAD
: return "BAD_PTHREAD";
208 case PTHDB_BAD_ATTR
: return "BAD_ATTR";
209 case PTHDB_BAD_MUTEX
: return "BAD_MUTEX";
210 case PTHDB_BAD_MUTEXATTR
: return "BAD_MUTEXATTR";
211 case PTHDB_BAD_COND
: return "BAD_COND";
212 case PTHDB_BAD_CONDATTR
: return "BAD_CONDATTR";
213 case PTHDB_BAD_RWLOCK
: return "BAD_RWLOCK";
214 case PTHDB_BAD_RWLOCKATTR
: return "BAD_RWLOCKATTR";
215 case PTHDB_BAD_KEY
: return "BAD_KEY";
216 case PTHDB_BAD_PTID
: return "BAD_PTID";
217 case PTHDB_BAD_TID
: return "BAD_TID";
218 case PTHDB_CALLBACK
: return "CALLBACK";
219 case PTHDB_CONTEXT
: return "CONTEXT";
220 case PTHDB_HELD
: return "HELD";
221 case PTHDB_NOT_HELD
: return "NOT_HELD";
222 case PTHDB_MEMORY
: return "MEMORY";
223 case PTHDB_NOT_PTHREADED
: return "NOT_PTHREADED";
224 case PTHDB_SYMBOL
: return "SYMBOL";
225 case PTHDB_NOT_AVAIL
: return "NOT_AVAIL";
226 case PTHDB_INTERNAL
: return "INTERNAL";
227 default: return "UNKNOWN";
231 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for exceptional
232 conditions and either return nonlocally or else return 1 for success and 0
236 ptrace_check (int req
, int id
, int ret
)
238 if (ret
== 0 && !errno
)
241 /* According to ptrace(2), ptrace may fail with EPERM if "the Identifier
242 parameter corresponds to a kernel thread which is stopped in kernel mode
243 and whose computational state cannot be read or written." This happens
244 quite often with register reads. */
251 if (ret
== -1 && errno
== EPERM
)
255 error ("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)",
256 req
, id
, ret
, errno
, strerror (errno
));
259 if (debug_aix_thread
)
260 fprintf_unfiltered (gdb_stdlog
, "ptrace (%d, %d) = %d (errno = %d)",
261 req
, id
, ret
, errno
);
262 return ret
== -1 ? 0 : 1;
265 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). Return success. */
268 ptrace64aix (int req
, int id
, long long addr
, int data
, int *buf
)
271 return ptrace_check (req
, id
, ptracex (req
, id
, addr
, data
, buf
));
274 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). Return success. */
277 ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
280 return ptrace_check (req
, id
, ptrace (req
, id
, (int *)addr
, data
, buf
));
283 /* If *PIDP is a composite process/thread id, convert it to a process id. */
286 pid_to_prc (ptid_t
*ptidp
)
292 *ptidp
= pid_to_ptid (PIDGET (ptid
));
295 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to the
296 address of SYMBOLS[<i>].name. */
299 pdc_symbol_addrs (pthdb_user_t user
, pthdb_symbol_t
*symbols
, int count
)
301 struct minimal_symbol
*ms
;
305 if (debug_aix_thread
)
306 fprintf_unfiltered (gdb_stdlog
,
307 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)",
308 user
, (long) symbols
, count
);
310 for (i
= 0; i
< count
; i
++)
312 name
= symbols
[i
].name
;
313 if (debug_aix_thread
)
314 fprintf_unfiltered (gdb_stdlog
, " symbols[%d].name = \"%s\"", i
, name
);
320 if (!(ms
= lookup_minimal_symbol (name
, NULL
, NULL
)))
322 if (debug_aix_thread
)
323 fprintf_unfiltered (gdb_stdlog
, " returning PDC_FAILURE");
326 symbols
[i
].addr
= SYMBOL_VALUE_ADDRESS (ms
);
328 if (debug_aix_thread
)
329 fprintf_unfiltered (gdb_stdlog
, " symbols[%d].addr = 0x%llx",
332 if (debug_aix_thread
)
333 fprintf_unfiltered (gdb_stdlog
, " returning PDC_SUCCESS");
337 /* Read registers call back function should be able to read the context */
338 /* information of a debuggee kernel thread from an active process or from */
339 /* a core file. The information should be formatted in context64 form for */
340 /* both 32-bit and 64-bit process. If successful return 0, else non-zero */
343 pdc_read_regs (pthdb_user_t user
,
345 unsigned long long flags
,
346 pthdb_context_t
*context
)
348 /* this function doesn't appear to be used, so we could probably just */
349 /* return 0 here. HOWEVER, if it is not defined, the OS will complain */
350 /* and several thread debug functions will fail. In case this is needed, */
351 /* I have implemented what I think it should do, however this code is */
356 struct ptxsprs sprs64
;
357 struct ptsprs sprs32
;
359 if (debug_aix_thread
)
360 fprintf_unfiltered (gdb_stdlog
, "pdc_read_regs tid=%d flags=%llx\n",
363 /* General-purpose registers. */
364 if (flags
& PTHDB_FLAG_GPRS
)
368 if (!ptrace64aix (PTT_READ_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
))
369 memset (gprs64
, 0, sizeof (gprs64
));
370 memcpy (context
->gpr
, gprs64
, sizeof(gprs64
));
374 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
375 memset (gprs32
, 0, sizeof (gprs32
));
376 memcpy (context
->gpr
, gprs32
, sizeof(gprs32
));
380 /* Floating-point registers. */
381 if (flags
& PTHDB_FLAG_FPRS
)
383 if (!ptrace32 (PTT_READ_FPRS
, tid
, (int *) fprs
, 0, NULL
))
384 memset (fprs
, 0, sizeof (fprs
));
385 memcpy (context
->fpr
, fprs
, sizeof(fprs
));
388 /* Special-purpose registers. */
389 if (flags
& PTHDB_FLAG_SPRS
)
393 if (!ptrace64aix (PTT_READ_SPRS
, tid
, (unsigned long) &sprs64
, 0, NULL
))
394 memset (&sprs64
, 0, sizeof (sprs64
));
395 memcpy (&context
->msr
, &sprs64
, sizeof(sprs64
));
399 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
400 memset (&sprs32
, 0, sizeof (sprs32
));
401 memcpy (&context
->msr
, &sprs32
, sizeof(sprs32
));
407 /* Write register function should be able to write requested context */
408 /* information to specified debuggee's kernel thread id. If successful */
409 /* return 0, else non-zero is returned. */
411 pdc_write_regs (pthdb_user_t user
,
413 unsigned long long flags
,
414 pthdb_context_t
*context
)
416 /* this function doesn't appear to be used, so we could probably just */
417 /* return 0 here. HOWEVER, if it is not defined, the OS will complain */
418 /* and several thread debug functions will fail. In case this is needed, */
419 /* I have implemented what I think it should do, however this code is */
422 if (debug_aix_thread
)
423 fprintf_unfiltered (gdb_stdlog
, "pdc_write_regs tid=%d flags=%llx\n",
426 /* General-purpose registers. */
427 if (flags
& PTHDB_FLAG_GPRS
)
430 ptrace64aix (PTT_WRITE_GPRS
, tid
, (unsigned long)context
->gpr
, 0, NULL
);
432 ptrace32 (PTT_WRITE_GPRS
, tid
, (int *)context
->gpr
, 0, NULL
);
435 /* Floating-point registers. */
436 if (flags
& PTHDB_FLAG_FPRS
)
438 ptrace32 (PTT_WRITE_FPRS
, tid
, (int *)context
->fpr
, 0, NULL
);
441 /* Special-purpose registers. */
442 if (flags
& PTHDB_FLAG_SPRS
)
446 ptrace64aix (PTT_WRITE_SPRS
, tid
, (unsigned long) &context
->msr
, 0, NULL
);
450 ptrace32 (PTT_WRITE_SPRS
, tid
, (int *)&context
->msr
, 0, NULL
);
456 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
459 pdc_read_data (pthdb_user_t user
, void *buf
, pthdb_addr_t addr
, size_t len
)
463 if (debug_aix_thread
)
464 fprintf_unfiltered (gdb_stdlog
,
465 "pdc_read_data (user = %ld, buf = 0x%lx, addr = 0x%llx, len = %ld)",
466 user
, (long) buf
, addr
, len
);
468 status
= target_read_memory (addr
, buf
, len
);
469 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
471 if (debug_aix_thread
)
472 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s", status
,
473 pd_status2str (ret
));
477 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
480 pdc_write_data (pthdb_user_t user
, void *buf
, pthdb_addr_t addr
, size_t len
)
484 if (debug_aix_thread
)
485 fprintf_unfiltered (gdb_stdlog
,
486 "pdc_write_data (user = %ld, buf = 0x%lx, addr = 0x%llx, len = %ld)",
487 user
, (long) buf
, addr
, len
);
489 status
= target_write_memory (addr
, buf
, len
);
490 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
492 if (debug_aix_thread
)
493 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s", status
,
494 pd_status2str (ret
));
498 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it in
502 pdc_alloc (pthdb_user_t user
, size_t len
, void **bufp
)
504 if (debug_aix_thread
)
505 fprintf_unfiltered (gdb_stdlog
,
506 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)",
507 user
, len
, (long) bufp
);
508 *bufp
= xmalloc (len
);
509 if (debug_aix_thread
)
510 fprintf_unfiltered (gdb_stdlog
, " malloc returned 0x%lx", (long) *bufp
);
511 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never be
513 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
516 /* pthdb callback: reallocate BUF, which was allocated by the alloc or realloc
517 callback, so that it contains LEN bytes, and store a pointer to the result
521 pdc_realloc (pthdb_user_t user
, void *buf
, size_t len
, void **bufp
)
523 if (debug_aix_thread
)
524 fprintf_unfiltered (gdb_stdlog
,
525 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)",
526 user
, (long) buf
, len
, (long) bufp
);
527 *bufp
= realloc (buf
, len
);
528 if (debug_aix_thread
)
529 fprintf_unfiltered (gdb_stdlog
, " realloc returned 0x%lx", (long) *bufp
);
530 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
533 /* pthdb callback: free BUF, which was allocated by the alloc or realloc
537 pdc_dealloc (pthdb_user_t user
, void *buf
)
539 if (debug_aix_thread
)
540 fprintf_unfiltered (gdb_stdlog
, "pdc_free (user = %ld, buf = 0x%lx)", user
,
546 /* Return a printable representation of pthread STATE. */
549 state2str (pthdb_state_t state
)
553 case PST_IDLE
: return "idle"; /* being created */
554 case PST_RUN
: return "running"; /* running */
555 case PST_SLEEP
: return "sleeping"; /* awaiting an event */
556 case PST_READY
: return "ready"; /* runnable */
557 case PST_TERM
: return "finished"; /* awaiting a join/detach */
558 default: return "unknown";
562 /* qsort() comparison function for sorting pd_thread structs by pthid. */
565 pcmp (const void *p1v
, const void *p2v
)
567 struct pd_thread
*p1
= (struct pd_thread
*) p1v
;
568 struct pd_thread
*p2
= (struct pd_thread
*) p2v
;
569 return p1
->pthid
< p2
->pthid
? -1 : p1
->pthid
> p2
->pthid
;
572 /* iterate_over_threads() callback for counting GDB threads. */
575 giter_count (struct thread_info
*thread
, void *countp
)
581 /* iterate_over_threads() callback for accumulating GDB thread pids. */
584 giter_accum (struct thread_info
*thread
, void *bufp
)
586 **(struct thread_info
***) bufp
= thread
;
587 (*(struct thread_info
***) bufp
)++;
591 /* ptid comparison function */
593 ptid_cmp (ptid_t ptid1
, ptid_t ptid2
)
597 if (ptid_get_pid (ptid1
) < ptid_get_pid (ptid2
))
599 else if (ptid_get_pid (ptid1
) > ptid_get_pid (ptid2
))
601 else if (ptid_get_tid (ptid1
) < ptid_get_tid (ptid2
))
603 else if (ptid_get_tid (ptid1
) > ptid_get_tid (ptid2
))
605 else if (ptid_get_lwp (ptid1
) < ptid_get_lwp (ptid2
))
607 else if (ptid_get_lwp (ptid1
) > ptid_get_lwp (ptid2
))
613 /* qsort() comparison function for sorting thread_info structs by pid. */
616 gcmp (const void *t1v
, const void *t2v
)
618 struct thread_info
*t1
= *(struct thread_info
**) t1v
;
619 struct thread_info
*t2
= *(struct thread_info
**) t2v
;
620 return ptid_cmp (t1
->ptid
, t2
->ptid
);
623 /* Synchronize GDB's thread list with libpthdebug's.
625 There are some benefits of doing this every time the inferior stops:
627 - allows users to run thread-specific commands without needing to run
630 - helps pthdb_tid_pthread() work properly (see "libpthdebug
631 peculiarities" at the top of this module)
633 - simplifies the demands placed on libpthdebug, which seems to have
634 difficulty with certain call patterns */
637 sync_threadlists (void)
639 int cmd
, status
, infpid
;
640 int pcount
, psize
, pi
, gcount
, gi
;
641 struct pd_thread
*pbuf
;
642 struct thread_info
**gbuf
, **g
, *thread
;
643 pthdb_pthread_t pdtid
;
648 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
652 pbuf
= (struct pd_thread
*) xmalloc (psize
* sizeof *pbuf
);
654 for (cmd
= PTHDB_LIST_FIRST
;; cmd
= PTHDB_LIST_NEXT
)
656 status
= pthdb_pthread (pd_session
, &pdtid
, cmd
);
657 if (status
!= PTHDB_SUCCESS
|| pdtid
== PTHDB_INVALID_PTHREAD
)
660 status
= pthdb_pthread_ptid (pd_session
, pdtid
, &pthid
);
661 if (status
!= PTHDB_SUCCESS
|| pthid
== PTHDB_INVALID_PTID
)
667 pbuf
= (struct pd_thread
*) xrealloc (pbuf
, psize
* sizeof *pbuf
);
669 pbuf
[pcount
].pdtid
= pdtid
;
670 pbuf
[pcount
].pthid
= pthid
;
674 for (pi
= 0; pi
< pcount
; pi
++)
676 status
= pthdb_pthread_tid (pd_session
, pbuf
[pi
].pdtid
, &tid
);
677 if (status
!= PTHDB_SUCCESS
)
678 tid
= PTHDB_INVALID_TID
;
682 qsort (pbuf
, pcount
, sizeof *pbuf
, pcmp
);
684 /* Accumulate an array of GDB threads sorted by pid. */
687 iterate_over_threads (giter_count
, &gcount
);
688 g
= gbuf
= (struct thread_info
**) xmalloc (gcount
* sizeof *gbuf
);
689 iterate_over_threads (giter_accum
, &g
);
690 qsort (gbuf
, gcount
, sizeof *gbuf
, gcmp
);
692 /* Apply differences between the two arrays to GDB's thread list. */
694 infpid
= PIDGET (inferior_ptid
);
695 for (pi
= gi
= 0; pi
< pcount
|| gi
< gcount
;)
697 pptid
= BUILD_THREAD (pbuf
[pi
].pthid
, infpid
);
698 gptid
= gbuf
[gi
]->ptid
;
699 pdtid
= pbuf
[pi
].pdtid
;
707 if (ptid_equal (pptid
, gptid
))
709 gbuf
[gi
]->private->pdtid
= pdtid
;
710 gbuf
[gi
]->private->tid
= tid
;
714 else if (ptid_cmp (pptid
, gptid
) > 0)
717 delete_thread (gptid
);
723 thread
= add_thread (pptid
);
724 thread
->private = xmalloc (sizeof (struct private_thread_info
));
725 thread
->private->pdtid
= pdtid
;
726 thread
->private->tid
= tid
;
736 /* iterate_over_threads() callback for locating a thread whose kernel thread
737 just received a trap signal. */
740 iter_trap (struct thread_info
*thread
, void *unused
)
742 struct thrdsinfo64 thrinf
;
745 /* getthrds(3) isn't prototyped in any AIX 4.3.3 #include file. */
746 extern int getthrds (pid_t
, struct thrdsinfo64
*, int, pthdb_tid_t
*, int);
748 tid
= thread
->private->tid
;
749 if (tid
== PTHDB_INVALID_TID
)
752 if (getthrds (PIDGET (inferior_ptid
), &thrinf
, sizeof (thrinf
), &tid
, 1) != 1)
755 return thrinf
.ti_cursig
== SIGTRAP
;
758 /* Synchronize libpthdebug's state with the inferior and with GDB, generate a
759 composite process/thread <pid> for the current thread, set inferior_ptid to
760 <pid> if SET_INFPID, and return <pid>. */
763 pd_update (int set_infpid
)
767 struct thread_info
*thread
;
770 return inferior_ptid
;
772 status
= pthdb_session_update (pd_session
);
773 if (status
!= PTHDB_SUCCESS
)
774 return inferior_ptid
;
778 /* Define "current thread" as one that just received a trap signal. */
780 thread
= iterate_over_threads (iter_trap
, NULL
);
782 ptid
= inferior_ptid
;
787 inferior_ptid
= ptid
;
792 /* Try to start debugging threads in the current process. If successful and
793 SET_INFPID, set inferior_ptid to reflect the current thread. */
796 pd_activate (int set_infpid
)
800 status
= pthdb_session_init (PD_USER
, arch64
? PEM_64BIT
: PEM_32BIT
,
801 PTHDB_FLAG_REGS
, &pd_callbacks
, &pd_session
);
802 if (status
!= PTHDB_SUCCESS
)
804 return inferior_ptid
;
807 return pd_update (set_infpid
);
810 /* Undo the effects of pd_activate(). */
817 pthdb_session_destroy (pd_session
);
819 pid_to_prc (&inferior_ptid
);
823 /* An object file has just been loaded. Check whether the current application
824 is pthreaded, and if so, prepare for thread debugging. */
831 struct minimal_symbol
*ms
;
833 /* Don't initialize twice. */
837 /* Check application word size. */
838 arch64
= REGISTER_RAW_SIZE (0) == 8;
840 /* Check whether the application is pthreaded. */
842 status
= pthdb_session_pthreaded (PD_USER
, PTHDB_FLAG_REGS
, &pd_callbacks
,
844 if ((status
!= PTHDB_SUCCESS
&& status
!= PTHDB_NOT_PTHREADED
) || !stub_name
)
847 /* Set a breakpoint on the returned stub function. */
848 if (!(ms
= lookup_minimal_symbol (stub_name
, NULL
, NULL
)))
850 pd_brk_addr
= SYMBOL_VALUE_ADDRESS (ms
);
851 if (!create_thread_event_breakpoint (pd_brk_addr
))
854 /* Prepare for thread debugging. */
855 base_ops
= current_target
;
859 /* If we're debugging a core file or an attached inferior, the pthread
860 library may already have been initialized, so try to activate thread
865 /* Undo the effects of pd_enable(). */
875 unpush_target (&ops
);
878 /* target_new_objfile_hook callback.
880 If OBJFILE is non-null, check whether a threaded application is being
881 debugged, and if so, prepare for thread debugging.
883 If OBJFILE is null, stop debugging threads. */
886 new_objfile (struct objfile
*objfile
)
893 if (target_new_objfile_chain
)
894 target_new_objfile_chain (objfile
);
897 /* Attach to process specified by ARGS. */
900 ops_attach (char *args
, int from_tty
)
902 base_ops
.to_attach (args
, from_tty
);
906 /* Detach from the process attached to by ops_attach(). */
909 ops_detach (char *args
, int from_tty
)
912 base_ops
.to_detach (args
, from_tty
);
915 /* Tell the inferior process to continue running thread PID if != -1
916 and all threads otherwise. */
919 ops_resume (ptid_t ptid
, int step
, enum target_signal sig
)
921 struct thread_info
*thread
;
925 CALL_BASE (base_ops
.to_resume (ptid
, step
, sig
));
928 thread
= find_thread_pid (ptid
);
930 error ("aix-thread resume: unknown pthread %ld", TIDGET (ptid
));
932 tid
[0] = thread
->private->tid
;
933 if (tid
[0] == PTHDB_INVALID_TID
)
934 error ("aix-thread resume: no tid for pthread %ld", TIDGET (ptid
));
938 ptrace64aix (PTT_CONTINUE
, tid
[0], 1, target_signal_to_host (sig
), (int *)tid
);
940 ptrace32 (PTT_CONTINUE
, tid
[0], (int *) 1,
941 target_signal_to_host (sig
), (int *)tid
);
945 /* Wait for thread/process ID if != -1 or for any thread otherwise. If an
946 error occurs, return -1, else return the pid of the stopped thread. */
949 ops_wait (ptid_t ptid
, struct target_waitstatus
*status
)
952 CALL_BASE (ptid
= base_ops
.to_wait (ptid
, status
));
953 if (PIDGET (ptid
) == -1)
954 return pid_to_ptid (-1);
956 /* Check whether libpthdebug might be ready to be initialized. */
957 if (!pd_active
&& status
->kind
== TARGET_WAITKIND_STOPPED
&&
958 status
->value
.sig
== TARGET_SIGNAL_TRAP
&&
959 read_pc_pid (ptid
) - DECR_PC_AFTER_BREAK
== pd_brk_addr
)
960 return pd_activate (0);
962 return pd_update (0);
965 /* Record that the 64-bit general-purpose registers contain VALS. */
968 supply_gprs64 (uint64_t *vals
)
972 for (regno
= 0; regno
< 32; regno
++)
973 supply_register (regno
, (char *) (vals
+ regno
));
976 /* Record that 32-bit register REGNO contains VAL. */
979 supply_reg32 (int regno
, uint32_t val
)
981 supply_register (regno
, (char *) &val
);
984 /* Record that the floating-point registers contain VALS. */
987 supply_fprs (double *vals
)
991 for (regno
= 0; regno
< 32; regno
++)
992 supply_register (regno
+ FP0_REGNUM
, (char *) (vals
+ regno
));
995 /* Record that the special registers contain the specified 64-bit and 32-bit
999 supply_sprs64 (uint64_t iar
, uint64_t msr
, uint32_t cr
,
1000 uint64_t lr
, uint64_t ctr
, uint32_t xer
)
1002 int regno
= FIRST_UISA_SP_REGNUM
;
1003 supply_register (regno
, (char *) &iar
);
1004 supply_register (regno
+ 1, (char *) &msr
);
1005 supply_register (regno
+ 2, (char *) &cr
);
1006 supply_register (regno
+ 3, (char *) &lr
);
1007 supply_register (regno
+ 4, (char *) &ctr
);
1008 supply_register (regno
+ 5, (char *) &xer
);
1011 /* Record that the special registers contain the specified 32-bit values. */
1014 supply_sprs32 (uint32_t iar
, uint32_t msr
, uint32_t cr
,
1015 uint32_t lr
, uint32_t ctr
, uint32_t xer
)
1017 int regno
= FIRST_UISA_SP_REGNUM
;
1018 supply_register (regno
, (char *) &iar
);
1019 supply_register (regno
+ 1, (char *) &msr
);
1020 supply_register (regno
+ 2, (char *) &cr
);
1021 supply_register (regno
+ 3, (char *) &lr
);
1022 supply_register (regno
+ 4, (char *) &ctr
);
1023 supply_register (regno
+ 5, (char *) &xer
);
1026 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1029 There's no way to query a single register from a non-kernel pthread,
1030 so there's no need for a single-register version of this function. */
1033 fetch_regs_lib (pthdb_pthread_t pdtid
)
1036 pthdb_context_t ctx
;
1038 if (debug_aix_thread
)
1039 fprintf_unfiltered (gdb_stdlog
, "fetch_regs_lib %lx\n", (long)pdtid
);
1040 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1041 if (status
!= PTHDB_SUCCESS
)
1042 PD_ERROR ("fetch_registers: pthdb_pthread_context", status
);
1044 /* General-purpose registers. */
1047 supply_gprs64 (ctx
.gpr
);
1049 for (i
= 0; i
< 32; i
++)
1050 supply_reg32 (i
, ctx
.gpr
[i
]);
1052 /* Floating-point registers. */
1054 supply_fprs (ctx
.fpr
);
1056 /* Special registers. */
1059 supply_sprs64 (ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
, ctx
.xer
);
1061 supply_sprs32 (ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
, ctx
.xer
);
1064 /* Fetch register REGNO if != -1 or all registers otherwise from kernel thread
1067 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or SPRs,
1068 but there's no way to query individual registers within those groups.
1069 Therefore, if REGNO != -1, this function fetches an entire group.
1071 Unfortunately, kernel thread register queries often fail with EPERM,
1072 indicating that the thread is in kernel space. This breaks backtraces of
1073 threads other than the current one. To make that breakage obvious without
1074 throwing an error to top level (which is bad e.g. during "info threads"
1075 output), zero registers that can't be retrieved. */
1078 fetch_regs_kern (int regno
, pthdb_tid_t tid
)
1080 uint64_t gprs64
[32];
1081 uint32_t gprs32
[32];
1083 struct ptxsprs sprs64
;
1084 struct ptsprs sprs32
;
1087 if (debug_aix_thread
)
1088 fprintf_unfiltered (gdb_stdlog
,
1089 "fetch_regs_kern tid=%lx regno=%d arch64=%d\n",
1090 (long)tid
, regno
, arch64
);
1092 /* General-purpose registers. */
1093 if (regno
== -1 || regno
< FP0_REGNUM
)
1097 if (!ptrace64aix (PTT_READ_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
))
1098 memset (gprs64
, 0, sizeof (gprs64
));
1099 supply_gprs64 (gprs64
);
1103 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
1104 memset (gprs32
, 0, sizeof (gprs32
));
1105 for (i
= 0; i
< 32; i
++)
1106 supply_reg32 (i
, gprs32
[i
]);
1110 /* Floating-point registers. */
1112 if (regno
== -1 || (regno
>= FP0_REGNUM
&& regno
<= FPLAST_REGNUM
))
1114 if (!ptrace32 (PTT_READ_FPRS
, tid
, (int *) fprs
, 0, NULL
))
1115 memset (fprs
, 0, sizeof (fprs
));
1119 /* Special-purpose registers. */
1121 if (regno
== -1 || (regno
> FPLAST_REGNUM
&& regno
<= LAST_UISA_SP_REGNUM
))
1125 if (!ptrace64aix (PTT_READ_SPRS
, tid
, (unsigned long) &sprs64
, 0, NULL
))
1126 memset (&sprs64
, 0, sizeof (sprs64
));
1127 supply_sprs64 (sprs64
.pt_iar
, sprs64
.pt_msr
, sprs64
.pt_cr
,
1128 sprs64
.pt_lr
, sprs64
.pt_ctr
, sprs64
.pt_xer
);
1132 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
1133 memset (&sprs32
, 0, sizeof (sprs32
));
1134 supply_sprs32 (sprs32
.pt_iar
, sprs32
.pt_msr
, sprs32
.pt_cr
,
1135 sprs32
.pt_lr
, sprs32
.pt_ctr
, sprs32
.pt_xer
);
1137 if (REGISTER_RAW_SIZE (LAST_UISA_SP_REGNUM
))
1138 supply_register (LAST_UISA_SP_REGNUM
, (char *) &sprs32
.pt_mq
);
1143 /* Fetch register REGNO if != -1 or all registers otherwise in the
1144 thread/process specified by inferior_ptid. */
1147 ops_fetch_registers (int regno
)
1149 struct thread_info
*thread
;
1152 if (!PD_TID (inferior_ptid
))
1153 base_ops
.to_fetch_registers (regno
);
1156 thread
= find_thread_pid (inferior_ptid
);
1157 tid
= thread
->private->tid
;
1159 if (tid
== PTHDB_INVALID_TID
)
1160 fetch_regs_lib (thread
->private->pdtid
);
1162 fetch_regs_kern (regno
, tid
);
1166 /* Store the special registers into the specified 64-bit and 32-bit
1170 fill_sprs64 (uint64_t *iar
, uint64_t *msr
, uint32_t *cr
,
1171 uint64_t *lr
, uint64_t *ctr
, uint32_t *xer
)
1173 int regno
= FIRST_UISA_SP_REGNUM
;
1174 *iar
= read_register (regno
);
1175 *msr
= read_register (regno
+ 1);
1176 *cr
= read_register (regno
+ 2);
1177 *lr
= read_register (regno
+ 3);
1178 *ctr
= read_register (regno
+ 4);
1179 *xer
= read_register (regno
+ 5);
1182 /* Store all registers into pthread PDTID, which doesn't have a kernel
1185 It's possible to store a single register into a non-kernel pthread, but I
1186 doubt it's worth the effort. */
1189 store_regs_lib (pthdb_pthread_t pdtid
)
1192 pthdb_context_t ctx
;
1194 if (debug_aix_thread
)
1195 fprintf_unfiltered (gdb_stdlog
, "store_regs_lib %lx\n", (long)pdtid
);
1197 /* Retrieve the thread's current context for its non-register values. */
1198 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1199 if (status
!= PTHDB_SUCCESS
)
1200 PD_ERROR ("store_registers: pthdb_pthread_context", status
);
1202 /* General-purpose registers. */
1204 for (i
= 0; i
< 32; i
++)
1205 ctx
.gpr
[i
] = read_register (i
);
1207 /* Floating-point registers. */
1209 for (i
= 0; i
< 32; i
++)
1210 ctx
.fpr
[i
] = *(double *) ®isters
[REGISTER_BYTE (FP0_REGNUM
+ i
)];
1212 /* Special registers. */
1214 fill_sprs64 (&ctx
.iar
, &ctx
.msr
, &ctx
.cr
, &ctx
.lr
, &ctx
.ctr
, &ctx
.xer
);
1216 status
= pthdb_pthread_setcontext (pd_session
, pdtid
, &ctx
);
1217 if (status
!= PTHDB_SUCCESS
)
1218 PD_ERROR ("store_registers: pthdb_pthread_setcontext", status
);
1221 /* Store register REGNO if != -1 or all registers otherwise into kernel
1224 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or SPRs, but
1225 there's no way to set individual registers within those groups. Therefore,
1226 if REGNO != -1, this function stores an entire group. */
1229 store_regs_kern (int regno
, pthdb_tid_t tid
)
1231 struct ptxsprs sprs64
;
1232 struct ptsprs sprs32
;
1235 if (debug_aix_thread
)
1236 fprintf_unfiltered (gdb_stdlog
, "store_regs_kern tid=%lx regno=%d\n",
1239 /* General-purpose registers. */
1240 if (regno
== -1 || regno
< FP0_REGNUM
)
1242 regp
= ®isters
[REGISTER_BYTE (0)];
1244 ptrace64aix (PTT_WRITE_GPRS
, tid
, (unsigned long) regp
, 0, NULL
);
1246 ptrace32 (PTT_WRITE_GPRS
, tid
, (int *) regp
, 0, NULL
);
1249 /* Floating-point registers. */
1251 if (regno
== -1 || (regno
>= FP0_REGNUM
&& regno
<= FPLAST_REGNUM
))
1253 regp
= ®isters
[REGISTER_BYTE (FP0_REGNUM
)];
1254 ptrace32 (PTT_WRITE_FPRS
, tid
, (int *) regp
, 0, NULL
);
1257 /* Special-purpose registers. */
1259 if (regno
== -1 || (regno
> FPLAST_REGNUM
&& regno
<= LAST_UISA_SP_REGNUM
))
1263 ptrace64aix (PTT_READ_SPRS
, tid
, (unsigned long) &sprs64
, 0, NULL
);
1264 fill_sprs64 (&sprs64
.pt_iar
, &sprs64
.pt_msr
, &sprs64
.pt_cr
,
1265 &sprs64
.pt_lr
, &sprs64
.pt_ctr
, &sprs64
.pt_xer
);
1266 ptrace64aix (PTT_WRITE_SPRS
, tid
, (unsigned long) &sprs64
, 0, NULL
);
1270 ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1272 regno
= FIRST_UISA_SP_REGNUM
;
1273 sprs32
.pt_iar
= read_register (regno
);
1274 sprs32
.pt_msr
= read_register (regno
+ 1);
1275 sprs32
.pt_cr
= read_register (regno
+ 2);
1276 sprs32
.pt_lr
= read_register (regno
+ 3);
1277 sprs32
.pt_ctr
= read_register (regno
+ 4);
1278 sprs32
.pt_xer
= read_register (regno
+ 5);
1280 if (REGISTER_RAW_SIZE (LAST_UISA_SP_REGNUM
))
1281 sprs32
.pt_mq
= read_register (LAST_UISA_SP_REGNUM
);
1283 ptrace32 (PTT_WRITE_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1288 /* Store gdb's current view of the register set into the thread/process
1289 specified by inferior_ptid. */
1292 ops_store_registers (int regno
)
1294 struct thread_info
*thread
;
1297 if (!PD_TID (inferior_ptid
))
1298 base_ops
.to_store_registers (regno
);
1301 thread
= find_thread_pid (inferior_ptid
);
1302 tid
= thread
->private->tid
;
1304 if (tid
== PTHDB_INVALID_TID
)
1305 store_regs_lib (thread
->private->pdtid
);
1307 store_regs_kern (regno
, tid
);
1311 /* Prepare to modify the registers array. */
1314 ops_prepare_to_store (void)
1316 if (!PD_TID (inferior_ptid
))
1317 base_ops
.to_prepare_to_store ();
1319 read_register_bytes (0, NULL
, REGISTER_BYTES
);
1322 /* Transfer LEN bytes of memory from GDB address MYADDR to target address
1323 MEMADDR if WRITE and vice versa otherwise. */
1326 ops_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
1327 struct mem_attrib
*attrib
,
1328 struct target_ops
*target
)
1332 CALL_BASE (n
= base_ops
.to_xfer_memory (memaddr
, myaddr
, len
, write
,
1333 attrib
, &base_ops
));
1337 /* Kill and forget about the inferior process. */
1342 CALL_BASE (base_ops
.to_kill ());
1345 /* Clean up after the inferior exits. */
1348 ops_mourn_inferior (void)
1351 base_ops
.to_mourn_inferior ();
1354 /* Return whether thread PID is still valid. */
1357 ops_thread_alive (ptid_t ptid
)
1360 return base_ops
.to_thread_alive (ptid
);
1362 /* We update the thread list every time the child stops, so all valid
1363 threads should be in the thread list. */
1364 return in_thread_list (ptid
);
1367 /* Return a printable representation of composite PID for use in "info
1371 ops_pid_to_str (ptid_t ptid
)
1373 static char *ret
= NULL
;
1376 return base_ops
.to_pid_to_str (ptid
);
1378 /* Free previous return value; a new one will be allocated by
1382 xasprintf (&ret
, "Thread %ld", ptid_get_tid (ptid
));
1386 /* Return a printable representation of extra information about THREAD, for
1387 use in "info threads" output. */
1390 ops_extra_thread_info (struct thread_info
*thread
)
1392 struct ui_file
*buf
;
1394 pthdb_pthread_t pdtid
;
1396 pthdb_state_t state
;
1397 pthdb_suspendstate_t suspendstate
;
1398 pthdb_detachstate_t detachstate
;
1401 static char *ret
= NULL
;
1403 if (!PD_TID (thread
->ptid
))
1406 buf
= mem_fileopen ();
1408 pdtid
= thread
->private->pdtid
;
1409 tid
= thread
->private->tid
;
1411 if (tid
!= PTHDB_INVALID_TID
)
1412 fprintf_unfiltered (buf
, "tid %d", tid
);
1414 status
= pthdb_pthread_state (pd_session
, pdtid
, &state
);
1415 if (status
!= PTHDB_SUCCESS
)
1417 fprintf_unfiltered (buf
, ", %s", state2str (state
));
1419 status
= pthdb_pthread_suspendstate (pd_session
, pdtid
, &suspendstate
);
1420 if (status
== PTHDB_SUCCESS
&& suspendstate
== PSS_SUSPENDED
)
1421 fprintf_unfiltered (buf
, ", suspended");
1423 status
= pthdb_pthread_detachstate (pd_session
, pdtid
, &detachstate
);
1424 if (status
== PTHDB_SUCCESS
&& detachstate
== PDS_DETACHED
)
1425 fprintf_unfiltered (buf
, ", detached");
1427 pthdb_pthread_cancelpend (pd_session
, pdtid
, &cancelpend
);
1428 if (status
== PTHDB_SUCCESS
&& cancelpend
)
1429 fprintf_unfiltered (buf
, ", cancel pending");
1431 ui_file_write (buf
, "", 1);
1433 xfree (ret
); /* Free old buffer. */
1435 ret
= ui_file_xstrdup (buf
, &length
);
1436 ui_file_delete (buf
);
1441 /* Initialize target ops. */
1446 ops
.to_shortname
= "aix-threads";
1447 ops
.to_longname
= "AIX pthread support";
1448 ops
.to_doc
= "AIX pthread support";
1450 ops
.to_attach
= ops_attach
;
1451 ops
.to_detach
= ops_detach
;
1452 ops
.to_resume
= ops_resume
;
1453 ops
.to_wait
= ops_wait
;
1454 ops
.to_fetch_registers
= ops_fetch_registers
;
1455 ops
.to_store_registers
= ops_store_registers
;
1456 ops
.to_prepare_to_store
= ops_prepare_to_store
;
1457 ops
.to_xfer_memory
= ops_xfer_memory
;
1458 /* No need for ops.to_create_inferior, because we activate thread debugging
1459 when the inferior reaches pd_brk_addr. */
1460 ops
.to_kill
= ops_kill
;
1461 ops
.to_mourn_inferior
= ops_mourn_inferior
;
1462 ops
.to_thread_alive
= ops_thread_alive
;
1463 ops
.to_pid_to_str
= ops_pid_to_str
;
1464 ops
.to_extra_thread_info
= ops_extra_thread_info
;
1465 ops
.to_stratum
= thread_stratum
;
1466 ops
.to_magic
= OPS_MAGIC
;
1469 /* Module startup initialization function, automagically called by
1473 _initialize_aix_thread (void)
1478 /* Notice when object files get loaded and unloaded. */
1479 target_new_objfile_chain
= target_new_objfile_hook
;
1480 target_new_objfile_hook
= new_objfile
;
1482 add_show_from_set (add_set_cmd ("aix-thread", no_class
, var_zinteger
,
1483 (char *) &debug_aix_thread
,
1484 "Set debugging of AIX thread module.\n"
1485 "Enables printf debugging output.\n",