Fix Segmentation Fault in AIX during multi process debugging.
[binutils-gdb.git] / gdb / aix-thread.c
blob327f5607d4598659e8ca8ee0c13ead8da4b21def
1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2024 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 "gdbthread.h"
43 #include "target.h"
44 #include "inferior.h"
45 #include "regcache.h"
46 #include "cli/cli-cmds.h"
47 #include "ppc-tdep.h"
48 #include "observable.h"
49 #include "objfiles.h"
51 #include <procinfo.h>
52 #include <sys/types.h>
53 #include <sys/ptrace.h>
54 #include <sys/reg.h>
55 #include <sched.h>
56 #include <sys/pthdebug.h>
57 #include <unordered_set>
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;
194 /* Describes the number of thread exit events reported. */
195 std::unordered_set<pthdb_pthread_t> exited_threads;
198 /* Key to our per-inferior data. */
199 static const registry<inferior>::key<aix_thread_variables>
200 aix_thread_variables_handle;
202 /* Function to Get aix_thread_variables data. */
203 static struct aix_thread_variables*
204 get_aix_thread_variables_data (struct inferior *inf)
206 if (inf == NULL)
207 return NULL;
209 struct aix_thread_variables* data;
211 data = aix_thread_variables_handle.get (inf);
212 if (data == NULL)
213 data = aix_thread_variables_handle.emplace (inf);
215 return data;
218 /* Helper to get data for ptid in a function. */
220 static struct aix_thread_variables*
221 get_thread_data_helper_for_ptid (ptid_t ptid)
223 inferior *inf = find_inferior_ptid (current_inferior ()->process_target (),
224 ptid);
225 return get_aix_thread_variables_data (inf);
228 /* Helper to get data for pid in a function. */
230 static struct aix_thread_variables*
231 get_thread_data_helper_for_pid (pid_t pid)
233 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
234 pid);
235 return get_aix_thread_variables_data (inf);
238 /* Return a printable representation of pthdebug function return
239 STATUS. */
241 static const char *
242 pd_status2str (int status)
244 switch (status)
246 case PTHDB_SUCCESS: return "SUCCESS";
247 case PTHDB_NOSYS: return "NOSYS";
248 case PTHDB_NOTSUP: return "NOTSUP";
249 case PTHDB_BAD_VERSION: return "BAD_VERSION";
250 case PTHDB_BAD_USER: return "BAD_USER";
251 case PTHDB_BAD_SESSION: return "BAD_SESSION";
252 case PTHDB_BAD_MODE: return "BAD_MODE";
253 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
254 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
255 case PTHDB_BAD_POINTER: return "BAD_POINTER";
256 case PTHDB_BAD_CMD: return "BAD_CMD";
257 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
258 case PTHDB_BAD_ATTR: return "BAD_ATTR";
259 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
260 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
261 case PTHDB_BAD_COND: return "BAD_COND";
262 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
263 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
264 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
265 case PTHDB_BAD_KEY: return "BAD_KEY";
266 case PTHDB_BAD_PTID: return "BAD_PTID";
267 case PTHDB_BAD_TID: return "BAD_TID";
268 case PTHDB_CALLBACK: return "CALLBACK";
269 case PTHDB_CONTEXT: return "CONTEXT";
270 case PTHDB_HELD: return "HELD";
271 case PTHDB_NOT_HELD: return "NOT_HELD";
272 case PTHDB_MEMORY: return "MEMORY";
273 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
274 case PTHDB_SYMBOL: return "SYMBOL";
275 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
276 case PTHDB_INTERNAL: return "INTERNAL";
277 default: return "UNKNOWN";
281 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
282 exceptional conditions and either return nonlocally or else return
283 1 for success and 0 for failure. */
285 static int
286 ptrace_check (int req, int id, int ret)
288 if (ret == 0 && !errno)
289 return 1;
291 /* According to ptrace(2), ptrace may fail with EPERM if "the
292 Identifier parameter corresponds to a kernel thread which is
293 stopped in kernel mode and whose computational state cannot be
294 read or written." This happens quite often with register reads. */
296 switch (req)
298 case PTT_READ_GPRS:
299 case PTT_READ_FPRS:
300 case PTT_READ_SPRS:
301 if (ret == -1 && errno == EPERM)
303 if (debug_aix_thread)
304 gdb_printf (gdb_stdlog,
305 "ptrace (%d, %d) = %d (errno = %d)\n",
306 req, id, ret, errno);
307 return ret == -1 ? 0 : 1;
309 break;
310 case PTT_READ_VEC:
311 case PTT_READ_VSX:
312 if (debug_aix_thread)
313 gdb_printf (gdb_stdlog,
314 "ptrace (%d, %d) = %d (errno = %d)\n",
315 req, id, ret, errno);
316 if (ret == -1)
317 return -1;
318 break;
320 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
321 req, id, ret, errno, safe_strerror (errno));
322 return 0; /* Not reached. */
325 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
326 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
327 Return success. */
329 #ifdef HAVE_PTRACE64
330 # define ptracex(request, pid, addr, data, buf) \
331 ptrace64 (request, pid, addr, data, buf)
332 #endif
334 static int
335 ptrace64aix (int req, int id, long long addr, int data, int *buf)
337 errno = 0;
338 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
341 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
342 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
343 Return success. */
345 #ifdef HAVE_PTRACE64
346 # define ptrace(request, pid, addr, data, buf) \
347 ptrace64 (request, pid, addr, data, buf)
348 # define addr_ptr long long
349 #else
350 # define addr_ptr int *
351 #endif
353 static int
354 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
356 errno = 0;
357 return ptrace_check (req, id,
358 ptrace (req, id, addr, data, buf));
361 /* If *PIDP is a composite process/thread id, convert it to a
362 process id. */
364 static void
365 pid_to_prc (ptid_t *ptidp)
367 ptid_t ptid;
369 ptid = *ptidp;
370 if (ptid.tid () != 0)
371 *ptidp = ptid_t (ptid.pid ());
374 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
375 the address of SYMBOLS[<i>].name. */
377 static int
378 pdc_symbol_addrs (pthdb_user_t user_current_pid, pthdb_symbol_t *symbols, int count)
380 struct bound_minimal_symbol ms;
381 int i;
382 char *name;
384 if (debug_aix_thread)
385 gdb_printf (gdb_stdlog,
386 "pdc_symbol_addrs (user_current_pid = %ld, symbols = 0x%lx, count = %d)\n",
387 user_current_pid, (long) symbols, count);
389 for (i = 0; i < count; i++)
391 name = symbols[i].name;
392 if (debug_aix_thread)
393 gdb_printf (gdb_stdlog,
394 " symbols[%d].name = \"%s\"\n", i, name);
396 if (!*name)
397 symbols[i].addr = 0;
398 else
400 ms = lookup_minimal_symbol (name, NULL, NULL);
401 if (ms.minsym == NULL)
403 if (debug_aix_thread)
404 gdb_printf (gdb_stdlog, " returning PDC_FAILURE\n");
405 return PDC_FAILURE;
407 symbols[i].addr = ms.value_address ();
409 if (debug_aix_thread)
410 gdb_printf (gdb_stdlog, " symbols[%d].addr = %s\n",
411 i, hex_string (symbols[i].addr));
413 if (debug_aix_thread)
414 gdb_printf (gdb_stdlog, " returning PDC_SUCCESS\n");
415 return PDC_SUCCESS;
418 /* Read registers call back function should be able to read the
419 context information of a debuggee kernel thread from an active
420 process or from a core file. The information should be formatted
421 in context64 form for both 32-bit and 64-bit process.
422 If successful return 0, else non-zero is returned. */
424 static int
425 pdc_read_regs (pthdb_user_t user_current_pid,
426 pthdb_tid_t tid,
427 unsigned long long flags,
428 pthdb_context_t *context)
430 /* This function doesn't appear to be used, so we could probably
431 just return 0 here. HOWEVER, if it is not defined, the OS will
432 complain and several thread debug functions will fail. In case
433 this is needed, I have implemented what I think it should do,
434 however this code is untested. */
436 uint64_t gprs64[ppc_num_gprs];
437 uint32_t gprs32[ppc_num_gprs];
438 double fprs[ppc_num_fprs];
439 struct ptxsprs sprs64;
440 struct ptsprs sprs32;
441 struct aix_thread_variables *data;
443 data = get_thread_data_helper_for_pid (user_current_pid);
445 if (debug_aix_thread)
446 gdb_printf (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
447 (int) tid, hex_string (flags));
449 /* General-purpose registers. */
450 if (flags & PTHDB_FLAG_GPRS)
452 if (data->arch64)
454 if (!ptrace64aix (PTT_READ_GPRS, tid,
455 (unsigned long) gprs64, 0, NULL))
456 memset (gprs64, 0, sizeof (gprs64));
457 memcpy (context->gpr, gprs64, sizeof(gprs64));
459 else
461 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
462 memset (gprs32, 0, sizeof (gprs32));
463 memcpy (context->gpr, gprs32, sizeof(gprs32));
467 /* Floating-point registers. */
468 if (flags & PTHDB_FLAG_FPRS)
470 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
471 memset (fprs, 0, sizeof (fprs));
472 memcpy (context->fpr, fprs, sizeof(fprs));
475 /* Special-purpose registers. */
476 if (flags & PTHDB_FLAG_SPRS)
478 if (data->arch64)
480 if (!ptrace64aix (PTT_READ_SPRS, tid,
481 (unsigned long) &sprs64, 0, NULL))
482 memset (&sprs64, 0, sizeof (sprs64));
483 memcpy (&context->msr, &sprs64, sizeof(sprs64));
485 else
487 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
488 memset (&sprs32, 0, sizeof (sprs32));
489 memcpy (&context->msr, &sprs32, sizeof(sprs32));
493 /* vector registers. */
494 __vmx_context_t vmx;
495 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
497 if (data->arch64)
499 if (!ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
500 memset (&vmx, 0, sizeof (vmx));
501 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
503 else
505 if (!ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
506 memset (&vmx, 0, sizeof (vmx));
507 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
511 /* vsx registers. */
512 __vsx_context_t vsx;
513 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
515 if (data->arch64)
517 if (!ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
518 memset (&vsx, 0, sizeof (vsx));
519 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
521 else
523 if (!ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
524 memset (&vsx, 0, sizeof (vsx));
525 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
528 return 0;
531 /* Write register function should be able to write requested context
532 information to specified debuggee's kernel thread id.
533 If successful return 0, else non-zero is returned. */
535 static int
536 pdc_write_regs (pthdb_user_t user_current_pid,
537 pthdb_tid_t tid,
538 unsigned long long flags,
539 pthdb_context_t *context)
541 /* This function doesn't appear to be used, so we could probably
542 just return 0 here. HOWEVER, if it is not defined, the OS will
543 complain and several thread debug functions will fail. In case
544 this is needed, I have implemented what I think it should do,
545 however this code is untested. */
547 struct aix_thread_variables *data;
549 data = get_thread_data_helper_for_pid (user_current_pid);
551 if (debug_aix_thread)
552 gdb_printf (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
553 (int) tid, hex_string (flags));
555 /* General-purpose registers. */
556 if (flags & PTHDB_FLAG_GPRS)
558 if (data->arch64)
559 ptrace64aix (PTT_WRITE_GPRS, tid,
560 (unsigned long) context->gpr, 0, NULL);
561 else
562 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
565 /* Floating-point registers. */
566 if (flags & PTHDB_FLAG_FPRS)
568 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
571 /* Special-purpose registers. */
572 if (flags & PTHDB_FLAG_SPRS)
574 if (data->arch64)
576 ptrace64aix (PTT_WRITE_SPRS, tid,
577 (unsigned long) &context->msr, 0, NULL);
579 else
581 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
585 /* vector registers. */
586 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
588 if (data->arch64)
589 ptrace64aix (PTT_WRITE_VEC, tid, (unsigned long) &context->vmx, 0, 0);
590 else
591 ptrace32 (PTT_WRITE_VEC, tid, (uintptr_t) &context->vmx, 0, 0);
594 /* vsx registers. */
595 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
597 if (data->arch64)
598 ptrace64aix (PTT_WRITE_VSX, tid, (unsigned long) &context->vsx, 0, 0);
599 else
600 ptrace32 (PTT_WRITE_VSX, tid, (uintptr_t) &context->vsx, 0, 0);
602 return 0;
605 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
607 static int
608 pdc_read_data (pthdb_user_t user_current_pid, void *buf,
609 pthdb_addr_t addr, size_t len)
611 int status, ret;
612 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
613 user_current_pid);
615 if (debug_aix_thread)
616 gdb_printf (gdb_stdlog,
617 "pdc_read_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
618 user_current_pid, (long) buf, hex_string (addr), len);
620 /* This is needed to eliminate the dependency of current thread
621 which is null so that thread reads the correct target memory. */
623 scoped_restore_current_inferior_for_memory save_inferior (inf);
624 status = target_read_memory (addr, (gdb_byte *) buf, len);
626 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
628 if (debug_aix_thread)
629 gdb_printf (gdb_stdlog, " status=%d, returning %s\n",
630 status, pd_status2str (ret));
631 return ret;
634 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
636 static int
637 pdc_write_data (pthdb_user_t user_current_pid, void *buf,
638 pthdb_addr_t addr, size_t len)
640 int status, ret;
641 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
642 user_current_pid);
644 if (debug_aix_thread)
645 gdb_printf (gdb_stdlog,
646 "pdc_write_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
647 user_current_pid, (long) buf, hex_string (addr), len);
650 scoped_restore_current_inferior_for_memory save_inferior (inf);
651 status = target_write_memory (addr, (gdb_byte *) buf, len);
654 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
656 if (debug_aix_thread)
657 gdb_printf (gdb_stdlog, " status=%d, returning %s\n", status,
658 pd_status2str (ret));
659 return ret;
662 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
663 in BUFP. */
665 static int
666 pdc_alloc (pthdb_user_t user_current_pid, size_t len, void **bufp)
668 if (debug_aix_thread)
669 gdb_printf (gdb_stdlog,
670 "pdc_alloc (user_current_pid = %ld, len = %ld, bufp = 0x%lx)\n",
671 user_current_pid, len, (long) bufp);
672 *bufp = xmalloc (len);
673 if (debug_aix_thread)
674 gdb_printf (gdb_stdlog,
675 " malloc returned 0x%lx\n", (long) *bufp);
677 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
678 be returned. */
680 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
683 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
684 realloc callback, so that it contains LEN bytes, and store a
685 pointer to the result in BUFP. */
687 static int
688 pdc_realloc (pthdb_user_t user_current_pid, void *buf, size_t len, void **bufp)
690 if (debug_aix_thread)
691 gdb_printf (gdb_stdlog,
692 "pdc_realloc (user_current_pid = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
693 user_current_pid, (long) buf, len, (long) bufp);
694 *bufp = xrealloc (buf, len);
695 if (debug_aix_thread)
696 gdb_printf (gdb_stdlog,
697 " realloc returned 0x%lx\n", (long) *bufp);
698 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
701 /* pthdb callback: free BUF, which was allocated by the alloc or
702 realloc callback. */
704 static int
705 pdc_dealloc (pthdb_user_t user_current_pid, void *buf)
707 if (debug_aix_thread)
708 gdb_printf (gdb_stdlog,
709 "pdc_free (user_current_pid = %ld, buf = 0x%lx)\n", user_current_pid,
710 (long) buf);
711 xfree (buf);
712 return PDC_SUCCESS;
715 /* Return a printable representation of pthread STATE. */
717 static char *
718 state2str (pthdb_state_t state)
720 switch (state)
722 case PST_IDLE:
723 /* i18n: Like "Thread-Id %d, [state] idle" */
724 return _("idle"); /* being created */
725 case PST_RUN:
726 /* i18n: Like "Thread-Id %d, [state] running" */
727 return _("running"); /* running */
728 case PST_SLEEP:
729 /* i18n: Like "Thread-Id %d, [state] sleeping" */
730 return _("sleeping"); /* awaiting an event */
731 case PST_READY:
732 /* i18n: Like "Thread-Id %d, [state] ready" */
733 return _("ready"); /* runnable */
734 case PST_TERM:
735 /* i18n: Like "Thread-Id %d, [state] finished" */
736 return _("finished"); /* awaiting a join/detach */
737 default:
738 /* i18n: Like "Thread-Id %d, [state] unknown" */
739 return _("unknown");
743 /* Search through the list of all kernel threads for the thread
744 that has stopped on a SIGTRAP signal, and return its TID.
745 Return 0 if none found. */
747 static pthdb_tid_t
748 get_signaled_thread (int pid)
750 struct thrdsinfo64 thrinf;
751 tid_t ktid = 0;
753 while (1)
755 if (getthrds (pid, &thrinf,
756 sizeof (thrinf), &ktid, 1) != 1)
757 break;
759 /* We also need to keep in mind Trap and interrupt or any
760 signal that needs to be handled in pd_update (). */
762 if (thrinf.ti_cursig)
763 return thrinf.ti_tid;
766 /* Didn't find any thread stopped on a SIGTRAP signal. */
767 return 0;
770 /* Synchronize GDB's thread list with libpthdebug's.
772 There are some benefits of doing this every time the inferior stops:
774 - allows users to run thread-specific commands without needing to
775 run "info threads" first
777 - helps pthdb_tid_pthread() work properly (see "libpthdebug
778 peculiarities" at the top of this module)
780 - simplifies the demands placed on libpthdebug, which seems to
781 have difficulty with certain call patterns */
783 static void
784 sync_threadlists (pid_t pid)
786 int cmd, status;
787 pthdb_pthread_t pdtid;
788 pthread_t pthid;
789 pthdb_tid_t tid;
790 process_stratum_target *proc_target = current_inferior ()->process_target ();
791 struct aix_thread_variables *data;
792 data = get_thread_data_helper_for_pid (pid);
793 pthdb_state_t state;
794 std::set<pthdb_pthread_t> in_queue_threads;
796 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
798 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
800 status = pthdb_pthread (data->pd_session, &pdtid, cmd);
801 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
802 break;
804 status = pthdb_pthread_ptid (data->pd_session, pdtid, &pthid);
805 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
806 continue;
808 ptid_t ptid (pid, 0, pthid);
809 status = pthdb_pthread_state (data->pd_session, pdtid, &state);
810 in_queue_threads.insert (pdtid);
812 /* If this thread has reported and exited, do not add it again. */
813 if (state == PST_TERM)
815 if (data->exited_threads.count (pdtid) != 0)
816 continue;
819 /* If this thread has never been reported to GDB, add it. */
820 if (!in_thread_list (proc_target, ptid))
822 aix_thread_info *priv = new aix_thread_info;
823 /* init priv */
824 priv->pdtid = pdtid;
825 status = pthdb_pthread_tid (data->pd_session, pdtid, &tid);
826 priv->tid = tid;
827 /* Check if this is the main thread. If it is, then change
828 its ptid and add its private data. */
829 if (in_thread_list (proc_target, ptid_t (pid)))
831 thread_info *tp = proc_target->find_thread (ptid_t (pid));
832 thread_change_ptid (proc_target, ptid_t (pid), ptid);
833 tp->priv.reset (priv);
835 else
836 add_thread_with_info (proc_target, ptid,
837 private_thread_info_up (priv));
840 /* The thread is terminated. Remove it. */
841 if (state == PST_TERM)
843 thread_info *thr = proc_target->find_thread (ptid);
844 gdb_assert (thr != nullptr);
845 delete_thread (thr);
846 data->exited_threads.insert (pdtid);
850 /* Sometimes there can be scenarios where the thread status is
851 unknown and we it will never iterate in the for loop above,
852 since cmd will be no longer be pointing to that threads. One
853 such scenario is the gdb.threads/thread_events.exp testcase
854 where in the end after the threadfunc breakpoint is hit, the
855 thread exits and gets into a PST_UNKNOWN state. So this thread
856 will not run in the above for loop. Therefore the below for loop
857 is to manually delete such threads. */
858 for (struct thread_info *it : all_threads ())
860 aix_thread_info *priv = get_aix_thread_info (it);
861 if (in_queue_threads.count (priv->pdtid) == 0
862 && in_thread_list (proc_target, it->ptid)
863 && pid == it->ptid.pid ())
865 delete_thread (it);
866 data->exited_threads.insert (priv->pdtid);
871 /* Iterate_over_threads() callback for locating a thread, using
872 the TID of its associated kernel thread. */
874 static int
875 iter_tid (struct thread_info *thread, void *tidp)
877 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
878 aix_thread_info *priv = get_aix_thread_info (thread);
880 return priv->tid == tid;
883 /* Synchronize libpthdebug's state with the inferior and with GDB,
884 generate a composite process/thread <pid> for the current thread,
885 Return the ptid of the event thread if one can be found, else
886 return a pid-only ptid with PID. */
888 static ptid_t
889 pd_update (pid_t pid)
891 int status;
892 ptid_t ptid;
893 pthdb_tid_t tid;
894 struct thread_info *thread = NULL;
895 struct aix_thread_variables *data;
897 data = get_thread_data_helper_for_pid (pid);
899 if (!data->pd_active)
900 return ptid_t (pid);
902 status = pthdb_session_update (data->pd_session);
903 if (status != PTHDB_SUCCESS)
904 return ptid_t (pid);
906 sync_threadlists (pid);
908 /* Define "current thread" as one that just received a trap signal. */
910 tid = get_signaled_thread (pid);
911 if (tid != 0)
912 thread = iterate_over_threads (iter_tid, &tid);
913 if (!thread)
914 ptid = ptid_t (pid);
915 else
916 ptid = thread->ptid;
918 return ptid;
921 /* Try to start debugging threads in the current process.
922 If successful and there exists and we can find an event thread, set
923 pd_active for that thread. Otherwise, return. */
925 static void
926 pd_activate (pid_t pid)
928 int status;
929 struct aix_thread_variables *data;
930 data = get_thread_data_helper_for_pid (pid);
932 status = pthdb_session_init (pid, data->arch64 ? PEM_64BIT : PEM_32BIT,
933 PTHDB_FLAG_REGS, &pd_callbacks,
934 &data->pd_session);
935 if (status == PTHDB_SUCCESS)
936 data->pd_active = 1;
939 /* AIX implementation of update_thread_list. */
941 void
942 aix_thread_target::update_thread_list ()
944 for (inferior *inf : all_inferiors ())
946 if (inf->pid == 0)
947 continue;
949 pd_update (inf->pid);
954 /* An object file has just been loaded. Check whether the current
955 application is pthreaded, and if so, prepare for thread debugging. */
957 static void
958 pd_enable (inferior *inf)
960 int status;
961 char *stub_name;
962 struct bound_minimal_symbol ms;
963 struct aix_thread_variables *data;
965 if (inf == NULL)
966 return;
968 data = get_aix_thread_variables_data (inf);
970 /* Don't initialize twice. */
971 if (data->pd_able)
972 return;
974 /* Check application word size. */
975 data->arch64 = register_size (current_inferior ()->arch (), 0) == 8;
977 /* Check whether the application is pthreaded. */
978 stub_name = NULL;
979 status = pthdb_session_pthreaded (inf->pid, PTHDB_FLAG_REGS,
980 &pd_callbacks, &stub_name);
981 if ((status != PTHDB_SUCCESS
982 && status != PTHDB_NOT_PTHREADED) || !stub_name)
983 return;
985 /* Set a breakpoint on the returned stub function. */
986 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
987 if (ms.minsym == NULL)
988 return;
989 data->pd_brk_addr = ms.value_address ();
990 if (!create_thread_event_breakpoint (current_inferior ()->arch (),
991 data->pd_brk_addr))
992 return;
994 /* Prepare for thread debugging. */
995 current_inferior ()->push_target (&aix_thread_ops);
996 data->pd_able = 1;
998 /* If we're debugging a core file or an attached inferior, the
999 pthread library may already have been initialized, so try to
1000 activate thread debugging. */
1001 pd_activate (inf->pid);
1004 /* Undo the effects of pd_enable(). */
1006 static void
1007 pd_disable (inferior *inf)
1009 struct aix_thread_variables *data;
1010 data = get_aix_thread_variables_data (inf);
1012 if (!data->pd_able)
1013 return;
1014 if (!data->pd_active)
1015 return;
1016 pthdb_session_destroy (data->pd_session);
1018 pid_to_prc (&inferior_ptid);
1019 data->pd_active = 0;
1020 data->pd_able = 0;
1021 current_inferior ()->unpush_target (&aix_thread_ops);
1024 /* new_objfile observer callback.
1026 Check whether a threaded application is being debugged, and if so, prepare
1027 for thread debugging. */
1029 static void
1030 new_objfile (struct objfile *objfile)
1032 pd_enable (current_inferior ());
1035 /* Attach to process specified by ARGS. */
1037 static void
1038 aix_thread_inferior_created (inferior *inf)
1040 pd_enable (inf);
1043 /* Detach from the process attached to by aix_thread_attach(). */
1045 void
1046 aix_thread_target::detach (inferior *inf, int from_tty)
1048 target_ops *beneath = this->beneath ();
1050 pd_disable (inf);
1051 beneath->detach (inf, from_tty);
1054 /* Tell the inferior process to continue running thread PID if != -1
1055 and all threads otherwise. */
1057 void
1058 aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1060 struct thread_info *thread;
1061 pthdb_tid_t tid[2];
1062 struct aix_thread_variables *data;
1064 data = get_thread_data_helper_for_ptid (ptid);
1066 if (ptid.tid () == 0)
1068 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1070 inferior_ptid = ptid_t (inferior_ptid.pid ());
1071 beneath ()->resume (ptid, step, sig);
1073 else
1075 thread = current_inferior ()->find_thread (ptid);
1076 if (!thread)
1077 error (_("aix-thread resume: unknown pthread %ld"),
1078 ptid.lwp ());
1080 aix_thread_info *priv = get_aix_thread_info (thread);
1082 tid[0] = priv->tid;
1083 if (tid[0] == PTHDB_INVALID_TID)
1084 error (_("aix-thread resume: no tid for pthread %ld"),
1085 ptid.lwp ());
1086 tid[1] = 0;
1088 if (data->arch64)
1089 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1090 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1091 else
1092 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1093 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1097 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1098 If an error occurs, return -1, else return the pid of the stopped
1099 thread. */
1101 ptid_t
1102 aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1103 target_wait_flags options)
1105 struct aix_thread_variables *data;
1107 pid_to_prc (&ptid);
1109 ptid = beneath ()->wait (ptid, status, options);
1112 if (ptid.pid () == -1)
1113 return ptid_t (-1);
1115 /* The target beneath does not deal with threads, so it should only return
1116 pid-only ptids. */
1117 gdb_assert (ptid.is_pid ());
1119 data = get_thread_data_helper_for_ptid (ptid);
1121 /* Check whether libpthdebug might be ready to be initialized. */
1122 if (!data->pd_active && status->kind () == TARGET_WAITKIND_STOPPED
1123 && status->sig () == GDB_SIGNAL_TRAP)
1125 process_stratum_target *proc_target
1126 = current_inferior ()->process_target ();
1127 struct regcache *regcache = get_thread_regcache (proc_target, ptid);
1128 struct gdbarch *gdbarch = regcache->arch ();
1130 if (regcache_read_pc (regcache)
1131 - gdbarch_decr_pc_after_break (gdbarch) == data->pd_brk_addr)
1132 pd_activate (ptid.pid ());
1135 return pd_update (ptid.pid ());
1138 /* Supply AIX altivec registers, both 64 and 32 bit. */
1140 static void
1141 supply_altivec_regs (struct regcache *regcache, __vmx_context_t vmx)
1143 ppc_gdbarch_tdep *tdep
1144 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1145 int regno;
1146 for (regno = 0; regno < ppc_num_vrs; regno++)
1147 regcache->raw_supply (tdep->ppc_vr0_regnum + regno,
1148 &(vmx.__vr[regno]));
1149 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1150 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1153 /* Supply AIX VSX registers, both 64 and 32 bit. */
1155 static void
1156 supply_vsx_regs (struct regcache *regcache, __vsx_context_t vsx)
1158 ppc_gdbarch_tdep *tdep
1159 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1160 int regno;
1162 for (regno = 0; regno < ppc_num_vshrs; regno++)
1163 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + regno,
1164 &(vsx.__vsr_dw1[regno]));
1167 /* Record that the 64-bit general-purpose registers contain VALS. */
1169 static void
1170 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1172 ppc_gdbarch_tdep *tdep
1173 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1174 int regno;
1176 for (regno = 0; regno < ppc_num_gprs; regno++)
1177 regcache->raw_supply (tdep->ppc_gp0_regnum + regno,
1178 (char *) (vals + regno));
1181 /* Record that 32-bit register REGNO contains VAL. */
1183 static void
1184 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1186 regcache->raw_supply (regno, (char *) &val);
1189 /* Record that the floating-point registers contain VALS. */
1191 static void
1192 supply_fprs (struct regcache *regcache, double *vals)
1194 struct gdbarch *gdbarch = regcache->arch ();
1195 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1196 int regno;
1198 /* This function should never be called on architectures without
1199 floating-point registers. */
1200 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1202 for (regno = tdep->ppc_fp0_regnum;
1203 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1204 regno++)
1205 regcache->raw_supply (regno,
1206 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1209 /* Predicate to test whether given register number is a "special" register. */
1210 static int
1211 special_register_p (struct gdbarch *gdbarch, int regno)
1213 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1215 return regno == gdbarch_pc_regnum (gdbarch)
1216 || regno == tdep->ppc_ps_regnum
1217 || regno == tdep->ppc_cr_regnum
1218 || regno == tdep->ppc_lr_regnum
1219 || regno == tdep->ppc_ctr_regnum
1220 || regno == tdep->ppc_xer_regnum
1221 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1222 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1226 /* Record that the special registers contain the specified 64-bit and
1227 32-bit values. */
1229 static void
1230 supply_sprs64 (struct regcache *regcache,
1231 uint64_t iar, uint64_t msr, uint32_t cr,
1232 uint64_t lr, uint64_t ctr, uint32_t xer,
1233 uint32_t fpscr)
1235 struct gdbarch *gdbarch = regcache->arch ();
1236 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1238 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1239 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1240 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1241 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1242 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1243 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1244 if (tdep->ppc_fpscr_regnum >= 0)
1245 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1248 /* Record that the special registers contain the specified 32-bit
1249 values. */
1251 static void
1252 supply_sprs32 (struct regcache *regcache,
1253 uint32_t iar, uint32_t msr, uint32_t cr,
1254 uint32_t lr, uint32_t ctr, uint32_t xer,
1255 uint32_t fpscr)
1257 struct gdbarch *gdbarch = regcache->arch ();
1258 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1260 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1261 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1262 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1263 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1264 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1265 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1266 if (tdep->ppc_fpscr_regnum >= 0)
1267 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1270 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1271 thread.
1273 There's no way to query a single register from a non-kernel
1274 pthread, so there's no need for a single-register version of this
1275 function. */
1277 static void
1278 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1280 struct gdbarch *gdbarch = regcache->arch ();
1281 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1282 int status, i;
1283 pthdb_context_t ctx;
1284 struct aix_thread_variables *data;
1285 data = get_thread_data_helper_for_ptid (inferior_ptid);
1287 if (debug_aix_thread)
1288 gdb_printf (gdb_stdlog,
1289 "fetch_regs_user_thread %lx\n", (long) pdtid);
1290 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1291 if (status != PTHDB_SUCCESS)
1292 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1293 pd_status2str (status));
1295 /* General-purpose registers. */
1297 if (data->arch64)
1298 supply_gprs64 (regcache, ctx.gpr);
1299 else
1300 for (i = 0; i < ppc_num_gprs; i++)
1301 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1303 /* Floating-point registers. */
1305 if (ppc_floating_point_unit_p (gdbarch))
1306 supply_fprs (regcache, ctx.fpr);
1308 /* Special registers. */
1310 if (data->arch64)
1311 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1312 ctx.xer, ctx.fpscr);
1313 else
1314 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1315 ctx.xer, ctx.fpscr);
1317 /* Altivec registers. */
1318 supply_altivec_regs (regcache, ctx.vmx);
1320 /* VSX registers. */
1321 supply_vsx_regs (regcache, ctx.vsx);
1324 /* Fetch register REGNO if != -1 or all registers otherwise from
1325 kernel thread TID.
1327 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1328 SPRs, but there's no way to query individual registers within those
1329 groups. Therefore, if REGNO != -1, this function fetches an entire
1330 group.
1332 Unfortunately, kernel thread register queries often fail with
1333 EPERM, indicating that the thread is in kernel space. This breaks
1334 backtraces of threads other than the current one. To make that
1335 breakage obvious without throwing an error to top level (which is
1336 bad e.g. during "info threads" output), zero registers that can't
1337 be retrieved. */
1339 static void
1340 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1341 pthdb_tid_t tid)
1343 struct gdbarch *gdbarch = regcache->arch ();
1344 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1345 uint64_t gprs64[ppc_num_gprs];
1346 uint32_t gprs32[ppc_num_gprs];
1347 double fprs[ppc_num_fprs];
1348 struct ptxsprs sprs64;
1349 struct ptsprs sprs32;
1350 int i;
1351 struct aix_thread_variables *data;
1353 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1355 if (debug_aix_thread)
1356 gdb_printf (gdb_stdlog,
1357 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1358 (long) tid, regno, data->arch64);
1360 /* General-purpose registers. */
1361 if (regno == -1
1362 || (tdep->ppc_gp0_regnum <= regno
1363 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1365 if (data->arch64)
1367 if (!ptrace64aix (PTT_READ_GPRS, tid,
1368 (unsigned long) gprs64, 0, NULL))
1369 memset (gprs64, 0, sizeof (gprs64));
1370 supply_gprs64 (regcache, gprs64);
1372 else
1374 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1375 memset (gprs32, 0, sizeof (gprs32));
1376 for (i = 0; i < ppc_num_gprs; i++)
1377 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1381 /* vector registers. */
1382 if (tdep->ppc_vr0_regnum != -1)
1384 int ret = 0;
1385 __vmx_context_t vmx;
1386 if (data->arch64)
1387 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1388 else
1389 ret = ptrace32 (PTT_READ_VEC, tid, (uintptr_t) &vmx, 0, 0);
1390 if (ret < 0)
1391 memset(&vmx, 0, sizeof(__vmx_context_t));
1392 for (i = 0; i < ppc_num_vrs; i++)
1393 regcache->raw_supply (tdep->ppc_vr0_regnum + i, &(vmx.__vr[i]));
1394 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1395 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1398 /* vsx registers. */
1399 if (tdep->ppc_vsr0_upper_regnum != -1)
1401 __vsx_context_t vsx;
1402 int ret = 0;
1403 if (data->arch64)
1404 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1405 else
1406 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1407 if (ret < 0)
1408 memset(&vsx, 0, sizeof(__vsx_context_t));
1409 for (i = 0; i < ppc_num_vshrs; i++)
1410 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i, &(vsx.__vsr_dw1[i]));
1413 /* Floating-point registers. */
1415 if (ppc_floating_point_unit_p (gdbarch)
1416 && (regno == -1
1417 || (regno >= tdep->ppc_fp0_regnum
1418 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1420 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1421 memset (fprs, 0, sizeof (fprs));
1422 supply_fprs (regcache, fprs);
1425 /* Special-purpose registers. */
1427 if (regno == -1 || special_register_p (gdbarch, regno))
1429 if (data->arch64)
1431 if (!ptrace64aix (PTT_READ_SPRS, tid,
1432 (unsigned long) &sprs64, 0, NULL))
1433 memset (&sprs64, 0, sizeof (sprs64));
1434 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1435 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1436 sprs64.pt_xer, sprs64.pt_fpscr);
1438 else
1440 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1441 memset (&sprs32, 0, sizeof (sprs32));
1442 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1443 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1444 sprs32.pt_fpscr);
1446 if (tdep->ppc_mq_regnum >= 0)
1447 regcache->raw_supply (tdep->ppc_mq_regnum, (char *) &sprs32.pt_mq);
1452 /* Fetch register REGNO if != -1 or all registers otherwise from the
1453 thread/process connected to REGCACHE. */
1455 void
1456 aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
1458 struct thread_info *thread;
1459 pthdb_tid_t tid;
1461 /* If a new inferior is born, then its pthread debug library is yet to
1462 initialised and hence has no private data. So the below if condition
1463 exists. */
1465 if (regcache->ptid ().tid () == 0)
1466 beneath ()->fetch_registers (regcache, regno);
1467 else
1469 thread = current_inferior ()->find_thread (regcache->ptid ());
1470 aix_thread_info *priv = get_aix_thread_info (thread);
1471 tid = priv->tid;
1473 if (tid == PTHDB_INVALID_TID)
1474 fetch_regs_user_thread (regcache, priv->pdtid);
1475 else
1476 fetch_regs_kernel_thread (regcache, regno, tid);
1480 /* Fill altivec registers. */
1482 static void
1483 fill_altivec (const struct regcache *regcache, __vmx_context_t *vmx)
1485 struct gdbarch *gdbarch = regcache->arch ();
1486 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1487 int regno;
1489 for (regno = 0; regno < ppc_num_vrs; regno++)
1490 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + regno))
1491 regcache->raw_collect (tdep->ppc_vr0_regnum + regno,
1492 &(vmx->__vr[regno]));
1494 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1495 regcache->raw_collect (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
1496 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1497 regcache->raw_collect (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
1500 /* Fill vsx registers. */
1502 static void
1503 fill_vsx (const struct regcache *regcache, __vsx_context_t *vsx)
1505 struct gdbarch *gdbarch = regcache->arch ();
1506 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1507 int regno;
1509 for (regno = 0; regno < ppc_num_vshrs; regno++)
1510 if (REG_VALID == regcache->get_register_status ( tdep->ppc_vsr0_upper_regnum + regno))
1511 regcache->raw_collect (tdep->ppc_vsr0_upper_regnum + regno,
1512 &(vsx->__vsr_dw1[0]) + regno);
1515 /* Store the gp registers into an array of uint32_t or uint64_t. */
1517 static void
1518 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1520 ppc_gdbarch_tdep *tdep
1521 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1522 int regno;
1524 for (regno = 0; regno < ppc_num_gprs; regno++)
1525 if (REG_VALID == regcache->get_register_status
1526 (tdep->ppc_gp0_regnum + regno))
1527 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1530 static void
1531 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1533 ppc_gdbarch_tdep *tdep
1534 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1535 int regno;
1537 for (regno = 0; regno < ppc_num_gprs; regno++)
1538 if (REG_VALID == regcache->get_register_status
1539 (tdep->ppc_gp0_regnum + regno))
1540 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1543 /* Store the floating point registers into a double array. */
1544 static void
1545 fill_fprs (const struct regcache *regcache, double *vals)
1547 struct gdbarch *gdbarch = regcache->arch ();
1548 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1549 int regno;
1551 /* This function should never be called on architectures without
1552 floating-point registers. */
1553 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1555 for (regno = tdep->ppc_fp0_regnum;
1556 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1557 regno++)
1558 if (REG_VALID == regcache->get_register_status (regno))
1559 regcache->raw_collect (regno, vals + regno - tdep->ppc_fp0_regnum);
1562 /* Store the special registers into the specified 64-bit and 32-bit
1563 locations. */
1565 static void
1566 fill_sprs64 (const struct regcache *regcache,
1567 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1568 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1569 uint32_t *fpscr)
1571 struct gdbarch *gdbarch = regcache->arch ();
1572 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1574 /* Verify that the size of the size of the IAR buffer is the
1575 same as the raw size of the PC (in the register cache). If
1576 they're not, then either GDB has been built incorrectly, or
1577 there's some other kind of internal error. To be really safe,
1578 we should check all of the sizes. */
1579 gdb_assert (sizeof (*iar) == register_size
1580 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1582 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1583 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1584 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1585 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1586 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1587 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1588 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1589 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1590 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1591 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1592 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1593 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1594 if (tdep->ppc_fpscr_regnum >= 0
1595 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1596 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1599 static void
1600 fill_sprs32 (const struct regcache *regcache,
1601 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1602 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1603 uint32_t *fpscr)
1605 struct gdbarch *gdbarch = regcache->arch ();
1606 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1608 /* Verify that the size of the size of the IAR buffer is the
1609 same as the raw size of the PC (in the register cache). If
1610 they're not, then either GDB has been built incorrectly, or
1611 there's some other kind of internal error. To be really safe,
1612 we should check all of the sizes. */
1613 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1614 gdbarch_pc_regnum (gdbarch)));
1616 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1617 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1618 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1619 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1620 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1621 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1622 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1623 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1624 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1625 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1626 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1627 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1628 if (tdep->ppc_fpscr_regnum >= 0
1629 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1630 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1633 /* Store all registers into pthread PDTID, which doesn't have a kernel
1634 thread.
1636 It's possible to store a single register into a non-kernel pthread,
1637 but I doubt it's worth the effort. */
1639 static void
1640 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1642 struct gdbarch *gdbarch = regcache->arch ();
1643 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1644 int status, i;
1645 pthdb_context_t ctx;
1646 uint32_t int32;
1647 uint64_t int64;
1648 struct aix_thread_variables *data;
1649 data = get_thread_data_helper_for_ptid (inferior_ptid);
1650 __vmx_context_t vmx;
1651 __vsx_context_t vsx;
1653 if (debug_aix_thread)
1654 gdb_printf (gdb_stdlog,
1655 "store_regs_user_thread %lx\n", (long) pdtid);
1657 /* Retrieve the thread's current context for its non-register
1658 values. */
1659 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1660 if (status != PTHDB_SUCCESS)
1661 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1662 pd_status2str (status));
1664 /* Fill altivec-registers. */
1666 if (__power_vmx())
1668 memset(&vmx, 0, sizeof(__vmx_context_t));
1669 for (i = 0; i < ppc_num_vrs; i++)
1670 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + i))
1672 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1673 &(vmx.__vr[i]));
1674 ctx.vmx.__vr[i] = vmx.__vr[i];
1676 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1677 ctx.vmx.__vrsave = vmx.__vrsave;
1678 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1679 ctx.vmx.__vscr = vmx.__vscr;
1682 /* Fill vsx registers. */
1684 if (__power_vsx())
1686 memset(&vsx, 0, sizeof(__vsx_context_t));
1687 for (i = 0; i < ppc_num_vshrs; i++)
1688 if (REG_VALID == regcache->get_register_status (tdep->ppc_vsr0_regnum + i))
1690 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1691 &(vsx.__vsr_dw1[i]));
1692 ctx.vsx.__vsr_dw1[i] = vsx.__vsr_dw1[i];
1696 /* Collect general-purpose register values from the regcache. */
1698 for (i = 0; i < ppc_num_gprs; i++)
1699 if (REG_VALID == regcache->get_register_status (tdep->ppc_gp0_regnum + i))
1701 if (data->arch64)
1703 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int64);
1704 ctx.gpr[i] = int64;
1706 else
1708 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int32);
1709 ctx.gpr[i] = int32;
1713 /* Collect floating-point register values from the regcache. */
1714 if (ppc_floating_point_unit_p (gdbarch))
1715 fill_fprs (regcache, ctx.fpr);
1717 /* Special registers (always kept in ctx as 64 bits). */
1718 if (data->arch64)
1720 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1721 &ctx.xer, &ctx.fpscr);
1723 else
1725 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1726 Solution: use 32-bit temp variables. */
1727 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1728 tmp_fpscr;
1730 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1731 &tmp_xer, &tmp_fpscr);
1732 if (REG_VALID == regcache->get_register_status
1733 (gdbarch_pc_regnum (gdbarch)))
1734 ctx.iar = tmp_iar;
1735 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1736 ctx.msr = tmp_msr;
1737 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1738 ctx.cr = tmp_cr;
1739 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1740 ctx.lr = tmp_lr;
1741 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1742 ctx.ctr = tmp_ctr;
1743 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1744 ctx.xer = tmp_xer;
1745 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1746 ctx.fpscr = tmp_fpscr;
1749 status = pthdb_pthread_setcontext (data->pd_session, pdtid, &ctx);
1750 if (status != PTHDB_SUCCESS)
1751 error (_("aix-thread: store_registers: "
1752 "pthdb_pthread_setcontext returned %s"),
1753 pd_status2str (status));
1756 /* Store register REGNO if != -1 or all registers otherwise into
1757 kernel thread TID.
1759 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1760 SPRs, but there's no way to set individual registers within those
1761 groups. Therefore, if REGNO != -1, this function stores an entire
1762 group. */
1764 static void
1765 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1766 pthdb_tid_t tid)
1768 struct gdbarch *gdbarch = regcache->arch ();
1769 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1770 uint64_t gprs64[ppc_num_gprs];
1771 uint32_t gprs32[ppc_num_gprs];
1772 double fprs[ppc_num_fprs];
1773 struct ptxsprs sprs64;
1774 struct ptsprs sprs32;
1775 struct aix_thread_variables *data;
1776 int ret = 0;
1778 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1780 if (debug_aix_thread)
1781 gdb_printf (gdb_stdlog,
1782 "store_regs_kernel_thread tid=%lx regno=%d\n",
1783 (long) tid, regno);
1785 /* General-purpose registers. */
1786 if (regno == -1
1787 || (tdep->ppc_gp0_regnum <= regno
1788 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1790 if (data->arch64)
1792 /* Pre-fetch: some regs may not be in the cache. */
1793 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1794 fill_gprs64 (regcache, gprs64);
1795 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1797 else
1799 /* Pre-fetch: some regs may not be in the cache. */
1800 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1801 fill_gprs32 (regcache, gprs32);
1802 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1806 /* Floating-point registers. */
1808 if (ppc_floating_point_unit_p (gdbarch)
1809 && (regno == -1
1810 || (regno >= tdep->ppc_fp0_regnum
1811 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1813 /* Pre-fetch: some regs may not be in the cache. */
1814 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1815 fill_fprs (regcache, fprs);
1816 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1819 /* Special-purpose registers. */
1821 if (regno == -1 || special_register_p (gdbarch, regno))
1823 if (data->arch64)
1825 /* Pre-fetch: some registers won't be in the cache. */
1826 ptrace64aix (PTT_READ_SPRS, tid,
1827 (unsigned long) &sprs64, 0, NULL);
1828 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1829 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1830 &sprs64.pt_xer, &sprs64.pt_fpscr);
1831 ptrace64aix (PTT_WRITE_SPRS, tid,
1832 (unsigned long) &sprs64, 0, NULL);
1834 else
1836 /* The contents of "struct ptspr" were declared as "unsigned
1837 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1838 Use temporaries to work around this problem. Also, add an
1839 assert here to make sure we fail if the system header files
1840 use "unsigned long", and the size of that type is not what
1841 the headers expect. */
1842 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1843 tmp_fpscr;
1845 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1847 /* Pre-fetch: some registers won't be in the cache. */
1848 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1850 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1851 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1853 sprs32.pt_iar = tmp_iar;
1854 sprs32.pt_msr = tmp_msr;
1855 sprs32.pt_cr = tmp_cr;
1856 sprs32.pt_lr = tmp_lr;
1857 sprs32.pt_ctr = tmp_ctr;
1858 sprs32.pt_xer = tmp_xer;
1859 sprs32.pt_fpscr = tmp_fpscr;
1861 if (tdep->ppc_mq_regnum >= 0)
1862 if (REG_VALID == regcache->get_register_status
1863 (tdep->ppc_mq_regnum))
1864 regcache->raw_collect (tdep->ppc_mq_regnum, &sprs32.pt_mq);
1866 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1870 /* Vector registers. */
1871 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1
1872 && (regno == -1 || (regno >= tdep->ppc_vr0_regnum
1873 && regno <= tdep->ppc_vrsave_regnum)))
1875 __vmx_context_t vmx;
1876 if (__power_vmx())
1878 if (data->arch64)
1879 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1880 else
1881 ret = ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1882 if (ret > 0)
1884 fill_altivec(regcache, &vmx);
1885 if (data->arch64)
1886 ret = ptrace64aix (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1887 else
1888 ret = ptrace32 (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1889 if (ret < 0)
1890 perror_with_name (_("Unable to store AltiVec register after read"));
1895 /* VSX registers. */
1896 if (tdep->ppc_vsr0_upper_regnum != -1 && (regno == -1
1897 || (regno >=tdep->ppc_vsr0_upper_regnum
1898 && regno < tdep->ppc_vsr0_upper_regnum + ppc_num_vshrs)))
1900 __vsx_context_t vsx;
1901 if (__power_vsx())
1903 if (data->arch64)
1904 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1905 else
1906 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1907 if (ret > 0)
1909 fill_vsx (regcache, &vsx);
1910 if (data->arch64)
1911 ret = ptrace64aix (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
1912 else
1913 ret = ptrace32 (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
1914 if (ret < 0)
1915 perror_with_name (_("Unable to store VSX register after read"));
1921 /* Store gdb's current view of the register set into the
1922 thread/process connected to REGCACHE. */
1924 void
1925 aix_thread_target::store_registers (struct regcache *regcache, int regno)
1927 struct thread_info *thread;
1928 pthdb_tid_t tid;
1930 if (regcache->ptid ().tid () == 0)
1931 beneath ()->store_registers (regcache, regno);
1932 else
1934 thread = current_inferior ()->find_thread (regcache->ptid ());
1935 aix_thread_info *priv = get_aix_thread_info (thread);
1936 tid = priv->tid;
1938 if (tid == PTHDB_INVALID_TID)
1939 store_regs_user_thread (regcache, priv->pdtid);
1940 else
1941 store_regs_kernel_thread (regcache, regno, tid);
1945 /* Implement the to_xfer_partial target_ops method. */
1947 enum target_xfer_status
1948 aix_thread_target::xfer_partial (enum target_object object,
1949 const char *annex, gdb_byte *readbuf,
1950 const gdb_byte *writebuf,
1951 ULONGEST offset, ULONGEST len,
1952 ULONGEST *xfered_len)
1954 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1956 inferior_ptid = ptid_t (inferior_ptid.pid ());
1957 return beneath ()->xfer_partial (object, annex, readbuf,
1958 writebuf, offset, len, xfered_len);
1961 /* Clean up after the inferior exits. */
1963 void
1964 aix_thread_target::mourn_inferior ()
1966 target_ops *beneath = this->beneath ();
1968 pd_disable (current_inferior ());
1969 beneath->mourn_inferior ();
1972 /* Return whether thread PID is still valid. */
1974 bool
1975 aix_thread_target::thread_alive (ptid_t ptid)
1977 if (ptid.tid () == 0)
1978 return beneath ()->thread_alive (ptid);
1980 /* We update the thread list every time the child stops, so all
1981 valid threads should be in the thread list. */
1982 process_stratum_target *proc_target
1983 = current_inferior ()->process_target ();
1984 return in_thread_list (proc_target, ptid);
1987 /* Return a printable representation of composite PID for use in
1988 "info threads" output. */
1990 std::string
1991 aix_thread_target::pid_to_str (ptid_t ptid)
1993 thread_info *thread_info = current_inferior ()->find_thread (ptid);
1995 if (thread_info != NULL && thread_info->priv != NULL)
1997 aix_thread_info *priv = get_aix_thread_info (thread_info);
1999 return string_printf (_("Thread %s (tid %s)"), pulongest (ptid.tid ()),
2000 pulongest (priv->tid));
2003 return beneath ()->pid_to_str (ptid);
2006 /* Return a printable representation of extra information about
2007 THREAD, for use in "info threads" output. */
2009 const char *
2010 aix_thread_target::extra_thread_info (struct thread_info *thread)
2012 int status;
2013 pthdb_pthread_t pdtid;
2014 pthdb_state_t state;
2015 pthdb_suspendstate_t suspendstate;
2016 pthdb_detachstate_t detachstate;
2017 int cancelpend;
2018 static char *ret = NULL;
2019 struct aix_thread_variables *data;
2021 data = get_thread_data_helper_for_ptid (thread->ptid);
2023 if (thread->ptid.tid () == 0)
2024 return NULL;
2026 string_file buf;
2027 aix_thread_info *priv = get_aix_thread_info (thread);
2029 pdtid = priv->pdtid;
2031 status = pthdb_pthread_state (data->pd_session, pdtid, &state);
2033 /* Output should look like Thread %d (tid %d) ([state]). */
2034 /* Example:- Thread 1 (tid 34144587) ([running]). */
2035 /* where state can be running, idle, sleeping, finished,
2036 suspended, detached, cancel pending, ready or unknown. */
2038 if (status != PTHDB_SUCCESS)
2039 state = PST_NOTSUP;
2040 buf.printf ("[%s]", state2str (state));
2042 status = pthdb_pthread_suspendstate (data->pd_session, pdtid,
2043 &suspendstate);
2044 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
2045 buf.printf (_("[suspended]"));
2047 status = pthdb_pthread_detachstate (data->pd_session, pdtid,
2048 &detachstate);
2049 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
2050 buf.printf (_("[detached]"));
2052 pthdb_pthread_cancelpend (data->pd_session, pdtid, &cancelpend);
2053 if (status == PTHDB_SUCCESS && cancelpend)
2054 buf.printf (_("[cancel pending]"));
2056 buf.write ("", 1);
2058 xfree (ret); /* Free old buffer. */
2060 ret = xstrdup (buf.c_str ());
2062 return ret;
2065 ptid_t
2066 aix_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
2068 return ptid_t (inferior_ptid.pid (), 0, thread);
2072 /* Module startup initialization function, automagically called by
2073 init.c. */
2075 void _initialize_aix_thread ();
2076 void
2077 _initialize_aix_thread ()
2079 /* Notice when object files get loaded and unloaded. */
2080 gdb::observers::new_objfile.attach (new_objfile, "aix-thread");
2082 /* Add ourselves to inferior_created event chain.
2083 This is needed to enable the thread target on "attach". */
2084 gdb::observers::inferior_created.attach (aix_thread_inferior_created,
2085 "aix-thread");
2087 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
2088 _("Set debugging of AIX thread module."),
2089 _("Show debugging of AIX thread module."),
2090 _("Enables debugging output (used to debug GDB)."),
2091 NULL, NULL,
2092 /* FIXME: i18n: Debugging of AIX thread
2093 module is \"%d\". */
2094 &setdebuglist, &showdebuglist);