arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / aix-thread.c
blob4a050cdff108086ca4bd685cb85836c6bbc71539
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 */
83 /* Return the aix_thread_info attached to THREAD. */
85 static aix_thread_info *
86 get_aix_thread_info (thread_info *thread)
88 return gdb::checked_static_cast<aix_thread_info *> (thread->priv.get ());
91 /* Information about a thread of which libpthdebug is aware. */
93 struct pd_thread {
94 pthdb_pthread_t pdtid;
95 pthread_t pthid;
96 pthdb_tid_t tid;
99 /* This module's target-specific operations, active while pd_able is true. */
101 static const target_info aix_thread_target_info = {
102 "aix-threads",
103 N_("AIX pthread support"),
104 N_("AIX pthread support")
107 class aix_thread_target final : public target_ops
109 public:
110 const target_info &info () const override
111 { return aix_thread_target_info; }
113 strata stratum () const override { return thread_stratum; }
115 void detach (inferior *, int) override;
116 void resume (ptid_t, int, enum gdb_signal) override;
117 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
119 void fetch_registers (struct regcache *, int) override;
120 void store_registers (struct regcache *, int) override;
122 enum target_xfer_status xfer_partial (enum target_object object,
123 const char *annex,
124 gdb_byte *readbuf,
125 const gdb_byte *writebuf,
126 ULONGEST offset, ULONGEST len,
127 ULONGEST *xfered_len) override;
129 void mourn_inferior () override;
131 bool thread_alive (ptid_t ptid) override;
133 std::string pid_to_str (ptid_t) override;
135 const char *extra_thread_info (struct thread_info *) override;
137 ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
139 void update_thread_list () override;
142 static aix_thread_target aix_thread_ops;
144 /* Forward declarations for pthdb callbacks. */
146 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
147 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
148 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
149 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
150 unsigned long long flags,
151 pthdb_context_t *context);
152 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
153 unsigned long long flags,
154 pthdb_context_t *context);
155 static int pdc_alloc (pthdb_user_t, size_t, void **);
156 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
157 static int pdc_dealloc (pthdb_user_t, void *);
159 /* pthdb callbacks. */
161 static pthdb_callbacks_t pd_callbacks = {
162 pdc_symbol_addrs,
163 pdc_read_data,
164 pdc_write_data,
165 pdc_read_regs,
166 pdc_write_regs,
167 pdc_alloc,
168 pdc_realloc,
169 pdc_dealloc,
170 NULL
173 /* Aix variable structure. */
174 struct aix_thread_variables
176 /* Whether the current application is debuggable by pthdb. */
177 int pd_able;
179 /* Whether a threaded application is being debugged. */
180 int pd_active;
182 /* Current pthdb session. */
183 pthdb_session_t pd_session;
185 /* Address of the function that libpthread will call when libpthdebug
186 is ready to be initialized. */
187 CORE_ADDR pd_brk_addr;
189 /* Whether the current architecture is 64-bit.
190 Only valid when pd_able is true. */
191 int arch64;
193 /* Describes the number of thread exit events reported. */
194 std::unordered_set<pthdb_pthread_t> exited_threads;
197 /* Key to our per-inferior data. */
198 static const registry<inferior>::key<aix_thread_variables>
199 aix_thread_variables_handle;
201 /* Function to Get aix_thread_variables data. */
202 static struct aix_thread_variables*
203 get_aix_thread_variables_data (struct inferior *inf)
205 if (inf == NULL)
206 return NULL;
208 struct aix_thread_variables* data;
210 data = aix_thread_variables_handle.get (inf);
211 if (data == NULL)
212 data = aix_thread_variables_handle.emplace (inf);
214 return data;
217 /* Helper to get data for ptid in a function. */
219 static struct aix_thread_variables*
220 get_thread_data_helper_for_ptid (ptid_t ptid)
222 inferior *inf = find_inferior_ptid (current_inferior ()->process_target (),
223 ptid);
224 return get_aix_thread_variables_data (inf);
227 /* Helper to get data for pid in a function. */
229 static struct aix_thread_variables*
230 get_thread_data_helper_for_pid (pid_t pid)
232 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
233 pid);
234 return get_aix_thread_variables_data (inf);
237 /* Return a printable representation of pthdebug function return
238 STATUS. */
240 static const char *
241 pd_status2str (int status)
243 switch (status)
245 case PTHDB_SUCCESS: return "SUCCESS";
246 case PTHDB_NOSYS: return "NOSYS";
247 case PTHDB_NOTSUP: return "NOTSUP";
248 case PTHDB_BAD_VERSION: return "BAD_VERSION";
249 case PTHDB_BAD_USER: return "BAD_USER";
250 case PTHDB_BAD_SESSION: return "BAD_SESSION";
251 case PTHDB_BAD_MODE: return "BAD_MODE";
252 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
253 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
254 case PTHDB_BAD_POINTER: return "BAD_POINTER";
255 case PTHDB_BAD_CMD: return "BAD_CMD";
256 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
257 case PTHDB_BAD_ATTR: return "BAD_ATTR";
258 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
259 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
260 case PTHDB_BAD_COND: return "BAD_COND";
261 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
262 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
263 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
264 case PTHDB_BAD_KEY: return "BAD_KEY";
265 case PTHDB_BAD_PTID: return "BAD_PTID";
266 case PTHDB_BAD_TID: return "BAD_TID";
267 case PTHDB_CALLBACK: return "CALLBACK";
268 case PTHDB_CONTEXT: return "CONTEXT";
269 case PTHDB_HELD: return "HELD";
270 case PTHDB_NOT_HELD: return "NOT_HELD";
271 case PTHDB_MEMORY: return "MEMORY";
272 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
273 case PTHDB_SYMBOL: return "SYMBOL";
274 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
275 case PTHDB_INTERNAL: return "INTERNAL";
276 default: return "UNKNOWN";
280 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
281 exceptional conditions and either return nonlocally or else return
282 1 for success and 0 for failure. */
284 static int
285 ptrace_check (int req, int id, int ret)
287 if (ret == 0 && !errno)
288 return 1;
290 /* According to ptrace(2), ptrace may fail with EPERM if "the
291 Identifier parameter corresponds to a kernel thread which is
292 stopped in kernel mode and whose computational state cannot be
293 read or written." This happens quite often with register reads. */
295 switch (req)
297 case PTT_READ_GPRS:
298 case PTT_READ_FPRS:
299 case PTT_READ_SPRS:
300 if (ret == -1 && errno == EPERM)
302 if (debug_aix_thread)
303 gdb_printf (gdb_stdlog,
304 "ptrace (%d, %d) = %d (errno = %d)\n",
305 req, id, ret, errno);
306 return ret == -1 ? 0 : 1;
308 break;
309 case PTT_READ_VEC:
310 case PTT_READ_VSX:
311 if (debug_aix_thread)
312 gdb_printf (gdb_stdlog,
313 "ptrace (%d, %d) = %d (errno = %d)\n",
314 req, id, ret, errno);
315 if (ret == -1)
316 return -1;
317 break;
319 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
320 req, id, ret, errno, safe_strerror (errno));
321 return 0; /* Not reached. */
324 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
325 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
326 Return success. */
328 #ifdef HAVE_PTRACE64
329 # define ptracex(request, pid, addr, data, buf) \
330 ptrace64 (request, pid, addr, data, buf)
331 #endif
333 static int
334 ptrace64aix (int req, int id, long long addr, int data, int *buf)
336 errno = 0;
337 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
340 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
341 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
342 Return success. */
344 #ifdef HAVE_PTRACE64
345 # define ptrace(request, pid, addr, data, buf) \
346 ptrace64 (request, pid, addr, data, buf)
347 # define addr_ptr long long
348 #else
349 # define addr_ptr int *
350 #endif
352 static int
353 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
355 errno = 0;
356 return ptrace_check (req, id,
357 ptrace (req, id, addr, data, buf));
360 /* If *PIDP is a composite process/thread id, convert it to a
361 process id. */
363 static void
364 pid_to_prc (ptid_t *ptidp)
366 ptid_t ptid;
368 ptid = *ptidp;
369 if (ptid.tid () != 0)
370 *ptidp = ptid_t (ptid.pid ());
373 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
374 the address of SYMBOLS[<i>].name. */
376 static int
377 pdc_symbol_addrs (pthdb_user_t user_current_pid, pthdb_symbol_t *symbols, int count)
379 int i;
380 char *name;
382 if (debug_aix_thread)
383 gdb_printf (gdb_stdlog,
384 "pdc_symbol_addrs (user_current_pid = %ld, symbols = 0x%lx, count = %d)\n",
385 user_current_pid, (long) symbols, count);
387 for (i = 0; i < count; i++)
389 name = symbols[i].name;
390 if (debug_aix_thread)
391 gdb_printf (gdb_stdlog,
392 " symbols[%d].name = \"%s\"\n", i, name);
394 if (!*name)
395 symbols[i].addr = 0;
396 else
398 bound_minimal_symbol ms
399 = lookup_minimal_symbol (current_program_space, name);
400 if (ms.minsym == NULL)
402 if (debug_aix_thread)
403 gdb_printf (gdb_stdlog, " returning PDC_FAILURE\n");
404 return PDC_FAILURE;
406 symbols[i].addr = ms.value_address ();
408 if (debug_aix_thread)
409 gdb_printf (gdb_stdlog, " symbols[%d].addr = %s\n",
410 i, hex_string (symbols[i].addr));
412 if (debug_aix_thread)
413 gdb_printf (gdb_stdlog, " returning PDC_SUCCESS\n");
414 return PDC_SUCCESS;
417 /* Read registers call back function should be able to read the
418 context information of a debuggee kernel thread from an active
419 process or from a core file. The information should be formatted
420 in context64 form for both 32-bit and 64-bit process.
421 If successful return 0, else non-zero is returned. */
423 static int
424 pdc_read_regs (pthdb_user_t user_current_pid,
425 pthdb_tid_t tid,
426 unsigned long long flags,
427 pthdb_context_t *context)
429 /* This function doesn't appear to be used, so we could probably
430 just return 0 here. HOWEVER, if it is not defined, the OS will
431 complain and several thread debug functions will fail. In case
432 this is needed, I have implemented what I think it should do,
433 however this code is untested. */
435 uint64_t gprs64[ppc_num_gprs];
436 uint32_t gprs32[ppc_num_gprs];
437 double fprs[ppc_num_fprs];
438 struct ptxsprs sprs64;
439 struct ptsprs sprs32;
440 struct aix_thread_variables *data;
442 data = get_thread_data_helper_for_pid (user_current_pid);
444 if (debug_aix_thread)
445 gdb_printf (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
446 (int) tid, hex_string (flags));
448 /* General-purpose registers. */
449 if (flags & PTHDB_FLAG_GPRS)
451 if (data->arch64)
453 if (!ptrace64aix (PTT_READ_GPRS, tid,
454 (unsigned long) gprs64, 0, NULL))
455 memset (gprs64, 0, sizeof (gprs64));
456 memcpy (context->gpr, gprs64, sizeof(gprs64));
458 else
460 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
461 memset (gprs32, 0, sizeof (gprs32));
462 memcpy (context->gpr, gprs32, sizeof(gprs32));
466 /* Floating-point registers. */
467 if (flags & PTHDB_FLAG_FPRS)
469 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
470 memset (fprs, 0, sizeof (fprs));
471 memcpy (context->fpr, fprs, sizeof(fprs));
474 /* Special-purpose registers. */
475 if (flags & PTHDB_FLAG_SPRS)
477 if (data->arch64)
479 if (!ptrace64aix (PTT_READ_SPRS, tid,
480 (unsigned long) &sprs64, 0, NULL))
481 memset (&sprs64, 0, sizeof (sprs64));
482 memcpy (&context->msr, &sprs64, sizeof(sprs64));
484 else
486 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
487 memset (&sprs32, 0, sizeof (sprs32));
488 memcpy (&context->msr, &sprs32, sizeof(sprs32));
492 /* vector registers. */
493 __vmx_context_t vmx;
494 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
496 if (data->arch64)
498 if (!ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
499 memset (&vmx, 0, sizeof (vmx));
500 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
502 else
504 if (!ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
505 memset (&vmx, 0, sizeof (vmx));
506 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
510 /* vsx registers. */
511 __vsx_context_t vsx;
512 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
514 if (data->arch64)
516 if (!ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
517 memset (&vsx, 0, sizeof (vsx));
518 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
520 else
522 if (!ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
523 memset (&vsx, 0, sizeof (vsx));
524 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
527 return 0;
530 /* Write register function should be able to write requested context
531 information to specified debuggee's kernel thread id.
532 If successful return 0, else non-zero is returned. */
534 static int
535 pdc_write_regs (pthdb_user_t user_current_pid,
536 pthdb_tid_t tid,
537 unsigned long long flags,
538 pthdb_context_t *context)
540 /* This function doesn't appear to be used, so we could probably
541 just return 0 here. HOWEVER, if it is not defined, the OS will
542 complain and several thread debug functions will fail. In case
543 this is needed, I have implemented what I think it should do,
544 however this code is untested. */
546 struct aix_thread_variables *data;
548 data = get_thread_data_helper_for_pid (user_current_pid);
550 if (debug_aix_thread)
551 gdb_printf (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
552 (int) tid, hex_string (flags));
554 /* General-purpose registers. */
555 if (flags & PTHDB_FLAG_GPRS)
557 if (data->arch64)
558 ptrace64aix (PTT_WRITE_GPRS, tid,
559 (unsigned long) context->gpr, 0, NULL);
560 else
561 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
564 /* Floating-point registers. */
565 if (flags & PTHDB_FLAG_FPRS)
567 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
570 /* Special-purpose registers. */
571 if (flags & PTHDB_FLAG_SPRS)
573 if (data->arch64)
575 ptrace64aix (PTT_WRITE_SPRS, tid,
576 (unsigned long) &context->msr, 0, NULL);
578 else
580 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
584 /* vector registers. */
585 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
587 if (data->arch64)
588 ptrace64aix (PTT_WRITE_VEC, tid, (unsigned long) &context->vmx, 0, 0);
589 else
590 ptrace32 (PTT_WRITE_VEC, tid, (uintptr_t) &context->vmx, 0, 0);
593 /* vsx registers. */
594 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
596 if (data->arch64)
597 ptrace64aix (PTT_WRITE_VSX, tid, (unsigned long) &context->vsx, 0, 0);
598 else
599 ptrace32 (PTT_WRITE_VSX, tid, (uintptr_t) &context->vsx, 0, 0);
601 return 0;
604 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
606 static int
607 pdc_read_data (pthdb_user_t user_current_pid, void *buf,
608 pthdb_addr_t addr, size_t len)
610 int status, ret;
611 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
612 user_current_pid);
614 if (debug_aix_thread)
615 gdb_printf (gdb_stdlog,
616 "pdc_read_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
617 user_current_pid, (long) buf, hex_string (addr), len);
619 /* This is needed to eliminate the dependency of current thread
620 which is null so that thread reads the correct target memory. */
622 scoped_restore_current_inferior_for_memory save_inferior (inf);
623 status = target_read_memory (addr, (gdb_byte *) buf, len);
625 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
627 if (debug_aix_thread)
628 gdb_printf (gdb_stdlog, " status=%d, returning %s\n",
629 status, pd_status2str (ret));
630 return ret;
633 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
635 static int
636 pdc_write_data (pthdb_user_t user_current_pid, void *buf,
637 pthdb_addr_t addr, size_t len)
639 int status, ret;
640 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
641 user_current_pid);
643 if (debug_aix_thread)
644 gdb_printf (gdb_stdlog,
645 "pdc_write_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
646 user_current_pid, (long) buf, hex_string (addr), len);
649 scoped_restore_current_inferior_for_memory save_inferior (inf);
650 status = target_write_memory (addr, (gdb_byte *) buf, len);
653 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
655 if (debug_aix_thread)
656 gdb_printf (gdb_stdlog, " status=%d, returning %s\n", status,
657 pd_status2str (ret));
658 return ret;
661 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
662 in BUFP. */
664 static int
665 pdc_alloc (pthdb_user_t user_current_pid, size_t len, void **bufp)
667 if (debug_aix_thread)
668 gdb_printf (gdb_stdlog,
669 "pdc_alloc (user_current_pid = %ld, len = %ld, bufp = 0x%lx)\n",
670 user_current_pid, len, (long) bufp);
671 *bufp = xmalloc (len);
672 if (debug_aix_thread)
673 gdb_printf (gdb_stdlog,
674 " malloc returned 0x%lx\n", (long) *bufp);
676 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
677 be returned. */
679 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
682 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
683 realloc callback, so that it contains LEN bytes, and store a
684 pointer to the result in BUFP. */
686 static int
687 pdc_realloc (pthdb_user_t user_current_pid, void *buf, size_t len, void **bufp)
689 if (debug_aix_thread)
690 gdb_printf (gdb_stdlog,
691 "pdc_realloc (user_current_pid = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
692 user_current_pid, (long) buf, len, (long) bufp);
693 *bufp = xrealloc (buf, len);
694 if (debug_aix_thread)
695 gdb_printf (gdb_stdlog,
696 " realloc returned 0x%lx\n", (long) *bufp);
697 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
700 /* pthdb callback: free BUF, which was allocated by the alloc or
701 realloc callback. */
703 static int
704 pdc_dealloc (pthdb_user_t user_current_pid, void *buf)
706 if (debug_aix_thread)
707 gdb_printf (gdb_stdlog,
708 "pdc_free (user_current_pid = %ld, buf = 0x%lx)\n", user_current_pid,
709 (long) buf);
710 xfree (buf);
711 return PDC_SUCCESS;
714 /* Return a printable representation of pthread STATE. */
716 static char *
717 state2str (pthdb_state_t state)
719 switch (state)
721 case PST_IDLE:
722 /* i18n: Like "Thread-Id %d, [state] idle" */
723 return _("idle"); /* being created */
724 case PST_RUN:
725 /* i18n: Like "Thread-Id %d, [state] running" */
726 return _("running"); /* running */
727 case PST_SLEEP:
728 /* i18n: Like "Thread-Id %d, [state] sleeping" */
729 return _("sleeping"); /* awaiting an event */
730 case PST_READY:
731 /* i18n: Like "Thread-Id %d, [state] ready" */
732 return _("ready"); /* runnable */
733 case PST_TERM:
734 /* i18n: Like "Thread-Id %d, [state] finished" */
735 return _("finished"); /* awaiting a join/detach */
736 default:
737 /* i18n: Like "Thread-Id %d, [state] unknown" */
738 return _("unknown");
742 /* Search through the list of all kernel threads for the thread
743 that has stopped on a SIGTRAP signal, and return its TID.
744 Return 0 if none found. */
746 static pthdb_tid_t
747 get_signaled_thread (int pid)
749 struct thrdsinfo64 thrinf;
750 tid_t ktid = 0;
752 while (1)
754 if (getthrds (pid, &thrinf,
755 sizeof (thrinf), &ktid, 1) != 1)
756 break;
758 /* We also need to keep in mind Trap and interrupt or any
759 signal that needs to be handled in pd_update (). */
761 if (thrinf.ti_cursig)
762 return thrinf.ti_tid;
765 /* Didn't find any thread stopped on a SIGTRAP signal. */
766 return 0;
769 /* Synchronize GDB's thread list with libpthdebug's.
771 There are some benefits of doing this every time the inferior stops:
773 - allows users to run thread-specific commands without needing to
774 run "info threads" first
776 - helps pthdb_tid_pthread() work properly (see "libpthdebug
777 peculiarities" at the top of this module)
779 - simplifies the demands placed on libpthdebug, which seems to
780 have difficulty with certain call patterns */
782 static void
783 sync_threadlists (pid_t pid)
785 int cmd, status;
786 pthdb_pthread_t pdtid;
787 pthread_t pthid;
788 pthdb_tid_t tid;
789 process_stratum_target *proc_target = current_inferior ()->process_target ();
790 struct aix_thread_variables *data;
791 data = get_thread_data_helper_for_pid (pid);
792 pthdb_state_t state;
793 std::set<pthdb_pthread_t> in_queue_threads;
795 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
797 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
799 status = pthdb_pthread (data->pd_session, &pdtid, cmd);
800 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
801 break;
803 status = pthdb_pthread_ptid (data->pd_session, pdtid, &pthid);
804 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
805 continue;
807 status = pthdb_pthread_tid (data->pd_session, pdtid, &tid);
808 ptid_t ptid (pid, tid, pthid);
810 status = pthdb_pthread_state (data->pd_session, pdtid, &state);
811 in_queue_threads.insert (pdtid);
813 /* If this thread has reported and exited, do not add it again. */
814 if (state == PST_TERM)
816 if (data->exited_threads.count (pdtid) != 0)
817 continue;
820 /* If this thread has never been reported to GDB, add it. */
821 if (!in_thread_list (proc_target, ptid))
823 aix_thread_info *priv = new aix_thread_info;
824 /* init priv */
825 priv->pdtid = pdtid;
826 /* Check if this is the main thread. If it is, then change
827 its ptid and add its private data. */
828 if (in_thread_list (proc_target, ptid_t (pid)))
830 thread_info *tp = proc_target->find_thread (ptid_t (pid));
831 thread_change_ptid (proc_target, ptid_t (pid), ptid);
832 tp->priv.reset (priv);
834 else
835 add_thread_with_info (proc_target, ptid,
836 private_thread_info_up (priv));
839 /* The thread is terminated. Remove it. */
840 if (state == PST_TERM)
842 thread_info *thr = proc_target->find_thread (ptid);
843 gdb_assert (thr != nullptr);
844 delete_thread (thr);
845 data->exited_threads.insert (pdtid);
849 /* Sometimes there can be scenarios where the thread status is
850 unknown and we it will never iterate in the for loop above,
851 since cmd will be no longer be pointing to that threads. One
852 such scenario is the gdb.threads/thread_events.exp testcase
853 where in the end after the threadfunc breakpoint is hit, the
854 thread exits and gets into a PST_UNKNOWN state. So this thread
855 will not run in the above for loop. Therefore the below for loop
856 is to manually delete such threads. */
857 for (struct thread_info *it : all_threads_safe ())
859 aix_thread_info *priv = get_aix_thread_info (it);
860 if (in_queue_threads.count (priv->pdtid) == 0
861 && in_thread_list (proc_target, it->ptid)
862 && pid == it->ptid.pid ())
864 delete_thread (it);
865 data->exited_threads.insert (priv->pdtid);
870 /* Iterate_over_threads() callback for locating a thread, using
871 the TID of its associated kernel thread. */
873 static int
874 iter_tid (struct thread_info *thread, void *tidp)
876 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
877 return thread->ptid.lwp () == tid;
880 /* Synchronize libpthdebug's state with the inferior and with GDB,
881 generate a composite process/thread <pid> for the current thread,
882 Return the ptid of the event thread if one can be found, else
883 return a pid-only ptid with PID. */
885 static ptid_t
886 pd_update (pid_t pid)
888 int status;
889 ptid_t ptid;
890 pthdb_tid_t tid;
891 struct thread_info *thread = NULL;
892 struct aix_thread_variables *data;
894 data = get_thread_data_helper_for_pid (pid);
896 if (!data->pd_active)
897 return ptid_t (pid);
899 status = pthdb_session_update (data->pd_session);
900 if (status != PTHDB_SUCCESS)
901 return ptid_t (pid);
903 sync_threadlists (pid);
905 /* Define "current thread" as one that just received a trap signal. */
907 tid = get_signaled_thread (pid);
908 if (tid != 0)
909 thread = iterate_over_threads (iter_tid, &tid);
910 if (!thread)
911 ptid = ptid_t (pid);
912 else
913 ptid = thread->ptid;
915 return ptid;
918 /* Try to start debugging threads in the current process.
919 If successful and there exists and we can find an event thread, set
920 pd_active for that thread. Otherwise, return. */
922 static void
923 pd_activate (pid_t pid)
925 int status;
926 struct aix_thread_variables *data;
927 data = get_thread_data_helper_for_pid (pid);
929 status = pthdb_session_init (pid, data->arch64 ? PEM_64BIT : PEM_32BIT,
930 PTHDB_FLAG_REGS, &pd_callbacks,
931 &data->pd_session);
932 if (status == PTHDB_SUCCESS)
933 data->pd_active = 1;
936 /* AIX implementation of update_thread_list. */
938 void
939 aix_thread_target::update_thread_list ()
941 for (inferior *inf : all_inferiors ())
943 if (inf->pid == 0)
944 continue;
946 pd_update (inf->pid);
951 /* An object file has just been loaded. Check whether the current
952 application is pthreaded, and if so, prepare for thread debugging. */
954 static void
955 pd_enable (inferior *inf)
957 int status;
958 char *stub_name;
959 struct aix_thread_variables *data;
961 if (inf == NULL)
962 return;
964 data = get_aix_thread_variables_data (inf);
966 /* Don't initialize twice. */
967 if (data->pd_able)
968 return;
970 /* Check application word size. */
971 data->arch64 = register_size (current_inferior ()->arch (), 0) == 8;
973 /* Check whether the application is pthreaded. */
974 stub_name = NULL;
975 status = pthdb_session_pthreaded (inf->pid, PTHDB_FLAG_REGS,
976 &pd_callbacks, &stub_name);
977 if ((status != PTHDB_SUCCESS
978 && status != PTHDB_NOT_PTHREADED) || !stub_name)
979 return;
981 /* Set a breakpoint on the returned stub function. */
982 bound_minimal_symbol ms
983 = lookup_minimal_symbol (current_program_space, stub_name);
984 if (ms.minsym == NULL)
985 return;
986 data->pd_brk_addr = ms.value_address ();
987 if (!create_thread_event_breakpoint (current_inferior ()->arch (),
988 data->pd_brk_addr))
989 return;
991 /* Prepare for thread debugging. */
992 current_inferior ()->push_target (&aix_thread_ops);
993 data->pd_able = 1;
995 /* If we're debugging a core file or an attached inferior, the
996 pthread library may already have been initialized, so try to
997 activate thread debugging. */
998 pd_activate (inf->pid);
1001 /* Undo the effects of pd_enable(). */
1003 static void
1004 pd_disable (inferior *inf)
1006 struct aix_thread_variables *data;
1007 data = get_aix_thread_variables_data (inf);
1009 if (!data->pd_able)
1010 return;
1011 if (!data->pd_active)
1012 return;
1013 pthdb_session_destroy (data->pd_session);
1015 pid_to_prc (&inferior_ptid);
1016 data->pd_active = 0;
1017 data->pd_able = 0;
1018 current_inferior ()->unpush_target (&aix_thread_ops);
1021 /* new_objfile observer callback.
1023 Check whether a threaded application is being debugged, and if so, prepare
1024 for thread debugging. */
1026 static void
1027 new_objfile (struct objfile *objfile)
1029 pd_enable (current_inferior ());
1032 /* Attach to process specified by ARGS. */
1034 static void
1035 aix_thread_inferior_created (inferior *inf)
1037 pd_enable (inf);
1040 /* Detach from the process attached to by aix_thread_attach(). */
1042 void
1043 aix_thread_target::detach (inferior *inf, int from_tty)
1045 target_ops *beneath = this->beneath ();
1047 pd_disable (inf);
1048 beneath->detach (inf, from_tty);
1051 /* Tell the inferior process to continue running thread PID if != -1
1052 and all threads otherwise. */
1054 void
1055 aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1057 struct thread_info *thread;
1058 pthdb_tid_t tid[2];
1059 struct aix_thread_variables *data;
1061 data = get_thread_data_helper_for_ptid (ptid);
1063 if (ptid.tid () == 0)
1065 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1067 inferior_ptid = ptid_t (inferior_ptid.pid ());
1068 beneath ()->resume (ptid, step, sig);
1070 else
1072 thread = current_inferior ()->find_thread (ptid);
1073 if (!thread)
1074 error (_("aix-thread resume: unknown pthread %ld"),
1075 ptid.lwp ());
1077 aix_thread_info *priv = get_aix_thread_info (thread);
1079 tid[0] = ptid.lwp ();
1080 if (tid[0] == PTHDB_INVALID_TID)
1081 error (_("aix-thread resume: no tid for pthread %ld"),
1082 ptid.lwp ());
1083 tid[1] = 0;
1085 if (data->arch64)
1086 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1087 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1088 else
1089 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1090 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1094 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1095 If an error occurs, return -1, else return the pid of the stopped
1096 thread. */
1098 ptid_t
1099 aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1100 target_wait_flags options)
1102 struct aix_thread_variables *data;
1104 pid_to_prc (&ptid);
1106 ptid = beneath ()->wait (ptid, status, options);
1109 if (ptid.pid () == -1)
1110 return ptid_t (-1);
1112 /* The target beneath does not deal with threads, so it should only return
1113 pid-only ptids. */
1114 gdb_assert (ptid.is_pid ());
1116 data = get_thread_data_helper_for_ptid (ptid);
1118 /* Check whether libpthdebug might be ready to be initialized. */
1119 if (!data->pd_active && status->kind () == TARGET_WAITKIND_STOPPED
1120 && status->sig () == GDB_SIGNAL_TRAP)
1122 process_stratum_target *proc_target
1123 = current_inferior ()->process_target ();
1124 struct regcache *regcache = get_thread_regcache (proc_target, ptid);
1125 struct gdbarch *gdbarch = regcache->arch ();
1127 if (regcache_read_pc (regcache)
1128 - gdbarch_decr_pc_after_break (gdbarch) == data->pd_brk_addr)
1129 pd_activate (ptid.pid ());
1132 return pd_update (ptid.pid ());
1135 /* Supply AIX altivec registers, both 64 and 32 bit. */
1137 static void
1138 supply_altivec_regs (struct regcache *regcache, __vmx_context_t vmx)
1140 ppc_gdbarch_tdep *tdep
1141 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1142 int regno;
1143 for (regno = 0; regno < ppc_num_vrs; regno++)
1144 regcache->raw_supply (tdep->ppc_vr0_regnum + regno,
1145 &(vmx.__vr[regno]));
1146 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1147 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1150 /* Supply AIX VSX registers, both 64 and 32 bit. */
1152 static void
1153 supply_vsx_regs (struct regcache *regcache, __vsx_context_t vsx)
1155 ppc_gdbarch_tdep *tdep
1156 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1157 int regno;
1159 for (regno = 0; regno < ppc_num_vshrs; regno++)
1160 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + regno,
1161 &(vsx.__vsr_dw1[regno]));
1164 /* Record that the 64-bit general-purpose registers contain VALS. */
1166 static void
1167 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1169 ppc_gdbarch_tdep *tdep
1170 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1171 int regno;
1173 for (regno = 0; regno < ppc_num_gprs; regno++)
1174 regcache->raw_supply (tdep->ppc_gp0_regnum + regno,
1175 (char *) (vals + regno));
1178 /* Record that 32-bit register REGNO contains VAL. */
1180 static void
1181 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1183 regcache->raw_supply (regno, (char *) &val);
1186 /* Record that the floating-point registers contain VALS. */
1188 static void
1189 supply_fprs (struct regcache *regcache, double *vals)
1191 struct gdbarch *gdbarch = regcache->arch ();
1192 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1193 int regno;
1195 /* This function should never be called on architectures without
1196 floating-point registers. */
1197 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1199 for (regno = tdep->ppc_fp0_regnum;
1200 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1201 regno++)
1202 regcache->raw_supply (regno,
1203 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1206 /* Predicate to test whether given register number is a "special" register. */
1207 static int
1208 special_register_p (struct gdbarch *gdbarch, int regno)
1210 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1212 return regno == gdbarch_pc_regnum (gdbarch)
1213 || regno == tdep->ppc_ps_regnum
1214 || regno == tdep->ppc_cr_regnum
1215 || regno == tdep->ppc_lr_regnum
1216 || regno == tdep->ppc_ctr_regnum
1217 || regno == tdep->ppc_xer_regnum
1218 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1219 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1223 /* Record that the special registers contain the specified 64-bit and
1224 32-bit values. */
1226 static void
1227 supply_sprs64 (struct regcache *regcache,
1228 uint64_t iar, uint64_t msr, uint32_t cr,
1229 uint64_t lr, uint64_t ctr, uint32_t xer,
1230 uint32_t fpscr)
1232 struct gdbarch *gdbarch = regcache->arch ();
1233 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1235 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1236 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1237 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1238 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1239 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1240 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1241 if (tdep->ppc_fpscr_regnum >= 0)
1242 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1245 /* Record that the special registers contain the specified 32-bit
1246 values. */
1248 static void
1249 supply_sprs32 (struct regcache *regcache,
1250 uint32_t iar, uint32_t msr, uint32_t cr,
1251 uint32_t lr, uint32_t ctr, uint32_t xer,
1252 uint32_t fpscr)
1254 struct gdbarch *gdbarch = regcache->arch ();
1255 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1257 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1258 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1259 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1260 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1261 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1262 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1263 if (tdep->ppc_fpscr_regnum >= 0)
1264 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1267 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1268 thread.
1270 There's no way to query a single register from a non-kernel
1271 pthread, so there's no need for a single-register version of this
1272 function. */
1274 static void
1275 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1277 struct gdbarch *gdbarch = regcache->arch ();
1278 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1279 int status, i;
1280 pthdb_context_t ctx;
1281 struct aix_thread_variables *data;
1282 data = get_thread_data_helper_for_ptid (inferior_ptid);
1284 if (debug_aix_thread)
1285 gdb_printf (gdb_stdlog,
1286 "fetch_regs_user_thread %lx\n", (long) pdtid);
1287 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1288 if (status != PTHDB_SUCCESS)
1289 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1290 pd_status2str (status));
1292 /* General-purpose registers. */
1294 if (data->arch64)
1295 supply_gprs64 (regcache, ctx.gpr);
1296 else
1297 for (i = 0; i < ppc_num_gprs; i++)
1298 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1300 /* Floating-point registers. */
1302 if (ppc_floating_point_unit_p (gdbarch))
1303 supply_fprs (regcache, ctx.fpr);
1305 /* Special registers. */
1307 if (data->arch64)
1308 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1309 ctx.xer, ctx.fpscr);
1310 else
1311 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1312 ctx.xer, ctx.fpscr);
1314 /* Altivec registers. */
1315 supply_altivec_regs (regcache, ctx.vmx);
1317 /* VSX registers. */
1318 supply_vsx_regs (regcache, ctx.vsx);
1321 /* Fetch register REGNO if != -1 or all registers otherwise from
1322 kernel thread TID.
1324 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1325 SPRs, but there's no way to query individual registers within those
1326 groups. Therefore, if REGNO != -1, this function fetches an entire
1327 group.
1329 Unfortunately, kernel thread register queries often fail with
1330 EPERM, indicating that the thread is in kernel space. This breaks
1331 backtraces of threads other than the current one. To make that
1332 breakage obvious without throwing an error to top level (which is
1333 bad e.g. during "info threads" output), zero registers that can't
1334 be retrieved. */
1336 static void
1337 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1338 pthdb_tid_t tid)
1340 struct gdbarch *gdbarch = regcache->arch ();
1341 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1342 uint64_t gprs64[ppc_num_gprs];
1343 uint32_t gprs32[ppc_num_gprs];
1344 double fprs[ppc_num_fprs];
1345 struct ptxsprs sprs64;
1346 struct ptsprs sprs32;
1347 int i;
1348 struct aix_thread_variables *data;
1350 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1352 if (debug_aix_thread)
1353 gdb_printf (gdb_stdlog,
1354 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1355 (long) tid, regno, data->arch64);
1357 /* General-purpose registers. */
1358 if (regno == -1
1359 || (tdep->ppc_gp0_regnum <= regno
1360 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1362 if (data->arch64)
1364 if (!ptrace64aix (PTT_READ_GPRS, tid,
1365 (unsigned long) gprs64, 0, NULL))
1366 memset (gprs64, 0, sizeof (gprs64));
1367 supply_gprs64 (regcache, gprs64);
1369 else
1371 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1372 memset (gprs32, 0, sizeof (gprs32));
1373 for (i = 0; i < ppc_num_gprs; i++)
1374 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1378 /* vector registers. */
1379 if (tdep->ppc_vr0_regnum != -1)
1381 int ret = 0;
1382 __vmx_context_t vmx;
1383 if (data->arch64)
1384 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1385 else
1386 ret = ptrace32 (PTT_READ_VEC, tid, (uintptr_t) &vmx, 0, 0);
1387 if (ret < 0)
1388 memset(&vmx, 0, sizeof(__vmx_context_t));
1389 for (i = 0; i < ppc_num_vrs; i++)
1390 regcache->raw_supply (tdep->ppc_vr0_regnum + i, &(vmx.__vr[i]));
1391 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1392 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1395 /* vsx registers. */
1396 if (tdep->ppc_vsr0_upper_regnum != -1)
1398 __vsx_context_t vsx;
1399 int ret = 0;
1400 if (data->arch64)
1401 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1402 else
1403 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1404 if (ret < 0)
1405 memset(&vsx, 0, sizeof(__vsx_context_t));
1406 for (i = 0; i < ppc_num_vshrs; i++)
1407 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i, &(vsx.__vsr_dw1[i]));
1410 /* Floating-point registers. */
1412 if (ppc_floating_point_unit_p (gdbarch)
1413 && (regno == -1
1414 || (regno >= tdep->ppc_fp0_regnum
1415 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1417 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1418 memset (fprs, 0, sizeof (fprs));
1419 supply_fprs (regcache, fprs);
1422 /* Special-purpose registers. */
1424 if (regno == -1 || special_register_p (gdbarch, regno))
1426 if (data->arch64)
1428 if (!ptrace64aix (PTT_READ_SPRS, tid,
1429 (unsigned long) &sprs64, 0, NULL))
1430 memset (&sprs64, 0, sizeof (sprs64));
1431 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1432 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1433 sprs64.pt_xer, sprs64.pt_fpscr);
1435 else
1437 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1438 memset (&sprs32, 0, sizeof (sprs32));
1439 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1440 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1441 sprs32.pt_fpscr);
1443 if (tdep->ppc_mq_regnum >= 0)
1444 regcache->raw_supply (tdep->ppc_mq_regnum, (char *) &sprs32.pt_mq);
1449 /* Fetch register REGNO if != -1 or all registers otherwise from the
1450 thread/process connected to REGCACHE. */
1452 void
1453 aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
1455 struct thread_info *thread;
1456 pthdb_tid_t tid;
1458 /* If a new inferior is born, then its pthread debug library is yet to
1459 initialised and hence has no private data. So the below if condition
1460 exists. */
1462 if (regcache->ptid ().tid () == 0)
1463 beneath ()->fetch_registers (regcache, regno);
1464 else
1466 thread = current_inferior ()->find_thread (regcache->ptid ());
1467 aix_thread_info *priv = get_aix_thread_info (thread);
1468 tid = regcache->ptid().lwp ();
1470 if (tid == PTHDB_INVALID_TID)
1471 fetch_regs_user_thread (regcache, priv->pdtid);
1472 else
1473 fetch_regs_kernel_thread (regcache, regno, tid);
1477 /* Fill altivec registers. */
1479 static void
1480 fill_altivec (const struct regcache *regcache, __vmx_context_t *vmx)
1482 struct gdbarch *gdbarch = regcache->arch ();
1483 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1484 int regno;
1486 for (regno = 0; regno < ppc_num_vrs; regno++)
1487 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + regno))
1488 regcache->raw_collect (tdep->ppc_vr0_regnum + regno,
1489 &(vmx->__vr[regno]));
1491 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1492 regcache->raw_collect (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
1493 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1494 regcache->raw_collect (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
1497 /* Fill vsx registers. */
1499 static void
1500 fill_vsx (const struct regcache *regcache, __vsx_context_t *vsx)
1502 struct gdbarch *gdbarch = regcache->arch ();
1503 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1504 int regno;
1506 for (regno = 0; regno < ppc_num_vshrs; regno++)
1507 if (REG_VALID == regcache->get_register_status ( tdep->ppc_vsr0_upper_regnum + regno))
1508 regcache->raw_collect (tdep->ppc_vsr0_upper_regnum + regno,
1509 &(vsx->__vsr_dw1[0]) + regno);
1512 /* Store the gp registers into an array of uint32_t or uint64_t. */
1514 static void
1515 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1517 ppc_gdbarch_tdep *tdep
1518 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1519 int regno;
1521 for (regno = 0; regno < ppc_num_gprs; regno++)
1522 if (REG_VALID == regcache->get_register_status
1523 (tdep->ppc_gp0_regnum + regno))
1524 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1527 static void
1528 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1530 ppc_gdbarch_tdep *tdep
1531 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1532 int regno;
1534 for (regno = 0; regno < ppc_num_gprs; regno++)
1535 if (REG_VALID == regcache->get_register_status
1536 (tdep->ppc_gp0_regnum + regno))
1537 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1540 /* Store the floating point registers into a double array. */
1541 static void
1542 fill_fprs (const struct regcache *regcache, double *vals)
1544 struct gdbarch *gdbarch = regcache->arch ();
1545 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1546 int regno;
1548 /* This function should never be called on architectures without
1549 floating-point registers. */
1550 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1552 for (regno = tdep->ppc_fp0_regnum;
1553 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1554 regno++)
1555 if (REG_VALID == regcache->get_register_status (regno))
1556 regcache->raw_collect (regno, vals + regno - tdep->ppc_fp0_regnum);
1559 /* Store the special registers into the specified 64-bit and 32-bit
1560 locations. */
1562 static void
1563 fill_sprs64 (const struct regcache *regcache,
1564 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1565 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1566 uint32_t *fpscr)
1568 struct gdbarch *gdbarch = regcache->arch ();
1569 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1571 /* Verify that the size of the size of the IAR buffer is the
1572 same as the raw size of the PC (in the register cache). If
1573 they're not, then either GDB has been built incorrectly, or
1574 there's some other kind of internal error. To be really safe,
1575 we should check all of the sizes. */
1576 gdb_assert (sizeof (*iar) == register_size
1577 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1579 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1580 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1581 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1582 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1583 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1584 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1585 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1586 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1587 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1588 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1589 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1590 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1591 if (tdep->ppc_fpscr_regnum >= 0
1592 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1593 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1596 static void
1597 fill_sprs32 (const struct regcache *regcache,
1598 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1599 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1600 uint32_t *fpscr)
1602 struct gdbarch *gdbarch = regcache->arch ();
1603 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1605 /* Verify that the size of the size of the IAR buffer is the
1606 same as the raw size of the PC (in the register cache). If
1607 they're not, then either GDB has been built incorrectly, or
1608 there's some other kind of internal error. To be really safe,
1609 we should check all of the sizes. */
1610 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1611 gdbarch_pc_regnum (gdbarch)));
1613 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1614 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1615 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1616 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1617 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1618 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1619 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1620 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1621 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1622 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1623 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1624 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1625 if (tdep->ppc_fpscr_regnum >= 0
1626 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1627 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1630 /* Store all registers into pthread PDTID, which doesn't have a kernel
1631 thread.
1633 It's possible to store a single register into a non-kernel pthread,
1634 but I doubt it's worth the effort. */
1636 static void
1637 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1639 struct gdbarch *gdbarch = regcache->arch ();
1640 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1641 int status, i;
1642 pthdb_context_t ctx;
1643 uint32_t int32;
1644 uint64_t int64;
1645 struct aix_thread_variables *data;
1646 data = get_thread_data_helper_for_ptid (inferior_ptid);
1647 __vmx_context_t vmx;
1648 __vsx_context_t vsx;
1650 if (debug_aix_thread)
1651 gdb_printf (gdb_stdlog,
1652 "store_regs_user_thread %lx\n", (long) pdtid);
1654 /* Retrieve the thread's current context for its non-register
1655 values. */
1656 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1657 if (status != PTHDB_SUCCESS)
1658 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1659 pd_status2str (status));
1661 /* Fill altivec-registers. */
1663 if (__power_vmx())
1665 memset(&vmx, 0, sizeof(__vmx_context_t));
1666 for (i = 0; i < ppc_num_vrs; i++)
1667 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + i))
1669 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1670 &(vmx.__vr[i]));
1671 ctx.vmx.__vr[i] = vmx.__vr[i];
1673 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1674 ctx.vmx.__vrsave = vmx.__vrsave;
1675 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1676 ctx.vmx.__vscr = vmx.__vscr;
1679 /* Fill vsx registers. */
1681 if (__power_vsx())
1683 memset(&vsx, 0, sizeof(__vsx_context_t));
1684 for (i = 0; i < ppc_num_vshrs; i++)
1685 if (REG_VALID == regcache->get_register_status (tdep->ppc_vsr0_regnum + i))
1687 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1688 &(vsx.__vsr_dw1[i]));
1689 ctx.vsx.__vsr_dw1[i] = vsx.__vsr_dw1[i];
1693 /* Collect general-purpose register values from the regcache. */
1695 for (i = 0; i < ppc_num_gprs; i++)
1696 if (REG_VALID == regcache->get_register_status (tdep->ppc_gp0_regnum + i))
1698 if (data->arch64)
1700 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int64);
1701 ctx.gpr[i] = int64;
1703 else
1705 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int32);
1706 ctx.gpr[i] = int32;
1710 /* Collect floating-point register values from the regcache. */
1711 if (ppc_floating_point_unit_p (gdbarch))
1712 fill_fprs (regcache, ctx.fpr);
1714 /* Special registers (always kept in ctx as 64 bits). */
1715 if (data->arch64)
1717 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1718 &ctx.xer, &ctx.fpscr);
1720 else
1722 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1723 Solution: use 32-bit temp variables. */
1724 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1725 tmp_fpscr;
1727 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1728 &tmp_xer, &tmp_fpscr);
1729 if (REG_VALID == regcache->get_register_status
1730 (gdbarch_pc_regnum (gdbarch)))
1731 ctx.iar = tmp_iar;
1732 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1733 ctx.msr = tmp_msr;
1734 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1735 ctx.cr = tmp_cr;
1736 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1737 ctx.lr = tmp_lr;
1738 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1739 ctx.ctr = tmp_ctr;
1740 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1741 ctx.xer = tmp_xer;
1742 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1743 ctx.fpscr = tmp_fpscr;
1746 status = pthdb_pthread_setcontext (data->pd_session, pdtid, &ctx);
1747 if (status != PTHDB_SUCCESS)
1748 error (_("aix-thread: store_registers: "
1749 "pthdb_pthread_setcontext returned %s"),
1750 pd_status2str (status));
1753 /* Store register REGNO if != -1 or all registers otherwise into
1754 kernel thread TID.
1756 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1757 SPRs, but there's no way to set individual registers within those
1758 groups. Therefore, if REGNO != -1, this function stores an entire
1759 group. */
1761 static void
1762 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1763 pthdb_tid_t tid)
1765 struct gdbarch *gdbarch = regcache->arch ();
1766 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1767 uint64_t gprs64[ppc_num_gprs];
1768 uint32_t gprs32[ppc_num_gprs];
1769 double fprs[ppc_num_fprs];
1770 struct ptxsprs sprs64;
1771 struct ptsprs sprs32;
1772 struct aix_thread_variables *data;
1773 int ret = 0;
1775 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1777 if (debug_aix_thread)
1778 gdb_printf (gdb_stdlog,
1779 "store_regs_kernel_thread tid=%lx regno=%d\n",
1780 (long) tid, regno);
1782 /* General-purpose registers. */
1783 if (regno == -1
1784 || (tdep->ppc_gp0_regnum <= regno
1785 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1787 if (data->arch64)
1789 /* Pre-fetch: some regs may not be in the cache. */
1790 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1791 fill_gprs64 (regcache, gprs64);
1792 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1794 else
1796 /* Pre-fetch: some regs may not be in the cache. */
1797 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1798 fill_gprs32 (regcache, gprs32);
1799 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1803 /* Floating-point registers. */
1805 if (ppc_floating_point_unit_p (gdbarch)
1806 && (regno == -1
1807 || (regno >= tdep->ppc_fp0_regnum
1808 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1810 /* Pre-fetch: some regs may not be in the cache. */
1811 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1812 fill_fprs (regcache, fprs);
1813 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1816 /* Special-purpose registers. */
1818 if (regno == -1 || special_register_p (gdbarch, regno))
1820 if (data->arch64)
1822 /* Pre-fetch: some registers won't be in the cache. */
1823 ptrace64aix (PTT_READ_SPRS, tid,
1824 (unsigned long) &sprs64, 0, NULL);
1825 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1826 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1827 &sprs64.pt_xer, &sprs64.pt_fpscr);
1828 ptrace64aix (PTT_WRITE_SPRS, tid,
1829 (unsigned long) &sprs64, 0, NULL);
1831 else
1833 /* The contents of "struct ptspr" were declared as "unsigned
1834 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1835 Use temporaries to work around this problem. Also, add an
1836 assert here to make sure we fail if the system header files
1837 use "unsigned long", and the size of that type is not what
1838 the headers expect. */
1839 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1840 tmp_fpscr;
1842 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1844 /* Pre-fetch: some registers won't be in the cache. */
1845 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1847 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1848 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1850 sprs32.pt_iar = tmp_iar;
1851 sprs32.pt_msr = tmp_msr;
1852 sprs32.pt_cr = tmp_cr;
1853 sprs32.pt_lr = tmp_lr;
1854 sprs32.pt_ctr = tmp_ctr;
1855 sprs32.pt_xer = tmp_xer;
1856 sprs32.pt_fpscr = tmp_fpscr;
1858 if (tdep->ppc_mq_regnum >= 0)
1859 if (REG_VALID == regcache->get_register_status
1860 (tdep->ppc_mq_regnum))
1861 regcache->raw_collect (tdep->ppc_mq_regnum, &sprs32.pt_mq);
1863 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1867 /* Vector registers. */
1868 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1
1869 && (regno == -1 || (regno >= tdep->ppc_vr0_regnum
1870 && regno <= tdep->ppc_vrsave_regnum)))
1872 __vmx_context_t vmx;
1873 if (__power_vmx())
1875 if (data->arch64)
1876 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1877 else
1878 ret = ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1879 if (ret > 0)
1881 fill_altivec(regcache, &vmx);
1882 if (data->arch64)
1883 ret = ptrace64aix (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1884 else
1885 ret = ptrace32 (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1886 if (ret < 0)
1887 perror_with_name (_("Unable to store AltiVec register after read"));
1892 /* VSX registers. */
1893 if (tdep->ppc_vsr0_upper_regnum != -1 && (regno == -1
1894 || (regno >=tdep->ppc_vsr0_upper_regnum
1895 && regno < tdep->ppc_vsr0_upper_regnum + ppc_num_vshrs)))
1897 __vsx_context_t vsx;
1898 if (__power_vsx())
1900 if (data->arch64)
1901 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1902 else
1903 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1904 if (ret > 0)
1906 fill_vsx (regcache, &vsx);
1907 if (data->arch64)
1908 ret = ptrace64aix (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
1909 else
1910 ret = ptrace32 (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
1911 if (ret < 0)
1912 perror_with_name (_("Unable to store VSX register after read"));
1918 /* Store gdb's current view of the register set into the
1919 thread/process connected to REGCACHE. */
1921 void
1922 aix_thread_target::store_registers (struct regcache *regcache, int regno)
1924 struct thread_info *thread;
1925 pthdb_tid_t tid;
1927 if (regcache->ptid ().tid () == 0)
1928 beneath ()->store_registers (regcache, regno);
1929 else
1931 thread = current_inferior ()->find_thread (regcache->ptid ());
1932 aix_thread_info *priv = get_aix_thread_info (thread);
1933 tid = regcache->ptid ().lwp ();
1935 if (tid == PTHDB_INVALID_TID)
1936 store_regs_user_thread (regcache, priv->pdtid);
1937 else
1938 store_regs_kernel_thread (regcache, regno, tid);
1942 /* Implement the to_xfer_partial target_ops method. */
1944 enum target_xfer_status
1945 aix_thread_target::xfer_partial (enum target_object object,
1946 const char *annex, gdb_byte *readbuf,
1947 const gdb_byte *writebuf,
1948 ULONGEST offset, ULONGEST len,
1949 ULONGEST *xfered_len)
1951 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1953 inferior_ptid = ptid_t (inferior_ptid.pid ());
1954 return beneath ()->xfer_partial (object, annex, readbuf,
1955 writebuf, offset, len, xfered_len);
1958 /* Clean up after the inferior exits. */
1960 void
1961 aix_thread_target::mourn_inferior ()
1963 target_ops *beneath = this->beneath ();
1965 pd_disable (current_inferior ());
1966 beneath->mourn_inferior ();
1969 /* Return whether thread PID is still valid. */
1971 bool
1972 aix_thread_target::thread_alive (ptid_t ptid)
1974 if (ptid.tid () == 0)
1975 return beneath ()->thread_alive (ptid);
1977 /* We update the thread list every time the child stops, so all
1978 valid threads should be in the thread list. */
1979 process_stratum_target *proc_target
1980 = current_inferior ()->process_target ();
1981 return in_thread_list (proc_target, ptid);
1984 /* Return a printable representation of composite PID for use in
1985 "info threads" output. */
1987 std::string
1988 aix_thread_target::pid_to_str (ptid_t ptid)
1990 thread_info *thread_info = current_inferior ()->find_thread (ptid);
1992 if (thread_info != NULL && thread_info->priv != NULL)
1994 aix_thread_info *priv = get_aix_thread_info (thread_info);
1996 return string_printf (_("Thread %s (tid %s)"), pulongest (ptid.tid ()),
1997 pulongest (ptid.lwp ()));
2000 return beneath ()->pid_to_str (ptid);
2003 /* Return a printable representation of extra information about
2004 THREAD, for use in "info threads" output. */
2006 const char *
2007 aix_thread_target::extra_thread_info (struct thread_info *thread)
2009 int status;
2010 pthdb_pthread_t pdtid;
2011 pthdb_state_t state;
2012 pthdb_suspendstate_t suspendstate;
2013 pthdb_detachstate_t detachstate;
2014 int cancelpend;
2015 static char *ret = NULL;
2016 struct aix_thread_variables *data;
2018 data = get_thread_data_helper_for_ptid (thread->ptid);
2020 if (thread->ptid.tid () == 0)
2021 return NULL;
2023 string_file buf;
2024 aix_thread_info *priv = get_aix_thread_info (thread);
2026 pdtid = priv->pdtid;
2028 status = pthdb_pthread_state (data->pd_session, pdtid, &state);
2030 /* Output should look like Thread %d (tid %d) ([state]). */
2031 /* Example:- Thread 1 (tid 34144587) ([running]). */
2032 /* where state can be running, idle, sleeping, finished,
2033 suspended, detached, cancel pending, ready or unknown. */
2035 if (status != PTHDB_SUCCESS)
2036 state = PST_NOTSUP;
2037 buf.printf ("[%s]", state2str (state));
2039 status = pthdb_pthread_suspendstate (data->pd_session, pdtid,
2040 &suspendstate);
2041 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
2042 buf.printf (_("[suspended]"));
2044 status = pthdb_pthread_detachstate (data->pd_session, pdtid,
2045 &detachstate);
2046 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
2047 buf.printf (_("[detached]"));
2049 pthdb_pthread_cancelpend (data->pd_session, pdtid, &cancelpend);
2050 if (status == PTHDB_SUCCESS && cancelpend)
2051 buf.printf (_("[cancel pending]"));
2053 buf.write ("", 1);
2055 xfree (ret); /* Free old buffer. */
2057 ret = xstrdup (buf.c_str ());
2059 return ret;
2062 ptid_t
2063 aix_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
2065 return ptid_t (inferior_ptid.pid (), 0, thread);
2069 /* Module startup initialization function, automagically called by
2070 init.c. */
2072 void _initialize_aix_thread ();
2073 void
2074 _initialize_aix_thread ()
2076 /* Notice when object files get loaded and unloaded. */
2077 gdb::observers::new_objfile.attach (new_objfile, "aix-thread");
2079 /* Add ourselves to inferior_created event chain.
2080 This is needed to enable the thread target on "attach". */
2081 gdb::observers::inferior_created.attach (aix_thread_inferior_created,
2082 "aix-thread");
2084 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
2085 _("Set debugging of AIX thread module."),
2086 _("Show debugging of AIX thread module."),
2087 _("Enables debugging output (used to debug GDB)."),
2088 NULL, NULL,
2089 /* FIXME: i18n: Debugging of AIX thread
2090 module is \"%d\". */
2091 &setdebuglist, &showdebuglist);