1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2000, 2002, 2007-2012 Free Software Foundation,
5 Written by Nick Duffek <nsd@redhat.com>.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
24 debugging pthread applications.
26 Some name prefix conventions:
27 pthdb_ provided by libpthdebug.a
28 pdc_ callbacks that this module provides to libpthdebug.a
29 pd_ variables or functions interfacing with libpthdebug.a
31 libpthdebug peculiarities:
33 - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but
34 it's not documented, and after several calls it stops working
35 and causes other libpthdebug functions to fail.
37 - pthdb_tid_pthread() doesn't always work after
38 pthdb_session_update(), but it does work after cycling through
39 all threads using pthdb_pthread().
44 #include "gdb_assert.h"
45 #include "gdbthread.h"
51 #include "gdb_string.h"
55 #include <sys/types.h>
56 #include <sys/ptrace.h>
59 #include <sys/pthdebug.h>
61 #if !HAVE_DECL_GETTHRDS
62 extern int getthrds (pid_t
, struct thrdsinfo64
*, int, pthdb_tid_t
*, int);
65 /* Whether to emit debugging output. */
66 static int debug_aix_thread
;
68 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
69 #ifndef PTHDB_VERSION_3
70 #define pthdb_tid_t tid_t
73 /* Return whether to treat PID as a debuggable thread id. */
75 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
77 /* Build a thread ptid. */
78 #define BUILD_THREAD(TID, PID) ptid_build (PID, 0, TID)
80 /* Build and lwp ptid. */
81 #define BUILD_LWP(LWP, PID) MERGEPID (PID, LWP)
83 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
84 PTHDB_BAD_USER errors, so use 1. */
88 /* Success and failure values returned by pthdb callbacks. */
90 #define PDC_SUCCESS PTHDB_SUCCESS
91 #define PDC_FAILURE PTHDB_CALLBACK
93 /* Private data attached to each element in GDB's thread list. */
95 struct private_thread_info
{
96 pthdb_pthread_t pdtid
; /* thread's libpthdebug id */
97 pthdb_tid_t tid
; /* kernel thread id */
100 /* Information about a thread of which libpthdebug is aware. */
103 pthdb_pthread_t pdtid
;
108 /* This module's target-specific operations, active while pd_able is true. */
110 static struct target_ops aix_thread_ops
;
112 /* Address of the function that libpthread will call when libpthdebug
113 is ready to be initialized. */
115 static CORE_ADDR pd_brk_addr
;
117 /* Whether the current application is debuggable by pthdb. */
119 static int pd_able
= 0;
121 /* Whether a threaded application is being debugged. */
123 static int pd_active
= 0;
125 /* Whether the current architecture is 64-bit.
126 Only valid when pd_able is true. */
130 /* Forward declarations for pthdb callbacks. */
132 static int pdc_symbol_addrs (pthdb_user_t
, pthdb_symbol_t
*, int);
133 static int pdc_read_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
134 static int pdc_write_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
135 static int pdc_read_regs (pthdb_user_t user
, pthdb_tid_t tid
,
136 unsigned long long flags
,
137 pthdb_context_t
*context
);
138 static int pdc_write_regs (pthdb_user_t user
, pthdb_tid_t tid
,
139 unsigned long long flags
,
140 pthdb_context_t
*context
);
141 static int pdc_alloc (pthdb_user_t
, size_t, void **);
142 static int pdc_realloc (pthdb_user_t
, void *, size_t, void **);
143 static int pdc_dealloc (pthdb_user_t
, void *);
145 /* pthdb callbacks. */
147 static pthdb_callbacks_t pd_callbacks
= {
159 /* Current pthdb session. */
161 static pthdb_session_t pd_session
;
163 /* Return a printable representation of pthdebug function return
167 pd_status2str (int status
)
171 case PTHDB_SUCCESS
: return "SUCCESS";
172 case PTHDB_NOSYS
: return "NOSYS";
173 case PTHDB_NOTSUP
: return "NOTSUP";
174 case PTHDB_BAD_VERSION
: return "BAD_VERSION";
175 case PTHDB_BAD_USER
: return "BAD_USER";
176 case PTHDB_BAD_SESSION
: return "BAD_SESSION";
177 case PTHDB_BAD_MODE
: return "BAD_MODE";
178 case PTHDB_BAD_FLAGS
: return "BAD_FLAGS";
179 case PTHDB_BAD_CALLBACK
: return "BAD_CALLBACK";
180 case PTHDB_BAD_POINTER
: return "BAD_POINTER";
181 case PTHDB_BAD_CMD
: return "BAD_CMD";
182 case PTHDB_BAD_PTHREAD
: return "BAD_PTHREAD";
183 case PTHDB_BAD_ATTR
: return "BAD_ATTR";
184 case PTHDB_BAD_MUTEX
: return "BAD_MUTEX";
185 case PTHDB_BAD_MUTEXATTR
: return "BAD_MUTEXATTR";
186 case PTHDB_BAD_COND
: return "BAD_COND";
187 case PTHDB_BAD_CONDATTR
: return "BAD_CONDATTR";
188 case PTHDB_BAD_RWLOCK
: return "BAD_RWLOCK";
189 case PTHDB_BAD_RWLOCKATTR
: return "BAD_RWLOCKATTR";
190 case PTHDB_BAD_KEY
: return "BAD_KEY";
191 case PTHDB_BAD_PTID
: return "BAD_PTID";
192 case PTHDB_BAD_TID
: return "BAD_TID";
193 case PTHDB_CALLBACK
: return "CALLBACK";
194 case PTHDB_CONTEXT
: return "CONTEXT";
195 case PTHDB_HELD
: return "HELD";
196 case PTHDB_NOT_HELD
: return "NOT_HELD";
197 case PTHDB_MEMORY
: return "MEMORY";
198 case PTHDB_NOT_PTHREADED
: return "NOT_PTHREADED";
199 case PTHDB_SYMBOL
: return "SYMBOL";
200 case PTHDB_NOT_AVAIL
: return "NOT_AVAIL";
201 case PTHDB_INTERNAL
: return "INTERNAL";
202 default: return "UNKNOWN";
206 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
207 exceptional conditions and either return nonlocally or else return
208 1 for success and 0 for failure. */
211 ptrace_check (int req
, int id
, int ret
)
213 if (ret
== 0 && !errno
)
216 /* According to ptrace(2), ptrace may fail with EPERM if "the
217 Identifier parameter corresponds to a kernel thread which is
218 stopped in kernel mode and whose computational state cannot be
219 read or written." This happens quite often with register reads. */
226 if (ret
== -1 && errno
== EPERM
)
228 if (debug_aix_thread
)
229 fprintf_unfiltered (gdb_stdlog
,
230 "ptrace (%d, %d) = %d (errno = %d)\n",
231 req
, id
, ret
, errno
);
232 return ret
== -1 ? 0 : 1;
236 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
237 req
, id
, ret
, errno
, safe_strerror (errno
));
238 return 0; /* Not reached. */
241 /* Call ptracex (REQ, ID, ADDR, DATA, BUF). Return success. */
244 ptrace64aix (int req
, int id
, long long addr
, int data
, int *buf
)
247 return ptrace_check (req
, id
, ptracex (req
, id
, addr
, data
, buf
));
250 /* Call ptrace (REQ, ID, ADDR, DATA, BUF). Return success. */
253 ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
256 return ptrace_check (req
, id
,
257 ptrace (req
, id
, (int *) addr
, data
, buf
));
260 /* If *PIDP is a composite process/thread id, convert it to a
264 pid_to_prc (ptid_t
*ptidp
)
270 *ptidp
= pid_to_ptid (PIDGET (ptid
));
273 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
274 the address of SYMBOLS[<i>].name. */
277 pdc_symbol_addrs (pthdb_user_t user
, pthdb_symbol_t
*symbols
, int count
)
279 struct minimal_symbol
*ms
;
283 if (debug_aix_thread
)
284 fprintf_unfiltered (gdb_stdlog
,
285 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
286 user
, (long) symbols
, count
);
288 for (i
= 0; i
< count
; i
++)
290 name
= symbols
[i
].name
;
291 if (debug_aix_thread
)
292 fprintf_unfiltered (gdb_stdlog
,
293 " symbols[%d].name = \"%s\"\n", i
, name
);
299 if (!(ms
= lookup_minimal_symbol (name
, NULL
, NULL
)))
301 if (debug_aix_thread
)
302 fprintf_unfiltered (gdb_stdlog
, " returning PDC_FAILURE\n");
305 symbols
[i
].addr
= SYMBOL_VALUE_ADDRESS (ms
);
307 if (debug_aix_thread
)
308 fprintf_unfiltered (gdb_stdlog
, " symbols[%d].addr = %s\n",
309 i
, hex_string (symbols
[i
].addr
));
311 if (debug_aix_thread
)
312 fprintf_unfiltered (gdb_stdlog
, " returning PDC_SUCCESS\n");
316 /* Read registers call back function should be able to read the
317 context information of a debuggee kernel thread from an active
318 process or from a core file. The information should be formatted
319 in context64 form for both 32-bit and 64-bit process.
320 If successful return 0, else non-zero is returned. */
323 pdc_read_regs (pthdb_user_t user
,
325 unsigned long long flags
,
326 pthdb_context_t
*context
)
328 /* This function doesn't appear to be used, so we could probably
329 just return 0 here. HOWEVER, if it is not defined, the OS will
330 complain and several thread debug functions will fail. In case
331 this is needed, I have implemented what I think it should do,
332 however this code is untested. */
334 uint64_t gprs64
[ppc_num_gprs
];
335 uint32_t gprs32
[ppc_num_gprs
];
336 double fprs
[ppc_num_fprs
];
337 struct ptxsprs sprs64
;
338 struct ptsprs sprs32
;
340 if (debug_aix_thread
)
341 fprintf_unfiltered (gdb_stdlog
, "pdc_read_regs tid=%d flags=%s\n",
342 (int) tid
, hex_string (flags
));
344 /* General-purpose registers. */
345 if (flags
& PTHDB_FLAG_GPRS
)
349 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
350 (unsigned long) gprs64
, 0, NULL
))
351 memset (gprs64
, 0, sizeof (gprs64
));
352 memcpy (context
->gpr
, gprs64
, sizeof(gprs64
));
356 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
357 memset (gprs32
, 0, sizeof (gprs32
));
358 memcpy (context
->gpr
, gprs32
, sizeof(gprs32
));
362 /* Floating-point registers. */
363 if (flags
& PTHDB_FLAG_FPRS
)
365 if (!ptrace32 (PTT_READ_FPRS
, tid
, (void *) fprs
, 0, NULL
))
366 memset (fprs
, 0, sizeof (fprs
));
367 memcpy (context
->fpr
, fprs
, sizeof(fprs
));
370 /* Special-purpose registers. */
371 if (flags
& PTHDB_FLAG_SPRS
)
375 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
376 (unsigned long) &sprs64
, 0, NULL
))
377 memset (&sprs64
, 0, sizeof (sprs64
));
378 memcpy (&context
->msr
, &sprs64
, sizeof(sprs64
));
382 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
383 memset (&sprs32
, 0, sizeof (sprs32
));
384 memcpy (&context
->msr
, &sprs32
, sizeof(sprs32
));
390 /* Write register function should be able to write requested context
391 information to specified debuggee's kernel thread id.
392 If successful return 0, else non-zero is returned. */
395 pdc_write_regs (pthdb_user_t user
,
397 unsigned long long flags
,
398 pthdb_context_t
*context
)
400 /* This function doesn't appear to be used, so we could probably
401 just return 0 here. HOWEVER, if it is not defined, the OS will
402 complain and several thread debug functions will fail. In case
403 this is needed, I have implemented what I think it should do,
404 however this code is untested. */
406 if (debug_aix_thread
)
407 fprintf_unfiltered (gdb_stdlog
, "pdc_write_regs tid=%d flags=%s\n",
408 (int) tid
, hex_string (flags
));
410 /* General-purpose registers. */
411 if (flags
& PTHDB_FLAG_GPRS
)
414 ptrace64aix (PTT_WRITE_GPRS
, tid
,
415 (unsigned long) context
->gpr
, 0, NULL
);
417 ptrace32 (PTT_WRITE_GPRS
, tid
, (int *) context
->gpr
, 0, NULL
);
420 /* Floating-point registers. */
421 if (flags
& PTHDB_FLAG_FPRS
)
423 ptrace32 (PTT_WRITE_FPRS
, tid
, (int *) context
->fpr
, 0, NULL
);
426 /* Special-purpose registers. */
427 if (flags
& PTHDB_FLAG_SPRS
)
431 ptrace64aix (PTT_WRITE_SPRS
, tid
,
432 (unsigned long) &context
->msr
, 0, NULL
);
436 ptrace32 (PTT_WRITE_SPRS
, tid
, (void *) &context
->msr
, 0, NULL
);
442 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
445 pdc_read_data (pthdb_user_t user
, void *buf
,
446 pthdb_addr_t addr
, size_t len
)
450 if (debug_aix_thread
)
451 fprintf_unfiltered (gdb_stdlog
,
452 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
453 user
, (long) buf
, hex_string (addr
), len
);
455 status
= target_read_memory (addr
, buf
, len
);
456 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
458 if (debug_aix_thread
)
459 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n",
460 status
, pd_status2str (ret
));
464 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
467 pdc_write_data (pthdb_user_t user
, void *buf
,
468 pthdb_addr_t addr
, size_t len
)
472 if (debug_aix_thread
)
473 fprintf_unfiltered (gdb_stdlog
,
474 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
475 user
, (long) buf
, hex_string (addr
), len
);
477 status
= target_write_memory (addr
, buf
, len
);
478 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
480 if (debug_aix_thread
)
481 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n", status
,
482 pd_status2str (ret
));
486 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
490 pdc_alloc (pthdb_user_t user
, size_t len
, void **bufp
)
492 if (debug_aix_thread
)
493 fprintf_unfiltered (gdb_stdlog
,
494 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
495 user
, len
, (long) bufp
);
496 *bufp
= xmalloc (len
);
497 if (debug_aix_thread
)
498 fprintf_unfiltered (gdb_stdlog
,
499 " malloc returned 0x%lx\n", (long) *bufp
);
501 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
504 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
507 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
508 realloc callback, so that it contains LEN bytes, and store a
509 pointer to the result in BUFP. */
512 pdc_realloc (pthdb_user_t user
, void *buf
, size_t len
, void **bufp
)
514 if (debug_aix_thread
)
515 fprintf_unfiltered (gdb_stdlog
,
516 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
517 user
, (long) buf
, len
, (long) bufp
);
518 *bufp
= xrealloc (buf
, len
);
519 if (debug_aix_thread
)
520 fprintf_unfiltered (gdb_stdlog
,
521 " realloc returned 0x%lx\n", (long) *bufp
);
522 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
525 /* pthdb callback: free BUF, which was allocated by the alloc or
529 pdc_dealloc (pthdb_user_t user
, void *buf
)
531 if (debug_aix_thread
)
532 fprintf_unfiltered (gdb_stdlog
,
533 "pdc_free (user = %ld, buf = 0x%lx)\n", user
,
539 /* Return a printable representation of pthread STATE. */
542 state2str (pthdb_state_t state
)
547 /* i18n: Like "Thread-Id %d, [state] idle" */
548 return _("idle"); /* being created */
550 /* i18n: Like "Thread-Id %d, [state] running" */
551 return _("running"); /* running */
553 /* i18n: Like "Thread-Id %d, [state] sleeping" */
554 return _("sleeping"); /* awaiting an event */
556 /* i18n: Like "Thread-Id %d, [state] ready" */
557 return _("ready"); /* runnable */
559 /* i18n: Like "Thread-Id %d, [state] finished" */
560 return _("finished"); /* awaiting a join/detach */
562 /* i18n: Like "Thread-Id %d, [state] unknown" */
567 /* qsort() comparison function for sorting pd_thread structs by pthid. */
570 pcmp (const void *p1v
, const void *p2v
)
572 struct pd_thread
*p1
= (struct pd_thread
*) p1v
;
573 struct pd_thread
*p2
= (struct pd_thread
*) p2v
;
574 return p1
->pthid
< p2
->pthid
? -1 : p1
->pthid
> p2
->pthid
;
577 /* iterate_over_threads() callback for counting GDB threads.
579 Do not count the main thread (whose tid is zero). This matches
580 the list of threads provided by the pthreaddebug library, which
581 does not include that main thread either, and thus allows us
582 to compare the two lists. */
585 giter_count (struct thread_info
*thread
, void *countp
)
587 if (PD_TID (thread
->ptid
))
592 /* iterate_over_threads() callback for accumulating GDB thread pids.
594 Do not include the main thread (whose tid is zero). This matches
595 the list of threads provided by the pthreaddebug library, which
596 does not include that main thread either, and thus allows us
597 to compare the two lists. */
600 giter_accum (struct thread_info
*thread
, void *bufp
)
602 if (PD_TID (thread
->ptid
))
604 **(struct thread_info
***) bufp
= thread
;
605 (*(struct thread_info
***) bufp
)++;
610 /* ptid comparison function */
613 ptid_cmp (ptid_t ptid1
, ptid_t ptid2
)
617 if (ptid_get_pid (ptid1
) < ptid_get_pid (ptid2
))
619 else if (ptid_get_pid (ptid1
) > ptid_get_pid (ptid2
))
621 else if (ptid_get_tid (ptid1
) < ptid_get_tid (ptid2
))
623 else if (ptid_get_tid (ptid1
) > ptid_get_tid (ptid2
))
625 else if (ptid_get_lwp (ptid1
) < ptid_get_lwp (ptid2
))
627 else if (ptid_get_lwp (ptid1
) > ptid_get_lwp (ptid2
))
633 /* qsort() comparison function for sorting thread_info structs by pid. */
636 gcmp (const void *t1v
, const void *t2v
)
638 struct thread_info
*t1
= *(struct thread_info
**) t1v
;
639 struct thread_info
*t2
= *(struct thread_info
**) t2v
;
640 return ptid_cmp (t1
->ptid
, t2
->ptid
);
643 /* Search through the list of all kernel threads for the thread
644 that has stopped on a SIGTRAP signal, and return its TID.
645 Return 0 if none found. */
648 get_signaled_thread (void)
650 struct thrdsinfo64 thrinf
;
651 pthdb_tid_t ktid
= 0;
656 if (getthrds (PIDGET (inferior_ptid
), &thrinf
,
657 sizeof (thrinf
), &ktid
, 1) != 1)
660 if (thrinf
.ti_cursig
== SIGTRAP
)
661 return thrinf
.ti_tid
;
664 /* Didn't find any thread stopped on a SIGTRAP signal. */
668 /* Synchronize GDB's thread list with libpthdebug's.
670 There are some benefits of doing this every time the inferior stops:
672 - allows users to run thread-specific commands without needing to
673 run "info threads" first
675 - helps pthdb_tid_pthread() work properly (see "libpthdebug
676 peculiarities" at the top of this module)
678 - simplifies the demands placed on libpthdebug, which seems to
679 have difficulty with certain call patterns */
682 sync_threadlists (void)
684 int cmd
, status
, infpid
;
685 int pcount
, psize
, pi
, gcount
, gi
;
686 struct pd_thread
*pbuf
;
687 struct thread_info
**gbuf
, **g
, *thread
;
688 pthdb_pthread_t pdtid
;
692 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
696 pbuf
= (struct pd_thread
*) xmalloc (psize
* sizeof *pbuf
);
698 for (cmd
= PTHDB_LIST_FIRST
;; cmd
= PTHDB_LIST_NEXT
)
700 status
= pthdb_pthread (pd_session
, &pdtid
, cmd
);
701 if (status
!= PTHDB_SUCCESS
|| pdtid
== PTHDB_INVALID_PTHREAD
)
704 status
= pthdb_pthread_ptid (pd_session
, pdtid
, &pthid
);
705 if (status
!= PTHDB_SUCCESS
|| pthid
== PTHDB_INVALID_PTID
)
711 pbuf
= (struct pd_thread
*) xrealloc (pbuf
,
712 psize
* sizeof *pbuf
);
714 pbuf
[pcount
].pdtid
= pdtid
;
715 pbuf
[pcount
].pthid
= pthid
;
719 for (pi
= 0; pi
< pcount
; pi
++)
721 status
= pthdb_pthread_tid (pd_session
, pbuf
[pi
].pdtid
, &tid
);
722 if (status
!= PTHDB_SUCCESS
)
723 tid
= PTHDB_INVALID_TID
;
727 qsort (pbuf
, pcount
, sizeof *pbuf
, pcmp
);
729 /* Accumulate an array of GDB threads sorted by pid. */
732 iterate_over_threads (giter_count
, &gcount
);
733 g
= gbuf
= (struct thread_info
**) xmalloc (gcount
* sizeof *gbuf
);
734 iterate_over_threads (giter_accum
, &g
);
735 qsort (gbuf
, gcount
, sizeof *gbuf
, gcmp
);
737 /* Apply differences between the two arrays to GDB's thread list. */
739 infpid
= PIDGET (inferior_ptid
);
740 for (pi
= gi
= 0; pi
< pcount
|| gi
< gcount
;)
744 delete_thread (gbuf
[gi
]->ptid
);
747 else if (gi
== gcount
)
749 thread
= add_thread (BUILD_THREAD (pbuf
[pi
].pthid
, infpid
));
750 thread
->private = xmalloc (sizeof (struct private_thread_info
));
751 thread
->private->pdtid
= pbuf
[pi
].pdtid
;
752 thread
->private->tid
= pbuf
[pi
].tid
;
760 pptid
= BUILD_THREAD (pbuf
[pi
].pthid
, infpid
);
761 gptid
= gbuf
[gi
]->ptid
;
762 pdtid
= pbuf
[pi
].pdtid
;
765 cmp_result
= ptid_cmp (pptid
, gptid
);
769 gbuf
[gi
]->private->pdtid
= pdtid
;
770 gbuf
[gi
]->private->tid
= tid
;
774 else if (cmp_result
> 0)
776 delete_thread (gptid
);
781 thread
= add_thread (pptid
);
782 thread
->private = xmalloc (sizeof (struct private_thread_info
));
783 thread
->private->pdtid
= pdtid
;
784 thread
->private->tid
= tid
;
794 /* Iterate_over_threads() callback for locating a thread, using
795 the TID of its associated kernel thread. */
798 iter_tid (struct thread_info
*thread
, void *tidp
)
800 const pthdb_tid_t tid
= *(pthdb_tid_t
*)tidp
;
802 return (thread
->private->tid
== tid
);
805 /* Synchronize libpthdebug's state with the inferior and with GDB,
806 generate a composite process/thread <pid> for the current thread,
807 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
810 pd_update (int set_infpid
)
815 struct thread_info
*thread
= NULL
;
818 return inferior_ptid
;
820 status
= pthdb_session_update (pd_session
);
821 if (status
!= PTHDB_SUCCESS
)
822 return inferior_ptid
;
826 /* Define "current thread" as one that just received a trap signal. */
828 tid
= get_signaled_thread ();
830 thread
= iterate_over_threads (iter_tid
, &tid
);
832 ptid
= inferior_ptid
;
837 inferior_ptid
= ptid
;
842 /* Try to start debugging threads in the current process.
843 If successful and SET_INFPID, set inferior_ptid to reflect the
847 pd_activate (int set_infpid
)
851 status
= pthdb_session_init (PD_USER
, arch64
? PEM_64BIT
: PEM_32BIT
,
852 PTHDB_FLAG_REGS
, &pd_callbacks
,
854 if (status
!= PTHDB_SUCCESS
)
856 return inferior_ptid
;
859 return pd_update (set_infpid
);
862 /* Undo the effects of pd_activate(). */
869 pthdb_session_destroy (pd_session
);
871 pid_to_prc (&inferior_ptid
);
875 /* An object file has just been loaded. Check whether the current
876 application is pthreaded, and if so, prepare for thread debugging. */
883 struct minimal_symbol
*ms
;
885 /* Don't initialize twice. */
889 /* Check application word size. */
890 arch64
= register_size (target_gdbarch
, 0) == 8;
892 /* Check whether the application is pthreaded. */
894 status
= pthdb_session_pthreaded (PD_USER
, PTHDB_FLAG_REGS
,
895 &pd_callbacks
, &stub_name
);
896 if ((status
!= PTHDB_SUCCESS
897 && status
!= PTHDB_NOT_PTHREADED
) || !stub_name
)
900 /* Set a breakpoint on the returned stub function. */
901 if (!(ms
= lookup_minimal_symbol (stub_name
, NULL
, NULL
)))
903 pd_brk_addr
= SYMBOL_VALUE_ADDRESS (ms
);
904 if (!create_thread_event_breakpoint (target_gdbarch
, pd_brk_addr
))
907 /* Prepare for thread debugging. */
908 push_target (&aix_thread_ops
);
911 /* If we're debugging a core file or an attached inferior, the
912 pthread library may already have been initialized, so try to
913 activate thread debugging. */
917 /* Undo the effects of pd_enable(). */
927 unpush_target (&aix_thread_ops
);
930 /* new_objfile observer callback.
932 If OBJFILE is non-null, check whether a threaded application is
933 being debugged, and if so, prepare for thread debugging.
935 If OBJFILE is null, stop debugging threads. */
938 new_objfile (struct objfile
*objfile
)
946 /* Attach to process specified by ARGS. */
949 aix_thread_attach (struct target_ops
*ops
, char *args
, int from_tty
)
951 struct target_ops
*beneath
= find_target_beneath (ops
);
953 beneath
->to_attach (beneath
, args
, from_tty
);
957 /* Detach from the process attached to by aix_thread_attach(). */
960 aix_thread_detach (struct target_ops
*ops
, char *args
, int from_tty
)
962 struct target_ops
*beneath
= find_target_beneath (ops
);
965 beneath
->to_detach (beneath
, args
, from_tty
);
968 /* Tell the inferior process to continue running thread PID if != -1
969 and all threads otherwise. */
972 aix_thread_resume (struct target_ops
*ops
,
973 ptid_t ptid
, int step
, enum target_signal sig
)
975 struct thread_info
*thread
;
980 struct cleanup
*cleanup
= save_inferior_ptid ();
981 struct target_ops
*beneath
= find_target_beneath (ops
);
983 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
984 beneath
->to_resume (beneath
, ptid
, step
, sig
);
985 do_cleanups (cleanup
);
989 thread
= find_thread_ptid (ptid
);
991 error (_("aix-thread resume: unknown pthread %ld"),
994 tid
[0] = thread
->private->tid
;
995 if (tid
[0] == PTHDB_INVALID_TID
)
996 error (_("aix-thread resume: no tid for pthread %ld"),
1001 ptrace64aix (PTT_CONTINUE
, tid
[0], 1,
1002 target_signal_to_host (sig
), (void *) tid
);
1004 ptrace32 (PTT_CONTINUE
, tid
[0], (int *) 1,
1005 target_signal_to_host (sig
), (void *) tid
);
1009 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1010 If an error occurs, return -1, else return the pid of the stopped
1014 aix_thread_wait (struct target_ops
*ops
,
1015 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
1017 struct cleanup
*cleanup
= save_inferior_ptid ();
1018 struct target_ops
*beneath
= find_target_beneath (ops
);
1022 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1023 ptid
= beneath
->to_wait (beneath
, ptid
, status
, options
);
1024 do_cleanups (cleanup
);
1026 if (PIDGET (ptid
) == -1)
1027 return pid_to_ptid (-1);
1029 /* Check whether libpthdebug might be ready to be initialized. */
1030 if (!pd_active
&& status
->kind
== TARGET_WAITKIND_STOPPED
1031 && status
->value
.sig
== TARGET_SIGNAL_TRAP
)
1033 struct regcache
*regcache
= get_thread_regcache (ptid
);
1034 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1036 if (regcache_read_pc (regcache
)
1037 - gdbarch_decr_pc_after_break (gdbarch
) == pd_brk_addr
)
1038 return pd_activate (0);
1041 return pd_update (0);
1044 /* Record that the 64-bit general-purpose registers contain VALS. */
1047 supply_gprs64 (struct regcache
*regcache
, uint64_t *vals
)
1049 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
1052 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1053 regcache_raw_supply (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1054 (char *) (vals
+ regno
));
1057 /* Record that 32-bit register REGNO contains VAL. */
1060 supply_reg32 (struct regcache
*regcache
, int regno
, uint32_t val
)
1062 regcache_raw_supply (regcache
, regno
, (char *) &val
);
1065 /* Record that the floating-point registers contain VALS. */
1068 supply_fprs (struct regcache
*regcache
, double *vals
)
1070 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1071 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1074 /* This function should never be called on architectures without
1075 floating-point registers. */
1076 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1078 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
1079 regcache_raw_supply (regcache
, regno
+ tdep
->ppc_fp0_regnum
,
1080 (char *) (vals
+ regno
));
1083 /* Predicate to test whether given register number is a "special" register. */
1085 special_register_p (struct gdbarch
*gdbarch
, int regno
)
1087 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1089 return regno
== gdbarch_pc_regnum (gdbarch
)
1090 || regno
== tdep
->ppc_ps_regnum
1091 || regno
== tdep
->ppc_cr_regnum
1092 || regno
== tdep
->ppc_lr_regnum
1093 || regno
== tdep
->ppc_ctr_regnum
1094 || regno
== tdep
->ppc_xer_regnum
1095 || (tdep
->ppc_fpscr_regnum
>= 0 && regno
== tdep
->ppc_fpscr_regnum
)
1096 || (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
);
1100 /* Record that the special registers contain the specified 64-bit and
1104 supply_sprs64 (struct regcache
*regcache
,
1105 uint64_t iar
, uint64_t msr
, uint32_t cr
,
1106 uint64_t lr
, uint64_t ctr
, uint32_t xer
,
1109 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1110 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1112 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
),
1114 regcache_raw_supply (regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1115 regcache_raw_supply (regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1116 regcache_raw_supply (regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1117 regcache_raw_supply (regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1118 regcache_raw_supply (regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1119 if (tdep
->ppc_fpscr_regnum
>= 0)
1120 regcache_raw_supply (regcache
, tdep
->ppc_fpscr_regnum
,
1124 /* Record that the special registers contain the specified 32-bit
1128 supply_sprs32 (struct regcache
*regcache
,
1129 uint32_t iar
, uint32_t msr
, uint32_t cr
,
1130 uint32_t lr
, uint32_t ctr
, uint32_t xer
,
1133 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1134 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1136 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
),
1138 regcache_raw_supply (regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1139 regcache_raw_supply (regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1140 regcache_raw_supply (regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1141 regcache_raw_supply (regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1142 regcache_raw_supply (regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1143 if (tdep
->ppc_fpscr_regnum
>= 0)
1144 regcache_raw_supply (regcache
, tdep
->ppc_fpscr_regnum
,
1148 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1151 There's no way to query a single register from a non-kernel
1152 pthread, so there's no need for a single-register version of this
1156 fetch_regs_user_thread (struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1158 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1159 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1161 pthdb_context_t ctx
;
1163 if (debug_aix_thread
)
1164 fprintf_unfiltered (gdb_stdlog
,
1165 "fetch_regs_user_thread %lx\n", (long) pdtid
);
1166 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1167 if (status
!= PTHDB_SUCCESS
)
1168 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1169 pd_status2str (status
));
1171 /* General-purpose registers. */
1174 supply_gprs64 (regcache
, ctx
.gpr
);
1176 for (i
= 0; i
< ppc_num_gprs
; i
++)
1177 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, ctx
.gpr
[i
]);
1179 /* Floating-point registers. */
1181 if (ppc_floating_point_unit_p (gdbarch
))
1182 supply_fprs (regcache
, ctx
.fpr
);
1184 /* Special registers. */
1187 supply_sprs64 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1188 ctx
.xer
, ctx
.fpscr
);
1190 supply_sprs32 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1191 ctx
.xer
, ctx
.fpscr
);
1194 /* Fetch register REGNO if != -1 or all registers otherwise from
1197 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1198 SPRs, but there's no way to query individual registers within those
1199 groups. Therefore, if REGNO != -1, this function fetches an entire
1202 Unfortunately, kernel thread register queries often fail with
1203 EPERM, indicating that the thread is in kernel space. This breaks
1204 backtraces of threads other than the current one. To make that
1205 breakage obvious without throwing an error to top level (which is
1206 bad e.g. during "info threads" output), zero registers that can't
1210 fetch_regs_kernel_thread (struct regcache
*regcache
, int regno
,
1213 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1214 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1215 uint64_t gprs64
[ppc_num_gprs
];
1216 uint32_t gprs32
[ppc_num_gprs
];
1217 double fprs
[ppc_num_fprs
];
1218 struct ptxsprs sprs64
;
1219 struct ptsprs sprs32
;
1222 if (debug_aix_thread
)
1223 fprintf_unfiltered (gdb_stdlog
,
1224 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1225 (long) tid
, regno
, arch64
);
1227 /* General-purpose registers. */
1229 || (tdep
->ppc_gp0_regnum
<= regno
1230 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
))
1234 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
1235 (unsigned long) gprs64
, 0, NULL
))
1236 memset (gprs64
, 0, sizeof (gprs64
));
1237 supply_gprs64 (regcache
, gprs64
);
1241 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
1242 memset (gprs32
, 0, sizeof (gprs32
));
1243 for (i
= 0; i
< ppc_num_gprs
; i
++)
1244 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, gprs32
[i
]);
1248 /* Floating-point registers. */
1250 if (ppc_floating_point_unit_p (gdbarch
)
1252 || (regno
>= tdep
->ppc_fp0_regnum
1253 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1255 if (!ptrace32 (PTT_READ_FPRS
, tid
, (void *) fprs
, 0, NULL
))
1256 memset (fprs
, 0, sizeof (fprs
));
1257 supply_fprs (regcache
, fprs
);
1260 /* Special-purpose registers. */
1262 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1266 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
1267 (unsigned long) &sprs64
, 0, NULL
))
1268 memset (&sprs64
, 0, sizeof (sprs64
));
1269 supply_sprs64 (regcache
, sprs64
.pt_iar
, sprs64
.pt_msr
,
1270 sprs64
.pt_cr
, sprs64
.pt_lr
, sprs64
.pt_ctr
,
1271 sprs64
.pt_xer
, sprs64
.pt_fpscr
);
1275 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1277 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
1278 memset (&sprs32
, 0, sizeof (sprs32
));
1279 supply_sprs32 (regcache
, sprs32
.pt_iar
, sprs32
.pt_msr
, sprs32
.pt_cr
,
1280 sprs32
.pt_lr
, sprs32
.pt_ctr
, sprs32
.pt_xer
,
1283 if (tdep
->ppc_mq_regnum
>= 0)
1284 regcache_raw_supply (regcache
, tdep
->ppc_mq_regnum
,
1285 (char *) &sprs32
.pt_mq
);
1290 /* Fetch register REGNO if != -1 or all registers otherwise in the
1291 thread/process specified by inferior_ptid. */
1294 aix_thread_fetch_registers (struct target_ops
*ops
,
1295 struct regcache
*regcache
, int regno
)
1297 struct thread_info
*thread
;
1299 struct target_ops
*beneath
= find_target_beneath (ops
);
1301 if (!PD_TID (inferior_ptid
))
1302 beneath
->to_fetch_registers (beneath
, regcache
, regno
);
1305 thread
= find_thread_ptid (inferior_ptid
);
1306 tid
= thread
->private->tid
;
1308 if (tid
== PTHDB_INVALID_TID
)
1309 fetch_regs_user_thread (regcache
, thread
->private->pdtid
);
1311 fetch_regs_kernel_thread (regcache
, regno
, tid
);
1315 /* Store the gp registers into an array of uint32_t or uint64_t. */
1318 fill_gprs64 (const struct regcache
*regcache
, uint64_t *vals
)
1320 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
1323 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1324 if (REG_VALID
== regcache_register_status (regcache
,
1325 tdep
->ppc_gp0_regnum
+ regno
))
1326 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1331 fill_gprs32 (const struct regcache
*regcache
, uint32_t *vals
)
1333 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
1336 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1337 if (REG_VALID
== regcache_register_status (regcache
,
1338 tdep
->ppc_gp0_regnum
+ regno
))
1339 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1343 /* Store the floating point registers into a double array. */
1345 fill_fprs (const struct regcache
*regcache
, double *vals
)
1347 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1348 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1351 /* This function should never be called on architectures without
1352 floating-point registers. */
1353 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1355 for (regno
= tdep
->ppc_fp0_regnum
;
1356 regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
;
1358 if (REG_VALID
== regcache_register_status (regcache
, regno
))
1359 regcache_raw_collect (regcache
, regno
,
1360 vals
+ regno
- tdep
->ppc_fp0_regnum
);
1363 /* Store the special registers into the specified 64-bit and 32-bit
1367 fill_sprs64 (const struct regcache
*regcache
,
1368 uint64_t *iar
, uint64_t *msr
, uint32_t *cr
,
1369 uint64_t *lr
, uint64_t *ctr
, uint32_t *xer
,
1372 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1373 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1375 /* Verify that the size of the size of the IAR buffer is the
1376 same as the raw size of the PC (in the register cache). If
1377 they're not, then either GDB has been built incorrectly, or
1378 there's some other kind of internal error. To be really safe,
1379 we should check all of the sizes. */
1380 gdb_assert (sizeof (*iar
) == register_size
1381 (gdbarch
, gdbarch_pc_regnum (gdbarch
)));
1383 if (REG_VALID
== regcache_register_status (regcache
,
1384 gdbarch_pc_regnum (gdbarch
)))
1385 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), iar
);
1386 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ps_regnum
))
1387 regcache_raw_collect (regcache
, tdep
->ppc_ps_regnum
, msr
);
1388 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_cr_regnum
))
1389 regcache_raw_collect (regcache
, tdep
->ppc_cr_regnum
, cr
);
1390 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_lr_regnum
))
1391 regcache_raw_collect (regcache
, tdep
->ppc_lr_regnum
, lr
);
1392 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ctr_regnum
))
1393 regcache_raw_collect (regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1394 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_xer_regnum
))
1395 regcache_raw_collect (regcache
, tdep
->ppc_xer_regnum
, xer
);
1396 if (tdep
->ppc_fpscr_regnum
>= 0
1397 && REG_VALID
== regcache_register_status (regcache
,
1398 tdep
->ppc_fpscr_regnum
))
1399 regcache_raw_collect (regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1403 fill_sprs32 (const struct regcache
*regcache
,
1404 uint32_t *iar
, uint32_t *msr
, uint32_t *cr
,
1405 uint32_t *lr
, uint32_t *ctr
, uint32_t *xer
,
1408 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1409 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1411 /* Verify that the size of the size of the IAR buffer is the
1412 same as the raw size of the PC (in the register cache). If
1413 they're not, then either GDB has been built incorrectly, or
1414 there's some other kind of internal error. To be really safe,
1415 we should check all of the sizes. */
1416 gdb_assert (sizeof (*iar
) == register_size (gdbarch
,
1417 gdbarch_pc_regnum (gdbarch
)));
1419 if (REG_VALID
== regcache_register_status (regcache
,
1420 gdbarch_pc_regnum (gdbarch
)))
1421 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), iar
);
1422 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ps_regnum
))
1423 regcache_raw_collect (regcache
, tdep
->ppc_ps_regnum
, msr
);
1424 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_cr_regnum
))
1425 regcache_raw_collect (regcache
, tdep
->ppc_cr_regnum
, cr
);
1426 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_lr_regnum
))
1427 regcache_raw_collect (regcache
, tdep
->ppc_lr_regnum
, lr
);
1428 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ctr_regnum
))
1429 regcache_raw_collect (regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1430 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_xer_regnum
))
1431 regcache_raw_collect (regcache
, tdep
->ppc_xer_regnum
, xer
);
1432 if (tdep
->ppc_fpscr_regnum
>= 0
1433 && REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_fpscr_regnum
))
1434 regcache_raw_collect (regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1437 /* Store all registers into pthread PDTID, which doesn't have a kernel
1440 It's possible to store a single register into a non-kernel pthread,
1441 but I doubt it's worth the effort. */
1444 store_regs_user_thread (const struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1446 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1447 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1449 pthdb_context_t ctx
;
1454 if (debug_aix_thread
)
1455 fprintf_unfiltered (gdb_stdlog
,
1456 "store_regs_user_thread %lx\n", (long) pdtid
);
1458 /* Retrieve the thread's current context for its non-register
1460 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1461 if (status
!= PTHDB_SUCCESS
)
1462 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1463 pd_status2str (status
));
1465 /* Collect general-purpose register values from the regcache. */
1467 for (i
= 0; i
< ppc_num_gprs
; i
++)
1468 if (REG_VALID
== regcache_register_status (regcache
,
1469 tdep
->ppc_gp0_regnum
+ i
))
1473 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ i
,
1479 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ i
,
1485 /* Collect floating-point register values from the regcache. */
1486 if (ppc_floating_point_unit_p (gdbarch
))
1487 fill_fprs (regcache
, ctx
.fpr
);
1489 /* Special registers (always kept in ctx as 64 bits). */
1492 fill_sprs64 (regcache
, &ctx
.iar
, &ctx
.msr
, &ctx
.cr
, &ctx
.lr
, &ctx
.ctr
,
1493 &ctx
.xer
, &ctx
.fpscr
);
1497 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1498 Solution: use 32-bit temp variables. */
1499 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1502 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
, &tmp_ctr
,
1503 &tmp_xer
, &tmp_fpscr
);
1504 if (REG_VALID
== regcache_register_status (regcache
,
1505 gdbarch_pc_regnum (gdbarch
)))
1507 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ps_regnum
))
1509 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_cr_regnum
))
1511 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_lr_regnum
))
1513 if (REG_VALID
== regcache_register_status (regcache
,
1514 tdep
->ppc_ctr_regnum
))
1516 if (REG_VALID
== regcache_register_status (regcache
,
1517 tdep
->ppc_xer_regnum
))
1519 if (REG_VALID
== regcache_register_status (regcache
,
1520 tdep
->ppc_xer_regnum
))
1521 ctx
.fpscr
= tmp_fpscr
;
1524 status
= pthdb_pthread_setcontext (pd_session
, pdtid
, &ctx
);
1525 if (status
!= PTHDB_SUCCESS
)
1526 error (_("aix-thread: store_registers: "
1527 "pthdb_pthread_setcontext returned %s"),
1528 pd_status2str (status
));
1531 /* Store register REGNO if != -1 or all registers otherwise into
1534 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1535 SPRs, but there's no way to set individual registers within those
1536 groups. Therefore, if REGNO != -1, this function stores an entire
1540 store_regs_kernel_thread (const struct regcache
*regcache
, int regno
,
1543 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1544 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1545 uint64_t gprs64
[ppc_num_gprs
];
1546 uint32_t gprs32
[ppc_num_gprs
];
1547 double fprs
[ppc_num_fprs
];
1548 struct ptxsprs sprs64
;
1549 struct ptsprs sprs32
;
1552 if (debug_aix_thread
)
1553 fprintf_unfiltered (gdb_stdlog
,
1554 "store_regs_kernel_thread tid=%lx regno=%d\n",
1557 /* General-purpose registers. */
1559 || (tdep
->ppc_gp0_regnum
<= regno
1560 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_fprs
))
1564 /* Pre-fetch: some regs may not be in the cache. */
1565 ptrace64aix (PTT_READ_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1566 fill_gprs64 (regcache
, gprs64
);
1567 ptrace64aix (PTT_WRITE_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1571 /* Pre-fetch: some regs may not be in the cache. */
1572 ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
);
1573 fill_gprs32 (regcache
, gprs32
);
1574 ptrace32 (PTT_WRITE_GPRS
, tid
, gprs32
, 0, NULL
);
1578 /* Floating-point registers. */
1580 if (ppc_floating_point_unit_p (gdbarch
)
1582 || (regno
>= tdep
->ppc_fp0_regnum
1583 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1585 /* Pre-fetch: some regs may not be in the cache. */
1586 ptrace32 (PTT_READ_FPRS
, tid
, (void *) fprs
, 0, NULL
);
1587 fill_fprs (regcache
, fprs
);
1588 ptrace32 (PTT_WRITE_FPRS
, tid
, (void *) fprs
, 0, NULL
);
1591 /* Special-purpose registers. */
1593 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1597 /* Pre-fetch: some registers won't be in the cache. */
1598 ptrace64aix (PTT_READ_SPRS
, tid
,
1599 (unsigned long) &sprs64
, 0, NULL
);
1600 fill_sprs64 (regcache
, &sprs64
.pt_iar
, &sprs64
.pt_msr
,
1601 &sprs64
.pt_cr
, &sprs64
.pt_lr
, &sprs64
.pt_ctr
,
1602 &sprs64
.pt_xer
, &sprs64
.pt_fpscr
);
1603 ptrace64aix (PTT_WRITE_SPRS
, tid
,
1604 (unsigned long) &sprs64
, 0, NULL
);
1608 /* The contents of "struct ptspr" were declared as "unsigned
1609 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1610 Use temporaries to work around this problem. Also, add an
1611 assert here to make sure we fail if the system header files
1612 use "unsigned long", and the size of that type is not what
1613 the headers expect. */
1614 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1617 gdb_assert (sizeof (sprs32
.pt_iar
) == 4);
1619 /* Pre-fetch: some registers won't be in the cache. */
1620 ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1622 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
,
1623 &tmp_ctr
, &tmp_xer
, &tmp_fpscr
);
1625 sprs32
.pt_iar
= tmp_iar
;
1626 sprs32
.pt_msr
= tmp_msr
;
1627 sprs32
.pt_cr
= tmp_cr
;
1628 sprs32
.pt_lr
= tmp_lr
;
1629 sprs32
.pt_ctr
= tmp_ctr
;
1630 sprs32
.pt_xer
= tmp_xer
;
1631 sprs32
.pt_fpscr
= tmp_fpscr
;
1633 if (tdep
->ppc_mq_regnum
>= 0)
1634 if (REG_VALID
== regcache_register_status (regcache
,
1635 tdep
->ppc_mq_regnum
))
1636 regcache_raw_collect (regcache
, tdep
->ppc_mq_regnum
,
1639 ptrace32 (PTT_WRITE_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1644 /* Store gdb's current view of the register set into the
1645 thread/process specified by inferior_ptid. */
1648 aix_thread_store_registers (struct target_ops
*ops
,
1649 struct regcache
*regcache
, int regno
)
1651 struct thread_info
*thread
;
1653 struct target_ops
*beneath
= find_target_beneath (ops
);
1655 if (!PD_TID (inferior_ptid
))
1656 beneath
->to_store_registers (beneath
, regcache
, regno
);
1659 thread
= find_thread_ptid (inferior_ptid
);
1660 tid
= thread
->private->tid
;
1662 if (tid
== PTHDB_INVALID_TID
)
1663 store_regs_user_thread (regcache
, thread
->private->pdtid
);
1665 store_regs_kernel_thread (regcache
, regno
, tid
);
1669 /* Attempt a transfer all LEN bytes starting at OFFSET between the
1670 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
1671 Return the number of bytes actually transferred. */
1674 aix_thread_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1675 const char *annex
, gdb_byte
*readbuf
,
1676 const gdb_byte
*writebuf
,
1677 ULONGEST offset
, LONGEST len
)
1679 struct cleanup
*old_chain
= save_inferior_ptid ();
1681 struct target_ops
*beneath
= find_target_beneath (ops
);
1683 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1684 xfer
= beneath
->to_xfer_partial (beneath
, object
, annex
,
1685 readbuf
, writebuf
, offset
, len
);
1687 do_cleanups (old_chain
);
1691 /* Clean up after the inferior exits. */
1694 aix_thread_mourn_inferior (struct target_ops
*ops
)
1696 struct target_ops
*beneath
= find_target_beneath (ops
);
1699 beneath
->to_mourn_inferior (beneath
);
1702 /* Return whether thread PID is still valid. */
1705 aix_thread_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1707 struct target_ops
*beneath
= find_target_beneath (ops
);
1710 return beneath
->to_thread_alive (beneath
, ptid
);
1712 /* We update the thread list every time the child stops, so all
1713 valid threads should be in the thread list. */
1714 return in_thread_list (ptid
);
1717 /* Return a printable representation of composite PID for use in
1718 "info threads" output. */
1721 aix_thread_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1723 static char *ret
= NULL
;
1724 struct target_ops
*beneath
= find_target_beneath (ops
);
1727 return beneath
->to_pid_to_str (beneath
, ptid
);
1729 /* Free previous return value; a new one will be allocated by
1733 ret
= xstrprintf (_("Thread %ld"), ptid_get_tid (ptid
));
1737 /* Return a printable representation of extra information about
1738 THREAD, for use in "info threads" output. */
1741 aix_thread_extra_thread_info (struct thread_info
*thread
)
1743 struct ui_file
*buf
;
1745 pthdb_pthread_t pdtid
;
1747 pthdb_state_t state
;
1748 pthdb_suspendstate_t suspendstate
;
1749 pthdb_detachstate_t detachstate
;
1751 static char *ret
= NULL
;
1753 if (!PD_TID (thread
->ptid
))
1756 buf
= mem_fileopen ();
1758 pdtid
= thread
->private->pdtid
;
1759 tid
= thread
->private->tid
;
1761 if (tid
!= PTHDB_INVALID_TID
)
1762 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1763 fprintf_unfiltered (buf
, _("tid %d"), (int)tid
);
1765 status
= pthdb_pthread_state (pd_session
, pdtid
, &state
);
1766 if (status
!= PTHDB_SUCCESS
)
1768 fprintf_unfiltered (buf
, ", %s", state2str (state
));
1770 status
= pthdb_pthread_suspendstate (pd_session
, pdtid
,
1772 if (status
== PTHDB_SUCCESS
&& suspendstate
== PSS_SUSPENDED
)
1773 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1774 fprintf_unfiltered (buf
, _(", suspended"));
1776 status
= pthdb_pthread_detachstate (pd_session
, pdtid
,
1778 if (status
== PTHDB_SUCCESS
&& detachstate
== PDS_DETACHED
)
1779 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1780 fprintf_unfiltered (buf
, _(", detached"));
1782 pthdb_pthread_cancelpend (pd_session
, pdtid
, &cancelpend
);
1783 if (status
== PTHDB_SUCCESS
&& cancelpend
)
1784 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1785 fprintf_unfiltered (buf
, _(", cancel pending"));
1787 ui_file_write (buf
, "", 1);
1789 xfree (ret
); /* Free old buffer. */
1791 ret
= ui_file_xstrdup (buf
, NULL
);
1792 ui_file_delete (buf
);
1798 aix_thread_get_ada_task_ptid (long lwp
, long thread
)
1800 return ptid_build (ptid_get_pid (inferior_ptid
), 0, thread
);
1803 /* Initialize target aix_thread_ops. */
1806 init_aix_thread_ops (void)
1808 aix_thread_ops
.to_shortname
= "aix-threads";
1809 aix_thread_ops
.to_longname
= _("AIX pthread support");
1810 aix_thread_ops
.to_doc
= _("AIX pthread support");
1812 aix_thread_ops
.to_attach
= aix_thread_attach
;
1813 aix_thread_ops
.to_detach
= aix_thread_detach
;
1814 aix_thread_ops
.to_resume
= aix_thread_resume
;
1815 aix_thread_ops
.to_wait
= aix_thread_wait
;
1816 aix_thread_ops
.to_fetch_registers
= aix_thread_fetch_registers
;
1817 aix_thread_ops
.to_store_registers
= aix_thread_store_registers
;
1818 aix_thread_ops
.to_xfer_partial
= aix_thread_xfer_partial
;
1819 /* No need for aix_thread_ops.to_create_inferior, because we activate thread
1820 debugging when the inferior reaches pd_brk_addr. */
1821 aix_thread_ops
.to_mourn_inferior
= aix_thread_mourn_inferior
;
1822 aix_thread_ops
.to_thread_alive
= aix_thread_thread_alive
;
1823 aix_thread_ops
.to_pid_to_str
= aix_thread_pid_to_str
;
1824 aix_thread_ops
.to_extra_thread_info
= aix_thread_extra_thread_info
;
1825 aix_thread_ops
.to_get_ada_task_ptid
= aix_thread_get_ada_task_ptid
;
1826 aix_thread_ops
.to_stratum
= thread_stratum
;
1827 aix_thread_ops
.to_magic
= OPS_MAGIC
;
1830 /* Module startup initialization function, automagically called by
1833 void _initialize_aix_thread (void);
1836 _initialize_aix_thread (void)
1838 init_aix_thread_ops ();
1839 add_target (&aix_thread_ops
);
1841 /* Notice when object files get loaded and unloaded. */
1842 observer_attach_new_objfile (new_objfile
);
1844 add_setshow_boolean_cmd ("aix-thread", class_maintenance
, &debug_aix_thread
,
1845 _("Set debugging of AIX thread module."),
1846 _("Show debugging of AIX thread module."),
1847 _("Enables debugging output (used to debug GDB)."),
1849 /* FIXME: i18n: Debugging of AIX thread
1850 module is \"%d\". */
1851 &setdebuglist
, &showdebuglist
);