1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright 1999, 2000, 2002 Free Software Foundation, Inc.
4 Written by Nick Duffek <nsd@redhat.com>.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
25 debugging pthread applications.
27 Some name prefix conventions:
28 pthdb_ provided by libpthdebug.a
29 pdc_ callbacks that this module provides to libpthdebug.a
30 pd_ variables or functions interfacing with libpthdebug.a
32 libpthdebug peculiarities:
34 - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but
35 it's not documented, and after several calls it stops working
36 and causes other libpthdebug functions to fail.
38 - pthdb_tid_pthread() doesn't always work after
39 pthdb_session_update(), but it does work after cycling through
40 all threads using pthdb_pthread().
45 #include "gdb_assert.h"
46 #include "gdbthread.h"
51 #include "language.h" /* for local_hex_string() */
53 #include "gdb_string.h"
56 #include <sys/types.h>
57 #include <sys/ptrace.h>
60 #include <sys/pthdebug.h>
62 /* Whether to emit debugging output. */
63 static int debug_aix_thread
;
65 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
66 #ifndef PTHDB_VERSION_3
67 #define pthdb_tid_t tid_t
70 /* Return whether to treat PID as a debuggable thread id. */
72 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
74 /* Build a thread ptid. */
75 #define BUILD_THREAD(TID, PID) ptid_build (PID, 0, TID)
77 /* Build and lwp ptid. */
78 #define BUILD_LWP(LWP, PID) MERGEPID (PID, LWP)
80 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
81 PTHDB_BAD_USER errors, so use 1. */
85 /* Success and failure values returned by pthdb callbacks. */
87 #define PDC_SUCCESS PTHDB_SUCCESS
88 #define PDC_FAILURE PTHDB_CALLBACK
90 /* Private data attached to each element in GDB's thread list. */
92 struct private_thread_info
{
93 pthdb_pthread_t pdtid
; /* thread's libpthdebug id */
94 pthdb_tid_t tid
; /* kernel thread id */
97 /* Information about a thread of which libpthdebug is aware. */
100 pthdb_pthread_t pdtid
;
105 /* This module's target-specific operations, active while pd_able is true. */
107 static struct target_ops aix_thread_ops
;
109 /* Copy of the target over which ops is pushed.
110 This is more convenient than a pointer to child_ops or core_ops,
111 because they lack current_target's default callbacks. */
113 static struct target_ops base_target
;
115 /* Address of the function that libpthread will call when libpthdebug
116 is ready to be initialized. */
118 static CORE_ADDR pd_brk_addr
;
120 /* Whether the current application is debuggable by pthdb. */
122 static int pd_able
= 0;
124 /* Whether a threaded application is being debugged. */
126 static int pd_active
= 0;
128 /* Whether the current architecture is 64-bit.
129 Only valid when pd_able is true. */
133 /* Saved pointer to previous owner of
134 deprecated_target_new_objfile_hook. */
136 static void (*target_new_objfile_chain
)(struct objfile
*);
138 /* Forward declarations for pthdb callbacks. */
140 static int pdc_symbol_addrs (pthdb_user_t
, pthdb_symbol_t
*, int);
141 static int pdc_read_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
142 static int pdc_write_data (pthdb_user_t
, void *, pthdb_addr_t
, size_t);
143 static int pdc_read_regs (pthdb_user_t user
, pthdb_tid_t tid
,
144 unsigned long long flags
,
145 pthdb_context_t
*context
);
146 static int pdc_write_regs (pthdb_user_t user
, pthdb_tid_t tid
,
147 unsigned long long flags
,
148 pthdb_context_t
*context
);
149 static int pdc_alloc (pthdb_user_t
, size_t, void **);
150 static int pdc_realloc (pthdb_user_t
, void *, size_t, void **);
151 static int pdc_dealloc (pthdb_user_t
, void *);
153 /* pthdb callbacks. */
155 static pthdb_callbacks_t pd_callbacks
= {
167 /* Current pthdb session. */
169 static pthdb_session_t pd_session
;
171 /* Return a printable representation of pthdebug function return
175 pd_status2str (int status
)
179 case PTHDB_SUCCESS
: return "SUCCESS";
180 case PTHDB_NOSYS
: return "NOSYS";
181 case PTHDB_NOTSUP
: return "NOTSUP";
182 case PTHDB_BAD_VERSION
: return "BAD_VERSION";
183 case PTHDB_BAD_USER
: return "BAD_USER";
184 case PTHDB_BAD_SESSION
: return "BAD_SESSION";
185 case PTHDB_BAD_MODE
: return "BAD_MODE";
186 case PTHDB_BAD_FLAGS
: return "BAD_FLAGS";
187 case PTHDB_BAD_CALLBACK
: return "BAD_CALLBACK";
188 case PTHDB_BAD_POINTER
: return "BAD_POINTER";
189 case PTHDB_BAD_CMD
: return "BAD_CMD";
190 case PTHDB_BAD_PTHREAD
: return "BAD_PTHREAD";
191 case PTHDB_BAD_ATTR
: return "BAD_ATTR";
192 case PTHDB_BAD_MUTEX
: return "BAD_MUTEX";
193 case PTHDB_BAD_MUTEXATTR
: return "BAD_MUTEXATTR";
194 case PTHDB_BAD_COND
: return "BAD_COND";
195 case PTHDB_BAD_CONDATTR
: return "BAD_CONDATTR";
196 case PTHDB_BAD_RWLOCK
: return "BAD_RWLOCK";
197 case PTHDB_BAD_RWLOCKATTR
: return "BAD_RWLOCKATTR";
198 case PTHDB_BAD_KEY
: return "BAD_KEY";
199 case PTHDB_BAD_PTID
: return "BAD_PTID";
200 case PTHDB_BAD_TID
: return "BAD_TID";
201 case PTHDB_CALLBACK
: return "CALLBACK";
202 case PTHDB_CONTEXT
: return "CONTEXT";
203 case PTHDB_HELD
: return "HELD";
204 case PTHDB_NOT_HELD
: return "NOT_HELD";
205 case PTHDB_MEMORY
: return "MEMORY";
206 case PTHDB_NOT_PTHREADED
: return "NOT_PTHREADED";
207 case PTHDB_SYMBOL
: return "SYMBOL";
208 case PTHDB_NOT_AVAIL
: return "NOT_AVAIL";
209 case PTHDB_INTERNAL
: return "INTERNAL";
210 default: return "UNKNOWN";
214 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
215 exceptional conditions and either return nonlocally or else return
216 1 for success and 0 for failure. */
219 ptrace_check (int req
, int id
, int ret
)
221 if (ret
== 0 && !errno
)
224 /* According to ptrace(2), ptrace may fail with EPERM if "the
225 Identifier parameter corresponds to a kernel thread which is
226 stopped in kernel mode and whose computational state cannot be
227 read or written." This happens quite often with register reads. */
234 if (ret
== -1 && errno
== EPERM
)
236 if (debug_aix_thread
)
237 fprintf_unfiltered (gdb_stdlog
,
238 "ptrace (%d, %d) = %d (errno = %d)\n",
239 req
, id
, ret
, errno
);
240 return ret
== -1 ? 0 : 1;
244 error ("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)",
245 req
, id
, ret
, errno
, safe_strerror (errno
));
246 return 0; /* Not reached. */
249 /* Call ptracex (REQ, ID, ADDR, DATA, BUF). Return success. */
252 ptrace64aix (int req
, int id
, long long addr
, int data
, int *buf
)
255 return ptrace_check (req
, id
, ptracex (req
, id
, addr
, data
, buf
));
258 /* Call ptrace (REQ, ID, ADDR, DATA, BUF). Return success. */
261 ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
264 return ptrace_check (req
, id
,
265 ptrace (req
, id
, (int *) addr
, data
, buf
));
268 /* If *PIDP is a composite process/thread id, convert it to a
272 pid_to_prc (ptid_t
*ptidp
)
278 *ptidp
= pid_to_ptid (PIDGET (ptid
));
281 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
282 the address of SYMBOLS[<i>].name. */
285 pdc_symbol_addrs (pthdb_user_t user
, pthdb_symbol_t
*symbols
, int count
)
287 struct minimal_symbol
*ms
;
291 if (debug_aix_thread
)
292 fprintf_unfiltered (gdb_stdlog
,
293 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
294 user
, (long) symbols
, count
);
296 for (i
= 0; i
< count
; i
++)
298 name
= symbols
[i
].name
;
299 if (debug_aix_thread
)
300 fprintf_unfiltered (gdb_stdlog
,
301 " symbols[%d].name = \"%s\"\n", i
, name
);
307 if (!(ms
= lookup_minimal_symbol (name
, NULL
, NULL
)))
309 if (debug_aix_thread
)
310 fprintf_unfiltered (gdb_stdlog
, " returning PDC_FAILURE\n");
313 symbols
[i
].addr
= SYMBOL_VALUE_ADDRESS (ms
);
315 if (debug_aix_thread
)
316 fprintf_unfiltered (gdb_stdlog
, " symbols[%d].addr = %s\n",
317 i
, local_hex_string (symbols
[i
].addr
));
319 if (debug_aix_thread
)
320 fprintf_unfiltered (gdb_stdlog
, " returning PDC_SUCCESS\n");
324 /* Read registers call back function should be able to read the
325 context information of a debuggee kernel thread from an active
326 process or from a core file. The information should be formatted
327 in context64 form for both 32-bit and 64-bit process.
328 If successful return 0, else non-zero is returned. */
331 pdc_read_regs (pthdb_user_t user
,
333 unsigned long long flags
,
334 pthdb_context_t
*context
)
336 /* This function doesn't appear to be used, so we could probably
337 just return 0 here. HOWEVER, if it is not defined, the OS will
338 complain and several thread debug functions will fail. In case
339 this is needed, I have implemented what I think it should do,
340 however this code is untested. */
342 uint64_t gprs64
[ppc_num_gprs
];
343 uint32_t gprs32
[ppc_num_gprs
];
344 double fprs
[ppc_num_fprs
];
345 struct ptxsprs sprs64
;
346 struct ptsprs sprs32
;
348 if (debug_aix_thread
)
349 fprintf_unfiltered (gdb_stdlog
, "pdc_read_regs tid=%d flags=%s\n",
350 (int) tid
, local_hex_string (flags
));
352 /* General-purpose registers. */
353 if (flags
& PTHDB_FLAG_GPRS
)
357 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
358 (unsigned long) gprs64
, 0, NULL
))
359 memset (gprs64
, 0, sizeof (gprs64
));
360 memcpy (context
->gpr
, gprs64
, sizeof(gprs64
));
364 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
365 memset (gprs32
, 0, sizeof (gprs32
));
366 memcpy (context
->gpr
, gprs32
, sizeof(gprs32
));
370 /* Floating-point registers. */
371 if (flags
& PTHDB_FLAG_FPRS
)
373 if (!ptrace32 (PTT_READ_FPRS
, tid
, (int *) fprs
, 0, NULL
))
374 memset (fprs
, 0, sizeof (fprs
));
375 memcpy (context
->fpr
, fprs
, sizeof(fprs
));
378 /* Special-purpose registers. */
379 if (flags
& PTHDB_FLAG_SPRS
)
383 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
384 (unsigned long) &sprs64
, 0, NULL
))
385 memset (&sprs64
, 0, sizeof (sprs64
));
386 memcpy (&context
->msr
, &sprs64
, sizeof(sprs64
));
390 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
391 memset (&sprs32
, 0, sizeof (sprs32
));
392 memcpy (&context
->msr
, &sprs32
, sizeof(sprs32
));
398 /* Write register function should be able to write requested context
399 information to specified debuggee's kernel thread id.
400 If successful return 0, else non-zero is returned. */
403 pdc_write_regs (pthdb_user_t user
,
405 unsigned long long flags
,
406 pthdb_context_t
*context
)
408 /* This function doesn't appear to be used, so we could probably
409 just return 0 here. HOWEVER, if it is not defined, the OS will
410 complain and several thread debug functions will fail. In case
411 this is needed, I have implemented what I think it should do,
412 however this code is untested. */
414 if (debug_aix_thread
)
415 fprintf_unfiltered (gdb_stdlog
, "pdc_write_regs tid=%d flags=%s\n",
416 (int) tid
, local_hex_string (flags
));
418 /* General-purpose registers. */
419 if (flags
& PTHDB_FLAG_GPRS
)
422 ptrace64aix (PTT_WRITE_GPRS
, tid
,
423 (unsigned long) context
->gpr
, 0, NULL
);
425 ptrace32 (PTT_WRITE_GPRS
, tid
, (int *) context
->gpr
, 0, NULL
);
428 /* Floating-point registers. */
429 if (flags
& PTHDB_FLAG_FPRS
)
431 ptrace32 (PTT_WRITE_FPRS
, tid
, (int *) context
->fpr
, 0, NULL
);
434 /* Special-purpose registers. */
435 if (flags
& PTHDB_FLAG_SPRS
)
439 ptrace64aix (PTT_WRITE_SPRS
, tid
,
440 (unsigned long) &context
->msr
, 0, NULL
);
444 ptrace32 (PTT_WRITE_SPRS
, tid
, (int *) &context
->msr
, 0, NULL
);
450 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
453 pdc_read_data (pthdb_user_t user
, void *buf
,
454 pthdb_addr_t addr
, size_t len
)
458 if (debug_aix_thread
)
459 fprintf_unfiltered (gdb_stdlog
,
460 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
461 user
, (long) buf
, local_hex_string (addr
), len
);
463 status
= target_read_memory (addr
, buf
, len
);
464 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
466 if (debug_aix_thread
)
467 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n",
468 status
, pd_status2str (ret
));
472 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
475 pdc_write_data (pthdb_user_t user
, void *buf
,
476 pthdb_addr_t addr
, size_t len
)
480 if (debug_aix_thread
)
481 fprintf_unfiltered (gdb_stdlog
,
482 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
483 user
, (long) buf
, local_hex_string (addr
), len
);
485 status
= target_write_memory (addr
, buf
, len
);
486 ret
= status
== 0 ? PDC_SUCCESS
: PDC_FAILURE
;
488 if (debug_aix_thread
)
489 fprintf_unfiltered (gdb_stdlog
, " status=%d, returning %s\n", status
,
490 pd_status2str (ret
));
494 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
498 pdc_alloc (pthdb_user_t user
, size_t len
, void **bufp
)
500 if (debug_aix_thread
)
501 fprintf_unfiltered (gdb_stdlog
,
502 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
503 user
, len
, (long) bufp
);
504 *bufp
= xmalloc (len
);
505 if (debug_aix_thread
)
506 fprintf_unfiltered (gdb_stdlog
,
507 " malloc returned 0x%lx\n", (long) *bufp
);
509 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
512 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
515 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
516 realloc callback, so that it contains LEN bytes, and store a
517 pointer to the result in BUFP. */
520 pdc_realloc (pthdb_user_t user
, void *buf
, size_t len
, void **bufp
)
522 if (debug_aix_thread
)
523 fprintf_unfiltered (gdb_stdlog
,
524 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
525 user
, (long) buf
, len
, (long) bufp
);
526 *bufp
= xrealloc (buf
, len
);
527 if (debug_aix_thread
)
528 fprintf_unfiltered (gdb_stdlog
,
529 " realloc returned 0x%lx\n", (long) *bufp
);
530 return *bufp
? PDC_SUCCESS
: PDC_FAILURE
;
533 /* pthdb callback: free BUF, which was allocated by the alloc or
537 pdc_dealloc (pthdb_user_t user
, void *buf
)
539 if (debug_aix_thread
)
540 fprintf_unfiltered (gdb_stdlog
,
541 "pdc_free (user = %ld, buf = 0x%lx)\n", user
,
547 /* Return a printable representation of pthread STATE. */
550 state2str (pthdb_state_t state
)
554 case PST_IDLE
: return "idle"; /* being created */
555 case PST_RUN
: return "running"; /* running */
556 case PST_SLEEP
: return "sleeping"; /* awaiting an event */
557 case PST_READY
: return "ready"; /* runnable */
558 case PST_TERM
: return "finished"; /* awaiting a join/detach */
559 default: return "unknown";
563 /* qsort() comparison function for sorting pd_thread structs by pthid. */
566 pcmp (const void *p1v
, const void *p2v
)
568 struct pd_thread
*p1
= (struct pd_thread
*) p1v
;
569 struct pd_thread
*p2
= (struct pd_thread
*) p2v
;
570 return p1
->pthid
< p2
->pthid
? -1 : p1
->pthid
> p2
->pthid
;
573 /* iterate_over_threads() callback for counting GDB threads. */
576 giter_count (struct thread_info
*thread
, void *countp
)
582 /* iterate_over_threads() callback for accumulating GDB thread pids. */
585 giter_accum (struct thread_info
*thread
, void *bufp
)
587 **(struct thread_info
***) bufp
= thread
;
588 (*(struct thread_info
***) bufp
)++;
592 /* ptid comparison function */
595 ptid_cmp (ptid_t ptid1
, ptid_t ptid2
)
599 if (ptid_get_pid (ptid1
) < ptid_get_pid (ptid2
))
601 else if (ptid_get_pid (ptid1
) > ptid_get_pid (ptid2
))
603 else if (ptid_get_tid (ptid1
) < ptid_get_tid (ptid2
))
605 else if (ptid_get_tid (ptid1
) > ptid_get_tid (ptid2
))
607 else if (ptid_get_lwp (ptid1
) < ptid_get_lwp (ptid2
))
609 else if (ptid_get_lwp (ptid1
) > ptid_get_lwp (ptid2
))
615 /* qsort() comparison function for sorting thread_info structs by pid. */
618 gcmp (const void *t1v
, const void *t2v
)
620 struct thread_info
*t1
= *(struct thread_info
**) t1v
;
621 struct thread_info
*t2
= *(struct thread_info
**) t2v
;
622 return ptid_cmp (t1
->ptid
, t2
->ptid
);
625 /* Search through the list of all kernel threads for the thread
626 that has stopped on a SIGTRAP signal, and return its TID.
627 Return 0 if none found. */
630 get_signaled_thread (void)
632 struct thrdsinfo64 thrinf
;
633 pthdb_tid_t ktid
= 0;
636 /* getthrds(3) isn't prototyped in any AIX 4.3.3 #include file. */
637 extern int getthrds (pid_t
, struct thrdsinfo64
*,
638 int, pthdb_tid_t
*, int);
642 if (getthrds (PIDGET (inferior_ptid
), &thrinf
,
643 sizeof (thrinf
), &ktid
, 1) != 1)
646 if (thrinf
.ti_cursig
== SIGTRAP
)
647 return thrinf
.ti_tid
;
650 /* Didn't find any thread stopped on a SIGTRAP signal. */
654 /* Synchronize GDB's thread list with libpthdebug's.
656 There are some benefits of doing this every time the inferior stops:
658 - allows users to run thread-specific commands without needing to
659 run "info threads" first
661 - helps pthdb_tid_pthread() work properly (see "libpthdebug
662 peculiarities" at the top of this module)
664 - simplifies the demands placed on libpthdebug, which seems to
665 have difficulty with certain call patterns */
668 sync_threadlists (void)
670 int cmd
, status
, infpid
;
671 int pcount
, psize
, pi
, gcount
, gi
;
672 struct pd_thread
*pbuf
;
673 struct thread_info
**gbuf
, **g
, *thread
;
674 pthdb_pthread_t pdtid
;
678 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
682 pbuf
= (struct pd_thread
*) xmalloc (psize
* sizeof *pbuf
);
684 for (cmd
= PTHDB_LIST_FIRST
;; cmd
= PTHDB_LIST_NEXT
)
686 status
= pthdb_pthread (pd_session
, &pdtid
, cmd
);
687 if (status
!= PTHDB_SUCCESS
|| pdtid
== PTHDB_INVALID_PTHREAD
)
690 status
= pthdb_pthread_ptid (pd_session
, pdtid
, &pthid
);
691 if (status
!= PTHDB_SUCCESS
|| pthid
== PTHDB_INVALID_PTID
)
697 pbuf
= (struct pd_thread
*) xrealloc (pbuf
,
698 psize
* sizeof *pbuf
);
700 pbuf
[pcount
].pdtid
= pdtid
;
701 pbuf
[pcount
].pthid
= pthid
;
705 for (pi
= 0; pi
< pcount
; pi
++)
707 status
= pthdb_pthread_tid (pd_session
, pbuf
[pi
].pdtid
, &tid
);
708 if (status
!= PTHDB_SUCCESS
)
709 tid
= PTHDB_INVALID_TID
;
713 qsort (pbuf
, pcount
, sizeof *pbuf
, pcmp
);
715 /* Accumulate an array of GDB threads sorted by pid. */
718 iterate_over_threads (giter_count
, &gcount
);
719 g
= gbuf
= (struct thread_info
**) xmalloc (gcount
* sizeof *gbuf
);
720 iterate_over_threads (giter_accum
, &g
);
721 qsort (gbuf
, gcount
, sizeof *gbuf
, gcmp
);
723 /* Apply differences between the two arrays to GDB's thread list. */
725 infpid
= PIDGET (inferior_ptid
);
726 for (pi
= gi
= 0; pi
< pcount
|| gi
< gcount
;)
730 delete_thread (gbuf
[gi
]->ptid
);
733 else if (gi
== gcount
)
735 thread
= add_thread (BUILD_THREAD (pbuf
[pi
].pthid
, infpid
));
736 thread
->private = xmalloc (sizeof (struct private_thread_info
));
737 thread
->private->pdtid
= pbuf
[pi
].pdtid
;
738 thread
->private->tid
= pbuf
[pi
].tid
;
746 pptid
= BUILD_THREAD (pbuf
[pi
].pthid
, infpid
);
747 gptid
= gbuf
[gi
]->ptid
;
748 pdtid
= pbuf
[pi
].pdtid
;
751 cmp_result
= ptid_cmp (pptid
, gptid
);
755 gbuf
[gi
]->private->pdtid
= pdtid
;
756 gbuf
[gi
]->private->tid
= tid
;
760 else if (cmp_result
> 0)
762 delete_thread (gptid
);
767 thread
= add_thread (pptid
);
768 thread
->private = xmalloc (sizeof (struct private_thread_info
));
769 thread
->private->pdtid
= pdtid
;
770 thread
->private->tid
= tid
;
780 /* Iterate_over_threads() callback for locating a thread, using
781 the TID of its associated kernel thread. */
784 iter_tid (struct thread_info
*thread
, void *tidp
)
786 const pthdb_tid_t tid
= *(pthdb_tid_t
*)tidp
;
788 return (thread
->private->tid
== tid
);
791 /* Synchronize libpthdebug's state with the inferior and with GDB,
792 generate a composite process/thread <pid> for the current thread,
793 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
796 pd_update (int set_infpid
)
801 struct thread_info
*thread
= NULL
;
804 return inferior_ptid
;
806 status
= pthdb_session_update (pd_session
);
807 if (status
!= PTHDB_SUCCESS
)
808 return inferior_ptid
;
812 /* Define "current thread" as one that just received a trap signal. */
814 tid
= get_signaled_thread ();
816 thread
= iterate_over_threads (iter_tid
, &tid
);
818 ptid
= inferior_ptid
;
823 inferior_ptid
= ptid
;
828 /* Try to start debugging threads in the current process.
829 If successful and SET_INFPID, set inferior_ptid to reflect the
833 pd_activate (int set_infpid
)
837 status
= pthdb_session_init (PD_USER
, arch64
? PEM_64BIT
: PEM_32BIT
,
838 PTHDB_FLAG_REGS
, &pd_callbacks
,
840 if (status
!= PTHDB_SUCCESS
)
842 return inferior_ptid
;
845 return pd_update (set_infpid
);
848 /* Undo the effects of pd_activate(). */
855 pthdb_session_destroy (pd_session
);
857 pid_to_prc (&inferior_ptid
);
861 /* An object file has just been loaded. Check whether the current
862 application is pthreaded, and if so, prepare for thread debugging. */
869 struct minimal_symbol
*ms
;
871 /* Don't initialize twice. */
875 /* Check application word size. */
876 arch64
= register_size (current_gdbarch
, 0) == 8;
878 /* Check whether the application is pthreaded. */
880 status
= pthdb_session_pthreaded (PD_USER
, PTHDB_FLAG_REGS
,
881 &pd_callbacks
, &stub_name
);
882 if ((status
!= PTHDB_SUCCESS
&&
883 status
!= PTHDB_NOT_PTHREADED
) || !stub_name
)
886 /* Set a breakpoint on the returned stub function. */
887 if (!(ms
= lookup_minimal_symbol (stub_name
, NULL
, NULL
)))
889 pd_brk_addr
= SYMBOL_VALUE_ADDRESS (ms
);
890 if (!create_thread_event_breakpoint (pd_brk_addr
))
893 /* Prepare for thread debugging. */
894 base_target
= current_target
;
895 push_target (&aix_thread_ops
);
898 /* If we're debugging a core file or an attached inferior, the
899 pthread library may already have been initialized, so try to
900 activate thread debugging. */
904 /* Undo the effects of pd_enable(). */
914 unpush_target (&aix_thread_ops
);
917 /* deprecated_target_new_objfile_hook callback.
919 If OBJFILE is non-null, check whether a threaded application is
920 being debugged, and if so, prepare for thread debugging.
922 If OBJFILE is null, stop debugging threads. */
925 new_objfile (struct objfile
*objfile
)
932 if (target_new_objfile_chain
)
933 target_new_objfile_chain (objfile
);
936 /* Attach to process specified by ARGS. */
939 aix_thread_attach (char *args
, int from_tty
)
941 base_target
.to_attach (args
, from_tty
);
945 /* Detach from the process attached to by aix_thread_attach(). */
948 aix_thread_detach (char *args
, int from_tty
)
951 base_target
.to_detach (args
, from_tty
);
954 /* Tell the inferior process to continue running thread PID if != -1
955 and all threads otherwise. */
958 aix_thread_resume (ptid_t ptid
, int step
, enum target_signal sig
)
960 struct thread_info
*thread
;
965 struct cleanup
*cleanup
= save_inferior_ptid ();
966 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
967 base_target
.to_resume (ptid
, step
, sig
);
968 do_cleanups (cleanup
);
972 thread
= find_thread_pid (ptid
);
974 error ("aix-thread resume: unknown pthread %ld",
977 tid
[0] = thread
->private->tid
;
978 if (tid
[0] == PTHDB_INVALID_TID
)
979 error ("aix-thread resume: no tid for pthread %ld",
984 ptrace64aix (PTT_CONTINUE
, tid
[0], 1,
985 target_signal_to_host (sig
), (int *) tid
);
987 ptrace32 (PTT_CONTINUE
, tid
[0], (int *) 1,
988 target_signal_to_host (sig
), (int *) tid
);
992 /* Wait for thread/process ID if != -1 or for any thread otherwise.
993 If an error occurs, return -1, else return the pid of the stopped
997 aix_thread_wait (ptid_t ptid
, struct target_waitstatus
*status
)
999 struct cleanup
*cleanup
= save_inferior_ptid ();
1003 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1004 ptid
= base_target
.to_wait (ptid
, status
);
1005 do_cleanups (cleanup
);
1007 if (PIDGET (ptid
) == -1)
1008 return pid_to_ptid (-1);
1010 /* Check whether libpthdebug might be ready to be initialized. */
1011 if (!pd_active
&& status
->kind
== TARGET_WAITKIND_STOPPED
&&
1012 status
->value
.sig
== TARGET_SIGNAL_TRAP
&&
1013 read_pc_pid (ptid
) - DECR_PC_AFTER_BREAK
== pd_brk_addr
)
1014 return pd_activate (0);
1016 return pd_update (0);
1019 /* Record that the 64-bit general-purpose registers contain VALS. */
1022 supply_gprs64 (uint64_t *vals
)
1024 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1027 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1028 regcache_raw_supply (current_regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1029 (char *) (vals
+ regno
));
1032 /* Record that 32-bit register REGNO contains VAL. */
1035 supply_reg32 (int regno
, uint32_t val
)
1037 regcache_raw_supply (current_regcache
, regno
, (char *) &val
);
1040 /* Record that the floating-point registers contain VALS. */
1043 supply_fprs (double *vals
)
1045 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1048 /* This function should never be called on architectures without
1049 floating-point registers. */
1050 gdb_assert (ppc_floating_point_unit_p (current_gdbarch
));
1052 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
1053 regcache_raw_supply (current_regcache
, regno
+ tdep
->ppc_fp0_regnum
,
1054 (char *) (vals
+ regno
));
1057 /* Predicate to test whether given register number is a "special" register. */
1059 special_register_p (int regno
)
1061 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1063 return regno
== PC_REGNUM
1064 || regno
== tdep
->ppc_ps_regnum
1065 || regno
== tdep
->ppc_cr_regnum
1066 || regno
== tdep
->ppc_lr_regnum
1067 || regno
== tdep
->ppc_ctr_regnum
1068 || regno
== tdep
->ppc_xer_regnum
1069 || (tdep
->ppc_fpscr_regnum
>= 0 && regno
== tdep
->ppc_fpscr_regnum
)
1070 || (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
);
1074 /* Record that the special registers contain the specified 64-bit and
1078 supply_sprs64 (uint64_t iar
, uint64_t msr
, uint32_t cr
,
1079 uint64_t lr
, uint64_t ctr
, uint32_t xer
,
1082 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1084 regcache_raw_supply (current_regcache
, PC_REGNUM
, (char *) &iar
);
1085 regcache_raw_supply (current_regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1086 regcache_raw_supply (current_regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1087 regcache_raw_supply (current_regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1088 regcache_raw_supply (current_regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1089 regcache_raw_supply (current_regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1090 if (tdep
->ppc_fpscr_regnum
>= 0)
1091 regcache_raw_supply (current_regcache
, tdep
->ppc_fpscr_regnum
,
1095 /* Record that the special registers contain the specified 32-bit
1099 supply_sprs32 (uint32_t iar
, uint32_t msr
, uint32_t cr
,
1100 uint32_t lr
, uint32_t ctr
, uint32_t xer
,
1103 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1105 regcache_raw_supply (current_regcache
, PC_REGNUM
, (char *) &iar
);
1106 regcache_raw_supply (current_regcache
, tdep
->ppc_ps_regnum
, (char *) &msr
);
1107 regcache_raw_supply (current_regcache
, tdep
->ppc_cr_regnum
, (char *) &cr
);
1108 regcache_raw_supply (current_regcache
, tdep
->ppc_lr_regnum
, (char *) &lr
);
1109 regcache_raw_supply (current_regcache
, tdep
->ppc_ctr_regnum
, (char *) &ctr
);
1110 regcache_raw_supply (current_regcache
, tdep
->ppc_xer_regnum
, (char *) &xer
);
1111 if (tdep
->ppc_fpscr_regnum
>= 0)
1112 regcache_raw_supply (current_regcache
, tdep
->ppc_fpscr_regnum
,
1116 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1119 There's no way to query a single register from a non-kernel
1120 pthread, so there's no need for a single-register version of this
1124 fetch_regs_user_thread (pthdb_pthread_t pdtid
)
1126 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1128 pthdb_context_t ctx
;
1130 if (debug_aix_thread
)
1131 fprintf_unfiltered (gdb_stdlog
,
1132 "fetch_regs_user_thread %lx\n", (long) pdtid
);
1133 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1134 if (status
!= PTHDB_SUCCESS
)
1135 error ("aix-thread: fetch_registers: pthdb_pthread_context returned %s",
1136 pd_status2str (status
));
1138 /* General-purpose registers. */
1141 supply_gprs64 (ctx
.gpr
);
1143 for (i
= 0; i
< ppc_num_gprs
; i
++)
1144 supply_reg32 (tdep
->ppc_gp0_regnum
+ i
, ctx
.gpr
[i
]);
1146 /* Floating-point registers. */
1148 if (ppc_floating_point_unit_p (current_gdbarch
))
1149 supply_fprs (ctx
.fpr
);
1151 /* Special registers. */
1154 supply_sprs64 (ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
, ctx
.xer
,
1157 supply_sprs32 (ctx
.iar
, ctx
.msr
, ctx
.cr
, ctx
.lr
, ctx
.ctr
, ctx
.xer
,
1161 /* Fetch register REGNO if != -1 or all registers otherwise from
1164 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1165 SPRs, but there's no way to query individual registers within those
1166 groups. Therefore, if REGNO != -1, this function fetches an entire
1169 Unfortunately, kernel thread register queries often fail with
1170 EPERM, indicating that the thread is in kernel space. This breaks
1171 backtraces of threads other than the current one. To make that
1172 breakage obvious without throwing an error to top level (which is
1173 bad e.g. during "info threads" output), zero registers that can't
1177 fetch_regs_kernel_thread (int regno
, pthdb_tid_t tid
)
1179 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1180 uint64_t gprs64
[ppc_num_gprs
];
1181 uint32_t gprs32
[ppc_num_gprs
];
1182 double fprs
[ppc_num_fprs
];
1183 struct ptxsprs sprs64
;
1184 struct ptsprs sprs32
;
1187 if (debug_aix_thread
)
1188 fprintf_unfiltered (gdb_stdlog
,
1189 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1190 (long) tid
, regno
, arch64
);
1192 /* General-purpose registers. */
1194 || (tdep
->ppc_gp0_regnum
<= regno
1195 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
))
1199 if (!ptrace64aix (PTT_READ_GPRS
, tid
,
1200 (unsigned long) gprs64
, 0, NULL
))
1201 memset (gprs64
, 0, sizeof (gprs64
));
1202 supply_gprs64 (gprs64
);
1206 if (!ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
))
1207 memset (gprs32
, 0, sizeof (gprs32
));
1208 for (i
= 0; i
< ppc_num_gprs
; i
++)
1209 supply_reg32 (tdep
->ppc_gp0_regnum
+ i
, gprs32
[i
]);
1213 /* Floating-point registers. */
1215 if (ppc_floating_point_unit_p (current_gdbarch
)
1217 || (regno
>= tdep
->ppc_fp0_regnum
1218 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1220 if (!ptrace32 (PTT_READ_FPRS
, tid
, (int *) fprs
, 0, NULL
))
1221 memset (fprs
, 0, sizeof (fprs
));
1225 /* Special-purpose registers. */
1227 if (regno
== -1 || special_register_p (regno
))
1231 if (!ptrace64aix (PTT_READ_SPRS
, tid
,
1232 (unsigned long) &sprs64
, 0, NULL
))
1233 memset (&sprs64
, 0, sizeof (sprs64
));
1234 supply_sprs64 (sprs64
.pt_iar
, sprs64
.pt_msr
, sprs64
.pt_cr
,
1235 sprs64
.pt_lr
, sprs64
.pt_ctr
, sprs64
.pt_xer
,
1240 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1242 if (!ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
))
1243 memset (&sprs32
, 0, sizeof (sprs32
));
1244 supply_sprs32 (sprs32
.pt_iar
, sprs32
.pt_msr
, sprs32
.pt_cr
,
1245 sprs32
.pt_lr
, sprs32
.pt_ctr
, sprs32
.pt_xer
,
1248 if (tdep
->ppc_mq_regnum
>= 0)
1249 regcache_raw_supply (current_regcache
, tdep
->ppc_mq_regnum
,
1250 (char *) &sprs32
.pt_mq
);
1255 /* Fetch register REGNO if != -1 or all registers otherwise in the
1256 thread/process specified by inferior_ptid. */
1259 aix_thread_fetch_registers (int regno
)
1261 struct thread_info
*thread
;
1264 if (!PD_TID (inferior_ptid
))
1265 base_target
.to_fetch_registers (regno
);
1268 thread
= find_thread_pid (inferior_ptid
);
1269 tid
= thread
->private->tid
;
1271 if (tid
== PTHDB_INVALID_TID
)
1272 fetch_regs_user_thread (thread
->private->pdtid
);
1274 fetch_regs_kernel_thread (regno
, tid
);
1278 /* Store the gp registers into an array of uint32_t or uint64_t. */
1281 fill_gprs64 (uint64_t *vals
)
1283 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1286 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1287 if (register_cached (tdep
->ppc_gp0_regnum
+ regno
))
1288 regcache_raw_collect (current_regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1293 fill_gprs32 (uint32_t *vals
)
1295 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1298 for (regno
= 0; regno
< ppc_num_gprs
; regno
++)
1299 if (register_cached (tdep
->ppc_gp0_regnum
+ regno
))
1300 regcache_raw_collect (current_regcache
, tdep
->ppc_gp0_regnum
+ regno
,
1304 /* Store the floating point registers into a double array. */
1306 fill_fprs (double *vals
)
1308 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1311 /* This function should never be called on architectures without
1312 floating-point registers. */
1313 gdb_assert (ppc_floating_point_unit_p (current_gdbarch
));
1315 for (regno
= tdep
->ppc_fp0_regnum
;
1316 regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
;
1318 if (register_cached (regno
))
1319 regcache_raw_collect (current_regcache
, regno
, vals
+ regno
);
1322 /* Store the special registers into the specified 64-bit and 32-bit
1326 fill_sprs64 (uint64_t *iar
, uint64_t *msr
, uint32_t *cr
,
1327 uint64_t *lr
, uint64_t *ctr
, uint32_t *xer
,
1330 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1332 /* Verify that the size of the size of the IAR buffer is the
1333 same as the raw size of the PC (in the register cache). If
1334 they're not, then either GDB has been built incorrectly, or
1335 there's some other kind of internal error. To be really safe,
1336 we should check all of the sizes. */
1337 gdb_assert (sizeof (*iar
) == register_size (current_gdbarch
, PC_REGNUM
));
1339 if (register_cached (PC_REGNUM
))
1340 regcache_raw_collect (current_regcache
, PC_REGNUM
, iar
);
1341 if (register_cached (tdep
->ppc_ps_regnum
))
1342 regcache_raw_collect (current_regcache
, tdep
->ppc_ps_regnum
, msr
);
1343 if (register_cached (tdep
->ppc_cr_regnum
))
1344 regcache_raw_collect (current_regcache
, tdep
->ppc_cr_regnum
, cr
);
1345 if (register_cached (tdep
->ppc_lr_regnum
))
1346 regcache_raw_collect (current_regcache
, tdep
->ppc_lr_regnum
, lr
);
1347 if (register_cached (tdep
->ppc_ctr_regnum
))
1348 regcache_raw_collect (current_regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1349 if (register_cached (tdep
->ppc_xer_regnum
))
1350 regcache_raw_collect (current_regcache
, tdep
->ppc_xer_regnum
, xer
);
1351 if (tdep
->ppc_fpscr_regnum
>= 0
1352 && register_cached (tdep
->ppc_fpscr_regnum
))
1353 regcache_raw_collect (current_regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1357 fill_sprs32 (unsigned long *iar
, unsigned long *msr
, unsigned long *cr
,
1358 unsigned long *lr
, unsigned long *ctr
, unsigned long *xer
,
1359 unsigned long *fpscr
)
1361 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1363 /* Verify that the size of the size of the IAR buffer is the
1364 same as the raw size of the PC (in the register cache). If
1365 they're not, then either GDB has been built incorrectly, or
1366 there's some other kind of internal error. To be really safe,
1367 we should check all of the sizes.
1369 If this assert() fails, the most likely reason is that GDB was
1370 built incorrectly. In order to make use of many of the header
1371 files in /usr/include/sys, GDB needs to be configured so that
1372 sizeof (long) == 4). */
1373 gdb_assert (sizeof (*iar
) == register_size (current_gdbarch
, PC_REGNUM
));
1375 if (register_cached (PC_REGNUM
))
1376 regcache_raw_collect (current_regcache
, PC_REGNUM
, iar
);
1377 if (register_cached (tdep
->ppc_ps_regnum
))
1378 regcache_raw_collect (current_regcache
, tdep
->ppc_ps_regnum
, msr
);
1379 if (register_cached (tdep
->ppc_cr_regnum
))
1380 regcache_raw_collect (current_regcache
, tdep
->ppc_cr_regnum
, cr
);
1381 if (register_cached (tdep
->ppc_lr_regnum
))
1382 regcache_raw_collect (current_regcache
, tdep
->ppc_lr_regnum
, lr
);
1383 if (register_cached (tdep
->ppc_ctr_regnum
))
1384 regcache_raw_collect (current_regcache
, tdep
->ppc_ctr_regnum
, ctr
);
1385 if (register_cached (tdep
->ppc_xer_regnum
))
1386 regcache_raw_collect (current_regcache
, tdep
->ppc_xer_regnum
, xer
);
1387 if (tdep
->ppc_fpscr_regnum
>= 0
1388 && register_cached (tdep
->ppc_fpscr_regnum
))
1389 regcache_raw_collect (current_regcache
, tdep
->ppc_fpscr_regnum
, fpscr
);
1392 /* Store all registers into pthread PDTID, which doesn't have a kernel
1395 It's possible to store a single register into a non-kernel pthread,
1396 but I doubt it's worth the effort. */
1399 store_regs_user_thread (pthdb_pthread_t pdtid
)
1401 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1403 pthdb_context_t ctx
;
1408 if (debug_aix_thread
)
1409 fprintf_unfiltered (gdb_stdlog
,
1410 "store_regs_user_thread %lx\n", (long) pdtid
);
1412 /* Retrieve the thread's current context for its non-register
1414 status
= pthdb_pthread_context (pd_session
, pdtid
, &ctx
);
1415 if (status
!= PTHDB_SUCCESS
)
1416 error ("aix-thread: store_registers: pthdb_pthread_context returned %s",
1417 pd_status2str (status
));
1419 /* Collect general-purpose register values from the regcache. */
1421 for (i
= 0; i
< ppc_num_gprs
; i
++)
1422 if (register_cached (tdep
->ppc_gp0_regnum
+ i
))
1426 regcache_raw_collect (current_regcache
, tdep
->ppc_gp0_regnum
+ i
,
1432 regcache_raw_collect (current_regcache
, tdep
->ppc_gp0_regnum
+ i
,
1438 /* Collect floating-point register values from the regcache. */
1439 if (ppc_floating_point_unit_p (current_gdbarch
))
1440 fill_fprs (ctx
.fpr
);
1442 /* Special registers (always kept in ctx as 64 bits). */
1445 fill_sprs64 (&ctx
.iar
, &ctx
.msr
, &ctx
.cr
, &ctx
.lr
, &ctx
.ctr
, &ctx
.xer
,
1450 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1451 Solution: use 32-bit temp variables. (The assert() in fill_sprs32()
1452 will fail if the size of an unsigned long is incorrect. If this
1453 happens, GDB needs to be reconfigured so that longs are 32-bits.) */
1454 unsigned long tmp_iar
, tmp_msr
, tmp_cr
, tmp_lr
, tmp_ctr
, tmp_xer
,
1457 fill_sprs32 (&tmp_iar
, &tmp_msr
, &tmp_cr
, &tmp_lr
, &tmp_ctr
, &tmp_xer
,
1459 if (register_cached (PC_REGNUM
))
1461 if (register_cached (tdep
->ppc_ps_regnum
))
1463 if (register_cached (tdep
->ppc_cr_regnum
))
1465 if (register_cached (tdep
->ppc_lr_regnum
))
1467 if (register_cached (tdep
->ppc_ctr_regnum
))
1469 if (register_cached (tdep
->ppc_xer_regnum
))
1471 if (register_cached (tdep
->ppc_xer_regnum
))
1472 ctx
.fpscr
= tmp_fpscr
;
1475 status
= pthdb_pthread_setcontext (pd_session
, pdtid
, &ctx
);
1476 if (status
!= PTHDB_SUCCESS
)
1477 error ("aix-thread: store_registers: pthdb_pthread_setcontext returned %s",
1478 pd_status2str (status
));
1481 /* Store register REGNO if != -1 or all registers otherwise into
1484 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1485 SPRs, but there's no way to set individual registers within those
1486 groups. Therefore, if REGNO != -1, this function stores an entire
1490 store_regs_kernel_thread (int regno
, pthdb_tid_t tid
)
1492 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1493 uint64_t gprs64
[ppc_num_gprs
];
1494 uint32_t gprs32
[ppc_num_gprs
];
1495 double fprs
[ppc_num_fprs
];
1496 struct ptxsprs sprs64
;
1497 struct ptsprs sprs32
;
1500 if (debug_aix_thread
)
1501 fprintf_unfiltered (gdb_stdlog
,
1502 "store_regs_kernel_thread tid=%lx regno=%d\n",
1505 /* General-purpose registers. */
1507 || (tdep
->ppc_gp0_regnum
<= regno
1508 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_fprs
))
1512 /* Pre-fetch: some regs may not be in the cache. */
1513 ptrace64aix (PTT_READ_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1514 fill_gprs64 (gprs64
);
1515 ptrace64aix (PTT_WRITE_GPRS
, tid
, (unsigned long) gprs64
, 0, NULL
);
1519 /* Pre-fetch: some regs may not be in the cache. */
1520 ptrace32 (PTT_READ_GPRS
, tid
, gprs32
, 0, NULL
);
1521 fill_gprs32 (gprs32
);
1522 ptrace32 (PTT_WRITE_GPRS
, tid
, gprs32
, 0, NULL
);
1526 /* Floating-point registers. */
1528 if (ppc_floating_point_unit_p (current_gdbarch
)
1530 || (regno
>= tdep
->ppc_fp0_regnum
1531 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)))
1533 /* Pre-fetch: some regs may not be in the cache. */
1534 ptrace32 (PTT_READ_FPRS
, tid
, (int *) fprs
, 0, NULL
);
1536 ptrace32 (PTT_WRITE_FPRS
, tid
, (int *) fprs
, 0, NULL
);
1539 /* Special-purpose registers. */
1541 if (regno
== -1 || special_register_p (regno
))
1545 /* Pre-fetch: some registers won't be in the cache. */
1546 ptrace64aix (PTT_READ_SPRS
, tid
,
1547 (unsigned long) &sprs64
, 0, NULL
);
1548 fill_sprs64 (&sprs64
.pt_iar
, &sprs64
.pt_msr
, &sprs64
.pt_cr
,
1549 &sprs64
.pt_lr
, &sprs64
.pt_ctr
, &sprs64
.pt_xer
,
1551 ptrace64aix (PTT_WRITE_SPRS
, tid
,
1552 (unsigned long) &sprs64
, 0, NULL
);
1556 /* Pre-fetch: some registers won't be in the cache. */
1557 ptrace32 (PTT_READ_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1559 fill_sprs32 (&sprs32
.pt_iar
, &sprs32
.pt_msr
, &sprs32
.pt_cr
,
1560 &sprs32
.pt_lr
, &sprs32
.pt_ctr
, &sprs32
.pt_xer
,
1563 if (tdep
->ppc_mq_regnum
>= 0)
1564 if (register_cached (tdep
->ppc_mq_regnum
))
1565 regcache_raw_collect (current_regcache
, tdep
->ppc_mq_regnum
,
1568 ptrace32 (PTT_WRITE_SPRS
, tid
, (int *) &sprs32
, 0, NULL
);
1573 /* Store gdb's current view of the register set into the
1574 thread/process specified by inferior_ptid. */
1577 aix_thread_store_registers (int regno
)
1579 struct thread_info
*thread
;
1582 if (!PD_TID (inferior_ptid
))
1583 base_target
.to_store_registers (regno
);
1586 thread
= find_thread_pid (inferior_ptid
);
1587 tid
= thread
->private->tid
;
1589 if (tid
== PTHDB_INVALID_TID
)
1590 store_regs_user_thread (thread
->private->pdtid
);
1592 store_regs_kernel_thread (regno
, tid
);
1596 /* Transfer LEN bytes of memory from GDB address MYADDR to target
1597 address MEMADDR if WRITE and vice versa otherwise. */
1600 aix_thread_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
1601 struct mem_attrib
*attrib
,
1602 struct target_ops
*target
)
1605 struct cleanup
*cleanup
= save_inferior_ptid ();
1607 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1608 n
= base_target
.to_xfer_memory (memaddr
, myaddr
, len
,
1609 write
, attrib
, &base_target
);
1610 do_cleanups (cleanup
);
1615 /* Kill and forget about the inferior process. */
1618 aix_thread_kill (void)
1620 struct cleanup
*cleanup
= save_inferior_ptid ();
1622 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1623 base_target
.to_kill ();
1624 do_cleanups (cleanup
);
1627 /* Clean up after the inferior exits. */
1630 aix_thread_mourn_inferior (void)
1633 base_target
.to_mourn_inferior ();
1636 /* Return whether thread PID is still valid. */
1639 aix_thread_thread_alive (ptid_t ptid
)
1642 return base_target
.to_thread_alive (ptid
);
1644 /* We update the thread list every time the child stops, so all
1645 valid threads should be in the thread list. */
1646 return in_thread_list (ptid
);
1649 /* Return a printable representation of composite PID for use in
1650 "info threads" output. */
1653 aix_thread_pid_to_str (ptid_t ptid
)
1655 static char *ret
= NULL
;
1658 return base_target
.to_pid_to_str (ptid
);
1660 /* Free previous return value; a new one will be allocated by
1664 ret
= xstrprintf ("Thread %ld", ptid_get_tid (ptid
));
1668 /* Return a printable representation of extra information about
1669 THREAD, for use in "info threads" output. */
1672 aix_thread_extra_thread_info (struct thread_info
*thread
)
1674 struct ui_file
*buf
;
1676 pthdb_pthread_t pdtid
;
1678 pthdb_state_t state
;
1679 pthdb_suspendstate_t suspendstate
;
1680 pthdb_detachstate_t detachstate
;
1683 static char *ret
= NULL
;
1685 if (!PD_TID (thread
->ptid
))
1688 buf
= mem_fileopen ();
1690 pdtid
= thread
->private->pdtid
;
1691 tid
= thread
->private->tid
;
1693 if (tid
!= PTHDB_INVALID_TID
)
1694 fprintf_unfiltered (buf
, "tid %d", tid
);
1696 status
= pthdb_pthread_state (pd_session
, pdtid
, &state
);
1697 if (status
!= PTHDB_SUCCESS
)
1699 fprintf_unfiltered (buf
, ", %s", state2str (state
));
1701 status
= pthdb_pthread_suspendstate (pd_session
, pdtid
,
1703 if (status
== PTHDB_SUCCESS
&& suspendstate
== PSS_SUSPENDED
)
1704 fprintf_unfiltered (buf
, ", suspended");
1706 status
= pthdb_pthread_detachstate (pd_session
, pdtid
,
1708 if (status
== PTHDB_SUCCESS
&& detachstate
== PDS_DETACHED
)
1709 fprintf_unfiltered (buf
, ", detached");
1711 pthdb_pthread_cancelpend (pd_session
, pdtid
, &cancelpend
);
1712 if (status
== PTHDB_SUCCESS
&& cancelpend
)
1713 fprintf_unfiltered (buf
, ", cancel pending");
1715 ui_file_write (buf
, "", 1);
1717 xfree (ret
); /* Free old buffer. */
1719 ret
= ui_file_xstrdup (buf
, &length
);
1720 ui_file_delete (buf
);
1725 /* Initialize target aix_thread_ops. */
1728 init_aix_thread_ops (void)
1730 aix_thread_ops
.to_shortname
= "aix-threads";
1731 aix_thread_ops
.to_longname
= "AIX pthread support";
1732 aix_thread_ops
.to_doc
= "AIX pthread support";
1734 aix_thread_ops
.to_attach
= aix_thread_attach
;
1735 aix_thread_ops
.to_detach
= aix_thread_detach
;
1736 aix_thread_ops
.to_resume
= aix_thread_resume
;
1737 aix_thread_ops
.to_wait
= aix_thread_wait
;
1738 aix_thread_ops
.to_fetch_registers
= aix_thread_fetch_registers
;
1739 aix_thread_ops
.to_store_registers
= aix_thread_store_registers
;
1740 aix_thread_ops
.to_xfer_memory
= aix_thread_xfer_memory
;
1741 /* No need for aix_thread_ops.to_create_inferior, because we activate thread
1742 debugging when the inferior reaches pd_brk_addr. */
1743 aix_thread_ops
.to_kill
= aix_thread_kill
;
1744 aix_thread_ops
.to_mourn_inferior
= aix_thread_mourn_inferior
;
1745 aix_thread_ops
.to_thread_alive
= aix_thread_thread_alive
;
1746 aix_thread_ops
.to_pid_to_str
= aix_thread_pid_to_str
;
1747 aix_thread_ops
.to_extra_thread_info
= aix_thread_extra_thread_info
;
1748 aix_thread_ops
.to_stratum
= thread_stratum
;
1749 aix_thread_ops
.to_magic
= OPS_MAGIC
;
1752 /* Module startup initialization function, automagically called by
1756 _initialize_aix_thread (void)
1758 init_aix_thread_ops ();
1759 add_target (&aix_thread_ops
);
1761 /* Notice when object files get loaded and unloaded. */
1762 target_new_objfile_chain
= deprecated_target_new_objfile_hook
;
1763 deprecated_target_new_objfile_hook
= new_objfile
;
1765 deprecated_add_show_from_set
1766 (add_set_cmd ("aix-thread", no_class
, var_zinteger
,
1767 (char *) &debug_aix_thread
,
1768 "Set debugging of AIX thread module.\n"
1769 "Enables printf debugging output.\n",