binutils/
[binutils-gdb.git] / gdb / aix-thread.c
blob806dcbe1e25d35044455d90324dd8f8da0ad21fd
1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2013 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().
42 #include "defs.h"
43 #include "gdb_assert.h"
44 #include "gdbthread.h"
45 #include "target.h"
46 #include "inferior.h"
47 #include "regcache.h"
48 #include "gdbcmd.h"
49 #include "ppc-tdep.h"
50 #include "gdb_string.h"
51 #include "observer.h"
53 #include <procinfo.h>
54 #include <sys/types.h>
55 #include <sys/ptrace.h>
56 #include <sys/reg.h>
57 #include <sched.h>
58 #include <sys/pthdebug.h>
60 #if !HAVE_DECL_GETTHRDS
61 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
62 #endif
64 /* Whether to emit debugging output. */
65 static int debug_aix_thread;
67 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
68 #ifndef PTHDB_VERSION_3
69 #define pthdb_tid_t tid_t
70 #endif
72 /* Return whether to treat PID as a debuggable thread id. */
74 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
76 /* Build a thread ptid. */
77 #define BUILD_THREAD(TID, PID) ptid_build (PID, 0, TID)
79 /* Build and lwp ptid. */
80 #define BUILD_LWP(LWP, PID) MERGEPID (PID, LWP)
82 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
83 PTHDB_BAD_USER errors, so use 1. */
85 #define PD_USER 1
87 /* Success and failure values returned by pthdb callbacks. */
89 #define PDC_SUCCESS PTHDB_SUCCESS
90 #define PDC_FAILURE PTHDB_CALLBACK
92 /* Private data attached to each element in GDB's thread list. */
94 struct private_thread_info {
95 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
96 pthdb_tid_t tid; /* kernel thread id */
99 /* Information about a thread of which libpthdebug is aware. */
101 struct pd_thread {
102 pthdb_pthread_t pdtid;
103 pthread_t pthid;
104 pthdb_tid_t tid;
107 /* This module's target-specific operations, active while pd_able is true. */
109 static struct target_ops aix_thread_ops;
111 /* Address of the function that libpthread will call when libpthdebug
112 is ready to be initialized. */
114 static CORE_ADDR pd_brk_addr;
116 /* Whether the current application is debuggable by pthdb. */
118 static int pd_able = 0;
120 /* Whether a threaded application is being debugged. */
122 static int pd_active = 0;
124 /* Whether the current architecture is 64-bit.
125 Only valid when pd_able is true. */
127 static int arch64;
129 /* Forward declarations for pthdb callbacks. */
131 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
132 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
133 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
134 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
135 unsigned long long flags,
136 pthdb_context_t *context);
137 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
138 unsigned long long flags,
139 pthdb_context_t *context);
140 static int pdc_alloc (pthdb_user_t, size_t, void **);
141 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
142 static int pdc_dealloc (pthdb_user_t, void *);
144 /* pthdb callbacks. */
146 static pthdb_callbacks_t pd_callbacks = {
147 pdc_symbol_addrs,
148 pdc_read_data,
149 pdc_write_data,
150 pdc_read_regs,
151 pdc_write_regs,
152 pdc_alloc,
153 pdc_realloc,
154 pdc_dealloc,
155 NULL
158 /* Current pthdb session. */
160 static pthdb_session_t pd_session;
162 /* Return a printable representation of pthdebug function return
163 STATUS. */
165 static char *
166 pd_status2str (int status)
168 switch (status)
170 case PTHDB_SUCCESS: return "SUCCESS";
171 case PTHDB_NOSYS: return "NOSYS";
172 case PTHDB_NOTSUP: return "NOTSUP";
173 case PTHDB_BAD_VERSION: return "BAD_VERSION";
174 case PTHDB_BAD_USER: return "BAD_USER";
175 case PTHDB_BAD_SESSION: return "BAD_SESSION";
176 case PTHDB_BAD_MODE: return "BAD_MODE";
177 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
178 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
179 case PTHDB_BAD_POINTER: return "BAD_POINTER";
180 case PTHDB_BAD_CMD: return "BAD_CMD";
181 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
182 case PTHDB_BAD_ATTR: return "BAD_ATTR";
183 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
184 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
185 case PTHDB_BAD_COND: return "BAD_COND";
186 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
187 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
188 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
189 case PTHDB_BAD_KEY: return "BAD_KEY";
190 case PTHDB_BAD_PTID: return "BAD_PTID";
191 case PTHDB_BAD_TID: return "BAD_TID";
192 case PTHDB_CALLBACK: return "CALLBACK";
193 case PTHDB_CONTEXT: return "CONTEXT";
194 case PTHDB_HELD: return "HELD";
195 case PTHDB_NOT_HELD: return "NOT_HELD";
196 case PTHDB_MEMORY: return "MEMORY";
197 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
198 case PTHDB_SYMBOL: return "SYMBOL";
199 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
200 case PTHDB_INTERNAL: return "INTERNAL";
201 default: return "UNKNOWN";
205 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
206 exceptional conditions and either return nonlocally or else return
207 1 for success and 0 for failure. */
209 static int
210 ptrace_check (int req, int id, int ret)
212 if (ret == 0 && !errno)
213 return 1;
215 /* According to ptrace(2), ptrace may fail with EPERM if "the
216 Identifier parameter corresponds to a kernel thread which is
217 stopped in kernel mode and whose computational state cannot be
218 read or written." This happens quite often with register reads. */
220 switch (req)
222 case PTT_READ_GPRS:
223 case PTT_READ_FPRS:
224 case PTT_READ_SPRS:
225 if (ret == -1 && errno == EPERM)
227 if (debug_aix_thread)
228 fprintf_unfiltered (gdb_stdlog,
229 "ptrace (%d, %d) = %d (errno = %d)\n",
230 req, id, ret, errno);
231 return ret == -1 ? 0 : 1;
233 break;
235 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
236 req, id, ret, errno, safe_strerror (errno));
237 return 0; /* Not reached. */
240 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
241 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
242 Return success. */
244 #ifdef HAVE_PTRACE64
245 # define ptracex(request, pid, addr, data, buf) \
246 ptrace64 (request, pid, addr, data, buf)
247 #endif
249 static int
250 ptrace64aix (int req, int id, long long addr, int data, int *buf)
252 errno = 0;
253 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
256 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
257 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
258 Return success. */
260 #ifdef HAVE_PTRACE64
261 # define ptrace(request, pid, addr, data, buf) \
262 ptrace64 (request, pid, addr, data, buf)
263 # define addr_ptr long long
264 #else
265 # define addr_ptr int *
266 #endif
268 static int
269 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
271 errno = 0;
272 return ptrace_check (req, id,
273 ptrace (req, id, (addr_ptr) addr, data, buf));
276 /* If *PIDP is a composite process/thread id, convert it to a
277 process id. */
279 static void
280 pid_to_prc (ptid_t *ptidp)
282 ptid_t ptid;
284 ptid = *ptidp;
285 if (PD_TID (ptid))
286 *ptidp = pid_to_ptid (PIDGET (ptid));
289 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
290 the address of SYMBOLS[<i>].name. */
292 static int
293 pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
295 struct minimal_symbol *ms;
296 int i;
297 char *name;
299 if (debug_aix_thread)
300 fprintf_unfiltered (gdb_stdlog,
301 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
302 user, (long) symbols, count);
304 for (i = 0; i < count; i++)
306 name = symbols[i].name;
307 if (debug_aix_thread)
308 fprintf_unfiltered (gdb_stdlog,
309 " symbols[%d].name = \"%s\"\n", i, name);
311 if (!*name)
312 symbols[i].addr = 0;
313 else
315 if (!(ms = lookup_minimal_symbol (name, NULL, NULL)))
317 if (debug_aix_thread)
318 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE\n");
319 return PDC_FAILURE;
321 symbols[i].addr = SYMBOL_VALUE_ADDRESS (ms);
323 if (debug_aix_thread)
324 fprintf_unfiltered (gdb_stdlog, " symbols[%d].addr = %s\n",
325 i, hex_string (symbols[i].addr));
327 if (debug_aix_thread)
328 fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS\n");
329 return PDC_SUCCESS;
332 /* Read registers call back function should be able to read the
333 context information of a debuggee kernel thread from an active
334 process or from a core file. The information should be formatted
335 in context64 form for both 32-bit and 64-bit process.
336 If successful return 0, else non-zero is returned. */
338 static int
339 pdc_read_regs (pthdb_user_t user,
340 pthdb_tid_t tid,
341 unsigned long long flags,
342 pthdb_context_t *context)
344 /* This function doesn't appear to be used, so we could probably
345 just return 0 here. HOWEVER, if it is not defined, the OS will
346 complain and several thread debug functions will fail. In case
347 this is needed, I have implemented what I think it should do,
348 however this code is untested. */
350 uint64_t gprs64[ppc_num_gprs];
351 uint32_t gprs32[ppc_num_gprs];
352 double fprs[ppc_num_fprs];
353 struct ptxsprs sprs64;
354 struct ptsprs sprs32;
356 if (debug_aix_thread)
357 fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
358 (int) tid, hex_string (flags));
360 /* General-purpose registers. */
361 if (flags & PTHDB_FLAG_GPRS)
363 if (arch64)
365 if (!ptrace64aix (PTT_READ_GPRS, tid,
366 (unsigned long) gprs64, 0, NULL))
367 memset (gprs64, 0, sizeof (gprs64));
368 memcpy (context->gpr, gprs64, sizeof(gprs64));
370 else
372 if (!ptrace32 (PTT_READ_GPRS, tid, gprs32, 0, NULL))
373 memset (gprs32, 0, sizeof (gprs32));
374 memcpy (context->gpr, gprs32, sizeof(gprs32));
378 /* Floating-point registers. */
379 if (flags & PTHDB_FLAG_FPRS)
381 if (!ptrace32 (PTT_READ_FPRS, tid, (addr_ptr) fprs, 0, NULL))
382 memset (fprs, 0, sizeof (fprs));
383 memcpy (context->fpr, fprs, sizeof(fprs));
386 /* Special-purpose registers. */
387 if (flags & PTHDB_FLAG_SPRS)
389 if (arch64)
391 if (!ptrace64aix (PTT_READ_SPRS, tid,
392 (unsigned long) &sprs64, 0, NULL))
393 memset (&sprs64, 0, sizeof (sprs64));
394 memcpy (&context->msr, &sprs64, sizeof(sprs64));
396 else
398 if (!ptrace32 (PTT_READ_SPRS, tid, (addr_ptr) &sprs32, 0, NULL))
399 memset (&sprs32, 0, sizeof (sprs32));
400 memcpy (&context->msr, &sprs32, sizeof(sprs32));
403 return 0;
406 /* Write register function should be able to write requested context
407 information to specified debuggee's kernel thread id.
408 If successful return 0, else non-zero is returned. */
410 static int
411 pdc_write_regs (pthdb_user_t user,
412 pthdb_tid_t tid,
413 unsigned long long flags,
414 pthdb_context_t *context)
416 /* This function doesn't appear to be used, so we could probably
417 just return 0 here. HOWEVER, if it is not defined, the OS will
418 complain and several thread debug functions will fail. In case
419 this is needed, I have implemented what I think it should do,
420 however this code is untested. */
422 if (debug_aix_thread)
423 fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
424 (int) tid, hex_string (flags));
426 /* General-purpose registers. */
427 if (flags & PTHDB_FLAG_GPRS)
429 if (arch64)
430 ptrace64aix (PTT_WRITE_GPRS, tid,
431 (unsigned long) context->gpr, 0, NULL);
432 else
433 ptrace32 (PTT_WRITE_GPRS, tid, (addr_ptr) context->gpr, 0, NULL);
436 /* Floating-point registers. */
437 if (flags & PTHDB_FLAG_FPRS)
439 ptrace32 (PTT_WRITE_FPRS, tid, (addr_ptr) context->fpr, 0, NULL);
442 /* Special-purpose registers. */
443 if (flags & PTHDB_FLAG_SPRS)
445 if (arch64)
447 ptrace64aix (PTT_WRITE_SPRS, tid,
448 (unsigned long) &context->msr, 0, NULL);
450 else
452 ptrace32 (PTT_WRITE_SPRS, tid, (addr_ptr) &context->msr, 0, NULL);
455 return 0;
458 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
460 static int
461 pdc_read_data (pthdb_user_t user, void *buf,
462 pthdb_addr_t addr, size_t len)
464 int status, ret;
466 if (debug_aix_thread)
467 fprintf_unfiltered (gdb_stdlog,
468 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
469 user, (long) buf, hex_string (addr), len);
471 status = target_read_memory (addr, buf, len);
472 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
474 if (debug_aix_thread)
475 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n",
476 status, pd_status2str (ret));
477 return ret;
480 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
482 static int
483 pdc_write_data (pthdb_user_t user, void *buf,
484 pthdb_addr_t addr, size_t len)
486 int status, ret;
488 if (debug_aix_thread)
489 fprintf_unfiltered (gdb_stdlog,
490 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
491 user, (long) buf, hex_string (addr), len);
493 status = target_write_memory (addr, buf, len);
494 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
496 if (debug_aix_thread)
497 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n", status,
498 pd_status2str (ret));
499 return ret;
502 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
503 in BUFP. */
505 static int
506 pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
508 if (debug_aix_thread)
509 fprintf_unfiltered (gdb_stdlog,
510 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
511 user, len, (long) bufp);
512 *bufp = xmalloc (len);
513 if (debug_aix_thread)
514 fprintf_unfiltered (gdb_stdlog,
515 " malloc returned 0x%lx\n", (long) *bufp);
517 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
518 be returned. */
520 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
523 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
524 realloc callback, so that it contains LEN bytes, and store a
525 pointer to the result in BUFP. */
527 static int
528 pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
530 if (debug_aix_thread)
531 fprintf_unfiltered (gdb_stdlog,
532 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
533 user, (long) buf, len, (long) bufp);
534 *bufp = xrealloc (buf, len);
535 if (debug_aix_thread)
536 fprintf_unfiltered (gdb_stdlog,
537 " realloc returned 0x%lx\n", (long) *bufp);
538 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
541 /* pthdb callback: free BUF, which was allocated by the alloc or
542 realloc callback. */
544 static int
545 pdc_dealloc (pthdb_user_t user, void *buf)
547 if (debug_aix_thread)
548 fprintf_unfiltered (gdb_stdlog,
549 "pdc_free (user = %ld, buf = 0x%lx)\n", user,
550 (long) buf);
551 xfree (buf);
552 return PDC_SUCCESS;
555 /* Return a printable representation of pthread STATE. */
557 static char *
558 state2str (pthdb_state_t state)
560 switch (state)
562 case PST_IDLE:
563 /* i18n: Like "Thread-Id %d, [state] idle" */
564 return _("idle"); /* being created */
565 case PST_RUN:
566 /* i18n: Like "Thread-Id %d, [state] running" */
567 return _("running"); /* running */
568 case PST_SLEEP:
569 /* i18n: Like "Thread-Id %d, [state] sleeping" */
570 return _("sleeping"); /* awaiting an event */
571 case PST_READY:
572 /* i18n: Like "Thread-Id %d, [state] ready" */
573 return _("ready"); /* runnable */
574 case PST_TERM:
575 /* i18n: Like "Thread-Id %d, [state] finished" */
576 return _("finished"); /* awaiting a join/detach */
577 default:
578 /* i18n: Like "Thread-Id %d, [state] unknown" */
579 return _("unknown");
583 /* qsort() comparison function for sorting pd_thread structs by pthid. */
585 static int
586 pcmp (const void *p1v, const void *p2v)
588 struct pd_thread *p1 = (struct pd_thread *) p1v;
589 struct pd_thread *p2 = (struct pd_thread *) p2v;
590 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
593 /* iterate_over_threads() callback for counting GDB threads.
595 Do not count the main thread (whose tid is zero). This matches
596 the list of threads provided by the pthreaddebug library, which
597 does not include that main thread either, and thus allows us
598 to compare the two lists. */
600 static int
601 giter_count (struct thread_info *thread, void *countp)
603 if (PD_TID (thread->ptid))
604 (*(int *) countp)++;
605 return 0;
608 /* iterate_over_threads() callback for accumulating GDB thread pids.
610 Do not include the main thread (whose tid is zero). This matches
611 the list of threads provided by the pthreaddebug library, which
612 does not include that main thread either, and thus allows us
613 to compare the two lists. */
615 static int
616 giter_accum (struct thread_info *thread, void *bufp)
618 if (PD_TID (thread->ptid))
620 **(struct thread_info ***) bufp = thread;
621 (*(struct thread_info ***) bufp)++;
623 return 0;
626 /* ptid comparison function */
628 static int
629 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
631 int pid1, pid2;
633 if (ptid_get_pid (ptid1) < ptid_get_pid (ptid2))
634 return -1;
635 else if (ptid_get_pid (ptid1) > ptid_get_pid (ptid2))
636 return 1;
637 else if (ptid_get_tid (ptid1) < ptid_get_tid (ptid2))
638 return -1;
639 else if (ptid_get_tid (ptid1) > ptid_get_tid (ptid2))
640 return 1;
641 else if (ptid_get_lwp (ptid1) < ptid_get_lwp (ptid2))
642 return -1;
643 else if (ptid_get_lwp (ptid1) > ptid_get_lwp (ptid2))
644 return 1;
645 else
646 return 0;
649 /* qsort() comparison function for sorting thread_info structs by pid. */
651 static int
652 gcmp (const void *t1v, const void *t2v)
654 struct thread_info *t1 = *(struct thread_info **) t1v;
655 struct thread_info *t2 = *(struct thread_info **) t2v;
656 return ptid_cmp (t1->ptid, t2->ptid);
659 /* Search through the list of all kernel threads for the thread
660 that has stopped on a SIGTRAP signal, and return its TID.
661 Return 0 if none found. */
663 static pthdb_tid_t
664 get_signaled_thread (void)
666 struct thrdsinfo64 thrinf;
667 tid_t ktid = 0;
668 int result = 0;
670 while (1)
672 if (getthrds (PIDGET (inferior_ptid), &thrinf,
673 sizeof (thrinf), &ktid, 1) != 1)
674 break;
676 if (thrinf.ti_cursig == SIGTRAP)
677 return thrinf.ti_tid;
680 /* Didn't find any thread stopped on a SIGTRAP signal. */
681 return 0;
684 /* Synchronize GDB's thread list with libpthdebug's.
686 There are some benefits of doing this every time the inferior stops:
688 - allows users to run thread-specific commands without needing to
689 run "info threads" first
691 - helps pthdb_tid_pthread() work properly (see "libpthdebug
692 peculiarities" at the top of this module)
694 - simplifies the demands placed on libpthdebug, which seems to
695 have difficulty with certain call patterns */
697 static void
698 sync_threadlists (void)
700 int cmd, status, infpid;
701 int pcount, psize, pi, gcount, gi;
702 struct pd_thread *pbuf;
703 struct thread_info **gbuf, **g, *thread;
704 pthdb_pthread_t pdtid;
705 pthread_t pthid;
706 pthdb_tid_t tid;
708 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
710 pcount = 0;
711 psize = 1;
712 pbuf = (struct pd_thread *) xmalloc (psize * sizeof *pbuf);
714 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
716 status = pthdb_pthread (pd_session, &pdtid, cmd);
717 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
718 break;
720 status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
721 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
722 continue;
724 if (pcount == psize)
726 psize *= 2;
727 pbuf = (struct pd_thread *) xrealloc (pbuf,
728 psize * sizeof *pbuf);
730 pbuf[pcount].pdtid = pdtid;
731 pbuf[pcount].pthid = pthid;
732 pcount++;
735 for (pi = 0; pi < pcount; pi++)
737 status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
738 if (status != PTHDB_SUCCESS)
739 tid = PTHDB_INVALID_TID;
740 pbuf[pi].tid = tid;
743 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
745 /* Accumulate an array of GDB threads sorted by pid. */
747 gcount = 0;
748 iterate_over_threads (giter_count, &gcount);
749 g = gbuf = (struct thread_info **) xmalloc (gcount * sizeof *gbuf);
750 iterate_over_threads (giter_accum, &g);
751 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
753 /* Apply differences between the two arrays to GDB's thread list. */
755 infpid = PIDGET (inferior_ptid);
756 for (pi = gi = 0; pi < pcount || gi < gcount;)
758 if (pi == pcount)
760 delete_thread (gbuf[gi]->ptid);
761 gi++;
763 else if (gi == gcount)
765 thread = add_thread (BUILD_THREAD (pbuf[pi].pthid, infpid));
766 thread->private = xmalloc (sizeof (struct private_thread_info));
767 thread->private->pdtid = pbuf[pi].pdtid;
768 thread->private->tid = pbuf[pi].tid;
769 pi++;
771 else
773 ptid_t pptid, gptid;
774 int cmp_result;
776 pptid = BUILD_THREAD (pbuf[pi].pthid, infpid);
777 gptid = gbuf[gi]->ptid;
778 pdtid = pbuf[pi].pdtid;
779 tid = pbuf[pi].tid;
781 cmp_result = ptid_cmp (pptid, gptid);
783 if (cmp_result == 0)
785 gbuf[gi]->private->pdtid = pdtid;
786 gbuf[gi]->private->tid = tid;
787 pi++;
788 gi++;
790 else if (cmp_result > 0)
792 delete_thread (gptid);
793 gi++;
795 else
797 thread = add_thread (pptid);
798 thread->private = xmalloc (sizeof (struct private_thread_info));
799 thread->private->pdtid = pdtid;
800 thread->private->tid = tid;
801 pi++;
806 xfree (pbuf);
807 xfree (gbuf);
810 /* Iterate_over_threads() callback for locating a thread, using
811 the TID of its associated kernel thread. */
813 static int
814 iter_tid (struct thread_info *thread, void *tidp)
816 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
818 return (thread->private->tid == tid);
821 /* Synchronize libpthdebug's state with the inferior and with GDB,
822 generate a composite process/thread <pid> for the current thread,
823 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
825 static ptid_t
826 pd_update (int set_infpid)
828 int status;
829 ptid_t ptid;
830 pthdb_tid_t tid;
831 struct thread_info *thread = NULL;
833 if (!pd_active)
834 return inferior_ptid;
836 status = pthdb_session_update (pd_session);
837 if (status != PTHDB_SUCCESS)
838 return inferior_ptid;
840 sync_threadlists ();
842 /* Define "current thread" as one that just received a trap signal. */
844 tid = get_signaled_thread ();
845 if (tid != 0)
846 thread = iterate_over_threads (iter_tid, &tid);
847 if (!thread)
848 ptid = inferior_ptid;
849 else
851 ptid = thread->ptid;
852 if (set_infpid)
853 inferior_ptid = ptid;
855 return ptid;
858 /* Try to start debugging threads in the current process.
859 If successful and SET_INFPID, set inferior_ptid to reflect the
860 current thread. */
862 static ptid_t
863 pd_activate (int set_infpid)
865 int status;
867 status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
868 PTHDB_FLAG_REGS, &pd_callbacks,
869 &pd_session);
870 if (status != PTHDB_SUCCESS)
872 return inferior_ptid;
874 pd_active = 1;
875 return pd_update (set_infpid);
878 /* Undo the effects of pd_activate(). */
880 static void
881 pd_deactivate (void)
883 if (!pd_active)
884 return;
885 pthdb_session_destroy (pd_session);
887 pid_to_prc (&inferior_ptid);
888 pd_active = 0;
891 /* An object file has just been loaded. Check whether the current
892 application is pthreaded, and if so, prepare for thread debugging. */
894 static void
895 pd_enable (void)
897 int status;
898 char *stub_name;
899 struct minimal_symbol *ms;
901 /* Don't initialize twice. */
902 if (pd_able)
903 return;
905 /* Check application word size. */
906 arch64 = register_size (target_gdbarch (), 0) == 8;
908 /* Check whether the application is pthreaded. */
909 stub_name = NULL;
910 status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
911 &pd_callbacks, &stub_name);
912 if ((status != PTHDB_SUCCESS
913 && status != PTHDB_NOT_PTHREADED) || !stub_name)
914 return;
916 /* Set a breakpoint on the returned stub function. */
917 if (!(ms = lookup_minimal_symbol (stub_name, NULL, NULL)))
918 return;
919 pd_brk_addr = SYMBOL_VALUE_ADDRESS (ms);
920 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
921 return;
923 /* Prepare for thread debugging. */
924 push_target (&aix_thread_ops);
925 pd_able = 1;
927 /* If we're debugging a core file or an attached inferior, the
928 pthread library may already have been initialized, so try to
929 activate thread debugging. */
930 pd_activate (1);
933 /* Undo the effects of pd_enable(). */
935 static void
936 pd_disable (void)
938 if (!pd_able)
939 return;
940 if (pd_active)
941 pd_deactivate ();
942 pd_able = 0;
943 unpush_target (&aix_thread_ops);
946 /* new_objfile observer callback.
948 If OBJFILE is non-null, check whether a threaded application is
949 being debugged, and if so, prepare for thread debugging.
951 If OBJFILE is null, stop debugging threads. */
953 static void
954 new_objfile (struct objfile *objfile)
956 if (objfile)
957 pd_enable ();
958 else
959 pd_disable ();
962 /* Attach to process specified by ARGS. */
964 static void
965 aix_thread_attach (struct target_ops *ops, char *args, int from_tty)
967 struct target_ops *beneath = find_target_beneath (ops);
969 beneath->to_attach (beneath, args, from_tty);
970 pd_activate (1);
973 /* Detach from the process attached to by aix_thread_attach(). */
975 static void
976 aix_thread_detach (struct target_ops *ops, char *args, int from_tty)
978 struct target_ops *beneath = find_target_beneath (ops);
980 pd_disable ();
981 beneath->to_detach (beneath, args, from_tty);
984 /* Tell the inferior process to continue running thread PID if != -1
985 and all threads otherwise. */
987 static void
988 aix_thread_resume (struct target_ops *ops,
989 ptid_t ptid, int step, enum gdb_signal sig)
991 struct thread_info *thread;
992 pthdb_tid_t tid[2];
994 if (!PD_TID (ptid))
996 struct cleanup *cleanup = save_inferior_ptid ();
997 struct target_ops *beneath = find_target_beneath (ops);
999 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1000 beneath->to_resume (beneath, ptid, step, sig);
1001 do_cleanups (cleanup);
1003 else
1005 thread = find_thread_ptid (ptid);
1006 if (!thread)
1007 error (_("aix-thread resume: unknown pthread %ld"),
1008 TIDGET (ptid));
1010 tid[0] = thread->private->tid;
1011 if (tid[0] == PTHDB_INVALID_TID)
1012 error (_("aix-thread resume: no tid for pthread %ld"),
1013 TIDGET (ptid));
1014 tid[1] = 0;
1016 if (arch64)
1017 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1018 gdb_signal_to_host (sig), (void *) tid);
1019 else
1020 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1021 gdb_signal_to_host (sig), (void *) tid);
1025 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1026 If an error occurs, return -1, else return the pid of the stopped
1027 thread. */
1029 static ptid_t
1030 aix_thread_wait (struct target_ops *ops,
1031 ptid_t ptid, struct target_waitstatus *status, int options)
1033 struct cleanup *cleanup = save_inferior_ptid ();
1034 struct target_ops *beneath = find_target_beneath (ops);
1036 pid_to_prc (&ptid);
1038 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1039 ptid = beneath->to_wait (beneath, ptid, status, options);
1040 do_cleanups (cleanup);
1042 if (PIDGET (ptid) == -1)
1043 return pid_to_ptid (-1);
1045 /* Check whether libpthdebug might be ready to be initialized. */
1046 if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
1047 && status->value.sig == GDB_SIGNAL_TRAP)
1049 struct regcache *regcache = get_thread_regcache (ptid);
1050 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1052 if (regcache_read_pc (regcache)
1053 - gdbarch_decr_pc_after_break (gdbarch) == pd_brk_addr)
1054 return pd_activate (0);
1057 return pd_update (0);
1060 /* Record that the 64-bit general-purpose registers contain VALS. */
1062 static void
1063 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1065 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1066 int regno;
1068 for (regno = 0; regno < ppc_num_gprs; regno++)
1069 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + regno,
1070 (char *) (vals + regno));
1073 /* Record that 32-bit register REGNO contains VAL. */
1075 static void
1076 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1078 regcache_raw_supply (regcache, regno, (char *) &val);
1081 /* Record that the floating-point registers contain VALS. */
1083 static void
1084 supply_fprs (struct regcache *regcache, double *vals)
1086 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1087 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1088 int regno;
1090 /* This function should never be called on architectures without
1091 floating-point registers. */
1092 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1094 for (regno = tdep->ppc_fp0_regnum;
1095 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1096 regno++)
1097 regcache_raw_supply (regcache, regno,
1098 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1101 /* Predicate to test whether given register number is a "special" register. */
1102 static int
1103 special_register_p (struct gdbarch *gdbarch, int regno)
1105 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1107 return regno == gdbarch_pc_regnum (gdbarch)
1108 || regno == tdep->ppc_ps_regnum
1109 || regno == tdep->ppc_cr_regnum
1110 || regno == tdep->ppc_lr_regnum
1111 || regno == tdep->ppc_ctr_regnum
1112 || regno == tdep->ppc_xer_regnum
1113 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1114 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1118 /* Record that the special registers contain the specified 64-bit and
1119 32-bit values. */
1121 static void
1122 supply_sprs64 (struct regcache *regcache,
1123 uint64_t iar, uint64_t msr, uint32_t cr,
1124 uint64_t lr, uint64_t ctr, uint32_t xer,
1125 uint32_t fpscr)
1127 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1128 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1130 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1131 (char *) &iar);
1132 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1133 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1134 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1135 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1136 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1137 if (tdep->ppc_fpscr_regnum >= 0)
1138 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1139 (char *) &fpscr);
1142 /* Record that the special registers contain the specified 32-bit
1143 values. */
1145 static void
1146 supply_sprs32 (struct regcache *regcache,
1147 uint32_t iar, uint32_t msr, uint32_t cr,
1148 uint32_t lr, uint32_t ctr, uint32_t xer,
1149 uint32_t fpscr)
1151 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1152 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1154 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1155 (char *) &iar);
1156 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1157 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1158 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1159 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1160 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1161 if (tdep->ppc_fpscr_regnum >= 0)
1162 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1163 (char *) &fpscr);
1166 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1167 thread.
1169 There's no way to query a single register from a non-kernel
1170 pthread, so there's no need for a single-register version of this
1171 function. */
1173 static void
1174 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1176 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1177 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1178 int status, i;
1179 pthdb_context_t ctx;
1181 if (debug_aix_thread)
1182 fprintf_unfiltered (gdb_stdlog,
1183 "fetch_regs_user_thread %lx\n", (long) pdtid);
1184 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1185 if (status != PTHDB_SUCCESS)
1186 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1187 pd_status2str (status));
1189 /* General-purpose registers. */
1191 if (arch64)
1192 supply_gprs64 (regcache, ctx.gpr);
1193 else
1194 for (i = 0; i < ppc_num_gprs; i++)
1195 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1197 /* Floating-point registers. */
1199 if (ppc_floating_point_unit_p (gdbarch))
1200 supply_fprs (regcache, ctx.fpr);
1202 /* Special registers. */
1204 if (arch64)
1205 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1206 ctx.xer, ctx.fpscr);
1207 else
1208 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1209 ctx.xer, ctx.fpscr);
1212 /* Fetch register REGNO if != -1 or all registers otherwise from
1213 kernel thread TID.
1215 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1216 SPRs, but there's no way to query individual registers within those
1217 groups. Therefore, if REGNO != -1, this function fetches an entire
1218 group.
1220 Unfortunately, kernel thread register queries often fail with
1221 EPERM, indicating that the thread is in kernel space. This breaks
1222 backtraces of threads other than the current one. To make that
1223 breakage obvious without throwing an error to top level (which is
1224 bad e.g. during "info threads" output), zero registers that can't
1225 be retrieved. */
1227 static void
1228 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1229 pthdb_tid_t tid)
1231 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1232 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1233 uint64_t gprs64[ppc_num_gprs];
1234 uint32_t gprs32[ppc_num_gprs];
1235 double fprs[ppc_num_fprs];
1236 struct ptxsprs sprs64;
1237 struct ptsprs sprs32;
1238 int i;
1240 if (debug_aix_thread)
1241 fprintf_unfiltered (gdb_stdlog,
1242 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1243 (long) tid, regno, arch64);
1245 /* General-purpose registers. */
1246 if (regno == -1
1247 || (tdep->ppc_gp0_regnum <= regno
1248 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1250 if (arch64)
1252 if (!ptrace64aix (PTT_READ_GPRS, tid,
1253 (unsigned long) gprs64, 0, NULL))
1254 memset (gprs64, 0, sizeof (gprs64));
1255 supply_gprs64 (regcache, gprs64);
1257 else
1259 if (!ptrace32 (PTT_READ_GPRS, tid, (addr_ptr) gprs32, 0, NULL))
1260 memset (gprs32, 0, sizeof (gprs32));
1261 for (i = 0; i < ppc_num_gprs; i++)
1262 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1266 /* Floating-point registers. */
1268 if (ppc_floating_point_unit_p (gdbarch)
1269 && (regno == -1
1270 || (regno >= tdep->ppc_fp0_regnum
1271 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1273 if (!ptrace32 (PTT_READ_FPRS, tid, (addr_ptr) fprs, 0, NULL))
1274 memset (fprs, 0, sizeof (fprs));
1275 supply_fprs (regcache, fprs);
1278 /* Special-purpose registers. */
1280 if (regno == -1 || special_register_p (gdbarch, regno))
1282 if (arch64)
1284 if (!ptrace64aix (PTT_READ_SPRS, tid,
1285 (unsigned long) &sprs64, 0, NULL))
1286 memset (&sprs64, 0, sizeof (sprs64));
1287 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1288 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1289 sprs64.pt_xer, sprs64.pt_fpscr);
1291 else
1293 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1295 if (!ptrace32 (PTT_READ_SPRS, tid, (addr_ptr) &sprs32, 0, NULL))
1296 memset (&sprs32, 0, sizeof (sprs32));
1297 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1298 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1299 sprs32.pt_fpscr);
1301 if (tdep->ppc_mq_regnum >= 0)
1302 regcache_raw_supply (regcache, tdep->ppc_mq_regnum,
1303 (char *) &sprs32.pt_mq);
1308 /* Fetch register REGNO if != -1 or all registers otherwise in the
1309 thread/process specified by inferior_ptid. */
1311 static void
1312 aix_thread_fetch_registers (struct target_ops *ops,
1313 struct regcache *regcache, int regno)
1315 struct thread_info *thread;
1316 pthdb_tid_t tid;
1317 struct target_ops *beneath = find_target_beneath (ops);
1319 if (!PD_TID (inferior_ptid))
1320 beneath->to_fetch_registers (beneath, regcache, regno);
1321 else
1323 thread = find_thread_ptid (inferior_ptid);
1324 tid = thread->private->tid;
1326 if (tid == PTHDB_INVALID_TID)
1327 fetch_regs_user_thread (regcache, thread->private->pdtid);
1328 else
1329 fetch_regs_kernel_thread (regcache, regno, tid);
1333 /* Store the gp registers into an array of uint32_t or uint64_t. */
1335 static void
1336 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1338 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1339 int regno;
1341 for (regno = 0; regno < ppc_num_gprs; regno++)
1342 if (REG_VALID == regcache_register_status (regcache,
1343 tdep->ppc_gp0_regnum + regno))
1344 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1345 vals + regno);
1348 static void
1349 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1351 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1352 int regno;
1354 for (regno = 0; regno < ppc_num_gprs; regno++)
1355 if (REG_VALID == regcache_register_status (regcache,
1356 tdep->ppc_gp0_regnum + regno))
1357 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1358 vals + regno);
1361 /* Store the floating point registers into a double array. */
1362 static void
1363 fill_fprs (const struct regcache *regcache, double *vals)
1365 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1366 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1367 int regno;
1369 /* This function should never be called on architectures without
1370 floating-point registers. */
1371 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1373 for (regno = tdep->ppc_fp0_regnum;
1374 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1375 regno++)
1376 if (REG_VALID == regcache_register_status (regcache, regno))
1377 regcache_raw_collect (regcache, regno,
1378 vals + regno - tdep->ppc_fp0_regnum);
1381 /* Store the special registers into the specified 64-bit and 32-bit
1382 locations. */
1384 static void
1385 fill_sprs64 (const struct regcache *regcache,
1386 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1387 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1388 uint32_t *fpscr)
1390 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1391 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1393 /* Verify that the size of the size of the IAR buffer is the
1394 same as the raw size of the PC (in the register cache). If
1395 they're not, then either GDB has been built incorrectly, or
1396 there's some other kind of internal error. To be really safe,
1397 we should check all of the sizes. */
1398 gdb_assert (sizeof (*iar) == register_size
1399 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1401 if (REG_VALID == regcache_register_status (regcache,
1402 gdbarch_pc_regnum (gdbarch)))
1403 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1404 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1405 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1406 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1407 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1408 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1409 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1410 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1411 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1412 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1413 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1414 if (tdep->ppc_fpscr_regnum >= 0
1415 && REG_VALID == regcache_register_status (regcache,
1416 tdep->ppc_fpscr_regnum))
1417 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1420 static void
1421 fill_sprs32 (const struct regcache *regcache,
1422 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1423 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1424 uint32_t *fpscr)
1426 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1427 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1429 /* Verify that the size of the size of the IAR buffer is the
1430 same as the raw size of the PC (in the register cache). If
1431 they're not, then either GDB has been built incorrectly, or
1432 there's some other kind of internal error. To be really safe,
1433 we should check all of the sizes. */
1434 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1435 gdbarch_pc_regnum (gdbarch)));
1437 if (REG_VALID == regcache_register_status (regcache,
1438 gdbarch_pc_regnum (gdbarch)))
1439 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1440 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1441 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1442 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1443 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1444 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1445 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1446 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1447 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1448 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1449 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1450 if (tdep->ppc_fpscr_regnum >= 0
1451 && REG_VALID == regcache_register_status (regcache, tdep->ppc_fpscr_regnum))
1452 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1455 /* Store all registers into pthread PDTID, which doesn't have a kernel
1456 thread.
1458 It's possible to store a single register into a non-kernel pthread,
1459 but I doubt it's worth the effort. */
1461 static void
1462 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1464 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1465 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1466 int status, i;
1467 pthdb_context_t ctx;
1468 uint32_t int32;
1469 uint64_t int64;
1470 double dbl;
1472 if (debug_aix_thread)
1473 fprintf_unfiltered (gdb_stdlog,
1474 "store_regs_user_thread %lx\n", (long) pdtid);
1476 /* Retrieve the thread's current context for its non-register
1477 values. */
1478 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1479 if (status != PTHDB_SUCCESS)
1480 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1481 pd_status2str (status));
1483 /* Collect general-purpose register values from the regcache. */
1485 for (i = 0; i < ppc_num_gprs; i++)
1486 if (REG_VALID == regcache_register_status (regcache,
1487 tdep->ppc_gp0_regnum + i))
1489 if (arch64)
1491 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1492 (void *) &int64);
1493 ctx.gpr[i] = int64;
1495 else
1497 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1498 (void *) &int32);
1499 ctx.gpr[i] = int32;
1503 /* Collect floating-point register values from the regcache. */
1504 if (ppc_floating_point_unit_p (gdbarch))
1505 fill_fprs (regcache, ctx.fpr);
1507 /* Special registers (always kept in ctx as 64 bits). */
1508 if (arch64)
1510 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1511 &ctx.xer, &ctx.fpscr);
1513 else
1515 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1516 Solution: use 32-bit temp variables. */
1517 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1518 tmp_fpscr;
1520 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1521 &tmp_xer, &tmp_fpscr);
1522 if (REG_VALID == regcache_register_status (regcache,
1523 gdbarch_pc_regnum (gdbarch)))
1524 ctx.iar = tmp_iar;
1525 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1526 ctx.msr = tmp_msr;
1527 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1528 ctx.cr = tmp_cr;
1529 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1530 ctx.lr = tmp_lr;
1531 if (REG_VALID == regcache_register_status (regcache,
1532 tdep->ppc_ctr_regnum))
1533 ctx.ctr = tmp_ctr;
1534 if (REG_VALID == regcache_register_status (regcache,
1535 tdep->ppc_xer_regnum))
1536 ctx.xer = tmp_xer;
1537 if (REG_VALID == regcache_register_status (regcache,
1538 tdep->ppc_xer_regnum))
1539 ctx.fpscr = tmp_fpscr;
1542 status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1543 if (status != PTHDB_SUCCESS)
1544 error (_("aix-thread: store_registers: "
1545 "pthdb_pthread_setcontext returned %s"),
1546 pd_status2str (status));
1549 /* Store register REGNO if != -1 or all registers otherwise into
1550 kernel thread TID.
1552 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1553 SPRs, but there's no way to set individual registers within those
1554 groups. Therefore, if REGNO != -1, this function stores an entire
1555 group. */
1557 static void
1558 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1559 pthdb_tid_t tid)
1561 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1562 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1563 uint64_t gprs64[ppc_num_gprs];
1564 uint32_t gprs32[ppc_num_gprs];
1565 double fprs[ppc_num_fprs];
1566 struct ptxsprs sprs64;
1567 struct ptsprs sprs32;
1568 int i;
1570 if (debug_aix_thread)
1571 fprintf_unfiltered (gdb_stdlog,
1572 "store_regs_kernel_thread tid=%lx regno=%d\n",
1573 (long) tid, regno);
1575 /* General-purpose registers. */
1576 if (regno == -1
1577 || (tdep->ppc_gp0_regnum <= regno
1578 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1580 if (arch64)
1582 /* Pre-fetch: some regs may not be in the cache. */
1583 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1584 fill_gprs64 (regcache, gprs64);
1585 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1587 else
1589 /* Pre-fetch: some regs may not be in the cache. */
1590 ptrace32 (PTT_READ_GPRS, tid, (addr_ptr) gprs32, 0, NULL);
1591 fill_gprs32 (regcache, gprs32);
1592 ptrace32 (PTT_WRITE_GPRS, tid, (addr_ptr) gprs32, 0, NULL);
1596 /* Floating-point registers. */
1598 if (ppc_floating_point_unit_p (gdbarch)
1599 && (regno == -1
1600 || (regno >= tdep->ppc_fp0_regnum
1601 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1603 /* Pre-fetch: some regs may not be in the cache. */
1604 ptrace32 (PTT_READ_FPRS, tid, (addr_ptr) fprs, 0, NULL);
1605 fill_fprs (regcache, fprs);
1606 ptrace32 (PTT_WRITE_FPRS, tid, (addr_ptr) fprs, 0, NULL);
1609 /* Special-purpose registers. */
1611 if (regno == -1 || special_register_p (gdbarch, regno))
1613 if (arch64)
1615 /* Pre-fetch: some registers won't be in the cache. */
1616 ptrace64aix (PTT_READ_SPRS, tid,
1617 (unsigned long) &sprs64, 0, NULL);
1618 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1619 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1620 &sprs64.pt_xer, &sprs64.pt_fpscr);
1621 ptrace64aix (PTT_WRITE_SPRS, tid,
1622 (unsigned long) &sprs64, 0, NULL);
1624 else
1626 /* The contents of "struct ptspr" were declared as "unsigned
1627 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1628 Use temporaries to work around this problem. Also, add an
1629 assert here to make sure we fail if the system header files
1630 use "unsigned long", and the size of that type is not what
1631 the headers expect. */
1632 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1633 tmp_fpscr;
1635 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1637 /* Pre-fetch: some registers won't be in the cache. */
1638 ptrace32 (PTT_READ_SPRS, tid, (addr_ptr) &sprs32, 0, NULL);
1640 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1641 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1643 sprs32.pt_iar = tmp_iar;
1644 sprs32.pt_msr = tmp_msr;
1645 sprs32.pt_cr = tmp_cr;
1646 sprs32.pt_lr = tmp_lr;
1647 sprs32.pt_ctr = tmp_ctr;
1648 sprs32.pt_xer = tmp_xer;
1649 sprs32.pt_fpscr = tmp_fpscr;
1651 if (tdep->ppc_mq_regnum >= 0)
1652 if (REG_VALID == regcache_register_status (regcache,
1653 tdep->ppc_mq_regnum))
1654 regcache_raw_collect (regcache, tdep->ppc_mq_regnum,
1655 &sprs32.pt_mq);
1657 ptrace32 (PTT_WRITE_SPRS, tid, (addr_ptr) &sprs32, 0, NULL);
1662 /* Store gdb's current view of the register set into the
1663 thread/process specified by inferior_ptid. */
1665 static void
1666 aix_thread_store_registers (struct target_ops *ops,
1667 struct regcache *regcache, int regno)
1669 struct thread_info *thread;
1670 pthdb_tid_t tid;
1671 struct target_ops *beneath = find_target_beneath (ops);
1673 if (!PD_TID (inferior_ptid))
1674 beneath->to_store_registers (beneath, regcache, regno);
1675 else
1677 thread = find_thread_ptid (inferior_ptid);
1678 tid = thread->private->tid;
1680 if (tid == PTHDB_INVALID_TID)
1681 store_regs_user_thread (regcache, thread->private->pdtid);
1682 else
1683 store_regs_kernel_thread (regcache, regno, tid);
1687 /* Attempt a transfer all LEN bytes starting at OFFSET between the
1688 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
1689 Return the number of bytes actually transferred. */
1691 static LONGEST
1692 aix_thread_xfer_partial (struct target_ops *ops, enum target_object object,
1693 const char *annex, gdb_byte *readbuf,
1694 const gdb_byte *writebuf,
1695 ULONGEST offset, LONGEST len)
1697 struct cleanup *old_chain = save_inferior_ptid ();
1698 LONGEST xfer;
1699 struct target_ops *beneath = find_target_beneath (ops);
1701 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1702 xfer = beneath->to_xfer_partial (beneath, object, annex,
1703 readbuf, writebuf, offset, len);
1705 do_cleanups (old_chain);
1706 return xfer;
1709 /* Clean up after the inferior exits. */
1711 static void
1712 aix_thread_mourn_inferior (struct target_ops *ops)
1714 struct target_ops *beneath = find_target_beneath (ops);
1716 pd_deactivate ();
1717 beneath->to_mourn_inferior (beneath);
1720 /* Return whether thread PID is still valid. */
1722 static int
1723 aix_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
1725 struct target_ops *beneath = find_target_beneath (ops);
1727 if (!PD_TID (ptid))
1728 return beneath->to_thread_alive (beneath, ptid);
1730 /* We update the thread list every time the child stops, so all
1731 valid threads should be in the thread list. */
1732 return in_thread_list (ptid);
1735 /* Return a printable representation of composite PID for use in
1736 "info threads" output. */
1738 static char *
1739 aix_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
1741 static char *ret = NULL;
1742 struct target_ops *beneath = find_target_beneath (ops);
1744 if (!PD_TID (ptid))
1745 return beneath->to_pid_to_str (beneath, ptid);
1747 /* Free previous return value; a new one will be allocated by
1748 xstrprintf(). */
1749 xfree (ret);
1751 ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
1752 return ret;
1755 /* Return a printable representation of extra information about
1756 THREAD, for use in "info threads" output. */
1758 static char *
1759 aix_thread_extra_thread_info (struct thread_info *thread)
1761 struct ui_file *buf;
1762 int status;
1763 pthdb_pthread_t pdtid;
1764 pthdb_tid_t tid;
1765 pthdb_state_t state;
1766 pthdb_suspendstate_t suspendstate;
1767 pthdb_detachstate_t detachstate;
1768 int cancelpend;
1769 static char *ret = NULL;
1771 if (!PD_TID (thread->ptid))
1772 return NULL;
1774 buf = mem_fileopen ();
1776 pdtid = thread->private->pdtid;
1777 tid = thread->private->tid;
1779 if (tid != PTHDB_INVALID_TID)
1780 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1781 fprintf_unfiltered (buf, _("tid %d"), (int)tid);
1783 status = pthdb_pthread_state (pd_session, pdtid, &state);
1784 if (status != PTHDB_SUCCESS)
1785 state = PST_NOTSUP;
1786 fprintf_unfiltered (buf, ", %s", state2str (state));
1788 status = pthdb_pthread_suspendstate (pd_session, pdtid,
1789 &suspendstate);
1790 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
1791 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1792 fprintf_unfiltered (buf, _(", suspended"));
1794 status = pthdb_pthread_detachstate (pd_session, pdtid,
1795 &detachstate);
1796 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
1797 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1798 fprintf_unfiltered (buf, _(", detached"));
1800 pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1801 if (status == PTHDB_SUCCESS && cancelpend)
1802 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1803 fprintf_unfiltered (buf, _(", cancel pending"));
1805 ui_file_write (buf, "", 1);
1807 xfree (ret); /* Free old buffer. */
1809 ret = ui_file_xstrdup (buf, NULL);
1810 ui_file_delete (buf);
1812 return ret;
1815 static ptid_t
1816 aix_thread_get_ada_task_ptid (long lwp, long thread)
1818 return ptid_build (ptid_get_pid (inferior_ptid), 0, thread);
1821 /* Initialize target aix_thread_ops. */
1823 static void
1824 init_aix_thread_ops (void)
1826 aix_thread_ops.to_shortname = "aix-threads";
1827 aix_thread_ops.to_longname = _("AIX pthread support");
1828 aix_thread_ops.to_doc = _("AIX pthread support");
1830 aix_thread_ops.to_attach = aix_thread_attach;
1831 aix_thread_ops.to_detach = aix_thread_detach;
1832 aix_thread_ops.to_resume = aix_thread_resume;
1833 aix_thread_ops.to_wait = aix_thread_wait;
1834 aix_thread_ops.to_fetch_registers = aix_thread_fetch_registers;
1835 aix_thread_ops.to_store_registers = aix_thread_store_registers;
1836 aix_thread_ops.to_xfer_partial = aix_thread_xfer_partial;
1837 /* No need for aix_thread_ops.to_create_inferior, because we activate thread
1838 debugging when the inferior reaches pd_brk_addr. */
1839 aix_thread_ops.to_mourn_inferior = aix_thread_mourn_inferior;
1840 aix_thread_ops.to_thread_alive = aix_thread_thread_alive;
1841 aix_thread_ops.to_pid_to_str = aix_thread_pid_to_str;
1842 aix_thread_ops.to_extra_thread_info = aix_thread_extra_thread_info;
1843 aix_thread_ops.to_get_ada_task_ptid = aix_thread_get_ada_task_ptid;
1844 aix_thread_ops.to_stratum = thread_stratum;
1845 aix_thread_ops.to_magic = OPS_MAGIC;
1848 /* Module startup initialization function, automagically called by
1849 init.c. */
1851 void _initialize_aix_thread (void);
1853 void
1854 _initialize_aix_thread (void)
1856 init_aix_thread_ops ();
1857 complete_target_initialization (&aix_thread_ops);
1859 /* Notice when object files get loaded and unloaded. */
1860 observer_attach_new_objfile (new_objfile);
1862 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
1863 _("Set debugging of AIX thread module."),
1864 _("Show debugging of AIX thread module."),
1865 _("Enables debugging output (used to debug GDB)."),
1866 NULL, NULL,
1867 /* FIXME: i18n: Debugging of AIX thread
1868 module is \"%d\". */
1869 &setdebuglist, &showdebuglist);