Automatic date update in version.in
[binutils-gdb.git] / gdb / aix-thread.c
blob3eb8f57422caf6c1a6053486eee28a086b8e080c
1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2023 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 "gdbthread.h"
44 #include "target.h"
45 #include "inferior.h"
46 #include "regcache.h"
47 #include "gdbcmd.h"
48 #include "ppc-tdep.h"
49 #include "observable.h"
50 #include "objfiles.h"
52 #include <procinfo.h>
53 #include <sys/types.h>
54 #include <sys/ptrace.h>
55 #include <sys/reg.h>
56 #include <sched.h>
57 #include <sys/pthdebug.h>
59 #if !HAVE_DECL_GETTHRDS
60 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
61 #endif
63 /* Whether to emit debugging output. */
64 static bool debug_aix_thread;
66 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
67 #ifndef PTHDB_VERSION_3
68 #define pthdb_tid_t tid_t
69 #endif
71 /* Success and failure values returned by pthdb callbacks. */
73 #define PDC_SUCCESS PTHDB_SUCCESS
74 #define PDC_FAILURE PTHDB_CALLBACK
76 /* Private data attached to each element in GDB's thread list. */
78 struct aix_thread_info : public private_thread_info
80 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
81 pthdb_tid_t tid; /* kernel thread id */
84 /* Return the aix_thread_info attached to THREAD. */
86 static aix_thread_info *
87 get_aix_thread_info (thread_info *thread)
89 return gdb::checked_static_cast<aix_thread_info *> (thread->priv.get ());
92 /* Information about a thread of which libpthdebug is aware. */
94 struct pd_thread {
95 pthdb_pthread_t pdtid;
96 pthread_t pthid;
97 pthdb_tid_t tid;
100 /* This module's target-specific operations, active while pd_able is true. */
102 static const target_info aix_thread_target_info = {
103 "aix-threads",
104 N_("AIX pthread support"),
105 N_("AIX pthread support")
108 class aix_thread_target final : public target_ops
110 public:
111 const target_info &info () const override
112 { return aix_thread_target_info; }
114 strata stratum () const override { return thread_stratum; }
116 void detach (inferior *, int) override;
117 void resume (ptid_t, int, enum gdb_signal) override;
118 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
120 void fetch_registers (struct regcache *, int) override;
121 void store_registers (struct regcache *, int) override;
123 enum target_xfer_status xfer_partial (enum target_object object,
124 const char *annex,
125 gdb_byte *readbuf,
126 const gdb_byte *writebuf,
127 ULONGEST offset, ULONGEST len,
128 ULONGEST *xfered_len) override;
130 void mourn_inferior () override;
132 bool thread_alive (ptid_t ptid) override;
134 std::string pid_to_str (ptid_t) override;
136 const char *extra_thread_info (struct thread_info *) override;
138 ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
140 void update_thread_list () override;
143 static aix_thread_target aix_thread_ops;
145 /* Forward declarations for pthdb callbacks. */
147 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
148 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
149 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
150 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
151 unsigned long long flags,
152 pthdb_context_t *context);
153 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
154 unsigned long long flags,
155 pthdb_context_t *context);
156 static int pdc_alloc (pthdb_user_t, size_t, void **);
157 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
158 static int pdc_dealloc (pthdb_user_t, void *);
160 /* pthdb callbacks. */
162 static pthdb_callbacks_t pd_callbacks = {
163 pdc_symbol_addrs,
164 pdc_read_data,
165 pdc_write_data,
166 pdc_read_regs,
167 pdc_write_regs,
168 pdc_alloc,
169 pdc_realloc,
170 pdc_dealloc,
171 NULL
174 /* Aix variable structure. */
175 struct aix_thread_variables
177 /* Whether the current application is debuggable by pthdb. */
178 int pd_able;
180 /* Whether a threaded application is being debugged. */
181 int pd_active;
183 /* Current pthdb session. */
184 pthdb_session_t pd_session;
186 /* Address of the function that libpthread will call when libpthdebug
187 is ready to be initialized. */
188 CORE_ADDR pd_brk_addr;
190 /* Whether the current architecture is 64-bit.
191 Only valid when pd_able is true. */
192 int arch64;
195 /* Key to our per-inferior data. */
196 static const registry<inferior>::key<aix_thread_variables>
197 aix_thread_variables_handle;
199 /* Function to Get aix_thread_variables data. */
200 static struct aix_thread_variables*
201 get_aix_thread_variables_data (struct inferior *inf)
203 if (inf == NULL)
204 return NULL;
206 struct aix_thread_variables* data;
208 data = aix_thread_variables_handle.get (inf);
209 if (data == NULL)
210 data = aix_thread_variables_handle.emplace (inf);
212 return data;
215 /* Helper to get data for ptid in a function. */
217 static struct aix_thread_variables*
218 get_thread_data_helper_for_ptid (ptid_t ptid)
220 inferior *inf = find_inferior_ptid (current_inferior ()->process_target (),
221 ptid);
222 return get_aix_thread_variables_data (inf);
225 /* Helper to get data for pid in a function. */
227 static struct aix_thread_variables*
228 get_thread_data_helper_for_pid (pid_t pid)
230 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
231 pid);
232 return get_aix_thread_variables_data (inf);
235 /* Return a printable representation of pthdebug function return
236 STATUS. */
238 static const char *
239 pd_status2str (int status)
241 switch (status)
243 case PTHDB_SUCCESS: return "SUCCESS";
244 case PTHDB_NOSYS: return "NOSYS";
245 case PTHDB_NOTSUP: return "NOTSUP";
246 case PTHDB_BAD_VERSION: return "BAD_VERSION";
247 case PTHDB_BAD_USER: return "BAD_USER";
248 case PTHDB_BAD_SESSION: return "BAD_SESSION";
249 case PTHDB_BAD_MODE: return "BAD_MODE";
250 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
251 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
252 case PTHDB_BAD_POINTER: return "BAD_POINTER";
253 case PTHDB_BAD_CMD: return "BAD_CMD";
254 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
255 case PTHDB_BAD_ATTR: return "BAD_ATTR";
256 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
257 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
258 case PTHDB_BAD_COND: return "BAD_COND";
259 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
260 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
261 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
262 case PTHDB_BAD_KEY: return "BAD_KEY";
263 case PTHDB_BAD_PTID: return "BAD_PTID";
264 case PTHDB_BAD_TID: return "BAD_TID";
265 case PTHDB_CALLBACK: return "CALLBACK";
266 case PTHDB_CONTEXT: return "CONTEXT";
267 case PTHDB_HELD: return "HELD";
268 case PTHDB_NOT_HELD: return "NOT_HELD";
269 case PTHDB_MEMORY: return "MEMORY";
270 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
271 case PTHDB_SYMBOL: return "SYMBOL";
272 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
273 case PTHDB_INTERNAL: return "INTERNAL";
274 default: return "UNKNOWN";
278 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
279 exceptional conditions and either return nonlocally or else return
280 1 for success and 0 for failure. */
282 static int
283 ptrace_check (int req, int id, int ret)
285 if (ret == 0 && !errno)
286 return 1;
288 /* According to ptrace(2), ptrace may fail with EPERM if "the
289 Identifier parameter corresponds to a kernel thread which is
290 stopped in kernel mode and whose computational state cannot be
291 read or written." This happens quite often with register reads. */
293 switch (req)
295 case PTT_READ_GPRS:
296 case PTT_READ_FPRS:
297 case PTT_READ_SPRS:
298 if (ret == -1 && errno == EPERM)
300 if (debug_aix_thread)
301 gdb_printf (gdb_stdlog,
302 "ptrace (%d, %d) = %d (errno = %d)\n",
303 req, id, ret, errno);
304 return ret == -1 ? 0 : 1;
306 break;
307 case PTT_READ_VEC:
308 case PTT_READ_VSX:
309 if (debug_aix_thread)
310 gdb_printf (gdb_stdlog,
311 "ptrace (%d, %d) = %d (errno = %d)\n",
312 req, id, ret, errno);
313 if (ret == -1)
314 return -1;
315 break;
317 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
318 req, id, ret, errno, safe_strerror (errno));
319 return 0; /* Not reached. */
322 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
323 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
324 Return success. */
326 #ifdef HAVE_PTRACE64
327 # define ptracex(request, pid, addr, data, buf) \
328 ptrace64 (request, pid, addr, data, buf)
329 #endif
331 static int
332 ptrace64aix (int req, int id, long long addr, int data, int *buf)
334 errno = 0;
335 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
338 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
339 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
340 Return success. */
342 #ifdef HAVE_PTRACE64
343 # define ptrace(request, pid, addr, data, buf) \
344 ptrace64 (request, pid, addr, data, buf)
345 # define addr_ptr long long
346 #else
347 # define addr_ptr int *
348 #endif
350 static int
351 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
353 errno = 0;
354 return ptrace_check (req, id,
355 ptrace (req, id, addr, data, buf));
358 /* If *PIDP is a composite process/thread id, convert it to a
359 process id. */
361 static void
362 pid_to_prc (ptid_t *ptidp)
364 ptid_t ptid;
366 ptid = *ptidp;
367 if (ptid.tid () != 0)
368 *ptidp = ptid_t (ptid.pid ());
371 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
372 the address of SYMBOLS[<i>].name. */
374 static int
375 pdc_symbol_addrs (pthdb_user_t user_current_pid, pthdb_symbol_t *symbols, int count)
377 struct bound_minimal_symbol ms;
378 int i;
379 char *name;
381 if (debug_aix_thread)
382 gdb_printf (gdb_stdlog,
383 "pdc_symbol_addrs (user_current_pid = %ld, symbols = 0x%lx, count = %d)\n",
384 user_current_pid, (long) symbols, count);
386 for (i = 0; i < count; i++)
388 name = symbols[i].name;
389 if (debug_aix_thread)
390 gdb_printf (gdb_stdlog,
391 " symbols[%d].name = \"%s\"\n", i, name);
393 if (!*name)
394 symbols[i].addr = 0;
395 else
397 ms = lookup_minimal_symbol (name, NULL, NULL);
398 if (ms.minsym == NULL)
400 if (debug_aix_thread)
401 gdb_printf (gdb_stdlog, " returning PDC_FAILURE\n");
402 return PDC_FAILURE;
404 symbols[i].addr = ms.value_address ();
406 if (debug_aix_thread)
407 gdb_printf (gdb_stdlog, " symbols[%d].addr = %s\n",
408 i, hex_string (symbols[i].addr));
410 if (debug_aix_thread)
411 gdb_printf (gdb_stdlog, " returning PDC_SUCCESS\n");
412 return PDC_SUCCESS;
415 /* Read registers call back function should be able to read the
416 context information of a debuggee kernel thread from an active
417 process or from a core file. The information should be formatted
418 in context64 form for both 32-bit and 64-bit process.
419 If successful return 0, else non-zero is returned. */
421 static int
422 pdc_read_regs (pthdb_user_t user_current_pid,
423 pthdb_tid_t tid,
424 unsigned long long flags,
425 pthdb_context_t *context)
427 /* This function doesn't appear to be used, so we could probably
428 just return 0 here. HOWEVER, if it is not defined, the OS will
429 complain and several thread debug functions will fail. In case
430 this is needed, I have implemented what I think it should do,
431 however this code is untested. */
433 uint64_t gprs64[ppc_num_gprs];
434 uint32_t gprs32[ppc_num_gprs];
435 double fprs[ppc_num_fprs];
436 struct ptxsprs sprs64;
437 struct ptsprs sprs32;
438 struct aix_thread_variables *data;
440 data = get_thread_data_helper_for_pid (user_current_pid);
442 if (debug_aix_thread)
443 gdb_printf (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
444 (int) tid, hex_string (flags));
446 /* General-purpose registers. */
447 if (flags & PTHDB_FLAG_GPRS)
449 if (data->arch64)
451 if (!ptrace64aix (PTT_READ_GPRS, tid,
452 (unsigned long) gprs64, 0, NULL))
453 memset (gprs64, 0, sizeof (gprs64));
454 memcpy (context->gpr, gprs64, sizeof(gprs64));
456 else
458 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
459 memset (gprs32, 0, sizeof (gprs32));
460 memcpy (context->gpr, gprs32, sizeof(gprs32));
464 /* Floating-point registers. */
465 if (flags & PTHDB_FLAG_FPRS)
467 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
468 memset (fprs, 0, sizeof (fprs));
469 memcpy (context->fpr, fprs, sizeof(fprs));
472 /* Special-purpose registers. */
473 if (flags & PTHDB_FLAG_SPRS)
475 if (data->arch64)
477 if (!ptrace64aix (PTT_READ_SPRS, tid,
478 (unsigned long) &sprs64, 0, NULL))
479 memset (&sprs64, 0, sizeof (sprs64));
480 memcpy (&context->msr, &sprs64, sizeof(sprs64));
482 else
484 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
485 memset (&sprs32, 0, sizeof (sprs32));
486 memcpy (&context->msr, &sprs32, sizeof(sprs32));
490 /* vector registers. */
491 __vmx_context_t vmx;
492 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
494 if (data->arch64)
496 if (!ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
497 memset (&vmx, 0, sizeof (vmx));
498 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
500 else
502 if (!ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
503 memset (&vmx, 0, sizeof (vmx));
504 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
508 /* vsx registers. */
509 __vsx_context_t vsx;
510 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
512 if (data->arch64)
514 if (!ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
515 memset (&vsx, 0, sizeof (vsx));
516 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
518 else
520 if (!ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
521 memset (&vsx, 0, sizeof (vsx));
522 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
525 return 0;
528 /* Write register function should be able to write requested context
529 information to specified debuggee's kernel thread id.
530 If successful return 0, else non-zero is returned. */
532 static int
533 pdc_write_regs (pthdb_user_t user_current_pid,
534 pthdb_tid_t tid,
535 unsigned long long flags,
536 pthdb_context_t *context)
538 /* This function doesn't appear to be used, so we could probably
539 just return 0 here. HOWEVER, if it is not defined, the OS will
540 complain and several thread debug functions will fail. In case
541 this is needed, I have implemented what I think it should do,
542 however this code is untested. */
544 struct aix_thread_variables *data;
546 data = get_thread_data_helper_for_pid (user_current_pid);
548 if (debug_aix_thread)
549 gdb_printf (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
550 (int) tid, hex_string (flags));
552 /* General-purpose registers. */
553 if (flags & PTHDB_FLAG_GPRS)
555 if (data->arch64)
556 ptrace64aix (PTT_WRITE_GPRS, tid,
557 (unsigned long) context->gpr, 0, NULL);
558 else
559 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
562 /* Floating-point registers. */
563 if (flags & PTHDB_FLAG_FPRS)
565 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
568 /* Special-purpose registers. */
569 if (flags & PTHDB_FLAG_SPRS)
571 if (data->arch64)
573 ptrace64aix (PTT_WRITE_SPRS, tid,
574 (unsigned long) &context->msr, 0, NULL);
576 else
578 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
582 /* vector registers. */
583 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
585 if (data->arch64)
586 ptrace64aix (PTT_WRITE_VEC, tid, (unsigned long) &context->vmx, 0, 0);
587 else
588 ptrace32 (PTT_WRITE_VEC, tid, (uintptr_t) &context->vmx, 0, 0);
591 /* vsx registers. */
592 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
594 if (data->arch64)
595 ptrace64aix (PTT_WRITE_VSX, tid, (unsigned long) &context->vsx, 0, 0);
596 else
597 ptrace32 (PTT_WRITE_VSX, tid, (uintptr_t) &context->vsx, 0, 0);
599 return 0;
602 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
604 static int
605 pdc_read_data (pthdb_user_t user_current_pid, void *buf,
606 pthdb_addr_t addr, size_t len)
608 int status, ret;
609 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
610 user_current_pid);
612 if (debug_aix_thread)
613 gdb_printf (gdb_stdlog,
614 "pdc_read_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
615 user_current_pid, (long) buf, hex_string (addr), len);
617 /* This is needed to eliminate the dependency of current thread
618 which is null so that thread reads the correct target memory. */
620 scoped_restore_current_inferior_for_memory save_inferior (inf);
621 status = target_read_memory (addr, (gdb_byte *) buf, len);
623 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
625 if (debug_aix_thread)
626 gdb_printf (gdb_stdlog, " status=%d, returning %s\n",
627 status, pd_status2str (ret));
628 return ret;
631 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
633 static int
634 pdc_write_data (pthdb_user_t user_current_pid, void *buf,
635 pthdb_addr_t addr, size_t len)
637 int status, ret;
638 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
639 user_current_pid);
641 if (debug_aix_thread)
642 gdb_printf (gdb_stdlog,
643 "pdc_write_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
644 user_current_pid, (long) buf, hex_string (addr), len);
647 scoped_restore_current_inferior_for_memory save_inferior (inf);
648 status = target_write_memory (addr, (gdb_byte *) buf, len);
651 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
653 if (debug_aix_thread)
654 gdb_printf (gdb_stdlog, " status=%d, returning %s\n", status,
655 pd_status2str (ret));
656 return ret;
659 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
660 in BUFP. */
662 static int
663 pdc_alloc (pthdb_user_t user_current_pid, size_t len, void **bufp)
665 if (debug_aix_thread)
666 gdb_printf (gdb_stdlog,
667 "pdc_alloc (user_current_pid = %ld, len = %ld, bufp = 0x%lx)\n",
668 user_current_pid, len, (long) bufp);
669 *bufp = xmalloc (len);
670 if (debug_aix_thread)
671 gdb_printf (gdb_stdlog,
672 " malloc returned 0x%lx\n", (long) *bufp);
674 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
675 be returned. */
677 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
680 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
681 realloc callback, so that it contains LEN bytes, and store a
682 pointer to the result in BUFP. */
684 static int
685 pdc_realloc (pthdb_user_t user_current_pid, void *buf, size_t len, void **bufp)
687 if (debug_aix_thread)
688 gdb_printf (gdb_stdlog,
689 "pdc_realloc (user_current_pid = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
690 user_current_pid, (long) buf, len, (long) bufp);
691 *bufp = xrealloc (buf, len);
692 if (debug_aix_thread)
693 gdb_printf (gdb_stdlog,
694 " realloc returned 0x%lx\n", (long) *bufp);
695 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
698 /* pthdb callback: free BUF, which was allocated by the alloc or
699 realloc callback. */
701 static int
702 pdc_dealloc (pthdb_user_t user_current_pid, void *buf)
704 if (debug_aix_thread)
705 gdb_printf (gdb_stdlog,
706 "pdc_free (user_current_pid = %ld, buf = 0x%lx)\n", user_current_pid,
707 (long) buf);
708 xfree (buf);
709 return PDC_SUCCESS;
712 /* Return a printable representation of pthread STATE. */
714 static char *
715 state2str (pthdb_state_t state)
717 switch (state)
719 case PST_IDLE:
720 /* i18n: Like "Thread-Id %d, [state] idle" */
721 return _("idle"); /* being created */
722 case PST_RUN:
723 /* i18n: Like "Thread-Id %d, [state] running" */
724 return _("running"); /* running */
725 case PST_SLEEP:
726 /* i18n: Like "Thread-Id %d, [state] sleeping" */
727 return _("sleeping"); /* awaiting an event */
728 case PST_READY:
729 /* i18n: Like "Thread-Id %d, [state] ready" */
730 return _("ready"); /* runnable */
731 case PST_TERM:
732 /* i18n: Like "Thread-Id %d, [state] finished" */
733 return _("finished"); /* awaiting a join/detach */
734 default:
735 /* i18n: Like "Thread-Id %d, [state] unknown" */
736 return _("unknown");
740 /* qsort() comparison function for sorting pd_thread structs by pthid. */
742 static int
743 pcmp (const void *p1v, const void *p2v)
745 struct pd_thread *p1 = (struct pd_thread *) p1v;
746 struct pd_thread *p2 = (struct pd_thread *) p2v;
747 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
750 /* ptid comparison function */
752 static int
753 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
755 if (ptid1.pid () < ptid2.pid ())
756 return -1;
757 else if (ptid1.pid () > ptid2.pid ())
758 return 1;
759 else if (ptid1.tid () < ptid2.tid ())
760 return -1;
761 else if (ptid1.tid () > ptid2.tid ())
762 return 1;
763 else if (ptid1.lwp () < ptid2.lwp ())
764 return -1;
765 else if (ptid1.lwp () > ptid2.lwp ())
766 return 1;
767 else
768 return 0;
771 /* qsort() comparison function for sorting thread_info structs by pid. */
773 static int
774 gcmp (const void *t1v, const void *t2v)
776 struct thread_info *t1 = *(struct thread_info **) t1v;
777 struct thread_info *t2 = *(struct thread_info **) t2v;
778 return ptid_cmp (t1->ptid, t2->ptid);
781 /* Search through the list of all kernel threads for the thread
782 that has stopped on a SIGTRAP signal, and return its TID.
783 Return 0 if none found. */
785 static pthdb_tid_t
786 get_signaled_thread (int pid)
788 struct thrdsinfo64 thrinf;
789 tid_t ktid = 0;
791 while (1)
793 if (getthrds (pid, &thrinf,
794 sizeof (thrinf), &ktid, 1) != 1)
795 break;
797 /* We also need to keep in mind Trap and interrupt or any
798 signal that needs to be handled in pd_update (). */
800 if (thrinf.ti_cursig)
801 return thrinf.ti_tid;
804 /* Didn't find any thread stopped on a SIGTRAP signal. */
805 return 0;
808 /* Synchronize GDB's thread list with libpthdebug's.
810 There are some benefits of doing this every time the inferior stops:
812 - allows users to run thread-specific commands without needing to
813 run "info threads" first
815 - helps pthdb_tid_pthread() work properly (see "libpthdebug
816 peculiarities" at the top of this module)
818 - simplifies the demands placed on libpthdebug, which seems to
819 have difficulty with certain call patterns */
821 static void
822 sync_threadlists (pid_t pid)
824 int cmd, status;
825 int pcount, psize, pi, gcount, gi;
826 struct pd_thread *pbuf;
827 struct thread_info **gbuf, **g, *thread;
828 pthdb_pthread_t pdtid;
829 pthread_t pthid;
830 pthdb_tid_t tid;
831 process_stratum_target *proc_target = current_inferior ()->process_target ();
832 struct aix_thread_variables *data;
833 data = get_thread_data_helper_for_pid (pid);
835 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
837 pcount = 0;
838 psize = 1;
839 pbuf = XNEWVEC (struct pd_thread, psize);
841 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
843 status = pthdb_pthread (data->pd_session, &pdtid, cmd);
844 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
845 break;
847 status = pthdb_pthread_ptid (data->pd_session, pdtid, &pthid);
848 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
849 continue;
851 if (pcount == psize)
853 psize *= 2;
854 pbuf = (struct pd_thread *) xrealloc (pbuf,
855 psize * sizeof *pbuf);
857 pbuf[pcount].pdtid = pdtid;
858 pbuf[pcount].pthid = pthid;
859 pcount++;
862 for (pi = 0; pi < pcount; pi++)
864 status = pthdb_pthread_tid (data->pd_session, pbuf[pi].pdtid, &tid);
865 if (status != PTHDB_SUCCESS)
866 tid = PTHDB_INVALID_TID;
867 pbuf[pi].tid = tid;
870 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
872 /* Accumulate an array of GDB threads sorted by pid. */
874 /* gcount is GDB thread count and pcount is pthreadlib thread count. */
876 gcount = 0;
877 for (thread_info *tp : all_threads (proc_target, ptid_t (pid)))
878 gcount++;
879 g = gbuf = XNEWVEC (struct thread_info *, gcount);
880 for (thread_info *tp : all_threads (proc_target, ptid_t (pid)))
881 *g++ = tp;
882 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
884 /* Apply differences between the two arrays to GDB's thread list. */
886 for (pi = gi = 0; pi < pcount || gi < gcount;)
888 if (pi == pcount)
890 delete_thread (gbuf[gi]);
891 gi++;
893 else if (gi == gcount)
895 aix_thread_info *priv = new aix_thread_info;
896 priv->pdtid = pbuf[pi].pdtid;
897 priv->tid = pbuf[pi].tid;
899 thread = add_thread_with_info (proc_target,
900 ptid_t (pid, 0, pbuf[pi].pthid),
901 private_thread_info_up (priv));
903 pi++;
905 else
907 ptid_t pptid, gptid;
908 int cmp_result;
910 pptid = ptid_t (pid, 0, pbuf[pi].pthid);
911 gptid = gbuf[gi]->ptid;
912 pdtid = pbuf[pi].pdtid;
913 tid = pbuf[pi].tid;
915 cmp_result = ptid_cmp (pptid, gptid);
917 if (cmp_result == 0)
919 aix_thread_info *priv = get_aix_thread_info (gbuf[gi]);
921 priv->pdtid = pdtid;
922 priv->tid = tid;
923 pi++;
924 gi++;
926 else if (cmp_result > 0)
928 /* This is to make the main process thread now look
929 like a thread. */
931 if (gptid.is_pid ())
933 thread_info *tp = proc_target->find_thread (gptid);
934 thread_change_ptid (proc_target, gptid, pptid);
935 aix_thread_info *priv = new aix_thread_info;
936 priv->pdtid = pbuf[pi].pdtid;
937 priv->tid = pbuf[pi].tid;
938 tp->priv.reset (priv);
939 gi++;
940 pi++;
942 else
944 delete_thread (gbuf[gi]);
945 gi++;
948 else
950 thread = add_thread (proc_target, pptid);
952 aix_thread_info *priv = new aix_thread_info;
953 thread->priv.reset (priv);
954 priv->pdtid = pdtid;
955 priv->tid = tid;
956 pi++;
961 xfree (pbuf);
962 xfree (gbuf);
965 /* Iterate_over_threads() callback for locating a thread, using
966 the TID of its associated kernel thread. */
968 static int
969 iter_tid (struct thread_info *thread, void *tidp)
971 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
972 aix_thread_info *priv = get_aix_thread_info (thread);
974 return priv->tid == tid;
977 /* Synchronize libpthdebug's state with the inferior and with GDB,
978 generate a composite process/thread <pid> for the current thread,
979 Return the ptid of the event thread if one can be found, else
980 return a pid-only ptid with PID. */
982 static ptid_t
983 pd_update (pid_t pid)
985 int status;
986 ptid_t ptid;
987 pthdb_tid_t tid;
988 struct thread_info *thread = NULL;
989 struct aix_thread_variables *data;
991 data = get_thread_data_helper_for_pid (pid);
993 if (!data->pd_active)
994 return ptid_t (pid);
996 status = pthdb_session_update (data->pd_session);
997 if (status != PTHDB_SUCCESS)
998 return ptid_t (pid);
1000 sync_threadlists (pid);
1002 /* Define "current thread" as one that just received a trap signal. */
1004 tid = get_signaled_thread (pid);
1005 if (tid != 0)
1006 thread = iterate_over_threads (iter_tid, &tid);
1007 if (!thread)
1008 ptid = ptid_t (pid);
1009 else
1010 ptid = thread->ptid;
1012 return ptid;
1015 /* Try to start debugging threads in the current process.
1016 If successful and there exists and we can find an event thread, return a ptid
1017 for that thread. Otherwise, return a ptid-only ptid using PID. */
1019 static void
1020 pd_activate (pid_t pid)
1022 int status;
1023 struct aix_thread_variables *data;
1024 data = get_thread_data_helper_for_pid (pid);
1026 status = pthdb_session_init (pid, data->arch64 ? PEM_64BIT : PEM_32BIT,
1027 PTHDB_FLAG_REGS, &pd_callbacks,
1028 &data->pd_session);
1029 if (status == PTHDB_SUCCESS)
1030 data->pd_active = 1;
1033 /* AIX implementation of update_thread_list. */
1035 void
1036 aix_thread_target::update_thread_list ()
1038 for (inferior *inf : all_inferiors ())
1040 if (inf->pid == 0)
1041 continue;
1043 pd_update (inf->pid);
1048 /* An object file has just been loaded. Check whether the current
1049 application is pthreaded, and if so, prepare for thread debugging. */
1051 static void
1052 pd_enable (inferior *inf)
1054 int status;
1055 char *stub_name;
1056 struct bound_minimal_symbol ms;
1057 struct aix_thread_variables *data;
1059 if (inf == NULL)
1060 return;
1062 data = get_aix_thread_variables_data (inf);
1064 /* Don't initialize twice. */
1065 if (data->pd_able)
1066 return;
1068 /* Check application word size. */
1069 data->arch64 = register_size (current_inferior ()->arch (), 0) == 8;
1071 /* Check whether the application is pthreaded. */
1072 stub_name = NULL;
1073 status = pthdb_session_pthreaded (inf->pid, PTHDB_FLAG_REGS,
1074 &pd_callbacks, &stub_name);
1075 if ((status != PTHDB_SUCCESS
1076 && status != PTHDB_NOT_PTHREADED) || !stub_name)
1077 return;
1079 /* Set a breakpoint on the returned stub function. */
1080 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
1081 if (ms.minsym == NULL)
1082 return;
1083 data->pd_brk_addr = ms.value_address ();
1084 if (!create_thread_event_breakpoint (current_inferior ()->arch (),
1085 data->pd_brk_addr))
1086 return;
1088 /* Prepare for thread debugging. */
1089 current_inferior ()->push_target (&aix_thread_ops);
1090 data->pd_able = 1;
1092 /* If we're debugging a core file or an attached inferior, the
1093 pthread library may already have been initialized, so try to
1094 activate thread debugging. */
1095 pd_activate (inf->pid);
1098 /* Undo the effects of pd_enable(). */
1100 static void
1101 pd_disable (inferior *inf)
1103 struct aix_thread_variables *data;
1104 data = get_aix_thread_variables_data (inf);
1106 if (!data->pd_able)
1107 return;
1108 if (!data->pd_active)
1109 return;
1110 pthdb_session_destroy (data->pd_session);
1112 pid_to_prc (&inferior_ptid);
1113 data->pd_active = 0;
1114 data->pd_able = 0;
1115 current_inferior ()->unpush_target (&aix_thread_ops);
1118 /* new_objfile observer callback.
1120 Check whether a threaded application is being debugged, and if so, prepare
1121 for thread debugging. */
1123 static void
1124 new_objfile (struct objfile *objfile)
1126 pd_enable (current_inferior ());
1129 /* Attach to process specified by ARGS. */
1131 static void
1132 aix_thread_inferior_created (inferior *inf)
1134 pd_enable (inf);
1137 /* Detach from the process attached to by aix_thread_attach(). */
1139 void
1140 aix_thread_target::detach (inferior *inf, int from_tty)
1142 target_ops *beneath = this->beneath ();
1144 pd_disable (inf);
1145 beneath->detach (inf, from_tty);
1148 /* Tell the inferior process to continue running thread PID if != -1
1149 and all threads otherwise. */
1151 void
1152 aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1154 struct thread_info *thread;
1155 pthdb_tid_t tid[2];
1156 struct aix_thread_variables *data;
1158 data = get_thread_data_helper_for_ptid (ptid);
1160 if (ptid.tid () == 0)
1162 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1164 inferior_ptid = ptid_t (inferior_ptid.pid ());
1165 beneath ()->resume (ptid, step, sig);
1167 else
1169 thread = current_inferior ()->find_thread (ptid);
1170 if (!thread)
1171 error (_("aix-thread resume: unknown pthread %ld"),
1172 ptid.lwp ());
1174 aix_thread_info *priv = get_aix_thread_info (thread);
1176 tid[0] = priv->tid;
1177 if (tid[0] == PTHDB_INVALID_TID)
1178 error (_("aix-thread resume: no tid for pthread %ld"),
1179 ptid.lwp ());
1180 tid[1] = 0;
1182 if (data->arch64)
1183 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1184 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1185 else
1186 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1187 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1191 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1192 If an error occurs, return -1, else return the pid of the stopped
1193 thread. */
1195 ptid_t
1196 aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1197 target_wait_flags options)
1199 struct aix_thread_variables *data;
1201 pid_to_prc (&ptid);
1203 ptid = beneath ()->wait (ptid, status, options);
1206 if (ptid.pid () == -1)
1207 return ptid_t (-1);
1209 /* The target beneath does not deal with threads, so it should only return
1210 pid-only ptids. */
1211 gdb_assert (ptid.is_pid ());
1213 data = get_thread_data_helper_for_ptid (ptid);
1215 /* Check whether libpthdebug might be ready to be initialized. */
1216 if (!data->pd_active && status->kind () == TARGET_WAITKIND_STOPPED
1217 && status->sig () == GDB_SIGNAL_TRAP)
1219 process_stratum_target *proc_target
1220 = current_inferior ()->process_target ();
1221 struct regcache *regcache = get_thread_regcache (proc_target, ptid);
1222 struct gdbarch *gdbarch = regcache->arch ();
1224 if (regcache_read_pc (regcache)
1225 - gdbarch_decr_pc_after_break (gdbarch) == data->pd_brk_addr)
1226 pd_activate (ptid.pid ());
1229 return pd_update (ptid.pid ());
1232 /* Supply AIX altivec registers, both 64 and 32 bit. */
1234 static void
1235 supply_altivec_regs (struct regcache *regcache, __vmx_context_t vmx)
1237 ppc_gdbarch_tdep *tdep
1238 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1239 int regno;
1240 for (regno = 0; regno < ppc_num_vrs; regno++)
1241 regcache->raw_supply (tdep->ppc_vr0_regnum + regno,
1242 &(vmx.__vr[regno]));
1243 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1244 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1247 /* Supply AIX VSX registers, both 64 and 32 bit. */
1249 static void
1250 supply_vsx_regs (struct regcache *regcache, __vsx_context_t vsx)
1252 ppc_gdbarch_tdep *tdep
1253 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1254 int regno;
1256 for (regno = 0; regno < ppc_num_vshrs; regno++)
1257 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + regno,
1258 &(vsx.__vsr_dw1[regno]));
1261 /* Record that the 64-bit general-purpose registers contain VALS. */
1263 static void
1264 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1266 ppc_gdbarch_tdep *tdep
1267 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1268 int regno;
1270 for (regno = 0; regno < ppc_num_gprs; regno++)
1271 regcache->raw_supply (tdep->ppc_gp0_regnum + regno,
1272 (char *) (vals + regno));
1275 /* Record that 32-bit register REGNO contains VAL. */
1277 static void
1278 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1280 regcache->raw_supply (regno, (char *) &val);
1283 /* Record that the floating-point registers contain VALS. */
1285 static void
1286 supply_fprs (struct regcache *regcache, double *vals)
1288 struct gdbarch *gdbarch = regcache->arch ();
1289 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1290 int regno;
1292 /* This function should never be called on architectures without
1293 floating-point registers. */
1294 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1296 for (regno = tdep->ppc_fp0_regnum;
1297 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1298 regno++)
1299 regcache->raw_supply (regno,
1300 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1303 /* Predicate to test whether given register number is a "special" register. */
1304 static int
1305 special_register_p (struct gdbarch *gdbarch, int regno)
1307 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1309 return regno == gdbarch_pc_regnum (gdbarch)
1310 || regno == tdep->ppc_ps_regnum
1311 || regno == tdep->ppc_cr_regnum
1312 || regno == tdep->ppc_lr_regnum
1313 || regno == tdep->ppc_ctr_regnum
1314 || regno == tdep->ppc_xer_regnum
1315 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1316 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1320 /* Record that the special registers contain the specified 64-bit and
1321 32-bit values. */
1323 static void
1324 supply_sprs64 (struct regcache *regcache,
1325 uint64_t iar, uint64_t msr, uint32_t cr,
1326 uint64_t lr, uint64_t ctr, uint32_t xer,
1327 uint32_t fpscr)
1329 struct gdbarch *gdbarch = regcache->arch ();
1330 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1332 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1333 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1334 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1335 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1336 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1337 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1338 if (tdep->ppc_fpscr_regnum >= 0)
1339 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1342 /* Record that the special registers contain the specified 32-bit
1343 values. */
1345 static void
1346 supply_sprs32 (struct regcache *regcache,
1347 uint32_t iar, uint32_t msr, uint32_t cr,
1348 uint32_t lr, uint32_t ctr, uint32_t xer,
1349 uint32_t fpscr)
1351 struct gdbarch *gdbarch = regcache->arch ();
1352 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1354 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1355 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1356 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1357 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1358 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1359 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1360 if (tdep->ppc_fpscr_regnum >= 0)
1361 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1364 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1365 thread.
1367 There's no way to query a single register from a non-kernel
1368 pthread, so there's no need for a single-register version of this
1369 function. */
1371 static void
1372 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1374 struct gdbarch *gdbarch = regcache->arch ();
1375 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1376 int status, i;
1377 pthdb_context_t ctx;
1378 struct aix_thread_variables *data;
1379 data = get_thread_data_helper_for_ptid (inferior_ptid);
1381 if (debug_aix_thread)
1382 gdb_printf (gdb_stdlog,
1383 "fetch_regs_user_thread %lx\n", (long) pdtid);
1384 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1385 if (status != PTHDB_SUCCESS)
1386 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1387 pd_status2str (status));
1389 /* General-purpose registers. */
1391 if (data->arch64)
1392 supply_gprs64 (regcache, ctx.gpr);
1393 else
1394 for (i = 0; i < ppc_num_gprs; i++)
1395 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1397 /* Floating-point registers. */
1399 if (ppc_floating_point_unit_p (gdbarch))
1400 supply_fprs (regcache, ctx.fpr);
1402 /* Special registers. */
1404 if (data->arch64)
1405 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1406 ctx.xer, ctx.fpscr);
1407 else
1408 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1409 ctx.xer, ctx.fpscr);
1411 /* Altivec registers. */
1412 supply_altivec_regs (regcache, ctx.vmx);
1414 /* VSX registers. */
1415 supply_vsx_regs (regcache, ctx.vsx);
1418 /* Fetch register REGNO if != -1 or all registers otherwise from
1419 kernel thread TID.
1421 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1422 SPRs, but there's no way to query individual registers within those
1423 groups. Therefore, if REGNO != -1, this function fetches an entire
1424 group.
1426 Unfortunately, kernel thread register queries often fail with
1427 EPERM, indicating that the thread is in kernel space. This breaks
1428 backtraces of threads other than the current one. To make that
1429 breakage obvious without throwing an error to top level (which is
1430 bad e.g. during "info threads" output), zero registers that can't
1431 be retrieved. */
1433 static void
1434 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1435 pthdb_tid_t tid)
1437 struct gdbarch *gdbarch = regcache->arch ();
1438 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1439 uint64_t gprs64[ppc_num_gprs];
1440 uint32_t gprs32[ppc_num_gprs];
1441 double fprs[ppc_num_fprs];
1442 struct ptxsprs sprs64;
1443 struct ptsprs sprs32;
1444 int i;
1445 struct aix_thread_variables *data;
1447 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1449 if (debug_aix_thread)
1450 gdb_printf (gdb_stdlog,
1451 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1452 (long) tid, regno, data->arch64);
1454 /* General-purpose registers. */
1455 if (regno == -1
1456 || (tdep->ppc_gp0_regnum <= regno
1457 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1459 if (data->arch64)
1461 if (!ptrace64aix (PTT_READ_GPRS, tid,
1462 (unsigned long) gprs64, 0, NULL))
1463 memset (gprs64, 0, sizeof (gprs64));
1464 supply_gprs64 (regcache, gprs64);
1466 else
1468 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1469 memset (gprs32, 0, sizeof (gprs32));
1470 for (i = 0; i < ppc_num_gprs; i++)
1471 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1475 /* vector registers. */
1476 if (tdep->ppc_vr0_regnum != -1)
1478 int ret = 0;
1479 __vmx_context_t vmx;
1480 if (data->arch64)
1481 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1482 else
1483 ret = ptrace32 (PTT_READ_VEC, tid, (uintptr_t) &vmx, 0, 0);
1484 if (ret < 0)
1485 memset(&vmx, 0, sizeof(__vmx_context_t));
1486 for (i = 0; i < ppc_num_vrs; i++)
1487 regcache->raw_supply (tdep->ppc_vr0_regnum + i, &(vmx.__vr[i]));
1488 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1489 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1492 /* vsx registers. */
1493 if (tdep->ppc_vsr0_upper_regnum != -1)
1495 __vsx_context_t vsx;
1496 int ret = 0;
1497 if (data->arch64)
1498 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1499 else
1500 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1501 if (ret < 0)
1502 memset(&vsx, 0, sizeof(__vsx_context_t));
1503 for (i = 0; i < ppc_num_vshrs; i++)
1504 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i, &(vsx.__vsr_dw1[i]));
1507 /* Floating-point registers. */
1509 if (ppc_floating_point_unit_p (gdbarch)
1510 && (regno == -1
1511 || (regno >= tdep->ppc_fp0_regnum
1512 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1514 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1515 memset (fprs, 0, sizeof (fprs));
1516 supply_fprs (regcache, fprs);
1519 /* Special-purpose registers. */
1521 if (regno == -1 || special_register_p (gdbarch, regno))
1523 if (data->arch64)
1525 if (!ptrace64aix (PTT_READ_SPRS, tid,
1526 (unsigned long) &sprs64, 0, NULL))
1527 memset (&sprs64, 0, sizeof (sprs64));
1528 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1529 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1530 sprs64.pt_xer, sprs64.pt_fpscr);
1532 else
1534 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1535 memset (&sprs32, 0, sizeof (sprs32));
1536 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1537 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1538 sprs32.pt_fpscr);
1540 if (tdep->ppc_mq_regnum >= 0)
1541 regcache->raw_supply (tdep->ppc_mq_regnum, (char *) &sprs32.pt_mq);
1546 /* Fetch register REGNO if != -1 or all registers otherwise from the
1547 thread/process connected to REGCACHE. */
1549 void
1550 aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
1552 struct thread_info *thread;
1553 pthdb_tid_t tid;
1555 /* If a new inferior is born, then its pthread debug library is yet to
1556 initialised and hence has no private data. So the below if condition
1557 exists. */
1559 if (regcache->ptid ().tid () == 0)
1560 beneath ()->fetch_registers (regcache, regno);
1561 else
1563 thread = current_inferior ()->find_thread (regcache->ptid ());
1564 aix_thread_info *priv = get_aix_thread_info (thread);
1565 tid = priv->tid;
1567 if (tid == PTHDB_INVALID_TID)
1568 fetch_regs_user_thread (regcache, priv->pdtid);
1569 else
1570 fetch_regs_kernel_thread (regcache, regno, tid);
1574 /* Fill altivec registers. */
1576 static void
1577 fill_altivec (const struct regcache *regcache, __vmx_context_t *vmx)
1579 struct gdbarch *gdbarch = regcache->arch ();
1580 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1581 int regno;
1583 for (regno = 0; regno < ppc_num_vrs; regno++)
1584 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + regno))
1585 regcache->raw_collect (tdep->ppc_vr0_regnum + regno,
1586 &(vmx->__vr[regno]));
1588 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1589 regcache->raw_collect (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
1590 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1591 regcache->raw_collect (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
1594 /* Fill vsx registers. */
1596 static void
1597 fill_vsx (const struct regcache *regcache, __vsx_context_t *vsx)
1599 struct gdbarch *gdbarch = regcache->arch ();
1600 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1601 int regno;
1603 for (regno = 0; regno < ppc_num_vshrs; regno++)
1604 if (REG_VALID == regcache->get_register_status ( tdep->ppc_vsr0_upper_regnum + regno))
1605 regcache->raw_collect (tdep->ppc_vsr0_upper_regnum + regno,
1606 &(vsx->__vsr_dw1[0]) + regno);
1609 /* Store the gp registers into an array of uint32_t or uint64_t. */
1611 static void
1612 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1614 ppc_gdbarch_tdep *tdep
1615 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1616 int regno;
1618 for (regno = 0; regno < ppc_num_gprs; regno++)
1619 if (REG_VALID == regcache->get_register_status
1620 (tdep->ppc_gp0_regnum + regno))
1621 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1624 static void
1625 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1627 ppc_gdbarch_tdep *tdep
1628 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1629 int regno;
1631 for (regno = 0; regno < ppc_num_gprs; regno++)
1632 if (REG_VALID == regcache->get_register_status
1633 (tdep->ppc_gp0_regnum + regno))
1634 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1637 /* Store the floating point registers into a double array. */
1638 static void
1639 fill_fprs (const struct regcache *regcache, double *vals)
1641 struct gdbarch *gdbarch = regcache->arch ();
1642 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1643 int regno;
1645 /* This function should never be called on architectures without
1646 floating-point registers. */
1647 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1649 for (regno = tdep->ppc_fp0_regnum;
1650 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1651 regno++)
1652 if (REG_VALID == regcache->get_register_status (regno))
1653 regcache->raw_collect (regno, vals + regno - tdep->ppc_fp0_regnum);
1656 /* Store the special registers into the specified 64-bit and 32-bit
1657 locations. */
1659 static void
1660 fill_sprs64 (const struct regcache *regcache,
1661 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1662 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1663 uint32_t *fpscr)
1665 struct gdbarch *gdbarch = regcache->arch ();
1666 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1668 /* Verify that the size of the size of the IAR buffer is the
1669 same as the raw size of the PC (in the register cache). If
1670 they're not, then either GDB has been built incorrectly, or
1671 there's some other kind of internal error. To be really safe,
1672 we should check all of the sizes. */
1673 gdb_assert (sizeof (*iar) == register_size
1674 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1676 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1677 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1678 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1679 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1680 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1681 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1682 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1683 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1684 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1685 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1686 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1687 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1688 if (tdep->ppc_fpscr_regnum >= 0
1689 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1690 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1693 static void
1694 fill_sprs32 (const struct regcache *regcache,
1695 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1696 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1697 uint32_t *fpscr)
1699 struct gdbarch *gdbarch = regcache->arch ();
1700 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1702 /* Verify that the size of the size of the IAR buffer is the
1703 same as the raw size of the PC (in the register cache). If
1704 they're not, then either GDB has been built incorrectly, or
1705 there's some other kind of internal error. To be really safe,
1706 we should check all of the sizes. */
1707 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1708 gdbarch_pc_regnum (gdbarch)));
1710 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1711 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1712 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1713 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1714 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1715 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1716 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1717 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1718 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1719 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1720 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1721 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1722 if (tdep->ppc_fpscr_regnum >= 0
1723 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1724 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1727 /* Store all registers into pthread PDTID, which doesn't have a kernel
1728 thread.
1730 It's possible to store a single register into a non-kernel pthread,
1731 but I doubt it's worth the effort. */
1733 static void
1734 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1736 struct gdbarch *gdbarch = regcache->arch ();
1737 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1738 int status, i;
1739 pthdb_context_t ctx;
1740 uint32_t int32;
1741 uint64_t int64;
1742 struct aix_thread_variables *data;
1743 data = get_thread_data_helper_for_ptid (inferior_ptid);
1744 __vmx_context_t vmx;
1745 __vsx_context_t vsx;
1747 if (debug_aix_thread)
1748 gdb_printf (gdb_stdlog,
1749 "store_regs_user_thread %lx\n", (long) pdtid);
1751 /* Retrieve the thread's current context for its non-register
1752 values. */
1753 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1754 if (status != PTHDB_SUCCESS)
1755 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1756 pd_status2str (status));
1758 /* Fill altivec-registers. */
1760 if (__power_vmx())
1762 memset(&vmx, 0, sizeof(__vmx_context_t));
1763 for (i = 0; i < ppc_num_vrs; i++)
1764 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + i))
1766 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1767 &(vmx.__vr[i]));
1768 ctx.vmx.__vr[i] = vmx.__vr[i];
1770 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1771 ctx.vmx.__vrsave = vmx.__vrsave;
1772 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1773 ctx.vmx.__vscr = vmx.__vscr;
1776 /* Fill vsx registers. */
1778 if (__power_vsx())
1780 memset(&vsx, 0, sizeof(__vsx_context_t));
1781 for (i = 0; i < ppc_num_vshrs; i++)
1782 if (REG_VALID == regcache->get_register_status (tdep->ppc_vsr0_regnum + i))
1784 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1785 &(vsx.__vsr_dw1[i]));
1786 ctx.vsx.__vsr_dw1[i] = vsx.__vsr_dw1[i];
1790 /* Collect general-purpose register values from the regcache. */
1792 for (i = 0; i < ppc_num_gprs; i++)
1793 if (REG_VALID == regcache->get_register_status (tdep->ppc_gp0_regnum + i))
1795 if (data->arch64)
1797 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int64);
1798 ctx.gpr[i] = int64;
1800 else
1802 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int32);
1803 ctx.gpr[i] = int32;
1807 /* Collect floating-point register values from the regcache. */
1808 if (ppc_floating_point_unit_p (gdbarch))
1809 fill_fprs (regcache, ctx.fpr);
1811 /* Special registers (always kept in ctx as 64 bits). */
1812 if (data->arch64)
1814 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1815 &ctx.xer, &ctx.fpscr);
1817 else
1819 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1820 Solution: use 32-bit temp variables. */
1821 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1822 tmp_fpscr;
1824 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1825 &tmp_xer, &tmp_fpscr);
1826 if (REG_VALID == regcache->get_register_status
1827 (gdbarch_pc_regnum (gdbarch)))
1828 ctx.iar = tmp_iar;
1829 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1830 ctx.msr = tmp_msr;
1831 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1832 ctx.cr = tmp_cr;
1833 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1834 ctx.lr = tmp_lr;
1835 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1836 ctx.ctr = tmp_ctr;
1837 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1838 ctx.xer = tmp_xer;
1839 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1840 ctx.fpscr = tmp_fpscr;
1843 status = pthdb_pthread_setcontext (data->pd_session, pdtid, &ctx);
1844 if (status != PTHDB_SUCCESS)
1845 error (_("aix-thread: store_registers: "
1846 "pthdb_pthread_setcontext returned %s"),
1847 pd_status2str (status));
1850 /* Store register REGNO if != -1 or all registers otherwise into
1851 kernel thread TID.
1853 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1854 SPRs, but there's no way to set individual registers within those
1855 groups. Therefore, if REGNO != -1, this function stores an entire
1856 group. */
1858 static void
1859 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1860 pthdb_tid_t tid)
1862 struct gdbarch *gdbarch = regcache->arch ();
1863 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1864 uint64_t gprs64[ppc_num_gprs];
1865 uint32_t gprs32[ppc_num_gprs];
1866 double fprs[ppc_num_fprs];
1867 struct ptxsprs sprs64;
1868 struct ptsprs sprs32;
1869 struct aix_thread_variables *data;
1870 int ret = 0;
1872 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1874 if (debug_aix_thread)
1875 gdb_printf (gdb_stdlog,
1876 "store_regs_kernel_thread tid=%lx regno=%d\n",
1877 (long) tid, regno);
1879 /* General-purpose registers. */
1880 if (regno == -1
1881 || (tdep->ppc_gp0_regnum <= regno
1882 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1884 if (data->arch64)
1886 /* Pre-fetch: some regs may not be in the cache. */
1887 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1888 fill_gprs64 (regcache, gprs64);
1889 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1891 else
1893 /* Pre-fetch: some regs may not be in the cache. */
1894 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1895 fill_gprs32 (regcache, gprs32);
1896 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1900 /* Floating-point registers. */
1902 if (ppc_floating_point_unit_p (gdbarch)
1903 && (regno == -1
1904 || (regno >= tdep->ppc_fp0_regnum
1905 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1907 /* Pre-fetch: some regs may not be in the cache. */
1908 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1909 fill_fprs (regcache, fprs);
1910 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1913 /* Special-purpose registers. */
1915 if (regno == -1 || special_register_p (gdbarch, regno))
1917 if (data->arch64)
1919 /* Pre-fetch: some registers won't be in the cache. */
1920 ptrace64aix (PTT_READ_SPRS, tid,
1921 (unsigned long) &sprs64, 0, NULL);
1922 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1923 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1924 &sprs64.pt_xer, &sprs64.pt_fpscr);
1925 ptrace64aix (PTT_WRITE_SPRS, tid,
1926 (unsigned long) &sprs64, 0, NULL);
1928 else
1930 /* The contents of "struct ptspr" were declared as "unsigned
1931 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1932 Use temporaries to work around this problem. Also, add an
1933 assert here to make sure we fail if the system header files
1934 use "unsigned long", and the size of that type is not what
1935 the headers expect. */
1936 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1937 tmp_fpscr;
1939 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1941 /* Pre-fetch: some registers won't be in the cache. */
1942 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1944 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1945 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1947 sprs32.pt_iar = tmp_iar;
1948 sprs32.pt_msr = tmp_msr;
1949 sprs32.pt_cr = tmp_cr;
1950 sprs32.pt_lr = tmp_lr;
1951 sprs32.pt_ctr = tmp_ctr;
1952 sprs32.pt_xer = tmp_xer;
1953 sprs32.pt_fpscr = tmp_fpscr;
1955 if (tdep->ppc_mq_regnum >= 0)
1956 if (REG_VALID == regcache->get_register_status
1957 (tdep->ppc_mq_regnum))
1958 regcache->raw_collect (tdep->ppc_mq_regnum, &sprs32.pt_mq);
1960 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1964 /* Vector registers. */
1965 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1
1966 && (regno == -1 || (regno >= tdep->ppc_vr0_regnum
1967 && regno <= tdep->ppc_vrsave_regnum)))
1969 __vmx_context_t vmx;
1970 if (__power_vmx())
1972 if (data->arch64)
1973 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1974 else
1975 ret = ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1976 if (ret > 0)
1978 fill_altivec(regcache, &vmx);
1979 if (data->arch64)
1980 ret = ptrace64aix (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1981 else
1982 ret = ptrace32 (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1983 if (ret < 0)
1984 perror_with_name (_("Unable to store AltiVec register after read"));
1989 /* VSX registers. */
1990 if (tdep->ppc_vsr0_upper_regnum != -1 && (regno == -1
1991 || (regno >=tdep->ppc_vsr0_upper_regnum
1992 && regno < tdep->ppc_vsr0_upper_regnum + ppc_num_vshrs)))
1994 __vsx_context_t vsx;
1995 if (__power_vsx())
1997 if (data->arch64)
1998 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1999 else
2000 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
2001 if (ret > 0)
2003 fill_vsx (regcache, &vsx);
2004 if (data->arch64)
2005 ret = ptrace64aix (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
2006 else
2007 ret = ptrace32 (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
2008 if (ret < 0)
2009 perror_with_name (_("Unable to store VSX register after read"));
2015 /* Store gdb's current view of the register set into the
2016 thread/process connected to REGCACHE. */
2018 void
2019 aix_thread_target::store_registers (struct regcache *regcache, int regno)
2021 struct thread_info *thread;
2022 pthdb_tid_t tid;
2024 if (regcache->ptid ().tid () == 0)
2025 beneath ()->store_registers (regcache, regno);
2026 else
2028 thread = current_inferior ()->find_thread (regcache->ptid ());
2029 aix_thread_info *priv = get_aix_thread_info (thread);
2030 tid = priv->tid;
2032 if (tid == PTHDB_INVALID_TID)
2033 store_regs_user_thread (regcache, priv->pdtid);
2034 else
2035 store_regs_kernel_thread (regcache, regno, tid);
2039 /* Implement the to_xfer_partial target_ops method. */
2041 enum target_xfer_status
2042 aix_thread_target::xfer_partial (enum target_object object,
2043 const char *annex, gdb_byte *readbuf,
2044 const gdb_byte *writebuf,
2045 ULONGEST offset, ULONGEST len,
2046 ULONGEST *xfered_len)
2048 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
2050 inferior_ptid = ptid_t (inferior_ptid.pid ());
2051 return beneath ()->xfer_partial (object, annex, readbuf,
2052 writebuf, offset, len, xfered_len);
2055 /* Clean up after the inferior exits. */
2057 void
2058 aix_thread_target::mourn_inferior ()
2060 target_ops *beneath = this->beneath ();
2062 pd_disable (current_inferior ());
2063 beneath->mourn_inferior ();
2066 /* Return whether thread PID is still valid. */
2068 bool
2069 aix_thread_target::thread_alive (ptid_t ptid)
2071 if (ptid.tid () == 0)
2072 return beneath ()->thread_alive (ptid);
2074 /* We update the thread list every time the child stops, so all
2075 valid threads should be in the thread list. */
2076 process_stratum_target *proc_target
2077 = current_inferior ()->process_target ();
2078 return in_thread_list (proc_target, ptid);
2081 /* Return a printable representation of composite PID for use in
2082 "info threads" output. */
2084 std::string
2085 aix_thread_target::pid_to_str (ptid_t ptid)
2087 if (ptid.tid () == 0)
2088 return beneath ()->pid_to_str (ptid);
2090 return string_printf (_("Thread %s"), pulongest (ptid.tid ()));
2093 /* Return a printable representation of extra information about
2094 THREAD, for use in "info threads" output. */
2096 const char *
2097 aix_thread_target::extra_thread_info (struct thread_info *thread)
2099 int status;
2100 pthdb_pthread_t pdtid;
2101 pthdb_tid_t tid;
2102 pthdb_state_t state;
2103 pthdb_suspendstate_t suspendstate;
2104 pthdb_detachstate_t detachstate;
2105 int cancelpend;
2106 static char *ret = NULL;
2107 struct aix_thread_variables *data;
2109 data = get_thread_data_helper_for_ptid (thread->ptid);
2111 if (thread->ptid.tid () == 0)
2112 return NULL;
2114 string_file buf;
2115 aix_thread_info *priv = get_aix_thread_info (thread);
2117 pdtid = priv->pdtid;
2118 tid = priv->tid;
2120 if (tid != PTHDB_INVALID_TID)
2121 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
2122 buf.printf (_("tid %d"), (int)tid);
2124 status = pthdb_pthread_state (data->pd_session, pdtid, &state);
2125 if (status != PTHDB_SUCCESS)
2126 state = PST_NOTSUP;
2127 buf.printf (", %s", state2str (state));
2129 status = pthdb_pthread_suspendstate (data->pd_session, pdtid,
2130 &suspendstate);
2131 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
2132 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
2133 buf.printf (_(", suspended"));
2135 status = pthdb_pthread_detachstate (data->pd_session, pdtid,
2136 &detachstate);
2137 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
2138 /* i18n: Like "Thread-Id %d, [state] running, detached" */
2139 buf.printf (_(", detached"));
2141 pthdb_pthread_cancelpend (data->pd_session, pdtid, &cancelpend);
2142 if (status == PTHDB_SUCCESS && cancelpend)
2143 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
2144 buf.printf (_(", cancel pending"));
2146 buf.write ("", 1);
2148 xfree (ret); /* Free old buffer. */
2150 ret = xstrdup (buf.c_str ());
2152 return ret;
2155 ptid_t
2156 aix_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
2158 return ptid_t (inferior_ptid.pid (), 0, thread);
2162 /* Module startup initialization function, automagically called by
2163 init.c. */
2165 void _initialize_aix_thread ();
2166 void
2167 _initialize_aix_thread ()
2169 /* Notice when object files get loaded and unloaded. */
2170 gdb::observers::new_objfile.attach (new_objfile, "aix-thread");
2172 /* Add ourselves to inferior_created event chain.
2173 This is needed to enable the thread target on "attach". */
2174 gdb::observers::inferior_created.attach (aix_thread_inferior_created,
2175 "aix-thread");
2177 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
2178 _("Set debugging of AIX thread module."),
2179 _("Show debugging of AIX thread module."),
2180 _("Enables debugging output (used to debug GDB)."),
2181 NULL, NULL,
2182 /* FIXME: i18n: Debugging of AIX thread
2183 module is \"%d\". */
2184 &setdebuglist, &showdebuglist);