1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2018 Free Software Foundation, Inc.
4 Written by Nick Duffek <nsd@redhat.com>.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
23 debugging pthread applications.
25 Some name prefix conventions:
26 pthdb_ provided by libpthdebug.a
27 pdc_ callbacks that this module provides to libpthdebug.a
28 pd_ variables or functions interfacing with libpthdebug.a
30 libpthdebug peculiarities:
32 - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but
33 it's not documented, and after several calls it stops working
34 and causes other libpthdebug functions to fail.
36 - pthdb_tid_pthread() doesn't always work after
37 pthdb_session_update(), but it does work after cycling through
38 all threads using pthdb_pthread().
43 #include "gdbthread.h"
49 #include "observable.h"
53 #include <sys/types.h>
54 #include <sys/ptrace.h>
57 #include <sys/pthdebug.h>
59 #if !HAVE_DECL_GETTHRDS
60 extern int getthrds (pid_t
, struct thrdsinfo64
*, int, tid_t
*, int);
63 /* Whether to emit debugging output. */
64 static int debug_aix_thread
;
66 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
67 #ifndef PTHDB_VERSION_3
68 #define pthdb_tid_t tid_t
71 /* Return whether to treat PID as a debuggable thread id. */
73 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
75 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
76 PTHDB_BAD_USER errors, so use 1. */
80 /* Success and failure values returned by pthdb callbacks. */
82 #define PDC_SUCCESS PTHDB_SUCCESS
83 #define PDC_FAILURE PTHDB_CALLBACK
85 /* Private data attached to each element in GDB's thread list. */
87 struct aix_thread_info
: public private_thread_info
89 pthdb_pthread_t pdtid
; /* thread's libpthdebug id */
90 pthdb_tid_t tid
; /* kernel thread id */
93 /* Return the aix_thread_info attached to THREAD. */
95 static aix_thread_info
*
96 get_aix_thread_info (thread_info
*thread
)
98 return static_cast<aix_thread_info
*> (thread
->priv
.get ());
101 /* Information about a thread of which libpthdebug is aware. */
104 pthdb_pthread_t pdtid
;
109 /* This module's target-specific operations, active while pd_able is true. */
111 class aix_thread_target final
: public target_ops
115 { to_stratum
= thread_stratum
; }
117 const char *shortname () override
118 { return "aix-threads"; }
119 const char *longname () override
120 { return _("AIX pthread support"); }
121 const char *doc () override
122 { return _("AIX pthread support"); }
124 void detach (inferior
*, int) override
;
125 void resume (ptid_t
, int, enum gdb_signal
) override
;
126 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
128 void fetch_registers (struct regcache
*, int) override
;
129 void store_registers (struct regcache
*, int) override
;
131 enum target_xfer_status
xfer_partial (enum target_object object
,
134 const gdb_byte
*writebuf
,
135 ULONGEST offset
, ULONGEST len
,
136 ULONGEST
*xfered_len
) override
;
138 void mourn_inferior () override
;
140 int thread_alive (ptid_t ptid
) override
;
142 const char *pid_to_str (ptid_t
) override
;
144 const char *extra_thread_info (struct thread_info
*) override
;
146 ptid_t
get_ada_task_ptid (long lwp
, long thread
) override
;
149 static aix_thread_target aix_thread_ops
;
151 /* Address of the function that libpthread will call when libpthdebug
152 is ready to be initialized. */
154 static CORE_ADDR pd_brk_addr
;
156 /* Whether the current application is debuggable by pthdb. */
158 static int pd_able
= 0;
160 /* Whether a threaded application is being debugged. */
162 static int pd_active
= 0;
164 /* Whether the current architecture is 64-bit.
165 Only valid when pd_able is true. */
169 /* Forward declarations for pthdb callbacks. */
171 static int pdc_symbol_addrs (pthdb_user_t
, pthdb_symbol_t
*, int);
172 static int pdc_read_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
173 static int pdc_write_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
174 static int pdc_read_regs (pthdb_user_t user
, pthdb_tid_t tid
,
175 unsigned long long flags
,
176 pthdb_context_t
*context
);
177 static int pdc_write_regs (pthdb_user_t user
, pthdb_tid_t tid
,
178 unsigned long long flags
,
179 pthdb_context_t
*context
);
180 static int pdc_alloc (pthdb_user_t
, size_t, void **);
181 static int pdc_realloc (pthdb_user_t
, void *, size_t, void **);
182 static int pdc_dealloc (pthdb_user_t
, void *);
184 /* pthdb callbacks. */
186 static pthdb_callbacks_t pd_callbacks
= {
198 /* Current pthdb session. */
200 static pthdb_session_t pd_session
;
202 /* Return a printable representation of pthdebug function return
206 pd_status2str (int status
)
210 case PTHDB_SUCCESS
: return "SUCCESS";
211 case PTHDB_NOSYS
: return "NOSYS";
212 case PTHDB_NOTSUP
: return "NOTSUP";
213 case PTHDB_BAD_VERSION
: return "BAD_VERSION";
214 case PTHDB_BAD_USER
: return "BAD_USER";
215 case PTHDB_BAD_SESSION
: return "BAD_SESSION";
216 case PTHDB_BAD_MODE
: return "BAD_MODE";
217 case PTHDB_BAD_FLAGS
: return "BAD_FLAGS";
218 case PTHDB_BAD_CALLBACK
: return "BAD_CALLBACK";
219 case PTHDB_BAD_POINTER
: return "BAD_POINTER";
220 case PTHDB_BAD_CMD
: return "BAD_CMD";
221 case PTHDB_BAD_PTHREAD
: return "BAD_PTHREAD";
222 case PTHDB_BAD_ATTR
: return "BAD_ATTR";
223 case PTHDB_BAD_MUTEX
: return "BAD_MUTEX";
224 case PTHDB_BAD_MUTEXATTR
: return "BAD_MUTEXATTR";
225 case PTHDB_BAD_COND
: return "BAD_COND";
226 case PTHDB_BAD_CONDATTR
: return "BAD_CONDATTR";
227 case PTHDB_BAD_RWLOCK
: return "BAD_RWLOCK";
228 case PTHDB_BAD_RWLOCKATTR
: return "BAD_RWLOCKATTR";
229 case PTHDB_BAD_KEY
: return "BAD_KEY";
230 case PTHDB_BAD_PTID
: return "BAD_PTID";
231 case PTHDB_BAD_TID
: return "BAD_TID";
232 case PTHDB_CALLBACK
: return "CALLBACK";
233 case PTHDB_CONTEXT
: return "CONTEXT";
234 case PTHDB_HELD
: return "HELD";
235 case PTHDB_NOT_HELD
: return "NOT_HELD";
236 case PTHDB_MEMORY
: return "MEMORY";
237 case PTHDB_NOT_PTHREADED
: return "NOT_PTHREADED";
238 case PTHDB_SYMBOL
: return "SYMBOL";
239 case PTHDB_NOT_AVAIL
: return "NOT_AVAIL";
240 case PTHDB_INTERNAL
: return "INTERNAL";
241 default: return "UNKNOWN";
245 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
246 exceptional conditions and either return nonlocally or else return
247 1 for success and 0 for failure. */
250 ptrace_check (int req
, int id
, int ret
)
252 if (ret
== 0 && !errno
)
255 /* According to ptrace(2), ptrace may fail with EPERM if "the
256 Identifier parameter corresponds to a kernel thread which is
257 stopped in kernel mode and whose computational state cannot be
258 read or written." This happens quite often with register reads. */
265 if (ret
== -1 && errno
== EPERM
)
267 if (debug_aix_thread
)
268 fprintf_unfiltered (gdb_stdlog
,
269 "ptrace (%d, %d) = %d (errno = %d)\n",
270 req
, id
, ret
, errno
);
271 return ret
== -1 ? 0 : 1;
275 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
276 req
, id
, ret
, errno
, safe_strerror (errno
));
277 return 0; /* Not reached. */
280 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
281 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
285 # define ptracex(request, pid, addr, data, buf) \
286 ptrace64 (request, pid, addr, data, buf)
290 ptrace64aix (int req
, int id
, long long addr
, int data
, int *buf
)
293 return ptrace_check (req
, id
, ptracex (req
, id
, addr
, data
, buf
));
296 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
297 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
301 # define ptrace(request, pid, addr, data, buf) \
302 ptrace64 (request, pid, addr, data, buf)
303 # define addr_ptr long long
305 # define addr_ptr int *
309 ptrace32 (int req
, int id
, addr_ptr addr
, int data
, int *buf
)
312 return ptrace_check (req
, id
,
313 ptrace (req
, id
, addr
, data
, buf
));
316 /* If *PIDP is a composite process/thread id, convert it to a
320 pid_to_prc (ptid_t
*ptidp
)
326 *ptidp
= pid_to_ptid (ptid_get_pid (ptid
));
329 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
330 the address of SYMBOLS[<i>].name. */
333 pdc_symbol_addrs (pthdb_user_t user
, pthdb_symbol_t
*symbols
, int count
)
335 struct bound_minimal_symbol ms
;
339 if (debug_aix_thread
)
340 fprintf_unfiltered (gdb_stdlog
,
341 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
342 user
, (long) symbols
, count
);
344 for (i
= 0; i
< count
; i
++)
346 name
= symbols
[i
].name
;
347 if (debug_aix_thread
)
348 fprintf_unfiltered (gdb_stdlog
,
349 " symbols[%d].name = \"%s\"\n", i
, name
);
355 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
356 if (ms
.minsym
== NULL
)
358 if (debug_aix_thread
)
359 fprintf_unfiltered (gdb_stdlog
, " returning PDC_FAILURE\n");
362 symbols
[i
].addr
= BMSYMBOL_VALUE_ADDRESS (ms
);
364 if (debug_aix_thread
)
365 fprintf_unfiltered (gdb_stdlog
, " symbols[%d].addr = %s\n",
366 i
, hex_string (symbols
[i
].addr
));
368 if (debug_aix_thread
)
369 fprintf_unfiltered (gdb_stdlog
, " returning PDC_SUCCESS\n");
373 /* Read registers call back function should be able to read the
374 context information of a debuggee kernel thread from an active
375 process or from a core file. The information should be formatted
376 in context64 form for both 32-bit and 64-bit process.
377 If successful return 0, else non-zero is returned. */
380 pdc_read_regs (pthdb_user_t user
,
382 unsigned long long flags
,
383 pthdb_context_t
*context
)
385 /* This function doesn't appear to be used, so we could probably
386 just return 0 here. HOWEVER, if it is not defined, the OS will
387 complain and several thread debug functions will fail. In case
388 this is needed, I have implemented what I think it should do,
389 however this code is untested. */
391 uint64_t gprs64
[ppc_num_gprs
];
392 uint32_t gprs32
[ppc_num_gprs
];
393 double fprs
[ppc_num_fprs
];
394 struct ptxsprs sprs64
;
395 struct ptsprs sprs32
;
397 if (debug_aix_thread
)
398 fprintf_unfiltered (gdb_stdlog
, "pdc_read_regs tid=%d flags=%s\n",
399 (int) tid
, hex_string (flags
));
401 /* General-purpose registers. */
402 if (flags
& PTHDB_FLAG_GPRS
)
406 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
407 (unsigned long) gprs64
, 0, NULL
))
408 memset (gprs64
, 0, sizeof (gprs64
));
409 memcpy (context
->gpr
, gprs64
, sizeof(gprs64
));
413 if (!ptrace32 (PTT_READ_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
))
414 memset (gprs32
, 0, sizeof (gprs32
));
415 memcpy (context
->gpr
, gprs32
, sizeof(gprs32
));
419 /* Floating-point registers. */
420 if (flags
& PTHDB_FLAG_FPRS
)
422 if (!ptrace32 (PTT_READ_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
))
423 memset (fprs
, 0, sizeof (fprs
));
424 memcpy (context
->fpr
, fprs
, sizeof(fprs
));
427 /* Special-purpose registers. */
428 if (flags
& PTHDB_FLAG_SPRS
)
432 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
433 (unsigned long) &sprs64
, 0, NULL
))
434 memset (&sprs64
, 0, sizeof (sprs64
));
435 memcpy (&context
->msr
, &sprs64
, sizeof(sprs64
));
439 if (!ptrace32 (PTT_READ_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
))
440 memset (&sprs32
, 0, sizeof (sprs32
));
441 memcpy (&context
->msr
, &sprs32
, sizeof(sprs32
));
447 /* Write register function should be able to write requested context
448 information to specified debuggee's kernel thread id.
449 If successful return 0, else non-zero is returned. */
452 pdc_write_regs (pthdb_user_t user
,
454 unsigned long long flags
,
455 pthdb_context_t
*context
)
457 /* This function doesn't appear to be used, so we could probably
458 just return 0 here. HOWEVER, if it is not defined, the OS will
459 complain and several thread debug functions will fail. In case
460 this is needed, I have implemented what I think it should do,
461 however this code is untested. */
463 if (debug_aix_thread
)
464 fprintf_unfiltered (gdb_stdlog
, "pdc_write_regs tid=%d flags=%s\n",
465 (int) tid
, hex_string (flags
));
467 /* General-purpose registers. */
468 if (flags
& PTHDB_FLAG_GPRS
)
471 ptrace64aix (PTT_WRITE_GPRS
, tid
,
472 (unsigned long) context
->gpr
, 0, NULL
);
474 ptrace32 (PTT_WRITE_GPRS
, tid
, (uintptr_t) context
->gpr
, 0, NULL
);
477 /* Floating-point registers. */
478 if (flags
& PTHDB_FLAG_FPRS
)
480 ptrace32 (PTT_WRITE_FPRS
, tid
, (uintptr_t) context
->fpr
, 0, NULL
);
483 /* Special-purpose registers. */
484 if (flags
& PTHDB_FLAG_SPRS
)
488 ptrace64aix (PTT_WRITE_SPRS
, tid
,
489 (unsigned long) &context
->msr
, 0, NULL
);
493 ptrace32 (PTT_WRITE_SPRS
, tid
, (uintptr_t) &context
->msr
, 0, NULL
);
499 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
502 pdc_read_data (pthdb_user_t user
, void *buf
,
503 pthdb_addr_t addr
, size_t len
)
507 if (debug_aix_thread
)
508 fprintf_unfiltered (gdb_stdlog
,
509 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
510 user
, (long) buf
, hex_string (addr
), len
);
512 status
= target_read_memory (addr
, (gdb_byte
*) buf
, len
);
513 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
515 if (debug_aix_thread
)
516 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n",
517 status
, pd_status2str (ret
));
521 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
524 pdc_write_data (pthdb_user_t user
, void *buf
,
525 pthdb_addr_t addr
, size_t len
)
529 if (debug_aix_thread
)
530 fprintf_unfiltered (gdb_stdlog
,
531 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
532 user
, (long) buf
, hex_string (addr
), len
);
534 status
= target_write_memory (addr
, (gdb_byte
*) buf
, len
);
535 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
537 if (debug_aix_thread
)
538 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n", status
,
539 pd_status2str (ret
));
543 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
547 pdc_alloc (pthdb_user_t user
, size_t len
, void **bufp
)
549 if (debug_aix_thread
)
550 fprintf_unfiltered (gdb_stdlog
,
551 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
552 user
, len
, (long) bufp
);
553 *bufp
= xmalloc (len
);
554 if (debug_aix_thread
)
555 fprintf_unfiltered (gdb_stdlog
,
556 " malloc returned 0x%lx\n", (long) *bufp
);
558 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
561 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
564 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
565 realloc callback, so that it contains LEN bytes, and store a
566 pointer to the result in BUFP. */
569 pdc_realloc (pthdb_user_t user
, void *buf
, size_t len
, void **bufp
)
571 if (debug_aix_thread
)
572 fprintf_unfiltered (gdb_stdlog
,
573 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
574 user
, (long) buf
, len
, (long) bufp
);
575 *bufp
= xrealloc (buf
, len
);
576 if (debug_aix_thread
)
577 fprintf_unfiltered (gdb_stdlog
,
578 " realloc returned 0x%lx\n", (long) *bufp
);
579 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
582 /* pthdb callback: free BUF, which was allocated by the alloc or
586 pdc_dealloc (pthdb_user_t user
, void *buf
)
588 if (debug_aix_thread
)
589 fprintf_unfiltered (gdb_stdlog
,
590 "pdc_free (user = %ld, buf = 0x%lx)\n", user
,
596 /* Return a printable representation of pthread STATE. */
599 state2str (pthdb_state_t state
)
604 /* i18n: Like "Thread-Id %d, [state] idle" */
605 return _("idle"); /* being created */
607 /* i18n: Like "Thread-Id %d, [state] running" */
608 return _("running"); /* running */
610 /* i18n: Like "Thread-Id %d, [state] sleeping" */
611 return _("sleeping"); /* awaiting an event */
613 /* i18n: Like "Thread-Id %d, [state] ready" */
614 return _("ready"); /* runnable */
616 /* i18n: Like "Thread-Id %d, [state] finished" */
617 return _("finished"); /* awaiting a join/detach */
619 /* i18n: Like "Thread-Id %d, [state] unknown" */
624 /* qsort() comparison function for sorting pd_thread structs by pthid. */
627 pcmp (const void *p1v
, const void *p2v
)
629 struct pd_thread
*p1
= (struct pd_thread
*) p1v
;
630 struct pd_thread
*p2
= (struct pd_thread
*) p2v
;
631 return p1
->pthid
< p2
->pthid
? -1 : p1
->pthid
> p2
->pthid
;
634 /* iterate_over_threads() callback for counting GDB threads.
636 Do not count the main thread (whose tid is zero). This matches
637 the list of threads provided by the pthreaddebug library, which
638 does not include that main thread either, and thus allows us
639 to compare the two lists. */
642 giter_count (struct thread_info
*thread
, void *countp
)
644 if (PD_TID (thread
->ptid
))
649 /* iterate_over_threads() callback for accumulating GDB thread pids.
651 Do not include the main thread (whose tid is zero). This matches
652 the list of threads provided by the pthreaddebug library, which
653 does not include that main thread either, and thus allows us
654 to compare the two lists. */
657 giter_accum (struct thread_info
*thread
, void *bufp
)
659 if (PD_TID (thread
->ptid
))
661 **(struct thread_info
***) bufp
= thread
;
662 (*(struct thread_info
***) bufp
)++;
667 /* ptid comparison function */
670 ptid_cmp (ptid_t ptid1
, ptid_t ptid2
)
674 if (ptid_get_pid (ptid1
) < ptid_get_pid (ptid2
))
676 else if (ptid_get_pid (ptid1
) > ptid_get_pid (ptid2
))
678 else if (ptid_get_tid (ptid1
) < ptid_get_tid (ptid2
))
680 else if (ptid_get_tid (ptid1
) > ptid_get_tid (ptid2
))
682 else if (ptid_get_lwp (ptid1
) < ptid_get_lwp (ptid2
))
684 else if (ptid_get_lwp (ptid1
) > ptid_get_lwp (ptid2
))
690 /* qsort() comparison function for sorting thread_info structs by pid. */
693 gcmp (const void *t1v
, const void *t2v
)
695 struct thread_info
*t1
= *(struct thread_info
**) t1v
;
696 struct thread_info
*t2
= *(struct thread_info
**) t2v
;
697 return ptid_cmp (t1
->ptid
, t2
->ptid
);
700 /* Search through the list of all kernel threads for the thread
701 that has stopped on a SIGTRAP signal, and return its TID.
702 Return 0 if none found. */
705 get_signaled_thread (void)
707 struct thrdsinfo64 thrinf
;
713 if (getthrds (ptid_get_pid (inferior_ptid
), &thrinf
,
714 sizeof (thrinf
), &ktid
, 1) != 1)
717 if (thrinf
.ti_cursig
== SIGTRAP
)
718 return thrinf
.ti_tid
;
721 /* Didn't find any thread stopped on a SIGTRAP signal. */
725 /* Synchronize GDB's thread list with libpthdebug's.
727 There are some benefits of doing this every time the inferior stops:
729 - allows users to run thread-specific commands without needing to
730 run "info threads" first
732 - helps pthdb_tid_pthread() work properly (see "libpthdebug
733 peculiarities" at the top of this module)
735 - simplifies the demands placed on libpthdebug, which seems to
736 have difficulty with certain call patterns */
739 sync_threadlists (void)
741 int cmd
, status
, infpid
;
742 int pcount
, psize
, pi
, gcount
, gi
;
743 struct pd_thread
*pbuf
;
744 struct thread_info
**gbuf
, **g
, *thread
;
745 pthdb_pthread_t pdtid
;
749 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
753 pbuf
= XNEWVEC (struct pd_thread
, psize
);
755 for (cmd
= PTHDB_LIST_FIRST
;; cmd
= PTHDB_LIST_NEXT
)
757 status
= pthdb_pthread (pd_session
, &pdtid
, cmd
);
758 if (status
!= PTHDB_SUCCESS
|| pdtid
== PTHDB_INVALID_PTHREAD
)
761 status
= pthdb_pthread_ptid (pd_session
, pdtid
, &pthid
);
762 if (status
!= PTHDB_SUCCESS
|| pthid
== PTHDB_INVALID_PTID
)
768 pbuf
= (struct pd_thread
*) xrealloc (pbuf
,
769 psize
* sizeof *pbuf
);
771 pbuf
[pcount
].pdtid
= pdtid
;
772 pbuf
[pcount
].pthid
= pthid
;
776 for (pi
= 0; pi
< pcount
; pi
++)
778 status
= pthdb_pthread_tid (pd_session
, pbuf
[pi
].pdtid
, &tid
);
779 if (status
!= PTHDB_SUCCESS
)
780 tid
= PTHDB_INVALID_TID
;
784 qsort (pbuf
, pcount
, sizeof *pbuf
, pcmp
);
786 /* Accumulate an array of GDB threads sorted by pid. */
789 iterate_over_threads (giter_count
, &gcount
);
790 g
= gbuf
= XNEWVEC (struct thread_info
*, gcount
);
791 iterate_over_threads (giter_accum
, &g
);
792 qsort (gbuf
, gcount
, sizeof *gbuf
, gcmp
);
794 /* Apply differences between the two arrays to GDB's thread list. */
796 infpid
= ptid_get_pid (inferior_ptid
);
797 for (pi
= gi
= 0; pi
< pcount
|| gi
< gcount
;)
801 delete_thread (gbuf
[gi
]->ptid
);
804 else if (gi
== gcount
)
806 aix_thread_info
*priv
= new aix_thread_info
;
807 priv
->pdtid
= pbuf
[pi
].pdtid
;
808 priv
->tid
= pbuf
[pi
].tid
;
810 thread
= add_thread_with_info (ptid_t (infpid
, 0, pbuf
[pi
].pthid
), priv
);
819 pptid
= ptid_build (infpid
, 0, pbuf
[pi
].pthid
);
820 gptid
= gbuf
[gi
]->ptid
;
821 pdtid
= pbuf
[pi
].pdtid
;
824 cmp_result
= ptid_cmp (pptid
, gptid
);
828 aix_thread_info
*priv
= get_aix_thread_info (gbuf
[gi
]);
835 else if (cmp_result
> 0)
837 delete_thread (gptid
);
842 thread
= add_thread (pptid
);
844 aix_thread_info
*priv
= new aix_thread_info
;
845 thread
->priv
.reset (priv
);
857 /* Iterate_over_threads() callback for locating a thread, using
858 the TID of its associated kernel thread. */
861 iter_tid (struct thread_info
*thread
, void *tidp
)
863 const pthdb_tid_t tid
= *(pthdb_tid_t
*)tidp
;
864 aix_thread_info
*priv
= get_aix_thread_info (thread
);
866 return priv
->tid
== tid
;
869 /* Synchronize libpthdebug's state with the inferior and with GDB,
870 generate a composite process/thread <pid> for the current thread,
871 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
874 pd_update (int set_infpid
)
879 struct thread_info
*thread
= NULL
;
882 return inferior_ptid
;
884 status
= pthdb_session_update (pd_session
);
885 if (status
!= PTHDB_SUCCESS
)
886 return inferior_ptid
;
890 /* Define "current thread" as one that just received a trap signal. */
892 tid
= get_signaled_thread ();
894 thread
= iterate_over_threads (iter_tid
, &tid
);
896 ptid
= inferior_ptid
;
901 inferior_ptid
= ptid
;
906 /* Try to start debugging threads in the current process.
907 If successful and SET_INFPID, set inferior_ptid to reflect the
911 pd_activate (int set_infpid
)
915 status
= pthdb_session_init (PD_USER
, arch64
? PEM_64BIT
: PEM_32BIT
,
916 PTHDB_FLAG_REGS
, &pd_callbacks
,
918 if (status
!= PTHDB_SUCCESS
)
920 return inferior_ptid
;
923 return pd_update (set_infpid
);
926 /* Undo the effects of pd_activate(). */
933 pthdb_session_destroy (pd_session
);
935 pid_to_prc (&inferior_ptid
);
939 /* An object file has just been loaded. Check whether the current
940 application is pthreaded, and if so, prepare for thread debugging. */
947 struct bound_minimal_symbol ms
;
949 /* Don't initialize twice. */
953 /* Check application word size. */
954 arch64
= register_size (target_gdbarch (), 0) == 8;
956 /* Check whether the application is pthreaded. */
958 status
= pthdb_session_pthreaded (PD_USER
, PTHDB_FLAG_REGS
,
959 &pd_callbacks
, &stub_name
);
960 if ((status
!= PTHDB_SUCCESS
961 && status
!= PTHDB_NOT_PTHREADED
) || !stub_name
)
964 /* Set a breakpoint on the returned stub function. */
965 ms
= lookup_minimal_symbol (stub_name
, NULL
, NULL
);
966 if (ms
.minsym
== NULL
)
968 pd_brk_addr
= BMSYMBOL_VALUE_ADDRESS (ms
);
969 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr
))
972 /* Prepare for thread debugging. */
973 push_target (&aix_thread_ops
);
976 /* If we're debugging a core file or an attached inferior, the
977 pthread library may already have been initialized, so try to
978 activate thread debugging. */
982 /* Undo the effects of pd_enable(). */
992 unpush_target (&aix_thread_ops
);
995 /* new_objfile observer callback.
997 If OBJFILE is non-null, check whether a threaded application is
998 being debugged, and if so, prepare for thread debugging.
1000 If OBJFILE is null, stop debugging threads. */
1003 new_objfile (struct objfile
*objfile
)
1011 /* Attach to process specified by ARGS. */
1014 aix_thread_inferior_created (struct target_ops
*ops
, int from_tty
)
1019 /* Detach from the process attached to by aix_thread_attach(). */
1022 aix_thread_target::detach (inferior
*inf
, int from_tty
)
1024 struct target_ops
*beneath
= find_target_beneath (this);
1027 beneath
->detach (inf
, from_tty
);
1030 /* Tell the inferior process to continue running thread PID if != -1
1031 and all threads otherwise. */
1034 aix_thread_target::resume (ptid_t ptid
, int step
, enum gdb_signal sig
)
1036 struct thread_info
*thread
;
1041 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
1042 struct target_ops
*beneath
= find_target_beneath (this);
1044 inferior_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
1045 beneath
->resume (ptid
, step
, sig
);
1049 thread
= find_thread_ptid (ptid
);
1051 error (_("aix-thread resume: unknown pthread %ld"),
1052 ptid_get_lwp (ptid
));
1054 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1057 if (tid
[0] == PTHDB_INVALID_TID
)
1058 error (_("aix-thread resume: no tid for pthread %ld"),
1059 ptid_get_lwp (ptid
));
1063 ptrace64aix (PTT_CONTINUE
, tid
[0], (long long) 1,
1064 gdb_signal_to_host (sig
), (PTRACE_TYPE_ARG5
) tid
);
1066 ptrace32 (PTT_CONTINUE
, tid
[0], (addr_ptr
) 1,
1067 gdb_signal_to_host (sig
), (PTRACE_TYPE_ARG5
) tid
);
1071 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1072 If an error occurs, return -1, else return the pid of the stopped
1076 aix_thread_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
1079 struct target_ops
*beneath
= find_target_beneath (this);
1082 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
1086 inferior_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
1087 ptid
= beneath
->wait (ptid
, status
, options
);
1090 if (ptid_get_pid (ptid
) == -1)
1091 return pid_to_ptid (-1);
1093 /* Check whether libpthdebug might be ready to be initialized. */
1094 if (!pd_active
&& status
->kind
== TARGET_WAITKIND_STOPPED
1095 && status
->value
.sig
== GDB_SIGNAL_TRAP
)
1097 struct regcache
*regcache
= get_thread_regcache (ptid
);
1098 struct gdbarch
*gdbarch
= regcache
->arch ();
1100 if (regcache_read_pc (regcache
)
1101 - gdbarch_decr_pc_after_break (gdbarch
) == pd_brk_addr
)
1102 return pd_activate (0);
1105 return pd_update (0);
1108 /* Record that the 64-bit general-purpose registers contain VALS. */
1111 supply_gprs64 (struct regcache
*regcache
, uint64_t *vals
)
1113 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
1116 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1117 regcache_raw_supply (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1118 (char *) (vals
+ regno
));
1121 /* Record that 32-bit register REGNO contains VAL. */
1124 supply_reg32 (struct regcache
*regcache
, int regno
, uint32_t val
)
1126 regcache_raw_supply (regcache
, regno
, (char *) &val
);
1129 /* Record that the floating-point registers contain VALS. */
1132 supply_fprs (struct regcache
*regcache
, double *vals
)
1134 struct gdbarch
*gdbarch
= regcache
->arch ();
1135 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1138 /* This function should never be called on architectures without
1139 floating-point registers. */
1140 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1142 for (regno
= tdep
->ppc_fp0_regnum
;
1143 regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
;
1145 regcache_raw_supply (regcache
, regno
,
1146 (char *) (vals
+ regno
- tdep
->ppc_fp0_regnum
));
1149 /* Predicate to test whether given register number is a "special" register. */
1151 special_register_p (struct gdbarch
*gdbarch
, int regno
)
1153 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1155 return regno
== gdbarch_pc_regnum (gdbarch
)
1156 || regno
== tdep
->ppc_ps_regnum
1157 || regno
== tdep
->ppc_cr_regnum
1158 || regno
== tdep
->ppc_lr_regnum
1159 || regno
== tdep
->ppc_ctr_regnum
1160 || regno
== tdep
->ppc_xer_regnum
1161 || (tdep
->ppc_fpscr_regnum
>= 0 && regno
== tdep
->ppc_fpscr_regnum
)
1162 || (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
);
1166 /* Record that the special registers contain the specified 64-bit and
1170 supply_sprs64 (struct regcache
*regcache
,
1171 uint64_t iar
, uint64_t msr
, uint32_t cr
,
1172 uint64_t lr
, uint64_t ctr
, uint32_t xer
,
1175 struct gdbarch
*gdbarch
= regcache
->arch ();
1176 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1178 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
),
1180 regcache_raw_supply (regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1181 regcache_raw_supply (regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1182 regcache_raw_supply (regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1183 regcache_raw_supply (regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1184 regcache_raw_supply (regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1185 if (tdep
->ppc_fpscr_regnum
>= 0)
1186 regcache_raw_supply (regcache
, tdep
->ppc_fpscr_regnum
,
1190 /* Record that the special registers contain the specified 32-bit
1194 supply_sprs32 (struct regcache
*regcache
,
1195 uint32_t iar
, uint32_t msr
, uint32_t cr
,
1196 uint32_t lr
, uint32_t ctr
, uint32_t xer
,
1199 struct gdbarch
*gdbarch
= regcache
->arch ();
1200 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1202 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
),
1204 regcache_raw_supply (regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1205 regcache_raw_supply (regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1206 regcache_raw_supply (regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1207 regcache_raw_supply (regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1208 regcache_raw_supply (regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1209 if (tdep
->ppc_fpscr_regnum
>= 0)
1210 regcache_raw_supply (regcache
, tdep
->ppc_fpscr_regnum
,
1214 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1217 There's no way to query a single register from a non-kernel
1218 pthread, so there's no need for a single-register version of this
1222 fetch_regs_user_thread (struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1224 struct gdbarch
*gdbarch
= regcache
->arch ();
1225 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1227 pthdb_context_t ctx
;
1229 if (debug_aix_thread
)
1230 fprintf_unfiltered (gdb_stdlog
,
1231 "fetch_regs_user_thread %lx\n", (long) pdtid
);
1232 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1233 if (status
!= PTHDB_SUCCESS
)
1234 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1235 pd_status2str (status
));
1237 /* General-purpose registers. */
1240 supply_gprs64 (regcache
, ctx
.gpr
);
1242 for (i
= 0; i
< ppc_num_gprs
; i
++)
1243 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, ctx
.gpr
[i
]);
1245 /* Floating-point registers. */
1247 if (ppc_floating_point_unit_p (gdbarch
))
1248 supply_fprs (regcache
, ctx
.fpr
);
1250 /* Special registers. */
1253 supply_sprs64 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1254 ctx
.xer
, ctx
.fpscr
);
1256 supply_sprs32 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1257 ctx
.xer
, ctx
.fpscr
);
1260 /* Fetch register REGNO if != -1 or all registers otherwise from
1263 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1264 SPRs, but there's no way to query individual registers within those
1265 groups. Therefore, if REGNO != -1, this function fetches an entire
1268 Unfortunately, kernel thread register queries often fail with
1269 EPERM, indicating that the thread is in kernel space. This breaks
1270 backtraces of threads other than the current one. To make that
1271 breakage obvious without throwing an error to top level (which is
1272 bad e.g. during "info threads" output), zero registers that can't
1276 fetch_regs_kernel_thread (struct regcache
*regcache
, int regno
,
1279 struct gdbarch
*gdbarch
= regcache
->arch ();
1280 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1281 uint64_t gprs64
[ppc_num_gprs
];
1282 uint32_t gprs32
[ppc_num_gprs
];
1283 double fprs
[ppc_num_fprs
];
1284 struct ptxsprs sprs64
;
1285 struct ptsprs sprs32
;
1288 if (debug_aix_thread
)
1289 fprintf_unfiltered (gdb_stdlog
,
1290 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1291 (long) tid
, regno
, arch64
);
1293 /* General-purpose registers. */
1295 || (tdep
->ppc_gp0_regnum
<= regno
1296 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
))
1300 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
1301 (unsigned long) gprs64
, 0, NULL
))
1302 memset (gprs64
, 0, sizeof (gprs64
));
1303 supply_gprs64 (regcache
, gprs64
);
1307 if (!ptrace32 (PTT_READ_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
))
1308 memset (gprs32
, 0, sizeof (gprs32
));
1309 for (i
= 0; i
< ppc_num_gprs
; i
++)
1310 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, gprs32
[i
]);
1314 /* Floating-point registers. */
1316 if (ppc_floating_point_unit_p (gdbarch
)
1318 || (regno
>= tdep
->ppc_fp0_regnum
1319 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1321 if (!ptrace32 (PTT_READ_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
))
1322 memset (fprs
, 0, sizeof (fprs
));
1323 supply_fprs (regcache
, fprs
);
1326 /* Special-purpose registers. */
1328 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1332 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
1333 (unsigned long) &sprs64
, 0, NULL
))
1334 memset (&sprs64
, 0, sizeof (sprs64
));
1335 supply_sprs64 (regcache
, sprs64
.pt_iar
, sprs64
.pt_msr
,
1336 sprs64
.pt_cr
, sprs64
.pt_lr
, sprs64
.pt_ctr
,
1337 sprs64
.pt_xer
, sprs64
.pt_fpscr
);
1341 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1343 if (!ptrace32 (PTT_READ_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
))
1344 memset (&sprs32
, 0, sizeof (sprs32
));
1345 supply_sprs32 (regcache
, sprs32
.pt_iar
, sprs32
.pt_msr
, sprs32
.pt_cr
,
1346 sprs32
.pt_lr
, sprs32
.pt_ctr
, sprs32
.pt_xer
,
1349 if (tdep
->ppc_mq_regnum
>= 0)
1350 regcache_raw_supply (regcache
, tdep
->ppc_mq_regnum
,
1351 (char *) &sprs32
.pt_mq
);
1356 /* Fetch register REGNO if != -1 or all registers otherwise from the
1357 thread/process connected to REGCACHE. */
1360 aix_thread_target::fetch_registers (struct regcache
*regcache
, int regno
)
1362 struct thread_info
*thread
;
1364 struct target_ops
*beneath
= find_target_beneath (this);
1366 if (!PD_TID (regcache_get_ptid (regcache
)))
1367 beneath
->fetch_registers (regcache
, regno
);
1370 thread
= find_thread_ptid (regcache_get_ptid (regcache
));
1371 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1374 if (tid
== PTHDB_INVALID_TID
)
1375 fetch_regs_user_thread (regcache
, priv
->pdtid
);
1377 fetch_regs_kernel_thread (regcache
, regno
, tid
);
1381 /* Store the gp registers into an array of uint32_t or uint64_t. */
1384 fill_gprs64 (const struct regcache
*regcache
, uint64_t *vals
)
1386 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
1389 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1390 if (REG_VALID
== regcache_register_status (regcache
,
1391 tdep
->ppc_gp0_regnum
+ regno
))
1392 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1397 fill_gprs32 (const struct regcache
*regcache
, uint32_t *vals
)
1399 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
1402 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1403 if (REG_VALID
== regcache_register_status (regcache
,
1404 tdep
->ppc_gp0_regnum
+ regno
))
1405 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1409 /* Store the floating point registers into a double array. */
1411 fill_fprs (const struct regcache
*regcache
, double *vals
)
1413 struct gdbarch
*gdbarch
= regcache
->arch ();
1414 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1417 /* This function should never be called on architectures without
1418 floating-point registers. */
1419 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1421 for (regno
= tdep
->ppc_fp0_regnum
;
1422 regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
;
1424 if (REG_VALID
== regcache_register_status (regcache
, regno
))
1425 regcache_raw_collect (regcache
, regno
,
1426 vals
+ regno
- tdep
->ppc_fp0_regnum
);
1429 /* Store the special registers into the specified 64-bit and 32-bit
1433 fill_sprs64 (const struct regcache
*regcache
,
1434 uint64_t *iar
, uint64_t *msr
, uint32_t *cr
,
1435 uint64_t *lr
, uint64_t *ctr
, uint32_t *xer
,
1438 struct gdbarch
*gdbarch
= regcache
->arch ();
1439 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1441 /* Verify that the size of the size of the IAR buffer is the
1442 same as the raw size of the PC (in the register cache). If
1443 they're not, then either GDB has been built incorrectly, or
1444 there's some other kind of internal error. To be really safe,
1445 we should check all of the sizes. */
1446 gdb_assert (sizeof (*iar
) == register_size
1447 (gdbarch
, gdbarch_pc_regnum (gdbarch
)));
1449 if (REG_VALID
== regcache_register_status (regcache
,
1450 gdbarch_pc_regnum (gdbarch
)))
1451 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), iar
);
1452 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ps_regnum
))
1453 regcache_raw_collect (regcache
, tdep
->ppc_ps_regnum
, msr
);
1454 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_cr_regnum
))
1455 regcache_raw_collect (regcache
, tdep
->ppc_cr_regnum
, cr
);
1456 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_lr_regnum
))
1457 regcache_raw_collect (regcache
, tdep
->ppc_lr_regnum
, lr
);
1458 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ctr_regnum
))
1459 regcache_raw_collect (regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1460 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_xer_regnum
))
1461 regcache_raw_collect (regcache
, tdep
->ppc_xer_regnum
, xer
);
1462 if (tdep
->ppc_fpscr_regnum
>= 0
1463 && REG_VALID
== regcache_register_status (regcache
,
1464 tdep
->ppc_fpscr_regnum
))
1465 regcache_raw_collect (regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1469 fill_sprs32 (const struct regcache
*regcache
,
1470 uint32_t *iar
, uint32_t *msr
, uint32_t *cr
,
1471 uint32_t *lr
, uint32_t *ctr
, uint32_t *xer
,
1474 struct gdbarch
*gdbarch
= regcache
->arch ();
1475 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1477 /* Verify that the size of the size of the IAR buffer is the
1478 same as the raw size of the PC (in the register cache). If
1479 they're not, then either GDB has been built incorrectly, or
1480 there's some other kind of internal error. To be really safe,
1481 we should check all of the sizes. */
1482 gdb_assert (sizeof (*iar
) == register_size (gdbarch
,
1483 gdbarch_pc_regnum (gdbarch
)));
1485 if (REG_VALID
== regcache_register_status (regcache
,
1486 gdbarch_pc_regnum (gdbarch
)))
1487 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), iar
);
1488 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ps_regnum
))
1489 regcache_raw_collect (regcache
, tdep
->ppc_ps_regnum
, msr
);
1490 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_cr_regnum
))
1491 regcache_raw_collect (regcache
, tdep
->ppc_cr_regnum
, cr
);
1492 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_lr_regnum
))
1493 regcache_raw_collect (regcache
, tdep
->ppc_lr_regnum
, lr
);
1494 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ctr_regnum
))
1495 regcache_raw_collect (regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1496 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_xer_regnum
))
1497 regcache_raw_collect (regcache
, tdep
->ppc_xer_regnum
, xer
);
1498 if (tdep
->ppc_fpscr_regnum
>= 0
1499 && REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_fpscr_regnum
))
1500 regcache_raw_collect (regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1503 /* Store all registers into pthread PDTID, which doesn't have a kernel
1506 It's possible to store a single register into a non-kernel pthread,
1507 but I doubt it's worth the effort. */
1510 store_regs_user_thread (const struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1512 struct gdbarch
*gdbarch
= regcache
->arch ();
1513 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1515 pthdb_context_t ctx
;
1520 if (debug_aix_thread
)
1521 fprintf_unfiltered (gdb_stdlog
,
1522 "store_regs_user_thread %lx\n", (long) pdtid
);
1524 /* Retrieve the thread's current context for its non-register
1526 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1527 if (status
!= PTHDB_SUCCESS
)
1528 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1529 pd_status2str (status
));
1531 /* Collect general-purpose register values from the regcache. */
1533 for (i
= 0; i
< ppc_num_gprs
; i
++)
1534 if (REG_VALID
== regcache_register_status (regcache
,
1535 tdep
->ppc_gp0_regnum
+ i
))
1539 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ i
,
1545 regcache_raw_collect (regcache
, tdep
->ppc_gp0_regnum
+ i
,
1551 /* Collect floating-point register values from the regcache. */
1552 if (ppc_floating_point_unit_p (gdbarch
))
1553 fill_fprs (regcache
, ctx
.fpr
);
1555 /* Special registers (always kept in ctx as 64 bits). */
1558 fill_sprs64 (regcache
, &ctx
.iar
, &ctx
.msr
, &ctx
.cr
, &ctx
.lr
, &ctx
.ctr
,
1559 &ctx
.xer
, &ctx
.fpscr
);
1563 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1564 Solution: use 32-bit temp variables. */
1565 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1568 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
, &tmp_ctr
,
1569 &tmp_xer
, &tmp_fpscr
);
1570 if (REG_VALID
== regcache_register_status (regcache
,
1571 gdbarch_pc_regnum (gdbarch
)))
1573 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_ps_regnum
))
1575 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_cr_regnum
))
1577 if (REG_VALID
== regcache_register_status (regcache
, tdep
->ppc_lr_regnum
))
1579 if (REG_VALID
== regcache_register_status (regcache
,
1580 tdep
->ppc_ctr_regnum
))
1582 if (REG_VALID
== regcache_register_status (regcache
,
1583 tdep
->ppc_xer_regnum
))
1585 if (REG_VALID
== regcache_register_status (regcache
,
1586 tdep
->ppc_xer_regnum
))
1587 ctx
.fpscr
= tmp_fpscr
;
1590 status
= pthdb_pthread_setcontext (pd_session
, pdtid
, &ctx
);
1591 if (status
!= PTHDB_SUCCESS
)
1592 error (_("aix-thread: store_registers: "
1593 "pthdb_pthread_setcontext returned %s"),
1594 pd_status2str (status
));
1597 /* Store register REGNO if != -1 or all registers otherwise into
1600 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1601 SPRs, but there's no way to set individual registers within those
1602 groups. Therefore, if REGNO != -1, this function stores an entire
1606 store_regs_kernel_thread (const struct regcache
*regcache
, int regno
,
1609 struct gdbarch
*gdbarch
= regcache
->arch ();
1610 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1611 uint64_t gprs64
[ppc_num_gprs
];
1612 uint32_t gprs32
[ppc_num_gprs
];
1613 double fprs
[ppc_num_fprs
];
1614 struct ptxsprs sprs64
;
1615 struct ptsprs sprs32
;
1618 if (debug_aix_thread
)
1619 fprintf_unfiltered (gdb_stdlog
,
1620 "store_regs_kernel_thread tid=%lx regno=%d\n",
1623 /* General-purpose registers. */
1625 || (tdep
->ppc_gp0_regnum
<= regno
1626 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_fprs
))
1630 /* Pre-fetch: some regs may not be in the cache. */
1631 ptrace64aix (PTT_READ_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1632 fill_gprs64 (regcache
, gprs64
);
1633 ptrace64aix (PTT_WRITE_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1637 /* Pre-fetch: some regs may not be in the cache. */
1638 ptrace32 (PTT_READ_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
);
1639 fill_gprs32 (regcache
, gprs32
);
1640 ptrace32 (PTT_WRITE_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
);
1644 /* Floating-point registers. */
1646 if (ppc_floating_point_unit_p (gdbarch
)
1648 || (regno
>= tdep
->ppc_fp0_regnum
1649 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1651 /* Pre-fetch: some regs may not be in the cache. */
1652 ptrace32 (PTT_READ_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
);
1653 fill_fprs (regcache
, fprs
);
1654 ptrace32 (PTT_WRITE_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
);
1657 /* Special-purpose registers. */
1659 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1663 /* Pre-fetch: some registers won't be in the cache. */
1664 ptrace64aix (PTT_READ_SPRS
, tid
,
1665 (unsigned long) &sprs64
, 0, NULL
);
1666 fill_sprs64 (regcache
, &sprs64
.pt_iar
, &sprs64
.pt_msr
,
1667 &sprs64
.pt_cr
, &sprs64
.pt_lr
, &sprs64
.pt_ctr
,
1668 &sprs64
.pt_xer
, &sprs64
.pt_fpscr
);
1669 ptrace64aix (PTT_WRITE_SPRS
, tid
,
1670 (unsigned long) &sprs64
, 0, NULL
);
1674 /* The contents of "struct ptspr" were declared as "unsigned
1675 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1676 Use temporaries to work around this problem. Also, add an
1677 assert here to make sure we fail if the system header files
1678 use "unsigned long", and the size of that type is not what
1679 the headers expect. */
1680 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1683 gdb_assert (sizeof (sprs32
.pt_iar
) == 4);
1685 /* Pre-fetch: some registers won't be in the cache. */
1686 ptrace32 (PTT_READ_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
);
1688 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
,
1689 &tmp_ctr
, &tmp_xer
, &tmp_fpscr
);
1691 sprs32
.pt_iar
= tmp_iar
;
1692 sprs32
.pt_msr
= tmp_msr
;
1693 sprs32
.pt_cr
= tmp_cr
;
1694 sprs32
.pt_lr
= tmp_lr
;
1695 sprs32
.pt_ctr
= tmp_ctr
;
1696 sprs32
.pt_xer
= tmp_xer
;
1697 sprs32
.pt_fpscr
= tmp_fpscr
;
1699 if (tdep
->ppc_mq_regnum
>= 0)
1700 if (REG_VALID
== regcache_register_status (regcache
,
1701 tdep
->ppc_mq_regnum
))
1702 regcache_raw_collect (regcache
, tdep
->ppc_mq_regnum
,
1705 ptrace32 (PTT_WRITE_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
);
1710 /* Store gdb's current view of the register set into the
1711 thread/process connected to REGCACHE. */
1714 aix_thread_target::store_registers (struct regcache
*regcache
, int regno
)
1716 struct thread_info
*thread
;
1718 struct target_ops
*beneath
= find_target_beneath (this);
1720 if (!PD_TID (regcache_get_ptid (regcache
)))
1721 beneath
->store_registers (regcache
, regno
);
1724 thread
= find_thread_ptid (regcache_get_ptid (regcache
));
1725 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1728 if (tid
== PTHDB_INVALID_TID
)
1729 store_regs_user_thread (regcache
, priv
->pdtid
);
1731 store_regs_kernel_thread (regcache
, regno
, tid
);
1735 /* Implement the to_xfer_partial target_ops method. */
1737 enum target_xfer_status
1738 aix_thread_target::xfer_partial (enum target_object object
,
1739 const char *annex
, gdb_byte
*readbuf
,
1740 const gdb_byte
*writebuf
,
1741 ULONGEST offset
, ULONGEST len
,
1742 ULONGEST
*xfered_len
)
1744 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
1745 struct target_ops
*beneath
= find_target_beneath (this);
1747 inferior_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
1748 return beneath
->xfer_partial (object
, annex
, readbuf
,
1749 writebuf
, offset
, len
, xfered_len
);
1752 /* Clean up after the inferior exits. */
1755 aix_thread_target::mourn_inferior ()
1757 struct target_ops
*beneath
= find_target_beneath (this);
1760 beneath
->mourn_inferior ();
1763 /* Return whether thread PID is still valid. */
1766 aix_thread_target::thread_alive (ptid_t ptid
)
1768 struct target_ops
*beneath
= find_target_beneath (this);
1771 return beneath
->thread_alive (ptid
);
1773 /* We update the thread list every time the child stops, so all
1774 valid threads should be in the thread list. */
1775 return in_thread_list (ptid
);
1778 /* Return a printable representation of composite PID for use in
1779 "info threads" output. */
1782 aix_thread_target::pid_to_str (ptid_t ptid
)
1784 static char *ret
= NULL
;
1785 struct target_ops
*beneath
= find_target_beneath (this);
1788 return beneath
->pid_to_str (ptid
);
1790 /* Free previous return value; a new one will be allocated by
1794 ret
= xstrprintf (_("Thread %ld"), ptid_get_tid (ptid
));
1798 /* Return a printable representation of extra information about
1799 THREAD, for use in "info threads" output. */
1802 aix_thread_target::extra_thread_info (struct thread_info
*thread
)
1805 pthdb_pthread_t pdtid
;
1807 pthdb_state_t state
;
1808 pthdb_suspendstate_t suspendstate
;
1809 pthdb_detachstate_t detachstate
;
1811 static char *ret
= NULL
;
1813 if (!PD_TID (thread
->ptid
))
1817 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1819 pdtid
= priv
->pdtid
;
1822 if (tid
!= PTHDB_INVALID_TID
)
1823 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1824 buf
.printf (_("tid %d"), (int)tid
);
1826 status
= pthdb_pthread_state (pd_session
, pdtid
, &state
);
1827 if (status
!= PTHDB_SUCCESS
)
1829 buf
.printf (", %s", state2str (state
));
1831 status
= pthdb_pthread_suspendstate (pd_session
, pdtid
,
1833 if (status
== PTHDB_SUCCESS
&& suspendstate
== PSS_SUSPENDED
)
1834 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1835 buf
.printf (_(", suspended"));
1837 status
= pthdb_pthread_detachstate (pd_session
, pdtid
,
1839 if (status
== PTHDB_SUCCESS
&& detachstate
== PDS_DETACHED
)
1840 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1841 buf
.printf (_(", detached"));
1843 pthdb_pthread_cancelpend (pd_session
, pdtid
, &cancelpend
);
1844 if (status
== PTHDB_SUCCESS
&& cancelpend
)
1845 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1846 buf
.printf (_(", cancel pending"));
1850 xfree (ret
); /* Free old buffer. */
1852 ret
= xstrdup (buf
.c_str ());
1858 aix_thread_target::get_ada_task_ptid (long lwp
, long thread
)
1860 return ptid_build (ptid_get_pid (inferior_ptid
), 0, thread
);
1864 /* Module startup initialization function, automagically called by
1868 _initialize_aix_thread (void)
1870 /* Notice when object files get loaded and unloaded. */
1871 gdb::observers::new_objfile
.attach (new_objfile
);
1873 /* Add ourselves to inferior_created event chain.
1874 This is needed to enable the thread target on "attach". */
1875 gdb::observers::inferior_created
.attach (aix_thread_inferior_created
);
1877 add_setshow_boolean_cmd ("aix-thread", class_maintenance
, &debug_aix_thread
,
1878 _("Set debugging of AIX thread module."),
1879 _("Show debugging of AIX thread module."),
1880 _("Enables debugging output (used to debug GDB)."),
1882 /* FIXME: i18n: Debugging of AIX thread
1883 module is \"%d\". */
1884 &setdebuglist
, &showdebuglist
);