1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999, 2000, 2002, 2007, 2008, 2009
4 Free Software Foundation, Inc.
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 /* Whether to emit debugging output. */
62 static int debug_aix_thread
;
64 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
65 #ifndef PTHDB_VERSION_3
66 #define pthdb_tid_t tid_t
69 /* Return whether to treat PID as a debuggable thread id. */
71 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
73 /* Build a thread ptid. */
74 #define BUILD_THREAD(TID, PID) ptid_build (PID, 0, TID)
76 /* Build and lwp ptid. */
77 #define BUILD_LWP(LWP, PID) MERGEPID (PID, LWP)
79 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
80 PTHDB_BAD_USER errors, so use 1. */
84 /* Success and failure values returned by pthdb callbacks. */
86 #define PDC_SUCCESS PTHDB_SUCCESS
87 #define PDC_FAILURE PTHDB_CALLBACK
89 /* Private data attached to each element in GDB's thread list. */
91 struct private_thread_info
{
92 pthdb_pthread_t pdtid
; /* thread's libpthdebug id */
93 pthdb_tid_t tid
; /* kernel thread id */
96 /* Information about a thread of which libpthdebug is aware. */
99 pthdb_pthread_t pdtid
;
104 /* This module's target-specific operations, active while pd_able is true. */
106 static struct target_ops aix_thread_ops
;
108 /* Copy of the target over which ops is pushed. This is more
109 convenient than a pointer to deprecated_child_ops or core_ops,
110 because they lack current_target's default callbacks. */
112 static struct target_ops base_target
;
114 /* Address of the function that libpthread will call when libpthdebug
115 is ready to be initialized. */
117 static CORE_ADDR pd_brk_addr
;
119 /* Whether the current application is debuggable by pthdb. */
121 static int pd_able
= 0;
123 /* Whether a threaded application is being debugged. */
125 static int pd_active
= 0;
127 /* Whether the current architecture is 64-bit.
128 Only valid when pd_able is true. */
132 /* Forward declarations for pthdb callbacks. */
134 static int pdc_symbol_addrs (pthdb_user_t
, pthdb_symbol_t
*, int);
135 static int pdc_read_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
136 static int pdc_write_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
137 static int pdc_read_regs (pthdb_user_t user
, pthdb_tid_t tid
,
138 unsigned long long flags
,
139 pthdb_context_t
*context
);
140 static int pdc_write_regs (pthdb_user_t user
, pthdb_tid_t tid
,
141 unsigned long long flags
,
142 pthdb_context_t
*context
);
143 static int pdc_alloc (pthdb_user_t
, size_t, void **);
144 static int pdc_realloc (pthdb_user_t
, void *, size_t, void **);
145 static int pdc_dealloc (pthdb_user_t
, void *);
147 /* pthdb callbacks. */
149 static pthdb_callbacks_t pd_callbacks
= {
161 /* Current pthdb session. */
163 static pthdb_session_t pd_session
;
165 /* Return a printable representation of pthdebug function return
169 pd_status2str (int status
)
173 case PTHDB_SUCCESS
: return "SUCCESS";
174 case PTHDB_NOSYS
: return "NOSYS";
175 case PTHDB_NOTSUP
: return "NOTSUP";
176 case PTHDB_BAD_VERSION
: return "BAD_VERSION";
177 case PTHDB_BAD_USER
: return "BAD_USER";
178 case PTHDB_BAD_SESSION
: return "BAD_SESSION";
179 case PTHDB_BAD_MODE
: return "BAD_MODE";
180 case PTHDB_BAD_FLAGS
: return "BAD_FLAGS";
181 case PTHDB_BAD_CALLBACK
: return "BAD_CALLBACK";
182 case PTHDB_BAD_POINTER
: return "BAD_POINTER";
183 case PTHDB_BAD_CMD
: return "BAD_CMD";
184 case PTHDB_BAD_PTHREAD
: return "BAD_PTHREAD";
185 case PTHDB_BAD_ATTR
: return "BAD_ATTR";
186 case PTHDB_BAD_MUTEX
: return "BAD_MUTEX";
187 case PTHDB_BAD_MUTEXATTR
: return "BAD_MUTEXATTR";
188 case PTHDB_BAD_COND
: return "BAD_COND";
189 case PTHDB_BAD_CONDATTR
: return "BAD_CONDATTR";
190 case PTHDB_BAD_RWLOCK
: return "BAD_RWLOCK";
191 case PTHDB_BAD_RWLOCKATTR
: return "BAD_RWLOCKATTR";
192 case PTHDB_BAD_KEY
: return "BAD_KEY";
193 case PTHDB_BAD_PTID
: return "BAD_PTID";
194 case PTHDB_BAD_TID
: return "BAD_TID";
195 case PTHDB_CALLBACK
: return "CALLBACK";
196 case PTHDB_CONTEXT
: return "CONTEXT";
197 case PTHDB_HELD
: return "HELD";
198 case PTHDB_NOT_HELD
: return "NOT_HELD";
199 case PTHDB_MEMORY
: return "MEMORY";
200 case PTHDB_NOT_PTHREADED
: return "NOT_PTHREADED";
201 case PTHDB_SYMBOL
: return "SYMBOL";
202 case PTHDB_NOT_AVAIL
: return "NOT_AVAIL";
203 case PTHDB_INTERNAL
: return "INTERNAL";
204 default: return "UNKNOWN";
208 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
209 exceptional conditions and either return nonlocally or else return
210 1 for success and 0 for failure. */
213 ptrace_check (int req
, int id
, int ret
)
215 if (ret
== 0 && !errno
)
218 /* According to ptrace(2), ptrace may fail with EPERM if "the
219 Identifier parameter corresponds to a kernel thread which is
220 stopped in kernel mode and whose computational state cannot be
221 read or written." This happens quite often with register reads. */
228 if (ret
== -1 && errno
== EPERM
)
230 if (debug_aix_thread
)
231 fprintf_unfiltered (gdb_stdlog
,
232 "ptrace (%d, %d) = %d (errno = %d)\n",
233 req
, id
, ret
, errno
);
234 return ret
== -1 ? 0 : 1;
238 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
239 req
, id
, ret
, errno
, safe_strerror (errno
));
240 return 0; /* Not reached. */
243 /* Call ptracex (REQ, ID, ADDR, DATA, BUF). Return success. */
246 ptrace64aix (int req
, int id
, long long addr
, int data
, int *buf
)
249 return ptrace_check (req
, id
, ptracex (req
, id
, addr
, data
, buf
));
252 /* Call ptrace (REQ, ID, ADDR, DATA, BUF). Return success. */
255 ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
258 return ptrace_check (req
, id
,
259 ptrace (req
, id
, (int *) addr
, data
, buf
));
262 /* If *PIDP is a composite process/thread id, convert it to a
266 pid_to_prc (ptid_t
*ptidp
)
272 *ptidp
= pid_to_ptid (PIDGET (ptid
));
275 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
276 the address of SYMBOLS[<i>].name. */
279 pdc_symbol_addrs (pthdb_user_t user
, pthdb_symbol_t
*symbols
, int count
)
281 struct minimal_symbol
*ms
;
285 if (debug_aix_thread
)
286 fprintf_unfiltered (gdb_stdlog
,
287 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
288 user
, (long) symbols
, count
);
290 for (i
= 0; i
< count
; i
++)
292 name
= symbols
[i
].name
;
293 if (debug_aix_thread
)
294 fprintf_unfiltered (gdb_stdlog
,
295 " symbols[%d].name = \"%s\"\n", i
, name
);
301 if (!(ms
= lookup_minimal_symbol (name
, NULL
, NULL
)))
303 if (debug_aix_thread
)
304 fprintf_unfiltered (gdb_stdlog
, " returning PDC_FAILURE\n");
307 symbols
[i
].addr
= SYMBOL_VALUE_ADDRESS (ms
);
309 if (debug_aix_thread
)
310 fprintf_unfiltered (gdb_stdlog
, " symbols[%d].addr = %s\n",
311 i
, hex_string (symbols
[i
].addr
));
313 if (debug_aix_thread
)
314 fprintf_unfiltered (gdb_stdlog
, " returning PDC_SUCCESS\n");
318 /* Read registers call back function should be able to read the
319 context information of a debuggee kernel thread from an active
320 process or from a core file. The information should be formatted
321 in context64 form for both 32-bit and 64-bit process.
322 If successful return 0, else non-zero is returned. */
325 pdc_read_regs (pthdb_user_t user
,
327 unsigned long long flags
,
328 pthdb_context_t
*context
)
330 /* This function doesn't appear to be used, so we could probably
331 just return 0 here. HOWEVER, if it is not defined, the OS will
332 complain and several thread debug functions will fail. In case
333 this is needed, I have implemented what I think it should do,
334 however this code is untested. */
336 uint64_t gprs64
[ppc_num_gprs
];
337 uint32_t gprs32
[ppc_num_gprs
];
338 double fprs
[ppc_num_fprs
];
339 struct ptxsprs sprs64
;
340 struct ptsprs sprs32
;
342 if (debug_aix_thread
)
343 fprintf_unfiltered (gdb_stdlog
, "pdc_read_regs tid=%d flags=%s\n",
344 (int) tid
, hex_string (flags
));
346 /* General-purpose registers. */
347 if (flags
& PTHDB_FLAG_GPRS
)
351 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
352 (unsigned long) gprs64
, 0, NULL
))
353 memset (gprs64
, 0, sizeof (gprs64
));
354 memcpy (context
->gpr
, gprs64
, sizeof(gprs64
));
358 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
359 memset (gprs32
, 0, sizeof (gprs32
));
360 memcpy (context
->gpr
, gprs32
, sizeof(gprs32
));
364 /* Floating-point registers. */
365 if (flags
& PTHDB_FLAG_FPRS
)
367 if (!ptrace32 (PTT_READ_FPRS
, tid
, (void *) fprs
, 0, NULL
))
368 memset (fprs
, 0, sizeof (fprs
));
369 memcpy (context
->fpr
, fprs
, sizeof(fprs
));
372 /* Special-purpose registers. */
373 if (flags
& PTHDB_FLAG_SPRS
)
377 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
378 (unsigned long) &sprs64
, 0, NULL
))
379 memset (&sprs64
, 0, sizeof (sprs64
));
380 memcpy (&context
->msr
, &sprs64
, sizeof(sprs64
));
384 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
385 memset (&sprs32
, 0, sizeof (sprs32
));
386 memcpy (&context
->msr
, &sprs32
, sizeof(sprs32
));
392 /* Write register function should be able to write requested context
393 information to specified debuggee's kernel thread id.
394 If successful return 0, else non-zero is returned. */
397 pdc_write_regs (pthdb_user_t user
,
399 unsigned long long flags
,
400 pthdb_context_t
*context
)
402 /* This function doesn't appear to be used, so we could probably
403 just return 0 here. HOWEVER, if it is not defined, the OS will
404 complain and several thread debug functions will fail. In case
405 this is needed, I have implemented what I think it should do,
406 however this code is untested. */
408 if (debug_aix_thread
)
409 fprintf_unfiltered (gdb_stdlog
, "pdc_write_regs tid=%d flags=%s\n",
410 (int) tid
, hex_string (flags
));
412 /* General-purpose registers. */
413 if (flags
& PTHDB_FLAG_GPRS
)
416 ptrace64aix (PTT_WRITE_GPRS
, tid
,
417 (unsigned long) context
->gpr
, 0, NULL
);
419 ptrace32 (PTT_WRITE_GPRS
, tid
, (int *) context
->gpr
, 0, NULL
);
422 /* Floating-point registers. */
423 if (flags
& PTHDB_FLAG_FPRS
)
425 ptrace32 (PTT_WRITE_FPRS
, tid
, (int *) context
->fpr
, 0, NULL
);
428 /* Special-purpose registers. */
429 if (flags
& PTHDB_FLAG_SPRS
)
433 ptrace64aix (PTT_WRITE_SPRS
, tid
,
434 (unsigned long) &context
->msr
, 0, NULL
);
438 ptrace32 (PTT_WRITE_SPRS
, tid
, (void *) &context
->msr
, 0, NULL
);
444 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
447 pdc_read_data (pthdb_user_t user
, void *buf
,
448 pthdb_addr_t addr
, size_t len
)
452 if (debug_aix_thread
)
453 fprintf_unfiltered (gdb_stdlog
,
454 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
455 user
, (long) buf
, hex_string (addr
), len
);
457 status
= target_read_memory (addr
, buf
, len
);
458 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
460 if (debug_aix_thread
)
461 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n",
462 status
, pd_status2str (ret
));
466 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
469 pdc_write_data (pthdb_user_t user
, void *buf
,
470 pthdb_addr_t addr
, size_t len
)
474 if (debug_aix_thread
)
475 fprintf_unfiltered (gdb_stdlog
,
476 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
477 user
, (long) buf
, hex_string (addr
), len
);
479 status
= target_write_memory (addr
, buf
, len
);
480 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
482 if (debug_aix_thread
)
483 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n", status
,
484 pd_status2str (ret
));
488 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
492 pdc_alloc (pthdb_user_t user
, size_t len
, void **bufp
)
494 if (debug_aix_thread
)
495 fprintf_unfiltered (gdb_stdlog
,
496 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
497 user
, len
, (long) bufp
);
498 *bufp
= xmalloc (len
);
499 if (debug_aix_thread
)
500 fprintf_unfiltered (gdb_stdlog
,
501 " malloc returned 0x%lx\n", (long) *bufp
);
503 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
506 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
509 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
510 realloc callback, so that it contains LEN bytes, and store a
511 pointer to the result in BUFP. */
514 pdc_realloc (pthdb_user_t user
, void *buf
, size_t len
, void **bufp
)
516 if (debug_aix_thread
)
517 fprintf_unfiltered (gdb_stdlog
,
518 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
519 user
, (long) buf
, len
, (long) bufp
);
520 *bufp
= xrealloc (buf
, len
);
521 if (debug_aix_thread
)
522 fprintf_unfiltered (gdb_stdlog
,
523 " realloc returned 0x%lx\n", (long) *bufp
);
524 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
527 /* pthdb callback: free BUF, which was allocated by the alloc or
531 pdc_dealloc (pthdb_user_t user
, void *buf
)
533 if (debug_aix_thread
)
534 fprintf_unfiltered (gdb_stdlog
,
535 "pdc_free (user = %ld, buf = 0x%lx)\n", user
,
541 /* Return a printable representation of pthread STATE. */
544 state2str (pthdb_state_t state
)
549 /* i18n: Like "Thread-Id %d, [state] idle" */
550 return _("idle"); /* being created */
552 /* i18n: Like "Thread-Id %d, [state] running" */
553 return _("running"); /* running */
555 /* i18n: Like "Thread-Id %d, [state] sleeping" */
556 return _("sleeping"); /* awaiting an event */
558 /* i18n: Like "Thread-Id %d, [state] ready" */
559 return _("ready"); /* runnable */
561 /* i18n: Like "Thread-Id %d, [state] finished" */
562 return _("finished"); /* awaiting a join/detach */
564 /* i18n: Like "Thread-Id %d, [state] unknown" */
569 /* qsort() comparison function for sorting pd_thread structs by pthid. */
572 pcmp (const void *p1v
, const void *p2v
)
574 struct pd_thread
*p1
= (struct pd_thread
*) p1v
;
575 struct pd_thread
*p2
= (struct pd_thread
*) p2v
;
576 return p1
->pthid
< p2
->pthid
? -1 : p1
->pthid
> p2
->pthid
;
579 /* iterate_over_threads() callback for counting GDB threads. */
582 giter_count (struct thread_info
*thread
, void *countp
)
588 /* iterate_over_threads() callback for accumulating GDB thread pids. */
591 giter_accum (struct thread_info
*thread
, void *bufp
)
593 **(struct thread_info
***) bufp
= thread
;
594 (*(struct thread_info
***) bufp
)++;
598 /* ptid comparison function */
601 ptid_cmp (ptid_t ptid1
, ptid_t ptid2
)
605 if (ptid_get_pid (ptid1
) < ptid_get_pid (ptid2
))
607 else if (ptid_get_pid (ptid1
) > ptid_get_pid (ptid2
))
609 else if (ptid_get_tid (ptid1
) < ptid_get_tid (ptid2
))
611 else if (ptid_get_tid (ptid1
) > ptid_get_tid (ptid2
))
613 else if (ptid_get_lwp (ptid1
) < ptid_get_lwp (ptid2
))
615 else if (ptid_get_lwp (ptid1
) > ptid_get_lwp (ptid2
))
621 /* qsort() comparison function for sorting thread_info structs by pid. */
624 gcmp (const void *t1v
, const void *t2v
)
626 struct thread_info
*t1
= *(struct thread_info
**) t1v
;
627 struct thread_info
*t2
= *(struct thread_info
**) t2v
;
628 return ptid_cmp (t1
->ptid
, t2
->ptid
);
631 /* Search through the list of all kernel threads for the thread
632 that has stopped on a SIGTRAP signal, and return its TID.
633 Return 0 if none found. */
636 get_signaled_thread (void)
638 struct thrdsinfo64 thrinf
;
639 pthdb_tid_t ktid
= 0;
642 /* getthrds(3) isn't prototyped in any AIX 4.3.3 #include file. */
643 extern int getthrds (pid_t
, struct thrdsinfo64
*,
644 int, pthdb_tid_t
*, int);
648 if (getthrds (PIDGET (inferior_ptid
), &thrinf
,
649 sizeof (thrinf
), &ktid
, 1) != 1)
652 if (thrinf
.ti_cursig
== SIGTRAP
)
653 return thrinf
.ti_tid
;
656 /* Didn't find any thread stopped on a SIGTRAP signal. */
660 /* Synchronize GDB's thread list with libpthdebug's.
662 There are some benefits of doing this every time the inferior stops:
664 - allows users to run thread-specific commands without needing to
665 run "info threads" first
667 - helps pthdb_tid_pthread() work properly (see "libpthdebug
668 peculiarities" at the top of this module)
670 - simplifies the demands placed on libpthdebug, which seems to
671 have difficulty with certain call patterns */
674 sync_threadlists (void)
676 int cmd
, status
, infpid
;
677 int pcount
, psize
, pi
, gcount
, gi
;
678 struct pd_thread
*pbuf
;
679 struct thread_info
**gbuf
, **g
, *thread
;
680 pthdb_pthread_t pdtid
;
684 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
688 pbuf
= (struct pd_thread
*) xmalloc (psize
* sizeof *pbuf
);
690 for (cmd
= PTHDB_LIST_FIRST
;; cmd
= PTHDB_LIST_NEXT
)
692 status
= pthdb_pthread (pd_session
, &pdtid
, cmd
);
693 if (status
!= PTHDB_SUCCESS
|| pdtid
== PTHDB_INVALID_PTHREAD
)
696 status
= pthdb_pthread_ptid (pd_session
, pdtid
, &pthid
);
697 if (status
!= PTHDB_SUCCESS
|| pthid
== PTHDB_INVALID_PTID
)
703 pbuf
= (struct pd_thread
*) xrealloc (pbuf
,
704 psize
* sizeof *pbuf
);
706 pbuf
[pcount
].pdtid
= pdtid
;
707 pbuf
[pcount
].pthid
= pthid
;
711 for (pi
= 0; pi
< pcount
; pi
++)
713 status
= pthdb_pthread_tid (pd_session
, pbuf
[pi
].pdtid
, &tid
);
714 if (status
!= PTHDB_SUCCESS
)
715 tid
= PTHDB_INVALID_TID
;
719 qsort (pbuf
, pcount
, sizeof *pbuf
, pcmp
);
721 /* Accumulate an array of GDB threads sorted by pid. */
724 iterate_over_threads (giter_count
, &gcount
);
725 g
= gbuf
= (struct thread_info
**) xmalloc (gcount
* sizeof *gbuf
);
726 iterate_over_threads (giter_accum
, &g
);
727 qsort (gbuf
, gcount
, sizeof *gbuf
, gcmp
);
729 /* Apply differences between the two arrays to GDB's thread list. */
731 infpid
= PIDGET (inferior_ptid
);
732 for (pi
= gi
= 0; pi
< pcount
|| gi
< gcount
;)
736 delete_thread (gbuf
[gi
]->ptid
);
739 else if (gi
== gcount
)
741 thread
= add_thread (BUILD_THREAD (pbuf
[pi
].pthid
, infpid
));
742 thread
->private = xmalloc (sizeof (struct private_thread_info
));
743 thread
->private->pdtid
= pbuf
[pi
].pdtid
;
744 thread
->private->tid
= pbuf
[pi
].tid
;
752 pptid
= BUILD_THREAD (pbuf
[pi
].pthid
, infpid
);
753 gptid
= gbuf
[gi
]->ptid
;
754 pdtid
= pbuf
[pi
].pdtid
;
757 cmp_result
= ptid_cmp (pptid
, gptid
);
761 gbuf
[gi
]->private->pdtid
= pdtid
;
762 gbuf
[gi
]->private->tid
= tid
;
766 else if (cmp_result
> 0)
768 delete_thread (gptid
);
773 thread
= add_thread (pptid
);
774 thread
->private = xmalloc (sizeof (struct private_thread_info
));
775 thread
->private->pdtid
= pdtid
;
776 thread
->private->tid
= tid
;
786 /* Iterate_over_threads() callback for locating a thread, using
787 the TID of its associated kernel thread. */
790 iter_tid (struct thread_info
*thread
, void *tidp
)
792 const pthdb_tid_t tid
= *(pthdb_tid_t
*)tidp
;
794 return (thread
->private->tid
== tid
);
797 /* Synchronize libpthdebug's state with the inferior and with GDB,
798 generate a composite process/thread <pid> for the current thread,
799 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
802 pd_update (int set_infpid
)
807 struct thread_info
*thread
= NULL
;
810 return inferior_ptid
;
812 status
= pthdb_session_update (pd_session
);
813 if (status
!= PTHDB_SUCCESS
)
814 return inferior_ptid
;
818 /* Define "current thread" as one that just received a trap signal. */
820 tid
= get_signaled_thread ();
822 thread
= iterate_over_threads (iter_tid
, &tid
);
824 ptid
= inferior_ptid
;
829 inferior_ptid
= ptid
;
834 /* Try to start debugging threads in the current process.
835 If successful and SET_INFPID, set inferior_ptid to reflect the
839 pd_activate (int set_infpid
)
843 status
= pthdb_session_init (PD_USER
, arch64
? PEM_64BIT
: PEM_32BIT
,
844 PTHDB_FLAG_REGS
, &pd_callbacks
,
846 if (status
!= PTHDB_SUCCESS
)
848 return inferior_ptid
;
851 return pd_update (set_infpid
);
854 /* Undo the effects of pd_activate(). */
861 pthdb_session_destroy (pd_session
);
863 pid_to_prc (&inferior_ptid
);
867 /* An object file has just been loaded. Check whether the current
868 application is pthreaded, and if so, prepare for thread debugging. */
875 struct minimal_symbol
*ms
;
877 /* Don't initialize twice. */
881 /* Check application word size. */
882 arch64
= register_size (current_gdbarch
, 0) == 8;
884 /* Check whether the application is pthreaded. */
886 status
= pthdb_session_pthreaded (PD_USER
, PTHDB_FLAG_REGS
,
887 &pd_callbacks
, &stub_name
);
888 if ((status
!= PTHDB_SUCCESS
&&
889 status
!= PTHDB_NOT_PTHREADED
) || !stub_name
)
892 /* Set a breakpoint on the returned stub function. */
893 if (!(ms
= lookup_minimal_symbol (stub_name
, NULL
, NULL
)))
895 pd_brk_addr
= SYMBOL_VALUE_ADDRESS (ms
);
896 if (!create_thread_event_breakpoint (pd_brk_addr
))
899 /* Prepare for thread debugging. */
900 base_target
= current_target
;
901 push_target (&aix_thread_ops
);
904 /* If we're debugging a core file or an attached inferior, the
905 pthread library may already have been initialized, so try to
906 activate thread debugging. */
910 /* Undo the effects of pd_enable(). */
920 unpush_target (&aix_thread_ops
);
923 /* new_objfile observer callback.
925 If OBJFILE is non-null, check whether a threaded application is
926 being debugged, and if so, prepare for thread debugging.
928 If OBJFILE is null, stop debugging threads. */
931 new_objfile (struct objfile
*objfile
)
939 /* Attach to process specified by ARGS. */
942 aix_thread_attach (struct target_ops
*ops
, char *args
, int from_tty
)
944 base_target
.to_attach (&base_target
, args
, from_tty
);
948 /* Detach from the process attached to by aix_thread_attach(). */
951 aix_thread_detach (struct target_ops
*ops
, char *args
, int from_tty
)
954 base_target
.to_detach (&base_target
, args
, from_tty
);
957 /* Tell the inferior process to continue running thread PID if != -1
958 and all threads otherwise. */
961 aix_thread_resume (ptid_t ptid
, int step
, enum target_signal sig
)
963 struct thread_info
*thread
;
968 struct cleanup
*cleanup
= save_inferior_ptid ();
969 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
970 base_target
.to_resume (ptid
, step
, sig
);
971 do_cleanups (cleanup
);
975 thread
= find_thread_pid (ptid
);
977 error (_("aix-thread resume: unknown pthread %ld"),
980 tid
[0] = thread
->private->tid
;
981 if (tid
[0] == PTHDB_INVALID_TID
)
982 error (_("aix-thread resume: no tid for pthread %ld"),
987 ptrace64aix (PTT_CONTINUE
, tid
[0], 1,
988 target_signal_to_host (sig
), (void *) tid
);
990 ptrace32 (PTT_CONTINUE
, tid
[0], (int *) 1,
991 target_signal_to_host (sig
), (void *) tid
);
995 /* Wait for thread/process ID if != -1 or for any thread otherwise.
996 If an error occurs, return -1, else return the pid of the stopped
1000 aix_thread_wait (struct target_ops
*ops
,
1001 ptid_t ptid
, struct target_waitstatus
*status
)
1003 struct cleanup
*cleanup
= save_inferior_ptid ();
1007 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1008 ptid
= base_target
.to_wait (&base_target
, ptid
, status
);
1009 do_cleanups (cleanup
);
1011 if (PIDGET (ptid
) == -1)
1012 return pid_to_ptid (-1);
1014 /* Check whether libpthdebug might be ready to be initialized. */
1015 if (!pd_active
&& status
->kind
== TARGET_WAITKIND_STOPPED
1016 && status
->value
.sig
== TARGET_SIGNAL_TRAP
)
1018 struct regcache
*regcache
= get_thread_regcache (ptid
);
1019 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1021 if (regcache_read_pc (regcache
)
1022 - gdbarch_decr_pc_after_break (gdbarch
) == pd_brk_addr
)
1023 return pd_activate (0);
1026 return pd_update (0);
1029 /* Record that the 64-bit general-purpose registers contain VALS. */
1032 supply_gprs64 (struct regcache
*regcache
, uint64_t *vals
)
1034 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
1037 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1038 regcache_raw_supply (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1039 (char *) (vals
+ regno
));
1042 /* Record that 32-bit register REGNO contains VAL. */
1045 supply_reg32 (struct regcache
*regcache
, int regno
, uint32_t val
)
1047 regcache_raw_supply (regcache
, regno
, (char *) &val
);
1050 /* Record that the floating-point registers contain VALS. */
1053 supply_fprs (struct regcache
*regcache
, double *vals
)
1055 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1056 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1059 /* This function should never be called on architectures without
1060 floating-point registers. */
1061 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1063 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
1064 regcache_raw_supply (regcache
, regno
+ tdep
->ppc_fp0_regnum
,
1065 (char *) (vals
+ regno
));
1068 /* Predicate to test whether given register number is a "special" register. */
1070 special_register_p (struct gdbarch
*gdbarch
, int regno
)
1072 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1074 return regno
== gdbarch_pc_regnum (gdbarch
)
1075 || regno
== tdep
->ppc_ps_regnum
1076 || regno
== tdep
->ppc_cr_regnum
1077 || regno
== tdep
->ppc_lr_regnum
1078 || regno
== tdep
->ppc_ctr_regnum
1079 || regno
== tdep
->ppc_xer_regnum
1080 || (tdep
->ppc_fpscr_regnum
>= 0 && regno
== tdep
->ppc_fpscr_regnum
)
1081 || (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
);
1085 /* Record that the special registers contain the specified 64-bit and
1089 supply_sprs64 (struct regcache
*regcache
,
1090 uint64_t iar
, uint64_t msr
, uint32_t cr
,
1091 uint64_t lr
, uint64_t ctr
, uint32_t xer
,
1094 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1095 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1097 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
),
1099 regcache_raw_supply (regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1100 regcache_raw_supply (regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1101 regcache_raw_supply (regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1102 regcache_raw_supply (regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1103 regcache_raw_supply (regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1104 if (tdep
->ppc_fpscr_regnum
>= 0)
1105 regcache_raw_supply (regcache
, tdep
->ppc_fpscr_regnum
,
1109 /* Record that the special registers contain the specified 32-bit
1113 supply_sprs32 (struct regcache
*regcache
,
1114 uint32_t iar
, uint32_t msr
, uint32_t cr
,
1115 uint32_t lr
, uint32_t ctr
, uint32_t xer
,
1118 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1119 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1121 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
),
1123 regcache_raw_supply (regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1124 regcache_raw_supply (regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1125 regcache_raw_supply (regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1126 regcache_raw_supply (regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1127 regcache_raw_supply (regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1128 if (tdep
->ppc_fpscr_regnum
>= 0)
1129 regcache_raw_supply (regcache
, tdep
->ppc_fpscr_regnum
,
1133 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1136 There's no way to query a single register from a non-kernel
1137 pthread, so there's no need for a single-register version of this
1141 fetch_regs_user_thread (struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1143 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1144 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1146 pthdb_context_t ctx
;
1148 if (debug_aix_thread
)
1149 fprintf_unfiltered (gdb_stdlog
,
1150 "fetch_regs_user_thread %lx\n", (long) pdtid
);
1151 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1152 if (status
!= PTHDB_SUCCESS
)
1153 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1154 pd_status2str (status
));
1156 /* General-purpose registers. */
1159 supply_gprs64 (regcache
, ctx
.gpr
);
1161 for (i
= 0; i
< ppc_num_gprs
; i
++)
1162 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, ctx
.gpr
[i
]);
1164 /* Floating-point registers. */
1166 if (ppc_floating_point_unit_p (gdbarch
))
1167 supply_fprs (regcache
, ctx
.fpr
);
1169 /* Special registers. */
1172 supply_sprs64 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1173 ctx
.xer
, ctx
.fpscr
);
1175 supply_sprs32 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1176 ctx
.xer
, ctx
.fpscr
);
1179 /* Fetch register REGNO if != -1 or all registers otherwise from
1182 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1183 SPRs, but there's no way to query individual registers within those
1184 groups. Therefore, if REGNO != -1, this function fetches an entire
1187 Unfortunately, kernel thread register queries often fail with
1188 EPERM, indicating that the thread is in kernel space. This breaks
1189 backtraces of threads other than the current one. To make that
1190 breakage obvious without throwing an error to top level (which is
1191 bad e.g. during "info threads" output), zero registers that can't
1195 fetch_regs_kernel_thread (struct regcache
*regcache
, int regno
,
1198 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1199 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1200 uint64_t gprs64
[ppc_num_gprs
];
1201 uint32_t gprs32
[ppc_num_gprs
];
1202 double fprs
[ppc_num_fprs
];
1203 struct ptxsprs sprs64
;
1204 struct ptsprs sprs32
;
1207 if (debug_aix_thread
)
1208 fprintf_unfiltered (gdb_stdlog
,
1209 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1210 (long) tid
, regno
, arch64
);
1212 /* General-purpose registers. */
1214 || (tdep
->ppc_gp0_regnum
<= regno
1215 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
))
1219 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
1220 (unsigned long) gprs64
, 0, NULL
))
1221 memset (gprs64
, 0, sizeof (gprs64
));
1222 supply_gprs64 (regcache
, gprs64
);
1226 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
1227 memset (gprs32
, 0, sizeof (gprs32
));
1228 for (i
= 0; i
< ppc_num_gprs
; i
++)
1229 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, gprs32
[i
]);
1233 /* Floating-point registers. */
1235 if (ppc_floating_point_unit_p (gdbarch
)
1237 || (regno
>= tdep
->ppc_fp0_regnum
1238 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1240 if (!ptrace32 (PTT_READ_FPRS
, tid
, (void *) fprs
, 0, NULL
))
1241 memset (fprs
, 0, sizeof (fprs
));
1242 supply_fprs (regcache
, fprs
);
1245 /* Special-purpose registers. */
1247 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1251 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
1252 (unsigned long) &sprs64
, 0, NULL
))
1253 memset (&sprs64
, 0, sizeof (sprs64
));
1254 supply_sprs64 (regcache
, sprs64
.pt_iar
, sprs64
.pt_msr
,
1255 sprs64
.pt_cr
, sprs64
.pt_lr
, sprs64
.pt_ctr
,
1256 sprs64
.pt_xer
, sprs64
.pt_fpscr
);
1260 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1262 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
1263 memset (&sprs32
, 0, sizeof (sprs32
));
1264 supply_sprs32 (regcache
, sprs32
.pt_iar
, sprs32
.pt_msr
, sprs32
.pt_cr
,
1265 sprs32
.pt_lr
, sprs32
.pt_ctr
, sprs32
.pt_xer
,
1268 if (tdep
->ppc_mq_regnum
>= 0)
1269 regcache_raw_supply (regcache
, tdep
->ppc_mq_regnum
,
1270 (char *) &sprs32
.pt_mq
);
1275 /* Fetch register REGNO if != -1 or all registers otherwise in the
1276 thread/process specified by inferior_ptid. */
1279 aix_thread_fetch_registers (struct regcache
*regcache
, int regno
)
1281 struct thread_info
*thread
;
1284 if (!PD_TID (inferior_ptid
))
1285 base_target
.to_fetch_registers (regcache
, regno
);
1288 thread
= find_thread_pid (inferior_ptid
);
1289 tid
= thread
->private->tid
;
1291 if (tid
== PTHDB_INVALID_TID
)
1292 fetch_regs_user_thread (regcache
, thread
->private->pdtid
);
1294 fetch_regs_kernel_thread (regcache
, regno
, tid
);
1298 /* Store the gp registers into an array of uint32_t or uint64_t. */
1301 fill_gprs64 (const struct regcache
*regcache
, uint64_t *vals
)
1303 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
1306 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1307 if (regcache_valid_p (regcache
, tdep
->ppc_gp0_regnum
+ regno
))
1308 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1313 fill_gprs32 (const struct regcache
*regcache
, uint32_t *vals
)
1315 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
1318 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1319 if (regcache_valid_p (regcache
, tdep
->ppc_gp0_regnum
+ regno
))
1320 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1324 /* Store the floating point registers into a double array. */
1326 fill_fprs (const struct regcache
*regcache
, double *vals
)
1328 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1329 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1332 /* This function should never be called on architectures without
1333 floating-point registers. */
1334 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1336 for (regno
= tdep
->ppc_fp0_regnum
;
1337 regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
;
1339 if (regcache_valid_p (regcache
, regno
))
1340 regcache_raw_collect (regcache
, regno
, vals
+ regno
);
1343 /* Store the special registers into the specified 64-bit and 32-bit
1347 fill_sprs64 (const struct regcache
*regcache
,
1348 uint64_t *iar
, uint64_t *msr
, uint32_t *cr
,
1349 uint64_t *lr
, uint64_t *ctr
, uint32_t *xer
,
1352 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1353 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1355 /* Verify that the size of the size of the IAR buffer is the
1356 same as the raw size of the PC (in the register cache). If
1357 they're not, then either GDB has been built incorrectly, or
1358 there's some other kind of internal error. To be really safe,
1359 we should check all of the sizes. */
1360 gdb_assert (sizeof (*iar
) == register_size
1361 (gdbarch
, gdbarch_pc_regnum (gdbarch
)));
1363 if (regcache_valid_p (regcache
, gdbarch_pc_regnum (gdbarch
)))
1364 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), iar
);
1365 if (regcache_valid_p (regcache
, tdep
->ppc_ps_regnum
))
1366 regcache_raw_collect (regcache
, tdep
->ppc_ps_regnum
, msr
);
1367 if (regcache_valid_p (regcache
, tdep
->ppc_cr_regnum
))
1368 regcache_raw_collect (regcache
, tdep
->ppc_cr_regnum
, cr
);
1369 if (regcache_valid_p (regcache
, tdep
->ppc_lr_regnum
))
1370 regcache_raw_collect (regcache
, tdep
->ppc_lr_regnum
, lr
);
1371 if (regcache_valid_p (regcache
, tdep
->ppc_ctr_regnum
))
1372 regcache_raw_collect (regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1373 if (regcache_valid_p (regcache
, tdep
->ppc_xer_regnum
))
1374 regcache_raw_collect (regcache
, tdep
->ppc_xer_regnum
, xer
);
1375 if (tdep
->ppc_fpscr_regnum
>= 0
1376 && regcache_valid_p (regcache
, tdep
->ppc_fpscr_regnum
))
1377 regcache_raw_collect (regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1381 fill_sprs32 (const struct regcache
*regcache
,
1382 uint32_t *iar
, uint32_t *msr
, uint32_t *cr
,
1383 uint32_t *lr
, uint32_t *ctr
, uint32_t *xer
,
1386 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1387 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1389 /* Verify that the size of the size of the IAR buffer is the
1390 same as the raw size of the PC (in the register cache). If
1391 they're not, then either GDB has been built incorrectly, or
1392 there's some other kind of internal error. To be really safe,
1393 we should check all of the sizes. */
1394 gdb_assert (sizeof (*iar
) == register_size (gdbarch
,
1395 gdbarch_pc_regnum (gdbarch
)));
1397 if (regcache_valid_p (regcache
, gdbarch_pc_regnum (gdbarch
)))
1398 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), iar
);
1399 if (regcache_valid_p (regcache
, tdep
->ppc_ps_regnum
))
1400 regcache_raw_collect (regcache
, tdep
->ppc_ps_regnum
, msr
);
1401 if (regcache_valid_p (regcache
, tdep
->ppc_cr_regnum
))
1402 regcache_raw_collect (regcache
, tdep
->ppc_cr_regnum
, cr
);
1403 if (regcache_valid_p (regcache
, tdep
->ppc_lr_regnum
))
1404 regcache_raw_collect (regcache
, tdep
->ppc_lr_regnum
, lr
);
1405 if (regcache_valid_p (regcache
, tdep
->ppc_ctr_regnum
))
1406 regcache_raw_collect (regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1407 if (regcache_valid_p (regcache
, tdep
->ppc_xer_regnum
))
1408 regcache_raw_collect (regcache
, tdep
->ppc_xer_regnum
, xer
);
1409 if (tdep
->ppc_fpscr_regnum
>= 0
1410 && regcache_valid_p (regcache
, tdep
->ppc_fpscr_regnum
))
1411 regcache_raw_collect (regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1414 /* Store all registers into pthread PDTID, which doesn't have a kernel
1417 It's possible to store a single register into a non-kernel pthread,
1418 but I doubt it's worth the effort. */
1421 store_regs_user_thread (const struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1423 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1424 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1426 pthdb_context_t ctx
;
1431 if (debug_aix_thread
)
1432 fprintf_unfiltered (gdb_stdlog
,
1433 "store_regs_user_thread %lx\n", (long) pdtid
);
1435 /* Retrieve the thread's current context for its non-register
1437 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1438 if (status
!= PTHDB_SUCCESS
)
1439 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1440 pd_status2str (status
));
1442 /* Collect general-purpose register values from the regcache. */
1444 for (i
= 0; i
< ppc_num_gprs
; i
++)
1445 if (regcache_valid_p (regcache
, tdep
->ppc_gp0_regnum
+ i
))
1449 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ i
,
1455 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ i
,
1461 /* Collect floating-point register values from the regcache. */
1462 if (ppc_floating_point_unit_p (gdbarch
))
1463 fill_fprs (regcache
, ctx
.fpr
);
1465 /* Special registers (always kept in ctx as 64 bits). */
1468 fill_sprs64 (regcache
, &ctx
.iar
, &ctx
.msr
, &ctx
.cr
, &ctx
.lr
, &ctx
.ctr
,
1469 &ctx
.xer
, &ctx
.fpscr
);
1473 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1474 Solution: use 32-bit temp variables. */
1475 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1478 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
, &tmp_ctr
,
1479 &tmp_xer
, &tmp_fpscr
);
1480 if (regcache_valid_p (regcache
, gdbarch_pc_regnum (gdbarch
)))
1482 if (regcache_valid_p (regcache
, tdep
->ppc_ps_regnum
))
1484 if (regcache_valid_p (regcache
, tdep
->ppc_cr_regnum
))
1486 if (regcache_valid_p (regcache
, tdep
->ppc_lr_regnum
))
1488 if (regcache_valid_p (regcache
, tdep
->ppc_ctr_regnum
))
1490 if (regcache_valid_p (regcache
, tdep
->ppc_xer_regnum
))
1492 if (regcache_valid_p (regcache
, tdep
->ppc_xer_regnum
))
1493 ctx
.fpscr
= tmp_fpscr
;
1496 status
= pthdb_pthread_setcontext (pd_session
, pdtid
, &ctx
);
1497 if (status
!= PTHDB_SUCCESS
)
1498 error (_("aix-thread: store_registers: pthdb_pthread_setcontext returned %s"),
1499 pd_status2str (status
));
1502 /* Store register REGNO if != -1 or all registers otherwise into
1505 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1506 SPRs, but there's no way to set individual registers within those
1507 groups. Therefore, if REGNO != -1, this function stores an entire
1511 store_regs_kernel_thread (const struct regcache
*regcache
, int regno
,
1514 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1515 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1516 uint64_t gprs64
[ppc_num_gprs
];
1517 uint32_t gprs32
[ppc_num_gprs
];
1518 double fprs
[ppc_num_fprs
];
1519 struct ptxsprs sprs64
;
1520 struct ptsprs sprs32
;
1523 if (debug_aix_thread
)
1524 fprintf_unfiltered (gdb_stdlog
,
1525 "store_regs_kernel_thread tid=%lx regno=%d\n",
1528 /* General-purpose registers. */
1530 || (tdep
->ppc_gp0_regnum
<= regno
1531 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_fprs
))
1535 /* Pre-fetch: some regs may not be in the cache. */
1536 ptrace64aix (PTT_READ_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1537 fill_gprs64 (regcache
, gprs64
);
1538 ptrace64aix (PTT_WRITE_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1542 /* Pre-fetch: some regs may not be in the cache. */
1543 ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
);
1544 fill_gprs32 (regcache
, gprs32
);
1545 ptrace32 (PTT_WRITE_GPRS
, tid
, gprs32
, 0, NULL
);
1549 /* Floating-point registers. */
1551 if (ppc_floating_point_unit_p (gdbarch
)
1553 || (regno
>= tdep
->ppc_fp0_regnum
1554 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1556 /* Pre-fetch: some regs may not be in the cache. */
1557 ptrace32 (PTT_READ_FPRS
, tid
, (void *) fprs
, 0, NULL
);
1558 fill_fprs (regcache
, fprs
);
1559 ptrace32 (PTT_WRITE_FPRS
, tid
, (void *) fprs
, 0, NULL
);
1562 /* Special-purpose registers. */
1564 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1568 /* Pre-fetch: some registers won't be in the cache. */
1569 ptrace64aix (PTT_READ_SPRS
, tid
,
1570 (unsigned long) &sprs64
, 0, NULL
);
1571 fill_sprs64 (regcache
, &sprs64
.pt_iar
, &sprs64
.pt_msr
,
1572 &sprs64
.pt_cr
, &sprs64
.pt_lr
, &sprs64
.pt_ctr
,
1573 &sprs64
.pt_xer
, &sprs64
.pt_fpscr
);
1574 ptrace64aix (PTT_WRITE_SPRS
, tid
,
1575 (unsigned long) &sprs64
, 0, NULL
);
1579 /* The contents of "struct ptspr" were declared as "unsigned
1580 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1581 Use temporaries to work around this problem. Also, add an
1582 assert here to make sure we fail if the system header files
1583 use "unsigned long", and the size of that type is not what
1584 the headers expect. */
1585 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1588 gdb_assert (sizeof (sprs32
.pt_iar
) == 4);
1590 /* Pre-fetch: some registers won't be in the cache. */
1591 ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1593 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
,
1594 &tmp_ctr
, &tmp_xer
, &tmp_fpscr
);
1596 sprs32
.pt_iar
= tmp_iar
;
1597 sprs32
.pt_msr
= tmp_msr
;
1598 sprs32
.pt_cr
= tmp_cr
;
1599 sprs32
.pt_lr
= tmp_lr
;
1600 sprs32
.pt_ctr
= tmp_ctr
;
1601 sprs32
.pt_xer
= tmp_xer
;
1602 sprs32
.pt_fpscr
= tmp_fpscr
;
1604 if (tdep
->ppc_mq_regnum
>= 0)
1605 if (regcache_valid_p (regcache
, tdep
->ppc_mq_regnum
))
1606 regcache_raw_collect (regcache
, tdep
->ppc_mq_regnum
,
1609 ptrace32 (PTT_WRITE_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1614 /* Store gdb's current view of the register set into the
1615 thread/process specified by inferior_ptid. */
1618 aix_thread_store_registers (struct regcache
*regcache
, int regno
)
1620 struct thread_info
*thread
;
1623 if (!PD_TID (inferior_ptid
))
1624 base_target
.to_store_registers (regcache
, regno
);
1627 thread
= find_thread_pid (inferior_ptid
);
1628 tid
= thread
->private->tid
;
1630 if (tid
== PTHDB_INVALID_TID
)
1631 store_regs_user_thread (regcache
, thread
->private->pdtid
);
1633 store_regs_kernel_thread (regcache
, regno
, tid
);
1637 /* Attempt a transfer all LEN bytes starting at OFFSET between the
1638 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
1639 Return the number of bytes actually transferred. */
1642 aix_thread_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1643 const char *annex
, gdb_byte
*readbuf
,
1644 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1646 struct cleanup
*old_chain
= save_inferior_ptid ();
1649 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1650 xfer
= base_target
.to_xfer_partial (ops
, object
, annex
,
1651 readbuf
, writebuf
, offset
, len
);
1653 do_cleanups (old_chain
);
1657 /* Kill and forget about the inferior process. */
1660 aix_thread_kill (void)
1662 struct cleanup
*cleanup
= save_inferior_ptid ();
1664 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1665 base_target
.to_kill ();
1666 do_cleanups (cleanup
);
1669 /* Clean up after the inferior exits. */
1672 aix_thread_mourn_inferior (struct target_ops
*ops
)
1675 base_target
.to_mourn_inferior (&base_target
);
1678 /* Return whether thread PID is still valid. */
1681 aix_thread_thread_alive (ptid_t ptid
)
1684 return base_target
.to_thread_alive (ptid
);
1686 /* We update the thread list every time the child stops, so all
1687 valid threads should be in the thread list. */
1688 return in_thread_list (ptid
);
1691 /* Return a printable representation of composite PID for use in
1692 "info threads" output. */
1695 aix_thread_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1697 static char *ret
= NULL
;
1700 return base_target
.to_pid_to_str (&base_target
, ptid
);
1702 /* Free previous return value; a new one will be allocated by
1706 ret
= xstrprintf (_("Thread %ld"), ptid_get_tid (ptid
));
1710 /* Return a printable representation of extra information about
1711 THREAD, for use in "info threads" output. */
1714 aix_thread_extra_thread_info (struct thread_info
*thread
)
1716 struct ui_file
*buf
;
1718 pthdb_pthread_t pdtid
;
1720 pthdb_state_t state
;
1721 pthdb_suspendstate_t suspendstate
;
1722 pthdb_detachstate_t detachstate
;
1725 static char *ret
= NULL
;
1727 if (!PD_TID (thread
->ptid
))
1730 buf
= mem_fileopen ();
1732 pdtid
= thread
->private->pdtid
;
1733 tid
= thread
->private->tid
;
1735 if (tid
!= PTHDB_INVALID_TID
)
1736 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1737 fprintf_unfiltered (buf
, _("tid %d"), (int)tid
);
1739 status
= pthdb_pthread_state (pd_session
, pdtid
, &state
);
1740 if (status
!= PTHDB_SUCCESS
)
1742 fprintf_unfiltered (buf
, ", %s", state2str (state
));
1744 status
= pthdb_pthread_suspendstate (pd_session
, pdtid
,
1746 if (status
== PTHDB_SUCCESS
&& suspendstate
== PSS_SUSPENDED
)
1747 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1748 fprintf_unfiltered (buf
, _(", suspended"));
1750 status
= pthdb_pthread_detachstate (pd_session
, pdtid
,
1752 if (status
== PTHDB_SUCCESS
&& detachstate
== PDS_DETACHED
)
1753 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1754 fprintf_unfiltered (buf
, _(", detached"));
1756 pthdb_pthread_cancelpend (pd_session
, pdtid
, &cancelpend
);
1757 if (status
== PTHDB_SUCCESS
&& cancelpend
)
1758 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1759 fprintf_unfiltered (buf
, _(", cancel pending"));
1761 ui_file_write (buf
, "", 1);
1763 xfree (ret
); /* Free old buffer. */
1765 ret
= ui_file_xstrdup (buf
, &length
);
1766 ui_file_delete (buf
);
1771 /* Initialize target aix_thread_ops. */
1774 init_aix_thread_ops (void)
1776 aix_thread_ops
.to_shortname
= "aix-threads";
1777 aix_thread_ops
.to_longname
= _("AIX pthread support");
1778 aix_thread_ops
.to_doc
= _("AIX pthread support");
1780 aix_thread_ops
.to_attach
= aix_thread_attach
;
1781 aix_thread_ops
.to_detach
= aix_thread_detach
;
1782 aix_thread_ops
.to_resume
= aix_thread_resume
;
1783 aix_thread_ops
.to_wait
= aix_thread_wait
;
1784 aix_thread_ops
.to_fetch_registers
= aix_thread_fetch_registers
;
1785 aix_thread_ops
.to_store_registers
= aix_thread_store_registers
;
1786 aix_thread_ops
.to_xfer_partial
= aix_thread_xfer_partial
;
1787 /* No need for aix_thread_ops.to_create_inferior, because we activate thread
1788 debugging when the inferior reaches pd_brk_addr. */
1789 aix_thread_ops
.to_kill
= aix_thread_kill
;
1790 aix_thread_ops
.to_mourn_inferior
= aix_thread_mourn_inferior
;
1791 aix_thread_ops
.to_thread_alive
= aix_thread_thread_alive
;
1792 aix_thread_ops
.to_pid_to_str
= aix_thread_pid_to_str
;
1793 aix_thread_ops
.to_extra_thread_info
= aix_thread_extra_thread_info
;
1794 aix_thread_ops
.to_stratum
= thread_stratum
;
1795 aix_thread_ops
.to_magic
= OPS_MAGIC
;
1798 /* Module startup initialization function, automagically called by
1802 _initialize_aix_thread (void)
1804 init_aix_thread_ops ();
1805 add_target (&aix_thread_ops
);
1807 /* Notice when object files get loaded and unloaded. */
1808 observer_attach_new_objfile (new_objfile
);
1810 add_setshow_boolean_cmd ("aix-thread", class_maintenance
, &debug_aix_thread
,
1811 _("Set debugging of AIX thread module."),
1812 _("Show debugging of AIX thread module."),
1813 _("Enables debugging output (used to debug GDB)."),
1814 NULL
, NULL
, /* FIXME: i18n: Debugging of AIX thread module is \"%d\". */
1815 &setdebuglist
, &showdebuglist
);