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 static const target_info aix_thread_target_info
= {
113 N_("AIX pthread support"),
114 N_("AIX pthread support")
117 class aix_thread_target final
: public target_ops
121 { to_stratum
= thread_stratum
; }
123 const target_info
&info () const override
124 { return aix_thread_target_info
; }
126 void detach (inferior
*, int) override
;
127 void resume (ptid_t
, int, enum gdb_signal
) override
;
128 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
130 void fetch_registers (struct regcache
*, int) override
;
131 void store_registers (struct regcache
*, int) override
;
133 enum target_xfer_status
xfer_partial (enum target_object object
,
136 const gdb_byte
*writebuf
,
137 ULONGEST offset
, ULONGEST len
,
138 ULONGEST
*xfered_len
) override
;
140 void mourn_inferior () override
;
142 bool thread_alive (ptid_t ptid
) override
;
144 const char *pid_to_str (ptid_t
) override
;
146 const char *extra_thread_info (struct thread_info
*) override
;
148 ptid_t
get_ada_task_ptid (long lwp
, long thread
) override
;
151 static aix_thread_target aix_thread_ops
;
153 /* Address of the function that libpthread will call when libpthdebug
154 is ready to be initialized. */
156 static CORE_ADDR pd_brk_addr
;
158 /* Whether the current application is debuggable by pthdb. */
160 static int pd_able
= 0;
162 /* Whether a threaded application is being debugged. */
164 static int pd_active
= 0;
166 /* Whether the current architecture is 64-bit.
167 Only valid when pd_able is true. */
171 /* Forward declarations for pthdb callbacks. */
173 static int pdc_symbol_addrs (pthdb_user_t
, pthdb_symbol_t
*, int);
174 static int pdc_read_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
175 static int pdc_write_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
176 static int pdc_read_regs (pthdb_user_t user
, pthdb_tid_t tid
,
177 unsigned long long flags
,
178 pthdb_context_t
*context
);
179 static int pdc_write_regs (pthdb_user_t user
, pthdb_tid_t tid
,
180 unsigned long long flags
,
181 pthdb_context_t
*context
);
182 static int pdc_alloc (pthdb_user_t
, size_t, void **);
183 static int pdc_realloc (pthdb_user_t
, void *, size_t, void **);
184 static int pdc_dealloc (pthdb_user_t
, void *);
186 /* pthdb callbacks. */
188 static pthdb_callbacks_t pd_callbacks
= {
200 /* Current pthdb session. */
202 static pthdb_session_t pd_session
;
204 /* Return a printable representation of pthdebug function return
208 pd_status2str (int status
)
212 case PTHDB_SUCCESS
: return "SUCCESS";
213 case PTHDB_NOSYS
: return "NOSYS";
214 case PTHDB_NOTSUP
: return "NOTSUP";
215 case PTHDB_BAD_VERSION
: return "BAD_VERSION";
216 case PTHDB_BAD_USER
: return "BAD_USER";
217 case PTHDB_BAD_SESSION
: return "BAD_SESSION";
218 case PTHDB_BAD_MODE
: return "BAD_MODE";
219 case PTHDB_BAD_FLAGS
: return "BAD_FLAGS";
220 case PTHDB_BAD_CALLBACK
: return "BAD_CALLBACK";
221 case PTHDB_BAD_POINTER
: return "BAD_POINTER";
222 case PTHDB_BAD_CMD
: return "BAD_CMD";
223 case PTHDB_BAD_PTHREAD
: return "BAD_PTHREAD";
224 case PTHDB_BAD_ATTR
: return "BAD_ATTR";
225 case PTHDB_BAD_MUTEX
: return "BAD_MUTEX";
226 case PTHDB_BAD_MUTEXATTR
: return "BAD_MUTEXATTR";
227 case PTHDB_BAD_COND
: return "BAD_COND";
228 case PTHDB_BAD_CONDATTR
: return "BAD_CONDATTR";
229 case PTHDB_BAD_RWLOCK
: return "BAD_RWLOCK";
230 case PTHDB_BAD_RWLOCKATTR
: return "BAD_RWLOCKATTR";
231 case PTHDB_BAD_KEY
: return "BAD_KEY";
232 case PTHDB_BAD_PTID
: return "BAD_PTID";
233 case PTHDB_BAD_TID
: return "BAD_TID";
234 case PTHDB_CALLBACK
: return "CALLBACK";
235 case PTHDB_CONTEXT
: return "CONTEXT";
236 case PTHDB_HELD
: return "HELD";
237 case PTHDB_NOT_HELD
: return "NOT_HELD";
238 case PTHDB_MEMORY
: return "MEMORY";
239 case PTHDB_NOT_PTHREADED
: return "NOT_PTHREADED";
240 case PTHDB_SYMBOL
: return "SYMBOL";
241 case PTHDB_NOT_AVAIL
: return "NOT_AVAIL";
242 case PTHDB_INTERNAL
: return "INTERNAL";
243 default: return "UNKNOWN";
247 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
248 exceptional conditions and either return nonlocally or else return
249 1 for success and 0 for failure. */
252 ptrace_check (int req
, int id
, int ret
)
254 if (ret
== 0 && !errno
)
257 /* According to ptrace(2), ptrace may fail with EPERM if "the
258 Identifier parameter corresponds to a kernel thread which is
259 stopped in kernel mode and whose computational state cannot be
260 read or written." This happens quite often with register reads. */
267 if (ret
== -1 && errno
== EPERM
)
269 if (debug_aix_thread
)
270 fprintf_unfiltered (gdb_stdlog
,
271 "ptrace (%d, %d) = %d (errno = %d)\n",
272 req
, id
, ret
, errno
);
273 return ret
== -1 ? 0 : 1;
277 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
278 req
, id
, ret
, errno
, safe_strerror (errno
));
279 return 0; /* Not reached. */
282 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
283 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
287 # define ptracex(request, pid, addr, data, buf) \
288 ptrace64 (request, pid, addr, data, buf)
292 ptrace64aix (int req
, int id
, long long addr
, int data
, int *buf
)
295 return ptrace_check (req
, id
, ptracex (req
, id
, addr
, data
, buf
));
298 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
299 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
303 # define ptrace(request, pid, addr, data, buf) \
304 ptrace64 (request, pid, addr, data, buf)
305 # define addr_ptr long long
307 # define addr_ptr int *
311 ptrace32 (int req
, int id
, addr_ptr addr
, int data
, int *buf
)
314 return ptrace_check (req
, id
,
315 ptrace (req
, id
, addr
, data
, buf
));
318 /* If *PIDP is a composite process/thread id, convert it to a
322 pid_to_prc (ptid_t
*ptidp
)
328 *ptidp
= pid_to_ptid (ptid_get_pid (ptid
));
331 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
332 the address of SYMBOLS[<i>].name. */
335 pdc_symbol_addrs (pthdb_user_t user
, pthdb_symbol_t
*symbols
, int count
)
337 struct bound_minimal_symbol ms
;
341 if (debug_aix_thread
)
342 fprintf_unfiltered (gdb_stdlog
,
343 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
344 user
, (long) symbols
, count
);
346 for (i
= 0; i
< count
; i
++)
348 name
= symbols
[i
].name
;
349 if (debug_aix_thread
)
350 fprintf_unfiltered (gdb_stdlog
,
351 " symbols[%d].name = \"%s\"\n", i
, name
);
357 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
358 if (ms
.minsym
== NULL
)
360 if (debug_aix_thread
)
361 fprintf_unfiltered (gdb_stdlog
, " returning PDC_FAILURE\n");
364 symbols
[i
].addr
= BMSYMBOL_VALUE_ADDRESS (ms
);
366 if (debug_aix_thread
)
367 fprintf_unfiltered (gdb_stdlog
, " symbols[%d].addr = %s\n",
368 i
, hex_string (symbols
[i
].addr
));
370 if (debug_aix_thread
)
371 fprintf_unfiltered (gdb_stdlog
, " returning PDC_SUCCESS\n");
375 /* Read registers call back function should be able to read the
376 context information of a debuggee kernel thread from an active
377 process or from a core file. The information should be formatted
378 in context64 form for both 32-bit and 64-bit process.
379 If successful return 0, else non-zero is returned. */
382 pdc_read_regs (pthdb_user_t user
,
384 unsigned long long flags
,
385 pthdb_context_t
*context
)
387 /* This function doesn't appear to be used, so we could probably
388 just return 0 here. HOWEVER, if it is not defined, the OS will
389 complain and several thread debug functions will fail. In case
390 this is needed, I have implemented what I think it should do,
391 however this code is untested. */
393 uint64_t gprs64
[ppc_num_gprs
];
394 uint32_t gprs32
[ppc_num_gprs
];
395 double fprs
[ppc_num_fprs
];
396 struct ptxsprs sprs64
;
397 struct ptsprs sprs32
;
399 if (debug_aix_thread
)
400 fprintf_unfiltered (gdb_stdlog
, "pdc_read_regs tid=%d flags=%s\n",
401 (int) tid
, hex_string (flags
));
403 /* General-purpose registers. */
404 if (flags
& PTHDB_FLAG_GPRS
)
408 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
409 (unsigned long) gprs64
, 0, NULL
))
410 memset (gprs64
, 0, sizeof (gprs64
));
411 memcpy (context
->gpr
, gprs64
, sizeof(gprs64
));
415 if (!ptrace32 (PTT_READ_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
))
416 memset (gprs32
, 0, sizeof (gprs32
));
417 memcpy (context
->gpr
, gprs32
, sizeof(gprs32
));
421 /* Floating-point registers. */
422 if (flags
& PTHDB_FLAG_FPRS
)
424 if (!ptrace32 (PTT_READ_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
))
425 memset (fprs
, 0, sizeof (fprs
));
426 memcpy (context
->fpr
, fprs
, sizeof(fprs
));
429 /* Special-purpose registers. */
430 if (flags
& PTHDB_FLAG_SPRS
)
434 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
435 (unsigned long) &sprs64
, 0, NULL
))
436 memset (&sprs64
, 0, sizeof (sprs64
));
437 memcpy (&context
->msr
, &sprs64
, sizeof(sprs64
));
441 if (!ptrace32 (PTT_READ_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
))
442 memset (&sprs32
, 0, sizeof (sprs32
));
443 memcpy (&context
->msr
, &sprs32
, sizeof(sprs32
));
449 /* Write register function should be able to write requested context
450 information to specified debuggee's kernel thread id.
451 If successful return 0, else non-zero is returned. */
454 pdc_write_regs (pthdb_user_t user
,
456 unsigned long long flags
,
457 pthdb_context_t
*context
)
459 /* This function doesn't appear to be used, so we could probably
460 just return 0 here. HOWEVER, if it is not defined, the OS will
461 complain and several thread debug functions will fail. In case
462 this is needed, I have implemented what I think it should do,
463 however this code is untested. */
465 if (debug_aix_thread
)
466 fprintf_unfiltered (gdb_stdlog
, "pdc_write_regs tid=%d flags=%s\n",
467 (int) tid
, hex_string (flags
));
469 /* General-purpose registers. */
470 if (flags
& PTHDB_FLAG_GPRS
)
473 ptrace64aix (PTT_WRITE_GPRS
, tid
,
474 (unsigned long) context
->gpr
, 0, NULL
);
476 ptrace32 (PTT_WRITE_GPRS
, tid
, (uintptr_t) context
->gpr
, 0, NULL
);
479 /* Floating-point registers. */
480 if (flags
& PTHDB_FLAG_FPRS
)
482 ptrace32 (PTT_WRITE_FPRS
, tid
, (uintptr_t) context
->fpr
, 0, NULL
);
485 /* Special-purpose registers. */
486 if (flags
& PTHDB_FLAG_SPRS
)
490 ptrace64aix (PTT_WRITE_SPRS
, tid
,
491 (unsigned long) &context
->msr
, 0, NULL
);
495 ptrace32 (PTT_WRITE_SPRS
, tid
, (uintptr_t) &context
->msr
, 0, NULL
);
501 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
504 pdc_read_data (pthdb_user_t user
, void *buf
,
505 pthdb_addr_t addr
, size_t len
)
509 if (debug_aix_thread
)
510 fprintf_unfiltered (gdb_stdlog
,
511 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
512 user
, (long) buf
, hex_string (addr
), len
);
514 status
= target_read_memory (addr
, (gdb_byte
*) buf
, len
);
515 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
517 if (debug_aix_thread
)
518 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n",
519 status
, pd_status2str (ret
));
523 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
526 pdc_write_data (pthdb_user_t user
, void *buf
,
527 pthdb_addr_t addr
, size_t len
)
531 if (debug_aix_thread
)
532 fprintf_unfiltered (gdb_stdlog
,
533 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
534 user
, (long) buf
, hex_string (addr
), len
);
536 status
= target_write_memory (addr
, (gdb_byte
*) buf
, len
);
537 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
539 if (debug_aix_thread
)
540 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n", status
,
541 pd_status2str (ret
));
545 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
549 pdc_alloc (pthdb_user_t user
, size_t len
, void **bufp
)
551 if (debug_aix_thread
)
552 fprintf_unfiltered (gdb_stdlog
,
553 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
554 user
, len
, (long) bufp
);
555 *bufp
= xmalloc (len
);
556 if (debug_aix_thread
)
557 fprintf_unfiltered (gdb_stdlog
,
558 " malloc returned 0x%lx\n", (long) *bufp
);
560 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
563 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
566 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
567 realloc callback, so that it contains LEN bytes, and store a
568 pointer to the result in BUFP. */
571 pdc_realloc (pthdb_user_t user
, void *buf
, size_t len
, void **bufp
)
573 if (debug_aix_thread
)
574 fprintf_unfiltered (gdb_stdlog
,
575 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
576 user
, (long) buf
, len
, (long) bufp
);
577 *bufp
= xrealloc (buf
, len
);
578 if (debug_aix_thread
)
579 fprintf_unfiltered (gdb_stdlog
,
580 " realloc returned 0x%lx\n", (long) *bufp
);
581 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
584 /* pthdb callback: free BUF, which was allocated by the alloc or
588 pdc_dealloc (pthdb_user_t user
, void *buf
)
590 if (debug_aix_thread
)
591 fprintf_unfiltered (gdb_stdlog
,
592 "pdc_free (user = %ld, buf = 0x%lx)\n", user
,
598 /* Return a printable representation of pthread STATE. */
601 state2str (pthdb_state_t state
)
606 /* i18n: Like "Thread-Id %d, [state] idle" */
607 return _("idle"); /* being created */
609 /* i18n: Like "Thread-Id %d, [state] running" */
610 return _("running"); /* running */
612 /* i18n: Like "Thread-Id %d, [state] sleeping" */
613 return _("sleeping"); /* awaiting an event */
615 /* i18n: Like "Thread-Id %d, [state] ready" */
616 return _("ready"); /* runnable */
618 /* i18n: Like "Thread-Id %d, [state] finished" */
619 return _("finished"); /* awaiting a join/detach */
621 /* i18n: Like "Thread-Id %d, [state] unknown" */
626 /* qsort() comparison function for sorting pd_thread structs by pthid. */
629 pcmp (const void *p1v
, const void *p2v
)
631 struct pd_thread
*p1
= (struct pd_thread
*) p1v
;
632 struct pd_thread
*p2
= (struct pd_thread
*) p2v
;
633 return p1
->pthid
< p2
->pthid
? -1 : p1
->pthid
> p2
->pthid
;
636 /* iterate_over_threads() callback for counting GDB threads.
638 Do not count the main thread (whose tid is zero). This matches
639 the list of threads provided by the pthreaddebug library, which
640 does not include that main thread either, and thus allows us
641 to compare the two lists. */
644 giter_count (struct thread_info
*thread
, void *countp
)
646 if (PD_TID (thread
->ptid
))
651 /* iterate_over_threads() callback for accumulating GDB thread pids.
653 Do not include the main thread (whose tid is zero). This matches
654 the list of threads provided by the pthreaddebug library, which
655 does not include that main thread either, and thus allows us
656 to compare the two lists. */
659 giter_accum (struct thread_info
*thread
, void *bufp
)
661 if (PD_TID (thread
->ptid
))
663 **(struct thread_info
***) bufp
= thread
;
664 (*(struct thread_info
***) bufp
)++;
669 /* ptid comparison function */
672 ptid_cmp (ptid_t ptid1
, ptid_t ptid2
)
676 if (ptid_get_pid (ptid1
) < ptid_get_pid (ptid2
))
678 else if (ptid_get_pid (ptid1
) > ptid_get_pid (ptid2
))
680 else if (ptid_get_tid (ptid1
) < ptid_get_tid (ptid2
))
682 else if (ptid_get_tid (ptid1
) > ptid_get_tid (ptid2
))
684 else if (ptid_get_lwp (ptid1
) < ptid_get_lwp (ptid2
))
686 else if (ptid_get_lwp (ptid1
) > ptid_get_lwp (ptid2
))
692 /* qsort() comparison function for sorting thread_info structs by pid. */
695 gcmp (const void *t1v
, const void *t2v
)
697 struct thread_info
*t1
= *(struct thread_info
**) t1v
;
698 struct thread_info
*t2
= *(struct thread_info
**) t2v
;
699 return ptid_cmp (t1
->ptid
, t2
->ptid
);
702 /* Search through the list of all kernel threads for the thread
703 that has stopped on a SIGTRAP signal, and return its TID.
704 Return 0 if none found. */
707 get_signaled_thread (void)
709 struct thrdsinfo64 thrinf
;
715 if (getthrds (ptid_get_pid (inferior_ptid
), &thrinf
,
716 sizeof (thrinf
), &ktid
, 1) != 1)
719 if (thrinf
.ti_cursig
== SIGTRAP
)
720 return thrinf
.ti_tid
;
723 /* Didn't find any thread stopped on a SIGTRAP signal. */
727 /* Synchronize GDB's thread list with libpthdebug's.
729 There are some benefits of doing this every time the inferior stops:
731 - allows users to run thread-specific commands without needing to
732 run "info threads" first
734 - helps pthdb_tid_pthread() work properly (see "libpthdebug
735 peculiarities" at the top of this module)
737 - simplifies the demands placed on libpthdebug, which seems to
738 have difficulty with certain call patterns */
741 sync_threadlists (void)
743 int cmd
, status
, infpid
;
744 int pcount
, psize
, pi
, gcount
, gi
;
745 struct pd_thread
*pbuf
;
746 struct thread_info
**gbuf
, **g
, *thread
;
747 pthdb_pthread_t pdtid
;
751 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
755 pbuf
= XNEWVEC (struct pd_thread
, psize
);
757 for (cmd
= PTHDB_LIST_FIRST
;; cmd
= PTHDB_LIST_NEXT
)
759 status
= pthdb_pthread (pd_session
, &pdtid
, cmd
);
760 if (status
!= PTHDB_SUCCESS
|| pdtid
== PTHDB_INVALID_PTHREAD
)
763 status
= pthdb_pthread_ptid (pd_session
, pdtid
, &pthid
);
764 if (status
!= PTHDB_SUCCESS
|| pthid
== PTHDB_INVALID_PTID
)
770 pbuf
= (struct pd_thread
*) xrealloc (pbuf
,
771 psize
* sizeof *pbuf
);
773 pbuf
[pcount
].pdtid
= pdtid
;
774 pbuf
[pcount
].pthid
= pthid
;
778 for (pi
= 0; pi
< pcount
; pi
++)
780 status
= pthdb_pthread_tid (pd_session
, pbuf
[pi
].pdtid
, &tid
);
781 if (status
!= PTHDB_SUCCESS
)
782 tid
= PTHDB_INVALID_TID
;
786 qsort (pbuf
, pcount
, sizeof *pbuf
, pcmp
);
788 /* Accumulate an array of GDB threads sorted by pid. */
791 iterate_over_threads (giter_count
, &gcount
);
792 g
= gbuf
= XNEWVEC (struct thread_info
*, gcount
);
793 iterate_over_threads (giter_accum
, &g
);
794 qsort (gbuf
, gcount
, sizeof *gbuf
, gcmp
);
796 /* Apply differences between the two arrays to GDB's thread list. */
798 infpid
= ptid_get_pid (inferior_ptid
);
799 for (pi
= gi
= 0; pi
< pcount
|| gi
< gcount
;)
803 delete_thread (gbuf
[gi
]->ptid
);
806 else if (gi
== gcount
)
808 aix_thread_info
*priv
= new aix_thread_info
;
809 priv
->pdtid
= pbuf
[pi
].pdtid
;
810 priv
->tid
= pbuf
[pi
].tid
;
812 thread
= add_thread_with_info (ptid_t (infpid
, 0, pbuf
[pi
].pthid
), priv
);
821 pptid
= ptid_build (infpid
, 0, pbuf
[pi
].pthid
);
822 gptid
= gbuf
[gi
]->ptid
;
823 pdtid
= pbuf
[pi
].pdtid
;
826 cmp_result
= ptid_cmp (pptid
, gptid
);
830 aix_thread_info
*priv
= get_aix_thread_info (gbuf
[gi
]);
837 else if (cmp_result
> 0)
839 delete_thread (gptid
);
844 thread
= add_thread (pptid
);
846 aix_thread_info
*priv
= new aix_thread_info
;
847 thread
->priv
.reset (priv
);
859 /* Iterate_over_threads() callback for locating a thread, using
860 the TID of its associated kernel thread. */
863 iter_tid (struct thread_info
*thread
, void *tidp
)
865 const pthdb_tid_t tid
= *(pthdb_tid_t
*)tidp
;
866 aix_thread_info
*priv
= get_aix_thread_info (thread
);
868 return priv
->tid
== tid
;
871 /* Synchronize libpthdebug's state with the inferior and with GDB,
872 generate a composite process/thread <pid> for the current thread,
873 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
876 pd_update (int set_infpid
)
881 struct thread_info
*thread
= NULL
;
884 return inferior_ptid
;
886 status
= pthdb_session_update (pd_session
);
887 if (status
!= PTHDB_SUCCESS
)
888 return inferior_ptid
;
892 /* Define "current thread" as one that just received a trap signal. */
894 tid
= get_signaled_thread ();
896 thread
= iterate_over_threads (iter_tid
, &tid
);
898 ptid
= inferior_ptid
;
903 inferior_ptid
= ptid
;
908 /* Try to start debugging threads in the current process.
909 If successful and SET_INFPID, set inferior_ptid to reflect the
913 pd_activate (int set_infpid
)
917 status
= pthdb_session_init (PD_USER
, arch64
? PEM_64BIT
: PEM_32BIT
,
918 PTHDB_FLAG_REGS
, &pd_callbacks
,
920 if (status
!= PTHDB_SUCCESS
)
922 return inferior_ptid
;
925 return pd_update (set_infpid
);
928 /* Undo the effects of pd_activate(). */
935 pthdb_session_destroy (pd_session
);
937 pid_to_prc (&inferior_ptid
);
941 /* An object file has just been loaded. Check whether the current
942 application is pthreaded, and if so, prepare for thread debugging. */
949 struct bound_minimal_symbol ms
;
951 /* Don't initialize twice. */
955 /* Check application word size. */
956 arch64
= register_size (target_gdbarch (), 0) == 8;
958 /* Check whether the application is pthreaded. */
960 status
= pthdb_session_pthreaded (PD_USER
, PTHDB_FLAG_REGS
,
961 &pd_callbacks
, &stub_name
);
962 if ((status
!= PTHDB_SUCCESS
963 && status
!= PTHDB_NOT_PTHREADED
) || !stub_name
)
966 /* Set a breakpoint on the returned stub function. */
967 ms
= lookup_minimal_symbol (stub_name
, NULL
, NULL
);
968 if (ms
.minsym
== NULL
)
970 pd_brk_addr
= BMSYMBOL_VALUE_ADDRESS (ms
);
971 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr
))
974 /* Prepare for thread debugging. */
975 push_target (&aix_thread_ops
);
978 /* If we're debugging a core file or an attached inferior, the
979 pthread library may already have been initialized, so try to
980 activate thread debugging. */
984 /* Undo the effects of pd_enable(). */
994 unpush_target (&aix_thread_ops
);
997 /* new_objfile observer callback.
999 If OBJFILE is non-null, check whether a threaded application is
1000 being debugged, and if so, prepare for thread debugging.
1002 If OBJFILE is null, stop debugging threads. */
1005 new_objfile (struct objfile
*objfile
)
1013 /* Attach to process specified by ARGS. */
1016 aix_thread_inferior_created (struct target_ops
*ops
, int from_tty
)
1021 /* Detach from the process attached to by aix_thread_attach(). */
1024 aix_thread_target::detach (inferior
*inf
, int from_tty
)
1026 struct target_ops
*beneath
= find_target_beneath (this);
1029 beneath
->detach (inf
, from_tty
);
1032 /* Tell the inferior process to continue running thread PID if != -1
1033 and all threads otherwise. */
1036 aix_thread_target::resume (ptid_t ptid
, int step
, enum gdb_signal sig
)
1038 struct thread_info
*thread
;
1043 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
1044 struct target_ops
*beneath
= find_target_beneath (this);
1046 inferior_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
1047 beneath
->resume (ptid
, step
, sig
);
1051 thread
= find_thread_ptid (ptid
);
1053 error (_("aix-thread resume: unknown pthread %ld"),
1054 ptid_get_lwp (ptid
));
1056 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1059 if (tid
[0] == PTHDB_INVALID_TID
)
1060 error (_("aix-thread resume: no tid for pthread %ld"),
1061 ptid_get_lwp (ptid
));
1065 ptrace64aix (PTT_CONTINUE
, tid
[0], (long long) 1,
1066 gdb_signal_to_host (sig
), (PTRACE_TYPE_ARG5
) tid
);
1068 ptrace32 (PTT_CONTINUE
, tid
[0], (addr_ptr
) 1,
1069 gdb_signal_to_host (sig
), (PTRACE_TYPE_ARG5
) tid
);
1073 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1074 If an error occurs, return -1, else return the pid of the stopped
1078 aix_thread_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
1081 struct target_ops
*beneath
= find_target_beneath (this);
1084 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
1088 inferior_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
1089 ptid
= beneath
->wait (ptid
, status
, options
);
1092 if (ptid_get_pid (ptid
) == -1)
1093 return pid_to_ptid (-1);
1095 /* Check whether libpthdebug might be ready to be initialized. */
1096 if (!pd_active
&& status
->kind
== TARGET_WAITKIND_STOPPED
1097 && status
->value
.sig
== GDB_SIGNAL_TRAP
)
1099 struct regcache
*regcache
= get_thread_regcache (ptid
);
1100 struct gdbarch
*gdbarch
= regcache
->arch ();
1102 if (regcache_read_pc (regcache
)
1103 - gdbarch_decr_pc_after_break (gdbarch
) == pd_brk_addr
)
1104 return pd_activate (0);
1107 return pd_update (0);
1110 /* Record that the 64-bit general-purpose registers contain VALS. */
1113 supply_gprs64 (struct regcache
*regcache
, uint64_t *vals
)
1115 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
1118 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1119 regcache
->raw_supply (tdep
->ppc_gp0_regnum
+ regno
,
1120 (char *) (vals
+ regno
));
1123 /* Record that 32-bit register REGNO contains VAL. */
1126 supply_reg32 (struct regcache
*regcache
, int regno
, uint32_t val
)
1128 regcache
->raw_supply (regno
, (char *) &val
);
1131 /* Record that the floating-point registers contain VALS. */
1134 supply_fprs (struct regcache
*regcache
, double *vals
)
1136 struct gdbarch
*gdbarch
= regcache
->arch ();
1137 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1140 /* This function should never be called on architectures without
1141 floating-point registers. */
1142 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1144 for (regno
= tdep
->ppc_fp0_regnum
;
1145 regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
;
1147 regcache
->raw_supply (regno
,
1148 (char *) (vals
+ regno
- tdep
->ppc_fp0_regnum
));
1151 /* Predicate to test whether given register number is a "special" register. */
1153 special_register_p (struct gdbarch
*gdbarch
, int regno
)
1155 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1157 return regno
== gdbarch_pc_regnum (gdbarch
)
1158 || regno
== tdep
->ppc_ps_regnum
1159 || regno
== tdep
->ppc_cr_regnum
1160 || regno
== tdep
->ppc_lr_regnum
1161 || regno
== tdep
->ppc_ctr_regnum
1162 || regno
== tdep
->ppc_xer_regnum
1163 || (tdep
->ppc_fpscr_regnum
>= 0 && regno
== tdep
->ppc_fpscr_regnum
)
1164 || (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
);
1168 /* Record that the special registers contain the specified 64-bit and
1172 supply_sprs64 (struct regcache
*regcache
,
1173 uint64_t iar
, uint64_t msr
, uint32_t cr
,
1174 uint64_t lr
, uint64_t ctr
, uint32_t xer
,
1177 struct gdbarch
*gdbarch
= regcache
->arch ();
1178 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1180 regcache
->raw_supply (gdbarch_pc_regnum (gdbarch
), (char *) &iar
);
1181 regcache
->raw_supply (tdep
->ppc_ps_regnum
, (char *) &msr
);
1182 regcache
->raw_supply (tdep
->ppc_cr_regnum
, (char *) &cr
);
1183 regcache
->raw_supply (tdep
->ppc_lr_regnum
, (char *) &lr
);
1184 regcache
->raw_supply (tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1185 regcache
->raw_supply (tdep
->ppc_xer_regnum
, (char *) &xer
);
1186 if (tdep
->ppc_fpscr_regnum
>= 0)
1187 regcache
->raw_supply (tdep
->ppc_fpscr_regnum
, (char *) &fpscr
);
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 (gdbarch_pc_regnum (gdbarch
), (char *) &iar
);
1203 regcache
->raw_supply (tdep
->ppc_ps_regnum
, (char *) &msr
);
1204 regcache
->raw_supply (tdep
->ppc_cr_regnum
, (char *) &cr
);
1205 regcache
->raw_supply (tdep
->ppc_lr_regnum
, (char *) &lr
);
1206 regcache
->raw_supply (tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1207 regcache
->raw_supply (tdep
->ppc_xer_regnum
, (char *) &xer
);
1208 if (tdep
->ppc_fpscr_regnum
>= 0)
1209 regcache
->raw_supply (tdep
->ppc_fpscr_regnum
, (char *) &fpscr
);
1212 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1215 There's no way to query a single register from a non-kernel
1216 pthread, so there's no need for a single-register version of this
1220 fetch_regs_user_thread (struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1222 struct gdbarch
*gdbarch
= regcache
->arch ();
1223 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1225 pthdb_context_t ctx
;
1227 if (debug_aix_thread
)
1228 fprintf_unfiltered (gdb_stdlog
,
1229 "fetch_regs_user_thread %lx\n", (long) pdtid
);
1230 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1231 if (status
!= PTHDB_SUCCESS
)
1232 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1233 pd_status2str (status
));
1235 /* General-purpose registers. */
1238 supply_gprs64 (regcache
, ctx
.gpr
);
1240 for (i
= 0; i
< ppc_num_gprs
; i
++)
1241 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, ctx
.gpr
[i
]);
1243 /* Floating-point registers. */
1245 if (ppc_floating_point_unit_p (gdbarch
))
1246 supply_fprs (regcache
, ctx
.fpr
);
1248 /* Special registers. */
1251 supply_sprs64 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1252 ctx
.xer
, ctx
.fpscr
);
1254 supply_sprs32 (regcache
, ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
,
1255 ctx
.xer
, ctx
.fpscr
);
1258 /* Fetch register REGNO if != -1 or all registers otherwise from
1261 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1262 SPRs, but there's no way to query individual registers within those
1263 groups. Therefore, if REGNO != -1, this function fetches an entire
1266 Unfortunately, kernel thread register queries often fail with
1267 EPERM, indicating that the thread is in kernel space. This breaks
1268 backtraces of threads other than the current one. To make that
1269 breakage obvious without throwing an error to top level (which is
1270 bad e.g. during "info threads" output), zero registers that can't
1274 fetch_regs_kernel_thread (struct regcache
*regcache
, int regno
,
1277 struct gdbarch
*gdbarch
= regcache
->arch ();
1278 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1279 uint64_t gprs64
[ppc_num_gprs
];
1280 uint32_t gprs32
[ppc_num_gprs
];
1281 double fprs
[ppc_num_fprs
];
1282 struct ptxsprs sprs64
;
1283 struct ptsprs sprs32
;
1286 if (debug_aix_thread
)
1287 fprintf_unfiltered (gdb_stdlog
,
1288 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1289 (long) tid
, regno
, arch64
);
1291 /* General-purpose registers. */
1293 || (tdep
->ppc_gp0_regnum
<= regno
1294 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
))
1298 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
1299 (unsigned long) gprs64
, 0, NULL
))
1300 memset (gprs64
, 0, sizeof (gprs64
));
1301 supply_gprs64 (regcache
, gprs64
);
1305 if (!ptrace32 (PTT_READ_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
))
1306 memset (gprs32
, 0, sizeof (gprs32
));
1307 for (i
= 0; i
< ppc_num_gprs
; i
++)
1308 supply_reg32 (regcache
, tdep
->ppc_gp0_regnum
+ i
, gprs32
[i
]);
1312 /* Floating-point registers. */
1314 if (ppc_floating_point_unit_p (gdbarch
)
1316 || (regno
>= tdep
->ppc_fp0_regnum
1317 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1319 if (!ptrace32 (PTT_READ_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
))
1320 memset (fprs
, 0, sizeof (fprs
));
1321 supply_fprs (regcache
, fprs
);
1324 /* Special-purpose registers. */
1326 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1330 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
1331 (unsigned long) &sprs64
, 0, NULL
))
1332 memset (&sprs64
, 0, sizeof (sprs64
));
1333 supply_sprs64 (regcache
, sprs64
.pt_iar
, sprs64
.pt_msr
,
1334 sprs64
.pt_cr
, sprs64
.pt_lr
, sprs64
.pt_ctr
,
1335 sprs64
.pt_xer
, sprs64
.pt_fpscr
);
1339 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1341 if (!ptrace32 (PTT_READ_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
))
1342 memset (&sprs32
, 0, sizeof (sprs32
));
1343 supply_sprs32 (regcache
, sprs32
.pt_iar
, sprs32
.pt_msr
, sprs32
.pt_cr
,
1344 sprs32
.pt_lr
, sprs32
.pt_ctr
, sprs32
.pt_xer
,
1347 if (tdep
->ppc_mq_regnum
>= 0)
1348 regcache
->raw_supply (tdep
->ppc_mq_regnum
, (char *) &sprs32
.pt_mq
);
1353 /* Fetch register REGNO if != -1 or all registers otherwise from the
1354 thread/process connected to REGCACHE. */
1357 aix_thread_target::fetch_registers (struct regcache
*regcache
, int regno
)
1359 struct thread_info
*thread
;
1361 struct target_ops
*beneath
= find_target_beneath (this);
1363 if (!PD_TID (regcache
->ptid ()))
1364 beneath
->fetch_registers (regcache
, regno
);
1367 thread
= find_thread_ptid (regcache
->ptid ());
1368 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1371 if (tid
== PTHDB_INVALID_TID
)
1372 fetch_regs_user_thread (regcache
, priv
->pdtid
);
1374 fetch_regs_kernel_thread (regcache
, regno
, tid
);
1378 /* Store the gp registers into an array of uint32_t or uint64_t. */
1381 fill_gprs64 (const struct regcache
*regcache
, uint64_t *vals
)
1383 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
1386 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1387 if (REG_VALID
== regcache
->get_register_status
1388 (tdep
->ppc_gp0_regnum
+ regno
))
1389 regcache
->raw_collect (tdep
->ppc_gp0_regnum
+ regno
, vals
+ regno
);
1393 fill_gprs32 (const struct regcache
*regcache
, uint32_t *vals
)
1395 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
1398 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1399 if (REG_VALID
== regcache
->get_register_status
1400 (tdep
->ppc_gp0_regnum
+ regno
))
1401 regcache
->raw_collect (tdep
->ppc_gp0_regnum
+ regno
, vals
+ regno
);
1404 /* Store the floating point registers into a double array. */
1406 fill_fprs (const struct regcache
*regcache
, double *vals
)
1408 struct gdbarch
*gdbarch
= regcache
->arch ();
1409 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1412 /* This function should never be called on architectures without
1413 floating-point registers. */
1414 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
1416 for (regno
= tdep
->ppc_fp0_regnum
;
1417 regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
;
1419 if (REG_VALID
== regcache
->get_register_status (regno
))
1420 regcache
->raw_collect (regno
, vals
+ regno
- tdep
->ppc_fp0_regnum
);
1423 /* Store the special registers into the specified 64-bit and 32-bit
1427 fill_sprs64 (const struct regcache
*regcache
,
1428 uint64_t *iar
, uint64_t *msr
, uint32_t *cr
,
1429 uint64_t *lr
, uint64_t *ctr
, uint32_t *xer
,
1432 struct gdbarch
*gdbarch
= regcache
->arch ();
1433 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1435 /* Verify that the size of the size of the IAR buffer is the
1436 same as the raw size of the PC (in the register cache). If
1437 they're not, then either GDB has been built incorrectly, or
1438 there's some other kind of internal error. To be really safe,
1439 we should check all of the sizes. */
1440 gdb_assert (sizeof (*iar
) == register_size
1441 (gdbarch
, gdbarch_pc_regnum (gdbarch
)));
1443 if (REG_VALID
== regcache
->get_register_status (gdbarch_pc_regnum (gdbarch
)))
1444 regcache
->raw_collect (gdbarch_pc_regnum (gdbarch
), iar
);
1445 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_ps_regnum
))
1446 regcache
->raw_collect (tdep
->ppc_ps_regnum
, msr
);
1447 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_cr_regnum
))
1448 regcache
->raw_collect (tdep
->ppc_cr_regnum
, cr
);
1449 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_lr_regnum
))
1450 regcache
->raw_collect (tdep
->ppc_lr_regnum
, lr
);
1451 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_ctr_regnum
))
1452 regcache
->raw_collect (tdep
->ppc_ctr_regnum
, ctr
);
1453 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_xer_regnum
))
1454 regcache
->raw_collect (tdep
->ppc_xer_regnum
, xer
);
1455 if (tdep
->ppc_fpscr_regnum
>= 0
1456 && REG_VALID
== regcache
->get_register_status (tdep
->ppc_fpscr_regnum
))
1457 regcache
->raw_collect (tdep
->ppc_fpscr_regnum
, fpscr
);
1461 fill_sprs32 (const struct regcache
*regcache
,
1462 uint32_t *iar
, uint32_t *msr
, uint32_t *cr
,
1463 uint32_t *lr
, uint32_t *ctr
, uint32_t *xer
,
1466 struct gdbarch
*gdbarch
= regcache
->arch ();
1467 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1469 /* Verify that the size of the size of the IAR buffer is the
1470 same as the raw size of the PC (in the register cache). If
1471 they're not, then either GDB has been built incorrectly, or
1472 there's some other kind of internal error. To be really safe,
1473 we should check all of the sizes. */
1474 gdb_assert (sizeof (*iar
) == register_size (gdbarch
,
1475 gdbarch_pc_regnum (gdbarch
)));
1477 if (REG_VALID
== regcache
->get_register_status (gdbarch_pc_regnum (gdbarch
)))
1478 regcache
->raw_collect (gdbarch_pc_regnum (gdbarch
), iar
);
1479 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_ps_regnum
))
1480 regcache
->raw_collect (tdep
->ppc_ps_regnum
, msr
);
1481 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_cr_regnum
))
1482 regcache
->raw_collect (tdep
->ppc_cr_regnum
, cr
);
1483 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_lr_regnum
))
1484 regcache
->raw_collect (tdep
->ppc_lr_regnum
, lr
);
1485 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_ctr_regnum
))
1486 regcache
->raw_collect (tdep
->ppc_ctr_regnum
, ctr
);
1487 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_xer_regnum
))
1488 regcache
->raw_collect (tdep
->ppc_xer_regnum
, xer
);
1489 if (tdep
->ppc_fpscr_regnum
>= 0
1490 && REG_VALID
== regcache
->get_register_status (tdep
->ppc_fpscr_regnum
))
1491 regcache
->raw_collect (tdep
->ppc_fpscr_regnum
, fpscr
);
1494 /* Store all registers into pthread PDTID, which doesn't have a kernel
1497 It's possible to store a single register into a non-kernel pthread,
1498 but I doubt it's worth the effort. */
1501 store_regs_user_thread (const struct regcache
*regcache
, pthdb_pthread_t pdtid
)
1503 struct gdbarch
*gdbarch
= regcache
->arch ();
1504 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1506 pthdb_context_t ctx
;
1511 if (debug_aix_thread
)
1512 fprintf_unfiltered (gdb_stdlog
,
1513 "store_regs_user_thread %lx\n", (long) pdtid
);
1515 /* Retrieve the thread's current context for its non-register
1517 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1518 if (status
!= PTHDB_SUCCESS
)
1519 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1520 pd_status2str (status
));
1522 /* Collect general-purpose register values from the regcache. */
1524 for (i
= 0; i
< ppc_num_gprs
; i
++)
1525 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_gp0_regnum
+ i
))
1529 regcache
->raw_collect (tdep
->ppc_gp0_regnum
+ i
, (void *) &int64
);
1534 regcache
->raw_collect (tdep
->ppc_gp0_regnum
+ i
, (void *) &int32
);
1539 /* Collect floating-point register values from the regcache. */
1540 if (ppc_floating_point_unit_p (gdbarch
))
1541 fill_fprs (regcache
, ctx
.fpr
);
1543 /* Special registers (always kept in ctx as 64 bits). */
1546 fill_sprs64 (regcache
, &ctx
.iar
, &ctx
.msr
, &ctx
.cr
, &ctx
.lr
, &ctx
.ctr
,
1547 &ctx
.xer
, &ctx
.fpscr
);
1551 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1552 Solution: use 32-bit temp variables. */
1553 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1556 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
, &tmp_ctr
,
1557 &tmp_xer
, &tmp_fpscr
);
1558 if (REG_VALID
== regcache
->get_register_status
1559 (gdbarch_pc_regnum (gdbarch
)))
1561 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_ps_regnum
))
1563 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_cr_regnum
))
1565 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_lr_regnum
))
1567 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_ctr_regnum
))
1569 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_xer_regnum
))
1571 if (REG_VALID
== regcache
->get_register_status (tdep
->ppc_xer_regnum
))
1572 ctx
.fpscr
= tmp_fpscr
;
1575 status
= pthdb_pthread_setcontext (pd_session
, pdtid
, &ctx
);
1576 if (status
!= PTHDB_SUCCESS
)
1577 error (_("aix-thread: store_registers: "
1578 "pthdb_pthread_setcontext returned %s"),
1579 pd_status2str (status
));
1582 /* Store register REGNO if != -1 or all registers otherwise into
1585 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1586 SPRs, but there's no way to set individual registers within those
1587 groups. Therefore, if REGNO != -1, this function stores an entire
1591 store_regs_kernel_thread (const struct regcache
*regcache
, int regno
,
1594 struct gdbarch
*gdbarch
= regcache
->arch ();
1595 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1596 uint64_t gprs64
[ppc_num_gprs
];
1597 uint32_t gprs32
[ppc_num_gprs
];
1598 double fprs
[ppc_num_fprs
];
1599 struct ptxsprs sprs64
;
1600 struct ptsprs sprs32
;
1603 if (debug_aix_thread
)
1604 fprintf_unfiltered (gdb_stdlog
,
1605 "store_regs_kernel_thread tid=%lx regno=%d\n",
1608 /* General-purpose registers. */
1610 || (tdep
->ppc_gp0_regnum
<= regno
1611 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_fprs
))
1615 /* Pre-fetch: some regs may not be in the cache. */
1616 ptrace64aix (PTT_READ_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1617 fill_gprs64 (regcache
, gprs64
);
1618 ptrace64aix (PTT_WRITE_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1622 /* Pre-fetch: some regs may not be in the cache. */
1623 ptrace32 (PTT_READ_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
);
1624 fill_gprs32 (regcache
, gprs32
);
1625 ptrace32 (PTT_WRITE_GPRS
, tid
, (uintptr_t) gprs32
, 0, NULL
);
1629 /* Floating-point registers. */
1631 if (ppc_floating_point_unit_p (gdbarch
)
1633 || (regno
>= tdep
->ppc_fp0_regnum
1634 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1636 /* Pre-fetch: some regs may not be in the cache. */
1637 ptrace32 (PTT_READ_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
);
1638 fill_fprs (regcache
, fprs
);
1639 ptrace32 (PTT_WRITE_FPRS
, tid
, (uintptr_t) fprs
, 0, NULL
);
1642 /* Special-purpose registers. */
1644 if (regno
== -1 || special_register_p (gdbarch
, regno
))
1648 /* Pre-fetch: some registers won't be in the cache. */
1649 ptrace64aix (PTT_READ_SPRS
, tid
,
1650 (unsigned long) &sprs64
, 0, NULL
);
1651 fill_sprs64 (regcache
, &sprs64
.pt_iar
, &sprs64
.pt_msr
,
1652 &sprs64
.pt_cr
, &sprs64
.pt_lr
, &sprs64
.pt_ctr
,
1653 &sprs64
.pt_xer
, &sprs64
.pt_fpscr
);
1654 ptrace64aix (PTT_WRITE_SPRS
, tid
,
1655 (unsigned long) &sprs64
, 0, NULL
);
1659 /* The contents of "struct ptspr" were declared as "unsigned
1660 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1661 Use temporaries to work around this problem. Also, add an
1662 assert here to make sure we fail if the system header files
1663 use "unsigned long", and the size of that type is not what
1664 the headers expect. */
1665 uint32_t tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1668 gdb_assert (sizeof (sprs32
.pt_iar
) == 4);
1670 /* Pre-fetch: some registers won't be in the cache. */
1671 ptrace32 (PTT_READ_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
);
1673 fill_sprs32 (regcache
, &tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
,
1674 &tmp_ctr
, &tmp_xer
, &tmp_fpscr
);
1676 sprs32
.pt_iar
= tmp_iar
;
1677 sprs32
.pt_msr
= tmp_msr
;
1678 sprs32
.pt_cr
= tmp_cr
;
1679 sprs32
.pt_lr
= tmp_lr
;
1680 sprs32
.pt_ctr
= tmp_ctr
;
1681 sprs32
.pt_xer
= tmp_xer
;
1682 sprs32
.pt_fpscr
= tmp_fpscr
;
1684 if (tdep
->ppc_mq_regnum
>= 0)
1685 if (REG_VALID
== regcache
->get_register_status
1686 (tdep
->ppc_mq_regnum
))
1687 regcache
->raw_collect (tdep
->ppc_mq_regnum
, &sprs32
.pt_mq
);
1689 ptrace32 (PTT_WRITE_SPRS
, tid
, (uintptr_t) &sprs32
, 0, NULL
);
1694 /* Store gdb's current view of the register set into the
1695 thread/process connected to REGCACHE. */
1698 aix_thread_target::store_registers (struct regcache
*regcache
, int regno
)
1700 struct thread_info
*thread
;
1702 struct target_ops
*beneath
= find_target_beneath (this);
1704 if (!PD_TID (regcache
->ptid ()))
1705 beneath
->store_registers (regcache
, regno
);
1708 thread
= find_thread_ptid (regcache
->ptid ());
1709 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1712 if (tid
== PTHDB_INVALID_TID
)
1713 store_regs_user_thread (regcache
, priv
->pdtid
);
1715 store_regs_kernel_thread (regcache
, regno
, tid
);
1719 /* Implement the to_xfer_partial target_ops method. */
1721 enum target_xfer_status
1722 aix_thread_target::xfer_partial (enum target_object object
,
1723 const char *annex
, gdb_byte
*readbuf
,
1724 const gdb_byte
*writebuf
,
1725 ULONGEST offset
, ULONGEST len
,
1726 ULONGEST
*xfered_len
)
1728 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
1729 struct target_ops
*beneath
= find_target_beneath (this);
1731 inferior_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
1732 return beneath
->xfer_partial (object
, annex
, readbuf
,
1733 writebuf
, offset
, len
, xfered_len
);
1736 /* Clean up after the inferior exits. */
1739 aix_thread_target::mourn_inferior ()
1741 struct target_ops
*beneath
= find_target_beneath (this);
1744 beneath
->mourn_inferior ();
1747 /* Return whether thread PID is still valid. */
1750 aix_thread_target::thread_alive (ptid_t ptid
)
1752 struct target_ops
*beneath
= find_target_beneath (this);
1755 return beneath
->thread_alive (ptid
);
1757 /* We update the thread list every time the child stops, so all
1758 valid threads should be in the thread list. */
1759 return in_thread_list (ptid
);
1762 /* Return a printable representation of composite PID for use in
1763 "info threads" output. */
1766 aix_thread_target::pid_to_str (ptid_t ptid
)
1768 static char *ret
= NULL
;
1769 struct target_ops
*beneath
= find_target_beneath (this);
1772 return beneath
->pid_to_str (ptid
);
1774 /* Free previous return value; a new one will be allocated by
1778 ret
= xstrprintf (_("Thread %ld"), ptid_get_tid (ptid
));
1782 /* Return a printable representation of extra information about
1783 THREAD, for use in "info threads" output. */
1786 aix_thread_target::extra_thread_info (struct thread_info
*thread
)
1789 pthdb_pthread_t pdtid
;
1791 pthdb_state_t state
;
1792 pthdb_suspendstate_t suspendstate
;
1793 pthdb_detachstate_t detachstate
;
1795 static char *ret
= NULL
;
1797 if (!PD_TID (thread
->ptid
))
1801 aix_thread_info
*priv
= get_aix_thread_info (thread
);
1803 pdtid
= priv
->pdtid
;
1806 if (tid
!= PTHDB_INVALID_TID
)
1807 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1808 buf
.printf (_("tid %d"), (int)tid
);
1810 status
= pthdb_pthread_state (pd_session
, pdtid
, &state
);
1811 if (status
!= PTHDB_SUCCESS
)
1813 buf
.printf (", %s", state2str (state
));
1815 status
= pthdb_pthread_suspendstate (pd_session
, pdtid
,
1817 if (status
== PTHDB_SUCCESS
&& suspendstate
== PSS_SUSPENDED
)
1818 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1819 buf
.printf (_(", suspended"));
1821 status
= pthdb_pthread_detachstate (pd_session
, pdtid
,
1823 if (status
== PTHDB_SUCCESS
&& detachstate
== PDS_DETACHED
)
1824 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1825 buf
.printf (_(", detached"));
1827 pthdb_pthread_cancelpend (pd_session
, pdtid
, &cancelpend
);
1828 if (status
== PTHDB_SUCCESS
&& cancelpend
)
1829 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1830 buf
.printf (_(", cancel pending"));
1834 xfree (ret
); /* Free old buffer. */
1836 ret
= xstrdup (buf
.c_str ());
1842 aix_thread_target::get_ada_task_ptid (long lwp
, long thread
)
1844 return ptid_build (ptid_get_pid (inferior_ptid
), 0, thread
);
1848 /* Module startup initialization function, automagically called by
1852 _initialize_aix_thread (void)
1854 /* Notice when object files get loaded and unloaded. */
1855 gdb::observers::new_objfile
.attach (new_objfile
);
1857 /* Add ourselves to inferior_created event chain.
1858 This is needed to enable the thread target on "attach". */
1859 gdb::observers::inferior_created
.attach (aix_thread_inferior_created
);
1861 add_setshow_boolean_cmd ("aix-thread", class_maintenance
, &debug_aix_thread
,
1862 _("Set debugging of AIX thread module."),
1863 _("Show debugging of AIX thread module."),
1864 _("Enables debugging output (used to debug GDB)."),
1866 /* FIXME: i18n: Debugging of AIX thread
1867 module is \"%d\". */
1868 &setdebuglist
, &showdebuglist
);